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
18 #include "bluetooth_ble_central_manager_stub.h"
19 #include "bluetooth_log.h"
20 #include "ipc_types.h"
21 #include "parcel_bt_uuid.h"
22
23 namespace OHOS {
24 namespace Bluetooth {
25 const int32_t BLE_CENTRAL_MANAGER_STUB_READ_DATA_SIZE_MAX_LEN = 0x100;
26 const std::map<uint32_t, std::function<ErrCode(BluetoothBleCentralManagerStub *, MessageParcel &, MessageParcel &)>>
27 BluetoothBleCentralManagerStub::interfaces_ = {
28 {IBluetoothBleCentralManager::Code::BLE_REGISTER_BLE_CENTRAL_MANAGER_CALLBACK,
29 std::bind(&BluetoothBleCentralManagerStub::RegisterBleCentralManagerCallbackInner, std::placeholders::_1,
30 std::placeholders::_2, std::placeholders::_3)},
31 {IBluetoothBleCentralManager::Code::BLE_DE_REGISTER_BLE_CENTRAL_MANAGER_CALLBACK,
32 std::bind(&BluetoothBleCentralManagerStub::DeregisterBleCentralManagerCallbackInner, std::placeholders::_1,
33 std::placeholders::_2, std::placeholders::_3)},
34 {IBluetoothBleCentralManager::Code::BLE_START_SCAN,
35 std::bind(&BluetoothBleCentralManagerStub::StartScanInner, std::placeholders::_1, std::placeholders::_2,
36 std::placeholders::_3)},
37 {IBluetoothBleCentralManager::Code::BLE_START_SCAN_WITH_SETTINGS,
38 std::bind(&BluetoothBleCentralManagerStub::StartScanWithSettingsInner, std::placeholders::_1,
39 std::placeholders::_2, std::placeholders::_3)},
40 {IBluetoothBleCentralManager::Code::BLE_CONFIG_SCAN_FILTER,
41 std::bind(&BluetoothBleCentralManagerStub::ConfigScanFilterInner, std::placeholders::_1,
42 std::placeholders::_2, std::placeholders::_3)},
43 {IBluetoothBleCentralManager::Code::BLE_REMOVE_SCAN_FILTER,
44 std::bind(&BluetoothBleCentralManagerStub::RemoveScanFilterInner, std::placeholders::_1,
45 std::placeholders::_2, std::placeholders::_3)},
46 {IBluetoothBleCentralManager::Code::BLE_STOP_SCAN,
47 std::bind(&BluetoothBleCentralManagerStub::StopScanInner, std::placeholders::_1, std::placeholders::_2,
48 std::placeholders::_3)},
49 {IBluetoothBleCentralManager::Code::BLE_PROXY_UID,
50 std::bind(&BluetoothBleCentralManagerStub::ProxyUidInner, std::placeholders::_1, std::placeholders::_2,
51 std::placeholders::_3)},
52 {IBluetoothBleCentralManager::Code::BLE_RESET_ALL_PROXY,
53 std::bind(&BluetoothBleCentralManagerStub::ResetAllProxyInner, std::placeholders::_1, std::placeholders::_2,
54 std::placeholders::_3)},
55 };
56
BluetoothBleCentralManagerStub()57 BluetoothBleCentralManagerStub::BluetoothBleCentralManagerStub()
58 {}
59
~BluetoothBleCentralManagerStub()60 BluetoothBleCentralManagerStub::~BluetoothBleCentralManagerStub()
61 {}
62
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)63 int BluetoothBleCentralManagerStub::OnRemoteRequest(
64 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
65 {
66 if (BluetoothBleCentralManagerStub::GetDescriptor() != data.ReadInterfaceToken()) {
67 HILOGW("[OnRemoteRequest] fail: invalid interface token!");
68 return OBJECT_NULL;
69 }
70
71 auto it = interfaces_.find(code);
72 if (it == interfaces_.end()) {
73 HILOGW("[OnRemoteRequest] fail: unknown code!");
74 return IRemoteStub<IBluetoothBleCentralManager>::OnRemoteRequest(code, data, reply, option);
75 }
76
77 auto fun = it->second;
78 if (fun == nullptr) {
79 HILOGW("[OnRemoteRequest] fail: not find function!");
80 return IRemoteStub<IBluetoothBleCentralManager>::OnRemoteRequest(code, data, reply, option);
81 }
82
83 ErrCode result = fun(this, data, reply);
84 if (SUCCEEDED(result)) {
85 return NO_ERROR;
86 }
87
88 HILOGW("[OnRemoteRequest] fail: Failed to call interface %{public}u, err:%{public}d", code, result);
89 return result;
90 }
91
RegisterBleCentralManagerCallbackInner(MessageParcel & data,MessageParcel & reply)92 ErrCode BluetoothBleCentralManagerStub::RegisterBleCentralManagerCallbackInner(
93 MessageParcel &data, MessageParcel &reply)
94 {
95 sptr<IRemoteObject> remote = data.ReadRemoteObject();
96 const sptr<IBluetoothBleCentralManagerCallback> callBack =
97 OHOS::iface_cast<IBluetoothBleCentralManagerCallback>(remote);
98 RegisterBleCentralManagerCallback(callBack);
99 return NO_ERROR;
100 }
101
DeregisterBleCentralManagerCallbackInner(MessageParcel & data,MessageParcel & reply)102 ErrCode BluetoothBleCentralManagerStub::DeregisterBleCentralManagerCallbackInner(
103 MessageParcel &data, MessageParcel &reply)
104 {
105 sptr<IRemoteObject> remote = data.ReadRemoteObject();
106 const sptr<IBluetoothBleCentralManagerCallback> callBack =
107 OHOS::iface_cast<IBluetoothBleCentralManagerCallback>(remote);
108 DeregisterBleCentralManagerCallback(callBack);
109 return NO_ERROR;
110 }
111
StartScanInner(MessageParcel & data,MessageParcel & reply)112 ErrCode BluetoothBleCentralManagerStub::StartScanInner(MessageParcel &data, MessageParcel &reply)
113 {
114 int ret = StartScan();
115 if (!reply.WriteInt32(ret)) {
116 HILOGE("reply writing faileded");
117 return ERR_INVALID_VALUE;
118 }
119 return NO_ERROR;
120 }
121
StartScanWithSettingsInner(MessageParcel & data,MessageParcel & reply)122 ErrCode BluetoothBleCentralManagerStub::StartScanWithSettingsInner(MessageParcel &data, MessageParcel &reply)
123 {
124 std::shared_ptr<BluetoothBleScanSettings> settings(data.ReadParcelable<BluetoothBleScanSettings>());
125 if (settings == nullptr) {
126 HILOGW("[StartScanWithSettingsInner] fail: read settings failed");
127 return TRANSACTION_ERR;
128 }
129
130 int ret = StartScan(*settings);
131 if (!reply.WriteInt32(ret)) {
132 HILOGE("reply writing faileded");
133 return ERR_INVALID_VALUE;
134 }
135 return NO_ERROR;
136 }
137
StopScanInner(MessageParcel & data,MessageParcel & reply)138 ErrCode BluetoothBleCentralManagerStub::StopScanInner(MessageParcel &data, MessageParcel &reply)
139 {
140 int ret = StopScan();
141 if (!reply.WriteInt32(ret)) {
142 HILOGE("reply writing faileded");
143 return ERR_INVALID_VALUE;
144 }
145 return NO_ERROR;
146 }
ConfigScanFilterInner(MessageParcel & data,MessageParcel & reply)147 ErrCode BluetoothBleCentralManagerStub::ConfigScanFilterInner(MessageParcel &data, MessageParcel &reply)
148 {
149 std::vector<BluetoothBleScanFilter> filters {};
150 int32_t clientId = data.ReadInt32();
151 int32_t itemsSize = 0;
152 if (!data.ReadInt32(itemsSize) || itemsSize > BLE_CENTRAL_MANAGER_STUB_READ_DATA_SIZE_MAX_LEN) {
153 HILOGE("read Parcelable size failed.");
154 return ERR_INVALID_VALUE;
155 }
156 for (int i = 0; i < itemsSize; i++) {
157 BluetoothBleScanFilter item = *(data.ReadParcelable<BluetoothBleScanFilter>());
158 filters.push_back(item);
159 }
160
161 int result = ConfigScanFilter(clientId, filters);
162 bool ret = reply.WriteInt32(result);
163 if (!ret) {
164 HILOGE("BluetoothBleCentralManagerStub: reply writing failed in: %{public}s.", __func__);
165 return ERR_INVALID_VALUE;
166 }
167 return NO_ERROR;
168 }
169
RemoveScanFilterInner(MessageParcel & data,MessageParcel & reply)170 ErrCode BluetoothBleCentralManagerStub::RemoveScanFilterInner(MessageParcel &data, MessageParcel &reply)
171 {
172 int32_t clientId = data.ReadInt32();
173
174 RemoveScanFilter(clientId);
175 return NO_ERROR;
176 }
177
ProxyUidInner(MessageParcel & data,MessageParcel & reply)178 ErrCode BluetoothBleCentralManagerStub::ProxyUidInner(MessageParcel &data, MessageParcel &reply)
179 {
180 int32_t uid = data.ReadInt32();
181 bool isProxy = data.ReadBool();
182
183 bool ret = ProxyUid(uid, isProxy);
184 if (!reply.WriteBool(ret)) {
185 return ERR_INVALID_VALUE;
186 }
187 return NO_ERROR;
188 }
189
ResetAllProxyInner(MessageParcel & data,MessageParcel & reply)190 ErrCode BluetoothBleCentralManagerStub::ResetAllProxyInner(MessageParcel &data, MessageParcel &reply)
191 {
192 bool ret = ResetAllProxy();
193 if (!reply.WriteBool(ret)) {
194 return ERR_INVALID_VALUE;
195 }
196 return NO_ERROR;
197 }
198 } // namespace Bluetooth
199 } // namespace OHOS
200