Просмотр исходного кода

NFC Maker: Refactor generation to support more types

Willy-JL 9 месяцев назад
Родитель
Сommit
0a70ad597e

+ 1 - 1
nfc_maker/application.fam

@@ -3,7 +3,7 @@ App(
     name="NFC Maker",
     apptype=FlipperAppType.EXTERNAL,
     entry_point="nfc_maker",
-    stack_size=1 * 1024,
+    stack_size=2 * 1024,
     fap_icon="nfc_maker_10px.png",
     fap_category="NFC",
     fap_author="@Willy-JL",

+ 45 - 24
nfc_maker/nfc_maker.c

@@ -1,28 +1,49 @@
 #include "nfc_maker.h"
 
-const NfcDataGeneratorType ntag_generators[NtagMAX] = {
-    [Ntag203] = NfcDataGeneratorTypeNTAG203,
-    [Ntag213] = NfcDataGeneratorTypeNTAG213,
-    [Ntag215] = NfcDataGeneratorTypeNTAG215,
-    [Ntag216] = NfcDataGeneratorTypeNTAG216,
-    [NtagI2C1K] = NfcDataGeneratorTypeNTAGI2C1k,
-    [NtagI2C2K] = NfcDataGeneratorTypeNTAGI2C2k,
-};
-const char* ntag_names[NtagMAX] = {
-    [Ntag203] = "NTAG203",
-    [Ntag213] = "NTAG213",
-    [Ntag215] = "NTAG215",
-    [Ntag216] = "NTAG216",
-    [NtagI2C1K] = "NTAG I2C 1K",
-    [NtagI2C2K] = "NTAG I2C 2K",
-};
-const size_t ntag_sizes[NtagMAX] = {
-    [Ntag203] = 0x12 * NTAG_DATA_AREA_UNIT_SIZE,
-    [Ntag213] = 0x12 * NTAG_DATA_AREA_UNIT_SIZE,
-    [Ntag215] = 0x3E * NTAG_DATA_AREA_UNIT_SIZE,
-    [Ntag216] = 0x6D * NTAG_DATA_AREA_UNIT_SIZE,
-    [NtagI2C1K] = 0x6D * NTAG_DATA_AREA_UNIT_SIZE,
-    [NtagI2C2K] = 0xEA * NTAG_DATA_AREA_UNIT_SIZE,
+const CardDef cards[CardMAX] = {
+    // MfUltralight
+    [CardNtag203] =
+        {
+            .name = "NTAG 203 (144B)",
+            .size = 0x12 * NTAG_DATA_AREA_UNIT_SIZE,
+            .protocol = NfcProtocolMfUltralight,
+            .generator = NfcDataGeneratorTypeNTAG203,
+        },
+    [CardNtag213] =
+        {
+            .name = "NTAG 213 (144B)",
+            .size = 0x12 * NTAG_DATA_AREA_UNIT_SIZE,
+            .protocol = NfcProtocolMfUltralight,
+            .generator = NfcDataGeneratorTypeNTAG213,
+        },
+    [CardNtag215] =
+        {
+            .name = "NTAG 215 (496B)",
+            .size = 0x3E * NTAG_DATA_AREA_UNIT_SIZE,
+            .protocol = NfcProtocolMfUltralight,
+            .generator = NfcDataGeneratorTypeNTAG215,
+        },
+    [CardNtag216] =
+        {
+            .name = "NTAG 216 (872B)",
+            .size = 0x6D * NTAG_DATA_AREA_UNIT_SIZE,
+            .protocol = NfcProtocolMfUltralight,
+            .generator = NfcDataGeneratorTypeNTAG216,
+        },
+    [CardNtagI2C1K] =
+        {
+            .name = "NTAG I2C 1K (872B)",
+            .size = 0x6D * NTAG_DATA_AREA_UNIT_SIZE,
+            .protocol = NfcProtocolMfUltralight,
+            .generator = NfcDataGeneratorTypeNTAGI2C1k,
+        },
+    [CardNtagI2C2K] =
+        {
+            .name = "NTAG I2C 2K (1872B)",
+            .size = 0xEA * NTAG_DATA_AREA_UNIT_SIZE,
+            .protocol = NfcProtocolMfUltralight,
+            .generator = NfcDataGeneratorTypeNTAGI2C2k,
+        },
 };
 
 static bool nfc_maker_custom_event_callback(void* context, uint32_t event) {
@@ -105,7 +126,7 @@ void nfc_maker_free(NfcMaker* app) {
     free(app);
 }
 
-extern int32_t nfc_maker(void* p) {
+int32_t nfc_maker(void* p) {
     UNUSED(p);
     NfcMaker* app = nfc_maker_alloc();
     scene_manager_set_scene_state(app->scene_manager, NfcMakerSceneStart, NfcMakerSceneHttps);

+ 24 - 18
nfc_maker/nfc_maker.h

@@ -1,6 +1,7 @@
 #pragma once
 
 #include <furi.h>
+#include <furi_hal.h>
 #include <gui/gui.h>
 #include <gui/view.h>
 #include <gui/modules/validators.h>
@@ -31,28 +32,33 @@ extern const Icon I_WarningDolphinFlip_45x42;
 #endif
 #include <lib/nfc/protocols/mf_ultralight/mf_ultralight.h>
 #include <lib/nfc/helpers/nfc_data_generator.h>
-#include <furi_hal_bt.h>
 
-#define MAC_INPUT_LEN   GAP_MAC_ADDR_SIZE
-#define MAIL_INPUT_LEN  128
-#define PHONE_INPUT_LEN 17
+#define MAC_INPUT_LEN   (GAP_MAC_ADDR_SIZE)
+#define MAIL_INPUT_LEN  (128)
+#define PHONE_INPUT_LEN (17)
 
-#define BIG_INPUT_LEN   248
-#define SMALL_INPUT_LEN 90
+#define BIG_INPUT_LEN   (248)
+#define SMALL_INPUT_LEN (90)
 
-#define NTAG_DATA_AREA_UNIT_SIZE 2 * MF_ULTRALIGHT_PAGE_SIZE
+#define NTAG_DATA_AREA_UNIT_SIZE (2 * MF_ULTRALIGHT_PAGE_SIZE)
 typedef enum {
-    Ntag203,
-    Ntag213,
-    Ntag215,
-    Ntag216,
-    NtagI2C1K,
-    NtagI2C2K,
-    NtagMAX,
-} Ntag;
-extern const NfcDataGeneratorType ntag_generators[NtagMAX];
-extern const char* ntag_names[NtagMAX];
-extern const size_t ntag_sizes[NtagMAX];
+    // MfUltralight
+    CardNtag203,
+    CardNtag213,
+    CardNtag215,
+    CardNtag216,
+    CardNtagI2C1K,
+    CardNtagI2C2K,
+
+    CardMAX,
+} Card;
+typedef struct {
+    const char* name;
+    size_t size;
+    NfcProtocol protocol;
+    NfcDataGeneratorType generator;
+} CardDef;
+extern const CardDef cards[CardMAX];
 
 typedef enum {
     WifiAuthenticationOpen = 0x01,

+ 1 - 1
nfc_maker/scenes/nfc_maker_scene_config.h

@@ -15,6 +15,6 @@ ADD_SCENE(nfc_maker, wifi_auth, WifiAuth)
 ADD_SCENE(nfc_maker, wifi_encr, WifiEncr)
 ADD_SCENE(nfc_maker, wifi_pass, WifiPass)
 ADD_SCENE(nfc_maker, save_generate, SaveGenerate)
-ADD_SCENE(nfc_maker, save_uid_mful, SaveUidMful)
+ADD_SCENE(nfc_maker, save_uid, SaveUid)
 ADD_SCENE(nfc_maker, save_name, SaveName)
 ADD_SCENE(nfc_maker, save_result, SaveResult)

+ 33 - 21
nfc_maker/scenes/nfc_maker_scene_save_generate.c

@@ -286,6 +286,26 @@ static void nfc_maker_scene_save_generate_populate_ndef_buffer(NfcMaker* app) {
     furi_check(app->ndef_size == (size_t)(buf - app->ndef_buffer));
 }
 
+static void nfc_maker_scene_save_generate_populate_device_mful(NfcMaker* app, Card card_type) {
+    const CardDef* card = &cards[card_type];
+
+    nfc_data_generator_fill_data(card->generator, app->nfc_device);
+    MfUltralightData* data = mf_ultralight_alloc();
+    nfc_device_copy_data(app->nfc_device, NfcProtocolMfUltralight, data);
+
+    size_t size =
+        MIN(card->size, // Known size
+            data->page[3].data[2] * NTAG_DATA_AREA_UNIT_SIZE // Capability Container
+        );
+    furi_check(app->ndef_size <= size);
+    memcpy(&data->page[4].data[0], app->ndef_buffer, app->ndef_size);
+    free(app->ndef_buffer);
+    app->ndef_buffer = NULL;
+
+    nfc_device_set_data(app->nfc_device, NfcProtocolMfUltralight, data);
+    mf_ultralight_free(data);
+}
+
 void nfc_maker_scene_save_generate_submenu_callback(void* context, uint32_t index) {
     NfcMaker* app = context;
     view_dispatcher_send_custom_event(app->view_dispatcher, index);
@@ -298,14 +318,14 @@ void nfc_maker_scene_save_generate_on_enter(void* context) {
 
     submenu_set_header(submenu, "Tag Type:");
 
-    for(Ntag ntag = 0; ntag < NtagMAX; ntag++) {
+    for(Card card = 0; card < CardMAX; card++) {
         submenu_add_lockable_item(
             submenu,
-            ntag_names[ntag],
-            ntag,
+            cards[card].name,
+            card,
             nfc_maker_scene_save_generate_submenu_callback,
             app,
-            app->ndef_size > ntag_sizes[ntag],
+            app->ndef_size > cards[card].size,
             "Data is\ntoo large!");
     }
 
@@ -321,26 +341,18 @@ bool nfc_maker_scene_save_generate_on_event(void* context, SceneManagerEvent eve
 
     if(event.type == SceneManagerEventTypeCustom) {
         scene_manager_set_scene_state(app->scene_manager, NfcMakerSceneSaveGenerate, event.event);
-        if(event.event >= NtagMAX) return consumed;
+        if(event.event >= CardMAX) return consumed;
         consumed = true;
 
-        nfc_data_generator_fill_data(ntag_generators[event.event], app->nfc_device);
-        MfUltralightData* data = mf_ultralight_alloc();
-        nfc_device_copy_data(app->nfc_device, NfcProtocolMfUltralight, data);
-
-        size_t size =
-            MIN(ntag_sizes[event.event], // Known size
-                data->page[3].data[2] * NTAG_DATA_AREA_UNIT_SIZE // Capability Container
-            );
-        furi_check(app->ndef_size <= size);
-        memcpy(&data->page[4].data[0], app->ndef_buffer, app->ndef_size);
-        free(app->ndef_buffer);
-        app->ndef_buffer = NULL;
-
-        nfc_device_set_data(app->nfc_device, NfcProtocolMfUltralight, data);
-        mf_ultralight_free(data);
+        switch(cards[event.event].protocol) {
+        case NfcProtocolMfUltralight:
+            nfc_maker_scene_save_generate_populate_device_mful(app, event.event);
+            break;
+        default:
+            break;
+        }
 
-        scene_manager_next_scene(app->scene_manager, NfcMakerSceneSaveUidMful);
+        scene_manager_next_scene(app->scene_manager, NfcMakerSceneSaveUid);
     }
 
     return consumed;

+ 7 - 7
nfc_maker/scenes/nfc_maker_scene_save_uid_mful.c → nfc_maker/scenes/nfc_maker_scene_save_uid.c

@@ -4,7 +4,7 @@ enum ByteInputResult {
     ByteInputResultOk,
 };
 
-static void nfc_maker_scene_save_uid_mful_byte_input_callback(void* context) {
+static void nfc_maker_scene_save_uid_byte_input_callback(void* context) {
     NfcMaker* app = context;
 
     size_t uid_len;
@@ -18,13 +18,13 @@ static void nfc_maker_scene_save_uid_mful_byte_input_callback(void* context) {
     }
 }
 
-static void nfc_maker_scene_save_uid_mful_byte_input_changed(void* context) {
+static void nfc_maker_scene_save_uid_byte_input_changed(void* context) {
     NfcMaker* app = context;
 
     byte_input_set_header_text(app->byte_input, "Change UID:");
 }
 
-void nfc_maker_scene_save_uid_mful_on_enter(void* context) {
+void nfc_maker_scene_save_uid_on_enter(void* context) {
     NfcMaker* app = context;
     ByteInput* byte_input = app->byte_input;
 
@@ -36,8 +36,8 @@ void nfc_maker_scene_save_uid_mful_on_enter(void* context) {
 
     byte_input_set_result_callback(
         byte_input,
-        nfc_maker_scene_save_uid_mful_byte_input_callback,
-        nfc_maker_scene_save_uid_mful_byte_input_changed,
+        nfc_maker_scene_save_uid_byte_input_callback,
+        nfc_maker_scene_save_uid_byte_input_changed,
         app,
         app->uid_buf,
         uid_len);
@@ -45,7 +45,7 @@ void nfc_maker_scene_save_uid_mful_on_enter(void* context) {
     view_dispatcher_switch_to_view(app->view_dispatcher, NfcMakerViewByteInput);
 }
 
-bool nfc_maker_scene_save_uid_mful_on_event(void* context, SceneManagerEvent event) {
+bool nfc_maker_scene_save_uid_on_event(void* context, SceneManagerEvent event) {
     NfcMaker* app = context;
     bool consumed = false;
 
@@ -63,7 +63,7 @@ bool nfc_maker_scene_save_uid_mful_on_event(void* context, SceneManagerEvent eve
     return consumed;
 }
 
-void nfc_maker_scene_save_uid_mful_on_exit(void* context) {
+void nfc_maker_scene_save_uid_on_exit(void* context) {
     NfcMaker* app = context;
     byte_input_set_result_callback(app->byte_input, NULL, NULL, NULL, NULL, 0);
     byte_input_set_header_text(app->byte_input, "");