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 "bluetooth_errorcode.h"
17 #include "napi_bluetooth_avrcp_tg.h"
18 #include "napi_bluetooth_profile.h"
19 #include "napi_bluetooth_event.h"
20 #include "napi_bluetooth_error.h"
21 #include "napi_event_subscribe_module.h"
22
23 namespace OHOS {
24 namespace Bluetooth {
25 using namespace std;
26
27 std::shared_ptr<NapiAvrcpTargetObserver> NapiAvrcpTarget::observer_ = std::make_shared<NapiAvrcpTargetObserver>();
28 bool NapiAvrcpTarget::isRegistered_ = false;
29
DefineAvrcpTargetJSClass(napi_env env)30 void NapiAvrcpTarget::DefineAvrcpTargetJSClass(napi_env env)
31 {
32 napi_value constructor;
33 napi_property_descriptor properties[] = {
34 DECLARE_NAPI_FUNCTION("connect", Connect),
35 DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
36 DECLARE_NAPI_FUNCTION("on", On),
37 DECLARE_NAPI_FUNCTION("off", Off),
38 DECLARE_NAPI_FUNCTION("getConnectionDevices", GetConnectionDevices),
39 DECLARE_NAPI_FUNCTION("getDeviceState", GetDeviceState),
40 };
41
42 napi_define_class(env, "AvrcpTarget", NAPI_AUTO_LENGTH, AvrcpTargetConstructor, nullptr,
43 sizeof(properties) / sizeof(properties[0]), properties, &constructor);
44 napi_value napiProfile;
45 napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
46 NapiProfile::SetProfile(env, ProfileId::PROFILE_AVRCP_TG, napiProfile);
47 }
48
AvrcpTargetConstructor(napi_env env, napi_callback_info info)49 napi_value NapiAvrcpTarget::AvrcpTargetConstructor(napi_env env, napi_callback_info info)
50 {
51 napi_value thisVar = nullptr;
52 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
53 return thisVar;
54 }
55
On(napi_env env, napi_callback_info info)56 napi_value NapiAvrcpTarget::On(napi_env env, napi_callback_info info)
57 {
58 if (observer_) {
59 auto status = observer_->eventSubscribe_.Register(env, info);
60 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
61 }
62
63 if (!isRegistered_) {
64 AvrcpTarget *profile = AvrcpTarget::GetProfile();
65 profile->RegisterObserver(observer_);
66 isRegistered_ = true;
67 }
68 HILOGI("Napi Avrcp Target is registered");
69 return NapiGetUndefinedRet(env);
70 }
71
Off(napi_env env, napi_callback_info info)72 napi_value NapiAvrcpTarget::Off(napi_env env, napi_callback_info info)
73 {
74 if (observer_) {
75 auto status = observer_->eventSubscribe_.Deregister(env, info);
76 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
77 }
78 return NapiGetUndefinedRet(env);
79 }
80
GetConnectionDevices(napi_env env, napi_callback_info info)81 napi_value NapiAvrcpTarget::GetConnectionDevices(napi_env env, napi_callback_info info)
82 {
83 HILOGI("enter");
84 napi_value ret = nullptr;
85 napi_create_array(env, &ret);
86 AvrcpTarget *profile = AvrcpTarget::GetProfile();
87 vector<int> states;
88 states.push_back(1);
89 vector<BluetoothRemoteDevice> devices = profile->GetDevicesByStates(states);
90 vector<string> deviceVector;
91 for (auto &device: devices) {
92 deviceVector.push_back(device.GetDeviceAddr());
93 }
94 ConvertStringVectorToJS(env, ret, deviceVector);
95 return ret;
96 }
97
GetDeviceState(napi_env env, napi_callback_info info)98 napi_value NapiAvrcpTarget::GetDeviceState(napi_env env, napi_callback_info info)
99 {
100 HILOGI("enter");
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 deviceId;
115 if (!ParseString(env, deviceId, argv[PARAM0])) {
116 HILOGE("string expected.");
117 return ret;
118 }
119
120 AvrcpTarget *profile = AvrcpTarget::GetProfile();
121 BluetoothRemoteDevice device(deviceId, 1);
122 int state = profile->GetDeviceState(device);
123 napi_value result = nullptr;
124 int status = GetProfileConnectionState(state);
125 napi_create_int32(env, status, &result);
126 HILOGI("status: %{public}d", status);
127 return result;
128 }
129
Connect(napi_env env, napi_callback_info info)130 napi_value NapiAvrcpTarget::Connect(napi_env env, napi_callback_info info)
131 {
132 HILOGI("enter");
133 size_t expectedArgsCount = ARGS_SIZE_ONE;
134 size_t argc = expectedArgsCount;
135 napi_value argv[ARGS_SIZE_ONE] = {0};
136 napi_value thisVar = nullptr;
137
138 napi_value ret = nullptr;
139 napi_get_undefined(env, &ret);
140
141 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
142 if (argc != expectedArgsCount) {
143 HILOGE("Requires 1 argument.");
144 return ret;
145 }
146 string deviceId;
147 if (!ParseString(env, deviceId, argv[PARAM0])) {
148 HILOGE("string expected.");
149 return ret;
150 }
151
152 AvrcpTarget *profile = AvrcpTarget::GetProfile();
153 BluetoothRemoteDevice device(deviceId, 1);
154 bool res = profile->Connect(device);
155
156 napi_value result = nullptr;
157 napi_get_boolean(env, res, &result);
158 HILOGI("res: %{public}d", res);
159 return result;
160 }
161
Disconnect(napi_env env, napi_callback_info info)162 napi_value NapiAvrcpTarget::Disconnect(napi_env env, napi_callback_info info)
163 {
164 HILOGI("enter");
165 size_t expectedArgsCount = ARGS_SIZE_ONE;
166 size_t argc = expectedArgsCount;
167 napi_value argv[ARGS_SIZE_ONE] = {0};
168 napi_value thisVar = nullptr;
169
170 napi_value ret = nullptr;
171 napi_get_undefined(env, &ret);
172
173 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
174 if (argc != expectedArgsCount) {
175 HILOGE("Requires 1 argument.");
176 return ret;
177 }
178 string deviceId;
179 if (!ParseString(env, deviceId, argv[PARAM0])) {
180 HILOGE("string expected.");
181 return ret;
182 }
183
184 AvrcpTarget *profile = AvrcpTarget::GetProfile();
185 BluetoothRemoteDevice device(deviceId, 1);
186 bool res = profile->Disconnect(device);
187
188 napi_value result = nullptr;
189 napi_get_boolean(env, res, &result);
190 HILOGI("res: %{public}d", res);
191 return result;
192 }
193
194 } // namespace Bluetooth
195 } // namespace OHOS
196