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     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
27         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
28     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
29 
30     MessageParcel reply;
31     MessageOption option(MessageOption::TF_SYNC);
32 
33     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_CONNECT),
34         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
35 
36     return reply.ReadInt32();
37 }
38 
Disconnect(const BluetoothRawAddress &device)39 int32_t BluetoothHidHostProxy::Disconnect(const BluetoothRawAddress &device)
40 {
41     MessageParcel data;
42     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
43         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
44     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
45 
46     MessageParcel reply;
47     MessageOption option(MessageOption::TF_SYNC);
48 
49     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_DISCONNECT),
50         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
51 
52     return reply.ReadInt32();
53 }
54 
GetDeviceState(const BluetoothRawAddress &device, int32_t &state)55 int32_t BluetoothHidHostProxy::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
56 {
57     MessageParcel data;
58     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
59         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
60     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
61 
62     MessageParcel reply;
63     MessageOption option(MessageOption::TF_SYNC);
64 
65     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_DEVICE_STATE),
66         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
67 
68     // read error code
69     int32_t errCode = reply.ReadInt32();
70     if (errCode != BT_NO_ERROR) {
71         HILOGE("reply errCode: %{public}d", errCode);
72         return errCode;
73     }
74 
75     // read state
76     state = reply.ReadInt32();
77     return BT_NO_ERROR;
78 }
79 
GetDevicesByStates(const std::vector<int32_t> &states, std::vector<BluetoothRawAddress>& result)80 int32_t BluetoothHidHostProxy::GetDevicesByStates(const std::vector<int32_t> &states,
81     std::vector<BluetoothRawAddress>& result)
82 {
83     MessageParcel data;
84     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
85         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
86     CHECK_AND_RETURN_LOG_RET(WriteParcelableInt32Vector(states, data), BT_ERR_IPC_TRANS_FAILED, "write states error");
87 
88     MessageParcel reply;
89     MessageOption option(MessageOption::TF_SYNC);
90 
91     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_DEVICES_BY_STATES),
92         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
93 
94     // read error code
95     int32_t errCode = reply.ReadInt32();
96     if (errCode != BT_NO_ERROR) {
97         HILOGE("reply errCode: %{public}d", errCode);
98         return errCode;
99     }
100 
101     // read size
102     int32_t rawAddsSize = reply.ReadInt32();
103 
104     // read devices
105     for (int i = 0; i < rawAddsSize; i++) {
106         std::unique_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
107         if (!address) {
108             return TRANSACTION_ERR;
109         }
110         result.push_back(*address);
111     }
112     return BT_NO_ERROR;
113 }
114 
RegisterObserver( const sptr<IBluetoothHidHostObserver> observer)115 int32_t BluetoothHidHostProxy::RegisterObserver(
116     const sptr<IBluetoothHidHostObserver> observer)
117 {
118     MessageParcel data;
119     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
120         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
121     CHECK_AND_RETURN_LOG_RET(
122         data.WriteRemoteObject(observer->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object error");
123 
124     MessageParcel reply;
125     MessageOption option(MessageOption::TF_ASYNC);
126 
127     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_REGISTER_OBSERVER),
128         data, reply, option, INVALID_DATA);
129 
130     return BT_NO_ERROR;
131 }
132 
DeregisterObserver( const sptr<IBluetoothHidHostObserver> observer)133 int32_t BluetoothHidHostProxy::DeregisterObserver(
134     const sptr<IBluetoothHidHostObserver> observer)
135 {
136     MessageParcel data;
137     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
138         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
139     CHECK_AND_RETURN_LOG_RET(
140         data.WriteRemoteObject(observer->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object error");
141 
142     MessageParcel reply;
143     MessageOption option(MessageOption::TF_ASYNC);
144 
145     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_DEREGISTER_OBSERVER),
146         data, reply, option, INVALID_DATA);
147 
148     return BT_NO_ERROR;
149 }
150 
HidHostVCUnplug(std::string &device, uint8_t &id, uint16_t &size, uint8_t &type, int& result)151 int32_t BluetoothHidHostProxy::HidHostVCUnplug(std::string &device,
152     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
153 {
154     MessageParcel data;
155     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
156         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
157     CHECK_AND_RETURN_LOG_RET(data.WriteString(device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
158     CHECK_AND_RETURN_LOG_RET(data.WriteUint8(id), BT_ERR_IPC_TRANS_FAILED, "Write id error");
159     CHECK_AND_RETURN_LOG_RET(data.WriteUint16(size), BT_ERR_IPC_TRANS_FAILED, "Write size error");
160     CHECK_AND_RETURN_LOG_RET(data.WriteUint8(type), BT_ERR_IPC_TRANS_FAILED, "Write type error");
161 
162     MessageParcel reply;
163     MessageOption option(MessageOption::TF_SYNC);
164 
165     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_VCUN_PLUG),
166         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
167 
168     result = reply.ReadInt32();
169     return BT_NO_ERROR;
170 }
171 
HidHostSendData(std::string &device, uint8_t &id, uint16_t &size, uint8_t &type, int& result)172 int32_t BluetoothHidHostProxy::HidHostSendData(std::string &device,
173     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
174 {
175     MessageParcel data;
176     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
177         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
178     CHECK_AND_RETURN_LOG_RET(data.WriteString(device), BT_ERR_IPC_TRANS_FAILED, "write device errorr");
179     CHECK_AND_RETURN_LOG_RET(data.WriteUint8(id), BT_ERR_IPC_TRANS_FAILED, "write id error");
180     CHECK_AND_RETURN_LOG_RET(data.WriteUint16(size), BT_ERR_IPC_TRANS_FAILED, "write size error");
181     CHECK_AND_RETURN_LOG_RET(data.WriteUint8(type), BT_ERR_IPC_TRANS_FAILED, "write type error");
182 
183     MessageParcel reply;
184     MessageOption option(MessageOption::TF_SYNC);
185 
186     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SEND_DATA),
187         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
188 
189     return BT_NO_ERROR;
190 }
191 
HidHostSetReport(std::string &device, uint8_t &type, std::string &report, int& result)192 int32_t BluetoothHidHostProxy::HidHostSetReport(std::string &device,
193     uint8_t &type, std::string &report, int& result)
194 {
195     MessageParcel data;
196     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
197         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
198     CHECK_AND_RETURN_LOG_RET(data.WriteString(device), BT_ERR_IPC_TRANS_FAILED, "write device error");
199     CHECK_AND_RETURN_LOG_RET(data.WriteUint8(type), BT_ERR_IPC_TRANS_FAILED, "write type error");
200     CHECK_AND_RETURN_LOG_RET(data.WriteString(report), BT_ERR_IPC_TRANS_FAILED, "write report error");
201 
202     MessageParcel reply;
203     MessageOption option(MessageOption::TF_SYNC);
204 
205     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SET_REPORT),
206         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
207 
208     result = reply.ReadInt32();
209     return BT_NO_ERROR;
210 }
211 
HidHostGetReport(std::string &device, uint8_t &id, uint16_t &size, uint8_t &type, int& result)212 int32_t BluetoothHidHostProxy::HidHostGetReport(std::string &device,
213     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
214 {
215     MessageParcel data;
216     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
217         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
218     CHECK_AND_RETURN_LOG_RET(data.WriteString(device), BT_ERR_IPC_TRANS_FAILED, "write device error");
219     CHECK_AND_RETURN_LOG_RET(data.WriteUint8(id), BT_ERR_IPC_TRANS_FAILED, "write id error");
220     CHECK_AND_RETURN_LOG_RET(data.WriteUint16(size), BT_ERR_IPC_TRANS_FAILED, "write size error");
221     CHECK_AND_RETURN_LOG_RET(data.WriteUint8(type), BT_ERR_IPC_TRANS_FAILED, "write type error");
222 
223     MessageParcel reply;
224     MessageOption option(MessageOption::TF_SYNC);
225 
226     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_REPORT),
227         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
228 
229     result = reply.ReadInt32();
230     return BT_NO_ERROR;
231 }
232 
WriteParcelableInt32Vector( const std::vector<int32_t> &parcelableVector, Parcel &reply)233 bool BluetoothHidHostProxy::WriteParcelableInt32Vector(
234     const std::vector<int32_t> &parcelableVector, Parcel &reply)
235 {
236     if (!reply.WriteInt32(parcelableVector.size())) {
237         HILOGE("write ParcelableVector failed");
238         return false;
239     }
240 
241     for (auto parcelable : parcelableVector) {
242         if (!reply.WriteInt32(parcelable)) {
243             HILOGE("write ParcelableVector failed");
244             return false;
245         }
246     }
247     return true;
248 }
249 
SetConnectStrategy(const BluetoothRawAddress &device, int strategy)250 int32_t BluetoothHidHostProxy::SetConnectStrategy(const BluetoothRawAddress &device, int strategy)
251 {
252     MessageParcel data;
253     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
254         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
255     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
256     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(strategy), BT_ERR_IPC_TRANS_FAILED, "write strategy error");
257 
258     MessageParcel reply;
259     MessageOption option(MessageOption::TF_SYNC);
260 
261     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SET_CONNECT_STRATEGY),
262         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
263 
264     return reply.ReadInt32();
265 }
266 
GetConnectStrategy(const BluetoothRawAddress &device, int &strategy)267 int32_t BluetoothHidHostProxy::GetConnectStrategy(const BluetoothRawAddress &device, int &strategy)
268 {
269     MessageParcel data;
270     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
271         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
272     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
273 
274     MessageParcel reply;
275     MessageOption option(MessageOption::TF_SYNC);
276 
277     SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_CONNECT_STRATEGY),
278         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
279 
280     int32_t res = reply.ReadInt32();
281     if (res == NO_ERROR) {
282         strategy = reply.ReadInt32();
283     }
284     return res;
285 }
286 } // Bluetooth
287 } // OHOS
288