nfc_apdu_runner.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * @Author: SpenserCai
  3. * @Date: 2025-02-28 18:02:04
  4. * @version:
  5. * @LastEditors: SpenserCai
  6. * @LastEditTime: 2025-03-13 12:22:41
  7. * @Description: file content
  8. */
  9. #pragma once
  10. #include <furi.h>
  11. #include <furi_hal.h>
  12. #include <gui/gui.h>
  13. #include <gui/view_dispatcher.h>
  14. #include <gui/scene_manager.h>
  15. #include <gui/modules/widget.h>
  16. #include <gui/modules/submenu.h>
  17. #include <gui/modules/text_input.h>
  18. #include <gui/modules/popup.h>
  19. #include <gui/modules/dialog_ex.h>
  20. #include <gui/modules/text_box.h>
  21. #include <gui/modules/button_menu.h>
  22. #include <storage/storage.h>
  23. #include <dialogs/dialogs.h>
  24. #include <nfc/nfc.h>
  25. #include <nfc/nfc_device.h>
  26. #include <nfc/nfc_poller.h>
  27. #include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
  28. #include <nfc/protocols/iso14443_3b/iso14443_3b_poller.h>
  29. #include <nfc/protocols/iso14443_4a/iso14443_4a_poller.h>
  30. #include <nfc/protocols/iso14443_4b/iso14443_4b_poller.h>
  31. #include <bit_lib/bit_lib.h>
  32. #include <toolbox/hex.h>
  33. #include <toolbox/path.h>
  34. #define APP_DIRECTORY_PATH "/ext/apps_data/nfc_apdu_runner"
  35. #define FILE_EXTENSION ".apduscr"
  36. #define RESPONSE_EXTENSION ".apdures"
  37. #define MAX_APDU_COMMANDS 50
  38. #define MAX_APDU_LENGTH 256
  39. #define MAX_LOG_ENTRIES 100
  40. #define MAX_FILENAME_LEN 64
  41. // 自定义事件枚举
  42. typedef enum {
  43. NfcApduRunnerCustomEventPopupClosed = 0,
  44. NfcApduRunnerCustomEventViewExit,
  45. } NfcApduRunnerCustomEvent;
  46. // 卡类型枚举
  47. typedef enum {
  48. CardTypeIso14443_3a,
  49. CardTypeIso14443_3b,
  50. CardTypeIso14443_4a,
  51. CardTypeIso14443_4b,
  52. CardTypeUnknown,
  53. } CardType;
  54. // 应用程序视图枚举
  55. typedef enum {
  56. NfcApduRunnerViewSubmenu,
  57. NfcApduRunnerViewWidget,
  58. NfcApduRunnerViewTextBox,
  59. NfcApduRunnerViewPopup,
  60. NfcApduRunnerViewTextInput,
  61. } NfcApduRunnerView;
  62. // 日志条目结构
  63. typedef struct {
  64. char* message;
  65. bool is_error;
  66. } LogEntry;
  67. // APDU脚本文件结构
  68. typedef struct {
  69. CardType card_type;
  70. char* commands[MAX_APDU_COMMANDS];
  71. uint32_t command_count;
  72. } NfcApduScript;
  73. // APDU响应结构
  74. typedef struct {
  75. char* command;
  76. uint8_t* response;
  77. uint16_t response_length;
  78. } NfcApduResponse;
  79. // NFC Worker状态枚举
  80. typedef enum {
  81. NfcWorkerStateReady, // 准备就绪
  82. NfcWorkerStateDetecting, // 正在检测卡片
  83. NfcWorkerStateRunning, // 正在执行APDU命令
  84. NfcWorkerStateStop // 停止
  85. } NfcWorkerState;
  86. // NFC Worker事件枚举
  87. typedef enum {
  88. NfcWorkerEventSuccess, // 操作成功
  89. NfcWorkerEventFail, // 操作失败
  90. NfcWorkerEventCardDetected, // 检测到卡片
  91. NfcWorkerEventCardLost, // 卡片丢失
  92. NfcWorkerEventAborted, // 操作被中止
  93. NfcWorkerEventStop, // 停止事件
  94. } NfcWorkerEvent;
  95. // NFC Worker回调函数类型
  96. typedef void (*NfcWorkerCallback)(NfcWorkerEvent event, void* context);
  97. // NFC Worker结构体
  98. typedef struct NfcWorker NfcWorker;
  99. // 应用程序状态结构
  100. typedef struct {
  101. // GUI
  102. Gui* gui;
  103. ViewDispatcher* view_dispatcher;
  104. SceneManager* scene_manager;
  105. Submenu* submenu;
  106. DialogsApp* dialogs;
  107. Widget* widget;
  108. TextBox* text_box;
  109. FuriString* text_box_store;
  110. Popup* popup;
  111. ButtonMenu* button_menu;
  112. TextInput* text_input;
  113. // NFC
  114. Nfc* nfc;
  115. NfcWorker* worker; // 添加NFC Worker
  116. // 文件
  117. Storage* storage;
  118. FuriString* file_path;
  119. NfcApduScript* script;
  120. NfcApduResponse* responses;
  121. uint32_t response_count;
  122. char file_name_buf[MAX_FILENAME_LEN + 1];
  123. // 日志
  124. LogEntry* log_entries;
  125. uint32_t log_count;
  126. // 状态
  127. bool running;
  128. bool card_lost;
  129. } NfcApduRunner;
  130. // 释放APDU脚本资源
  131. void nfc_apdu_script_free(NfcApduScript* script);
  132. // 释放APDU响应资源
  133. void nfc_apdu_responses_free(NfcApduResponse* responses, uint32_t count);
  134. // 解析APDU脚本文件
  135. NfcApduScript* nfc_apdu_script_parse(Storage* storage, const char* file_path);
  136. // 保存APDU响应结果
  137. bool nfc_apdu_save_responses(
  138. Storage* storage,
  139. const char* file_path,
  140. NfcApduResponse* responses,
  141. uint32_t response_count,
  142. const char* custom_save_path);
  143. // 添加日志
  144. void add_log_entry(NfcApduRunner* app, const char* message, bool is_error);
  145. // 释放日志资源
  146. void free_logs(NfcApduRunner* app);
  147. // 应用程序入口点
  148. int32_t nfc_apdu_runner_app(void* p);
  149. // NFC Worker函数声明
  150. NfcWorker* nfc_worker_alloc(Nfc* nfc);
  151. void nfc_worker_free(NfcWorker* worker);
  152. void nfc_worker_start(
  153. NfcWorker* worker,
  154. NfcWorkerState state,
  155. NfcApduScript* script,
  156. NfcWorkerCallback callback,
  157. void* context);
  158. void nfc_worker_stop(NfcWorker* worker);
  159. bool nfc_worker_is_running(NfcWorker* worker);
  160. void nfc_worker_get_responses(
  161. NfcWorker* worker,
  162. NfcApduResponse** out_responses,
  163. uint32_t* out_response_count);
  164. const char* nfc_worker_get_error_message(NfcWorker* worker);