Skip to content

Commit

Permalink
Implemented #106 (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov committed Mar 31, 2023
1 parent 2901da8 commit 16ba603
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 36 deletions.
2 changes: 2 additions & 0 deletions cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ void totp_cli_command_add_docopt_options() {
" - Type <Enter> key at the end of token input automation\r\n");
TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME
" - Type <Tab> key at the end of token input automation\r\n");
TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME
" - Type slower\r\n");
}

void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
Expand Down
25 changes: 14 additions & 11 deletions cli/commands/details/details.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
#include "../../cli_helpers.h"
#include "../../common_command_arguments.h"

#define AUTOMATION_FEATURES_PROPERTY_HEADER "Automation features"
#define TOTP_CLI_PRINTF_AUTOMATION_FEATURE(description, header_printed) \
do { \
TOTP_CLI_PRINTF( \
"| %-20s | %-28.28s |\r\n", \
header_printed ? "" : "Automation features", \
description); \
header_printed = true; \
} while(false)

static void print_automation_features(const TokenInfo* token_info) {
if(token_info->automation_features == TOKEN_AUTOMATION_FEATURE_NONE) {
Expand All @@ -17,19 +24,15 @@ static void print_automation_features(const TokenInfo* token_info) {

bool header_printed = false;
if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
TOTP_CLI_PRINTF(
"| %-20s | %-28.28s |\r\n",
AUTOMATION_FEATURES_PROPERTY_HEADER,
"Type <Enter> key at the end");
header_printed = true;
TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Enter> key at the end", header_printed);
}

if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) {
TOTP_CLI_PRINTF(
"| %-20s | %-28.28s |\r\n",
header_printed ? "" : AUTOMATION_FEATURES_PROPERTY_HEADER,
"Type <Tab> key at the end");
header_printed = true;
TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Tab> key at the end", header_printed);
}

if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER) {
TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type slower", header_printed);
}
}

Expand Down
5 changes: 5 additions & 0 deletions types/token_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const Fur
return true;
}

if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME) == 0) {
token_info->automation_features |= TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER;
return true;
}

