nfc_cli.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <cli/cli.h>
  4. #include <lib/toolbox/args.h>
  5. #include <lib/toolbox/hex.h>
  6. #include <lib/nfc/nfc_types.h>
  7. #include <lib/nfc/nfc_device.h>
  8. static void nfc_cli_print_usage() {
  9. printf("Usage:\r\n");
  10. printf("nfc <cmd>\r\n");
  11. printf("Cmd list:\r\n");
  12. printf("\tdetect\t - detect nfc device\r\n");
  13. printf("\temulate\t - emulate predefined nfca card\r\n");
  14. printf("\tapdu\t - Send APDU and print response \r\n");
  15. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  16. printf("\tfield\t - turn field on\r\n");
  17. }
  18. }
  19. static void nfc_cli_detect(Cli* cli, FuriString* args) {
  20. UNUSED(args);
  21. // Check if nfc worker is not busy
  22. if(furi_hal_nfc_is_busy()) {
  23. printf("Nfc is busy\r\n");
  24. return;
  25. }
  26. FuriHalNfcDevData dev_data = {};
  27. bool cmd_exit = false;
  28. furi_hal_nfc_exit_sleep();
  29. printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n");
  30. while(!cmd_exit) {
  31. cmd_exit |= cli_cmd_interrupt_received(cli);
  32. if(furi_hal_nfc_detect(&dev_data, 400)) {
  33. printf("Found: %s ", nfc_get_dev_type(dev_data.type));
  34. printf("UID length: %d, UID:", dev_data.uid_len);
  35. for(size_t i = 0; i < dev_data.uid_len; i++) {
  36. printf("%02X", dev_data.uid[i]);
  37. }
  38. printf("\r\n");
  39. break;
  40. }
  41. furi_hal_nfc_sleep();
  42. furi_delay_ms(50);
  43. }
  44. furi_hal_nfc_sleep();
  45. }
  46. static void nfc_cli_emulate(Cli* cli, FuriString* args) {
  47. UNUSED(args);
  48. // Check if nfc worker is not busy
  49. if(furi_hal_nfc_is_busy()) {
  50. printf("Nfc is busy\r\n");
  51. return;
  52. }
  53. furi_hal_nfc_exit_sleep();
  54. printf("Emulating NFC-A Type: T2T UID: 36 9C E7 B1 0A C1 34 SAK: 00 ATQA: 00/44\r\n");
  55. printf("Press Ctrl+C to abort\r\n");
  56. FuriHalNfcDevData params = {
  57. .uid = {0x36, 0x9C, 0xe7, 0xb1, 0x0A, 0xC1, 0x34},
  58. .uid_len = 7,
  59. .atqa = {0x44, 0x00},
  60. .sak = 0x00,
  61. .type = FuriHalNfcTypeA,
  62. };
  63. while(!cli_cmd_interrupt_received(cli)) {
  64. if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, false, 100)) {
  65. printf("Reader detected\r\n");
  66. furi_hal_nfc_sleep();
  67. }
  68. furi_delay_ms(50);
  69. }
  70. furi_hal_nfc_sleep();
  71. }
  72. static void nfc_cli_field(Cli* cli, FuriString* args) {
  73. UNUSED(args);
  74. // Check if nfc worker is not busy
  75. if(furi_hal_nfc_is_busy()) {
  76. printf("Nfc is busy\r\n");
  77. return;
  78. }
  79. furi_hal_nfc_exit_sleep();
  80. furi_hal_nfc_field_on();
  81. printf("Field is on. Don't leave device in this mode for too long.\r\n");
  82. printf("Press Ctrl+C to abort\r\n");
  83. while(!cli_cmd_interrupt_received(cli)) {
  84. furi_delay_ms(50);
  85. }
  86. furi_hal_nfc_field_off();
  87. furi_hal_nfc_sleep();
  88. }
  89. static void nfc_cli_apdu(Cli* cli, FuriString* args) {
  90. UNUSED(cli);
  91. if(furi_hal_nfc_is_busy()) {
  92. printf("Nfc is busy\r\n");
  93. return;
  94. }
  95. furi_hal_nfc_exit_sleep();
  96. FuriString* data = NULL;
  97. data = furi_string_alloc();
  98. FuriHalNfcTxRxContext tx_rx = {};
  99. FuriHalNfcDevData dev_data = {};
  100. uint8_t* req_buffer = NULL;
  101. uint8_t* resp_buffer = NULL;
  102. size_t apdu_size = 0;
  103. size_t resp_size = 0;
  104. do {
  105. if(!args_read_string_and_trim(args, data)) {
  106. printf(
  107. "Use like `nfc apdu 00a404000e325041592e5359532e444446303100 00a4040008a0000003010102` \r\n");
  108. break;
  109. }
  110. printf("detecting tag\r\n");
  111. if(!furi_hal_nfc_detect(&dev_data, 300)) {
  112. printf("Failed to detect tag\r\n");
  113. break;
  114. }
  115. do {
  116. apdu_size = furi_string_size(data) / 2;
  117. req_buffer = malloc(apdu_size);
  118. hex_chars_to_uint8(furi_string_get_cstr(data), req_buffer);
  119. memcpy(tx_rx.tx_data, req_buffer, apdu_size);
  120. tx_rx.tx_bits = apdu_size * 8;
  121. tx_rx.tx_rx_type = FuriHalNfcTxRxTypeDefault;
  122. printf("Sending APDU:%s to Tag\r\n", furi_string_get_cstr(data));
  123. if(!furi_hal_nfc_tx_rx(&tx_rx, 300)) {
  124. printf("Failed to tx_rx\r\n");
  125. break;
  126. }
  127. resp_size = (tx_rx.rx_bits / 8) * 2;
  128. resp_buffer = malloc(resp_size);
  129. uint8_to_hex_chars(tx_rx.rx_data, resp_buffer, resp_size);
  130. resp_buffer[resp_size] = 0;
  131. printf("Response: %s\r\n", resp_buffer);
  132. free(req_buffer);
  133. free(resp_buffer);
  134. req_buffer = NULL;
  135. resp_buffer = NULL;
  136. } while(args_read_string_and_trim(args, data));
  137. } while(false);
  138. free(req_buffer);
  139. free(resp_buffer);
  140. furi_string_free(data);
  141. furi_hal_nfc_sleep();
  142. }
  143. static void nfc_cli(Cli* cli, FuriString* args, void* context) {
  144. UNUSED(context);
  145. FuriString* cmd;
  146. cmd = furi_string_alloc();
  147. do {
  148. if(!args_read_string_and_trim(args, cmd)) {
  149. nfc_cli_print_usage();
  150. break;
  151. }
  152. if(furi_string_cmp_str(cmd, "detect") == 0) {
  153. nfc_cli_detect(cli, args);
  154. break;
  155. }
  156. if(furi_string_cmp_str(cmd, "emulate") == 0) {
  157. nfc_cli_emulate(cli, args);
  158. break;
  159. }
  160. if(furi_string_cmp_str(cmd, "apdu") == 0) {
  161. nfc_cli_apdu(cli, args);
  162. break;
  163. }
  164. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  165. if(furi_string_cmp_str(cmd, "field") == 0) {
  166. nfc_cli_field(cli, args);
  167. break;
  168. }
  169. }
  170. nfc_cli_print_usage();
  171. } while(false);
  172. furi_string_free(cmd);
  173. }
  174. void nfc_on_system_start() {
  175. #ifdef SRV_CLI
  176. Cli* cli = furi_record_open(RECORD_CLI);
  177. cli_add_command(cli, "nfc", CliCommandFlagDefault, nfc_cli, NULL);
  178. furi_record_close(RECORD_CLI);
  179. #else
  180. UNUSED(nfc_cli);
  181. #endif
  182. }