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