test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*******************************************************
  2. Demo Program for HIDAPI
  3. Alan Ott
  4. Signal 11 Software
  5. 2010-07-20
  6. Copyright 2010, All Rights Reserved
  7. This contents of this file may be used by anyone
  8. for any reason without any conditions and may be
  9. used as a starting point for your own applications
  10. which use HIDAPI.
  11. ********************************************************/
  12. #include <fx.h>
  13. #include "hidapi.h"
  14. #include "mac_support.h"
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <limits.h>
  18. #ifdef _WIN32
  19. // Thanks Microsoft, but I know how to use strncpy().
  20. #pragma warning(disable:4996)
  21. #endif
  22. class MainWindow : public FXMainWindow {
  23. FXDECLARE(MainWindow)
  24. public:
  25. enum {
  26. ID_FIRST = FXMainWindow::ID_LAST,
  27. ID_CONNECT,
  28. ID_DISCONNECT,
  29. ID_RESCAN,
  30. ID_SEND_OUTPUT_REPORT,
  31. ID_SEND_FEATURE_REPORT,
  32. ID_GET_FEATURE_REPORT,
  33. ID_CLEAR,
  34. ID_TIMER,
  35. ID_MAC_TIMER,
  36. ID_LAST,
  37. };
  38. private:
  39. FXList *device_list;
  40. FXButton *connect_button;
  41. FXButton *disconnect_button;
  42. FXButton *rescan_button;
  43. FXButton *output_button;
  44. FXLabel *connected_label;
  45. FXTextField *output_text;
  46. FXTextField *output_len;
  47. FXButton *feature_button;
  48. FXButton *get_feature_button;
  49. FXTextField *feature_text;
  50. FXTextField *feature_len;
  51. FXTextField *get_feature_text;
  52. FXText *input_text;
  53. FXFont *title_font;
  54. struct hid_device_info *devices;
  55. hid_device *connected_device;
  56. size_t getDataFromTextField(FXTextField *tf, char *buf, size_t len);
  57. int getLengthFromTextField(FXTextField *tf);
  58. protected:
  59. MainWindow() {};
  60. public:
  61. MainWindow(FXApp *a);
  62. ~MainWindow();
  63. virtual void create();
  64. long onConnect(FXObject *sender, FXSelector sel, void *ptr);
  65. long onDisconnect(FXObject *sender, FXSelector sel, void *ptr);
  66. long onRescan(FXObject *sender, FXSelector sel, void *ptr);
  67. long onSendOutputReport(FXObject *sender, FXSelector sel, void *ptr);
  68. long onSendFeatureReport(FXObject *sender, FXSelector sel, void *ptr);
  69. long onGetFeatureReport(FXObject *sender, FXSelector sel, void *ptr);
  70. long onClear(FXObject *sender, FXSelector sel, void *ptr);
  71. long onTimeout(FXObject *sender, FXSelector sel, void *ptr);
  72. long onMacTimeout(FXObject *sender, FXSelector sel, void *ptr);
  73. };
  74. // FOX 1.7 changes the timeouts to all be nanoseconds.
  75. // Fox 1.6 had all timeouts as milliseconds.
  76. #if (FOX_MINOR >= 7)
  77. const int timeout_scalar = 1000*1000;
  78. #else
  79. const int timeout_scalar = 1;
  80. #endif
  81. FXMainWindow *g_main_window;
  82. FXDEFMAP(MainWindow) MainWindowMap [] = {
  83. FXMAPFUNC(SEL_COMMAND, MainWindow::ID_CONNECT, MainWindow::onConnect ),
  84. FXMAPFUNC(SEL_COMMAND, MainWindow::ID_DISCONNECT, MainWindow::onDisconnect ),
  85. FXMAPFUNC(SEL_COMMAND, MainWindow::ID_RESCAN, MainWindow::onRescan ),
  86. FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SEND_OUTPUT_REPORT, MainWindow::onSendOutputReport ),
  87. FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SEND_FEATURE_REPORT, MainWindow::onSendFeatureReport ),
  88. FXMAPFUNC(SEL_COMMAND, MainWindow::ID_GET_FEATURE_REPORT, MainWindow::onGetFeatureReport ),
  89. FXMAPFUNC(SEL_COMMAND, MainWindow::ID_CLEAR, MainWindow::onClear ),
  90. FXMAPFUNC(SEL_TIMEOUT, MainWindow::ID_TIMER, MainWindow::onTimeout ),
  91. FXMAPFUNC(SEL_TIMEOUT, MainWindow::ID_MAC_TIMER, MainWindow::onMacTimeout ),
  92. };
  93. FXIMPLEMENT(MainWindow, FXMainWindow, MainWindowMap, ARRAYNUMBER(MainWindowMap));
  94. MainWindow::MainWindow(FXApp *app)
  95. : FXMainWindow(app, "HIDAPI Test Application", NULL, NULL, DECOR_ALL, 200,100, 425,700)
  96. {
  97. devices = NULL;
  98. connected_device = NULL;
  99. FXVerticalFrame *vf = new FXVerticalFrame(this, LAYOUT_FILL_Y|LAYOUT_FILL_X);
  100. FXLabel *label = new FXLabel(vf, "HIDAPI Test Tool");
  101. title_font = new FXFont(getApp(), "Arial", 14, FXFont::Bold);
  102. label->setFont(title_font);
  103. new FXLabel(vf,
  104. "Select a device and press Connect.", NULL, JUSTIFY_LEFT);
  105. new FXLabel(vf,
  106. "Output data bytes can be entered in the Output section, \n"
  107. "separated by space, comma or brackets. Data starting with 0x\n"
  108. "is treated as hex. Data beginning with a 0 is treated as \n"
  109. "octal. All other data is treated as decimal.", NULL, JUSTIFY_LEFT);
  110. new FXLabel(vf,
  111. "Data received from the device appears in the Input section.",
  112. NULL, JUSTIFY_LEFT);
  113. new FXLabel(vf,
  114. "Optionally, a report length may be specified. Extra bytes are\n"
  115. "padded with zeros. If no length is specified, the length is \n"
  116. "inferred from the data.",
  117. NULL, JUSTIFY_LEFT);
  118. new FXLabel(vf, "");
  119. // Device List and Connect/Disconnect buttons
  120. FXHorizontalFrame *hf = new FXHorizontalFrame(vf, LAYOUT_FILL_X);
  121. //device_list = new FXList(new FXHorizontalFrame(hf,FRAME_SUNKEN|FRAME_THICK, 0,0,0,0, 0,0,0,0), NULL, 0, LISTBOX_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT, 0,0,300,200);
  122. device_list = new FXList(new FXHorizontalFrame(hf,FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,0,0, 0,0,0,0), NULL, 0, LISTBOX_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,300,200);
  123. FXVerticalFrame *buttonVF = new FXVerticalFrame(hf);
  124. connect_button = new FXButton(buttonVF, "Connect", NULL, this, ID_CONNECT, BUTTON_NORMAL|LAYOUT_FILL_X);
  125. disconnect_button = new FXButton(buttonVF, "Disconnect", NULL, this, ID_DISCONNECT, BUTTON_NORMAL|LAYOUT_FILL_X);
  126. disconnect_button->disable();
  127. rescan_button = new FXButton(buttonVF, "Re-Scan devices", NULL, this, ID_RESCAN, BUTTON_NORMAL|LAYOUT_FILL_X);
  128. new FXHorizontalFrame(buttonVF, 0, 0,0,0,0, 0,0,50,0);
  129. connected_label = new FXLabel(vf, "Disconnected");
  130. new FXHorizontalFrame(vf);
  131. // Output Group Box
  132. FXGroupBox *gb = new FXGroupBox(vf, "Output", FRAME_GROOVE|LAYOUT_FILL_X);
  133. FXMatrix *matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X);
  134. new FXLabel(matrix, "Data");
  135. new FXLabel(matrix, "Length");
  136. new FXLabel(matrix, "");
  137. //hf = new FXHorizontalFrame(gb, LAYOUT_FILL_X);
  138. output_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
  139. output_text->setText("1 0x81 0");
  140. output_len = new FXTextField(matrix, 5, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
  141. output_button = new FXButton(matrix, "Send Output Report", NULL, this, ID_SEND_OUTPUT_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
  142. output_button->disable();
  143. //new FXHorizontalFrame(matrix, LAYOUT_FILL_X);
  144. //hf = new FXHorizontalFrame(gb, LAYOUT_FILL_X);
  145. feature_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
  146. feature_len = new FXTextField(matrix, 5, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
  147. feature_button = new FXButton(matrix, "Send Feature Report", NULL, this, ID_SEND_FEATURE_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
  148. feature_button->disable();
  149. get_feature_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
  150. new FXWindow(matrix);
  151. get_feature_button = new FXButton(matrix, "Get Feature Report", NULL, this, ID_GET_FEATURE_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
  152. get_feature_button->disable();
  153. // Input Group Box
  154. gb = new FXGroupBox(vf, "Input", FRAME_GROOVE|LAYOUT_FILL_X|LAYOUT_FILL_Y);
  155. FXVerticalFrame *innerVF = new FXVerticalFrame(gb, LAYOUT_FILL_X|LAYOUT_FILL_Y);
  156. input_text = new FXText(new FXHorizontalFrame(innerVF,LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN|FRAME_THICK, 0,0,0,0, 0,0,0,0), NULL, 0, LAYOUT_FILL_X|LAYOUT_FILL_Y);
  157. input_text->setEditable(false);
  158. new FXButton(innerVF, "Clear", NULL, this, ID_CLEAR, BUTTON_NORMAL|LAYOUT_RIGHT);
  159. }
  160. MainWindow::~MainWindow()
  161. {
  162. if (connected_device)
  163. hid_close(connected_device);
  164. hid_exit();
  165. delete title_font;
  166. }
  167. void
  168. MainWindow::create()
  169. {
  170. FXMainWindow::create();
  171. show();
  172. onRescan(NULL, 0, NULL);
  173. #ifdef __APPLE__
  174. init_apple_message_system();
  175. #endif
  176. getApp()->addTimeout(this, ID_MAC_TIMER,
  177. 50 * timeout_scalar /*50ms*/);
  178. }
  179. long
  180. MainWindow::onConnect(FXObject *sender, FXSelector sel, void *ptr)
  181. {
  182. if (connected_device != NULL)
  183. return 1;
  184. FXint cur_item = device_list->getCurrentItem();
  185. if (cur_item < 0)
  186. return -1;
  187. FXListItem *item = device_list->getItem(cur_item);
  188. if (!item)
  189. return -1;
  190. struct hid_device_info *device_info = (struct hid_device_info*) item->getData();
  191. if (!device_info)
  192. return -1;
  193. connected_device = hid_open_path(device_info->path);
  194. if (!connected_device) {
  195. FXMessageBox::error(this, MBOX_OK, "Device Error", "Unable To Connect to Device");
  196. return -1;
  197. }
  198. hid_set_nonblocking(connected_device, 1);
  199. getApp()->addTimeout(this, ID_TIMER,
  200. 5 * timeout_scalar /*5ms*/);
  201. FXString s;
  202. s.format("Connected to: %04hx:%04hx -", device_info->vendor_id, device_info->product_id);
  203. s += FXString(" ") + device_info->manufacturer_string;
  204. s += FXString(" ") + device_info->product_string;
  205. connected_label->setText(s);
  206. output_button->enable();
  207. feature_button->enable();
  208. get_feature_button->enable();
  209. connect_button->disable();
  210. disconnect_button->enable();
  211. input_text->setText("");
  212. return 1;
  213. }
  214. long
  215. MainWindow::onDisconnect(FXObject *sender, FXSelector sel, void *ptr)
  216. {
  217. hid_close(connected_device);
  218. connected_device = NULL;
  219. connected_label->setText("Disconnected");
  220. output_button->disable();
  221. feature_button->disable();
  222. get_feature_button->disable();
  223. connect_button->enable();
  224. disconnect_button->disable();
  225. getApp()->removeTimeout(this, ID_TIMER);
  226. return 1;
  227. }
  228. long
  229. MainWindow::onRescan(FXObject *sender, FXSelector sel, void *ptr)
  230. {
  231. struct hid_device_info *cur_dev;
  232. device_list->clearItems();
  233. // List the Devices
  234. hid_free_enumeration(devices);
  235. devices = hid_enumerate(0x0, 0x0);
  236. cur_dev = devices;
  237. while (cur_dev) {
  238. // Add it to the List Box.
  239. FXString s;
  240. FXString usage_str;
  241. s.format("%04hx:%04hx -", cur_dev->vendor_id, cur_dev->product_id);
  242. s += FXString(" ") + cur_dev->manufacturer_string;
  243. s += FXString(" ") + cur_dev->product_string;
  244. usage_str.format(" (usage: %04hx:%04hx) ", cur_dev->usage_page, cur_dev->usage);
  245. s += usage_str;
  246. FXListItem *li = new FXListItem(s, NULL, cur_dev);
  247. device_list->appendItem(li);
  248. cur_dev = cur_dev->next;
  249. }
  250. if (device_list->getNumItems() == 0)
  251. device_list->appendItem("*** No Devices Connected ***");
  252. else {
  253. device_list->selectItem(0);
  254. }
  255. return 1;
  256. }
  257. size_t
  258. MainWindow::getDataFromTextField(FXTextField *tf, char *buf, size_t len)
  259. {
  260. const char *delim = " ,{}\t\r\n";
  261. FXString data = tf->getText();
  262. const FXchar *d = data.text();
  263. size_t i = 0;
  264. // Copy the string from the GUI.
  265. size_t sz = strlen(d);
  266. char *str = (char*) malloc(sz+1);
  267. strcpy(str, d);
  268. // For each token in the string, parse and store in buf[].
  269. char *token = strtok(str, delim);
  270. while (token) {
  271. char *endptr;
  272. long int val = strtol(token, &endptr, 0);
  273. buf[i++] = val;
  274. token = strtok(NULL, delim);
  275. }
  276. free(str);
  277. return i;
  278. }
  279. /* getLengthFromTextField()
  280. Returns length:
  281. 0: empty text field
  282. >0: valid length
  283. -1: invalid length */
  284. int
  285. MainWindow::getLengthFromTextField(FXTextField *tf)
  286. {
  287. long int len;
  288. FXString str = tf->getText();
  289. size_t sz = str.length();
  290. if (sz > 0) {
  291. char *endptr;
  292. len = strtol(str.text(), &endptr, 0);
  293. if (endptr != str.text() && *endptr == '\0') {
  294. if (len <= 0) {
  295. FXMessageBox::error(this, MBOX_OK, "Invalid length", "Enter a length greater than zero.");
  296. return -1;
  297. }
  298. return len;
  299. }
  300. else
  301. return -1;
  302. }
  303. return 0;
  304. }
  305. long
  306. MainWindow::onSendOutputReport(FXObject *sender, FXSelector sel, void *ptr)
  307. {
  308. char buf[256];
  309. size_t data_len, len;
  310. int textfield_len;
  311. memset(buf, 0x0, sizeof(buf));
  312. textfield_len = getLengthFromTextField(output_len);
  313. data_len = getDataFromTextField(output_text, buf, sizeof(buf));
  314. if (textfield_len < 0) {
  315. FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is invalid. Please enter a number in hex, octal, or decimal.");
  316. return 1;
  317. }
  318. if (textfield_len > sizeof(buf)) {
  319. FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is too long.");
  320. return 1;
  321. }
  322. len = (textfield_len)? textfield_len: data_len;
  323. int res = hid_write(connected_device, (const unsigned char*)buf, len);
  324. if (res < 0) {
  325. FXMessageBox::error(this, MBOX_OK, "Error Writing", "Could not write to device. Error reported was: %ls", hid_error(connected_device));
  326. }
  327. return 1;
  328. }
  329. long
  330. MainWindow::onSendFeatureReport(FXObject *sender, FXSelector sel, void *ptr)
  331. {
  332. char buf[256];
  333. size_t data_len, len;
  334. int textfield_len;
  335. memset(buf, 0x0, sizeof(buf));
  336. textfield_len = getLengthFromTextField(feature_len);
  337. data_len = getDataFromTextField(feature_text, buf, sizeof(buf));
  338. if (textfield_len < 0) {
  339. FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is invalid. Please enter a number in hex, octal, or decimal.");
  340. return 1;
  341. }
  342. if (textfield_len > sizeof(buf)) {
  343. FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is too long.");
  344. return 1;
  345. }
  346. len = (textfield_len)? textfield_len: data_len;
  347. int res = hid_send_feature_report(connected_device, (const unsigned char*)buf, len);
  348. if (res < 0) {
  349. FXMessageBox::error(this, MBOX_OK, "Error Writing", "Could not send feature report to device. Error reported was: %ls", hid_error(connected_device));
  350. }
  351. return 1;
  352. }
  353. long
  354. MainWindow::onGetFeatureReport(FXObject *sender, FXSelector sel, void *ptr)
  355. {
  356. char buf[256];
  357. size_t len;
  358. memset(buf, 0x0, sizeof(buf));
  359. len = getDataFromTextField(get_feature_text, buf, sizeof(buf));
  360. if (len != 1) {
  361. FXMessageBox::error(this, MBOX_OK, "Too many numbers", "Enter only a single report number in the text field");
  362. }
  363. int res = hid_get_feature_report(connected_device, (unsigned char*)buf, sizeof(buf));
  364. if (res < 0) {
  365. FXMessageBox::error(this, MBOX_OK, "Error Getting Report", "Could not get feature report from device. Error reported was: %ls", hid_error(connected_device));
  366. }
  367. if (res > 0) {
  368. FXString s;
  369. s.format("Returned Feature Report. %d bytes:\n", res);
  370. for (int i = 0; i < res; i++) {
  371. FXString t;
  372. t.format("%02hhx ", buf[i]);
  373. s += t;
  374. if ((i+1) % 4 == 0)
  375. s += " ";
  376. if ((i+1) % 16 == 0)
  377. s += "\n";
  378. }
  379. s += "\n";
  380. input_text->appendText(s);
  381. input_text->setBottomLine(INT_MAX);
  382. }
  383. return 1;
  384. }
  385. long
  386. MainWindow::onClear(FXObject *sender, FXSelector sel, void *ptr)
  387. {
  388. input_text->setText("");
  389. return 1;
  390. }
  391. long
  392. MainWindow::onTimeout(FXObject *sender, FXSelector sel, void *ptr)
  393. {
  394. unsigned char buf[256];
  395. int res = hid_read(connected_device, buf, sizeof(buf));
  396. if (res > 0) {
  397. FXString s;
  398. s.format("Received %d bytes:\n", res);
  399. for (int i = 0; i < res; i++) {
  400. FXString t;
  401. t.format("%02hhx ", buf[i]);
  402. s += t;
  403. if ((i+1) % 4 == 0)
  404. s += " ";
  405. if ((i+1) % 16 == 0)
  406. s += "\n";
  407. }
  408. s += "\n";
  409. input_text->appendText(s);
  410. input_text->setBottomLine(INT_MAX);
  411. }
  412. if (res < 0) {
  413. input_text->appendText("hid_read() returned error\n");
  414. input_text->setBottomLine(INT_MAX);
  415. }
  416. getApp()->addTimeout(this, ID_TIMER,
  417. 5 * timeout_scalar /*5ms*/);
  418. return 1;
  419. }
  420. long
  421. MainWindow::onMacTimeout(FXObject *sender, FXSelector sel, void *ptr)
  422. {
  423. #ifdef __APPLE__
  424. check_apple_events();
  425. getApp()->addTimeout(this, ID_MAC_TIMER,
  426. 50 * timeout_scalar /*50ms*/);
  427. #endif
  428. return 1;
  429. }
  430. int main(int argc, char **argv)
  431. {
  432. FXApp app("HIDAPI Test Application", "Signal 11 Software");
  433. app.init(argc, argv);
  434. g_main_window = new MainWindow(&app);
  435. app.create();
  436. app.run();
  437. return 0;
  438. }