usb_test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/view.h>
  4. #include <gui/view_dispatcher.h>
  5. #include <gui/modules/submenu.h>
  6. #include <gui/gui.h>
  7. typedef struct {
  8. Gui* gui;
  9. ViewDispatcher* view_dispatcher;
  10. Submenu* submenu;
  11. FuriHalUsbHidConfig hid_cfg;
  12. } UsbTestApp;
  13. typedef enum {
  14. UsbTestSubmenuIndexEnable,
  15. UsbTestSubmenuIndexDisable,
  16. UsbTestSubmenuIndexRestart,
  17. UsbTestSubmenuIndexVcpSingle,
  18. UsbTestSubmenuIndexVcpDual,
  19. UsbTestSubmenuIndexHid,
  20. UsbTestSubmenuIndexHidWithParams,
  21. UsbTestSubmenuIndexHidU2F,
  22. } SubmenuIndex;
  23. void usb_test_submenu_callback(void* context, uint32_t index) {
  24. furi_assert(context);
  25. UsbTestApp* app = context;
  26. if(index == UsbTestSubmenuIndexEnable) {
  27. furi_hal_usb_enable();
  28. } else if(index == UsbTestSubmenuIndexDisable) {
  29. furi_hal_usb_disable();
  30. } else if(index == UsbTestSubmenuIndexRestart) {
  31. furi_hal_usb_reinit();
  32. } else if(index == UsbTestSubmenuIndexVcpSingle) {
  33. furi_hal_usb_set_config(&usb_cdc_single, NULL);
  34. } else if(index == UsbTestSubmenuIndexVcpDual) {
  35. furi_hal_usb_set_config(&usb_cdc_dual, NULL);
  36. } else if(index == UsbTestSubmenuIndexHid) {
  37. furi_hal_usb_set_config(&usb_hid, NULL);
  38. } else if(index == UsbTestSubmenuIndexHidWithParams) {
  39. app->hid_cfg.vid = 0x1234;
  40. app->hid_cfg.pid = 0xabcd;
  41. strncpy(app->hid_cfg.manuf, "WEN", sizeof(app->hid_cfg.manuf));
  42. strncpy(app->hid_cfg.product, "FLIP", sizeof(app->hid_cfg.product));
  43. furi_hal_usb_set_config(&usb_hid, &app->hid_cfg);
  44. } else if(index == UsbTestSubmenuIndexHidU2F) {
  45. furi_hal_usb_set_config(&usb_hid_u2f, NULL);
  46. }
  47. }
  48. uint32_t usb_test_exit(void* context) {
  49. return VIEW_NONE;
  50. }
  51. UsbTestApp* usb_test_app_alloc() {
  52. UsbTestApp* app = malloc(sizeof(UsbTestApp));
  53. // Gui
  54. app->gui = furi_record_open("gui");
  55. // View dispatcher
  56. app->view_dispatcher = view_dispatcher_alloc();
  57. view_dispatcher_enable_queue(app->view_dispatcher);
  58. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  59. // Views
  60. app->submenu = submenu_alloc();
  61. submenu_add_item(
  62. app->submenu, "Enable", UsbTestSubmenuIndexEnable, usb_test_submenu_callback, app);
  63. submenu_add_item(
  64. app->submenu, "Disable", UsbTestSubmenuIndexDisable, usb_test_submenu_callback, app);
  65. submenu_add_item(
  66. app->submenu, "Restart", UsbTestSubmenuIndexRestart, usb_test_submenu_callback, app);
  67. submenu_add_item(
  68. app->submenu, "Single VCP", UsbTestSubmenuIndexVcpSingle, usb_test_submenu_callback, app);
  69. submenu_add_item(
  70. app->submenu, "Dual VCP", UsbTestSubmenuIndexVcpDual, usb_test_submenu_callback, app);
  71. submenu_add_item(
  72. app->submenu, "HID KB+Mouse", UsbTestSubmenuIndexHid, usb_test_submenu_callback, app);
  73. submenu_add_item(
  74. app->submenu,
  75. "HID KB+Mouse custom ID",
  76. UsbTestSubmenuIndexHidWithParams,
  77. usb_test_submenu_callback,
  78. app);
  79. submenu_add_item(
  80. app->submenu, "HID U2F", UsbTestSubmenuIndexHidU2F, usb_test_submenu_callback, app);
  81. view_set_previous_callback(submenu_get_view(app->submenu), usb_test_exit);
  82. view_dispatcher_add_view(app->view_dispatcher, 0, submenu_get_view(app->submenu));
  83. // Switch to menu
  84. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  85. return app;
  86. }
  87. void usb_test_app_free(UsbTestApp* app) {
  88. furi_assert(app);
  89. // Free views
  90. view_dispatcher_remove_view(app->view_dispatcher, 0);
  91. submenu_free(app->submenu);
  92. view_dispatcher_free(app->view_dispatcher);
  93. // Close gui record
  94. furi_record_close("gui");
  95. app->gui = NULL;
  96. // Free rest
  97. free(app);
  98. }
  99. int32_t usb_test_app(void* p) {
  100. UsbTestApp* app = usb_test_app_alloc();
  101. view_dispatcher_run(app->view_dispatcher);
  102. usb_test_app_free(app);
  103. return 0;
  104. }