• 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_ct.h"
16 #include "napi_bluetooth_avrcp_ct.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 NapiAvrcpControllerObserver NapiAvrcpController::observer_;
25 bool NapiAvrcpController::isRegistered_ = false;
26 
DefineAvrcpControllerJSClass(napi_env env)27 void NapiAvrcpController::DefineAvrcpControllerJSClass(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, "AvrcpController", NAPI_AUTO_LENGTH, AvrcpControllerConstructor, 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_CT, napiProfile);
44     HILOGI("finished");
45 }
46 
AvrcpControllerConstructor(napi_env env,napi_callback_info info)47 napi_value NapiAvrcpController::AvrcpControllerConstructor(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 NapiAvrcpController::On(napi_env env, napi_callback_info info)
55 {
56     HILOGI("enter");
57     std::unique_lock<std::shared_mutex> guard(NapiAvrcpControllerObserver::g_avrcpCtCallbackInfosMutex);
58 
59     napi_value ret = nullptr;
60     ret = NapiEvent::OnEvent(env, info, observer_.callbackInfos_);
61     if (!isRegistered_) {
62         AvrcpController *profile = AvrcpController::GetProfile();
63         profile->RegisterObserver(&observer_);
64         isRegistered_ = true;
65     }
66 
67     HILOGI("Napi Avrcp is registered");
68     return ret;
69 }
70 
Off(napi_env env,napi_callback_info info)71 napi_value NapiAvrcpController::Off(napi_env env, napi_callback_info info)
72 {
73     HILOGI("enter");
74     std::unique_lock<std::shared_mutex> guard(NapiAvrcpControllerObserver::g_avrcpCtCallbackInfosMutex);
75 
76     napi_value ret = nullptr;
77     ret = NapiEvent::OffEvent(env, info, observer_.callbackInfos_);
78     HILOGI("Napi Avrcp is unregistered");
79     return ret;
80 }
81 
GetConnectionDevices(napi_env env,napi_callback_info info)82 napi_value NapiAvrcpController::GetConnectionDevices(napi_env env, napi_callback_info info)
83 {
84     HILOGI("enter");
85     napi_value ret = nullptr;
86     napi_create_array(env, &ret);
87     AvrcpController *profile = AvrcpController::GetProfile();
88     vector<int> states;
89     states.push_back(1);
90     vector<BluetoothRemoteDevice> devices = profile->GetDevicesByStates(states);
91     vector<string> deviceVector;
92     for (auto &device: devices) {
93         deviceVector.push_back(device.GetDeviceAddr());
94     }
95     ConvertStringVectorToJS(env, ret, deviceVector);
96     return ret;
97 }
98 
GetDeviceState(napi_env env,napi_callback_info info)99 napi_value NapiAvrcpController::GetDeviceState(napi_env env, napi_callback_info info)
100 {
101     HILOGI("enter");
102     size_t expectedArgsCount = ARGS_SIZE_ONE;
103     size_t argc = expectedArgsCount;
104     napi_value argv[ARGS_SIZE_ONE] = {0};
105     napi_value thisVar = nullptr;
106 
107     napi_value ret = nullptr;
108     napi_get_undefined(env, &ret);
109 
110     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
111     if (argc != expectedArgsCount) {
112         HILOGE("Requires 1 argument.");
113         return ret;
114     }
115     string deviceId;
116     if (!ParseString(env, deviceId, argv[PARAM0])) {
117         HILOGE("string expected.");
118         return ret;
119     }
120 
121     AvrcpController *profile = AvrcpController::GetProfile();
122     BluetoothRemoteDevice device(deviceId, 1);
123     int state = profile->GetDeviceState(device);
124     napi_value result = nullptr;
125     int status = GetProfileConnectionState(state);
126     napi_create_int32(env, status, &result);
127     HILOGI("status: %{public}d", status);
128     return result;
129 }
130 
131 
Connect(napi_env env,napi_callback_info info)132 napi_value NapiAvrcpController::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     AvrcpController *profile = AvrcpController::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 NapiAvrcpController::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     AvrcpController *profile = AvrcpController::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 } // namespace Bluetooth
196 } // namespace OHOS
197