3 * Copyright (c) 2021 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <platform/CHIPDeviceConfig.h>
22 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
27 #include <ble/CHIPBleServiceData.h>
28 #include <core/CHIPError.h>
29 #include <platform/Linux/dbus/bluez/DbusBluez.h>
30 #include <system/SystemLayer.h>
33 namespace DeviceLayer {
36 /// Receives callbacks when chip devices are being scanned
37 class ChipDeviceScannerDelegate
40 virtual ~ChipDeviceScannerDelegate() {}
42 // Called when a CHIP device was found
43 virtual void OnDeviceScanned(BluezDevice1 * device, const chip::Ble::ChipBLEDeviceIdentificationInfo & info) = 0;
45 // Called when a scan was completed (stopped or timed out)
46 virtual void OnScanComplete() = 0;
49 /// Allows scanning for CHIP devices
51 /// Will perform scan operations and call back whenever a device is discovered.
52 class ChipDeviceScanner
55 /// NOTE: prefer to use the ::Create method instead direct constructor calling.
56 ChipDeviceScanner(GDBusObjectManager * manager, BluezAdapter1 * adapter, GCancellable * cancellable,
57 ChipDeviceScannerDelegate * delegate);
59 ChipDeviceScanner(ChipDeviceScanner &&) = default;
60 ChipDeviceScanner(const ChipDeviceScanner &) = delete;
61 ChipDeviceScanner & operator=(const ChipDeviceScanner &) = delete;
65 /// Initiate a scan for devices, with the given timeout
66 CHIP_ERROR StartScan(unsigned timeoutMs);
68 /// Stop any currently running scan
69 CHIP_ERROR StopScan();
71 /// Create a new device scanner
73 /// Convenience method to allocate any required variables.
74 /// On success, maintains a reference to the provided adapter.
75 static std::unique_ptr<ChipDeviceScanner> Create(BluezAdapter1 * adapter, ChipDeviceScannerDelegate * delegate);
78 static void TimerExpiredCallback(chip::System::Layer * layer, void * appState, chip::System::Error error);
79 static int MainLoopStartScan(ChipDeviceScanner * self);
80 static int MainLoopStopScan(ChipDeviceScanner * self);
81 static void SignalObjectAdded(GDBusObjectManager * manager, GDBusObject * object, ChipDeviceScanner * self);
82 static void SignalInterfaceChanged(GDBusObjectManagerClient * manager, GDBusObjectProxy * object, GDBusProxy * aInterface,
83 GVariant * aChangedProperties, const gchar * const * aInvalidatedProps,
84 ChipDeviceScanner * self);
86 /// Check if a given device is a CHIP device and if yes, report it as discovered
87 void ReportDevice(BluezDevice1 * device);
89 GDBusObjectManager * mManager = nullptr;
90 BluezAdapter1 * mAdapter = nullptr;
91 GCancellable * mCancellable = nullptr;
92 ChipDeviceScannerDelegate * mDelegate = nullptr;
93 gulong mObjectAddedSignal = 0;
94 gulong mInterfaceChangedSignal = 0;
95 bool mIsScanning = false;
96 bool mIsStopping = false;
99 } // namespace Internal
100 } // namespace DeviceLayer
103 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE