hid_worker.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "hid_worker.h"
  2. const uint8_t hid_number_keys[10] = {
  3. HID_KEYBOARD_0,
  4. HID_KEYBOARD_1,
  5. HID_KEYBOARD_2,
  6. HID_KEYBOARD_3,
  7. HID_KEYBOARD_4,
  8. HID_KEYBOARD_5,
  9. HID_KEYBOARD_6,
  10. HID_KEYBOARD_7,
  11. HID_KEYBOARD_8,
  12. HID_KEYBOARD_9};
  13. static void totp_hid_worker_type_code(TotpHidWorkerTypeContext* context) {
  14. FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
  15. furi_hal_usb_unlock();
  16. furi_check(furi_hal_usb_set_config(&usb_hid, NULL) == true);
  17. uint8_t i = 0;
  18. do {
  19. furi_delay_ms(500);
  20. i++;
  21. } while(!furi_hal_hid_is_connected() && i < 100);
  22. if(furi_hal_hid_is_connected() &&
  23. furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) {
  24. i = 0;
  25. while(i < context->string_length && context->string[i] != 0) {
  26. uint8_t digit = context->string[i] - '0';
  27. if(digit > 9) break;
  28. uint8_t hid_kb_key = hid_number_keys[digit];
  29. furi_hal_hid_kb_press(hid_kb_key);
  30. furi_delay_ms(30);
  31. furi_hal_hid_kb_release(hid_kb_key);
  32. i++;
  33. }
  34. furi_mutex_release(context->string_sync);
  35. furi_hal_hid_kb_press(HID_KEYBOARD_RETURN);
  36. furi_delay_ms(30);
  37. furi_hal_hid_kb_release(HID_KEYBOARD_RETURN);
  38. furi_delay_ms(100);
  39. }
  40. furi_hal_usb_set_config(usb_mode_prev, NULL);
  41. }
  42. static int32_t totp_hid_worker_callback(void* context) {
  43. ValueMutex context_mutex;
  44. if(!init_mutex(&context_mutex, context, sizeof(TotpHidWorkerTypeContext))) {
  45. return 251;
  46. }
  47. while(true) {
  48. uint32_t flags = furi_thread_flags_wait(
  49. TotpHidWorkerEvtStop | TotpHidWorkerEvtType, FuriFlagWaitAny, FuriWaitForever);
  50. furi_check((flags & FuriFlagError) == 0); //-V562
  51. if(flags & TotpHidWorkerEvtStop) break;
  52. TotpHidWorkerTypeContext* h_context = acquire_mutex_block(&context_mutex);
  53. if(flags & TotpHidWorkerEvtType) {
  54. totp_hid_worker_type_code(h_context);
  55. }
  56. release_mutex(&context_mutex, h_context);
  57. }
  58. delete_mutex(&context_mutex);
  59. return 0;
  60. }
  61. TotpHidWorkerTypeContext* totp_hid_worker_start() {
  62. TotpHidWorkerTypeContext* context = malloc(sizeof(TotpHidWorkerTypeContext));
  63. furi_check(context != NULL);
  64. context->string_sync = furi_mutex_alloc(FuriMutexTypeNormal);
  65. context->thread = furi_thread_alloc();
  66. furi_thread_set_name(context->thread, "TOTPHidWorker");
  67. furi_thread_set_stack_size(context->thread, 1024);
  68. furi_thread_set_context(context->thread, context);
  69. furi_thread_set_callback(context->thread, totp_hid_worker_callback);
  70. furi_thread_start(context->thread);
  71. return context;
  72. }
  73. void totp_hid_worker_stop(TotpHidWorkerTypeContext* context) {
  74. furi_assert(context);
  75. furi_thread_flags_set(furi_thread_get_id(context->thread), TotpHidWorkerEvtStop);
  76. furi_thread_join(context->thread);
  77. furi_thread_free(context->thread);
  78. furi_mutex_free(context->string_sync);
  79. free(context);
  80. }
  81. void totp_hid_worker_notify(TotpHidWorkerTypeContext* context, TotpHidWorkerEvtFlags event) {
  82. furi_assert(context);
  83. furi_thread_flags_set(furi_thread_get_id(context->thread), event);
  84. }