2022-03-29 13:01:56 +00:00
|
|
|
#include <furi.h>
|
|
|
|
#include <furi_hal.h>
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
#include <cli/cli.h>
|
2023-03-02 13:23:33 +00:00
|
|
|
#include <toolbox/args.h>
|
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
#include <one_wire/one_wire_host.h>
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
#include <one_wire/ibutton/ibutton_key.h>
|
|
|
|
#include <one_wire/ibutton/ibutton_worker.h>
|
|
|
|
#include <one_wire/ibutton/ibutton_protocols.h>
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static void ibutton_cli(Cli* cli, FuriString* args, void* context);
|
|
|
|
static void onewire_cli(Cli* cli, FuriString* args, void* context);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
// app cli function
|
|
|
|
void ibutton_on_system_start() {
|
|
|
|
#ifdef SRV_CLI
|
2022-07-26 12:21:51 +00:00
|
|
|
Cli* cli = furi_record_open(RECORD_CLI);
|
2022-03-29 13:01:56 +00:00
|
|
|
cli_add_command(cli, "ikey", CliCommandFlagDefault, ibutton_cli, cli);
|
|
|
|
cli_add_command(cli, "onewire", CliCommandFlagDefault, onewire_cli, cli);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_CLI);
|
2022-04-13 20:50:25 +00:00
|
|
|
#else
|
|
|
|
UNUSED(ibutton_cli);
|
|
|
|
UNUSED(onewire_cli);
|
2022-03-29 13:01:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
static void ibutton_cli_print_usage() {
|
2022-03-29 13:01:56 +00:00
|
|
|
printf("Usage:\r\n");
|
|
|
|
printf("ikey read\r\n");
|
|
|
|
printf("ikey emulate <key_type> <key_data>\r\n");
|
|
|
|
printf("ikey write Dallas <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");
|
2022-03-30 11:54:29 +00:00
|
|
|
printf("\tMetakom (4 bytes key_data), must contain correct parity\r\n");
|
2022-03-29 13:01:56 +00:00
|
|
|
printf("\t<key_data> are hex-formatted\r\n");
|
|
|
|
};
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
static bool ibutton_cli_parse_key(iButtonProtocols* protocols, iButtonKey* key, FuriString* args) {
|
2022-03-29 13:01:56 +00:00
|
|
|
bool result = false;
|
2023-03-02 13:23:33 +00:00
|
|
|
FuriString* name = furi_string_alloc();
|
|
|
|
|
|
|
|
do {
|
|
|
|
// Read protocol name
|
|
|
|
if(!args_read_string_and_trim(args, name)) break;
|
|
|
|
|
|
|
|
// Make the protocol name uppercase
|
|
|
|
const char first = furi_string_get_char(name, 0);
|
|
|
|
furi_string_set_char(name, 0, toupper((int)first));
|
|
|
|
|
|
|
|
const iButtonProtocolId id =
|
|
|
|
ibutton_protocols_get_id_by_name(protocols, furi_string_get_cstr(name));
|
|
|
|
if(id == iButtonProtocolIdInvalid) break;
|
|
|
|
|
|
|
|
ibutton_key_set_protocol_id(key, id);
|
|
|
|
|
|
|
|
// Get the data pointer
|
|
|
|
iButtonEditableData data;
|
|
|
|
ibutton_protocols_get_editable_data(protocols, key, &data);
|
|
|
|
|
|
|
|
// Read data
|
|
|
|
if(!args_read_hex_bytes(args, data.ptr, data.size)) break;
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
result = true;
|
2023-03-02 13:23:33 +00:00
|
|
|
} while(false);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
furi_string_free(name);
|
2022-03-29 13:01:56 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
static void ibutton_cli_print_key(iButtonProtocols* protocols, iButtonKey* key) {
|
|
|
|
const char* name = ibutton_protocols_get_name(protocols, ibutton_key_get_protocol_id(key));
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
if(strncmp(name, "DS", 2) == 0) {
|
|
|
|
name = "Dallas";
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s ", name);
|
|
|
|
|
|
|
|
iButtonEditableData data;
|
|
|
|
ibutton_protocols_get_editable_data(protocols, key, &data);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < data.size; i++) {
|
|
|
|
printf("%02X", data.ptr[i]);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#define EVENT_FLAG_IBUTTON_COMPLETE (1 << 0)
|
|
|
|
|
|
|
|
static void ibutton_cli_worker_read_cb(void* context) {
|
2022-04-01 12:21:31 +00:00
|
|
|
furi_assert(context);
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriEventFlag* event = context;
|
|
|
|
furi_event_flag_set(event, EVENT_FLAG_IBUTTON_COMPLETE);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
static void ibutton_cli_read(Cli* cli) {
|
|
|
|
iButtonProtocols* protocols = ibutton_protocols_alloc();
|
|
|
|
iButtonWorker* worker = ibutton_worker_alloc(protocols);
|
|
|
|
iButtonKey* key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(protocols));
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriEventFlag* event = furi_event_flag_alloc();
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
ibutton_worker_start_thread(worker);
|
|
|
|
ibutton_worker_read_set_callback(worker, ibutton_cli_worker_read_cb, event);
|
|
|
|
|
2022-03-30 11:54:29 +00:00
|
|
|
printf("Reading iButton...\r\nPress Ctrl+C to abort\r\n");
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_worker_read_start(worker, key);
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
while(true) {
|
2022-07-20 10:56:33 +00:00
|
|
|
uint32_t flags =
|
|
|
|
furi_event_flag_wait(event, EVENT_FLAG_IBUTTON_COMPLETE, FuriFlagWaitAny, 100);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
if(flags & EVENT_FLAG_IBUTTON_COMPLETE) {
|
2023-03-02 13:23:33 +00:00
|
|
|
ibutton_cli_print_key(protocols, key);
|
2022-03-29 13:01:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cli_cmd_interrupt_received(cli)) break;
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
ibutton_worker_stop(worker);
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_worker_stop_thread(worker);
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_key_free(key);
|
2023-03-02 13:23:33 +00:00
|
|
|
ibutton_worker_free(worker);
|
|
|
|
ibutton_protocols_free(protocols);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_free(event);
|
2022-03-29 13:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriEventFlag* event;
|
2022-03-29 13:01:56 +00:00
|
|
|
iButtonWorkerWriteResult result;
|
|
|
|
} iButtonWriteContext;
|
|
|
|
|
|
|
|
static void ibutton_cli_worker_write_cb(void* context, iButtonWorkerWriteResult result) {
|
2022-04-01 12:21:31 +00:00
|
|
|
furi_assert(context);
|
2022-03-29 13:01:56 +00:00
|
|
|
iButtonWriteContext* write_context = (iButtonWriteContext*)context;
|
|
|
|
write_context->result = result;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_set(write_context->event, EVENT_FLAG_IBUTTON_COMPLETE);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
void ibutton_cli_write(Cli* cli, FuriString* args) {
|
2023-03-02 13:23:33 +00:00
|
|
|
iButtonProtocols* protocols = ibutton_protocols_alloc();
|
|
|
|
iButtonWorker* worker = ibutton_worker_alloc(protocols);
|
|
|
|
iButtonKey* key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(protocols));
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
iButtonWriteContext write_context;
|
2022-07-20 10:56:33 +00:00
|
|
|
write_context.event = furi_event_flag_alloc();
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
ibutton_worker_start_thread(worker);
|
|
|
|
ibutton_worker_write_set_callback(worker, ibutton_cli_worker_write_cb, &write_context);
|
|
|
|
|
|
|
|
do {
|
2023-03-02 13:23:33 +00:00
|
|
|
if(!ibutton_cli_parse_key(protocols, key, args)) {
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_cli_print_usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
if(!(ibutton_protocols_get_features(protocols, ibutton_key_get_protocol_id(key)) &
|
|
|
|
iButtonProtocolFeatureWriteBlank)) {
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_cli_print_usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Writing key ");
|
2023-03-02 13:23:33 +00:00
|
|
|
ibutton_cli_print_key(protocols, key);
|
2022-03-29 13:01:56 +00:00
|
|
|
printf("Press Ctrl+C to abort\r\n");
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
ibutton_worker_write_blank_start(worker, key);
|
2022-03-29 13:01:56 +00:00
|
|
|
while(true) {
|
2022-07-20 10:56:33 +00:00
|
|
|
uint32_t flags = furi_event_flag_wait(
|
|
|
|
write_context.event, EVENT_FLAG_IBUTTON_COMPLETE, FuriFlagWaitAny, 100);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
if(flags & EVENT_FLAG_IBUTTON_COMPLETE) {
|
|
|
|
if(write_context.result == iButtonWorkerWriteSameKey ||
|
|
|
|
write_context.result == iButtonWorkerWriteOK) {
|
|
|
|
printf("Write success\r\n");
|
|
|
|
break;
|
|
|
|
} else if(write_context.result == iButtonWorkerWriteCannotWrite) {
|
|
|
|
printf("Write fail\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cli_cmd_interrupt_received(cli)) break;
|
|
|
|
}
|
|
|
|
} while(false);
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
ibutton_worker_stop(worker);
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_worker_stop_thread(worker);
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_key_free(key);
|
2023-03-02 13:23:33 +00:00
|
|
|
ibutton_worker_free(worker);
|
|
|
|
ibutton_protocols_free(protocols);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_free(write_context.event);
|
2023-03-02 13:23:33 +00:00
|
|
|
}
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
void ibutton_cli_emulate(Cli* cli, FuriString* args) {
|
2023-03-02 13:23:33 +00:00
|
|
|
iButtonProtocols* protocols = ibutton_protocols_alloc();
|
|
|
|
iButtonWorker* worker = ibutton_worker_alloc(protocols);
|
|
|
|
iButtonKey* key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(protocols));
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
ibutton_worker_start_thread(worker);
|
|
|
|
|
|
|
|
do {
|
2023-03-02 13:23:33 +00:00
|
|
|
if(!ibutton_cli_parse_key(protocols, key, args)) {
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_cli_print_usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Emulating key ");
|
2023-03-02 13:23:33 +00:00
|
|
|
ibutton_cli_print_key(protocols, key);
|
2022-03-29 13:01:56 +00:00
|
|
|
printf("Press Ctrl+C to abort\r\n");
|
|
|
|
|
|
|
|
ibutton_worker_emulate_start(worker, key);
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
while(!cli_cmd_interrupt_received(cli)) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_ms(100);
|
2022-03-29 13:01:56 +00:00
|
|
|
};
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
} while(false);
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
ibutton_worker_stop(worker);
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_worker_stop_thread(worker);
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_key_free(key);
|
2023-03-02 13:23:33 +00:00
|
|
|
ibutton_worker_free(worker);
|
|
|
|
ibutton_protocols_free(protocols);
|
2022-03-29 13:01:56 +00:00
|
|
|
};
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
void ibutton_cli(Cli* cli, FuriString* args, void* context) {
|
|
|
|
UNUSED(cli);
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(context);
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* cmd;
|
|
|
|
cmd = furi_string_alloc();
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
if(!args_read_string_and_trim(args, cmd)) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(cmd);
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_cli_print_usage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_cmp_str(cmd, "read") == 0) {
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_cli_read(cli);
|
2022-10-05 15:15:23 +00:00
|
|
|
} else if(furi_string_cmp_str(cmd, "write") == 0) {
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_cli_write(cli, args);
|
2022-10-05 15:15:23 +00:00
|
|
|
} else if(furi_string_cmp_str(cmd, "emulate") == 0) {
|
2022-03-29 13:01:56 +00:00
|
|
|
ibutton_cli_emulate(cli, args);
|
|
|
|
} else {
|
|
|
|
ibutton_cli_print_usage();
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(cmd);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
static void onewire_cli_print_usage() {
|
2022-03-29 13:01:56 +00:00
|
|
|
printf("Usage:\r\n");
|
|
|
|
printf("onewire search\r\n");
|
|
|
|
};
|
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
static void onewire_cli_search(Cli* cli) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(cli);
|
2023-02-08 05:40:44 +00:00
|
|
|
OneWireHost* onewire = onewire_host_alloc(&ibutton_gpio);
|
2022-03-29 13:01:56 +00:00
|
|
|
uint8_t address[8];
|
|
|
|
bool done = false;
|
|
|
|
|
|
|
|
printf("Search started\r\n");
|
|
|
|
|
|
|
|
onewire_host_start(onewire);
|
|
|
|
furi_hal_power_enable_otg();
|
|
|
|
|
|
|
|
while(!done) {
|
2023-03-02 13:23:33 +00:00
|
|
|
if(onewire_host_search(onewire, address, OneWireHostSearchModeNormal) != 1) {
|
2022-03-29 13:01:56 +00:00
|
|
|
printf("Search finished\r\n");
|
|
|
|
onewire_host_reset_search(onewire);
|
|
|
|
done = true;
|
|
|
|
} else {
|
|
|
|
printf("Found: ");
|
|
|
|
for(uint8_t i = 0; i < 8; i++) {
|
|
|
|
printf("%02X", address[i]);
|
|
|
|
}
|
|
|
|
printf("\r\n");
|
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_ms(100);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
furi_hal_power_disable_otg();
|
|
|
|
onewire_host_free(onewire);
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
void onewire_cli(Cli* cli, FuriString* args, void* context) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(context);
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* cmd;
|
|
|
|
cmd = furi_string_alloc();
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
if(!args_read_string_and_trim(args, cmd)) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(cmd);
|
2022-03-29 13:01:56 +00:00
|
|
|
onewire_cli_print_usage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_cmp_str(cmd, "search") == 0) {
|
2022-03-29 13:01:56 +00:00
|
|
|
onewire_cli_search(cli);
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(cmd);
|
2022-05-06 13:37:10 +00:00
|
|
|
}
|