/src/connectedhomeip/src/platform/Linux/bluez/ChipDeviceScanner.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2021 Project CHIP Authors |
4 | | * |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <platform/CHIPDeviceConfig.h> |
21 | | |
22 | | #include <glib.h> |
23 | | |
24 | | #include <ble/Ble.h> |
25 | | #include <lib/core/CHIPError.h> |
26 | | #include <platform/GLibTypeDeleter.h> |
27 | | #include <platform/Linux/dbus/bluez/DBusBluez.h> |
28 | | #include <system/SystemLayer.h> |
29 | | |
30 | | #include "BluezObjectManager.h" |
31 | | #include "Types.h" |
32 | | |
33 | | namespace chip { |
34 | | namespace DeviceLayer { |
35 | | namespace Internal { |
36 | | |
37 | | /// Receives callbacks when chip devices are being scanned |
38 | | class ChipDeviceScannerDelegate |
39 | | { |
40 | | public: |
41 | 107 | virtual ~ChipDeviceScannerDelegate() {} |
42 | | |
43 | | // Called when a CHIP device was found |
44 | | virtual void OnDeviceScanned(BluezDevice1 & device, const chip::Ble::ChipBLEDeviceIdentificationInfo & info) = 0; |
45 | | |
46 | | // Called when a scan was completed (stopped or timed out) |
47 | | virtual void OnScanComplete() = 0; |
48 | | |
49 | | // Call on scan error |
50 | | virtual void OnScanError(CHIP_ERROR) = 0; |
51 | | }; |
52 | | |
53 | | /// Allows scanning for CHIP devices |
54 | | /// |
55 | | /// Will perform scan operations and call back whenever a device is discovered. |
56 | | class ChipDeviceScanner : public BluezObjectManagerAdapterNotificationsDelegate |
57 | | { |
58 | | public: |
59 | 52 | ChipDeviceScanner(BluezObjectManager & aObjectManager) : mObjectManager(aObjectManager) {} |
60 | | ChipDeviceScanner(ChipDeviceScanner &&) = default; |
61 | | ChipDeviceScanner(const ChipDeviceScanner &) = delete; |
62 | | ChipDeviceScanner & operator=(const ChipDeviceScanner &) = delete; |
63 | | |
64 | 107 | ~ChipDeviceScanner() { Shutdown(); } |
65 | | |
66 | | /// Initialize the scanner. |
67 | | CHIP_ERROR Init(BluezAdapter1 * adapter, ChipDeviceScannerDelegate * delegate); |
68 | | |
69 | | /// Release any resources associated with the scanner. |
70 | | void Shutdown(); |
71 | | |
72 | | /// Initiate a scan for devices, with the given timeout |
73 | | /// |
74 | | /// This method must be called while in the Matter context (from the Matter event |
75 | | /// loop, or while holding the Matter stack lock). |
76 | | CHIP_ERROR StartScan(); |
77 | | |
78 | | /// Stop any currently running scan |
79 | | CHIP_ERROR StopScan(); |
80 | | |
81 | | /// Check if the scanner is active |
82 | 0 | bool IsScanning() const { return mScannerState == ChipDeviceScannerState::SCANNING; } |
83 | | |
84 | | /// Members that implement virtual methods on BluezObjectManagerAdapterNotificationsDelegate |
85 | | void OnDeviceAdded(BluezDevice1 & device) override; |
86 | | void OnDevicePropertyChanged(BluezDevice1 & device, GVariant * changedProps, const char * const * invalidatedProps) override; |
87 | 0 | void OnDeviceRemoved(BluezDevice1 & device) override {} |
88 | | |
89 | | private: |
90 | | enum class ChipDeviceScannerState |
91 | | { |
92 | | UNINITIALIZED, |
93 | | INITIALIZED, |
94 | | SCANNING |
95 | | }; |
96 | | |
97 | | CHIP_ERROR StartScanImpl(); |
98 | | CHIP_ERROR StopScanImpl(); |
99 | | |
100 | | /// Check if a given device is a CHIP device and if yes, report it as discovered |
101 | | void ReportDevice(BluezDevice1 & device); |
102 | | |
103 | | /// Check if a given device is a CHIP device and if yes, remove it from the adapter |
104 | | /// so that it can be re-discovered if it's still advertising. |
105 | | void RemoveDevice(BluezDevice1 & device); |
106 | | |
107 | | BluezObjectManager & mObjectManager; |
108 | | GAutoPtr<BluezAdapter1> mAdapter; |
109 | | |
110 | | ChipDeviceScannerDelegate * mDelegate = nullptr; |
111 | | ChipDeviceScannerState mScannerState = ChipDeviceScannerState::UNINITIALIZED; |
112 | | GAutoPtr<GCancellable> mCancellable; |
113 | | }; |
114 | | |
115 | | } // namespace Internal |
116 | | } // namespace DeviceLayer |
117 | | } // namespace chip |