• 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_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 };
79 
BluetoothBleCentralManagerStub()80 BluetoothBleCentralManagerStub::BluetoothBleCentralManagerStub()
81 {}
82 
~BluetoothBleCentralManagerStub()83 BluetoothBleCentralManagerStub::~BluetoothBleCentralManagerStub()
84 {}
85 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)86 int BluetoothBleCentralManagerStub::OnRemoteRequest(
87     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
88 {
89     if (BluetoothBleCentralManagerStub::GetDescriptor() != data.ReadInterfaceToken()) {
90         HILOGW("[OnRemoteRequest] fail: invalid interface token!");
91         return OBJECT_NULL;
92     }
93 
94     auto it = interfaces_.find(code);
95     if (it == interfaces_.end()) {
96         HILOGW("[OnRemoteRequest] fail: unknown code!");
97         return IRemoteStub<IBluetoothBleCentralManager>::OnRemoteRequest(code, data, reply, option);
98     }
99 
100     auto fun = it->second;
101     if (fun == nullptr) {
102         HILOGW("[OnRemoteRequest] fail: not find function!");
103         return IRemoteStub<IBluetoothBleCentralManager>::OnRemoteRequest(code, data, reply, option);
104     }
105 
106     ErrCode result = fun(this, data, reply);
107     if (SUCCEEDED(result)) {
108         return NO_ERROR;
109     }
110 
111     HILOGW("[OnRemoteRequest] fail: Failed to call interface %{public}u, err:%{public}d", code, result);
112     return result;
113 }
114 
RegisterBleCentralManagerCallbackInner(MessageParcel & data,MessageParcel & reply)115 ErrCode BluetoothBleCentralManagerStub::RegisterBleCentralManagerCallbackInner(
116     MessageParcel &data, MessageParcel &reply)
117 {
118     sptr<IRemoteObject> remote = data.ReadRemoteObject();
119     const sptr<IBluetoothBleCentralManagerCallback> callBack =
120         OHOS::iface_cast<IBluetoothBleCentralManagerCallback>(remote);
121     bool enableRandomAddrMode = data.ReadBool();
122     int32_t scannerId = 0;
123     RegisterBleCentralManagerCallback(scannerId, enableRandomAddrMode, callBack);
124     if (!reply.WriteInt32(scannerId)) {
125         HILOGE("reply writing failed");
126         return ERR_INVALID_VALUE;
127     }
128     return NO_ERROR;
129 }
130 
DeregisterBleCentralManagerCallbackInner(MessageParcel & data,MessageParcel & reply)131 ErrCode BluetoothBleCentralManagerStub::DeregisterBleCentralManagerCallbackInner(
132     MessageParcel &data, MessageParcel &reply)
133 {
134     int32_t scannerId = data.ReadInt32();
135     sptr<IRemoteObject> remote = data.ReadRemoteObject();
136     const sptr<IBluetoothBleCentralManagerCallback> callBack =
137         OHOS::iface_cast<IBluetoothBleCentralManagerCallback>(remote);
138     DeregisterBleCentralManagerCallback(scannerId, callBack);
139     return NO_ERROR;
140 }
141 
StartScanInner(MessageParcel & data,MessageParcel & reply)142 ErrCode BluetoothBleCentralManagerStub::StartScanInner(MessageParcel &data, MessageParcel &reply)
143 {
144     int32_t scannerId = data.ReadInt32();
145     std::shared_ptr<BluetoothBleScanSettings> settings(data.ReadParcelable<BluetoothBleScanSettings>());
146     if (settings == nullptr) {
147         HILOGW("[StartScanInner] fail: read settings failed");
148         return TRANSACTION_ERR;
149     }
150 
151     std::vector<BluetoothBleScanFilter> filters {};
152     int32_t itemsSize = 0;
153     if (!data.ReadInt32(itemsSize) || itemsSize > BLE_CENTRAL_MANAGER_STUB_READ_DATA_SIZE_MAX_LEN) {
154         HILOGE("read Parcelable size failed.");
155         return ERR_INVALID_VALUE;
156     }
157     for (int i = 0; i < itemsSize; i++) {
158         std::shared_ptr<BluetoothBleScanFilter> res(data.ReadParcelable<BluetoothBleScanFilter>());
159         if (res == nullptr) {
160             HILOGE("null pointer");
161             return ERR_INVALID_VALUE;
162         }
163         BluetoothBleScanFilter item = *(res);
164         filters.push_back(item);
165     }
166 
167     bool isNewApi = data.ReadBool();
168     int ret = StartScan(scannerId, *settings, filters, isNewApi);
169     if (!reply.WriteInt32(ret)) {
170         HILOGE("reply writing failed");
171         return ERR_INVALID_VALUE;
172     }
173     return NO_ERROR;
174 }
175 
StopScanInner(MessageParcel & data,MessageParcel & reply)176 ErrCode BluetoothBleCentralManagerStub::StopScanInner(MessageParcel &data, MessageParcel &reply)
177 {
178     int32_t scannerId = data.ReadInt32();
179     int ret = StopScan(scannerId);
180     if (!reply.WriteInt32(ret)) {
181         HILOGE("reply writing failed");
182         return ERR_INVALID_VALUE;
183     }
184     return NO_ERROR;
185 }
186 
RemoveScanFilterInner(MessageParcel & data,MessageParcel & reply)187 ErrCode BluetoothBleCentralManagerStub::RemoveScanFilterInner(MessageParcel &data, MessageParcel &reply)
188 {
189     int32_t scannerId = data.ReadInt32();
190 
191     RemoveScanFilter(scannerId);
192     return NO_ERROR;
193 }
194 
FreezeByRssInner(MessageParcel & data,MessageParcel & reply)195 ErrCode BluetoothBleCentralManagerStub::FreezeByRssInner(MessageParcel &data, MessageParcel &reply)
196 {
197     std::vector<int> pidVec {};
198     CHECK_AND_RETURN_LOG_RET(data.ReadInt32Vector(&pidVec), ERR_INVALID_VALUE, "ipc failed");
199     bool isProxy = data.ReadBool();
200 
201     std::set<int> pidSet(pidVec.begin(), pidVec.end());
202     bool ret = FreezeByRss(pidSet, isProxy);
203     if (!reply.WriteBool(ret)) {
204         return ERR_INVALID_VALUE;
205     }
206     return NO_ERROR;
207 }
208 
ResetAllProxyInner(MessageParcel & data,MessageParcel & reply)209 ErrCode BluetoothBleCentralManagerStub::ResetAllProxyInner(MessageParcel &data, MessageParcel &reply)
210 {
211     bool ret = ResetAllProxy();
212     if (!reply.WriteBool(ret)) {
213         return ERR_INVALID_VALUE;
214     }
215     return NO_ERROR;
216 }
217 
SetLpDeviceAdvParamInner(MessageParcel & data,MessageParcel & reply)218 ErrCode BluetoothBleCentralManagerStub::SetLpDeviceAdvParamInner(MessageParcel &data, MessageParcel &reply)
219 {
220     return NO_ERROR;
221 }
222 
SetScanReportChannelToLpDeviceInner(MessageParcel & data,MessageParcel & reply)223 ErrCode BluetoothBleCentralManagerStub::SetScanReportChannelToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
224 {
225     return NO_ERROR;
226 }
227 
EnableSyncDataToLpDeviceInner(MessageParcel & data,MessageParcel & reply)228 ErrCode BluetoothBleCentralManagerStub::EnableSyncDataToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
229 {
230     return NO_ERROR;
231 }
232 
DisableSyncDataToLpDeviceInner(MessageParcel & data,MessageParcel & reply)233 ErrCode BluetoothBleCentralManagerStub::DisableSyncDataToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
234 {
235     return NO_ERROR;
236 }
237 
SendParamsToLpDeviceInner(MessageParcel & data,MessageParcel & reply)238 ErrCode BluetoothBleCentralManagerStub::SendParamsToLpDeviceInner(MessageParcel &data, MessageParcel &reply)
239 {
240     return NO_ERROR;
241 }
242 
IsLpDeviceAvailableInner(MessageParcel & data,MessageParcel & reply)243 ErrCode BluetoothBleCentralManagerStub::IsLpDeviceAvailableInner(MessageParcel &data, MessageParcel &reply)
244 {
245     return NO_ERROR;
246 }
247 
SetLpDeviceParamInner(MessageParcel & data,MessageParcel & reply)248 ErrCode BluetoothBleCentralManagerStub::SetLpDeviceParamInner(MessageParcel &data, MessageParcel &reply)
249 {
250     return NO_ERROR;
251 }
252 
RemoveLpDeviceParamInner(MessageParcel & data,MessageParcel & reply)253 ErrCode BluetoothBleCentralManagerStub::RemoveLpDeviceParamInner(MessageParcel &data, MessageParcel &reply)
254 {
255     return NO_ERROR;
256 }
257 
ChangeScanParamsInner(MessageParcel & data,MessageParcel & reply)258 ErrCode BluetoothBleCentralManagerStub::ChangeScanParamsInner(MessageParcel &data, MessageParcel &reply)
259 {
260     int32_t scannerId = data.ReadInt32();
261     std::shared_ptr<BluetoothBleScanSettings> settings(data.ReadParcelable<BluetoothBleScanSettings>());
262     CHECK_AND_RETURN_LOG_RET(settings != nullptr, ERR_INVALID_VALUE, "settings nullptr");
263 
264     std::vector<BluetoothBleScanFilter> filters{};
265     int32_t filterSize = 0;
266     if (!data.ReadInt32(filterSize) || filterSize > BLE_CENTRAL_MANAGER_STUB_READ_DATA_SIZE_MAX_LEN) {
267         HILOGE("read Parcelable size failed.");
268         return ERR_INVALID_VALUE;
269     }
270 
271     for (int32_t i = 0; i < filterSize; i++) {
272         std::shared_ptr<BluetoothBleScanFilter> filter(data.ReadParcelable<BluetoothBleScanFilter>());
273         if (filter == nullptr) {
274             HILOGE("filter nullptr");
275             return ERR_INVALID_VALUE;
276         }
277         BluetoothBleScanFilter item = *filter;
278         filters.push_back(item);
279     }
280     uint32_t filterAction = 0;
281     if (!data.ReadUint32(filterAction)) {
282         HILOGE("read Parcelable filterAction failed.");
283         return ERR_INVALID_VALUE;
284     }
285     int32_t ret = ChangeScanParams(scannerId, *settings, filters, filterAction);
286     if (!reply.WriteInt32(ret)) {
287         HILOGE("reply writing failed.");
288         return ERR_INVALID_VALUE;
289     }
290     return NO_ERROR;
291 }
292 }  // namespace Bluetooth
293 }  // namespace OHOS
294