• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,bool isRawData)71 int BluetoothBleAdvertiserProxy::StartAdvertising(const BluetoothBleAdvertiserSettings &settings,
72     const BluetoothBleAdvertiserData &advData, const BluetoothBleAdvertiserData &scanResponse, int32_t advHandle,
73     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.WriteBool(isRawData)) {
102         HILOGE("[StartAdvertising] fail: write isRawData failed.");
103         return BT_ERR_IPC_TRANS_FAILED;
104     }
105 
106     MessageParcel reply;
107     MessageOption option = {MessageOption::TF_SYNC};
108     ErrCode result = InnerTransact(BLE_START_ADVERTISING, option, data, reply);
109     if (result != NO_ERROR) {
110         HILOGE("[StartAdvertising] fail: transact ErrCode=%{public}d", result);
111         return BT_ERR_IPC_TRANS_FAILED;
112     }
113     return reply.ReadInt32();
114 }
115 
StopAdvertising(int32_t advHandle)116 int BluetoothBleAdvertiserProxy::StopAdvertising(int32_t advHandle)
117 {
118     MessageParcel data;
119     if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
120         HILOGE("[StopAdvertising] fail: write interface token failed.");
121         return BT_ERR_IPC_TRANS_FAILED;
122     }
123 
124     if (!data.WriteInt32(advHandle)) {
125         HILOGE("[StopAdvertising] fail: write advHandle failed.");
126         return BT_ERR_IPC_TRANS_FAILED;
127     }
128 
129     MessageParcel reply;
130     MessageOption option = {MessageOption::TF_SYNC};
131     ErrCode result = InnerTransact(BLE_STOP_ADVERTISING, option, data, reply);
132     if (result != NO_ERROR) {
133         HILOGE("[StopAdvertising] fail: transact ErrCode=%{public}d", result);
134         return BT_ERR_IPC_TRANS_FAILED;
135     }
136     return reply.ReadInt32();
137 }
138 
Close(int32_t advHandle)139 void BluetoothBleAdvertiserProxy::Close(int32_t advHandle)
140 {
141     MessageParcel data;
142     if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
143         HILOGW("[Close] fail: write interface token failed.");
144         return;
145     }
146 
147     if (!data.WriteInt32(advHandle)) {
148         HILOGW("[Close] fail: write advHandle failed.");
149         return;
150     }
151 
152     MessageParcel reply;
153     MessageOption option = {MessageOption::TF_SYNC};
154     ErrCode result = InnerTransact(BLE_CLOSE, option, data, reply);
155     if (result != NO_ERROR) {
156         HILOGW("[Close] fail: transact ErrCode=%{public}d", result);
157     }
158 }
159 
GetAdvertiserHandle(int32_t & advHandle)160 int32_t BluetoothBleAdvertiserProxy::GetAdvertiserHandle(int32_t &advHandle)
161 {
162     MessageParcel data;
163     if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
164         HILOGW("[GetAdvertiserHandle] fail: write interface token failed.");
165         return BT_ERR_INTERNAL_ERROR;
166     }
167 
168     MessageParcel reply;
169     MessageOption option = {MessageOption::TF_SYNC};
170     ErrCode result = InnerTransact(BLE_GET_ADVERTISER_HANDLE, option, data, reply);
171     if (result != NO_ERROR) {
172         HILOGW("[GetAdvertiserHandle] fail: transact ErrCode=%{public}d", result);
173         return BT_ERR_INTERNAL_ERROR;
174     }
175     int32_t ret = reply.ReadInt32();
176     advHandle = reply.ReadInt32();
177     return ret;
178 }
179 
SetAdvertisingData(const BluetoothBleAdvertiserData & advData,const BluetoothBleAdvertiserData & scanResponse,int32_t advHandle)180 void BluetoothBleAdvertiserProxy::SetAdvertisingData(const BluetoothBleAdvertiserData &advData,
181     const BluetoothBleAdvertiserData &scanResponse, int32_t advHandle)
182 {
183     MessageParcel data;
184     if (!data.WriteInterfaceToken(BluetoothBleAdvertiserProxy::GetDescriptor())) {
185         HILOGW("[SetAdvertisingData] fail: write interface token failed.");
186         return;
187     }
188 
189     if (!data.WriteParcelable(&advData)) {
190         HILOGW("[SetAdvertisingData] fail:write advData failed");
191         return;
192     }
193 
194     if (!data.WriteParcelable(&scanResponse)) {
195         HILOGW("[SetAdvertisingData] fail:write scanResponse failed");
196         return;
197     }
198 
199     if (!data.WriteInt32(advHandle)) {
200         HILOGW("[SetAdvertisingData] fail: write advHandle failed.");
201         return;
202     }
203 
204     MessageParcel reply;
205     MessageOption option = {MessageOption::TF_SYNC};
206     ErrCode result = InnerTransact(BLE_SET_ADVERTISING_DATA, option, data, reply);
207     if (result != NO_ERROR) {
208         HILOGW("[SetAdvertisingData] fail: transact ErrCode=%{public}d", result);
209     }
210 }
211 
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)212 ErrCode BluetoothBleAdvertiserProxy::InnerTransact(
213     uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
214 {
215     auto remote = Remote();
216     if (remote == nullptr) {
217         HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
218         return OBJECT_NULL;
219     }
220     int err = remote->SendRequest(code, data, reply, flags);
221     switch (err) {
222         case NO_ERROR: {
223             return NO_ERROR;
224         }
225         case DEAD_OBJECT: {
226             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
227             return DEAD_OBJECT;
228         }
229         default: {
230             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
231             return TRANSACTION_ERR;
232         }
233     }
234 }
235 }  // namespace Bluetooth
236 }  // namespace OHOS