Welcome to mirror list, hosted at ThFree Co, Russian Federation.

usb_test.c « debug_tools « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ed86c37a88ac2f225bb0ee819f76fe6b735bcd78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <furi.h>
#include <furi_hal.h>

#include <gui/view.h>
#include <gui/view_dispatcher.h>
#include <gui/modules/submenu.h>
#include <gui/gui.h>

typedef struct {
    Gui* gui;
    ViewDispatcher* view_dispatcher;
    Submenu* submenu;
    FuriHalUsbHidConfig hid_cfg;
} UsbTestApp;

typedef enum {
    UsbTestSubmenuIndexEnable,
    UsbTestSubmenuIndexDisable,
    UsbTestSubmenuIndexRestart,
    UsbTestSubmenuIndexVcpSingle,
    UsbTestSubmenuIndexVcpDual,
    UsbTestSubmenuIndexHid,
    UsbTestSubmenuIndexHidWithParams,
    UsbTestSubmenuIndexHidU2F,
} SubmenuIndex;

void usb_test_submenu_callback(void* context, uint32_t index) {
    furi_assert(context);
    UsbTestApp* app = context;
    if(index == UsbTestSubmenuIndexEnable) {
        furi_hal_usb_enable();
    } else if(index == UsbTestSubmenuIndexDisable) {
        furi_hal_usb_disable();
    } else if(index == UsbTestSubmenuIndexRestart) {
        furi_hal_usb_reinit();
    } else if(index == UsbTestSubmenuIndexVcpSingle) {
        furi_hal_usb_set_config(&usb_cdc_single, NULL);
    } else if(index == UsbTestSubmenuIndexVcpDual) {
        furi_hal_usb_set_config(&usb_cdc_dual, NULL);
    } else if(index == UsbTestSubmenuIndexHid) {
        furi_hal_usb_set_config(&usb_hid, NULL);
    } else if(index == UsbTestSubmenuIndexHidWithParams) {
        app->hid_cfg.vid = 0x1234;
        app->hid_cfg.pid = 0xabcd;
        strlcpy(app->hid_cfg.manuf, "WEN", sizeof(app->hid_cfg.manuf));
        strlcpy(app->hid_cfg.product, "FLIP", sizeof(app->hid_cfg.product));
        furi_hal_usb_set_config(&usb_hid, &app->hid_cfg);
    } else if(index == UsbTestSubmenuIndexHidU2F) {
        furi_hal_usb_set_config(&usb_hid_u2f, NULL);
    }
}

uint32_t usb_test_exit(void* context) {
    UNUSED(context);
    return VIEW_NONE;
}

UsbTestApp* usb_test_app_alloc() {
    UsbTestApp* app = malloc(sizeof(UsbTestApp));

    // Gui
    app->gui = furi_record_open(RECORD_GUI);

    // View dispatcher
    app->view_dispatcher = view_dispatcher_alloc();
    view_dispatcher_enable_queue(app->view_dispatcher);
    view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);

    // Views
    app->submenu = submenu_alloc();
    submenu_add_item(
        app->submenu, "Enable", UsbTestSubmenuIndexEnable, usb_test_submenu_callback, app);
    submenu_add_item(
        app->submenu, "Disable", UsbTestSubmenuIndexDisable, usb_test_submenu_callback, app);
    submenu_add_item(
        app->submenu, "Restart", UsbTestSubmenuIndexRestart, usb_test_submenu_callback, app);
    submenu_add_item(
        app->submenu, "Single VCP", UsbTestSubmenuIndexVcpSingle, usb_test_submenu_callback, app);
    submenu_add_item(
        app->submenu, "Dual VCP", UsbTestSubmenuIndexVcpDual, usb_test_submenu_callback, app);
    submenu_add_item(
        app->submenu, "HID KB+Mouse", UsbTestSubmenuIndexHid, usb_test_submenu_callback, app);
    submenu_add_item(
        app->submenu,
        "HID KB+Mouse custom ID",
        UsbTestSubmenuIndexHidWithParams,
        usb_test_submenu_callback,
        app);
    submenu_add_item(
        app->submenu, "HID U2F", UsbTestSubmenuIndexHidU2F, usb_test_submenu_callback, app);
    view_set_previous_callback(submenu_get_view(app->submenu), usb_test_exit);
    view_dispatcher_add_view(app->view_dispatcher, 0, submenu_get_view(app->submenu));

    // Switch to menu
    view_dispatcher_switch_to_view(app->view_dispatcher, 0);

    return app;
}

void usb_test_app_free(UsbTestApp* app) {
    furi_assert(app);

    // Free views
    view_dispatcher_remove_view(app->view_dispatcher, 0);
    submenu_free(app->submenu);
    view_dispatcher_free(app->view_dispatcher);

    // Close gui record
    furi_record_close(RECORD_GUI);
    app->gui = NULL;

    // Free rest
    free(app);
}

int32_t usb_test_app(void* p) {
    UNUSED(p);
    UsbTestApp* app = usb_test_app_alloc();

    view_dispatcher_run(app->view_dispatcher);

    usb_test_app_free(app);
    return 0;
}