lfrfid_cli.cpp 4.9 KB

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