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