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 #include "xrt/xrt_settings.h"
17 
18 #ifdef XRT_HAVE_LIBUSB
19 #include <libusb-1.0/libusb.h>
20 #endif
21 
22 #ifdef XRT_HAVE_LIBUVC
23 #include <libuvc/libuvc.h>
24 #endif
25 
26 
27 /*
28  *
29  * Struct and defines
30  *
31  */
32 
33 #define P_SPEW(p, ...) \
34  do { \
35  if (p->print_spew) { \
36  fprintf(stderr, "%s - ", __func__); \
37  fprintf(stderr, __VA_ARGS__); \
38  fprintf(stderr, "\n"); \
39  } \
40  } while (false)
41 
42 #define P_DEBUG(p, ...) \
43  do { \
44  if (p->print_debug) { \
45  fprintf(stderr, "%s - ", __func__); \
46  fprintf(stderr, __VA_ARGS__); \
47  fprintf(stderr, "\n"); \
48  } \
49  } while (false)
50 
51 #define P_ERROR(p, ...) \
52  do { \
53  fprintf(stderr, "%s - ", __func__); \
54  fprintf(stderr, __VA_ARGS__); \
55  fprintf(stderr, "\n"); \
56  } while (false)
57 
58 #define MAX_AUTO_PROBERS 8
59 
60 
61 #ifdef XRT_OS_LINUX
62 /*!
63  * A hidraw interface that a @ref prober_device exposes.
64  */
65 struct prober_hidraw
66 {
67  ssize_t interface;
68  const char *path;
69 };
70 
71 /*!
72  * A v4l interface that a @ref prober_device exposes.
73  */
74 struct prober_v4l
75 {
76  const char *path;
77  int32_t usb_iface;
78  uint32_t v4l_index;
79 };
80 #endif
81 
82 /*!
83  * A prober device.
84  */
86 {
87  struct xrt_prober_device base;
88 
89  struct
90  {
91  uint16_t bus;
92  uint16_t addr;
93 
94 #ifdef XRT_OS_LINUX
95  const char *product;
96  const char *manufacturer;
97  const char *serial;
98  const char *path;
99 #endif
100 
101  uint8_t ports[8];
102  uint32_t num_ports;
103 
104 #ifdef XRT_HAVE_LIBUSB
105  libusb_device *dev;
106 #endif
107  } usb;
108 
109  struct
110  {
111  uint64_t id;
112  } bluetooth;
113 
114 #ifdef XRT_HAVE_LIBUVC
115  struct
116  {
117  uvc_device_t *dev;
118  } uvc;
119 #endif
120 
121 #ifdef XRT_OS_LINUX
122  size_t num_v4ls;
123  struct prober_v4l *v4ls;
124 
125  size_t num_hidraws;
126  struct prober_hidraw *hidraws;
127 #endif
128 };
129 
130 struct prober
131 {
132  struct xrt_prober base;
133 
135 
136  struct
137  {
139  } json;
140 
141 #ifdef XRT_HAVE_LIBUSB
142  struct
143  {
144  libusb_context *ctx;
145  libusb_device **list;
146  ssize_t count;
147  } usb;
148 #endif
149 
150 #ifdef XRT_HAVE_LIBUVC
151  struct
152  {
153  uvc_context_t *ctx;
154  uvc_device_t **list;
155  ssize_t count;
156  } uvc;
157 #endif
158 
159  struct xrt_auto_prober *auto_probers[MAX_AUTO_PROBERS];
160 
161  size_t num_devices;
163 
164  size_t num_entries;
166 
169 };
170 
171 
172 /*
173  *
174  * Functions.
175  *
176  */
177 
178 /*!
179  * Load the JSON config file.
180  */
181 cJSON *
183 
184 /*!
185  * Extract tracking settings from the JSON.
186  */
187 bool
189 
190 /*!
191  * Dump the given device to stdout.
192  */
193 void
194 p_dump_device(struct prober *p, struct prober_device *pdev, int id);
195 
196 /*!
197  * Get or create a @ref prober_device from the device.
198  */
199 int
200 p_dev_get_usb_dev(struct prober *p,
201  uint16_t bus,
202  uint16_t addr,
203  uint16_t vendor_id,
204  uint16_t product_id,
205  struct prober_device **out_pdev);
206 
207 /*!
208  * Get or create a @ref prober_device from the device.
209  */
210 int
212  uint64_t id,
213  uint16_t vendor_id,
214  uint16_t product_id,
215  struct prober_device **out_pdev);
216 
217 /*!
218  * Init the tracking factory.
219  */
220 int
221 p_tracking_init(struct prober *p);
222 
223 /*!
224  * Teardown the tracking factory.
225  */
226 void
227 p_tracking_teardown(struct prober *p);
228 
229 #ifdef XRT_HAVE_LIBUSB
230 int
231 p_libusb_init(struct prober *p);
232 
233 void
234 p_libusb_teardown(struct prober *p);
235 
236 int
237 p_libusb_probe(struct prober *p);
238 
239 int
241  struct prober_device *pdev,
242  enum xrt_prober_string which_string,
243  unsigned char *buffer,
244  int length);
245 
246 bool
247 p_libusb_can_open(struct prober *p, struct prober_device *pdev);
248 #endif
249 
250 #ifdef XRT_HAVE_LIBUVC
251 int
252 p_libuvc_init(struct prober *p);
253 
254 void
255 p_libuvc_teardown(struct prober *p);
256 
257 int
258 p_libuvc_probe(struct prober *p);
259 #endif
260 
261 #ifdef XRT_HAVE_LIBUDEV
262 int
263 p_udev_probe(struct prober *p);
264 #endif
int p_tracking_init(struct prober *p)
Init the tracking factory.
Definition: p_tracking.c:288
Auto detect OS and certain features.
size_t num_devices
Definition: p_prober.h:161
bool print_spew
Definition: p_prober.h:168
bool print_debug
Definition: p_prober.h:167
uint16_t vendor_id
Definition: xrt_prober.h:50
struct prober_device * devices
Definition: p_prober.h:162
size_t num_entries
Definition: p_prober.h:164
struct cJSON cJSON
Definition: xrt_prober.h:26
uint16_t addr
Definition: p_prober.h:92
int p_libuvc_init(struct prober *p)
Definition: p_libuvc.c:19
void p_libusb_teardown(struct prober *p)
Definition: p_libusb.c:25
xrt_prober_string
String descriptor types.
Definition: xrt_prober.h:107
Common settings structs to be transferred between different parts of Monadon.
uint16_t bus
Definition: p_prober.h:91
Common interface to probe for devices.
cJSON * root
Definition: p_prober.h:138
int p_libusb_get_string_descriptor(struct prober *p, struct prober_device *pdev, enum xrt_prober_string which_string, unsigned char *buffer, int length)
Definition: p_libusb.c:124
The main prober that probes and manages found but not opened HMD devices that are connected to the sy...
Definition: xrt_prober.h:153
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:75
void p_libuvc_teardown(struct prober *p)
Definition: p_libuvc.c:25
A simple prober to probe for a HMD device connected to the system.
Definition: xrt_prober.h:339
uint64_t id
Definition: p_prober.h:111
uint32_t num_ports
Definition: p_prober.h:102
void p_tracking_teardown(struct prober *p)
Teardown the tracking factory.
Definition: p_tracking.c:320
struct xrt_prober_entry_lists list
Definition: cli_prober.c:37
Entry for a single device.
Definition: xrt_prober.h:48
bool p_json_get_tracking_settings(cJSON *root, struct xrt_settings_tracking *s)
Extract tracking settings from the JSON.
Definition: p_json.c:131
int p_libusb_init(struct prober *p)
Definition: p_libusb.c:19
A prober device.
Definition: p_prober.h:85
Holding enough information to recreate a tracking pipeline.
Definition: xrt_settings.h:42
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:151
Definition: p_prober.h:130
struct xrt_prober_entry ** entries
Definition: p_prober.h:165
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:196
#define MAX_AUTO_PROBERS
Definition: p_prober.h:58
Header holding common defines.
int p_libuvc_probe(struct prober *p)
Definition: p_libuvc.c:40
cJSON * p_json_open_or_create_main_file(void)
Load the JSON config file.
Definition: p_json.c:47
int p_libusb_probe(struct prober *p)
Definition: p_libusb.c:40
A probed device, may or may not be opened.
Definition: xrt_prober.h:125
uint16_t product_id
Definition: xrt_prober.h:51
int p_udev_probe(struct prober *p)
Definition: p_udev.c:136
struct xrt_prober_entry_lists * lists
Definition: p_prober.h:134
bool p_libusb_can_open(struct prober *p, struct prober_device *pdev)
Definition: p_libusb.c:173