gen4_poller.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #include "gen4_poller_i.h"
  2. #include "protocols/gen4/gen4_poller.h"
  3. #include <nfc/protocols/iso14443_3a/iso14443_3a.h>
  4. #include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
  5. #include <nfc/nfc_poller.h>
  6. #include <bit_lib.h>
  7. #define GEN4_POLLER_THREAD_FLAG_DETECTED (1U << 0)
  8. typedef NfcCommand (*Gen4PollerStateHandler)(Gen4Poller* instance);
  9. typedef struct {
  10. NfcPoller* poller;
  11. uint32_t password;
  12. BitBuffer* tx_buffer;
  13. BitBuffer* rx_buffer;
  14. FuriThreadId thread_id;
  15. Gen4PollerError error;
  16. } Gen4PollerDetectContext;
  17. static const uint8_t gen4_poller_default_config[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
  18. 0x00, 0x09, 0x78, 0x00, 0x91, 0x02, 0xDA,
  19. 0xBC, 0x19, 0x10, 0x10, 0x11, 0x12, 0x13,
  20. 0x14, 0x15, 0x16, 0x04, 0x00, 0x08, 0x00};
  21. static const uint8_t gen4_poller_default_block_0[GEN4_POLLER_BLOCK_SIZE] =
  22. {0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  23. static const uint8_t gen4_poller_default_empty_block[GEN4_POLLER_BLOCK_SIZE] = {0};
  24. static const uint8_t gen4_poller_default_sector_trailer_block[GEN4_POLLER_BLOCK_SIZE] =
  25. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  26. static bool gen4_poller_is_sector_trailer(uint8_t block_num) {
  27. uint8_t sec_tr_block_num = 0;
  28. if(block_num < 128) {
  29. sec_tr_block_num = block_num | 0x03;
  30. } else {
  31. sec_tr_block_num = block_num | 0x0f;
  32. }
  33. return block_num == sec_tr_block_num;
  34. }
  35. Gen4Poller* gen4_poller_alloc(Nfc* nfc) {
  36. furi_assert(nfc);
  37. Gen4Poller* instance = malloc(sizeof(Gen4Poller));
  38. instance->poller = nfc_poller_alloc(nfc, NfcProtocolIso14443_3a);
  39. instance->gen4_event.data = &instance->gen4_event_data;
  40. instance->tx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  41. instance->rx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  42. return instance;
  43. }
  44. void gen4_poller_free(Gen4Poller* instance) {
  45. furi_assert(instance);
  46. nfc_poller_free(instance->poller);
  47. bit_buffer_free(instance->tx_buffer);
  48. bit_buffer_free(instance->rx_buffer);
  49. free(instance);
  50. }
  51. void gen4_poller_set_password(Gen4Poller* instance, uint32_t password) {
  52. furi_assert(instance);
  53. instance->password = password;
  54. }
  55. NfcCommand gen4_poller_detect_callback(NfcGenericEvent event, void* context) {
  56. furi_assert(context);
  57. furi_assert(event.protocol == NfcProtocolIso14443_3a);
  58. furi_assert(event.instance);
  59. furi_assert(event.event_data);
  60. NfcCommand command = NfcCommandStop;
  61. Gen4PollerDetectContext* gen4_poller_detect_ctx = context;
  62. Iso14443_3aPoller* iso3_poller = event.instance;
  63. Iso14443_3aPollerEvent* iso3_event = event.event_data;
  64. gen4_poller_detect_ctx->error = Gen4PollerErrorTimeout;
  65. if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
  66. do {
  67. bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_PREFIX);
  68. uint8_t pwd_arr[4] = {};
  69. bit_lib_num_to_bytes_be(gen4_poller_detect_ctx->password, COUNT_OF(pwd_arr), pwd_arr);
  70. bit_buffer_append_bytes(gen4_poller_detect_ctx->tx_buffer, pwd_arr, COUNT_OF(pwd_arr));
  71. bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_GET_CFG);
  72. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  73. iso3_poller,
  74. gen4_poller_detect_ctx->tx_buffer,
  75. gen4_poller_detect_ctx->rx_buffer,
  76. GEN4_POLLER_MAX_FWT);
  77. if(error != Iso14443_3aErrorNone) {
  78. gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
  79. break;
  80. }
  81. size_t rx_bytes = bit_buffer_get_size_bytes(gen4_poller_detect_ctx->rx_buffer);
  82. if((rx_bytes != 30) && (rx_bytes != 32)) {
  83. gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
  84. break;
  85. }
  86. gen4_poller_detect_ctx->error = Gen4PollerErrorNone;
  87. } while(false);
  88. } else if(iso3_event->type == Iso14443_3aPollerEventTypeError) {
  89. gen4_poller_detect_ctx->error = Gen4PollerErrorTimeout;
  90. }
  91. furi_thread_flags_set(gen4_poller_detect_ctx->thread_id, GEN4_POLLER_THREAD_FLAG_DETECTED);
  92. return command;
  93. }
  94. Gen4PollerError gen4_poller_detect(Nfc* nfc, uint32_t password) {
  95. furi_assert(nfc);
  96. Gen4PollerDetectContext gen4_poller_detect_ctx = {};
  97. gen4_poller_detect_ctx.poller = nfc_poller_alloc(nfc, NfcProtocolIso14443_3a);
  98. gen4_poller_detect_ctx.password = password;
  99. gen4_poller_detect_ctx.tx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  100. gen4_poller_detect_ctx.rx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  101. gen4_poller_detect_ctx.thread_id = furi_thread_get_current_id();
  102. gen4_poller_detect_ctx.error = Gen4PollerErrorNone;
  103. nfc_poller_start(
  104. gen4_poller_detect_ctx.poller, gen4_poller_detect_callback, &gen4_poller_detect_ctx);
  105. uint32_t flags =
  106. furi_thread_flags_wait(GEN4_POLLER_THREAD_FLAG_DETECTED, FuriFlagWaitAny, FuriWaitForever);
  107. if(flags & GEN4_POLLER_THREAD_FLAG_DETECTED) {
  108. furi_thread_flags_clear(GEN4_POLLER_THREAD_FLAG_DETECTED);
  109. }
  110. nfc_poller_stop(gen4_poller_detect_ctx.poller);
  111. nfc_poller_free(gen4_poller_detect_ctx.poller);
  112. bit_buffer_free(gen4_poller_detect_ctx.tx_buffer);
  113. bit_buffer_free(gen4_poller_detect_ctx.rx_buffer);
  114. return gen4_poller_detect_ctx.error;
  115. }
  116. NfcCommand gen4_poller_idle_handler(Gen4Poller* instance) {
  117. NfcCommand command = NfcCommandContinue;
  118. instance->current_block = 0;
  119. memset(instance->config, 0, sizeof(instance->config));
  120. instance->gen4_event.type = Gen4PollerEventTypeCardDetected;
  121. command = instance->callback(instance->gen4_event, instance->context);
  122. instance->state = Gen4PollerStateRequestMode;
  123. return command;
  124. }
  125. NfcCommand gen4_poller_request_mode_handler(Gen4Poller* instance) {
  126. NfcCommand command = NfcCommandContinue;
  127. instance->gen4_event.type = Gen4PollerEventTypeRequestMode;
  128. command = instance->callback(instance->gen4_event, instance->context);
  129. if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeWipe) {
  130. instance->state = Gen4PollerStateWipe;
  131. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeWrite) {
  132. instance->state = Gen4PollerStateRequestWriteData;
  133. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetPassword) {
  134. instance->state = Gen4PollerStateChangePassword;
  135. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetDefaultCfg) {
  136. instance->state = Gen4PollerStateSetDefaultConfig;
  137. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeGetCfg) {
  138. instance->state = Gen4PollerStateGetCurrentConfig;
  139. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeGetRevision) {
  140. instance->state = Gen4PollerStateGetRevision;
  141. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetShadowMode) {
  142. instance->state = Gen4PollerStateSetShadowMode;
  143. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetDirectWriteBlock0Mode) {
  144. instance->state = Gen4PollerStateSetDirectWriteBlock0;
  145. } else {
  146. instance->state = Gen4PollerStateFail;
  147. }
  148. return command;
  149. }
  150. NfcCommand gen4_poller_wipe_handler(Gen4Poller* instance) {
  151. NfcCommand command = NfcCommandContinue;
  152. do {
  153. Gen4PollerError error = Gen4PollerErrorNone;
  154. if(instance->current_block == 0) {
  155. error = gen4_poller_set_config(
  156. instance,
  157. instance->password,
  158. gen4_poller_default_config,
  159. sizeof(gen4_poller_default_config),
  160. false);
  161. if(error != Gen4PollerErrorNone) {
  162. FURI_LOG_D(TAG, "Failed to set default config: %d", error);
  163. instance->state = Gen4PollerStateFail;
  164. break;
  165. }
  166. instance->password = 0;
  167. error = gen4_poller_write_block(
  168. instance, instance->password, instance->current_block, gen4_poller_default_block_0);
  169. if(error != Gen4PollerErrorNone) {
  170. FURI_LOG_D(TAG, "Failed to write 0 block: %d", error);
  171. instance->state = Gen4PollerStateFail;
  172. break;
  173. }
  174. } else if(instance->current_block < GEN4_POLLER_BLOCKS_TOTAL) {
  175. const uint8_t* block = gen4_poller_is_sector_trailer(instance->current_block) ?
  176. gen4_poller_default_sector_trailer_block :
  177. gen4_poller_default_empty_block;
  178. error = gen4_poller_write_block(
  179. instance, instance->password, instance->current_block, block);
  180. if(error != Gen4PollerErrorNone) {
  181. FURI_LOG_D(TAG, "Failed to write %d block: %d", instance->current_block, error);
  182. instance->state = Gen4PollerStateFail;
  183. break;
  184. }
  185. } else {
  186. instance->state = Gen4PollerStateSuccess;
  187. break;
  188. }
  189. instance->current_block++;
  190. } while(false);
  191. return command;
  192. }
  193. NfcCommand gen4_poller_request_write_data_handler(Gen4Poller* instance) {
  194. NfcCommand command = NfcCommandContinue;
  195. instance->gen4_event.type = Gen4PollerEventTypeRequestDataToWrite;
  196. command = instance->callback(instance->gen4_event, instance->context);
  197. instance->protocol = instance->gen4_event_data.request_data.protocol;
  198. instance->data = instance->gen4_event_data.request_data.data;
  199. if((instance->protocol == NfcProtocolMfClassic) ||
  200. (instance->protocol == NfcProtocolMfUltralight)) {
  201. instance->state = Gen4PollerStateWrite;
  202. } else {
  203. FURI_LOG_E(TAG, "Unsupported protocol");
  204. instance->state = Gen4PollerStateFail;
  205. }
  206. return command;
  207. }
  208. static NfcCommand gen4_poller_write_mf_classic(Gen4Poller* instance) {
  209. NfcCommand command = NfcCommandContinue;
  210. do {
  211. const MfClassicData* mfc_data = instance->data;
  212. const Iso14443_3aData* iso3_data = mfc_data->iso14443_3a_data;
  213. if(instance->current_block == 0) {
  214. instance->config[0] = 0x00;
  215. instance->total_blocks = mf_classic_get_total_block_num(mfc_data->type);
  216. if(iso3_data->uid_len == 4) {
  217. instance->config[1] = Gen4PollerUIDLengthSingle;
  218. } else if(iso3_data->uid_len == 7) {
  219. instance->config[1] = Gen4PollerUIDLengthDouble;
  220. } else {
  221. FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
  222. instance->state = Gen4PollerStateFail;
  223. break;
  224. }
  225. instance->config[6] = Gen4PollerShadowModeDisabled;
  226. instance->config[24] = iso3_data->atqa[0];
  227. instance->config[25] = iso3_data->atqa[1];
  228. instance->config[26] = iso3_data->sak;
  229. instance->config[27] = 0x00;
  230. instance->config[28] = instance->total_blocks - 1;
  231. instance->config[29] = Gen4PollerDirectWriteBlock0ModeDisabled;
  232. Gen4PollerError error = gen4_poller_set_config(
  233. instance, instance->password, instance->config, sizeof(instance->config), false);
  234. if(error != Gen4PollerErrorNone) {
  235. FURI_LOG_D(TAG, "Failed to write config: %d", error);
  236. instance->state = Gen4PollerStateFail;
  237. break;
  238. }
  239. }
  240. if(instance->current_block < instance->total_blocks) {
  241. FURI_LOG_D(TAG, "Writing block %d", instance->current_block);
  242. Gen4PollerError error = gen4_poller_write_block(
  243. instance,
  244. instance->password,
  245. instance->current_block,
  246. mfc_data->block[instance->current_block].data);
  247. if(error != Gen4PollerErrorNone) {
  248. FURI_LOG_D(TAG, "Failed to write %d block: %d", instance->current_block, error);
  249. instance->state = Gen4PollerStateFail;
  250. break;
  251. }
  252. } else {
  253. instance->state = Gen4PollerStateSuccess;
  254. break;
  255. }
  256. instance->current_block++;
  257. } while(false);
  258. return command;
  259. }
  260. static NfcCommand gen4_poller_write_mf_ultralight(Gen4Poller* instance) {
  261. NfcCommand command = NfcCommandContinue;
  262. do {
  263. const MfUltralightData* mfu_data = instance->data;
  264. const Iso14443_3aData* iso3_data = mfu_data->iso14443_3a_data;
  265. if(instance->current_block == 0) {
  266. instance->total_blocks = 64;
  267. instance->config[0] = 0x01;
  268. switch(mfu_data->type) {
  269. case MfUltralightTypeNTAG203:
  270. case MfUltralightTypeNTAG213:
  271. case MfUltralightTypeNTAG215:
  272. case MfUltralightTypeNTAG216:
  273. case MfUltralightTypeNTAGI2C1K:
  274. case MfUltralightTypeNTAGI2C2K:
  275. case MfUltralightTypeNTAGI2CPlus1K:
  276. case MfUltralightTypeNTAGI2CPlus2K:
  277. instance->config[27] = Gen4PollerUltralightModeNTAG;
  278. instance->total_blocks = 64 * 2;
  279. break;
  280. case MfUltralightTypeUL11:
  281. case MfUltralightTypeUL21:
  282. // UL-C?
  283. // UL?
  284. default:
  285. instance->config[27] = Gen4PollerUltralightModeUL_EV1;
  286. break;
  287. }
  288. if(iso3_data->uid_len == 4) {
  289. instance->config[1] = Gen4PollerUIDLengthSingle;
  290. } else if(iso3_data->uid_len == 7) {
  291. instance->config[1] = Gen4PollerUIDLengthDouble;
  292. } else {
  293. FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
  294. instance->state = Gen4PollerStateFail;
  295. break;
  296. }
  297. instance->config[6] = Gen4PollerShadowModeHighSpeedDisabled;
  298. instance->config[24] = iso3_data->atqa[0];
  299. instance->config[25] = iso3_data->atqa[1];
  300. instance->config[26] = iso3_data->sak;
  301. instance->config[27] = 0x00;
  302. instance->config[28] = instance->total_blocks - 1;
  303. instance->config[29] = Gen4PollerDirectWriteBlock0ModeDisabled;
  304. Gen4PollerError error = gen4_poller_set_config(
  305. instance, instance->password, instance->config, sizeof(instance->config), false);
  306. if(error != Gen4PollerErrorNone) {
  307. FURI_LOG_D(TAG, "Failed to write config: %d", error);
  308. instance->state = Gen4PollerStateFail;
  309. break;
  310. }
  311. }
  312. if(instance->current_block < mfu_data->pages_read) {
  313. FURI_LOG_D(
  314. TAG, "Writing page %zu / %zu", instance->current_block, mfu_data->pages_read);
  315. Gen4PollerError error = gen4_poller_write_block(
  316. instance,
  317. instance->password,
  318. instance->current_block,
  319. mfu_data->page[instance->current_block].data);
  320. if(error != Gen4PollerErrorNone) {
  321. FURI_LOG_D(TAG, "Failed to write %d page: %d", instance->current_block, error);
  322. instance->state = Gen4PollerStateFail;
  323. break;
  324. }
  325. instance->current_block++;
  326. } else {
  327. uint8_t block[GEN4_POLLER_BLOCK_SIZE] = {};
  328. bool write_success = true;
  329. for(size_t i = 0; i < 8; i++) {
  330. memcpy(block, &mfu_data->signature.data[i * 4], 4); //-V1086
  331. Gen4PollerError error =
  332. gen4_poller_write_block(instance, instance->password, 0xF2 + i, block);
  333. if(error != Gen4PollerErrorNone) {
  334. write_success = false;
  335. break;
  336. }
  337. }
  338. if(!write_success) {
  339. FURI_LOG_E(TAG, "Failed to write Signature");
  340. instance->state = Gen4PollerStateFail;
  341. break;
  342. }
  343. block[0] = mfu_data->version.header;
  344. block[1] = mfu_data->version.vendor_id;
  345. block[2] = mfu_data->version.prod_type;
  346. block[3] = mfu_data->version.prod_subtype;
  347. Gen4PollerError error =
  348. gen4_poller_write_block(instance, instance->password, 0xFA, block);
  349. if(error != Gen4PollerErrorNone) {
  350. FURI_LOG_E(TAG, "Failed to write 1st part Version");
  351. instance->state = Gen4PollerStateFail;
  352. break;
  353. }
  354. block[0] = mfu_data->version.prod_ver_major;
  355. block[1] = mfu_data->version.prod_ver_minor;
  356. block[2] = mfu_data->version.storage_size;
  357. block[3] = mfu_data->version.protocol_type;
  358. error = gen4_poller_write_block(instance, instance->password, 0xFB, block);
  359. if(error != Gen4PollerErrorNone) {
  360. FURI_LOG_E(TAG, "Failed to write 2nd part Version");
  361. instance->state = Gen4PollerStateFail;
  362. break;
  363. }
  364. instance->state = Gen4PollerStateSuccess;
  365. }
  366. } while(false);
  367. return command;
  368. }
  369. NfcCommand gen4_poller_write_handler(Gen4Poller* instance) {
  370. NfcCommand command = NfcCommandContinue;
  371. memcpy(instance->config, gen4_poller_default_config, sizeof(gen4_poller_default_config));
  372. uint8_t password_arr[4] = {};
  373. bit_lib_num_to_bytes_be(instance->password, sizeof(password_arr), password_arr);
  374. memcpy(&instance->config[2], password_arr, sizeof(password_arr));
  375. memset(&instance->config[7], 0, 17);
  376. if(instance->protocol == NfcProtocolMfClassic) {
  377. command = gen4_poller_write_mf_classic(instance);
  378. } else if(instance->protocol == NfcProtocolMfUltralight) {
  379. command = gen4_poller_write_mf_ultralight(instance);
  380. } else {
  381. furi_crash("Unsupported protocol to write");
  382. }
  383. return command;
  384. }
  385. NfcCommand gen4_poller_change_password_handler(Gen4Poller* instance) {
  386. NfcCommand command = NfcCommandContinue;
  387. do {
  388. instance->gen4_event.type = Gen4PollerEventTypeRequestNewPassword;
  389. command = instance->callback(instance->gen4_event, instance->context);
  390. if(command != NfcCommandContinue) break;
  391. uint32_t new_password = instance->gen4_event_data.request_password.password;
  392. Gen4PollerError error =
  393. gen4_poller_change_password(instance, instance->password, new_password);
  394. if(error != Gen4PollerErrorNone) {
  395. FURI_LOG_E(TAG, "Failed to change password: %d", error);
  396. instance->state = Gen4PollerStateFail;
  397. break;
  398. }
  399. instance->password = new_password;
  400. instance->state = Gen4PollerStateSuccess;
  401. } while(false);
  402. return command;
  403. }
  404. NfcCommand gen4_poller_set_default_cfg_handler(Gen4Poller* instance) {
  405. NfcCommand command = NfcCommandContinue;
  406. do {
  407. Gen4PollerError error = gen4_poller_set_config(
  408. instance,
  409. instance->password,
  410. gen4_poller_default_config,
  411. sizeof(gen4_poller_default_config),
  412. false);
  413. if(error != Gen4PollerErrorNone) {
  414. FURI_LOG_E(TAG, "Failed to set default config: %d", error);
  415. instance->state = Gen4PollerStateFail;
  416. break;
  417. }
  418. instance->state = Gen4PollerStateSuccess;
  419. } while(false);
  420. return command;
  421. }
  422. NfcCommand gen4_poller_get_current_cfg_handler(Gen4Poller* instance) {
  423. NfcCommand command = NfcCommandContinue;
  424. do {
  425. uint8_t the_config[32] = {};
  426. Gen4PollerError error = gen4_poller_get_config(instance, instance->password, the_config);
  427. if(error != Gen4PollerErrorNone) {
  428. FURI_LOG_E(TAG, "Failed to get current config: %d", error);
  429. instance->state = Gen4PollerStateFail;
  430. break;
  431. }
  432. // Copy config data to event data buffer
  433. memcpy(instance->gen4_event_data.display_config, the_config, sizeof(the_config));
  434. instance->state = Gen4PollerStateSuccess;
  435. } while(false);
  436. return command;
  437. }
  438. NfcCommand gen4_poller_get_revision_handler(Gen4Poller* instance) {
  439. NfcCommand command = NfcCommandContinue;
  440. do {
  441. uint8_t the_revision[5] = {0};
  442. Gen4PollerError error =
  443. gen4_poller_get_revision(instance, instance->password, the_revision);
  444. if(error != Gen4PollerErrorNone) {
  445. FURI_LOG_E(TAG, "Failed to get revision: %d", error);
  446. instance->state = Gen4PollerStateFail;
  447. break;
  448. }
  449. // Copy revision data to event data buffer
  450. memcpy(instance->gen4_event_data.revision_data, the_revision, sizeof(the_revision));
  451. instance->state = Gen4PollerStateSuccess;
  452. } while(false);
  453. return command;
  454. }
  455. NfcCommand gen4_poller_set_shadow_mode_handler(Gen4Poller* instance) {
  456. NfcCommand command = NfcCommandContinue;
  457. do {
  458. Gen4PollerError error =
  459. gen4_poller_set_shadow_mode(instance, instance->password, instance->shadow_mode);
  460. if(error != Gen4PollerErrorNone) {
  461. FURI_LOG_E(TAG, "Failed to set shadow mode: %d", error);
  462. instance->state = Gen4PollerStateFail;
  463. break;
  464. }
  465. instance->state = Gen4PollerStateSuccess;
  466. } while(false);
  467. return command;
  468. }
  469. NfcCommand gen4_poller_set_direct_write_block_0_mode_handler(Gen4Poller* instance) {
  470. NfcCommand command = NfcCommandContinue;
  471. do {
  472. Gen4PollerError error = gen4_poller_set_direct_write_block_0_mode(
  473. instance, instance->password, instance->direct_write_block_0_mode);
  474. if(error != Gen4PollerErrorNone) {
  475. FURI_LOG_E(TAG, "Failed to set direct write to block 0 mode: %d", error);
  476. instance->state = Gen4PollerStateFail;
  477. break;
  478. }
  479. instance->state = Gen4PollerStateSuccess;
  480. } while(false);
  481. return command;
  482. }
  483. NfcCommand gen4_poller_success_handler(Gen4Poller* instance) {
  484. NfcCommand command = NfcCommandContinue;
  485. instance->gen4_event.type = Gen4PollerEventTypeSuccess;
  486. command = instance->callback(instance->gen4_event, instance->context);
  487. if(command != NfcCommandStop) {
  488. furi_delay_ms(100);
  489. }
  490. return command;
  491. }
  492. NfcCommand gen4_poller_fail_handler(Gen4Poller* instance) {
  493. NfcCommand command = NfcCommandContinue;
  494. instance->gen4_event.type = Gen4PollerEventTypeFail;
  495. command = instance->callback(instance->gen4_event, instance->context);
  496. if(command != NfcCommandStop) {
  497. furi_delay_ms(100);
  498. }
  499. return command;
  500. }
  501. static const Gen4PollerStateHandler gen4_poller_state_handlers[Gen4PollerStateNum] = {
  502. [Gen4PollerStateIdle] = gen4_poller_idle_handler,
  503. [Gen4PollerStateRequestMode] = gen4_poller_request_mode_handler,
  504. [Gen4PollerStateRequestWriteData] = gen4_poller_request_write_data_handler,
  505. [Gen4PollerStateWrite] = gen4_poller_write_handler,
  506. [Gen4PollerStateWipe] = gen4_poller_wipe_handler,
  507. [Gen4PollerStateChangePassword] = gen4_poller_change_password_handler,
  508. [Gen4PollerStateSetDefaultConfig] = gen4_poller_set_default_cfg_handler,
  509. [Gen4PollerStateGetCurrentConfig] = gen4_poller_get_current_cfg_handler,
  510. [Gen4PollerStateGetRevision] = gen4_poller_get_revision_handler,
  511. [Gen4PollerStateSetShadowMode] = gen4_poller_set_shadow_mode_handler,
  512. [Gen4PollerStateSetDirectWriteBlock0] = gen4_poller_set_direct_write_block_0_mode_handler,
  513. [Gen4PollerStateSuccess] = gen4_poller_success_handler,
  514. [Gen4PollerStateFail] = gen4_poller_fail_handler,
  515. };
  516. static NfcCommand gen4_poller_callback(NfcGenericEvent event, void* context) {
  517. furi_assert(context);
  518. furi_assert(event.protocol == NfcProtocolIso14443_3a);
  519. furi_assert(event.event_data);
  520. furi_assert(event.instance);
  521. NfcCommand command = NfcCommandContinue;
  522. Gen4Poller* instance = context;
  523. instance->iso3_poller = event.instance;
  524. Iso14443_3aPollerEvent* iso3_event = event.event_data;
  525. if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
  526. command = gen4_poller_state_handlers[instance->state](instance);
  527. }
  528. return command;
  529. }
  530. void gen4_poller_start(Gen4Poller* instance, Gen4PollerCallback callback, void* context) {
  531. furi_assert(instance);
  532. furi_assert(callback);
  533. instance->callback = callback;
  534. instance->context = context;
  535. nfc_poller_start(instance->poller, gen4_poller_callback, instance);
  536. }
  537. void gen4_poller_stop(Gen4Poller* instance) {
  538. furi_assert(instance);
  539. nfc_poller_stop(instance->poller);
  540. }