nfc_cli.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "nfc_cli.h"
  2. #include "nfc_types.h"
  3. #include <furi.h>
  4. #include <furi-hal.h>
  5. void nfc_cli_init() {
  6. Cli* cli = furi_record_open("cli");
  7. cli_add_command(cli, "nfc_detect", CliCommandFlagDefault, nfc_cli_detect, NULL);
  8. cli_add_command(cli, "nfc_emulate", CliCommandFlagDefault, nfc_cli_emulate, NULL);
  9. furi_record_close("cli");
  10. }
  11. void nfc_cli_detect(Cli* cli, string_t args, void* context) {
  12. // Check if nfc worker is not busy
  13. if(furi_hal_nfc_is_busy()) {
  14. printf("Nfc is busy");
  15. return;
  16. }
  17. rfalNfcDevice* dev_list;
  18. uint8_t dev_cnt = 0;
  19. bool cmd_exit = false;
  20. furi_hal_nfc_exit_sleep();
  21. printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n");
  22. while(!cmd_exit) {
  23. cmd_exit |= cli_cmd_interrupt_received(cli);
  24. cmd_exit |= furi_hal_nfc_detect(&dev_list, &dev_cnt, 400, true);
  25. if(dev_cnt > 0) {
  26. printf("Found %d devices\r\n", dev_cnt);
  27. for(uint8_t i = 0; i < dev_cnt; i++) {
  28. printf("%d found: %s ", i + 1, nfc_get_rfal_type(dev_list[i].type));
  29. if(dev_list[i].type == RFAL_NFC_LISTEN_TYPE_NFCA) {
  30. printf("type: %s, ", nfc_get_nfca_type(dev_list[i].dev.nfca.type));
  31. }
  32. printf("UID length: %d, UID:", dev_list[i].nfcidLen);
  33. for(uint8_t j = 0; j < dev_list[i].nfcidLen; j++) {
  34. printf("%02X", dev_list[i].nfcid[j]);
  35. }
  36. printf("\r\n");
  37. }
  38. }
  39. osDelay(50);
  40. }
  41. furi_hal_nfc_deactivate();
  42. }
  43. void nfc_cli_emulate(Cli* cli, string_t args, void* context) {
  44. // Check if nfc worker is not busy
  45. if(furi_hal_nfc_is_busy()) {
  46. printf("Nfc is busy");
  47. return;
  48. }
  49. furi_hal_nfc_exit_sleep();
  50. printf("Emulating NFC-A Type: T2T UID: CF72D440 SAK: 20 ATQA: 00/04\r\n");
  51. printf("Press Ctrl+C to abort\r\n");
  52. NfcDeviceCommonData params = {
  53. .uid = {0x36, 0x9C, 0xe7, 0xb1, 0x0A, 0xC1, 0x34},
  54. .uid_len = 7,
  55. .atqa = {0x44, 0x00},
  56. .sak = 0x00,
  57. .device = NfcDeviceNfca,
  58. .protocol = NfcDeviceProtocolMifareUl,
  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_deactivate();
  64. }
  65. osDelay(50);
  66. }
  67. furi_hal_nfc_deactivate();
  68. }