gen4_poller.c 24 KB

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