lfrfid-cli.cpp 4.6 KB

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