Atlas - test.c
Home / ext / SDL / src / hidapi / hidtest Lines: 40 | Size: 8064 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/******************************************************* 2 HIDAPI - Multi-Platform library for 3 communication with HID devices. 4 5 Alan Ott 6 Signal 11 Software 7 8 libusb/hidapi Team 9 10 Copyright 2022. 11 12 This contents of this file may be used by anyone 13 for any reason without any conditions and may be 14 used as a starting point for your own applications 15 which use HIDAPI. 16********************************************************/ 17 18#include <stdio.h> 19#include <wchar.h> 20#include <string.h> 21#include <stdlib.h> 22 23#include <hidapi.h> 24 25// Headers needed for sleeping. 26#ifdef _WIN32 27 #include <windows.h> 28#else 29 #include <unistd.h> 30#endif 31 32// Fallback/example 33#ifndef HID_API_MAKE_VERSION 34#define HID_API_MAKE_VERSION(mj, mn, p) (((mj) << 24) | ((mn) << 8) | (p)) 35#endif 36#ifndef HID_API_VERSION 37#define HID_API_VERSION HID_API_MAKE_VERSION(HID_API_VERSION_MAJOR, HID_API_VERSION_MINOR, HID_API_VERSION_PATCH) 38#endif 39 40// 41// Sample using platform-specific headers 42#if defined(__APPLE__) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0) 43#include <hidapi_darwin.h> 44#endif 45 46#if defined(_WIN32) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0) 47#include <hidapi_winapi.h> 48#endif 49 50#if defined(USING_HIDAPI_LIBUSB) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0) 51#include <hidapi_libusb.h> 52#endif 53// 54 55const char *hid_bus_name(hid_bus_type bus_type) { 56 static const char *const HidBusTypeName[] = { 57 "Unknown", 58 "USB", 59 "Bluetooth", 60 "I2C", 61 "SPI", 62 }; 63 64 if ((int)bus_type < 0) 65 bus_type = HID_API_BUS_UNKNOWN; 66 if ((int)bus_type >= (int)(sizeof(HidBusTypeName) / sizeof(HidBusTypeName[0]))) 67 bus_type = HID_API_BUS_UNKNOWN; 68 69 return HidBusTypeName[bus_type]; 70} 71 72void print_device(struct hid_device_info *cur_dev) { 73 printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number); 74 printf("\n"); 75 printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string); 76 printf(" Product: %ls\n", cur_dev->product_string); 77 printf(" Release: %hx\n", cur_dev->release_number); 78 printf(" Interface: %d\n", cur_dev->interface_number); 79 printf(" Usage (page): 0x%hx (0x%hx)\n", cur_dev->usage, cur_dev->usage_page); 80 printf(" Bus type: %u (%s)\n", (unsigned)cur_dev->bus_type, hid_bus_name(cur_dev->bus_type)); 81 printf("\n"); 82} 83 84void print_hid_report_descriptor_from_device(hid_device *device) { 85 unsigned char descriptor[HID_API_MAX_REPORT_DESCRIPTOR_SIZE]; 86 int res = 0; 87 88 printf(" Report Descriptor: "); 89 res = hid_get_report_descriptor(device, descriptor, sizeof(descriptor)); 90 if (res < 0) { 91 printf("error getting: %ls", hid_error(device)); 92 } 93 else { 94 printf("(%d bytes)", res); 95 } 96 for (int i = 0; i < res; i++) { 97 if (i % 10 == 0) { 98 printf("\n"); 99 } 100 printf("0x%02x, ", descriptor[i]); 101 } 102 printf("\n"); 103} 104 105void print_hid_report_descriptor_from_path(const char *path) { 106 hid_device *device = hid_open_path(path); 107 if (device) { 108 print_hid_report_descriptor_from_device(device); 109 hid_close(device); 110 } 111 else { 112 printf(" Report Descriptor: Unable to open device by path\n"); 113 } 114} 115 116void print_devices(struct hid_device_info *cur_dev) { 117 for (; cur_dev; cur_dev = cur_dev->next) { 118 print_device(cur_dev); 119 } 120} 121 122void print_devices_with_descriptor(struct hid_device_info *cur_dev) { 123 for (; cur_dev; cur_dev = cur_dev->next) { 124 print_device(cur_dev); 125 print_hid_report_descriptor_from_path(cur_dev->path); 126 } 127} 128 129int main(int argc, char* argv[]) 130{ 131 (void)argc; 132 (void)argv; 133 134 int res; 135 unsigned char buf[256]; 136 #define MAX_STR 255 137 wchar_t wstr[MAX_STR]; 138 hid_device *handle; 139 int i; 140 141 struct hid_device_info *devs; 142 143 printf("hidapi test/example tool. Compiled with hidapi version %s, runtime version %s.\n", HID_API_VERSION_STR, hid_version_str()); 144 if (HID_API_VERSION == HID_API_MAKE_VERSION(hid_version()->major, hid_version()->minor, hid_version()->patch)) { 145 printf("Compile-time version matches runtime version of hidapi.\n\n"); 146 } 147 else { 148 printf("Compile-time version is different than runtime version of hidapi.\n]n"); 149 } 150 151 if (hid_init()) 152 return -1; 153 154#if defined(__APPLE__) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0) 155 // To work properly needs to be called before hid_open/hid_open_path after hid_init. 156 // Best/recommended option - call it right after hid_init. 157 hid_darwin_set_open_exclusive(0); 158#endif 159 160 devs = hid_enumerate(0x0, 0x0); 161 print_devices_with_descriptor(devs); 162 hid_free_enumeration(devs); 163 164 // Set up the command buffer. 165 memset(buf,0x00,sizeof(buf)); 166 buf[0] = 0x01; 167 buf[1] = 0x81; 168 169 170 // Open the device using the VID, PID, 171 // and optionally the Serial number. 172 ////handle = hid_open(0x4d8, 0x3f, L"12345"); 173 handle = hid_open(0x4d8, 0x3f, NULL); 174 if (!handle) { 175 printf("unable to open device\n"); 176 hid_exit(); 177 return 1; 178 } 179 180 // Read the Manufacturer String 181 wstr[0] = 0x0000; 182 res = hid_get_manufacturer_string(handle, wstr, MAX_STR); 183 if (res < 0) 184 printf("Unable to read manufacturer string\n"); 185 printf("Manufacturer String: %ls\n", wstr); 186 187 // Read the Product String 188 wstr[0] = 0x0000; 189 res = hid_get_product_string(handle, wstr, MAX_STR); 190 if (res < 0) 191 printf("Unable to read product string\n"); 192 printf("Product String: %ls\n", wstr); 193 194 // Read the Serial Number String 195 wstr[0] = 0x0000; 196 res = hid_get_serial_number_string(handle, wstr, MAX_STR); 197 if (res < 0) 198 printf("Unable to read serial number string\n"); 199 printf("Serial Number String: (%d) %ls\n", wstr[0], wstr); 200 201 print_hid_report_descriptor_from_device(handle); 202 203 struct hid_device_info* info = hid_get_device_info(handle); 204 if (info == NULL) { 205 printf("Unable to get device info\n"); 206 } else { 207 print_devices(info); 208 } 209 210 // Read Indexed String 1 211 wstr[0] = 0x0000; 212 res = hid_get_indexed_string(handle, 1, wstr, MAX_STR); 213 if (res < 0) 214 printf("Unable to read indexed string 1\n"); 215 printf("Indexed String 1: %ls\n", wstr); 216 217 // Set the hid_read() function to be non-blocking. 218 hid_set_nonblocking(handle, 1); 219 220 // Try to read from the device. There should be no 221 // data here, but execution should not block. 222 res = hid_read(handle, buf, 17); 223 224 // Send a Feature Report to the device 225 buf[0] = 0x2; 226 buf[1] = 0xa0; 227 buf[2] = 0x0a; 228 buf[3] = 0x00; 229 buf[4] = 0x00; 230 res = hid_send_feature_report(handle, buf, 17); 231 if (res < 0) { 232 printf("Unable to send a feature report.\n"); 233 } 234 235 memset(buf,0,sizeof(buf)); 236 237 // Read a Feature Report from the device 238 buf[0] = 0x2; 239 res = hid_get_feature_report(handle, buf, sizeof(buf)); 240 if (res < 0) { 241 printf("Unable to get a feature report: %ls\n", hid_error(handle)); 242 } 243 else { 244 // Print out the returned buffer. 245 printf("Feature Report\n "); 246 for (i = 0; i < res; i++) 247 printf("%02x ", (unsigned int) buf[i]); 248 printf("\n"); 249 } 250 251 memset(buf,0,sizeof(buf)); 252 253 // Toggle LED (cmd 0x80). The first byte is the report number (0x1). 254 buf[0] = 0x1; 255 buf[1] = 0x80; 256 res = hid_write(handle, buf, 17); 257 if (res < 0) { 258 printf("Unable to write(): %ls\n", hid_error(handle)); 259 } 260 261 262 // Request state (cmd 0x81). The first byte is the report number (0x1). 263 buf[0] = 0x1; 264 buf[1] = 0x81; 265 hid_write(handle, buf, 17); 266 if (res < 0) { 267 printf("Unable to write()/2: %ls\n", hid_error(handle)); 268 } 269 270 // Read requested state. hid_read() has been set to be 271 // non-blocking by the call to hid_set_nonblocking() above. 272 // This loop demonstrates the non-blocking nature of hid_read(). 273 res = 0; 274 i = 0; 275 while (res == 0) { 276 res = hid_read(handle, buf, sizeof(buf)); 277 if (res == 0) { 278 printf("waiting...\n"); 279 } 280 if (res < 0) { 281 printf("Unable to read(): %ls\n", hid_error(handle)); 282 break; 283 } 284 285 i++; 286 if (i >= 10) { /* 10 tries by 500 ms - 5 seconds of waiting*/ 287 printf("read() timeout\n"); 288 break; 289 } 290 291#ifdef _WIN32 292 Sleep(500); 293#else 294 usleep(500*1000); 295#endif 296 } 297 298 if (res > 0) { 299 printf("Data read:\n "); 300 // Print out the returned buffer. 301 for (i = 0; i < res; i++) 302 printf("%02x ", (unsigned int) buf[i]); 303 printf("\n"); 304 } 305 306 hid_close(handle); 307 308 /* Free static HIDAPI objects. */ 309 hid_exit(); 310 311#ifdef _WIN32 312 system("pause"); 313#endif 314 315 return 0; 316} 317[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.