MX 1 год назад
Родитель
Сommit
f1e21a5331
4 измененных файлов с 109 добавлено и 64 удалено
  1. 1 0
      application.fam
  2. 27 64
      workers/bt_type_code/bt_type_code.c
  3. 59 0
      workers/type_code_common.c
  4. 22 0
      workers/type_code_common.h

+ 1 - 0
application.fam

@@ -15,6 +15,7 @@ App(
     fap_icon_assets="images",
     fap_icon="totp_10px.png",
     fap_file_assets="assets",
+    fap_libs=["ble_profile"],
     fap_private_libs=[
         Lib(
             name="base32",

+ 27 - 64
workers/bt_type_code/bt_type_code.c

@@ -1,6 +1,6 @@
 #include "bt_type_code.h"
 #include <furi_hal_bt.h>
-#include <furi_hal_bt_hid.h>
+#include <extra_profiles/hid_profile.h>
 #include <furi_hal_version.h>
 #include <furi/core/thread.h>
 #include <furi/core/mutex.h>
@@ -14,11 +14,6 @@
 #include "../../config/app/config.h"
 #include "../../services/config/constants.h"
 
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-#define TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN FURI_HAL_BT_ADV_NAME_LENGTH
-#define TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN GAP_MAC_ADDR_SIZE
-#endif
-
 #define HID_BT_KEYS_STORAGE_PATH CONFIG_FILE_DIRECTORY_PATH "/.bt_hid.keys"
 
 struct TotpBtTypeCodeWorkerContext {
@@ -30,10 +25,7 @@ struct TotpBtTypeCodeWorkerContext {
     Bt* bt;
     bool is_advertising;
     bool is_connected;
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-    char previous_bt_name[TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN];
-    uint8_t previous_bt_mac[TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN];
-#endif
+    FuriHalBleProfileBase* ble_hid_profile;
     AutomationKeyboardLayout keyboard_layout;
     uint16_t initial_delay;
 };
@@ -42,25 +34,23 @@ static inline bool totp_type_code_worker_stop_requested() {
     return furi_thread_flags_get() & TotpBtTypeCodeWorkerEventStop;
 }
 
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-static void totp_type_code_worker_bt_set_app_mac(uint8_t* mac) {
-    uint8_t max_i;
-    size_t uid_size = furi_hal_version_uid_size();
-    if(uid_size < TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN) {
-        max_i = uid_size;
-    } else {
-        max_i = TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN;
-    }
+// static void totp_type_code_worker_bt_set_app_mac(uint8_t* mac) {
+//     uint8_t max_i;
+//     size_t uid_size = furi_hal_version_uid_size();
+//     if(uid_size < TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN) {
+//         max_i = uid_size;
+//     } else {
+//         max_i = TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN;
+//     }
 
-    const uint8_t* uid = (const uint8_t*)UID64_BASE; //-V566
-    memcpy(mac, uid, max_i);
-    for(uint8_t i = max_i; i < TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN; i++) {
-        mac[i] = 0;
-    }
+//     const uint8_t* uid = (const uint8_t*)UID64_BASE; //-V566
+//     memcpy(mac, uid, max_i);
+//     for(uint8_t i = max_i; i < TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN; i++) {
+//         mac[i] = 0;
+//     }
 
-    mac[0] = 0b10;
-}
-#endif
+//     mac[0] = 0b10;
+// }
 
 static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context) {
     uint8_t i = 0;
@@ -71,9 +61,10 @@ static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context
 
     if(context->is_connected &&
        furi_mutex_acquire(context->code_buffer_sync, 500) == FuriStatusOk) {
-        totp_type_code_worker_execute_automation(
-            &furi_hal_bt_hid_kb_press,
-            &furi_hal_bt_hid_kb_release,
+        totp_type_code_worker_execute_automation_ctx(
+            (TOTP_AUTOMATION_KEY_HANDLER_CTX)&ble_profile_hid_kb_press,
+            (TOTP_AUTOMATION_KEY_HANDLER_CTX)&ble_profile_hid_kb_release,
+            context->ble_hid_profile,
             context->code_buffer,
             context->code_buffer_size,
             context->flags,
@@ -166,37 +157,17 @@ TotpBtTypeCodeWorkerContext* totp_bt_type_code_worker_init() {
     context->is_advertising = false;
     context->is_connected = false;
     bt_disconnect(context->bt);
-    furi_hal_bt_reinit();
     furi_delay_ms(200);
     bt_keys_storage_set_storage_path(context->bt, HID_BT_KEYS_STORAGE_PATH);
 
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-    memcpy(
-        &context->previous_bt_name[0],
-        furi_hal_bt_get_profile_adv_name(FuriHalBtProfileHidKeyboard),
-        TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN);
-    memcpy(
-        &context->previous_bt_mac[0],
-        furi_hal_bt_get_profile_mac_addr(FuriHalBtProfileHidKeyboard),
-        TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN);
-    char new_name[TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN];
-    snprintf(new_name, sizeof(new_name), "%s TOTP Auth", furi_hal_version_get_name_ptr());
-    uint8_t new_bt_mac[TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN];
-    totp_type_code_worker_bt_set_app_mac(new_bt_mac);
-    furi_hal_bt_set_profile_adv_name(FuriHalBtProfileHidKeyboard, new_name);
-    furi_hal_bt_set_profile_mac_addr(FuriHalBtProfileHidKeyboard, new_bt_mac);
-#endif
-
-    if(!bt_set_profile(context->bt, BtProfileHidKeyboard)) {
-        FURI_LOG_E(LOGGING_TAG, "Failed to switch BT to keyboard HID profile");
-    }
+    BleProfileHidParams params = {
+        .device_name_prefix = "TOTP",
+    };
+    context->ble_hid_profile = bt_profile_start(context->bt, ble_profile_hid, &params);
+    furi_check(context->ble_hid_profile);
 
     furi_hal_bt_start_advertising();
 
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-    bt_enable_peer_key_update(context->bt);
-#endif
-
     context->is_advertising = true;
     bt_set_status_changed_callback(context->bt, connection_status_changed_callback, context);
 
@@ -212,7 +183,6 @@ void totp_bt_type_code_worker_free(TotpBtTypeCodeWorkerContext* context) {
 
     bt_set_status_changed_callback(context->bt, NULL, NULL);
 
-    furi_hal_bt_stop_advertising();
     context->is_advertising = false;
     context->is_connected = false;
 
@@ -220,14 +190,7 @@ void totp_bt_type_code_worker_free(TotpBtTypeCodeWorkerContext* context) {
     furi_delay_ms(200);
     bt_keys_storage_set_default_path(context->bt);
 
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-    furi_hal_bt_set_profile_adv_name(FuriHalBtProfileHidKeyboard, context->previous_bt_name);
-    furi_hal_bt_set_profile_mac_addr(FuriHalBtProfileHidKeyboard, context->previous_bt_mac);
-#endif
-
-    if(!bt_set_profile(context->bt, BtProfileSerial)) {
-        FURI_LOG_E(LOGGING_TAG, "Failed to switch BT to Serial profile");
-    }
+    furi_check(bt_profile_restore_default(context->bt));
     furi_record_close(RECORD_BT);
     context->bt = NULL;
 

+ 59 - 0
workers/type_code_common.c

@@ -75,3 +75,62 @@ void totp_type_code_worker_execute_automation(
         totp_type_code_worker_press_key(HID_KEYBOARD_TAB, key_press_fn, key_release_fn, features);
     }
 }
+
+static void totp_type_code_worker_press_key_ctx(
+    uint16_t key,
+    TOTP_AUTOMATION_KEY_HANDLER_CTX key_press_fn,
+    TOTP_AUTOMATION_KEY_HANDLER_CTX key_release_fn,
+    void* ctx,
+    TokenAutomationFeature features) {
+    (*key_press_fn)(ctx, key);
+    furi_delay_ms(get_keypress_delay(features));
+    (*key_release_fn)(ctx, key);
+}
+
+void totp_type_code_worker_execute_automation_ctx(
+    TOTP_AUTOMATION_KEY_HANDLER_CTX key_press_fn,
+    TOTP_AUTOMATION_KEY_HANDLER_CTX key_release_fn,
+    void* ctx,
+    const char* code_buffer,
+    uint8_t code_buffer_size,
+    TokenAutomationFeature features,
+    AutomationKeyboardLayout keyboard_layout,
+    uint16_t initial_delay) {
+    uint16_t keyboard_layout_dict[TOTP_KB_LAYOUT_DATA_LENGTH];
+    if(!totp_kb_layout_provider_get_layout_data(keyboard_layout, &keyboard_layout_dict[0])) {
+        return;
+    }
+
+    furi_delay_ms(initial_delay);
+
+    uint32_t keystroke_delay = get_keystroke_delay(features);
+
+    char cb_char;
+    uint8_t i = 0;
+    while(i < code_buffer_size && (cb_char = code_buffer[i]) != 0) {
+        uint8_t char_index = CONVERT_CHAR_TO_DIGIT(cb_char);
+        if(char_index > 9) {
+            char_index = cb_char - 'A' + 10;
+        }
+
+        if(char_index >= TOTP_KB_LAYOUT_DATA_LENGTH) break;
+
+        uint16_t hid_kb_key = keyboard_layout_dict[char_index];
+        totp_type_code_worker_press_key_ctx(
+            hid_kb_key, key_press_fn, key_release_fn, ctx, features);
+        furi_delay_ms(keystroke_delay);
+        i++;
+    }
+
+    if(features & TokenAutomationFeatureEnterAtTheEnd) {
+        furi_delay_ms(keystroke_delay);
+        totp_type_code_worker_press_key_ctx(
+            HID_KEYBOARD_RETURN, key_press_fn, key_release_fn, ctx, features);
+    }
+
+    if(features & TokenAutomationFeatureTabAtTheEnd) {
+        furi_delay_ms(keystroke_delay);
+        totp_type_code_worker_press_key_ctx(
+            HID_KEYBOARD_TAB, key_press_fn, key_release_fn, ctx, features);
+    }
+}

+ 22 - 0
workers/type_code_common.h

@@ -4,6 +4,7 @@
 #include "../types/automation_kb_layout.h"
 
 typedef bool (*TOTP_AUTOMATION_KEY_HANDLER)(uint16_t key);
+typedef bool (*TOTP_AUTOMATION_KEY_HANDLER_CTX)(void* ctx, uint16_t key);
 
 /**
  * @brief Executes token input automation using given key press\release handlers
@@ -23,3 +24,24 @@ void totp_type_code_worker_execute_automation(
     TokenAutomationFeature features,
     AutomationKeyboardLayout keyboard_layout,
     uint16_t initial_delay);
+
+/**
+ * @brief Executes token input automation using given key press\release handlers
+ * @param key_press_fn key press handler
+ * @param key_release_fn key release handler
+ * @param ctx user data context
+ * @param code_buffer code buffer to be typed
+ * @param code_buffer_size code buffer size
+ * @param features automation features
+ * @param keyboard_layout keyboard layout to be used
+ * @param initial_delay initial delay before starting automation
+ */
+void totp_type_code_worker_execute_automation_ctx(
+    TOTP_AUTOMATION_KEY_HANDLER_CTX key_press_fn,
+    TOTP_AUTOMATION_KEY_HANDLER_CTX key_release_fn,
+    void* ctx,
+    const char* code_buffer,
+    uint8_t code_buffer_size,
+    TokenAutomationFeature features,
+    AutomationKeyboardLayout keyboard_layout,
+    uint16_t initial_delay);