• 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_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 std::shared_ptr<NapiAvrcpTargetObserver> NapiAvrcpTarget::observer_ = std::make_shared<NapiAvrcpTargetObserver>();
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 }
45 
AvrcpTargetConstructor(napi_env env,napi_callback_info info)46 napi_value NapiAvrcpTarget::AvrcpTargetConstructor(napi_env env, napi_callback_info info)
47 {
48     napi_value thisVar = nullptr;
49     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
50     return thisVar;
51 }
52 
On(napi_env env,napi_callback_info info)53 napi_value NapiAvrcpTarget::On(napi_env env, napi_callback_info info)
54 {
55     HILOGI("enter");
56     std::unique_lock<std::shared_mutex> guard(NapiAvrcpTargetObserver::g_avrcpTgCallbackInfosMutex);
57 
58     napi_value ret = nullptr;
59     ret = NapiEvent::OnEvent(env, info, observer_->callbackInfos_);
60     if (!isRegistered_) {
61         AvrcpTarget *profile = AvrcpTarget::GetProfile();
62         profile->RegisterObserver(observer_);
63         isRegistered_ = true;
64     }
65     HILOGI("Napi Avrcp Target is registered");
66     return ret;
67 }
68 
Off(napi_env env,napi_callback_info info)69 napi_value NapiAvrcpTarget::Off(napi_env env, napi_callback_info info)
70 {
71     HILOGI("enter");
72     std::unique_lock<std::shared_mutex> guard(NapiAvrcpTargetObserver::g_avrcpTgCallbackInfosMutex);
73 
74     napi_value ret = nullptr;
75     ret = NapiEvent::OffEvent(env, info, observer_->callbackInfos_);
76     HILOGI("Napi Avrcp Target is unregistered");
77     return ret;
78 }
79 
GetConnectionDevices(napi_env env,napi_callback_info info)80 napi_value NapiAvrcpTarget::GetConnectionDevices(napi_env env, napi_callback_info info)
81 {
82     HILOGI("enter");
83     napi_value ret = nullptr;
84     napi_create_array(env, &ret);
85     AvrcpTarget *profile = AvrcpTarget::GetProfile();
86     vector<int> states;
87     states.push_back(1);
88     vector<BluetoothRemoteDevice> devices = profile->GetDevicesByStates(states);
89     vector<string> deviceVector;
90     for (auto &device: devices) {
91         deviceVector.push_back(device.GetDeviceAddr());
92     }
93     ConvertStringVectorToJS(env, ret, deviceVector);
94     return ret;
95 }
96 
GetDeviceState(napi_env env,napi_callback_info info)97 napi_value NapiAvrcpTarget::GetDeviceState(napi_env env, napi_callback_info info)
98 {
99     HILOGI("enter");
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 deviceId;
114     if (!ParseString(env, deviceId, argv[PARAM0])) {
115         HILOGE("string expected.");
116         return ret;
117     }
118 
119     AvrcpTarget *profile = AvrcpTarget::GetProfile();
120     BluetoothRemoteDevice device(deviceId, 1);
121     int state = profile->GetDeviceState(device);
122     napi_value result = nullptr;
123     int status = GetProfileConnectionState(state);
124     napi_create_int32(env, status, &result);
125     HILOGI("status: %{public}d", status);
126     return result;
127 }
128 
Connect(napi_env env,napi_callback_info info)129 napi_value NapiAvrcpTarget::Connect(napi_env env, napi_callback_info info)
130 {
131     HILOGI("enter");
132     size_t expectedArgsCount = ARGS_SIZE_ONE;
133     size_t argc = expectedArgsCount;
134     napi_value argv[ARGS_SIZE_ONE] = {0};
135     napi_value thisVar = nullptr;
136 
137     napi_value ret = nullptr;
138     napi_get_undefined(env, &ret);
139 
140     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
141     if (argc != expectedArgsCount) {
142         HILOGE("Requires 1 argument.");
143         return ret;
144     }
145     string deviceId;
146     if (!ParseString(env, deviceId, argv[PARAM0])) {
147         HILOGE("string expected.");
148         return ret;
149     }
150 
151     AvrcpTarget *profile = AvrcpTarget::GetProfile();
152     BluetoothRemoteDevice device(deviceId, 1);
153     bool res = profile->Connect(device);
154 
155     napi_value result = nullptr;
156     napi_get_boolean(env, res, &result);
157     HILOGI("res: %{public}d", res);
158     return result;
159 }
160 
Disconnect(napi_env env,napi_callback_info info)161 napi_value NapiAvrcpTarget::Disconnect(napi_env env, napi_callback_info info)
162 {
163     HILOGI("enter");
164     size_t expectedArgsCount = ARGS_SIZE_ONE;
165     size_t argc = expectedArgsCount;
166     napi_value argv[ARGS_SIZE_ONE] = {0};
167     napi_value thisVar = nullptr;
168 
169     napi_value ret = nullptr;
170     napi_get_undefined(env, &ret);
171 
172     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
173     if (argc != expectedArgsCount) {
174         HILOGE("Requires 1 argument.");
175         return ret;
176     }
177     string deviceId;
178     if (!ParseString(env, deviceId, argv[PARAM0])) {
179         HILOGE("string expected.");
180         return ret;
181     }
182 
183     AvrcpTarget *profile = AvrcpTarget::GetProfile();
184     BluetoothRemoteDevice device(deviceId, 1);
185     bool res = profile->Disconnect(device);
186 
187     napi_value result = nullptr;
188     napi_get_boolean(env, res, &result);
189     HILOGI("res: %{public}d", res);
190     return result;
191 }
192 
193 } // namespace Bluetooth
194 } // namespace OHOS
195