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