• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 
16 #include "bluetooth_hid_host.h"
17 
18 #include "bluetooth_errorcode.h"
19 #include "bluetooth_log.h"
20 #include "bluetooth_utils.h"
21 #include "napi_bluetooth_hid_host_observer.h"
22 #include "napi_bluetooth_error.h"
23 #include "napi_bluetooth_utils.h"
24 #include "napi_bluetooth_profile.h"
25 #include "napi_bluetooth_hid_host.h"
26 
27 namespace OHOS {
28 namespace Bluetooth {
29 using namespace std;
30 NapiBluetoothHidHostObserver NapiBluetoothHidHost::observer_;
31 
DefineHidHostJSClass(napi_env env)32 void NapiBluetoothHidHost::DefineHidHostJSClass(napi_env env)
33 {
34     napi_value constructor;
35     napi_property_descriptor properties[] = {
36         DECLARE_NAPI_FUNCTION("on", On),
37         DECLARE_NAPI_FUNCTION("off", Off),
38         DECLARE_NAPI_FUNCTION("getConnectionDevices", GetConnectionDevices),
39         DECLARE_NAPI_FUNCTION("getDeviceState", GetDeviceState),
40         DECLARE_NAPI_FUNCTION("connect", Connect),
41         DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
42     };
43 
44     napi_define_class(env, "NapiBluetoothHidHost", NAPI_AUTO_LENGTH, HidHostConstructor, nullptr,
45         sizeof(properties) / sizeof(properties[0]), properties, &constructor);
46 
47     napi_value napiProfile;
48     napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
49     NapiProfile::SetProfile(env, ProfileId::PROFILE_HID_HOST, napiProfile);
50     HidHost *profile = HidHost::GetProfile();
51     profile->RegisterObserver(&observer_);
52     HILOGI("finished");
53 }
54 
HidHostConstructor(napi_env env,napi_callback_info info)55 napi_value NapiBluetoothHidHost::HidHostConstructor(napi_env env, napi_callback_info info)
56 {
57     napi_value thisVar = nullptr;
58     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
59     return thisVar;
60 }
61 
On(napi_env env,napi_callback_info info)62 napi_value NapiBluetoothHidHost::On(napi_env env, napi_callback_info info)
63 {
64     HILOGI("enter");
65     size_t expectedArgsCount = ARGS_SIZE_TWO;
66     size_t argc = expectedArgsCount;
67     napi_value argv[ARGS_SIZE_TWO] = {0};
68     napi_value thisVar = nullptr;
69 
70     napi_value ret = nullptr;
71     napi_get_undefined(env, &ret);
72 
73     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
74     if (argc != expectedArgsCount) {
75         HILOGE("Requires 2 argument.");
76         return ret;
77     }
78     string type;
79     if (!ParseString(env, type, argv[PARAM0])) {
80         HILOGE("string expected.");
81         return ret;
82     }
83     std::shared_ptr<BluetoothCallbackInfo> callbackInfo = std::make_shared<BluetoothCallbackInfo>();
84     callbackInfo->env_ = env;
85 
86     napi_valuetype valueType = napi_undefined;
87     napi_typeof(env, argv[PARAM1], &valueType);
88     if (valueType != napi_function) {
89         HILOGE("Wrong argument type. Function expected.");
90         return ret;
91     }
92     napi_create_reference(env, argv[PARAM1], 1, &callbackInfo->callback_);
93     observer_.callbackInfos_[type] = callbackInfo;
94     HILOGI("%{public}s is registered", type.c_str());
95     return ret;
96 }
97 
Off(napi_env env,napi_callback_info info)98 napi_value NapiBluetoothHidHost::Off(napi_env env, napi_callback_info info)
99 {
100     HILOGI("enter");
101     size_t expectedArgsCount = ARGS_SIZE_ONE;
102     size_t argc = expectedArgsCount;
103     napi_value argv[ARGS_SIZE_ONE] = {0};
104     napi_value thisVar = nullptr;
105 
106     napi_value ret = nullptr;
107     napi_get_undefined(env, &ret);
108 
109     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
110     if (argc != expectedArgsCount) {
111         HILOGE("Requires 1 argument.");
112         return ret;
113     }
114     string type;
115     if (!ParseString(env, type, argv[PARAM0])) {
116         HILOGE("string expected.");
117         return ret;
118     }
119 
120     if (observer_.callbackInfos_[type] != nullptr) {
121     std::shared_ptr<BluetoothCallbackInfo> callbackInfo = observer_.callbackInfos_[type];
122     napi_delete_reference(env, callbackInfo->callback_);
123     }
124     observer_.callbackInfos_[type] = nullptr;
125     HILOGI("%{public}s is unregistered", type.c_str());
126     return ret;
127 }
128 
GetConnectionDevices(napi_env env,napi_callback_info info)129 napi_value NapiBluetoothHidHost::GetConnectionDevices(napi_env env, napi_callback_info info)
130 {
131     HILOGI("enter");
132 
133     napi_value ret = nullptr;
134     if (napi_create_array(env, &ret) != napi_ok) {
135         HILOGE("napi_create_array failed.");
136     }
137     napi_status checkRet = CheckEmptyParam(env, info);
138     NAPI_BT_ASSERT_RETURN(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM, ret);
139 
140     HidHost *profile = HidHost::GetProfile();
141     vector<int> states = { static_cast<int>(BTConnectState::CONNECTED) };
142     vector<BluetoothRemoteDevice> devices;
143     int errorCode = profile->GetDevicesByStates(states, devices);
144     HILOGI("errorCode:%{public}s, devices size:%{public}zu", GetErrorCode(errorCode).c_str(), devices.size());
145     NAPI_BT_ASSERT_RETURN(env, errorCode == BT_SUCCESS, errorCode, ret);
146 
147     vector<string> deviceVector;
148     for (auto &device: devices) {
149         deviceVector.push_back(device.GetDeviceAddr());
150     }
151     ConvertStringVectorToJS(env, ret, deviceVector);
152     return ret;
153 }
154 
GetDeviceState(napi_env env,napi_callback_info info)155 napi_value NapiBluetoothHidHost::GetDeviceState(napi_env env, napi_callback_info info)
156 {
157     HILOGI("enter");
158     napi_value result = nullptr;
159     int32_t profileState = ProfileConnectionState::STATE_DISCONNECTED;
160     if (napi_create_int32(env, profileState, &result) != napi_ok) {
161         HILOGE("napi_create_int32 failed.");
162     }
163 
164     std::string remoteAddr {};
165     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
166     NAPI_BT_ASSERT_RETURN(env, checkRet, BT_ERR_INVALID_PARAM, result);
167 
168     HidHost *profile = HidHost::GetProfile();
169     BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
170     int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
171     int32_t errorCode = profile->GetDeviceState(device, state);
172     HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
173     NAPI_BT_ASSERT_RETURN(env, errorCode == BT_SUCCESS, errorCode, result);
174 
175     profileState = GetProfileConnectionState(state);
176     if (napi_create_int32(env, profileState, &result) != napi_ok) {
177         HILOGE("napi_create_int32 failed.");
178     }
179     HILOGI("profileState: %{public}d", profileState);
180     return result;
181 }
182 
Connect(napi_env env,napi_callback_info info)183 napi_value NapiBluetoothHidHost::Connect(napi_env env, napi_callback_info info)
184 {
185     HILOGI("enter");
186     std::string remoteAddr {};
187     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
188     NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
189 
190     HidHost *profile = HidHost::GetProfile();
191     BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
192     int32_t errorCode = profile->Connect(device);
193     HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
194     NAPI_BT_ASSERT_RETURN_FALSE(env, errorCode == BT_SUCCESS, errorCode);
195     return NapiGetBooleanTrue(env);
196 }
197 
Disconnect(napi_env env,napi_callback_info info)198 napi_value NapiBluetoothHidHost::Disconnect(napi_env env, napi_callback_info info)
199 {
200     HILOGI("Disconnect called");
201     std::string remoteAddr {};
202     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
203     NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
204 
205     HidHost *profile = HidHost::GetProfile();
206     BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
207     int32_t errorCode = profile->Disconnect(device);
208     HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
209     NAPI_BT_ASSERT_RETURN_FALSE(env, errorCode == BT_SUCCESS, errorCode);
210     return NapiGetBooleanTrue(env);
211 }
212 }  // namespace Bluetooth
213 }  // namespace OHOS