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)219 int32_t BluetoothBleAdvertiserProxy::GetAdvertiserHandle(int32_t &advHandle)
220 {
221 MessageParcel data;
222 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
223 HILOGW("[GetAdvertiserHandle] fail: write interface token failed.");
224 return BT_ERR_INTERNAL_ERROR;
225 }
226
227 MessageParcel reply;
228 MessageOption option = {MessageOption::TF_SYNC};
229 ErrCode result = InnerTransact(BLE_GET_ADVERTISER_HANDLE, option, data, reply);
230 if (result != NO_ERROR) {
231 HILOGW("[GetAdvertiserHandle] fail: transact ErrCode=%{public}d", result);
232 return BT_ERR_INTERNAL_ERROR;
233 }
234 int32_t ret = reply.ReadInt32();
235 advHandle = reply.ReadInt32();
236 return ret;
237 }
238
SetAdvertisingData(const BluetoothBleAdvertiserData & advData,const BluetoothBleAdvertiserData & scanResponse,int32_t advHandle)239 void BluetoothBleAdvertiserProxy::SetAdvertisingData(const BluetoothBleAdvertiserData &advData,
240 const BluetoothBleAdvertiserData &scanResponse, int32_t advHandle)
241 {
242 MessageParcel data;
243 if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
244 HILOGW("[SetAdvertisingData] fail: write interface token failed.");
245 return;
246 }
247
248 if (!data.WriteParcelable(&advData)) {
249 HILOGW("[SetAdvertisingData] fail:write advData failed");
250 return;
251 }
252
253 if (!data.WriteParcelable(&scanResponse)) {
254 HILOGW("[SetAdvertisingData] fail:write scanResponse failed");
255 return;
256 }
257
258 if (!data.WriteInt32(advHandle)) {
259 HILOGW("[SetAdvertisingData] fail: write advHandle failed.");
260 return;
261 }
262
263 MessageParcel reply;
264 MessageOption option = {MessageOption::TF_SYNC};
265 ErrCode result = InnerTransact(BLE_SET_ADVERTISING_DATA, option, data, reply);
266 if (result != NO_ERROR) {
267 HILOGW("[SetAdvertisingData] fail: transact ErrCode=%{public}d", result);
268 }
269 }
270
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)271 ErrCode BluetoothBleAdvertiserProxy::InnerTransact(
272 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
273 {
274 auto remote = Remote();
275 if (remote == nullptr) {
276 HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
277 return OBJECT_NULL;
278 }
279 int err = remote->SendRequest(code, data, reply, flags);
280 switch (err) {
281 case NO_ERROR: {
282 return NO_ERROR;
283 }
284 case DEAD_OBJECT: {
285 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
286 return DEAD_OBJECT;
287 }
288 default: {
289 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
290 return TRANSACTION_ERR;
291 }
292 }
293 }
294 } // namespace Bluetooth
295 } // namespace OHOS