Skip to content

Commit

Permalink
Fixed #24 (#25)
Browse files Browse the repository at this point in the history
* Fixed #24
* Small improvement to the USB mode restore code
  • Loading branch information
akopachov authored Nov 10, 2022
1 parent 766a3d6 commit 9dcb6cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
20 changes: 13 additions & 7 deletions services/hid_worker/hid_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ const uint8_t hid_number_keys[10] = {
HID_KEYBOARD_8,
HID_KEYBOARD_9};

static void totp_hid_worker_restore_usb_mode(TotpHidWorkerTypeContext* context) {
if(context->usb_mode_prev != NULL) {
furi_hal_usb_set_config(context->usb_mode_prev, NULL);
context->usb_mode_prev = NULL;
}
}

static void totp_hid_worker_type_code(TotpHidWorkerTypeContext* context) {
FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
context->usb_mode_prev = furi_hal_usb_get_config();
furi_hal_usb_unlock();
furi_check(furi_hal_usb_set_config(&usb_hid, NULL) == true);
uint8_t i = 0;
do {
furi_delay_ms(500);
i++;
} while(!furi_hal_hid_is_connected() && i < 100);
} while(!furi_hal_hid_is_connected() && i < 50);
furi_delay_ms(500);

if(furi_hal_hid_is_connected() &&
furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) {
Expand All @@ -37,14 +45,10 @@ static void totp_hid_worker_type_code(TotpHidWorkerTypeContext* context) {

furi_mutex_release(context->string_sync);

furi_hal_hid_kb_press(HID_KEYBOARD_RETURN);
furi_delay_ms(30);
furi_hal_hid_kb_release(HID_KEYBOARD_RETURN);

furi_delay_ms(100);
}

furi_hal_usb_set_config(usb_mode_prev, NULL);
totp_hid_worker_restore_usb_mode(context);
}

static int32_t totp_hid_worker_callback(void* context) {
Expand Down Expand Up @@ -77,6 +81,7 @@ TotpHidWorkerTypeContext* totp_hid_worker_start() {
furi_check(context != NULL);
context->string_sync = furi_mutex_alloc(FuriMutexTypeNormal);
context->thread = furi_thread_alloc();
context->usb_mode_prev = NULL;
furi_thread_set_name(context->thread, "TOTPHidWorker");
furi_thread_set_stack_size(context->thread, 1024);
furi_thread_set_context(context->thread, context);
Expand All @@ -91,6 +96,7 @@ void totp_hid_worker_stop(TotpHidWorkerTypeContext* context) {
furi_thread_join(context->thread);
furi_thread_free(context->thread);
furi_mutex_free(context->string_sync);
totp_hid_worker_restore_usb_mode(context);
free(context);
}

Expand Down
1 change: 1 addition & 0 deletions services/hid_worker/hid_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ typedef struct {
uint8_t string_length;
FuriThread* thread;
FuriMutex* string_sync;
FuriHalUsbInterface* usb_mode_prev;
} TotpHidWorkerTypeContext;

typedef enum {
Expand Down

0 comments on commit 9dcb6cb

Please sign in to comment.