[FL-2315] USB Mode switch lock (#1036)

* usb mode switch lock
* lock_mutex removed
* Wait for session termination in rpc_cli, lock badusb and u2f if rpc session is opened

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
Nikolay Minaylov
2022-03-24 18:45:03 +03:00
committed by GitHub
parent eafeefb843
commit 38e92cf789
20 changed files with 216 additions and 43 deletions

View File

@@ -16,6 +16,7 @@ typedef struct {
osTimerId_t tmr;
bool enabled;
bool connected;
bool mode_lock;
FuriHalUsbInterface* if_cur;
FuriHalUsbInterface* if_next;
void* if_ctx;
@@ -89,27 +90,47 @@ void furi_hal_usb_init(void) {
FURI_LOG_I(TAG, "Init OK");
}
void furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
if(usb.mode_lock) {
return false;
}
usb.if_next = new_if;
usb.if_ctx = ctx;
if(usb.thread == NULL) {
// Service thread hasn't started yet, so just save interface mode
return;
return true;
}
furi_assert(usb.thread);
osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange);
return true;
}
FuriHalUsbInterface* furi_hal_usb_get_config() {
return usb.if_cur;
}
void furi_hal_usb_lock() {
FURI_LOG_I(TAG, "Mode lock");
usb.mode_lock = true;
}
void furi_hal_usb_unlock() {
FURI_LOG_I(TAG, "Mode unlock");
usb.mode_lock = false;
}
bool furi_hal_usb_is_locked() {
return usb.mode_lock;
}
void furi_hal_usb_disable() {
furi_assert(usb.thread);
osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventDisable);
}
void furi_hal_usb_enable() {
furi_assert(usb.thread);
osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventEnable);
}

View File

@@ -42,8 +42,9 @@ void furi_hal_usb_init();
*
* @param mode new USB device mode
* @param ctx context passed to device mode init function
* @return true - mode switch started, false - mode switch is locked
*/
void furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx);
bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx);
/** Get USB device configuration
*
@@ -51,6 +52,20 @@ void furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx);
*/
FuriHalUsbInterface* furi_hal_usb_get_config();
/** Lock USB device mode switch
*/
void furi_hal_usb_lock();
/** Unlock USB device mode switch
*/
void furi_hal_usb_unlock();
/** Check if USB device mode switch locked
*
* @return lock state
*/
bool furi_hal_usb_is_locked();
/** Disable USB device
*/
void furi_hal_usb_disable();