• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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_a2dp_snk.h"
16 #include "napi_bluetooth_profile.h"
17 #include "napi_bluetooth_a2dp_snk.h"
18 
19 namespace OHOS {
20 namespace Bluetooth {
21 using namespace std;
22 
23 NapiA2dpSinkObserver NapiA2dpSink::observer_;
24 bool NapiA2dpSink::isRegistered_ = false;
25 
DefineA2dpSinkJSClass(napi_env env)26 void NapiA2dpSink::DefineA2dpSinkJSClass(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("getPlayingState", getPlayingState),
35         DECLARE_NAPI_FUNCTION("connect", Connect),
36         DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
37     };
38 
39     napi_define_class(env, "A2dpSink", NAPI_AUTO_LENGTH, A2dpSinkConstructor, 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_A2DP_SINK, napiProfile);
45     HILOGI("DefineA2dpSinkJSClass finished");
46 }
47 
A2dpSinkConstructor(napi_env env,napi_callback_info info)48 napi_value NapiA2dpSink::A2dpSinkConstructor(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 NapiA2dpSink::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         A2dpSink *profile = A2dpSink::GetProfile();
90         profile->RegisterObserver(&observer_);
91         isRegistered_ = true;
92     }
93 
94     HILOGI("%{public}s is registered", type.c_str());
95     return ret;
96 }
97 
Off(napi_env env,napi_callback_info info)98 napi_value NapiA2dpSink::Off(napi_env env, napi_callback_info info)
99 {
100     HILOGI("Off called");
101     size_t expectedArgsCount = ARGS_SIZE_ONE;
102     size_t argc = expectedArgsCount;
103     napi_value argv[ARGS_SIZE_ONE] = {0};
104     napi_value thisVar = nullptr;
105 
106     napi_value ret = nullptr;
107     napi_get_undefined(env, &ret);
108 
109     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
110     if (argc != expectedArgsCount) {
111         HILOGE("Requires 1 argument.");
112         return ret;
113     }
114     string type;
115     if (!ParseString(env, type, argv[PARAM0])) {
116         HILOGE("string expected.");
117         return ret;
118     }
119     observer_.callbackInfos_[type] = nullptr;
120 
121     HILOGI("%{public}s is unregistered", type.c_str());
122 
123     return ret;
124 }
125 
GetConnectionDevices(napi_env env,napi_callback_info info)126 napi_value NapiA2dpSink::GetConnectionDevices(napi_env env, napi_callback_info info)
127 {
128     HILOGI("GetConnectionDevices called");
129     napi_value ret = nullptr;
130     napi_create_array(env, &ret);
131     A2dpSink *profile = A2dpSink::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 NapiA2dpSink::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     A2dpSink *profile = A2dpSink::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 
Connect(napi_env env,napi_callback_info info)174 napi_value NapiA2dpSink::Connect(napi_env env, napi_callback_info info)
175 {
176     HILOGI("Connect 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     A2dpSink *profile = A2dpSink::GetProfile();
198     BluetoothRemoteDevice device(deviceId, 1);
199     bool res = profile->Connect(device);
200 
201     napi_value result = nullptr;
202     napi_get_boolean(env, res, &result);
203     return result;
204 }
205 
Disconnect(napi_env env,napi_callback_info info)206 napi_value NapiA2dpSink::Disconnect(napi_env env, napi_callback_info info)
207 {
208     HILOGI("Disconnect called");
209 
210     size_t expectedArgsCount = ARGS_SIZE_ONE;
211     size_t argc = expectedArgsCount;
212     napi_value argv[ARGS_SIZE_ONE] = {0};
213     napi_value thisVar = nullptr;
214 
215     napi_value ret = nullptr;
216     napi_get_undefined(env, &ret);
217 
218     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
219     if (argc != expectedArgsCount) {
220         HILOGE("Requires 1 argument.");
221         return ret;
222     }
223     string deviceId;
224     if (!ParseString(env, deviceId, argv[PARAM0])) {
225         HILOGE("string expected.");
226         return ret;
227     }
228 
229     A2dpSink *profile = A2dpSink::GetProfile();
230     BluetoothRemoteDevice device(deviceId, 1);
231     bool res = profile->Disconnect(device);
232 
233     napi_value result = nullptr;
234     napi_get_boolean(env, res, &result);
235     return result;
236 }
237 
getPlayingState(napi_env env,napi_callback_info info)238 napi_value NapiA2dpSink::getPlayingState(napi_env env, napi_callback_info info)
239 {
240     HILOGI("getPlayingState called");
241 
242     size_t expectedArgsCount = ARGS_SIZE_ONE;
243     size_t argc = expectedArgsCount;
244     napi_value argv[ARGS_SIZE_ONE] = {0};
245     napi_value thisVar = nullptr;
246 
247     napi_value ret = nullptr;
248     napi_get_undefined(env, &ret);
249 
250     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
251     if (argc != expectedArgsCount) {
252         HILOGE("Requires 1 argument.");
253         return ret;
254     }
255     string deviceId;
256     if (!ParseString(env, deviceId, argv[PARAM0])) {
257         HILOGE("string expected.");
258         return ret;
259     }
260 
261     A2dpSink *profile = A2dpSink::GetProfile();
262     BluetoothRemoteDevice device(deviceId, 1);
263     int res = profile->GetPlayingState(device);
264 
265     napi_value result = nullptr;
266     napi_create_int32(env, res, &result);
267     return result;
268 }
269 
270 } // namespace Bluetooth
271 } // namespace OHOS
272