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 #include "napi_bluetooth_event.h"
19
20 namespace OHOS {
21 namespace Bluetooth {
22 using namespace std;
23
24 NapiAvrcpTargetObserver NapiAvrcpTarget::observer_;
25 bool NapiAvrcpTarget::isRegistered_ = false;
26
DefineAvrcpTargetJSClass(napi_env env)27 void NapiAvrcpTarget::DefineAvrcpTargetJSClass(napi_env env)
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 napi_value napiProfile;
42 napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
43 NapiProfile::SetProfile(env, ProfileId::PROFILE_AVRCP_TG, napiProfile);
44 HILOGI("finished");
45 }
46
AvrcpTargetConstructor(napi_env env,napi_callback_info info)47 napi_value NapiAvrcpTarget::AvrcpTargetConstructor(napi_env env, napi_callback_info info)
48 {
49 napi_value thisVar = nullptr;
50 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
51 return thisVar;
52 }
53
On(napi_env env,napi_callback_info info)54 napi_value NapiAvrcpTarget::On(napi_env env, napi_callback_info info)
55 {
56 HILOGI("enter");
57 std::unique_lock<std::shared_mutex> guard(NapiAvrcpTargetObserver::g_avrcpTgCallbackInfosMutex);
58
59 napi_value ret = nullptr;
60 ret = NapiEvent::OnEvent(env, info, observer_.callbackInfos_);
61 if (!isRegistered_) {
62 AvrcpTarget *profile = AvrcpTarget::GetProfile();
63 profile->RegisterObserver(&observer_);
64 isRegistered_ = true;
65 }
66 HILOGI("Napi Avrcp Target is registered");
67 return ret;
68 }
69
Off(napi_env env,napi_callback_info info)70 napi_value NapiAvrcpTarget::Off(napi_env env, napi_callback_info info)
71 {
72 HILOGI("enter");
73 std::unique_lock<std::shared_mutex> guard(NapiAvrcpTargetObserver::g_avrcpTgCallbackInfosMutex);
74
75 napi_value ret = nullptr;
76 ret = NapiEvent::OffEvent(env, info, observer_.callbackInfos_);
77 HILOGI("Napi Avrcp Target is unregistered");
78 return ret;
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