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_errorcode.h"
17 #include "bluetooth_log.h"
18 #include "bluetooth_pan.h"
19 #include "bluetooth_utils.h"
20 #include "napi_bluetooth_error.h"
21 #include "napi_bluetooth_pan.h"
22 #include "napi_bluetooth_pan_observer.h"
23 #include "napi_bluetooth_profile.h"
24 #include "napi_bluetooth_utils.h"
25
26 namespace OHOS {
27 namespace Bluetooth {
28 using namespace std;
29 NapiBluetoothPanObserver NapiBluetoothPan::observer_;
30
DefinePanJSClass(napi_env env)31 void NapiBluetoothPan::DefinePanJSClass(napi_env env)
32 {
33 napi_value constructor;
34 napi_property_descriptor properties [] = {
35 DECLARE_NAPI_FUNCTION("on", NapiBluetoothPan::On),
36 DECLARE_NAPI_FUNCTION("off", NapiBluetoothPan::Off),
37 DECLARE_NAPI_FUNCTION("getConnectionDevices", NapiBluetoothPan::GetConnectionDevices),
38 DECLARE_NAPI_FUNCTION("getDeviceState", NapiBluetoothPan::GetDeviceState),
39 DECLARE_NAPI_FUNCTION("disconnect", NapiBluetoothPan::Disconnect),
40 DECLARE_NAPI_FUNCTION("setTethering", NapiBluetoothPan::SetTethering),
41 DECLARE_NAPI_FUNCTION("isTetheringOn", NapiBluetoothPan::IsTetheringOn),
42 };
43
44 napi_define_class(env, "NapiBluetoothPan", NAPI_AUTO_LENGTH, NapiBluetoothPan::PanConstructor, nullptr,
45 sizeof(properties) / sizeof(properties[0]), properties, &constructor);
46 napi_value napiProfile;
47 napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
48 NapiProfile::SetProfile(env, ProfileId::PROFILE_PAN_NETWORK, napiProfile);
49 Pan *profile = Pan::GetProfile();
50 profile->RegisterObserver(&NapiBluetoothPan::observer_);
51 HILOGI("finished");
52 }
53
PanConstructor(napi_env env,napi_callback_info info)54 napi_value NapiBluetoothPan::PanConstructor(napi_env env, napi_callback_info info)
55 {
56 napi_value thisVar = nullptr;
57 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
58 return thisVar;
59 }
60
On(napi_env env,napi_callback_info info)61 napi_value NapiBluetoothPan::On(napi_env env, napi_callback_info info)
62 {
63 HILOGI("enter");
64 size_t expectedArgsCount = ARGS_SIZE_TWO;
65 size_t argc = expectedArgsCount;
66 napi_value argv[ARGS_SIZE_TWO] = {0};
67 napi_value thisVar = nullptr;
68
69 napi_value ret = nullptr;
70 napi_get_undefined(env, &ret);
71
72 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
73 if (argc != expectedArgsCount) {
74 HILOGE("Requires 2 argument.");
75 return ret;
76 }
77 string type;
78 if (!ParseString(env, type, argv[PARAM0])) {
79 HILOGE("string expected.");
80 return ret;
81 }
82 std::shared_ptr<BluetoothCallbackInfo> callbackInfo = std::make_shared<BluetoothCallbackInfo>();
83 callbackInfo->env_ = env;
84
85 napi_valuetype valueType = napi_undefined;
86 napi_typeof(env, argv[PARAM1], &valueType);
87 if (valueType != napi_function) {
88 HILOGE("Wrong argument type. Function expected.");
89 return ret;
90 }
91 napi_create_reference(env, argv[PARAM1], 1, &callbackInfo->callback_);
92 observer_.callbackInfos_[type] = callbackInfo;
93
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 NapiBluetoothPan::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 if (observer_.callbackInfos_[type] != nullptr) {
120 std::shared_ptr<BluetoothCallbackInfo> callbackInfo = observer_.callbackInfos_[type];
121 napi_delete_reference(env, callbackInfo->callback_);
122 }
123 observer_.callbackInfos_[type] = nullptr;
124
125 HILOGI("%{public}s is unregistered", type.c_str());
126
127 return ret;
128 }
129
GetConnectionDevices(napi_env env,napi_callback_info info)130 napi_value NapiBluetoothPan::GetConnectionDevices(napi_env env, napi_callback_info info)
131 {
132 HILOGI("enter");
133 napi_value ret = nullptr;
134 if (napi_create_array(env, &ret) != napi_ok) {
135 HILOGE("napi_create_array failed.");
136 }
137
138 napi_status checkRet = CheckEmptyParam(env, info);
139 NAPI_BT_ASSERT_RETURN(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM, ret);
140
141 Pan *profile = Pan::GetProfile();
142 vector<int> states = { static_cast<int>(BTConnectState::CONNECTED) };
143 vector<BluetoothRemoteDevice> devices;
144 int errorCode = profile->GetDevicesByStates(states, devices);
145 HILOGI("errorCode:%{public}s, devices size:%{public}zu", GetErrorCode(errorCode).c_str(), devices.size());
146 NAPI_BT_ASSERT_RETURN(env, errorCode == BT_SUCCESS, errorCode, ret);
147
148 vector<string> deviceVector;
149 for (auto &device : devices) {
150 deviceVector.push_back(device.GetDeviceAddr());
151 }
152 ConvertStringVectorToJS(env, ret, deviceVector);
153 return ret;
154 }
155
GetDeviceState(napi_env env,napi_callback_info info)156 napi_value NapiBluetoothPan::GetDeviceState(napi_env env, napi_callback_info info)
157 {
158 HILOGI("enter");
159 napi_value result = nullptr;
160 int32_t profileState = ProfileConnectionState::STATE_DISCONNECTED;
161 if (napi_create_int32(env, profileState, &result) != napi_ok) {
162 HILOGE("napi_create_int32 failed.");
163 }
164
165 std::string remoteAddr {};
166 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
167 NAPI_BT_ASSERT_RETURN(env, checkRet, BT_ERR_INVALID_PARAM, result);
168
169 Pan *profile = Pan::GetProfile();
170 BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
171 int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
172 int32_t errorCode = profile->GetDeviceState(device, state);
173 HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
174 NAPI_BT_ASSERT_RETURN(env, errorCode == BT_SUCCESS, errorCode, result);
175
176 profileState = GetProfileConnectionState(state);
177 if (napi_create_int32(env, profileState, &result) != napi_ok) {
178 HILOGE("napi_create_int32 failed.");
179 }
180 HILOGI("profileState: %{public}d", profileState);
181 return result;
182 }
183
Disconnect(napi_env env,napi_callback_info info)184 napi_value NapiBluetoothPan::Disconnect(napi_env env, napi_callback_info info)
185 {
186 HILOGI("enter");
187 std::string remoteAddr {};
188 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
189 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
190
191 Pan *profile = Pan::GetProfile();
192 BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
193 int32_t errorCode = profile->Disconnect(device);
194 HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
195 NAPI_BT_ASSERT_RETURN_FALSE(env, errorCode == BT_SUCCESS, errorCode);
196
197 return NapiGetBooleanTrue(env);
198 }
199
CheckSetTetheringParam(napi_env env,napi_callback_info info,bool & out)200 static bool CheckSetTetheringParam(napi_env env, napi_callback_info info, bool &out)
201 {
202 size_t argc = ARGS_SIZE_ONE;
203 napi_value argv[ARGS_SIZE_ONE] = {nullptr};
204 NAPI_BT_RETURN_IF(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok, "call failed.", false);
205 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_ONE, "Requires 1 argument.", false);
206 NAPI_BT_RETURN_IF(!ParseBool(env, out, argv[PARAM0]), "Bool expected.", false);
207 return true;
208 }
209
SetTethering(napi_env env,napi_callback_info info)210 napi_value NapiBluetoothPan::SetTethering(napi_env env, napi_callback_info info)
211 {
212 HILOGI("enter");
213 bool value = false;
214 bool checkRet = CheckSetTetheringParam(env, info, value);
215 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
216
217 Pan *profile = Pan::GetProfile();
218 int32_t errorCode = profile->SetTethering(value);
219 HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
220 NAPI_BT_ASSERT_RETURN_UNDEF(env, errorCode == BT_SUCCESS, errorCode);
221
222 return NapiGetUndefinedRet(env);
223 }
224
IsTetheringOn(napi_env env,napi_callback_info info)225 napi_value NapiBluetoothPan::IsTetheringOn(napi_env env, napi_callback_info info)
226 {
227 HILOGI("enter");
228 napi_status checkRet = CheckEmptyParam(env, info);
229 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
230
231 Pan *profile = Pan::GetProfile();
232 bool result = false;
233 int32_t errorCode = profile->IsTetheringOn(result);
234 HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
235 NAPI_BT_ASSERT_RETURN_FALSE(env, errorCode == BT_SUCCESS, errorCode);
236
237 HILOGI("IsTetheringOn: %{public}d", result);
238 return NapiGetBooleanRet(env, result);
239 }
240 } // namespace Bluetooth
241 } // namespace OHOS