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