nfc_cli.c 3.5 KB

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