• 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_a2dp_snk.h"
16 #include "napi_bluetooth_profile.h"
17 #include "napi_bluetooth_a2dp_snk.h"
18 #include "napi_bluetooth_event.h"
19 
20 namespace OHOS {
21 namespace Bluetooth {
22 using namespace std;
23 
24 NapiA2dpSinkObserver NapiA2dpSink::observer_;
25 bool NapiA2dpSink::isRegistered_ = false;
26 
DefineA2dpSinkJSClass(napi_env env)27 void NapiA2dpSink::DefineA2dpSinkJSClass(napi_env env)
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("getPlayingState", getPlayingState),
36         DECLARE_NAPI_FUNCTION("connect", Connect),
37         DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
38     };
39 
40     napi_define_class(env, "A2dpSink", NAPI_AUTO_LENGTH, A2dpSinkConstructor, nullptr,
41         sizeof(properties) / sizeof(properties[0]), properties, &constructor);
42     napi_value napiProfile;
43     napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
44     NapiProfile::SetProfile(env, ProfileId::PROFILE_A2DP_SINK, napiProfile);
45     HILOGI("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("enter");
58     std::unique_lock<std::shared_mutex> guard(NapiA2dpSinkObserver::g_a2dpSinkCallbackInfosMutex);
59 
60     napi_value ret = nullptr;
61     ret = NapiEvent::OnEvent(env, info, observer_.callbackInfos_);
62     if (!isRegistered_) {
63         A2dpSink *profile = A2dpSink::GetProfile();
64         profile->RegisterObserver(&observer_);
65         isRegistered_ = true;
66     }
67     HILOGI("Napi A2dpSink is registered");
68     return ret;
69 }
70 
Off(napi_env env,napi_callback_info info)71 napi_value NapiA2dpSink::Off(napi_env env, napi_callback_info info)
72 {
73     HILOGI("enter");
74     std::unique_lock<std::shared_mutex> guard(NapiA2dpSinkObserver::g_a2dpSinkCallbackInfosMutex);
75 
76     napi_value ret = nullptr;
77     ret = NapiEvent::OffEvent(env, info, observer_.callbackInfos_);
78     HILOGI("napi A2dpSink is unregistered");
79     return ret;
80 }
81 
GetConnectionDevices(napi_env env,napi_callback_info info)82 napi_value NapiA2dpSink::GetConnectionDevices(napi_env env, napi_callback_info info)
83 {
84     HILOGI("enter");
85     napi_value ret = nullptr;
86     napi_create_array(env, &ret);
87     A2dpSink *profile = A2dpSink::GetProfile();
88     vector<int> states;
89     states.push_back(1);
90     vector<BluetoothRemoteDevice> devices = profile->GetDevicesByStates(states);
91     vector<string> deviceVector;
92     for (auto &device: devices) {
93         deviceVector.push_back(device.GetDeviceAddr());
94     }
95     ConvertStringVectorToJS(env, ret, deviceVector);
96     return ret;
97 }
98 
GetDeviceState(napi_env env,napi_callback_info info)99 napi_value NapiA2dpSink::GetDeviceState(napi_env env, napi_callback_info info)
100 {
101     HILOGI("enter");
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 deviceId;
116     if (!ParseString(env, deviceId, argv[PARAM0])) {
117         HILOGE("string expected.");
118         return ret;
119     }
120 
121     A2dpSink *profile = A2dpSink::GetProfile();
122     BluetoothRemoteDevice device(deviceId, 1);
123     int state = profile->GetDeviceState(device);
124     napi_value result = nullptr;
125     int status = GetProfileConnectionState(state);
126     napi_create_int32(env, status, &result);
127     HILOGI("status: %{public}d", status);
128     return result;
129 }
130 
Connect(napi_env env,napi_callback_info info)131 napi_value NapiA2dpSink::Connect(napi_env env, napi_callback_info info)
132 {
133     HILOGI("enter");
134     size_t expectedArgsCount = ARGS_SIZE_ONE;
135     size_t argc = expectedArgsCount;
136     napi_value argv[ARGS_SIZE_ONE] = {0};
137     napi_value thisVar = nullptr;
138 
139     napi_value ret = nullptr;
140     napi_get_undefined(env, &ret);
141 
142     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
143     if (argc != expectedArgsCount) {
144         HILOGE("Requires 1 argument.");
145         return ret;
146     }
147     string deviceId;
148     if (!ParseString(env, deviceId, argv[PARAM0])) {
149         HILOGE("string expected.");
150         return ret;
151     }
152 
153     A2dpSink *profile = A2dpSink::GetProfile();
154     BluetoothRemoteDevice device(deviceId, 1);
155     bool res = profile->Connect(device);
156 
157     napi_value result = nullptr;
158     napi_get_boolean(env, res, &result);
159     HILOGI("res: %{public}d", res);
160     return result;
161 }
162 
Disconnect(napi_env env,napi_callback_info info)163 napi_value NapiA2dpSink::Disconnect(napi_env env, napi_callback_info info)
164 {
165     HILOGI("enter");
166     size_t expectedArgsCount = ARGS_SIZE_ONE;
167     size_t argc = expectedArgsCount;
168     napi_value argv[ARGS_SIZE_ONE] = {0};
169     napi_value thisVar = nullptr;
170 
171     napi_value ret = nullptr;
172     napi_get_undefined(env, &ret);
173 
174     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
175     if (argc != expectedArgsCount) {
176         HILOGE("Requires 1 argument.");
177         return ret;
178     }
179     string deviceId;
180     if (!ParseString(env, deviceId, argv[PARAM0])) {
181         HILOGE("string expected.");
182         return ret;
183     }
184 
185     A2dpSink *profile = A2dpSink::GetProfile();
186     BluetoothRemoteDevice device(deviceId, 1);
187     bool res = profile->Disconnect(device);
188 
189     napi_value result = nullptr;
190     napi_get_boolean(env, res, &result);
191     HILOGI("res: %{public}d", res);
192     return result;
193 }
194 
getPlayingState(napi_env env,napi_callback_info info)195 napi_value NapiA2dpSink::getPlayingState(napi_env env, napi_callback_info info)
196 {
197     HILOGI("enter");
198     size_t expectedArgsCount = ARGS_SIZE_ONE;
199     size_t argc = expectedArgsCount;
200     napi_value argv[ARGS_SIZE_ONE] = {0};
201     napi_value thisVar = nullptr;
202 
203     napi_value ret = nullptr;
204     napi_get_undefined(env, &ret);
205 
206     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
207     if (argc != expectedArgsCount) {
208         HILOGE("Requires 1 argument.");
209         return ret;
210     }
211     string deviceId;
212     if (!ParseString(env, deviceId, argv[PARAM0])) {
213         HILOGE("string expected.");
214         return ret;
215     }
216 
217     A2dpSink *profile = A2dpSink::GetProfile();
218     BluetoothRemoteDevice device(deviceId, 1);
219     int res = profile->GetPlayingState(device);
220 
221     napi_value result = nullptr;
222     napi_create_int32(env, res, &result);
223     HILOGI("res: %{public}d", res);
224     return result;
225 }
226 
227 } // namespace Bluetooth
228 } // namespace OHOS
229