Main MRPT website > C++ reference for MRPT 1.5.7
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-2017, 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)
24 {
25  delete m_libraryLoader;
26 }
27 
29 {
30  if (!m_libraryLoader->isLoaded())
31  m_libraryLoader->load("libudev.so");
32 
33  m_uDev.unew = NULL;
34  m_uDev.unref = NULL;
35  m_uDev.device_unref = NULL;
36  m_uDev.enumerate_new = NULL;
40  m_uDev.enumerate_unref = NULL;
48 
49  if (m_libraryLoader->isLoaded())
50  {
51  m_uDev.unew = (uDEV_new*)m_libraryLoader->resolve("udev_new");
52  m_uDev.unref = (uDEV_unref*)m_libraryLoader->resolve("udev_unref");
53  m_uDev.device_unref = (uDEV_device_unref*)m_libraryLoader->resolve("udev_device_unref");
54  m_uDev.enumerate_new = (uDEV_enumerate_new*)m_libraryLoader->resolve("udev_enumerate_new");
55  m_uDev.enumerate_add_match_subsystem = (uDEV_enumerate_add_match_subsystem*)m_libraryLoader->resolve("udev_enumerate_add_match_subsystem");
56  m_uDev.enumerate_scan_devices = (uDEV_enumerate_scan_devices*)m_libraryLoader->resolve("udev_enumerate_scan_devices");
57  m_uDev.enumerate_get_list_entry = (uDEV_enumerate_get_list_entry*)m_libraryLoader->resolve("udev_enumerate_get_list_entry");
58  m_uDev.enumerate_unref = (uDEV_enumerate_unref*)m_libraryLoader->resolve("udev_enumerate_unref");
59  m_uDev.list_entry_get_next = (uDEV_list_entry_get_next*)m_libraryLoader->resolve("udev_list_entry_get_next");
60  m_uDev.list_entry_get_name = (uDEV_list_entry_get_name*)m_libraryLoader->resolve("udev_list_entry_get_name");
61  m_uDev.device_new_from_syspath = (uDEV_device_new_from_syspath*)m_libraryLoader->resolve("udev_device_new_from_syspath");
62  m_uDev.device_get_parent = (uDEV_device_get_parent*)m_libraryLoader->resolve("udev_device_get_parent");
63  m_uDev.device_get_devnode = (uDEV_device_get_devnode*)m_libraryLoader->resolve("udev_device_get_devnode");
64  m_uDev.device_get_parent_with_subsystem_devtype = (uDEV_device_get_parent_with_subsystem_devtype*)m_libraryLoader->resolve("udev_device_get_parent_with_subsystem_devtype");
65  m_uDev.device_get_sysattr_value = (uDEV_device_get_sysattr_value*)m_libraryLoader->resolve("udev_device_get_sysattr_value");
66  }
67 }
68 
69 /*! \brief Create udev library context.
70 
71  This reads the udev configuration file, and fills in the default values.
72 
73  The initial refcount is 1, and needs to be decremented to release the resources of the udev library context.
74 
75  \returns a new udev library context
76 */
77 udev *XsUdev::unew(void)
78 {
79  if (m_uDev.unew)
80  return m_uDev.unew();
81  else
82  return NULL;
83 }
84 
85 /*! \brief Drop a reference of the udev library context.
86 
87  \param udev udev library context
88 
89  If the refcount reaches zero, the resources of the context will be released.
90 */
91 udev *XsUdev::unref(struct udev *udev)
92 {
93  if (m_uDev.unref)
94  return m_uDev.unref(udev);
95  else
96  return NULL;
97 }
98 
99 /*! \brief Drop a reference of a udev device.
100 
101  If the refcount reaches zero, the resources of the device will be released.
102 
103  \param udev_device udev device
104  \return NULL
105 */
106 udev_device *XsUdev::device_unref(struct udev_device *udev_device)
107 {
108  if (m_uDev.device_unref)
109  return m_uDev.device_unref(udev_device);
110  else
111  return NULL;
112 }
113 
114 /*! \brief Create an enumeration context to scan.
115  \param udev udev library context
116 
117  \return an enumeration context.
118 */
119 udev_enumerate *XsUdev::enumerate_new(struct udev *udev)
120 {
121  if (m_uDev.enumerate_new)
122  return m_uDev.enumerate_new(udev);
123  else
124  return NULL;
125 }
126 
127 /*! \brief Match only devices belonging to a certain kernel subsystem.
128  \param udev_enumerate context
129  \param subsystem filter for a subsystem of the device to include in the list
130  \return: 0 on success, otherwise a negative error value.
131 */
132 int XsUdev::enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
133 {
135  return m_uDev.enumerate_add_match_subsystem(udev_enumerate, subsystem);
136  else
137  return -1;
138 }
139 
140 /*! \brief Scan /sys for all devices which match the given filters. No matches will return all currently available devices.
141  \param udev_enumerate udev enumeration context
142  \return 0 on success, otherwise a negative error value.
143 */
144 int XsUdev::enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
145 {
147  return m_uDev.enumerate_scan_devices(udev_enumerate);
148  else
149  return -1;
150 }
151 
152 /*! \brief Get the next entry from the list.
153 
154  \param list_entry current entry
155  \return udev_list_entry, NULL if no more entries are available.
156 */
157 udev_list_entry *XsUdev::list_entry_get_next(struct udev_list_entry *list_entry)
158 {
160  return m_uDev.list_entry_get_next(list_entry);
161  else
162  return NULL;
163 }
164 
165 /*! \brief Get the first entry of the sorted list of device paths.
166  \param udev_enumerate context
167  \return a udev_list_entry.
168 */
169 udev_list_entry *XsUdev::enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
170 {
172  return m_uDev.enumerate_get_list_entry(udev_enumerate);
173  else
174  return NULL;
175 }
176 
177 /*! \brief Drop a reference of an enumeration context.
178 
179  If the refcount reaches zero, all resources of the enumeration context will be released.
180 
181  \param udev_enumerate context
182 
183  \return: NULL
184 */
185 udev_enumerate *XsUdev::enumerate_unref(struct udev_enumerate *udev_enumerate)
186 {
188  return m_uDev.enumerate_unref(udev_enumerate);
189  else
190  return NULL;
191 }
192 
193 /*! \brief Get the name of a list entry.
194  \param list_entry: current entry
195  \return the name string of this entry.
196 */
197 const char *XsUdev::list_entry_get_name(struct udev_list_entry *list_entry)
198 {
200  return m_uDev.list_entry_get_name(list_entry);
201  else
202  return "";
203 }
204 
205 /*! \brief Create new udev device, and fill in information from the sys device and the udev database entry.
206 
207  The syspath is the absolute path to the device, including the sys mount point.
208 
209  \param udev udev library context
210  \param syspath sys device path including sys directory
211  \return a new udev device, or NULL, if it does not exist
212 */
213 udev_device *XsUdev::device_new_from_syspath(struct udev *udev, const char *syspath)
214 {
216  return m_uDev.device_new_from_syspath(udev, syspath);
217  else
218  return NULL;
219 }
220 
221 /*! \brief Find the next parent device, and fill in information from the sys device and the udev database entry.
222 
223  Returned device is not referenced. It is attached to the child device, and will be cleaned up when the child device is cleaned up.
224  It is not necessarily just the upper level directory, empty or not recognized sys directories are ignored.
225 
226  It can be called as many times as needed, without caring about references.
227 
228  \param udev_device: the device to start searching from
229  \return a new udev device, or NULL, if it no parent exist.
230 */
231 udev_device *XsUdev::device_get_parent(struct udev_device *udev_device)
232 {
234  return m_uDev.device_get_parent(udev_device);
235  else
236  return NULL;
237 }
238 
239 /*! \brief Retrieve the device node file name belonging to the udev device.
240 
241  The path is an absolute path, and starts with the device directory.
242 
243  \param udev_device udev device
244  \return the device node file name of the udev device, or NULL if no device node exists
245 */
246 const char *XsUdev::device_get_devnode(struct udev_device *udev_device)
247 {
249  return m_uDev.device_get_devnode(udev_device);
250  else
251  return "";
252 }
253 
254 /*! \brief Find the next parent device, with a matching subsystem and devtypevalue, and fill in information from the sys device and the udev database entry.
255 
256  If devtype is NULL, only subsystem is checked, and any devtype will match.
257 
258  Returned device is not referenced. It is attached to the child device, and will be cleaned up when the child device is cleaned up.
259 
260  It can be called as many times as needed, without caring about references.
261 
262  \param udev_device udev device to start searching from
263  \param subsystem the subsystem of the device
264  \param devtype the type (DEVTYPE) of the device
265  \return a new udev device, or NULL if no matching parent exists.
266 */
267 udev_device *XsUdev::device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
268 {
270  return m_uDev.device_get_parent_with_subsystem_devtype(udev_device, subsystem, devtype);
271  else
272  return NULL;
273 }
274 
275 /*! \brief Get a sys attribute value
276 
277  The retrieved value is cached in the device. Repeated calls will return the same value and not open the attribute again.
278 
279  \param udev_device udev device
280  \param sysattr attribute name
281 
282  \return the content of a sys attribute file, or NULL if there is no sys attribute value.
283 */
284 const char *XsUdev::device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
285 {
287  return m_uDev.device_get_sysattr_value(udev_device, sysattr);
288  else
289  return "";
290 }
291 
292 #endif // patch for MRPT
struct udev_list_entry * uDEV_list_entry_get_next(struct udev_list_entry *list_entry)
Definition: xsudev.h:24
uDEV_device_new_from_syspath * device_new_from_syspath
Definition: xsudev.h:68
uDEV_device_unref * device_unref
Definition: xsudev.h:60
uDEV_enumerate_get_list_entry * enumerate_get_list_entry
Definition: xsudev.h:64
uDEV_list_entry_get_next * list_entry_get_next
Definition: xsudev.h:66
const char * uDEV_list_entry_get_name(struct udev_list_entry *list_entry)
Definition: xsudev.h:25
struct XsLibraryLoader XsLibraryLoader
const char * uDEV_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
Definition: xsudev.h:30
uDEV_enumerate_add_match_subsystem enumerate_add_match_subsystem
Definition: xsudev.h:42
uDEV_unref * unref
Definition: xsudev.h:59
struct udev_device * uDEV_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
Definition: xsudev.h:29
uDEV_device_unref device_unref
Definition: xsudev.h:40
uDEV_enumerate_get_list_entry enumerate_get_list_entry
Definition: xsudev.h:44
uDEV_device_get_parent device_get_parent
Definition: xsudev.h:49
struct udev_device * uDEV_device_new_from_syspath(struct udev *udev, const char *syspath)
Definition: xsudev.h:26
XsLibraryLoader * m_libraryLoader
Definition: xsudev.h:76
int uDEV_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
Definition: xsudev.h:20
struct udev_device * uDEV_device_get_parent(struct udev_device *udev_device)
Definition: xsudev.h:27
uDEV_unref unref
Definition: xsudev.h:39
uDEV_new * unew
Definition: xsudev.h:58
uDEV_list_entry_get_name * list_entry_get_name
Definition: xsudev.h:67
uDEV_device_get_parent_with_subsystem_devtype device_get_parent_with_subsystem_devtype
Definition: xsudev.h:51
uDEV_device_get_sysattr_value device_get_sysattr_value
Definition: xsudev.h:52
uDEV_list_entry_get_next list_entry_get_next
Definition: xsudev.h:46
uDEV_enumerate_scan_devices * enumerate_scan_devices
Definition: xsudev.h:63
struct udev_enumerate * uDEV_enumerate_new(struct udev *udev)
Definition: xsudev.h:19
const char * uDEV_device_get_devnode(struct udev_device *udev_device)
Definition: xsudev.h:28
struct udev_list_entry * uDEV_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
Definition: xsudev.h:22
struct udev_device * uDEV_device_unref(struct udev_device *udev_device)
Definition: xsudev.h:18
UDEV_API m_uDev
Definition: xsudev.h:75
int uDEV_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
Definition: xsudev.h:21
uDEV_device_get_parent_with_subsystem_devtype * device_get_parent_with_subsystem_devtype
Definition: xsudev.h:71
uDEV_device_get_sysattr_value * device_get_sysattr_value
Definition: xsudev.h:72
uDEV_enumerate_new enumerate_new
Definition: xsudev.h:41
uDEV_enumerate_unref * enumerate_unref
Definition: xsudev.h:65
uDEV_device_get_parent * device_get_parent
Definition: xsudev.h:69
void initLibrary()
struct udev_enumerate * uDEV_enumerate_unref(struct udev_enumerate *udev_enumerate)
Definition: xsudev.h:23
XsUdev(void)
struct udev * uDEV_new(void)
Definition: xsudev.h:16
uDEV_device_get_devnode device_get_devnode
Definition: xsudev.h:50
uDEV_device_get_devnode * device_get_devnode
Definition: xsudev.h:70
uDEV_enumerate_add_match_subsystem * enumerate_add_match_subsystem
Definition: xsudev.h:62
uDEV_device_new_from_syspath device_new_from_syspath
Definition: xsudev.h:48
uDEV_enumerate_unref enumerate_unref
Definition: xsudev.h:45
uDEV_enumerate_new * enumerate_new
Definition: xsudev.h:61
uDEV_enumerate_scan_devices enumerate_scan_devices
Definition: xsudev.h:43
uDEV_list_entry_get_name list_entry_get_name
Definition: xsudev.h:47
~XsUdev(void)
struct udev * uDEV_unref(struct udev *udev)
Definition: xsudev.h:17
uDEV_new unew
Definition: xsudev.h:38



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019