Kallisto Linux API
Library for interacting with Kallisto devices
kallisto_impl.hpp
Go to the documentation of this file.
1 
42 #ifndef KALLISTO_IMPL_HPP
43 #define KALLISTO_IMPL_HPP
44 
45 #include <string>
46 #include <vector>
47 #include <map>
48 #include <thread>
49 #include <mutex>
50 #include <atomic>
51 
52 #include "../util/error/kallisto_error.hpp"
53 
54 #include "kallisto.hpp"
61 #include "../bluetooth/adapter/bluetooth_adapter.hpp"
62 #include "../bluetooth/device/bluetooth_device.hpp"
63 #include "../bluetooth/device/bluetooth_dispatcher.hpp"
64 
65 namespace kallisto {
66 namespace hardware {
67 
71 class KallistoImpl : public Kallisto, public bluetooth::BluetoothDispatcher {
72 private:
74  static const std::string DIS_SERVICE_UUID; //Device information service UUID
76  static const std::string SW_VERSION_UUID; //Software Version Characteristic UUID
78  static const std::string FW_VERSION_UUID; //Firmware Version Characteristic UUID
80  static const std::string HW_VERSION_UUID; //Hardware Version Characteristic UUID
82  static const std::string MANUFACTURER_UUID; //Manufacturer Characteristic UUID
83 
85  std::atomic_bool m_valid {false}; //Signals if this object is valid
87  bluetooth::BluetoothDevice* m_bluetooth_device; //The native bluetooth device
89  std::atomic_bool m_discovered {false}; //Signals if already got the metadata from the device
91  std::atomic_bool m_stay_connected {false}; //Signals if we should keep a connection to the device
92 
93  //Link Loss variables
95  std::atomic_bool m_link_loss {false}; //Signals if we are in link loss
97  std::atomic_bool m_link_removed {false}; //Signals if we are in removal procedure
99  std::thread m_link_loss_worker; //Link loss timeout thread
101  std::mutex m_link_loss_worker_mutex; //Link loss mutex for the condition variable
103  std::condition_variable m_link_loss_cv; //Link loss condition variable to wait for a timeout
104 
106  std::map<std::string, std::vector<uint8_t>> m_service_data;
108  std::map<uint16_t, std::vector<uint8_t>> m_manufacturer_data;
110  std::list<KallistoEventListener*> m_listeners; //Holds the event listeners.
112  std::unique_ptr<TimeSynchronizer> m_synchronizer = nullptr;
113 
115  SensorFactory& m_sensor_factory; //The sensor builder
117  std::vector<std::unique_ptr<SensorImpl> > m_sensors; //The sensor list
119  util::Executor& m_event_dispatcher; //The event dispatcher
121  std::mutex m_mutex; //Used to sync the object
122 
126  KallistoImpl(KallistoImpl const&) = delete;
127  KallistoImpl(KallistoImpl&&) = delete;
128  void operator=(KallistoImpl const&) = delete;
129  void operator=(KallistoImpl &&) = delete;
130 
135  void closeInternal();
136 
141  error::KallistoDetailedResult connect();
142 
146  void onConnection();
147 
152  error::KallistoDetailedResult discover();
153 
158  error::KallistoDetailedResult disconnect();
159 
163  void onDisconnection();
164 
169  bool getConnected();
170 
175  bool getBeingUsedInternal();
176 
177 public:
189  KallistoImpl(kallisto::bluetooth::BluetoothDevice& bluetooth_device, const bluetooth::BluetoothAdapter::ScanListener::ScanRecord& record, bool legacy_discovery,
190  util::Executor& event_dispatcher, SensorFactory& sensor_factory);
191 
195  virtual ~KallistoImpl();
196 
200  void close();
201 
202  /*************************************************************
203  * Kallisto methods.
204  *************************************************************/
205 
210  bool getBeingUsed();
211 
220  bool getLinkLoss();
221 
222  /*************************************************************
223  * Public API
224  *************************************************************/
230  error::KallistoDetailedResult registerListener(KallistoEventListener& listener);
231 
237  error::KallistoDetailedResult unregisterListener(KallistoEventListener& listener);
238 
248  error::KallistoDetailedResult stayConnected(bool enable);
249 
254  std::vector<Sensor*> getSensors();
255 
260  Sensor* getSensor(unsigned int handle);
261 
269  error::KallistoDetailedResult registerSensorListener(Sensor& sensor, std::map<std::string, const void*> parameters, SensorEventListener& listener);
270 
277  error::KallistoDetailedResult unregisterSensorListener(Sensor& sensor, SensorEventListener& listener);
278 
284  error::KallistoDetailedResult onBluetoothDeviceRecovered(kallisto::bluetooth::BluetoothDevice& bluetooth_device);
285 
286  /*************************************************************
287  * BluetoothDispatcher methods.
288  * @see BluetoothDispatcher
289  *************************************************************/
290  error::KallistoDetailedResult getRssi(int& buffer);
291  std::vector<std::string> getServices();
292  std::vector<std::string> getCharacteristics(std::string service_uuid);
293  error::KallistoDetailedResult readCharacteristic(std::string service_uuid, std::string characteristic_uuid, std::vector<unsigned char>& buffer);
294  error::KallistoDetailedResult writeCharacteristic(std::string service_uuid, std::string characteristic_uuid, std::vector<unsigned char>& buffer);
295  error::KallistoDetailedResult enableNotification(std::string service_uuid, std::string characteristic_uuid, BluetoothDispatcher::NotificationListener& listener);
296  error::KallistoDetailedResult disableNotification(std::string service_uuid, std::string characteristic_uuid, BluetoothDispatcher::NotificationListener& listener);
297 };
298 
299 } /* namespace hardware */
300 } /* namespace kallisto */
301 
302 #endif // KALLISTO_IMPL_HPP
error::KallistoDetailedResult onBluetoothDeviceRecovered(kallisto::bluetooth::BluetoothDevice &bluetooth_device)
std::vector< std::string > getServices()
error::KallistoDetailedResult unregisterListener(KallistoEventListener &listener)
std::vector< Sensor * > getSensors()
std::vector< std::string > getCharacteristics(std::string service_uuid)
Definition: bluetooth_adapter.hpp:52
Definition: bluetooth_device.hpp:58
error::KallistoDetailedResult getRssi(int &buffer)
error::KallistoDetailedResult readCharacteristic(std::string service_uuid, std::string characteristic_uuid, std::vector< unsigned char > &buffer)
Sensor * getSensor(unsigned int handle)
error::KallistoDetailedResult stayConnected(bool enable)
error::KallistoDetailedResult writeCharacteristic(std::string service_uuid, std::string characteristic_uuid, std::vector< unsigned char > &buffer)
error::KallistoDetailedResult registerSensorListener(Sensor &sensor, std::map< std::string, const void *> parameters, SensorEventListener &listener)
error::KallistoDetailedResult unregisterSensorListener(Sensor &sensor, SensorEventListener &listener)
error::KallistoDetailedResult registerListener(KallistoEventListener &listener)