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 HILOGI("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 HILOGI("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 (this->*memberFunc)(data, reply);
81 }
82 }
83 HILOGW("BluetoothGattClientCallbackStub::OnRemoteRequest, default case, need check.");
84 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
85 }
86
OnConnectionStateChangedInner(MessageParcel & data,MessageParcel & reply)87 ErrCode BluetoothGattClientCallbackStub::OnConnectionStateChangedInner(MessageParcel &data, MessageParcel &reply)
88 {
89 HILOGD("BluetoothGattClientCallbackStub::OnConnectionStateChangedInner Triggered!");
90 int32_t state = data.ReadInt32();
91 int32_t newState = data.ReadInt32();
92 OnConnectionStateChanged(state, newState);
93 return NO_ERROR;
94 }
95
OnCharacteristicChangedInner(MessageParcel & data,MessageParcel & reply)96 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicChangedInner(MessageParcel &data, MessageParcel &reply)
97 {
98 std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
99 if (!characteristic) {
100 return TRANSACTION_ERR;
101 }
102 OnCharacteristicChanged(*characteristic);
103 return NO_ERROR;
104 }
105
OnCharacteristicReadInner(MessageParcel & data,MessageParcel & reply)106 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicReadInner(MessageParcel &data, MessageParcel &reply)
107 {
108 HILOGI("BluetoothGattClientCallbackStub::OnCharacteristicReadInner Triggered!");
109 int32_t ret = data.ReadInt32();
110 std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
111 if (!characteristic) {
112 return TRANSACTION_ERR;
113 }
114 OnCharacteristicRead(ret, *characteristic);
115 return NO_ERROR;
116 }
117
OnCharacteristicWriteInner(MessageParcel & data,MessageParcel & reply)118 ErrCode BluetoothGattClientCallbackStub::OnCharacteristicWriteInner(MessageParcel &data, MessageParcel &reply)
119 {
120 HILOGI("BluetoothGattClientCallbackStub::OnCharacteristicWriteInner Triggered!");
121 int32_t ret = data.ReadInt32();
122 std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
123 if (!characteristic) {
124 return TRANSACTION_ERR;
125 }
126 OnCharacteristicWrite(ret, *characteristic);
127 return NO_ERROR;
128 }
129
OnDescriptorReadInner(MessageParcel & data,MessageParcel & reply)130 ErrCode BluetoothGattClientCallbackStub::OnDescriptorReadInner(MessageParcel &data, MessageParcel &reply)
131 {
132 HILOGI("BluetoothGattClientCallbackStub::OnDescriptorReadInner Triggered!");
133 int32_t ret = data.ReadInt32();
134 std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
135 if (!descriptor) {
136 return TRANSACTION_ERR;
137 }
138 OnDescriptorRead(ret, *descriptor);
139
140 return NO_ERROR;
141 }
142
OnDescriptorWriteInner(MessageParcel & data,MessageParcel & reply)143 ErrCode BluetoothGattClientCallbackStub::OnDescriptorWriteInner(MessageParcel &data, MessageParcel &reply)
144 {
145 HILOGI("BluetoothGattClientCallbackStub::OnDescriptorWriteInner 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 OnDescriptorWrite(ret, *descriptor);
152 return NO_ERROR;
153 }
154
OnMtuChangedInner(MessageParcel & data,MessageParcel & reply)155 ErrCode BluetoothGattClientCallbackStub::OnMtuChangedInner(MessageParcel &data, MessageParcel &reply)
156 {
157 HILOGI("BluetoothGattClientCallbackStub::OnMtuChangedInner Triggered!");
158 int32_t state = data.ReadInt32();
159 int32_t mtu = data.ReadInt32();
160 OnMtuChanged(state, mtu);
161 return NO_ERROR;
162 }
163
OnServicesDiscoveredInner(MessageParcel & data,MessageParcel & reply)164 ErrCode BluetoothGattClientCallbackStub::OnServicesDiscoveredInner(MessageParcel &data, MessageParcel &reply)
165 {
166 HILOGI("BluetoothGattClientCallbackStub::OnServicesDiscoveredInner Triggered!");
167 int32_t status = data.ReadInt32();
168 OnServicesDiscovered(status);
169 return NO_ERROR;
170 }
171
OnConnectionParameterChangedInner(MessageParcel & data,MessageParcel & reply)172 ErrCode BluetoothGattClientCallbackStub::OnConnectionParameterChangedInner(MessageParcel &data, MessageParcel &reply)
173 {
174 HILOGI("BluetoothGattClientCallbackStub::OnConnectionParameterChangedInner Triggered!");
175 int32_t interval = data.ReadInt32();
176 int32_t latency = data.ReadInt32();
177 int32_t timeout = data.ReadInt32();
178 int32_t status = data.ReadInt32();
179 OnConnectionParameterChanged(interval, latency, timeout, status);
180 return NO_ERROR;
181 }
182
OnServicesChangedInner(MessageParcel & data,MessageParcel & reply)183 ErrCode BluetoothGattClientCallbackStub::OnServicesChangedInner(MessageParcel &data, MessageParcel &reply)
184 {
185 HILOGI("BluetoothGattClientCallbackStub::OnServicesChangedInner Triggered!");
186 int32_t num = 0;
187 if (!data.ReadInt32(num) || num > GATT_CLIENT_CALLBACK_READ_DATA_SIZE_MAX_LEN) {
188 HILOGE("read Parcelable size failed.");
189 return TRANSACTION_ERR;
190 }
191 std::vector<BluetoothGattService> service;
192 for (int i = num; i > 0; i--) {
193 std::shared_ptr<BluetoothGattService> dev(data.ReadParcelable<BluetoothGattService>());
194 if (!dev) {
195 return TRANSACTION_ERR;
196 }
197 service.push_back(*dev);
198 }
199 OnServicesChanged(service);
200 return NO_ERROR;
201 }
202
OnReadRemoteRssiValueInner(MessageParcel & data,MessageParcel & reply)203 ErrCode BluetoothGattClientCallbackStub::OnReadRemoteRssiValueInner(MessageParcel &data, MessageParcel &reply)
204 {
205 HILOGI("BluetoothGattClientCallbackStub::OnReadRemoteRssiValueInner Triggered!");
206 bluetooth::RawAddress address(data.ReadString());
207 int32_t rssi = data.ReadInt32();
208 int32_t state = data.ReadInt32();
209 OnReadRemoteRssiValue(address, rssi, state);
210 return NO_ERROR;
211 }
212
213 } // namespace Bluetooth
214 } // namespace OHOS
215