backend-hid.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 #pragma once
4 
5 #include "backend.h"
6 #include "types.h"
7 
8 #include <limits.h>
9 #include <list>
10 
11 namespace librealsense
12 {
13  namespace platform
14  {
15  const uint32_t hid_buf_len = 128;
16 
18  {
21  int index = -1;
22  bool enabled = false;
23 
31  // TODO: parse 'offset' and 'scale'
32  };
33 
34  // SYSFS or HAL for (IIO) device drivers differs requires a non-standard std::fstream operation mode:
35  // The first in/out operation selects the mode exclusively.
36  // Switching from read to write or vice versa requires fstream's close/open sequence.
37  // Writing/appending to the fstream requires flush synchronization
38  template<typename T>
39  inline bool write_fs_attribute(const std::string& path, const T& val)
40  {
42  "write_fs_attribute supports arithmetic and std::string types only");
43 
44  bool res = false;
45  std::fstream fs_handle(path);
46  if (!fs_handle.good())
47  {
48  LOG_WARNING(__FUNCTION__ <<" with " << val << " failed. The specified path " << path << " is not valid");
49  return res;
50  }
51 
52  try // Read/Modify/Confirm
53  {
54  T cval{};
55  fs_handle >> cval;
56 
57  if (cval!=val)
58  {
59  fs_handle.close();
60  fs_handle.open(path);
61  fs_handle << val;
62  fs_handle.flush();
63  std::ifstream vnv_handle(path);
64  vnv_handle >> cval;
65  fs_handle >> cval;
66  res = (cval==val);
67  if (!res)
68  LOG_WARNING(__FUNCTION__ << " Could not change " << cval << " to " << val << " : path " << path);
69  }
70  }
71  catch (const std::exception& exc)
72  {
73  LOG_WARNING(__FUNCTION__ << " with " << path << " failed: " << exc.what());
74  }
75 
76  return res;
77  }
78 
79  // manage an IIO input. or what is called a scan.
80  class hid_input
81  {
82  public:
83  hid_input(const std::string& iio_device_path, const std::string& input_name);
84  ~hid_input();
85 
86  // enable scan input. doing so cause the input to be part of the data provided in the polling.
87  void enable(bool is_enable);
88 
89  const hid_input_info& get_hid_input_info() const { return info; }
90 
91  private:
92  // initialize the input by reading the input parameters.
93  void init();
94 
96  };
97 
99  public:
100  hid_custom_sensor(const std::string& device_path, const std::string& sensor_name);
101 
103 
104  std::vector<uint8_t> get_report_data(const std::string& report_name, custom_sensor_report_field report_field);
105 
107 
108  // start capturing and polling.
109  void start_capture(hid_callback sensor_callback);
110 
111  void stop_capture();
112  private:
113  std::vector<uint8_t> read_report(const std::string& name_report_path);
114 
115  void init();
116 
117  void enable(bool state);
118 
119  void signal_stop();
120 
121  int _fd;
122  int _stop_pipe_fd[2]; // write to _stop_pipe_fd[1] and read from _stop_pipe_fd[0]
123  std::map<std::string, std::string> _reports;
128  std::atomic<bool> _is_capturing;
129  std::unique_ptr<std::thread> _hid_thread;
130  };
131 
132  // declare device sensor with all of its inputs.
134  public:
135  iio_hid_sensor(const std::string& device_path, uint32_t frequency, float sensitivity);
136 
137  ~iio_hid_sensor();
138 
139  // start capturing and polling.
140  void start_capture(hid_callback sensor_callback);
141 
142  void stop_capture();
143 
144  const std::string& get_sensor_name() const { return _sensor_name; }
145 
146  private:
147  void clear_buffer();
148 
149  void set_frequency(uint32_t frequency);
150  void set_sensitivity( float sensitivity );
151  void set_power(bool on);
152 
153  void signal_stop();
154 
155  bool has_metadata();
156 
157  static bool sort_hids(hid_input* first, hid_input* second);
158 
159  void create_channel_array();
160 
161  // initialize the device sensor. reading its name and all of its inputs.
162  void init(uint32_t frequency, float sensitivity);
163 
164  // calculate the storage size of a scan
165  uint32_t get_channel_size() const;
166 
167  // calculate the actual size of data
168  uint32_t get_output_size() const;
169 
171 
173 
174  // read the IIO device inputs.
175  void read_device_inputs();
176 
177  int _stop_pipe_fd[2]; // write to _stop_pipe_fd[1] and read from _stop_pipe_fd[0]
178  int _fd;
184  std::list<hid_input*> _inputs;
185  std::list<hid_input*> _channels;
187  std::atomic<bool> _is_capturing;
188  std::unique_ptr<std::thread> _hid_thread;
189  std::unique_ptr<std::thread> _pm_thread; // Delayed initialization due to power-up sequence
190  dispatcher _pm_dispatcher; // Asynchronous power management
191  };
192 
193  class v4l_hid_device : public hid_device
194  {
195  public:
197 
198  ~v4l_hid_device();
199 
200  void register_profiles(const std::vector<hid_profile>& hid_profiles) override { _hid_profiles = hid_profiles;}
201 
202  void open(const std::vector<hid_profile>& hid_profiles) override;
203 
204  void close() override;
205 
206  std::vector<hid_sensor> get_sensors() override;
207 
208  void start_capture(hid_callback callback) override;
209 
210  void stop_capture() override;
211 
212  std::vector<uint8_t> get_custom_report_data(const std::string& custom_sensor_name,
213  const std::string& report_name,
214  custom_sensor_report_field report_field) override;
215 
216  static void foreach_hid_device(std::function<void(const hid_device_info&)> action);
217 
218  void set_gyro_scale_factor( double scale_factor ) override{};
219 
220  private:
221  static bool get_hid_device_info(const char* dev_path, hid_device_info& device_info);
222 
223  std::vector<hid_profile> _hid_profiles;
224  std::vector<hid_device_info> _hid_device_infos;
225  std::vector<std::unique_ptr<iio_hid_sensor>> _iio_hid_sensors;
226  std::vector<std::unique_ptr<hid_custom_sensor>> _hid_custom_sensors;
227  std::vector<iio_hid_sensor*> _streaming_iio_sensors;
228  std::vector<hid_custom_sensor*> _streaming_custom_sensors;
229  static constexpr const char* custom_id{"custom"};
230  };
231  }
232 }
librealsense::platform::iio_hid_sensor::_inputs
std::list< hid_input * > _inputs
Definition: backend-hid.h:184
librealsense::platform::write_fs_attribute
bool write_fs_attribute(const std::string &path, const T &val)
Definition: backend-hid.h:39
librealsense::platform::hid_input::get_hid_input_info
const hid_input_info & get_hid_input_info() const
Definition: backend-hid.h:89
librealsense::platform::hid_callback
std::function< void(const sensor_data &) > hid_callback
Definition: platform/hid-device.h:56
librealsense::platform::iio_hid_sensor::_channels
std::list< hid_input * > _channels
Definition: backend-hid.h:185
librealsense::platform::hid_device
Definition: platform/hid-device.h:71
librealsense
Definition: algo.h:18
librealsense::platform::v4l_hid_device::_hid_profiles
std::vector< hid_profile > _hid_profiles
Definition: backend-hid.h:223
librealsense::platform::hid_custom_sensor::read_report
std::vector< uint8_t > read_report(const std::string &name_report_path)
Definition: backend-hid.cpp:357
librealsense::platform::hid_custom_sensor::_custom_device_name
std::string _custom_device_name
Definition: backend-hid.h:126
librealsense::platform::v4l_hid_device::open
void open(const std::vector< hid_profile > &hid_profiles) override
Definition: backend-hid.cpp:1039
enabled
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: glad/glad/glad.h:3607
librealsense::platform::hid_custom_sensor::_is_capturing
std::atomic< bool > _is_capturing
Definition: backend-hid.h:128
librealsense::platform::hid_custom_sensor::_reports
std::map< std::string, std::string > _reports
Definition: backend-hid.h:123
librealsense::platform::hid_custom_sensor::signal_stop
void signal_stop()
Definition: backend-hid.cpp:446
librealsense::device_info
Definition: device-info.h:21
librealsense::platform::hid_device_info
Definition: hid-device-info.h:13
librealsense::platform::v4l_hid_device::get_sensors
std::vector< hid_sensor > get_sensors() override
Definition: backend-hid.cpp:1101
librealsense::platform::iio_hid_sensor::_iio_device_number
int _iio_device_number
Definition: backend-hid.h:179
librealsense::platform::v4l_hid_device::_hid_custom_sensors
std::vector< std::unique_ptr< hid_custom_sensor > > _hid_custom_sensors
Definition: backend-hid.h:226
librealsense::platform::hid_custom_sensor::_callback
hid_callback _callback
Definition: backend-hid.h:127
librealsense::platform::hid_custom_sensor::_hid_thread
std::unique_ptr< std::thread > _hid_thread
Definition: backend-hid.h:129
librealsense::platform::hid_custom_sensor::stop_capture
void stop_capture()
Definition: backend-hid.cpp:331
librealsense::platform::iio_hid_sensor::signal_stop
void signal_stop()
Definition: backend-hid.cpp:744
librealsense::platform::iio_hid_sensor::get_channel_size
uint32_t get_channel_size() const
Definition: backend-hid.cpp:853
rspy.devices.path
path
Definition: unit-tests/py/rspy/devices.py:47
librealsense::platform::iio_hid_sensor::create_channel_array
void create_channel_array()
Definition: backend-hid.cpp:767
librealsense::platform::v4l_hid_device::custom_id
static constexpr const char * custom_id
Definition: backend-hid.h:229
librealsense::platform::v4l_hid_device::set_gyro_scale_factor
void set_gyro_scale_factor(double scale_factor) override
Definition: backend-hid.h:218
librealsense::platform::hid_input_info::is_signed
uint32_t is_signed
Definition: backend-hid.h:27
index
GLuint index
Definition: glad/glad/glad.h:2777
librealsense::platform::iio_hid_sensor::stop_capture
void stop_capture()
Definition: backend-hid.cpp:647
string
GLsizei const GLchar *const * string
Definition: glad/glad/glad.h:2861
librealsense::platform::v4l_hid_device::_iio_hid_sensors
std::vector< std::unique_ptr< iio_hid_sensor > > _iio_hid_sensors
Definition: backend-hid.h:225
librealsense::bytes
Definition: align.cpp:22
librealsense::platform::iio_hid_sensor::_sampling_frequency_name
std::string _sampling_frequency_name
Definition: backend-hid.h:182
librealsense::platform::iio_hid_sensor::iio_hid_sensor
iio_hid_sensor(const std::string &device_path, uint32_t frequency, float sensitivity)
Definition: backend-hid.cpp:456
librealsense::platform::hid_input::init
void init()
Definition: backend-hid.cpp:88
librealsense::platform::v4l_hid_device::register_profiles
void register_profiles(const std::vector< hid_profile > &hid_profiles) override
Definition: backend-hid.h:200
librealsense::platform::hid_input::hid_input
hid_input(const std::string &iio_device_path, const std::string &input_name)
Definition: backend-hid.cpp:40
librealsense::platform::iio_hid_sensor
Definition: backend-hid.h:133
librealsense::platform::hid_buf_len
const uint32_t hid_buf_len
Definition: backend-hid.h:15
librealsense::platform::iio_hid_sensor::has_metadata
bool has_metadata()
Definition: backend-hid.cpp:754
librealsense::platform::iio_hid_sensor::_sensitivity_name
std::string _sensitivity_name
Definition: backend-hid.h:183
librealsense::platform::iio_hid_sensor::get_sensor_name
const std::string & get_sensor_name() const
Definition: backend-hid.h:144
dispatcher
Definition: concurrency.h:272
librealsense::platform::hid_input_info::bits_used
uint32_t bits_used
Definition: backend-hid.h:25
LOG_WARNING
#define LOG_WARNING(...)
Definition: easyloggingpp.h:72
librealsense::platform::hid_custom_sensor::start_capture
void start_capture(hid_callback sensor_callback)
Definition: backend-hid.cpp:210
librealsense::platform::iio_hid_sensor::start_capture
void start_capture(hid_callback sensor_callback)
Definition: backend-hid.cpp:489
librealsense::platform::v4l_hid_device::v4l_hid_device
v4l_hid_device(const hid_device_info &info)
Definition: backend-hid.cpp:997
val
GLuint GLfloat * val
Definition: glad/glad/glad.h:3411
librealsense::platform::hid_custom_sensor::hid_custom_sensor
hid_custom_sensor(const std::string &device_path, const std::string &sensor_name)
Definition: backend-hid.cpp:162
librealsense::platform::hid_input::~hid_input
~hid_input()
Definition: backend-hid.cpp:57
librealsense::platform::hid_custom_sensor::~hid_custom_sensor
~hid_custom_sensor()
Definition: backend-hid.cpp:175
uint32_t
unsigned int uint32_t
Definition: stdint.h:80
librealsense::platform::iio_hid_sensor::_fd
int _fd
Definition: backend-hid.h:178
librealsense::platform::hid_input_info
Definition: backend-hid.h:17
librealsense::platform::hid_input_info::input
std::string input
Definition: backend-hid.h:19
value
GLfloat value
Definition: glad/glad/glad.h:2099
uint64_t
unsigned __int64 uint64_t
Definition: stdint.h:90
librealsense::platform::hid_custom_sensor::get_sensor_name
const std::string & get_sensor_name() const
Definition: backend-hid.h:106
realdds::topics::notification::stream_header::key::sensor_name
const std::string sensor_name
librealsense::platform::iio_hid_sensor::read_device_inputs
void read_device_inputs()
Definition: backend-hid.cpp:955
librealsense::platform::v4l_hid_device
Definition: backend-hid.h:193
location
GLint location
Definition: glad/glad/glad.h:2834
librealsense::platform::iio_hid_sensor::clear_buffer
void clear_buffer()
Definition: backend-hid.cpp:671
librealsense::platform::iio_hid_sensor::get_output_size
uint32_t get_output_size() const
Definition: backend-hid.cpp:878
librealsense::platform::hid_custom_sensor
Definition: backend-hid.h:98
librealsense::platform::v4l_hid_device::stop_capture
void stop_capture() override
Definition: backend-hid.cpp:1182
librealsense::platform::v4l_hid_device::foreach_hid_device
static void foreach_hid_device(std::function< void(const hid_device_info &)> action)
Definition: backend-hid.cpp:1215
librealsense::platform::v4l_hid_device::close
void close() override
Definition: backend-hid.cpp:1086
devices.action
action
Definition: third-party/realdds/scripts/devices.py:6
librealsense::platform::hid_custom_sensor::_stop_pipe_fd
int _stop_pipe_fd[2]
Definition: backend-hid.h:122
librealsense::platform::hid_custom_sensor::enable
void enable(bool state)
Definition: backend-hid.cpp:421
librealsense::platform::hid_custom_sensor::_fd
int _fd
Definition: backend-hid.h:121
librealsense::platform::v4l_hid_device::~v4l_hid_device
~v4l_hid_device()
Definition: backend-hid.cpp:1012
backend.h
librealsense::platform::hid_custom_sensor::_custom_device_path
std::string _custom_device_path
Definition: backend-hid.h:124
librealsense::platform::iio_hid_sensor::_hid_thread
std::unique_ptr< std::thread > _hid_thread
Definition: backend-hid.h:188
librealsense::platform::iio_hid_sensor::init
void init(uint32_t frequency, float sensitivity)
Definition: backend-hid.cpp:782
librealsense::platform::iio_hid_sensor::_iio_device_path
std::string _iio_device_path
Definition: backend-hid.h:180
librealsense::platform::iio_hid_sensor::get_sensitivity_name
std::string get_sensitivity_name() const
Definition: backend-hid.cpp:923
librealsense::platform::hid_custom_sensor::get_report_data
std::vector< uint8_t > get_report_data(const std::string &report_name, custom_sensor_report_field report_field)
Definition: backend-hid.cpp:188
fps.info
info
Definition: fps.py:50
librealsense::platform::iio_hid_sensor::_pm_dispatcher
dispatcher _pm_dispatcher
Definition: backend-hid.h:190
librealsense::platform::hid_input
Definition: backend-hid.h:80
librealsense::platform::custom_sensor_report_field
custom_sensor_report_field
Definition: platform/hid-device.h:59
librealsense::platform::hid_input_info::shift
uint32_t shift
Definition: backend-hid.h:29
librealsense::platform::hid_custom_sensor::init
void init()
Definition: backend-hid.cpp:376
librealsense::big_endian
Definition: src/types.h:78
first
GLint first
Definition: glad/glad/glad.h:2301
librealsense::platform::iio_hid_sensor::sort_hids
static bool sort_hids(hid_input *first, hid_input *second)
Definition: backend-hid.cpp:762
librealsense::platform::iio_hid_sensor::set_frequency
void set_frequency(uint32_t frequency)
Definition: backend-hid.cpp:698
state
Definition: rs-measure.cpp:80
librealsense::platform::v4l_hid_device::_streaming_custom_sensors
std::vector< hid_custom_sensor * > _streaming_custom_sensors
Definition: backend-hid.h:228
librealsense::platform::iio_hid_sensor::set_power
void set_power(bool on)
Definition: backend-hid.cpp:728
librealsense::platform::iio_hid_sensor::_is_capturing
std::atomic< bool > _is_capturing
Definition: backend-hid.h:187
librealsense::platform::hid_input_info::mask
uint64_t mask
Definition: backend-hid.h:30
librealsense::platform::v4l_hid_device::start_capture
void start_capture(hid_callback callback) override
Definition: backend-hid.cpp:1116
librealsense::platform::v4l_hid_device::get_custom_report_data
std::vector< uint8_t > get_custom_report_data(const std::string &custom_sensor_name, const std::string &report_name, custom_sensor_report_field report_field) override
Definition: backend-hid.cpp:1199
librealsense::platform::iio_hid_sensor::_sensor_name
std::string _sensor_name
Definition: backend-hid.h:181
librealsense::platform::iio_hid_sensor::~iio_hid_sensor
~iio_hid_sensor()
Definition: backend-hid.cpp:470
librealsense::platform::iio_hid_sensor::set_sensitivity
void set_sensitivity(float sensitivity)
Definition: backend-hid.cpp:712
librealsense::platform::v4l_hid_device::_hid_device_infos
std::vector< hid_device_info > _hid_device_infos
Definition: backend-hid.h:224
librealsense::platform::hid_input_info::device_path
std::string device_path
Definition: backend-hid.h:20
librealsense::platform::v4l_hid_device::get_hid_device_info
static bool get_hid_device_info(const char *dev_path, hid_device_info &device_info)
Definition: backend-hid.cpp:1289
librealsense::platform::hid_input::info
hid_input_info info
Definition: backend-hid.h:95
librealsense::platform::iio_hid_sensor::_callback
hid_callback _callback
Definition: backend-hid.h:186
librealsense::platform::iio_hid_sensor::_pm_thread
std::unique_ptr< std::thread > _pm_thread
Definition: backend-hid.h:189
librealsense::platform::hid_custom_sensor::_custom_sensor_name
std::string _custom_sensor_name
Definition: backend-hid.h:125
librealsense::platform::iio_hid_sensor::get_sampling_frequency_name
std::string get_sampling_frequency_name() const
Definition: backend-hid.cpp:892
librealsense::platform::hid_input::enable
void enable(bool is_enable)
Definition: backend-hid.cpp:70
librealsense::platform::iio_hid_sensor::_stop_pipe_fd
int _stop_pipe_fd[2]
Definition: backend-hid.h:177
librealsense::platform::v4l_hid_device::_streaming_iio_sensors
std::vector< iio_hid_sensor * > _streaming_iio_sensors
Definition: backend-hid.h:227


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:01