1 /*
2 * Copyright (C) 2021 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_client_callback_proxy.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
OnConnectionStateChanged(int32_t state,int32_t newState,int32_t disconnectReason)21 void BluetoothGattClientCallbackProxy::OnConnectionStateChanged(
22 int32_t state, int32_t newState, int32_t disconnectReason)
23 {
24 HILOGI("BluetoothGattClientCallbackProxy::OnConnectionStateChanged Triggered!");
25 MessageParcel data;
26 if (!data.WriteInterfaceToken(BluetoothGattClientCallbackProxy::GetDescriptor())) {
27 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionStateChanged WriteInterfaceToken error");
28 return;
29 }
30 if (!data.WriteInt32(state)) {
31 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionStateChanged transport error");
32 return;
33 }
34 if (!data.WriteInt32(newState)) {
35 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionStateChanged transport error");
36 return;
37 }
38 if (!data.WriteInt32(disconnectReason)) {
39 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionStateChanged transport error");
40 return;
41 }
42 MessageParcel reply;
43 MessageOption option {
44 MessageOption::TF_ASYNC
45 };
46 int error = Remote()->SendRequest(
47 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_CONNECT_STATE_CHANGE, data, reply, option);
48 if (error != NO_ERROR) {
49 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionStateChanged done fail, error: %{public}d", error);
50 return;
51 }
52 }
53
OnCharacteristicChanged(const BluetoothGattCharacteristic & characteristic)54 void BluetoothGattClientCallbackProxy::OnCharacteristicChanged(const BluetoothGattCharacteristic &characteristic)
55 {
56 HILOGI("BluetoothGattClientCallbackProxy::OnCharacteristicChanged Triggered!");
57 MessageParcel data;
58 if (!data.WriteInterfaceToken(BluetoothGattClientCallbackProxy::GetDescriptor())) {
59 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicChanged WriteInterfaceToken error");
60 return;
61 }
62 if (!data.WriteParcelable(&characteristic)) {
63 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicChanged transport error");
64 return;
65 }
66 MessageParcel reply;
67 MessageOption option {
68 MessageOption::TF_ASYNC
69 };
70 int error = Remote()->SendRequest(
71 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_CHARACTER_CHANGE, data, reply, option);
72 if (error != NO_ERROR) {
73 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicChanged done fail, error: %{public}d", error);
74 return;
75 }
76 }
77
OnCharacteristicRead(int32_t ret,const BluetoothGattCharacteristic & characteristic)78 void BluetoothGattClientCallbackProxy::OnCharacteristicRead(
79 int32_t ret, const BluetoothGattCharacteristic &characteristic)
80 {
81 HILOGI("BluetoothGattClientCallbackProxy::OnCharacteristicRead Triggered!");
82 MessageParcel data;
83 if (!data.WriteInterfaceToken(BluetoothGattClientCallbackProxy::GetDescriptor())) {
84 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicRead WriteInterfaceToken error");
85 return;
86 }
87 if (!data.WriteInt32(ret)) {
88 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicRead transport error");
89 return;
90 }
91 if (!data.WriteParcelable(&characteristic)) {
92 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicRead transport error");
93 return;
94 }
95 MessageParcel reply;
96 MessageOption option {
97 MessageOption::TF_ASYNC
98 };
99 int error = Remote()->SendRequest(
100 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_CHARACTER_READ, data, reply, option);
101 if (error != NO_ERROR) {
102 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicRead done fail, error: %{public}d", error);
103 return;
104 }
105 }
106
OnCharacteristicWrite(int32_t ret,const BluetoothGattCharacteristic & characteristic)107 void BluetoothGattClientCallbackProxy::OnCharacteristicWrite(
108 int32_t ret, const BluetoothGattCharacteristic &characteristic)
109 {
110 HILOGI("BluetoothGattClientCallbackProxy::OnCharacteristicWrite Triggered!");
111 MessageParcel data;
112 if (!data.WriteInterfaceToken(BluetoothGattClientCallbackProxy::GetDescriptor())) {
113 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicWrite WriteInterfaceToken error");
114 return;
115 }
116 if (!data.WriteInt32(ret)) {
117 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicWrite transport error");
118 return;
119 }
120 if (!data.WriteParcelable(&characteristic)) {
121 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicWrite transport error");
122 return;
123 }
124 MessageParcel reply;
125 MessageOption option {
126 MessageOption::TF_ASYNC
127 };
128 int error = Remote()->SendRequest(
129 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_CHARACTER_WRITE, data, reply, option);
130 if (error != NO_ERROR) {
131 HILOGE("BluetoothGattClientCallbackProxy::OnCharacteristicWrite done fail, error: %{public}d", error);
132 return;
133 }
134 }
135
OnDescriptorRead(int32_t ret,const BluetoothGattDescriptor & descriptor)136 void BluetoothGattClientCallbackProxy::OnDescriptorRead(int32_t ret, const BluetoothGattDescriptor &descriptor)
137 {
138 HILOGI("BluetoothGattClientCallbackProxy::OnDescriptorRead Triggered!");
139 MessageParcel data;
140 if (!data.WriteInterfaceToken(BluetoothGattClientCallbackProxy::GetDescriptor())) {
141 HILOGE("BluetoothGattClientCallbackProxy::OnDescriptorRead WriteInterfaceToken error");
142 return;
143 }
144 if (!data.WriteInt32(ret)) {
145 HILOGE("BluetoothGattClientCallbackProxy::OnDescriptorRead transport error");
146 return;
147 }
148 if (!data.WriteParcelable(&descriptor)) {
149 HILOGE("BluetoothGattClientCallbackProxy::OnDescriptorRead transport error");
150 return;
151 }
152 MessageParcel reply;
153 MessageOption option {
154 MessageOption::TF_ASYNC
155 };
156 int error = Remote()->SendRequest(
157 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_DESCRIPTOR_READ, data, reply, option);
158 if (error != NO_ERROR) {
159 HILOGE("BluetoothGattClientCallbackProxy::OnDescriptorRead done fail, error: %{public}d", error);
160 return;
161 }
162 }
163
OnDescriptorWrite(int32_t ret,const BluetoothGattDescriptor & descriptor)164 void BluetoothGattClientCallbackProxy::OnDescriptorWrite(int32_t ret, const BluetoothGattDescriptor &descriptor)
165 {
166 HILOGI("BluetoothGattClientCallbackProxy::OnDescriptorWrite Triggered!");
167 MessageParcel data;
168 if (!data.WriteInterfaceToken(BluetoothGattClientCallbackProxy::GetDescriptor())) {
169 HILOGE("BluetoothGattClientCallbackProxy::OnDescriptorWrite WriteInterfaceToken error");
170 return;
171 }
172 if (!data.WriteInt32(ret)) {
173 HILOGE("BluetoothGattClientCallbackProxy::OnDescriptorWrite transport error");
174 return;
175 }
176 if (!data.WriteParcelable(&descriptor)) {
177 HILOGE("BluetoothGattClientCallbackProxy::OnDescriptorWrite transport error");
178 return;
179 }
180 MessageParcel reply;
181 MessageOption option {
182 MessageOption::TF_ASYNC
183 };
184 int error = Remote()->SendRequest(
185 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_DESCRIPTOR_WRITE, data, reply, option);
186 if (error != NO_ERROR) {
187 HILOGE("BluetoothGattClientCallbackProxy::OnDescriptorWrite done fail, error: %{public}d", error);
188 return;
189 }
190 }
191
OnMtuChanged(int32_t state,int32_t mtu)192 void BluetoothGattClientCallbackProxy::OnMtuChanged(int32_t state, int32_t mtu)
193 {
194 HILOGI("BluetoothGattClientCallbackProxy::OnMtuChanged Triggered!");
195 MessageParcel data;
196 if (!data.WriteInterfaceToken(BluetoothGattClientCallbackProxy::GetDescriptor())) {
197 HILOGE("BluetoothGattClientCallbackProxy::OnMtuChanged WriteInterfaceToken error");
198 return;
199 }
200 if (!data.WriteInt32(state)) {
201 HILOGE("BluetoothGattClientCallbackProxy::OnMtuChanged transport error");
202 return;
203 }
204 if (!data.WriteInt32(mtu)) {
205 HILOGE("BluetoothGattClientCallbackProxy::OnMtuChanged transport error");
206 return;
207 }
208 MessageParcel reply;
209 MessageOption option {
210 MessageOption::TF_ASYNC
211 };
212 int error = Remote()->SendRequest(
213 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_MTU_UPDATE, data, reply, option);
214 if (error != NO_ERROR) {
215 HILOGE("BluetoothGattClientCallbackProxy::OnMtuChanged done fail, error: %{public}d", error);
216 return;
217 }
218 }
219
OnServicesDiscovered(int32_t status)220 void BluetoothGattClientCallbackProxy::OnServicesDiscovered(int32_t status)
221 {
222 HILOGI("BluetoothGattClientCallbackProxy::OnServicesDiscovered Triggered!");
223 MessageParcel data;
224 if (!data.WriteInterfaceToken(BluetoothGattClientCallbackProxy::GetDescriptor())) {
225 HILOGE("BluetoothGattClientCallbackProxy::OnServicesDiscovered WriteInterfaceToken error");
226 return;
227 }
228 if (!data.WriteInt32(status)) {
229 HILOGE("BluetoothGattClientCallbackProxy::OnServicesDiscovered transport error");
230 return;
231 }
232 MessageParcel reply;
233 MessageOption option {
234 MessageOption::TF_ASYNC
235 };
236 int error = Remote()->SendRequest(
237 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_SERVICES_DISCOVER, data, reply, option);
238 if (error != NO_ERROR) {
239 HILOGE("BluetoothGattClientCallbackProxy::OnServicesDiscovered done fail, error: %{public}d", error);
240 return;
241 }
242 }
243
OnConnectionParameterChanged(int32_t interval,int32_t latency,int32_t timeout,int32_t status)244 void BluetoothGattClientCallbackProxy::OnConnectionParameterChanged(
245 int32_t interval, int32_t latency, int32_t timeout, int32_t status)
246 {
247 HILOGI("BluetoothGattClientCallbackProxy::OnConnectionParameterChanged Triggered!");
248 MessageParcel data;
249 if (!data.WriteInterfaceToken(BluetoothGattClientCallbackProxy::GetDescriptor())) {
250 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionParameterChanged WriteInterfaceToken error");
251 return;
252 }
253 if (!data.WriteInt32(interval)) {
254 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionParameterChanged transport error");
255 return;
256 }
257 if (!data.WriteInt32(latency)) {
258 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionParameterChanged transport error");
259 return;
260 }
261 if (!data.WriteInt32(timeout)) {
262 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionParameterChanged transport error");
263 return;
264 }
265 if (!data.WriteInt32(status)) {
266 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionParameterChanged transport error");
267 return;
268 }
269 MessageParcel reply;
270 MessageOption option {
271 MessageOption::TF_ASYNC
272 };
273 int error = Remote()->SendRequest(
274 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_CONNECTION_PARA_CHANGE, data, reply, option);
275 if (error != NO_ERROR) {
276 HILOGE("BluetoothGattClientCallbackProxy::OnConnectionParameterChanged done fail, error: %{public}d", error);
277 return;
278 }
279 }
280
OnServicesChanged(std::vector<BluetoothGattService> & service)281 void BluetoothGattClientCallbackProxy::OnServicesChanged(std::vector<BluetoothGattService> &service)
282 {
283 HILOGI("BluetoothGattClientCallbackProxy::OnServicesChanged Triggered!");
284 MessageParcel data;
285 if (!data.WriteInterfaceToken(BluetoothGattClientCallbackProxy::GetDescriptor())) {
286 HILOGE("BluetoothGattClientCallbackProxy::OnServicesChanged WriteInterfaceToken error");
287 return;
288 }
289 int num = service.size();
290 if (!data.WriteInt32(num)) {
291 HILOGE("BluetoothGattClientCallbackProxy::OnServicesChanged transport error");
292 return;
293 }
294 for (int i = 0; i < num; i++) {
295 if (!data.WriteParcelable(&service[i])) {
296 HILOGE("BluetoothGattClientCallbackProxy::OnServicesChanged transport error");
297 return;
298 }
299 }
300 MessageParcel reply;
301 MessageOption option {
302 MessageOption::TF_ASYNC
303 };
304 int error = Remote()->SendRequest(
305 BluetoothGattClientCallbackInterfaceCode::BT_GATT_CLIENT_CALLBACK_SERVICES_CHANGED, data, reply, option);
306 if (error != NO_ERROR) {
307 HILOGE("BluetoothGattClientCallbackProxy::OnServicesChanged done fail, error: %{public}d", error);
308 return;
309 }
310 }
311
OnReadRemoteRssiValue(const bluetooth::RawAddress & addr,int rssi,int status)312 void BluetoothGattClientCallbackProxy::OnReadRemoteRssiValue(const bluetooth::RawAddress &addr, int rssi, int status)
313 {
314 return;
315 }
316 } // namespace Bluetooth
317 } // namespace OHOS