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_avrcp_tg.h"
16 #include "napi_bluetooth_avrcp_tg.h"
17 #include "napi_bluetooth_profile.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
21 using namespace std;
22
23 NapiAvrcpTargetObserver NapiAvrcpTarget::observer_;
24 bool NapiAvrcpTarget::isRegistered_ = false;
25
DefineAvrcpTargetJSClass(napi_env env)26 void NapiAvrcpTarget::DefineAvrcpTargetJSClass(napi_env env)
27 {
28
29 napi_value constructor;
30 napi_property_descriptor properties[] = {
31 DECLARE_NAPI_FUNCTION("connect", Connect),
32 DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
33 DECLARE_NAPI_FUNCTION("on", On),
34 DECLARE_NAPI_FUNCTION("off", Off),
35 DECLARE_NAPI_FUNCTION("getConnectionDevices", GetConnectionDevices),
36 DECLARE_NAPI_FUNCTION("getDeviceState", GetDeviceState),
37 };
38
39 napi_define_class(env, "AvrcpTarget", NAPI_AUTO_LENGTH, AvrcpTargetConstructor, 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_AVRCP_TG, napiProfile);
45 HILOGI("DefineAvrcpTargetJSClass finished");
46 }
47
AvrcpTargetConstructor(napi_env env,napi_callback_info info)48 napi_value NapiAvrcpTarget::AvrcpTargetConstructor(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 NapiAvrcpTarget::On(napi_env env, napi_callback_info info) {
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 AvrcpTarget *profile = AvrcpTarget::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 NapiAvrcpTarget::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 NapiAvrcpTarget::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 AvrcpTarget *profile = AvrcpTarget::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 NapiAvrcpTarget::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 AvrcpTarget *profile = AvrcpTarget::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 NapiAvrcpTarget::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 AvrcpTarget *profile = AvrcpTarget::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 NapiAvrcpTarget::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 AvrcpTarget *profile = AvrcpTarget::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
238 } // namespace Bluetooth
239 } // namespace OHOS
240