BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
BluetoothAvailabilityManager.h
1 #pragma once
2 
3 #include <list>
4 #include <map>
5 #include <string>
6 
7 #include <Poco/Logger.h>
8 #include <Poco/Mutex.h>
9 #include <Poco/RunnableAdapter.h>
10 #include <Poco/Thread.h>
11 #include <Poco/Timespan.h>
12 
13 #include "bluetooth/BluetoothDevice.h"
14 #include "bluetooth/HciInterface.h"
15 #include "commands/DeviceAcceptCommand.h"
16 #include "core/DongleDeviceManager.h"
17 #include "model/DeviceID.h"
18 #include "model/SensorData.h"
19 
20 namespace BeeeOn {
21 
23 public:
25 
30  void dongleAvailable() override;
31 
32  bool dongleMissing() override;
33 
34  void dongleFailed(const FailDetector &dongleStatus) override;
35 
40  std::string dongleMatch(const HotplugEvent &e) override;
41 
42  void stop() override;
43 
44  Poco::Timespan detectAll(const HciInterface &hci);
45 
49  void setWakeUpTime(const Poco::Timespan &time);
50 
51  void setModes(const std::list<std::string> &modes);
52 
56  void setLEScanTime(const Poco::Timespan &time);
57 
61  void setHciManager(HciInterfaceManager::Ptr manager);
62 
63  void handleRemoteStatus(
64  const DevicePrefix &prefix,
65  const std::set<DeviceID> &devices,
66  const DeviceStatusHandler::DeviceValues &values) override;
67 
68 protected:
69  void handleAccept(const DeviceAcceptCommand::Ptr cmd) override;
70  AsyncWork<>::Ptr startDiscovery(const Poco::Timespan &timeout) override;
72  const DeviceID &id,
73  const Poco::Timespan &timeout) override;
74  void notifyDongleRemoved() override;
75 
76 private:
77  /*
78  * Scan for all paired devices. Any inactive device is
79  * temporarily saved. Information about active devices
80  * is immediatelly shipped.
81  * @return list of (potentially) inactive devices
82  */
83  std::list<DeviceID> detectClassic(const HciInterface &hci);
84 
85  /*
86  * Scan for BLE devices.
87  * There is no inactive list.
88  * The last BLE scan result is stored in m_leScanCache.
89  * m_leScanCache exists for fast result of listen command.
90  */
91  void detectLE(const HciInterface &hci);
92 
93  bool haveTimeForInactive(Poco::Timespan elapsedTime);
94 
99  void loadPairedDevices();
100 
101  bool enoughTimeForScan(const Poco::Timestamp &startTime);
102 
103  void reportFoundDevices(const int mode, const std::map<MACAddress, std::string> &devices);
104 
105  void listen();
106 
107  void removeDevice(const DeviceID &id);
108 
109  void shipStatusOf(const BluetoothDevice &device);
110 
111  void sendNewDevice(
112  const DeviceID &id,
113  const MACAddress &address,
114  const std::string &name);
115 
116  std::list<ModuleType> moduleTypes() const;
117 
118  DeviceID createDeviceID(const MACAddress &numMac) const;
119 
120  DeviceID createLEDeviceID(const MACAddress &numMac) const;
121 
122  Poco::RunnableAdapter<BluetoothAvailabilityManager> m_listenThread;
123  Poco::Timespan m_wakeUpTime;
124  Poco::Timespan m_leScanTime;
125  Poco::Thread m_thread;
126  std::map<DeviceID, BluetoothDevice> m_deviceList;
127  Poco::FastMutex m_lock;
128  Poco::FastMutex m_scanLock;
129  HciInterfaceManager::Ptr m_hciManager;
130  Poco::Timespan m_listenTime;
131  int m_mode;
132  std::map<MACAddress, std::string> m_leScanCache;
133 };
134 
135 }
Definition: FailDetector.h:8
void handleRemoteStatus(const DevicePrefix &prefix, const std::set< DeviceID > &devices, const DeviceStatusHandler::DeviceValues &values) override
Handle device status as understood by a remote server. All devices of a certain prefix are notified i...
Definition: BluetoothAvailabilityManager.cpp:318
Definition: BluetoothAvailabilityManager.h:22
void stop() override
Definition: BluetoothAvailabilityManager.cpp:167
std::string dongleMatch(const HotplugEvent &e) override
Recognizes compatible dongle by testing HotplugEvent property as bluetooth.BEEEON_DONGLE == bluetooth...
Definition: BluetoothAvailabilityManager.cpp:162
void handleAccept(const DeviceAcceptCommand::Ptr cmd) override
Generic handler of the DeviceAcceptCommand. It might be helpful to override this method in case we ne...
Definition: BluetoothAvailabilityManager.cpp:277
AsyncWork is an interface allowing to wait until an asynchronous operation finishes. It is also possible to force-stop it by calling cancel().
Definition: AsyncWork.h:21
Definition: MACAddress.h:8
bool dongleMissing() override
Definition: BluetoothAvailabilityManager.cpp:121
AsyncWork< std::set< DeviceID > >::Ptr startUnpair(const DeviceID &id, const Poco::Timespan &timeout) override
Starts device unpair process in a technology-specific way. This method is always called inside a crit...
Definition: BluetoothAvailabilityManager.cpp:294
void dongleAvailable() override
Definition: BluetoothAvailabilityManager.cpp:94
void setLEScanTime(const Poco::Timespan &time)
Definition: BluetoothAvailabilityManager.cpp:76
void dongleFailed(const FailDetector &dongleStatus) override
Definition: BluetoothAvailabilityManager.cpp:136
Definition: HotplugEvent.h:10
AsyncWork::Ptr startDiscovery(const Poco::Timespan &timeout) override
Starts device discovery process in a technology-specific way. This method is always called inside a c...
Definition: BluetoothAvailabilityManager.cpp:286
Definition: DongleDeviceManager.h:14
void setHciManager(HciInterfaceManager::Ptr manager)
Definition: BluetoothAvailabilityManager.cpp:463
void notifyDongleRemoved() override
The default implementation does nothing.
Definition: BluetoothAvailabilityManager.cpp:151
Definition: BluetoothDevice.h:16
Definition: DeviceID.h:17
Definition: HciInterface.h:16
void setWakeUpTime(const Poco::Timespan &time)
Definition: BluetoothAvailabilityManager.cpp:66
DevicePrefix prefix() const override
Definition: DeviceManager.cpp:33