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