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