• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 <map>
17 #include <vector>
18 
19 #include "bluetooth_ble_central_manager_stub.h"
20 #include "bluetooth_log.h"
21 #include "ipc_types.h"
22 #include "parcel_bt_uuid.h"
23 #include "bluetooth_bt_uuid.h"
24 
25 namespace OHOS {
26 namespace Bluetooth {
27 const int32_t BLE_CENTRAL_MANAGER_STUB_READ_DATA_SIZE_MAX_LEN = 0x100;
28 const std::map<uint32_t, std::function<ErrCode(BluetoothBleCentralManagerStub *, MessageParcel &, MessageParcel &)>>
29     BluetoothBleCentralManagerStub::interfaces_ = {
30         {BluetoothBleCentralManagerInterfaceCode::BLE_REGISTER_BLE_CENTRAL_MANAGER_CALLBACK,
31             std::bind(&BluetoothBleCentralManagerStub::RegisterBleCentralManagerCallbackInner, std::placeholders::_1,
32                 std::placeholders::_2, std::placeholders::_3)},
33         {BluetoothBleCentralManagerInterfaceCode::BLE_DE_REGISTER_BLE_CENTRAL_MANAGER_CALLBACK,
34             std::bind(&BluetoothBleCentralManagerStub::DeregisterBleCentralManagerCallbackInner, std::placeholders::_1,
35                 std::placeholders::_2, std::placeholders::_3)},
36         {BluetoothBleCentralManagerInterfaceCode::BLE_START_SCAN,
37             std::bind(&BluetoothBleCentralManagerStub::StartScanInner, std::placeholders::_1, std::placeholders::_2,
38                 std::placeholders::_3)},
39         {BluetoothBleCentralManagerInterfaceCode::BLE_START_SCAN_WITH_SETTINGS,
40             std::bind(&BluetoothBleCentralManagerStub::StartScanWithSettingsInner, std::placeholders::_1,
41                 std::placeholders::_2, std::placeholders::_3)},
42         {BluetoothBleCentralManagerInterfaceCode::BLE_CONFIG_SCAN_FILTER,
43             std::bind(&BluetoothBleCentralManagerStub::ConfigScanFilterInner, std::placeholders::_1,
44                 std::placeholders::_2, std::placeholders::_3)},
45         {BluetoothBleCentralManagerInterfaceCode::BLE_REMOVE_SCAN_FILTER,
46             std::bind(&BluetoothBleCentralManagerStub::RemoveScanFilterInner, std::placeholders::_1,
47                 std::placeholders::_2, std::placeholders::_3)},
48         {BluetoothBleCentralManagerInterfaceCode::BLE_STOP_SCAN,
49             std::bind(&BluetoothBleCentralManagerStub::StopScanInner, std::placeholders::_1, std::placeholders::_2,
50                 std::placeholders::_3)},
51         {BluetoothBleCentralManagerInterfaceCode::BLE_PROXY_UID,
52             std::bind(&BluetoothBleCentralManagerStub::ProxyUidInner, std::placeholders::_1, std::placeholders::_2,
53                 std::placeholders::_3)},
54         {BluetoothBleCentralManagerInterfaceCode::BLE_RESET_ALL_PROXY,
55             std::bind(&BluetoothBleCentralManagerStub::ResetAllProxyInner, std::placeholders::_1, std::placeholders::_2,
56                 std::placeholders::_3)},
57         {BluetoothBleCentralManagerInterfaceCode::BLE_SET_LPDEVICE_ADV_PARAM,
58             std::bind(&BluetoothBleCentralManagerStub::SetLpDeviceAdvParamInner, std::placeholders::_1,
59             std::placeholders::_2, std::placeholders::_3)},
60         {BluetoothBleCentralManagerInterfaceCode::BLE_SET_SCAN_REPORT_CHANNEL_TO_LPDEVICE,
61             std::bind(&BluetoothBleCentralManagerStub::SetScanReportChannelToLpDeviceInner, std::placeholders::_1,
62             std::placeholders::_2, std::placeholders::_3)},
63         {BluetoothBleCentralManagerInterfaceCode::BLE_ENABLE_SYNC_DATA_TO_LPDEVICE,
64             std::bind(&BluetoothBleCentralManagerStub::EnableSyncDataToLpDeviceInner, std::placeholders::_1,
65             std::placeholders::_2, std::placeholders::_3)},
66         {BluetoothBleCentralManagerInterfaceCode::BLE_DISABLE_SYNC_DATA_TO_LPDEVICE,
67             std::bind(&BluetoothBleCentralManagerStub::DisableSyncDataToLpDeviceInner, std::placeholders::_1,
68             std::placeholders::_2, std::placeholders::_3)},
69         {BluetoothBleCentralManagerInterfaceCode::BLE_SEND_PARAMS_TO_LPDEVICE,
70             std::bind(&BluetoothBleCentralManagerStub::SendParamsToLpDeviceInner, std::placeholders::_1,
71             std::placeholders::_2, std::placeholders::_3)},
72         {BluetoothBleCentralManagerInterfaceCode::BLE_IS_LPDEVICE_AVAILABLE,
73             std::bind(&BluetoothBleCentralManagerStub::IsLpDeviceAvailableInner, std::placeholders::_1,
74             std::placeholders::_2, std::placeholders::_3)},
75         {BluetoothBleCentralManagerInterfaceCode::BLE_SET_LPDEVICE_PARAM,
76             std::bind(&BluetoothBleCentralManagerStub::SetLpDeviceParamInner, std::placeholders::_1,
77             std::placeholders::_2, std::placeholders::_3)},
78         {BluetoothBleCentralManagerInterfaceCode::BLE_REMOVE_LPDEVICE_PARAM,
79             std::bind(&BluetoothBleCentralManagerStub::RemoveLpDeviceParamInner, std::placeholders::_1,
80             std::placeholders::_2, std::placeholders::_3)},
81 };
82 
BluetoothBleCentralManagerStub()83 BluetoothBleCentralManagerStub::BluetoothBleCentralManagerStub()
84 {}
85 
~BluetoothBleCentralManagerStub()86 BluetoothBleCentralManagerStub::~BluetoothBleCentralManagerStub()
87 {}
88 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)89 int BluetoothBleCentralManagerStub::OnRemoteRequest(
90     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
91 {
92     if (BluetoothBleCentralManagerStub::GetDescriptor() != data.ReadInterfaceToken()) {
93         HILOGW("[OnRemoteRequest] fail: invalid interface token!");
94         return OBJECT_NULL;
95     }
96 
97     auto it = interfaces_.find(code);
98     if (it == interfaces_.end()) {
99         HILOGW("[OnRemoteRequest] fail: unknown code!");
100         return IRemoteStub<IBluetoothBleCentralManager>::OnRemoteRequest(code, data, reply, option);
101     }
102 
103     auto fun = it->second;
104     if (fun == nullptr) {
105         HILOGW("[OnRemoteRequest] fail: not find function!");
106         return IRemoteStub<IBluetoothBleCentralManager>::OnRemoteRequest(code, data, reply, option);
107     }
108 
109     ErrCode result = fun(this, data, reply);
110     if (SUCCEEDED(result)) {
111         return NO_ERROR;
112     }
113 
114     HILOGW("[OnRemoteRequest] fail: Failed to call interface %{public}u, err:%{public}d", code, result);
115     return result;
116 }
117 
RegisterBleCentralManagerCallbackInner(MessageParcel & data,MessageParcel & reply)118 ErrCode BluetoothBleCentralManagerStub::RegisterBleCentralManagerCallbackInner(
119     MessageParcel &data, MessageParcel &reply)
120 {
121     sptr<IRemoteObject> remote = data.ReadRemoteObject();
122     const sptr<IBluetoothBleCentralManagerCallback> callBack =
123         OHOS::iface_cast<IBluetoothBleCentralManagerCallback>(remote);
124     bool enableRandomAddrMode = data.ReadBool();
125     int32_t scannerId = 0;
126     RegisterBleCentralManagerCallback(scannerId, enableRandomAddrMode, callBack);
127     if (!reply.WriteInt32(scannerId)) {
128         HILOGE("reply writing failed");
129         return ERR_INVALID_VALUE;
130     }
131     return NO_ERROR;
132 }
133 
DeregisterBleCentralManagerCallbackInner(MessageParcel & data,MessageParcel & reply)134 ErrCode BluetoothBleCentralManagerStub::DeregisterBleCentralManagerCallbackInner(
135     MessageParcel &data, MessageParcel &reply)
136 {
137     int32_t scannerId = data.ReadInt32();
138     sptr<IRemoteObject> remote = data.ReadRemoteObject();
139     const sptr<IBluetoothBleCentralManagerCallback> callBack =
140         OHOS::iface_cast<IBluetoothBleCentralManagerCallback>(remote);
141     DeregisterBleCentralManagerCallback(scannerId, callBack);
142     return NO_ERROR;
143 }
144 
StartScanInner(MessageParcel & data,MessageParcel & reply)145 ErrCode BluetoothBleCentralManagerStub::StartScanInner(MessageParcel &data, MessageParcel &reply)
146 {
147     int32_t scannerId = data.ReadInt32();
148     int ret = StartScan(scannerId);
149     if (!reply.WriteInt32(ret)) {
150         HILOGE("reply writing failed");
151         return ERR_INVALID_VALUE;
152     }
153     return NO_ERROR;
154 }
155 
StartScanWithSettingsInner(MessageParcel & data,MessageParcel & reply)156 ErrCode BluetoothBleCentralManagerStub::StartScanWithSettingsInner(MessageParcel &data, MessageParcel &reply)
157 {
158     int32_t scannerId = data.ReadInt32();
159     std::shared_ptr<BluetoothBleScanSettings> settings(data.ReadParcelable<BluetoothBleScanSettings>());
160     if (settings == nullptr) {
161         HILOGW("[StartScanWithSettingsInner] fail: read settings failed");
162         return TRANSACTION_ERR;
163     }
164 
165     int ret = StartScan(scannerId, *settings);
166     if (!reply.WriteInt32(ret)) {
167         HILOGE("reply writing failed");
168         return ERR_INVALID_VALUE;
169     }
170     return NO_ERROR;
171 }
172 
StopScanInner(MessageParcel & data,MessageParcel & reply)173 ErrCode BluetoothBleCentralManagerStub::StopScanInner(MessageParcel &data, MessageParcel &reply)
174 {
175     int32_t scannerId = data.ReadInt32();
176     int ret = StopScan(scannerId);
177     if (!reply.WriteInt32(ret)) {
178         HILOGE("reply writing failed");
179         return ERR_INVALID_VALUE;
180     }
181     return NO_ERROR;
182 }
ConfigScanFilterInner(MessageParcel & data,MessageParcel & reply)183 ErrCode BluetoothBleCentralManagerStub::ConfigScanFilterInner(MessageParcel &data, MessageParcel &reply)
184 {
185     std::vector<BluetoothBleScanFilter> filters {};
186     int32_t scannerId = data.ReadInt32();
187     int32_t itemsSize = 0;
188     if (!data.ReadInt32(itemsSize) || itemsSize > BLE_CENTRAL_MANAGER_STUB_READ_DATA_SIZE_MAX_LEN) {
189         HILOGE("read Parcelable size failed.");
190         return ERR_INVALID_VALUE;
191     }
192     for (int i = 0; i < itemsSize; i++) {
193         std::shared_ptr<BluetoothBleScanFilter> res(data.ReadParcelable<BluetoothBleScanFilter>());
194         if (res == nullptr) {
195             HILOGE("null pointer");
196             return ERR_INVALID_VALUE;
197         }
198         BluetoothBleScanFilter item = *(res);
199         filters.push_back(item);
200     }
201 
202     int result = ConfigScanFilter(scannerId, filters);
203     bool resultRet = reply.WriteInt32(result);
204     bool idRet = reply.WriteInt32(scannerId);
205     if (!(resultRet && idRet)) {
206         HILOGE("BluetoothBleCentralManagerStub: reply writing failed in: %{public}s.", __func__);
207         return ERR_INVALID_VALUE;
208     }
209     return NO_ERROR;
210 }
211 
RemoveScanFilterInner(MessageParcel & data,MessageParcel & reply)212 ErrCode BluetoothBleCentralManagerStub::RemoveScanFilterInner(MessageParcel &data, MessageParcel &reply)
213 {
214     int32_t scannerId = data.ReadInt32();
215 
216     RemoveScanFilter(scannerId);
217     return NO_ERROR;
218 }
219 
ProxyUidInner(MessageParcel & data,MessageParcel & reply)220 ErrCode BluetoothBleCentralManagerStub::ProxyUidInner(MessageParcel &data, MessageParcel &reply)
221 {
222     int32_t uid = data.ReadInt32();
223     bool isProxy = data.ReadBool();
224 
225     bool ret = ProxyUid(uid, isProxy);
226     if (!reply.WriteBool(ret)) {
227         return ERR_INVALID_VALUE;
228     }
229     return NO_ERROR;
230 }
231 
ResetAllProxyInner(MessageParcel & data,MessageParcel & reply)232 ErrCode BluetoothBleCentralManagerStub::ResetAllProxyInner(MessageParcel &data, MessageParcel &reply)
233 {
234     bool ret = ResetAllProxy();
235     if (!reply.WriteBool(ret)) {
236         return ERR_INVALID_VALUE;
237     }
238     return NO_ERROR;
239 }
240 
SetLpDeviceAdvParamInner(MessageParcel & data,MessageParcel & reply)241 ErrCode BluetoothBleCentralManagerStub::SetLpDeviceAdvParamInner(MessageParcel &data, MessageParcel &reply)
242 {
243     return NO_ERROR;
244 }
245 
SetScanReportChannelToLpDeviceInner(MessageParcel & data,MessageParcel & reply)246 ErrCode BluetoothBleCentralManagerStub::SetScanReportChannelToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
247 {
248     return NO_ERROR;
249 }
250 
EnableSyncDataToLpDeviceInner(MessageParcel & data,MessageParcel & reply)251 ErrCode BluetoothBleCentralManagerStub::EnableSyncDataToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
252 {
253     return NO_ERROR;
254 }
255 
DisableSyncDataToLpDeviceInner(MessageParcel & data,MessageParcel & reply)256 ErrCode BluetoothBleCentralManagerStub::DisableSyncDataToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
257 {
258     return NO_ERROR;
259 }
260 
SendParamsToLpDeviceInner(MessageParcel & data,MessageParcel & reply)261 ErrCode BluetoothBleCentralManagerStub::SendParamsToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
262 {
263     return NO_ERROR;
264 }
265 
IsLpDeviceAvailableInner(MessageParcel & data,MessageParcel & reply)266 ErrCode BluetoothBleCentralManagerStub::IsLpDeviceAvailableInner(MessageParcel &data, MessageParcel &reply)
267 {
268     return NO_ERROR;
269 }
270 
SetLpDeviceParamInner(MessageParcel & data,MessageParcel & reply)271 ErrCode BluetoothBleCentralManagerStub::SetLpDeviceParamInner(MessageParcel &data, MessageParcel &reply)
272 {
273     return NO_ERROR;
274 }
275 
RemoveLpDeviceParamInner(MessageParcel & data,MessageParcel & reply)276 ErrCode BluetoothBleCentralManagerStub::RemoveLpDeviceParamInner(MessageParcel &data, MessageParcel &reply)
277 {
278     return NO_ERROR;
279 }
280 }  // namespace Bluetooth
281 }  // namespace OHOS
282