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 
106  const std::string& get_sensor_name() const { return _custom_sensor_name; }
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);
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_power(bool on);
151 
152  void signal_stop();
153 
154  bool has_metadata();
155 
156  static bool sort_hids(hid_input* first, hid_input* second);
157 
158  void create_channel_array();
159 
160  // initialize the device sensor. reading its name and all of its inputs.
161  void init(uint32_t frequency);
162 
163  // calculate the storage size of a scan
164  uint32_t get_channel_size() const;
165 
166  // calculate the actual size of data
167  uint32_t get_output_size() const;
168 
169  std::string get_sampling_frequency_name() const;
170 
171  // read the IIO device inputs.
172  void read_device_inputs();
173 
174  int _stop_pipe_fd[2]; // write to _stop_pipe_fd[1] and read from _stop_pipe_fd[0]
175  int _fd;
180  std::list<hid_input*> _inputs;
181  std::list<hid_input*> _channels;
183  std::atomic<bool> _is_capturing;
184  std::unique_ptr<std::thread> _hid_thread;
185  std::unique_ptr<std::thread> _pm_thread; // Delayed initialization due to power-up sequence
186  dispatcher _pm_dispatcher; // Asynchronous power management
187  };
188 
189  class v4l_hid_device : public hid_device
190  {
191  public:
193 
194  ~v4l_hid_device();
195 
196  void register_profiles(const std::vector<hid_profile>& hid_profiles) override { _hid_profiles = hid_profiles;}
197 
198  void open(const std::vector<hid_profile>& hid_profiles) override;
199 
200  void close() override;
201 
202  std::vector<hid_sensor> get_sensors() override;
203 
204  void start_capture(hid_callback callback) override;
205 
206  void stop_capture() override;
207 
208  std::vector<uint8_t> get_custom_report_data(const std::string& custom_sensor_name,
209  const std::string& report_name,
210  custom_sensor_report_field report_field) override;
211 
212  static void foreach_hid_device(std::function<void(const hid_device_info&)> action);
213 
214  private:
215  static bool get_hid_device_info(const char* dev_path, hid_device_info& device_info);
216 
217  std::vector<hid_profile> _hid_profiles;
218  std::vector<hid_device_info> _hid_device_infos;
219  std::vector<std::unique_ptr<iio_hid_sensor>> _iio_hid_sensors;
220  std::vector<std::unique_ptr<hid_custom_sensor>> _hid_custom_sensors;
221  std::vector<iio_hid_sensor*> _streaming_iio_sensors;
222  std::vector<hid_custom_sensor*> _streaming_custom_sensors;
223  static constexpr const char* custom_id{"custom"};
224  };
225  }
226 }
std::vector< hid_device_info > _hid_device_infos
Definition: backend-hid.h:218
#define LOG_WARNING(...)
Definition: src/types.h:241
std::unique_ptr< std::thread > _pm_thread
Definition: backend-hid.h:185
std::map< std::string, std::string > _reports
Definition: backend-hid.h:123
GLint location
GLsizei const GLchar *const * path
Definition: glext.h:4276
GLfloat value
std::vector< iio_hid_sensor * > _streaming_iio_sensors
Definition: backend-hid.h:221
std::list< hid_input * > _channels
Definition: backend-hid.h:181
std::unique_ptr< std::thread > _hid_thread
Definition: backend-hid.h:184
GLsizei const GLchar *const * string
GLuint index
GLuint GLfloat * val
def info(name, value, persistent=False)
Definition: test.py:301
std::vector< std::unique_ptr< iio_hid_sensor > > _iio_hid_sensors
Definition: backend-hid.h:219
unsigned int uint32_t
Definition: stdint.h:80
def callback(frame)
Definition: t265_stereo.py:91
unsigned __int64 uint64_t
Definition: stdint.h:90
GLenum GLenum GLsizei const GLuint GLboolean enabled
void init(void)
Definition: boing.c:180
GLint first
action
Definition: enums.py:62
void register_profiles(const std::vector< hid_profile > &hid_profiles) override
Definition: backend-hid.h:196
std::unique_ptr< std::thread > _hid_thread
Definition: backend-hid.h:129
std::list< hid_input * > _inputs
Definition: backend-hid.h:180
const hid_input_info & get_hid_input_info() const
Definition: backend-hid.h:89
bool write_fs_attribute(const std::string &path, const T &val)
Definition: backend-hid.h:39
const uint32_t hid_buf_len
Definition: backend-hid.h:15
std::vector< hid_profile > _hid_profiles
Definition: backend-hid.h:217
GLboolean enable
Definition: glext.h:5688
GLenum GLenum GLenum input
Definition: glext.h:10805
const std::string & get_sensor_name() const
Definition: backend-hid.h:106
std::vector< std::unique_ptr< hid_custom_sensor > > _hid_custom_sensors
Definition: backend-hid.h:220
std::vector< hid_custom_sensor * > _streaming_custom_sensors
Definition: backend-hid.h:222
GLuint res
Definition: glext.h:8856
std::function< void(const sensor_data &)> hid_callback
Definition: backend.h:327
const std::string & get_sensor_name() const
Definition: backend-hid.h:144


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:45:06