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 HILOGI("BluetoothGattClientProxy::RegisterApplication start");
28 MessageParcel data;
29 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
30 HILOGE("BluetoothGattClientProxy::RegisterApplication WriteInterfaceToken error");
31 return BT_ERR_INTERNAL_ERROR;
32 }
33 if (!data.WriteRemoteObject(callback->AsObject())) {
34 HILOGE("BluetoothGattClientProxy::RegisterApplication transport error");
35 return BT_ERR_INTERNAL_ERROR;
36 }
37 if (!data.WriteParcelable(&addr)) {
38 HILOGE("BluetoothGattClientProxy::RegisterApplication transport error");
39 return BT_ERR_INTERNAL_ERROR;
40 }
41 if (!data.WriteInt32(transport)) {
42 HILOGE("BluetoothGattClientProxy::RegisterApplication transport error");
43 return BT_ERR_INTERNAL_ERROR;
44 }
45 MessageParcel reply;
46 MessageOption option {
47 MessageOption::TF_SYNC
48 };
49 int error = Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_REGISTER_APP, data, reply, option);
50 if (error != BT_SUCCESS) {
51 HILOGE("BluetoothGattClientProxy::RegisterApplication done fail, error: %{public}d", error);
52 return BT_ERR_INTERNAL_ERROR;
53 }
54 int32_t result = reply.ReadInt32();
55 appId = reply.ReadInt32();
56 return result;
57 }
58
RegisterApplication(const sptr<IBluetoothGattClientCallback> & callback,const BluetoothRawAddress & addr,int32_t transport)59 int BluetoothGattClientProxy::RegisterApplication(
60 const sptr<IBluetoothGattClientCallback> &callback, const BluetoothRawAddress &addr, int32_t transport)
61 {
62 HILOGI("BluetoothGattClientProxy::RegisterApplication start");
63 MessageParcel data;
64 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
65 HILOGE("BluetoothGattClientProxy::RegisterApplication WriteInterfaceToken error");
66 return ERROR;
67 }
68 if (!data.WriteRemoteObject(callback->AsObject())) {
69 HILOGE("BluetoothGattClientProxy::RegisterApplication transport error");
70 return ERROR;
71 }
72 if (!data.WriteParcelable(&addr)) {
73 HILOGE("BluetoothGattClientProxy::RegisterApplication transport error");
74 return ERROR;
75 }
76 if (!data.WriteInt32(transport)) {
77 HILOGE("BluetoothGattClientProxy::RegisterApplication transport error");
78 return ERROR;
79 }
80 MessageParcel reply;
81 MessageOption option {
82 MessageOption::TF_SYNC
83 };
84 int error = Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_REGISTER_APP, data, reply, option);
85 if (error != NO_ERROR) {
86 HILOGE("BluetoothGattClientProxy::RegisterApplication done fail, error: %{public}d", error);
87 return ERROR;
88 }
89 return reply.ReadInt32();
90 }
91
DeregisterApplication(int32_t appId)92 int BluetoothGattClientProxy::DeregisterApplication(int32_t appId)
93 {
94 HILOGI("BluetoothGattClientProxy::DeregisterApplication start");
95 MessageParcel data;
96 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
97 HILOGE("BluetoothGattClientProxy::DeregisterApplication WriteInterfaceToken error");
98 return BT_ERR_INTERNAL_ERROR;
99 }
100 if (!data.WriteInt32(appId)) {
101 HILOGE("BluetoothGattClientProxy::DeregisterApplication transport error");
102 return BT_ERR_INTERNAL_ERROR;
103 }
104 MessageParcel reply;
105 MessageOption option {
106 MessageOption::TF_SYNC
107 };
108 HILOGE("mobaiye2 BluetoothGattClientProxy::DeregisterApplication appId : %{public}d", appId);
109 int error = Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_DEREGISTER_APP, data, reply, option);
110 if (error != BT_SUCCESS) {
111 HILOGE("BluetoothGattClientProxy::DeregisterApplication done fail, error: %{public}d", error);
112 return BT_ERR_INTERNAL_ERROR;
113 }
114 return reply.ReadInt32();
115 }
116
Connect(int32_t appId,bool autoConnect)117 int BluetoothGattClientProxy::Connect(int32_t appId, bool autoConnect)
118 {
119 HILOGI("BluetoothGattClientProxy::Connect start");
120 MessageParcel data;
121 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
122 HILOGE("BluetoothGattClientProxy::Connect WriteInterfaceToken error");
123 return BT_ERR_INTERNAL_ERROR;
124 }
125 if (!data.WriteInt32(appId)) {
126 HILOGE("BluetoothGattClientProxy::Connect transport error");
127 return BT_ERR_INTERNAL_ERROR;
128 }
129 if (!data.WriteBool(autoConnect)) {
130 HILOGE("BluetoothGattClientProxy::Connect transport error");
131 return BT_ERR_INTERNAL_ERROR;
132 }
133 MessageParcel reply;
134 MessageOption option {
135 MessageOption::TF_SYNC
136 };
137 int error = Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_CONNECT, data, reply, option);
138 if (error != BT_SUCCESS) {
139 HILOGE("BluetoothGattClientProxy::Connect done fail, error: %{public}d", error);
140 return BT_ERR_INTERNAL_ERROR;
141 }
142 return reply.ReadInt32();
143 }
144
Disconnect(int32_t appId)145 int BluetoothGattClientProxy::Disconnect(int32_t appId)
146 {
147 HILOGI("BluetoothGattClientProxy::Disconnect start");
148 MessageParcel data;
149 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
150 HILOGE("BluetoothGattClientProxy::Disconnect WriteInterfaceToken error");
151 return BT_ERR_INTERNAL_ERROR;
152 }
153 if (!data.WriteInt32(appId)) {
154 HILOGE("BluetoothGattClientProxy::Disconnect transport error");
155 return BT_ERR_INTERNAL_ERROR;
156 }
157 MessageParcel reply;
158 MessageOption option {
159 MessageOption::TF_SYNC
160 };
161 int error = Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_DIS_CONNECT, data, reply, option);
162 if (error != BT_SUCCESS) {
163 HILOGE("BluetoothGattClientProxy::Disconnect done fail, error: %{public}d", error);
164 return BT_ERR_INTERNAL_ERROR;
165 }
166 return reply.ReadInt32();
167 }
168
DiscoveryServices(int32_t appId)169 int BluetoothGattClientProxy::DiscoveryServices(int32_t appId)
170 {
171 HILOGI("BluetoothGattClientProxy::DiscoveryServices start");
172 MessageParcel data;
173 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
174 HILOGE("BluetoothGattClientProxy::DiscoveryServices WriteInterfaceToken error");
175 return BT_ERR_INTERNAL_ERROR;
176 }
177 if (!data.WriteInt32(appId)) {
178 HILOGE("BluetoothGattClientProxy::DiscoveryServices transport error");
179 return BT_ERR_INTERNAL_ERROR;
180 }
181 MessageParcel reply;
182 MessageOption option {
183 MessageOption::TF_SYNC
184 };
185 int error =
186 Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_DISCOVERY_SERVICES, data, reply, option);
187 if (error != BT_SUCCESS) {
188 HILOGE("BluetoothGattClientProxy::DiscoveryServices done fail, error: %{public}d", error);
189 return BT_ERR_INTERNAL_ERROR;
190 }
191 return reply.ReadInt32();
192 }
193
ReadCharacteristic(int32_t appId,const BluetoothGattCharacteristic & characteristic)194 int BluetoothGattClientProxy::ReadCharacteristic(int32_t appId, const BluetoothGattCharacteristic &characteristic)
195 {
196 HILOGI("BluetoothGattClientProxy::ReadCharacteristic start");
197 MessageParcel data;
198 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
199 HILOGE("BluetoothGattClientProxy::ReadCharacteristic WriteInterfaceToken error");
200 return BT_ERR_INTERNAL_ERROR;
201 }
202 if (!data.WriteInt32(appId)) {
203 HILOGE("BluetoothGattClientProxy::ReadCharacteristic transport error");
204 return BT_ERR_INTERNAL_ERROR;
205 }
206 if (!data.WriteParcelable(&characteristic)) {
207 HILOGE("BluetoothGattClientProxy::ReadCharacteristic transport error");
208 return BT_ERR_INTERNAL_ERROR;
209 }
210 MessageParcel reply;
211 MessageOption option {
212 MessageOption::TF_SYNC
213 };
214 int error =
215 Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_READ_CHARACTERISTIC, data, reply, option);
216 if (error != BT_SUCCESS) {
217 HILOGE("BluetoothGattClientProxy::ReadCharacteristic done fail, error: %{public}d", error);
218 return BT_ERR_INTERNAL_ERROR;
219 }
220 return reply.ReadInt32();
221 }
222
WriteCharacteristic(int32_t appId,BluetoothGattCharacteristic * characteristic,bool withoutRespond)223 int BluetoothGattClientProxy::WriteCharacteristic(
224 int32_t appId, BluetoothGattCharacteristic *characteristic, bool withoutRespond)
225 {
226 HILOGI("BluetoothGattClientProxy::WriteCharacteristic start");
227 MessageParcel data;
228 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
229 HILOGE("BluetoothGattClientProxy::WriteCharacteristic WriteInterfaceToken error");
230 return BT_ERR_INTERNAL_ERROR;
231 }
232 if (!data.WriteInt32(appId)) {
233 HILOGE("BluetoothGattClientProxy::WriteCharacteristic transport error");
234 return BT_ERR_INTERNAL_ERROR;
235 }
236 if (!data.WriteParcelable(characteristic)) {
237 HILOGE("BluetoothGattClientProxy::WriteCharacteristic transport error");
238 return BT_ERR_INTERNAL_ERROR;
239 }
240 if (!data.WriteBool(withoutRespond)) {
241 HILOGE("BluetoothGattClientProxy::WriteCharacteristic transport error");
242 return BT_ERR_INTERNAL_ERROR;
243 }
244 MessageParcel reply;
245 MessageOption option {
246 MessageOption::TF_SYNC
247 };
248 int error =
249 Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_WRITE_CHARACTERISTIC, data, reply, option);
250 if (error != BT_SUCCESS) {
251 HILOGE("BluetoothGattClientProxy::WriteCharacteristic done fail, error: %{public}d", error);
252 return BT_ERR_INTERNAL_ERROR;
253 }
254 return reply.ReadInt32();
255 }
256
SignedWriteCharacteristic(int32_t appId,BluetoothGattCharacteristic * characteristic)257 int BluetoothGattClientProxy::SignedWriteCharacteristic(int32_t appId, BluetoothGattCharacteristic *characteristic)
258 {
259 HILOGI("BluetoothGattClientProxy::SignedWriteCharacteristic start");
260 MessageParcel data;
261 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
262 HILOGE("BluetoothGattClientProxy::SignedWriteCharacteristic WriteInterfaceToken error");
263 return ERROR;
264 }
265 if (!data.WriteInt32(appId)) {
266 HILOGE("BluetoothGattClientProxy::SignedWriteCharacteristic transport error");
267 return ERROR;
268 }
269 if (!data.WriteParcelable(characteristic)) {
270 HILOGE("BluetoothGattClientProxy::SignedWriteCharacteristic transport error");
271 return ERROR;
272 }
273 MessageParcel reply;
274 MessageOption option {
275 MessageOption::TF_SYNC
276 };
277 int error = Remote()->SendRequest(
278 IBluetoothGattClient::Code::BT_GATT_CLIENT_SIGNED_WRITE_CHARACTERISTIC, data, reply, option);
279 if (error != NO_ERROR) {
280 HILOGE("BluetoothGattClientProxy::SignedWriteCharacteristic done fail, error: %{public}d", error);
281 return ERROR;
282 }
283 return reply.ReadInt32();
284 }
285
ReadDescriptor(int32_t appId,const BluetoothGattDescriptor & descriptor)286 int BluetoothGattClientProxy::ReadDescriptor(int32_t appId, const BluetoothGattDescriptor &descriptor)
287 {
288 HILOGI("BluetoothGattClientProxy::ReadDescriptor start");
289 MessageParcel data;
290 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
291 HILOGE("BluetoothGattClientProxy::ReadDescriptor WriteInterfaceToken error");
292 return BT_ERR_INTERNAL_ERROR;
293 }
294 if (!data.WriteInt32(appId)) {
295 HILOGE("BluetoothGattClientProxy::ReadDescriptor transport error");
296 return BT_ERR_INTERNAL_ERROR;
297 }
298 if (!data.WriteParcelable(&descriptor)) {
299 HILOGE("BluetoothGattClientProxy::ReadDescriptor transport error");
300 return BT_ERR_INTERNAL_ERROR;
301 }
302 MessageParcel reply;
303 MessageOption option {
304 MessageOption::TF_SYNC
305 };
306 int error = Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_READ_DESCRIPTOR, data, reply, option);
307 if (error != BT_SUCCESS) {
308 HILOGE("BluetoothGattClientProxy::ReadDescriptor done fail, error: %{public}d", error);
309 return BT_ERR_INTERNAL_ERROR;
310 }
311 return reply.ReadInt32();
312 }
313
WriteDescriptor(int32_t appId,BluetoothGattDescriptor * descriptor)314 int BluetoothGattClientProxy::WriteDescriptor(int32_t appId, BluetoothGattDescriptor *descriptor)
315 {
316 HILOGI("BluetoothGattClientProxy::WriteDescriptor start");
317 MessageParcel data;
318 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
319 HILOGE("BluetoothGattClientProxy::WriteDescriptor WriteInterfaceToken error");
320 return BT_ERR_INTERNAL_ERROR;
321 }
322 if (!data.WriteInt32(appId)) {
323 HILOGE("BluetoothGattClientProxy::WriteDescriptor transport error");
324 return BT_ERR_INTERNAL_ERROR;
325 }
326 if (!data.WriteParcelable(descriptor)) {
327 HILOGE("BluetoothGattClientProxy::WriteDescriptor transport error");
328 return BT_ERR_INTERNAL_ERROR;
329 }
330 MessageParcel reply;
331 MessageOption option {
332 MessageOption::TF_SYNC
333 };
334 int error = Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_WRITE_DESCRIPTOR, data, reply, option);
335 if (error != BT_SUCCESS) {
336 HILOGE("BluetoothGattClientProxy::WriteDescriptor done fail, error: %{public}d", error);
337 return BT_ERR_INTERNAL_ERROR;
338 }
339 return reply.ReadInt32();
340 }
341
RequestExchangeMtu(int32_t appId,int32_t mtu)342 int BluetoothGattClientProxy::RequestExchangeMtu(int32_t appId, int32_t mtu)
343 {
344 HILOGI("BluetoothGattClientProxy::RequestExchangeMtu start");
345 MessageParcel data;
346 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
347 HILOGE("BluetoothGattClientProxy::RequestExchangeMtu WriteInterfaceToken error");
348 return BT_ERR_INTERNAL_ERROR;
349 }
350 if (!data.WriteInt32(appId)) {
351 HILOGE("BluetoothGattClientProxy::RequestExchangeMtu transport error");
352 return BT_ERR_INTERNAL_ERROR;
353 }
354 if (!data.WriteInt32(mtu)) {
355 HILOGE("BluetoothGattClientProxy::RequestExchangeMtu transport error");
356 return BT_ERR_INTERNAL_ERROR;
357 }
358 MessageParcel reply;
359 MessageOption option {
360 MessageOption::TF_SYNC
361 };
362 int error =
363 Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_REQUEST_EXCHANGE_MTU, data, reply, option);
364 if (error != BT_SUCCESS) {
365 HILOGE("BluetoothGattClientProxy::RequestExchangeMtu done fail, error: %{public}d", error);
366 return BT_ERR_INTERNAL_ERROR;
367 }
368 return reply.ReadInt32();
369 }
370
GetAllDevice(std::vector<BluetoothGattDevice> & device)371 void BluetoothGattClientProxy::GetAllDevice(std::vector<BluetoothGattDevice> &device)
372 {
373 HILOGI("BluetoothGattClientProxy::GetAllDevice start");
374 MessageParcel data;
375 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
376 HILOGE("BluetoothGattClientProxy::GetAllDevice WriteInterfaceToken error");
377 return;
378 }
379 MessageParcel reply;
380 MessageOption option {
381 MessageOption::TF_SYNC
382 };
383 int error = Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_GET_ALL_DEVICE, data, reply, option);
384 if (error != NO_ERROR) {
385 HILOGE("BluetoothGattClientProxy::GetAllDevice done fail, error: %d", error);
386 }
387 int DevNum = 0;
388 if (!reply.ReadInt32(DevNum) || DevNum > GATT_CLIENT_READ_DATA_SIZE_MAX_LEN) {
389 HILOGE("read Parcelable size failed.");
390 return;
391 }
392 for (int i = DevNum; i > 0; i--) {
393 std::shared_ptr<BluetoothGattDevice> dev(reply.ReadParcelable<BluetoothGattDevice>());
394 if (!dev) {
395 return;
396 }
397 device.push_back(*dev);
398 }
399 }
400
RequestConnectionPriority(int32_t appId,int32_t connPriority)401 int BluetoothGattClientProxy::RequestConnectionPriority(int32_t appId, int32_t connPriority)
402 {
403 HILOGI("BluetoothGattClientProxy::RequestConnectionPriority start");
404 MessageParcel data;
405 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
406 HILOGE("BluetoothGattClientProxy::RequestConnectionPriority WriteInterfaceToken error");
407 return ERROR;
408 }
409 if (!data.WriteInt32(appId)) {
410 HILOGE("BluetoothGattClientProxy::RequestConnectionPriority transport error");
411 return ERROR;
412 }
413 if (!data.WriteInt32(connPriority)) {
414 HILOGE("BluetoothGattClientProxy::RequestConnectionPriority transport error");
415 return ERROR;
416 }
417 MessageParcel reply;
418 MessageOption option {
419 MessageOption::TF_SYNC
420 };
421 int error = Remote()->SendRequest(
422 IBluetoothGattClient::Code::BT_GATT_CLIENT_REQUEST_CONNECTION_PRIORITY, data, reply, option);
423 if (error != NO_ERROR) {
424 HILOGE("BluetoothGattClientProxy::RequestConnectionPriority done fail, error: %{public}d", error);
425 return ERROR;
426 }
427 return reply.ReadInt32();
428 }
429
GetServices(int32_t appId,std::vector<BluetoothGattService> & service)430 void BluetoothGattClientProxy::GetServices(int32_t appId, std::vector<BluetoothGattService> &service)
431 {
432 HILOGI("BluetoothGattClientProxy::GetServices start");
433 MessageParcel data;
434 if (!data.WriteInterfaceToken(BluetoothGattClientProxy::GetDescriptor())) {
435 HILOGE("BluetoothGattClientProxy::GetServices WriteInterfaceToken error");
436 return;
437 }
438 if (!data.WriteInt32(appId)) {
439 HILOGE("BluetoothGattClientProxy::GetServices transport error");
440 return;
441 }
442 MessageParcel reply;
443 MessageOption option {
444 MessageOption::TF_SYNC
445 };
446 int error = Remote()->SendRequest(IBluetoothGattClient::Code::BT_GATT_CLIENT_GET_SERVICES, data, reply, option);
447 if (error != NO_ERROR) {
448 HILOGE("BluetoothGattClientProxy::GetServices done fail, error: %d", error);
449 }
450 int DevNum = 0;
451 if (!reply.ReadInt32(DevNum) || DevNum > GATT_CLIENT_READ_DATA_SIZE_MAX_LEN) {
452 HILOGE("read Parcelable size failed.");
453 return;
454 }
455 for (int i = DevNum; i > 0; i--) {
456 std::shared_ptr<BluetoothGattService> dev(reply.ReadParcelable<BluetoothGattService>());
457 if (!dev) {
458 return;
459 }
460 service.push_back(*dev);
461 }
462 }
463 } // namespace Bluetooth
464 } // namespace OHOS