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