nfc_cli.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. }
  13. void nfc_cli_detect(Cli* cli, string_t args) {
  14. // Check if nfc worker is not busy
  15. if(furi_hal_nfc_is_busy()) {
  16. printf("Nfc is busy\r\n");
  17. return;
  18. }
  19. rfalNfcDevice* dev_list;
  20. uint8_t dev_cnt = 0;
  21. bool cmd_exit = false;
  22. furi_hal_nfc_exit_sleep();
  23. printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n");
  24. while(!cmd_exit) {
  25. cmd_exit |= cli_cmd_interrupt_received(cli);
  26. cmd_exit |= furi_hal_nfc_detect(&dev_list, &dev_cnt, 400, true);
  27. if(dev_cnt > 0) {
  28. printf("Found %d devices\r\n", dev_cnt);
  29. for(uint8_t i = 0; i < dev_cnt; i++) {
  30. printf("%d found: %s ", i + 1, nfc_get_rfal_type(dev_list[i].type));
  31. if(dev_list[i].type == RFAL_NFC_LISTEN_TYPE_NFCA) {
  32. printf("type: %s, ", nfc_get_nfca_type(dev_list[i].dev.nfca.type));
  33. }
  34. printf("UID length: %d, UID:", dev_list[i].nfcidLen);
  35. for(uint8_t j = 0; j < dev_list[i].nfcidLen; j++) {
  36. printf("%02X", dev_list[i].nfcid[j]);
  37. }
  38. printf("\r\n");
  39. }
  40. }
  41. osDelay(50);
  42. }
  43. furi_hal_nfc_deactivate();
  44. }
  45. void nfc_cli_emulate(Cli* cli, string_t 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. NfcDeviceCommonData params = {
  55. .uid = {0x36, 0x9C, 0xe7, 0xb1, 0x0A, 0xC1, 0x34},
  56. .uid_len = 7,
  57. .atqa = {0x44, 0x00},
  58. .sak = 0x00,
  59. .device = NfcDeviceNfca,
  60. .protocol = NfcDeviceProtocolMifareUl,
  61. };
  62. while(!cli_cmd_interrupt_received(cli)) {
  63. if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, false, 100)) {
  64. printf("Reader detected\r\n");
  65. furi_hal_nfc_deactivate();
  66. }
  67. osDelay(50);
  68. }
  69. furi_hal_nfc_deactivate();
  70. }
  71. static void nfc_cli(Cli* cli, string_t args, void* context) {
  72. string_t cmd;
  73. string_init(cmd);
  74. do {
  75. if(!args_read_string_and_trim(args, cmd)) {
  76. nfc_cli_print_usage();
  77. break;
  78. }
  79. if(string_cmp_str(cmd, "detect") == 0) {
  80. nfc_cli_detect(cli, args);
  81. break;
  82. }
  83. if(string_cmp_str(cmd, "emulate") == 0) {
  84. nfc_cli_emulate(cli, args);
  85. break;
  86. }
  87. nfc_cli_print_usage();
  88. } while(false);
  89. string_clear(cmd);
  90. }
  91. void nfc_on_system_start() {
  92. #ifdef SRV_CLI
  93. Cli* cli = furi_record_open("cli");
  94. cli_add_command(cli, "nfc", CliCommandFlagDefault, nfc_cli, NULL);
  95. furi_record_close("cli");
  96. #endif
  97. }