• 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_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     int32_t disconnectReason = data.ReadInt32();
115     stub->OnConnectionStateChanged(*device, ret, state, disconnectReason);
116 
117     return NO_ERROR;
118 }
119 __attribute__((no_sanitize("cfi")))
OnAddServiceInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)120 ErrCode BluetoothGattServerCallbackStub::OnAddServiceInner(
121     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
122 {
123     HILOGD("BluetoothGattServerCallbackStub::OnAddServiceInner Triggered!");
124     int32_t ret = data.ReadInt32();
125     std::shared_ptr<BluetoothGattService> service(data.ReadParcelable<BluetoothGattService>());
126     if (!service) {
127         return TRANSACTION_ERR;
128     }
129 
130     stub->OnAddService(ret, *service);
131 
132     service = nullptr;
133 
134     return NO_ERROR;
135 }
136 __attribute__((no_sanitize("cfi")))
OnCharacteristicWriteRequestInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)137 ErrCode BluetoothGattServerCallbackStub::OnCharacteristicWriteRequestInner(
138     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
139 {
140     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
141     if (!device) {
142         return TRANSACTION_ERR;
143     }
144     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
145     if (!characteristic) {
146         return TRANSACTION_ERR;
147     }
148     bool needRespones = data.ReadBool();
149 
150     stub->OnCharacteristicWriteRequest(*device, *characteristic, needRespones);
151 
152     return NO_ERROR;
153 }
154 __attribute__((no_sanitize("cfi")))
OnDescriptorReadRequestInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)155 ErrCode BluetoothGattServerCallbackStub::OnDescriptorReadRequestInner(
156     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
157 {
158     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
159     if (!device) {
160         return TRANSACTION_ERR;
161     }
162     std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
163     if (!descriptor) {
164         return TRANSACTION_ERR;
165     }
166 
167     stub->OnDescriptorReadRequest(*device, *descriptor);
168 
169     return NO_ERROR;
170 }
171 __attribute__((no_sanitize("cfi")))
OnDescriptorWriteRequestInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)172 ErrCode BluetoothGattServerCallbackStub::OnDescriptorWriteRequestInner(
173     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
174 {
175     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
176     if (!device) {
177         return TRANSACTION_ERR;
178     }
179     std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
180     if (!descriptor) {
181         return TRANSACTION_ERR;
182     }
183 
184     stub->OnDescriptorWriteRequest(*device, *descriptor);
185 
186     return NO_ERROR;
187 }
188 __attribute__((no_sanitize("cfi")))
OnMtuChangedInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)189 ErrCode BluetoothGattServerCallbackStub::OnMtuChangedInner(
190     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
191 {
192     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
193     if (!device) {
194         return TRANSACTION_ERR;
195     }
196     int32_t mtu = data.ReadInt32();
197 
198     stub->OnMtuChanged(*device, mtu);
199 
200     return NO_ERROR;
201 }
202 __attribute__((no_sanitize("cfi")))
OnNotifyConfirmInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)203 ErrCode BluetoothGattServerCallbackStub::OnNotifyConfirmInner(
204     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
205 {
206     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
207     if (!device) {
208         return TRANSACTION_ERR;
209     }
210     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
211     if (!characteristic) {
212         return TRANSACTION_ERR;
213     }
214     int32_t result = data.ReadInt32();
215 
216     stub->OnNotifyConfirm(*device, *characteristic, result);
217 
218     return NO_ERROR;
219 }
220 __attribute__((no_sanitize("cfi")))
OnConnectionParameterChangedInner(BluetoothGattServerCallbackStub * stub,MessageParcel & data,MessageParcel & reply)221 ErrCode BluetoothGattServerCallbackStub::OnConnectionParameterChangedInner(
222     BluetoothGattServerCallbackStub *stub, MessageParcel &data, MessageParcel &reply)
223 {
224     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
225     if (!device) {
226         return TRANSACTION_ERR;
227     }
228     int32_t interval = data.ReadInt32();
229     int32_t latency = data.ReadInt32();
230     int32_t timeout = data.ReadInt32();
231     int32_t status = data.ReadInt32();
232 
233     stub->OnConnectionParameterChanged(*device, interval, latency, timeout, status);
234 
235     return NO_ERROR;
236 }
237 }  // namespace Bluetooth
238 }  // namespace OHOS