nfc_cli.c 3.6 KB

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