nfc_cli.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <cli/cli.h>
  4. #include <lib/toolbox/args.h>
  5. #include <lib/nfc/nfc_types.h>
  6. #include <lib/nfc/nfc_device.h>
  7. static void nfc_cli_print_usage() {
  8. printf("Usage:\r\n");
  9. printf("nfc <cmd>\r\n");
  10. printf("Cmd list:\r\n");
  11. printf("\tdetect\t - detect nfc device\r\n");
  12. printf("\temulate\t - emulate predefined nfca card\r\n");
  13. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  14. printf("\tfield\t - turn field on\r\n");
  15. }
  16. }
  17. static void nfc_cli_detect(Cli* cli, string_t args) {
  18. UNUSED(args);
  19. // Check if nfc worker is not busy
  20. if(furi_hal_nfc_is_busy()) {
  21. printf("Nfc is busy\r\n");
  22. return;
  23. }
  24. FuriHalNfcDevData dev_data = {};
  25. bool cmd_exit = false;
  26. furi_hal_nfc_exit_sleep();
  27. printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n");
  28. while(!cmd_exit) {
  29. cmd_exit |= cli_cmd_interrupt_received(cli);
  30. if(furi_hal_nfc_detect(&dev_data, 400)) {
  31. printf("found: %s ", nfc_get_dev_type(dev_data.type));
  32. printf("UID length: %d, UID:", dev_data.uid_len);
  33. for(size_t i = 0; i < dev_data.uid_len; i++) {
  34. printf("%02X", dev_data.uid[i]);
  35. }
  36. printf("\r\n");
  37. break;
  38. }
  39. furi_hal_nfc_sleep();
  40. furi_delay_ms(50);
  41. }
  42. furi_hal_nfc_sleep();
  43. }
  44. static void nfc_cli_emulate(Cli* cli, string_t args) {
  45. UNUSED(args);
  46. // Check if nfc worker is not busy
  47. if(furi_hal_nfc_is_busy()) {
  48. printf("Nfc is busy\r\n");
  49. return;
  50. }
  51. furi_hal_nfc_exit_sleep();
  52. printf("Emulating NFC-A Type: T2T UID: 36 9C E7 B1 0A C1 34 SAK: 00 ATQA: 00/44\r\n");
  53. printf("Press Ctrl+C to abort\r\n");
  54. FuriHalNfcDevData params = {
  55. .uid = {0x36, 0x9C, 0xe7, 0xb1, 0x0A, 0xC1, 0x34},
  56. .uid_len = 7,
  57. .atqa = {0x44, 0x00},
  58. .sak = 0x00,
  59. .type = FuriHalNfcTypeA,
  60. };
  61. while(!cli_cmd_interrupt_received(cli)) {
  62. if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, false, 100)) {
  63. printf("Reader detected\r\n");
  64. furi_hal_nfc_sleep();
  65. }
  66. furi_delay_ms(50);
  67. }
  68. furi_hal_nfc_sleep();
  69. }
  70. static void nfc_cli_field(Cli* cli, string_t args) {
  71. UNUSED(args);
  72. // Check if nfc worker is not busy
  73. if(furi_hal_nfc_is_busy()) {
  74. printf("Nfc is busy\r\n");
  75. return;
  76. }
  77. furi_hal_nfc_exit_sleep();
  78. furi_hal_nfc_field_on();
  79. printf("Field is on. Don't leave device in this mode for too long.\r\n");
  80. printf("Press Ctrl+C to abort\r\n");
  81. while(!cli_cmd_interrupt_received(cli)) {
  82. furi_delay_ms(50);
  83. }
  84. furi_hal_nfc_field_off();
  85. furi_hal_nfc_sleep();
  86. }
  87. static void nfc_cli(Cli* cli, string_t args, void* context) {
  88. UNUSED(context);
  89. string_t cmd;
  90. string_init(cmd);
  91. do {
  92. if(!args_read_string_and_trim(args, cmd)) {
  93. nfc_cli_print_usage();
  94. break;
  95. }
  96. if(string_cmp_str(cmd, "detect") == 0) {
  97. nfc_cli_detect(cli, args);
  98. break;
  99. }
  100. if(string_cmp_str(cmd, "emulate") == 0) {
  101. nfc_cli_emulate(cli, args);
  102. break;
  103. }
  104. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  105. if(string_cmp_str(cmd, "field") == 0) {
  106. nfc_cli_field(cli, args);
  107. break;
  108. }
  109. }
  110. nfc_cli_print_usage();
  111. } while(false);
  112. string_clear(cmd);
  113. }
  114. void nfc_on_system_start() {
  115. #ifdef SRV_CLI
  116. Cli* cli = furi_record_open(RECORD_CLI);
  117. cli_add_command(cli, "nfc", CliCommandFlagDefault, nfc_cli, NULL);
  118. furi_record_close(RECORD_CLI);
  119. #else
  120. UNUSED(nfc_cli);
  121. #endif
  122. }