1 /*
2 * Copyright (C) 2021 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 #include "bluetooth_ble_central_manager.h"
16 #include "bluetooth_ble_central_manager_callback_stub.h"
17 #include "bluetooth_def.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_observer_list.h"
20 #include "i_bluetooth_ble_central_manager.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS {
25 namespace Bluetooth {
26 struct BleCentralManager::impl {
27 impl();
28 ~impl();
29
30 void BindServer();
31 class BluetoothBleCentralManagerCallbackImp : public BluetoothBleCentralManagerCallBackStub {
32 public:
BluetoothBleCentralManagerCallbackImp(BleCentralManager::impl & bleCentralManger)33 BluetoothBleCentralManagerCallbackImp(BleCentralManager::impl &bleCentralManger)
34 : bleCentralManger_(bleCentralManger){};
35 ~BluetoothBleCentralManagerCallbackImp() override = default;
OnScanCallback(const BluetoothBleScanResult & result)36 void OnScanCallback(const BluetoothBleScanResult &result) override
37 {
38 HILOGD("BleCentralManager::impl::BluetoothBleCentralManagerCallbackImp::OnScanCallback");
39 bleCentralManger_.callbacks_.ForEach([&result](std::shared_ptr<BleCentralManagerCallback> observer) {
40 BluetoothBleScanResult tempResult = result;
41 BleScanResult scanResult;
42 for (auto &manufacturerData : tempResult.GetManufacturerData()) {
43 scanResult.AddManufacturerData(manufacturerData.first, manufacturerData.second);
44 }
45
46 for (auto &serviceUuidData : tempResult.GetServiceUuids()) {
47 UUID uuid = UUID::ConvertFrom128Bits(serviceUuidData.ConvertTo128Bits());
48 scanResult.AddServiceUuid(uuid);
49 }
50
51 for (auto &serviceData : tempResult.GetServiceData()) {
52 UUID uuid = UUID::ConvertFrom128Bits(serviceData.first.ConvertTo128Bits());
53 scanResult.AddServiceData(uuid, serviceData.second);
54 }
55
56 scanResult.SetAdvertiseFlag(tempResult.GetAdvertiseFlag());
57 scanResult.SetRssi(tempResult.GetRssi());
58 scanResult.SetConnectable(tempResult.IsConnectable());
59 BluetoothRemoteDevice device(tempResult.GetPeripheralDevice().GetAddress(), BT_TRANSPORT_BLE);
60 scanResult.SetPeripheralDevice(device);
61 scanResult.SetPayload(tempResult.GetPayload());
62
63 observer->OnScanCallback(scanResult);
64 });
65 }
OnBleBatchScanResultsEvent(std::vector<BluetoothBleScanResult> & results)66 void OnBleBatchScanResultsEvent(std::vector<BluetoothBleScanResult> &results) override
67 {
68 HILOGD("BleCentralManager::impl::BluetoothBleCentralManagerCallbackImp::OnBleBatchScanResultsEvent");
69 bleCentralManger_.callbacks_.ForEach([&results](std::shared_ptr<BleCentralManagerCallback> observer) {
70 std::vector<BleScanResult> scanResults;
71 for (auto &result : results) {
72 BleScanResult scanResult;
73 for (auto &manufaturerData : result.GetManufacturerData()) {
74 scanResult.AddManufacturerData(manufaturerData.first, manufaturerData.second);
75 }
76
77 for (auto &serviceData : result.GetServiceData()) {
78 UUID uuid = UUID::ConvertFrom128Bits(serviceData.first.ConvertTo128Bits());
79 scanResult.AddServiceData(uuid, serviceData.second);
80 }
81
82 for (auto &serviceUuidData : result.GetServiceUuids()) {
83 UUID uuid = UUID::ConvertFrom128Bits(serviceUuidData.ConvertTo128Bits());
84 scanResult.AddServiceUuid(uuid);
85 }
86
87 scanResult.SetAdvertiseFlag(result.GetAdvertiseFlag());
88 scanResult.SetConnectable(result.IsConnectable());
89 scanResult.SetRssi(result.GetRssi());
90 BluetoothRemoteDevice device(result.GetPeripheralDevice().GetAddress(), BT_TRANSPORT_BLE);
91 scanResult.SetPeripheralDevice(device);
92 scanResult.SetPayload(result.GetPayload());
93
94 scanResults.push_back(scanResult);
95 }
96
97 observer->OnBleBatchScanResultsEvent(scanResults);
98 });
99 }
100
OnStartScanFailed(int resultCode)101 void OnStartScanFailed(int resultCode) override
102 {
103 bleCentralManger_.callbacks_.ForEach([resultCode](std::shared_ptr<BleCentralManagerCallback> observer) {
104 observer->OnStartScanFailed(resultCode);
105 });
106 }
107
108 private:
109 BleCentralManager::impl &bleCentralManger_;
110 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothBleCentralManagerCallbackImp);
111 };
112 sptr<BluetoothBleCentralManagerCallbackImp> callbackImp_ = nullptr;
113
114 sptr<IBluetoothBleCentralManager> proxy_ = nullptr;
115 BluetoothObserverList<BleCentralManagerCallback> callbacks_;
116 };
117
impl()118 BleCentralManager::impl::impl()
119 {
120 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
121 if (!samgr) {
122 HILOGE("BleCentralManager::impl::impl() error: no samgr");
123 return;
124 }
125
126 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
127 if (!hostRemote) {
128 HILOGE("BleCentralManager::impl::impl() error: host no remote");
129 return;
130 }
131 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
132 if (!hostProxy) {
133 HILOGE("BleCentralManager::impl::impl() error: host no proxy");
134 return;
135 }
136 sptr<IRemoteObject> remote = hostProxy->GetBleRemote(BLE_CENTRAL_MANAGER_SERVER);
137 if (!remote) {
138 HILOGE("BleCentralManager::impl::impl() error: no remote");
139 return;
140 }
141 proxy_ = iface_cast<IBluetoothBleCentralManager>(remote);
142 if (!proxy_) {
143 HILOGE("BleCentralManager::impl::impl() error: no proxy");
144 return;
145 }
146 callbackImp_ = new BluetoothBleCentralManagerCallbackImp(*this);
147 proxy_->RegisterBleCentralManagerCallback(callbackImp_);
148 }
149
~impl()150 BleCentralManager::impl::~impl()
151 {
152 proxy_->DeregisterBleCentralManagerCallback(callbackImp_);
153 }
154
BleCentralManager(BleCentralManagerCallback & callback)155 BleCentralManager::BleCentralManager(BleCentralManagerCallback &callback) : callback_(&callback), pimpl(nullptr)
156 {
157 if (pimpl == nullptr) {
158 pimpl = std::make_unique<impl>();
159 if (pimpl == nullptr) {
160 HILOGE("BleCentralManager::BleCentralManager fails: no pimpl");
161 }
162 }
163
164 HILOGD("BleCentralManager::BleCentralManager success");
165 std::shared_ptr<BleCentralManagerCallback> pointer(&callback, [](BleCentralManagerCallback *) {});
166 bool ret = pimpl->callbacks_.Register(pointer);
167 if (ret)
168 return;
169 callback_ = &callback;
170 }
171
~BleCentralManager()172 BleCentralManager::~BleCentralManager()
173 {
174 callback_ = nullptr;
175 }
176
StartScan()177 void BleCentralManager::StartScan()
178 {
179 if (pimpl->proxy_ != nullptr) {
180 HILOGD("BleCentralManager::StartScan success");
181 pimpl->proxy_->StartScan();
182 }
183 }
184
StartScan(const BleScanSettings & settings)185 void BleCentralManager::StartScan(const BleScanSettings &settings)
186 {
187 if (pimpl->proxy_ != nullptr) {
188 BluetoothBleScanSettings setting;
189 setting.SetReportDelay(settings.GetReportDelayMillisValue());
190 setting.SetScanMode(settings.GetScanMode());
191 setting.SetLegacy(settings.GetLegacy());
192 setting.SetPhy(settings.GetPhy());
193 pimpl->proxy_->StartScan(setting);
194 }
195 }
196
StopScan()197 void BleCentralManager::StopScan()
198 {
199 if (pimpl->proxy_ != nullptr) {
200 pimpl->proxy_->StopScan();
201 }
202 }
203
BleScanResult()204 BleScanResult::BleScanResult()
205 {}
206
~BleScanResult()207 BleScanResult::~BleScanResult()
208 {}
209
GetServiceUuids() const210 std::vector<UUID> BleScanResult::GetServiceUuids() const
211 {
212 return serviceUuids_;
213 }
214
GetManufacturerData() const215 std::map<uint16_t, std::string> BleScanResult::GetManufacturerData() const
216 {
217 return manufacturerSpecificData_;
218 }
219
GetServiceData() const220 std::map<UUID, std::string> BleScanResult::GetServiceData() const
221 {
222 return serviceData_;
223 }
224
GetPeripheralDevice() const225 BluetoothRemoteDevice BleScanResult::GetPeripheralDevice() const
226 {
227 return peripheralDevice_;
228 }
229
GetRssi() const230 int8_t BleScanResult::GetRssi() const
231 {
232 return rssi_;
233 }
234
IsConnectable() const235 bool BleScanResult::IsConnectable() const
236 {
237 return connectable_;
238 }
239
GetAdvertiseFlag() const240 uint8_t BleScanResult::GetAdvertiseFlag() const
241 {
242 return advertiseFlag_;
243 }
244
GetPayload() const245 std::vector<uint8_t> BleScanResult::GetPayload() const
246 {
247 return payload_;
248 }
249
AddManufacturerData(uint16_t manufacturerId,const std::string & data)250 void BleScanResult::AddManufacturerData(uint16_t manufacturerId, const std::string &data)
251 {
252 manufacturerSpecificData_.insert(std::make_pair(manufacturerId, data));
253 }
254
AddServiceData(const UUID & uuid,const std::string & data)255 void BleScanResult::AddServiceData(const UUID &uuid, const std::string &data)
256 {
257 serviceData_.insert(std::make_pair(uuid, data));
258 }
259
AddServiceUuid(const UUID & serviceUuid)260 void BleScanResult::AddServiceUuid(const UUID &serviceUuid)
261 {
262 serviceUuids_.push_back(serviceUuid);
263 }
264
SetPayload(std::string payload)265 void BleScanResult::SetPayload(std::string payload)
266 {
267 payload_.assign(payload.begin(), payload.end());
268 }
269
SetPeripheralDevice(const BluetoothRemoteDevice & device)270 void BleScanResult::SetPeripheralDevice(const BluetoothRemoteDevice &device)
271 {
272 peripheralDevice_ = device;
273 }
274
SetRssi(int8_t rssi)275 void BleScanResult::SetRssi(int8_t rssi)
276 {
277 rssi_ = rssi;
278 }
279
SetConnectable(bool connectable)280 void BleScanResult::SetConnectable(bool connectable)
281 {
282 connectable_ = connectable;
283 }
284
SetAdvertiseFlag(uint8_t flag)285 void BleScanResult::SetAdvertiseFlag(uint8_t flag)
286 {
287 advertiseFlag_ = flag;
288 }
289
BleScanSettings()290 BleScanSettings::BleScanSettings()
291 {}
292
~BleScanSettings()293 BleScanSettings::~BleScanSettings()
294 {}
295
SetReportDelay(long reportDelayMillis)296 void BleScanSettings::SetReportDelay(long reportDelayMillis)
297 {
298 reportDelayMillis_ = reportDelayMillis;
299 }
300
GetReportDelayMillisValue() const301 long BleScanSettings::GetReportDelayMillisValue() const
302 {
303 return reportDelayMillis_;
304 }
305
SetScanMode(int scanMode)306 int BleScanSettings::SetScanMode(int scanMode)
307 {
308 if (scanMode < SCAN_MODE_LOW_POWER || scanMode > SCAN_MODE_LOW_LATENCY) {
309 return RET_BAD_PARAM;
310 }
311
312 scanMode_ = scanMode;
313 return RET_NO_ERROR;
314 }
315
GetScanMode() const316 int BleScanSettings::GetScanMode() const
317 {
318 return scanMode_;
319 }
320
SetLegacy(bool legacy)321 void BleScanSettings::SetLegacy(bool legacy)
322 {
323 legacy_ = legacy;
324 }
325
GetLegacy() const326 bool BleScanSettings::GetLegacy() const
327 {
328 return legacy_;
329 }
330
SetPhy(int phy)331 void BleScanSettings::SetPhy(int phy)
332 {
333 phy_ = phy;
334 }
335
GetPhy() const336 int BleScanSettings::GetPhy() const
337 {
338 return phy_;
339 }
340 } // namespace Bluetooth
341 } // namespace OHOS
342