usb_test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. strlcpy(app->hid_cfg.manuf, "WEN", sizeof(app->hid_cfg.manuf));
  42. strlcpy(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. UNUSED(context);
  50. return VIEW_NONE;
  51. }
  52. UsbTestApp* usb_test_app_alloc() {
  53. UsbTestApp* app = malloc(sizeof(UsbTestApp));
  54. // Gui
  55. app->gui = furi_record_open("gui");
  56. // View dispatcher
  57. app->view_dispatcher = view_dispatcher_alloc();
  58. view_dispatcher_enable_queue(app->view_dispatcher);
  59. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  60. // Views
  61. app->submenu = submenu_alloc();
  62. submenu_add_item(
  63. app->submenu, "Enable", UsbTestSubmenuIndexEnable, usb_test_submenu_callback, app);
  64. submenu_add_item(
  65. app->submenu, "Disable", UsbTestSubmenuIndexDisable, usb_test_submenu_callback, app);
  66. submenu_add_item(
  67. app->submenu, "Restart", UsbTestSubmenuIndexRestart, usb_test_submenu_callback, app);
  68. submenu_add_item(
  69. app->submenu, "Single VCP", UsbTestSubmenuIndexVcpSingle, usb_test_submenu_callback, app);
  70. submenu_add_item(
  71. app->submenu, "Dual VCP", UsbTestSubmenuIndexVcpDual, usb_test_submenu_callback, app);
  72. submenu_add_item(
  73. app->submenu, "HID KB+Mouse", UsbTestSubmenuIndexHid, usb_test_submenu_callback, app);
  74. submenu_add_item(
  75. app->submenu,
  76. "HID KB+Mouse custom ID",
  77. UsbTestSubmenuIndexHidWithParams,
  78. usb_test_submenu_callback,
  79. app);
  80. submenu_add_item(
  81. app->submenu, "HID U2F", UsbTestSubmenuIndexHidU2F, usb_test_submenu_callback, app);
  82. view_set_previous_callback(submenu_get_view(app->submenu), usb_test_exit);
  83. view_dispatcher_add_view(app->view_dispatcher, 0, submenu_get_view(app->submenu));
  84. // Switch to menu
  85. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  86. return app;
  87. }
  88. void usb_test_app_free(UsbTestApp* app) {
  89. furi_assert(app);
  90. // Free views
  91. view_dispatcher_remove_view(app->view_dispatcher, 0);
  92. submenu_free(app->submenu);
  93. view_dispatcher_free(app->view_dispatcher);
  94. // Close gui record
  95. furi_record_close("gui");
  96. app->gui = NULL;
  97. // Free rest
  98. free(app);
  99. }
  100. int32_t usb_test_app(void* p) {
  101. UNUSED(p);
  102. UsbTestApp* app = usb_test_app_alloc();
  103. view_dispatcher_run(app->view_dispatcher);
  104. usb_test_app_free(app);
  105. return 0;
  106. }