Przeglądaj źródła

Working on peripheral-as-credential

Eric Betts 10 miesięcy temu
rodzic
commit
bffa200f07

+ 7 - 6
scenes/seos_scene_saved_menu.c

@@ -28,13 +28,14 @@ void seos_scene_saved_menu_on_enter(void* context) {
             SubmenuIndexBLEEmulateCentral,
             seos_scene_saved_menu_submenu_callback,
             seos);
-        submenu_add_item(
-            submenu,
-            "BLE Emulate Peripheral",
-            SubmenuIndexBLEEmulatePeripheral,
-            seos_scene_saved_menu_submenu_callback,
-            seos);
     }
+    submenu_add_item(
+        submenu,
+        "BLE Emulate Peripheral",
+        SubmenuIndexBLEEmulatePeripheral,
+        seos_scene_saved_menu_submenu_callback,
+        seos);
+
     submenu_add_item(
         submenu, "Info", SubmenuIndexInfo, seos_scene_saved_menu_submenu_callback, seos);
     submenu_add_item(

+ 5 - 1
seos_native_peripheral.c

@@ -102,10 +102,14 @@ void seos_native_peripheral_start(SeosNativePeripheral* seos_native_peripheral,
     UNUSED(mode);
     bt_disconnect(seos_native_peripheral->bt);
 
+    BleProfileParams params = {
+        .mode = mode,
+    };
+
     // Wait 2nd core to update nvm storage
     furi_delay_ms(200);
     seos_native_peripheral->ble_profile =
-        bt_profile_start(seos_native_peripheral->bt, ble_profile_seos, NULL);
+        bt_profile_start(seos_native_peripheral->bt, ble_profile_seos, &params);
     furi_check(seos_native_peripheral->ble_profile);
     bt_set_status_changed_callback(
         seos_native_peripheral->bt, seos_ble_connection_status_callback, seos_native_peripheral);

+ 26 - 7
seos_profile.c

@@ -12,17 +12,18 @@
 typedef struct {
     FuriHalBleProfileBase base;
     BleServiceSeos* seos_svc;
+    FuriHalBleProfileParams params;
 } BleProfileSeos;
 _Static_assert(offsetof(BleProfileSeos, base) == 0, "Wrong layout");
 
 static FuriHalBleProfileBase* ble_profile_seos_start(FuriHalBleProfileParams profile_params) {
-    UNUSED(profile_params);
-
     BleProfileSeos* profile = malloc(sizeof(BleProfileSeos));
+    BleProfileParams* params = profile_params;
 
+    profile->params = profile_params;
     profile->base.config = ble_profile_seos;
 
-    profile->seos_svc = ble_svc_seos_start();
+    profile->seos_svc = ble_svc_seos_start(params->mode);
 
     return &profile->base;
 }
@@ -41,9 +42,9 @@ static void ble_profile_seos_stop(FuriHalBleProfileBase* profile) {
 // Up to 45 ms
 #define CONNECTION_INTERVAL_MAX (0x24)
 
-static GapConfig seos_template_config = {
+static GapConfig seos_reader_template_config = {
     .adv_service.UUID_Type = UUID_TYPE_128,
-    .adv_service.Service_UUID_128 = BLE_SVC_SEOS_UUID,
+    .adv_service.Service_UUID_128 = BLE_SVC_SEOS_READER_UUID,
     .mfg_data =
         {0x2e,
          0x01,
@@ -75,12 +76,30 @@ static GapConfig seos_template_config = {
         .supervisor_timeout = 0,
     }};
 
+static GapConfig seos_cred_template_config = {
+    .adv_service.UUID_Type = UUID_TYPE_128,
+    .adv_service.Service_UUID_128 = BLE_SVC_SEOS_CRED_UUID,
+    .adv_name = "Seos",
+    .bonding_mode = false,
+    .pairing_method = GapPairingNone,
+    .conn_param = {
+        .conn_int_min = CONNECTION_INTERVAL_MIN,
+        .conn_int_max = CONNECTION_INTERVAL_MAX,
+        .slave_latency = 0,
+        .supervisor_timeout = 0,
+    }};
+
 static void
     ble_profile_seos_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
-    UNUSED(profile_params);
+    BleProfileParams* params = profile_params;
 
+    FURI_LOG_D(TAG, "ble_profile_seos_get_config FlowMode %d", params->mode);
     furi_check(config);
-    memcpy(config, &seos_template_config, sizeof(GapConfig));
+    if(params->mode == FLOW_READER) {
+        memcpy(config, &seos_reader_template_config, sizeof(GapConfig));
+    } else if(params->mode == FLOW_CRED) {
+        memcpy(config, &seos_cred_template_config, sizeof(GapConfig));
+    }
     // Set mac address
     memcpy(config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
 }

+ 5 - 0
seos_profile.h

@@ -2,6 +2,7 @@
 
 #include <furi_ble/profile_interface.h>
 
+#include "seos_common.h"
 #include "seos_service.h"
 
 #ifdef __cplusplus
@@ -10,6 +11,10 @@ extern "C" {
 
 #define BLE_PROFILE_SEOS_PACKET_SIZE_MAX BLE_SVC_SEOS_DATA_LEN_MAX
 
+typedef struct ble_profile_params {
+    FlowMode mode;
+} BleProfileParams;
+
 /** Seos service callback type */
 typedef SeosServiceEventCallback FuriHalBtSeosCallback;
 

+ 37 - 29
seos_service.c

@@ -13,6 +13,9 @@
 
 #define TAG "BtSeosSvc"
 
+static uint8_t select[] =
+    {0xc0, 0x00, 0xa4, 0x04, 0x00, 0x0a, 0xa0, 0x00, 0x00, 0x04, 0x40, 0x00, 0x01, 0x01, 0x00, 0x01};
+
 typedef enum {
     SeosSvcGattCharacteristicRxTx = 0,
     SeosSvcGattCharacteristicCount,
@@ -25,6 +28,7 @@ typedef struct {
 
 static bool
     ble_svc_seos_data_callback(const void* context, const uint8_t** data, uint16_t* data_len) {
+    FURI_LOG_D(TAG, "ble_svc_seos_data_callback");
     const SeosSvcDataWrapper* report_data = context;
     if(data) {
         *data = report_data->data_ptr;
@@ -35,14 +39,13 @@ static bool
     return false;
 }
 
-static const BleGattCharacteristicParams ble_svc_seos_chars[SeosSvcGattCharacteristicCount] = {
+static BleGattCharacteristicParams ble_svc_seos_chars[SeosSvcGattCharacteristicCount] = {
     [SeosSvcGattCharacteristicRxTx] =
         {.name = "SEOS",
          .data_prop_type = FlipperGattCharacteristicDataCallback,
          .data.callback.fn = ble_svc_seos_data_callback,
          .data.callback.context = NULL,
-         //.max_length = BLE_SVC_SEOS_DATA_LEN_MAX,
-         .uuid.Char_UUID_128 = BLE_SVC_SEOS_CHAR_UUID,
+         .uuid.Char_UUID_128 = BLE_SVC_SEOS_READER_CHAR_UUID, // changed before init
          .uuid_type = UUID_TYPE_128,
          .char_properties = CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_NOTIFY,
          .security_permissions = ATTR_PERMISSION_NONE,
@@ -80,24 +83,6 @@ static BleEventAckStatus ble_svc_seos_event_handler(void* event, void* context)
                 if(attribute_modified->Attr_Data_Length == 2) {
                     uint16_t* value = (uint16_t*)attribute_modified->Attr_Data;
                     if(*value == 1) { // ENABLE_NOTIFICATION_VALUE)
-                        uint8_t select[] = {
-                            0xc0,
-                            0x00,
-                            0xa4,
-                            0x04,
-                            0x00,
-                            0x0a,
-                            0xa0,
-                            0x00,
-                            0x00,
-                            0x04,
-                            0x40,
-                            0x00,
-                            0x01,
-                            0x01,
-                            0x00,
-                            0x01};
-
                         SeosSvcDataWrapper report_data = {
                             .data_ptr = select, .data_len = sizeof(select)};
 
@@ -110,7 +95,6 @@ static BleEventAckStatus ble_svc_seos_event_handler(void* event, void* context)
                     FURI_LOG_D(
                         TAG, "descriptor event %d bytes", attribute_modified->Attr_Data_Length);
                 }
-
             } else if(
                 attribute_modified->Attr_Handle ==
                 seos_svc->chars[SeosSvcGattCharacteristicRxTx].handle + 1) {
@@ -155,20 +139,44 @@ static BleEventAckStatus ble_svc_seos_event_handler(void* event, void* context)
     return ret;
 }
 
-BleServiceSeos* ble_svc_seos_start(void) {
+BleServiceSeos* ble_svc_seos_start(FlowMode mode) {
     BleServiceSeos* seos_svc = malloc(sizeof(BleServiceSeos));
 
     seos_svc->event_handler =
         ble_event_dispatcher_register_svc_handler(ble_svc_seos_event_handler, seos_svc);
 
-    if(!ble_gatt_service_add(
-           UUID_TYPE_128, &service_uuid, PRIMARY_SERVICE, 12, &seos_svc->svc_handle)) {
-        free(seos_svc);
-        return NULL;
+    if(mode == FLOW_READER) {
+        if(!ble_gatt_service_add(
+               UUID_TYPE_128, &reader_service_uuid, PRIMARY_SERVICE, 12, &seos_svc->svc_handle)) {
+            free(seos_svc);
+            return NULL;
+        }
+    } else if(mode == FLOW_CRED) {
+        if(!ble_gatt_service_add(
+               UUID_TYPE_128, &cred_service_uuid, PRIMARY_SERVICE, 12, &seos_svc->svc_handle)) {
+            free(seos_svc);
+            return NULL;
+        }
     }
+
     for(uint8_t i = 0; i < SeosSvcGattCharacteristicCount; i++) {
-        ble_gatt_characteristic_init(
-            seos_svc->svc_handle, &ble_svc_seos_chars[i], &seos_svc->chars[i]);
+        if(i == SeosSvcGattCharacteristicRxTx) {
+            if(mode == FLOW_READER) {
+                uint8_t uuid[] = BLE_SVC_SEOS_READER_CHAR_UUID;
+                memcpy(
+                    ble_svc_seos_chars[SeosSvcGattCharacteristicRxTx].uuid.Char_UUID_128,
+                    uuid,
+                    sizeof(uuid));
+            } else if(mode == FLOW_CRED) {
+                uint8_t uuid[] = BLE_SVC_SEOS_CRED_CHAR_UUID;
+                memcpy(
+                    ble_svc_seos_chars[SeosSvcGattCharacteristicRxTx].uuid.Char_UUID_128,
+                    uuid,
+                    sizeof(uuid));
+            }
+            ble_gatt_characteristic_init(
+                seos_svc->svc_handle, &ble_svc_seos_chars[i], &seos_svc->chars[i]);
+        }
     }
 
     seos_svc->buff_size_mtx = furi_mutex_alloc(FuriMutexTypeNormal);

+ 2 - 1
seos_service.h

@@ -2,6 +2,7 @@
 
 #include <stdint.h>
 #include <stdbool.h>
+#include "seos_common.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -34,7 +35,7 @@ typedef uint16_t (*SeosServiceEventCallback)(SeosServiceEvent event, void* conte
 
 typedef struct BleServiceSeos BleServiceSeos;
 
-BleServiceSeos* ble_svc_seos_start(void);
+BleServiceSeos* ble_svc_seos_start(FlowMode mode);
 
 void ble_svc_seos_stop(BleServiceSeos* service);
 

+ 11 - 3
seos_service_uuid.inc

@@ -1,9 +1,17 @@
 #include <ble/core/auto/ble_types.h>
 
-#define BLE_SVC_SEOS_UUID      \
+#define BLE_SVC_SEOS_READER_UUID      \
     {0x02, 0x00, 0x00, 0x7a, 0x17, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00 }
 
-#define BLE_SVC_SEOS_CHAR_UUID      \
+#define BLE_SVC_SEOS_READER_CHAR_UUID      \
     {0x02, 0x00, 0x00, 0x7a, 0x17, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0xaa, 0x00, 0x00}
 
-static const Service_UUID_t service_uuid = { .Service_UUID_128 = BLE_SVC_SEOS_UUID };
+static const Service_UUID_t reader_service_uuid = { .Service_UUID_128 = BLE_SVC_SEOS_READER_UUID };
+
+#define BLE_SVC_SEOS_CRED_UUID      \
+    {0x02, 0x00, 0x00, 0x7a, 0x17, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x01, 0x98, 0x00, 0x00 }
+
+#define BLE_SVC_SEOS_CRED_CHAR_UUID      \
+    {0x02, 0x00, 0x00, 0x7a, 0x17, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x01, 0xaa, 0x00, 0x00}
+
+static const Service_UUID_t cred_service_uuid = { .Service_UUID_128 = BLE_SVC_SEOS_CRED_UUID };