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