lfrfid-cli.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <furi.h>
  2. #include <api-hal.h>
  3. #include <stdarg.h>
  4. #include <cli/cli.h>
  5. #include <lib/toolbox/args.h>
  6. #include "helpers/rfid-reader.h"
  7. #include "helpers/rfid-timer-emulator.h"
  8. void lfrfid_cli(Cli* cli, string_t args, void* context);
  9. // app cli function
  10. extern "C" void lfrfid_cli_init() {
  11. Cli* cli = static_cast<Cli*>(furi_record_open("cli"));
  12. cli_add_command(cli, "rfid", CliCommandFlagDefault, lfrfid_cli, NULL);
  13. furi_record_close("cli");
  14. }
  15. void lfrfid_cli_print_usage() {
  16. printf("Usage:\r\n");
  17. printf("rfid read\r\n");
  18. printf("rfid <write | emulate> <key_type> <key_data>\r\n");
  19. printf("\t<key_type> choose from:\r\n");
  20. printf("\tEM4100, EM-Marin (5 bytes key_data)\r\n");
  21. printf("\tH10301, HID26 (3 bytes key_data)\r\n");
  22. printf("\tI40134, Indala (3 bytes key_data)\r\n");
  23. printf("\t<key_data> are hex-formatted\r\n");
  24. };
  25. bool lfrfid_cli_get_key_type(string_t data, LfrfidKeyType* type) {
  26. bool result = false;
  27. if(string_cmp_str(data, "EM4100") == 0 || string_cmp_str(data, "EM-Marin") == 0) {
  28. result = true;
  29. *type = LfrfidKeyType::KeyEM4100;
  30. } else if(string_cmp_str(data, "H10301") == 0 || string_cmp_str(data, "HID26") == 0) {
  31. result = true;
  32. *type = LfrfidKeyType::KeyH10301;
  33. } else if(string_cmp_str(data, "I40134") == 0 || string_cmp_str(data, "Indala") == 0) {
  34. result = true;
  35. *type = LfrfidKeyType::KeyI40134;
  36. }
  37. return result;
  38. }
  39. void lfrfid_cli_read(Cli* cli) {
  40. RfidReader reader;
  41. reader.start();
  42. static const uint8_t data_size = LFRFID_KEY_SIZE;
  43. uint8_t data[data_size] = {0};
  44. LfrfidKeyType type;
  45. printf("Reading RFID...\r\nPress Ctrl+C to abort\r\n");
  46. while(!cli_cmd_interrupt_received(cli)) {
  47. if(reader.read(&type, data, data_size)) {
  48. printf(lfrfid_key_get_type_string(type));
  49. printf(" ");
  50. for(uint8_t i = 0; i < lfrfid_key_get_type_data_count(type); i++) {
  51. printf("%02X", data[i]);
  52. }
  53. printf("\r\n");
  54. break;
  55. }
  56. delay(100);
  57. }
  58. printf("Reading stopped\r\n");
  59. reader.stop();
  60. }
  61. void lfrfid_cli_write(Cli* cli, string_t args) {
  62. // TODO implement rfid write
  63. printf("Not implemented :(\r\n");
  64. }
  65. void lfrfid_cli_emulate(Cli* cli, string_t args) {
  66. string_t data;
  67. string_init(data);
  68. RfidTimerEmulator emulator;
  69. static const uint8_t data_size = LFRFID_KEY_SIZE;
  70. uint8_t key_data[data_size] = {0};
  71. uint8_t key_data_size = 0;
  72. LfrfidKeyType type;
  73. if(!args_read_string_and_trim(args, data)) {
  74. lfrfid_cli_print_usage();
  75. string_clear(data);
  76. return;
  77. }
  78. if(!lfrfid_cli_get_key_type(data, &type)) {
  79. lfrfid_cli_print_usage();
  80. string_clear(data);
  81. return;
  82. }
  83. key_data_size = lfrfid_key_get_type_data_count(type);
  84. if(!args_read_hex_bytes(args, key_data, key_data_size)) {
  85. lfrfid_cli_print_usage();
  86. string_clear(data);
  87. return;
  88. }
  89. emulator.start(type, key_data, key_data_size);
  90. printf("Emulating RFID...\r\nPress Ctrl+C to abort\r\n");
  91. while(!cli_cmd_interrupt_received(cli)) {
  92. delay(100);
  93. }
  94. printf("Emulation stopped\r\n");
  95. emulator.stop();
  96. string_clear(data);
  97. }
  98. void lfrfid_cli(Cli* cli, string_t args, void* context) {
  99. string_t cmd;
  100. string_init(cmd);
  101. if(!args_read_string_and_trim(args, cmd)) {
  102. string_clear(cmd);
  103. lfrfid_cli_print_usage();
  104. return;
  105. }
  106. if(string_cmp_str(cmd, "read") == 0) {
  107. lfrfid_cli_read(cli);
  108. } else if(string_cmp_str(cmd, "write") == 0) {
  109. lfrfid_cli_write(cli, args);
  110. } else if(string_cmp_str(cmd, "emulate") == 0) {
  111. lfrfid_cli_emulate(cli, args);
  112. } else {
  113. lfrfid_cli_print_usage();
  114. }
  115. string_clear(cmd);
  116. }