From 68f51411a1c39f68c3aefe9c7615323873a42f27 Mon Sep 17 00:00:00 2001 From: RogueMaster Date: Sat, 26 Nov 2022 02:23:03 -0500 Subject: [PATCH] fmt --- .../main/nfc/scenes/nfc_scene_emulate_nfcv.c | 6 +- .../main/nfc/scenes/nfc_scene_nfc_data_info.c | 175 +- .../main/nfc/scenes/nfc_scene_nfcv_menu.c | 7 +- .../main/nfc/scenes/nfc_scene_nfcv_unlock.c | 31 +- .../nfc/scenes/nfc_scene_nfcv_unlock_menu.c | 6 +- applications/plugins/nrf24scan/nrf24scan.c | 1423 +++++++++-------- lib/digital_signal/digital_signal.c | 98 +- lib/digital_signal/digital_signal.h | 18 +- lib/nfc/nfc_device.c | 161 +- lib/nfc/nfc_worker.c | 31 +- lib/nfc/nfc_worker.h | 1 - lib/nfc/protocols/nfca_trans_rx.c | 19 +- lib/nfc/protocols/nfca_trans_rx.h | 47 +- lib/nfc/protocols/nfcv.c | 769 +++++---- lib/nfc/protocols/nfcv.h | 125 +- lib/nfc/protocols/slix.c | 89 +- lib/nfc/protocols/slix.h | 8 +- lib/pulse_reader/pulse_reader.c | 78 +- lib/pulse_reader/pulse_reader.h | 16 +- 19 files changed, 1689 insertions(+), 1419 deletions(-) diff --git a/applications/main/nfc/scenes/nfc_scene_emulate_nfcv.c b/applications/main/nfc/scenes/nfc_scene_emulate_nfcv.c index 0b09bb412..e6fc60d86 100644 --- a/applications/main/nfc/scenes/nfc_scene_emulate_nfcv.c +++ b/applications/main/nfc/scenes/nfc_scene_emulate_nfcv.c @@ -38,7 +38,8 @@ static void nfc_scene_emulate_nfcv_widget_config(Nfc* nfc, bool data_received) { info_str = furi_string_alloc(); widget_add_icon_element(widget, 0, 3, &I_RFIDDolphinSend_97x61); - widget_add_string_element(widget, 89, 32, AlignCenter, AlignTop, FontPrimary, "Emulating NfcV"); + widget_add_string_element( + widget, 89, 32, AlignCenter, AlignTop, FontPrimary, "Emulating NfcV"); if(strcmp(nfc->dev->dev_name, "")) { furi_string_printf(info_str, "%s", nfc->dev->dev_name); } else { @@ -97,7 +98,8 @@ bool nfc_scene_emulate_nfcv_on_event(void* context, SceneManagerEvent event) { } if(strlen(nfcv_data->last_command) > 0) { /* use the last n bytes from the log so there's enough space for the new log entry */ - size_t maxSize = NFC_SCENE_EMULATE_NFCV_LOG_SIZE_MAX - (strlen(nfcv_data->last_command) + 1); + size_t maxSize = + NFC_SCENE_EMULATE_NFCV_LOG_SIZE_MAX - (strlen(nfcv_data->last_command) + 1); if(furi_string_size(nfc->text_box_store) >= maxSize) { furi_string_right(nfc->text_box_store, (strlen(nfcv_data->last_command) + 1)); } diff --git a/applications/main/nfc/scenes/nfc_scene_nfc_data_info.c b/applications/main/nfc/scenes/nfc_scene_nfc_data_info.c index 6d5f7a1ca..258da6349 100644 --- a/applications/main/nfc/scenes/nfc_scene_nfc_data_info.c +++ b/applications/main/nfc/scenes/nfc_scene_nfc_data_info.c @@ -7,7 +7,7 @@ void nfc_scene_nfc_data_info_widget_callback(GuiButtonType result, InputType typ } } -uint32_t nfc_scene_nfc_data_info_get_key(uint8_t *data) { +uint32_t nfc_scene_nfc_data_info_get_key(uint8_t* data) { uint32_t value = 0; for(uint32_t pos = 0; pos < 4; pos++) { @@ -25,8 +25,8 @@ void nfc_scene_nfc_data_info_on_enter(void* context) { NfcDeviceData* dev_data = &nfc->dev->dev_data; NfcProtocol protocol = dev_data->protocol; uint8_t text_scroll_height = 0; - if((protocol == NfcDeviceProtocolMifareDesfire) || (protocol == NfcDeviceProtocolMifareUl) - || (protocol == NfcDeviceProtocolNfcV)) { + if((protocol == NfcDeviceProtocolMifareDesfire) || (protocol == NfcDeviceProtocolMifareUl) || + (protocol == NfcDeviceProtocolNfcV)) { widget_add_button_element( widget, GuiButtonTypeRight, "More", nfc_scene_nfc_data_info_widget_callback, nfc); text_scroll_height = 52; @@ -55,26 +55,25 @@ void nfc_scene_nfc_data_info_on_enter(void* context) { } else if(protocol == NfcDeviceProtocolMifareDesfire) { furi_string_cat_printf(temp_str, "\e#MIFARE DESfire\n"); } else if(protocol == NfcDeviceProtocolNfcV) { - switch (dev_data->nfcv_data.type) - { - case NfcVTypePlain: - furi_string_cat_printf(temp_str, "\e#ISO15693\n"); - break; - case NfcVTypeSlix: - furi_string_cat_printf(temp_str, "\e#ISO15693 SLIX\n"); - break; - case NfcVTypeSlixS: - furi_string_cat_printf(temp_str, "\e#ISO15693 SLIX-S\n"); - break; - case NfcVTypeSlixL: - furi_string_cat_printf(temp_str, "\e#ISO15693 SLIX-L\n"); - break; - case NfcVTypeSlix2: - furi_string_cat_printf(temp_str, "\e#ISO15693 SLIX2\n"); - break; - default: - furi_string_cat_printf(temp_str, "\e#ISO15693 (unknown)\n"); - break; + switch(dev_data->nfcv_data.type) { + case NfcVTypePlain: + furi_string_cat_printf(temp_str, "\e#ISO15693\n"); + break; + case NfcVTypeSlix: + furi_string_cat_printf(temp_str, "\e#ISO15693 SLIX\n"); + break; + case NfcVTypeSlixS: + furi_string_cat_printf(temp_str, "\e#ISO15693 SLIX-S\n"); + break; + case NfcVTypeSlixL: + furi_string_cat_printf(temp_str, "\e#ISO15693 SLIX-L\n"); + break; + case NfcVTypeSlix2: + furi_string_cat_printf(temp_str, "\e#ISO15693 SLIX2\n"); + break; + default: + furi_string_cat_printf(temp_str, "\e#ISO15693 (unknown)\n"); + break; } } else { furi_string_cat_printf(temp_str, "\e#Unknown ISO tag\n"); @@ -83,20 +82,21 @@ void nfc_scene_nfc_data_info_on_enter(void* context) { // Set tag iso data if(protocol == NfcDeviceProtocolNfcV) { NfcVData* nfcv_data = &nfc->dev->dev_data.nfcv_data; - + furi_string_cat_printf(temp_str, "UID:\n"); for(size_t i = 0; i < nfc_data->uid_len; i++) { furi_string_cat_printf(temp_str, " %02X", nfc_data->uid[i]); } furi_string_cat_printf(temp_str, "\n"); - + furi_string_cat_printf(temp_str, "DSFID: %02X\n", nfcv_data->dsfid); furi_string_cat_printf(temp_str, "AFI: %02X\n", nfcv_data->afi); furi_string_cat_printf(temp_str, "IC Ref: %02X\n", nfcv_data->ic_ref); furi_string_cat_printf(temp_str, "Blocks: %02X\n", nfcv_data->block_num); furi_string_cat_printf(temp_str, "Blocksize: %02X\n", nfcv_data->block_size); - furi_string_cat_printf(temp_str, "Data (%d byte)\n", nfcv_data->block_num * nfcv_data->block_size); + furi_string_cat_printf( + temp_str, "Data (%d byte)\n", nfcv_data->block_num * nfcv_data->block_size); int maxBlocks = nfcv_data->block_num; if(maxBlocks > 32) { @@ -106,50 +106,92 @@ void nfc_scene_nfc_data_info_on_enter(void* context) { for(int block = 0; block < maxBlocks; block++) { for(int pos = 0; pos < nfcv_data->block_size; pos++) { - furi_string_cat_printf(temp_str, " %02X", nfcv_data->data[block * nfcv_data->block_size + pos]); + furi_string_cat_printf( + temp_str, " %02X", nfcv_data->data[block * nfcv_data->block_size + pos]); } furi_string_cat_printf(temp_str, "\n"); } furi_string_cat_printf(temp_str, "\n"); - switch (dev_data->nfcv_data.type) - { - case NfcVTypePlain: - furi_string_cat_printf(temp_str, "Type: Plain\n"); - break; - case NfcVTypeSlix: - furi_string_cat_printf(temp_str, "Type: SLIX\n"); - furi_string_cat_printf(temp_str, "Keys:\n"); - furi_string_cat_printf(temp_str, " EAS %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix.key_eas)); - break; - case NfcVTypeSlixS: - furi_string_cat_printf(temp_str, "Type: SLIX-S\n"); - furi_string_cat_printf(temp_str, "Keys:\n"); - furi_string_cat_printf(temp_str, " Read %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_s.key_read)); - furi_string_cat_printf(temp_str, " Write %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_s.key_write)); - furi_string_cat_printf(temp_str, " Privacy %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_s.key_privacy)); - furi_string_cat_printf(temp_str, " Destroy %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_s.key_destroy)); - furi_string_cat_printf(temp_str, " EAS %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_s.key_eas)); - break; - case NfcVTypeSlixL: - furi_string_cat_printf(temp_str, "Type: SLIX-L\n"); - furi_string_cat_printf(temp_str, "Keys:\n"); - furi_string_cat_printf(temp_str, " Privacy %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_l.key_privacy)); - furi_string_cat_printf(temp_str, " Destroy %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_l.key_destroy)); - furi_string_cat_printf(temp_str, " EAS %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_l.key_eas)); - break; - case NfcVTypeSlix2: - furi_string_cat_printf(temp_str, "Type: SLIX2\n"); - furi_string_cat_printf(temp_str, "Keys:\n"); - furi_string_cat_printf(temp_str, " Read %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix2.key_read)); - furi_string_cat_printf(temp_str, " Write %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix2.key_write)); - furi_string_cat_printf(temp_str, " Privacy %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix2.key_privacy)); - furi_string_cat_printf(temp_str, " Destroy %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix2.key_destroy)); - furi_string_cat_printf(temp_str, " EAS %08lX\n", nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix2.key_eas)); - break; - default: - furi_string_cat_printf(temp_str, "\e#ISO15693 (unknown)\n"); - break; + switch(dev_data->nfcv_data.type) { + case NfcVTypePlain: + furi_string_cat_printf(temp_str, "Type: Plain\n"); + break; + case NfcVTypeSlix: + furi_string_cat_printf(temp_str, "Type: SLIX\n"); + furi_string_cat_printf(temp_str, "Keys:\n"); + furi_string_cat_printf( + temp_str, + " EAS %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix.key_eas)); + break; + case NfcVTypeSlixS: + furi_string_cat_printf(temp_str, "Type: SLIX-S\n"); + furi_string_cat_printf(temp_str, "Keys:\n"); + furi_string_cat_printf( + temp_str, + " Read %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_s.key_read)); + furi_string_cat_printf( + temp_str, + " Write %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_s.key_write)); + furi_string_cat_printf( + temp_str, + " Privacy %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_s.key_privacy)); + furi_string_cat_printf( + temp_str, + " Destroy %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_s.key_destroy)); + furi_string_cat_printf( + temp_str, + " EAS %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_s.key_eas)); + break; + case NfcVTypeSlixL: + furi_string_cat_printf(temp_str, "Type: SLIX-L\n"); + furi_string_cat_printf(temp_str, "Keys:\n"); + furi_string_cat_printf( + temp_str, + " Privacy %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_l.key_privacy)); + furi_string_cat_printf( + temp_str, + " Destroy %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_l.key_destroy)); + furi_string_cat_printf( + temp_str, + " EAS %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix_l.key_eas)); + break; + case NfcVTypeSlix2: + furi_string_cat_printf(temp_str, "Type: SLIX2\n"); + furi_string_cat_printf(temp_str, "Keys:\n"); + furi_string_cat_printf( + temp_str, + " Read %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix2.key_read)); + furi_string_cat_printf( + temp_str, + " Write %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix2.key_write)); + furi_string_cat_printf( + temp_str, + " Privacy %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix2.key_privacy)); + furi_string_cat_printf( + temp_str, + " Destroy %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix2.key_destroy)); + furi_string_cat_printf( + temp_str, + " EAS %08lX\n", + nfc_scene_nfc_data_info_get_key(nfcv_data->sub_data.slix2.key_eas)); + break; + default: + furi_string_cat_printf(temp_str, "\e#ISO15693 (unknown)\n"); + break; } } else { char iso_type = FURI_BIT(nfc_data->sak, 5) ? '4' : '3'; @@ -158,7 +200,8 @@ void nfc_scene_nfc_data_info_on_enter(void* context) { for(size_t i = 0; i < nfc_data->uid_len; i++) { furi_string_cat_printf(temp_str, " %02X", nfc_data->uid[i]); } - furi_string_cat_printf(temp_str, "\nATQA: %02X %02X ", nfc_data->atqa[1], nfc_data->atqa[0]); + furi_string_cat_printf( + temp_str, "\nATQA: %02X %02X ", nfc_data->atqa[1], nfc_data->atqa[0]); furi_string_cat_printf(temp_str, " SAK: %02X", nfc_data->sak); } diff --git a/applications/main/nfc/scenes/nfc_scene_nfcv_menu.c b/applications/main/nfc/scenes/nfc_scene_nfcv_menu.c index ee26baf69..b30495a05 100644 --- a/applications/main/nfc/scenes/nfc_scene_nfcv_menu.c +++ b/applications/main/nfc/scenes/nfc_scene_nfcv_menu.c @@ -16,11 +16,10 @@ void nfc_scene_nfcv_menu_on_enter(void* context) { Nfc* nfc = context; Submenu* submenu = nfc->submenu; - submenu_add_item( - submenu, "Save", SubmenuIndexSave, nfc_scene_nfcv_menu_submenu_callback, nfc); + submenu_add_item(submenu, "Save", SubmenuIndexSave, nfc_scene_nfcv_menu_submenu_callback, nfc); submenu_add_item( submenu, "Emulate", SubmenuIndexEmulate, nfc_scene_nfcv_menu_submenu_callback, nfc); - + submenu_set_selected_item( nfc->submenu, scene_manager_get_scene_state(nfc->scene_manager, NfcSceneNfcVMenu)); @@ -46,7 +45,7 @@ bool nfc_scene_nfcv_menu_on_event(void* context, SceneManagerEvent event) { DOLPHIN_DEED(DolphinDeedNfcEmulate); } consumed = true; - } + } scene_manager_set_scene_state(nfc->scene_manager, NfcSceneNfcVMenu, event.event); } else if(event.type == SceneManagerEventTypeBack) { diff --git a/applications/main/nfc/scenes/nfc_scene_nfcv_unlock.c b/applications/main/nfc/scenes/nfc_scene_nfcv_unlock.c index 96293a7c6..4a3ec7667 100644 --- a/applications/main/nfc/scenes/nfc_scene_nfcv_unlock.c +++ b/applications/main/nfc/scenes/nfc_scene_nfcv_unlock.c @@ -30,29 +30,36 @@ void nfc_scene_nfcv_unlock_set_state(Nfc* nfc, NfcSceneNfcVUnlockState state) { FuriHalNfcDevData* nfc_data = &(nfc->dev->dev_data.nfc_data); NfcVData* nfcv_data = &(nfc->dev->dev_data.nfcv_data); - uint32_t curr_state = - scene_manager_get_scene_state(nfc->scene_manager, NfcSceneNfcVUnlock); + uint32_t curr_state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneNfcVUnlock); if(curr_state != state) { Popup* popup = nfc->popup; if(state == NfcSceneNfcVUnlockStateDetecting) { popup_reset(popup); - popup_set_text( - popup, "Put Tonie On\nFlipper's Back", 97, 24, AlignCenter, AlignTop); + popup_set_text(popup, "Put Tonie On\nFlipper's Back", 97, 24, AlignCenter, AlignTop); popup_set_icon(popup, 0, 8, &I_NFC_manual_60x50); } else if(state == NfcSceneNfcVUnlockStateUnlocked) { popup_reset(popup); if(nfc_worker_get_state(nfc->worker) == NfcWorkerStateNfcVUnlockAndSave) { - nfc_text_store_set(nfc, "SLIX_%02X%02X%02X%02X%02X%02X%02X%02X", - nfc_data->uid[0], nfc_data->uid[1], nfc_data->uid[2], nfc_data->uid[3], - nfc_data->uid[4], nfc_data->uid[5], nfc_data->uid[6], nfc_data->uid[7]); + nfc_text_store_set( + nfc, + "SLIX_%02X%02X%02X%02X%02X%02X%02X%02X", + nfc_data->uid[0], + nfc_data->uid[1], + nfc_data->uid[2], + nfc_data->uid[3], + nfc_data->uid[4], + nfc_data->uid[5], + nfc_data->uid[6], + nfc_data->uid[7]); nfc->dev->format = NfcDeviceSaveFormatNfcV; if(nfc_device_save(nfc->dev, nfc->text_store)) { popup_set_header(popup, "Successfully\nsaved", 94, 3, AlignCenter, AlignTop); } else { - popup_set_header(popup, "Unlocked but\nsave failed!", 94, 3, AlignCenter, AlignTop); + popup_set_header( + popup, "Unlocked but\nsave failed!", 94, 3, AlignCenter, AlignTop); } } else { popup_set_header(popup, "Successfully\nunlocked", 94, 3, AlignCenter, AlignTop); @@ -82,13 +89,7 @@ void nfc_scene_nfcv_unlock_set_state(Nfc* nfc, NfcSceneNfcVUnlockState state) { } else if(state == NfcSceneNfcVUnlockStateNotSupportedCard) { popup_reset(popup); popup_set_header(popup, "Wrong Type Of Card!", 64, 3, AlignCenter, AlignTop); - popup_set_text( - popup, - nfcv_data->error, - 4, - 22, - AlignLeft, - AlignTop); + popup_set_text(popup, nfcv_data->error, 4, 22, AlignLeft, AlignTop); popup_set_icon(popup, 73, 20, &I_DolphinCommon_56x48); } scene_manager_set_scene_state(nfc->scene_manager, NfcSceneNfcVUnlock, state); diff --git a/applications/main/nfc/scenes/nfc_scene_nfcv_unlock_menu.c b/applications/main/nfc/scenes/nfc_scene_nfcv_unlock_menu.c index afa19ee41..9c4c81fbd 100644 --- a/applications/main/nfc/scenes/nfc_scene_nfcv_unlock_menu.c +++ b/applications/main/nfc/scenes/nfc_scene_nfcv_unlock_menu.c @@ -16,8 +16,7 @@ void nfc_scene_nfcv_unlock_menu_on_enter(void* context) { Nfc* nfc = context; Submenu* submenu = nfc->submenu; - uint32_t state = - scene_manager_get_scene_state(nfc->scene_manager, NfcSceneNfcVUnlockMenu); + uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneNfcVUnlockMenu); submenu_add_item( submenu, "Enter PWD Manually", @@ -49,8 +48,7 @@ bool nfc_scene_nfcv_unlock_menu_on_event(void* context, SceneManagerEvent event) DOLPHIN_DEED(DolphinDeedNfcRead); consumed = true; } - scene_manager_set_scene_state( - nfc->scene_manager, NfcSceneNfcVUnlockMenu, event.event); + scene_manager_set_scene_state(nfc->scene_manager, NfcSceneNfcVUnlockMenu, event.event); } return consumed; } diff --git a/applications/plugins/nrf24scan/nrf24scan.c b/applications/plugins/nrf24scan/nrf24scan.c index 1ad082c0c..d27c9959c 100644 --- a/applications/plugins/nrf24scan/nrf24scan.c +++ b/applications/plugins/nrf24scan/nrf24scan.c @@ -3,7 +3,7 @@ // ver. 1.1 // #include "nrf24scan.h" - + #include #include #include @@ -15,29 +15,30 @@ #define TAG "nrf24scan" #define MAX_CHANNEL 125 -#define MAX_ADDR 6 +#define MAX_ADDR 6 #define SCAN_APP_PATH_FOLDER "/ext/nrf24scan" -#define ADDR_FILENAME "addr.txt" // File format (1 parameter per line): - // Rate: 0/1/2 - rate in Mbps (=0.25/1/2) - // Ch: 0..125 - default channel - // ESB: 0/1 (1 - Enhanced ShockBurst) - // DPL: 0/1 (1 - Dynamic Payload Length) - // CRC: 0/1/2 (CRC length) - // Payload: 1..32 (bytes) - // P0: address P0 in hex (5 byte, LSB last) - // P1: address P1 in hex (5 byte, LSB last) - // P2: address P2, LSB in hex (1 byte) - // P3: address P3, LSB in hex (1 byte) - // P4: address P4, LSB in hex (1 byte) - // P5: address P5, LSB in hex (1 byte) - // captured data in raw format, first byte = address # 0..5, Payload len if DPL - // ... up to MAX_LOG_RECORDS-1 +#define ADDR_FILENAME \ + "addr.txt" // File format (1 parameter per line): \ + // Rate: 0/1/2 - rate in Mbps (=0.25/1/2) \ + // Ch: 0..125 - default channel \ + // ESB: 0/1 (1 - Enhanced ShockBurst) \ + // DPL: 0/1 (1 - Dynamic Payload Length) \ + // CRC: 0/1/2 (CRC length) \ + // Payload: 1..32 (bytes) \ + // P0: address P0 in hex (5 byte, LSB last) \ + // P1: address P1 in hex (5 byte, LSB last) \ + // P2: address P2, LSB in hex (1 byte) \ + // P3: address P3, LSB in hex (1 byte) \ + // P4: address P4, LSB in hex (1 byte) \ + // P5: address P5, LSB in hex (1 byte) \ + // captured data in raw format, first byte = address # 0..5, Payload len if DPL \ + // ... up to MAX_LOG_RECORDS-1 #define LOG_FILENAME "log" -#define LOG_FILEEXT ".txt" -#define MAX_LOG_RECORDS 100 -#define LOG_REC_SIZE 33 // max packet size -#define VIEW_LOG_MAX_X 22 +#define LOG_FILEEXT ".txt" +#define MAX_LOG_RECORDS 100 +#define LOG_REC_SIZE 33 // max packet size +#define VIEW_LOG_MAX_X 22 const char SettingsFld_Rate[] = "Rate:"; const char SettingsFld_Ch[] = "Ch:"; @@ -52,25 +53,25 @@ uint8_t what_doing = 0; // 0 - setup, 1 - view log, 2 - view addresses uint8_t what_to_do = 1; // 0 - view, 1 - view & scan char screen_buf[64]; char addr_file_name[32]; -uint8_t NRF_rate = 1; // 0 - 250Kbps, 1 - 1Mbps, 2 - 2Mbps -uint8_t NRF_channel = 0;// 0..125 -uint8_t NRF_ESB = 0; // 0 - ShockBurst, 1 - Enhanced ShockBurst -uint8_t NRF_DPL = 0; // 1 - Dynamic Payload Length -uint8_t NRF_CRC = 0; // 1 - No, 1 - CRC 1byte, 2 - CRC 2byte -uint8_t NRF_Payload = 32;// len in bytes, max 32 +uint8_t NRF_rate = 1; // 0 - 250Kbps, 1 - 1Mbps, 2 - 2Mbps +uint8_t NRF_channel = 0; // 0..125 +uint8_t NRF_ESB = 0; // 0 - ShockBurst, 1 - Enhanced ShockBurst +uint8_t NRF_DPL = 0; // 1 - Dynamic Payload Length +uint8_t NRF_CRC = 0; // 1 - No, 1 - CRC 1byte, 2 - CRC 2byte +uint8_t NRF_Payload = 32; // len in bytes, max 32 struct { - uint8_t addr_P0[5]; // MSB first - uint8_t addr_P1[5]; // MSB first - uint8_t addr_P2; // LSB only, MSB bytes equal addr_P1 - uint8_t addr_P3; // LSB only, MSB bytes equal addr_P1 - uint8_t addr_P4; // LSB only, MSB bytes equal addr_P1 - uint8_t addr_P5; // LSB only, MSB bytes equal addr_P1 - uint8_t addr_len; // 2..5 - uint8_t addr_count; + uint8_t addr_P0[5]; // MSB first + uint8_t addr_P1[5]; // MSB first + uint8_t addr_P2; // LSB only, MSB bytes equal addr_P1 + uint8_t addr_P3; // LSB only, MSB bytes equal addr_P1 + uint8_t addr_P4; // LSB only, MSB bytes equal addr_P1 + uint8_t addr_P5; // LSB only, MSB bytes equal addr_P1 + uint8_t addr_len; // 2..5 + uint8_t addr_count; } addrs; -int8_t log_to_file = 0; // 0 - no, 1 - yes(new), 2 - append, -1 - only clear +int8_t log_to_file = 0; // 0 - no, 1 - yes(new), 2 - append, -1 - only clear uint16_t log_arr_idx; uint16_t view_log_arr_idx = 0; uint16_t view_log_arr_x = 0; @@ -79,667 +80,799 @@ uint16_t last_packet_send = -1; uint8_t last_packet_send_st = 0; int16_t find_channel_period = 0; // sec uint8_t menu_selected = 0; -uint32_t start_time; +uint32_t start_time; #define menu_selected_max 5 enum { - Menu_open_file = 0, - Menu_enter_channel, - Menu_enter_rate, - Menu_enter_scan_period, - Menu_log, - Menu_ok + Menu_open_file = 0, + Menu_enter_channel, + Menu_enter_rate, + Menu_enter_scan_period, + Menu_log, + Menu_ok }; -#define MIN(a, b) ((alog_arr = malloc(LOG_REC_SIZE * MAX_LOG_RECORDS); - if(APP->log_arr == NULL) { - FURI_LOG_E(TAG, "Not enouch memory: %d", LOG_REC_SIZE * MAX_LOG_RECORDS); - strcpy(addr_file_name, "MEMORY LOW!"); - } - clear_log(); +void allocate_log_array() { + APP->log_arr = malloc(LOG_REC_SIZE * MAX_LOG_RECORDS); + if(APP->log_arr == NULL) { + FURI_LOG_E(TAG, "Not enouch memory: %d", LOG_REC_SIZE * MAX_LOG_RECORDS); + strcpy(addr_file_name, "MEMORY LOW!"); + } + clear_log(); } -void write_to_log_file(Storage* storage) -{ - if(log_arr_idx == 0) return; - Stream* file_stream = file_stream_alloc(storage); - FuriString* str = furi_string_alloc(); - furi_string_set(str, SCAN_APP_PATH_FOLDER); - furi_string_cat(str, "/"); - furi_string_cat(str, LOG_FILENAME); - furi_string_cat(str, LOG_FILEEXT); - bool fl; - if(save_to_new_log) { - int cnt = 1; - do { - fl = file_stream_open(file_stream, furi_string_get_cstr(str), FSAM_READ_WRITE, FSOM_CREATE_NEW); - if(fl) break; - file_stream_close(file_stream); - furi_string_set(str, SCAN_APP_PATH_FOLDER); - furi_string_cat(str, "/"); - furi_string_cat(str, LOG_FILENAME); - furi_string_cat_printf(str, "-%02d", cnt); - furi_string_cat(str, LOG_FILEEXT); - } while(++cnt < 100); - if(!fl) { - FURI_LOG_E(TAG, "Failed to create new log file"); - notification_message(APP->notification, &sequence_blink_red_100); - } - } else { - fl = file_stream_open(file_stream, furi_string_get_cstr(str), FSAM_READ_WRITE, FSOM_OPEN_APPEND); - if(fl) { - if(stream_size(file_stream) == 0) save_to_new_log = true; - } else file_stream_close(file_stream); - } - if(fl) { - FURI_LOG_D(TAG, "Save to log %s", furi_string_get_cstr(str)); - if(save_to_new_log) { - furi_string_printf(str, "%s %d\n%s %d\n%s %d\n", SettingsFld_Rate, NRF_rate, SettingsFld_Ch, NRF_channel, SettingsFld_ESB, NRF_ESB); - furi_string_cat_printf(str, "%s %d\n%s %d\n%s %d\n", SettingsFld_DPL, NRF_DPL, SettingsFld_CRC, NRF_CRC, SettingsFld_Payload, NRF_Payload); - furi_string_cat_printf(str, "P0: "); - add_to_furi_str_hex_bytes(str, (char*)addrs.addr_P0, addrs.addr_len); furi_string_cat(str, "\n"); - if(addrs.addr_count > 1) { furi_string_cat_printf(str, "P1: "); add_to_furi_str_hex_bytes(str, (char*)addrs.addr_P1, addrs.addr_len); furi_string_cat(str, "\n"); } - if(addrs.addr_count > 2) { furi_string_cat_printf(str, "P2: "); furi_string_cat_printf(str, "%02X\n", addrs.addr_P2); } - if(addrs.addr_count > 3) { furi_string_cat_printf(str, "P3: "); furi_string_cat_printf(str, "%02X\n", addrs.addr_P3); } - if(addrs.addr_count > 4) { furi_string_cat_printf(str, "P4: "); furi_string_cat_printf(str, "%02X\n", addrs.addr_P4); } - if(addrs.addr_count > 5) { furi_string_cat_printf(str, "P5: "); furi_string_cat_printf(str, "%02X\n", addrs.addr_P5); } - if(!(fl = stream_write_string(file_stream, str) == furi_string_size(str))) { - FURI_LOG_E(TAG, "Failed to write header to log!"); - notification_message(APP->notification, &sequence_blink_red_100); - } - } - if(fl) { - int i = 0; - for(; i < log_arr_idx; i++) { - furi_string_reset(str); - add_to_furi_str_hex_bytes(str, (char*)APP->log_arr + i * LOG_REC_SIZE, LOG_REC_SIZE); - furi_string_cat(str, "\n"); - if(stream_write_string(file_stream, str) != furi_string_size(str)) { - FURI_LOG_E(TAG, "Failed to write to log!"); - break; - } - } - if(i == log_arr_idx) { - notification_message(APP->notification, &sequence_blink_yellow_100); - FURI_LOG_D(TAG, "Log saved"); - } - } - save_to_new_log = false; - file_stream_close(file_stream); - } else { - FURI_LOG_E(TAG, "Failed to open log file"); - notification_message(APP->notification, &sequence_blink_red_100); - } - stream_free(file_stream); - furi_string_free(str); +void write_to_log_file(Storage* storage) { + if(log_arr_idx == 0) return; + Stream* file_stream = file_stream_alloc(storage); + FuriString* str = furi_string_alloc(); + furi_string_set(str, SCAN_APP_PATH_FOLDER); + furi_string_cat(str, "/"); + furi_string_cat(str, LOG_FILENAME); + furi_string_cat(str, LOG_FILEEXT); + bool fl; + if(save_to_new_log) { + int cnt = 1; + do { + fl = file_stream_open( + file_stream, furi_string_get_cstr(str), FSAM_READ_WRITE, FSOM_CREATE_NEW); + if(fl) break; + file_stream_close(file_stream); + furi_string_set(str, SCAN_APP_PATH_FOLDER); + furi_string_cat(str, "/"); + furi_string_cat(str, LOG_FILENAME); + furi_string_cat_printf(str, "-%02d", cnt); + furi_string_cat(str, LOG_FILEEXT); + } while(++cnt < 100); + if(!fl) { + FURI_LOG_E(TAG, "Failed to create new log file"); + notification_message(APP->notification, &sequence_blink_red_100); + } + } else { + fl = file_stream_open( + file_stream, furi_string_get_cstr(str), FSAM_READ_WRITE, FSOM_OPEN_APPEND); + if(fl) { + if(stream_size(file_stream) == 0) save_to_new_log = true; + } else + file_stream_close(file_stream); + } + if(fl) { + FURI_LOG_D(TAG, "Save to log %s", furi_string_get_cstr(str)); + if(save_to_new_log) { + furi_string_printf( + str, + "%s %d\n%s %d\n%s %d\n", + SettingsFld_Rate, + NRF_rate, + SettingsFld_Ch, + NRF_channel, + SettingsFld_ESB, + NRF_ESB); + furi_string_cat_printf( + str, + "%s %d\n%s %d\n%s %d\n", + SettingsFld_DPL, + NRF_DPL, + SettingsFld_CRC, + NRF_CRC, + SettingsFld_Payload, + NRF_Payload); + furi_string_cat_printf(str, "P0: "); + add_to_furi_str_hex_bytes(str, (char*)addrs.addr_P0, addrs.addr_len); + furi_string_cat(str, "\n"); + if(addrs.addr_count > 1) { + furi_string_cat_printf(str, "P1: "); + add_to_furi_str_hex_bytes(str, (char*)addrs.addr_P1, addrs.addr_len); + furi_string_cat(str, "\n"); + } + if(addrs.addr_count > 2) { + furi_string_cat_printf(str, "P2: "); + furi_string_cat_printf(str, "%02X\n", addrs.addr_P2); + } + if(addrs.addr_count > 3) { + furi_string_cat_printf(str, "P3: "); + furi_string_cat_printf(str, "%02X\n", addrs.addr_P3); + } + if(addrs.addr_count > 4) { + furi_string_cat_printf(str, "P4: "); + furi_string_cat_printf(str, "%02X\n", addrs.addr_P4); + } + if(addrs.addr_count > 5) { + furi_string_cat_printf(str, "P5: "); + furi_string_cat_printf(str, "%02X\n", addrs.addr_P5); + } + if(!(fl = stream_write_string(file_stream, str) == furi_string_size(str))) { + FURI_LOG_E(TAG, "Failed to write header to log!"); + notification_message(APP->notification, &sequence_blink_red_100); + } + } + if(fl) { + int i = 0; + for(; i < log_arr_idx; i++) { + furi_string_reset(str); + add_to_furi_str_hex_bytes( + str, (char*)APP->log_arr + i * LOG_REC_SIZE, LOG_REC_SIZE); + furi_string_cat(str, "\n"); + if(stream_write_string(file_stream, str) != furi_string_size(str)) { + FURI_LOG_E(TAG, "Failed to write to log!"); + break; + } + } + if(i == log_arr_idx) { + notification_message(APP->notification, &sequence_blink_yellow_100); + FURI_LOG_D(TAG, "Log saved"); + } + } + save_to_new_log = false; + file_stream_close(file_stream); + } else { + FURI_LOG_E(TAG, "Failed to open log file"); + notification_message(APP->notification, &sequence_blink_red_100); + } + stream_free(file_stream); + furi_string_free(str); } static bool select_settings_file(Stream* stream) { - DialogsApp* dialogs = furi_record_open("dialogs"); - bool result = false; - FuriString* path; - path = furi_string_alloc(); - furi_string_set(path, SCAN_APP_PATH_FOLDER); + DialogsApp* dialogs = furi_record_open("dialogs"); + bool result = false; + FuriString* path; + path = furi_string_alloc(); + furi_string_set(path, SCAN_APP_PATH_FOLDER); - DialogsFileBrowserOptions browser_options; - dialog_file_browser_set_basic_options(&browser_options, ".txt", NULL); - browser_options.hide_ext = false; + DialogsFileBrowserOptions browser_options; + dialog_file_browser_set_basic_options(&browser_options, ".txt", NULL); + browser_options.hide_ext = false; - bool ret = dialog_file_browser_show(dialogs, path, path, &browser_options); + bool ret = dialog_file_browser_show(dialogs, path, path, &browser_options); - furi_record_close("dialogs"); - if(ret) { - if(!file_stream_open(stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) { - FURI_LOG_D(TAG, "Cannot open file \"%s\"", furi_string_get_cstr(path)); - file_stream_close(stream); - } else { - FURI_LOG_D(TAG, "Open file \"%s\"", furi_string_get_cstr(path)); - strncpy(addr_file_name, furi_string_get_cstr(path) + sizeof(SCAN_APP_PATH_FOLDER), sizeof(addr_file_name)); - result = true; - } - } - furi_string_free(path); - return result; + furi_record_close("dialogs"); + if(ret) { + if(!file_stream_open(stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) { + FURI_LOG_D(TAG, "Cannot open file \"%s\"", furi_string_get_cstr(path)); + file_stream_close(stream); + } else { + FURI_LOG_D(TAG, "Open file \"%s\"", furi_string_get_cstr(path)); + strncpy( + addr_file_name, + furi_string_get_cstr(path) + sizeof(SCAN_APP_PATH_FOLDER), + sizeof(addr_file_name)); + result = true; + } + } + furi_string_free(path); + return result; } // 0 - success, otherwise an error static uint8_t load_settings_file(Stream* file_stream) { - size_t file_size = 0; - char* file_buf; - bool loaded = false; - file_size = stream_size(file_stream); - if(file_size == (size_t)0) { - FURI_LOG_D(TAG, "load failed. file_size: %d", file_size); - return 1; - } - file_buf = malloc(MIN(file_size + 1, LOG_REC_SIZE * MAX_LOG_RECORDS * 2 + 100)); - if(file_buf == NULL) { - FURI_LOG_D(TAG, "Memory low, need: %d", file_size); - return 2; - } - memset(file_buf, 0, file_size); - if(stream_read(file_stream, (uint8_t*)file_buf, file_size) == file_size) { - FURI_LOG_D(TAG, "Loading settings file"); - char* line_ptr = file_buf; - int16_t line_num = 0; - memset((uint8_t*)&addrs, 0, sizeof(addrs)); - bool log_loaded = 3; - while(line_ptr && line_ptr - file_buf < file_size) { - char* end_ptr = strstr((char*)line_ptr, "\n"); - if(end_ptr == NULL) end_ptr = file_buf + file_size; else *end_ptr = '\0'; - int line_len = end_ptr - line_ptr; - if(*line_ptr == '\r' || line_len == 0) { - line_ptr = end_ptr + 1; - continue; - } - if(*(end_ptr - 1) < '0') { - *(end_ptr - 1) = '\0'; - line_len--; - } - FURI_LOG_D(TAG, " L#%d: [%d]%s", line_num, line_len, line_ptr); - if(strncmp(line_ptr, SettingsFld_Rate, sizeof(SettingsFld_Rate)-1) == 0) { - NRF_rate = atoi(line_ptr + sizeof(SettingsFld_Rate)); - } else if(strncmp(line_ptr, SettingsFld_Ch, sizeof(SettingsFld_Ch)-1) == 0) { - NRF_channel = atoi(line_ptr + sizeof(SettingsFld_Ch)); - } else if(strncmp(line_ptr, SettingsFld_ESB, sizeof(SettingsFld_ESB)-1) == 0) { - NRF_ESB = atoi(line_ptr + sizeof(SettingsFld_ESB)); - } else if(strncmp(line_ptr, SettingsFld_DPL, sizeof(SettingsFld_DPL)-1) == 0) { - NRF_DPL = atoi(line_ptr + sizeof(SettingsFld_DPL)); - } else if(strncmp(line_ptr, SettingsFld_CRC, sizeof(SettingsFld_CRC)-1) == 0) { - NRF_CRC = atoi(line_ptr + sizeof(SettingsFld_CRC)); - } else if(strncmp(line_ptr, SettingsFld_Payload, sizeof(SettingsFld_Payload)-1) == 0) { - NRF_Payload = atoi(line_ptr + sizeof(SettingsFld_Payload)); - if(NRF_Payload == 0 || NRF_Payload > 32) NRF_Payload = 32; - } else if(*line_ptr == SettingsFld_Addr) { - char a = *(++line_ptr); - line_ptr += 3; - switch(a) { - case '0': - addrs.addr_len = ConvertHexToArray(line_ptr, addrs.addr_P0, 5); - FURI_LOG_D(TAG, " +Addr(%d): %02X%02X%02X...", addrs.addr_len, addrs.addr_P0[0], addrs.addr_P0[1], addrs.addr_P0[2]); - loaded = true; - break; - case '1': - ConvertHexToArray(line_ptr, addrs.addr_P1, 5); - FURI_LOG_D(TAG, " +Addr: %02X%02X%02X...", addrs.addr_P0[1], addrs.addr_P1[1], addrs.addr_P1[2]); - break; - case '2': - ConvertHexToArray(line_ptr, &addrs.addr_P2, 1); - break; - case '3': - ConvertHexToArray(line_ptr, &addrs.addr_P3, 1); - break; - case '4': - ConvertHexToArray(line_ptr, &addrs.addr_P4, 1); - break; - case '5': - ConvertHexToArray(line_ptr, &addrs.addr_P5, 1); - break; - default: - a = 0; - break; - } - if(a) addrs.addr_count = a - '0' + 1; - } else if(line_len >= LOG_REC_SIZE * 2) { // data - if(!log_loaded) { - clear_log(); - what_to_do = 0; - log_loaded = 0; - } - if(log_arr_idx < MAX_LOG_RECORDS - 1) { - ConvertHexToArray(line_ptr, APP->log_arr + log_arr_idx * LOG_REC_SIZE, LOG_REC_SIZE); - log_arr_idx++; - } - } - line_ptr = end_ptr + 1; - line_num++; - } - } else { - FURI_LOG_D(TAG, "load failed. file size: %d", file_size); - loaded = 4; - } - free(file_buf); - return loaded; + size_t file_size = 0; + char* file_buf; + bool loaded = false; + file_size = stream_size(file_stream); + if(file_size == (size_t)0) { + FURI_LOG_D(TAG, "load failed. file_size: %d", file_size); + return 1; + } + file_buf = malloc(MIN(file_size + 1, LOG_REC_SIZE * MAX_LOG_RECORDS * 2 + 100)); + if(file_buf == NULL) { + FURI_LOG_D(TAG, "Memory low, need: %d", file_size); + return 2; + } + memset(file_buf, 0, file_size); + if(stream_read(file_stream, (uint8_t*)file_buf, file_size) == file_size) { + FURI_LOG_D(TAG, "Loading settings file"); + char* line_ptr = file_buf; + int16_t line_num = 0; + memset((uint8_t*)&addrs, 0, sizeof(addrs)); + bool log_loaded = 3; + while(line_ptr && line_ptr - file_buf < file_size) { + char* end_ptr = strstr((char*)line_ptr, "\n"); + if(end_ptr == NULL) + end_ptr = file_buf + file_size; + else + *end_ptr = '\0'; + int line_len = end_ptr - line_ptr; + if(*line_ptr == '\r' || line_len == 0) { + line_ptr = end_ptr + 1; + continue; + } + if(*(end_ptr - 1) < '0') { + *(end_ptr - 1) = '\0'; + line_len--; + } + FURI_LOG_D(TAG, " L#%d: [%d]%s", line_num, line_len, line_ptr); + if(strncmp(line_ptr, SettingsFld_Rate, sizeof(SettingsFld_Rate) - 1) == 0) { + NRF_rate = atoi(line_ptr + sizeof(SettingsFld_Rate)); + } else if(strncmp(line_ptr, SettingsFld_Ch, sizeof(SettingsFld_Ch) - 1) == 0) { + NRF_channel = atoi(line_ptr + sizeof(SettingsFld_Ch)); + } else if(strncmp(line_ptr, SettingsFld_ESB, sizeof(SettingsFld_ESB) - 1) == 0) { + NRF_ESB = atoi(line_ptr + sizeof(SettingsFld_ESB)); + } else if(strncmp(line_ptr, SettingsFld_DPL, sizeof(SettingsFld_DPL) - 1) == 0) { + NRF_DPL = atoi(line_ptr + sizeof(SettingsFld_DPL)); + } else if(strncmp(line_ptr, SettingsFld_CRC, sizeof(SettingsFld_CRC) - 1) == 0) { + NRF_CRC = atoi(line_ptr + sizeof(SettingsFld_CRC)); + } else if(strncmp(line_ptr, SettingsFld_Payload, sizeof(SettingsFld_Payload) - 1) == 0) { + NRF_Payload = atoi(line_ptr + sizeof(SettingsFld_Payload)); + if(NRF_Payload == 0 || NRF_Payload > 32) NRF_Payload = 32; + } else if(*line_ptr == SettingsFld_Addr) { + char a = *(++line_ptr); + line_ptr += 3; + switch(a) { + case '0': + addrs.addr_len = ConvertHexToArray(line_ptr, addrs.addr_P0, 5); + FURI_LOG_D( + TAG, + " +Addr(%d): %02X%02X%02X...", + addrs.addr_len, + addrs.addr_P0[0], + addrs.addr_P0[1], + addrs.addr_P0[2]); + loaded = true; + break; + case '1': + ConvertHexToArray(line_ptr, addrs.addr_P1, 5); + FURI_LOG_D( + TAG, + " +Addr: %02X%02X%02X...", + addrs.addr_P0[1], + addrs.addr_P1[1], + addrs.addr_P1[2]); + break; + case '2': + ConvertHexToArray(line_ptr, &addrs.addr_P2, 1); + break; + case '3': + ConvertHexToArray(line_ptr, &addrs.addr_P3, 1); + break; + case '4': + ConvertHexToArray(line_ptr, &addrs.addr_P4, 1); + break; + case '5': + ConvertHexToArray(line_ptr, &addrs.addr_P5, 1); + break; + default: + a = 0; + break; + } + if(a) addrs.addr_count = a - '0' + 1; + } else if(line_len >= LOG_REC_SIZE * 2) { // data + if(!log_loaded) { + clear_log(); + what_to_do = 0; + log_loaded = 0; + } + if(log_arr_idx < MAX_LOG_RECORDS - 1) { + ConvertHexToArray( + line_ptr, APP->log_arr + log_arr_idx * LOG_REC_SIZE, LOG_REC_SIZE); + log_arr_idx++; + } + } + line_ptr = end_ptr + 1; + line_num++; + } + } else { + FURI_LOG_D(TAG, "load failed. file size: %d", file_size); + loaded = 4; + } + free(file_buf); + return loaded; } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { - furi_assert(event_queue); - PluginEvent event = {.type = EventTypeKey, .input = *input_event}; - furi_message_queue_put(event_queue, &event, FuriWaitForever); + furi_assert(event_queue); + PluginEvent event = {.type = EventTypeKey, .input = *input_event}; + furi_message_queue_put(event_queue, &event, FuriWaitForever); } -static void prepare_nrf24() -{ - uint8_t addr[5]; - uint8_t erx_addr = (1<<0); // Enable RX_P0 - if(addrs.addr_count == 0) return; - nrf24_write_reg(nrf24_HANDLE, REG_CONFIG, NRF_CRC == 1 ? 0b1000 : NRF_CRC == 2 ? 0b1100 : 0); - nrf24_write_reg(nrf24_HANDLE, REG_STATUS, 0x70); // clear interrupts - nrf24_write_reg(nrf24_HANDLE, REG_SETUP_RETR, NRF_ESB ? 1 : 0); // Disable Automatic Retransmission - nrf24_write_reg(nrf24_HANDLE, REG_EN_AA, NRF_ESB ? 0x3F : 0); // Auto acknowledgement - nrf24_write_reg(nrf24_HANDLE, REG_FEATURE, 1 + (NRF_DPL || NRF_ESB ? 4 : 1)); // Enables the W_TX_PAYLOAD_NOACK command, Disable Payload with ACK, set Dynamic Payload - nrf24_set_maclen(nrf24_HANDLE, addrs.addr_len); - for(int i = 0; i < addrs.addr_len; i++) addr[i] = addrs.addr_P0[addrs.addr_len - i - 1]; - nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P0, &addr[0], addrs.addr_len); - nrf24_write_reg(nrf24_HANDLE, RX_PW_P0, NRF_Payload); - if(addrs.addr_count == 1) nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1<<0) : 0); // Enable dynamic payload reg - if(addrs.addr_count > 1) { - for(int i = 0; i < addrs.addr_len; i++) addr[i] = addrs.addr_P1[addrs.addr_len - i - 1]; - nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P1, &addr[0], addrs.addr_len); - nrf24_write_reg(nrf24_HANDLE, RX_PW_P1, NRF_Payload); - nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1<<1) : 0); - erx_addr |= (1<<1); // Enable RX_P1 - } else nrf24_write_reg(nrf24_HANDLE, RX_PW_P1, 0); - if(addrs.addr_count > 2) { - nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P2, &addrs.addr_P2, 1); - nrf24_write_reg(nrf24_HANDLE, RX_PW_P2, NRF_Payload); - nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1<<2) : 0); - erx_addr |= (1<<2); // Enable RX_P2 - } else nrf24_write_reg(nrf24_HANDLE, RX_PW_P2, 0); - if(addrs.addr_count > 3) { - nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P3, &addrs.addr_P3, 1); - nrf24_write_reg(nrf24_HANDLE, RX_PW_P3, NRF_Payload); - nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1<<3) : 0); - erx_addr |= (1<<3); // Enable RX_P3 - } else nrf24_write_reg(nrf24_HANDLE, RX_PW_P3, 0); - if(addrs.addr_count > 4) { - nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P4, &addrs.addr_P4, 1); - nrf24_write_reg(nrf24_HANDLE, RX_PW_P4, NRF_Payload); - nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1<<4) : 0); - erx_addr |= (1<<4); // Enable RX_P4 - } else nrf24_write_reg(nrf24_HANDLE, RX_PW_P4, 0); - if(addrs.addr_count > 5) { - nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P5, &addrs.addr_P5, 1); - nrf24_write_reg(nrf24_HANDLE, RX_PW_P5, NRF_Payload); - nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1<<5) : 0); - erx_addr |= (1<<5); // Enable RX_P5 - } else nrf24_write_reg(nrf24_HANDLE, RX_PW_P5, 0); - nrf24_write_reg(nrf24_HANDLE, REG_STATUS, 0x50); // clear RX_DR, MAX_RT. - nrf24_write_reg(nrf24_HANDLE, REG_EN_RXADDR, erx_addr); - nrf24_write_reg(nrf24_HANDLE, REG_RF_CH, NRF_channel); - nrf24_write_reg(nrf24_HANDLE, REG_RF_SETUP, NRF_rate); - nrf24_flush_rx(nrf24_HANDLE); - nrf24_flush_tx(nrf24_HANDLE); - nrf24_set_idle(nrf24_HANDLE); +static void prepare_nrf24() { + uint8_t addr[5]; + uint8_t erx_addr = (1 << 0); // Enable RX_P0 + if(addrs.addr_count == 0) return; + nrf24_write_reg(nrf24_HANDLE, REG_CONFIG, NRF_CRC == 1 ? 0b1000 : NRF_CRC == 2 ? 0b1100 : 0); + nrf24_write_reg(nrf24_HANDLE, REG_STATUS, 0x70); // clear interrupts + nrf24_write_reg( + nrf24_HANDLE, REG_SETUP_RETR, NRF_ESB ? 1 : 0); // Disable Automatic Retransmission + nrf24_write_reg(nrf24_HANDLE, REG_EN_AA, NRF_ESB ? 0x3F : 0); // Auto acknowledgement + nrf24_write_reg( + nrf24_HANDLE, + REG_FEATURE, + 1 + (NRF_DPL || NRF_ESB ? + 4 : + 1)); // Enables the W_TX_PAYLOAD_NOACK command, Disable Payload with ACK, set Dynamic Payload + nrf24_set_maclen(nrf24_HANDLE, addrs.addr_len); + for(int i = 0; i < addrs.addr_len; i++) addr[i] = addrs.addr_P0[addrs.addr_len - i - 1]; + nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P0, &addr[0], addrs.addr_len); + nrf24_write_reg(nrf24_HANDLE, RX_PW_P0, NRF_Payload); + if(addrs.addr_count == 1) + nrf24_write_reg( + nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1 << 0) : 0); // Enable dynamic payload reg + if(addrs.addr_count > 1) { + for(int i = 0; i < addrs.addr_len; i++) addr[i] = addrs.addr_P1[addrs.addr_len - i - 1]; + nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P1, &addr[0], addrs.addr_len); + nrf24_write_reg(nrf24_HANDLE, RX_PW_P1, NRF_Payload); + nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1 << 1) : 0); + erx_addr |= (1 << 1); // Enable RX_P1 + } else + nrf24_write_reg(nrf24_HANDLE, RX_PW_P1, 0); + if(addrs.addr_count > 2) { + nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P2, &addrs.addr_P2, 1); + nrf24_write_reg(nrf24_HANDLE, RX_PW_P2, NRF_Payload); + nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1 << 2) : 0); + erx_addr |= (1 << 2); // Enable RX_P2 + } else + nrf24_write_reg(nrf24_HANDLE, RX_PW_P2, 0); + if(addrs.addr_count > 3) { + nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P3, &addrs.addr_P3, 1); + nrf24_write_reg(nrf24_HANDLE, RX_PW_P3, NRF_Payload); + nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1 << 3) : 0); + erx_addr |= (1 << 3); // Enable RX_P3 + } else + nrf24_write_reg(nrf24_HANDLE, RX_PW_P3, 0); + if(addrs.addr_count > 4) { + nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P4, &addrs.addr_P4, 1); + nrf24_write_reg(nrf24_HANDLE, RX_PW_P4, NRF_Payload); + nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1 << 4) : 0); + erx_addr |= (1 << 4); // Enable RX_P4 + } else + nrf24_write_reg(nrf24_HANDLE, RX_PW_P4, 0); + if(addrs.addr_count > 5) { + nrf24_write_buf_reg(nrf24_HANDLE, REG_RX_ADDR_P5, &addrs.addr_P5, 1); + nrf24_write_reg(nrf24_HANDLE, RX_PW_P5, NRF_Payload); + nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? (1 << 5) : 0); + erx_addr |= (1 << 5); // Enable RX_P5 + } else + nrf24_write_reg(nrf24_HANDLE, RX_PW_P5, 0); + nrf24_write_reg(nrf24_HANDLE, REG_STATUS, 0x50); // clear RX_DR, MAX_RT. + nrf24_write_reg(nrf24_HANDLE, REG_EN_RXADDR, erx_addr); + nrf24_write_reg(nrf24_HANDLE, REG_RF_CH, NRF_channel); + nrf24_write_reg(nrf24_HANDLE, REG_RF_SETUP, NRF_rate); + nrf24_flush_rx(nrf24_HANDLE); + nrf24_flush_tx(nrf24_HANDLE); + nrf24_set_idle(nrf24_HANDLE); } -static void start_scanning() -{ - prepare_nrf24(); - nrf24_set_rx_mode(nrf24_HANDLE); - start_time = furi_get_tick(); - FURI_LOG_D(TAG, "Start scan: Ch=%d Rate=%d", NRF_channel, NRF_rate); +static void start_scanning() { + prepare_nrf24(); + nrf24_set_rx_mode(nrf24_HANDLE); + start_time = furi_get_tick(); + FURI_LOG_D(TAG, "Start scan: Ch=%d Rate=%d", NRF_channel, NRF_rate); } bool nrf24_read_newpacket() { - if(APP->log_arr == NULL) return false; - bool found = false; - uint8_t packetsize; - uint8_t packet[32] = {0}; - uint8_t *ptr = APP->log_arr + log_arr_idx * LOG_REC_SIZE; - uint8_t status = nrf24_rxpacket(nrf24_HANDLE, ptr + 1, &packetsize, !NRF_DPL); - if(status & 0x40) { - *ptr = ((packetsize & 0x1F) << 3) | ((status >> 1) & 7); // payload size + pipe # - if(packetsize < 32) memset(ptr + packetsize + 1, 0, 32 - packetsize); - if(log_arr_idx < MAX_LOG_RECORDS - 1) { - log_arr_idx++; - } else { - if(log_to_file == 1 || log_to_file == 2) { - write_to_log_file(APP->storage); - clear_log(); - } else { - memmove(APP->log_arr, APP->log_arr + LOG_REC_SIZE, log_arr_idx * LOG_REC_SIZE); - } - } - FURI_LOG_D(TAG, "Found packet #%d pipe %d", log_arr_idx, (status >> 1) & 7); - notification_message(APP->notification, &sequence_blink_white_100); - found = true; - } - return found; + if(APP->log_arr == NULL) return false; + bool found = false; + uint8_t packetsize; + uint8_t packet[32] = {0}; + uint8_t* ptr = APP->log_arr + log_arr_idx * LOG_REC_SIZE; + uint8_t status = nrf24_rxpacket(nrf24_HANDLE, ptr + 1, &packetsize, !NRF_DPL); + if(status & 0x40) { + *ptr = ((packetsize & 0x1F) << 3) | ((status >> 1) & 7); // payload size + pipe # + if(packetsize < 32) memset(ptr + packetsize + 1, 0, 32 - packetsize); + if(log_arr_idx < MAX_LOG_RECORDS - 1) { + log_arr_idx++; + } else { + if(log_to_file == 1 || log_to_file == 2) { + write_to_log_file(APP->storage); + clear_log(); + } else { + memmove(APP->log_arr, APP->log_arr + LOG_REC_SIZE, log_arr_idx * LOG_REC_SIZE); + } + } + FURI_LOG_D(TAG, "Found packet #%d pipe %d", log_arr_idx, (status >> 1) & 7); + notification_message(APP->notification, &sequence_blink_white_100); + found = true; + } + return found; } -bool nrf24_send_packet() -{ - if(log_arr_idx == 0) return false; - if(!what_to_do) prepare_nrf24(); - last_packet_send_st = nrf24_txpacket(nrf24_HANDLE, APP->log_arr + view_log_arr_idx * LOG_REC_SIZE + 1, 32, false); - last_packet_send = view_log_arr_idx; - notification_message(APP->notification, last_packet_send_st ? &sequence_blink_blue_100 : &sequence_blink_red_100); - if(what_to_do) start_scanning(); - return last_packet_send_st; +bool nrf24_send_packet() { + if(log_arr_idx == 0) return false; + if(!what_to_do) prepare_nrf24(); + last_packet_send_st = nrf24_txpacket( + nrf24_HANDLE, APP->log_arr + view_log_arr_idx * LOG_REC_SIZE + 1, 32, false); + last_packet_send = view_log_arr_idx; + notification_message( + APP->notification, + last_packet_send_st ? &sequence_blink_blue_100 : &sequence_blink_red_100); + if(what_to_do) start_scanning(); + return last_packet_send_st; } static void render_callback(Canvas* const canvas, void* ctx) { - const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); - if(plugin_state == NULL) return; - //canvas_draw_frame(canvas, 0, 0, 128, 64); // border around the edge of the screen - if(what_doing == 0) { - canvas_set_font(canvas, FontSecondary); // 8x10 font - snprintf(screen_buf, sizeof(screen_buf), "Settings: %s", addr_file_name); // menu_selected = 0 - canvas_draw_str(canvas, 10, 10, screen_buf); - snprintf(screen_buf, sizeof(screen_buf), "Ch: %d", NRF_channel); // menu_selected = 1 - canvas_draw_str(canvas, 10, 20, screen_buf); - if(NRF_ESB) { - strcpy(screen_buf, "ESB"); - if(NRF_DPL) strcat(screen_buf, " DPL"); - canvas_draw_str(canvas, 80, 20, screen_buf); - } - snprintf(screen_buf, sizeof(screen_buf), "Rate: %sbps", NRF_rate == 2 ? "2M" : NRF_rate == 1 ? "1M" : "250K"); // menu_selected = 2 - canvas_draw_str(canvas, 10, 30, screen_buf); - canvas_set_font(canvas, FontBatteryPercent); - snprintf(screen_buf, sizeof(screen_buf), "R:%d", NRF_Payload); - canvas_draw_str(canvas, 80, 30, screen_buf); - if(NRF_CRC == 1) canvas_draw_str(canvas, 105, 30, "CRC1"); - else if(NRF_CRC == 2) canvas_draw_str(canvas, 105, 30, "CRC2"); - canvas_set_font(canvas, FontSecondary); - strcpy(screen_buf, "Find channel period: "); // menu_selected = 3 - if(find_channel_period == 0) strcat(screen_buf, "off"); else snprintf(screen_buf + strlen(screen_buf), sizeof(screen_buf), "%d s", find_channel_period); - canvas_draw_str(canvas, 10, 40, screen_buf); - snprintf(screen_buf, sizeof(screen_buf), "Log: %s", log_to_file == 0 ? "No" : log_to_file == 1 ? "Yes" : log_to_file == 2 ? "Append" : "Clear");// menu_selected = 4 - canvas_draw_str(canvas, 10, 50, screen_buf); - snprintf(screen_buf, sizeof(screen_buf), "%s (pipes: %d)", what_to_do ? "Start scan" : "View log", addrs.addr_count); // menu_selected = 5 - canvas_draw_str(canvas, 10, 60, screen_buf); - canvas_draw_str(canvas, 0, menu_selected * 10 + 10, ">"); - } else { - canvas_set_font(canvas, FontBatteryPercent); // 5x7 font, 9 lines - bool ch2 = false; - screen_buf[0] = '\0'; - if(view_log_arr_x == 0) { strcat(screen_buf, " "); ch2 = true; - } else { - snprintf(screen_buf, sizeof(screen_buf), "<%d", view_log_arr_x); - if(view_log_arr_x < VIEW_LOG_MAX_X) ch2 = true; - } - snprintf(screen_buf + strlen(screen_buf), sizeof(screen_buf), " %s ch: %d - %d.", what_to_do ? "Read" : "View", NRF_channel, log_arr_idx); - canvas_draw_str(canvas, 0, 7, screen_buf); - if(ch2) canvas_draw_str(canvas, 121, 7, ">"); - if(log_arr_idx) { - if(view_log_arr_idx >= log_arr_idx) view_log_arr_idx = log_arr_idx - 1; - uint16_t page = view_log_arr_idx & ~7; - for(uint8_t i = 0; i < 8 && page + i < log_arr_idx; i++) { - snprintf(screen_buf, sizeof(screen_buf), "%d:%c", page + i + 1, (view_log_arr_idx & 7) != i ? ' ' : last_packet_send != view_log_arr_idx ? '>' : last_packet_send_st ? '+' : '!'); - char *ptr = (char*)APP->log_arr + (page + i) * LOG_REC_SIZE; - uint8_t dpl = *ptr++; - uint8_t pipe = dpl & 7; - dpl >>= 3; - if(dpl == 0) dpl = 32; - int count = dpl - view_log_arr_x; - if(count > 10) count = 10; - ptr += view_log_arr_x; - if(count > 0) { - if(view_log_arr_x == 0 && addrs.addr_count > 1) { - snprintf(screen_buf + strlen(screen_buf), sizeof(screen_buf), "%d-", pipe); - add_to_str_hex_bytes(screen_buf, ptr, count - 1); - } else { - add_to_str_hex_bytes(screen_buf, ptr, count); - } - } - canvas_draw_str(canvas, 0, 14 + i * 7, screen_buf); - } - } - } - release_mutex((ValueMutex*)ctx, plugin_state); + const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); + if(plugin_state == NULL) return; + //canvas_draw_frame(canvas, 0, 0, 128, 64); // border around the edge of the screen + if(what_doing == 0) { + canvas_set_font(canvas, FontSecondary); // 8x10 font + snprintf( + screen_buf, sizeof(screen_buf), "Settings: %s", addr_file_name); // menu_selected = 0 + canvas_draw_str(canvas, 10, 10, screen_buf); + snprintf(screen_buf, sizeof(screen_buf), "Ch: %d", NRF_channel); // menu_selected = 1 + canvas_draw_str(canvas, 10, 20, screen_buf); + if(NRF_ESB) { + strcpy(screen_buf, "ESB"); + if(NRF_DPL) strcat(screen_buf, " DPL"); + canvas_draw_str(canvas, 80, 20, screen_buf); + } + snprintf( + screen_buf, + sizeof(screen_buf), + "Rate: %sbps", + NRF_rate == 2 ? "2M" : + NRF_rate == 1 ? "1M" : + "250K"); // menu_selected = 2 + canvas_draw_str(canvas, 10, 30, screen_buf); + canvas_set_font(canvas, FontBatteryPercent); + snprintf(screen_buf, sizeof(screen_buf), "R:%d", NRF_Payload); + canvas_draw_str(canvas, 80, 30, screen_buf); + if(NRF_CRC == 1) + canvas_draw_str(canvas, 105, 30, "CRC1"); + else if(NRF_CRC == 2) + canvas_draw_str(canvas, 105, 30, "CRC2"); + canvas_set_font(canvas, FontSecondary); + strcpy(screen_buf, "Find channel period: "); // menu_selected = 3 + if(find_channel_period == 0) + strcat(screen_buf, "off"); + else + snprintf( + screen_buf + strlen(screen_buf), sizeof(screen_buf), "%d s", find_channel_period); + canvas_draw_str(canvas, 10, 40, screen_buf); + snprintf( + screen_buf, + sizeof(screen_buf), + "Log: %s", + log_to_file == 0 ? "No" : + log_to_file == 1 ? "Yes" : + log_to_file == 2 ? "Append" : + "Clear"); // menu_selected = 4 + canvas_draw_str(canvas, 10, 50, screen_buf); + snprintf( + screen_buf, + sizeof(screen_buf), + "%s (pipes: %d)", + what_to_do ? "Start scan" : "View log", + addrs.addr_count); // menu_selected = 5 + canvas_draw_str(canvas, 10, 60, screen_buf); + canvas_draw_str(canvas, 0, menu_selected * 10 + 10, ">"); + } else { + canvas_set_font(canvas, FontBatteryPercent); // 5x7 font, 9 lines + bool ch2 = false; + screen_buf[0] = '\0'; + if(view_log_arr_x == 0) { + strcat(screen_buf, " "); + ch2 = true; + } else { + snprintf(screen_buf, sizeof(screen_buf), "<%d", view_log_arr_x); + if(view_log_arr_x < VIEW_LOG_MAX_X) ch2 = true; + } + snprintf( + screen_buf + strlen(screen_buf), + sizeof(screen_buf), + " %s ch: %d - %d.", + what_to_do ? "Read" : "View", + NRF_channel, + log_arr_idx); + canvas_draw_str(canvas, 0, 7, screen_buf); + if(ch2) canvas_draw_str(canvas, 121, 7, ">"); + if(log_arr_idx) { + if(view_log_arr_idx >= log_arr_idx) view_log_arr_idx = log_arr_idx - 1; + uint16_t page = view_log_arr_idx & ~7; + for(uint8_t i = 0; i < 8 && page + i < log_arr_idx; i++) { + snprintf( + screen_buf, + sizeof(screen_buf), + "%d:%c", + page + i + 1, + (view_log_arr_idx & 7) != i ? ' ' : + last_packet_send != view_log_arr_idx ? '>' : + last_packet_send_st ? '+' : + '!'); + char* ptr = (char*)APP->log_arr + (page + i) * LOG_REC_SIZE; + uint8_t dpl = *ptr++; + uint8_t pipe = dpl & 7; + dpl >>= 3; + if(dpl == 0) dpl = 32; + int count = dpl - view_log_arr_x; + if(count > 10) count = 10; + ptr += view_log_arr_x; + if(count > 0) { + if(view_log_arr_x == 0 && addrs.addr_count > 1) { + snprintf(screen_buf + strlen(screen_buf), sizeof(screen_buf), "%d-", pipe); + add_to_str_hex_bytes(screen_buf, ptr, count - 1); + } else { + add_to_str_hex_bytes(screen_buf, ptr, count); + } + } + canvas_draw_str(canvas, 0, 14 + i * 7, screen_buf); + } + } + } + release_mutex((ValueMutex*)ctx, plugin_state); } int32_t nrf24scan_app(void* p) { - UNUSED(p); - APP = malloc(sizeof(Nrf24Scan)); - APP->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent)); - APP->plugin_state = malloc(sizeof(PluginState)); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, APP->plugin_state, sizeof(PluginState))) { - furi_message_queue_free(APP->event_queue); - FURI_LOG_E(TAG, "cannot create mutex"); - free(APP->plugin_state); - return 255; - } - memset((uint8_t*)&addrs, 0, sizeof(addrs)); - nrf24_init(); + UNUSED(p); + APP = malloc(sizeof(Nrf24Scan)); + APP->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent)); + APP->plugin_state = malloc(sizeof(PluginState)); + ValueMutex state_mutex; + if(!init_mutex(&state_mutex, APP->plugin_state, sizeof(PluginState))) { + furi_message_queue_free(APP->event_queue); + FURI_LOG_E(TAG, "cannot create mutex"); + free(APP->plugin_state); + return 255; + } + memset((uint8_t*)&addrs, 0, sizeof(addrs)); + nrf24_init(); - // Set system callbacks - APP->view_port = view_port_alloc(); - view_port_draw_callback_set(APP->view_port, render_callback, &state_mutex); - view_port_input_callback_set(APP->view_port, input_callback, APP->event_queue); + // Set system callbacks + APP->view_port = view_port_alloc(); + view_port_draw_callback_set(APP->view_port, render_callback, &state_mutex); + view_port_input_callback_set(APP->view_port, input_callback, APP->event_queue); - // Open GUI and register view_port - APP->gui = furi_record_open(RECORD_GUI); - gui_add_view_port(APP->gui, APP->view_port, GuiLayerFullscreen); - APP->notification = furi_record_open(RECORD_NOTIFICATION); - APP->storage = furi_record_open(RECORD_STORAGE); - storage_common_mkdir(APP->storage, SCAN_APP_PATH_FOLDER); - Stream* file_stream = file_stream_alloc(APP->storage); - FuriString* path = furi_string_alloc(); - furi_string_set(path, SCAN_APP_PATH_FOLDER); - furi_string_cat(path, "/"); - furi_string_cat(path, ADDR_FILENAME); - if(file_stream_open(file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) { - uint8_t err = load_settings_file(file_stream); - if(!err) strcpy(addr_file_name, ADDR_FILENAME); else snprintf(addr_file_name, sizeof(addr_file_name), "LOAD ERROR#%d", err); - } else { - strcpy(addr_file_name, "NONE"); - } - //file_stream_close(file_stream); - stream_free(file_stream); - furi_string_free(path); - allocate_log_array(); + // Open GUI and register view_port + APP->gui = furi_record_open(RECORD_GUI); + gui_add_view_port(APP->gui, APP->view_port, GuiLayerFullscreen); + APP->notification = furi_record_open(RECORD_NOTIFICATION); + APP->storage = furi_record_open(RECORD_STORAGE); + storage_common_mkdir(APP->storage, SCAN_APP_PATH_FOLDER); + Stream* file_stream = file_stream_alloc(APP->storage); + FuriString* path = furi_string_alloc(); + furi_string_set(path, SCAN_APP_PATH_FOLDER); + furi_string_cat(path, "/"); + furi_string_cat(path, ADDR_FILENAME); + if(file_stream_open(file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) { + uint8_t err = load_settings_file(file_stream); + if(!err) + strcpy(addr_file_name, ADDR_FILENAME); + else + snprintf(addr_file_name, sizeof(addr_file_name), "LOAD ERROR#%d", err); + } else { + strcpy(addr_file_name, "NONE"); + } + //file_stream_close(file_stream); + stream_free(file_stream); + furi_string_free(path); + allocate_log_array(); - PluginEvent event; - for(bool processing = true; processing;) { - FuriStatus event_status = furi_message_queue_get(APP->event_queue, &event, 100); - PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex); + PluginEvent event; + for(bool processing = true; processing;) { + FuriStatus event_status = furi_message_queue_get(APP->event_queue, &event, 100); + PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex); - if(event_status == FuriStatusOk) { - // press events - if(event.type == EventTypeKey) { - switch(event.input.key) { - case InputKeyUp: - if(event.input.type == InputTypePress || event.input.type == InputTypeRepeat) { - if(what_doing == 0) { - if(menu_selected > 0) menu_selected--; else menu_selected = menu_selected_max; - } else { - view_log_arr_idx -= event.input.type == InputTypeRepeat ? 10 : 1; - if(view_log_arr_idx >= log_arr_idx) view_log_arr_idx = 0; - } - } - break; - case InputKeyDown: - if(event.input.type == InputTypePress || event.input.type == InputTypeRepeat) { - if(what_doing == 0) { - if(menu_selected < menu_selected_max) menu_selected++; else menu_selected = 0; - } else { - view_log_arr_idx += event.input.type == InputTypeRepeat ? 10 : 1; - if(view_log_arr_idx >= log_arr_idx) view_log_arr_idx = log_arr_idx - 1; - } - } - break; - case InputKeyRight: - if(event.input.type == InputTypePress || event.input.type == InputTypeRepeat) { - if(what_doing == 0) { - switch(menu_selected) { - case Menu_enter_channel: - NRF_channel += event.input.type == InputTypeRepeat ? 10 : 1; - if(NRF_channel > MAX_CHANNEL) NRF_channel = 0; - break; - case Menu_enter_rate: - NRF_rate++; - if(NRF_rate > 2) NRF_rate = 0; - break; - case Menu_enter_scan_period: - find_channel_period += event.input.type == InputTypeRepeat ? 10 : 1; - break; - case Menu_log: - if(++log_to_file > 2) log_to_file = -1; - break; - case Menu_ok: - what_to_do = !what_to_do; - break; - } - } else { - if(view_log_arr_x < VIEW_LOG_MAX_X) view_log_arr_x++; - } - } - break; - case InputKeyLeft: - if(event.input.type == InputTypePress || event.input.type == InputTypeRepeat) { - if(what_doing == 0) { - switch(menu_selected) { - case Menu_enter_channel: - NRF_channel -= event.input.type == InputTypeRepeat ? 10 : 1; - if(NRF_channel > MAX_CHANNEL) NRF_channel = MAX_CHANNEL; - break; - case Menu_enter_rate: - NRF_Payload -= event.input.type == InputTypeRepeat ? 10 : 1; - if(NRF_Payload == 0 || NRF_Payload > 32) NRF_Payload = 32; - break; - case Menu_enter_scan_period: - find_channel_period -= event.input.type == InputTypeRepeat ? 10 : 1; - if(find_channel_period < 0) find_channel_period = 0; - break; - case Menu_log: - if(--log_to_file < -1) log_to_file = 2; - break; - case Menu_ok: - what_to_do = !what_to_do; - break; - } - } else { - if(view_log_arr_x > 0) view_log_arr_x--; - } - } - break; - case InputKeyOk: - if(event.input.type == InputTypePress) { - if(what_doing == 0) { - switch(menu_selected) { - case Menu_open_file: - file_stream = file_stream_alloc(APP->storage); - if(select_settings_file(file_stream)) { - uint8_t err = load_settings_file(file_stream); - if(!err) save_to_new_log = true; else snprintf(addr_file_name, sizeof(addr_file_name), "LOAD ERROR#%d", err); - file_stream_close(file_stream); - } - stream_free(file_stream); - break; - case Menu_enter_channel: - if(NRF_ESB) { - if(NRF_DPL) NRF_DPL = NRF_ESB = 0; else NRF_DPL = 1; - } else NRF_ESB = 1; - break; - case Menu_enter_rate: - if(++NRF_CRC > 2) NRF_CRC = 0; - break; - case Menu_ok: - what_doing = !what_doing; - if(what_doing) { - if(what_to_do) { - if(addrs.addr_count == 0) what_doing = 0; - else { - if(log_to_file == -1) { - clear_log(); - save_to_new_log = true; - } else if(log_to_file == 1) save_to_new_log = true; - start_scanning(); - } - } - } else nrf24_set_idle(nrf24_HANDLE); - break; - } - } else { // Send - nrf24_send_packet(); - } - } else if(event.input.type == InputTypeLong) { - if(what_doing == 0) { - if(menu_selected == Menu_log) { // Log - if(log_arr_idx && (log_to_file == 1 || log_to_file == 2)) { - write_to_log_file(APP->storage); - clear_log(); - } - } - nrf24_set_idle(nrf24_HANDLE); - } else if(what_doing == 1) { + if(event_status == FuriStatusOk) { + // press events + if(event.type == EventTypeKey) { + switch(event.input.key) { + case InputKeyUp: + if(event.input.type == InputTypePress || event.input.type == InputTypeRepeat) { + if(what_doing == 0) { + if(menu_selected > 0) + menu_selected--; + else + menu_selected = menu_selected_max; + } else { + view_log_arr_idx -= event.input.type == InputTypeRepeat ? 10 : 1; + if(view_log_arr_idx >= log_arr_idx) view_log_arr_idx = 0; + } + } + break; + case InputKeyDown: + if(event.input.type == InputTypePress || event.input.type == InputTypeRepeat) { + if(what_doing == 0) { + if(menu_selected < menu_selected_max) + menu_selected++; + else + menu_selected = 0; + } else { + view_log_arr_idx += event.input.type == InputTypeRepeat ? 10 : 1; + if(view_log_arr_idx >= log_arr_idx) view_log_arr_idx = log_arr_idx - 1; + } + } + break; + case InputKeyRight: + if(event.input.type == InputTypePress || event.input.type == InputTypeRepeat) { + if(what_doing == 0) { + switch(menu_selected) { + case Menu_enter_channel: + NRF_channel += event.input.type == InputTypeRepeat ? 10 : 1; + if(NRF_channel > MAX_CHANNEL) NRF_channel = 0; + break; + case Menu_enter_rate: + NRF_rate++; + if(NRF_rate > 2) NRF_rate = 0; + break; + case Menu_enter_scan_period: + find_channel_period += event.input.type == InputTypeRepeat ? 10 : + 1; + break; + case Menu_log: + if(++log_to_file > 2) log_to_file = -1; + break; + case Menu_ok: + what_to_do = !what_to_do; + break; + } + } else { + if(view_log_arr_x < VIEW_LOG_MAX_X) view_log_arr_x++; + } + } + break; + case InputKeyLeft: + if(event.input.type == InputTypePress || event.input.type == InputTypeRepeat) { + if(what_doing == 0) { + switch(menu_selected) { + case Menu_enter_channel: + NRF_channel -= event.input.type == InputTypeRepeat ? 10 : 1; + if(NRF_channel > MAX_CHANNEL) NRF_channel = MAX_CHANNEL; + break; + case Menu_enter_rate: + NRF_Payload -= event.input.type == InputTypeRepeat ? 10 : 1; + if(NRF_Payload == 0 || NRF_Payload > 32) NRF_Payload = 32; + break; + case Menu_enter_scan_period: + find_channel_period -= event.input.type == InputTypeRepeat ? 10 : + 1; + if(find_channel_period < 0) find_channel_period = 0; + break; + case Menu_log: + if(--log_to_file < -1) log_to_file = 2; + break; + case Menu_ok: + what_to_do = !what_to_do; + break; + } + } else { + if(view_log_arr_x > 0) view_log_arr_x--; + } + } + break; + case InputKeyOk: + if(event.input.type == InputTypePress) { + if(what_doing == 0) { + switch(menu_selected) { + case Menu_open_file: + file_stream = file_stream_alloc(APP->storage); + if(select_settings_file(file_stream)) { + uint8_t err = load_settings_file(file_stream); + if(!err) + save_to_new_log = true; + else + snprintf( + addr_file_name, + sizeof(addr_file_name), + "LOAD ERROR#%d", + err); + file_stream_close(file_stream); + } + stream_free(file_stream); + break; + case Menu_enter_channel: + if(NRF_ESB) { + if(NRF_DPL) + NRF_DPL = NRF_ESB = 0; + else + NRF_DPL = 1; + } else + NRF_ESB = 1; + break; + case Menu_enter_rate: + if(++NRF_CRC > 2) NRF_CRC = 0; + break; + case Menu_ok: + what_doing = !what_doing; + if(what_doing) { + if(what_to_do) { + if(addrs.addr_count == 0) + what_doing = 0; + else { + if(log_to_file == -1) { + clear_log(); + save_to_new_log = true; + } else if(log_to_file == 1) + save_to_new_log = true; + start_scanning(); + } + } + } else + nrf24_set_idle(nrf24_HANDLE); + break; + } + } else { // Send + nrf24_send_packet(); + } + } else if(event.input.type == InputTypeLong) { + if(what_doing == 0) { + if(menu_selected == Menu_log) { // Log + if(log_arr_idx && (log_to_file == 1 || log_to_file == 2)) { + write_to_log_file(APP->storage); + clear_log(); + } + } + nrf24_set_idle(nrf24_HANDLE); + } else if(what_doing == 1) { + } + } + break; + case InputKeyBack: + if(event.input.type == InputTypeLong) + processing = false; + else + what_doing = 0; + nrf24_set_idle(nrf24_HANDLE); + break; + default: + break; + } + } + } + if(what_to_do) { + nrf24_read_newpacket(); + if(find_channel_period && + furi_get_tick() - start_time >= (uint32_t)find_channel_period * 1000UL) { + if(++NRF_channel > MAX_CHANNEL) NRF_channel = 0; + start_scanning(); + } + } - } - } - break; - case InputKeyBack: - if(event.input.type == InputTypeLong) processing = false; else what_doing = 0; - nrf24_set_idle(nrf24_HANDLE); - break; - default: - break; - } - } - } - if(what_to_do) { - nrf24_read_newpacket(); - if(find_channel_period && furi_get_tick() - start_time >= (uint32_t)find_channel_period * 1000UL) { - if(++NRF_channel > MAX_CHANNEL) NRF_channel = 0; - start_scanning(); - } - } + view_port_update(APP->view_port); + release_mutex(&state_mutex, plugin_state); + } + if(log_arr_idx && (log_to_file == 1 || log_to_file == 2)) { + write_to_log_file(APP->storage); + } + nrf24_deinit(); - view_port_update(APP->view_port); - release_mutex(&state_mutex, plugin_state); - } - if(log_arr_idx && (log_to_file == 1 || log_to_file == 2)) { - write_to_log_file(APP->storage); - } - nrf24_deinit(); - - view_port_enabled_set(APP->view_port, false); - gui_remove_view_port(APP->gui, APP->view_port); - furi_record_close(RECORD_GUI); - furi_record_close(RECORD_NOTIFICATION); - furi_record_close(RECORD_STORAGE); - view_port_free(APP->view_port); - furi_message_queue_free(APP->event_queue); - free(APP->plugin_state); - if(APP->log_arr) free(APP->log_arr); - free(APP); - return 0; + view_port_enabled_set(APP->view_port, false); + gui_remove_view_port(APP->gui, APP->view_port); + furi_record_close(RECORD_GUI); + furi_record_close(RECORD_NOTIFICATION); + furi_record_close(RECORD_STORAGE); + view_port_free(APP->view_port); + furi_message_queue_free(APP->event_queue); + free(APP->plugin_state); + if(APP->log_arr) free(APP->log_arr); + free(APP); + return 0; } \ No newline at end of file diff --git a/lib/digital_signal/digital_signal.c b/lib/digital_signal/digital_signal.c index c7e8bbca5..faea80933 100644 --- a/lib/digital_signal/digital_signal.c +++ b/lib/digital_signal/digital_signal.c @@ -6,11 +6,9 @@ #define TAG "DigitalSignal" - -#define F_TIM (64000000.0) -#define T_TIM 1562 /* 15.625 ns *100 */ -#define T_TIM_DIV2 781 /* 15.625 ns / 2 *100 */ - +#define F_TIM (64000000.0) +#define T_TIM 1562 /* 15.625 ns *100 */ +#define T_TIM_DIV2 781 /* 15.625 ns / 2 *100 */ DigitalSignal* digital_signal_alloc(uint32_t max_edges_cnt) { DigitalSignal* signal = malloc(sizeof(DigitalSignal)); @@ -21,7 +19,7 @@ DigitalSignal* digital_signal_alloc(uint32_t max_edges_cnt) { signal->reload_reg_buff = malloc(signal->edges_max_cnt * sizeof(uint32_t)); signal->reload_reg_entries = 0; signal->reload_reg_remainder = 0; - + signal->dma_config_gpio.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH; signal->dma_config_gpio.Mode = LL_DMA_MODE_CIRCULAR; signal->dma_config_gpio.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT; @@ -32,7 +30,7 @@ DigitalSignal* digital_signal_alloc(uint32_t max_edges_cnt) { signal->dma_config_gpio.PeriphRequest = LL_DMAMUX_REQ_TIM2_UP; signal->dma_config_gpio.Priority = LL_DMA_PRIORITY_VERYHIGH; - signal->dma_config_timer.PeriphOrM2MSrcAddress = (uint32_t) &(TIM2->ARR); + signal->dma_config_timer.PeriphOrM2MSrcAddress = (uint32_t) & (TIM2->ARR); signal->dma_config_timer.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH; signal->dma_config_timer.Mode = LL_DMA_MODE_NORMAL; signal->dma_config_timer.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT; @@ -135,7 +133,7 @@ void digital_signal_prepare(DigitalSignal* signal) { furi_assert(signal); furi_assert(signal->gpio); furi_assert(signal->gpio->pin); - + /* set up signal polarities */ uint32_t bit_set = signal->gpio->pin; uint32_t bit_reset = signal->gpio->pin << 16; @@ -181,8 +179,8 @@ static bool digital_signal_setup_dma(DigitalSignal* signal) { return false; } - signal->dma_config_gpio.MemoryOrM2MDstAddress = (uint32_t) signal->gpio_buff; - signal->dma_config_gpio.PeriphOrM2MSrcAddress = (uint32_t) &(signal->gpio->port->BSRR); + signal->dma_config_gpio.MemoryOrM2MDstAddress = (uint32_t)signal->gpio_buff; + signal->dma_config_gpio.PeriphOrM2MSrcAddress = (uint32_t) & (signal->gpio->port->BSRR); signal->dma_config_timer.MemoryOrM2MDstAddress = (uint32_t)signal->reload_reg_buff; signal->dma_config_timer.NbData = signal->reload_reg_entries; @@ -198,7 +196,6 @@ static bool digital_signal_setup_dma(DigitalSignal* signal) { } static void digital_signal_setup_timer() { - digital_signal_stop_timer(); LL_TIM_SetCounterMode(TIM2, LL_TIM_COUNTERMODE_UP); @@ -256,7 +253,6 @@ void digital_sequence_alloc_sequence(DigitalSequence* sequence, uint32_t size) { } DigitalSequence* digital_sequence_alloc(uint32_t size, const GpioPin* gpio) { - DigitalSequence* sequence = malloc(sizeof(DigitalSequence)); sequence->gpio = gpio; @@ -276,7 +272,10 @@ void digital_sequence_free(DigitalSequence* sequence) { free(sequence); } -void digital_sequence_set_signal(DigitalSequence* sequence, uint8_t signal_index, DigitalSignal* signal) { +void digital_sequence_set_signal( + DigitalSequence* sequence, + uint8_t signal_index, + DigitalSignal* signal) { furi_assert(sequence); furi_assert(signal); furi_assert(signal_index < sequence->signals_size); @@ -301,52 +300,48 @@ void digital_sequence_add(DigitalSequence* sequence, uint8_t signal_index) { } void digital_signal_update_dma(DigitalSignal* signal) { - volatile uint32_t dma1_data[] = { - /* R6 */ (uint32_t)&(DMA1_Channel1->CCR), + /* R6 */ (uint32_t) & (DMA1_Channel1->CCR), /* R7 */ DMA1_Channel1->CCR & ~DMA_CCR_EN, /* R8 */ 2, - /* R9 */ (uint32_t)&(signal->gpio->port->BSRR), + /* R9 */ (uint32_t) & (signal->gpio->port->BSRR), /* R10 */ (uint32_t)signal->gpio_buff, - /* R11 */ DMA1_Channel1->CCR | DMA_CCR_EN }; + /* R11 */ DMA1_Channel1->CCR | DMA_CCR_EN}; volatile uint32_t dma2_data[] = { - /* R0 */ (uint32_t)&(DMA1_Channel2->CCR), + /* R0 */ (uint32_t) & (DMA1_Channel2->CCR), /* R1 */ DMA1_Channel2->CCR & ~DMA_CCR_EN, /* R2 */ (uint32_t)signal->reload_reg_entries, - /* R3 */ (uint32_t)&(TIM2->ARR), + /* R3 */ (uint32_t) & (TIM2->ARR), /* R4 */ (uint32_t)signal->reload_reg_buff, - /* R5 */ DMA1_Channel2->CCR | DMA_CCR_EN }; + /* R5 */ DMA1_Channel2->CCR | DMA_CCR_EN}; - /* hurry when setting up next transfer */ asm volatile("\t" - "MOV r6, %[data1]\n\t" - "MOV r7, %[data2]\n\t" + "MOV r6, %[data1]\n\t" + "MOV r7, %[data2]\n\t" - "PUSH {r0-r12}\n\t" + "PUSH {r0-r12}\n\t" - "LDM r7, {r0-r5}\n\t" - "LDM r6, {r6-r11}\n\t" + "LDM r7, {r0-r5}\n\t" + "LDM r6, {r6-r11}\n\t" - "loop:\n\t" - "LDR r12, [r0, #4]\n\t" - "CMP r12, #0\n\t" - "BNE loop\n\t" + "loop:\n\t" + "LDR r12, [r0, #4]\n\t" + "CMP r12, #0\n\t" + "BNE loop\n\t" - "STM r6, {r7-r10}\n\t" /* disable channel and set up new parameters */ - "STR r11, [r6, #0]\n\t" /* enable channel again */ - "STM r0, {r1-r4}\n\t" /* disable channel and set up new parameters */ - "STR r5, [r0, #0]\n\t" /* enable channel again */ + "STM r6, {r7-r10}\n\t" /* disable channel and set up new parameters */ + "STR r11, [r6, #0]\n\t" /* enable channel again */ + "STM r0, {r1-r4}\n\t" /* disable channel and set up new parameters */ + "STR r5, [r0, #0]\n\t" /* enable channel again */ - "POP {r0-r12}\n\t" + "POP {r0-r12}\n\t" - : /* no outputs*/ - : /* inputs */ - [data1] "r" (dma1_data), - [data2] "r" (dma2_data) - : "r6", "r7" ); - + : /* no outputs*/ + : /* inputs */ + [data1] "r"(dma1_data), [data2] "r"(dma2_data) + : "r6", "r7"); LL_DMA_ClearFlag_TC1(DMA1); LL_DMA_ClearFlag_TC2(DMA1); @@ -354,7 +349,7 @@ void digital_signal_update_dma(DigitalSignal* signal) { static bool digital_sequence_send_signal(DigitalSignal* signal) { furi_assert(signal); - + /* the first iteration has to set up the whole machinery */ if(!LL_DMA_IsEnabledChannel(DMA1, LL_DMA_CHANNEL_1)) { if(!digital_signal_setup_dma(signal)) { @@ -372,21 +367,20 @@ static bool digital_sequence_send_signal(DigitalSignal* signal) { } DigitalSignal* digital_sequence_bake(DigitalSequence* sequence) { - uint32_t edges = 0; for(uint32_t pos = 0; pos < sequence->sequence_used; pos++) { uint8_t signal_index = sequence->sequence[pos]; - DigitalSignal *sig = sequence->signals[signal_index]; + DigitalSignal* sig = sequence->signals[signal_index]; edges += sig->edge_cnt; } DigitalSignal* ret = digital_signal_alloc(edges); - + for(uint32_t pos = 0; pos < sequence->sequence_used; pos++) { uint8_t signal_index = sequence->sequence[pos]; - DigitalSignal *sig = sequence->signals[signal_index]; + DigitalSignal* sig = sequence->signals[signal_index]; digital_signal_append(ret, sig); } @@ -412,10 +406,14 @@ bool digital_sequence_send(DigitalSequence* sequence) { for(uint32_t pos = 0; pos < sequence->sequence_used; pos++) { uint8_t signal_index = sequence->sequence[pos]; - DigitalSignal *sig = sequence->signals[signal_index]; + DigitalSignal* sig = sequence->signals[signal_index]; if(!sig) { - FURI_LOG_D(TAG, "digital_sequence_send: Signal at index %u, used at pos %lu is NULL, aborting", signal_index, pos); + FURI_LOG_D( + TAG, + "digital_sequence_send: Signal at index %u, used at pos %lu is NULL, aborting", + signal_index, + pos); break; } @@ -453,10 +451,10 @@ bool digital_sequence_send(DigitalSequence* sequence) { digital_signal_stop_timer(); digital_signal_stop_dma(); - + /* undo previously prolonged edges */ for(uint32_t pos = 0; pos < sequence->signals_size; pos++) { - DigitalSignal *sig = sequence->signals[pos]; + DigitalSignal* sig = sequence->signals[pos]; if(sig && sequence->signals_prolonged[pos]) { sig->edge_timings[0]--; diff --git a/lib/digital_signal/digital_signal.h b/lib/digital_signal/digital_signal.h index 8f6142258..3b250074b 100644 --- a/lib/digital_signal/digital_signal.h +++ b/lib/digital_signal/digital_signal.h @@ -13,11 +13,10 @@ extern "C" { #endif /* helper for easier signal generation */ -#define DIGITAL_SIGNAL_MS(x) (x*100000000UL) -#define DIGITAL_SIGNAL_US(x) (x*100000UL) -#define DIGITAL_SIGNAL_NS(x) (x*100UL) -#define DIGITAL_SIGNAL_PS(x) (x/10UL) - +#define DIGITAL_SIGNAL_MS(x) (x * 100000000UL) +#define DIGITAL_SIGNAL_US(x) (x * 100000UL) +#define DIGITAL_SIGNAL_NS(x) (x * 100UL) +#define DIGITAL_SIGNAL_PS(x) (x / 10UL) typedef struct { bool start_level; @@ -44,7 +43,6 @@ typedef struct { const GpioPin* gpio; } DigitalSequence; - DigitalSignal* digital_signal_alloc(uint32_t max_edges_cnt); void digital_signal_free(DigitalSignal* signal); @@ -65,16 +63,16 @@ uint32_t digital_signal_get_edge(DigitalSignal* signal, uint32_t edge_num); void digital_signal_send(DigitalSignal* signal, const GpioPin* gpio); - DigitalSequence* digital_sequence_alloc(uint32_t size, const GpioPin* gpio); void digital_sequence_free(DigitalSequence* sequence); -void digital_sequence_set_signal(DigitalSequence* sequence, uint8_t signal_index, DigitalSignal* signal); +void digital_sequence_set_signal( + DigitalSequence* sequence, + uint8_t signal_index, + DigitalSignal* signal); void digital_sequence_add(DigitalSequence* sequence, uint8_t signal_index); bool digital_sequence_send(DigitalSequence* sequence); void digital_sequence_clear(DigitalSequence* sequence); - - #ifdef __cplusplus } #endif diff --git a/lib/nfc/nfc_device.c b/lib/nfc/nfc_device.c index 67c0bc072..6be9866f1 100644 --- a/lib/nfc/nfc_device.c +++ b/lib/nfc/nfc_device.c @@ -656,7 +656,8 @@ static bool nfc_device_save_slix_data(FlipperFormat* file, NfcDevice* dev) { do { if(!flipper_format_write_comment_cstr(file, "SLIX specific data")) break; - if(!flipper_format_write_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) break; + if(!flipper_format_write_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) + break; saved = true; } while(false); @@ -667,10 +668,9 @@ bool nfc_device_load_slix_data(FlipperFormat* file, NfcDevice* dev) { bool parsed = false; NfcVSlixData* data = &dev->dev_data.nfcv_data.sub_data.slix; memset(data, 0, sizeof(NfcVData)); - + do { - if(!flipper_format_read_hex( - file, "Password EAS", data->key_eas, sizeof(data->key_eas))) + if(!flipper_format_read_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) break; parsed = true; @@ -685,11 +685,19 @@ static bool nfc_device_save_slix_s_data(FlipperFormat* file, NfcDevice* dev) { do { if(!flipper_format_write_comment_cstr(file, "SLIX-S specific data")) break; - if(!flipper_format_write_hex(file, "Password Read", data->key_read, sizeof(data->key_read))) break; - if(!flipper_format_write_hex(file, "Password Write", data->key_write, sizeof(data->key_write))) break; - if(!flipper_format_write_hex(file, "Password Privacy", data->key_privacy, sizeof(data->key_privacy))) break; - if(!flipper_format_write_hex(file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy))) break; - if(!flipper_format_write_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) break; + if(!flipper_format_write_hex(file, "Password Read", data->key_read, sizeof(data->key_read))) + break; + if(!flipper_format_write_hex( + file, "Password Write", data->key_write, sizeof(data->key_write))) + break; + if(!flipper_format_write_hex( + file, "Password Privacy", data->key_privacy, sizeof(data->key_privacy))) + break; + if(!flipper_format_write_hex( + file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy))) + break; + if(!flipper_format_write_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) + break; if(!flipper_format_write_bool(file, "Privacy Mode", &data->privacy, 1)) break; saved = true; } while(false); @@ -701,10 +709,9 @@ bool nfc_device_load_slix_s_data(FlipperFormat* file, NfcDevice* dev) { bool parsed = false; NfcVSlixSData* data = &dev->dev_data.nfcv_data.sub_data.slix_s; memset(data, 0, sizeof(NfcVData)); - + do { - if(!flipper_format_read_hex( - file, "Password Read", data->key_read, sizeof(data->key_read))) + if(!flipper_format_read_hex(file, "Password Read", data->key_read, sizeof(data->key_read))) break; if(!flipper_format_read_hex( file, "Password Write", data->key_write, sizeof(data->key_write))) @@ -715,8 +722,7 @@ bool nfc_device_load_slix_s_data(FlipperFormat* file, NfcDevice* dev) { if(!flipper_format_read_hex( file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy))) break; - if(!flipper_format_read_hex( - file, "Password EAS", data->key_eas, sizeof(data->key_eas))) + if(!flipper_format_read_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) break; if(!flipper_format_read_bool(file, "Privacy Mode", &data->privacy, 1)) break; @@ -732,9 +738,14 @@ static bool nfc_device_save_slix_l_data(FlipperFormat* file, NfcDevice* dev) { do { if(!flipper_format_write_comment_cstr(file, "SLIX-L specific data")) break; - if(!flipper_format_write_hex(file, "Password Privacy", data->key_privacy, sizeof(data->key_privacy))) break; - if(!flipper_format_write_hex(file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy))) break; - if(!flipper_format_write_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) break; + if(!flipper_format_write_hex( + file, "Password Privacy", data->key_privacy, sizeof(data->key_privacy))) + break; + if(!flipper_format_write_hex( + file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy))) + break; + if(!flipper_format_write_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) + break; if(!flipper_format_write_bool(file, "Privacy Mode", &data->privacy, 1)) break; saved = true; } while(false); @@ -746,7 +757,7 @@ bool nfc_device_load_slix_l_data(FlipperFormat* file, NfcDevice* dev) { bool parsed = false; NfcVSlixLData* data = &dev->dev_data.nfcv_data.sub_data.slix_l; memset(data, 0, sizeof(NfcVData)); - + do { if(!flipper_format_read_hex( file, "Password Privacy", data->key_privacy, sizeof(data->key_privacy))) @@ -754,8 +765,7 @@ bool nfc_device_load_slix_l_data(FlipperFormat* file, NfcDevice* dev) { if(!flipper_format_read_hex( file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy))) break; - if(!flipper_format_read_hex( - file, "Password EAS", data->key_eas, sizeof(data->key_eas))) + if(!flipper_format_read_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) break; if(!flipper_format_read_bool(file, "Privacy Mode", &data->privacy, 1)) break; @@ -771,11 +781,19 @@ static bool nfc_device_save_slix2_data(FlipperFormat* file, NfcDevice* dev) { do { if(!flipper_format_write_comment_cstr(file, "SLIX2 specific data")) break; - if(!flipper_format_write_hex(file, "Password Read", data->key_read, sizeof(data->key_read))) break; - if(!flipper_format_write_hex(file, "Password Write", data->key_write, sizeof(data->key_write))) break; - if(!flipper_format_write_hex(file, "Password Privacy", data->key_privacy, sizeof(data->key_privacy))) break; - if(!flipper_format_write_hex(file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy))) break; - if(!flipper_format_write_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) break; + if(!flipper_format_write_hex(file, "Password Read", data->key_read, sizeof(data->key_read))) + break; + if(!flipper_format_write_hex( + file, "Password Write", data->key_write, sizeof(data->key_write))) + break; + if(!flipper_format_write_hex( + file, "Password Privacy", data->key_privacy, sizeof(data->key_privacy))) + break; + if(!flipper_format_write_hex( + file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy))) + break; + if(!flipper_format_write_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) + break; if(!flipper_format_write_bool(file, "Privacy Mode", &data->privacy, 1)) break; saved = true; } while(false); @@ -787,10 +805,9 @@ bool nfc_device_load_slix2_data(FlipperFormat* file, NfcDevice* dev) { bool parsed = false; NfcVSlix2Data* data = &dev->dev_data.nfcv_data.sub_data.slix2; memset(data, 0, sizeof(NfcVData)); - + do { - if(!flipper_format_read_hex( - file, "Password Read", data->key_read, sizeof(data->key_read))) + if(!flipper_format_read_hex(file, "Password Read", data->key_read, sizeof(data->key_read))) break; if(!flipper_format_read_hex( file, "Password Write", data->key_write, sizeof(data->key_write))) @@ -801,8 +818,7 @@ bool nfc_device_load_slix2_data(FlipperFormat* file, NfcDevice* dev) { if(!flipper_format_read_hex( file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy))) break; - if(!flipper_format_read_hex( - file, "Password EAS", data->key_eas, sizeof(data->key_eas))) + if(!flipper_format_read_hex(file, "Password EAS", data->key_eas, sizeof(data->key_eas))) break; if(!flipper_format_read_bool(file, "Privacy Mode", &data->privacy, 1)) break; @@ -826,32 +842,39 @@ static bool nfc_device_save_nfcv_data(FlipperFormat* file, NfcDevice* dev) { if(!flipper_format_write_hex(file, "AFI", &(data->afi), 1)) break; if(!flipper_format_write_hex(file, "IC Reference", &(data->ic_ref), 1)) break; temp_uint32 = data->block_num; - if(!flipper_format_write_comment_cstr(file, "Number of memory blocks, usually 0 to 256")) break; + if(!flipper_format_write_comment_cstr(file, "Number of memory blocks, usually 0 to 256")) + break; if(!flipper_format_write_uint32(file, "Block Count", &temp_uint32, 1)) break; - if(!flipper_format_write_comment_cstr(file, "Size of a single memory block, usually 4")) break; + if(!flipper_format_write_comment_cstr(file, "Size of a single memory block, usually 4")) + break; if(!flipper_format_write_hex(file, "Block Size", &(data->block_size), 1)) break; - if(!flipper_format_write_hex(file, "Data Content", data->data, data->block_num * data->block_size)) break; - if(!flipper_format_write_comment_cstr(file, "Subtype of this card (0 = ISO15693, 1 = SLIX, 2 = SLIX-S, 3 = SLIX-L, 4 = SLIX2)")) break; + if(!flipper_format_write_hex( + file, "Data Content", data->data, data->block_num * data->block_size)) + break; + if(!flipper_format_write_comment_cstr( + file, + "Subtype of this card (0 = ISO15693, 1 = SLIX, 2 = SLIX-S, 3 = SLIX-L, 4 = SLIX2)")) + break; temp_uint8 = (uint8_t)data->type; if(!flipper_format_write_hex(file, "Subtype", &temp_uint8, 1)) break; switch(data->type) { - case NfcVTypePlain: - if(!flipper_format_write_comment_cstr(file, "End of ISO15693 parameters")) break; - saved = true; - break; - case NfcVTypeSlix: - saved = nfc_device_save_slix_data(file, dev); - break; - case NfcVTypeSlixS: - saved = nfc_device_save_slix_s_data(file, dev); - break; - case NfcVTypeSlixL: - saved = nfc_device_save_slix_l_data(file, dev); - break; - case NfcVTypeSlix2: - saved = nfc_device_save_slix2_data(file, dev); - break; + case NfcVTypePlain: + if(!flipper_format_write_comment_cstr(file, "End of ISO15693 parameters")) break; + saved = true; + break; + case NfcVTypeSlix: + saved = nfc_device_save_slix_data(file, dev); + break; + case NfcVTypeSlixS: + saved = nfc_device_save_slix_s_data(file, dev); + break; + case NfcVTypeSlixL: + saved = nfc_device_save_slix_l_data(file, dev); + break; + case NfcVTypeSlix2: + saved = nfc_device_save_slix2_data(file, dev); + break; } } while(false); @@ -863,7 +886,7 @@ bool nfc_device_load_nfcv_data(FlipperFormat* file, NfcDevice* dev) { NfcVData* data = &dev->dev_data.nfcv_data; memset(data, 0, sizeof(NfcVData)); - + do { uint32_t temp_uint32 = 0; uint8_t temp_value = 0; @@ -881,21 +904,21 @@ bool nfc_device_load_nfcv_data(FlipperFormat* file, NfcDevice* dev) { data->type = temp_value; switch(data->type) { - case NfcVTypePlain: - parsed = true; - break; - case NfcVTypeSlix: - parsed = nfc_device_load_slix_data(file, dev); - break; - case NfcVTypeSlixS: - parsed = nfc_device_load_slix_s_data(file, dev); - break; - case NfcVTypeSlixL: - parsed = nfc_device_load_slix_l_data(file, dev); - break; - case NfcVTypeSlix2: - parsed = nfc_device_load_slix2_data(file, dev); - break; + case NfcVTypePlain: + parsed = true; + break; + case NfcVTypeSlix: + parsed = nfc_device_load_slix_data(file, dev); + break; + case NfcVTypeSlixS: + parsed = nfc_device_load_slix_s_data(file, dev); + break; + case NfcVTypeSlixL: + parsed = nfc_device_load_slix_l_data(file, dev); + break; + case NfcVTypeSlix2: + parsed = nfc_device_load_slix2_data(file, dev); + break; } } while(false); @@ -1325,14 +1348,12 @@ bool nfc_device_save(NfcDevice* dev, const char* dev_name) { nfc_device_prepare_format_string(dev, temp_str); if(!flipper_format_write_string(file, "Device type", temp_str)) break; // Write UID - if(!flipper_format_write_comment_cstr(file, "UID is common for all formats")) - break; + if(!flipper_format_write_comment_cstr(file, "UID is common for all formats")) break; if(!flipper_format_write_hex(file, "UID", data->uid, data->uid_len)) break; if(dev->format != NfcDeviceSaveFormatNfcV) { // Write ATQA, SAK - if(!flipper_format_write_comment_cstr(file, "ISO14443 specific fields")) - break; + if(!flipper_format_write_comment_cstr(file, "ISO14443 specific fields")) break; if(!flipper_format_write_hex(file, "ATQA", data->atqa, 2)) break; if(!flipper_format_write_hex(file, "SAK", &data->sak, 1)) break; } diff --git a/lib/nfc/nfc_worker.c b/lib/nfc/nfc_worker.c index cbd65411f..ebc16bdb9 100644 --- a/lib/nfc/nfc_worker.c +++ b/lib/nfc/nfc_worker.c @@ -159,7 +159,7 @@ void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker) { NfcVData* nfcv_data = &nfc_worker->dev_data->nfcv_data; FuriHalNfcTxRxContext tx_rx = {}; - uint8_t *key_data = nfcv_data->sub_data.slix_l.key_privacy; + uint8_t* key_data = nfcv_data->sub_data.slix_l.key_privacy; uint32_t key = 0; if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) { @@ -169,14 +169,14 @@ void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker) { furi_hal_nfc_sleep(); - - while((nfc_worker->state == NfcWorkerStateNfcVUnlock) || - (nfc_worker->state == NfcWorkerStateNfcVUnlockAndSave)) { - + while((nfc_worker->state == NfcWorkerStateNfcVUnlock) || + (nfc_worker->state == NfcWorkerStateNfcVUnlockAndSave)) { furi_hal_nfc_exit_sleep(); furi_hal_nfc_ll_txrx_on(); furi_hal_nfc_ll_poll(); - if(furi_hal_nfc_ll_set_mode(FuriHalNfcModePollNfcv, FuriHalNfcBitrate26p48, FuriHalNfcBitrate26p48) != FuriHalNfcReturnOk) { + if(furi_hal_nfc_ll_set_mode( + FuriHalNfcModePollNfcv, FuriHalNfcBitrate26p48, FuriHalNfcBitrate26p48) != + FuriHalNfcReturnOk) { break; } @@ -184,7 +184,7 @@ void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker) { furi_hal_nfc_ll_set_fdt_poll(FURI_HAL_NFC_LL_FDT_POLL_NFCV_POLLER); furi_hal_nfc_ll_set_error_handling(FuriHalNfcErrorHandlingNfc); furi_hal_nfc_ll_set_guard_time(FURI_HAL_NFC_LL_GT_NFCV); - + furi_hal_console_printf("Detect presence\r\n"); ReturnCode ret = slix_l_get_random(nfcv_data); @@ -198,25 +198,26 @@ void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker) { /* chip is also visible, so no action required, just save */ if(nfc_worker->state == NfcWorkerStateNfcVUnlockAndSave) { NfcVReader reader = {}; - + if(!nfcv_read_card(&reader, &nfc_worker->dev_data->nfc_data, nfcv_data)) { furi_hal_console_printf(" => failed, wait for chip to disappear.\r\n"); snprintf(nfcv_data->error, sizeof(nfcv_data->error), "Read card\nfailed"); nfc_worker->callback(NfcWorkerEventWrongCardDetected, nfc_worker->context); } else { furi_hal_console_printf(" => success, wait for chip to disappear.\r\n"); - nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context); + nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context); } } else { furi_hal_console_printf(" => success, wait for chip to disappear.\r\n"); - nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context); + nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context); } while(slix_l_get_random(NULL) == ERR_NONE) { furi_delay_ms(100); } - furi_hal_console_printf(" => chip is already visible, wait for chip to disappear.\r\n"); + furi_hal_console_printf( + " => chip is already visible, wait for chip to disappear.\r\n"); nfc_worker->callback(NfcWorkerEventAborted, nfc_worker->context); while(slix_l_get_random(NULL) == ERR_NONE) { furi_delay_ms(100); @@ -232,7 +233,6 @@ void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker) { furi_hal_console_printf(" chip is invisible, unlocking\r\n"); if(nfcv_data->auth_method == NfcVAuthMethodManual) { - key |= key_data[0] << 24; key |= key_data[1] << 16; key |= key_data[2] << 8; @@ -271,7 +271,8 @@ void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker) { if(ret != ERR_NONE) { /* unlock failed */ furi_hal_console_printf(" => failed, wait for chip to disappear.\r\n"); - snprintf(nfcv_data->error, sizeof(nfcv_data->error), "Passwords not\naccepted"); + snprintf( + nfcv_data->error, sizeof(nfcv_data->error), "Passwords not\naccepted"); nfc_worker->callback(NfcWorkerEventWrongCardDetected, nfc_worker->context); /* reset chip */ @@ -299,8 +300,6 @@ void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker) { } } - - static bool nfc_worker_read_mf_ultralight(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) { bool read_success = false; MfUltralightReader reader = {}; @@ -600,7 +599,7 @@ static bool nfc_worker_read_nfcb(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* t static bool nfc_worker_read_nfcv(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) { furi_assert(nfc_worker); furi_assert(tx_rx); - + bool card_read = false; furi_hal_nfc_sleep(); diff --git a/lib/nfc/nfc_worker.h b/lib/nfc/nfc_worker.h index b261c26ba..60f993dd5 100644 --- a/lib/nfc/nfc_worker.h +++ b/lib/nfc/nfc_worker.h @@ -96,4 +96,3 @@ void nfc_worker_start( void nfc_worker_stop(NfcWorker* nfc_worker); void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker); void nfc_worker_emulate_nfcv(NfcWorker* nfc_worker); - diff --git a/lib/nfc/protocols/nfca_trans_rx.c b/lib/nfc/protocols/nfca_trans_rx.c index 3f8047272..28737fa2d 100644 --- a/lib/nfc/protocols/nfca_trans_rx.c +++ b/lib/nfc/protocols/nfca_trans_rx.c @@ -14,8 +14,7 @@ #define TAG "NfcA-trans-rx" - -void nfca_trans_rx_init(NfcaTransRxState *state) { +void nfca_trans_rx_init(NfcaTransRxState* state) { FURI_LOG_D(TAG, "Starting NfcA transparent rx"); st25r3916ExecuteCommand(ST25R3916_CMD_STOP); @@ -29,7 +28,7 @@ void nfca_trans_rx_init(NfcaTransRxState *state) { state->reader_signal = pulse_reader_alloc(&gpio_spi_r_miso, 512); /* timebase shall be 1 ns */ pulse_reader_set_timebase(state->reader_signal, PulseReaderUnitNanosecond); - + pulse_reader_start(state->reader_signal); /* set start values */ @@ -38,21 +37,20 @@ void nfca_trans_rx_init(NfcaTransRxState *state) { state->valid_frame = false; } -void nfca_trans_rx_deinit(NfcaTransRxState *state) { +void nfca_trans_rx_deinit(NfcaTransRxState* state) { furi_hal_spi_bus_handle_init(&furi_hal_spi_bus_handle_nfc); pulse_reader_free(state->reader_signal); } -void nfca_trans_rx_pause(NfcaTransRxState *state) { +void nfca_trans_rx_pause(NfcaTransRxState* state) { pulse_reader_stop(state->reader_signal); } -void nfca_trans_rx_continue(NfcaTransRxState *state) { +void nfca_trans_rx_continue(NfcaTransRxState* state) { pulse_reader_start(state->reader_signal); } -static void nfca_bit_received(NfcaTransRxState *state, uint8_t bit) { - +static void nfca_bit_received(NfcaTransRxState* state, uint8_t bit) { /* According to ISO14443-3 short frames have 7 bits and standard 9 bits per byte, where the 9th bit is odd parity. Data is transmitted LSB first. */ uint32_t byte_num = (state->bits_received / 9); @@ -75,10 +73,9 @@ static void nfca_bit_received(NfcaTransRxState *state, uint8_t bit) { state->bits_received++; } - -bool nfca_trans_rx_loop(NfcaTransRxState *state, uint32_t timeout_ms) { +bool nfca_trans_rx_loop(NfcaTransRxState* state, uint32_t timeout_ms) { furi_assert(state); - + state->valid_frame = false; state->have_sof = false; state->bits_received = 0; diff --git a/lib/nfc/protocols/nfca_trans_rx.h b/lib/nfc/protocols/nfca_trans_rx.h index d9c5f9513..cf6d428ec 100644 --- a/lib/nfc/protocols/nfca_trans_rx.h +++ b/lib/nfc/protocols/nfca_trans_rx.h @@ -10,30 +10,29 @@ #include "nfc_util.h" /* assume fc/128 */ -#define NFCA_FC (13560000.0f) /* MHz */ -#define NFCA_FC_K ((uint32_t)(NFCA_FC/1000)) /* kHz */ -#define NFCA_T1 (28.0f / NFCA_FC * 1000000000) -#define NFCA_T1_MIN (24.0f / NFCA_FC * 1000000000) -#define NFCA_T1_MAX (41.0f / NFCA_FC * 1000000000) -#define NFCA_TX (64.0f / NFCA_FC * 1000000000) /* 4.7198 µs */ -#define NFCA_TX_MIN (0.90f * NFCA_TX) -#define NFCA_TX_MAX (1.10f * NFCA_TX) -#define NFCA_TB (128.0f / NFCA_FC * 1000000000) /* 9.4395 µs */ -#define NFCA_TB_MIN (0.80f * NFCA_TB) -#define NFCA_TB_MAX (1.20f * NFCA_TB) +#define NFCA_FC (13560000.0f) /* MHz */ +#define NFCA_FC_K ((uint32_t)(NFCA_FC / 1000)) /* kHz */ +#define NFCA_T1 (28.0f / NFCA_FC * 1000000000) +#define NFCA_T1_MIN (24.0f / NFCA_FC * 1000000000) +#define NFCA_T1_MAX (41.0f / NFCA_FC * 1000000000) +#define NFCA_TX (64.0f / NFCA_FC * 1000000000) /* 4.7198 µs */ +#define NFCA_TX_MIN (0.90f * NFCA_TX) +#define NFCA_TX_MAX (1.10f * NFCA_TX) +#define NFCA_TB (128.0f / NFCA_FC * 1000000000) /* 9.4395 µs */ +#define NFCA_TB_MIN (0.80f * NFCA_TB) +#define NFCA_TB_MAX (1.20f * NFCA_TB) -#define IS_T1(x) ((x)>=NFCA_T1_MIN && (x)<=NFCA_T1_MAX) -#define IS_TX(x) ((x)>=NFCA_TX_MIN && (x)<=NFCA_TX_MAX) -#define IS_TB(x) ((x)>=NFCA_TB_MIN && (x)<=NFCA_TB_MAX) -#define IS_ZERO(x) ((x)>=-NFCA_T1_MIN/2 && (x)<=NFCA_T1_MIN/2) +#define IS_T1(x) ((x) >= NFCA_T1_MIN && (x) <= NFCA_T1_MAX) +#define IS_TX(x) ((x) >= NFCA_TX_MIN && (x) <= NFCA_TX_MAX) +#define IS_TB(x) ((x) >= NFCA_TB_MIN && (x) <= NFCA_TB_MAX) +#define IS_ZERO(x) ((x) >= -NFCA_T1_MIN / 2 && (x) <= NFCA_T1_MIN / 2) -#define DIGITAL_SIGNAL_UNIT_S (100000000000.0f) -#define DIGITAL_SIGNAL_UNIT_US (100000.0f) +#define DIGITAL_SIGNAL_UNIT_S (100000000000.0f) +#define DIGITAL_SIGNAL_UNIT_US (100000.0f) #define NFCA_FRAME_LENGTH 32 #define NFCA_DEBUG_LENGTH 128 - typedef struct { bool have_sof; bool valid_frame; @@ -43,12 +42,12 @@ typedef struct { uint32_t debug_buffer[NFCA_DEBUG_LENGTH]; size_t debug_pos; uint32_t parity_bits; - PulseReader *reader_signal; + PulseReader* reader_signal; } NfcaTransRxState; -bool nfca_trans_rx_loop(NfcaTransRxState *state, uint32_t timeout_ms); -void nfca_trans_rx_deinit(NfcaTransRxState *state); -void nfca_trans_rx_init(NfcaTransRxState *state); +bool nfca_trans_rx_loop(NfcaTransRxState* state, uint32_t timeout_ms); +void nfca_trans_rx_deinit(NfcaTransRxState* state); +void nfca_trans_rx_init(NfcaTransRxState* state); -void nfca_trans_rx_pause(NfcaTransRxState *state); -void nfca_trans_rx_continue(NfcaTransRxState *state); \ No newline at end of file +void nfca_trans_rx_pause(NfcaTransRxState* state); +void nfca_trans_rx_continue(NfcaTransRxState* state); \ No newline at end of file diff --git a/lib/nfc/protocols/nfcv.c b/lib/nfc/protocols/nfcv.c index 9557c28fa..91fe92003 100644 --- a/lib/nfc/protocols/nfcv.c +++ b/lib/nfc/protocols/nfcv.c @@ -22,8 +22,7 @@ ReturnCode nfcv_inventory(uint8_t* uid) { for(int tries = 0; tries < 5; tries++) { /* TODO: needs proper abstraction via fury_hal(_ll)_* */ - ret = rfalNfcvPollerInventory( - RFAL_NFCV_NUM_SLOTS_1, 0, NULL, &res, &received); + ret = rfalNfcvPollerInventory(RFAL_NFCV_NUM_SLOTS_1, 0, NULL, &res, &received); if(ret == ERR_NONE) { break; @@ -39,10 +38,7 @@ ReturnCode nfcv_inventory(uint8_t* uid) { return ret; } -ReturnCode nfcv_read_blocks( - NfcVReader* reader, - NfcVData* data) { - +ReturnCode nfcv_read_blocks(NfcVReader* reader, NfcVData* data) { UNUSED(reader); uint16_t received = 0; @@ -53,8 +49,7 @@ ReturnCode nfcv_read_blocks( ReturnCode ret = ERR_NONE; for(int tries = 0; tries < 5; tries++) { ret = rfalNfcvPollerReadSingleBlock( - RFAL_NFCV_REQ_FLAG_DEFAULT, NULL, block, - rxBuf, sizeof(rxBuf), &received); + RFAL_NFCV_REQ_FLAG_DEFAULT, NULL, block, rxBuf, sizeof(rxBuf), &received); if(ret == ERR_NONE) { break; @@ -65,9 +60,13 @@ ReturnCode nfcv_read_blocks( return ret; } memcpy(&(data->data[block * data->block_size]), &rxBuf[1], data->block_size); - FURI_LOG_D(TAG, " %02X %02X %02X %02X", - data->data[block * data->block_size + 0], data->data[block * data->block_size + 1], - data->data[block * data->block_size + 2], data->data[block * data->block_size + 3]); + FURI_LOG_D( + TAG, + " %02X %02X %02X %02X", + data->data[block * data->block_size + 0], + data->data[block * data->block_size + 1], + data->data[block * data->block_size + 2], + data->data[block * data->block_size + 3]); } return ERR_NONE; @@ -102,10 +101,25 @@ ReturnCode nfcv_read_sysinfo(FuriHalNfcDevData* nfc_data, NfcVData* data) { data->block_num = rxBuf[12] + 1; data->block_size = rxBuf[13] + 1; data->ic_ref = rxBuf[14]; - FURI_LOG_D(TAG, " UID: %02X %02X %02X %02X %02X %02X %02X %02X", - nfc_data->uid[0], nfc_data->uid[1], nfc_data->uid[2], nfc_data->uid[3], - nfc_data->uid[4], nfc_data->uid[5], nfc_data->uid[6], nfc_data->uid[7]); - FURI_LOG_D(TAG, " DSFID %d, AFI %d, Blocks %d, Size %d, IC Ref %d", data->dsfid, data->afi, data->block_num, data->block_size, data->ic_ref); + FURI_LOG_D( + TAG, + " UID: %02X %02X %02X %02X %02X %02X %02X %02X", + nfc_data->uid[0], + nfc_data->uid[1], + nfc_data->uid[2], + nfc_data->uid[3], + nfc_data->uid[4], + nfc_data->uid[5], + nfc_data->uid[6], + nfc_data->uid[7]); + FURI_LOG_D( + TAG, + " DSFID %d, AFI %d, Blocks %d, Size %d, IC Ref %d", + data->dsfid, + data->afi, + data->block_num, + data->block_size, + data->ic_ref); return ret; } FURI_LOG_D(TAG, "Failed: %d", ret); @@ -113,10 +127,7 @@ ReturnCode nfcv_read_sysinfo(FuriHalNfcDevData* nfc_data, NfcVData* data) { return ret; } -bool nfcv_read_card( - NfcVReader* reader, - FuriHalNfcDevData* nfc_data, - NfcVData* nfcv_data) { +bool nfcv_read_card(NfcVReader* reader, FuriHalNfcDevData* nfc_data, NfcVData* nfcv_data) { furi_assert(reader); furi_assert(nfc_data); furi_assert(nfcv_data); @@ -148,15 +159,13 @@ bool nfcv_read_card( return true; } - - void nfcv_crc(uint8_t* data, uint32_t length) { uint32_t reg = 0xFFFF; - for (size_t i = 0; i < length; i++) { + for(size_t i = 0; i < length; i++) { reg = reg ^ ((uint32_t)data[i]); - for (size_t j = 0; j < 8; j++) { - if (reg & 0x0001) { + for(size_t j = 0; j < 8; j++) { + if(reg & 0x0001) { reg = (reg >> 1) ^ 0x8408; } else { reg = (reg >> 1); @@ -198,15 +207,18 @@ void nfcv_emu_alloc(NfcVData* data) { /* unmodulated 256/fc signal as building block */ data->emulation.nfcv_resp_unmod_256 = digital_signal_alloc(4); data->emulation.nfcv_resp_unmod_256->start_level = false; - data->emulation.nfcv_resp_unmod_256->edge_timings[0] = (uint32_t)(NFCV_RESP_SUBC1_UNMOD_256 * DIGITAL_SIGNAL_UNIT_S); + data->emulation.nfcv_resp_unmod_256->edge_timings[0] = + (uint32_t)(NFCV_RESP_SUBC1_UNMOD_256 * DIGITAL_SIGNAL_UNIT_S); data->emulation.nfcv_resp_unmod_256->edge_cnt = 1; } if(!data->emulation.nfcv_resp_pulse_32) { /* modulated fc/32 pulse as building block */ data->emulation.nfcv_resp_pulse_32 = digital_signal_alloc(4); data->emulation.nfcv_resp_pulse_32->start_level = true; - data->emulation.nfcv_resp_pulse_32->edge_timings[0] = (uint32_t)(NFCV_RESP_SUBC1_PULSE_32 * DIGITAL_SIGNAL_UNIT_S); - data->emulation.nfcv_resp_pulse_32->edge_timings[1] = (uint32_t)(NFCV_RESP_SUBC1_PULSE_32 * DIGITAL_SIGNAL_UNIT_S); + data->emulation.nfcv_resp_pulse_32->edge_timings[0] = + (uint32_t)(NFCV_RESP_SUBC1_PULSE_32 * DIGITAL_SIGNAL_UNIT_S); + data->emulation.nfcv_resp_pulse_32->edge_timings[1] = + (uint32_t)(NFCV_RESP_SUBC1_PULSE_32 * DIGITAL_SIGNAL_UNIT_S); data->emulation.nfcv_resp_pulse_32->edge_cnt = 2; } if(!data->emulation.nfcv_resp_one) { @@ -214,14 +226,16 @@ void nfcv_emu_alloc(NfcVData* data) { data->emulation.nfcv_resp_one = digital_signal_alloc(24); digital_signal_append(data->emulation.nfcv_resp_one, data->emulation.nfcv_resp_unmod_256); for(size_t i = 0; i < 8; i++) { - digital_signal_append(data->emulation.nfcv_resp_one, data->emulation.nfcv_resp_pulse_32); + digital_signal_append( + data->emulation.nfcv_resp_one, data->emulation.nfcv_resp_pulse_32); } } if(!data->emulation.nfcv_resp_zero) { /* logical zero: 8 pulses fc/32 then 256/fc unmodulated */ data->emulation.nfcv_resp_zero = digital_signal_alloc(24); for(size_t i = 0; i < 8; i++) { - digital_signal_append(data->emulation.nfcv_resp_zero, data->emulation.nfcv_resp_pulse_32); + digital_signal_append( + data->emulation.nfcv_resp_zero, data->emulation.nfcv_resp_pulse_32); } digital_signal_append(data->emulation.nfcv_resp_zero, data->emulation.nfcv_resp_unmod_256); } @@ -232,7 +246,8 @@ void nfcv_emu_alloc(NfcVData* data) { digital_signal_append(data->emulation.nfcv_resp_sof, data->emulation.nfcv_resp_unmod_256); digital_signal_append(data->emulation.nfcv_resp_sof, data->emulation.nfcv_resp_unmod_256); for(size_t i = 0; i < 24; i++) { - digital_signal_append(data->emulation.nfcv_resp_sof, data->emulation.nfcv_resp_pulse_32); + digital_signal_append( + data->emulation.nfcv_resp_sof, data->emulation.nfcv_resp_pulse_32); } digital_signal_append(data->emulation.nfcv_resp_sof, data->emulation.nfcv_resp_one); } @@ -241,7 +256,8 @@ void nfcv_emu_alloc(NfcVData* data) { data->emulation.nfcv_resp_eof = digital_signal_alloc(128); digital_signal_append(data->emulation.nfcv_resp_eof, data->emulation.nfcv_resp_zero); for(size_t i = 0; i < 24; i++) { - digital_signal_append(data->emulation.nfcv_resp_eof, data->emulation.nfcv_resp_pulse_32); + digital_signal_append( + data->emulation.nfcv_resp_eof, data->emulation.nfcv_resp_pulse_32); } digital_signal_append(data->emulation.nfcv_resp_eof, data->emulation.nfcv_resp_unmod_256); digital_signal_append(data->emulation.nfcv_resp_eof, data->emulation.nfcv_resp_unmod_256); @@ -250,13 +266,16 @@ void nfcv_emu_alloc(NfcVData* data) { digital_signal_append(data->emulation.nfcv_resp_eof, data->emulation.nfcv_resp_unmod_256); } - digital_sequence_set_signal(data->emulation.nfcv_signal, NFCV_SIG_SOF, data->emulation.nfcv_resp_sof); - digital_sequence_set_signal(data->emulation.nfcv_signal, NFCV_SIG_BIT0, data->emulation.nfcv_resp_zero); - digital_sequence_set_signal(data->emulation.nfcv_signal, NFCV_SIG_BIT1, data->emulation.nfcv_resp_one); - digital_sequence_set_signal(data->emulation.nfcv_signal, NFCV_SIG_EOF, data->emulation.nfcv_resp_eof); + digital_sequence_set_signal( + data->emulation.nfcv_signal, NFCV_SIG_SOF, data->emulation.nfcv_resp_sof); + digital_sequence_set_signal( + data->emulation.nfcv_signal, NFCV_SIG_BIT0, data->emulation.nfcv_resp_zero); + digital_sequence_set_signal( + data->emulation.nfcv_signal, NFCV_SIG_BIT1, data->emulation.nfcv_resp_one); + digital_sequence_set_signal( + data->emulation.nfcv_signal, NFCV_SIG_EOF, data->emulation.nfcv_resp_eof); } - static void nfcv_emu_send_raw(NfcVData* nfcv, uint8_t* data, uint8_t length) { digital_sequence_clear(nfcv->emulation.nfcv_signal); digital_sequence_add(nfcv->emulation.nfcv_signal, NFCV_SIG_SOF); @@ -266,7 +285,9 @@ static void nfcv_emu_send_raw(NfcVData* nfcv, uint8_t* data, uint8_t length) { uint32_t bit_pos = bit_total % 8; uint8_t bit_val = 0x01 << bit_pos; - digital_sequence_add(nfcv->emulation.nfcv_signal, (data[byte_pos] & bit_val) ? NFCV_SIG_BIT1 : NFCV_SIG_BIT0); + digital_sequence_add( + nfcv->emulation.nfcv_signal, + (data[byte_pos] & bit_val) ? NFCV_SIG_BIT1 : NFCV_SIG_BIT0); } digital_sequence_add(nfcv->emulation.nfcv_signal, NFCV_SIG_EOF); @@ -277,8 +298,8 @@ static void nfcv_emu_send_raw(NfcVData* nfcv, uint8_t* data, uint8_t length) { furi_hal_gpio_write(&gpio_spi_r_mosi, false); } -static void nfcv_emu_send(FuriHalNfcTxRxContext* tx_rx, NfcVData* nfcv, uint8_t* data, uint8_t length) { - +static void + nfcv_emu_send(FuriHalNfcTxRxContext* tx_rx, NfcVData* nfcv, uint8_t* data, uint8_t length) { nfcv_crc(data, length); nfcv_emu_send_raw(nfcv, data, length + 2); if(tx_rx->sniff_tx) { @@ -286,22 +307,22 @@ static void nfcv_emu_send(FuriHalNfcTxRxContext* tx_rx, NfcVData* nfcv, uint8_t* } } -static void nfcv_uidcpy(uint8_t *dst, uint8_t *src) { +static void nfcv_uidcpy(uint8_t* dst, uint8_t* src) { for(int pos = 0; pos < 8; pos++) { - dst[pos] = src[7-pos]; + dst[pos] = src[7 - pos]; } } -static int nfcv_uidcmp(uint8_t *dst, uint8_t *src) { +static int nfcv_uidcmp(uint8_t* dst, uint8_t* src) { for(int pos = 0; pos < 8; pos++) { - if(dst[pos] != src[7-pos]) { + if(dst[pos] != src[7 - pos]) { return 1; } } return 0; } -static uint32_t nfcv_read_be(uint8_t *data, uint32_t length) { +static uint32_t nfcv_read_be(uint8_t* data, uint32_t length) { uint32_t value = 0; for(uint32_t pos = 0; pos < length; pos++) { @@ -312,215 +333,261 @@ static uint32_t nfcv_read_be(uint8_t *data, uint32_t length) { return value; } -void nfcv_emu_handle_packet(FuriHalNfcTxRxContext* tx_rx, FuriHalNfcDevData* nfc_data, NfcVData* nfcv_data, uint8_t* payload, uint32_t payload_length) { - +void nfcv_emu_handle_packet( + FuriHalNfcTxRxContext* tx_rx, + FuriHalNfcDevData* nfc_data, + NfcVData* nfcv_data, + uint8_t* payload, + uint32_t payload_length) { if(!payload_length) { return; } - + uint8_t flags = payload[0]; uint8_t command = payload[1]; - bool addressed = !(flags & RFAL_NFCV_REQ_FLAG_INVENTORY) && (flags & RFAL_NFCV_REQ_FLAG_ADDRESS); + bool addressed = !(flags & RFAL_NFCV_REQ_FLAG_INVENTORY) && + (flags & RFAL_NFCV_REQ_FLAG_ADDRESS); bool advanced = (command >= 0xA0); uint8_t address_offset = 2 + (advanced ? 1 : 0); uint8_t payload_offset = address_offset + (addressed ? 8 : 0); - uint8_t *address = &payload[address_offset]; + uint8_t* address = &payload[address_offset]; uint8_t response_buffer[32]; if(addressed && nfcv_uidcmp(address, nfc_data->uid)) { FURI_LOG_D(TAG, "addressed command 0x%02X, but not for us:", command); - FURI_LOG_D(TAG, " dest: %02X%02X%02X%02X%02X%02X%02X%02X", address[7], address[6], address[5], address[4], address[3], address[2], address[1], address[0]); - FURI_LOG_D(TAG, " our UID: %02X%02X%02X%02X%02X%02X%02X%02X", nfc_data->uid[0], nfc_data->uid[1], nfc_data->uid[2], nfc_data->uid[3], nfc_data->uid[4], nfc_data->uid[5], nfc_data->uid[6], nfc_data->uid[7]); + FURI_LOG_D( + TAG, + " dest: %02X%02X%02X%02X%02X%02X%02X%02X", + address[7], + address[6], + address[5], + address[4], + address[3], + address[2], + address[1], + address[0]); + FURI_LOG_D( + TAG, + " our UID: %02X%02X%02X%02X%02X%02X%02X%02X", + nfc_data->uid[0], + nfc_data->uid[1], + nfc_data->uid[2], + nfc_data->uid[3], + nfc_data->uid[4], + nfc_data->uid[5], + nfc_data->uid[6], + nfc_data->uid[7]); return; } - switch(nfcv_data->type) { - case NfcVTypeSlixL: - if(nfcv_data->sub_data.slix_l.privacy && - command != ISO15693_CMD_NXP_GET_RANDOM_NUMBER && - command != ISO15693_CMD_NXP_SET_PASSWORD) { - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "command 0x%02X ignored, privacy mode", command); - FURI_LOG_D(TAG, "%s", nfcv_data->last_command); - return; - } - break; + case NfcVTypeSlixL: + if(nfcv_data->sub_data.slix_l.privacy && command != ISO15693_CMD_NXP_GET_RANDOM_NUMBER && + command != ISO15693_CMD_NXP_SET_PASSWORD) { + snprintf( + nfcv_data->last_command, + sizeof(nfcv_data->last_command), + "command 0x%02X ignored, privacy mode", + command); + FURI_LOG_D(TAG, "%s", nfcv_data->last_command); + return; + } + break; - default: - break; + default: + break; } - + /* unfortunately our response is quicker than the original NFC tag which causes frame misses */ furi_delay_us(270); switch(command) { - case ISO15693_INVENTORY: - { - response_buffer[0] = ISO15693_NOERROR; - response_buffer[1] = nfcv_data->dsfid; - nfcv_uidcpy(&response_buffer[2], nfc_data->uid); + case ISO15693_INVENTORY: { + response_buffer[0] = ISO15693_NOERROR; + response_buffer[1] = nfcv_data->dsfid; + nfcv_uidcpy(&response_buffer[2], nfc_data->uid); - nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 10); - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "INVENTORY"); - break; - } + nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 10); + snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "INVENTORY"); + break; + } - case ISO15693_STAYQUIET: - { - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "STAYQUIET"); - break; - } + case ISO15693_STAYQUIET: { + snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "STAYQUIET"); + break; + } - case ISO15693_LOCKBLOCK: - { - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "LOCKBLOCK"); - break; - } + case ISO15693_LOCKBLOCK: { + snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "LOCKBLOCK"); + break; + } - case ISO15693_READ_MULTI_BLOCK: - { - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "READ_MULTI_BLOCK"); - break; - } + case ISO15693_READ_MULTI_BLOCK: { + snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "READ_MULTI_BLOCK"); + break; + } - case ISO15693_WRITE_MULTI_BLOCK: - { - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "WRITE_MULTI_BLOCK"); - break; - } + case ISO15693_WRITE_MULTI_BLOCK: { + snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "WRITE_MULTI_BLOCK"); + break; + } - case ISO15693_SELECT: - { - response_buffer[0] = ISO15693_NOERROR; + case ISO15693_SELECT: { + response_buffer[0] = ISO15693_NOERROR; + nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1); + snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "SELECT"); + break; + } + + case ISO15693_READBLOCK: { + uint8_t block = payload[payload_offset]; + + if(block >= nfcv_data->block_num) { + response_buffer[0] = ISO15693_ERROR_BLOCK_WRITE; nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1); - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "SELECT"); - break; - } - - case ISO15693_READBLOCK: - { - uint8_t block = payload[payload_offset]; - - if(block >= nfcv_data->block_num) { - response_buffer[0] = ISO15693_ERROR_BLOCK_WRITE; - nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1); - } else { - response_buffer[0] = ISO15693_NOERROR; - memcpy(&response_buffer[1], &nfcv_data->data[nfcv_data->block_size * block], nfcv_data->block_size); - nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1 + nfcv_data->block_size); - } - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "READ BLOCK %d", block); - break; - } - - case ISO15693_WRITEBLOCK: - { - uint8_t block = payload[payload_offset]; - uint8_t *data = &payload[payload_offset + 1]; - - if(block >= nfcv_data->block_num) { - response_buffer[0] = ISO15693_ERROR_BLOCK_WRITE; - } else { - response_buffer[0] = ISO15693_NOERROR; - memcpy(&nfcv_data->data[nfcv_data->block_size * block], &response_buffer[1], nfcv_data->block_size); - } - nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1); - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "WRITE BLOCK %d <- %02X %02X %02X %02X", block, data[0], data[1], data[2], data[3]); - break; - } - - case ISO15693_GET_SYSTEM_INFO: - { - response_buffer[0] = ISO15693_NOERROR; - response_buffer[1] = 0x0F; - nfcv_uidcpy(&response_buffer[2], nfc_data->uid); - response_buffer[10] = nfcv_data->dsfid; /* DSFID */ - response_buffer[11] = nfcv_data->afi; /* AFI */ - response_buffer[12] = nfcv_data->block_num - 1; /* number of blocks */ - response_buffer[13] = nfcv_data->block_size - 1; /* block size */ - response_buffer[14] = nfcv_data->ic_ref; /* IC reference */ - - nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 15); - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "SYSTEMINFO"); - - break; - } - - case ISO15693_CMD_NXP_GET_RANDOM_NUMBER: - { - nfcv_data->sub_data.slix_l.rand[0] = furi_hal_random_get(); - nfcv_data->sub_data.slix_l.rand[1] = furi_hal_random_get(); - + } else { response_buffer[0] = ISO15693_NOERROR; - response_buffer[1] = nfcv_data->sub_data.slix_l.rand[1]; - response_buffer[2] = nfcv_data->sub_data.slix_l.rand[0]; - - nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 3); - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), - "GET_RANDOM_NUMBER -> 0x%02X%02X", - nfcv_data->sub_data.slix_l.rand[0], - nfcv_data->sub_data.slix_l.rand[1]); - break; + memcpy( + &response_buffer[1], + &nfcv_data->data[nfcv_data->block_size * block], + nfcv_data->block_size); + nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1 + nfcv_data->block_size); } + snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "READ BLOCK %d", block); + break; + } - case ISO15693_CMD_NXP_SET_PASSWORD: - { - uint8_t password_id = payload[payload_offset]; - uint8_t *password_xored = &payload[payload_offset + 1]; - uint8_t *rand = nfcv_data->sub_data.slix_l.rand; - uint8_t *password = NULL; - uint8_t password_rcv[4]; + case ISO15693_WRITEBLOCK: { + uint8_t block = payload[payload_offset]; + uint8_t* data = &payload[payload_offset + 1]; - switch(password_id) { - case 4: - password = nfcv_data->sub_data.slix_l.key_privacy; - break; - case 8: - password = nfcv_data->sub_data.slix_l.key_destroy; - break; - case 10: - password = nfcv_data->sub_data.slix_l.key_eas; - break; - default: - break; - } - - for(int pos = 0; pos < 4; pos++) { - password_rcv[pos] = password_xored[3 - pos] ^ rand[pos % 2]; - } - uint32_t pass_expect = nfcv_read_be(password, 4); - uint32_t pass_received = nfcv_read_be(password_rcv, 4); - - /* if the password is all-zeroes, just accept any password*/ - if(!pass_expect || pass_expect == pass_received) { - nfcv_data->sub_data.slix_l.privacy = false; - response_buffer[0] = ISO15693_NOERROR; - nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1); - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "SET_PASSWORD #%02X 0x%08lX OK", password_id, pass_received); - } else { - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "SET_PASSWORD #%02X 0x%08lX/%08lX FAIL", password_id, pass_received, pass_expect); - } - break; - } - - case ISO15693_CMD_NXP_ENABLE_PRIVACY: - { + if(block >= nfcv_data->block_num) { + response_buffer[0] = ISO15693_ERROR_BLOCK_WRITE; + } else { response_buffer[0] = ISO15693_NOERROR; - - nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1); - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "ISO15693_CMD_NXP_ENABLE_PRIVACY"); - - nfcv_data->sub_data.slix_l.privacy = true; - break; + memcpy( + &nfcv_data->data[nfcv_data->block_size * block], + &response_buffer[1], + nfcv_data->block_size); } + nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1); + snprintf( + nfcv_data->last_command, + sizeof(nfcv_data->last_command), + "WRITE BLOCK %d <- %02X %02X %02X %02X", + block, + data[0], + data[1], + data[2], + data[3]); + break; + } + case ISO15693_GET_SYSTEM_INFO: { + response_buffer[0] = ISO15693_NOERROR; + response_buffer[1] = 0x0F; + nfcv_uidcpy(&response_buffer[2], nfc_data->uid); + response_buffer[10] = nfcv_data->dsfid; /* DSFID */ + response_buffer[11] = nfcv_data->afi; /* AFI */ + response_buffer[12] = nfcv_data->block_num - 1; /* number of blocks */ + response_buffer[13] = nfcv_data->block_size - 1; /* block size */ + response_buffer[14] = nfcv_data->ic_ref; /* IC reference */ + + nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 15); + snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "SYSTEMINFO"); + + break; + } + + case ISO15693_CMD_NXP_GET_RANDOM_NUMBER: { + nfcv_data->sub_data.slix_l.rand[0] = furi_hal_random_get(); + nfcv_data->sub_data.slix_l.rand[1] = furi_hal_random_get(); + + response_buffer[0] = ISO15693_NOERROR; + response_buffer[1] = nfcv_data->sub_data.slix_l.rand[1]; + response_buffer[2] = nfcv_data->sub_data.slix_l.rand[0]; + + nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 3); + snprintf( + nfcv_data->last_command, + sizeof(nfcv_data->last_command), + "GET_RANDOM_NUMBER -> 0x%02X%02X", + nfcv_data->sub_data.slix_l.rand[0], + nfcv_data->sub_data.slix_l.rand[1]); + break; + } + + case ISO15693_CMD_NXP_SET_PASSWORD: { + uint8_t password_id = payload[payload_offset]; + uint8_t* password_xored = &payload[payload_offset + 1]; + uint8_t* rand = nfcv_data->sub_data.slix_l.rand; + uint8_t* password = NULL; + uint8_t password_rcv[4]; + + switch(password_id) { + case 4: + password = nfcv_data->sub_data.slix_l.key_privacy; + break; + case 8: + password = nfcv_data->sub_data.slix_l.key_destroy; + break; + case 10: + password = nfcv_data->sub_data.slix_l.key_eas; + break; default: - snprintf(nfcv_data->last_command, sizeof(nfcv_data->last_command), "unsupported: %02X", command); break; + } + + for(int pos = 0; pos < 4; pos++) { + password_rcv[pos] = password_xored[3 - pos] ^ rand[pos % 2]; + } + uint32_t pass_expect = nfcv_read_be(password, 4); + uint32_t pass_received = nfcv_read_be(password_rcv, 4); + + /* if the password is all-zeroes, just accept any password*/ + if(!pass_expect || pass_expect == pass_received) { + nfcv_data->sub_data.slix_l.privacy = false; + response_buffer[0] = ISO15693_NOERROR; + nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1); + snprintf( + nfcv_data->last_command, + sizeof(nfcv_data->last_command), + "SET_PASSWORD #%02X 0x%08lX OK", + password_id, + pass_received); + } else { + snprintf( + nfcv_data->last_command, + sizeof(nfcv_data->last_command), + "SET_PASSWORD #%02X 0x%08lX/%08lX FAIL", + password_id, + pass_received, + pass_expect); + } + break; + } + + case ISO15693_CMD_NXP_ENABLE_PRIVACY: { + response_buffer[0] = ISO15693_NOERROR; + + nfcv_emu_send(tx_rx, nfcv_data, response_buffer, 1); + snprintf( + nfcv_data->last_command, + sizeof(nfcv_data->last_command), + "ISO15693_CMD_NXP_ENABLE_PRIVACY"); + + nfcv_data->sub_data.slix_l.privacy = true; + break; + } + + default: + snprintf( + nfcv_data->last_command, sizeof(nfcv_data->last_command), "unsupported: %02X", command); + break; } if(strlen(nfcv_data->last_command) > 0) { - FURI_LOG_D(TAG, "Received command %s", nfcv_data->last_command); + FURI_LOG_D(TAG, "Received command %s", nfcv_data->last_command); } } @@ -536,30 +603,45 @@ void nfcv_emu_init(FuriHalNfcDevData* nfc_data, NfcVData* nfcv_data) { furi_hal_spi_bus_handle_deinit(&furi_hal_spi_bus_handle_nfc); FURI_LOG_D(TAG, "Starting NfcV emulation"); - FURI_LOG_D(TAG, " UID: %02X %02X %02X %02X %02X %02X %02X %02X", - nfc_data->uid[0], nfc_data->uid[1], nfc_data->uid[2], nfc_data->uid[3], - nfc_data->uid[4], nfc_data->uid[5], nfc_data->uid[6], nfc_data->uid[7]); + FURI_LOG_D( + TAG, + " UID: %02X %02X %02X %02X %02X %02X %02X %02X", + nfc_data->uid[0], + nfc_data->uid[1], + nfc_data->uid[2], + nfc_data->uid[3], + nfc_data->uid[4], + nfc_data->uid[5], + nfc_data->uid[6], + nfc_data->uid[7]); switch(nfcv_data->type) { - case NfcVTypeSlixL: - FURI_LOG_D(TAG, " Card type: SLIX-L"); - FURI_LOG_D(TAG, " Privacy pass: 0x%08lX", nfcv_read_be(nfcv_data->sub_data.slix_l.key_privacy, 4)); - FURI_LOG_D(TAG, " Destroy pass: 0x%08lX", nfcv_read_be(nfcv_data->sub_data.slix_l.key_destroy, 4)); - FURI_LOG_D(TAG, " EAS pass: 0x%08lX", nfcv_read_be(nfcv_data->sub_data.slix_l.key_eas, 4)); - FURI_LOG_D(TAG, " Privacy mode: %s", nfcv_data->sub_data.slix_l.privacy ? "ON" : "OFF"); - break; - case NfcVTypeSlixS: - FURI_LOG_D(TAG, " Card type: SLIX-S"); - break; - case NfcVTypeSlix2: - FURI_LOG_D(TAG, " Card type: SLIX2"); - break; - case NfcVTypePlain: - FURI_LOG_D(TAG, " Card type: Plain"); - break; - case NfcVTypeSlix: - FURI_LOG_D(TAG, " Card type: SLIX-L"); - break; + case NfcVTypeSlixL: + FURI_LOG_D(TAG, " Card type: SLIX-L"); + FURI_LOG_D( + TAG, + " Privacy pass: 0x%08lX", + nfcv_read_be(nfcv_data->sub_data.slix_l.key_privacy, 4)); + FURI_LOG_D( + TAG, + " Destroy pass: 0x%08lX", + nfcv_read_be(nfcv_data->sub_data.slix_l.key_destroy, 4)); + FURI_LOG_D( + TAG, " EAS pass: 0x%08lX", nfcv_read_be(nfcv_data->sub_data.slix_l.key_eas, 4)); + FURI_LOG_D(TAG, " Privacy mode: %s", nfcv_data->sub_data.slix_l.privacy ? "ON" : "OFF"); + break; + case NfcVTypeSlixS: + FURI_LOG_D(TAG, " Card type: SLIX-S"); + break; + case NfcVTypeSlix2: + FURI_LOG_D(TAG, " Card type: SLIX2"); + break; + case NfcVTypePlain: + FURI_LOG_D(TAG, " Card type: Plain"); + break; + case NfcVTypeSlix: + FURI_LOG_D(TAG, " Card type: SLIX-L"); + break; } /* allocate a 512 edge buffer, more than enough */ @@ -579,8 +661,11 @@ void nfcv_emu_deinit(NfcVData* nfcv_data) { pulse_reader_free(nfcv_data->emulation.reader_signal); } -bool nfcv_emu_loop(FuriHalNfcTxRxContext* tx_rx, FuriHalNfcDevData* nfc_data, NfcVData* nfcv_data, uint32_t timeout_ms) { - +bool nfcv_emu_loop( + FuriHalNfcTxRxContext* tx_rx, + FuriHalNfcDevData* nfc_data, + NfcVData* nfcv_data, + uint32_t timeout_ms) { bool ret = false; uint32_t frame_state = NFCV_FRAME_STATE_SOF1; uint32_t periods_previous = 0; @@ -592,8 +677,8 @@ bool nfcv_emu_loop(FuriHalNfcTxRxContext* tx_rx, FuriHalNfcDevData* nfc_data, Nf bool wait_for_pulse = false; while(true) { - - uint32_t periods = pulse_reader_receive(nfcv_data->emulation.reader_signal, timeout_ms * 1000); + uint32_t periods = + pulse_reader_receive(nfcv_data->emulation.reader_signal, timeout_ms * 1000); if(periods == PULSE_READER_NO_EDGE) { break; @@ -602,113 +687,137 @@ bool nfcv_emu_loop(FuriHalNfcTxRxContext* tx_rx, FuriHalNfcDevData* nfc_data, Nf if(wait_for_pulse) { wait_for_pulse = false; if(periods != 1) { - snprintf(reset_reason, sizeof(reset_reason), "SOF: Expected a single low pulse in state %lu, but got %lu", frame_state, periods); + snprintf( + reset_reason, + sizeof(reset_reason), + "SOF: Expected a single low pulse in state %lu, but got %lu", + frame_state, + periods); frame_state = NFCV_FRAME_STATE_RESET; } continue; } switch(frame_state) { - case NFCV_FRAME_STATE_SOF1: - if(periods == 1) { - frame_state = NFCV_FRAME_STATE_SOF2; - } else { - frame_state = NFCV_FRAME_STATE_SOF1; - break; - } + case NFCV_FRAME_STATE_SOF1: + if(periods == 1) { + frame_state = NFCV_FRAME_STATE_SOF2; + } else { + frame_state = NFCV_FRAME_STATE_SOF1; break; + } + break; - case NFCV_FRAME_STATE_SOF2: - /* waiting for the second low period, telling us about coding */ - if(periods == 6) { - frame_state = NFCV_FRAME_STATE_CODING_256; - periods_previous = 0; - wait_for_pulse = true; - } else if(periods == 4) { - frame_state = NFCV_FRAME_STATE_CODING_4; - periods_previous = 2; - wait_for_pulse = true; - } else { - snprintf(reset_reason, sizeof(reset_reason), "SOF: Expected 4/6 periods, got %lu", periods); - frame_state = NFCV_FRAME_STATE_SOF1; - } - break; - - case NFCV_FRAME_STATE_CODING_256: - if(periods_previous > periods) { - snprintf(reset_reason, sizeof(reset_reason), "1oo256: Missing %lu periods from previous symbol, got %lu", periods_previous, periods); - frame_state = NFCV_FRAME_STATE_RESET; - break; - } - /* previous symbol left us with some pulse periods */ - periods -= periods_previous; - - if(periods > 512) { - snprintf(reset_reason, sizeof(reset_reason), "1oo256: %lu periods is too much", periods); - frame_state = NFCV_FRAME_STATE_RESET; - break; - } - - if(periods == 2) { - frame_state = NFCV_FRAME_STATE_EOF; - break; - } - - periods_previous = 512 - (periods + 1); - byte_value = (periods - 1) / 2; - frame_payload[frame_pos++] = (uint8_t)byte_value; - - wait_for_pulse = true; - - break; - - case NFCV_FRAME_STATE_CODING_4: - if(periods_previous > periods) { - snprintf(reset_reason, sizeof(reset_reason), "1oo4: Missing %lu periods from previous symbol, got %lu", periods_previous, periods); - frame_state = NFCV_FRAME_STATE_RESET; - break; - } - - /* previous symbol left us with some pulse periods */ - periods -= periods_previous; + case NFCV_FRAME_STATE_SOF2: + /* waiting for the second low period, telling us about coding */ + if(periods == 6) { + frame_state = NFCV_FRAME_STATE_CODING_256; periods_previous = 0; - - byte_value >>= 2; - bits_received += 2; - - if(periods == 1) { - byte_value |= 0x00 << 6; - periods_previous = 6; - } else if(periods == 3) { - byte_value |= 0x01 << 6; - periods_previous = 4; - } else if(periods == 5) { - byte_value |= 0x02 << 6; - periods_previous = 2; - } else if(periods == 7) { - byte_value |= 0x03 << 6; - periods_previous = 0; - } else if(periods == 2) { - frame_state = NFCV_FRAME_STATE_EOF; - break; - } else { - snprintf(reset_reason, sizeof(reset_reason), "1oo4: Expected 1/3/5/7 low pulses, but got %lu", periods); - frame_state = NFCV_FRAME_STATE_RESET; - break; - } - - if(bits_received >= 8) { - frame_payload[frame_pos++] = (uint8_t)byte_value; - bits_received = 0; - } wait_for_pulse = true; + } else if(periods == 4) { + frame_state = NFCV_FRAME_STATE_CODING_4; + periods_previous = 2; + wait_for_pulse = true; + } else { + snprintf( + reset_reason, + sizeof(reset_reason), + "SOF: Expected 4/6 periods, got %lu", + periods); + frame_state = NFCV_FRAME_STATE_SOF1; + } + break; + + case NFCV_FRAME_STATE_CODING_256: + if(periods_previous > periods) { + snprintf( + reset_reason, + sizeof(reset_reason), + "1oo256: Missing %lu periods from previous symbol, got %lu", + periods_previous, + periods); + frame_state = NFCV_FRAME_STATE_RESET; break; + } + /* previous symbol left us with some pulse periods */ + periods -= periods_previous; + + if(periods > 512) { + snprintf( + reset_reason, sizeof(reset_reason), "1oo256: %lu periods is too much", periods); + frame_state = NFCV_FRAME_STATE_RESET; + break; + } + + if(periods == 2) { + frame_state = NFCV_FRAME_STATE_EOF; + break; + } + + periods_previous = 512 - (periods + 1); + byte_value = (periods - 1) / 2; + frame_payload[frame_pos++] = (uint8_t)byte_value; + + wait_for_pulse = true; + + break; + + case NFCV_FRAME_STATE_CODING_4: + if(periods_previous > periods) { + snprintf( + reset_reason, + sizeof(reset_reason), + "1oo4: Missing %lu periods from previous symbol, got %lu", + periods_previous, + periods); + frame_state = NFCV_FRAME_STATE_RESET; + break; + } + + /* previous symbol left us with some pulse periods */ + periods -= periods_previous; + periods_previous = 0; + + byte_value >>= 2; + bits_received += 2; + + if(periods == 1) { + byte_value |= 0x00 << 6; + periods_previous = 6; + } else if(periods == 3) { + byte_value |= 0x01 << 6; + periods_previous = 4; + } else if(periods == 5) { + byte_value |= 0x02 << 6; + periods_previous = 2; + } else if(periods == 7) { + byte_value |= 0x03 << 6; + periods_previous = 0; + } else if(periods == 2) { + frame_state = NFCV_FRAME_STATE_EOF; + break; + } else { + snprintf( + reset_reason, + sizeof(reset_reason), + "1oo4: Expected 1/3/5/7 low pulses, but got %lu", + periods); + frame_state = NFCV_FRAME_STATE_RESET; + break; + } + + if(bits_received >= 8) { + frame_payload[frame_pos++] = (uint8_t)byte_value; + bits_received = 0; + } + wait_for_pulse = true; + break; } - + /* post-state-machine cleanup and reset */ if(frame_state == NFCV_FRAME_STATE_RESET) { frame_state = NFCV_FRAME_STATE_SOF1; - + FURI_LOG_D(TAG, "Resetting state machine, reason: '%s'", reset_reason); } else if(frame_state == NFCV_FRAME_STATE_EOF) { break; diff --git a/lib/nfc/protocols/nfcv.h b/lib/nfc/protocols/nfcv.h index 3e1c651dd..e6e63f7d4 100644 --- a/lib/nfc/protocols/nfcv.h +++ b/lib/nfc/protocols/nfcv.h @@ -8,83 +8,77 @@ #include "nfc_util.h" #include +#define NFCV_FC (13560000.0f) /* MHz */ +#define NFCV_RESP_SUBC1_PULSE_32 (1.0f / (NFCV_FC / 32) / 2.0f) /* 1.1799 µs */ +#define NFCV_RESP_SUBC1_UNMOD_256 (256.0f / NFCV_FC) /* 18.8791 µs */ +#define PULSE_DURATION_NS (128.0f * 1000000000.0f / NFCV_FC) /* ns */ +#define DIGITAL_SIGNAL_UNIT_S (100000000000.0f) +#define DIGITAL_SIGNAL_UNIT_US (100000.0f) -#define NFCV_FC (13560000.0f) /* MHz */ -#define NFCV_RESP_SUBC1_PULSE_32 (1.0f / (NFCV_FC/32) / 2.0f) /* 1.1799 µs */ -#define NFCV_RESP_SUBC1_UNMOD_256 (256.0f / NFCV_FC) /* 18.8791 µs */ +#define NFCV_TOTAL_BLOCKS_MAX 256 +#define NFCV_BLOCK_SIZE 4 +#define NFCV_MAX_DUMP_SIZE (NFCV_BLOCK_SIZE * NFCV_TOTAL_BLOCKS_MAX) -#define PULSE_DURATION_NS (128.0f * 1000000000.0f / NFCV_FC) /* ns */ +#define NFCV_FRAME_STATE_SOF1 0 +#define NFCV_FRAME_STATE_SOF2 1 +#define NFCV_FRAME_STATE_CODING_4 2 +#define NFCV_FRAME_STATE_CODING_256 3 +#define NFCV_FRAME_STATE_EOF 4 +#define NFCV_FRAME_STATE_RESET 5 -#define DIGITAL_SIGNAL_UNIT_S (100000000000.0f) -#define DIGITAL_SIGNAL_UNIT_US (100000.0f) - -#define NFCV_TOTAL_BLOCKS_MAX 256 -#define NFCV_BLOCK_SIZE 4 -#define NFCV_MAX_DUMP_SIZE (NFCV_BLOCK_SIZE*NFCV_TOTAL_BLOCKS_MAX) - - -#define NFCV_FRAME_STATE_SOF1 0 -#define NFCV_FRAME_STATE_SOF2 1 -#define NFCV_FRAME_STATE_CODING_4 2 -#define NFCV_FRAME_STATE_CODING_256 3 -#define NFCV_FRAME_STATE_EOF 4 -#define NFCV_FRAME_STATE_RESET 5 - - -#define NFCV_SIG_SOF 0 +#define NFCV_SIG_SOF 0 #define NFCV_SIG_BIT0 1 #define NFCV_SIG_BIT1 2 -#define NFCV_SIG_EOF 3 +#define NFCV_SIG_EOF 3 /* */ -#define ISO15693_INVENTORY 0x01 -#define ISO15693_STAYQUIET 0x02 -#define ISO15693_READBLOCK 0x20 -#define ISO15693_WRITEBLOCK 0x21 -#define ISO15693_LOCKBLOCK 0x22 -#define ISO15693_READ_MULTI_BLOCK 0x23 -#define ISO15693_WRITE_MULTI_BLOCK 0x24 -#define ISO15693_SELECT 0x25 -#define ISO15693_RESET_TO_READY 0x26 -#define ISO15693_WRITE_AFI 0x27 -#define ISO15693_LOCK_AFI 0x28 -#define ISO15693_WRITE_DSFID 0x29 -#define ISO15693_LOCK_DSFID 0x2A -#define ISO15693_GET_SYSTEM_INFO 0x2B -#define ISO15693_READ_MULTI_SECSTATUS 0x2C +#define ISO15693_INVENTORY 0x01 +#define ISO15693_STAYQUIET 0x02 +#define ISO15693_READBLOCK 0x20 +#define ISO15693_WRITEBLOCK 0x21 +#define ISO15693_LOCKBLOCK 0x22 +#define ISO15693_READ_MULTI_BLOCK 0x23 +#define ISO15693_WRITE_MULTI_BLOCK 0x24 +#define ISO15693_SELECT 0x25 +#define ISO15693_RESET_TO_READY 0x26 +#define ISO15693_WRITE_AFI 0x27 +#define ISO15693_LOCK_AFI 0x28 +#define ISO15693_WRITE_DSFID 0x29 +#define ISO15693_LOCK_DSFID 0x2A +#define ISO15693_GET_SYSTEM_INFO 0x2B +#define ISO15693_READ_MULTI_SECSTATUS 0x2C // ISO15693 MANUFACTURER CODES #define ISO15693_MANUFACTURER_NXP 0x04 // ISO15693-3 CUSTOM NXP COMMANDS -#define ISO15693_CMD_NXP_SET_EAS 0xA2 -#define ISO15693_CMD_NXP_RESET_EAS 0xA3 -#define ISO15693_CMD_NXP_LOCK_EAS 0xA4 -#define ISO15693_CMD_NXP_EAS_ALARM 0xA5 +#define ISO15693_CMD_NXP_SET_EAS 0xA2 +#define ISO15693_CMD_NXP_RESET_EAS 0xA3 +#define ISO15693_CMD_NXP_LOCK_EAS 0xA4 +#define ISO15693_CMD_NXP_EAS_ALARM 0xA5 #define ISO15693_CMD_NXP_PASSWORD_PROTECT_EAS_AFI 0xA6 -#define ISO15693_CMD_NXP_WRITE_EAS_ID 0xA7 -#define ISO15693_CMD_NXP_INVENTORY_PAGE_READ 0xB0 +#define ISO15693_CMD_NXP_WRITE_EAS_ID 0xA7 +#define ISO15693_CMD_NXP_INVENTORY_PAGE_READ 0xB0 #define ISO15693_CMD_NXP_INVENTORY_PAGE_READ_FAST 0xB1 -#define ISO15693_CMD_NXP_GET_RANDOM_NUMBER 0xB2 -#define ISO15693_CMD_NXP_SET_PASSWORD 0xB3 -#define ISO15693_CMD_NXP_WRITE_PASSWORD 0xB4 -#define ISO15693_CMD_NXP_DESTROY 0xB9 -#define ISO15693_CMD_NXP_ENABLE_PRIVACY 0xBA +#define ISO15693_CMD_NXP_GET_RANDOM_NUMBER 0xB2 +#define ISO15693_CMD_NXP_SET_PASSWORD 0xB3 +#define ISO15693_CMD_NXP_WRITE_PASSWORD 0xB4 +#define ISO15693_CMD_NXP_DESTROY 0xB9 +#define ISO15693_CMD_NXP_ENABLE_PRIVACY 0xBA // ISO15693 RESPONSE ERROR CODES -#define ISO15693_NOERROR 0x00 -#define ISO15693_ERROR_CMD_NOT_SUP 0x01 // Command not supported -#define ISO15693_ERROR_CMD_NOT_REC 0x02 // Command not recognized (eg. parameter error) -#define ISO15693_ERROR_CMD_OPTION 0x03 // Command option not supported -#define ISO15693_ERROR_GENERIC 0x0F // No additional Info about this error -#define ISO15693_ERROR_BLOCK_UNAVAILABLE 0x10 -#define ISO15693_ERROR_BLOCK_LOCKED_ALREADY 0x11 // cannot lock again -#define ISO15693_ERROR_BLOCK_LOCKED 0x12 // cannot be changed -#define ISO15693_ERROR_BLOCK_WRITE 0x13 // Writing was unsuccessful -#define ISO15693_ERROR_BLOCL_WRITELOCK 0x14 // Locking was unsuccessful - +#define ISO15693_NOERROR 0x00 +#define ISO15693_ERROR_CMD_NOT_SUP 0x01 // Command not supported +#define ISO15693_ERROR_CMD_NOT_REC 0x02 // Command not recognized (eg. parameter error) +#define ISO15693_ERROR_CMD_OPTION 0x03 // Command option not supported +#define ISO15693_ERROR_GENERIC 0x0F // No additional Info about this error +#define ISO15693_ERROR_BLOCK_UNAVAILABLE 0x10 +#define ISO15693_ERROR_BLOCK_LOCKED_ALREADY 0x11 // cannot lock again +#define ISO15693_ERROR_BLOCK_LOCKED 0x12 // cannot be changed +#define ISO15693_ERROR_BLOCK_WRITE 0x13 // Writing was unsuccessful +#define ISO15693_ERROR_BLOCL_WRITELOCK 0x14 // Locking was unsuccessful typedef enum { NfcVAuthMethodManual, @@ -93,7 +87,7 @@ typedef enum { typedef enum { NfcVTypePlain = 0, - NfcVTypeSlix = 1, + NfcVTypeSlix = 1, NfcVTypeSlixS = 2, NfcVTypeSlixL = 3, NfcVTypeSlix2 = 4, @@ -139,9 +133,8 @@ typedef union { NfcVSlixLData slix_l; } NfcVSubtypeData; - typedef struct { - PulseReader *reader_signal; + PulseReader* reader_signal; DigitalSignal* nfcv_resp_pulse_32; DigitalSignal* nfcv_resp_unmod; DigitalSignal* nfcv_resp_one; @@ -179,11 +172,15 @@ typedef struct { int16_t blocks_read; } NfcVReader; -ReturnCode nfcv_read_blocks(NfcVReader* reader, NfcVData* data); +ReturnCode nfcv_read_blocks(NfcVReader* reader, NfcVData* data); ReturnCode nfcv_read_sysinfo(FuriHalNfcDevData* nfc_data, NfcVData* data); ReturnCode nfcv_inventory(uint8_t* uid); bool nfcv_read_card(NfcVReader* reader, FuriHalNfcDevData* nfc_data, NfcVData* data); void nfcv_emu_init(FuriHalNfcDevData* nfc_data, NfcVData* nfcv_data); void nfcv_emu_deinit(NfcVData* nfcv_data); -bool nfcv_emu_loop(FuriHalNfcTxRxContext* tx_rx, FuriHalNfcDevData* nfc_data, NfcVData* nfcv_data, uint32_t timeout_ms); +bool nfcv_emu_loop( + FuriHalNfcTxRxContext* tx_rx, + FuriHalNfcDevData* nfc_data, + NfcVData* nfcv_data, + uint32_t timeout_ms); diff --git a/lib/nfc/protocols/slix.c b/lib/nfc/protocols/slix.c index ca3ebcd67..659be07ca 100644 --- a/lib/nfc/protocols/slix.c +++ b/lib/nfc/protocols/slix.c @@ -6,59 +6,49 @@ #include #include "furi_hal_nfc.h" - bool slix_check_card_type(FuriHalNfcDevData* nfc_data) { - if((nfc_data->uid[0] == 0xE0) - && (nfc_data->uid[1] == 0x04) - && (nfc_data->uid[2] == 0x01) - && (((nfc_data->uid[3] >> 4) & 3) == 2)) { + if((nfc_data->uid[0] == 0xE0) && (nfc_data->uid[1] == 0x04) && (nfc_data->uid[2] == 0x01) && + (((nfc_data->uid[3] >> 4) & 3) == 2)) { return true; } return false; } bool slix2_check_card_type(FuriHalNfcDevData* nfc_data) { - if((nfc_data->uid[0] == 0xE0) - && (nfc_data->uid[1] == 0x04) - && (nfc_data->uid[2] == 0x01) - && (((nfc_data->uid[3] >> 4) & 3) == 1)) { + if((nfc_data->uid[0] == 0xE0) && (nfc_data->uid[1] == 0x04) && (nfc_data->uid[2] == 0x01) && + (((nfc_data->uid[3] >> 4) & 3) == 1)) { return true; } return false; } bool slix_s_check_card_type(FuriHalNfcDevData* nfc_data) { - if((nfc_data->uid[0] == 0xE0) - && (nfc_data->uid[1] == 0x04) - && (nfc_data->uid[2] == 0x02)) { + if((nfc_data->uid[0] == 0xE0) && (nfc_data->uid[1] == 0x04) && (nfc_data->uid[2] == 0x02)) { return true; } return false; } bool slix_l_check_card_type(FuriHalNfcDevData* nfc_data) { - if((nfc_data->uid[0] == 0xE0) - && (nfc_data->uid[1] == 0x04) - && (nfc_data->uid[2] == 0x03)) { + if((nfc_data->uid[0] == 0xE0) && (nfc_data->uid[1] == 0x04) && (nfc_data->uid[2] == 0x03)) { return true; } return false; } - ReturnCode slix_l_get_random(NfcVData* data) { uint16_t received = 0; uint8_t rxBuf[32]; ReturnCode ret = rfalNfcvPollerTransceiveReq( - ISO15693_CMD_NXP_GET_RANDOM_NUMBER, - RFAL_NFCV_REQ_FLAG_DEFAULT, - ISO15693_MANUFACTURER_NXP, - NULL, + ISO15693_CMD_NXP_GET_RANDOM_NUMBER, + RFAL_NFCV_REQ_FLAG_DEFAULT, + ISO15693_MANUFACTURER_NXP, NULL, - 0, - rxBuf, - sizeof(rxBuf), + NULL, + 0, + rxBuf, + sizeof(rxBuf), &received); if(ret == ERR_NONE) { @@ -76,30 +66,29 @@ ReturnCode slix_l_get_random(NfcVData* data) { ReturnCode slix_l_unlock(NfcVData* data, uint32_t password_id) { furi_assert(rand); - + uint16_t received = 0; uint8_t rxBuf[32]; - uint8_t cmd_set_pass[] = { - password_id, - data->sub_data.slix_l.rand[1], - data->sub_data.slix_l.rand[0], - data->sub_data.slix_l.rand[1], - data->sub_data.slix_l.rand[0] - }; - uint8_t *password = NULL; + uint8_t cmd_set_pass[] = { + password_id, + data->sub_data.slix_l.rand[1], + data->sub_data.slix_l.rand[0], + data->sub_data.slix_l.rand[1], + data->sub_data.slix_l.rand[0]}; + uint8_t* password = NULL; switch(password_id) { - case 4: - password = data->sub_data.slix_l.key_privacy; - break; - case 8: - password = data->sub_data.slix_l.key_destroy; - break; - case 10: - password = data->sub_data.slix_l.key_eas; - break; - default: - break; + case 4: + password = data->sub_data.slix_l.key_privacy; + break; + case 8: + password = data->sub_data.slix_l.key_destroy; + break; + case 10: + password = data->sub_data.slix_l.key_eas; + break; + default: + break; } if(!password) { @@ -111,14 +100,14 @@ ReturnCode slix_l_unlock(NfcVData* data, uint32_t password_id) { } ReturnCode ret = rfalNfcvPollerTransceiveReq( - ISO15693_CMD_NXP_SET_PASSWORD, - RFAL_NFCV_REQ_FLAG_DATA_RATE, - ISO15693_MANUFACTURER_NXP, - NULL, + ISO15693_CMD_NXP_SET_PASSWORD, + RFAL_NFCV_REQ_FLAG_DATA_RATE, + ISO15693_MANUFACTURER_NXP, + NULL, cmd_set_pass, - sizeof(cmd_set_pass), - rxBuf, - sizeof(rxBuf), + sizeof(cmd_set_pass), + rxBuf, + sizeof(rxBuf), &received); return ret; diff --git a/lib/nfc/protocols/slix.h b/lib/nfc/protocols/slix.h index b95a27abd..70dc35bac 100644 --- a/lib/nfc/protocols/slix.h +++ b/lib/nfc/protocols/slix.h @@ -5,10 +5,9 @@ #include "nfc_util.h" #include -#define ISO15693_CMD_NXP_GET_RANDOM_NUMBER 0xB2 -#define ISO15693_CMD_NXP_SET_PASSWORD 0xB3 -#define ISO15693_MANUFACTURER_NXP 0x04 - +#define ISO15693_CMD_NXP_GET_RANDOM_NUMBER 0xB2 +#define ISO15693_CMD_NXP_SET_PASSWORD 0xB3 +#define ISO15693_MANUFACTURER_NXP 0x04 bool slix_check_card_type(FuriHalNfcDevData* nfc_data); bool slix2_check_card_type(FuriHalNfcDevData* nfc_data); @@ -16,4 +15,3 @@ bool slix_s_check_card_type(FuriHalNfcDevData* nfc_data); bool slix_l_check_card_type(FuriHalNfcDevData* nfc_data); ReturnCode slix_l_get_random(NfcVData* data); ReturnCode slix_l_unlock(NfcVData* data, uint32_t password_id); - diff --git a/lib/pulse_reader/pulse_reader.c b/lib/pulse_reader/pulse_reader.c index f829bf831..18ebfefe8 100644 --- a/lib/pulse_reader/pulse_reader.c +++ b/lib/pulse_reader/pulse_reader.c @@ -5,7 +5,6 @@ #include "pulse_reader.h" - #define GPIO_PIN_MAP(pin, prefix) \ (((pin) == (LL_GPIO_PIN_0)) ? prefix##0 : \ ((pin) == (LL_GPIO_PIN_1)) ? prefix##1 : \ @@ -26,10 +25,7 @@ #define GET_DMAMUX_EXTI_LINE(pin) GPIO_PIN_MAP(pin, LL_DMAMUX_REQ_GEN_EXTI_LINE) - - PulseReader* pulse_reader_alloc(const GpioPin* gpio, uint32_t size) { - PulseReader* signal = malloc(sizeof(PulseReader)); signal->timer_buffer = malloc(size * sizeof(uint32_t)); signal->gpio_buffer = malloc(size * sizeof(uint32_t)); @@ -43,23 +39,25 @@ PulseReader* pulse_reader_alloc(const GpioPin* gpio, uint32_t size) { pulse_reader_set_bittime(signal, 1); signal->dma_config_timer.Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY; - signal->dma_config_timer.PeriphOrM2MSrcAddress = (uint32_t) &(TIM2->CNT); + signal->dma_config_timer.PeriphOrM2MSrcAddress = (uint32_t) & (TIM2->CNT); signal->dma_config_timer.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT; signal->dma_config_timer.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD; - signal->dma_config_timer.MemoryOrM2MDstAddress = (uint32_t) signal->timer_buffer; + signal->dma_config_timer.MemoryOrM2MDstAddress = (uint32_t)signal->timer_buffer; signal->dma_config_timer.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT; signal->dma_config_timer.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_WORD; signal->dma_config_timer.Mode = LL_DMA_MODE_CIRCULAR; - signal->dma_config_timer.PeriphRequest = LL_DMAMUX_REQ_GENERATOR0; /* executes LL_DMA_SetPeriphRequest */ + signal->dma_config_timer.PeriphRequest = + LL_DMAMUX_REQ_GENERATOR0; /* executes LL_DMA_SetPeriphRequest */ signal->dma_config_timer.Priority = LL_DMA_PRIORITY_VERYHIGH; - + signal->dma_config_gpio.Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY; signal->dma_config_gpio.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT; signal->dma_config_gpio.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD; signal->dma_config_gpio.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT; signal->dma_config_gpio.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_WORD; signal->dma_config_gpio.Mode = LL_DMA_MODE_CIRCULAR; - signal->dma_config_gpio.PeriphRequest = LL_DMAMUX_REQ_GENERATOR0; /* executes LL_DMA_SetPeriphRequest */ + signal->dma_config_gpio.PeriphRequest = + LL_DMAMUX_REQ_GENERATOR0; /* executes LL_DMA_SetPeriphRequest */ signal->dma_config_gpio.Priority = LL_DMA_PRIORITY_VERYHIGH; return signal; @@ -67,22 +65,22 @@ PulseReader* pulse_reader_alloc(const GpioPin* gpio, uint32_t size) { void pulse_reader_set_timebase(PulseReader* signal, PulseReaderUnit unit) { switch(unit) { - case PulseReaderUnit64MHz: - signal->unit_multiplier = 1; - signal->unit_divider = 1; - break; - case PulseReaderUnitPicosecond: - signal->unit_multiplier = 15625; - signal->unit_divider = 1; - break; - case PulseReaderUnitNanosecond: - signal->unit_multiplier = 15625; - signal->unit_divider = 1000; - break; - case PulseReaderUnitMicrosecond: - signal->unit_multiplier = 15625; - signal->unit_divider = 1000000; - break; + case PulseReaderUnit64MHz: + signal->unit_multiplier = 1; + signal->unit_divider = 1; + break; + case PulseReaderUnitPicosecond: + signal->unit_multiplier = 15625; + signal->unit_divider = 1; + break; + case PulseReaderUnitNanosecond: + signal->unit_multiplier = 15625; + signal->unit_divider = 1000; + break; + case PulseReaderUnitMicrosecond: + signal->unit_multiplier = 15625; + signal->unit_divider = 1000000; + break; } } @@ -104,7 +102,7 @@ uint32_t pulse_reader_samples(PulseReader* signal) { void pulse_reader_stop(PulseReader* signal) { LL_DMA_DisableChannel(DMA1, signal->dma_channel); - LL_DMA_DisableChannel(DMA1, signal->dma_channel+1); + LL_DMA_DisableChannel(DMA1, signal->dma_channel + 1); LL_DMAMUX_DisableRequestGen(NULL, LL_DMAMUX_REQ_GEN_0); LL_TIM_DisableCounter(TIM2); } @@ -113,10 +111,10 @@ void pulse_reader_start(PulseReader* signal) { /* configure DMA to read from a timer peripheral */ signal->dma_config_timer.NbData = signal->size; - signal->dma_config_gpio.PeriphOrM2MSrcAddress = (uint32_t) &(signal->gpio->port->IDR); - signal->dma_config_gpio.MemoryOrM2MDstAddress = (uint32_t) signal->gpio_buffer; + signal->dma_config_gpio.PeriphOrM2MSrcAddress = (uint32_t) & (signal->gpio->port->IDR); + signal->dma_config_gpio.MemoryOrM2MDstAddress = (uint32_t)signal->gpio_buffer; signal->dma_config_gpio.NbData = signal->size; - + /* start counter */ LL_TIM_SetCounterMode(TIM2, LL_TIM_COUNTERMODE_UP); LL_TIM_SetClockDivision(TIM2, LL_TIM_CLOCKDIVISION_DIV1); @@ -126,14 +124,16 @@ void pulse_reader_start(PulseReader* signal) { LL_TIM_EnableCounter(TIM2); /* generator 0 gets fed by EXTI_LINEn */ - LL_DMAMUX_SetRequestSignalID(NULL, LL_DMAMUX_REQ_GEN_0, GET_DMAMUX_EXTI_LINE(signal->gpio->pin)); + LL_DMAMUX_SetRequestSignalID( + NULL, LL_DMAMUX_REQ_GEN_0, GET_DMAMUX_EXTI_LINE(signal->gpio->pin)); /* trigger on rising edge of the interrupt */ LL_DMAMUX_SetRequestGenPolarity(NULL, LL_DMAMUX_REQ_GEN_0, LL_DMAMUX_REQ_GEN_POL_RISING); /* now enable request generation again */ LL_DMAMUX_EnableRequestGen(NULL, LL_DMAMUX_REQ_GEN_0); /* we need the EXTI to be configured as interrupt generating line, but no ISR registered */ - furi_hal_gpio_init_ex(signal->gpio, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedVeryHigh, GpioAltFnUnused); + furi_hal_gpio_init_ex( + signal->gpio, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedVeryHigh, GpioAltFnUnused); /* capture current timer */ signal->pos = 0; @@ -143,23 +143,22 @@ void pulse_reader_start(PulseReader* signal) { /* now set up DMA with these settings */ LL_DMA_Init(DMA1, signal->dma_channel, &signal->dma_config_timer); - LL_DMA_Init(DMA1, signal->dma_channel+1, &signal->dma_config_gpio); + LL_DMA_Init(DMA1, signal->dma_channel + 1, &signal->dma_config_gpio); LL_DMA_EnableChannel(DMA1, signal->dma_channel); - LL_DMA_EnableChannel(DMA1, signal->dma_channel+1); + LL_DMA_EnableChannel(DMA1, signal->dma_channel + 1); } uint32_t pulse_reader_receive(PulseReader* signal, int timeout_us) { - uint32_t start_time = DWT->CYCCNT; - uint32_t timeout_ticks = timeout_us * (F_TIM2/1000000); + uint32_t timeout_ticks = timeout_us * (F_TIM2 / 1000000); do { /* get the DMA's next write position by reading "remaining length" register */ - uint32_t dma_pos = signal->size - (uint32_t)LL_DMA_GetDataLength(DMA1, signal->dma_channel); + uint32_t dma_pos = + signal->size - (uint32_t)LL_DMA_GetDataLength(DMA1, signal->dma_channel); /* the DMA has advanced in the ringbuffer */ if(dma_pos != signal->pos) { - uint32_t delta = signal->timer_buffer[signal->pos] - signal->timer_value; uint32_t last_gpio_value = signal->gpio_value; @@ -174,12 +173,13 @@ uint32_t pulse_reader_receive(PulseReader* signal, int timeout_us) { signal->pos++; signal->pos %= signal->size; - + uint32_t delta_unit = 0; /* probably larger values, so choose a wider data type */ if(signal->unit_divider > 1) { - delta_unit = (uint32_t)((uint64_t)delta * (uint64_t)signal->unit_multiplier / signal->unit_divider); + delta_unit = + (uint32_t)((uint64_t)delta * (uint64_t)signal->unit_multiplier / signal->unit_divider); } else { delta_unit = delta * signal->unit_multiplier; } diff --git a/lib/pulse_reader/pulse_reader.h b/lib/pulse_reader/pulse_reader.h index 886c9d9e7..fb65f62bc 100644 --- a/lib/pulse_reader/pulse_reader.h +++ b/lib/pulse_reader/pulse_reader.h @@ -14,9 +14,9 @@ extern "C" { #endif -#define PULSE_READER_NO_EDGE 0xFFFFFFFFUL -#define PULSE_READER_LOST_EDGE 0xFFFFFFFEUL -#define F_TIM2 64000000UL +#define PULSE_READER_NO_EDGE 0xFFFFFFFFUL +#define PULSE_READER_LOST_EDGE 0xFFFFFFFEUL +#define F_TIM2 64000000UL /** * unit of the edge durations to return @@ -28,7 +28,6 @@ typedef enum { PulseReaderUnitMicrosecond, } PulseReaderUnit; - typedef struct { bool start_level; uint32_t* timer_buffer; @@ -47,7 +46,6 @@ typedef struct { LL_DMA_InitTypeDef dma_config_gpio; } PulseReader; - /** Allocate a PulseReader object * * Allocates memory for a ringbuffer and initalizes the object @@ -57,7 +55,6 @@ typedef struct { */ PulseReader* pulse_reader_alloc(const GpioPin* gpio, uint32_t size); - /** Free a PulseReader object * * Frees all memory of the given object @@ -66,7 +63,6 @@ PulseReader* pulse_reader_alloc(const GpioPin* gpio, uint32_t size); */ void pulse_reader_free(PulseReader* signal); - /** Start signal capturing * * Initializes DMA1, TIM2 and DMAMUX_REQ_GEN_0 to automatically capture timer values @@ -83,7 +79,6 @@ void pulse_reader_start(PulseReader* signal); */ void pulse_reader_stop(PulseReader* signal); - /** Recevie a sample from ringbuffer * * Waits for the specified time until a new edge gets detected. @@ -98,7 +93,6 @@ void pulse_reader_stop(PulseReader* signal); */ uint32_t pulse_reader_receive(PulseReader* signal, int timeout_us); - /** Get available samples * * Get the number of available samples in the ringbuffer @@ -109,7 +103,6 @@ uint32_t pulse_reader_receive(PulseReader* signal, int timeout_us); */ uint32_t pulse_reader_samples(PulseReader* signal); - /** Set timebase * * Set the timebase to be used when returning pulse duration. @@ -119,8 +112,6 @@ uint32_t pulse_reader_samples(PulseReader* signal); */ void pulse_reader_set_timebase(PulseReader* signal, PulseReaderUnit unit); - - /** Set bit time * * Set the number of timebase units per bit. @@ -134,7 +125,6 @@ void pulse_reader_set_timebase(PulseReader* signal, PulseReaderUnit unit); */ void pulse_reader_set_bittime(PulseReader* signal, uint32_t bit_time); - #ifdef __cplusplus } #endif