• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int32_t disconnectReason = data.ReadInt32();
98     stub->OnConnectionStateChanged(state, newState, disconnectReason);
99     return NO_ERROR;
100 }
101 
102 __attribute__((no_sanitize("cfi")))
OnCharacteristicChangedInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)103 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicChangedInner(
104     BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
105 {
106     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
107     if (!characteristic) {
108         return TRANSACTION_ERR;
109     }
110     stub->OnCharacteristicChanged(*characteristic);
111     return NO_ERROR;
112 }
113 
114 __attribute__((no_sanitize("cfi")))
OnCharacteristicReadInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)115 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicReadInner(
116     BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
117 {
118     HILOGI("BluetoothGattClientCallbackStub::OnCharacteristicReadInner Triggered!");
119     int32_t ret = data.ReadInt32();
120     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
121     if (!characteristic) {
122         return TRANSACTION_ERR;
123     }
124     stub->OnCharacteristicRead(ret, *characteristic);
125     return NO_ERROR;
126 }
127 
128 __attribute__((no_sanitize("cfi")))
OnCharacteristicWriteInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)129 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicWriteInner(
130     BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
131 {
132     HILOGD("BluetoothGattClientCallbackStub::OnCharacteristicWriteInner Triggered!");
133     int32_t ret = data.ReadInt32();
134     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
135     if (!characteristic) {
136         return TRANSACTION_ERR;
137     }
138     stub->OnCharacteristicWrite(ret, *characteristic);
139     return NO_ERROR;
140 }
141 
142 __attribute__((no_sanitize("cfi")))
OnDescriptorReadInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)143 ErrCode BluetoothGattClientCallbackStub::OnDescriptorReadInner(
144     BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
145 {
146     HILOGI("BluetoothGattClientCallbackStub::OnDescriptorReadInner Triggered!");
147     int32_t ret = data.ReadInt32();
148     std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
149     if (!descriptor) {
150         return TRANSACTION_ERR;
151     }
152     stub->OnDescriptorRead(ret, *descriptor);
153 
154     return NO_ERROR;
155 }
156 
157 __attribute__((no_sanitize("cfi")))
OnDescriptorWriteInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)158 ErrCode BluetoothGattClientCallbackStub::OnDescriptorWriteInner(
159     BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
160 {
161     HILOGD("BluetoothGattClientCallbackStub::OnDescriptorWriteInner Triggered!");
162     int32_t ret = data.ReadInt32();
163     std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
164     if (!descriptor) {
165         return TRANSACTION_ERR;
166     }
167     stub->OnDescriptorWrite(ret, *descriptor);
168     return NO_ERROR;
169 }
170 
171 __attribute__((no_sanitize("cfi")))
OnMtuChangedInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)172 ErrCode BluetoothGattClientCallbackStub::OnMtuChangedInner(
173     BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
174 {
175     HILOGI("BluetoothGattClientCallbackStub::OnMtuChangedInner Triggered!");
176     int32_t state = data.ReadInt32();
177     int32_t mtu = data.ReadInt32();
178     stub->OnMtuChanged(state, mtu);
179     return NO_ERROR;
180 }
181 
182 __attribute__((no_sanitize("cfi")))
OnServicesDiscoveredInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)183 ErrCode BluetoothGattClientCallbackStub::OnServicesDiscoveredInner(
184     BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
185 {
186     HILOGI("BluetoothGattClientCallbackStub::OnServicesDiscoveredInner Triggered!");
187     int32_t status = data.ReadInt32();
188     stub->OnServicesDiscovered(status);
189     return NO_ERROR;
190 }
191 
192 __attribute__((no_sanitize("cfi")))
OnConnectionParameterChangedInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)193 ErrCode BluetoothGattClientCallbackStub::OnConnectionParameterChangedInner(
194     BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
195 {
196     HILOGD("BluetoothGattClientCallbackStub::OnConnectionParameterChangedInner Triggered!");
197     int32_t interval = data.ReadInt32();
198     int32_t latency = data.ReadInt32();
199     int32_t timeout = data.ReadInt32();
200     int32_t status = data.ReadInt32();
201     stub->OnConnectionParameterChanged(interval, latency, timeout, status);
202     return NO_ERROR;
203 }
204 
205 __attribute__((no_sanitize("cfi")))
OnServicesChangedInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)206 ErrCode BluetoothGattClientCallbackStub::OnServicesChangedInner(
207     BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
208 {
209     HILOGI("BluetoothGattClientCallbackStub::OnServicesChangedInner Triggered!");
210     int32_t num = 0;
211     if (!data.ReadInt32(num) || num > GATT_CLIENT_CALLBACK_READ_DATA_SIZE_MAX_LEN) {
212         HILOGE("read Parcelable size failed.");
213         return TRANSACTION_ERR;
214     }
215     std::vector<BluetoothGattService> service;
216     for (int i = num; i > 0; i--) {
217         std::shared_ptr<BluetoothGattService> dev(data.ReadParcelable<BluetoothGattService>());
218         if (!dev) {
219             return TRANSACTION_ERR;
220         }
221         service.push_back(*dev);
222     }
223     stub->OnServicesChanged(service);
224     return NO_ERROR;
225 }
226 
227 __attribute__((no_sanitize("cfi")))
OnReadRemoteRssiValueInner(BluetoothGattClientCallbackStub * stub,MessageParcel & data,MessageParcel & reply)228 ErrCode BluetoothGattClientCallbackStub::OnReadRemoteRssiValueInner(
229     BluetoothGattClientCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
230 {
231     HILOGI("BluetoothGattClientCallbackStub::OnReadRemoteRssiValueInner Triggered!");
232     bluetooth::RawAddress address(data.ReadString());
233     int32_t rssi = data.ReadInt32();
234     int32_t state = data.ReadInt32();
235     stub->OnReadRemoteRssiValue(address, rssi, state);
236     return NO_ERROR;
237 }
238 
239 }  // namespace Bluetooth
240 }  // namespace OHOS
241