Monado OpenXR Runtime
os_hid.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 Wrapper around OS native hid functions.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
8 *
9 * @ingroup aux_os
10 */
11
12#pragma once
13
14#include "xrt/xrt_config_os.h"
15#include <stdint.h>
16#include <stddef.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22
23/*!
24 * @interface os_hid_device
25 *
26 * Representing a single hid interface on a device.
27 */
29{
30 int (*read)(struct os_hid_device *hid_dev, uint8_t *data, size_t size, int milliseconds);
31
32 int (*write)(struct os_hid_device *hid_dev, const uint8_t *data, size_t size);
33
34 int (*get_feature)(struct os_hid_device *hid_dev, uint8_t report_num, uint8_t *data, size_t size);
35
36 int (*get_feature_timeout)(struct os_hid_device *hid_dev, void *data, size_t size, uint32_t timeout);
37
38 int (*set_feature)(struct os_hid_device *hid_dev, const uint8_t *data, size_t size);
39
40 int (*get_physical_address)(struct os_hid_device *hid_dev, uint8_t *data, size_t size);
41
42 void (*destroy)(struct os_hid_device *hid_dev);
43};
44
45/*!
46 * Read the next input report, if any, from the given hid device.
47 *
48 * If milliseconds are negative, this call blocks indefinitely, 0 polls,
49 * and positive will block for that amount of milliseconds.
50 *
51 * @public @memberof os_hid_device
52 */
53static inline int
54os_hid_read(struct os_hid_device *hid_dev, uint8_t *data, size_t size, int milliseconds)
55{
56 return hid_dev->read(hid_dev, data, size, milliseconds);
57}
58
59/*!
60 * Write an output report to the given device.
61 *
62 * @public @memberof os_hid_device
63 */
64static inline int
65os_hid_write(struct os_hid_device *hid_dev, const uint8_t *data, size_t size)
66{
67 return hid_dev->write(hid_dev, data, size);
68}
69
70/*!
71 * Get a numbered feature report.
72 *
73 * If the device doesn't have more than one feature report, request report 0.
74 *
75 * @public @memberof os_hid_device
76 */
77static inline int
78os_hid_get_feature(struct os_hid_device *hid_dev, uint8_t report_num, uint8_t *data, size_t size)
79{
80 return hid_dev->get_feature(hid_dev, report_num, data, size);
81}
82
83/*!
84 * Get a feature report with a timeout.
85 *
86 * @public @memberof os_hid_device
87 */
88static inline int
89os_hid_get_feature_timeout(struct os_hid_device *hid_dev, void *data, size_t size, uint32_t timeout)
90{
91 return hid_dev->get_feature_timeout(hid_dev, data, size, timeout);
92}
93
94/*!
95 * Set a feature report.
96 *
97 * The first byte of the buffer is the report number, to be followed by
98 * the data of the report.
99 *
100 * @public @memberof os_hid_device
101 */
102static inline int
103os_hid_set_feature(struct os_hid_device *hid_dev, const uint8_t *data, size_t size)
104{
105 return hid_dev->set_feature(hid_dev, data, size);
106}
107
108/*!
109 * Get the physical address.
110 *
111 * For USB devices, the string contains the physical path to the device (the
112 * USB controller, hubs, ports, etc). For Bluetooth *devices, the string
113 * contains the hardware (MAC) address of the device.
114 *
115 * @public @memberof os_hid_device
116 */
117static inline int
118os_hid_get_physical_address(struct os_hid_device *hid_dev, uint8_t *data, size_t size)
119{
120 return hid_dev->get_physical_address(hid_dev, data, size);
121}
122
123/*!
124 * Close and free the given device.
125 *
126 * @public @memberof os_hid_device
127 */
128static inline void
130{
131 hid_dev->destroy(hid_dev);
132}
133
134#ifdef XRT_OS_LINUX
135/*!
136 * Open the given path as a hidraw device.
137 *
138 * @see hid_hidraw
139 * @public @memberof os_hid_device
140 */
141int
142os_hid_open_hidraw(const char *path, struct os_hid_device **out_hid);
143#endif
144
145#ifdef __cplusplus
146} // extern "C"
147#endif
Representing a single hid interface on a device.
Definition: os_hid.h:29
static void os_hid_destroy(struct os_hid_device *hid_dev)
Close and free the given device.
Definition: os_hid.h:129
static int os_hid_set_feature(struct os_hid_device *hid_dev, const uint8_t *data, size_t size)
Set a feature report.
Definition: os_hid.h:103
static int os_hid_get_feature(struct os_hid_device *hid_dev, uint8_t report_num, uint8_t *data, size_t size)
Get a numbered feature report.
Definition: os_hid.h:78
int os_hid_open_hidraw(const char *path, struct os_hid_device **out_hid)
Open the given path as a hidraw device.
Definition: os_hid_hidraw.c:139
static int os_hid_get_physical_address(struct os_hid_device *hid_dev, uint8_t *data, size_t size)
Get the physical address.
Definition: os_hid.h:118
static int os_hid_read(struct os_hid_device *hid_dev, uint8_t *data, size_t size, int milliseconds)
Read the next input report, if any, from the given hid device.
Definition: os_hid.h:54
static int os_hid_write(struct os_hid_device *hid_dev, const uint8_t *data, size_t size)
Write an output report to the given device.
Definition: os_hid.h:65
static int os_hid_get_feature_timeout(struct os_hid_device *hid_dev, void *data, size_t size, uint32_t timeout)
Get a feature report with a timeout.
Definition: os_hid.h:89
Auto detect OS and certain features.