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_server_proxy.h"
17 #include "bluetooth_log.h"
18 #include "bluetooth_errorcode.h"
19
20 namespace OHOS {
21 namespace Bluetooth {
AddService(int32_t appId,BluetoothGattService * services)22 int BluetoothGattServerProxy::AddService(int32_t appId, BluetoothGattService *services)
23 {
24 MessageParcel data;
25 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
26 HILOGE("BluetoothGattServerProxy::AddService WriteInterfaceToken error");
27 return BT_ERR_IPC_TRANS_FAILED;
28 }
29 if (!data.WriteInt32(appId)) {
30 HILOGE("BluetoothGattServerProxy::AddService error");
31 return BT_ERR_IPC_TRANS_FAILED;
32 }
33
34 if (!data.WriteParcelable(services)) {
35 HILOGE("BluetoothGattServerProxy::AddService error");
36 return BT_ERR_IPC_TRANS_FAILED;
37 }
38
39 MessageParcel reply;
40 MessageOption option {
41 MessageOption::TF_SYNC
42 };
43
44 int error = Remote()->SendRequest(BluetoothGattServerInterfaceCode::GATT_SERVER_ADD_SERVICE, data, reply, option);
45 if (error != BT_NO_ERROR) {
46 HILOGE("BluetoothGattServerProxy::AddService done fail, error: %{public}d", error);
47 return BT_ERR_IPC_TRANS_FAILED;
48 }
49 return reply.ReadInt32();
50 }
ClearServices(int appId)51 void BluetoothGattServerProxy::ClearServices(int appId)
52 {
53 MessageParcel data;
54 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
55 HILOGE("BluetoothGattServerProxy::ClearServices WriteInterfaceToken error");
56 return;
57 }
58 if (!data.WriteInt32(appId)) {
59 HILOGE("BluetoothGattServerProxy::ClearServices error");
60 return;
61 }
62 MessageParcel reply;
63 MessageOption option {
64 MessageOption::TF_SYNC
65 };
66
67 int error = Remote()->SendRequest(
68 BluetoothGattServerInterfaceCode::GATT_SERVER_CLEAR_SERVICES, data, reply, option);
69 if (error != NO_ERROR) {
70 HILOGE("BluetoothGattServerProxy::ClearServices done fail, error: %{public}d", error);
71 return;
72 }
73 return;
74 }
CancelConnection(const BluetoothGattDevice & device)75 void BluetoothGattServerProxy::CancelConnection(const BluetoothGattDevice &device)
76 {
77 MessageParcel data;
78 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
79 HILOGE("BluetoothGattServerProxy::CancelConnection WriteInterfaceToken error");
80 return;
81 }
82 if (!data.WriteParcelable(&device)) {
83 HILOGE("BluetoothGattServerProxy::CancelConnection error");
84 return;
85 }
86 MessageParcel reply;
87 MessageOption option {
88 MessageOption::TF_SYNC
89 };
90 int error = Remote()->SendRequest(
91 BluetoothGattServerInterfaceCode::GATT_SERVER_CANCEL_CONNECTION, data, reply, option);
92 if (error != NO_ERROR) {
93 HILOGE("BluetoothGattServerProxy::CancelConnection done fail, error: %{public}d", error);
94 return;
95 }
96 return;
97 }
RegisterApplication(const sptr<IBluetoothGattServerCallback> & callback)98 int BluetoothGattServerProxy::RegisterApplication(const sptr<IBluetoothGattServerCallback> &callback)
99 {
100 MessageParcel data;
101 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
102 HILOGE("BluetoothGattServerProxy::RegisterApplication WriteInterfaceToken error");
103 return ERROR;
104 }
105 if (!data.WriteRemoteObject(callback->AsObject())) {
106 HILOGE("BluetoothGattServerProxy::RegisterApplication error");
107 return ERROR;
108 }
109
110 MessageParcel reply;
111 MessageOption option {
112 MessageOption::TF_SYNC
113 };
114 int error = Remote()->SendRequest(BluetoothGattServerInterfaceCode::GATT_SERVER_REGISTER, data, reply, option);
115 if (error != NO_ERROR) {
116 HILOGE("BluetoothGattServerProxy::RegisterApplication done fail, error: %{public}d", error);
117 return ERROR;
118 }
119 return reply.ReadInt32();
120 }
DeregisterApplication(int appId)121 int BluetoothGattServerProxy::DeregisterApplication(int appId)
122 {
123 MessageParcel data;
124 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
125 HILOGE("BluetoothGattServerProxy::WriteInterfaceToken WriteInterfaceToken error");
126 return BT_ERR_IPC_TRANS_FAILED;
127 }
128 if (!data.WriteInt32(appId)) {
129 HILOGE("BluetoothGattServerProxy::DeregisterApplication error");
130 return BT_ERR_IPC_TRANS_FAILED;
131 }
132 MessageParcel reply;
133 MessageOption option {
134 MessageOption::TF_SYNC
135 };
136
137 int error = Remote()->SendRequest(BluetoothGattServerInterfaceCode::GATT_SERVER_DEREGISTER, data, reply, option);
138 if (error != NO_ERROR) {
139 HILOGE("BluetoothGattServerProxy::DeregisterApplication done fail, error: %{public}d", error);
140 return BT_ERR_IPC_TRANS_FAILED;
141 }
142 return reply.ReadInt32();
143 }
NotifyClient(const BluetoothGattDevice & device,BluetoothGattCharacteristic * characteristic,bool needConfirm)144 int BluetoothGattServerProxy::NotifyClient(
145 const BluetoothGattDevice &device, BluetoothGattCharacteristic *characteristic, bool needConfirm)
146 {
147 MessageParcel data;
148 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
149 HILOGE("BluetoothGattServerProxy::WriteInterfaceToken WriteInterfaceToken error");
150 return BT_ERR_IPC_TRANS_FAILED;
151 }
152 if (!data.WriteParcelable(&device)) {
153 HILOGE("BluetoothGattServerProxy::NotifyClient error");
154 return BT_ERR_IPC_TRANS_FAILED;
155 }
156 if (!data.WriteParcelable(characteristic)) {
157 HILOGE("BluetoothGattServerProxy::NotifyClient error");
158 return BT_ERR_IPC_TRANS_FAILED;
159 }
160 if (!data.WriteBool(needConfirm)) {
161 HILOGE("BluetoothGattServerProxy::NotifyClient error");
162 return BT_ERR_IPC_TRANS_FAILED;
163 }
164 MessageParcel reply;
165 MessageOption option {
166 MessageOption::TF_SYNC
167 };
168
169 int error = Remote()->SendRequest(BluetoothGattServerInterfaceCode::GATT_SERVER_NOTIFY_CLIENT, data, reply, option);
170 if (error != BT_NO_ERROR) {
171 HILOGE("BluetoothGattServerProxy::NotifyClient done fail, error: %{public}d", error);
172 return BT_ERR_IPC_TRANS_FAILED;
173 }
174 return reply.ReadInt32();
175 }
RemoveService(int32_t appId,const BluetoothGattService & services)176 int BluetoothGattServerProxy::RemoveService(int32_t appId, const BluetoothGattService &services)
177 {
178 MessageParcel data;
179 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
180 HILOGE("BluetoothGattServerProxy::RemoveService WriteInterfaceToken error");
181 return BT_ERR_IPC_TRANS_FAILED;
182 }
183 if (!data.WriteInt32(appId)) {
184 HILOGE("BluetoothGattServerProxy::RemoveService error");
185 return BT_ERR_IPC_TRANS_FAILED;
186 }
187 if (!data.WriteParcelable(&services)) {
188 HILOGE("BluetoothGattServerProxy::RemoveService error");
189 return BT_ERR_IPC_TRANS_FAILED;
190 }
191 MessageParcel reply;
192 MessageOption option {
193 MessageOption::TF_SYNC
194 };
195
196 int error = Remote()->SendRequest(
197 BluetoothGattServerInterfaceCode::GATT_SERVER_REMOVE_SERVICE, data, reply, option);
198 if (error != NO_ERROR) {
199 HILOGE("BluetoothGattServerProxy::RemoveService done fail, error: %{public}d", error);
200 return BT_ERR_IPC_TRANS_FAILED;
201 }
202 return reply.ReadInt32();
203 }
RespondCharacteristicRead(const BluetoothGattDevice & device,BluetoothGattCharacteristic * characteristic,int32_t ret)204 int BluetoothGattServerProxy::RespondCharacteristicRead(
205 const BluetoothGattDevice &device, BluetoothGattCharacteristic *characteristic, int32_t ret)
206 {
207 MessageParcel data;
208 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
209 HILOGE("BluetoothGattServerProxy::RespondCharacteristicRead WriteInterfaceToken error");
210 return BT_ERR_IPC_TRANS_FAILED;
211 }
212 if (!data.WriteParcelable(&device)) {
213 HILOGE("BluetoothGattServerProxy::RespondCharacteristicRead error");
214 return BT_ERR_IPC_TRANS_FAILED;
215 }
216 if (!data.WriteParcelable(characteristic)) {
217 HILOGE("BluetoothGattServerProxy::RespondCharacteristicRead error");
218 return BT_ERR_IPC_TRANS_FAILED;
219 }
220 if (!data.WriteInt32(ret)) {
221 HILOGE("BluetoothGattServerProxy::RespondCharacteristicRead error");
222 return BT_ERR_IPC_TRANS_FAILED;
223 }
224 MessageParcel reply;
225 MessageOption option {
226 MessageOption::TF_SYNC
227 };
228 int error =
229 Remote()->SendRequest(
230 BluetoothGattServerInterfaceCode::GATT_SERVER_RESPOND_CHARACTERISTIC_READ, data, reply, option);
231 if (error != BT_NO_ERROR) {
232 HILOGE("BluetoothGattServerProxy::RespondCharacteristicRead done fail, error: %{public}d", error);
233 return BT_ERR_IPC_TRANS_FAILED;
234 }
235 return reply.ReadInt32();
236 }
RespondCharacteristicWrite(const BluetoothGattDevice & device,const BluetoothGattCharacteristic & characteristic,int32_t ret)237 int BluetoothGattServerProxy::RespondCharacteristicWrite(
238 const BluetoothGattDevice &device, const BluetoothGattCharacteristic &characteristic, int32_t ret)
239 {
240 MessageParcel data;
241 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
242 HILOGE("BluetoothGattServerProxy::RespondCharacteristicWrite WriteInterfaceToken error");
243 return BT_ERR_IPC_TRANS_FAILED;
244 }
245 if (!data.WriteParcelable(&device)) {
246 HILOGE("BluetoothGattServerProxy::RespondCharacteristicWrite error");
247 return BT_ERR_IPC_TRANS_FAILED;
248 }
249 if (!data.WriteParcelable(&characteristic)) {
250 HILOGE("BluetoothGattServerProxy::RespondCharacteristicWrite error");
251 return BT_ERR_IPC_TRANS_FAILED;
252 }
253 if (!data.WriteInt32(ret)) {
254 HILOGE("BluetoothGattServerProxy::RespondCharacteristicWrite error");
255 return BT_ERR_IPC_TRANS_FAILED;
256 }
257 MessageParcel reply;
258 MessageOption option {
259 MessageOption::TF_SYNC
260 };
261 int error = Remote()->SendRequest(
262 BluetoothGattServerInterfaceCode::GATT_SERVER_RESPOND_CHARACTERISTIC_WRITE, data, reply, option);
263 if (error != BT_NO_ERROR) {
264 HILOGE("BluetoothGattServerProxy::RespondCharacteristicWrite done fail, error: %{public}d", error);
265 return BT_ERR_IPC_TRANS_FAILED;
266 }
267 return reply.ReadInt32();
268 }
RespondDescriptorRead(const BluetoothGattDevice & device,BluetoothGattDescriptor * descriptor,int32_t ret)269 int BluetoothGattServerProxy::RespondDescriptorRead(
270 const BluetoothGattDevice &device, BluetoothGattDescriptor *descriptor, int32_t ret)
271 {
272 MessageParcel data;
273 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
274 HILOGE("BluetoothGattServerProxy::RespondDescriptorRead WriteInterfaceToken error");
275 return BT_ERR_IPC_TRANS_FAILED;
276 }
277 if (!data.WriteParcelable(&device)) {
278 HILOGE("BluetoothGattServerProxy::RespondDescriptorRead error");
279 return BT_ERR_IPC_TRANS_FAILED;
280 }
281 if (!data.WriteParcelable(descriptor)) {
282 HILOGE("BluetoothGattServerProxy::RespondDescriptorRead error");
283 return BT_ERR_IPC_TRANS_FAILED;
284 }
285 if (!data.WriteInt32(ret)) {
286 HILOGE("BluetoothGattServerProxy::RespondDescriptorRead error");
287 return BT_ERR_IPC_TRANS_FAILED;
288 }
289 MessageParcel reply;
290 MessageOption option {
291 MessageOption::TF_SYNC
292 };
293 int error =
294 Remote()->SendRequest(
295 BluetoothGattServerInterfaceCode::GATT_SERVER_RESPOND_DESCRIPTOR_READ, data, reply, option);
296 if (error != BT_NO_ERROR) {
297 HILOGE("BluetoothGattServerProxy::RespondDescriptorRead done fail, error: %{public}d", error);
298 return BT_ERR_IPC_TRANS_FAILED;
299 }
300 return reply.ReadInt32();
301 }
RespondDescriptorWrite(const BluetoothGattDevice & device,const BluetoothGattDescriptor & descriptor,int32_t ret)302 int BluetoothGattServerProxy::RespondDescriptorWrite(
303 const BluetoothGattDevice &device, const BluetoothGattDescriptor &descriptor, int32_t ret)
304 {
305 MessageParcel data;
306 if (!data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor())) {
307 HILOGE("BluetoothGattServerProxy::RespondDescriptorWrite WriteInterfaceToken error");
308 return BT_ERR_IPC_TRANS_FAILED;
309 }
310 if (!data.WriteParcelable(&device)) {
311 HILOGE("BluetoothGattServerProxy::RespondDescriptorWrite error");
312 return BT_ERR_IPC_TRANS_FAILED;
313 }
314 if (!data.WriteParcelable(&descriptor)) {
315 HILOGE("BluetoothGattServerProxy::RespondDescriptorWrite error");
316 return BT_ERR_IPC_TRANS_FAILED;
317 }
318 if (!data.WriteInt32(ret)) {
319 HILOGE("BluetoothGattServerProxy::RespondDescriptorWrite error");
320 return BT_ERR_IPC_TRANS_FAILED;
321 }
322 MessageParcel reply;
323 MessageOption option {
324 MessageOption::TF_SYNC
325 };
326 int error =
327 Remote()->SendRequest(
328 BluetoothGattServerInterfaceCode::GATT_SERVER_RESPOND_DESCRIPTOR_WRITE, data, reply, option);
329 if (error != BT_NO_ERROR) {
330 HILOGE("BluetoothGattServerProxy::RespondDescriptorWrite done fail, error: %{public}d", error);
331 return BT_ERR_IPC_TRANS_FAILED;
332 }
333 return reply.ReadInt32();
334 }
335 } // namespace Bluetooth
336 } // namespace OHOS