1 /*
2 * Copyright (C) 2023 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 #include "bluetooth_errorcode.h"
16 #include "bluetooth_map_mse.h"
17 #include "bluetooth_utils.h"
18 #include "napi_async_work.h"
19 #include "napi_bluetooth_event.h"
20 #include "napi_bluetooth_error.h"
21 #include "napi_bluetooth_profile.h"
22 #include "napi_bluetooth_map_mse.h"
23 #include "napi_bluetooth_utils.h"
24 #include "../parser/napi_parser_utils.h"
25 #include "hitrace_meter.h"
26
27 namespace OHOS {
28 namespace Bluetooth {
29 using namespace std;
30
31 std::shared_ptr<NapiMapMseObserver> NapiMapMse::observer_ = std::make_shared<NapiMapMseObserver>();
32 thread_local napi_ref g_consRef_ = nullptr;
33
DefineMapMseJSClass(napi_env env,napi_value exports)34 void NapiMapMse::DefineMapMseJSClass(napi_env env, napi_value exports)
35 {
36 napi_value constructor;
37 napi_property_descriptor properties[] = {
38 DECLARE_NAPI_FUNCTION("on", On),
39 DECLARE_NAPI_FUNCTION("off", Off),
40 DECLARE_NAPI_FUNCTION("getConnectedDevices", GetConnectedDevices),
41 DECLARE_NAPI_FUNCTION("getConnectionState", GetConnectionState),
42 DECLARE_NAPI_FUNCTION("setConnectionStrategy", SetConnectionStrategy),
43 DECLARE_NAPI_FUNCTION("getConnectionStrategy", GetConnectionStrategy),
44 DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
45 DECLARE_NAPI_FUNCTION("setMessageAccessAuthorization", SetMessageAccessAuthorization),
46 DECLARE_NAPI_FUNCTION("getMessageAccessAuthorization", GetMessageAccessAuthorization),
47 };
48
49 napi_define_class(env, "MapMse", NAPI_AUTO_LENGTH, MapMseConstructor, nullptr,
50 sizeof(properties) / sizeof(properties[0]), properties, &constructor);
51
52 DefineCreateProfile(env, exports);
53 napi_create_reference(env, constructor, 1, &g_consRef_);
54 }
55
DefineCreateProfile(napi_env env,napi_value exports)56 napi_value NapiMapMse::DefineCreateProfile(napi_env env, napi_value exports)
57 {
58 napi_property_descriptor properties[] = {
59 DECLARE_NAPI_FUNCTION("createMapMseProfile", CreateMapMseProfile),
60 };
61 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "mapmse:napi_define_properties");
62 napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties);
63 return exports;
64 }
65
CreateMapMseProfile(napi_env env,napi_callback_info info)66 napi_value NapiMapMse::CreateMapMseProfile(napi_env env, napi_callback_info info)
67 {
68 napi_value napiProfile;
69 napi_value constructor = nullptr;
70 napi_get_reference_value(env, g_consRef_, &constructor);
71 napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
72
73 MapMse *profile = MapMse::GetProfile();
74 profile->RegisterObserver(observer_);
75 return napiProfile;
76 }
77
MapMseConstructor(napi_env env,napi_callback_info info)78 napi_value NapiMapMse::MapMseConstructor(napi_env env, napi_callback_info info)
79 {
80 napi_value thisVar = nullptr;
81 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
82 return thisVar;
83 }
84
On(napi_env env,napi_callback_info info)85 napi_value NapiMapMse::On(napi_env env, napi_callback_info info)
86 {
87 if (observer_) {
88 auto status = observer_->eventSubscribe_.Register(env, info);
89 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
90 }
91 return NapiGetUndefinedRet(env);
92 }
93
Off(napi_env env,napi_callback_info info)94 napi_value NapiMapMse::Off(napi_env env, napi_callback_info info)
95 {
96 if (observer_) {
97 auto status = observer_->eventSubscribe_.Deregister(env, info);
98 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
99 }
100 return NapiGetUndefinedRet(env);
101 }
102
GetConnectedDevices(napi_env env,napi_callback_info info)103 napi_value NapiMapMse::GetConnectedDevices(napi_env env, napi_callback_info info)
104 {
105 HILOGI("enter");
106 napi_value ret = nullptr;
107 napi_create_array(env, &ret);
108 napi_status checkRet = CheckEmptyParam(env, info);
109 NAPI_BT_ASSERT_RETURN(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM, ret);
110
111 MapMse *profile = MapMse::GetProfile();
112 vector<int32_t> states = { static_cast<int32_t>(BTConnectState::CONNECTED) };
113 vector<BluetoothRemoteDevice> devices {};
114 int32_t errorCode = profile->GetDevicesByStates(states, devices);
115 NAPI_BT_ASSERT_RETURN(env, errorCode == BT_NO_ERROR, errorCode, ret);
116
117 vector<string> deviceVector;
118 for (auto &device : devices) {
119 deviceVector.push_back(device.GetDeviceAddr());
120 }
121
122 auto status = ConvertStringVectorToJS(env, ret, deviceVector);
123 NAPI_BT_ASSERT_RETURN(env, status == napi_ok, BT_ERR_INTERNAL_ERROR, ret);
124 return ret;
125 }
126
GetConnectionState(napi_env env,napi_callback_info info)127 napi_value NapiMapMse::GetConnectionState(napi_env env, napi_callback_info info)
128 {
129 HILOGI("enter");
130 std::string remoteAddr {};
131 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
132 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
133
134 MapMse *profile = MapMse::GetProfile();
135 BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
136 int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
137 int32_t errorCode = profile->GetDeviceState(device, state);
138 HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
139 NAPI_BT_ASSERT_RETURN_UNDEF(env, errorCode == BT_NO_ERROR, errorCode);
140
141 napi_value result = nullptr;
142 int32_t profileState = GetProfileConnectionState(state);
143 napi_create_int32(env, profileState, &result);
144 return result;
145 }
146
Disconnect(napi_env env,napi_callback_info info)147 napi_value NapiMapMse::Disconnect(napi_env env, napi_callback_info info)
148 {
149 HILOGD("enter");
150 std::string remoteAddr{};
151 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
152 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
153
154 MapMse *profile = MapMse::GetProfile();
155 BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
156 int32_t errorCode = profile->Disconnect(device);
157 HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
158 NAPI_BT_ASSERT_RETURN_FALSE(env, errorCode == BT_NO_ERROR, errorCode);
159 return NapiGetBooleanTrue(env);
160 }
161
SetConnectionStrategy(napi_env env,napi_callback_info info)162 napi_value NapiMapMse::SetConnectionStrategy(napi_env env, napi_callback_info info)
163 {
164 HILOGD("enter");
165 std::string remoteAddr{};
166 int32_t strategy = 0;
167 auto status = CheckSetConnectStrategyParam(env, info, remoteAddr, strategy);
168 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
169
170 auto func = [remoteAddr, strategy]() {
171 BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR);
172 MapMse *profile = MapMse::GetProfile();
173 int32_t errorCode = profile->SetConnectionStrategy(remoteDevice, strategy);
174 HILOGI("err: %{public}d", errorCode);
175 return NapiAsyncWorkRet(errorCode);
176 };
177 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
178 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
179 asyncWork->Run();
180 return asyncWork->GetRet();
181 }
182
GetConnectionStrategy(napi_env env,napi_callback_info info)183 napi_value NapiMapMse::GetConnectionStrategy(napi_env env, napi_callback_info info)
184 {
185 HILOGD("enter");
186 std::string remoteAddr{};
187 auto status = CheckDeviceAddressParam(env, info, remoteAddr);
188 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
189
190 auto func = [remoteAddr]() {
191 int32_t strategy = 0;
192 BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR);
193 MapMse *profile = MapMse::GetProfile();
194 int32_t errorCode = profile->GetConnectionStrategy(remoteDevice, strategy);
195 HILOGI("errorCode: %{public}d, deviceName: %{public}d", errorCode, strategy);
196 auto object = std::make_shared<NapiNativeInt>(strategy);
197 return NapiAsyncWorkRet(errorCode, object);
198 };
199 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
200 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
201 asyncWork->Run();
202 return asyncWork->GetRet();
203 }
204
SetMessageAccessAuthorization(napi_env env,napi_callback_info info)205 napi_value NapiMapMse::SetMessageAccessAuthorization(napi_env env, napi_callback_info info)
206 {
207 std::string remoteAddr{};
208 int32_t accessAuthorization = 0;
209 auto status = CheckAccessAuthorizationParam(env, info, remoteAddr, accessAuthorization);
210 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
211
212 auto func = [remoteAddr, accessAuthorization]() {
213 BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR);
214 MapMse *profile = MapMse::GetProfile();
215 int32_t errorCode = profile->SetMessageAccessAuthorization(remoteDevice, accessAuthorization);
216 HILOGI("errorCode: %{public}d", errorCode);
217 return NapiAsyncWorkRet(errorCode);
218 };
219 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
220 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
221 asyncWork->Run();
222 return asyncWork->GetRet();
223 }
224
GetMessageAccessAuthorization(napi_env env,napi_callback_info info)225 napi_value NapiMapMse::GetMessageAccessAuthorization(napi_env env, napi_callback_info info)
226 {
227 std::string remoteAddr{};
228 auto status = CheckDeviceAddressParam(env, info, remoteAddr);
229 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
230
231 auto func = [remoteAddr]() {
232 int32_t accessAuthorization = 0;
233 BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR);
234 MapMse *profile = MapMse::GetProfile();
235 int32_t errorCode = profile->GetMessageAccessAuthorization(remoteDevice, accessAuthorization);
236 HILOGI("errorCode: %{public}d, accessAuthorization: %{public}d", errorCode, accessAuthorization);
237 auto object = std::make_shared<NapiNativeInt>(accessAuthorization);
238 return NapiAsyncWorkRet(errorCode, object);
239 };
240 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
241 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
242 asyncWork->Run();
243 return asyncWork->GetRet();
244 }
245
246 } // namespace Bluetooth
247 } // namespace OHOS