• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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_hid_host_proxy.h"
16 
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_log.h"
19 #include "refbase.h"
20 
21 namespace OHOS {
22 namespace Bluetooth {
Connect(const BluetoothRawAddress & device)23 int32_t BluetoothHidHostProxy::Connect(const BluetoothRawAddress &device)
24 {
25     MessageParcel data;
26     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
27         HILOGE("BluetoothHidHostProxy::Connect WriteInterfaceToken error");
28         return BT_ERR_IPC_TRANS_FAILED;
29     }
30     if (!data.WriteParcelable(&device)) {
31         HILOGE("BluetoothHidHostProxy::Connect write device error");
32         return BT_ERR_IPC_TRANS_FAILED;
33     }
34 
35     MessageParcel reply;
36     MessageOption option {
37         MessageOption::TF_SYNC
38     };
39 
40     int error = Remote()->SendRequest(COMMAND_CONNECT, data, reply, option);
41     if (error != BT_SUCCESS) {
42         HILOGE("BluetoothHidHostProxy::Connect done fail, error: %{public}d", error);
43         return BT_ERR_IPC_TRANS_FAILED;
44     }
45 
46     return reply.ReadInt32();
47 }
48 
Disconnect(const BluetoothRawAddress & device)49 int32_t BluetoothHidHostProxy::Disconnect(const BluetoothRawAddress &device)
50 {
51     MessageParcel data;
52     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
53         HILOGE("BluetoothHidHostProxy::Disconnect WriteInterfaceToken error");
54         return BT_ERR_IPC_TRANS_FAILED;
55     }
56     if (!data.WriteParcelable(&device)) {
57         HILOGE("BluetoothHidHostProxy::Disconnect write device error");
58         return BT_ERR_IPC_TRANS_FAILED;
59     }
60 
61     MessageParcel reply;
62     MessageOption option {
63         MessageOption::TF_SYNC
64     };
65 
66     int error = Remote()->SendRequest(COMMAND_DISCONNECT, data, reply, option);
67     if (error != BT_SUCCESS) {
68         HILOGE("BluetoothHidHostProxy::Disconnect done fail, error: %{public}d", error);
69         return BT_ERR_IPC_TRANS_FAILED;
70     }
71 
72     return reply.ReadInt32();
73 }
74 
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)75 int32_t BluetoothHidHostProxy::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
76 {
77     MessageParcel data;
78     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
79         HILOGE("BluetoothHidHostProxy::GetDeviceState WriteInterfaceToken error");
80         return BT_ERR_IPC_TRANS_FAILED;
81     }
82     if (!data.WriteParcelable(&device)) {
83         HILOGE("BluetoothHidHostProxy::GetDeviceState write device error");
84         return BT_ERR_IPC_TRANS_FAILED;
85     }
86 
87     MessageParcel reply;
88     MessageOption option {
89         MessageOption::TF_SYNC
90     };
91 
92     int error = Remote()->SendRequest(COMMAND_GET_DEVICE_STATE, data, reply, option);
93     if (error != BT_SUCCESS) {
94         HILOGE("BluetoothHidHostProxy::GetDeviceState done fail, error: %{public}d", error);
95         return BT_ERR_IPC_TRANS_FAILED;
96     }
97 
98     // read error code
99     int32_t errCode = reply.ReadInt32();
100     if (errCode != BT_SUCCESS) {
101         HILOGE("reply errCode: %{public}d", errCode);
102         return errCode;
103     }
104 
105     // read state
106     state = reply.ReadInt32();
107     return BT_SUCCESS;
108 }
109 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)110 int32_t BluetoothHidHostProxy::GetDevicesByStates(const std::vector<int32_t> &states,
111     std::vector<BluetoothRawAddress>& result)
112 {
113     MessageParcel data;
114     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
115         HILOGE("BluetoothHidHostProxy::GetDevicesByStates WriteInterfaceToken error");
116         return BT_ERR_IPC_TRANS_FAILED;
117     }
118     if (!WriteParcelableInt32Vector(states, data)) {
119         HILOGE("[GetDevicesByStates] fail: write result failed");
120         return BT_ERR_IPC_TRANS_FAILED;
121     }
122 
123     MessageParcel reply;
124     MessageOption option = {MessageOption::TF_SYNC};
125     int error = Remote()->SendRequest(COMMAND_GET_DEVICES_BY_STATES, data, reply, option);
126     if (error != BT_SUCCESS) {
127         HILOGE("BluetoothHidHostProxy::GetDevicesByStates done fail, error: %{public}d", error);
128         return BT_ERR_IPC_TRANS_FAILED;
129     }
130     // read error code
131     int32_t errCode = reply.ReadInt32();
132     if (errCode != BT_SUCCESS) {
133         HILOGE("reply errCode: %{public}d", errCode);
134         return errCode;
135     }
136 
137     // read size
138     int32_t rawAddsSize = reply.ReadInt32();
139 
140     // read devices
141     for (int i = 0; i < rawAddsSize; i++) {
142         std::unique_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
143         result.push_back(*address);
144     }
145     return BT_SUCCESS;
146 }
147 
RegisterObserver(const sptr<IBluetoothHidHostObserver> observer)148 ErrCode BluetoothHidHostProxy::RegisterObserver(
149     const sptr<IBluetoothHidHostObserver> observer)
150 {
151     MessageParcel data;
152     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
153         HILOGE("BluetoothHidHostProxy::RegisterObserver WriteInterfaceToken error");
154         return IPC_PROXY_TRANSACTION_ERR;
155     }
156     if (!data.WriteRemoteObject(observer->AsObject())) {
157         HILOGE("BluetoothHidHostProxy::RegisterObserver error");
158         return INVALID_DATA;
159     }
160 
161     MessageParcel reply;
162     MessageOption option {
163         MessageOption::TF_ASYNC
164     };
165 
166     int error = Remote()->SendRequest(COMMAND_REGISTER_OBSERVER, data, reply, option);
167     if (error != NO_ERROR) {
168         HILOGE("BluetoothHidHostProxy::RegisterObserver done fail, error: %{public}d", error);
169         return INVALID_DATA;
170     }
171     return error;
172 }
173 
DeregisterObserver(const sptr<IBluetoothHidHostObserver> observer)174 ErrCode BluetoothHidHostProxy::DeregisterObserver(
175     const sptr<IBluetoothHidHostObserver> observer)
176 {
177     MessageParcel data;
178     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
179         HILOGE("BluetoothHidHostProxy::DeregisterObserver WriteInterfaceToken error");
180         return IPC_PROXY_TRANSACTION_ERR;
181     }
182     if (!data.WriteRemoteObject(observer->AsObject())) {
183         HILOGE("BluetoothHidHostProxy::DeregisterObserver error");
184         return INVALID_DATA;
185     }
186 
187     MessageParcel reply;
188     MessageOption option {
189         MessageOption::TF_ASYNC
190     };
191 
192     int error = Remote()->SendRequest(COMMAND_DEREGISTER_OBSERVER, data, reply, option);
193     if (error != NO_ERROR) {
194         HILOGE("BluetoothHidHostProxy::DeregisterObserver done fail, error: %{public}d", error);
195         return INVALID_DATA;
196     }
197     return error;
198 }
199 
HidHostVCUnplug(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)200 ErrCode BluetoothHidHostProxy::HidHostVCUnplug(std::string &device,
201     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
202 {
203     MessageParcel data;
204     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
205         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug WriteInterfaceToken error");
206         return IPC_PROXY_TRANSACTION_ERR;
207     }
208 
209     if (!data.WriteString(device)) {
210         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug error");
211         return INVALID_DATA;
212     }
213 
214     if (!data.WriteUint8(id)) {
215         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug error");
216         return INVALID_DATA;
217     }
218 
219     if (!data.WriteUint16(size)) {
220         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug error");
221         return INVALID_DATA;
222     }
223 
224     if (!data.WriteUint8(type)) {
225         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug error");
226         return INVALID_DATA;
227     }
228 
229     MessageParcel reply;
230     MessageOption option {
231         MessageOption::TF_SYNC
232     };
233 
234     int error = Remote()->SendRequest(COMMAND_VCUN_PLUG, data, reply, option);
235     if (error != NO_ERROR) {
236         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug done fail, error: %{public}d", error);
237         return error;
238     }
239     result = reply.ReadInt32();
240     return ERR_OK;
241 }
242 
HidHostSendData(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)243 ErrCode BluetoothHidHostProxy::HidHostSendData(std::string &device,
244     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
245 {
246     MessageParcel data;
247     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
248         HILOGE("BluetoothHidHostProxy::HidHostSendData WriteInterfaceToken error");
249         return IPC_PROXY_TRANSACTION_ERR;
250     }
251 
252     if (!data.WriteString(device)) {
253         HILOGE("BluetoothHidHostProxy::HidHostSendData error");
254         return INVALID_DATA;
255     }
256 
257     if (!data.WriteUint8(id)) {
258         HILOGE("BluetoothHidHostProxy::HidHostSendData error");
259         return INVALID_DATA;
260     }
261 
262     if (!data.WriteUint16(size)) {
263         HILOGE("BluetoothHidHostProxy::HidHostSendData error");
264         return INVALID_DATA;
265     }
266 
267     if (!data.WriteUint8(type)) {
268         HILOGE("BluetoothHidHostProxy::HidHostSendData error");
269         return INVALID_DATA;
270     }
271 
272     MessageParcel reply;
273     MessageOption option {
274         MessageOption::TF_SYNC
275     };
276 
277     int error = Remote()->SendRequest(COMMAND_SEND_DATA, data, reply, option);
278     if (error != NO_ERROR) {
279         HILOGE("BluetoothHidHostProxy::HidHostSendData done fail, error: %{public}d", error);
280         return error;
281     }
282     result = reply.ReadInt32();
283     return ERR_OK;
284 }
285 
HidHostSetReport(std::string & device,uint8_t & type,uint16_t & size,uint8_t & report,int & result)286 ErrCode BluetoothHidHostProxy::HidHostSetReport(std::string &device,
287     uint8_t &type, uint16_t &size, uint8_t &report, int& result)
288 {
289     MessageParcel data;
290     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
291         HILOGE("BluetoothHidHostProxy::HidHostSetReport WriteInterfaceToken error");
292         return IPC_PROXY_TRANSACTION_ERR;
293     }
294 
295     if (!data.WriteString(device)) {
296         HILOGE("BluetoothHidHostProxy::HidHostSetReport error");
297         return INVALID_DATA;
298     }
299 
300     if (!data.WriteUint8(type)) {
301         HILOGE("BluetoothHidHostProxy::HidHostSetReport error");
302         return INVALID_DATA;
303     }
304 
305     if (!data.WriteUint16(size)) {
306         HILOGE("BluetoothHidHostProxy::HidHostSetReport error");
307         return INVALID_DATA;
308     }
309 
310     if (!data.WriteUint8(report)) {
311         HILOGE("BluetoothHidHostProxy::HidHostSetReport error");
312         return INVALID_DATA;
313     }
314 
315     MessageParcel reply;
316     MessageOption option {
317         MessageOption::TF_SYNC
318     };
319 
320     int error = Remote()->SendRequest(COMMAND_SET_REPORT, data, reply, option);
321     if (error != NO_ERROR) {
322         HILOGE("BluetoothHidHostProxy::HidHostSetReport done fail, error: %{public}d", error);
323         return error;
324     }
325     result = reply.ReadInt32();
326     return ERR_OK;
327 }
328 
HidHostGetReport(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)329 ErrCode BluetoothHidHostProxy::HidHostGetReport(std::string &device,
330     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
331 {
332     MessageParcel data;
333     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
334         HILOGE("BluetoothHidHostProxy::HidHostGetReport WriteInterfaceToken error");
335         return IPC_PROXY_TRANSACTION_ERR;
336     }
337 
338     if (!data.WriteString(device)) {
339         HILOGE("BluetoothHidHostProxy::HidHostGetReport error");
340         return INVALID_DATA;
341     }
342 
343     if (!data.WriteUint8(id)) {
344         HILOGE("BluetoothHidHostProxy::HidHostGetReport error");
345         return INVALID_DATA;
346     }
347 
348     if (!data.WriteUint16(size)) {
349         HILOGE("BluetoothHidHostProxy::HidHostGetReport error");
350         return INVALID_DATA;
351     }
352 
353     if (!data.WriteUint8(type)) {
354         HILOGE("BluetoothHidHostProxy::HidHostGetReport error");
355         return INVALID_DATA;
356     }
357 
358     MessageParcel reply;
359     MessageOption option {
360         MessageOption::TF_SYNC
361     };
362 
363     int error = Remote()->SendRequest(COMMAND_GET_REPORT, data, reply, option);
364     if (error != NO_ERROR) {
365         HILOGE("BluetoothHidHostProxy::HidHostGetReport done fail, error: %{public}d", error);
366         return error;
367     }
368     result = reply.ReadInt32();
369     return ERR_OK;
370 }
371 
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)372 bool BluetoothHidHostProxy::WriteParcelableInt32Vector(
373     const std::vector<int32_t> &parcelableVector, Parcel &reply)
374 {
375     if (!reply.WriteInt32(parcelableVector.size())) {
376         HILOGE("write ParcelableVector failed");
377         return false;
378     }
379 
380     for (auto parcelable : parcelableVector) {
381         if (!reply.WriteInt32(parcelable)) {
382             HILOGE("write ParcelableVector failed");
383             return false;
384         }
385     }
386     return true;
387 }
388 } // Bluetooth
389 } // OHOS
390