2021-06-10 11:53:59 +00:00
|
|
|
#include <furi.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2021-06-10 11:53:59 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <cli/cli.h>
|
2021-07-25 11:34:54 +00:00
|
|
|
#include <lib/toolbox/args.h>
|
2021-06-10 11:53:59 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
#include "helpers/rfid_reader.h"
|
|
|
|
#include "helpers/rfid_timer_emulator.h"
|
2021-06-10 11:53:59 +00:00
|
|
|
|
|
|
|
void lfrfid_cli(Cli* cli, string_t args, void* context);
|
|
|
|
|
|
|
|
// app cli function
|
2021-12-24 18:47:48 +00:00
|
|
|
extern "C" void lfrfid_on_system_start() {
|
|
|
|
#ifdef SRV_CLI
|
2021-06-10 11:53:59 +00:00
|
|
|
Cli* cli = static_cast<Cli*>(furi_record_open("cli"));
|
2021-07-18 18:09:00 +00:00
|
|
|
cli_add_command(cli, "rfid", CliCommandFlagDefault, lfrfid_cli, NULL);
|
2021-06-10 11:53:59 +00:00
|
|
|
furi_record_close("cli");
|
2021-12-24 18:47:48 +00:00
|
|
|
#endif
|
2021-06-10 11:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lfrfid_cli_print_usage() {
|
|
|
|
printf("Usage:\r\n");
|
2021-11-16 08:47:49 +00:00
|
|
|
printf("rfid read <optional: normal | indala>\r\n");
|
2021-06-10 11:53:59 +00:00
|
|
|
printf("rfid <write | emulate> <key_type> <key_data>\r\n");
|
|
|
|
printf("\t<key_type> choose from:\r\n");
|
|
|
|
printf("\tEM4100, EM-Marin (5 bytes key_data)\r\n");
|
|
|
|
printf("\tH10301, HID26 (3 bytes key_data)\r\n");
|
|
|
|
printf("\tI40134, Indala (3 bytes key_data)\r\n");
|
|
|
|
printf("\t<key_data> are hex-formatted\r\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
bool lfrfid_cli_get_key_type(string_t data, LfrfidKeyType* type) {
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if(string_cmp_str(data, "EM4100") == 0 || string_cmp_str(data, "EM-Marin") == 0) {
|
|
|
|
result = true;
|
|
|
|
*type = LfrfidKeyType::KeyEM4100;
|
|
|
|
} else if(string_cmp_str(data, "H10301") == 0 || string_cmp_str(data, "HID26") == 0) {
|
|
|
|
result = true;
|
|
|
|
*type = LfrfidKeyType::KeyH10301;
|
|
|
|
} else if(string_cmp_str(data, "I40134") == 0 || string_cmp_str(data, "Indala") == 0) {
|
|
|
|
result = true;
|
|
|
|
*type = LfrfidKeyType::KeyI40134;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-11-16 08:47:49 +00:00
|
|
|
void lfrfid_cli_read(Cli* cli, string_t args) {
|
2021-06-10 11:53:59 +00:00
|
|
|
RfidReader reader;
|
2021-11-16 08:47:49 +00:00
|
|
|
string_t type_string;
|
|
|
|
string_init(type_string);
|
|
|
|
bool simple_mode = true;
|
|
|
|
LfrfidKeyType type;
|
|
|
|
RfidReader::Type reader_type = RfidReader::Type::Normal;
|
2021-06-10 11:53:59 +00:00
|
|
|
static const uint8_t data_size = LFRFID_KEY_SIZE;
|
|
|
|
uint8_t data[data_size] = {0};
|
2021-11-16 08:47:49 +00:00
|
|
|
|
|
|
|
if(args_read_string_and_trim(args, type_string)) {
|
|
|
|
simple_mode = false;
|
|
|
|
|
|
|
|
if(string_cmp_str(type_string, "normal") == 0) {
|
|
|
|
reader_type = RfidReader::Type::Normal;
|
|
|
|
} else if(string_cmp_str(type_string, "indala") == 0) {
|
|
|
|
reader_type = RfidReader::Type::Indala;
|
|
|
|
} else {
|
|
|
|
lfrfid_cli_print_usage();
|
|
|
|
string_clear(type_string);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(simple_mode) {
|
|
|
|
reader.start();
|
|
|
|
} else {
|
|
|
|
reader.start_forced(reader_type);
|
|
|
|
}
|
2021-06-10 11:53:59 +00:00
|
|
|
|
|
|
|
printf("Reading RFID...\r\nPress Ctrl+C to abort\r\n");
|
|
|
|
while(!cli_cmd_interrupt_received(cli)) {
|
2021-11-16 08:47:49 +00:00
|
|
|
if(reader.read(&type, data, data_size, simple_mode)) {
|
2021-10-03 23:30:47 +00:00
|
|
|
printf("%s", lfrfid_key_get_type_string(type));
|
2021-06-10 11:53:59 +00:00
|
|
|
printf(" ");
|
|
|
|
|
|
|
|
for(uint8_t i = 0; i < lfrfid_key_get_type_data_count(type); i++) {
|
|
|
|
printf("%02X", data[i]);
|
|
|
|
}
|
|
|
|
printf("\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
delay(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Reading stopped\r\n");
|
|
|
|
reader.stop();
|
2021-11-16 08:47:49 +00:00
|
|
|
|
|
|
|
string_clear(type_string);
|
2021-06-10 11:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lfrfid_cli_write(Cli* cli, string_t args) {
|
|
|
|
// TODO implement rfid write
|
|
|
|
printf("Not implemented :(\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void lfrfid_cli_emulate(Cli* cli, string_t args) {
|
|
|
|
string_t data;
|
|
|
|
string_init(data);
|
|
|
|
RfidTimerEmulator emulator;
|
|
|
|
|
|
|
|
static const uint8_t data_size = LFRFID_KEY_SIZE;
|
|
|
|
uint8_t key_data[data_size] = {0};
|
|
|
|
uint8_t key_data_size = 0;
|
|
|
|
LfrfidKeyType type;
|
|
|
|
|
|
|
|
if(!args_read_string_and_trim(args, data)) {
|
|
|
|
lfrfid_cli_print_usage();
|
|
|
|
string_clear(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!lfrfid_cli_get_key_type(data, &type)) {
|
|
|
|
lfrfid_cli_print_usage();
|
|
|
|
string_clear(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
key_data_size = lfrfid_key_get_type_data_count(type);
|
|
|
|
|
|
|
|
if(!args_read_hex_bytes(args, key_data, key_data_size)) {
|
|
|
|
lfrfid_cli_print_usage();
|
|
|
|
string_clear(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emulator.start(type, key_data, key_data_size);
|
|
|
|
|
|
|
|
printf("Emulating RFID...\r\nPress Ctrl+C to abort\r\n");
|
|
|
|
while(!cli_cmd_interrupt_received(cli)) {
|
|
|
|
delay(100);
|
|
|
|
}
|
|
|
|
printf("Emulation stopped\r\n");
|
|
|
|
emulator.stop();
|
|
|
|
|
|
|
|
string_clear(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lfrfid_cli(Cli* cli, string_t args, void* context) {
|
|
|
|
string_t cmd;
|
|
|
|
string_init(cmd);
|
|
|
|
|
|
|
|
if(!args_read_string_and_trim(args, cmd)) {
|
|
|
|
string_clear(cmd);
|
|
|
|
lfrfid_cli_print_usage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(string_cmp_str(cmd, "read") == 0) {
|
2021-11-16 08:47:49 +00:00
|
|
|
lfrfid_cli_read(cli, args);
|
2021-06-10 11:53:59 +00:00
|
|
|
} else if(string_cmp_str(cmd, "write") == 0) {
|
|
|
|
lfrfid_cli_write(cli, args);
|
|
|
|
} else if(string_cmp_str(cmd, "emulate") == 0) {
|
|
|
|
lfrfid_cli_emulate(cli, args);
|
|
|
|
} else {
|
|
|
|
lfrfid_cli_print_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
string_clear(cmd);
|
|
|
|
}
|