Kallisto Linux API
Library for interacting with Kallisto devices
kallisto_core_impl.hpp
Go to the documentation of this file.
1 
42 #ifndef KALLISTO_CORE_IMPL_HPP_
43 #define KALLISTO_CORE_IMPL_HPP_
44 
45 #include <mutex>
46 #include <list>
47 #include <memory>
48 
49 #include "core/kallisto_core.hpp"
50 
51 #include "../util/error/kallisto_error.hpp"
52 #include "../util/log/logger_configurator.hpp"
53 
54 #include "../bluetooth/adapter/bluetooth_adapter_manager.hpp"
55 #include "../bluetooth/scanner/bluetooth_scanner.hpp"
56 #include "../bluetooth/bluetooth_adapter_event_listener.hpp"
57 #include "../bluetooth/bluetooth_scan_event_listener.hpp"
58 
59 #include "../hardware/kallisto_factory.hpp"
60 #include "../hardware/kallisto_impl.hpp"
61 #include "../hardware/kallisto_helper.hpp"
62 #include "../hardware/kallisto_event_listener.hpp"
63 #include "../hardware/hal/sensors/sensor_factory.hpp"
64 #include "../hardware/hal/sensors/sensor.hpp"
65 #include "../hardware/hal/sensors/sensor_event_listener.hpp"
66 
67 namespace kallisto {
68 namespace core {
69 
72 
73 private:
74  //Event dispatcher
75  util::Executor m_event_dispatcher{"CoreEventDispatcher", 1};
76 
77  //Holds our Bluetooth adapter
78  std::unique_ptr<bluetooth::BluetoothAdapterManager> m_adapter_manager = nullptr;
79  bluetooth::BluetoothAdapter* m_adapter = nullptr;
80 
81  //Holds our Bluetooth scanner variables
82  std::unique_ptr<bluetooth::BluetoothScanner> m_scanner = nullptr;
83  bluetooth::BluetoothScannerConfiguration m_scanner_configuration;
84  bluetooth::BluetoothScannerFilter m_scanner_filter;
85 
86  //Event listeners variables
87  enum class AdapterEventType {
88  ADAPTER_ENABLED, ADAPTER_DISABLED, ADAPTER_IRRECUPERABLE
89  };
90  enum class ScanEventType {
91  DEVICE_FOUND, DEVICE_LOST, DEVICE_REPORT
92  };
93  enum class KallistoEventType {
94  LINK_LOSS, LINK_RECOVERED, LINK_REMOVED
95  };
96  std::mutex m_listener_list_mutex;
97  std::list<bluetooth::BluetoothAdapterEventListener*> m_adapter_listeners;
98  std::list<bluetooth::BluetoothScanEventListener*> m_scan_listeners;
99 
100  //Link Loss flags
101  enum class LinkLossScanStatus {
102  SCANNING, STOPPING, STOPPED
103  };
104  std::atomic<LinkLossScanStatus> m_link_loss_scan_status {LinkLossScanStatus::STOPPED};
105 
106  //Device factories
107  hardware::SensorFactory m_sensor_factory;
108  hardware::KallistoFactory m_device_factory{m_sensor_factory};
109 
110  //Device list variables
111  std::mutex m_device_list_mutex;
112  std::list<std::unique_ptr<hardware::KallistoImpl> > m_device_list;
113  std::list<std::unique_ptr<hardware::KallistoImpl> > m_device_to_remove_list;
114 
118  void start();
119 
123  void stop();
124 
131  void removeDevice(hardware::Kallisto& kallisto);
132 
136  void stopScanningForLinkLoss();
137 
141  void onLinkLossScanStop();
142 
147  void onNormalScanStop(std::vector<kallisto::bluetooth::BluetoothDevice*> devices_found);
148 
154  void onScanBuildDevice(bluetooth::BluetoothDevice& device, const bluetooth::BluetoothAdapter::ScanListener::ScanRecord record);
155 
161  void onScanRecoverDevice(bluetooth::BluetoothDevice& device);
162 
163 
164 public:
169 
174 
178  KallistoCoreImpl(KallistoCoreImpl const&) = delete;
180  void operator=(KallistoCoreImpl const&) = delete;
181  void operator=(KallistoCoreImpl &&) = delete;
182 
183  /*********************************************************************
184  * System Manager API
185  ********************************************************************/
194 
203 
212 
219 
228 
235 
244 
253 
254  /*********************************************************************
255  * Kallisto Manager API
256  ********************************************************************/
261  std::vector<hardware::KallistoImpl*> getDevices();
262 
272 
282 
294 
295  /*********************************************************************
296  * Sensor Manager API
297  ********************************************************************/
305  error::KallistoDetailedResult registerSensorListener(hardware::SensorEventListener& listener, hardware::Sensor& sensor, std::map<std::string, const void*> parameters);
306 
314 
315  /*********************************************************************
316  * Bluetooth adapter callbacks
317  ********************************************************************/
328 
334 
340 
346 
351  void sendAdapterEvent(AdapterEventType type);
352 
353  /*********************************************************************
354  * Bluetooth scanner callbacks
355  ********************************************************************/
359  void onScanStart();
360 
364  void onScanStartFailed();
365 
374  void onScanStop(std::vector<kallisto::bluetooth::BluetoothDevice*> devices_found);
375 
382 
389  void sendScanEvent(ScanEventType type, hardware::Kallisto& device, const bluetooth::BluetoothAdapter::ScanListener::ScanRecord record);
390 
391  /*********************************************************************
392  * Kallisto callbacks
393  ********************************************************************/
399  void onLinkLoss(std::chrono::nanoseconds timestamp, hardware::Kallisto& device);
400 
406  void onLinkRecovered(std::chrono::nanoseconds timestamp, hardware::Kallisto& device);
407 
413  void onLinkRemoved(std::chrono::nanoseconds /*timestamp*/, hardware::Kallisto& device);
414 
419  void sendKallistoEvent(KallistoEventType type);
420 };
421 
422 } /* namespace core */
423 } /* namespace kallisto */
424 
425 #endif /* KALLISTO_CORE_IMPL_HPP_ */
426 
427 
error::KallistoDetailedResult unregisterBluetoothAdapterEventListener(bluetooth::BluetoothAdapterEventListener &listener)
error::KallistoDetailedResult unregisterListener(hardware::KallistoEventListener &listener, hardware::Kallisto &kallisto)
Definition: sensor.hpp:53
void sendAdapterEvent(AdapterEventType type)
void sendScanEvent(ScanEventType type, hardware::Kallisto &device, const bluetooth::BluetoothAdapter::ScanListener::ScanRecord record)
error::KallistoDetailedResult stayConnected(hardware::Kallisto &kallisto, bool enable)
void sendKallistoEvent(KallistoEventType type)
error::KallistoDetailedResult registerSensorListener(hardware::SensorEventListener &listener, hardware::Sensor &sensor, std::map< std::string, const void *> parameters)
void onAdapterDisconnected(bluetooth::BluetoothAdapter &adapter)
Definition: bluetooth_adapter.hpp:52
Definition: bluetooth_adapter.hpp:67
Definition: kallisto_event_listener.hpp:56
KallistoCoreImpl(kallisto::bluetooth::BluetoothScannerConfiguration &scanner_configuration, kallisto::bluetooth::BluetoothScannerFilter &scanner_filter)
void onAdapterEnableFailed(bluetooth::BluetoothAdapter &adapter, bluetooth::BluetoothAdapter::Listener::Error error)
Class that encapsulates a result.
Definition: kallisto_error.hpp:81
Definition: bluetooth_adapter_event_listener.hpp:52
error::KallistoDetailedResult unregisterBluetoothScanEventListener(bluetooth::BluetoothScanEventListener &listener)
Error
Definition: bluetooth_adapter.hpp:75
Definition: bluetooth_device.hpp:58
error::KallistoDetailedResult registerBluetoothScanEventListener(bluetooth::BluetoothScanEventListener &listener)
void onAdapterEnabled(bluetooth::BluetoothAdapter &adapter)
void onLinkRecovered(std::chrono::nanoseconds timestamp, hardware::Kallisto &device)
error::KallistoDetailedResult registerListener(hardware::KallistoEventListener &listener, hardware::Kallisto &kallisto)
error::KallistoDetailedResult registerBluetoothAdapterEventListener(bluetooth::BluetoothAdapterEventListener &listener)
error::KallistoDetailedResult setScannerConfiguration(bluetooth::BluetoothScannerConfiguration &configuration)
Definition: bluetooth_scanner_filter.hpp:64
Definition: bluetooth_adapter.hpp:62
Definition: kallisto.hpp:52
Definition: bluetooth_scanner_configuration.hpp:92
void onScan(bluetooth::BluetoothDevice &device, const bluetooth::BluetoothAdapter::ScanListener::ScanRecord record)
Definition: executor.hpp:71
Definition: sensor_factory.hpp:60
const bluetooth::BluetoothScannerFilter * getScannerFilter()
Definition: kallisto_core.hpp:57
void onLinkLoss(std::chrono::nanoseconds timestamp, hardware::Kallisto &device)
const bluetooth::BluetoothScannerConfiguration * getScannerConfiguration()
Definition: kallisto_factory.hpp:56
std::vector< hardware::KallistoImpl * > getDevices()
void onAdapterDisabled(bluetooth::BluetoothAdapter &adapter)
void onLinkRemoved(std::chrono::nanoseconds, hardware::Kallisto &device)
error::KallistoDetailedResult setScannerFilter(bluetooth::BluetoothScannerFilter &filter)
error::KallistoDetailedResult unregisterSensorListener(hardware::SensorEventListener &listener, hardware::Sensor &sensor)
Definition: bluetooth_scan_event_listener.hpp:57
void onAdapterConnected(bluetooth::BluetoothAdapter &adapter)
Definition: bluetooth_adapter_manager.hpp:63
void onScanStop(std::vector< kallisto::bluetooth::BluetoothDevice *> devices_found)
Definition: sensor_event_listener.hpp:123
Definition: bluetooth_adapter.hpp:103
Definition: kallisto_core_impl.hpp:69