lfrfid_cli.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. static void lfrfid_cli(Cli* cli, string_t args, void* context);
  9. // app cli function
  10. extern "C" void lfrfid_on_system_start() {
  11. #ifdef SRV_CLI
  12. Cli* cli = static_cast<Cli*>(furi_record_open("cli"));
  13. cli_add_command(cli, "rfid", CliCommandFlagDefault, lfrfid_cli, NULL);
  14. furi_record_close("cli");
  15. #else
  16. UNUSED(lfrfid_cli);
  17. #endif
  18. }
  19. void lfrfid_cli_print_usage() {
  20. printf("Usage:\r\n");
  21. printf("rfid read <optional: normal | indala>\r\n");
  22. printf("rfid <write | emulate> <key_type> <key_data>\r\n");
  23. printf("\t<key_type> choose from:\r\n");
  24. printf("\tEM4100, EM-Marin (5 bytes key_data)\r\n");
  25. printf("\tH10301, HID26 (3 bytes key_data)\r\n");
  26. printf("\tI40134, Indala (3 bytes key_data)\r\n");
  27. printf("\t<key_data> are hex-formatted\r\n");
  28. };
  29. static bool lfrfid_cli_get_key_type(string_t data, LfrfidKeyType* type) {
  30. bool result = false;
  31. if(string_cmp_str(data, "EM4100") == 0 || string_cmp_str(data, "EM-Marin") == 0) {
  32. result = true;
  33. *type = LfrfidKeyType::KeyEM4100;
  34. } else if(string_cmp_str(data, "H10301") == 0 || string_cmp_str(data, "HID26") == 0) {
  35. result = true;
  36. *type = LfrfidKeyType::KeyH10301;
  37. } else if(string_cmp_str(data, "I40134") == 0 || string_cmp_str(data, "Indala") == 0) {
  38. result = true;
  39. *type = LfrfidKeyType::KeyI40134;
  40. }
  41. return result;
  42. }
  43. static void lfrfid_cli_read(Cli* cli, string_t args) {
  44. RfidReader reader;
  45. string_t type_string;
  46. string_init(type_string);
  47. bool simple_mode = true;
  48. LfrfidKeyType type;
  49. RfidReader::Type reader_type = RfidReader::Type::Normal;
  50. static const uint8_t data_size = LFRFID_KEY_SIZE;
  51. uint8_t data[data_size] = {0};
  52. if(args_read_string_and_trim(args, type_string)) {
  53. simple_mode = false;
  54. if(string_cmp_str(type_string, "normal") == 0) {
  55. reader_type = RfidReader::Type::Normal;
  56. } else if(string_cmp_str(type_string, "indala") == 0) {
  57. reader_type = RfidReader::Type::Indala;
  58. } else {
  59. lfrfid_cli_print_usage();
  60. string_clear(type_string);
  61. return;
  62. }
  63. }
  64. if(simple_mode) {
  65. reader.start();
  66. } else {
  67. reader.start_forced(reader_type);
  68. }
  69. printf("Reading RFID...\r\nPress Ctrl+C to abort\r\n");
  70. while(!cli_cmd_interrupt_received(cli)) {
  71. if(reader.read(&type, data, data_size, simple_mode)) {
  72. printf("%s", lfrfid_key_get_type_string(type));
  73. printf(" ");
  74. for(uint8_t i = 0; i < lfrfid_key_get_type_data_count(type); i++) {
  75. printf("%02X", data[i]);
  76. }
  77. printf("\r\n");
  78. break;
  79. }
  80. furi_hal_delay_ms(100);
  81. }
  82. printf("Reading stopped\r\n");
  83. reader.stop();
  84. string_clear(type_string);
  85. }
  86. static void lfrfid_cli_write(Cli* cli, string_t args) {
  87. // TODO implement rfid write
  88. printf("Not implemented :(\r\n");
  89. }
  90. static void lfrfid_cli_emulate(Cli* cli, string_t args) {
  91. string_t data;
  92. string_init(data);
  93. RfidTimerEmulator emulator;
  94. static const uint8_t data_size = LFRFID_KEY_SIZE;
  95. uint8_t key_data[data_size] = {0};
  96. uint8_t key_data_size = 0;
  97. LfrfidKeyType type;
  98. if(!args_read_string_and_trim(args, data)) {
  99. lfrfid_cli_print_usage();
  100. string_clear(data);
  101. return;
  102. }
  103. if(!lfrfid_cli_get_key_type(data, &type)) {
  104. lfrfid_cli_print_usage();
  105. string_clear(data);
  106. return;
  107. }
  108. key_data_size = lfrfid_key_get_type_data_count(type);
  109. if(!args_read_hex_bytes(args, key_data, key_data_size)) {
  110. lfrfid_cli_print_usage();
  111. string_clear(data);
  112. return;
  113. }
  114. emulator.start(type, key_data, key_data_size);
  115. printf("Emulating RFID...\r\nPress Ctrl+C to abort\r\n");
  116. while(!cli_cmd_interrupt_received(cli)) {
  117. furi_hal_delay_ms(100);
  118. }
  119. printf("Emulation stopped\r\n");
  120. emulator.stop();
  121. string_clear(data);
  122. }
  123. static void lfrfid_cli(Cli* cli, string_t args, void* context) {
  124. string_t cmd;
  125. string_init(cmd);
  126. if(!args_read_string_and_trim(args, cmd)) {
  127. string_clear(cmd);
  128. lfrfid_cli_print_usage();
  129. return;
  130. }
  131. if(string_cmp_str(cmd, "read") == 0) {
  132. lfrfid_cli_read(cli, args);
  133. } else if(string_cmp_str(cmd, "write") == 0) {
  134. lfrfid_cli_write(cli, args);
  135. } else if(string_cmp_str(cmd, "emulate") == 0) {
  136. lfrfid_cli_emulate(cli, args);
  137. } else {
  138. lfrfid_cli_print_usage();
  139. }
  140. string_clear(cmd);
  141. }