forked from flipperdevices/flipperzero-firmware
-
-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented "type code in badusb mode"
* Implemented #21
- Loading branch information
Showing
4 changed files
with
161 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include "hid_worker.h" | ||
|
||
const uint8_t hid_number_keys[10] = { | ||
HID_KEYBOARD_0, | ||
HID_KEYBOARD_1, | ||
HID_KEYBOARD_2, | ||
HID_KEYBOARD_3, | ||
HID_KEYBOARD_4, | ||
HID_KEYBOARD_5, | ||
HID_KEYBOARD_6, | ||
HID_KEYBOARD_7, | ||
HID_KEYBOARD_8, | ||
HID_KEYBOARD_9}; | ||
|
||
static void totp_hid_worker_type_code(TotpHidWorkerTypeContext* context) { | ||
FuriHalUsbInterface* 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); | ||
|
||
if(furi_hal_hid_is_connected() && | ||
furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) { | ||
i = 0; | ||
while(i < context->string_length && context->string[i] != 0) { | ||
uint8_t digit = context->string[i] - '0'; | ||
if(digit > 9) break; | ||
uint8_t hid_kb_key = hid_number_keys[digit]; | ||
furi_hal_hid_kb_press(hid_kb_key); | ||
furi_delay_ms(30); | ||
furi_hal_hid_kb_release(hid_kb_key); | ||
i++; | ||
} | ||
|
||
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); | ||
} | ||
|
||
static int32_t totp_hid_worker_callback(void* context) { | ||
ValueMutex context_mutex; | ||
if(!init_mutex(&context_mutex, context, sizeof(TotpHidWorkerTypeContext))) { | ||
return 251; | ||
} | ||
|
||
while(true) { | ||
uint32_t flags = furi_thread_flags_wait( | ||
TotpHidWorkerEvtStop | TotpHidWorkerEvtType, FuriFlagWaitAny, FuriWaitForever); | ||
furi_check((flags & FuriFlagError) == 0); //-V562 | ||
if(flags & TotpHidWorkerEvtStop) break; | ||
|
||
TotpHidWorkerTypeContext* h_context = acquire_mutex_block(&context_mutex); | ||
if(flags & TotpHidWorkerEvtType) { | ||
totp_hid_worker_type_code(h_context); | ||
} | ||
|
||
release_mutex(&context_mutex, h_context); | ||
} | ||
|
||
delete_mutex(&context_mutex); | ||
|
||
return 0; | ||
} | ||
|
||
TotpHidWorkerTypeContext* totp_hid_worker_start() { | ||
TotpHidWorkerTypeContext* context = malloc(sizeof(TotpHidWorkerTypeContext)); | ||
furi_check(context != NULL); | ||
context->string_sync = furi_mutex_alloc(FuriMutexTypeNormal); | ||
context->thread = furi_thread_alloc(); | ||
furi_thread_set_name(context->thread, "TOTPHidWorker"); | ||
furi_thread_set_stack_size(context->thread, 1024); | ||
furi_thread_set_context(context->thread, context); | ||
furi_thread_set_callback(context->thread, totp_hid_worker_callback); | ||
furi_thread_start(context->thread); | ||
return context; | ||
} | ||
|
||
void totp_hid_worker_stop(TotpHidWorkerTypeContext* context) { | ||
furi_assert(context); | ||
furi_thread_flags_set(furi_thread_get_id(context->thread), TotpHidWorkerEvtStop); | ||
furi_thread_join(context->thread); | ||
furi_thread_free(context->thread); | ||
furi_mutex_free(context->string_sync); | ||
free(context); | ||
} | ||
|
||
void totp_hid_worker_notify(TotpHidWorkerTypeContext* context, TotpHidWorkerEvtFlags event) { | ||
furi_assert(context); | ||
furi_thread_flags_set(furi_thread_get_id(context->thread), event); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include <stdlib.h> | ||
#include <furi/furi.h> | ||
#include <furi_hal.h> | ||
|
||
typedef struct { | ||
char* string; | ||
uint8_t string_length; | ||
FuriThread* thread; | ||
FuriMutex* string_sync; | ||
} TotpHidWorkerTypeContext; | ||
|
||
typedef enum { | ||
TotpHidWorkerEvtReserved = (1 << 0), | ||
TotpHidWorkerEvtStop = (1 << 1), | ||
TotpHidWorkerEvtType = (1 << 2) | ||
} TotpHidWorkerEvtFlags; | ||
|
||
TotpHidWorkerTypeContext* totp_hid_worker_start(); | ||
void totp_hid_worker_stop(TotpHidWorkerTypeContext* context); | ||
void totp_hid_worker_notify(TotpHidWorkerTypeContext* context, TotpHidWorkerEvtFlags event); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters