• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 #include "bluetooth_pbap_client.h"
16 #include "napi_bluetooth_pbap_pce.h"
17 #include "napi_bluetooth_profile.h"
18 
19 namespace OHOS {
20 namespace Bluetooth {
21 using namespace std;
22 
23 NapiPbapPceObserver NapiPbapClient::observer_;
24 bool NapiPbapClient::isRegistered_ = false;
25 
DefinePbapClientJSClass(napi_env env)26 void NapiPbapClient::DefinePbapClientJSClass(napi_env env)
27 {
28 
29     napi_value constructor;
30     napi_property_descriptor properties[] = {
31         DECLARE_NAPI_FUNCTION("on", On),
32         DECLARE_NAPI_FUNCTION("off", Off),
33         DECLARE_NAPI_FUNCTION("getConnectionDevices", GetConnectionDevices),
34         DECLARE_NAPI_FUNCTION("getDeviceState", GetDeviceState),
35         DECLARE_NAPI_FUNCTION("connect", Connect),
36         DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
37     };
38 
39     napi_define_class(env, "PbapClient", NAPI_AUTO_LENGTH, PbapClientConstructor, nullptr,
40         sizeof(properties) / sizeof(properties[0]), properties, &constructor);
41 
42     napi_value NapiProfile;
43     napi_new_instance(env, constructor, 0, nullptr, &NapiProfile);
44     NapiProfile::SetProfile(ProfileCode::CODE_BT_PROFILE_PBAP_CLIENT, NapiProfile);
45     HILOGI("DefinePbapClientJSClass finished");
46 }
47 
PbapClientConstructor(napi_env env,napi_callback_info info)48 napi_value NapiPbapClient::PbapClientConstructor(napi_env env, napi_callback_info info)
49 {
50     napi_value thisVar = nullptr;
51     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
52     return thisVar;
53 }
54 
On(napi_env env,napi_callback_info info)55 napi_value NapiPbapClient::On(napi_env env, napi_callback_info info)
56 {
57     HILOGI("On called");
58     size_t expectedArgsCount = ARGS_SIZE_TWO;
59     size_t argc = expectedArgsCount;
60     napi_value argv[ARGS_SIZE_TWO] = {0};
61     napi_value thisVar = nullptr;
62 
63     napi_value ret = nullptr;
64     napi_get_undefined(env, &ret);
65 
66     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
67     if (argc != expectedArgsCount) {
68         HILOGE("Requires 2 argument.");
69         return ret;
70     }
71     string type;
72     if (!ParseString(env, type, argv[PARAM0])) {
73         HILOGE("string expected.");
74         return ret;
75     }
76     std::shared_ptr<BluetoothCallbackInfo> callbackInfo = std::make_shared<BluetoothCallbackInfo>();
77     callbackInfo->env_ = env;
78 
79     napi_valuetype valueType = napi_undefined;
80     napi_typeof(env, argv[PARAM1], &valueType);
81     if (valueType != napi_function) {
82         HILOGE("Wrong argument type. Function expected.");
83         return ret;
84     }
85     napi_create_reference(env, argv[PARAM1], 1, &callbackInfo->callback_);
86     observer_.callbackInfos_[type] = callbackInfo;
87 
88     if (!isRegistered_) {
89         PbapClient *profile = PbapClient::GetProfile();
90         profile->RegisterObserver(&observer_);
91         isRegistered_ = true;
92     }
93 
94     HILOGI("%{public}s is registered", type.c_str());
95     return ret;
96 
97 }
98 
Off(napi_env env,napi_callback_info info)99 napi_value NapiPbapClient::Off(napi_env env, napi_callback_info info)
100 {
101     HILOGI("Off called");
102     size_t expectedArgsCount = ARGS_SIZE_ONE;
103     size_t argc = expectedArgsCount;
104     napi_value argv[ARGS_SIZE_ONE] = {0};
105     napi_value thisVar = nullptr;
106 
107     napi_value ret = nullptr;
108     napi_get_undefined(env, &ret);
109 
110     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
111     if (argc != expectedArgsCount) {
112         HILOGE("Requires 1 argument.");
113         return ret;
114     }
115     string type;
116     if (!ParseString(env, type, argv[PARAM0])) {
117         HILOGE("string expected.");
118         return ret;
119     }
120     observer_.callbackInfos_[type] = nullptr;
121 
122     HILOGI("%{public}s is unregistered", type.c_str());
123 
124     return ret;
125 }
126 
GetConnectionDevices(napi_env env,napi_callback_info info)127 napi_value NapiPbapClient::GetConnectionDevices(napi_env env, napi_callback_info info)
128 {
129 
130     HILOGI("GetConnectionDevices called");
131     napi_value ret = nullptr;
132     napi_create_array(env, &ret);
133     PbapClient *profile = PbapClient::GetProfile();
134     vector<int> states;
135     states.push_back(1);
136     vector<BluetoothRemoteDevice> devices = profile->GetDevicesByStates(states);
137     vector<string> deviceVector;
138     for (auto &device: devices) {
139         deviceVector.push_back(device.GetDeviceAddr());
140     }
141     ConvertStringVectorToJS(env, ret, deviceVector);
142     return ret;
143 }
144 
GetDeviceState(napi_env env,napi_callback_info info)145 napi_value NapiPbapClient::GetDeviceState(napi_env env, napi_callback_info info)
146 {
147     HILOGI("GetDeviceState called");
148 
149     size_t expectedArgsCount = ARGS_SIZE_ONE;
150     size_t argc = expectedArgsCount;
151     napi_value argv[ARGS_SIZE_ONE] = {0};
152     napi_value thisVar = nullptr;
153 
154     napi_value ret = nullptr;
155     napi_get_undefined(env, &ret);
156 
157     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
158     if (argc != expectedArgsCount) {
159         HILOGE("Requires 1 argument.");
160         return ret;
161     }
162     string deviceId;
163     if (!ParseString(env, deviceId, argv[PARAM0])) {
164         HILOGE("string expected.");
165         return ret;
166     }
167 
168     PbapClient *profile = PbapClient::GetProfile();
169     BluetoothRemoteDevice device(deviceId, 1);
170     int state = profile->GetDeviceState(device);
171     napi_value result = nullptr;
172     napi_create_int32(env, GetProfileConnectionState(state), &result);
173     return result;
174 }
175 
Connect(napi_env env,napi_callback_info info)176 napi_value NapiPbapClient::Connect(napi_env env, napi_callback_info info)
177 {
178     HILOGI("Connect called");
179 
180     size_t expectedArgsCount = ARGS_SIZE_ONE;
181     size_t argc = expectedArgsCount;
182     napi_value argv[ARGS_SIZE_ONE] = {0};
183     napi_value thisVar = nullptr;
184 
185     napi_value ret = nullptr;
186     napi_get_undefined(env, &ret);
187 
188     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
189     if (argc != expectedArgsCount) {
190         HILOGE("Requires 1 argument.");
191         return ret;
192     }
193     string deviceId;
194     if (!ParseString(env, deviceId, argv[PARAM0])) {
195         HILOGE("string expected.");
196         return ret;
197     }
198 
199     PbapClient *profile = PbapClient::GetProfile();
200     BluetoothRemoteDevice device(deviceId, 1);
201     bool res = profile->Connect(device);
202 
203     napi_value result = nullptr;
204     napi_get_boolean(env, res, &result);
205     return result;
206 }
207 
Disconnect(napi_env env,napi_callback_info info)208 napi_value NapiPbapClient::Disconnect(napi_env env, napi_callback_info info)
209 {
210     HILOGI("Disconnect called");
211 
212     size_t expectedArgsCount = ARGS_SIZE_ONE;
213     size_t argc = expectedArgsCount;
214     napi_value argv[ARGS_SIZE_ONE] = {0};
215     napi_value thisVar = nullptr;
216 
217     napi_value ret = nullptr;
218     napi_get_undefined(env, &ret);
219 
220     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
221     if (argc != expectedArgsCount) {
222         HILOGE("Requires 1 argument.");
223         return ret;
224     }
225     string deviceId;
226     if (!ParseString(env, deviceId, argv[PARAM0])) {
227         HILOGE("string expected.");
228         return ret;
229     }
230 
231     PbapClient *profile = PbapClient::GetProfile();
232     BluetoothRemoteDevice device(deviceId, 1);
233     bool res = profile->Disconnect(device);
234 
235     napi_value result = nullptr;
236     napi_get_boolean(env, res, &result);
237     return result;
238 }
239 } // namespace Bluetooth
240 } // namespace OHOS