Skip to content

Commit

Permalink
[FL-2379] BadUSB and furi_hal_usb fixes flipperdevices#1057
Browse files Browse the repository at this point in the history
  • Loading branch information
nminaylov authored Mar 25, 2022
1 parent 969d1ef commit 7c4b0f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
4 changes: 4 additions & 0 deletions applications/bad_usb/scenes/bad_usb_scene_file_select.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../bad_usb_app_i.h"
#include "furi_hal_power.h"
#include "furi_hal_usb.h"

static bool bad_usb_file_select(BadUsbApp* bad_usb) {
furi_assert(bad_usb);
Expand All @@ -18,9 +19,12 @@ static bool bad_usb_file_select(BadUsbApp* bad_usb) {
void bad_usb_scene_file_select_on_enter(void* context) {
BadUsbApp* bad_usb = context;

furi_hal_usb_disable();

if(bad_usb_file_select(bad_usb)) {
scene_manager_next_scene(bad_usb->scene_manager, BadUsbSceneWork);
} else {
furi_hal_usb_enable();
//scene_manager_previous_scene(bad_usb->scene_manager);
view_dispatcher_stop(bad_usb->view_dispatcher);
}
Expand Down
36 changes: 16 additions & 20 deletions firmware/targets/f7/furi_hal/furi_hal_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

typedef struct {
FuriThread* thread;
osTimerId_t tmr;
bool enabled;
bool connected;
bool mode_lock;
Expand Down Expand Up @@ -53,8 +52,6 @@ static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep);
static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep);
static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep);

static void furi_hal_usb_tmr_cb(void* context);

/* Low-level init */
void furi_hal_usb_init(void) {
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
Expand Down Expand Up @@ -139,11 +136,6 @@ void furi_hal_usb_reinit() {
osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReinit);
}

static void furi_hal_usb_tmr_cb(void* context) {
furi_assert(usb.thread);
osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChangeStart);
}

/* Get device / configuration descriptors */
static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) {
const uint8_t dtype = req->wValue >> 8;
Expand Down Expand Up @@ -226,10 +218,10 @@ static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
}

static int32_t furi_hal_usb_thread(void* context) {
usb.tmr = osTimerNew(furi_hal_usb_tmr_cb, osTimerOnce, NULL, NULL);

bool usb_request_pending = false;
uint8_t usb_wait_time = 0;
FuriHalUsbInterface* if_new = NULL;
FuriHalUsbInterface* if_ctx_new = NULL;

if(usb.if_next != NULL) {
osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange);
Expand All @@ -240,14 +232,15 @@ static int32_t furi_hal_usb_thread(void* context) {
if((flags & osFlagsError) == 0) {
if(flags & EventModeChange) {
if(usb.if_next != usb.if_cur) {
if_new = usb.if_next;
if_ctx_new = usb.if_ctx;
if(usb.enabled) { // Disable current interface
susp_evt(&udev, 0, 0);
usbd_connect(&udev, false);
usb.enabled = false;
osTimerStart(usb.tmr, USB_RECONNECT_DELAY);
} else {
flags |= EventModeChangeStart;
osDelay(USB_RECONNECT_DELAY);
}
flags |= EventModeChangeStart;
}
}
if(flags & EventReinit) {
Expand All @@ -261,19 +254,20 @@ static int32_t furi_hal_usb_thread(void* context) {
usbd_enable(&udev, false);
usbd_enable(&udev, true);

usb.if_next = usb.if_cur;
osTimerStart(usb.tmr, USB_RECONNECT_DELAY);
if_new = usb.if_cur;
osDelay(USB_RECONNECT_DELAY);
flags |= EventModeChangeStart;
}
if(flags & EventModeChangeStart) { // Second stage of mode change process
if(usb.if_cur != NULL) {
usb.if_cur->deinit(&udev);
}
if(usb.if_next != NULL) {
usb.if_next->init(&udev, usb.if_next, usb.if_ctx);
if(if_new != NULL) {
if_new->init(&udev, if_new, if_ctx_new);
usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
FURI_LOG_I(TAG, "USB Mode change done");
usb.enabled = true;
usb.if_cur = usb.if_next;
usb.if_cur = if_new;
}
}
if(flags & EventEnable) {
Expand All @@ -293,8 +287,10 @@ static int32_t furi_hal_usb_thread(void* context) {
}
}
if(flags & EventReset) {
usb_request_pending = true;
usb_wait_time = 0;
if(usb.enabled) {
usb_request_pending = true;
usb_wait_time = 0;
}
}
if(flags & EventRequest) {
usb_request_pending = false;
Expand Down

0 comments on commit 7c4b0f5

Please sign in to comment.