Skip to content

Commit

Permalink
[FL-3113] BadUSB: disable CDC mode, USB mode switch fix (flipperdevic…
Browse files Browse the repository at this point in the history
  • Loading branch information
nminaylov authored Feb 13, 2023
1 parent d0c6c34 commit bc06d40
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
8 changes: 8 additions & 0 deletions applications/main/bad_usb/bad_usb_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@ BadUsbApp* bad_usb_app_alloc(char* arg) {

if(furi_hal_usb_is_locked()) {
app->error = BadUsbAppErrorCloseRpc;
app->usb_if_prev = NULL;
scene_manager_next_scene(app->scene_manager, BadUsbSceneError);
} else {
app->usb_if_prev = furi_hal_usb_get_config();
furi_check(furi_hal_usb_set_config(NULL, NULL));

if(!furi_string_empty(app->file_path)) {
app->bad_usb_script = bad_usb_script_open(app->file_path);
bad_usb_script_set_keyboard_layout(app->bad_usb_script, app->keyboard_layout);
Expand All @@ -138,6 +142,10 @@ void bad_usb_app_free(BadUsbApp* app) {
app->bad_usb_script = NULL;
}

if(app->usb_if_prev) {
furi_check(furi_hal_usb_set_config(app->usb_if_prev, NULL));
}

// Views
view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewWork);
bad_usb_free(app->bad_usb_view);
Expand Down
3 changes: 3 additions & 0 deletions applications/main/bad_usb/bad_usb_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <gui/modules/variable_item_list.h>
#include <gui/modules/widget.h>
#include "views/bad_usb_view.h"
#include <furi_hal_usb.h>

#define BAD_USB_APP_BASE_FOLDER ANY_PATH("badusb")
#define BAD_USB_APP_PATH_LAYOUT_FOLDER BAD_USB_APP_BASE_FOLDER "/assets/layouts"
Expand All @@ -39,6 +40,8 @@ struct BadUsbApp {
FuriString* keyboard_layout;
BadUsb* bad_usb_view;
BadUsbScript* bad_usb_script;

FuriHalUsbInterface* usb_if_prev;
};

typedef enum {
Expand Down
4 changes: 0 additions & 4 deletions applications/main/bad_usb/bad_usb_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,6 @@ static int32_t bad_usb_worker(void* context) {
BadUsbWorkerState worker_state = BadUsbStateInit;
int32_t delay_val = 0;

FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();

FURI_LOG_I(WORKER_TAG, "Init");
File* script_file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
bad_usb->line = furi_string_alloc();
Expand Down Expand Up @@ -642,8 +640,6 @@ static int32_t bad_usb_worker(void* context) {

furi_hal_hid_set_state_callback(NULL, NULL);

furi_hal_usb_set_config(usb_mode_prev, NULL);

storage_file_close(script_file);
storage_file_free(script_file);
furi_string_free(bad_usb->line);
Expand Down
2 changes: 1 addition & 1 deletion firmware/targets/f7/furi_hal/furi_hal_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ static void usb_process_mode_start(FuriHalUsbInterface* interface, void* context
}

static void usb_process_mode_change(FuriHalUsbInterface* interface, void* context) {
if(interface != usb.interface) {
if((interface != usb.interface) || (context != usb.interface_context)) {
if(usb.enabled) {
// Disable current interface
susp_evt(&udev, 0, 0);
Expand Down

0 comments on commit bc06d40

Please sign in to comment.