Skip to content

Commit

Permalink
Merge pull request #12 from rdefeo/ibutton
Browse files Browse the repository at this point in the history
Add iButton support
  • Loading branch information
rdefeo authored Sep 10, 2024
2 parents 2fe1ce7 + 99287fd commit c927f76
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 5 deletions.
17 changes: 17 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CompileFlags:
Add:
- -Wno-unknown-warning-option
- -Wno-format
Remove:
- -mword-relocations

Diagnostics:
ClangTidy:
FastCheckFilter: None

---

If:
PathMatch: .*\.h
Diagnostics:
UnusedIncludes: None
2 changes: 2 additions & 0 deletions actions/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ void action_tx(void* context, Item* item, FuriString* error) {
action_rfid_tx(context, item->path, error);
} else if(!strcmp(item->ext, ".nfc")) {
action_nfc_tx(context, item->path, error);
} else if(!strcmp(item->ext, ".ibtn")) {
action_ibutton_tx(context, item->path, error);
} else if(!strcmp(item->ext, ".qpl")) {
action_qpl_tx(context, item->path, error);
} else {
Expand Down
1 change: 1 addition & 0 deletions actions/action_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ void action_subghz_tx(void* context, const FuriString* action_path, FuriString*
void action_rfid_tx(void* context, const FuriString* action_path, FuriString* error);
void action_ir_tx(void* context, const FuriString* action_path, FuriString* error);
void action_nfc_tx(void* context, const FuriString* action_path, FuriString* error);
void action_ibutton_tx(void* context, const FuriString* action_path, FuriString* error);
void action_qpl_tx(void* context, const FuriString* action_path, FuriString* error);
48 changes: 48 additions & 0 deletions actions/action_ibutton.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Methods for iButton transmission

#include <furi.h>
#include <furi_hal.h>

#include <ibutton/ibutton_key.h>
#include <ibutton/ibutton_worker.h>
#include <ibutton/ibutton_protocols.h>

#include "action_i.h"
#include "quac.h"

void action_ibutton_tx(void* context, const FuriString* action_path, FuriString* error) {
App* app = context;
const char* cpath = furi_string_get_cstr(action_path);

FURI_LOG_I(TAG, "iButton: Tx %s", cpath);

iButtonProtocols* protocols = ibutton_protocols_alloc();
iButtonKey* key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(protocols));

const bool success = ibutton_protocols_load(protocols, key, cpath);
if(!success) {
FURI_LOG_E(TAG, "Error loading iButton file %s", cpath);
ACTION_SET_ERROR("Error loading %s", cpath);
} else {
FURI_LOG_I(TAG, "iButton: Starting...");
iButtonWorker* worker = ibutton_worker_alloc(protocols);
ibutton_worker_start_thread(worker);

ibutton_worker_emulate_start(worker, key);

int16_t time_ms = app->settings.ibutton_duration;
const int16_t interval_ms = 100;
while(time_ms > 0) {
furi_delay_ms(interval_ms);
time_ms -= interval_ms;
}

FURI_LOG_I(TAG, "iButton: Done");
ibutton_worker_stop(worker);
ibutton_worker_stop_thread(worker);
ibutton_worker_free(worker);
}

ibutton_key_free(key);
ibutton_protocols_free(protocols);
}
17 changes: 14 additions & 3 deletions actions/action_qpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
void action_qpl_tx(void* context, const FuriString* action_path, FuriString* error) {
App* app = context;

// Save the current RFID and NFC Durations, in case the are changed during playback
// Save the current durations, in case the are changed during playback
uint32_t orig_rfid_duration = app->settings.rfid_duration;
uint32_t orig_nfc_duration = app->settings.nfc_duration;
uint32_t orig_ibutton_duration = app->settings.ibutton_duration;

FuriString* buffer;
buffer = furi_string_alloc();
Expand Down Expand Up @@ -84,7 +85,7 @@ void action_qpl_tx(void* context, const FuriString* action_path, FuriString* err
break;
}

// First token wasn't "pause", so maybe args_tmp is a .rfid filename followed
// First token wasn't "pause", so maybe args_tmp is a filename followed
// by a transmit duration in ms in buffer
// Note: Not using path_extract_extension since it expects to find slashes in the
// path, and thus won't work if we have a relative path file
Expand All @@ -109,6 +110,12 @@ void action_qpl_tx(void* context, const FuriString* action_path, FuriString* err
FURI_LOG_I(TAG, "NFC duration = %lu", nfc_duration);
app->settings.nfc_duration = nfc_duration;
}
} else if(!strcmp(ext, ".ibtn")) {
uint32_t ibutton_duration = 0;
if(sscanf(furi_string_get_cstr(buffer), "%lu", &ibutton_duration) == 1) {
FURI_LOG_I(TAG, "iButton duration = %lu", ibutton_duration);
app->settings.ibutton_duration = ibutton_duration;
}
}

} while(false);
Expand Down Expand Up @@ -145,6 +152,10 @@ void action_qpl_tx(void* context, const FuriString* action_path, FuriString* err
action_nfc_tx(context, buffer, error);
// Reset our default duration back - in case it was changed during playback
app->settings.nfc_duration = orig_nfc_duration;
} else if(!strcmp(ext, ".ibtn")) {
action_ibutton_tx(context, buffer, error);
// Reset our default duration back - in case it was changed during playback
app->settings.ibutton_duration = orig_ibutton_duration;
} else if(!strcmp(ext, ".qpl")) {
ACTION_SET_ERROR("Playlist: Can't call playlist from playlist");
} else {
Expand All @@ -170,4 +181,4 @@ void action_qpl_tx(void* context, const FuriString* action_path, FuriString* err
furi_string_free(buffer);
file_stream_close(file);
stream_free(file);
}
}
Binary file added images/iButton_10px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions item.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ ItemType item_get_item_type_from_extension(const char* ext) {
type = Item_IR;
} else if(!strcmp(ext, ".nfc")) {
type = Item_NFC;
} else if(!strcmp(ext, ".ibtn")) {
type = Item_iButton;
} else if(!strcmp(ext, ".qpl")) {
type = Item_Playlist;
}
Expand Down
5 changes: 3 additions & 2 deletions item.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// Max length of a filename, final path element only
#define MAX_NAME_LEN 64
#define MAX_EXT_LEN 6
#define MAX_EXT_LEN 6

