BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
DBusHciInterface.h
1 #pragma once
2 
3 #include <glib.h>
4 #include <gio/gio.h>
5 
6 #include <functional>
7 #include <map>
8 #include <string>
9 #include <vector>
10 
11 #include <Poco/Condition.h>
12 #include <Poco/Mutex.h>
13 #include <Poco/RunnableAdapter.h>
14 #include <Poco/SharedPtr.h>
15 #include <Poco/Thread.h>
16 #include <Poco/Timespan.h>
17 #include <Poco/Timestamp.h>
18 
19 #include "bluetooth/HciInfo.h"
20 #include "bluetooth/HciInterface.h"
21 #include "bluetooth/GlibPtr.h"
22 #include "bluetooth/org-bluez-adapter1.h"
23 #include "bluetooth/org-bluez-device1.h"
24 #include "bluetooth/org-bluez-gattcharacteristic1.h"
25 #include "net/MACAddress.h"
26 #include "util/Loggable.h"
27 #include "util/WaitCondition.h"
28 
29 namespace BeeeOn {
30 
44 public:
45  class Device;
46  friend class DBusHciConnection;
47 
48  typedef Poco::SharedPtr<DBusHciInterface> Ptr;
49  typedef std::function<bool(const std::string& path)> PathFilter;
50  typedef std::pair<Poco::FastMutex, std::map<MACAddress, Device>> ThreadSafeDevices;
51 
59  class Device {
60  public:
65  Device(
66  const GlibPtr<OrgBluezDevice1> device,
67  const uint64_t rssiHandle);
68 
69  GlibPtr<OrgBluezDevice1> device() const
70  {
71  return m_device;
72  }
73 
74  uint64_t rssiHandle() const
75  {
76  return m_rssiHandle;
77  }
78 
79  void updateLastSeen()
80  {
81  m_lastSeen = Poco::Timestamp();
82  }
83 
84  Poco::Timestamp lastSeen() const
85  {
86  return m_lastSeen;
87  }
88 
89  uint64_t watchHandle() const
90  {
91  return m_watchHandle;
92  }
93 
94  void watch(
95  uint64_t watchHandle,
96  Poco::SharedPtr<WatchCallback> callBack)
97  {
98  m_watchHandle = watchHandle;
99  m_callBack = callBack;
100  }
101 
102  void unwatch()
103  {
104  m_watchHandle = 0;
105  m_callBack = nullptr;
106  }
107 
108  bool isWatched() const
109  {
110  return !m_callBack.isNull();
111  }
112 
113  std::string name();
114  MACAddress macAddress();
115  int16_t rssi();
116 
117  private:
118  GlibPtr<OrgBluezDevice1> m_device;
119  Poco::Timestamp m_lastSeen;
120  uint64_t m_rssiHandle;
121  uint64_t m_watchHandle;
122  Poco::SharedPtr<WatchCallback> m_callBack;
123  };
124 
125 
136  const std::string& name,
137  const Poco::Timespan& leMaxAgeRssi,
138  const Poco::Timespan& leMaxUnavailabilityTime,
139  const Poco::Timespan& classicArtificialAvaibilityTimeout);
140  ~DBusHciInterface();
141 
146  void down() const;
147 
148  void up() const override;
149  void reset() const override;
150 
157  bool detect(const MACAddress &address) const override;
158 
163  std::map<MACAddress, std::string> scan() const override;
164 
170  std::map<MACAddress, std::string> lescan(
171  const Poco::Timespan &timeout) const override;
172 
176  HciInfo info() const override;
177 
178  HciConnection::Ptr connect(
179  const MACAddress& address,
180  const Poco::Timespan& timeout) const override;
181 
182  void watch(
183  const MACAddress& address,
184  Poco::SharedPtr<WatchCallback> callBack) override;
185  void unwatch(const MACAddress& address) override;
186 
187 protected:
192  static gboolean onStopLoop(gpointer loop);
193 
199  static void onDBusObjectAdded(
200  GDBusObjectManager* objectManager,
201  GDBusObject* object,
202  gpointer userData);
203 
208  static gboolean onDeviceRSSIChanged(
209  OrgBluezDevice1* device,
210  GVariant* properties,
211  const gchar* const* invalidatedProperties,
212  gpointer userData);
213 
217  static gboolean onDeviceManufacturerDataRecieved(
218  OrgBluezDevice1* device,
219  GVariant* properties,
220  const gchar* const* invalidatedProperties,
221  gpointer userData);
222 
227  static void processManufacturerData(
228  OrgBluezDevice1* device,
229  GVariant* value,
230  gpointer userData);
231 
232 private:
239  void waitUntilPoweredChange(GlibPtr<OrgBluezAdapter1> adapter, const bool powered) const;
240 
246  void startDiscovery(
248  const std::string& trasport) const;
249 
253  void stopDiscovery(GlibPtr<OrgBluezAdapter1> adapter) const;
254 
260  void initDiscoveryFilter(
262  const std::string& trasport) const;
263 
267  std::vector<GlibPtr<OrgBluezDevice1>> processKnownDevices(
268  GlibPtr<GDBusObjectManager> objectManager) const;
269 
273  void removeUnvailableDevices() const;
274 
279  void runLoop();
280 
285  static std::vector<std::string> retrievePathsOfBluezObjects(
286  GlibPtr<GDBusObjectManager> objectManager,
287  PathFilter pathFilter,
288  const std::string& objectFilter);
289 
293  static const std::string createAdapterPath(const std::string& name);
294 
298  static const std::string createDevicePath(
299  const std::string& name,
300  const MACAddress& address);
301 
307  static GlibPtr<GDBusObjectManager> createBluezObjectManager();
308 
313  static GlibPtr<OrgBluezAdapter1> retrieveBluezAdapter(const std::string& path);
314 
319  static GlibPtr<OrgBluezDevice1> retrieveBluezDevice(const std::string& path);
320 
321 private:
322  std::string m_name;
323  mutable WaitCondition m_resetCondition;
324 
325  GlibPtr<GMainLoop> m_loop;
326  Poco::RunnableAdapter<DBusHciInterface> m_loopThread;
327  Poco::Thread m_thread;
328  GlibPtr<GDBusObjectManager> m_objectManager;
329  uint64_t m_objectManagerHandle;
330  mutable ThreadSafeDevices m_devices;
331  mutable GlibPtr<OrgBluezAdapter1> m_adapter;
332  Poco::Timespan m_leMaxAgeRssi;
333  Poco::Timespan m_leMaxUnavailabilityTime;
334 
335  mutable std::map<MACAddress, Poco::Timestamp> m_seenClassicDevices;
336  Poco::Timespan m_classicArtificialAvaibilityTimeout;
337  mutable Poco::FastMutex m_classicMutex;
338 
339  mutable Poco::Condition m_condition;
340  mutable Poco::FastMutex m_statusMutex;
341  mutable Poco::FastMutex m_discoveringMutex;
342 };
343 
345 public:
347 
348  void setLeMaxAgeRssi(const Poco::Timespan& time);
349  void setLeMaxUnavailabilityTime(const Poco::Timespan& time);
350 
355  void setClassicArtificialAvaibilityTimeout(const Poco::Timespan& time);
356 
357  HciInterface::Ptr lookup(const std::string &name) override;
358 
359 private:
360  Poco::FastMutex m_mutex;
361  std::map<std::string, DBusHciInterface::Ptr> m_interfaces;
362  Poco::Timespan m_leMaxAgeRssi;
363  Poco::Timespan m_leMaxUnavailabilityTime;
364  Poco::Timespan m_classicArtificialAvaibilityTimeout;
365 };
366 
367 }
void down() const
Sets hci interface down.
Definition: DBusHciInterface.cpp:112
Definition: HciInterface.h:87
bool detect(const MACAddress &address) const override
Uses detect method of BluezHciInterface class to check state of device with MACAddress. If the device is unavailable and has been seen within a certain time (m_classicUnavailableMaxAge), the method returns true.
Definition: DBusHciInterface.cpp:134
The class realizes communication with BlueZ deamon using D-Bus. It allows to find new BLE and Bluetoo...
Definition: DBusHciInterface.h:43
static gboolean onStopLoop(gpointer loop)
Callback handling the timeout event which stops the given GMainLoop.
Definition: DBusHciInterface.cpp:432
static gboolean onDeviceRSSIChanged(OrgBluezDevice1 *device, GVariant *properties, const gchar *const *invalidatedProperties, gpointer userData)
Callback handling the event of RSSI changed. It is used to detecting that the device is available...
Definition: DBusHciInterface.cpp:469
static void processManufacturerData(OrgBluezDevice1 *device, GVariant *value, gpointer userData)
Process the single advertising data and call callback stored in userData.
Definition: DBusHciInterface.cpp:524
HciConnection::Ptr connect(const MACAddress &address, const Poco::Timespan &timeout) const override
Definition: DBusHciInterface.cpp:223
Definition: MACAddress.h:8
void reset() const override
Definition: DBusHciInterface.cpp:128
Definition: HciInfo.h:15
static void onDBusObjectAdded(GDBusObjectManager *objectManager, GDBusObject *object, gpointer userData)
Callback handling the event of creating new device. The method adds the new device to m_devices and r...
Definition: DBusHciInterface.cpp:438
std::map< MACAddress, std::string > scan() const override
Uses scan method of BluezHciInterface class to discover the bluetooh classic devices.
Definition: DBusHciInterface.cpp:167
Device(const GlibPtr< OrgBluezDevice1 > device, const uint64_t rssiHandle)
Definition: DBusHciInterface.cpp:604
DBusHciInterface(const std::string &name, const Poco::Timespan &leMaxAgeRssi, const Poco::Timespan &leMaxUnavailabilityTime, const Poco::Timespan &classicArtificialAvaibilityTimeout)
Definition: DBusHciInterface.cpp:28
void up() const override
Definition: DBusHciInterface.cpp:97
The class represents connection with Bluetooth Low energy device. It allows sending read/write reques...
Definition: DBusHciConnection.h:24
HciInfo info() const override
Uses BluezHciInterface to retrieve hci info.
Definition: DBusHciInterface.cpp:217
Definition: Loggable.h:19
void watch(const MACAddress &address, Poco::SharedPtr< WatchCallback > callBack) override
Definition: DBusHciInterface.cpp:249
std::map< MACAddress, std::string > lescan(const Poco::Timespan &timeout) const override
Scans the Bluetooth LE network and returns all available devices. The method returns devices whitch u...
Definition: DBusHciInterface.cpp:173
void unwatch(const MACAddress &address) override
Definition: DBusHciInterface.cpp:277
WaitCondition works as a barrier that waits until some condition is met. When the condition is met an...
Definition: WaitCondition.h:19
void setClassicArtificialAvaibilityTimeout(const Poco::Timespan &time)
Sets the maximum time from the last seen of the Bluetooth Classic device to declare the device is ava...
Definition: DBusHciInterface.cpp:656
Definition: HciInterface.h:16
Definition: DBusHciInterface.h:344
static gboolean onDeviceManufacturerDataRecieved(OrgBluezDevice1 *device, GVariant *properties, const gchar *const *invalidatedProperties, gpointer userData)
Callback handling the event of receiving advertising data.
Definition: DBusHciInterface.cpp:499
The class represents the Bluetooth Low Energy device and stores necessary data about device such as i...
Definition: DBusHciInterface.h:59