Monado OpenXR Runtime
p_prober.h
Go to the documentation of this file.
1 // Copyright 2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Main prober code.
6  * @author Jakob Bornecrantz <jakob@collabora.com>
7  * @ingroup st_prober
8  */
9 
10 #pragma once
11 
12 #include "xrt/xrt_config_have.h"
13 #include "xrt/xrt_config_os.h"
14 #include "xrt/xrt_compiler.h"
15 #include "xrt/xrt_prober.h"
16 
17 #ifdef XRT_HAVE_LIBUSB
18 #include <libusb-1.0/libusb.h>
19 #endif
20 
21 #ifdef XRT_HAVE_LIBUVC
22 #include <libuvc/libuvc.h>
23 #endif
24 
25 
26 /*
27  *
28  * Struct and defines
29  *
30  */
31 
32 #define P_SPEW(p, ...) \
33  do { \
34  if (p->print_spew) { \
35  fprintf(stderr, "%s - ", __func__); \
36  fprintf(stderr, __VA_ARGS__); \
37  fprintf(stderr, "\n"); \
38  } \
39  } while (false)
40 
41 #define P_DEBUG(p, ...) \
42  do { \
43  if (p->print_debug) { \
44  fprintf(stderr, "%s - ", __func__); \
45  fprintf(stderr, __VA_ARGS__); \
46  fprintf(stderr, "\n"); \
47  } \
48  } while (false)
49 
50 #define P_ERROR(p, ...) \
51  do { \
52  fprintf(stderr, "%s - ", __func__); \
53  fprintf(stderr, __VA_ARGS__); \
54  fprintf(stderr, "\n"); \
55  } while (false)
56 
57 #define MAX_AUTO_PROBERS 8
58 
59 
60 #ifdef XRT_OS_LINUX
61 /*!
62  * A hidraw interface that a @ref prober_device exposes.
63  */
64 struct prober_hidraw
65 {
66  ssize_t interface;
67  const char *path;
68 };
69 
70 /*!
71  * A v4l interface that a @ref prober_device exposes.
72  */
73 struct prober_v4l
74 {
75  const char *path;
76  int32_t usb_iface;
77  uint32_t v4l_index;
78 };
79 #endif
80 
81 /*!
82  * A prober device.
83  */
85 {
86  struct xrt_prober_device base;
87 
88  struct
89  {
90  uint16_t bus;
91  uint16_t addr;
92 
93 #ifdef XRT_OS_LINUX
94  const char *product;
95  const char *path;
96 #endif
97 
98  uint8_t ports[8];
99  uint32_t num_ports;
100 
101 #ifdef XRT_HAVE_LIBUSB
102  libusb_device *dev;
103 #endif
104  } usb;
105 
106  struct
107  {
108  uint64_t id;
109  } bluetooth;
110 
111 #ifdef XRT_HAVE_LIBUVC
112  struct
113  {
114  uvc_device_t *dev;
115  } uvc;
116 #endif
117 
118 #ifdef XRT_OS_LINUX
119  size_t num_v4ls;
120  struct prober_v4l *v4ls;
121 
122  size_t num_hidraws;
123  struct prober_hidraw *hidraws;
124 #endif
125 };
126 
127 struct prober
128 {
129  struct xrt_prober base;
130 
131  struct xrt_prober_entry_lists *lists;
132 
133 #ifdef XRT_HAVE_LIBUSB
134  struct
135  {
136  libusb_context *ctx;
137  libusb_device **list;
138  ssize_t count;
139  } usb;
140 #endif
141 
142 #ifdef XRT_HAVE_LIBUVC
143  struct
144  {
145  uvc_context_t *ctx;
146  uvc_device_t **list;
147  ssize_t count;
148  } uvc;
149 #endif
150 
151  struct xrt_auto_prober *auto_probers[MAX_AUTO_PROBERS];
152 
153  size_t num_devices;
154  struct prober_device *devices;
155 
156  size_t num_entries;
157  struct xrt_prober_entry **entries;
158 
159  bool print_debug;
160  bool print_spew;
161 };
162 
163 
164 /*
165  *
166  * Functions.
167  *
168  */
169 
170 /*!
171  * Dump the given device to stdout.
172  */
173 void
174 p_dump_device(struct prober *p, struct prober_device *pdev, int id);
175 
176 /*!
177  * Get or create a @ref prober_device from the device.
178  */
179 int
180 p_dev_get_usb_dev(struct prober *p,
181  uint16_t bus,
182  uint16_t addr,
183  uint16_t vendor_id,
184  uint16_t product_id,
185  struct prober_device **out_pdev);
186 
187 /*!
188  * Get or create a @ref prober_device from the device.
189  */
190 int
192  uint64_t id,
193  uint16_t vendor_id,
194  uint16_t product_id,
195  struct prober_device **out_pdev);
196 
197 /*!
198  * Init the tracking factory.
199  */
200 int
201 p_tracking_init(struct prober *p);
202 
203 /*!
204  * Teardown the tracking factory.
205  */
206 void
207 p_tracking_teardown(struct prober *p);
208 
209 #ifdef XRT_HAVE_LIBUSB
210 int
211 p_libusb_init(struct prober *p);
212 
213 void
214 p_libusb_teardown(struct prober *p);
215 
216 int
217 p_libusb_probe(struct prober *p);
218 
219 int
220 p_libusb_get_string_descriptor(struct prober *p,
221  struct prober_device *pdev,
222  enum xrt_prober_string which_string,
223  unsigned char *buffer,
224  int length);
225 
226 bool
227 p_libusb_can_open(struct prober *p, struct prober_device *pdev);
228 #endif
229 
230 #ifdef XRT_HAVE_LIBUVC
231 int
232 p_libuvc_init(struct prober *p);
233 
234 void
235 p_libuvc_teardown(struct prober *p);
236 
237 int
238 p_libuvc_probe(struct prober *p);
239 #endif
240 
241 #ifdef XRT_HAVE_LIBUDEV
242 int
243 p_udev_probe(struct prober *p);
244 #endif
int p_tracking_init(struct prober *p)
Init the tracking factory.
Definition: p_tracking.c:236
Auto detect OS and certain features.
xrt_prober_string
String descriptor types.
Definition: xrt_prober.h:103
Common interface to probe for devices.
The main prober that probes and manages found but not opened HMD devices that are connected to the sy...
Definition: xrt_prober.h:147
void p_dump_device(struct prober *p, struct prober_device *pdev, int id)
Dump the given device to stdout.
Definition: p_dump.c:66
Main root of all of the probing device.
Definition: xrt_prober.h:71
A simple prober to probe for a HMD device connected to the system.
Definition: xrt_prober.h:333
void p_tracking_teardown(struct prober *p)
Teardown the tracking factory.
Definition: p_tracking.c:261
Entry for a single device.
Definition: xrt_prober.h:45
A prober device.
Definition: p_prober.h:84
int p_dev_get_usb_dev(struct prober *p, uint16_t bus, uint16_t addr, uint16_t vendor_id, uint16_t product_id, struct prober_device **out_pdev)
Get or create a prober_device from the device.
Definition: p_prober.c:150
Definition: p_prober.h:127
int p_dev_get_bluetooth_dev(struct prober *p, uint64_t id, uint16_t vendor_id, uint16_t product_id, struct prober_device **out_pdev)
Get or create a prober_device from the device.
Definition: p_prober.c:195
Header holding common defines.
A probed device, may or may not be opened.
Definition: xrt_prober.h:121