Kallisto Linux API
Library for interacting with Kallisto devices
time_adjust_synchronizer.hpp
Go to the documentation of this file.
1 
42 #ifndef TIME_ADJUST_SYNCHRONIZER_HPP_
43 #define TIME_ADJUST_SYNCHRONIZER_HPP_
44 
45 #include <list>
46 #include <chrono>
47 #include <limits>
48 #include <mutex>
49 
50 #include "time_synchronizer.hpp"
51 #include "../sensors/sensor.hpp"
52 
53 namespace kallisto {
54 namespace hardware {
55 
62 class TimeAjustSynchronizer : public TimeSynchronizer {
63 
64 private:
68  class SensorSyncContainer {
69  private:
70  unsigned int m_sensor_handle; //Sensor handle.
71  std::chrono::nanoseconds m_timestamp_reference = std::chrono::nanoseconds(std::numeric_limits<long>::max()); //Sensor reference
72  std::chrono::nanoseconds m_latest_timestamp = std::chrono::nanoseconds(std::numeric_limits<long>::max()); //Latest sensor raw timestamp
73  std::chrono::nanoseconds m_real_latest_timestamp = std::chrono::nanoseconds(std::numeric_limits<long>::max()); //The host timestamp from latest sensor sample
74 
75  public:
79  SensorSyncContainer(unsigned int sensor_handle) {
80  m_sensor_handle = sensor_handle;
81  }
82 
86  unsigned int getSensorHandle() {
87  return m_sensor_handle;
88  }
89 
93  std::chrono::nanoseconds getTimestampReference() {
94  return m_timestamp_reference;
95  }
96 
101  void setTimestampReference(std::chrono::nanoseconds timestamp_reference) {
102  m_timestamp_reference = timestamp_reference;
103  }
104 
108  std::chrono::nanoseconds getLatestTimestamp() {
109  return m_latest_timestamp;
110  }
111 
116  void setLatestTimestamp(std::chrono::nanoseconds latest_timestamp) {
117  m_latest_timestamp = latest_timestamp;
118  }
119 
123  std::chrono::nanoseconds getRealLatestTimestamp() {
124  return m_real_latest_timestamp;
125  }
126 
130  void setRealTimestamp() {
131  m_real_latest_timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch());
132  }
133  };
134 
135  //The timeout to remove sensor container.
136  static const std::chrono::nanoseconds SENSOR_TIMEOUT_MILLIS;
137 
138  //Maximum timestamp value reported by the sensor.
139  static const std::chrono::nanoseconds SENSOR_MAX_RAW_TIMESTAMP;
140 
141  //Our list to save sensor sync data
142  std::list<SensorSyncContainer> m_sensor_containers;
143 
144  //Used to sync the object
145  std::mutex m_mutex;
146 
151 
155  void cleanSensorContainers();
156 
163  std::chrono::nanoseconds calculateSensorReference(std::chrono::nanoseconds raw_sensor_timestamp);
164 
172  std::chrono::nanoseconds syncSensorTimestamp(SensorSyncContainer& sensor_container, std::chrono::nanoseconds raw_sensor_timestamp);
173 
178  error::KallistoDetailedResult sync();
179 
180 public:
184  TimeAjustSynchronizer(bluetooth::BluetoothDispatcher& dispatcher);
185 
191  error::KallistoDetailedResult syncSensorEvent(Sensor& sensor, SensorEvent& event);
192 };
193 
194 } /* namespace bluetooth */
195 } /* namespace kallisto */
196 
197 #endif /* TIME_ADJUST_SYNCHRONIZER_HPP_ */
error::KallistoDetailedResult syncSensorEvent(Sensor &sensor, SensorEvent &event)
Definition: bluetooth_adapter.hpp:52
TimeAjustSynchronizer(bluetooth::BluetoothDispatcher &dispatcher)