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_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "string_ex.h"
20 
21 namespace OHOS {
22 namespace Bluetooth {
BluetoothGattClientStub()23 BluetoothGattClientStub::BluetoothGattClientStub()
24 {
25     HILOGD("%{public}s start.", __func__);
26     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REGISTER_APP)] =
27         &BluetoothGattClientStub::RegisterApplicationInner;
28     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_DEREGISTER_APP)] =
29         &BluetoothGattClientStub::DeregisterApplicationInner;
30     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_CONNECT)] =
31         &BluetoothGattClientStub::ConnectInner;
32     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_DIS_CONNECT)] =
33         &BluetoothGattClientStub::DisconnectInner;
34     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_DISCOVERY_SERVICES)] =
35         &BluetoothGattClientStub::DiscoveryServicesInner;
36     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_READ_CHARACTERISTIC)] =
37         &BluetoothGattClientStub::ReadCharacteristicInner;
38     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_WRITE_CHARACTERISTIC)] =
39         &BluetoothGattClientStub::WriteCharacteristicInner;
40     memberFuncMap_[static_cast<uint32_t>(
41         BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_SIGNED_WRITE_CHARACTERISTIC)] =
42         &BluetoothGattClientStub::SignedWriteCharacteristicInner;
43     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_READ_DESCRIPTOR)] =
44         &BluetoothGattClientStub::ReadDescriptorInner;
45     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_WRITE_DESCRIPTOR)] =
46         &BluetoothGattClientStub::WriteDescriptorInner;
47     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REQUEST_EXCHANGE_MTU)] =
48         &BluetoothGattClientStub::RequestExchangeMtuInner;
49     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_GET_ALL_DEVICE)] =
50         &BluetoothGattClientStub::GetAllDeviceInner;
51     memberFuncMap_[static_cast<uint32_t>(
52         BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REQUEST_CONNECTION_PRIORITY)] =
53         &BluetoothGattClientStub::RequestConnectionPriorityInner;
54     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_GET_SERVICES)] =
55         &BluetoothGattClientStub::GetServicesInner;
56     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REQUEST_FASTEST_CONNECTION)] =
57         &BluetoothGattClientStub::RequestFastestConnInner;
58     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_READ_REMOTE_RSSI_VALUE)] =
59         &BluetoothGattClientStub::ReadRemoteRssiValueInner;
60     memberFuncMap_[static_cast<uint32_t>(BluetoothGattClientInterfaceCode::BT_GATT_CLIENT_REQUEST_NOTIFICATION)] =
61         &BluetoothGattClientStub::RequestNotificationInner;
62 }
63 
~BluetoothGattClientStub()64 BluetoothGattClientStub::~BluetoothGattClientStub()
65 {
66     HILOGD("%{public}s start.", __func__);
67     memberFuncMap_.clear();
68 }
69 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)70 int BluetoothGattClientStub::OnRemoteRequest(
71     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
72 {
73     HILOGD("BluetoothGattClientStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
74     std::u16string descriptor = BluetoothGattClientStub::GetDescriptor();
75     std::u16string remoteDescriptor = data.ReadInterfaceToken();
76     if (descriptor != remoteDescriptor) {
77         HILOGI("local descriptor is not equal to remote");
78         return ERR_INVALID_STATE;
79     }
80     auto itFunc = memberFuncMap_.find(code);
81     if (itFunc != memberFuncMap_.end()) {
82         auto memberFunc = itFunc->second;
83         if (memberFunc != nullptr) {
84             return (this->*memberFunc)(data, reply);
85         }
86     }
87     HILOGW("BluetoothGattClientStub::OnRemoteRequest, default case, need check.");
88     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
89 }
90 
RegisterApplicationInner(MessageParcel & data,MessageParcel & reply)91 ErrCode BluetoothGattClientStub::RegisterApplicationInner(MessageParcel &data, MessageParcel &reply)
92 {
93     HILOGI("BluetoothGattClientStub::RegisterApplicationInner starts");
94     sptr<IRemoteObject> remote = data.ReadRemoteObject();
95     const sptr<IBluetoothGattClientCallback> callback = OHOS::iface_cast<IBluetoothGattClientCallback>(remote);
96     std::shared_ptr<BluetoothRawAddress> addr(data.ReadParcelable<BluetoothRawAddress>());
97     if (!addr) {
98         return TRANSACTION_ERR;
99     }
100     int32_t transport = data.ReadInt32();
101     int appId = 0;
102     int result = RegisterApplication(callback, *addr, transport, appId);
103     bool resultRet = reply.WriteInt32(result);
104     bool appIdRet = reply.WriteInt32(appId);
105     if (!(resultRet && appIdRet)) {
106         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
107         return ERR_INVALID_VALUE;
108     }
109     return NO_ERROR;
110 }
111 
DeregisterApplicationInner(MessageParcel & data,MessageParcel & reply)112 ErrCode BluetoothGattClientStub::DeregisterApplicationInner(MessageParcel &data, MessageParcel &reply)
113 {
114     HILOGI("BluetoothGattClientStub::DeregisterApplicationInner starts");
115     int32_t appId = data.ReadInt32();
116     int result = DeregisterApplication(appId);
117     bool ret = reply.WriteInt32(result);
118     if (!ret) {
119         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
120         return ERR_INVALID_VALUE;
121     }
122     return NO_ERROR;
123 }
124 
ConnectInner(MessageParcel & data,MessageParcel & reply)125 ErrCode BluetoothGattClientStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
126 {
127     HILOGI("BluetoothGattClientStub::ConnectInner starts");
128     int32_t appId = data.ReadInt32();
129     bool autoConnect = data.ReadBool();
130     int result = Connect(appId, autoConnect);
131     bool ret = reply.WriteInt32(result);
132     if (!ret) {
133         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
134         return ERR_INVALID_VALUE;
135     }
136     return NO_ERROR;
137 }
138 
DisconnectInner(MessageParcel & data,MessageParcel & reply)139 ErrCode BluetoothGattClientStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
140 {
141     HILOGI("BluetoothGattClientStub::DisconnectInner starts");
142     int32_t appId = data.ReadInt32();
143     int result = Disconnect(appId);
144     bool ret = reply.WriteInt32(result);
145     if (!ret) {
146         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
147         return ERR_INVALID_VALUE;
148     }
149     return NO_ERROR;
150 }
151 
DiscoveryServicesInner(MessageParcel & data,MessageParcel & reply)152 ErrCode BluetoothGattClientStub::DiscoveryServicesInner(MessageParcel &data, MessageParcel &reply)
153 {
154     HILOGI("BluetoothGattClientStub::DiscoveryServicesInner starts");
155     int32_t appId = data.ReadInt32();
156     int result = DiscoveryServices(appId);
157     bool ret = reply.WriteInt32(result);
158     if (!ret) {
159         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
160         return ERR_INVALID_VALUE;
161     }
162     return NO_ERROR;
163 }
164 
ReadCharacteristicInner(MessageParcel & data,MessageParcel & reply)165 ErrCode BluetoothGattClientStub::ReadCharacteristicInner(MessageParcel &data, MessageParcel &reply)
166 {
167     HILOGI("BluetoothGattClientStub::ReadCharacteristicInner starts");
168     int32_t appId = data.ReadInt32();
169     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
170     if (!characteristic) {
171         return TRANSACTION_ERR;
172     }
173     int result = ReadCharacteristic(appId, *characteristic);
174     bool ret = reply.WriteInt32(result);
175     if (!ret) {
176         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
177         return ERR_INVALID_VALUE;
178     }
179     return NO_ERROR;
180 }
181 
WriteCharacteristicInner(MessageParcel & data,MessageParcel & reply)182 ErrCode BluetoothGattClientStub::WriteCharacteristicInner(MessageParcel &data, MessageParcel &reply)
183 {
184     HILOGI("BluetoothGattClientStub::WriteCharacteristicInner starts");
185     int32_t appId = data.ReadInt32();
186     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
187     if (!characteristic) {
188         return TRANSACTION_ERR;
189     }
190     bool withoutRespond = data.ReadBool();
191     int result = WriteCharacteristic(appId, characteristic.get(), withoutRespond);
192     bool ret = reply.WriteInt32(result);
193     if (!ret) {
194         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
195         return ERR_INVALID_VALUE;
196     }
197     return NO_ERROR;
198 }
199 
SignedWriteCharacteristicInner(MessageParcel & data,MessageParcel & reply)200 ErrCode BluetoothGattClientStub::SignedWriteCharacteristicInner(MessageParcel &data, MessageParcel &reply)
201 {
202     HILOGI("BluetoothGattClientStub::SignedWriteCharacteristicInner starts");
203     int32_t appId = data.ReadInt32();
204     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
205     if (!characteristic) {
206         return TRANSACTION_ERR;
207     }
208     int result = SignedWriteCharacteristic(appId, characteristic.get());
209     bool ret = reply.WriteInt32(result);
210     if (!ret) {
211         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
212         return ERR_INVALID_VALUE;
213     }
214     return NO_ERROR;
215 }
216 
ReadDescriptorInner(MessageParcel & data,MessageParcel & reply)217 ErrCode BluetoothGattClientStub::ReadDescriptorInner(MessageParcel &data, MessageParcel &reply)
218 {
219     HILOGI("BluetoothGattClientStub::ReadDescriptorInner starts");
220     int32_t appId = data.ReadInt32();
221     std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
222     if (!descriptor) {
223         return TRANSACTION_ERR;
224     }
225     int result = ReadDescriptor(appId, *descriptor);
226     bool ret = reply.WriteInt32(result);
227     if (!ret) {
228         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
229         return ERR_INVALID_VALUE;
230     }
231     return NO_ERROR;
232 }
233 
WriteDescriptorInner(MessageParcel & data,MessageParcel & reply)234 ErrCode BluetoothGattClientStub::WriteDescriptorInner(MessageParcel &data, MessageParcel &reply)
235 {
236     HILOGI("BluetoothGattClientStub::WriteDescriptorInner starts");
237     int32_t appId = data.ReadInt32();
238     std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
239     if (!descriptor) {
240         return TRANSACTION_ERR;
241     }
242     int result = WriteDescriptor(appId, descriptor.get());
243     HILOGI("appId=%{public}d, result=%{public}d", appId, result);
244     bool ret = reply.WriteInt32(result);
245     if (!ret) {
246         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
247         return ERR_INVALID_VALUE;
248     }
249     return NO_ERROR;
250 }
251 
RequestExchangeMtuInner(MessageParcel & data,MessageParcel & reply)252 ErrCode BluetoothGattClientStub::RequestExchangeMtuInner(MessageParcel &data, MessageParcel &reply)
253 {
254     HILOGI("BluetoothGattClientStub::RequestExchangeMtuInner starts");
255     int32_t appId = data.ReadInt32();
256     int32_t mtu = data.ReadInt32();
257     int result = RequestExchangeMtu(appId, mtu);
258     HILOGI("appId=%{public}d, mtu=%{public}d, result=%{public}d", appId, mtu, result);
259     bool ret = reply.WriteInt32(result);
260     if (!ret) {
261         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
262         return ERR_INVALID_VALUE;
263     }
264     return NO_ERROR;
265 }
266 
GetAllDeviceInner(MessageParcel & data,MessageParcel & reply)267 ErrCode BluetoothGattClientStub::GetAllDeviceInner(MessageParcel &data, MessageParcel &reply)
268 {
269     HILOGI("BluetoothGattClientStub::GetAllDeviceInner starts");
270     std::vector<BluetoothGattDevice> device;
271     GetAllDevice(device);
272     reply.WriteInt32(device.size());
273     int num = device.size();
274     for (int i = 0; i < num; i++) {
275         bool ret = reply.WriteParcelable(&device[i]);
276         if (!ret) {
277             HILOGE("WriteParcelable<GetAllDeviceInner> failed");
278             return ERR_INVALID_VALUE;
279         }
280     }
281     return NO_ERROR;
282 }
283 
RequestConnectionPriorityInner(MessageParcel & data,MessageParcel & reply)284 ErrCode BluetoothGattClientStub::RequestConnectionPriorityInner(MessageParcel &data, MessageParcel &reply)
285 {
286     HILOGI("BluetoothGattClientStub::RequestConnectionPriorityInner starts");
287     int32_t appId = data.ReadInt32();
288     int32_t connPriority = data.ReadInt32();
289     int result = RequestConnectionPriority(appId, connPriority);
290     bool ret = reply.WriteInt32(result);
291     if (!ret) {
292         HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
293         return ERR_INVALID_VALUE;
294     }
295     return NO_ERROR;
296 }
297 
GetServicesInner(MessageParcel & data,MessageParcel & reply)298 ErrCode BluetoothGattClientStub::GetServicesInner(MessageParcel &data, MessageParcel &reply)
299 {
300     HILOGI("BluetoothGattClientStub::GetServicesInner starts");
301     int32_t appId = data.ReadInt32();
302     std::vector<BluetoothGattService> service;
303     int result = GetServices(appId, service);
304     bool resultRet = reply.WriteInt32(result);
305     bool sizeRet = reply.WriteInt32(service.size());
306     if (!(resultRet && sizeRet)) {
307       HILOGE("Write data fail");
308       return ERR_INVALID_VALUE;
309     }
310     int num = service.size();
311     for (int i = 0; i < num; i++) {
312         bool ret = reply.WriteParcelable(&service[i]);
313         if (!ret) {
314             HILOGE("WriteParcelable<GetServicesInner> failed");
315             return ERR_INVALID_VALUE;
316         }
317     }
318     return NO_ERROR;
319 }
320 
RequestFastestConnInner(MessageParcel & data,MessageParcel & reply)321 ErrCode BluetoothGattClientStub::RequestFastestConnInner(MessageParcel &data, MessageParcel &reply)
322 {
323     return NO_ERROR;
324 }
325 
ReadRemoteRssiValueInner(MessageParcel & data,MessageParcel & reply)326 ErrCode BluetoothGattClientStub::ReadRemoteRssiValueInner(MessageParcel &data, MessageParcel &reply)
327 {
328     return NO_ERROR;
329 }
330 
RequestNotificationInner(MessageParcel & data,MessageParcel & reply)331 ErrCode BluetoothGattClientStub::RequestNotificationInner(MessageParcel &data, MessageParcel &reply)
332 {
333     return NO_ERROR;
334 }
335 }  // namespace Bluetooth
336 }  // namespace OHOS