nfc_cli.c 3.9 KB

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