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