Main MRPT website > C++ reference for MRPT 1.9.9
xsudev.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef _WIN32 // patch for MRPT
10 
11 #include "xsudev.h"
12 #include <xsens/xslibraryloader.h>
13 
14 /*! \class XsUdev
15  \brief Class for dynamic loading of winusb
16 */
17 XsUdev::XsUdev(void)
18 {
20  initLibrary();
21 }
22 
23 XsUdev::~XsUdev(void) { delete m_libraryLoader; }
25 {
26  if (!m_libraryLoader->isLoaded()) m_libraryLoader->load("libudev.so");
27 
28  m_uDev.unew = nullptr;
29  m_uDev.unref = nullptr;
30  m_uDev.device_unref = nullptr;
31  m_uDev.enumerate_new = nullptr;
35  m_uDev.enumerate_unref = nullptr;
36  m_uDev.list_entry_get_next = nullptr;
37  m_uDev.list_entry_get_name = nullptr;
39  m_uDev.device_get_parent = nullptr;
40  m_uDev.device_get_devnode = nullptr;
43 
44  if (m_libraryLoader->isLoaded())
45  {
46  m_uDev.unew = (uDEV_new*)m_libraryLoader->resolve("udev_new");
47  m_uDev.unref = (uDEV_unref*)m_libraryLoader->resolve("udev_unref");
49  (uDEV_device_unref*)m_libraryLoader->resolve("udev_device_unref");
51  (uDEV_enumerate_new*)m_libraryLoader->resolve("udev_enumerate_new");
54  "udev_enumerate_add_match_subsystem");
57  "udev_enumerate_scan_devices");
60  "udev_enumerate_get_list_entry");
63  "udev_enumerate_unref");
66  "udev_list_entry_get_next");
69  "udev_list_entry_get_name");
72  "udev_device_new_from_syspath");
75  "udev_device_get_parent");
78  "udev_device_get_devnode");
81  ->resolve("udev_device_get_parent_with_subsystem_devtype");
84  "udev_device_get_sysattr_value");
85  }
86 }
87 
88 /*! \brief Create udev library context.
89 
90  This reads the udev configuration file, and fills in the default values.
91 
92  The initial refcount is 1, and needs to be decremented to release the
93  resources of the udev library context.
94 
95  \returns a new udev library context
96 */
97 udev* XsUdev::unew(void)
98 {
99  if (m_uDev.unew)
100  return m_uDev.unew();
101  else
102  return nullptr;
103 }
104 
105 /*! \brief Drop a reference of the udev library context.
106 
107  \param udev udev library context
108 
109  If the refcount reaches zero, the resources of the context will be released.
110 */
111 udev* XsUdev::unref(struct udev* udev)
112 {
113  if (m_uDev.unref)
114  return m_uDev.unref(udev);
115  else
116  return nullptr;
117 }
118 
119 /*! \brief Drop a reference of a udev device.
120 
121  If the refcount reaches zero, the resources of the device will be released.
122 
123  \param udev_device udev device
124  \return NULL
125 */
126 udev_device* XsUdev::device_unref(struct udev_device* udev_device)
127 {
128  if (m_uDev.device_unref)
129  return m_uDev.device_unref(udev_device);
130  else
131  return nullptr;
132 }
133 
134 /*! \brief Create an enumeration context to scan.
135  \param udev udev library context
136 
137  \return an enumeration context.
138 */
139 udev_enumerate* XsUdev::enumerate_new(struct udev* udev)
140 {
141  if (m_uDev.enumerate_new)
142  return m_uDev.enumerate_new(udev);
143  else
144  return nullptr;
145 }
146 
147 /*! \brief Match only devices belonging to a certain kernel subsystem.
148  \param udev_enumerate context
149  \param subsystem filter for a subsystem of the device to include in the
150  list
151  \return: 0 on success, otherwise a negative error value.
152 */
154  struct udev_enumerate* udev_enumerate, const char* subsystem)
155 {
157  return m_uDev.enumerate_add_match_subsystem(udev_enumerate, subsystem);
158  else
159  return -1;
160 }
161 
162 /*! \brief Scan /sys for all devices which match the given filters. No matches
163  will return all currently available devices.
164  \param udev_enumerate udev enumeration context
165  \return 0 on success, otherwise a negative error value.
166 */
167 int XsUdev::enumerate_scan_devices(struct udev_enumerate* udev_enumerate)
168 {
170  return m_uDev.enumerate_scan_devices(udev_enumerate);
171  else
172  return -1;
173 }
174 
175 /*! \brief Get the next entry from the list.
176 
177  \param list_entry current entry
178  \return udev_list_entry, nullptr if no more entries are available.
179 */
180 udev_list_entry* XsUdev::list_entry_get_next(struct udev_list_entry* list_entry)
181 {
183  return m_uDev.list_entry_get_next(list_entry);
184  else
185  return nullptr;
186 }
187 
188 /*! \brief Get the first entry of the sorted list of device paths.
189  \param udev_enumerate context
190  \return a udev_list_entry.
191 */
192 udev_list_entry* XsUdev::enumerate_get_list_entry(
193  struct udev_enumerate* udev_enumerate)
194 {
196  return m_uDev.enumerate_get_list_entry(udev_enumerate);
197  else
198  return nullptr;
199 }
200 
201 /*! \brief Drop a reference of an enumeration context.
202 
203  If the refcount reaches zero, all resources of the enumeration context will
204  be released.
205 
206  \param udev_enumerate context
207 
208  \return: NULL
209 */
210 udev_enumerate* XsUdev::enumerate_unref(struct udev_enumerate* udev_enumerate)
211 {
213  return m_uDev.enumerate_unref(udev_enumerate);
214  else
215  return nullptr;
216 }
217 
218 /*! \brief Get the name of a list entry.
219  \param list_entry: current entry
220  \return the name string of this entry.
221 */
222 const char* XsUdev::list_entry_get_name(struct udev_list_entry* list_entry)
223 {
225  return m_uDev.list_entry_get_name(list_entry);
226  else
227  return "";
228 }
229 
230 /*! \brief Create new udev device, and fill in information from the sys device
231  and the udev database entry.
232 
233  The syspath is the absolute path to the device, including the sys mount
234  point.
235 
236  \param udev udev library context
237  \param syspath sys device path including sys directory
238  \return a new udev device, or nullptr, if it does not exist
239 */
241  struct udev* udev, const char* syspath)
242 {
244  return m_uDev.device_new_from_syspath(udev, syspath);
245  else
246  return nullptr;
247 }
248 
249 /*! \brief Find the next parent device, and fill in information from the sys
250  device and the udev database entry.
251 
252  Returned device is not referenced. It is attached to the child device, and
253  will be cleaned up when the child device is cleaned up.
254  It is not necessarily just the upper level directory, empty or not
255  recognized sys directories are ignored.
256 
257  It can be called as many times as needed, without caring about references.
258 
259  \param udev_device: the device to start searching from
260  \return a new udev device, or nullptr, if it no parent exist.
261 */
262 udev_device* XsUdev::device_get_parent(struct udev_device* udev_device)
263 {
265  return m_uDev.device_get_parent(udev_device);
266  else
267  return nullptr;
268 }
269 
270 /*! \brief Retrieve the device node file name belonging to the udev device.
271 
272  The path is an absolute path, and starts with the device directory.
273 
274  \param udev_device udev device
275  \return the device node file name of the udev device, or nullptr if no
276  device node exists
277 */
278 const char* XsUdev::device_get_devnode(struct udev_device* udev_device)
279 {
281  return m_uDev.device_get_devnode(udev_device);
282  else
283  return "";
284 }
285 
286 /*! \brief Find the next parent device, with a matching subsystem and
287  devtypevalue, and fill in information from the sys device and the udev
288  database entry.
289 
290  If devtype is nullptr, only subsystem is checked, and any devtype will
291  match.
292 
293  Returned device is not referenced. It is attached to the child device, and
294  will be cleaned up when the child device is cleaned up.
295 
296  It can be called as many times as needed, without caring about references.
297 
298  \param udev_device udev device to start searching from
299  \param subsystem the subsystem of the device
300  \param devtype the type (DEVTYPE) of the device
301  \return a new udev device, or nullptr if no matching parent exists.
302 */
304  struct udev_device* udev_device, const char* subsystem, const char* devtype)
305 {
308  udev_device, subsystem, devtype);
309  else
310  return nullptr;
311 }
312 
313 /*! \brief Get a sys attribute value
314 
315  The retrieved value is cached in the device. Repeated calls will return the
316  same value and not open the attribute again.
317 
318  \param udev_device udev device
319  \param sysattr attribute name
320 
321  \return the content of a sys attribute file, or nullptr if there is no sys
322  attribute value.
323 */
325  struct udev_device* udev_device, const char* sysattr)
326 {
328  return m_uDev.device_get_sysattr_value(udev_device, sysattr);
329  else
330  return "";
331 }
332 
333 #endif // patch for MRPT
uDEV_device_get_parent_with_subsystem_devtype
struct udev_device * uDEV_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
Definition: xsudev.h:36
uDEV_device_get_devnode
const typedef char * uDEV_device_get_devnode(struct udev_device *udev_device)
XsUdev::~XsUdev
~XsUdev(void)
uDEV_new
struct udev * uDEV_new(void)
Definition: xsudev.h:16
uDEV_enumerate_unref
struct udev_enumerate * uDEV_enumerate_unref(struct udev_enumerate *udev_enumerate)
Definition: xsudev.h:25
XsUdev::device_get_parent
uDEV_device_get_parent device_get_parent
Definition: xsudev.h:59
uDEV_enumerate_scan_devices
int uDEV_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
Definition: xsudev.h:22
XsUdev::unew
uDEV_new unew
Definition: xsudev.h:48
XsUdev::device_unref
uDEV_device_unref device_unref
Definition: xsudev.h:50
uDEV_device_new_from_syspath
struct udev_device * uDEV_device_new_from_syspath(struct udev *udev, const char *syspath)
Definition: xsudev.h:31
XsUdev::enumerate_unref
uDEV_enumerate_unref enumerate_unref
Definition: xsudev.h:55
XsUdev::_UDEV_API::list_entry_get_name
uDEV_list_entry_get_name * list_entry_get_name
Definition: xsudev.h:77
xslibraryloader.h
uDEV_device_get_sysattr_value
const typedef char * uDEV_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
XsUdev::_UDEV_API::enumerate_add_match_subsystem
uDEV_enumerate_add_match_subsystem * enumerate_add_match_subsystem
Definition: xsudev.h:72
XsUdev::enumerate_add_match_subsystem
uDEV_enumerate_add_match_subsystem enumerate_add_match_subsystem
Definition: xsudev.h:52
XsUdev::unref
uDEV_unref unref
Definition: xsudev.h:49
XsUdev::device_get_parent_with_subsystem_devtype
uDEV_device_get_parent_with_subsystem_devtype device_get_parent_with_subsystem_devtype
Definition: xsudev.h:62
XsUdev::_UDEV_API::list_entry_get_next
uDEV_list_entry_get_next * list_entry_get_next
Definition: xsudev.h:76
XsUdev::enumerate_new
uDEV_enumerate_new enumerate_new
Definition: xsudev.h:51
XsUdev::enumerate_get_list_entry
uDEV_enumerate_get_list_entry enumerate_get_list_entry
Definition: xsudev.h:54
XsUdev::_UDEV_API::enumerate_get_list_entry
uDEV_enumerate_get_list_entry * enumerate_get_list_entry
Definition: xsudev.h:74
uDEV_list_entry_get_next
struct udev_list_entry * uDEV_list_entry_get_next(struct udev_list_entry *list_entry)
Definition: xsudev.h:27
xsudev.h
uDEV_unref
struct udev * uDEV_unref(struct udev *udev)
Definition: xsudev.h:17
XsUdev::_UDEV_API::device_get_parent
uDEV_device_get_parent * device_get_parent
Definition: xsudev.h:79
XsUdev::list_entry_get_next
uDEV_list_entry_get_next list_entry_get_next
Definition: xsudev.h:56
uDEV_list_entry_get_name
const typedef char * uDEV_list_entry_get_name(struct udev_list_entry *list_entry)
XsUdev::_UDEV_API::device_new_from_syspath
uDEV_device_new_from_syspath * device_new_from_syspath
Definition: xsudev.h:78
XsUdev::m_libraryLoader
XsLibraryLoader * m_libraryLoader
Definition: xsudev.h:87
XsUdev::device_get_sysattr_value
uDEV_device_get_sysattr_value device_get_sysattr_value
Definition: xsudev.h:63
XsUdev::device_get_devnode
uDEV_device_get_devnode device_get_devnode
Definition: xsudev.h:60
XsUdev::_UDEV_API::device_get_sysattr_value
uDEV_device_get_sysattr_value * device_get_sysattr_value
Definition: xsudev.h:83
XsUdev::_UDEV_API::enumerate_unref
uDEV_enumerate_unref * enumerate_unref
Definition: xsudev.h:75
XsLibraryLoader
struct XsLibraryLoader XsLibraryLoader
Definition: xslibraryloader.h:24
XsUdev::XsUdev
XsUdev(void)
XsUdev::initLibrary
void initLibrary()
XsUdev::m_uDev
UDEV_API m_uDev
Definition: xsudev.h:86
XsUdev::_UDEV_API::enumerate_scan_devices
uDEV_enumerate_scan_devices * enumerate_scan_devices
Definition: xsudev.h:73
XsUdev::_UDEV_API::unref
uDEV_unref * unref
Definition: xsudev.h:69
uDEV_enumerate_new
struct udev_enumerate * uDEV_enumerate_new(struct udev *udev)
Definition: xsudev.h:19
uDEV_enumerate_get_list_entry
struct udev_list_entry * uDEV_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
Definition: xsudev.h:23
XsUdev::_UDEV_API::device_get_parent_with_subsystem_devtype
uDEV_device_get_parent_with_subsystem_devtype * device_get_parent_with_subsystem_devtype
Definition: xsudev.h:81
XsUdev::enumerate_scan_devices
uDEV_enumerate_scan_devices enumerate_scan_devices
Definition: xsudev.h:53
XsUdev::_UDEV_API::device_unref
uDEV_device_unref * device_unref
Definition: xsudev.h:70
uDEV_device_get_parent
struct udev_device * uDEV_device_get_parent(struct udev_device *udev_device)
Definition: xsudev.h:33
XsUdev::_UDEV_API::device_get_devnode
uDEV_device_get_devnode * device_get_devnode
Definition: xsudev.h:80
XsUdev::_UDEV_API::enumerate_new
uDEV_enumerate_new * enumerate_new
Definition: xsudev.h:71
XsUdev::_UDEV_API::unew
uDEV_new * unew
Definition: xsudev.h:68
uDEV_enumerate_add_match_subsystem
int uDEV_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
Definition: xsudev.h:20
uDEV_device_unref
struct udev_device * uDEV_device_unref(struct udev_device *udev_device)
Definition: xsudev.h:18
XsUdev::list_entry_get_name
uDEV_list_entry_get_name list_entry_get_name
Definition: xsudev.h:57
XsUdev::device_new_from_syspath
uDEV_device_new_from_syspath device_new_from_syspath
Definition: xsudev.h:58



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST