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_ble_advertiser_proxy"
17 #endif
18
19 #include "bluetooth_ble_advertiser_proxy.h"
20 #include "bluetooth_errorcode.h"
21 #include "bluetooth_log.h"
22 #include "parcel_bt_uuid.h"
23
24 namespace OHOS {
25 namespace Bluetooth {
BluetoothBleAdvertiserProxy(const sptr<IRemoteObject> & impl)26 BluetoothBleAdvertiserProxy::BluetoothBleAdvertiserProxy(const sptr<IRemoteObject> &impl)
27 : IRemoteProxy<IBluetoothBleAdvertiser>(impl)
28 {}
29
~BluetoothBleAdvertiserProxy()30 BluetoothBleAdvertiserProxy::~BluetoothBleAdvertiserProxy()
31 {}
32
RegisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> & callback)33 void BluetoothBleAdvertiserProxy::RegisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> &callback)
34 {
35 MessageParcel data;
36 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
37 HILOGW("[RegisterBleAdvertiserCallback] fail: write interface token failed.");
38 return;
39 }
40
41 if (!data.WriteRemoteObject(callback->AsObject())) {
42 HILOGW("[RegisterBleAdvertiserCallback] fail: write callback failed.");
43 return;
44 }
45
46 MessageParcel reply;
47 MessageOption option = {MessageOption::TF_SYNC};
48 ErrCode result = InnerTransact(BLE_REGISTER_BLE_ADVERTISER_CALLBACK, option, data, reply);
49 if (result != NO_ERROR) {
50 HILOGW("[RegisterBleAdvertiserCallback] fail: transact ErrCode=%{public}d", result);
51 }
52 }
DeregisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> & callback)53 void BluetoothBleAdvertiserProxy::DeregisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> &callback)
54 {
55 MessageParcel data;
56 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
57 HILOGW("[DeregisterBleAdvertiserCallback] fail: write interface token failed.");
58 return;
59 }
60
61 if (!data.WriteRemoteObject(callback->AsObject())) {
62 HILOGW("[DeregisterBleAdvertiserCallback] fail: write callback failed.");
63 return;
64 }
65
66 MessageParcel reply;
67 MessageOption option = {MessageOption::TF_SYNC};
68 ErrCode result = InnerTransact(BLE_DE_REGISTER_BLE_ADVERTISER_CALLBACK, option, data, reply);
69 if (result != NO_ERROR) {
70 HILOGW("[DeregisterBleAdvertiserCallback] fail: transact ErrCode=%{public}d", result);
71 }
72 }
73
StartAdvertising(const BluetoothBleAdvertiserSettings & settings,const BluetoothBleAdvertiserData & advData,const BluetoothBleAdvertiserData & scanResponse,int32_t advHandle,uint16_t duration,bool isRawData)74 int BluetoothBleAdvertiserProxy::StartAdvertising(const BluetoothBleAdvertiserSettings &settings,
75 const BluetoothBleAdvertiserData &advData, const BluetoothBleAdvertiserData &scanResponse, int32_t advHandle,
76 uint16_t duration, bool isRawData)
77 {
78 MessageParcel data;
79 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
80 HILOGE("[StartAdvertising] fail: write interface token failed.");
81 return BT_ERR_IPC_TRANS_FAILED;
82 }
83
84 if (!data.WriteParcelable(&settings)) {
85 HILOGE("[StartAdvertising] fail:write settings failed");
86 return BT_ERR_IPC_TRANS_FAILED;
87 }
88
89 if (!data.WriteParcelable(&advData)) {
90 HILOGE("[StartAdvertising] fail:write advData failed");
91 return BT_ERR_IPC_TRANS_FAILED;
92 }
93
94 if (!data.WriteParcelable(&scanResponse)) {
95 HILOGE("[StartAdvertising] fail:write scanResponse failed");
96 return BT_ERR_IPC_TRANS_FAILED;
97 }
98
99 if (!data.WriteInt32(advHandle)) {
100 HILOGE("[StartAdvertising] fail: write advHandle failed.");
101 return BT_ERR_IPC_TRANS_FAILED;
102 }
103
104 if (!data.WriteUint16(duration)) {
105 HILOGE("[StartAdvertising] fail: write duration failed.");
106 return BT_ERR_IPC_TRANS_FAILED;
107 }
108
109 if (!data.WriteBool(isRawData)) {
110 HILOGE("[StartAdvertising] fail: write isRawData failed.");
111 return BT_ERR_IPC_TRANS_FAILED;
112 }
113
114 MessageParcel reply;
115 MessageOption option = {MessageOption::TF_SYNC};
116 ErrCode result = InnerTransact(BLE_START_ADVERTISING, option, data, reply);
117 if (result != NO_ERROR) {
118 HILOGE("[StartAdvertising] fail: transact ErrCode=%{public}d", result);
119 return BT_ERR_IPC_TRANS_FAILED;
120 }
121 return reply.ReadInt32();
122 }
123
EnableAdvertising(uint8_t advHandle,uint16_t duration)124 int BluetoothBleAdvertiserProxy::EnableAdvertising(uint8_t advHandle, uint16_t duration)
125 {
126 MessageParcel data;
127 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
128 HILOGE("[EnableAdvertising] fail: write interface token failed.");
129 return BT_ERR_IPC_TRANS_FAILED;
130 }
131
132 if (!data.WriteUint8(advHandle)) {
133 HILOGE("[EnableAdvertising] fail: write advHandle failed.");
134 return BT_ERR_IPC_TRANS_FAILED;
135 }
136
137 if (!data.WriteUint16(duration)) {
138 HILOGE("[EnableAdvertising] fail: write duration failed.");
139 return BT_ERR_IPC_TRANS_FAILED;
140 }
141
142 MessageParcel reply;
143 MessageOption option = {MessageOption::TF_SYNC};
144 ErrCode result = InnerTransact(BLE_ENABLE_ADVERTISING, option, data, reply);
145 if (result != NO_ERROR) {
146 HILOGE("[EnableAdvertising] fail: transact ErrCode=%{public}d", result);
147 return BT_ERR_IPC_TRANS_FAILED;
148 }
149 return reply.ReadInt32();
150 }
151
DisableAdvertising(uint8_t advHandle)152 int BluetoothBleAdvertiserProxy::DisableAdvertising(uint8_t advHandle)
153 {
154 MessageParcel data;
155 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
156 HILOGE("[DisableAdvertising] fail: write interface token failed.");
157 return BT_ERR_IPC_TRANS_FAILED;
158 }
159
160 if (!data.WriteUint8(advHandle)) {
161 HILOGE("[DisableAdvertising] fail: write advHandle failed.");
162 return BT_ERR_IPC_TRANS_FAILED;
163 }
164
165 MessageParcel reply;
166 MessageOption option = {MessageOption::TF_SYNC};
167 ErrCode result = InnerTransact(BLE_DISABLE_ADVERTISING, option, data, reply);
168 if (result != NO_ERROR) {
169 HILOGE("[DisableAdvertising] fail: transact ErrCode=%{public}d", result);
170 return BT_ERR_IPC_TRANS_FAILED;
171 }
172 return reply.ReadInt32();
173 }
174
StopAdvertising(int32_t advHandle)175 int BluetoothBleAdvertiserProxy::StopAdvertising(int32_t advHandle)
176 {
177 MessageParcel data;
178 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
179 HILOGE("[StopAdvertising] fail: write interface token failed.");
180 return BT_ERR_IPC_TRANS_FAILED;
181 }
182
183 if (!data.WriteInt32(advHandle)) {
184 HILOGE("[StopAdvertising] fail: write advHandle failed.");
185 return BT_ERR_IPC_TRANS_FAILED;
186 }
187
188 MessageParcel reply;
189 MessageOption option = {MessageOption::TF_SYNC};
190 ErrCode result = InnerTransact(BLE_STOP_ADVERTISING, option, data, reply);
191 if (result != NO_ERROR) {
192 HILOGE("[StopAdvertising] fail: transact ErrCode=%{public}d", result);
193 return BT_ERR_IPC_TRANS_FAILED;
194 }
195 return reply.ReadInt32();
196 }
197
Close(int32_t advHandle)198 void BluetoothBleAdvertiserProxy::Close(int32_t advHandle)
199 {
200 MessageParcel data;
201 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
202 HILOGW("[Close] fail: write interface token failed.");
203 return;
204 }
205
206 if (!data.WriteInt32(advHandle)) {
207 HILOGW("[Close] fail: write advHandle failed.");
208 return;
209 }
210
211 MessageParcel reply;
212 MessageOption option = {MessageOption::TF_SYNC};
213 ErrCode result = InnerTransact(BLE_CLOSE, option, data, reply);
214 if (result != NO_ERROR) {
215 HILOGW("[Close] fail: transact ErrCode=%{public}d", result);
216 }
217 }
218
GetAdvertiserHandle(int32_t & advHandle,const sptr<IBluetoothBleAdvertiseCallback> & callback)219 int32_t BluetoothBleAdvertiserProxy::GetAdvertiserHandle(int32_t &advHandle,
220 const sptr<IBluetoothBleAdvertiseCallback> &callback)
221 {
222 MessageParcel data;
223 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
224 HILOGW("[GetAdvertiserHandle] fail: write interface token failed.");
225 return BT_ERR_INTERNAL_ERROR;
226 }
227 if (!data.WriteRemoteObject(callback->AsObject())) {
228 HILOGW("[GetAdvertiserHandle] fail: write callback failed.");
229 return BT_ERR_INTERNAL_ERROR;
230 }
231
232 MessageParcel reply;
233 MessageOption option = {MessageOption::TF_SYNC};
234 ErrCode result = InnerTransact(BLE_GET_ADVERTISER_HANDLE, option, data, reply);
235 if (result != NO_ERROR) {
236 HILOGW("[GetAdvertiserHandle] fail: transact ErrCode=%{public}d", result);
237 return BT_ERR_INTERNAL_ERROR;
238 }
239 int32_t ret = reply.ReadInt32();
240 advHandle = reply.ReadInt32();
241 return ret;
242 }
243
SetAdvertisingData(const BluetoothBleAdvertiserData & advData,const BluetoothBleAdvertiserData & scanResponse,int32_t advHandle)244 void BluetoothBleAdvertiserProxy::SetAdvertisingData(const BluetoothBleAdvertiserData &advData,
245 const BluetoothBleAdvertiserData &scanResponse, int32_t advHandle)
246 {
247 MessageParcel data;
248 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
249 HILOGW("[SetAdvertisingData] fail: write interface token failed.");
250 return;
251 }
252
253 if (!data.WriteParcelable(&advData)) {
254 HILOGW("[SetAdvertisingData] fail:write advData failed");
255 return;
256 }
257
258 if (!data.WriteParcelable(&scanResponse)) {
259 HILOGW("[SetAdvertisingData] fail:write scanResponse failed");
260 return;
261 }
262
263 if (!data.WriteInt32(advHandle)) {
264 HILOGW("[SetAdvertisingData] fail: write advHandle failed.");
265 return;
266 }
267
268 MessageParcel reply;
269 MessageOption option = {MessageOption::TF_SYNC};
270 ErrCode result = InnerTransact(BLE_SET_ADVERTISING_DATA, option, data, reply);
271 if (result != NO_ERROR) {
272 HILOGW("[SetAdvertisingData] fail: transact ErrCode=%{public}d", result);
273 }
274 }
275
ChangeAdvertisingParams(uint8_t advHandle,const BluetoothBleAdvertiserSettings & settings)276 int BluetoothBleAdvertiserProxy::ChangeAdvertisingParams(uint8_t advHandle,
277 const BluetoothBleAdvertiserSettings &settings)
278 {
279 MessageParcel data;
280 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
281 HILOGW("[ChangeAdvertisingParams] fail: transact desCriptor");
282 return BT_ERR_IPC_TRANS_FAILED;
283 }
284
285 if (!data.WriteUint8(advHandle)) {
286 HILOGW("[ChangeAdvertisingParams] fail: transact advHandle:%{public}u", advHandle);
287 return BT_ERR_IPC_TRANS_FAILED;
288 }
289 if (!data.WriteParcelable(&settings)) {
290 HILOGW("[ChangeAdvertisingParams] fail: transact settings");
291 return BT_ERR_IPC_TRANS_FAILED;
292 }
293 MessageParcel reply;
294 MessageOption option = { MessageOption::TF_SYNC };
295 int result = InnerTransact(BLE_CHANGE_ADVERTISING_PARAMS, option, data, reply);
296 if (result != BT_NO_ERROR) {
297 HILOGW("[ChangeAdvertisingParams] fail: transact ErrCode=%{public}d", result);
298 }
299 return result;
300 }
301
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)302 ErrCode BluetoothBleAdvertiserProxy::InnerTransact(
303 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
304 {
305 auto remote = Remote();
306 if (remote == nullptr) {
307 HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
308 return OBJECT_NULL;
309 }
310 int err = remote->SendRequest(code, data, reply, flags);
311 switch (err) {
312 case NO_ERROR: {
313 return NO_ERROR;
314 }
315 case DEAD_OBJECT: {
316 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
317 return DEAD_OBJECT;
318 }
319 default: {
320 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
321 return TRANSACTION_ERR;
322 }
323 }
324 }
325 } // namespace Bluetooth
326 } // namespace OHOS