-
Notifications
You must be signed in to change notification settings - Fork 3
/
foohid.c
276 lines (232 loc) · 8.52 KB
/
foohid.c
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
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return. Thomas Buck
* ----------------------------------------------------------------------------
*/
#include <stdint.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <IOKit/IOKitLib.h>
#include "serial.h"
#define BAUDRATE 115200
#define PACKETSIZE 18
#define HEADERBYTES 2
#define HEADERBYTE_A 85
#define HEADERBYTE_B 252
#define CHECKSUMBYTES 2
#define PAYLOADBYTES (PACKETSIZE - HEADERBYTES - CHECKSUMBYTES)
#define CHANNELS 6
#define TESTCHANNEL 2
#define CHANNELMAXIMUM 1022
#define FOOHID_NAME "it_unbit_foohid"
#define FOOHID_CREATE 0
#define FOOHID_DESTROY 1
#define FOOHID_SEND 2
#define FOOHID_LIST 3
#define VIRTUAL_DEVICE_NAME "Virtual Serial Transmitter"
#define VIRTUAL_DEVICE_SERIAL "SN 123456"
struct gamepad_report_t {
int16_t leftX;
int16_t leftY;
int16_t rightX;
int16_t rightY;
int16_t aux1;
int16_t aux2;
};
static int running = 1;
static io_iterator_t iterator;
static io_service_t service;
static io_connect_t connect;
#define input_count 8
static uint64_t input[input_count];
static struct gamepad_report_t gamepad;
/*
* This is my USB HID Descriptor for this emulated Gamepad.
* For more informations refer to:
* http://eleccelerator.com/tutorial-about-usb-hid-report-descriptors/
* http://www.usb.org/developers/hidpage#HID%20Descriptor%20Tool
*/
static char report_descriptor[36] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x05, // USAGE (Game Pad)
0xa1, 0x01, // COLLECTION (Application)
0xa1, 0x00, // COLLECTION (Physical)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x09, 0x32, // USAGE (Z)
0x09, 0x33, // USAGE (Rx)
0x09, 0x34, // USAGE (Ry)
0x09, 0x35, // USAGE (Rz)
0x16, 0x01, 0xfe, // LOGICAL_MINIMUM (-511)
0x26, 0xff, 0x01, // LOGICAL_MAXIMUM (511)
0x75, 0x10, // REPORT_SIZE (16)
0x95, 0x06, // REPORT_COUNT (6)
0x81, 0x02, // INPUT (Data,Var,Abs)
0xc0, // END_COLLECTION
0xc0 // END_COLLECTION
};
static int foohidInit() {
printf("Searching for foohid Kernel extension...\n");
// get a reference to the IOService
kern_return_t ret = IOServiceGetMatchingServices(kIOMasterPortDefault,
IOServiceMatching(FOOHID_NAME), &iterator);
if (ret != KERN_SUCCESS) {
printf("Unable to access foohid IOService\n");
return 1;
}
int found = 0;
while ((service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
ret = IOServiceOpen(service, mach_task_self(), 0, &connect);
if (ret == KERN_SUCCESS) {
found = 1;
break;
}
}
IOObjectRelease(iterator);
if (!found) {
printf("Unable to open foohid IOService\n");
return 1;
}
printf("Creating virtual HID device...\n");
input[0] = (uint64_t)strdup(VIRTUAL_DEVICE_NAME);
input[1] = strlen((char*)input[0]);
input[2] = (uint64_t)report_descriptor;
input[3] = sizeof(report_descriptor);
input[4] = (uint64_t)strdup(VIRTUAL_DEVICE_SERIAL);
input[5] = strlen((char*)input[4]);
input[6] = (uint64_t)2; // vendor ID
input[7] = (uint64_t)3; // device ID
ret = IOConnectCallScalarMethod(connect, FOOHID_CREATE, input, input_count, NULL, 0);
if (ret != KERN_SUCCESS) {
printf("Unable to create virtual HID device\n");
return 1;
}
return 0;
}
static void foohidClose() {
printf("Destroying virtual HID device\n");
kern_return_t ret = IOConnectCallScalarMethod(connect, FOOHID_DESTROY, input, 2, NULL, 0);
if (ret != KERN_SUCCESS) {
printf("Unable to destroy virtual HID device\n");
}
}
static void foohidSend(uint16_t *data) {
for (int i = 0; i < CHANNELS; i++) {
if (data[i] > CHANNELMAXIMUM) {
data[i] = CHANNELMAXIMUM;
}
}
gamepad.leftX = data[3] - 511;
gamepad.leftY = data[2] - 511;
gamepad.rightX = data[0] - 511;
gamepad.rightY = data[1] - 511;
gamepad.aux1 = data[4] - 511;
gamepad.aux2 = data[5] - 511;
/*
printf("Sending data packet:\n");
printf("Left X: %d\n", gamepad.leftX);
printf("Left Y: %d\n", gamepad.leftY);
printf("Right X: %d\n", gamepad.rightX);
printf("Right Y: %d\n", gamepad.rightY);
printf("Aux 1: %d\n", gamepad.aux1);
printf("Aux 2: %d\n", gamepad.aux2);
*/
input[2] = (uint64_t)&gamepad;
input[3] = sizeof(struct gamepad_report_t);
kern_return_t ret = IOConnectCallScalarMethod(connect, FOOHID_SEND, input, 4, NULL, 0);
if (ret != KERN_SUCCESS) {
printf("Unable to send packet to virtual HID device\n");
}
}
static void signalHandler(int signo) {
running = 0;
printf("\n");
}
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("Usage:\n\t%s /dev/serial_port\n", argv[0]);
return 1;
}
printf("Opening serial port...\n");
int fd = serialOpen(argv[1], BAUDRATE);
if (fd == -1) {
return 1;
}
if (foohidInit() != 0) {
serialClose(fd);
return 1;
}
if (signal(SIGINT, signalHandler) == SIG_ERR) {
perror("Couldn't register signal handler");
return 1;
}
printf("Entering main-loop...\n");
while (running != 0) {
if (serialHasChar(fd)) {
unsigned char c1;
serialReadChar(fd, (char*)&c1);
if (c1 == HEADERBYTE_A) {
// Found first byte of protocol start
while (!serialHasChar(fd)) {
if (running == 0) {
serialClose(fd);
return 0;
}
}
unsigned char c2;
serialReadChar(fd, (char*)&c2);
if (c2 == HEADERBYTE_B) {
// Protocol start has been found, read payload
unsigned char data[PAYLOADBYTES];
int read = 0;
while ((read < PAYLOADBYTES) && (running != 0)) {
read += serialReadRaw(fd, (char*)data + read, PAYLOADBYTES - read);
}
// Read 16bit checksum
unsigned char checksumData[CHECKSUMBYTES];
read = 0;
while ((read < CHECKSUMBYTES) && (running != 0)) {
read += serialReadRaw(fd, (char*)checksumData + read,
CHECKSUMBYTES - read);
}
// Check if checksum matches
uint16_t checksum = 0;
for (int i = 0; i < PAYLOADBYTES; i++) {
checksum += data[i];
}
if (checksum != ((checksumData[0] << 8) | checksumData[1])) {
printf("Wrong checksum: %d != %d\n",
checksum, ((checksumData[0] << 8) | checksumData[1]));
} else {
// Decode channel values
uint16_t buff[CHANNELS + 1];
for (int i = 0; i < (CHANNELS + 1); i++) {
buff[i] = data[2 * i] << 8;
buff[i] |= data[(2 * i) + 1];
if (i < CHANNELS) {
buff[i] -= 1000;
}
}
// Check Test Channel Value
if (buff[CHANNELS] != buff[TESTCHANNEL]) {
printf("Wrong test channel value: %d != %d\n",
buff[CHANNELS], buff[TESTCHANNEL]);
}
foohidSend(buff);
}
}
}
}
usleep(1000);
}
printf("Closing serial port...\n");
serialClose(fd);
foohidClose();
return 0;
}