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_server.h"
16 #include "napi_bluetooth_pbap_pse.h"
17 #include "napi_bluetooth_profile.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
21 using namespace std;
22
23 NapiPbapPseObserver NapiPbapServer::observer_;
24 bool NapiPbapServer::isRegistered_ = false;
25
DefinePbapServerJSClass(napi_env env)26 void NapiPbapServer::DefinePbapServerJSClass(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("disconnect", Disconnect),
35 };
36
37 napi_define_class(env, "PbapServer", NAPI_AUTO_LENGTH, PbapServerConstructor, nullptr,
38 sizeof(properties) / sizeof(properties[0]), properties, &constructor);
39 napi_value napiProfile;
40 napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
41 NapiProfile::SetProfile(env, ProfileId::PROFILE_PBAP_SERVER, napiProfile);
42 HILOGI("finished");
43 }
44
PbapServerConstructor(napi_env env,napi_callback_info info)45 napi_value NapiPbapServer::PbapServerConstructor(napi_env env, napi_callback_info info)
46 {
47 napi_value thisVar = nullptr;
48 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
49 return thisVar;
50 }
51
On(napi_env env,napi_callback_info info)52 napi_value NapiPbapServer::On(napi_env env, napi_callback_info info)
53 {
54 HILOGI("enter");
55 size_t expectedArgsCount = ARGS_SIZE_TWO;
56 size_t argc = expectedArgsCount;
57 napi_value argv[ARGS_SIZE_TWO] = {0};
58 napi_value thisVar = nullptr;
59
60 napi_value ret = nullptr;
61 napi_get_undefined(env, &ret);
62
63 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
64 if (argc != expectedArgsCount) {
65 HILOGE("Requires 2 argument.");
66 return ret;
67 }
68 string type;
69 if (!ParseString(env, type, argv[PARAM0])) {
70 HILOGE("string expected.");
71 return ret;
72 }
73 std::shared_ptr<BluetoothCallbackInfo> callbackInfo = std::make_shared<BluetoothCallbackInfo>();
74 callbackInfo->env_ = env;
75
76 napi_valuetype valueType = napi_undefined;
77 napi_typeof(env, argv[PARAM1], &valueType);
78 if (valueType != napi_function) {
79 HILOGE("Wrong argument type. Function expected.");
80 return ret;
81 }
82 napi_create_reference(env, argv[PARAM1], 1, &callbackInfo->callback_);
83 observer_.callbackInfos_[type] = callbackInfo;
84
85 if (!isRegistered_) {
86 PbapServer *profile = PbapServer::GetProfile();
87 profile->RegisterObserver(&observer_);
88 isRegistered_ = true;
89 }
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 NapiPbapServer::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 HILOGI("%{public}s is unregistered", type.c_str());
118 return ret;
119 }
120
GetConnectionDevices(napi_env env,napi_callback_info info)121 napi_value NapiPbapServer::GetConnectionDevices(napi_env env, napi_callback_info info)
122 {
123 HILOGI("enter");
124 napi_value ret = nullptr;
125 napi_create_array(env, &ret);
126 PbapServer *profile = PbapServer::GetProfile();
127 vector<int> states;
128 states.push_back(1);
129 vector<BluetoothRemoteDevice> devices = profile->GetDevicesByStates(states);
130 vector<string> deviceVector;
131 for (auto &device: devices) {
132 deviceVector.push_back(device.GetDeviceAddr());
133 }
134 ConvertStringVectorToJS(env, ret, deviceVector);
135 return ret;
136 }
137
GetDeviceState(napi_env env,napi_callback_info info)138 napi_value NapiPbapServer::GetDeviceState(napi_env env, napi_callback_info info)
139 {
140 HILOGI("enter");
141 size_t expectedArgsCount = ARGS_SIZE_ONE;
142 size_t argc = expectedArgsCount;
143 napi_value argv[ARGS_SIZE_ONE] = {0};
144 napi_value thisVar = nullptr;
145
146 napi_value ret = nullptr;
147 napi_get_undefined(env, &ret);
148
149 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
150 if (argc != expectedArgsCount) {
151 HILOGE("Requires 1 argument.");
152 return ret;
153 }
154 string deviceId;
155 if (!ParseString(env, deviceId, argv[PARAM0])) {
156 HILOGE("string expected.");
157 return ret;
158 }
159
160 PbapServer *profile = PbapServer::GetProfile();
161 BluetoothRemoteDevice device(deviceId, 1);
162 int state = profile->GetDeviceState(device);
163 int status = GetProfileConnectionState(state);
164 napi_value result = nullptr;
165 napi_create_int32(env, status, &result);
166 HILOGI("status: %{public}d", status);
167 return result;
168 }
169
Disconnect(napi_env env,napi_callback_info info)170 napi_value NapiPbapServer::Disconnect(napi_env env, napi_callback_info info)
171 {
172 HILOGI("enter");
173 size_t expectedArgsCount = ARGS_SIZE_ONE;
174 size_t argc = expectedArgsCount;
175 napi_value argv[ARGS_SIZE_ONE] = {0};
176 napi_value thisVar = nullptr;
177
178 napi_value ret = nullptr;
179 napi_get_undefined(env, &ret);
180
181 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
182 if (argc != expectedArgsCount) {
183 HILOGE("Requires 1 argument.");
184 return ret;
185 }
186 string deviceId;
187 if (!ParseString(env, deviceId, argv[PARAM0])) {
188 HILOGE("string expected.");
189 return ret;
190 }
191
192 PbapServer *profile = PbapServer::GetProfile();
193 BluetoothRemoteDevice device(deviceId, 1);
194 bool res = profile->Disconnect(device);
195
196 napi_value result = nullptr;
197 napi_get_boolean(env, res, &result);
198 HILOGI("res: %{public}d", res);
199 return result;
200 }
201
202 } // namespace Bluetooth
203 } // namespace OHOS