1 /*
2 * Copyright (C) 2021-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
16 #include "bluetooth_gatt_client_proxy.h"
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_log.h"
19 #include "parcel_bt_uuid.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
23 const int GATT_CLIENT_READ_DATA_SIZE_MAX_LEN = 0xFF;
RegisterApplication(const sptr<IBluetoothGattClientCallback> & callback,const BluetoothRawAddress & addr,int32_t transport,int & appId)24 int BluetoothGattClientProxy::RegisterApplication(
25 const sptr<IBluetoothGattClientCallback> &callback, const BluetoothRawAddress &addr, int32_t transport, int &appId)
26 {
27 MessageParcel data;
28 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
29 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
30 CHECK_AND_RETURN_LOG_RET(
31 data.WriteRemoteObject(callback->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object error");
32 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&addr), BT_ERR_IPC_TRANS_FAILED, "write addr error");
33 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(transport), BT_ERR_IPC_TRANS_FAILED, "write transport error");
34
35 MessageParcel reply;
36 MessageOption option(MessageOption::TF_SYNC);
37
38 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REGISTER_APP,
39 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
40
41 int32_t result = reply.ReadInt32();
42 appId = reply.ReadInt32();
43 return result;
44 }
45
RegisterApplication(const sptr<IBluetoothGattClientCallback> & callback,const BluetoothRawAddress & addr,int32_t transport)46 int BluetoothGattClientProxy::RegisterApplication(
47 const sptr<IBluetoothGattClientCallback> &callback, const BluetoothRawAddress &addr, int32_t transport)
48 {
49 MessageParcel data;
50 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
51 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
52 CHECK_AND_RETURN_LOG_RET(
53 data.WriteRemoteObject(callback->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object error");
54 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&addr), BT_ERR_IPC_TRANS_FAILED, "write addr error");
55 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(transport), BT_ERR_IPC_TRANS_FAILED, "write transport error");
56
57 MessageParcel reply;
58 MessageOption option(MessageOption::TF_SYNC);
59
60 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REGISTER_APP,
61 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
62
63 return reply.ReadInt32();
64 }
65
DeregisterApplication(int32_t appId)66 int BluetoothGattClientProxy::DeregisterApplication(int32_t appId)
67 {
68 MessageParcel data;
69 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
70 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
71 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
72
73 MessageParcel reply;
74 MessageOption option(MessageOption::TF_SYNC);
75
76 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_DEREGISTER_APP,
77 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
78
79 return reply.ReadInt32();
80 }
81
Connect(int32_t appId,bool autoConnect)82 int BluetoothGattClientProxy::Connect(int32_t appId, bool autoConnect)
83 {
84 MessageParcel data;
85 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
86 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
87 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
88 CHECK_AND_RETURN_LOG_RET(data.WriteBool(autoConnect), BT_ERR_IPC_TRANS_FAILED, "write autoConnect error");
89
90 MessageParcel reply;
91 MessageOption option(MessageOption::TF_SYNC);
92
93 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_CONNECT,
94 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
95
96 return reply.ReadInt32();
97 }
98
Disconnect(int32_t appId)99 int BluetoothGattClientProxy::Disconnect(int32_t appId)
100 {
101 MessageParcel data;
102 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
103 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
104 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
105
106 MessageParcel reply;
107 MessageOption option(MessageOption::TF_SYNC);
108
109 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_DIS_CONNECT,
110 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
111
112 return reply.ReadInt32();
113 }
114
DiscoveryServices(int32_t appId)115 int BluetoothGattClientProxy::DiscoveryServices(int32_t appId)
116 {
117 MessageParcel data;
118 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
119 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
120 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
121
122 MessageParcel reply;
123 MessageOption option(MessageOption::TF_SYNC);
124
125 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_DISCOVERY_SERVICES,
126 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
127
128 return reply.ReadInt32();
129 }
130
ReadCharacteristic(int32_t appId,const BluetoothGattCharacteristic & characteristic)131 int BluetoothGattClientProxy::ReadCharacteristic(int32_t appId, const BluetoothGattCharacteristic &characteristic)
132 {
133 MessageParcel data;
134 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
135 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
136 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
137 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&characteristic),
138 BT_ERR_IPC_TRANS_FAILED, "write characteristic error");
139
140 MessageParcel reply;
141 MessageOption option(MessageOption::TF_SYNC);
142
143 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_READ_CHARACTERISTIC,
144 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
145
146 return reply.ReadInt32();
147 }
148
WriteCharacteristic(int32_t appId,BluetoothGattCharacteristic * characteristic,bool withoutRespond)149 int BluetoothGattClientProxy::WriteCharacteristic(
150 int32_t appId, BluetoothGattCharacteristic *characteristic, bool withoutRespond)
151 {
152 MessageParcel data;
153 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
154 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
155 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
156 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(characteristic),
157 BT_ERR_IPC_TRANS_FAILED, "write characteristic error");
158 CHECK_AND_RETURN_LOG_RET(data.WriteBool(withoutRespond),
159 BT_ERR_IPC_TRANS_FAILED, "write withoutRespond error");
160
161 MessageParcel reply;
162 MessageOption option(MessageOption::TF_SYNC);
163
164 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_WRITE_CHARACTERISTIC,
165 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
166
167 return reply.ReadInt32();
168 }
169
SignedWriteCharacteristic(int32_t appId,BluetoothGattCharacteristic * characteristic)170 int BluetoothGattClientProxy::SignedWriteCharacteristic(int32_t appId, BluetoothGattCharacteristic *characteristic)
171 {
172 MessageParcel data;
173 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
174 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
175 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId),
176 BT_ERR_IPC_TRANS_FAILED, "write appId error");
177 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(characteristic),
178 BT_ERR_IPC_TRANS_FAILED, "write characteristic error");
179
180 MessageParcel reply;
181 MessageOption option(MessageOption::TF_SYNC);
182
183 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_SIGNED_WRITE_CHARACTERISTIC,
184 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
185
186 return reply.ReadInt32();
187 }
188
ReadDescriptor(int32_t appId,const BluetoothGattDescriptor & descriptor)189 int BluetoothGattClientProxy::ReadDescriptor(int32_t appId, const BluetoothGattDescriptor &descriptor)
190 {
191 MessageParcel data;
192 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
193 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
194 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
195 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&descriptor), BT_ERR_IPC_TRANS_FAILED, "write descriptor error");
196
197 MessageParcel reply;
198 MessageOption option(MessageOption::TF_SYNC);
199
200 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_READ_DESCRIPTOR,
201 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
202
203 return reply.ReadInt32();
204 }
205
WriteDescriptor(int32_t appId,BluetoothGattDescriptor * descriptor)206 int BluetoothGattClientProxy::WriteDescriptor(int32_t appId, BluetoothGattDescriptor *descriptor)
207 {
208 MessageParcel data;
209 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
210 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
211 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
212 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(descriptor), BT_ERR_IPC_TRANS_FAILED, "write descriptor error");
213
214 MessageParcel reply;
215 MessageOption option(MessageOption::TF_SYNC);
216
217 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_WRITE_DESCRIPTOR,
218 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
219
220 return reply.ReadInt32();
221 }
222
RequestExchangeMtu(int32_t appId,int32_t mtu)223 int BluetoothGattClientProxy::RequestExchangeMtu(int32_t appId, int32_t mtu)
224 {
225 MessageParcel data;
226 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
227 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
228 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
229 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(mtu), BT_ERR_IPC_TRANS_FAILED, "write mtu error");
230 MessageParcel reply;
231 MessageOption option(MessageOption::TF_SYNC);
232
233 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REQUEST_EXCHANGE_MTU,
234 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
235
236 return reply.ReadInt32();
237 }
238
GetAllDevice(std::vector<BluetoothGattDevice> & device)239 void BluetoothGattClientProxy::GetAllDevice(std::vector<BluetoothGattDevice> &device)
240 {
241 MessageParcel data;
242 CHECK_AND_RETURN_LOG(
243 data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()), "WriteInterfaceToken error");
244
245 MessageParcel reply;
246 MessageOption option(MessageOption::TF_SYNC);
247
248 SEND_IPC_REQUEST_RETURN(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_GET_ALL_DEVICE, data, reply, option);
249
250 int DevNum = 0;
251 if (!reply.ReadInt32(DevNum) || DevNum > GATT_CLIENT_READ_DATA_SIZE_MAX_LEN) {
252 HILOGE("read Parcelable size failed.");
253 return;
254 }
255 for (int i = DevNum; i > 0; i--) {
256 std::shared_ptr<BluetoothGattDevice> dev(reply.ReadParcelable<BluetoothGattDevice>());
257 if (!dev) {
258 return;
259 }
260 device.push_back(*dev);
261 }
262 }
263
RequestConnectionPriority(int32_t appId,int32_t connPriority)264 int BluetoothGattClientProxy::RequestConnectionPriority(int32_t appId, int32_t connPriority)
265 {
266 MessageParcel data;
267 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
268 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
269 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
270 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(connPriority), BT_ERR_IPC_TRANS_FAILED, "write connPriority error");
271
272 MessageParcel reply;
273 MessageOption option(MessageOption::TF_SYNC);
274
275 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REQUEST_CONNECTION_PRIORITY,
276 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
277
278 return reply.ReadInt32();
279 }
280
GetServices(int32_t appId,std::vector<BluetoothGattService> & service)281 int BluetoothGattClientProxy::GetServices(int32_t appId, std::vector<BluetoothGattService> &service)
282 {
283 MessageParcel data;
284 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
285 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
286 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
287
288 MessageParcel reply;
289 MessageOption option(MessageOption::TF_SYNC);
290
291 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_GET_SERVICES,
292 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
293
294 int ret = reply.ReadInt32();
295 int DevNum = 0;
296 if (!reply.ReadInt32(DevNum) || DevNum > GATT_CLIENT_READ_DATA_SIZE_MAX_LEN) {
297 HILOGE("read Parcelable size failed.");
298 return BT_ERR_IPC_TRANS_FAILED;
299 }
300 for (int i = DevNum; i > 0; i--) {
301 std::shared_ptr<BluetoothGattService> dev(reply.ReadParcelable<BluetoothGattService>());
302 if (!dev) {
303 return BT_ERR_IPC_TRANS_FAILED;
304 }
305 service.push_back(*dev);
306 }
307 return ret;
308 }
309
RequestFastestConn(const BluetoothRawAddress & addr)310 int BluetoothGattClientProxy::RequestFastestConn(const BluetoothRawAddress &addr)
311 {
312 MessageParcel data;
313 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
314 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
315 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&addr), BT_ERR_IPC_TRANS_FAILED, "write addr error");
316
317 MessageParcel reply;
318 MessageOption option(MessageOption::TF_SYNC);
319
320 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REQUEST_FASTEST_CONNECTION,
321 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
322
323 return reply.ReadInt32();
324 }
325
ReadRemoteRssiValue(int32_t appId)326 int BluetoothGattClientProxy::ReadRemoteRssiValue(int32_t appId)
327 {
328 MessageParcel data;
329 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
330 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
331 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
332
333 MessageParcel reply;
334 MessageOption option(MessageOption::TF_SYNC);
335
336 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_READ_REMOTE_RSSI_VALUE,
337 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
338
339 return reply.ReadInt32();
340 }
341
RequestNotification(int32_t appId,uint16_t characterHandle,bool enable)342 int BluetoothGattClientProxy::RequestNotification(int32_t appId, uint16_t characterHandle, bool enable)
343 {
344 MessageParcel data;
345 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor()),
346 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
347 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
348 CHECK_AND_RETURN_LOG_RET(data.WriteUint16(characterHandle), BT_ERR_IPC_TRANS_FAILED, "write characterHandle error");
349 CHECK_AND_RETURN_LOG_RET(data.WriteBool(enable), BT_ERR_IPC_TRANS_FAILED, "write enable error");
350
351 MessageParcel reply;
352 MessageOption option(MessageOption::TF_SYNC);
353
354 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REQUEST_NOTIFICATION,
355 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
356
357 return reply.ReadInt32();
358 }
359
360 } // namespace Bluetooth
361 } // namespace OHOS
362