| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #include <furi.h>
- #include <furi_hal.h>
- #include <stdarg.h>
- #include <cli/cli.h>
- #include <lib/toolbox/args.h>
- #include "helpers/key_info.h"
- #include "helpers/key_worker.h"
- #include <memory>
- void ibutton_cli(Cli* cli, string_t args, void* context);
- void onewire_cli(Cli* cli, string_t args, void* context);
- // app cli function
- extern "C" void ibutton_on_system_start() {
- #ifdef SRV_CLI
- Cli* cli = static_cast<Cli*>(furi_record_open("cli"));
- cli_add_command(cli, "ikey", CliCommandFlagDefault, ibutton_cli, cli);
- cli_add_command(cli, "onewire", CliCommandFlagDefault, onewire_cli, cli);
- furi_record_close("cli");
- #endif
- }
- void ibutton_cli_print_usage() {
- printf("Usage:\r\n");
- printf("ikey read\r\n");
- printf("ikey <write | emulate> <key_type> <key_data>\r\n");
- printf("\t<key_type> choose from:\r\n");
- printf("\tDallas (8 bytes key_data)\r\n");
- printf("\tCyfral (2 bytes key_data)\r\n");
- printf("\tMetakom (4 bytes key_data)\r\n");
- printf("\t<key_data> are hex-formatted\r\n");
- };
- bool ibutton_cli_get_key_type(string_t data, iButtonKeyType* type) {
- bool result = false;
- if(string_cmp_str(data, "Dallas") == 0 || string_cmp_str(data, "dallas") == 0) {
- result = true;
- *type = iButtonKeyType::KeyDallas;
- } else if(string_cmp_str(data, "Cyfral") == 0 || string_cmp_str(data, "cyfral") == 0) {
- result = true;
- *type = iButtonKeyType::KeyCyfral;
- } else if(string_cmp_str(data, "Metakom") == 0 || string_cmp_str(data, "metakom") == 0) {
- result = true;
- *type = iButtonKeyType::KeyMetakom;
- }
- return result;
- }
- void ibutton_cli_print_key_data(iButtonKey* key) {
- uint8_t* key_data = key->get_data();
- switch(key->get_key_type()) {
- case iButtonKeyType::KeyDallas:
- printf(
- "Dallas %02X%02X%02X%02X%02X%02X%02X%02X\r\n",
- key_data[0],
- key_data[1],
- key_data[2],
- key_data[3],
- key_data[4],
- key_data[5],
- key_data[6],
- key_data[7]);
- break;
- case iButtonKeyType::KeyCyfral:
- printf("Cyfral %02X%02X\r\n", key_data[0], key_data[1]);
- break;
- case iButtonKeyType::KeyMetakom:
- printf("Metakom %02X%02X%02X%02X\r\n", key_data[0], key_data[1], key_data[2], key_data[3]);
- break;
- }
- }
- void ibutton_cli_read(Cli* cli) {
- iButtonKey key;
- std::unique_ptr<KeyWorker> worker(new KeyWorker(&ibutton_gpio));
- bool exit = false;
- worker->start_read();
- printf("Reading iButton...\r\nPress Ctrl+C to abort\r\n");
- while(!exit) {
- exit = cli_cmd_interrupt_received(cli);
- switch(worker->read(&key)) {
- case KeyReader::Error::EMPTY:
- break;
- case KeyReader::Error::CRC_ERROR:
- ibutton_cli_print_key_data(&key);
- printf("Warning: invalid CRC\r\n");
- exit = true;
- break;
- case KeyReader::Error::OK:
- ibutton_cli_print_key_data(&key);
- exit = true;
- break;
- case KeyReader::Error::NOT_ARE_KEY:
- ibutton_cli_print_key_data(&key);
- printf("Warning: not a key\r\n");
- exit = true;
- break;
- }
- delay(100);
- }
- worker->stop_read();
- };
- void ibutton_cli_write(Cli* cli, string_t args) {
- iButtonKey key;
- iButtonKeyType type;
- std::unique_ptr<KeyWorker> worker(new KeyWorker(&ibutton_gpio));
- bool exit = false;
- string_t data;
- string_init(data);
- if(!args_read_string_and_trim(args, data)) {
- ibutton_cli_print_usage();
- string_clear(data);
- return;
- }
- if(!ibutton_cli_get_key_type(data, &type)) {
- ibutton_cli_print_usage();
- string_clear(data);
- return;
- }
- key.set_type(type);
- if(!args_read_hex_bytes(args, key.get_data(), key.get_type_data_size())) {
- ibutton_cli_print_usage();
- string_clear(data);
- return;
- }
- printf("Writing key ");
- ibutton_cli_print_key_data(&key);
- printf("Press Ctrl+C to abort\r\n");
- worker->start_write();
- while(!exit) {
- exit = cli_cmd_interrupt_received(cli);
- KeyWriter::Error result = worker->write(&key);
- switch(result) {
- case KeyWriter::Error::SAME_KEY:
- case KeyWriter::Error::OK:
- printf("Write success\r\n");
- exit = true;
- break;
- case KeyWriter::Error::NO_DETECT:
- break;
- case KeyWriter::Error::CANNOT_WRITE:
- printf("Write fail\r\n");
- exit = true;
- break;
- }
- };
- worker->stop_write();
- string_clear(data);
- };
- void ibutton_cli_emulate(Cli* cli, string_t args) {
- iButtonKey key;
- iButtonKeyType type;
- std::unique_ptr<KeyWorker> worker(new KeyWorker(&ibutton_gpio));
- bool exit = false;
- string_t data;
- string_init(data);
- if(!args_read_string_and_trim(args, data)) {
- ibutton_cli_print_usage();
- string_clear(data);
- return;
- }
- if(!ibutton_cli_get_key_type(data, &type)) {
- ibutton_cli_print_usage();
- string_clear(data);
- return;
- }
- key.set_type(type);
- if(!args_read_hex_bytes(args, key.get_data(), key.get_type_data_size())) {
- ibutton_cli_print_usage();
- string_clear(data);
- return;
- }
- printf("Emulating key ");
- ibutton_cli_print_key_data(&key);
- printf("Press Ctrl+C to abort\r\n");
- worker->start_emulate(&key);
- while(!exit) {
- exit = cli_cmd_interrupt_received(cli);
- };
- worker->stop_emulate();
- string_clear(data);
- };
- void ibutton_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);
- ibutton_cli_print_usage();
- return;
- }
- if(string_cmp_str(cmd, "read") == 0) {
- ibutton_cli_read(cli);
- } else if(string_cmp_str(cmd, "write") == 0) {
- ibutton_cli_write(cli, args);
- } else if(string_cmp_str(cmd, "emulate") == 0) {
- ibutton_cli_emulate(cli, args);
- } else {
- ibutton_cli_print_usage();
- }
- string_clear(cmd);
- }
- void onewire_cli_print_usage() {
- printf("Usage:\r\n");
- printf("onewire search\r\n");
- };
- void onewire_cli_search(Cli* cli) {
- OneWireMaster onewire(&ibutton_gpio);
- uint8_t address[8];
- bool done = false;
- printf("Search started\r\n");
- onewire.start();
- furi_hal_power_enable_otg();
- while(!done) {
- if(onewire.search(address, true) != 1) {
- printf("Search finished\r\n");
- onewire.reset_search();
- done = true;
- } else {
- printf("Found: ");
- for(uint8_t i = 0; i < 8; i++) {
- printf("%02X", address[i]);
- }
- printf("\r\n");
- }
- delay(100);
- }
- furi_hal_power_disable_otg();
- onewire.stop();
- }
- void onewire_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);
- onewire_cli_print_usage();
- return;
- }
- if(string_cmp_str(cmd, "search") == 0) {
- onewire_cli_search(cli);
- }
- string_clear(cmd);
- }
|