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