• 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 
19 namespace OHOS {
20 namespace Bluetooth {
21 using namespace std;
22 
23 NapiAvrcpControllerObserver NapiAvrcpController::observer_;
24 bool NapiAvrcpController::isRegistered_ = false;
25 
DefineAvrcpControllerJSClass(napi_env env)26 void NapiAvrcpController::DefineAvrcpControllerJSClass(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, "AvrcpController", NAPI_AUTO_LENGTH, AvrcpControllerConstructor, 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_CT, napiProfile);
45     HILOGI("DefineAvrcpControllerJSClass finished");
46 }
47 
AvrcpControllerConstructor(napi_env env,napi_callback_info info)48 napi_value NapiAvrcpController::AvrcpControllerConstructor(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 NapiAvrcpController::On(napi_env env, napi_callback_info info)
56 {
57     HILOGI("On called");
58     size_t expectedArgsCount = ARGS_SIZE_TWO;
59     size_t argc = expectedArgsCount;
60     napi_value argv[ARGS_SIZE_TWO] = {0};
61     napi_value thisVar = nullptr;
62 
63     napi_value ret = nullptr;
64     napi_get_undefined(env, &ret);
65 
66     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
67     if (argc != expectedArgsCount) {
68         HILOGE("Requires 2 argument.");
69         return ret;
70     }
71     string type;
72     if (!ParseString(env, type, argv[PARAM0])) {
73         HILOGE("string expected.");
74         return ret;
75     }
76     std::shared_ptr<BluetoothCallbackInfo> callbackInfo = std::make_shared<BluetoothCallbackInfo>();
77     callbackInfo->env_ = env;
78 
79     napi_valuetype valueType = napi_undefined;
80     napi_typeof(env, argv[PARAM1], &valueType);
81     if (valueType != napi_function) {
82         HILOGE("Wrong argument type. Function expected.");
83         return ret;
84     }
85     napi_create_reference(env, argv[PARAM1], 1, &callbackInfo->callback_);
86     observer_.callbackInfos_[type] = callbackInfo;
87 
88     if (!isRegistered_) {
89         AvrcpController *profile = AvrcpController::GetProfile();
90         profile->RegisterObserver(&observer_);
91         isRegistered_ = true;
92     }
93 
94     HILOGI("%{public}s is registered", type.c_str());
95     return ret;
96 }
97 
Off(napi_env env,napi_callback_info info)98 napi_value NapiAvrcpController::Off(napi_env env, napi_callback_info info)
99 {
100     HILOGI("Off called");
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 type;
115     if (!ParseString(env, type, argv[PARAM0])) {
116         HILOGE("string expected.");
117         return ret;
118     }
119     observer_.callbackInfos_[type] = nullptr;
120 
121     HILOGI("%{public}s is unregistered", type.c_str());
122 
123     return ret;
124 }
125 
GetConnectionDevices(napi_env env,napi_callback_info info)126 napi_value NapiAvrcpController::GetConnectionDevices(napi_env env, napi_callback_info info)
127 {
128     HILOGI("GetConnectionDevices called");
129     napi_value ret = nullptr;
130     napi_create_array(env, &ret);
131     AvrcpController *profile = AvrcpController::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 NapiAvrcpController::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     AvrcpController *profile = AvrcpController::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 
174 
Connect(napi_env env,napi_callback_info info)175 napi_value NapiAvrcpController::Connect(napi_env env, napi_callback_info info)
176 {
177     HILOGI("Connect called");
178 
179     size_t expectedArgsCount = ARGS_SIZE_ONE;
180     size_t argc = expectedArgsCount;
181     napi_value argv[ARGS_SIZE_ONE] = {0};
182     napi_value thisVar = nullptr;
183 
184     napi_value ret = nullptr;
185     napi_get_undefined(env, &ret);
186 
187     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
188     if (argc != expectedArgsCount) {
189         HILOGE("Requires 1 argument.");
190         return ret;
191     }
192     string deviceId;
193     if (!ParseString(env, deviceId, argv[PARAM0])) {
194         HILOGE("string expected.");
195         return ret;
196     }
197 
198     AvrcpController *profile = AvrcpController::GetProfile();
199     BluetoothRemoteDevice device(deviceId, 1);
200     bool res = profile->Connect(device);
201 
202     napi_value result = nullptr;
203     napi_get_boolean(env, res, &result);
204     return result;
205 }
206 
Disconnect(napi_env env,napi_callback_info info)207 napi_value NapiAvrcpController::Disconnect(napi_env env, napi_callback_info info)
208 {
209     HILOGI("Disconnect called");
210 
211     size_t expectedArgsCount = ARGS_SIZE_ONE;
212     size_t argc = expectedArgsCount;
213     napi_value argv[ARGS_SIZE_ONE] = {0};
214     napi_value thisVar = nullptr;
215 
216     napi_value ret = nullptr;
217     napi_get_undefined(env, &ret);
218 
219     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
220     if (argc != expectedArgsCount) {
221         HILOGE("Requires 1 argument.");
222         return ret;
223     }
224     string deviceId;
225     if (!ParseString(env, deviceId, argv[PARAM0])) {
226         HILOGE("string expected.");
227         return ret;
228     }
229 
230     AvrcpController *profile = AvrcpController::GetProfile();
231     BluetoothRemoteDevice device(deviceId, 1);
232     bool res = profile->Disconnect(device);
233 
234     napi_value result = nullptr;
235     napi_get_boolean(env, res, &result);
236     return result;
237 }
238 } // namespace Bluetooth
239 } // namespace OHOS
240