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