[FL-2379] BadUSB and furi_hal_usb fixes #1057
This commit is contained in:
		
							parent
							
								
									969d1efb6c
								
							
						
					
					
						commit
						7c4b0f534f
					
				| @ -1,5 +1,6 @@ | |||||||
| #include "../bad_usb_app_i.h" | #include "../bad_usb_app_i.h" | ||||||
| #include "furi_hal_power.h" | #include "furi_hal_power.h" | ||||||
|  | #include "furi_hal_usb.h" | ||||||
| 
 | 
 | ||||||
| static bool bad_usb_file_select(BadUsbApp* bad_usb) { | static bool bad_usb_file_select(BadUsbApp* bad_usb) { | ||||||
|     furi_assert(bad_usb); |     furi_assert(bad_usb); | ||||||
| @ -18,9 +19,12 @@ static bool bad_usb_file_select(BadUsbApp* bad_usb) { | |||||||
| void bad_usb_scene_file_select_on_enter(void* context) { | void bad_usb_scene_file_select_on_enter(void* context) { | ||||||
|     BadUsbApp* bad_usb = context; |     BadUsbApp* bad_usb = context; | ||||||
| 
 | 
 | ||||||
|  |     furi_hal_usb_disable(); | ||||||
|  | 
 | ||||||
|     if(bad_usb_file_select(bad_usb)) { |     if(bad_usb_file_select(bad_usb)) { | ||||||
|         scene_manager_next_scene(bad_usb->scene_manager, BadUsbSceneWork); |         scene_manager_next_scene(bad_usb->scene_manager, BadUsbSceneWork); | ||||||
|     } else { |     } else { | ||||||
|  |         furi_hal_usb_enable(); | ||||||
|         //scene_manager_previous_scene(bad_usb->scene_manager);
 |         //scene_manager_previous_scene(bad_usb->scene_manager);
 | ||||||
|         view_dispatcher_stop(bad_usb->view_dispatcher); |         view_dispatcher_stop(bad_usb->view_dispatcher); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -13,7 +13,6 @@ | |||||||
| 
 | 
 | ||||||
| typedef struct { | typedef struct { | ||||||
|     FuriThread* thread; |     FuriThread* thread; | ||||||
|     osTimerId_t tmr; |  | ||||||
|     bool enabled; |     bool enabled; | ||||||
|     bool connected; |     bool connected; | ||||||
|     bool mode_lock; |     bool mode_lock; | ||||||
| @ -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 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 wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep); | ||||||
| 
 | 
 | ||||||
| static void furi_hal_usb_tmr_cb(void* context); |  | ||||||
| 
 |  | ||||||
| /* Low-level init */ | /* Low-level init */ | ||||||
| void furi_hal_usb_init(void) { | void furi_hal_usb_init(void) { | ||||||
|     LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; |     LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; | ||||||
| @ -139,11 +136,6 @@ void furi_hal_usb_reinit() { | |||||||
|     osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReinit); |     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 */ | /* Get device / configuration descriptors */ | ||||||
| static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) { | static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) { | ||||||
|     const uint8_t dtype = req->wValue >> 8; |     const uint8_t dtype = req->wValue >> 8; | ||||||
| @ -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) { | 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; |     bool usb_request_pending = false; | ||||||
|     uint8_t usb_wait_time = 0; |     uint8_t usb_wait_time = 0; | ||||||
|  |     FuriHalUsbInterface* if_new = NULL; | ||||||
|  |     FuriHalUsbInterface* if_ctx_new = NULL; | ||||||
| 
 | 
 | ||||||
|     if(usb.if_next != NULL) { |     if(usb.if_next != NULL) { | ||||||
|         osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange); |         osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange); | ||||||
| @ -240,14 +232,15 @@ static int32_t furi_hal_usb_thread(void* context) { | |||||||
|         if((flags & osFlagsError) == 0) { |         if((flags & osFlagsError) == 0) { | ||||||
|             if(flags & EventModeChange) { |             if(flags & EventModeChange) { | ||||||
|                 if(usb.if_next != usb.if_cur) { |                 if(usb.if_next != usb.if_cur) { | ||||||
|  |                     if_new = usb.if_next; | ||||||
|  |                     if_ctx_new = usb.if_ctx; | ||||||
|                     if(usb.enabled) { // Disable current interface
 |                     if(usb.enabled) { // Disable current interface
 | ||||||
|                         susp_evt(&udev, 0, 0); |                         susp_evt(&udev, 0, 0); | ||||||
|                         usbd_connect(&udev, false); |                         usbd_connect(&udev, false); | ||||||
|                         usb.enabled = false; |                         usb.enabled = false; | ||||||
|                         osTimerStart(usb.tmr, USB_RECONNECT_DELAY); |                         osDelay(USB_RECONNECT_DELAY); | ||||||
|                     } else { |  | ||||||
|                         flags |= EventModeChangeStart; |  | ||||||
|                     } |                     } | ||||||
|  |                     flags |= EventModeChangeStart; | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             if(flags & EventReinit) { |             if(flags & EventReinit) { | ||||||
| @ -261,19 +254,20 @@ static int32_t furi_hal_usb_thread(void* context) { | |||||||
|                 usbd_enable(&udev, false); |                 usbd_enable(&udev, false); | ||||||
|                 usbd_enable(&udev, true); |                 usbd_enable(&udev, true); | ||||||
| 
 | 
 | ||||||
|                 usb.if_next = usb.if_cur; |                 if_new = usb.if_cur; | ||||||
|                 osTimerStart(usb.tmr, USB_RECONNECT_DELAY); |                 osDelay(USB_RECONNECT_DELAY); | ||||||
|  |                 flags |= EventModeChangeStart; | ||||||
|             } |             } | ||||||
|             if(flags & EventModeChangeStart) { // Second stage of mode change process
 |             if(flags & EventModeChangeStart) { // Second stage of mode change process
 | ||||||
|                 if(usb.if_cur != NULL) { |                 if(usb.if_cur != NULL) { | ||||||
|                     usb.if_cur->deinit(&udev); |                     usb.if_cur->deinit(&udev); | ||||||
|                 } |                 } | ||||||
|                 if(usb.if_next != NULL) { |                 if(if_new != NULL) { | ||||||
|                     usb.if_next->init(&udev, usb.if_next, usb.if_ctx); |                     if_new->init(&udev, if_new, if_ctx_new); | ||||||
|                     usbd_reg_event(&udev, usbd_evt_reset, reset_evt); |                     usbd_reg_event(&udev, usbd_evt_reset, reset_evt); | ||||||
|                     FURI_LOG_I(TAG, "USB Mode change done"); |                     FURI_LOG_I(TAG, "USB Mode change done"); | ||||||
|                     usb.enabled = true; |                     usb.enabled = true; | ||||||
|                     usb.if_cur = usb.if_next; |                     usb.if_cur = if_new; | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             if(flags & EventEnable) { |             if(flags & EventEnable) { | ||||||
| @ -293,9 +287,11 @@ static int32_t furi_hal_usb_thread(void* context) { | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             if(flags & EventReset) { |             if(flags & EventReset) { | ||||||
|  |                 if(usb.enabled) { | ||||||
|                     usb_request_pending = true; |                     usb_request_pending = true; | ||||||
|                     usb_wait_time = 0; |                     usb_wait_time = 0; | ||||||
|                 } |                 } | ||||||
|  |             } | ||||||
|             if(flags & EventRequest) { |             if(flags & EventRequest) { | ||||||
|                 usb_request_pending = false; |                 usb_request_pending = false; | ||||||
|             } |             } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Nikolay Minaylov
						Nikolay Minaylov