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