gen4_poller.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetDirectWriteBlock0Mode) {
  147. instance->state = Gen4PollerStateSetDirectWriteBlock0;
  148. } else {
  149. instance->state = Gen4PollerStateFail;
  150. }
  151. return command;
  152. }
  153. NfcCommand gen4_poller_wipe_handler(Gen4Poller* instance) {
  154. NfcCommand command = NfcCommandContinue;
  155. do {
  156. Gen4PollerError error = Gen4PollerErrorNone;
  157. if(instance->current_block == 0) {
  158. error = gen4_poller_set_config(
  159. instance,
  160. instance->password,
  161. gen4_poller_default_config,
  162. sizeof(gen4_poller_default_config),
  163. false);
  164. if(error != Gen4PollerErrorNone) {
  165. FURI_LOG_D(TAG, "Failed to set default config: %d", error);
  166. instance->state = Gen4PollerStateFail;
  167. break;
  168. }
  169. instance->password = 0;
  170. error = gen4_poller_write_block(
  171. instance, instance->password, instance->current_block, gen4_poller_default_block_0);
  172. if(error != Gen4PollerErrorNone) {
  173. FURI_LOG_D(TAG, "Failed to write 0 block: %d", error);
  174. instance->state = Gen4PollerStateFail;
  175. break;
  176. }
  177. } else if(instance->current_block < GEN4_POLLER_BLOCKS_TOTAL) {
  178. const uint8_t* block = gen4_poller_is_sector_trailer(instance->current_block) ?
  179. gen4_poller_default_sector_trailer_block :
  180. gen4_poller_default_empty_block;
  181. error = gen4_poller_write_block(
  182. instance, instance->password, instance->current_block, block);
  183. if(error != Gen4PollerErrorNone) {
  184. FURI_LOG_D(TAG, "Failed to write %d block: %d", instance->current_block, error);
  185. instance->state = Gen4PollerStateFail;
  186. break;
  187. }
  188. } else {
  189. instance->state = Gen4PollerStateSuccess;
  190. break;
  191. }
  192. instance->current_block++;
  193. } while(false);
  194. return command;
  195. }
  196. NfcCommand gen4_poller_request_write_data_handler(Gen4Poller* instance) {
  197. NfcCommand command = NfcCommandContinue;
  198. instance->gen4_event.type = Gen4PollerEventTypeRequestDataToWrite;
  199. command = instance->callback(instance->gen4_event, instance->context);
  200. instance->protocol = instance->gen4_event_data.request_data.protocol;
  201. instance->data = instance->gen4_event_data.request_data.data;
  202. if((instance->protocol == NfcProtocolMfClassic) ||
  203. (instance->protocol == NfcProtocolMfUltralight)) {
  204. instance->state = Gen4PollerStateWrite;
  205. } else {
  206. FURI_LOG_E(TAG, "Unsupported protocol");
  207. instance->state = Gen4PollerStateFail;
  208. }
  209. return command;
  210. }
  211. static NfcCommand gen4_poller_write_mf_classic(Gen4Poller* instance) {
  212. NfcCommand command = NfcCommandContinue;
  213. do {
  214. const MfClassicData* mfc_data = instance->data;
  215. const Iso14443_3aData* iso3_data = mfc_data->iso14443_3a_data;
  216. if(instance->current_block == 0) {
  217. instance->config[0] = 0x00;
  218. instance->total_blocks = mf_classic_get_total_block_num(mfc_data->type);
  219. if(iso3_data->uid_len == 4) {
  220. instance->config[1] = Gen4PollerUIDLengthSingle;
  221. } else if(iso3_data->uid_len == 7) {
  222. instance->config[1] = Gen4PollerUIDLengthDouble;
  223. } else {
  224. FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
  225. instance->state = Gen4PollerStateFail;
  226. break;
  227. }
  228. instance->config[6] = Gen4PollerShadowModeDisabled;
  229. instance->config[24] = iso3_data->atqa[0];
  230. instance->config[25] = iso3_data->atqa[1];
  231. instance->config[26] = iso3_data->sak;
  232. instance->config[27] = 0x00;
  233. instance->config[28] = instance->total_blocks;
  234. instance->config[29] = Gen4PollerDirectWriteBlock0ModeDisabled;
  235. Gen4PollerError error = gen4_poller_set_config(
  236. instance, instance->password, instance->config, sizeof(instance->config), false);
  237. if(error != Gen4PollerErrorNone) {
  238. FURI_LOG_D(TAG, "Failed to write config: %d", error);
  239. instance->state = Gen4PollerStateFail;
  240. break;
  241. }
  242. }
  243. if(instance->current_block < instance->total_blocks) {
  244. FURI_LOG_D(TAG, "Writing block %d", instance->current_block);
  245. Gen4PollerError error = gen4_poller_write_block(
  246. instance,
  247. instance->password,
  248. instance->current_block,
  249. mfc_data->block[instance->current_block].data);
  250. if(error != Gen4PollerErrorNone) {
  251. FURI_LOG_D(TAG, "Failed to write %d block: %d", instance->current_block, error);
  252. instance->state = Gen4PollerStateFail;
  253. break;
  254. }
  255. } else {
  256. instance->state = Gen4PollerStateSuccess;
  257. break;
  258. }
  259. instance->current_block++;
  260. } while(false);
  261. return command;
  262. }
  263. static NfcCommand gen4_poller_write_mf_ultralight(Gen4Poller* instance) {
  264. NfcCommand command = NfcCommandContinue;
  265. do {
  266. const MfUltralightData* mfu_data = instance->data;
  267. const Iso14443_3aData* iso3_data = mfu_data->iso14443_3a_data;
  268. if(instance->current_block == 0) {
  269. instance->total_blocks = 64;
  270. instance->config[0] = 0x01;
  271. switch(mfu_data->type) {
  272. case MfUltralightTypeNTAG203:
  273. case MfUltralightTypeNTAG213:
  274. case MfUltralightTypeNTAG215:
  275. case MfUltralightTypeNTAG216:
  276. case MfUltralightTypeNTAGI2C1K:
  277. case MfUltralightTypeNTAGI2C2K:
  278. case MfUltralightTypeNTAGI2CPlus1K:
  279. case MfUltralightTypeNTAGI2CPlus2K:
  280. instance->config[27] = Gen4PollerUltralightModeNTAG;
  281. instance->total_blocks = 64 * 2;
  282. break;
  283. case MfUltralightTypeUL11:
  284. case MfUltralightTypeUL21:
  285. // UL-C?
  286. // UL?
  287. default:
  288. instance->config[27] = Gen4PollerUltralightModeUL_EV1;
  289. break;
  290. }
  291. if(iso3_data->uid_len == 4) {
  292. instance->config[1] = Gen4PollerUIDLengthSingle;
  293. } else if(iso3_data->uid_len == 7) {
  294. instance->config[1] = Gen4PollerUIDLengthDouble;
  295. } else {
  296. FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
  297. instance->state = Gen4PollerStateFail;
  298. break;
  299. }
  300. instance->config[6] = Gen4PollerShadowModeHighSpeedDisabled;
  301. instance->config[24] = iso3_data->atqa[0];
  302. instance->config[25] = iso3_data->atqa[1];
  303. instance->config[26] = iso3_data->sak;
  304. instance->config[27] = 0x00;
  305. instance->config[28] = instance->total_blocks;
  306. instance->config[29] = Gen4PollerDirectWriteBlock0ModeDisabled;
  307. Gen4PollerError error = gen4_poller_set_config(
  308. instance, instance->password, instance->config, sizeof(instance->config), false);
  309. if(error != Gen4PollerErrorNone) {
  310. FURI_LOG_D(TAG, "Failed to write config: %d", error);
  311. instance->state = Gen4PollerStateFail;
  312. break;
  313. }
  314. }
  315. if(instance->current_block < mfu_data->pages_read) {
  316. FURI_LOG_D(
  317. TAG, "Writing page %zu / %zu", instance->current_block, mfu_data->pages_read);
  318. Gen4PollerError error = gen4_poller_write_block(
  319. instance,
  320. instance->password,
  321. instance->current_block,
  322. mfu_data->page[instance->current_block].data);
  323. if(error != Gen4PollerErrorNone) {
  324. FURI_LOG_D(TAG, "Failed to write %d page: %d", instance->current_block, error);
  325. instance->state = Gen4PollerStateFail;
  326. break;
  327. }
  328. instance->current_block++;
  329. } else {
  330. uint8_t block[GEN4_POLLER_BLOCK_SIZE] = {};
  331. bool write_success = true;
  332. for(size_t i = 0; i < 8; i++) {
  333. memcpy(block, &mfu_data->signature.data[i * 4], 4); //-V1086
  334. Gen4PollerError error =
  335. gen4_poller_write_block(instance, instance->password, 0xF2 + i, block);
  336. if(error != Gen4PollerErrorNone) {
  337. write_success = false;
  338. break;
  339. }
  340. }
  341. if(!write_success) {
  342. FURI_LOG_E(TAG, "Failed to write Signature");
  343. instance->state = Gen4PollerStateFail;
  344. break;
  345. }
  346. block[0] = mfu_data->version.header;
  347. block[1] = mfu_data->version.vendor_id;
  348. block[2] = mfu_data->version.prod_type;
  349. block[3] = mfu_data->version.prod_subtype;
  350. Gen4PollerError error =
  351. gen4_poller_write_block(instance, instance->password, 0xFA, block);
  352. if(error != Gen4PollerErrorNone) {
  353. FURI_LOG_E(TAG, "Failed to write 1st part Version");
  354. instance->state = Gen4PollerStateFail;
  355. break;
  356. }
  357. block[0] = mfu_data->version.prod_ver_major;
  358. block[1] = mfu_data->version.prod_ver_minor;
  359. block[2] = mfu_data->version.storage_size;
  360. block[3] = mfu_data->version.protocol_type;
  361. error = gen4_poller_write_block(instance, instance->password, 0xFB, block);
  362. if(error != Gen4PollerErrorNone) {
  363. FURI_LOG_E(TAG, "Failed to write 2nd part Version");
  364. instance->state = Gen4PollerStateFail;
  365. break;
  366. }
  367. instance->state = Gen4PollerStateSuccess;
  368. }
  369. } while(false);
  370. return command;
  371. }
  372. NfcCommand gen4_poller_write_handler(Gen4Poller* instance) {
  373. NfcCommand command = NfcCommandContinue;
  374. memcpy(instance->config, gen4_poller_default_config, sizeof(gen4_poller_default_config));
  375. uint8_t password_arr[4] = {};
  376. nfc_util_num2bytes(instance->password, sizeof(password_arr), password_arr);
  377. memcpy(&instance->config[2], password_arr, sizeof(password_arr));
  378. memset(&instance->config[7], 0, 17);
  379. if(instance->protocol == NfcProtocolMfClassic) {
  380. command = gen4_poller_write_mf_classic(instance);
  381. } else if(instance->protocol == NfcProtocolMfUltralight) {
  382. command = gen4_poller_write_mf_ultralight(instance);
  383. } else {
  384. furi_crash("Unsupported protocol to write");
  385. }
  386. return command;
  387. }
  388. NfcCommand gen4_poller_change_password_handler(Gen4Poller* instance) {
  389. NfcCommand command = NfcCommandContinue;
  390. do {
  391. instance->gen4_event.type = Gen4PollerEventTypeRequestNewPassword;
  392. command = instance->callback(instance->gen4_event, instance->context);
  393. if(command != NfcCommandContinue) break;
  394. uint32_t new_password = instance->gen4_event_data.request_password.password;
  395. Gen4PollerError error =
  396. gen4_poller_change_password(instance, instance->password, new_password);
  397. if(error != Gen4PollerErrorNone) {
  398. FURI_LOG_E(TAG, "Failed to change password: %d", error);
  399. instance->state = Gen4PollerStateFail;
  400. break;
  401. }
  402. instance->password = new_password;
  403. instance->state = Gen4PollerStateSuccess;
  404. } while(false);
  405. return command;
  406. }
  407. NfcCommand gen4_poller_set_default_cfg_handler(Gen4Poller* instance) {
  408. NfcCommand command = NfcCommandContinue;
  409. do {
  410. Gen4PollerError error = gen4_poller_set_config(
  411. instance,
  412. instance->password,
  413. gen4_poller_default_config,
  414. sizeof(gen4_poller_default_config),
  415. false);
  416. if(error != Gen4PollerErrorNone) {
  417. FURI_LOG_E(TAG, "Failed to set default config: %d", error);
  418. instance->state = Gen4PollerStateFail;
  419. break;
  420. }
  421. instance->state = Gen4PollerStateSuccess;
  422. } while(false);
  423. return command;
  424. }
  425. NfcCommand gen4_poller_get_current_cfg_handler(Gen4Poller* instance) {
  426. NfcCommand command = NfcCommandContinue;
  427. do {
  428. uint8_t the_config[32] = {};
  429. Gen4PollerError error = gen4_poller_get_config(instance, instance->password, the_config);
  430. if(error != Gen4PollerErrorNone) {
  431. FURI_LOG_E(TAG, "Failed to get current config: %d", error);
  432. instance->state = Gen4PollerStateFail;
  433. break;
  434. }
  435. // Copy config data to event data buffer
  436. memcpy(instance->gen4_event_data.display_config, the_config, sizeof(the_config));
  437. instance->state = Gen4PollerStateSuccess;
  438. } while(false);
  439. return command;
  440. }
  441. NfcCommand gen4_poller_get_revision_handler(Gen4Poller* instance) {
  442. NfcCommand command = NfcCommandContinue;
  443. do {
  444. uint8_t the_revision[5] = {0};
  445. Gen4PollerError error =
  446. gen4_poller_get_revision(instance, instance->password, the_revision);
  447. if(error != Gen4PollerErrorNone) {
  448. FURI_LOG_E(TAG, "Failed to get revision: %d", error);
  449. instance->state = Gen4PollerStateFail;
  450. break;
  451. }
  452. // Copy revision data to event data buffer
  453. memcpy(instance->gen4_event_data.revision_data, the_revision, sizeof(the_revision));
  454. instance->state = Gen4PollerStateSuccess;
  455. } while(false);
  456. return command;
  457. }
  458. NfcCommand gen4_poller_set_shadow_mode_handler(Gen4Poller* instance) {
  459. NfcCommand command = NfcCommandContinue;
  460. do {
  461. Gen4PollerError error =
  462. gen4_poller_set_shadow_mode(instance, instance->password, instance->shadow_mode);
  463. if(error != Gen4PollerErrorNone) {
  464. FURI_LOG_E(TAG, "Failed to set shadow mode: %d", error);
  465. instance->state = Gen4PollerStateFail;
  466. break;
  467. }
  468. instance->state = Gen4PollerStateSuccess;
  469. } while(false);
  470. return command;
  471. }
  472. NfcCommand gen4_poller_set_direct_write_block_0_mode_handler(Gen4Poller* instance) {
  473. NfcCommand command = NfcCommandContinue;
  474. do {
  475. Gen4PollerError error = gen4_poller_set_direct_write_block_0_mode(
  476. instance, instance->password, instance->direct_write_block_0_mode);
  477. if(error != Gen4PollerErrorNone) {
  478. FURI_LOG_E(TAG, "Failed to set direct write to block 0 mode: %d", error);
  479. instance->state = Gen4PollerStateFail;
  480. break;
  481. }
  482. instance->state = Gen4PollerStateSuccess;
  483. } while(false);
  484. return command;
  485. }
  486. NfcCommand gen4_poller_success_handler(Gen4Poller* instance) {
  487. NfcCommand command = NfcCommandContinue;
  488. instance->gen4_event.type = Gen4PollerEventTypeSuccess;
  489. command = instance->callback(instance->gen4_event, instance->context);
  490. if(command != NfcCommandStop) {
  491. furi_delay_ms(100);
  492. }
  493. return command;
  494. }
  495. NfcCommand gen4_poller_fail_handler(Gen4Poller* instance) {
  496. NfcCommand command = NfcCommandContinue;
  497. instance->gen4_event.type = Gen4PollerEventTypeFail;
  498. command = instance->callback(instance->gen4_event, instance->context);
  499. if(command != NfcCommandStop) {
  500. furi_delay_ms(100);
  501. }
  502. return command;
  503. }
  504. static const Gen4PollerStateHandler gen4_poller_state_handlers[Gen4PollerStateNum] = {
  505. [Gen4PollerStateIdle] = gen4_poller_idle_handler,
  506. [Gen4PollerStateRequestMode] = gen4_poller_request_mode_handler,
  507. [Gen4PollerStateRequestWriteData] = gen4_poller_request_write_data_handler,
  508. [Gen4PollerStateWrite] = gen4_poller_write_handler,
  509. [Gen4PollerStateWipe] = gen4_poller_wipe_handler,
  510. [Gen4PollerStateChangePassword] = gen4_poller_change_password_handler,
  511. [Gen4PollerStateSetDefaultConfig] = gen4_poller_set_default_cfg_handler,
  512. [Gen4PollerStateGetCurrentConfig] = gen4_poller_get_current_cfg_handler,
  513. [Gen4PollerStateGetRevision] = gen4_poller_get_revision_handler,
  514. [Gen4PollerStateSetShadowMode] = gen4_poller_set_shadow_mode_handler,
  515. [Gen4PollerStateSetDirectWriteBlock0] = gen4_poller_set_direct_write_block_0_mode_handler,
  516. [Gen4PollerStateSuccess] = gen4_poller_success_handler,
  517. [Gen4PollerStateFail] = gen4_poller_fail_handler,
  518. };
  519. static NfcCommand gen4_poller_callback(NfcGenericEvent event, void* context) {
  520. furi_assert(context);
  521. furi_assert(event.protocol == NfcProtocolIso14443_3a);
  522. furi_assert(event.event_data);
  523. furi_assert(event.instance);
  524. NfcCommand command = NfcCommandContinue;
  525. Gen4Poller* instance = context;
  526. instance->iso3_poller = event.instance;
  527. Iso14443_3aPollerEvent* iso3_event = event.event_data;
  528. if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
  529. command = gen4_poller_state_handlers[instance->state](instance);
  530. }
  531. return command;
  532. }
  533. void gen4_poller_start(Gen4Poller* instance, Gen4PollerCallback callback, void* context) {
  534. furi_assert(instance);
  535. furi_assert(callback);
  536. instance->callback = callback;
  537. instance->context = context;
  538. nfc_poller_start(instance->poller, gen4_poller_callback, instance);
  539. }
  540. void gen4_poller_stop(Gen4Poller* instance) {
  541. furi_assert(instance);
  542. nfc_poller_stop(instance->poller);
  543. }