• 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(
41         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_CONNECT), data, reply, option);
42     if (error != BT_NO_ERROR) {
43         HILOGE("BluetoothHidHostProxy::Connect done fail, error: %{public}d", error);
44         return BT_ERR_IPC_TRANS_FAILED;
45     }
46 
47     return reply.ReadInt32();
48 }
49 
Disconnect(const BluetoothRawAddress & device)50 int32_t BluetoothHidHostProxy::Disconnect(const BluetoothRawAddress &device)
51 {
52     MessageParcel data;
53     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
54         HILOGE("BluetoothHidHostProxy::Disconnect WriteInterfaceToken error");
55         return BT_ERR_IPC_TRANS_FAILED;
56     }
57     if (!data.WriteParcelable(&device)) {
58         HILOGE("BluetoothHidHostProxy::Disconnect write device error");
59         return BT_ERR_IPC_TRANS_FAILED;
60     }
61 
62     MessageParcel reply;
63     MessageOption option {
64         MessageOption::TF_SYNC
65     };
66 
67     int error = Remote()->SendRequest(
68         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_DISCONNECT), data, reply, option);
69     if (error != BT_NO_ERROR) {
70         HILOGE("BluetoothHidHostProxy::Disconnect done fail, error: %{public}d", error);
71         return BT_ERR_IPC_TRANS_FAILED;
72     }
73 
74     return reply.ReadInt32();
75 }
76 
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)77 int32_t BluetoothHidHostProxy::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
78 {
79     MessageParcel data;
80     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
81         HILOGE("BluetoothHidHostProxy::GetDeviceState WriteInterfaceToken error");
82         return BT_ERR_IPC_TRANS_FAILED;
83     }
84     if (!data.WriteParcelable(&device)) {
85         HILOGE("BluetoothHidHostProxy::GetDeviceState write device error");
86         return BT_ERR_IPC_TRANS_FAILED;
87     }
88 
89     MessageParcel reply;
90     MessageOption option {
91         MessageOption::TF_SYNC
92     };
93 
94     int error = Remote()->SendRequest(
95         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_DEVICE_STATE), data, reply, option);
96     if (error != BT_NO_ERROR) {
97         HILOGE("BluetoothHidHostProxy::GetDeviceState done fail, error: %{public}d", error);
98         return BT_ERR_IPC_TRANS_FAILED;
99     }
100 
101     // read error code
102     int32_t errCode = reply.ReadInt32();
103     if (errCode != BT_NO_ERROR) {
104         HILOGE("reply errCode: %{public}d", errCode);
105         return errCode;
106     }
107 
108     // read state
109     state = reply.ReadInt32();
110     return BT_NO_ERROR;
111 }
112 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)113 int32_t BluetoothHidHostProxy::GetDevicesByStates(const std::vector<int32_t> &states,
114     std::vector<BluetoothRawAddress>& result)
115 {
116     MessageParcel data;
117     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
118         HILOGE("BluetoothHidHostProxy::GetDevicesByStates WriteInterfaceToken error");
119         return BT_ERR_IPC_TRANS_FAILED;
120     }
121     if (!WriteParcelableInt32Vector(states, data)) {
122         HILOGE("[GetDevicesByStates] fail: write result failed");
123         return BT_ERR_IPC_TRANS_FAILED;
124     }
125 
126     MessageParcel reply;
127     MessageOption option = {MessageOption::TF_SYNC};
128     int error = Remote()->SendRequest(
129         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_DEVICES_BY_STATES), data, reply, option);
130     if (error != BT_NO_ERROR) {
131         HILOGE("BluetoothHidHostProxy::GetDevicesByStates done fail, error: %{public}d", error);
132         return BT_ERR_IPC_TRANS_FAILED;
133     }
134     // read error code
135     int32_t errCode = reply.ReadInt32();
136     if (errCode != BT_NO_ERROR) {
137         HILOGE("reply errCode: %{public}d", errCode);
138         return errCode;
139     }
140 
141     // read size
142     int32_t rawAddsSize = reply.ReadInt32();
143 
144     // read devices
145     for (int i = 0; i < rawAddsSize; i++) {
146         std::unique_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
147         result.push_back(*address);
148     }
149     return BT_NO_ERROR;
150 }
151 
RegisterObserver(const sptr<IBluetoothHidHostObserver> observer)152 ErrCode BluetoothHidHostProxy::RegisterObserver(
153     const sptr<IBluetoothHidHostObserver> observer)
154 {
155     MessageParcel data;
156     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
157         HILOGE("BluetoothHidHostProxy::RegisterObserver WriteInterfaceToken error");
158         return IPC_PROXY_TRANSACTION_ERR;
159     }
160     if (!data.WriteRemoteObject(observer->AsObject())) {
161         HILOGE("BluetoothHidHostProxy::RegisterObserver error");
162         return INVALID_DATA;
163     }
164 
165     MessageParcel reply;
166     MessageOption option {
167         MessageOption::TF_ASYNC
168     };
169 
170     int error = Remote()->SendRequest(
171         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_REGISTER_OBSERVER), data, reply, option);
172     if (error != NO_ERROR) {
173         HILOGE("BluetoothHidHostProxy::RegisterObserver done fail, error: %{public}d", error);
174         return INVALID_DATA;
175     }
176     return error;
177 }
178 
DeregisterObserver(const sptr<IBluetoothHidHostObserver> observer)179 ErrCode BluetoothHidHostProxy::DeregisterObserver(
180     const sptr<IBluetoothHidHostObserver> observer)
181 {
182     MessageParcel data;
183     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
184         HILOGE("BluetoothHidHostProxy::DeregisterObserver WriteInterfaceToken error");
185         return IPC_PROXY_TRANSACTION_ERR;
186     }
187     if (!data.WriteRemoteObject(observer->AsObject())) {
188         HILOGE("BluetoothHidHostProxy::DeregisterObserver error");
189         return INVALID_DATA;
190     }
191 
192     MessageParcel reply;
193     MessageOption option {
194         MessageOption::TF_ASYNC
195     };
196 
197     int error = Remote()->SendRequest(
198         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_DEREGISTER_OBSERVER), data, reply, option);
199     if (error != NO_ERROR) {
200         HILOGE("BluetoothHidHostProxy::DeregisterObserver done fail, error: %{public}d", error);
201         return INVALID_DATA;
202     }
203     return error;
204 }
205 
HidHostVCUnplug(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)206 ErrCode BluetoothHidHostProxy::HidHostVCUnplug(std::string &device,
207     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
208 {
209     MessageParcel data;
210     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
211         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug WriteInterfaceToken error");
212         return IPC_PROXY_TRANSACTION_ERR;
213     }
214 
215     if (!data.WriteString(device)) {
216         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug error");
217         return INVALID_DATA;
218     }
219 
220     if (!data.WriteUint8(id)) {
221         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug error");
222         return INVALID_DATA;
223     }
224 
225     if (!data.WriteUint16(size)) {
226         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug error");
227         return INVALID_DATA;
228     }
229 
230     if (!data.WriteUint8(type)) {
231         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug error");
232         return INVALID_DATA;
233     }
234 
235     MessageParcel reply;
236     MessageOption option {
237         MessageOption::TF_SYNC
238     };
239 
240     int error = Remote()->SendRequest(
241         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_VCUN_PLUG), data, reply, option);
242     if (error != NO_ERROR) {
243         HILOGE("BluetoothHidHostProxy::HidHostVCUnplug done fail, error: %{public}d", error);
244         return error;
245     }
246     result = reply.ReadInt32();
247     return ERR_OK;
248 }
249 
HidHostSendData(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)250 ErrCode BluetoothHidHostProxy::HidHostSendData(std::string &device,
251     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
252 {
253     MessageParcel data;
254     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
255         HILOGE("BluetoothHidHostProxy::HidHostSendData WriteInterfaceToken error");
256         return IPC_PROXY_TRANSACTION_ERR;
257     }
258 
259     if (!data.WriteString(device)) {
260         HILOGE("BluetoothHidHostProxy::HidHostSendData error");
261         return INVALID_DATA;
262     }
263 
264     if (!data.WriteUint8(id)) {
265         HILOGE("BluetoothHidHostProxy::HidHostSendData error");
266         return INVALID_DATA;
267     }
268 
269     if (!data.WriteUint16(size)) {
270         HILOGE("BluetoothHidHostProxy::HidHostSendData error");
271         return INVALID_DATA;
272     }
273 
274     if (!data.WriteUint8(type)) {
275         HILOGE("BluetoothHidHostProxy::HidHostSendData error");
276         return INVALID_DATA;
277     }
278 
279     MessageParcel reply;
280     MessageOption option {
281         MessageOption::TF_SYNC
282     };
283 
284     int error = Remote()->SendRequest(
285         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SEND_DATA), data, reply, option);
286     if (error != NO_ERROR) {
287         HILOGE("BluetoothHidHostProxy::HidHostSendData done fail, error: %{public}d", error);
288         return error;
289     }
290     result = reply.ReadInt32();
291     return ERR_OK;
292 }
293 
HidHostSetReport(std::string & device,uint8_t & type,uint16_t & size,uint8_t & report,int & result)294 ErrCode BluetoothHidHostProxy::HidHostSetReport(std::string &device,
295     uint8_t &type, uint16_t &size, uint8_t &report, int& result)
296 {
297     MessageParcel data;
298     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
299         HILOGE("BluetoothHidHostProxy::HidHostSetReport WriteInterfaceToken error");
300         return IPC_PROXY_TRANSACTION_ERR;
301     }
302 
303     if (!data.WriteString(device)) {
304         HILOGE("BluetoothHidHostProxy::HidHostSetReport error");
305         return INVALID_DATA;
306     }
307 
308     if (!data.WriteUint8(type)) {
309         HILOGE("BluetoothHidHostProxy::HidHostSetReport error");
310         return INVALID_DATA;
311     }
312 
313     if (!data.WriteUint16(size)) {
314         HILOGE("BluetoothHidHostProxy::HidHostSetReport error");
315         return INVALID_DATA;
316     }
317 
318     if (!data.WriteUint8(report)) {
319         HILOGE("BluetoothHidHostProxy::HidHostSetReport error");
320         return INVALID_DATA;
321     }
322 
323     MessageParcel reply;
324     MessageOption option {
325         MessageOption::TF_SYNC
326     };
327 
328     int error = Remote()->SendRequest(
329         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SET_REPORT), data, reply, option);
330     if (error != NO_ERROR) {
331         HILOGE("BluetoothHidHostProxy::HidHostSetReport done fail, error: %{public}d", error);
332         return error;
333     }
334     result = reply.ReadInt32();
335     return ERR_OK;
336 }
337 
HidHostGetReport(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)338 ErrCode BluetoothHidHostProxy::HidHostGetReport(std::string &device,
339     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
340 {
341     MessageParcel data;
342     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
343         HILOGE("BluetoothHidHostProxy::HidHostGetReport WriteInterfaceToken error");
344         return IPC_PROXY_TRANSACTION_ERR;
345     }
346 
347     if (!data.WriteString(device)) {
348         HILOGE("BluetoothHidHostProxy::HidHostGetReport error");
349         return INVALID_DATA;
350     }
351 
352     if (!data.WriteUint8(id)) {
353         HILOGE("BluetoothHidHostProxy::HidHostGetReport error");
354         return INVALID_DATA;
355     }
356 
357     if (!data.WriteUint16(size)) {
358         HILOGE("BluetoothHidHostProxy::HidHostGetReport error");
359         return INVALID_DATA;
360     }
361 
362     if (!data.WriteUint8(type)) {
363         HILOGE("BluetoothHidHostProxy::HidHostGetReport error");
364         return INVALID_DATA;
365     }
366 
367     MessageParcel reply;
368     MessageOption option {
369         MessageOption::TF_SYNC
370     };
371 
372     int error = Remote()->SendRequest(
373         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_REPORT), data, reply, option);
374     if (error != NO_ERROR) {
375         HILOGE("BluetoothHidHostProxy::HidHostGetReport done fail, error: %{public}d", error);
376         return error;
377     }
378     result = reply.ReadInt32();
379     return ERR_OK;
380 }
381 
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)382 bool BluetoothHidHostProxy::WriteParcelableInt32Vector(
383     const std::vector<int32_t> &parcelableVector, Parcel &reply)
384 {
385     if (!reply.WriteInt32(parcelableVector.size())) {
386         HILOGE("write ParcelableVector failed");
387         return false;
388     }
389 
390     for (auto parcelable : parcelableVector) {
391         if (!reply.WriteInt32(parcelable)) {
392             HILOGE("write ParcelableVector failed");
393             return false;
394         }
395     }
396     return true;
397 }
398 
SetConnectStrategy(const BluetoothRawAddress & device,int strategy)399 int32_t BluetoothHidHostProxy::SetConnectStrategy(const BluetoothRawAddress &device, int strategy)
400 {
401     MessageParcel data;
402     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
403         HILOGE("WriteInterfaceToken error");
404         return BT_ERR_IPC_TRANS_FAILED;
405     }
406     if (!data.WriteParcelable(&device)) {
407         HILOGE("write device error");
408         return BT_ERR_IPC_TRANS_FAILED;
409     }
410     if (!data.WriteInt32(strategy)) {
411         HILOGE("write strategy error");
412         return BT_ERR_IPC_TRANS_FAILED;
413     }
414 
415     MessageParcel reply;
416     MessageOption option {
417         MessageOption::TF_SYNC
418     };
419 
420     int error = Remote()->SendRequest(
421         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SET_CONNECT_STRATEGY), data, reply, option);
422     if (error != NO_ERROR) {
423         HILOGE("BluetoothHfpAgProxy::SetConnectStrategy done fail, error: %{public}d", error);
424         return BT_ERR_IPC_TRANS_FAILED;
425     }
426 
427     return reply.ReadInt32();
428 }
429 
GetConnectStrategy(const BluetoothRawAddress & device,int & strategy)430 int32_t BluetoothHidHostProxy::GetConnectStrategy(const BluetoothRawAddress &device, int &strategy)
431 {
432     MessageParcel data;
433     if (!data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor())) {
434         HILOGE("WriteInterfaceToken error");
435         return BT_ERR_IPC_TRANS_FAILED;
436     }
437     if (!data.WriteParcelable(&device)) {
438         HILOGE("write device error");
439         return BT_ERR_IPC_TRANS_FAILED;
440     }
441 
442     MessageParcel reply;
443     MessageOption option {
444         MessageOption::TF_SYNC
445     };
446 
447     int error = Remote()->SendRequest(
448         static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_CONNECT_STRATEGY), data, reply, option);
449     if (error != NO_ERROR) {
450         HILOGE("BluetoothHfpAgProxy::GetConnectStrategy done fail, error: %{public}d", error);
451         return BT_ERR_IPC_TRANS_FAILED;
452     }
453 
454     int32_t res = reply.ReadInt32();
455     if (res == NO_ERROR) {
456         strategy = reply.ReadInt32();
457     }
458 
459     return res;
460 }
461 } // Bluetooth
462 } // OHOS
463