gen4_poller.c 25 KB

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