-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathhid.c
More file actions
289 lines (253 loc) · 9.87 KB
/
hid.c
File metadata and controls
289 lines (253 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
SlimeVR Code is placed under the MIT license
Copyright (c) 2025 SlimeVR Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "globals.h"
#include <zephyr/kernel.h>
#include <zephyr/usb/usb_device.h>
#include <zephyr/usb/class/usb_hid.h>
static struct k_work report_send;
static struct tracker_report {
uint8_t data[16];
} __packed report = {
.data = {0}
};;
uint8_t reports[256*sizeof(report)];
uint8_t report_count = 0;
uint8_t report_sent = 0;
static bool configured;
static const struct device *hdev;
static ATOMIC_DEFINE(hid_ep_in_busy, 1);
#define HID_EP_BUSY_FLAG 0
#define REPORT_PERIOD K_MSEC(1) // streaming reports
LOG_MODULE_REGISTER(hid_event, LOG_LEVEL_INF);
static void report_event_handler(struct k_timer *dummy);
static K_TIMER_DEFINE(event_timer, report_event_handler, NULL);
static const uint8_t hid_report_desc[] = {
HID_USAGE_PAGE(HID_USAGE_GEN_DESKTOP),
HID_USAGE(HID_USAGE_GEN_DESKTOP_UNDEFINED),
HID_COLLECTION(HID_COLLECTION_APPLICATION),
HID_USAGE(HID_USAGE_GEN_DESKTOP_UNDEFINED),
HID_REPORT_SIZE(8),
HID_REPORT_COUNT(64),
HID_INPUT(0x02),
HID_END_COLLECTION,
};
uint16_t sent_device_addr = 0;
bool usb_enabled = false;
int64_t last_registration_sent = 0;
static void packet_device_addr(uint8_t *report, uint16_t id) // associate id and tracker address
{
report[0] = 255; // receiver packet 0
report[1] = id;
memcpy(&report[2], &stored_tracker_addr[id], 6);
memset(&report[8], 0, 8); // last 8 bytes unused for now
}
static void send_report(struct k_work *work)
{
if (!usb_enabled) return;
if (!stored_trackers) return;
if (report_count == 0 && k_uptime_get() - 100 < last_registration_sent) return; // send registrations only every 100ms
int ret, wrote;
last_registration_sent = k_uptime_get();
if (!atomic_test_and_set_bit(hid_ep_in_busy, HID_EP_BUSY_FLAG)) {
// TODO: this really sucks, how can i send as much or as little as i want instead??
// for (int i = report_count; i < 4; i++) memcpy(&reports[sizeof(report) * (report_sent+i)], &reports[sizeof(report) * report_sent], sizeof(report)); // just duplicate first entry a bunch, this will definitely cause problems
// cycle through devices and send associated address for server to register
for (int i = report_count; i < 4; i++) {
packet_device_addr(&reports[sizeof(report) * (report_sent + i)], sent_device_addr);
sent_device_addr++;
sent_device_addr %= stored_trackers;
}
// ret = hid_int_ep_write(hdev, &reports, sizeof(report) * report_count, &wrote);
ret = hid_int_ep_write(hdev, &reports[sizeof(report) * report_sent], sizeof(report) * 4, &wrote);
if (report_count > 4) {
LOG_INF("Dropped %u report%s", report_count - 4, report_count - 4 > 1 ? "s" : "");
}
report_sent += report_count;
report_sent += 3; // this is a hack to make sure the ep isnt reading the same bits as trackers write to
if (report_sent > 128) report_sent = 0; // an attempt to make ringbuffer so the ep isnt reading the same bits as trackers write to
report_count = 0;
if (ret != 0) {
/*
* Do nothing and wait until host has reset the device
* and hid_ep_in_busy is cleared.
*/
LOG_ERR("Failed to submit report");
} else {
//LOG_DBG("Report submitted");
}
} else { // busy with what
//LOG_DBG("HID IN endpoint busy");
}
}
static void int_in_ready_cb(const struct device *dev)
{
ARG_UNUSED(dev);
if (!atomic_test_and_clear_bit(hid_ep_in_busy, HID_EP_BUSY_FLAG)) {
LOG_WRN("IN endpoint callback without preceding buffer write");
}
}
/*
* On Idle callback is available here as an example even if actual use is
* very limited. In contrast to report_event_handler(),
* report value is not incremented here.
*/
static void on_idle_cb(const struct device *dev, uint16_t report_id)
{
LOG_DBG("On idle callback");
k_work_submit(&report_send);
}
static void report_event_handler(struct k_timer *dummy)
{
if (usb_enabled)
k_work_submit(&report_send);
}
static void protocol_cb(const struct device *dev, uint8_t protocol)
{
LOG_INF("New protocol: %s", protocol == HID_PROTOCOL_BOOT ?
"boot" : "report");
}
static const struct hid_ops ops = {
.int_in_ready = int_in_ready_cb,
.on_idle = on_idle_cb,
.protocol_change = protocol_cb,
};
static void status_cb(enum usb_dc_status_code status, const uint8_t *param)
{
switch (status) {
case USB_DC_RESET:
configured = false;
break;
case USB_DC_CONFIGURED:
int configurationIndex = *param;
if(configurationIndex == 0) {
// from usb_device.c: A configuration index of 0 unconfigures the device.
configured = false;
} else {
if (!configured) {
int_in_ready_cb(hdev);
configured = true;
}
}
break;
case USB_DC_SOF:
break;
default:
LOG_DBG("status %u unhandled", status);
break;
}
}
static int composite_pre_init()
{
hdev = device_get_binding("HID_0");
if (hdev == NULL) {
LOG_ERR("Cannot get USB HID Device");
return -ENODEV;
}
LOG_INF("HID Device: dev %p", hdev);
usb_hid_register_device(hdev, hid_report_desc, sizeof(hid_report_desc),
&ops);
atomic_set_bit(hid_ep_in_busy, HID_EP_BUSY_FLAG);
k_timer_start(&event_timer, REPORT_PERIOD, REPORT_PERIOD);
if (usb_hid_set_proto_code(hdev, HID_BOOT_IFACE_CODE_NONE)) {
LOG_WRN("Failed to set Protocol Code");
}
return usb_hid_init(hdev);
}
SYS_INIT(composite_pre_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
void usb_init_thread(void)
{
usb_enable(status_cb);
k_work_init(&report_send, send_report);
usb_enabled = true;
}
K_THREAD_DEFINE(usb_init_thread_id, 256, usb_init_thread, NULL, NULL, NULL, 6, 0, 0);
//|b0 |b1 |b2 |b3 |b4 |b5 |b6 |b7 |b8 |b9 |b10 |b11 |b12 |b13 |b14 |b15 |
//|type |id |packet data |
//|0 |id |proto |batt |batt_v |temp |brd_id |mcu_id |imu_id |mag_id |fw_date |major |minor |patch |rssi |
//|1 |id |q0 |q1 |q2 |q3 |a0 |a1 |a2 |
//|2 |id |batt |batt_v |temp |q_buf |a0 |a1 |a2 |rssi |
//|3 |id |svr_stat|status |resv |rssi |
//|4 |id |q0 |q1 |q2 |q3 |m0 |m1 |m2 |
//|255 |id |addr |resv |
#include "util.h"
static float last_q_trackers[256][4] = {0};
static int last_valid_trackers[256] = {0};
void hid_write_packet_n(uint8_t *data, uint8_t rssi)
{
// discard packets with abnormal rotation // TODO:
if (data[0] == 1 || data[0] == 2 || data[0] == 4)
{
float v[3] = {0};
float q[4] = {0};
int16_t *buf = (int16_t *)&data[2];
uint32_t *q_buf = (uint32_t *)&data[5];
if (data[0] == 1 || data[0] == 4)
{
q[0] = FIXED_15_TO_DOUBLE(buf[3]);
q[1] = FIXED_15_TO_DOUBLE(buf[0]);
q[2] = FIXED_15_TO_DOUBLE(buf[1]);
q[3] = FIXED_15_TO_DOUBLE(buf[2]);
}
else
{
v[0] = FIXED_10_TO_DOUBLE(*q_buf & 1023);
v[1] = FIXED_11_TO_DOUBLE((*q_buf >> 10) & 2047);
v[2] = FIXED_11_TO_DOUBLE((*q_buf >> 21) & 2047);
for (int i = 0; i < 3; i++)
v[i] = v[i] * 2 - 1;
q_iem(v, q);
}
float *last_q = last_q_trackers[data[1]];
float mag = q_diff_mag(q, last_q);
if (mag > 0.5f && mag < 6.28f - 0.5f)
{
LOG_ERR("Abnormal rotation for %012llX, ID %d, packet ID %d, mag: %.2f rad, last valid: %d", stored_tracker_addr[data[1]], data[1], data[0], (double)mag, last_valid_trackers[data[1]]);
printk("quat: %5.2f %5.2f %5.2f %5.2f\n", (double)q[0], (double)q[1], (double)q[2], (double)q[3]);
printk("last: %5.2f %5.2f %5.2f %5.2f\n", (double)last_q[0], (double)last_q[1], (double)last_q[2], (double)last_q[3]);
last_valid_trackers[data[1]]++;
if (last_valid_trackers[data[1]] > 2) // reset last_q
{
LOG_WRN("Reset rotation for %012llX, ID %d", stored_tracker_addr[data[1]], data[1]);
last_valid_trackers[data[1]] = 0;
memcpy(last_q, q, sizeof(q));
return;
}
return;
}
last_valid_trackers[data[1]] = 0;
memcpy(last_q, q, sizeof(q));
}
memcpy(&report.data, data, 16); // all data can be passed through
if (data[0] != 1 && data[0] != 4) // packet 1 and 4 are full precision quat and accel/mag, no room for rssi
report.data[15]=rssi;
// TODO: this sucks
for (int i = 0; i < report_count; i++) // replace existing entry instead
{
if (reports[sizeof(report) * (report_sent + i) + 1] == report.data[1])
{
memcpy(&reports[sizeof(report) * (report_sent + i)], &report, sizeof(report));
break;
}
}
if (report_count > 100) // overflow
return;
memcpy(&reports[sizeof(report) * (report_sent + report_count)], &report, sizeof(report));
report_count++;
}