if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME) == 0) {
token_info->automation_features = TOKEN_AUTOMATION_FEATURE_NONE;
return true;
Expand Down
12 changes: 9 additions & 3 deletions types/token_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME "none"
#define TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME "enter"
#define TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME "tab"
#define TOTP_TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME "slower"

typedef uint8_t TokenHashAlgo;
typedef uint8_t TokenDigitsCount;
Expand Down Expand Up @@ -61,17 +62,22 @@ enum TokenAutomationFeatures {
/**
* @brief No features enabled
*/
TOKEN_AUTOMATION_FEATURE_NONE = 0b00,
TOKEN_AUTOMATION_FEATURE_NONE = 0b000,

/**
* @brief Press "Enter" key at the end as a part of token input automation
*/
TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END = 0b01,
TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END = 0b001,

/**
* @brief Press "Tab" key at the end as a part of token input automation
*/
TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END = 0b10
TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END = 0b010,

/**
* @brief Press keys slower and wait longer between keystrokes
*/
TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER = 0b100
};

#define TOTP_TOKEN_DIGITS_MAX_COUNT 8
Expand Down
9 changes: 2 additions & 7 deletions workers/bt_type_code/bt_type_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ static inline bool totp_type_code_worker_stop_requested() {
return furi_thread_flags_get() & TotpBtTypeCodeWorkerEventStop;
}

static void totp_type_code_worker_press_key(uint8_t key) {
furi_hal_bt_hid_kb_press(key);
furi_delay_ms(30);
furi_hal_bt_hid_kb_release(key);
}

static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context) {
uint8_t i = 0;
do {
Expand All @@ -26,7 +20,8 @@ static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context

if(context->is_connected && furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) {
totp_type_code_worker_execute_automation(
&totp_type_code_worker_press_key,
&furi_hal_bt_hid_kb_press,
&furi_hal_bt_hid_kb_release,
context->string,
context->string_length,
context->flags);
Expand Down
41 changes: 35 additions & 6 deletions workers/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,35 @@ static const uint8_t hid_number_keys[10] = {
HID_KEYBOARD_8,
HID_KEYBOARD_9};

static uint32_t get_keystroke_delay(TokenAutomationFeature features) {
if(features & TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER) {
return 100;
}

return 30;
}

static uint32_t get_keypress_delay(TokenAutomationFeature features) {
if(features & TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER) {
return 60;
}

return 30;
}

static void totp_type_code_worker_press_key(
uint8_t key,
TOTP_AUTOMATION_KEY_HANDLER key_press_fn,
TOTP_AUTOMATION_KEY_HANDLER key_release_fn,
TokenAutomationFeature features) {
(*key_press_fn)(key);
furi_delay_ms(get_keypress_delay(features));
(*key_release_fn)(key);
}

void totp_type_code_worker_execute_automation(
TOTP_AUTOMATION_PRESS_KEY key_press_fn,
TOTP_AUTOMATION_KEY_HANDLER key_press_fn,
TOTP_AUTOMATION_KEY_HANDLER key_release_fn,
const char* string,
uint8_t string_length,
TokenAutomationFeature features) {
Expand All @@ -26,17 +53,19 @@ void totp_type_code_worker_execute_automation(
uint8_t digit = CONVERT_CHAR_TO_DIGIT(string[i]);
if(digit > 9) break;
uint8_t hid_kb_key = hid_number_keys[digit];
(*key_press_fn)(hid_kb_key);
totp_type_code_worker_press_key(hid_kb_key, key_press_fn, key_release_fn, features);
furi_delay_ms(get_keystroke_delay(features));
i++;
}

if(features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
furi_delay_ms(30);
(*key_press_fn)(HID_KEYBOARD_RETURN);
furi_delay_ms(get_keystroke_delay(features));
totp_type_code_worker_press_key(
HID_KEYBOARD_RETURN, key_press_fn, key_release_fn, features);
}

if(features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) {
furi_delay_ms(30);
(*key_press_fn)(HID_KEYBOARD_TAB);
furi_delay_ms(get_keystroke_delay(features));
totp_type_code_worker_press_key(HID_KEYBOARD_TAB, key_press_fn, key_release_fn, features);
}
}
5 changes: 3 additions & 2 deletions workers/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#include <stdint.h>
#include "../types/token_info.h"

typedef void (*TOTP_AUTOMATION_PRESS_KEY)(uint8_t key);
typedef bool (*TOTP_AUTOMATION_KEY_HANDLER)(uint16_t key);

void totp_type_code_worker_execute_automation(
TOTP_AUTOMATION_PRESS_KEY key_press_fn,
TOTP_AUTOMATION_KEY_HANDLER key_press_fn,
TOTP_AUTOMATION_KEY_HANDLER key_release_fn,
const char* string,
uint8_t string_length,
TokenAutomationFeature features);
9 changes: 2 additions & 7 deletions workers/usb_type_code/usb_type_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ static inline bool totp_type_code_worker_stop_requested() {
return furi_thread_flags_get() & TotpUsbTypeCodeWorkerEventStop;
}

static void totp_type_code_worker_press_key(uint8_t key) {
furi_hal_hid_kb_press(key);
furi_delay_ms(30);
furi_hal_hid_kb_release(key);
}

static void totp_type_code_worker_type_code(TotpUsbTypeCodeWorkerContext* context) {
context->usb_mode_prev = furi_hal_usb_get_config();
furi_hal_usb_unlock();
Expand All @@ -33,7 +27,8 @@ static void totp_type_code_worker_type_code(TotpUsbTypeCodeWorkerContext* contex
if(furi_hal_hid_is_connected() &&
furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) {
totp_type_code_worker_execute_automation(
&totp_type_code_worker_press_key,
&furi_hal_hid_kb_press,
&furi_hal_hid_kb_release,
context->string,
context->string_length,
context->flags);
Expand Down

0 comments on commit 16ba603

Please sign in to comment.