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_callback_stub.h"
17 #include "bluetooth_log.h"
18 #include "raw_address.h"
19
20 namespace OHOS {
21 namespace Bluetooth {
22 const int32_t GATT_CLIENT_CALLBACK_READ_DATA_SIZE_MAX_LEN = 0x100;
BluetoothGattClientCallbackStub()23 BluetoothGattClientCallbackStub::BluetoothGattClientCallbackStub()
24 {
25 HILOGD("start.");
26 memberFuncMap_[static_cast<uint32_t>(
27 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_CONNECT_STATE_CHANGE)] =
28 BluetoothGattClientCallbackStub::OnConnectionStateChangedInner;
29 memberFuncMap_[static_cast<uint32_t>(
30 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_CHARACTER_CHANGE)] =
31 BluetoothGattClientCallbackStub::OnCharacteristicChangedInner;
32 memberFuncMap_[static_cast<uint32_t>(
33 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_CHARACTER_READ)] =
34 BluetoothGattClientCallbackStub::OnCharacteristicReadInner;
35 memberFuncMap_[static_cast<uint32_t>(
36 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_CHARACTER_WRITE)] =
37 BluetoothGattClientCallbackStub::OnCharacteristicWriteInner;
38 memberFuncMap_[static_cast<uint32_t>(
39 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_DESCRIPTOR_READ)] =
40 BluetoothGattClientCallbackStub::OnDescriptorReadInner;
41 memberFuncMap_[static_cast<uint32_t>(
42 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_DESCRIPTOR_WRITE)] =
43 BluetoothGattClientCallbackStub::OnDescriptorWriteInner;
44 memberFuncMap_[static_cast<uint32_t>(
45 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_MTU_UPDATE)] =
46 BluetoothGattClientCallbackStub::OnMtuChangedInner;
47 memberFuncMap_[static_cast<uint32_t>(
48 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_SERVICES_DISCOVER)] =
49 BluetoothGattClientCallbackStub::OnServicesDiscoveredInner;
50 memberFuncMap_[static_cast<uint32_t>(
51 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_CONNECTION_PARA_CHANGE)] =
52 BluetoothGattClientCallbackStub::OnConnectionParameterChangedInner;
53 memberFuncMap_[static_cast<uint32_t>(
54 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_SERVICES_CHANGED)] =
55 BluetoothGattClientCallbackStub::OnServicesChangedInner;
56 memberFuncMap_[static_cast<uint32_t>(
57 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_READ_REMOTE_RSSI_VALUE)] =
58 BluetoothGattClientCallbackStub::OnReadRemoteRssiValueInner;
59 }
60
~BluetoothGattClientCallbackStub()61 BluetoothGattClientCallbackStub::~BluetoothGattClientCallbackStub()
62 {
63 HILOGD("start.");
64 memberFuncMap_.clear();
65 }
66
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)67 int BluetoothGattClientCallbackStub::OnRemoteRequest(
68 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
69 {
70 HILOGD("BluetoothGattClientCallbackStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d",
71 code, option.GetFlags());
72 if (BluetoothGattClientCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
73 HILOGI("local descriptor is not equal to remote");
74 return ERR_INVALID_STATE;
75 }
76 auto itFunc = memberFuncMap_.find(code);
77 if (itFunc != memberFuncMap_.end()) {
78 auto memberFunc = itFunc->second;
79 if (memberFunc != nullptr) {
80 return memberFunc(this, data, reply);
81 }
82 }
83 HILOGW("BluetoothGattClientCallbackStub::OnRemoteRequest, default case, need check.");
84 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
85 }
86
87 __attribute__((no_sanitize("cfi")))
OnConnectionStateChangedInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)88 ErrCode BluetoothGattClientCallbackStub::OnConnectionStateChangedInner(
89 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
90 {
91 HILOGD("BluetoothGattClientCallbackStub::OnConnectionStateChangedInner Triggered!");
92 int32_t state = data.ReadInt32();
93 int32_t newState = data.ReadInt32();
94 stub->OnConnectionStateChanged(state, newState);
95 return NO_ERROR;
96 }
97
98 __attribute__((no_sanitize("cfi")))
OnCharacteristicChangedInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)99 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicChangedInner(
100 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
101 {
102 std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
103 if (!characteristic) {
104 return TRANSACTION_ERR;
105 }
106 stub->OnCharacteristicChanged(*characteristic);
107 return NO_ERROR;
108 }
109
110 __attribute__((no_sanitize("cfi")))
OnCharacteristicReadInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)111 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicReadInner(
112 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
113 {
114 HILOGI("BluetoothGattClientCallbackStub::OnCharacteristicReadInner Triggered!");
115 int32_t ret = data.ReadInt32();
116 std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
117 if (!characteristic) {
118 return TRANSACTION_ERR;
119 }
120 stub->OnCharacteristicRead(ret, *characteristic);
121 return NO_ERROR;
122 }
123
124 __attribute__((no_sanitize("cfi")))
OnCharacteristicWriteInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)125 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicWriteInner(
126 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
127 {
128 HILOGI("BluetoothGattClientCallbackStub::OnCharacteristicWriteInner Triggered!");
129 int32_t ret = data.ReadInt32();
130 std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
131 if (!characteristic) {
132 return TRANSACTION_ERR;
133 }
134 stub->OnCharacteristicWrite(ret, *characteristic);
135 return NO_ERROR;
136 }
137
138 __attribute__((no_sanitize("cfi")))
OnDescriptorReadInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)139 ErrCode BluetoothGattClientCallbackStub::OnDescriptorReadInner(
140 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
141 {
142 HILOGI("BluetoothGattClientCallbackStub::OnDescriptorReadInner Triggered!");
143 int32_t ret = data.ReadInt32();
144 std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
145 if (!descriptor) {
146 return TRANSACTION_ERR;
147 }
148 stub->OnDescriptorRead(ret, *descriptor);
149
150 return NO_ERROR;
151 }
152
153 __attribute__((no_sanitize("cfi")))
OnDescriptorWriteInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)154 ErrCode BluetoothGattClientCallbackStub::OnDescriptorWriteInner(
155 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
156 {
157 HILOGD("BluetoothGattClientCallbackStub::OnDescriptorWriteInner Triggered!");
158 int32_t ret = data.ReadInt32();
159 std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
160 if (!descriptor) {
161 return TRANSACTION_ERR;
162 }
163 stub->OnDescriptorWrite(ret, *descriptor);
164 return NO_ERROR;
165 }
166
167 __attribute__((no_sanitize("cfi")))
OnMtuChangedInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)168 ErrCode BluetoothGattClientCallbackStub::OnMtuChangedInner(
169 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
170 {
171 HILOGI("BluetoothGattClientCallbackStub::OnMtuChangedInner Triggered!");
172 int32_t state = data.ReadInt32();
173 int32_t mtu = data.ReadInt32();
174 stub->OnMtuChanged(state, mtu);
175 return NO_ERROR;
176 }
177
178 __attribute__((no_sanitize("cfi")))
OnServicesDiscoveredInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)179 ErrCode BluetoothGattClientCallbackStub::OnServicesDiscoveredInner(
180 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
181 {
182 HILOGI("BluetoothGattClientCallbackStub::OnServicesDiscoveredInner Triggered!");
183 int32_t status = data.ReadInt32();
184 stub->OnServicesDiscovered(status);
185 return NO_ERROR;
186 }
187
188 __attribute__((no_sanitize("cfi")))
OnConnectionParameterChangedInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)189 ErrCode BluetoothGattClientCallbackStub::OnConnectionParameterChangedInner(
190 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
191 {
192 HILOGD("BluetoothGattClientCallbackStub::OnConnectionParameterChangedInner Triggered!");
193 int32_t interval = data.ReadInt32();
194 int32_t latency = data.ReadInt32();
195 int32_t timeout = data.ReadInt32();
196 int32_t status = data.ReadInt32();
197 stub->OnConnectionParameterChanged(interval, latency, timeout, status);
198 return NO_ERROR;
199 }
200
201 __attribute__((no_sanitize("cfi")))
OnServicesChangedInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)202 ErrCode BluetoothGattClientCallbackStub::OnServicesChangedInner(
203 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
204 {
205 HILOGI("BluetoothGattClientCallbackStub::OnServicesChangedInner Triggered!");
206 int32_t num = 0;
207 if (!data.ReadInt32(num) || num > GATT_CLIENT_CALLBACK_READ_DATA_SIZE_MAX_LEN) {
208 HILOGE("read Parcelable size failed.");
209 return TRANSACTION_ERR;
210 }
211 std::vector<BluetoothGattService> service;
212 for (int i = num; i > 0; i--) {
213 std::shared_ptr<BluetoothGattService> dev(data.ReadParcelable<BluetoothGattService>());
214 if (!dev) {
215 return TRANSACTION_ERR;
216 }
217 service.push_back(*dev);
218 }
219 stub->OnServicesChanged(service);
220 return NO_ERROR;
221 }
222
223 __attribute__((no_sanitize("cfi")))
OnReadRemoteRssiValueInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)224 ErrCode BluetoothGattClientCallbackStub::OnReadRemoteRssiValueInner(
225 BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
226 {
227 HILOGI("BluetoothGattClientCallbackStub::OnReadRemoteRssiValueInner Triggered!");
228 bluetooth::RawAddress address(data.ReadString());
229 int32_t rssi = data.ReadInt32();
230 int32_t state = data.ReadInt32();
231 stub->OnReadRemoteRssiValue(address, rssi, state);
232 return NO_ERROR;
233 }
234
235 } // namespace Bluetooth
236 } // namespace OHOS
237