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