Selaa lähdekoodia

Revert "Totp: BLE Refactor"

This reverts commit d9f7c15535d42772bb72da31bfb5fb1d8c220e73.
Willy-JL 1 vuosi sitten
vanhempi
commit
76cc292df3
3 muutettua tiedostoa jossa 79 lisäystä ja 24 poistoa
  1. 0 1
      totp/application.fam
  2. 18 0
      totp/config/app/config.h
  3. 61 23
      totp/workers/bt_type_code/bt_type_code.c

+ 0 - 1
totp/application.fam

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

+ 18 - 0
totp/config/app/config.h

@@ -1,7 +1,25 @@
+// List of compatible firmwares
+#define TOTP_FIRMWARE_OFFICIAL_STABLE (1)
+#define TOTP_FIRMWARE_OFFICIAL_DEV (2)
+#define TOTP_FIRMWARE_XTREME_UL (3)
+// End of list
+
 #if __has_include("ufbt_def.h")
 #if __has_include("ufbt_def.h")
 #include "ufbt_def.h"
 #include "ufbt_def.h"
 #endif
 #endif
 
 
+#ifndef TOTP_TARGET_FIRMWARE
+#if defined(TARGET_FIRMWARE_OFFICIAL) || defined(FW_ORIGIN_Official)
+#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_OFFICIAL_STABLE
+#elif defined(TARGET_FIRMWARE_UNLEASHED) || defined(FW_ORIGIN_Unleashed)
+#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_XTREME_UL
+#elif defined(TARGET_FIRMWARE_XTREME) || defined(FW_ORIGIN_Xtreme)
+#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_XTREME_UL
+#else
+#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_OFFICIAL_STABLE
+#endif
+#endif
+
 // Application automatic lock timeout if user IDLE. (ticks)
 // Application automatic lock timeout if user IDLE. (ticks)
 #ifndef TOTP_AUTO_LOCK_IDLE_TIMEOUT_SEC
 #ifndef TOTP_AUTO_LOCK_IDLE_TIMEOUT_SEC
 #define TOTP_AUTO_LOCK_IDLE_TIMEOUT_SEC (60)
 #define TOTP_AUTO_LOCK_IDLE_TIMEOUT_SEC (60)

+ 61 - 23
totp/workers/bt_type_code/bt_type_code.c

@@ -1,6 +1,6 @@
 #include "bt_type_code.h"
 #include "bt_type_code.h"
 #include <furi_hal_bt.h>
 #include <furi_hal_bt.h>
-#include <extra_profiles/hid_profile.h>
+#include <furi_hal_bt_hid.h>
 #include <furi_hal_version.h>
 #include <furi_hal_version.h>
 #include <furi/core/thread.h>
 #include <furi/core/thread.h>
 #include <furi/core/mutex.h>
 #include <furi/core/mutex.h>
@@ -14,6 +14,11 @@
 #include "../../config/app/config.h"
 #include "../../config/app/config.h"
 #include "../../services/config/constants.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"
 #define HID_BT_KEYS_STORAGE_PATH CONFIG_FILE_DIRECTORY_PATH "/.bt_hid.keys"
 
 
 struct TotpBtTypeCodeWorkerContext {
 struct TotpBtTypeCodeWorkerContext {
@@ -25,7 +30,10 @@ struct TotpBtTypeCodeWorkerContext {
     Bt* bt;
     Bt* bt;
     bool is_advertising;
     bool is_advertising;
     bool is_connected;
     bool is_connected;
-    FuriHalBleProfileBase* ble_hid_profile;
+#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
     AutomationKeyboardLayout keyboard_layout;
     AutomationKeyboardLayout keyboard_layout;
     uint16_t initial_delay;
     uint16_t initial_delay;
 };
 };
@@ -34,23 +42,25 @@ static inline bool totp_type_code_worker_stop_requested() {
     return furi_thread_flags_get() & TotpBtTypeCodeWorkerEventStop;
     return furi_thread_flags_get() & TotpBtTypeCodeWorkerEventStop;
 }
 }
 
 
-// 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;
-//     }
+#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;
+    }
 
 
-//     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;
-// }
+    mac[0] = 0b10;
+}
+#endif
 
 
 static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context) {
 static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context) {
     uint8_t i = 0;
     uint8_t i = 0;
@@ -156,17 +166,37 @@ TotpBtTypeCodeWorkerContext* totp_bt_type_code_worker_init() {
     context->is_advertising = false;
     context->is_advertising = false;
     context->is_connected = false;
     context->is_connected = false;
     bt_disconnect(context->bt);
     bt_disconnect(context->bt);
+    furi_hal_bt_reinit();
     furi_delay_ms(200);
     furi_delay_ms(200);
     bt_keys_storage_set_storage_path(context->bt, HID_BT_KEYS_STORAGE_PATH);
     bt_keys_storage_set_storage_path(context->bt, HID_BT_KEYS_STORAGE_PATH);
 
 
-    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);
+#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");
+    }
 
 
     furi_hal_bt_start_advertising();
     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;
     context->is_advertising = true;
     bt_set_status_changed_callback(context->bt, connection_status_changed_callback, context);
     bt_set_status_changed_callback(context->bt, connection_status_changed_callback, context);
 
 
@@ -182,6 +212,7 @@ void totp_bt_type_code_worker_free(TotpBtTypeCodeWorkerContext* context) {
 
 
     bt_set_status_changed_callback(context->bt, NULL, NULL);
     bt_set_status_changed_callback(context->bt, NULL, NULL);
 
 
+    furi_hal_bt_stop_advertising();
     context->is_advertising = false;
     context->is_advertising = false;
     context->is_connected = false;
     context->is_connected = false;
 
 
@@ -189,7 +220,14 @@ void totp_bt_type_code_worker_free(TotpBtTypeCodeWorkerContext* context) {
     furi_delay_ms(200);
     furi_delay_ms(200);
     bt_keys_storage_set_default_path(context->bt);
     bt_keys_storage_set_default_path(context->bt);
 
 
-    furi_check(bt_profile_restore_default(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_record_close(RECORD_BT);
     furi_record_close(RECORD_BT);
     context->bt = NULL;
     context->bt = NULL;