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_proxy.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
OnCharacteristicReadRequest(const BluetoothGattDevice & device,const BluetoothGattCharacteristic & characteristic)21 void BluetoothGattServerCallbackProxy::OnCharacteristicReadRequest(
22 const BluetoothGattDevice &device, const BluetoothGattCharacteristic &characteristic)
23 {
24 MessageParcel data;
25 if (!data.WriteInterfaceToken(BluetoothGattServerCallbackProxy::GetDescriptor())) {
26 HILOGE("BluetoothGattServerCallbackProxy::OnCharacteristicReadRequest WriteInterfaceToken error");
27 return;
28 }
29 if (!data.WriteParcelable(&device)) {
30 HILOGE("BluetoothGattServerCallbackProxy::OnCharacteristicReadRequest error");
31 return;
32 }
33 if (!data.WriteParcelable(&characteristic)) {
34 HILOGE("BluetoothGattServerCallbackProxy::OnCharacteristicReadRequest error");
35 return;
36 }
37 MessageParcel reply;
38 MessageOption option {
39 MessageOption::TF_ASYNC
40 };
41 int error = Remote()->SendRequest(
42 BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CHARACTERISTIC_READREQUEST, data, reply, option);
43 if (error != NO_ERROR) {
44 HILOGE("BluetoothGattServerCallbackProxy::OnCharacteristicReadRequest done fail, error: %d", error);
45 return;
46 }
47 return;
48 }
OnConnectionStateChanged(const BluetoothGattDevice & device,int32_t ret,int32_t state)49 void BluetoothGattServerCallbackProxy::OnConnectionStateChanged(
50 const BluetoothGattDevice &device, int32_t ret, int32_t state)
51 {
52 HILOGI("BluetoothGattServerCallbackProxy::OnConnectionStateChanged Triggered!");
53 MessageParcel data;
54 if (!data.WriteInterfaceToken(BluetoothGattServerCallbackProxy::GetDescriptor())) {
55 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionStateChanged WriteInterfaceToken error");
56 return;
57 }
58 if (!data.WriteParcelable(&device)) {
59 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionStateChanged error");
60 return;
61 }
62 if (!data.WriteInt32(ret)) {
63 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionStateChanged error");
64 return;
65 }
66 if (!data.WriteInt32(state)) {
67 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionStateChanged error");
68 return;
69 }
70 MessageParcel reply;
71 MessageOption option {
72 MessageOption::TF_ASYNC
73 };
74 int error = Remote()->SendRequest(
75 BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CONNECTIONSTATE_CHANGED, data, reply, option);
76 if (error != NO_ERROR) {
77 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionStateChanged done fail, error: %d", error);
78 return;
79 }
80 return;
81 }
OnAddService(int32_t ret,const BluetoothGattService & service)82 void BluetoothGattServerCallbackProxy::OnAddService(int32_t ret, const BluetoothGattService &service)
83 {
84 MessageParcel data;
85 if (!data.WriteInterfaceToken(BluetoothGattServerCallbackProxy::GetDescriptor())) {
86 HILOGE("BluetoothGattServerCallbackProxy::OnAddService WriteInterfaceToken error");
87 return;
88 }
89 if (!data.WriteInt32(ret)) {
90 HILOGE("BluetoothGattServerCallbackProxy::OnAddService error");
91 return;
92 }
93 if (!data.WriteParcelable(&service)) {
94 HILOGE("BluetoothGattServerCallbackProxy::OnAddService error");
95 return;
96 }
97 MessageParcel reply;
98 MessageOption option {
99 MessageOption::TF_ASYNC
100 };
101 int error = Remote()->SendRequest(
102 BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_ADD_SERVICE, data, reply, option);
103 if (error != NO_ERROR) {
104 HILOGE("BluetoothGattServerCallbackProxy::OnAddService done fail, error: %d", error);
105 return;
106 }
107 return;
108 }
OnCharacteristicWriteRequest(const BluetoothGattDevice & device,const BluetoothGattCharacteristic & characteristic,bool needRespones)109 void BluetoothGattServerCallbackProxy::OnCharacteristicWriteRequest(
110 const BluetoothGattDevice &device, const BluetoothGattCharacteristic &characteristic, bool needRespones)
111 {
112 MessageParcel data;
113 if (!data.WriteInterfaceToken(BluetoothGattServerCallbackProxy::GetDescriptor())) {
114 HILOGE("BluetoothGattServerCallbackProxy::OnCharacteristicWriteRequest WriteInterfaceToken error");
115 return;
116 }
117 if (!data.WriteParcelable(&device)) {
118 HILOGE("BluetoothGattServerCallbackProxy::OnCharacteristicWriteRequest error");
119 return;
120 }
121 if (!data.WriteParcelable(&characteristic)) {
122 HILOGE("BluetoothGattServerCallbackProxy::OnCharacteristicWriteRequest error");
123 return;
124 }
125 if (!data.WriteBool(needRespones)) {
126 HILOGE("BluetoothGattServerProxy::OnCharacteristicWriteRequest error");
127 return;
128 }
129 MessageParcel reply;
130 MessageOption option {
131 MessageOption::TF_ASYNC
132 };
133 int error = Remote()->SendRequest(
134 BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CHARACTERISTIC_WRITE_REQUEST,
135 data, reply, option);
136 if (error != NO_ERROR) {
137 HILOGE("BluetoothGattServerCallbackProxy::OnCharacteristicWriteRequest done fail, error: %d", error);
138 return;
139 }
140 return;
141 }
OnDescriptorReadRequest(const BluetoothGattDevice & device,const BluetoothGattDescriptor & descriptor)142 void BluetoothGattServerCallbackProxy::OnDescriptorReadRequest(
143 const BluetoothGattDevice &device, const BluetoothGattDescriptor &descriptor)
144 {
145 MessageParcel data;
146 if (!data.WriteInterfaceToken(BluetoothGattServerCallbackProxy::GetDescriptor())) {
147 HILOGE("BluetoothGattServerCallbackProxy::OnDescriptorReadRequest WriteInterfaceToken error");
148 return;
149 }
150 if (!data.WriteParcelable(&device)) {
151 HILOGE("BluetoothGattServerCallbackProxy::OnDescriptorReadRequest error");
152 return;
153 }
154 if (!data.WriteParcelable(&descriptor)) {
155 HILOGE("BluetoothGattServerCallbackProxy::OnDescriptorReadRequest error");
156 return;
157 }
158 MessageParcel reply;
159 MessageOption option {
160 MessageOption::TF_ASYNC
161 };
162 int error = Remote()->SendRequest(
163 BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_DESCRIPTOR_READ_REQUEST, data, reply, option);
164 if (error != NO_ERROR) {
165 HILOGE("BluetoothGattServerCallbackProxy::OnDescriptorReadRequest done fail, error: %d", error);
166 return;
167 }
168 return;
169 }
OnDescriptorWriteRequest(const BluetoothGattDevice & device,const BluetoothGattDescriptor & descriptor)170 void BluetoothGattServerCallbackProxy::OnDescriptorWriteRequest(
171 const BluetoothGattDevice &device, const BluetoothGattDescriptor &descriptor)
172 {
173 MessageParcel data;
174 if (!data.WriteInterfaceToken(BluetoothGattServerCallbackProxy::GetDescriptor())) {
175 HILOGE("BluetoothGattServerCallbackProxy::OnDescriptorWriteRequest WriteInterfaceToken error");
176 return;
177 }
178 if (!data.WriteParcelable(&device)) {
179 HILOGE("BluetoothGattServerCallbackProxy::OnDescriptorWriteRequest error");
180 return;
181 }
182 if (!data.WriteParcelable(&descriptor)) {
183 HILOGE("BluetoothGattServerCallbackProxy::OnDescriptorWriteRequest error");
184 return;
185 }
186 MessageParcel reply;
187 MessageOption option {
188 MessageOption::TF_ASYNC
189 };
190 int error = Remote()->SendRequest(
191 BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_DESCRIPTOR_WRITE_REQUEST, data, reply, option);
192 if (error != NO_ERROR) {
193 HILOGE("BluetoothGattServerCallbackProxy::OnDescriptorWriteRequest done fail, error: %d", error);
194 return;
195 }
196 return;
197 }
OnMtuChanged(const BluetoothGattDevice & device,int32_t mtu)198 void BluetoothGattServerCallbackProxy::OnMtuChanged(const BluetoothGattDevice &device, int32_t mtu)
199 {
200 MessageParcel data;
201 if (!data.WriteInterfaceToken(BluetoothGattServerCallbackProxy::GetDescriptor())) {
202 HILOGE("BluetoothGattServerCallbackProxy::OnMtuUpdateOnMtuChanged WriteInterfaceToken error");
203 return;
204 }
205 if (!data.WriteParcelable(&device)) {
206 HILOGE("BluetoothGattServerCallbackProxy::OnMtuUpdateOnMtuChanged error");
207 return;
208 }
209 if (!data.WriteInt32(mtu)) {
210 HILOGE("BluetoothGattServerCallbackProxy::OnMtuUpdateOnMtuChanged error");
211 return;
212 }
213 MessageParcel reply;
214 MessageOption option {
215 MessageOption::TF_ASYNC
216 };
217 int error = Remote()->SendRequest(
218 BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_MTU_CHANGED, data, reply, option);
219 if (error != NO_ERROR) {
220 HILOGE("BluetoothGattServerCallbackProxy::OnMtuUpdateOnMtuChanged done fail, error: %d", error);
221 return;
222 }
223 return;
224 }
OnNotifyConfirm(const BluetoothGattDevice & device,const BluetoothGattCharacteristic & characteristic,int result)225 void BluetoothGattServerCallbackProxy::OnNotifyConfirm(
226 const BluetoothGattDevice &device, const BluetoothGattCharacteristic &characteristic, int result)
227 {
228 MessageParcel data;
229 if (!data.WriteInterfaceToken(BluetoothGattServerCallbackProxy::GetDescriptor())) {
230 HILOGE("BluetoothGattServerCallbackProxy::OnNotifyConfirm WriteInterfaceToken error");
231 return;
232 }
233 if (!data.WriteParcelable(&device)) {
234 HILOGE("BluetoothGattServerCallbackProxy::OnNotifyConfirm error");
235 return;
236 }
237 if (!data.WriteParcelable(&characteristic)) {
238 HILOGE("BluetoothGattServerCallbackProxy::OnNotifyConfirm error");
239 return;
240 }
241 if (!data.WriteInt32(result)) {
242 HILOGE("BluetoothGattServerCallbackProxy::OnNotifyConfirm error");
243 return;
244 }
245 MessageParcel reply;
246 MessageOption option {
247 MessageOption::TF_ASYNC
248 };
249 int error = Remote()->SendRequest(
250 BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_NOTIFY_CONFIRM, data, reply, option);
251 if (error != NO_ERROR) {
252 HILOGE("BluetoothGattServerCallbackProxy::OnNotifyConfirm done fail, error: %d", error);
253 return;
254 }
255 return;
256 }
OnConnectionParameterChanged(const BluetoothGattDevice & device,int32_t interval,int32_t latency,int32_t timeout,int32_t status)257 void BluetoothGattServerCallbackProxy::OnConnectionParameterChanged(
258 const BluetoothGattDevice &device, int32_t interval, int32_t latency, int32_t timeout, int32_t status)
259 {
260 MessageParcel data;
261 if (!data.WriteInterfaceToken(BluetoothGattServerCallbackProxy::GetDescriptor())) {
262 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionParameterChanged WriteInterfaceToken error");
263 return;
264 }
265 if (!data.WriteParcelable(&device)) {
266 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionParameterChanged error");
267 return;
268 }
269 if (!data.WriteInt32(interval)) {
270 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionParameterChanged error");
271 return;
272 }
273 if (!data.WriteInt32(latency)) {
274 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionParameterChanged error");
275 return;
276 }
277 if (!data.WriteInt32(timeout)) {
278 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionParameterChanged error");
279 return;
280 }
281 if (!data.WriteInt32(status)) {
282 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionParameterChanged error");
283 return;
284 }
285 MessageParcel reply;
286 MessageOption option {
287 MessageOption::TF_ASYNC
288 };
289 int error = Remote()->SendRequest(
290 BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CONNECTION_PARAMETER_CHANGED,
291 data, reply, option);
292 if (error != NO_ERROR) {
293 HILOGE("BluetoothGattServerCallbackProxy::OnConnectionParameterChanged done fail, error: %d", error);
294 return;
295 }
296 return;
297 }
298 } // namespace Bluetooth
299 } // namespace OHOS