/** Defines an individual item action or item group. Each object contains
* the relevant file and type information needed to both render correctly
Expand All @@ -16,6 +16,7 @@ typedef enum {
Item_RFID,
Item_IR,
Item_NFC,
Item_iButton,
Item_Playlist,
Item_Group,
Item_Settings,
Expand Down Expand Up @@ -62,4 +63,4 @@ void item_prettify_name(FuriString* name);
/** Return the ItemType enum for the given extension
* @param ext File extension
*/
ItemType item_get_item_type_from_extension(const char* ext);
ItemType item_get_item_type_from_extension(const char* ext);
1 change: 1 addition & 0 deletions quac.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ typedef struct App {
bool show_headers; // Defaults to True
uint32_t rfid_duration; // Defaults to 2500 ms
uint32_t nfc_duration; // Defaults to 1000 ms
uint32_t ibutton_duration; // Defaults to 1000 ms
uint32_t subghz_repeat; // Defaults to 10, just like the CLI
bool subghz_use_ext_antenna; // Defaults to False
bool ir_use_ext_module; // Defaults to False
Expand Down
12 changes: 12 additions & 0 deletions quac_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void quac_set_default_settings(App* app) {
app->settings.show_headers = true;
app->settings.rfid_duration = 2500;
app->settings.nfc_duration = 1000;
app->settings.ibutton_duration = 1000;
app->settings.subghz_repeat = 10;
app->settings.subghz_use_ext_antenna = false;
app->settings.ir_use_ext_module = false;
Expand Down Expand Up @@ -83,6 +84,12 @@ void quac_load_settings(App* app) {
app->settings.nfc_duration = temp_data32;
}

if(!flipper_format_read_uint32(fff_settings, "iButton Duration", &temp_data32, 1)) {
FURI_LOG_W(TAG, "SETTINGS: Missing 'iButton Duration'");
} else {
app->settings.ibutton_duration = temp_data32;
}

if(!flipper_format_read_uint32(fff_settings, "SubGHz Repeat", &temp_data32, 1)) {
FURI_LOG_W(TAG, "SETTINGS: Missing 'SubGHz Repeat'");
} else {
Expand Down Expand Up @@ -158,6 +165,11 @@ void quac_save_settings(App* app) {
FURI_LOG_E(TAG, "SETTINGS: Failed to write 'NFC Duration'");
break;
}
if(!flipper_format_write_uint32(
fff_settings, "iButton Duration", &app->settings.ibutton_duration, 1)) {
FURI_LOG_E(TAG, "SETTINGS: Failed to write 'iButton Duration'");
break;
}
if(!flipper_format_write_uint32(
fff_settings, "SubGHz Repeat", &app->settings.subghz_repeat, 1)) {
FURI_LOG_E(TAG, "SETTINGS: Failed to write 'SubGHz Repeat'");
Expand Down
1 change: 1 addition & 0 deletions scenes/scene_items.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static const ActionMenuItemType ItemToMenuItem[] = {
[Item_RFID] = ActionMenuItemTypeRFID,
[Item_IR] = ActionMenuItemTypeIR,
[Item_NFC] = ActionMenuItemTypeNFC,
[Item_iButton] = ActionMenuItemTypeiButton,
[Item_Playlist] = ActionMenuItemTypePlaylist,
[Item_Group] = ActionMenuItemTypeGroup,
[Item_Settings] = ActionMenuItemTypeSettings,
Expand Down
14 changes: 14 additions & 0 deletions scenes/scene_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ static void scene_settings_nfc_duration_changed(VariableItem* item) {
app->settings.nfc_duration = duration_value[index];
}

static void scene_settings_ibutton_duration_changed(VariableItem* item) {
App* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, duration_text[index]);
app->settings.ibutton_duration = duration_value[index];
}

static void scene_settings_subghz_repeat_changed(VariableItem* item) {
App* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
Expand Down Expand Up @@ -171,6 +178,13 @@ void scene_settings_on_enter(void* context) {
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, duration_text[value_index]);

item = variable_item_list_add(
vil, "iButton Duration", V_DURATION_COUNT, scene_settings_ibutton_duration_changed, app);
value_index =
value_index_uint32(app->settings.ibutton_duration, duration_value, V_DURATION_COUNT);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, duration_text[value_index]);

item = variable_item_list_add(
vil, "SubGHz Repeat", V_REPEAT_COUNT, scene_settings_subghz_repeat_changed, app);
value_index = value_index_uint32(app->settings.subghz_repeat, repeat_value, V_REPEAT_COUNT);
Expand Down
1 change: 1 addition & 0 deletions views/action_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static const Icon* ActionMenuIcons[] = {
[ActionMenuItemTypeRFID] = &I_RFID_10px,
[ActionMenuItemTypeIR] = &I_IR_10px,
[ActionMenuItemTypeNFC] = &I_NFC_10px,
[ActionMenuItemTypeiButton] = &I_iButton_10px,
[ActionMenuItemTypePlaylist] = &I_Playlist_10px,
[ActionMenuItemTypeGroup] = &I_Directory_10px,
[ActionMenuItemTypeSettings] = &I_Settings_10px,
Expand Down
1 change: 1 addition & 0 deletions views/action_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typedef enum {
ActionMenuItemTypeRFID,
ActionMenuItemTypeIR,
ActionMenuItemTypeNFC,
ActionMenuItemTypeiButton,
ActionMenuItemTypePlaylist,
ActionMenuItemTypeGroup,
ActionMenuItemTypeSettings,
Expand Down

0 comments on commit c927f76

Please sign in to comment.