Alexander Kopachov 2 лет назад
Родитель
Сommit
2d52c7894e

+ 13 - 1
features_config.h

@@ -1,2 +1,14 @@
+// Include Bluetooth token input automation
 #define TOTP_BADBT_TYPE_ENABLED
 #define TOTP_BADBT_TYPE_ENABLED
-#define TOTP_AUTOMATION_ICONS_ENABLED
+
+// Include token input automation icons on the main screen
+#define TOTP_AUTOMATION_ICONS_ENABLED
+
+// List of compatible firmwares
+#define TOTP_FIRMWARE_OFFICIAL_STABLE 1
+#define TOTP_FIRMWARE_OFFICIAL_DEV 2
+#define TOTP_FIRMWARE_XTREME 3
+// End of list
+
+// Target firmware to build for
+#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_XTREME

+ 53 - 5
workers/bt_type_code/bt_type_code.c

@@ -1,5 +1,6 @@
 #include "bt_type_code.h"
 #include "bt_type_code.h"
 #include <furi_hal_bt_hid.h>
 #include <furi_hal_bt_hid.h>
+#include <bt/bt_service/bt_i.h>
 #include <storage/storage.h>
 #include <storage/storage.h>
 #include "../../types/common.h"
 #include "../../types/common.h"
 #include "../../types/token_info.h"
 #include "../../types/token_info.h"
@@ -11,6 +12,26 @@ static inline bool totp_type_code_worker_stop_requested() {
     return furi_thread_flags_get() & TotpBtTypeCodeWorkerEventStop;
     return furi_thread_flags_get() & TotpBtTypeCodeWorkerEventStop;
 }
 }
 
 
