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 
16 #include "bluetooth_errorcode.h"
17 #include "bluetooth_opp.h"
18 #include "bluetooth_utils.h"
19 #include "napi_async_work.h"
20 #include "napi_bluetooth_event.h"
21 #include "napi_bluetooth_error.h"
22 #include "napi_bluetooth_opp.h"
23 #include "napi_bluetooth_profile.h"
24 #include "napi_bluetooth_utils.h"
25 #include "../parser/napi_parser_utils.h"
26 #include "hitrace_meter.h"
27 
28 namespace OHOS {
29 namespace Bluetooth {
30 using namespace std;
31 
32 std::shared_ptr<NapiBluetoothOppObserver> NapiBluetoothOpp::observer_ = std::make_shared<NapiBluetoothOppObserver>();
33 thread_local napi_ref g_consRef_ = nullptr;
34 
DefineOppJSClass(napi_env env, napi_value exports)35 void NapiBluetoothOpp::DefineOppJSClass(napi_env env, napi_value exports)
36 {
37     napi_value constructor;
38     napi_property_descriptor properties[] = {
39         DECLARE_NAPI_FUNCTION("on", On),
40         DECLARE_NAPI_FUNCTION("off", Off),
41         DECLARE_NAPI_FUNCTION("onEvent", On),
42         DECLARE_NAPI_FUNCTION("offEvent", Off),
43         DECLARE_NAPI_FUNCTION("sendFile", SendFile),
44         DECLARE_NAPI_FUNCTION("setIncomingFileConfirmation", SetIncomingFileConfirmation),
45         DECLARE_NAPI_FUNCTION("getCurrentTransferInformation", GetCurrentTransferInformation),
46         DECLARE_NAPI_FUNCTION("cancelTransfer", CancelTransfer),
47         DECLARE_NAPI_FUNCTION("getConnectionDevices", GetConnectionDevices),
48         DECLARE_NAPI_FUNCTION("getDeviceState", GetDeviceState),
49     };
50 
51     napi_define_class(env, "NapiBluetoothOpp", NAPI_AUTO_LENGTH, OppConstructor, nullptr,
52         sizeof(properties) / sizeof(properties[0]), properties, &constructor);
53 
54     DefineCreateProfile(env, exports);
55     napi_create_reference(env, constructor, 1, &g_consRef_);
56 }
57 
DefineCreateProfile(napi_env env, napi_value exports)58 napi_value NapiBluetoothOpp::DefineCreateProfile(napi_env env, napi_value exports)
59 {
60     napi_property_descriptor properties[] = {
61         DECLARE_NAPI_FUNCTION("createOppServerProfile", CreateOppServerProfile),
62     };
63     HITRACE_METER_NAME(HITRACE_TAG_OHOS, "opp:napi_define_properties");
64     napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties);
65     return exports;
66 }
67 
CreateOppServerProfile(napi_env env, napi_callback_info info)68 napi_value NapiBluetoothOpp::CreateOppServerProfile(napi_env env, napi_callback_info info)
69 {
70     napi_value napiProfile;
71     napi_value constructor = nullptr;
72     napi_get_reference_value(env, g_consRef_, &constructor);
73     napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
74 
75     Opp *profile = Opp::GetProfile();
76     profile->RegisterObserver(observer_);
77     return napiProfile;
78 }
79 
OppConstructor(napi_env env, napi_callback_info info)80 napi_value NapiBluetoothOpp::OppConstructor(napi_env env, napi_callback_info info)
81 {
82     napi_value thisVar = nullptr;
83     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
84     return thisVar;
85 }
86 
On(napi_env env, napi_callback_info info)87 napi_value NapiBluetoothOpp::On(napi_env env, napi_callback_info info)
88 {
89     if (observer_) {
90         auto status = observer_->eventSubscribe_.Register(env, info);
91         NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
92     }
93     return NapiGetUndefinedRet(env);
94 }
95 
Off(napi_env env, napi_callback_info info)96 napi_value NapiBluetoothOpp::Off(napi_env env, napi_callback_info info)
97 {
98     if (observer_) {
99         auto status = observer_->eventSubscribe_.Deregister(env, info);
100         NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
101     }
102     return NapiGetUndefinedRet(env);
103 }
104 
CheckSetIncomingFileConfirmation(napi_env env, napi_callback_info info, bool &accept)105 napi_status CheckSetIncomingFileConfirmation(napi_env env, napi_callback_info info, bool &accept)
106 {
107     size_t argc = ARGS_SIZE_ONE;
108     napi_value argv[ARGS_SIZE_ONE] = {nullptr};
109     NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
110     NAPI_BT_RETURN_IF(argc != ARGS_SIZE_ONE, "Require 1 arguments.", napi_invalid_arg);
111     NAPI_BT_CALL_RETURN(NapiParseBoolean(env, argv[PARAM0], accept));
112     return napi_ok;
113 }
114 
CheckSendFileParam(napi_env env, napi_callback_info info, std::string &addr, std::vector<std::string> &filePaths, std::vector<std::string> &mimeTypes)115 napi_status CheckSendFileParam(napi_env env, napi_callback_info info, std::string &addr,
116     std::vector<std::string> &filePaths, std::vector<std::string> &mimeTypes)
117 {
118     size_t argc = ARGS_SIZE_THREE;
119     napi_value argv[ARGS_SIZE_THREE] = {nullptr};
120     NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
121     NAPI_BT_RETURN_IF(argc != ARGS_SIZE_THREE, "Require 3 arguments.", napi_invalid_arg);
122     NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], addr));
123     NAPI_BT_CALL_RETURN(NapiParseStringArray(env, argv[PARAM1], filePaths));
124     NAPI_BT_CALL_RETURN(NapiParseStringArray(env, argv[PARAM2], mimeTypes));
125     return napi_ok;
126 }
127 
SendFile(napi_env env, napi_callback_info info)128 napi_value NapiBluetoothOpp::SendFile(napi_env env, napi_callback_info info)
129 {
130     HILOGI("enter");
131 
132     std::string device {};
133     std::vector<std::string> filePaths;
134     std::vector<std::string> mimeTypes;
135 
136     auto status = CheckSendFileParam(env, info, device, filePaths, mimeTypes);
137     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
138 
139     auto func = [device, filePaths, mimeTypes]() {
140         Opp *profile = Opp::GetProfile();
141         bool result = false;
142         int32_t errorCode = profile->SendFile(device, filePaths, mimeTypes, result);
143         HILOGI("err: %{public}d result: %{public}d", errorCode, result);
144         return NapiAsyncWorkRet(errorCode);
145     };
146     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
147     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
148     asyncWork->Run();
149     return asyncWork->GetRet();
150 }
151 
SetIncomingFileConfirmation(napi_env env, napi_callback_info info)152 napi_value NapiBluetoothOpp::SetIncomingFileConfirmation(napi_env env, napi_callback_info info)
153 {
154     HILOGI("enter");
155 
156     bool accept = false;
157     auto status = CheckSetIncomingFileConfirmation(env, info, accept);
158     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
159 
160     auto func = [accept]() {
161         Opp *profile = Opp::GetProfile();
162         int32_t errorCode = profile->SetIncomingFileConfirmation(accept);
163         HILOGI("err: %{public}d", errorCode);
164         return NapiAsyncWorkRet(errorCode);
165     };
166     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
167     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
168     asyncWork->Run();
169     return asyncWork->GetRet();
170 }
171 
GetCurrentTransferInformation(napi_env env, napi_callback_info info)172 napi_value NapiBluetoothOpp::GetCurrentTransferInformation(napi_env env, napi_callback_info info)
173 {
174     HILOGI("enter");
175 
176     napi_value ret = nullptr;
177     napi_create_object(env, &ret);
178     napi_status checkRet = CheckEmptyParam(env, info);
179     NAPI_BT_ASSERT_RETURN(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM, ret);
180 
181     Opp *profile = Opp::GetProfile();
182     BluetoothOppTransferInformation information;
183     int32_t errorCode = profile->GetCurrentTransferInformation(information);
184     HILOGI("GetCurrentTransferInformation errorCode is %{public}d", errorCode);
185     NAPI_BT_ASSERT_RETURN_UNDEF(env, errorCode == BT_NO_ERROR, errorCode);
186 
187     auto status = ConvertOppTransferInformationToJS(env, ret, information);
188     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INTERNAL_ERROR);
189     return ret;
190 }
191 
CancelTransfer(napi_env env, napi_callback_info info)192 napi_value NapiBluetoothOpp::CancelTransfer(napi_env env, napi_callback_info info)
193 {
194     HILOGI("enter");
195 
196     napi_value ret = nullptr;
197     bool isOk = false;
198     napi_get_boolean(env, isOk, &ret);
199     napi_status checkRet = CheckEmptyParam(env, info);
200     NAPI_BT_ASSERT_RETURN(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM, ret);
201 
202     Opp *profile = Opp::GetProfile();
203     profile->CancelTransfer(isOk);
204     napi_get_boolean(env, isOk, &ret);
205     return ret;
206 }
207 
GetConnectionDevices(napi_env env, napi_callback_info info)208 napi_value NapiBluetoothOpp::GetConnectionDevices(napi_env env, napi_callback_info info)
209 {
210     HILOGI("enter");
211 
212     napi_value ret = nullptr;
213     napi_create_array(env, &ret);
214     napi_status checkRet = CheckEmptyParam(env, info);
215     NAPI_BT_ASSERT_RETURN(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM, ret);
216 
217     Opp *profile = Opp::GetProfile();
218     vector<int32_t> states = { static_cast<int32_t>(BTConnectState::CONNECTED) };
219     vector<BluetoothRemoteDevice> devices {};
220     int32_t errorCode = profile->GetDevicesByStates(states, devices);
221     NAPI_BT_ASSERT_RETURN(env, errorCode == BT_NO_ERROR, errorCode, ret);
222 
223     vector<string> deviceVector;
224     for (auto &device : devices) {
225         deviceVector.push_back(device.GetDeviceAddr());
226     }
227     auto status = ConvertStringVectorToJS(env, ret, deviceVector);
228     NAPI_BT_ASSERT_RETURN(env, status == napi_ok, BT_ERR_INTERNAL_ERROR, ret);
229     return ret;
230 }
231 
GetDeviceState(napi_env env, napi_callback_info info)232 napi_value NapiBluetoothOpp::GetDeviceState(napi_env env, napi_callback_info info)
233 {
234     HILOGI("enter");
235     std::string remoteAddr{};
236     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
237     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
238 
239     Opp *profile = Opp::GetProfile();
240     BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
241     int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
242     int32_t errorCode = profile->GetDeviceState(device, state);
243     HILOGD("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
244     NAPI_BT_ASSERT_RETURN_UNDEF(env, errorCode == BT_NO_ERROR, errorCode);
245 
246     napi_value result = nullptr;
247     int32_t profileState = GetProfileConnectionState(state);
248     napi_create_int32(env, profileState, &result);
249     return result;
250 }
251 }  // namespace Bluetooth
252 }  // namespace OHOS