1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef LOG_TAG
16 #define LOG_TAG "bt_cj_hid_host"
17 #endif
18
19 #include "bluetooth_hid_host_impl.h"
20
21 #include <vector>
22
23 #include "bluetooth_errorcode.h"
24 #include "bluetooth_hid_host_utils.h"
25 #include "bluetooth_utils.h"
26 #include "cj_lambda.h"
27 #include "napi_bluetooth_utils.h"
28
29 using namespace OHOS::FFI;
30
31 namespace OHOS {
32 namespace Bluetooth {
33 std::shared_ptr<BluetoothHidHostObserverImpl> BluetoothHidHostImpl::observer_ =
34 std::make_shared<BluetoothHidHostObserverImpl>();
35 bool BluetoothHidHostImpl::isRegistered_ = false;
36
GetConnectionDevices(int32_t * errCode)37 CArrString BluetoothHidHostImpl::GetConnectionDevices(int32_t* errCode)
38 {
39 HILOGD("enter");
40 HidHost* profile = HidHost::GetProfile();
41 if (profile == nullptr) {
42 HILOGE("profile is nullptr.");
43 *errCode = BT_ERR_INTERNAL_ERROR;
44 return CArrString { 0 };
45 }
46 std::vector<int> states = { static_cast<int>(BTConnectState::CONNECTED) };
47 std::vector<BluetoothRemoteDevice> devices;
48 int errorCode = profile->GetDevicesByStates(states, devices);
49 if (errorCode != BT_NO_ERROR) {
50 HILOGE("bluetooth assert failed.");
51 *errCode = static_cast<int32_t>(errorCode);
52 return CArrString { 0 };
53 }
54 if (devices.size() <= 0) {
55 return CArrString { 0 };
56 }
57 char** deviceVector = static_cast<char**>(malloc(sizeof(char*) * devices.size()));
58 if (deviceVector == nullptr) {
59 HILOGE("deviceVector malloc failed.");
60 *errCode = BT_ERR_INTERNAL_ERROR;
61 return CArrString { 0 };
62 }
63 int64_t i = 0;
64 for (auto& device : devices) {
65 deviceVector[i] = MallocCString(device.GetDeviceAddr());
66 i++;
67 }
68 CArrString ret = CArrString { .head = deviceVector, .size = i };
69 return ret;
70 }
71
GetConnectionState(char * device,int32_t * errCode)72 int32_t BluetoothHidHostImpl::GetConnectionState(char* device, int32_t* errCode)
73 {
74 HILOGD("enter");
75 int32_t profileState = ProfileConnectionState::STATE_DISCONNECTED;
76 std::string remoteAddr = device;
77 if (!IsValidAddress(remoteAddr)) {
78 HILOGE("Invalid addr");
79 *errCode = static_cast<int32_t>(BT_ERR_INVALID_PARAM);
80 return profileState;
81 }
82 HidHost* profile = HidHost::GetProfile();
83 if (profile == nullptr) {
84 HILOGE("profile is nullptr.");
85 *errCode = BT_ERR_INTERNAL_ERROR;
86 return profileState;
87 }
88 BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR);
89 int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
90 int32_t errorCode = profile->GetDeviceState(remoteDevice, state);
91 if (errorCode != BT_NO_ERROR) {
92 HILOGE("bluetooth assert failed.");
93 *errCode = errorCode;
94 return profileState;
95 }
96 profileState = GetProfileConnectionState(state);
97 return profileState;
98 }
99
On(int32_t type,int64_t id,int32_t * errCode)100 void BluetoothHidHostImpl::On(int32_t type, int64_t id, int32_t* errCode)
101 {
102 if (observer_) {
103 auto observerFunc = CJLambda::Create(reinterpret_cast<void (*)(StateChangeParam)>(id));
104 if (!observerFunc) {
105 HILOGD("Register state change event failed");
106 *errCode = BT_ERR_INVALID_PARAM;
107 return;
108 }
109 observer_->RegisterStateChangeFunc(observerFunc);
110 }
111
112 if (!isRegistered_) {
113 HidHost* profile = HidHost::GetProfile();
114 if (profile == nullptr) {
115 HILOGE("profile is nullptr.");
116 *errCode = BT_ERR_INVALID_PARAM;
117 return;
118 }
119 profile->RegisterObserver(observer_);
120 isRegistered_ = true;
121 }
122 return;
123 }
124 } // namespace Bluetooth
125 } // namespace OHOS