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_server_callback_stub"
17 #endif
18 
19 #include "bluetooth_gatt_server_callback_stub.h"
20 #include "bluetooth_log.h"
21 #include "ipc_types.h"
22 #include "string_ex.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
BluetoothGattServerCallbackStub()26 BluetoothGattServerCallbackStub::BluetoothGattServerCallbackStub()
27 {
28     HILOGD("start.");
29     memberFuncMap_[static_cast<uint32_t>(
30         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CHARACTERISTIC_READREQUEST)] =
31         BluetoothGattServerCallbackStub::OnCharacteristicReadRequestInner;
32     memberFuncMap_[static_cast<uint32_t>(
33         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CONNECTIONSTATE_CHANGED)] =
34         BluetoothGattServerCallbackStub::OnConnectionStateChangedInner;
35     memberFuncMap_[static_cast<uint32_t>(
36         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_ADD_SERVICE)] =
37         BluetoothGattServerCallbackStub::OnAddServiceInner;
38     memberFuncMap_[static_cast<uint32_t>(
39         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CHARACTERISTIC_WRITE_REQUEST)] =
40         BluetoothGattServerCallbackStub::OnCharacteristicWriteRequestInner;
41     memberFuncMap_[static_cast<uint32_t>(
42         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_DESCRIPTOR_READ_REQUEST)] =
43         BluetoothGattServerCallbackStub::OnDescriptorReadRequestInner;
44     memberFuncMap_[static_cast<uint32_t>(
45         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_DESCRIPTOR_WRITE_REQUEST)] =
46         BluetoothGattServerCallbackStub::OnDescriptorWriteRequestInner;
47     memberFuncMap_[static_cast<uint32_t>(
48         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_MTU_CHANGED)] =
49         BluetoothGattServerCallbackStub::OnMtuChangedInner;
50     memberFuncMap_[static_cast<uint32_t>(
51         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_NOTIFY_CONFIRM)] =
52         BluetoothGattServerCallbackStub::OnNotifyConfirmInner;
53     memberFuncMap_[static_cast<uint32_t>(
54         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CONNECTION_PARAMETER_CHANGED)] =
55         BluetoothGattServerCallbackStub::OnConnectionParameterChangedInner;
56     HILOGD("ends.");
57 }
58 
~BluetoothGattServerCallbackStub()59 BluetoothGattServerCallbackStub::~BluetoothGattServerCallbackStub()
60 {
61     HILOGD("start.");
62     memberFuncMap_.clear();
63 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)64 int BluetoothGattServerCallbackStub::OnRemoteRequest(
65     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
66 {
67     HILOGD("BluetoothGattServerCallbackStub::OnRemoteRequest, cmd = %{public}u, flags= %{public}d",
68         code, option.GetFlags());
69     if (BluetoothGattServerCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
70         HILOGI("local descriptor is not equal to remote");
71         return ERR_INVALID_STATE;
72     }
73 
74     auto itFunc = memberFuncMap_.find(code);
75     if (itFunc != memberFuncMap_.end()) {
76         auto memberFunc = itFunc->second;
77         if (memberFunc != nullptr) {
78             return memberFunc(this, data, reply);
79         }
80     }
81     HILOGW("BluetoothGattServerCallbackStub::OnRemoteRequest, default case, need check.");
82     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
83 };
84 __attribute__((no_sanitize("cfi")))
OnCharacteristicReadRequestInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)85 ErrCode BluetoothGattServerCallbackStub::OnCharacteristicReadRequestInner(
86     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
87 {
88     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
89     if (!device) {
90         return TRANSACTION_ERR;
91     }
92     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
93     if (!characteristic) {
94         return TRANSACTION_ERR;
95     }
96 
97     stub->OnCharacteristicReadRequest(*device, *characteristic);
98 
99     return NO_ERROR;
100 }
101 __attribute__((no_sanitize("cfi")))
OnConnectionStateChangedInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)102 ErrCode BluetoothGattServerCallbackStub::OnConnectionStateChangedInner(
103     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
104 {
105     HILOGD("BluetoothGattServerCallbackStub::OnConnectionStateChangedInner Triggered!");
106     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
107     if (!device) {
108         HILOGE("BluetoothGattServerCallbackStub::OnConnectionStateChangedInner device is null");
109         return TRANSACTION_ERR;
110     }
111 
112     int32_t ret = data.ReadInt32();
113     int32_t state = data.ReadInt32();
114     stub->OnConnectionStateChanged(*device, ret, state);
115 
116     return NO_ERROR;
117 }
118 __attribute__((no_sanitize("cfi")))
OnAddServiceInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)119 ErrCode BluetoothGattServerCallbackStub::OnAddServiceInner(
120     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
121 {
122     HILOGD("BluetoothGattServerCallbackStub::OnAddServiceInner Triggered!");
123     int32_t ret = data.ReadInt32();
124     std::shared_ptr<BluetoothGattService> service(data.ReadParcelable<BluetoothGattService>());
125     if (!service) {
126         return TRANSACTION_ERR;
127     }
128 
129     stub->OnAddService(ret, *service);
130 
131     service = nullptr;
132 
133     return NO_ERROR;
134 }
135 __attribute__((no_sanitize("cfi")))
OnCharacteristicWriteRequestInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)136 ErrCode BluetoothGattServerCallbackStub::OnCharacteristicWriteRequestInner(
137     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
138 {
139     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
140     if (!device) {
141         return TRANSACTION_ERR;
142     }
143     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
144     if (!characteristic) {
145         return TRANSACTION_ERR;
146     }
147     bool needRespones = data.ReadBool();
148 
149     stub->OnCharacteristicWriteRequest(*device, *characteristic, needRespones);
150 
151     return NO_ERROR;
152 }
153 __attribute__((no_sanitize("cfi")))
OnDescriptorReadRequestInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)154 ErrCode BluetoothGattServerCallbackStub::OnDescriptorReadRequestInner(
155     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
156 {
157     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
158     if (!device) {
159         return TRANSACTION_ERR;
160     }
161     std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
162     if (!descriptor) {
163         return TRANSACTION_ERR;
164     }
165 
166     stub->OnDescriptorReadRequest(*device, *descriptor);
167 
168     return NO_ERROR;
169 }
170 __attribute__((no_sanitize("cfi")))
OnDescriptorWriteRequestInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)171 ErrCode BluetoothGattServerCallbackStub::OnDescriptorWriteRequestInner(
172     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
173 {
174     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
175     if (!device) {
176         return TRANSACTION_ERR;
177     }
178     std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
179     if (!descriptor) {
180         return TRANSACTION_ERR;
181     }
182 
183     stub->OnDescriptorWriteRequest(*device, *descriptor);
184 
185     return NO_ERROR;
186 }
187 __attribute__((no_sanitize("cfi")))
OnMtuChangedInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)188 ErrCode BluetoothGattServerCallbackStub::OnMtuChangedInner(
189     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
190 {
191     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
192     if (!device) {
193         return TRANSACTION_ERR;
194     }
195     int32_t mtu = data.ReadInt32();
196 
197     stub->OnMtuChanged(*device, mtu);
198 
199     return NO_ERROR;
200 }
201 __attribute__((no_sanitize("cfi")))
OnNotifyConfirmInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)202 ErrCode BluetoothGattServerCallbackStub::OnNotifyConfirmInner(
203     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
204 {
205     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
206     if (!device) {
207         return TRANSACTION_ERR;
208     }
209     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
210     if (!characteristic) {
211         return TRANSACTION_ERR;
212     }
213     int32_t result = data.ReadInt32();
214 
215     stub->OnNotifyConfirm(*device, *characteristic, result);
216 
217     return NO_ERROR;
218 }
219 __attribute__((no_sanitize("cfi")))
OnConnectionParameterChangedInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)220 ErrCode BluetoothGattServerCallbackStub::OnConnectionParameterChangedInner(
221     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
222 {
223     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
224     if (!device) {
225         return TRANSACTION_ERR;
226     }
227     int32_t interval = data.ReadInt32();
228     int32_t latency = data.ReadInt32();
229     int32_t timeout = data.ReadInt32();
230     int32_t status = data.ReadInt32();
231 
232     stub->OnConnectionParameterChanged(*device, interval, latency, timeout, status);
233 
234     return NO_ERROR;
235 }
236 }  // namespace Bluetooth
237 }  // namespace OHOS