lfrfid-cli.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <furi.h>
  2. #include <furi-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 <optional: normal | indala>\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, string_t args) {
  40. RfidReader reader;
  41. string_t type_string;
  42. string_init(type_string);
  43. bool simple_mode = true;
  44. LfrfidKeyType type;
  45. RfidReader::Type reader_type = RfidReader::Type::Normal;
  46. static const uint8_t data_size = LFRFID_KEY_SIZE;
  47. uint8_t data[data_size] = {0};
  48. if(args_read_string_and_trim(args, type_string)) {
  49. simple_mode = false;
  50. if(string_cmp_str(type_string, "normal") == 0) {
  51. reader_type = RfidReader::Type::Normal;
  52. } else if(string_cmp_str(type_string, "indala") == 0) {
  53. reader_type = RfidReader::Type::Indala;
  54. } else {
  55. lfrfid_cli_print_usage();
  56. string_clear(type_string);
  57. return;
  58. }
  59. }
  60. if(simple_mode) {
  61. reader.start();
  62. } else {
  63. reader.start_forced(reader_type);
  64. }
  65. printf("Reading RFID...\r\nPress Ctrl+C to abort\r\n");
  66. while(!cli_cmd_interrupt_received(cli)) {
  67. if(reader.read(&type, data, data_size, simple_mode)) {
  68. printf("%s", lfrfid_key_get_type_string(type));
  69. printf(" ");
  70. for(uint8_t i = 0; i < lfrfid_key_get_type_data_count(type); i++) {
  71. printf("%02X", data[i]);
  72. }
  73. printf("\r\n");
  74. break;
  75. }
  76. delay(100);
  77. }
  78. printf("Reading stopped\r\n");
  79. reader.stop();
  80. string_clear(type_string);
  81. }
  82. void lfrfid_cli_write(Cli* cli, string_t args) {
  83. // TODO implement rfid write
  84. printf("Not implemented :(\r\n");
  85. }
  86. void lfrfid_cli_emulate(Cli* cli, string_t args) {
  87. string_t data;
  88. string_init(data);
  89. RfidTimerEmulator emulator;
  90. static const uint8_t data_size = LFRFID_KEY_SIZE;
  91. uint8_t key_data[data_size] = {0};
  92. uint8_t key_data_size = 0;
  93. LfrfidKeyType type;
  94. if(!args_read_string_and_trim(args, data)) {
  95. lfrfid_cli_print_usage();
  96. string_clear(data);
  97. return;
  98. }
  99. if(!lfrfid_cli_get_key_type(data, &type)) {
  100. lfrfid_cli_print_usage();
  101. string_clear(data);
  102. return;
  103. }
  104. key_data_size = lfrfid_key_get_type_data_count(type);
  105. if(!args_read_hex_bytes(args, key_data, key_data_size)) {
  106. lfrfid_cli_print_usage();
  107. string_clear(data);
  108. return;
  109. }
  110. emulator.start(type, key_data, key_data_size);
  111. printf("Emulating RFID...\r\nPress Ctrl+C to abort\r\n");
  112. while(!cli_cmd_interrupt_received(cli)) {
  113. delay(100);
  114. }
  115. printf("Emulation stopped\r\n");
  116. emulator.stop();
  117. string_clear(data);
  118. }
  119. void lfrfid_cli(Cli* cli, string_t args, void* context) {
  120. string_t cmd;
  121. string_init(cmd);
  122. if(!args_read_string_and_trim(args, cmd)) {
  123. string_clear(cmd);
  124. lfrfid_cli_print_usage();
  125. return;
  126. }
  127. if(string_cmp_str(cmd, "read") == 0) {
  128. lfrfid_cli_read(cli, args);
  129. } else if(string_cmp_str(cmd, "write") == 0) {
  130. lfrfid_cli_write(cli, args);
  131. } else if(string_cmp_str(cmd, "emulate") == 0) {
  132. lfrfid_cli_emulate(cli, args);
  133. } else {
  134. lfrfid_cli_print_usage();
  135. }
  136. string_clear(cmd);
  137. }