+#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME
+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 < 6) {
+        max_i = uid_size;
+    } else {
+        max_i = 6;
+    }
+
+    const uint8_t* uid = furi_hal_version_uid();
+    memcpy(mac, uid, max_i);
+    for(uint8_t i = max_i; i < 6; i++) {
+        mac[i] = 0;
+    }
+
+    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;
     do {
     do {
@@ -30,7 +51,7 @@ static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context
 }
 }
 
 
 static int32_t totp_type_code_worker_callback(void* context) {
 static int32_t totp_type_code_worker_callback(void* context) {
-    furi_assert(context);
+    furi_check(context);
     FuriMutex* context_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
     FuriMutex* context_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
     if(context_mutex == NULL) {
     if(context_mutex == NULL) {
         return 251;
         return 251;
@@ -74,7 +95,7 @@ void totp_bt_type_code_worker_start(
     char* code_buf,
     char* code_buf,
     uint8_t code_buf_length,
     uint8_t code_buf_length,
     FuriMutex* code_buf_update_sync) {
     FuriMutex* code_buf_update_sync) {
-    furi_assert(context != NULL);
+    furi_check(context != NULL);
     context->string = code_buf;
     context->string = code_buf;
     context->string_length = code_buf_length;
     context->string_length = code_buf_length;
     context->string_sync = code_buf_update_sync;
     context->string_sync = code_buf_update_sync;
@@ -87,7 +108,7 @@ void totp_bt_type_code_worker_start(
 }
 }
 
 
 void totp_bt_type_code_worker_stop(TotpBtTypeCodeWorkerContext* context) {
 void totp_bt_type_code_worker_stop(TotpBtTypeCodeWorkerContext* context) {
-    furi_assert(context != NULL);
+    furi_check(context != NULL);
     furi_thread_flags_set(furi_thread_get_id(context->thread), TotpBtTypeCodeWorkerEventStop);
     furi_thread_flags_set(furi_thread_get_id(context->thread), TotpBtTypeCodeWorkerEventStop);
     furi_thread_join(context->thread);
     furi_thread_join(context->thread);
     furi_thread_free(context->thread);
     furi_thread_free(context->thread);
@@ -98,7 +119,7 @@ void totp_bt_type_code_worker_notify(
     TotpBtTypeCodeWorkerContext* context,
     TotpBtTypeCodeWorkerContext* context,
     TotpBtTypeCodeWorkerEvent event,
     TotpBtTypeCodeWorkerEvent event,
     uint8_t flags) {
     uint8_t flags) {
-    furi_assert(context != NULL);
+    furi_check(context != NULL);
     context->flags = flags;
     context->flags = flags;
     furi_thread_flags_set(furi_thread_get_id(context->thread), event);
     furi_thread_flags_set(furi_thread_get_id(context->thread), event);
 }
 }
@@ -114,11 +135,33 @@ TotpBtTypeCodeWorkerContext* totp_bt_type_code_worker_init() {
     furi_hal_bt_reinit();
     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);
+
+#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME
+    totp_type_code_worker_bt_set_app_mac(&context->bt_mac[0]);
+    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());
+    furi_hal_bt_set_profile_adv_name(FuriHalBtProfileHidKeyboard, new_name);
+    furi_hal_bt_set_profile_mac_addr(FuriHalBtProfileHidKeyboard, context->bt_mac);
+#endif
+
     if(!bt_set_profile(context->bt, BtProfileHidKeyboard)) {
     if(!bt_set_profile(context->bt, BtProfileHidKeyboard)) {
         FURI_LOG_E(LOGGING_TAG, "Failed to switch BT to keyboard HID profile");
         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
+    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);
 
 
@@ -126,7 +169,7 @@ TotpBtTypeCodeWorkerContext* totp_bt_type_code_worker_init() {
 }
 }
 
 
 void totp_bt_type_code_worker_free(TotpBtTypeCodeWorkerContext* context) {
 void totp_bt_type_code_worker_free(TotpBtTypeCodeWorkerContext* context) {
-    furi_assert(context != NULL);
+    furi_check(context != NULL);
 
 
     if(context->thread != NULL) {
     if(context->thread != NULL) {
         totp_bt_type_code_worker_stop(context);
         totp_bt_type_code_worker_stop(context);
@@ -142,6 +185,11 @@ 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);
 
 
+#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME
+    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)) {
     if(!bt_set_profile(context->bt, BtProfileSerial)) {
         FURI_LOG_E(LOGGING_TAG, "Failed to switch BT to Serial profile");
         FURI_LOG_E(LOGGING_TAG, "Failed to switch BT to Serial profile");
     }
     }

+ 11 - 0
workers/bt_type_code/bt_type_code.h

@@ -4,6 +4,12 @@
 #include <furi/furi.h>
 #include <furi/furi.h>
 #include <furi_hal.h>
 #include <furi_hal.h>
 #include <bt/bt_service/bt.h>
 #include <bt/bt_service/bt.h>
+#include "../../features_config.h"
+
+#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME
+#define TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN 18
+#define TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN GAP_MAC_ADDR_SIZE
+#endif
 
 
 typedef uint8_t TotpBtTypeCodeWorkerEvent;
 typedef uint8_t TotpBtTypeCodeWorkerEvent;
 
 
@@ -16,6 +22,11 @@ typedef struct {
     Bt* bt;
     Bt* bt;
     bool is_advertising;
     bool is_advertising;
     bool is_connected;
     bool is_connected;
+#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME
+    uint8_t bt_mac[TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN];
+    char previous_bt_name[TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN + 1];
+    uint8_t previous_bt_mac[TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN];
+#endif
 } TotpBtTypeCodeWorkerContext;
 } TotpBtTypeCodeWorkerContext;
 
 
 enum TotpBtTypeCodeWorkerEvents {
 enum TotpBtTypeCodeWorkerEvents {

+ 3 - 3
workers/usb_type_code/usb_type_code.c

@@ -41,7 +41,7 @@ static void totp_type_code_worker_type_code(TotpUsbTypeCodeWorkerContext* contex
 }
 }
 
 
 static int32_t totp_type_code_worker_callback(void* context) {
 static int32_t totp_type_code_worker_callback(void* context) {
-    furi_assert(context);
+    furi_check(context);
     FuriMutex* context_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
     FuriMutex* context_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
     if(context_mutex == NULL) {
     if(context_mutex == NULL) {
         return 251;
         return 251;
@@ -89,7 +89,7 @@ TotpUsbTypeCodeWorkerContext* totp_usb_type_code_worker_start(
 }
 }
 
 
 void totp_usb_type_code_worker_stop(TotpUsbTypeCodeWorkerContext* context) {
 void totp_usb_type_code_worker_stop(TotpUsbTypeCodeWorkerContext* context) {
-    furi_assert(context != NULL);
+    furi_check(context != NULL);
     furi_thread_flags_set(furi_thread_get_id(context->thread), TotpUsbTypeCodeWorkerEventStop);
     furi_thread_flags_set(furi_thread_get_id(context->thread), TotpUsbTypeCodeWorkerEventStop);
     furi_thread_join(context->thread);
     furi_thread_join(context->thread);
     furi_thread_free(context->thread);
     furi_thread_free(context->thread);
@@ -101,7 +101,7 @@ void totp_usb_type_code_worker_notify(
     TotpUsbTypeCodeWorkerContext* context,
     TotpUsbTypeCodeWorkerContext* context,
     TotpUsbTypeCodeWorkerEvent event,
     TotpUsbTypeCodeWorkerEvent event,
     uint8_t flags) {
     uint8_t flags) {
-    furi_assert(context != NULL);
+    furi_check(context != NULL);
     context->flags = flags;
     context->flags = flags;
     furi_thread_flags_set(furi_thread_get_id(context->thread), event);
     furi_thread_flags_set(furi_thread_get_id(context->thread), event);
 }
 }