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_REMOVE_SCAN_FILTER,
40 std::bind(&BluetoothBleCentralManagerStub::RemoveScanFilterInner, std::placeholders::_1,
41 std::placeholders::_2, std::placeholders::_3)},
42 {BluetoothBleCentralManagerInterfaceCode::BLE_STOP_SCAN,
43 std::bind(&BluetoothBleCentralManagerStub::StopScanInner, std::placeholders::_1, std::placeholders::_2,
44 std::placeholders::_3)},
45 {BluetoothBleCentralManagerInterfaceCode::BLE_FREEZE_BY_RSS,
46 std::bind(&BluetoothBleCentralManagerStub::FreezeByRssInner, std::placeholders::_1, std::placeholders::_2,
47 std::placeholders::_3)},
48 {BluetoothBleCentralManagerInterfaceCode::BLE_RESET_ALL_PROXY,
49 std::bind(&BluetoothBleCentralManagerStub::ResetAllProxyInner, std::placeholders::_1, std::placeholders::_2,
50 std::placeholders::_3)},
51 {BluetoothBleCentralManagerInterfaceCode::BLE_SET_LPDEVICE_ADV_PARAM,
52 std::bind(&BluetoothBleCentralManagerStub::SetLpDeviceAdvParamInner, std::placeholders::_1,
53 std::placeholders::_2, std::placeholders::_3)},
54 {BluetoothBleCentralManagerInterfaceCode::BLE_SET_SCAN_REPORT_CHANNEL_TO_LPDEVICE,
55 std::bind(&BluetoothBleCentralManagerStub::SetScanReportChannelToLpDeviceInner, std::placeholders::_1,
56 std::placeholders::_2, std::placeholders::_3)},
57 {BluetoothBleCentralManagerInterfaceCode::BLE_ENABLE_SYNC_DATA_TO_LPDEVICE,
58 std::bind(&BluetoothBleCentralManagerStub::EnableSyncDataToLpDeviceInner, std::placeholders::_1,
59 std::placeholders::_2, std::placeholders::_3)},
60 {BluetoothBleCentralManagerInterfaceCode::BLE_DISABLE_SYNC_DATA_TO_LPDEVICE,
61 std::bind(&BluetoothBleCentralManagerStub::DisableSyncDataToLpDeviceInner, std::placeholders::_1,
62 std::placeholders::_2, std::placeholders::_3)},
63 {BluetoothBleCentralManagerInterfaceCode::BLE_SEND_PARAMS_TO_LPDEVICE,
64 std::bind(&BluetoothBleCentralManagerStub::SendParamsToLpDeviceInner, std::placeholders::_1,
65 std::placeholders::_2, std::placeholders::_3)},
66 {BluetoothBleCentralManagerInterfaceCode::BLE_IS_LPDEVICE_AVAILABLE,
67 std::bind(&BluetoothBleCentralManagerStub::IsLpDeviceAvailableInner, std::placeholders::_1,
68 std::placeholders::_2, std::placeholders::_3)},
69 {BluetoothBleCentralManagerInterfaceCode::BLE_SET_LPDEVICE_PARAM,
70 std::bind(&BluetoothBleCentralManagerStub::SetLpDeviceParamInner, std::placeholders::_1,
71 std::placeholders::_2, std::placeholders::_3)},
72 {BluetoothBleCentralManagerInterfaceCode::BLE_REMOVE_LPDEVICE_PARAM,
73 std::bind(&BluetoothBleCentralManagerStub::RemoveLpDeviceParamInner, std::placeholders::_1,
74 std::placeholders::_2, std::placeholders::_3)},
75 {BluetoothBleCentralManagerInterfaceCode::BLE_CHANGE_SCAN_PARAM,
76 std::bind(&BluetoothBleCentralManagerStub::ChangeScanParamsInner, std::placeholders::_1,
77 std::placeholders::_2, std::placeholders::_3)},
78 {BluetoothBleCentralManagerInterfaceCode::BLE_IS_VALID_SCANNERID,
79 std::bind(&BluetoothBleCentralManagerStub::IsValidScannerIdInner, 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 std::shared_ptr<BluetoothBleScanSettings> settings(data.ReadParcelable<BluetoothBleScanSettings>());
149 if (settings == nullptr) {
150 HILOGW("[StartScanInner] fail: read settings failed");
151 return TRANSACTION_ERR;
152 }
153
154 std::vector<BluetoothBleScanFilter> filters {};
155 int32_t itemsSize = 0;
156 if (!data.ReadInt32(itemsSize) || itemsSize > BLE_CENTRAL_MANAGER_STUB_READ_DATA_SIZE_MAX_LEN) {
157 HILOGE("read Parcelable size failed.");
158 return ERR_INVALID_VALUE;
159 }
160 for (int i = 0; i < itemsSize; i++) {
161 std::shared_ptr<BluetoothBleScanFilter> res(data.ReadParcelable<BluetoothBleScanFilter>());
162 if (res == nullptr) {
163 HILOGE("null pointer");
164 return ERR_INVALID_VALUE;
165 }
166 BluetoothBleScanFilter item = *(res);
167 filters.push_back(item);
168 }
169
170 bool isNewApi = data.ReadBool();
171 int ret = StartScan(scannerId, *settings, filters, isNewApi);
172 if (!reply.WriteInt32(ret)) {
173 HILOGE("reply writing failed");
174 return ERR_INVALID_VALUE;
175 }
176 return NO_ERROR;
177 }
178
StopScanInner(MessageParcel & data,MessageParcel & reply)179 ErrCode BluetoothBleCentralManagerStub::StopScanInner(MessageParcel &data, MessageParcel &reply)
180 {
181 int32_t scannerId = data.ReadInt32();
182 int ret = StopScan(scannerId);
183 if (!reply.WriteInt32(ret)) {
184 HILOGE("reply writing failed");
185 return ERR_INVALID_VALUE;
186 }
187 return NO_ERROR;
188 }
189
RemoveScanFilterInner(MessageParcel & data,MessageParcel & reply)190 ErrCode BluetoothBleCentralManagerStub::RemoveScanFilterInner(MessageParcel &data, MessageParcel &reply)
191 {
192 int32_t scannerId = data.ReadInt32();
193
194 RemoveScanFilter(scannerId);
195 return NO_ERROR;
196 }
197
FreezeByRssInner(MessageParcel & data,MessageParcel & reply)198 ErrCode BluetoothBleCentralManagerStub::FreezeByRssInner(MessageParcel &data, MessageParcel &reply)
199 {
200 std::vector<int> pidVec {};
201 CHECK_AND_RETURN_LOG_RET(data.ReadInt32Vector(&pidVec), ERR_INVALID_VALUE, "ipc failed");
202 bool isProxy = data.ReadBool();
203
204 std::set<int> pidSet(pidVec.begin(), pidVec.end());
205 bool ret = FreezeByRss(pidSet, isProxy);
206 if (!reply.WriteBool(ret)) {
207 return ERR_INVALID_VALUE;
208 }
209 return NO_ERROR;
210 }
211
ResetAllProxyInner(MessageParcel & data,MessageParcel & reply)212 ErrCode BluetoothBleCentralManagerStub::ResetAllProxyInner(MessageParcel &data, MessageParcel &reply)
213 {
214 bool ret = ResetAllProxy();
215 if (!reply.WriteBool(ret)) {
216 return ERR_INVALID_VALUE;
217 }
218 return NO_ERROR;
219 }
220
SetLpDeviceAdvParamInner(MessageParcel & data,MessageParcel & reply)221 ErrCode BluetoothBleCentralManagerStub::SetLpDeviceAdvParamInner(MessageParcel &data, MessageParcel &reply)
222 {
223 return NO_ERROR;
224 }
225
SetScanReportChannelToLpDeviceInner(MessageParcel & data,MessageParcel & reply)226 ErrCode BluetoothBleCentralManagerStub::SetScanReportChannelToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
227 {
228 return NO_ERROR;
229 }
230
EnableSyncDataToLpDeviceInner(MessageParcel & data,MessageParcel & reply)231 ErrCode BluetoothBleCentralManagerStub::EnableSyncDataToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
232 {
233 return NO_ERROR;
234 }
235
DisableSyncDataToLpDeviceInner(MessageParcel & data,MessageParcel & reply)236 ErrCode BluetoothBleCentralManagerStub::DisableSyncDataToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
237 {
238 return NO_ERROR;
239 }
240
SendParamsToLpDeviceInner(MessageParcel & data,MessageParcel & reply)241 ErrCode BluetoothBleCentralManagerStub::SendParamsToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
242 {
243 return NO_ERROR;
244 }
245
IsLpDeviceAvailableInner(MessageParcel & data,MessageParcel & reply)246 ErrCode BluetoothBleCentralManagerStub::IsLpDeviceAvailableInner(MessageParcel &data, MessageParcel &reply)
247 {
248 return NO_ERROR;
249 }
250
SetLpDeviceParamInner(MessageParcel & data,MessageParcel & reply)251 ErrCode BluetoothBleCentralManagerStub::SetLpDeviceParamInner(MessageParcel &data, MessageParcel &reply)
252 {
253 return NO_ERROR;
254 }
255
RemoveLpDeviceParamInner(MessageParcel & data,MessageParcel & reply)256 ErrCode BluetoothBleCentralManagerStub::RemoveLpDeviceParamInner(MessageParcel &data, MessageParcel &reply)
257 {
258 return NO_ERROR;
259 }
260
ChangeScanParamsInner(MessageParcel & data,MessageParcel & reply)261 ErrCode BluetoothBleCentralManagerStub::ChangeScanParamsInner(MessageParcel &data, MessageParcel &reply)
262 {
263 int32_t scannerId = data.ReadInt32();
264 std::shared_ptr<BluetoothBleScanSettings> settings(data.ReadParcelable<BluetoothBleScanSettings>());
265 CHECK_AND_RETURN_LOG_RET(settings != nullptr, ERR_INVALID_VALUE, "settings nullptr");
266
267 std::vector<BluetoothBleScanFilter> filters{};
268 int32_t filterSize = 0;
269 if (!data.ReadInt32(filterSize) || filterSize > BLE_CENTRAL_MANAGER_STUB_READ_DATA_SIZE_MAX_LEN) {
270 HILOGE("read Parcelable size failed.");
271 return ERR_INVALID_VALUE;
272 }
273
274 for (int32_t i = 0; i < filterSize; i++) {
275 std::shared_ptr<BluetoothBleScanFilter> filter(data.ReadParcelable<BluetoothBleScanFilter>());
276 if (filter == nullptr) {
277 HILOGE("filter nullptr");
278 return ERR_INVALID_VALUE;
279 }
280 BluetoothBleScanFilter item = *filter;
281 filters.push_back(item);
282 }
283 uint32_t filterAction = 0;
284 if (!data.ReadUint32(filterAction)) {
285 HILOGE("read Parcelable filterAction failed.");
286 return ERR_INVALID_VALUE;
287 }
288 int32_t ret = ChangeScanParams(scannerId, *settings, filters, filterAction);
289 if (!reply.WriteInt32(ret)) {
290 HILOGE("reply writing failed.");
291 return ERR_INVALID_VALUE;
292 }
293 return NO_ERROR;
294 }
295
IsValidScannerIdInner(MessageParcel & data,MessageParcel & reply)296 ErrCode BluetoothBleCentralManagerStub::IsValidScannerIdInner(MessageParcel &data, MessageParcel &reply)
297 {
298 int32_t scannerId = data.ReadInt32();
299 bool isValid = true;
300 IsValidScannerId(scannerId, isValid);
301 if (!reply.WriteBool(isValid)) {
302 HILOGE("reply writing failed");
303 return ERR_INVALID_VALUE;
304 }
305 return NO_ERROR;
306 }
307 } // namespace Bluetooth
308 } // namespace OHOS
309