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 "bluetooth_errorcode.h"
17 #include "napi_bluetooth_avrcp_ct.h"
18 #include "napi_bluetooth_profile.h"
19 #include "napi_bluetooth_event.h"
20 #include "napi_bluetooth_error.h"
21 
22 namespace OHOS {
23 namespace Bluetooth {
24 using namespace std;
25 
26 std::shared_ptr<NapiAvrcpControllerObserver> NapiAvrcpController::observer_ =
27     std::make_shared<NapiAvrcpControllerObserver>();
28 bool NapiAvrcpController::isRegistered_ = false;
29 
DefineAvrcpControllerJSClass(napi_env env)30 void NapiAvrcpController::DefineAvrcpControllerJSClass(napi_env env)
31 {
32     napi_value constructor;
33     napi_property_descriptor properties[] = {
34         DECLARE_NAPI_FUNCTION("connect", Connect),
35         DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
36         DECLARE_NAPI_FUNCTION("on", On),
37         DECLARE_NAPI_FUNCTION("off", Off),
38         DECLARE_NAPI_FUNCTION("getConnectionDevices", GetConnectionDevices),
39         DECLARE_NAPI_FUNCTION("getDeviceState", GetDeviceState),
40     };
41 
42     napi_define_class(env, "AvrcpController", NAPI_AUTO_LENGTH, AvrcpControllerConstructor, nullptr,
43         sizeof(properties) / sizeof(properties[0]), properties, &constructor);
44     napi_value napiProfile;
45     napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
46     NapiProfile::SetProfile(env, ProfileId::PROFILE_AVRCP_CT, napiProfile);
47 }
48 
AvrcpControllerConstructor(napi_env env, napi_callback_info info)49 napi_value NapiAvrcpController::AvrcpControllerConstructor(napi_env env, napi_callback_info info)
50 {
51     napi_value thisVar = nullptr;
52     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
53     return thisVar;
54 }
55 
On(napi_env env, napi_callback_info info)56 napi_value NapiAvrcpController::On(napi_env env, napi_callback_info info)
57 {
58     if (observer_) {
59         auto status = observer_->eventSubscribe_.Register(env, info);
60         NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
61     }
62 
63     if (!isRegistered_) {
64         AvrcpController *profile = AvrcpController::GetProfile();
65         profile->RegisterObserver(observer_);
66         isRegistered_ = true;
67     }
68     HILOGI("Napi Avrcp is registered");
69     return NapiGetUndefinedRet(env);
70 }
71 
Off(napi_env env, napi_callback_info info)72 napi_value NapiAvrcpController::Off(napi_env env, napi_callback_info info)
73 {
74     if (observer_) {
75         auto status = observer_->eventSubscribe_.Deregister(env, info);
76         NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
77     }
78     return NapiGetUndefinedRet(env);
79 }
80 
GetConnectionDevices(napi_env env, napi_callback_info info)81 napi_value NapiAvrcpController::GetConnectionDevices(napi_env env, napi_callback_info info)
82 {
83     HILOGI("enter");
84     napi_value ret = nullptr;
85     napi_create_array(env, &ret);
86     AvrcpController *profile = AvrcpController::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 NapiAvrcpController::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     AvrcpController *profile = AvrcpController::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 
130 
Connect(napi_env env, napi_callback_info info)131 napi_value NapiAvrcpController::Connect(napi_env env, napi_callback_info info)
132 {
133     HILOGI("enter");
134     size_t expectedArgsCount = ARGS_SIZE_ONE;
135     size_t argc = expectedArgsCount;
136     napi_value argv[ARGS_SIZE_ONE] = {0};
137     napi_value thisVar = nullptr;
138 
139     napi_value ret = nullptr;
140     napi_get_undefined(env, &ret);
141 
142     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
143     if (argc != expectedArgsCount) {
144         HILOGE("Requires 1 argument.");
145         return ret;
146     }
147     string deviceId;
148     if (!ParseString(env, deviceId, argv[PARAM0])) {
149         HILOGE("string expected.");
150         return ret;
151     }
152 
153     AvrcpController *profile = AvrcpController::GetProfile();
154     BluetoothRemoteDevice device(deviceId, 1);
155     bool res = profile->Connect(device);
156 
157     napi_value result = nullptr;
158     napi_get_boolean(env, res, &result);
159     HILOGI("res: %{public}d", res);
160     return result;
161 }
162 
Disconnect(napi_env env, napi_callback_info info)163 napi_value NapiAvrcpController::Disconnect(napi_env env, napi_callback_info info)
164 {
165     HILOGI("enter");
166     size_t expectedArgsCount = ARGS_SIZE_ONE;
167     size_t argc = expectedArgsCount;
168     napi_value argv[ARGS_SIZE_ONE] = {0};
169     napi_value thisVar = nullptr;
170 
171     napi_value ret = nullptr;
172     napi_get_undefined(env, &ret);
173 
174     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
175     if (argc != expectedArgsCount) {
176         HILOGE("Requires 1 argument.");
177         return ret;
178     }
179     string deviceId;
180     if (!ParseString(env, deviceId, argv[PARAM0])) {
181         HILOGE("string expected.");
182         return ret;
183     }
184 
185     AvrcpController *profile = AvrcpController::GetProfile();
186     BluetoothRemoteDevice device(deviceId, 1);
187     bool res = profile->Disconnect(device);
188 
189     napi_value result = nullptr;
190     napi_get_boolean(env, res, &result);
191     HILOGI("res: %{public}d", res);
192     return result;
193 }
194 } // namespace Bluetooth
195 } // namespace OHOS
196