diff options
author | Akshay <[email protected]> | 2022-04-10 12:13:40 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2022-04-10 12:13:40 +0100 |
commit | dc90387ce7d8ba7b607d9c48540bf6d8b560f14d (patch) | |
tree | 4ccb8fa5886b66fa9d480edef74236c27f035e16 /lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/test-usb-hid.c |
Diffstat (limited to 'lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/test-usb-hid.c')
-rw-r--r-- | lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/test-usb-hid.c | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/test-usb-hid.c b/lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/test-usb-hid.c new file mode 100644 index 000000000..863c44d4b --- /dev/null +++ b/lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/test-usb-hid.c | |||
@@ -0,0 +1,177 @@ | |||
1 | /* | ||
2 | Copyright (c) 2014 Guillaume Duc <[email protected]> | ||
3 | Modifications copyright (c) 2020 Alex Lewontin <[email protected]> | ||
4 | |||
5 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
6 | of this software and associated documentation files (the "Software"), to deal | ||
7 | in the Software without restriction, including without limitation the rights | ||
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
9 | copies of the Software, and to permit persons to whom the Software is | ||
10 | furnished to do so, subject to the following conditions: | ||
11 | |||
12 | The above copyright notice and this permission notice shall be included in all | ||
13 | copies or substantial portions of the Software. | ||
14 | |||
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
21 | SOFTWARE. | ||
22 | |||
23 | */ | ||
24 | |||
25 | #include "hidapi.h" | ||
26 | |||
27 | #include <assert.h> | ||
28 | #include <errno.h> | ||
29 | #include <fcntl.h> | ||
30 | #include <stdint.h> | ||
31 | #include <stdio.h> | ||
32 | #include <stdlib.h> | ||
33 | #include <string.h> | ||
34 | #include <sys/ioctl.h> | ||
35 | #include <sys/signal.h> | ||
36 | #include <sys/stat.h> | ||
37 | #include <sys/types.h> | ||
38 | #include <unistd.h> | ||
39 | |||
40 | #define USB_HID_IN_REPORT_SIZE 1 | ||
41 | #define USB_HID_OUT_REPORT_SIZE 1 | ||
42 | |||
43 | struct usb_hid_in_report_s { | ||
44 | uint8_t sequence_number; | ||
45 | }; | ||
46 | |||
47 | struct usb_hid_out_report_s { | ||
48 | uint8_t sequence_number; | ||
49 | }; | ||
50 | |||
51 | static uint8_t usb_hid_in_report_buf[USB_HID_IN_REPORT_SIZE]; | ||
52 | /* +1 for the report index */ | ||
53 | static uint8_t usb_hid_out_report_buf[USB_HID_OUT_REPORT_SIZE + 1]; | ||
54 | |||
55 | static struct usb_hid_in_report_s* usb_hid_in_report = | ||
56 | (struct usb_hid_in_report_s*)usb_hid_in_report_buf; | ||
57 | |||
58 | static struct usb_hid_out_report_s* usb_hid_out_report = | ||
59 | (struct usb_hid_out_report_s*)(&usb_hid_out_report_buf[1]); | ||
60 | |||
61 | static hid_device* handle; | ||
62 | |||
63 | static void close_hidapi() __attribute__((noreturn)); | ||
64 | static void close_client() __attribute__((noreturn)); | ||
65 | |||
66 | static void read_in_report() | ||
67 | { | ||
68 | int res, i; | ||
69 | |||
70 | printf("read()\n"); | ||
71 | res = hid_read(handle, usb_hid_in_report_buf, USB_HID_IN_REPORT_SIZE); | ||
72 | if (res < 0) { | ||
73 | perror("read"); | ||
74 | exit(EXIT_FAILURE); | ||
75 | } else { | ||
76 | printf("read() read %d bytes:\t", res); | ||
77 | for (i = 0; i < res; i++) | ||
78 | printf("%02hhx ", usb_hid_in_report_buf[i]); | ||
79 | printf("\n"); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | static void send_out_report() | ||
84 | { | ||
85 | int res; | ||
86 | |||
87 | usb_hid_out_report_buf[0] = 0; | ||
88 | |||
89 | res = | ||
90 | hid_write(handle, usb_hid_out_report_buf, USB_HID_OUT_REPORT_SIZE + 1); | ||
91 | if (res < 0) { | ||
92 | perror("write"); | ||
93 | exit(EXIT_FAILURE); | ||
94 | } | ||
95 | |||
96 | usb_hid_out_report->sequence_number++; | ||
97 | } | ||
98 | |||
99 | static void close_hidapi() | ||
100 | { | ||
101 | int res = hid_exit(); | ||
102 | if (res) { | ||
103 | perror("Could not close hidapi library"); | ||
104 | exit(EXIT_FAILURE); | ||
105 | } | ||
106 | exit(EXIT_SUCCESS); | ||
107 | } | ||
108 | |||
109 | static void close_client() | ||
110 | { | ||
111 | hid_close(handle); | ||
112 | close_hidapi(); | ||
113 | } | ||
114 | |||
115 | int main(int argc, char** argv) | ||
116 | { | ||
117 | int res; | ||
118 | unsigned long vid, pid; | ||
119 | struct hid_device_info *devs, *cur_dev; | ||
120 | |||
121 | if (argc < 2) { | ||
122 | fprintf(stderr, "Usage: %s [VID] [PID]\n", argv[0]); | ||
123 | exit(EXIT_FAILURE); | ||
124 | } | ||
125 | vid = strtoul(argv[1], NULL, 16); | ||
126 | pid = strtoul(argv[2], NULL, 16); | ||
127 | |||
128 | devs = hid_enumerate(0x0, 0x0); | ||
129 | cur_dev = devs; | ||
130 | while (cur_dev) { | ||
131 | printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: " | ||
132 | "%ls", | ||
133 | cur_dev->vendor_id, | ||
134 | cur_dev->product_id, | ||
135 | cur_dev->path, | ||
136 | cur_dev->serial_number); | ||
137 | printf("\n"); | ||
138 | printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string); | ||
139 | printf(" Product: %ls\n", cur_dev->product_string); | ||
140 | printf(" Release: %hx\n", cur_dev->release_number); | ||
141 | printf(" Interface: %d\n", cur_dev->interface_number); | ||
142 | printf(" Usage (page): 0x%hx (0x%hx)\n", | ||
143 | cur_dev->usage, | ||
144 | cur_dev->usage_page); | ||
145 | printf("\n"); | ||
146 | cur_dev = cur_dev->next; | ||
147 | } | ||
148 | hid_free_enumeration(devs); | ||
149 | |||
150 | /* Make sure we clean up on CTRL-C interrupt */ | ||
151 | signal(SIGINT, close_client); | ||
152 | |||
153 | res = hid_init(); | ||
154 | if (res) { | ||
155 | perror("Could not load hidapi library"); | ||
156 | exit(EXIT_FAILURE); | ||
157 | } | ||
158 | handle = hid_open(vid, pid, NULL); | ||
159 | if (!handle) { | ||
160 | perror("Unable to open device"); | ||
161 | close_hidapi(); | ||
162 | exit(EXIT_FAILURE); | ||
163 | } | ||
164 | |||
165 | usb_hid_out_report->sequence_number = 4; | ||
166 | send_out_report(); | ||
167 | |||
168 | while (1) { | ||
169 | read_in_report(); | ||
170 | |||
171 | if (usb_hid_in_report->sequence_number == 40) { | ||
172 | usb_hid_out_report->sequence_number = | ||
173 | usb_hid_in_report->sequence_number / 2; | ||
174 | send_out_report(); | ||
175 | } | ||
176 | } | ||
177 | } | ||