Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Tizen / bluez / ChipDeviceScanner.h
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 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
23
24 #include <glib.h>
25 #include <memory>
26
27 #include <ble/CHIPBleServiceData.h>
28 #include <core/CHIPError.h>
29 #include <platform/Linux/dbus/bluez/DbusBluez.h>
30 #include <system/SystemLayer.h>
31
32 namespace chip {
33 namespace DeviceLayer {
34 namespace Internal {
35
36 /// Receives callbacks when chip devices are being scanned
37 class ChipDeviceScannerDelegate
38 {
39 public:
40     virtual ~ChipDeviceScannerDelegate() {}
41
42     // Called when a CHIP device was found
43     virtual void OnDeviceScanned(BluezDevice1 * device, const chip::Ble::ChipBLEDeviceIdentificationInfo & info) = 0;
44
45     // Called when a scan was completed (stopped or timed out)
46     virtual void OnScanComplete() = 0;
47 };
48
49 /// Allows scanning for CHIP devices
50 ///
51 /// Will perform scan operations and call back whenever a device is discovered.
52 class ChipDeviceScanner
53 {
54 public:
55     /// NOTE: prefer to use the  ::Create method instead direct constructor calling.
56     ChipDeviceScanner(GDBusObjectManager * manager, BluezAdapter1 * adapter, GCancellable * cancellable,
57                       ChipDeviceScannerDelegate * delegate);
58
59     ChipDeviceScanner(ChipDeviceScanner &&)      = default;
60     ChipDeviceScanner(const ChipDeviceScanner &) = delete;
61     ChipDeviceScanner & operator=(const ChipDeviceScanner &) = delete;
62
63     ~ChipDeviceScanner();
64
65     /// Initiate a scan for devices, with the given timeout
66     CHIP_ERROR StartScan(unsigned timeoutMs);
67
68     /// Stop any currently running scan
69     CHIP_ERROR StopScan();
70
71     /// Create a new device scanner
72     ///
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);
76
77 private:
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);
85
86     /// Check if a given device is a CHIP device and if yes, report it as discovered
87     void ReportDevice(BluezDevice1 * device);
88
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;
97 };
98
99 } // namespace Internal
100 } // namespace DeviceLayer
101 } // namespace chip
102
103 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE