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 result.push_back(*address);
108 }
109 return BT_NO_ERROR;
110 }
111
RegisterObserver(const sptr<IBluetoothHidHostObserver> observer)112 int32_t BluetoothHidHostProxy::RegisterObserver(
113 const sptr<IBluetoothHidHostObserver> observer)
114 {
115 MessageParcel data;
116 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
117 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
118 CHECK_AND_RETURN_LOG_RET(
119 data.WriteRemoteObject(observer->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object error");
120
121 MessageParcel reply;
122 MessageOption option(MessageOption::TF_ASYNC);
123
124 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_REGISTER_OBSERVER),
125 data, reply, option, INVALID_DATA);
126
127 return BT_NO_ERROR;
128 }
129
DeregisterObserver(const sptr<IBluetoothHidHostObserver> observer)130 int32_t BluetoothHidHostProxy::DeregisterObserver(
131 const sptr<IBluetoothHidHostObserver> observer)
132 {
133 MessageParcel data;
134 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
135 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
136 CHECK_AND_RETURN_LOG_RET(
137 data.WriteRemoteObject(observer->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object error");
138
139 MessageParcel reply;
140 MessageOption option(MessageOption::TF_ASYNC);
141
142 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_DEREGISTER_OBSERVER),
143 data, reply, option, INVALID_DATA);
144
145 return BT_NO_ERROR;
146 }
147
HidHostVCUnplug(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)148 int32_t BluetoothHidHostProxy::HidHostVCUnplug(std::string &device,
149 uint8_t &id, uint16_t &size, uint8_t &type, int& result)
150 {
151 MessageParcel data;
152 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
153 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
154 CHECK_AND_RETURN_LOG_RET(data.WriteString(device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
155 CHECK_AND_RETURN_LOG_RET(data.WriteUint8(id), BT_ERR_IPC_TRANS_FAILED, "Write id error");
156 CHECK_AND_RETURN_LOG_RET(data.WriteUint16(size), BT_ERR_IPC_TRANS_FAILED, "Write size error");
157 CHECK_AND_RETURN_LOG_RET(data.WriteUint8(type), BT_ERR_IPC_TRANS_FAILED, "Write type error");
158
159 MessageParcel reply;
160 MessageOption option(MessageOption::TF_SYNC);
161
162 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_VCUN_PLUG),
163 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
164
165 result = reply.ReadInt32();
166 return BT_NO_ERROR;
167 }
168
HidHostSendData(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)169 int32_t BluetoothHidHostProxy::HidHostSendData(std::string &device,
170 uint8_t &id, uint16_t &size, uint8_t &type, int& result)
171 {
172 MessageParcel data;
173 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
174 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
175 CHECK_AND_RETURN_LOG_RET(data.WriteString(device), BT_ERR_IPC_TRANS_FAILED, "write device errorr");
176 CHECK_AND_RETURN_LOG_RET(data.WriteUint8(id), BT_ERR_IPC_TRANS_FAILED, "write id error");
177 CHECK_AND_RETURN_LOG_RET(data.WriteUint16(size), BT_ERR_IPC_TRANS_FAILED, "write size error");
178 CHECK_AND_RETURN_LOG_RET(data.WriteUint8(type), BT_ERR_IPC_TRANS_FAILED, "write type error");
179
180 MessageParcel reply;
181 MessageOption option(MessageOption::TF_SYNC);
182
183 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SEND_DATA),
184 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
185
186 return BT_NO_ERROR;
187 }
188
HidHostSetReport(std::string & device,uint8_t & type,uint16_t & size,uint8_t & report,int & result)189 int32_t BluetoothHidHostProxy::HidHostSetReport(std::string &device,
190 uint8_t &type, uint16_t &size, uint8_t &report, int& result)
191 {
192 MessageParcel data;
193 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
194 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
195 CHECK_AND_RETURN_LOG_RET(data.WriteString(device), BT_ERR_IPC_TRANS_FAILED, "write device error");
196 CHECK_AND_RETURN_LOG_RET(data.WriteUint8(type), BT_ERR_IPC_TRANS_FAILED, "write type error");
197 CHECK_AND_RETURN_LOG_RET(data.WriteUint16(size), BT_ERR_IPC_TRANS_FAILED, "write size error");
198 CHECK_AND_RETURN_LOG_RET(data.WriteUint8(report), BT_ERR_IPC_TRANS_FAILED, "write report error");
199
200 MessageParcel reply;
201 MessageOption option(MessageOption::TF_SYNC);
202
203 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SET_REPORT),
204 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
205
206 result = reply.ReadInt32();
207 return BT_NO_ERROR;
208 }
209
HidHostGetReport(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)210 int32_t BluetoothHidHostProxy::HidHostGetReport(std::string &device,
211 uint8_t &id, uint16_t &size, uint8_t &type, int& result)
212 {
213 MessageParcel data;
214 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
215 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
216 CHECK_AND_RETURN_LOG_RET(data.WriteString(device), BT_ERR_IPC_TRANS_FAILED, "write device error");
217 CHECK_AND_RETURN_LOG_RET(data.WriteUint8(id), BT_ERR_IPC_TRANS_FAILED, "write id error");
218 CHECK_AND_RETURN_LOG_RET(data.WriteUint16(size), BT_ERR_IPC_TRANS_FAILED, "write size error");
219 CHECK_AND_RETURN_LOG_RET(data.WriteUint8(type), BT_ERR_IPC_TRANS_FAILED, "write type error");
220
221 MessageParcel reply;
222 MessageOption option(MessageOption::TF_SYNC);
223
224 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_REPORT),
225 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
226
227 result = reply.ReadInt32();
228 return BT_NO_ERROR;
229 }
230
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)231 bool BluetoothHidHostProxy::WriteParcelableInt32Vector(
232 const std::vector<int32_t> &parcelableVector, Parcel &reply)
233 {
234 if (!reply.WriteInt32(parcelableVector.size())) {
235 HILOGE("write ParcelableVector failed");
236 return false;
237 }
238
239 for (auto parcelable : parcelableVector) {
240 if (!reply.WriteInt32(parcelable)) {
241 HILOGE("write ParcelableVector failed");
242 return false;
243 }
244 }
245 return true;
246 }
247
SetConnectStrategy(const BluetoothRawAddress & device,int strategy)248 int32_t BluetoothHidHostProxy::SetConnectStrategy(const BluetoothRawAddress &device, int strategy)
249 {
250 MessageParcel data;
251 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
252 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
253 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
254 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(strategy), BT_ERR_IPC_TRANS_FAILED, "write strategy error");
255
256 MessageParcel reply;
257 MessageOption option(MessageOption::TF_SYNC);
258
259 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SET_CONNECT_STRATEGY),
260 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
261
262 return reply.ReadInt32();
263 }
264
GetConnectStrategy(const BluetoothRawAddress & device,int & strategy)265 int32_t BluetoothHidHostProxy::GetConnectStrategy(const BluetoothRawAddress &device, int &strategy)
266 {
267 MessageParcel data;
268 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHidHostProxy::GetDescriptor()),
269 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
270 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
271
272 MessageParcel reply;
273 MessageOption option(MessageOption::TF_SYNC);
274
275 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_CONNECT_STRATEGY),
276 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
277
278 int32_t res = reply.ReadInt32();
279 if (res == NO_ERROR) {
280 strategy = reply.ReadInt32();
281 }
282 return res;
283 }
284 } // Bluetooth
285 } // OHOS
286