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     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
26         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
27     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
28     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(services), BT_ERR_IPC_TRANS_FAILED, "write services error");
29 
30     MessageParcel reply;
31     MessageOption option(MessageOption::TF_SYNC);
32 
33     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_ADD_SERVICE,
34         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
35 
36     return reply.ReadInt32();
37 }
ClearServices(int appId)38 void BluetoothGattServerProxy::ClearServices(int appId)
39 {
40     MessageParcel data;
41     CHECK_AND_RETURN_LOG(
42         data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()), "WriteInterfaceToken error");
43     CHECK_AND_RETURN_LOG(data.WriteInt32(appId), "WriteInterfaceToken error");
44 
45     MessageParcel reply;
46     MessageOption option(MessageOption::TF_SYNC);
47 
48     SEND_IPC_REQUEST_RETURN(BluetoothGattServerInterfaceCode::GATT_SERVER_CLEAR_SERVICES, data, reply, option);
49 
50     return;
51 }
52 
Connect(int appId, const BluetoothGattDevice &device, bool isDirect)53 int BluetoothGattServerProxy::Connect(int appId, const BluetoothGattDevice &device, bool isDirect)
54 {
55     MessageParcel data;
56     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
57         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
58     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
59     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
60     CHECK_AND_RETURN_LOG_RET(data.WriteBool(isDirect), BT_ERR_IPC_TRANS_FAILED, "write isDirect error");
61 
62     MessageParcel reply;
63     MessageOption option(MessageOption::TF_SYNC);
64 
65     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_CONNECT,
66         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
67 
68     return reply.ReadInt32();
69 }
70 
CancelConnection(int appId, const BluetoothGattDevice &device)71 int BluetoothGattServerProxy::CancelConnection(int appId, const BluetoothGattDevice &device)
72 {
73     MessageParcel data;
74     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
75         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
76     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
77     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
78 
79     MessageParcel reply;
80     MessageOption option(MessageOption::TF_SYNC);
81 
82     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_CANCEL_CONNECTION,
83         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
84 
85     return reply.ReadInt32();
86 }
RegisterApplication(const sptr<IBluetoothGattServerCallback> &callback)87 int BluetoothGattServerProxy::RegisterApplication(const sptr<IBluetoothGattServerCallback> &callback)
88 {
89     MessageParcel data;
90     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
91         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
92     CHECK_AND_RETURN_LOG_RET(
93         data.WriteRemoteObject(callback->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object error");
94 
95     MessageParcel reply;
96     MessageOption option(MessageOption::TF_SYNC);
97 
98     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_REGISTER,
99         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
100 
101     return reply.ReadInt32();
102 }
DeregisterApplication(int appId)103 int BluetoothGattServerProxy::DeregisterApplication(int appId)
104 {
105     MessageParcel data;
106     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
107         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
108     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
109 
110     MessageParcel reply;
111     MessageOption option(MessageOption::TF_SYNC);
112 
113     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_DEREGISTER,
114         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
115 
116     return reply.ReadInt32();
117 }
NotifyClient( const BluetoothGattDevice &device, BluetoothGattCharacteristic *characteristic, bool needConfirm)118 int BluetoothGattServerProxy::NotifyClient(
119     const BluetoothGattDevice &device, BluetoothGattCharacteristic *characteristic, bool needConfirm)
120 {
121     MessageParcel data;
122     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
123         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
124     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
125     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(characteristic),
126         BT_ERR_IPC_TRANS_FAILED, "write characteristic error");
127     CHECK_AND_RETURN_LOG_RET(data.WriteBool(needConfirm), BT_ERR_IPC_TRANS_FAILED, "write needConfirm error");
128 
129     MessageParcel reply;
130     MessageOption option(MessageOption::TF_SYNC);
131 
132     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_NOTIFY_CLIENT,
133         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
134 
135     return reply.ReadInt32();
136 }
RemoveService(int32_t appId, const BluetoothGattService &services)137 int BluetoothGattServerProxy::RemoveService(int32_t appId, const BluetoothGattService &services)
138 {
139     MessageParcel data;
140     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
141         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
142     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(appId), BT_ERR_IPC_TRANS_FAILED, "write appId error");
143     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&services), BT_ERR_IPC_TRANS_FAILED, "write services error");
144 
145     MessageParcel reply;
146     MessageOption option(MessageOption::TF_SYNC);
147 
148     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_REMOVE_SERVICE,
149         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
150 
151     return reply.ReadInt32();
152 }
RespondCharacteristicRead( const BluetoothGattDevice &device, BluetoothGattCharacteristic *characteristic, int32_t ret)153 int BluetoothGattServerProxy::RespondCharacteristicRead(
154     const BluetoothGattDevice &device, BluetoothGattCharacteristic *characteristic, int32_t ret)
155 {
156     MessageParcel data;
157     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
158         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
159     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
160     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(characteristic),
161         BT_ERR_IPC_TRANS_FAILED, "write characteristic error");
162     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(ret), BT_ERR_IPC_TRANS_FAILED, "write ret error");
163 
164     MessageParcel reply;
165     MessageOption option(MessageOption::TF_SYNC);
166 
167     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_RESPOND_CHARACTERISTIC_READ,
168         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
169 
170     return reply.ReadInt32();
171 }
RespondCharacteristicWrite( const BluetoothGattDevice &device, const BluetoothGattCharacteristic &characteristic, int32_t ret)172 int BluetoothGattServerProxy::RespondCharacteristicWrite(
173     const BluetoothGattDevice &device, const BluetoothGattCharacteristic &characteristic, int32_t ret)
174 {
175     MessageParcel data;
176     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
177         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
178     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
179     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&characteristic),
180         BT_ERR_IPC_TRANS_FAILED, "write characteristic error");
181     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(ret), BT_ERR_IPC_TRANS_FAILED, "write ret error");
182 
183     MessageParcel reply;
184     MessageOption option(MessageOption::TF_SYNC);
185 
186     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_RESPOND_CHARACTERISTIC_WRITE,
187         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
188 
189     return reply.ReadInt32();
190 }
RespondDescriptorRead( const BluetoothGattDevice &device, BluetoothGattDescriptor *descriptor, int32_t ret)191 int BluetoothGattServerProxy::RespondDescriptorRead(
192     const BluetoothGattDevice &device, BluetoothGattDescriptor *descriptor, int32_t ret)
193 {
194     MessageParcel data;
195     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
196         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
197     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
198     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(descriptor), BT_ERR_IPC_TRANS_FAILED, "write descriptor error");
199     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(ret), BT_ERR_IPC_TRANS_FAILED, "write ret error");
200 
201     MessageParcel reply;
202     MessageOption option(MessageOption::TF_SYNC);
203 
204     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_RESPOND_DESCRIPTOR_READ,
205         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
206 
207     return reply.ReadInt32();
208 }
RespondDescriptorWrite( const BluetoothGattDevice &device, const BluetoothGattDescriptor &descriptor, int32_t ret)209 int BluetoothGattServerProxy::RespondDescriptorWrite(
210     const BluetoothGattDevice &device, const BluetoothGattDescriptor &descriptor, int32_t ret)
211 {
212     MessageParcel data;
213     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothGattServerProxy::GetDescriptor()),
214         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
215     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
216     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&descriptor), BT_ERR_IPC_TRANS_FAILED, "write descriptor error");
217     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(ret), BT_ERR_IPC_TRANS_FAILED, "write ret error");
218 
219     MessageParcel reply;
220     MessageOption option(MessageOption::TF_SYNC);
221 
222     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothGattServerInterfaceCode::GATT_SERVER_RESPOND_DESCRIPTOR_WRITE,
223         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
224 
225     return reply.ReadInt32();
226 }
227 }  // namespace Bluetooth
228 }  // namespace OHOS