[FL-2627] Flipper applications: SDK, build and debug system (#1387)

* Added support for running applications from SD card (FAPs - Flipper Application Packages)
* Added plugin_dist target for fbt to build FAPs
* All apps of type FlipperAppType.EXTERNAL and FlipperAppType.PLUGIN are built as FAPs by default
* Updated VSCode configuration for new fbt features - re-deploy stock configuration to use them
* Added debugging support for FAPs with fbt debug & VSCode
* Added public firmware API with automated versioning

Co-authored-by: hedger <hedger@users.noreply.github.com>
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG
2022-09-15 02:11:38 +10:00
committed by Aleksandr Kutuzov
parent 0f6f9ad52e
commit b9a766d909
895 changed files with 8862 additions and 1465 deletions

View File

@@ -0,0 +1,17 @@
App(
appid="main_apps",
name="Basic applications for main menu",
apptype=FlipperAppType.METAPACKAGE,
provides=[
"gpio",
"ibutton",
"infrared",
"lfrfid",
"nfc",
"subghz",
"bad_usb",
"u2f",
"fap_loader",
"archive",
],
)

View File

@@ -0,0 +1,11 @@
App(
appid="archive",
name="Archive",
apptype=FlipperAppType.ARCHIVE,
entry_point="archive_app",
cdefines=["APP_ARCHIVE"],
requires=["gui"],
stack_size=4 * 1024,
icon="A_FileManager_14",
order=0,
)

View File

@@ -0,0 +1,79 @@
#include "archive_i.h"
#include "m-string.h"
bool archive_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
ArchiveApp* archive = (ArchiveApp*)context;
return scene_manager_handle_custom_event(archive->scene_manager, event);
}
bool archive_back_event_callback(void* context) {
furi_assert(context);
ArchiveApp* archive = (ArchiveApp*)context;
return scene_manager_handle_back_event(archive->scene_manager);
}
ArchiveApp* archive_alloc() {
ArchiveApp* archive = malloc(sizeof(ArchiveApp));
archive->gui = furi_record_open(RECORD_GUI);
archive->text_input = text_input_alloc();
string_init(archive->fav_move_str);
archive->view_dispatcher = view_dispatcher_alloc();
archive->scene_manager = scene_manager_alloc(&archive_scene_handlers, archive);
view_dispatcher_enable_queue(archive->view_dispatcher);
view_dispatcher_attach_to_gui(
archive->view_dispatcher, archive->gui, ViewDispatcherTypeFullscreen);
view_dispatcher_set_event_callback_context(archive->view_dispatcher, archive);
view_dispatcher_set_custom_event_callback(
archive->view_dispatcher, archive_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
archive->view_dispatcher, archive_back_event_callback);
archive->browser = browser_alloc();
view_dispatcher_add_view(
archive->view_dispatcher, ArchiveViewBrowser, archive_browser_get_view(archive->browser));
view_dispatcher_add_view(
archive->view_dispatcher, ArchiveViewTextInput, text_input_get_view(archive->text_input));
archive->widget = widget_alloc();
view_dispatcher_add_view(
archive->view_dispatcher, ArchiveViewWidget, widget_get_view(archive->widget));
return archive;
}
void archive_free(ArchiveApp* archive) {
furi_assert(archive);
view_dispatcher_remove_view(archive->view_dispatcher, ArchiveViewBrowser);
view_dispatcher_remove_view(archive->view_dispatcher, ArchiveViewTextInput);
view_dispatcher_remove_view(archive->view_dispatcher, ArchiveViewWidget);
widget_free(archive->widget);
view_dispatcher_free(archive->view_dispatcher);
scene_manager_free(archive->scene_manager);
browser_free(archive->browser);
string_clear(archive->fav_move_str);
text_input_free(archive->text_input);
furi_record_close(RECORD_GUI);
archive->gui = NULL;
free(archive);
}
int32_t archive_app(void* p) {
UNUSED(p);
ArchiveApp* archive = archive_alloc();
scene_manager_next_scene(archive->scene_manager, ArchiveAppSceneBrowser);
view_dispatcher_run(archive->view_dispatcher);
archive_free(archive);
return 0;
}

View File

@@ -0,0 +1,3 @@
#pragma once
typedef struct ArchiveApp ArchiveApp;

View File

@@ -0,0 +1,34 @@
#pragma once
#include "archive.h"
#include <stdint.h>
#include <furi.h>
#include <gui/gui_i.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/text_input.h>
#include <gui/modules/widget.h>
#include <loader/loader.h>
#include "views/archive_browser_view.h"
#include "scenes/archive_scene.h"
typedef enum {
ArchiveViewBrowser,
ArchiveViewTextInput,
ArchiveViewWidget,
ArchiveViewTotal,
} ArchiveViewEnum;
struct ArchiveApp {
Gui* gui;
ViewDispatcher* view_dispatcher;
SceneManager* scene_manager;
ArchiveBrowserView* browser;
TextInput* text_input;
Widget* widget;
FuriPubSubSubscription* loader_stop_subscription;
string_t fav_move_str;
char text_store[MAX_NAME_LEN];
char file_extension[MAX_EXT_LEN + 1];
};

View File

@@ -0,0 +1,84 @@
#include "archive_files.h"
#include "archive_apps.h"
#include "archive_browser.h"
static const char* known_apps[] = {
[ArchiveAppTypeU2f] = "u2f",
};
ArchiveAppTypeEnum archive_get_app_type(const char* path) {
const char* app_name = strchr(path, ':');
if(app_name == NULL) {
return ArchiveAppTypeUnknown;
}
app_name++;
for(size_t i = 0; i < COUNT_OF(known_apps); i++) {
if(strncmp(app_name, known_apps[i], strlen(known_apps[i])) == 0) {
return i;
}
}
return ArchiveAppTypeUnknown;
}
bool archive_app_is_available(void* context, const char* path) {
UNUSED(context);
furi_assert(path);
ArchiveAppTypeEnum app = archive_get_app_type(path);
if(app == ArchiveAppTypeU2f) {
bool file_exists = false;
Storage* storage = furi_record_open(RECORD_STORAGE);
if(storage_file_exists(storage, ANY_PATH("u2f/key.u2f"))) {
file_exists = storage_file_exists(storage, ANY_PATH("u2f/cnt.u2f"));
}
furi_record_close(RECORD_STORAGE);
return file_exists;
} else {
return false;
}
}
bool archive_app_read_dir(void* context, const char* path) {
furi_assert(context);
furi_assert(path);
ArchiveBrowserView* browser = context;
archive_file_array_rm_all(browser);
ArchiveAppTypeEnum app = archive_get_app_type(path);
if(app == ArchiveAppTypeU2f) {
archive_add_app_item(browser, "/app:u2f/U2F Token");
return true;
} else {
return false;
}
}
void archive_app_delete_file(void* context, const char* path) {
furi_assert(context);
furi_assert(path);
ArchiveBrowserView* browser = context;
ArchiveAppTypeEnum app = archive_get_app_type(path);
bool res = false;
if(app == ArchiveAppTypeU2f) {
Storage* fs_api = furi_record_open(RECORD_STORAGE);
res = (storage_common_remove(fs_api, ANY_PATH("u2f/key.u2f")) == FSE_OK);
res |= (storage_common_remove(fs_api, ANY_PATH("u2f/cnt.u2f")) == FSE_OK);
furi_record_close(RECORD_STORAGE);
if(archive_is_favorite("/app:u2f/U2F Token")) {
archive_favorites_delete("/app:u2f/U2F Token");
}
}
if(res) {
archive_file_array_rm_selected(browser);
}
}

View File

@@ -0,0 +1,21 @@
#pragma once
typedef enum {
ArchiveAppTypeU2f,
ArchiveAppTypeUnknown,
ArchiveAppsTotal,
} ArchiveAppTypeEnum;
static const ArchiveFileTypeEnum app_file_types[] = {
[ArchiveAppTypeU2f] = ArchiveFileTypeU2f,
[ArchiveAppTypeUnknown] = ArchiveFileTypeUnknown,
};
static inline ArchiveFileTypeEnum archive_get_app_filetype(ArchiveAppTypeEnum app) {
return app_file_types[app];
}
ArchiveAppTypeEnum archive_get_app_type(const char* path);
bool archive_app_is_available(void* context, const char* path);
bool archive_app_read_dir(void* context, const char* path);
void archive_app_delete_file(void* context, const char* path);

View File

@@ -0,0 +1,506 @@
#include <archive/views/archive_browser_view.h>
#include "archive_files.h"
#include "archive_apps.h"
#include "archive_browser.h"
#include <core/common_defines.h>
#include <core/log.h>
#include "gui/modules/file_browser_worker.h"
#include "m-string.h"
#include <math.h>
static void
archive_folder_open_cb(void* context, uint32_t item_cnt, int32_t file_idx, bool is_root) {
furi_assert(context);
ArchiveBrowserView* browser = (ArchiveBrowserView*)context;
int32_t load_offset = 0;
browser->is_root = is_root;
ArchiveTabEnum tab = archive_get_tab(browser);
if((item_cnt == 0) && (archive_is_home(browser)) && (tab != ArchiveTabBrowser)) {
archive_switch_tab(browser, browser->last_tab_switch_dir);
} else if(!string_start_with_str_p(browser->path, "/app:")) {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_reset(model->files);
model->item_cnt = item_cnt;
model->item_idx = (file_idx > 0) ? file_idx : 0;
load_offset =
CLAMP(model->item_idx - FILE_LIST_BUF_LEN / 2, (int32_t)model->item_cnt, 0);
model->array_offset = 0;
model->list_offset = 0;
model->list_loading = true;
model->folder_loading = false;
return false;
});
archive_update_offset(browser);
file_browser_worker_load(browser->worker, load_offset, FILE_LIST_BUF_LEN);
}
}
static void archive_list_load_cb(void* context, uint32_t list_load_offset) {
furi_assert(context);
ArchiveBrowserView* browser = (ArchiveBrowserView*)context;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_reset(model->files);
model->array_offset = list_load_offset;
return false;
});
}
static void archive_list_item_cb(void* context, string_t item_path, bool is_folder, bool is_last) {
furi_assert(context);
ArchiveBrowserView* browser = (ArchiveBrowserView*)context;
if(!is_last) {
archive_add_file_item(browser, is_folder, string_get_cstr(item_path));
} else {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->list_loading = false;
return true;
});
}
}
static void archive_long_load_cb(void* context) {
furi_assert(context);
ArchiveBrowserView* browser = (ArchiveBrowserView*)context;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->folder_loading = true;
return true;
});
}
static void archive_file_browser_set_path(
ArchiveBrowserView* browser,
string_t path,
const char* filter_ext,
bool skip_assets) {
furi_assert(browser);
if(!browser->worker_running) {
browser->worker = file_browser_worker_alloc(path, filter_ext, skip_assets);
file_browser_worker_set_callback_context(browser->worker, browser);
file_browser_worker_set_folder_callback(browser->worker, archive_folder_open_cb);
file_browser_worker_set_list_callback(browser->worker, archive_list_load_cb);
file_browser_worker_set_item_callback(browser->worker, archive_list_item_cb);
file_browser_worker_set_long_load_callback(browser->worker, archive_long_load_cb);
browser->worker_running = true;
} else {
furi_assert(browser->worker);
file_browser_worker_set_config(browser->worker, path, filter_ext, skip_assets);
}
}
bool archive_is_item_in_array(ArchiveBrowserViewModel* model, uint32_t idx) {
size_t array_size = files_array_size(model->files);
if((idx >= (uint32_t)model->array_offset + array_size) ||
(idx < (uint32_t)model->array_offset)) {
return false;
}
return true;
}
void archive_update_offset(ArchiveBrowserView* browser) {
furi_assert(browser);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
uint16_t bounds = model->item_cnt > 3 ? 2 : model->item_cnt;
if((model->item_cnt > 3u) && (model->item_idx >= ((int32_t)model->item_cnt - 1))) {
model->list_offset = model->item_idx - 3;
} else if(model->list_offset < model->item_idx - bounds) {
model->list_offset =
CLAMP(model->item_idx - 2, (int32_t)model->item_cnt - bounds, 0);
} else if(model->list_offset > model->item_idx - bounds) {
model->list_offset =
CLAMP(model->item_idx - 1, (int32_t)model->item_cnt - bounds, 0);
}
return true;
});
}
void archive_update_focus(ArchiveBrowserView* browser, const char* target) {
furi_assert(browser);
furi_assert(target);
archive_get_items(browser, string_get_cstr(browser->path));
if(!archive_file_get_array_size(browser) && archive_is_home(browser)) {
archive_switch_tab(browser, TAB_RIGHT);
} else {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
uint16_t idx = 0;
while(idx < files_array_size(model->files)) {
ArchiveFile_t* current = files_array_get(model->files, idx);
if(!string_search(current->path, target)) {
model->item_idx = idx + model->array_offset;
break;
}
++idx;
}
return false;
});
archive_update_offset(browser);
}
}
size_t archive_file_get_array_size(ArchiveBrowserView* browser) {
furi_assert(browser);
uint16_t size = 0;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
size = files_array_size(model->files);
return false;
});
return size;
}
void archive_set_item_count(ArchiveBrowserView* browser, uint32_t count) {
furi_assert(browser);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->item_cnt = count;
model->item_idx = CLAMP(model->item_idx, (int32_t)model->item_cnt - 1, 0);
return false;
});
archive_update_offset(browser);
}
void archive_file_array_rm_selected(ArchiveBrowserView* browser) {
furi_assert(browser);
uint32_t items_cnt = 0;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_remove_v(
model->files,
model->item_idx - model->array_offset,
model->item_idx - model->array_offset + 1);
model->item_cnt--;
model->item_idx = CLAMP(model->item_idx, (int32_t)model->item_cnt - 1, 0);
items_cnt = model->item_cnt;
return false;
});
if((items_cnt == 0) && (archive_is_home(browser))) {
archive_switch_tab(browser, TAB_RIGHT);
}
archive_update_offset(browser);
}
void archive_file_array_swap(ArchiveBrowserView* browser, int8_t dir) {
furi_assert(browser);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
ArchiveFile_t temp;
size_t array_size = files_array_size(model->files) - 1;
uint8_t swap_idx = CLAMP((size_t)(model->item_idx + dir), array_size, 0u);
if(model->item_idx == 0 && dir < 0) {
ArchiveFile_t_init(&temp);
files_array_pop_at(&temp, model->files, array_size);
files_array_push_at(model->files, model->item_idx, temp);
ArchiveFile_t_clear(&temp);
} else if(((uint32_t)model->item_idx == array_size) && (dir > 0)) {
ArchiveFile_t_init(&temp);
files_array_pop_at(&temp, model->files, 0);
files_array_push_at(model->files, array_size, temp);
ArchiveFile_t_clear(&temp);
} else {
files_array_swap_at(model->files, model->item_idx, swap_idx);
}
return false;
});
}
void archive_file_array_rm_all(ArchiveBrowserView* browser) {
furi_assert(browser);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_reset(model->files);
return false;
});
}
void archive_file_array_load(ArchiveBrowserView* browser, int8_t dir) {
furi_assert(browser);
int32_t offset_new = 0;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
if(model->item_cnt > FILE_LIST_BUF_LEN) {
if(dir < 0) {
offset_new = model->item_idx - FILE_LIST_BUF_LEN / 4 * 3;
} else if(dir == 0) {
offset_new = model->item_idx - FILE_LIST_BUF_LEN / 4 * 2;
} else {
offset_new = model->item_idx - FILE_LIST_BUF_LEN / 4 * 1;
}
if(offset_new > 0) {
offset_new =
CLAMP(offset_new, (int32_t)model->item_cnt - FILE_LIST_BUF_LEN, 0);
} else {
offset_new = 0;
}
}
return false;
});
file_browser_worker_load(browser->worker, offset_new, FILE_LIST_BUF_LEN);
}
ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser) {
furi_assert(browser);
ArchiveFile_t* selected;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
selected = files_array_size(model->files) ?
files_array_get(model->files, model->item_idx - model->array_offset) :
NULL;
return false;
});
return selected;
}
ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx) {
furi_assert(browser);
ArchiveFile_t* selected;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
idx = CLAMP(idx - model->array_offset, files_array_size(model->files), 0u);
selected = files_array_size(model->files) ? files_array_get(model->files, idx) : NULL;
return false;
});
return selected;
}
ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser) {
furi_assert(browser);
ArchiveTabEnum tab_id;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
tab_id = model->tab_idx;
return false;
});
return tab_id;
}
bool archive_is_home(ArchiveBrowserView* browser) {
furi_assert(browser);
if(browser->is_root) {
return true;
}
const char* default_path = archive_get_default_path(archive_get_tab(browser));
return (string_cmp_str(browser->path, default_path) == 0);
}
const char* archive_get_name(ArchiveBrowserView* browser) {
ArchiveFile_t* selected = archive_get_current_file(browser);
return string_get_cstr(selected->path);
}
void archive_set_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
furi_assert(browser);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->tab_idx = tab;
return false;
});
}
void archive_add_app_item(ArchiveBrowserView* browser, const char* name) {
furi_assert(browser);
furi_assert(name);
ArchiveFile_t item;
ArchiveFile_t_init(&item);
string_set_str(item.path, name);
archive_set_file_type(&item, name, false, true);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_push_back(model->files, item);
model->item_cnt = files_array_size(model->files);
return false;
});
ArchiveFile_t_clear(&item);
}
void archive_add_file_item(ArchiveBrowserView* browser, bool is_folder, const char* name) {
furi_assert(browser);
furi_assert(name);
ArchiveFile_t item;
ArchiveFile_t_init(&item);
string_init_set_str(item.path, name);
archive_set_file_type(&item, string_get_cstr(browser->path), is_folder, false);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_push_back(model->files, item);
return false;
});
ArchiveFile_t_clear(&item);
}
void archive_show_file_menu(ArchiveBrowserView* browser, bool show) {
furi_assert(browser);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
if(show) {
if(archive_is_item_in_array(model, model->item_idx)) {
model->menu = true;
model->menu_idx = 0;
ArchiveFile_t* selected =
files_array_get(model->files, model->item_idx - model->array_offset);
selected->fav = archive_is_favorite("%s", string_get_cstr(selected->path));
}
} else {
model->menu = false;
model->menu_idx = 0;
}
return true;
});
}
void archive_favorites_move_mode(ArchiveBrowserView* browser, bool active) {
furi_assert(browser);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->move_fav = active;
return true;
});
}
static bool archive_is_dir_exists(string_t path) {
if(string_equal_str_p(path, STORAGE_ANY_PATH_PREFIX)) {
return true;
}
bool state = false;
FileInfo file_info;
Storage* storage = furi_record_open(RECORD_STORAGE);
if(storage_common_stat(storage, string_get_cstr(path), &file_info) == FSE_OK) {
if(file_info.flags & FSF_DIRECTORY) {
state = true;
}
}
furi_record_close(RECORD_STORAGE);
return state;
}
void archive_switch_tab(ArchiveBrowserView* browser, InputKey key) {
furi_assert(browser);
ArchiveTabEnum tab = archive_get_tab(browser);
browser->last_tab_switch_dir = key;
if(key == InputKeyLeft) {
tab = ((tab - 1) + ArchiveTabTotal) % ArchiveTabTotal;
} else {
tab = (tab + 1) % ArchiveTabTotal;
}
browser->is_root = true;
archive_set_tab(browser, tab);
string_set_str(browser->path, archive_get_default_path(tab));
bool tab_empty = true;
if(tab == ArchiveTabFavorites) {
if(archive_favorites_count(browser) > 0) {
tab_empty = false;
}
} else if(string_start_with_str_p(browser->path, "/app:")) {
char* app_name = strchr(string_get_cstr(browser->path), ':');
if(app_name != NULL) {
if(archive_app_is_available(browser, string_get_cstr(browser->path))) {
tab_empty = false;
}
}
} else {
tab = archive_get_tab(browser);
if(archive_is_dir_exists(browser->path)) {
bool skip_assets = (strcmp(archive_get_tab_ext(tab), "*") == 0) ? false : true;
archive_file_browser_set_path(
browser, browser->path, archive_get_tab_ext(tab), skip_assets);
tab_empty = false; // Empty check will be performed later
} else {
tab_empty = true;
}
}
if((tab_empty) && (tab != ArchiveTabBrowser)) {
archive_switch_tab(browser, key);
} else {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->item_idx = 0;
model->array_offset = 0;
return false;
});
archive_get_items(browser, string_get_cstr(browser->path));
archive_update_offset(browser);
}
}
void archive_enter_dir(ArchiveBrowserView* browser, string_t path) {
furi_assert(browser);
furi_assert(path);
int32_t idx_temp = 0;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
idx_temp = model->item_idx;
return false;
});
string_set(browser->path, path);
file_browser_worker_folder_enter(browser->worker, path, idx_temp);
}
void archive_leave_dir(ArchiveBrowserView* browser) {
furi_assert(browser);
file_browser_worker_folder_exit(browser->worker);
}
void archive_refresh_dir(ArchiveBrowserView* browser) {
furi_assert(browser);
int32_t idx_temp = 0;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
idx_temp = model->item_idx;
return false;
});
file_browser_worker_folder_refresh(browser->worker, idx_temp);
}

View File

@@ -0,0 +1,89 @@
#pragma once
#include "../archive_i.h"
#include <storage/storage.h>
#define TAB_RIGHT InputKeyRight // Default tab swith direction
#define TAB_DEFAULT ArchiveTabFavorites // Start tab
#define FILE_LIST_BUF_LEN 100
static const char* tab_default_paths[] = {
[ArchiveTabFavorites] = "/app:favorites",
[ArchiveTabIButton] = ANY_PATH("ibutton"),
[ArchiveTabNFC] = ANY_PATH("nfc"),
[ArchiveTabSubGhz] = ANY_PATH("subghz"),
[ArchiveTabLFRFID] = ANY_PATH("lfrfid"),
[ArchiveTabInfrared] = ANY_PATH("infrared"),
[ArchiveTabBadUsb] = ANY_PATH("badusb"),
[ArchiveTabU2f] = "/app:u2f",
[ArchiveTabBrowser] = STORAGE_ANY_PATH_PREFIX,
};
static const char* known_ext[] = {
[ArchiveFileTypeIButton] = ".ibtn",
[ArchiveFileTypeNFC] = ".nfc",
[ArchiveFileTypeSubGhz] = ".sub",
[ArchiveFileTypeLFRFID] = ".rfid",
[ArchiveFileTypeInfrared] = ".ir",
[ArchiveFileTypeBadUsb] = ".txt",
[ArchiveFileTypeU2f] = "?",
[ArchiveFileTypeUpdateManifest] = ".fuf",
[ArchiveFileTypeFolder] = "?",
[ArchiveFileTypeUnknown] = "*",
};
static const ArchiveFileTypeEnum known_type[] = {
[ArchiveTabFavorites] = ArchiveFileTypeUnknown,
[ArchiveTabIButton] = ArchiveFileTypeIButton,
[ArchiveTabNFC] = ArchiveFileTypeNFC,
[ArchiveTabSubGhz] = ArchiveFileTypeSubGhz,
[ArchiveTabLFRFID] = ArchiveFileTypeLFRFID,
[ArchiveTabInfrared] = ArchiveFileTypeInfrared,
[ArchiveTabBadUsb] = ArchiveFileTypeBadUsb,
[ArchiveTabU2f] = ArchiveFileTypeU2f,
[ArchiveTabBrowser] = ArchiveFileTypeUnknown,
};
static inline ArchiveFileTypeEnum archive_get_tab_filetype(ArchiveTabEnum tab) {
return known_type[tab];
}
static inline const char* archive_get_tab_ext(ArchiveTabEnum tab) {
return known_ext[archive_get_tab_filetype(tab)];
}
static inline const char* archive_get_default_path(ArchiveTabEnum tab) {
return tab_default_paths[tab];
}
inline bool archive_is_known_app(ArchiveFileTypeEnum type) {
return (type != ArchiveFileTypeFolder && type != ArchiveFileTypeUnknown);
}
bool archive_is_item_in_array(ArchiveBrowserViewModel* model, uint32_t idx);
void archive_update_offset(ArchiveBrowserView* browser);
void archive_update_focus(ArchiveBrowserView* browser, const char* target);
void archive_file_array_load(ArchiveBrowserView* browser, int8_t dir);
size_t archive_file_get_array_size(ArchiveBrowserView* browser);
void archive_file_array_rm_selected(ArchiveBrowserView* browser);
void archive_file_array_swap(ArchiveBrowserView* browser, int8_t dir);
void archive_file_array_rm_all(ArchiveBrowserView* browser);
void archive_set_item_count(ArchiveBrowserView* browser, uint32_t count);
ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser);
ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx);
ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser);
bool archive_is_home(ArchiveBrowserView* browser);
const char* archive_get_name(ArchiveBrowserView* browser);
void archive_add_app_item(ArchiveBrowserView* browser, const char* name);
void archive_add_file_item(ArchiveBrowserView* browser, bool is_folder, const char* name);
void archive_show_file_menu(ArchiveBrowserView* browser, bool show);
void archive_favorites_move_mode(ArchiveBrowserView* browser, bool active);
void archive_switch_tab(ArchiveBrowserView* browser, InputKey key);
void archive_enter_dir(ArchiveBrowserView* browser, string_t name);
void archive_leave_dir(ArchiveBrowserView* browser);
void archive_refresh_dir(ArchiveBrowserView* browser);

View File

@@ -0,0 +1,337 @@
#include "archive_favorites.h"
#include "archive_files.h"
#include "archive_apps.h"
#include "archive_browser.h"
#define ARCHIVE_FAV_FILE_BUF_LEN 32
static bool archive_favorites_read_line(File* file, string_t str_result) {
string_reset(str_result);
uint8_t buffer[ARCHIVE_FAV_FILE_BUF_LEN];
bool result = false;
do {
uint16_t read_count = storage_file_read(file, buffer, ARCHIVE_FAV_FILE_BUF_LEN);
if(storage_file_get_error(file) != FSE_OK) {
return false;
}
for(uint16_t i = 0; i < read_count; i++) {
if(buffer[i] == '\n') {
uint32_t position = storage_file_tell(file);
if(storage_file_get_error(file) != FSE_OK) {
return false;
}
position = position - read_count + i + 1;
storage_file_seek(file, position, true);
if(storage_file_get_error(file) != FSE_OK) {
return false;
}
result = true;
break;
} else {
string_push_back(str_result, buffer[i]);
}
}
if(result || read_count == 0) {
break;
}
} while(true);
return result;
}
uint16_t archive_favorites_count(void* context) {
furi_assert(context);
Storage* fs_api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(fs_api);
string_t buffer;
string_init(buffer);
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
uint16_t lines = 0;
if(result) {
while(1) {
if(!archive_favorites_read_line(file, buffer)) {
break;
}
if(!string_size(buffer)) {
continue; // Skip empty lines
}
++lines;
}
}
storage_file_close(file);
string_clear(buffer);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return lines;
}
static bool archive_favourites_rescan() {
string_t buffer;
string_init(buffer);
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) {
while(1) {
if(!archive_favorites_read_line(file, buffer)) {
break;
}
if(!string_size(buffer)) {
continue;
}
if(string_search(buffer, "/app:") == 0) {
if(archive_app_is_available(NULL, string_get_cstr(buffer))) {
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
}
} else {
if(storage_file_exists(storage, string_get_cstr(buffer))) {
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
}
}
}
}
string_clear(buffer);
storage_file_close(file);
storage_common_remove(storage, ARCHIVE_FAV_PATH);
storage_common_rename(storage, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
storage_common_remove(storage, ARCHIVE_FAV_TEMP_PATH);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return result;
}
bool archive_favorites_read(void* context) {
furi_assert(context);
ArchiveBrowserView* browser = context;
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
string_t buffer;
FileInfo file_info;
string_init(buffer);
bool need_refresh = false;
uint16_t file_count = 0;
archive_file_array_rm_all(browser);
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) {
while(1) {
if(!archive_favorites_read_line(file, buffer)) {
break;
}
if(!string_size(buffer)) {
continue;
}
if(string_search(buffer, "/app:") == 0) {
if(archive_app_is_available(browser, string_get_cstr(buffer))) {
archive_add_app_item(browser, string_get_cstr(buffer));
file_count++;
} else {
need_refresh = true;
}
} else {
if(storage_file_exists(storage, string_get_cstr(buffer))) {
storage_common_stat(storage, string_get_cstr(buffer), &file_info);
archive_add_file_item(
browser, (file_info.flags & FSF_DIRECTORY), string_get_cstr(buffer));
file_count++;
} else {
need_refresh = true;
}
}
string_reset(buffer);
}
}
storage_file_close(file);
string_clear(buffer);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
archive_set_item_count(browser, file_count);
if(need_refresh) {
archive_favourites_rescan();
}
return result;
}
bool archive_favorites_delete(const char* format, ...) {
string_t buffer;
string_t filename;
va_list args;
va_start(args, format);
string_init_vprintf(filename, format, args);
va_end(args);
string_init(buffer);
Storage* fs_api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(fs_api);
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) {
while(1) {
if(!archive_favorites_read_line(file, buffer)) {
break;
}
if(!string_size(buffer)) {
continue;
}
if(string_search(buffer, filename)) {
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
}
}
}
string_clear(buffer);
string_clear(filename);
storage_file_close(file);
storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return result;
}
bool archive_is_favorite(const char* format, ...) {
string_t buffer;
string_t filename;
va_list args;
va_start(args, format);
string_init_vprintf(filename, format, args);
va_end(args);
string_init(buffer);
Storage* fs_api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(fs_api);
bool found = false;
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) {
while(1) {
if(!archive_favorites_read_line(file, buffer)) {
break;
}
if(!string_size(buffer)) {
continue;
}
if(!string_search(buffer, filename)) {
found = true;
break;
}
}
}
storage_file_close(file);
string_clear(buffer);
string_clear(filename);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return found;
}
bool archive_favorites_rename(const char* src, const char* dst) {
furi_assert(src);
furi_assert(dst);
Storage* fs_api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(fs_api);
string_t path;
string_t buffer;
string_init(buffer);
string_init(path);
string_printf(path, "%s", src);
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) {
while(1) {
if(!archive_favorites_read_line(file, buffer)) {
break;
}
if(!string_size(buffer)) {
continue;
}
archive_file_append(
ARCHIVE_FAV_TEMP_PATH,
"%s\n",
string_search(buffer, path) ? string_get_cstr(buffer) : dst);
}
}
string_clear(buffer);
string_clear(path);
storage_file_close(file);
storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return result;
}
void archive_add_to_favorites(const char* file_path) {
furi_assert(file_path);
archive_file_append(ARCHIVE_FAV_PATH, "%s\n", file_path);
}
void archive_favorites_save(void* context) {
furi_assert(context);
ArchiveBrowserView* browser = context;
Storage* fs_api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(fs_api);
for(size_t i = 0; i < archive_file_get_array_size(browser); i++) {
ArchiveFile_t* item = archive_get_file_at(browser, i);
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(item->path));
}
storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include <storage/storage.h>
#define ARCHIVE_FAV_PATH ANY_PATH("favorites.txt")
#define ARCHIVE_FAV_TEMP_PATH ANY_PATH("favorites.tmp")
uint16_t archive_favorites_count(void* context);
bool archive_favorites_read(void* context);
bool archive_favorites_delete(const char* format, ...);
bool archive_is_favorite(const char* format, ...);
bool archive_favorites_rename(const char* src, const char* dst);
void archive_add_to_favorites(const char* file_path);
void archive_favorites_save(void* context);

View File

@@ -0,0 +1,111 @@
#include "archive_files.h"
#include "archive_apps.h"
#include "archive_browser.h"
#define TAG "Archive"
#define ASSETS_DIR "assets"
void archive_set_file_type(ArchiveFile_t* file, const char* path, bool is_folder, bool is_app) {
furi_assert(file);
file->is_app = is_app;
if(is_app) {
file->type = archive_get_app_filetype(archive_get_app_type(path));
} else {
for(size_t i = 0; i < COUNT_OF(known_ext); i++) {
if((known_ext[i][0] == '?') || (known_ext[i][0] == '*')) continue;
if(string_search_str(file->path, known_ext[i], 0) != STRING_FAILURE) {
if(i == ArchiveFileTypeBadUsb) {
if(string_search_str(file->path, archive_get_default_path(ArchiveTabBadUsb)) ==
0) {
file->type = i;
return; // *.txt file is a BadUSB script only if it is in BadUSB folder
}
} else {
file->type = i;
return;
}
}
}
if(is_folder) {
file->type = ArchiveFileTypeFolder;
} else {
file->type = ArchiveFileTypeUnknown;
}
}
}
bool archive_get_items(void* context, const char* path) {
furi_assert(context);
bool res = false;
ArchiveBrowserView* browser = context;
if(archive_get_tab(browser) == ArchiveTabFavorites) {
res = archive_favorites_read(browser);
} else if(strncmp(path, "/app:", 5) == 0) {
res = archive_app_read_dir(browser, path);
}
return res;
}
void archive_file_append(const char* path, const char* format, ...) {
furi_assert(path);
string_t string;
va_list args;
va_start(args, format);
string_init_vprintf(string, format, args);
va_end(args);
Storage* fs_api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(fs_api);
bool res = storage_file_open(file, path, FSAM_WRITE, FSOM_OPEN_APPEND);
if(res) {
storage_file_write(file, string_get_cstr(string), string_size(string));
}
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
}
void archive_delete_file(void* context, const char* format, ...) {
furi_assert(context);
string_t filename;
va_list args;
va_start(args, format);
string_init_vprintf(filename, format, args);
va_end(args);
ArchiveBrowserView* browser = context;
Storage* fs_api = furi_record_open(RECORD_STORAGE);
FileInfo fileinfo;
storage_common_stat(fs_api, string_get_cstr(filename), &fileinfo);
bool res = false;
if(fileinfo.flags & FSF_DIRECTORY) {
res = storage_simply_remove_recursive(fs_api, string_get_cstr(filename));
} else {
res = (storage_common_remove(fs_api, string_get_cstr(filename)) == FSE_OK);
}
furi_record_close(RECORD_STORAGE);
if(archive_is_favorite("%s", string_get_cstr(filename))) {
archive_favorites_delete("%s", string_get_cstr(filename));
}
if(res) {
archive_file_array_rm_selected(browser);
}
string_clear(filename);
}

View File

@@ -0,0 +1,64 @@
#pragma once
#include <m-array.h>
#include <m-string.h>
#include <storage/storage.h>
typedef enum {
ArchiveFileTypeIButton,
ArchiveFileTypeNFC,
ArchiveFileTypeSubGhz,
ArchiveFileTypeLFRFID,
ArchiveFileTypeInfrared,
ArchiveFileTypeBadUsb,
ArchiveFileTypeU2f,
ArchiveFileTypeUpdateManifest,
ArchiveFileTypeFolder,
ArchiveFileTypeUnknown,
ArchiveFileTypeLoading,
} ArchiveFileTypeEnum;
typedef struct {
string_t path;
ArchiveFileTypeEnum type;
bool fav;
bool is_app;
} ArchiveFile_t;
static void ArchiveFile_t_init(ArchiveFile_t* obj) {
obj->type = ArchiveFileTypeUnknown;
obj->is_app = false;
obj->fav = false;
string_init(obj->path);
}
static void ArchiveFile_t_init_set(ArchiveFile_t* obj, const ArchiveFile_t* src) {
obj->type = src->type;
obj->is_app = src->is_app;
obj->fav = src->fav;
string_init_set(obj->path, src->path);
}
static void ArchiveFile_t_set(ArchiveFile_t* obj, const ArchiveFile_t* src) {
obj->type = src->type;
obj->is_app = src->is_app;
obj->fav = src->fav;
string_set(obj->path, src->path);
}
static void ArchiveFile_t_clear(ArchiveFile_t* obj) {
string_clear(obj->path);
}
ARRAY_DEF(
files_array,
ArchiveFile_t,
(INIT(API_2(ArchiveFile_t_init)),
SET(API_6(ArchiveFile_t_set)),
INIT_SET(API_6(ArchiveFile_t_init_set)),
CLEAR(API_2(ArchiveFile_t_clear))))
void archive_set_file_type(ArchiveFile_t* file, const char* path, bool is_folder, bool is_app);
bool archive_get_items(void* context, const char* path);
void archive_file_append(const char* path, const char* format, ...);
void archive_delete_file(void* context, const char* format, ...);

View File

@@ -0,0 +1,30 @@
#include "archive_scene.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const archive_on_enter_handlers[])(void*) = {
#include "archive_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const archive_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "archive_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const archive_on_exit_handlers[])(void* context) = {
#include "archive_scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers archive_scene_handlers = {
.on_enter_handlers = archive_on_enter_handlers,
.on_event_handlers = archive_on_event_handlers,
.on_exit_handlers = archive_on_exit_handlers,
.scene_num = ArchiveAppSceneNum,
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) ArchiveAppScene##id,
typedef enum {
#include "archive_scene_config.h"
ArchiveAppSceneNum,
} ArchiveAppScene;
#undef ADD_SCENE
extern const SceneManagerHandlers archive_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "archive_scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "archive_scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "archive_scene_config.h"
#undef ADD_SCENE

View File

@@ -0,0 +1,221 @@
#include "../archive_i.h"
#include "../helpers/archive_files.h"
#include "../helpers/archive_apps.h"
#include "../helpers/archive_favorites.h"
#include "../helpers/archive_browser.h"
#include "../views/archive_browser_view.h"
#include "archive/scenes/archive_scene.h"
#define TAG "ArchiveSceneBrowser"
#define SCENE_STATE_DEFAULT (0)
#define SCENE_STATE_NEED_REFRESH (1)
static const char* flipper_app_name[] = {
[ArchiveFileTypeIButton] = "iButton",
[ArchiveFileTypeNFC] = "NFC",
[ArchiveFileTypeSubGhz] = "Sub-GHz",
[ArchiveFileTypeLFRFID] = "125 kHz RFID",
[ArchiveFileTypeInfrared] = "Infrared",
[ArchiveFileTypeBadUsb] = "Bad USB",
[ArchiveFileTypeU2f] = "U2F",
[ArchiveFileTypeUpdateManifest] = "UpdaterApp",
};
static void archive_loader_callback(const void* message, void* context) {
furi_assert(message);
furi_assert(context);
const LoaderEvent* event = message;
ArchiveApp* archive = (ArchiveApp*)context;
if(event->type == LoaderEventTypeApplicationStopped) {
view_dispatcher_send_custom_event(
archive->view_dispatcher, ArchiveBrowserEventListRefresh);
}
}
static void archive_run_in_app(ArchiveBrowserView* browser, ArchiveFile_t* selected) {
UNUSED(browser);
Loader* loader = furi_record_open(RECORD_LOADER);
LoaderStatus status;
if(selected->is_app) {
char* param = strrchr(string_get_cstr(selected->path), '/');
if(param != NULL) {
param++;
}
status = loader_start(loader, flipper_app_name[selected->type], param);
} else {
status = loader_start(
loader, flipper_app_name[selected->type], string_get_cstr(selected->path));
}
if(status != LoaderStatusOk) {
FURI_LOG_E(TAG, "loader_start failed: %d", status);
}
furi_record_close(RECORD_LOADER);
}
void archive_scene_browser_callback(ArchiveBrowserEvent event, void* context) {
ArchiveApp* archive = (ArchiveApp*)context;
view_dispatcher_send_custom_event(archive->view_dispatcher, event);
}
void archive_scene_browser_on_enter(void* context) {
ArchiveApp* archive = (ArchiveApp*)context;
ArchiveBrowserView* browser = archive->browser;
browser->is_root = true;
archive_browser_set_callback(browser, archive_scene_browser_callback, archive);
archive_update_focus(browser, archive->text_store);
view_dispatcher_switch_to_view(archive->view_dispatcher, ArchiveViewBrowser);
Loader* loader = furi_record_open(RECORD_LOADER);
archive->loader_stop_subscription =
furi_pubsub_subscribe(loader_get_pubsub(loader), archive_loader_callback, archive);
furi_record_close(RECORD_LOADER);
uint32_t state = scene_manager_get_scene_state(archive->scene_manager, ArchiveAppSceneBrowser);
if(state == SCENE_STATE_NEED_REFRESH) {
view_dispatcher_send_custom_event(
archive->view_dispatcher, ArchiveBrowserEventListRefresh);
}
scene_manager_set_scene_state(
archive->scene_manager, ArchiveAppSceneBrowser, SCENE_STATE_DEFAULT);
}
bool archive_scene_browser_on_event(void* context, SceneManagerEvent event) {
ArchiveApp* archive = (ArchiveApp*)context;
ArchiveBrowserView* browser = archive->browser;
ArchiveFile_t* selected = archive_get_current_file(browser);
bool favorites = archive_get_tab(browser) == ArchiveTabFavorites;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case ArchiveBrowserEventFileMenuOpen:
archive_show_file_menu(browser, true);
consumed = true;
break;
case ArchiveBrowserEventFileMenuClose:
archive_show_file_menu(browser, false);
consumed = true;
break;
case ArchiveBrowserEventFileMenuRun:
if(archive_is_known_app(selected->type)) {
archive_run_in_app(browser, selected);
archive_show_file_menu(browser, false);
}
consumed = true;
break;
case ArchiveBrowserEventFileMenuPin: {
const char* name = archive_get_name(browser);
if(favorites) {
archive_favorites_delete(name);
archive_file_array_rm_selected(browser);
archive_show_file_menu(browser, false);
} else if(archive_is_known_app(selected->type)) {
if(archive_is_favorite("%s", name)) {
archive_favorites_delete("%s", name);
} else {
archive_file_append(ARCHIVE_FAV_PATH, "%s\n", name);
}
archive_show_file_menu(browser, false);
}
consumed = true;
} break;
case ArchiveBrowserEventFileMenuRename:
if(favorites) {
browser->callback(ArchiveBrowserEventEnterFavMove, browser->context);
} else if((archive_is_known_app(selected->type)) && (selected->is_app == false)) {
archive_show_file_menu(browser, false);
scene_manager_set_scene_state(
archive->scene_manager, ArchiveAppSceneBrowser, SCENE_STATE_NEED_REFRESH);
scene_manager_next_scene(archive->scene_manager, ArchiveAppSceneRename);
}
consumed = true;
break;
case ArchiveBrowserEventFileMenuDelete:
if(archive_get_tab(browser) != ArchiveTabFavorites) {
scene_manager_next_scene(archive->scene_manager, ArchiveAppSceneDelete);
}
consumed = true;
break;
case ArchiveBrowserEventEnterDir:
archive_enter_dir(browser, selected->path);
consumed = true;
break;
case ArchiveBrowserEventFavMoveUp:
archive_file_array_swap(browser, 1);
consumed = true;
break;
case ArchiveBrowserEventFavMoveDown:
archive_file_array_swap(browser, -1);
consumed = true;
break;
case ArchiveBrowserEventEnterFavMove:
string_set(archive->fav_move_str, selected->path);
archive_show_file_menu(browser, false);
archive_favorites_move_mode(archive->browser, true);
consumed = true;
break;
case ArchiveBrowserEventExitFavMove:
archive_update_focus(browser, string_get_cstr(archive->fav_move_str));
archive_favorites_move_mode(archive->browser, false);
consumed = true;
break;
case ArchiveBrowserEventSaveFavMove:
archive_favorites_move_mode(archive->browser, false);
archive_favorites_save(archive->browser);
consumed = true;
break;
case ArchiveBrowserEventLoadPrevItems:
archive_file_array_load(archive->browser, -1);
consumed = true;
break;
case ArchiveBrowserEventLoadNextItems:
archive_file_array_load(archive->browser, 1);
consumed = true;
break;
case ArchiveBrowserEventListRefresh:
if(!favorites) {
archive_refresh_dir(browser);
} else {
archive_favorites_read(browser);
}
consumed = true;
break;
case ArchiveBrowserEventExit:
if(!archive_is_home(browser)) {
archive_leave_dir(browser);
} else {
Loader* loader = furi_record_open(RECORD_LOADER);
furi_pubsub_unsubscribe(
loader_get_pubsub(loader), archive->loader_stop_subscription);
furi_record_close(RECORD_LOADER);
view_dispatcher_stop(archive->view_dispatcher);
}
consumed = true;
break;
default:
break;
}
}
return consumed;
}
void archive_scene_browser_on_exit(void* context) {
ArchiveApp* archive = (ArchiveApp*)context;
Loader* loader = furi_record_open(RECORD_LOADER);
furi_pubsub_unsubscribe(loader_get_pubsub(loader), archive->loader_stop_subscription);
furi_record_close(RECORD_LOADER);
}

View File

@@ -0,0 +1,3 @@
ADD_SCENE(archive, browser, Browser)
ADD_SCENE(archive, rename, Rename)
ADD_SCENE(archive, delete, Delete)

View File

@@ -0,0 +1,74 @@
#include "../archive_i.h"
#include "../helpers/archive_favorites.h"
#include "../helpers/archive_files.h"
#include "../helpers/archive_apps.h"
#include "../helpers/archive_browser.h"
#include "toolbox/path.h"
#include "m-string.h"
#define SCENE_DELETE_CUSTOM_EVENT (0UL)
#define MAX_TEXT_INPUT_LEN 22
void archive_scene_delete_widget_callback(GuiButtonType result, InputType type, void* context) {
furi_assert(context);
ArchiveApp* app = (ArchiveApp*)context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(app->view_dispatcher, result);
}
}
void archive_scene_delete_on_enter(void* context) {
furi_assert(context);
ArchiveApp* app = (ArchiveApp*)context;
widget_add_button_element(
app->widget, GuiButtonTypeLeft, "Cancel", archive_scene_delete_widget_callback, app);
widget_add_button_element(
app->widget, GuiButtonTypeRight, "Delete", archive_scene_delete_widget_callback, app);
string_t filename;
string_init(filename);
ArchiveFile_t* current = archive_get_current_file(app->browser);
path_extract_filename(current->path, filename, false);
char delete_str[64];
snprintf(delete_str, sizeof(delete_str), "\e#Delete %s?\e#", string_get_cstr(filename));
widget_add_text_box_element(
app->widget, 0, 0, 128, 23, AlignCenter, AlignCenter, delete_str, false);
string_clear(filename);
view_dispatcher_switch_to_view(app->view_dispatcher, ArchiveViewWidget);
}
bool archive_scene_delete_on_event(void* context, SceneManagerEvent event) {
furi_assert(context);
ArchiveApp* app = (ArchiveApp*)context;
ArchiveBrowserView* browser = app->browser;
ArchiveFile_t* selected = archive_get_current_file(browser);
const char* name = archive_get_name(browser);
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GuiButtonTypeRight) {
if(selected->is_app) {
archive_app_delete_file(browser, name);
} else {
archive_delete_file(browser, "%s", name);
}
archive_show_file_menu(browser, false);
return scene_manager_previous_scene(app->scene_manager);
} else if(event.event == GuiButtonTypeLeft) {
return scene_manager_previous_scene(app->scene_manager);
}
}
return false;
}
void archive_scene_delete_on_exit(void* context) {
furi_assert(context);
ArchiveApp* app = (ArchiveApp*)context;
widget_reset(app->widget);
}

View File

@@ -0,0 +1,89 @@
#include "../archive_i.h"
#include "../helpers/archive_favorites.h"
#include "../helpers/archive_files.h"
#include "../helpers/archive_browser.h"
#include "archive/views/archive_browser_view.h"
#include "toolbox/path.h"
#define SCENE_RENAME_CUSTOM_EVENT (0UL)
#define MAX_TEXT_INPUT_LEN 22
void archive_scene_rename_text_input_callback(void* context) {
ArchiveApp* archive = (ArchiveApp*)context;
view_dispatcher_send_custom_event(archive->view_dispatcher, SCENE_RENAME_CUSTOM_EVENT);
}
void archive_scene_rename_on_enter(void* context) {
ArchiveApp* archive = (ArchiveApp*)context;
TextInput* text_input = archive->text_input;
ArchiveFile_t* current = archive_get_current_file(archive->browser);
string_t filename;
string_init(filename);
path_extract_filename(current->path, filename, true);
strlcpy(archive->text_store, string_get_cstr(filename), MAX_NAME_LEN);
path_extract_extension(current->path, archive->file_extension, MAX_EXT_LEN);
text_input_set_header_text(text_input, "Rename:");
text_input_set_result_callback(
text_input,
archive_scene_rename_text_input_callback,
archive,
archive->text_store,
MAX_TEXT_INPUT_LEN,
false);
ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
string_get_cstr(archive->browser->path), archive->file_extension, "");
text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
string_clear(filename);
view_dispatcher_switch_to_view(archive->view_dispatcher, ArchiveViewTextInput);
}
bool archive_scene_rename_on_event(void* context, SceneManagerEvent event) {
ArchiveApp* archive = (ArchiveApp*)context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SCENE_RENAME_CUSTOM_EVENT) {
Storage* fs_api = furi_record_open(RECORD_STORAGE);
const char* path_src = archive_get_name(archive->browser);
ArchiveFile_t* file = archive_get_current_file(archive->browser);
string_t path_dst;
string_init(path_dst);
path_extract_dirname(path_src, path_dst);
string_cat_printf(path_dst, "/%s%s", archive->text_store, known_ext[file->type]);
storage_common_rename(fs_api, path_src, string_get_cstr(path_dst));
furi_record_close(RECORD_STORAGE);
if(file->fav) {
archive_favorites_rename(path_src, string_get_cstr(path_dst));
}
string_clear(path_dst);
scene_manager_next_scene(archive->scene_manager, ArchiveAppSceneBrowser);
consumed = true;
}
}
return consumed;
}
void archive_scene_rename_on_exit(void* context) {
ArchiveApp* archive = (ArchiveApp*)context;
// Clear view
void* validator_context = text_input_get_validator_callback_context(archive->text_input);
text_input_set_validator(archive->text_input, NULL, NULL);
validator_is_file_free(validator_context);
text_input_reset(archive->text_input);
}

View File

@@ -0,0 +1,393 @@
#include "assets_icons.h"
#include "toolbox/path.h"
#include <furi.h>
#include "../archive_i.h"
#include "archive_browser_view.h"
#include "../helpers/archive_browser.h"
static const char* ArchiveTabNames[] = {
[ArchiveTabFavorites] = "Favorites",
[ArchiveTabIButton] = "iButton",
[ArchiveTabNFC] = "NFC",
[ArchiveTabSubGhz] = "Sub-GHz",
[ArchiveTabLFRFID] = "RFID LF",
[ArchiveTabInfrared] = "Infrared",
[ArchiveTabBadUsb] = "Bad USB",
[ArchiveTabU2f] = "U2F",
[ArchiveTabBrowser] = "Browser",
};
static const Icon* ArchiveItemIcons[] = {
[ArchiveFileTypeIButton] = &I_ibutt_10px,
[ArchiveFileTypeNFC] = &I_Nfc_10px,
[ArchiveFileTypeSubGhz] = &I_sub1_10px,
[ArchiveFileTypeLFRFID] = &I_125_10px,
[ArchiveFileTypeInfrared] = &I_ir_10px,
[ArchiveFileTypeBadUsb] = &I_badusb_10px,
[ArchiveFileTypeU2f] = &I_u2f_10px,
[ArchiveFileTypeUpdateManifest] = &I_update_10px,
[ArchiveFileTypeFolder] = &I_dir_10px,
[ArchiveFileTypeUnknown] = &I_unknown_10px,
[ArchiveFileTypeLoading] = &I_loading_10px,
};
void archive_browser_set_callback(
ArchiveBrowserView* browser,
ArchiveBrowserViewCallback callback,
void* context) {
furi_assert(browser);
furi_assert(callback);
browser->callback = callback;
browser->context = context;
}
static void render_item_menu(Canvas* canvas, ArchiveBrowserViewModel* model) {
canvas_set_color(canvas, ColorWhite);
canvas_draw_box(canvas, 71, 17, 57, 46);
canvas_set_color(canvas, ColorBlack);
elements_slightly_rounded_frame(canvas, 70, 16, 58, 48);
string_t menu[MENU_ITEMS];
string_init_set_str(menu[0], "Run in app");
string_init_set_str(menu[1], "Pin");
string_init_set_str(menu[2], "Rename");
string_init_set_str(menu[3], "Delete");
ArchiveFile_t* selected = files_array_get(model->files, model->item_idx - model->array_offset);
if((selected->fav) || (model->tab_idx == ArchiveTabFavorites)) {
string_set_str(menu[1], "Unpin");
}
if(!archive_is_known_app(selected->type)) {
string_set_str(menu[0], "---");
string_set_str(menu[1], "---");
string_set_str(menu[2], "---");
} else {
if(model->tab_idx == ArchiveTabFavorites) {
string_set_str(menu[2], "Move");
string_set_str(menu[3], "---");
} else if(selected->is_app) {
string_set_str(menu[2], "---");
}
}
for(size_t i = 0; i < MENU_ITEMS; i++) {
canvas_draw_str(canvas, 82, 27 + i * 11, string_get_cstr(menu[i]));
string_clear(menu[i]);
}
canvas_draw_icon(canvas, 74, 20 + model->menu_idx * 11, &I_ButtonRight_4x7);
}
static void archive_draw_frame(Canvas* canvas, uint16_t idx, bool scrollbar, bool moving) {
uint8_t x_offset = moving ? MOVE_OFFSET : 0;
canvas_set_color(canvas, ColorBlack);
canvas_draw_box(
canvas,
0 + x_offset,
15 + idx * FRAME_HEIGHT,
(scrollbar ? 122 : 127) - x_offset,
FRAME_HEIGHT);
canvas_set_color(canvas, ColorWhite);
canvas_draw_dot(canvas, 0 + x_offset, 15 + idx * FRAME_HEIGHT);
canvas_draw_dot(canvas, 1 + x_offset, 15 + idx * FRAME_HEIGHT);
canvas_draw_dot(canvas, 0 + x_offset, (15 + idx * FRAME_HEIGHT) + 1);
canvas_draw_dot(canvas, 0 + x_offset, (15 + idx * FRAME_HEIGHT) + 11);
canvas_draw_dot(canvas, scrollbar ? 121 : 126, 15 + idx * FRAME_HEIGHT);
canvas_draw_dot(canvas, scrollbar ? 121 : 126, (15 + idx * FRAME_HEIGHT) + 11);
}
static void archive_draw_loading(Canvas* canvas, ArchiveBrowserViewModel* model) {
furi_assert(model);
uint8_t x = 128 / 2 - 24 / 2;
uint8_t y = 64 / 2 - 24 / 2;
canvas_draw_icon(canvas, x, y, &A_Loading_24);
}
static void draw_list(Canvas* canvas, ArchiveBrowserViewModel* model) {
furi_assert(model);
size_t array_size = files_array_size(model->files);
bool scrollbar = model->item_cnt > 4;
for(uint32_t i = 0; i < MIN(model->item_cnt, MENU_ITEMS); ++i) {
string_t str_buf;
string_init(str_buf);
int32_t idx = CLAMP((uint32_t)(i + model->list_offset), model->item_cnt, 0u);
uint8_t x_offset = (model->move_fav && model->item_idx == idx) ? MOVE_OFFSET : 0;
ArchiveFileTypeEnum file_type = ArchiveFileTypeLoading;
if(archive_is_item_in_array(model, idx)) {
ArchiveFile_t* file = files_array_get(
model->files, CLAMP(idx - model->array_offset, (int32_t)(array_size - 1), 0));
path_extract_filename(file->path, str_buf, archive_is_known_app(file->type));
file_type = file->type;
} else {
string_set_str(str_buf, "---");
}
elements_string_fit_width(
canvas, str_buf, (scrollbar ? MAX_LEN_PX - 6 : MAX_LEN_PX) - x_offset);
if(model->item_idx == idx) {
archive_draw_frame(canvas, i, scrollbar, model->move_fav);
} else {
canvas_set_color(canvas, ColorBlack);
}
canvas_draw_icon(canvas, 2 + x_offset, 16 + i * FRAME_HEIGHT, ArchiveItemIcons[file_type]);
canvas_draw_str(canvas, 15 + x_offset, 24 + i * FRAME_HEIGHT, string_get_cstr(str_buf));
string_clear(str_buf);
}
if(scrollbar) {
elements_scrollbar_pos(canvas, 126, 15, 49, model->item_idx, model->item_cnt);
}
if(model->menu) {
render_item_menu(canvas, model);
}
}
static void archive_render_status_bar(Canvas* canvas, ArchiveBrowserViewModel* model) {
furi_assert(model);
const char* tab_name = ArchiveTabNames[model->tab_idx];
canvas_draw_icon(canvas, 0, 0, &I_Background_128x11);
canvas_set_color(canvas, ColorWhite);
canvas_draw_box(canvas, 0, 0, 50, 13);
canvas_draw_box(canvas, 107, 0, 20, 13);
canvas_set_color(canvas, ColorBlack);
canvas_draw_rframe(canvas, 0, 0, 51, 13, 1); // frame
canvas_draw_line(canvas, 49, 1, 49, 11); // shadow right
canvas_draw_line(canvas, 1, 11, 49, 11); // shadow bottom
canvas_draw_str_aligned(canvas, 25, 9, AlignCenter, AlignBottom, tab_name);
canvas_draw_rframe(canvas, 107, 0, 21, 13, 1);
canvas_draw_line(canvas, 126, 1, 126, 11);
canvas_draw_line(canvas, 108, 11, 126, 11);
if(model->move_fav) {
canvas_draw_icon(canvas, 110, 4, &I_ButtonUp_7x4);
canvas_draw_icon(canvas, 117, 4, &I_ButtonDown_7x4);
} else {
canvas_draw_icon(canvas, 111, 2, &I_ButtonLeft_4x7);
canvas_draw_icon(canvas, 119, 2, &I_ButtonRight_4x7);
}
canvas_set_color(canvas, ColorWhite);
canvas_draw_dot(canvas, 50, 0);
canvas_draw_dot(canvas, 127, 0);
canvas_set_color(canvas, ColorBlack);
}
static void archive_view_render(Canvas* canvas, void* mdl) {
ArchiveBrowserViewModel* model = mdl;
archive_render_status_bar(canvas, mdl);
if(model->folder_loading) {
archive_draw_loading(canvas, model);
} else if(model->item_cnt > 0) {
draw_list(canvas, model);
} else {
canvas_draw_str_aligned(
canvas, GUI_DISPLAY_WIDTH / 2, 40, AlignCenter, AlignCenter, "Empty");
}
}
View* archive_browser_get_view(ArchiveBrowserView* browser) {
furi_assert(browser);
return browser->view;
}
static bool is_file_list_load_required(ArchiveBrowserViewModel* model) {
size_t array_size = files_array_size(model->files);
if((model->list_loading) || (array_size >= model->item_cnt)) {
return false;
}
if((model->array_offset > 0) &&
(model->item_idx < (model->array_offset + FILE_LIST_BUF_LEN / 4))) {
return true;
}
if(((model->array_offset + array_size) < model->item_cnt) &&
(model->item_idx > (int32_t)(model->array_offset + array_size - FILE_LIST_BUF_LEN / 4))) {
return true;
}
return false;
}
static bool archive_view_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
ArchiveBrowserView* browser = context;
bool in_menu;
bool move_fav_mode;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
in_menu = model->menu;
move_fav_mode = model->move_fav;
return false;
});
if(in_menu) {
if(event->type == InputTypeShort) {
if(event->key == InputKeyUp || event->key == InputKeyDown) {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
if(event->key == InputKeyUp) {
model->menu_idx = ((model->menu_idx - 1) + MENU_ITEMS) % MENU_ITEMS;
} else if(event->key == InputKeyDown) {
model->menu_idx = (model->menu_idx + 1) % MENU_ITEMS;
}
return true;
});
}
if(event->key == InputKeyOk) {
uint8_t idx;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
idx = model->menu_idx;
return false;
});
browser->callback(file_menu_actions[idx], browser->context);
} else if(event->key == InputKeyBack) {
browser->callback(ArchiveBrowserEventFileMenuClose, browser->context);
}
}
} else {
if(event->type == InputTypeShort) {
if(event->key == InputKeyLeft || event->key == InputKeyRight) {
if(move_fav_mode) return false;
archive_switch_tab(browser, event->key);
} else if(event->key == InputKeyBack) {
if(move_fav_mode) {
browser->callback(ArchiveBrowserEventExitFavMove, browser->context);
} else {
browser->callback(ArchiveBrowserEventExit, browser->context);
}
}
}
if((event->key == InputKeyUp || event->key == InputKeyDown) &&
(event->type == InputTypeShort || event->type == InputTypeRepeat)) {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
if(event->key == InputKeyUp) {
model->item_idx =
((model->item_idx - 1) + model->item_cnt) % model->item_cnt;
if(is_file_list_load_required(model)) {
model->list_loading = true;
browser->callback(ArchiveBrowserEventLoadPrevItems, browser->context);
}
if(move_fav_mode) {
browser->callback(ArchiveBrowserEventFavMoveUp, browser->context);
}
} else if(event->key == InputKeyDown) {
model->item_idx = (model->item_idx + 1) % model->item_cnt;
if(is_file_list_load_required(model)) {
model->list_loading = true;
browser->callback(ArchiveBrowserEventLoadNextItems, browser->context);
}
if(move_fav_mode) {
browser->callback(ArchiveBrowserEventFavMoveDown, browser->context);
}
}
return true;
});
archive_update_offset(browser);
}
if(event->key == InputKeyOk) {
ArchiveFile_t* selected = archive_get_current_file(browser);
if(selected) {
bool favorites = archive_get_tab(browser) == ArchiveTabFavorites;
bool folder = selected->type == ArchiveFileTypeFolder;
if(event->type == InputTypeShort) {
if(favorites) {
if(move_fav_mode) {
browser->callback(ArchiveBrowserEventSaveFavMove, browser->context);
} else {
browser->callback(ArchiveBrowserEventFileMenuRun, browser->context);
}
} else if(folder) {
browser->callback(ArchiveBrowserEventEnterDir, browser->context);
} else {
browser->callback(ArchiveBrowserEventFileMenuOpen, browser->context);
}
} else if(event->type == InputTypeLong) {
if(move_fav_mode) {
browser->callback(ArchiveBrowserEventSaveFavMove, browser->context);
} else if(folder || favorites) {
browser->callback(ArchiveBrowserEventFileMenuOpen, browser->context);
}
}
}
}
}
return true;
}
ArchiveBrowserView* browser_alloc() {
ArchiveBrowserView* browser = malloc(sizeof(ArchiveBrowserView));
browser->view = view_alloc();
view_allocate_model(browser->view, ViewModelTypeLocking, sizeof(ArchiveBrowserViewModel));
view_set_context(browser->view, browser);
view_set_draw_callback(browser->view, archive_view_render);
view_set_input_callback(browser->view, archive_view_input);
string_init_set_str(browser->path, archive_get_default_path(TAB_DEFAULT));
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_init(model->files);
model->tab_idx = TAB_DEFAULT;
return true;
});
return browser;
}
void browser_free(ArchiveBrowserView* browser) {
furi_assert(browser);
if(browser->worker_running) {
file_browser_worker_free(browser->worker);
}
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_clear(model->files);
return false;
});
string_clear(browser->path);
view_free(browser->view);
free(browser);
}

View File

@@ -0,0 +1,109 @@
#pragma once
#include <gui/gui_i.h>
#include <gui/view.h>
#include <gui/canvas.h>
#include <gui/elements.h>
#include <furi.h>
#include <storage/storage.h>
#include "../helpers/archive_files.h"
#include "../helpers/archive_favorites.h"
#include "gui/modules/file_browser_worker.h"
#define MAX_LEN_PX 110
#define MAX_NAME_LEN 255
#define MAX_EXT_LEN 6
#define FRAME_HEIGHT 12
#define MENU_ITEMS 4u
#define MOVE_OFFSET 5u
typedef enum {
ArchiveTabFavorites,
ArchiveTabSubGhz,
ArchiveTabLFRFID,
ArchiveTabNFC,
ArchiveTabInfrared,
ArchiveTabIButton,
ArchiveTabBadUsb,
ArchiveTabU2f,
ArchiveTabBrowser,
ArchiveTabTotal,
} ArchiveTabEnum;
typedef enum {
ArchiveBrowserEventFileMenuOpen,
ArchiveBrowserEventFileMenuClose,
ArchiveBrowserEventFileMenuRun,
ArchiveBrowserEventFileMenuPin,
ArchiveBrowserEventFileMenuRename,
ArchiveBrowserEventFileMenuDelete,
ArchiveBrowserEventEnterDir,
ArchiveBrowserEventFavMoveUp,
ArchiveBrowserEventFavMoveDown,
ArchiveBrowserEventEnterFavMove,
ArchiveBrowserEventExitFavMove,
ArchiveBrowserEventSaveFavMove,
ArchiveBrowserEventLoadPrevItems,
ArchiveBrowserEventLoadNextItems,
ArchiveBrowserEventListRefresh,
ArchiveBrowserEventExit,
} ArchiveBrowserEvent;
static const uint8_t file_menu_actions[MENU_ITEMS] = {
[0] = ArchiveBrowserEventFileMenuRun,
[1] = ArchiveBrowserEventFileMenuPin,
[2] = ArchiveBrowserEventFileMenuRename,
[3] = ArchiveBrowserEventFileMenuDelete,
};
typedef struct ArchiveBrowserView ArchiveBrowserView;
typedef void (*ArchiveBrowserViewCallback)(ArchiveBrowserEvent event, void* context);
typedef enum {
BrowserActionBrowse,
BrowserActionItemMenu,
BrowserActionTotal,
} BrowserActionEnum;
struct ArchiveBrowserView {
View* view;
BrowserWorker* worker;
bool worker_running;
ArchiveBrowserViewCallback callback;
void* context;
string_t path;
InputKey last_tab_switch_dir;
bool is_root;
};
typedef struct {
ArchiveTabEnum tab_idx;
files_array_t files;
uint8_t menu_idx;
bool menu;
bool move_fav;
bool list_loading;
bool folder_loading;
uint32_t item_cnt;
int32_t item_idx;
int32_t array_offset;
int32_t list_offset;
} ArchiveBrowserViewModel;
void archive_browser_set_callback(
ArchiveBrowserView* browser,
ArchiveBrowserViewCallback callback,
void* context);
View* archive_browser_get_view(ArchiveBrowserView* browser);
ArchiveBrowserView* browser_alloc();
void browser_free(ArchiveBrowserView* browser);

View File

@@ -0,0 +1,14 @@
App(
appid="bad_usb",
name="Bad USB",
apptype=FlipperAppType.APP,
entry_point="bad_usb_app",
cdefines=["APP_BAD_USB"],
requires=[
"gui",
"dialogs",
],
stack_size=2 * 1024,
icon="A_BadUsb_14",
order=70,
)

View File

@@ -0,0 +1,110 @@
#include "bad_usb_app_i.h"
#include "m-string.h"
#include <furi.h>
#include <furi_hal.h>
#include <storage/storage.h>
#include <lib/toolbox/path.h>
static bool bad_usb_app_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
BadUsbApp* app = context;
return scene_manager_handle_custom_event(app->scene_manager, event);
}
static bool bad_usb_app_back_event_callback(void* context) {
furi_assert(context);
BadUsbApp* app = context;
return scene_manager_handle_back_event(app->scene_manager);
}
static void bad_usb_app_tick_event_callback(void* context) {
furi_assert(context);
BadUsbApp* app = context;
scene_manager_handle_tick_event(app->scene_manager);
}
BadUsbApp* bad_usb_app_alloc(char* arg) {
BadUsbApp* app = malloc(sizeof(BadUsbApp));
string_init(app->file_path);
if(arg && strlen(arg)) {
string_set_str(app->file_path, arg);
}
app->gui = furi_record_open(RECORD_GUI);
app->notifications = furi_record_open(RECORD_NOTIFICATION);
app->dialogs = furi_record_open(RECORD_DIALOGS);
app->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(app->view_dispatcher);
app->scene_manager = scene_manager_alloc(&bad_usb_scene_handlers, app);
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
view_dispatcher_set_tick_event_callback(
app->view_dispatcher, bad_usb_app_tick_event_callback, 500);
view_dispatcher_set_custom_event_callback(
app->view_dispatcher, bad_usb_app_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
app->view_dispatcher, bad_usb_app_back_event_callback);
// Custom Widget
app->widget = widget_alloc();
view_dispatcher_add_view(
app->view_dispatcher, BadUsbAppViewError, widget_get_view(app->widget));
app->bad_usb_view = bad_usb_alloc();
view_dispatcher_add_view(
app->view_dispatcher, BadUsbAppViewWork, bad_usb_get_view(app->bad_usb_view));
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
if(furi_hal_usb_is_locked()) {
app->error = BadUsbAppErrorCloseRpc;
scene_manager_next_scene(app->scene_manager, BadUsbSceneError);
} else {
if(!string_empty_p(app->file_path)) {
scene_manager_next_scene(app->scene_manager, BadUsbSceneWork);
} else {
string_set_str(app->file_path, BAD_USB_APP_PATH_FOLDER);
scene_manager_next_scene(app->scene_manager, BadUsbSceneFileSelect);
}
}
return app;
}
void bad_usb_app_free(BadUsbApp* app) {
furi_assert(app);
// Views
view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewWork);
bad_usb_free(app->bad_usb_view);
// Custom Widget
view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewError);
widget_free(app->widget);
// View dispatcher
view_dispatcher_free(app->view_dispatcher);
scene_manager_free(app->scene_manager);
// Close records
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_NOTIFICATION);
furi_record_close(RECORD_DIALOGS);
string_clear(app->file_path);
free(app);
}
int32_t bad_usb_app(void* p) {
BadUsbApp* bad_usb_app = bad_usb_app_alloc((char*)p);
view_dispatcher_run(bad_usb_app->view_dispatcher);
bad_usb_app_free(bad_usb_app);
return 0;
}

View File

@@ -0,0 +1,11 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef struct BadUsbApp BadUsbApp;
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,42 @@
#pragma once
#include "bad_usb_app.h"
#include "scenes/bad_usb_scene.h"
#include "bad_usb_script.h"
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/submenu.h>
#include <dialogs/dialogs.h>
#include <notification/notification_messages.h>
#include <gui/modules/variable_item_list.h>
#include <gui/modules/widget.h>
#include "views/bad_usb_view.h"
#define BAD_USB_APP_PATH_FOLDER ANY_PATH("badusb")
#define BAD_USB_APP_EXTENSION ".txt"
typedef enum {
BadUsbAppErrorNoFiles,
BadUsbAppErrorCloseRpc,
} BadUsbAppError;
struct BadUsbApp {
Gui* gui;
ViewDispatcher* view_dispatcher;
SceneManager* scene_manager;
NotificationApp* notifications;
DialogsApp* dialogs;
Widget* widget;
BadUsbAppError error;
string_t file_path;
BadUsb* bad_usb_view;
BadUsbScript* bad_usb_script;
};
typedef enum {
BadUsbAppViewError,
BadUsbAppViewWork,
} BadUsbAppView;

View File

@@ -0,0 +1,624 @@
#include <furi.h>
#include <furi_hal.h>
#include <gui/gui.h>
#include <input/input.h>
#include <lib/toolbox/args.h>
#include <furi_hal_usb_hid.h>
#include <storage/storage.h>
#include "bad_usb_script.h"
#include <dolphin/dolphin.h>
#define TAG "BadUSB"
#define WORKER_TAG TAG "Worker"
#define FILE_BUFFER_LEN 16
#define SCRIPT_STATE_ERROR (-1)
#define SCRIPT_STATE_END (-2)
#define SCRIPT_STATE_NEXT_LINE (-3)
typedef enum {
WorkerEvtToggle = (1 << 0),
WorkerEvtEnd = (1 << 1),
WorkerEvtConnect = (1 << 2),
WorkerEvtDisconnect = (1 << 3),
} WorkerEvtFlags;
struct BadUsbScript {
FuriHalUsbHidConfig hid_cfg;
BadUsbState st;
string_t file_path;
uint32_t defdelay;
FuriThread* thread;
uint8_t file_buf[FILE_BUFFER_LEN + 1];
uint8_t buf_start;
uint8_t buf_len;
bool file_end;
string_t line;
string_t line_prev;
uint32_t repeat_cnt;
};
typedef struct {
char* name;
uint16_t keycode;
} DuckyKey;
static const DuckyKey ducky_keys[] = {
{"CTRL-ALT", KEY_MOD_LEFT_CTRL | KEY_MOD_LEFT_ALT},
{"CTRL-SHIFT", KEY_MOD_LEFT_CTRL | KEY_MOD_LEFT_SHIFT},
{"ALT-SHIFT", KEY_MOD_LEFT_ALT | KEY_MOD_LEFT_SHIFT},
{"ALT-GUI", KEY_MOD_LEFT_ALT | KEY_MOD_LEFT_GUI},
{"GUI-SHIFT", KEY_MOD_LEFT_GUI | KEY_MOD_LEFT_SHIFT},
{"CTRL", KEY_MOD_LEFT_CTRL},
{"CONTROL", KEY_MOD_LEFT_CTRL},
{"SHIFT", KEY_MOD_LEFT_SHIFT},
{"ALT", KEY_MOD_LEFT_ALT},
{"GUI", KEY_MOD_LEFT_GUI},
{"WINDOWS", KEY_MOD_LEFT_GUI},
{"DOWNARROW", HID_KEYBOARD_DOWN_ARROW},
{"DOWN", HID_KEYBOARD_DOWN_ARROW},
{"LEFTARROW", HID_KEYBOARD_LEFT_ARROW},
{"LEFT", HID_KEYBOARD_LEFT_ARROW},
{"RIGHTARROW", HID_KEYBOARD_RIGHT_ARROW},
{"RIGHT", HID_KEYBOARD_RIGHT_ARROW},
{"UPARROW", HID_KEYBOARD_UP_ARROW},
{"UP", HID_KEYBOARD_UP_ARROW},
{"ENTER", HID_KEYBOARD_RETURN},
{"BREAK", HID_KEYBOARD_PAUSE},
{"PAUSE", HID_KEYBOARD_PAUSE},
{"CAPSLOCK", HID_KEYBOARD_CAPS_LOCK},
{"DELETE", HID_KEYBOARD_DELETE},
{"BACKSPACE", HID_KEYPAD_BACKSPACE},
{"END", HID_KEYBOARD_END},
{"ESC", HID_KEYBOARD_ESCAPE},
{"ESCAPE", HID_KEYBOARD_ESCAPE},
{"HOME", HID_KEYBOARD_HOME},
{"INSERT", HID_KEYBOARD_INSERT},
{"NUMLOCK", HID_KEYPAD_NUMLOCK},
{"PAGEUP", HID_KEYBOARD_PAGE_UP},
{"PAGEDOWN", HID_KEYBOARD_PAGE_DOWN},
{"PRINTSCREEN", HID_KEYBOARD_PRINT_SCREEN},
{"SCROLLOCK", HID_KEYBOARD_SCROLL_LOCK},
{"SPACE", HID_KEYBOARD_SPACEBAR},
{"TAB", HID_KEYBOARD_TAB},
{"MENU", HID_KEYBOARD_APPLICATION},
{"APP", HID_KEYBOARD_APPLICATION},
{"F1", HID_KEYBOARD_F1},
{"F2", HID_KEYBOARD_F2},
{"F3", HID_KEYBOARD_F3},
{"F4", HID_KEYBOARD_F4},
{"F5", HID_KEYBOARD_F5},
{"F6", HID_KEYBOARD_F6},
{"F7", HID_KEYBOARD_F7},
{"F8", HID_KEYBOARD_F8},
{"F9", HID_KEYBOARD_F9},
{"F10", HID_KEYBOARD_F10},
{"F11", HID_KEYBOARD_F11},
{"F12", HID_KEYBOARD_F12},
};
static const char ducky_cmd_comment[] = {"REM"};
static const char ducky_cmd_id[] = {"ID"};
static const char ducky_cmd_delay[] = {"DELAY "};
static const char ducky_cmd_string[] = {"STRING "};
static const char ducky_cmd_defdelay_1[] = {"DEFAULT_DELAY "};
static const char ducky_cmd_defdelay_2[] = {"DEFAULTDELAY "};
static const char ducky_cmd_repeat[] = {"REPEAT "};
static const char ducky_cmd_altchar[] = {"ALTCHAR "};
static const char ducky_cmd_altstr_1[] = {"ALTSTRING "};
static const char ducky_cmd_altstr_2[] = {"ALTCODE "};
static const uint8_t numpad_keys[10] = {
HID_KEYPAD_0,
HID_KEYPAD_1,
HID_KEYPAD_2,
HID_KEYPAD_3,
HID_KEYPAD_4,
HID_KEYPAD_5,
HID_KEYPAD_6,
HID_KEYPAD_7,
HID_KEYPAD_8,
HID_KEYPAD_9,
};
static bool ducky_get_number(const char* param, uint32_t* val) {
uint32_t value = 0;
if(sscanf(param, "%lu", &value) == 1) {
*val = value;
return true;
}
return false;
}
static uint32_t ducky_get_command_len(const char* line) {
uint32_t len = strlen(line);
for(uint32_t i = 0; i < len; i++) {
if(line[i] == ' ') return i;
}
return 0;
}
static bool ducky_is_line_end(const char chr) {
return ((chr == ' ') || (chr == '\0') || (chr == '\r') || (chr == '\n'));
}
static void ducky_numlock_on() {
if((furi_hal_hid_get_led_state() & HID_KB_LED_NUM) == 0) {
furi_hal_hid_kb_press(HID_KEYBOARD_LOCK_NUM_LOCK);
furi_hal_hid_kb_release(HID_KEYBOARD_LOCK_NUM_LOCK);
}
}
static bool ducky_numpad_press(const char num) {
if((num < '0') || (num > '9')) return false;
uint16_t key = numpad_keys[num - '0'];
furi_hal_hid_kb_press(key);
furi_hal_hid_kb_release(key);
return true;
}
static bool ducky_altchar(const char* charcode) {
uint8_t i = 0;
bool state = false;
FURI_LOG_I(WORKER_TAG, "char %s", charcode);
furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT);
while(!ducky_is_line_end(charcode[i])) {
state = ducky_numpad_press(charcode[i]);
if(state == false) break;
i++;
}
furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT);
return state;
}
static bool ducky_altstring(const char* param) {
uint32_t i = 0;
bool state = false;
while(param[i] != '\0') {
if((param[i] < ' ') || (param[i] > '~')) {
i++;
continue; // Skip non-printable chars
}
char temp_str[4];
snprintf(temp_str, 4, "%u", param[i]);
state = ducky_altchar(temp_str);
if(state == false) break;
i++;
}
return state;
}
static bool ducky_string(const char* param) {
uint32_t i = 0;
while(param[i] != '\0') {
uint16_t keycode = HID_ASCII_TO_KEY(param[i]);
if(keycode != HID_KEYBOARD_NONE) {
furi_hal_hid_kb_press(keycode);
furi_hal_hid_kb_release(keycode);
}
i++;
}
return true;
}
static uint16_t ducky_get_keycode(const char* param, bool accept_chars) {
for(uint8_t i = 0; i < (sizeof(ducky_keys) / sizeof(ducky_keys[0])); i++) {
uint8_t key_cmd_len = strlen(ducky_keys[i].name);
if((strncmp(param, ducky_keys[i].name, key_cmd_len) == 0) &&
(ducky_is_line_end(param[key_cmd_len]))) {
return ducky_keys[i].keycode;
}
}
if((accept_chars) && (strlen(param) > 0)) {
return (HID_ASCII_TO_KEY(param[0]) & 0xFF);
}
return 0;
}
static int32_t ducky_parse_line(BadUsbScript* bad_usb, string_t line) {
uint32_t line_len = string_size(line);
const char* line_tmp = string_get_cstr(line);
bool state = false;
for(uint32_t i = 0; i < line_len; i++) {
if((line_tmp[i] != ' ') && (line_tmp[i] != '\t') && (line_tmp[i] != '\n')) {
line_tmp = &line_tmp[i];
break; // Skip spaces and tabs
}
if(i == line_len - 1) return SCRIPT_STATE_NEXT_LINE; // Skip empty lines
}
FURI_LOG_D(WORKER_TAG, "line:%s", line_tmp);
// General commands
if(strncmp(line_tmp, ducky_cmd_comment, strlen(ducky_cmd_comment)) == 0) {
// REM - comment line
return (0);
} else if(strncmp(line_tmp, ducky_cmd_id, strlen(ducky_cmd_id)) == 0) {
// ID - executed in ducky_script_preload
return (0);
} else if(strncmp(line_tmp, ducky_cmd_delay, strlen(ducky_cmd_delay)) == 0) {
// DELAY
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
uint32_t delay_val = 0;
state = ducky_get_number(line_tmp, &delay_val);
if((state) && (delay_val > 0)) {
return (int32_t)delay_val;
}
return SCRIPT_STATE_ERROR;
} else if(
(strncmp(line_tmp, ducky_cmd_defdelay_1, strlen(ducky_cmd_defdelay_1)) == 0) ||
(strncmp(line_tmp, ducky_cmd_defdelay_2, strlen(ducky_cmd_defdelay_2)) == 0)) {
// DEFAULT_DELAY
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
state = ducky_get_number(line_tmp, &bad_usb->defdelay);
return (state) ? (0) : SCRIPT_STATE_ERROR;
} else if(strncmp(line_tmp, ducky_cmd_string, strlen(ducky_cmd_string)) == 0) {
// STRING
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
state = ducky_string(line_tmp);
return (state) ? (0) : SCRIPT_STATE_ERROR;
} else if(strncmp(line_tmp, ducky_cmd_altchar, strlen(ducky_cmd_altchar)) == 0) {
// ALTCHAR
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
ducky_numlock_on();
state = ducky_altchar(line_tmp);
return (state) ? (0) : SCRIPT_STATE_ERROR;
} else if(
(strncmp(line_tmp, ducky_cmd_altstr_1, strlen(ducky_cmd_altstr_1)) == 0) ||
(strncmp(line_tmp, ducky_cmd_altstr_2, strlen(ducky_cmd_altstr_2)) == 0)) {
// ALTSTRING
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
ducky_numlock_on();
state = ducky_altstring(line_tmp);
return (state) ? (0) : SCRIPT_STATE_ERROR;
} else if(strncmp(line_tmp, ducky_cmd_repeat, strlen(ducky_cmd_repeat)) == 0) {
// REPEAT
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
state = ducky_get_number(line_tmp, &bad_usb->repeat_cnt);
return (state) ? (0) : SCRIPT_STATE_ERROR;
} else {
// Special keys + modifiers
uint16_t key = ducky_get_keycode(line_tmp, false);
if(key == HID_KEYBOARD_NONE) return SCRIPT_STATE_ERROR;
if((key & 0xFF00) != 0) {
// It's a modifier key
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
key |= ducky_get_keycode(line_tmp, true);
}
furi_hal_hid_kb_press(key);
furi_hal_hid_kb_release(key);
return (0);
}
return SCRIPT_STATE_ERROR;
}
static bool ducky_set_usb_id(BadUsbScript* bad_usb, const char* line) {
if(sscanf(line, "%lX:%lX", &bad_usb->hid_cfg.vid, &bad_usb->hid_cfg.pid) == 2) {
bad_usb->hid_cfg.manuf[0] = '\0';
bad_usb->hid_cfg.product[0] = '\0';
uint8_t id_len = ducky_get_command_len(line);
if(!ducky_is_line_end(line[id_len + 1])) {
sscanf(
&line[id_len + 1],
"%31[^\r\n:]:%31[^\r\n]",
bad_usb->hid_cfg.manuf,
bad_usb->hid_cfg.product);
}
FURI_LOG_D(
WORKER_TAG,
"set id: %04X:%04X mfr:%s product:%s",
bad_usb->hid_cfg.vid,
bad_usb->hid_cfg.pid,
bad_usb->hid_cfg.manuf,
bad_usb->hid_cfg.product);
return true;
}
return false;
}
static bool ducky_script_preload(BadUsbScript* bad_usb, File* script_file) {
uint8_t ret = 0;
uint32_t line_len = 0;
string_reset(bad_usb->line);
do {
ret = storage_file_read(script_file, bad_usb->file_buf, FILE_BUFFER_LEN);
for(uint16_t i = 0; i < ret; i++) {
if(bad_usb->file_buf[i] == '\n' && line_len > 0) {
bad_usb->st.line_nb++;
line_len = 0;
} else {
if(bad_usb->st.line_nb == 0) { // Save first line
string_push_back(bad_usb->line, bad_usb->file_buf[i]);
}
line_len++;
}
}
if(storage_file_eof(script_file)) {
if(line_len > 0) {
bad_usb->st.line_nb++;
break;
}
}
} while(ret > 0);
const char* line_tmp = string_get_cstr(bad_usb->line);
bool id_set = false; // Looking for ID command at first line
if(strncmp(line_tmp, ducky_cmd_id, strlen(ducky_cmd_id)) == 0) {
id_set = ducky_set_usb_id(bad_usb, &line_tmp[strlen(ducky_cmd_id) + 1]);
}
if(id_set) {
furi_check(furi_hal_usb_set_config(&usb_hid, &bad_usb->hid_cfg));
} else {
furi_check(furi_hal_usb_set_config(&usb_hid, NULL));
}
storage_file_seek(script_file, 0, true);
string_reset(bad_usb->line);
return true;
}
static int32_t ducky_script_execute_next(BadUsbScript* bad_usb, File* script_file) {
int32_t delay_val = 0;
if(bad_usb->repeat_cnt > 0) {
bad_usb->repeat_cnt--;
delay_val = ducky_parse_line(bad_usb, bad_usb->line_prev);
if(delay_val == SCRIPT_STATE_NEXT_LINE) { // Empty line
return 0;
} else if(delay_val < 0) { // Script error
bad_usb->st.error_line = bad_usb->st.line_cur - 1;
FURI_LOG_E(WORKER_TAG, "Unknown command at line %lu", bad_usb->st.line_cur - 1);
return SCRIPT_STATE_ERROR;
} else {
return (delay_val + bad_usb->defdelay);
}
}
string_set(bad_usb->line_prev, bad_usb->line);
string_reset(bad_usb->line);
while(1) {
if(bad_usb->buf_len == 0) {
bad_usb->buf_len = storage_file_read(script_file, bad_usb->file_buf, FILE_BUFFER_LEN);
if(storage_file_eof(script_file)) {
if((bad_usb->buf_len < FILE_BUFFER_LEN) && (bad_usb->file_end == false)) {
bad_usb->file_buf[bad_usb->buf_len] = '\n';
bad_usb->buf_len++;
bad_usb->file_end = true;
}
}
bad_usb->buf_start = 0;
if(bad_usb->buf_len == 0) return SCRIPT_STATE_END;
}
for(uint8_t i = bad_usb->buf_start; i < (bad_usb->buf_start + bad_usb->buf_len); i++) {
if(bad_usb->file_buf[i] == '\n' && string_size(bad_usb->line) > 0) {
bad_usb->st.line_cur++;
bad_usb->buf_len = bad_usb->buf_len + bad_usb->buf_start - (i + 1);
bad_usb->buf_start = i + 1;
delay_val = ducky_parse_line(bad_usb, bad_usb->line);
if(delay_val < 0) {
bad_usb->st.error_line = bad_usb->st.line_cur;
FURI_LOG_E(WORKER_TAG, "Unknown command at line %lu", bad_usb->st.line_cur);
return SCRIPT_STATE_ERROR;
} else {
return (delay_val + bad_usb->defdelay);
}
} else {
string_push_back(bad_usb->line, bad_usb->file_buf[i]);
}
}
bad_usb->buf_len = 0;
if(bad_usb->file_end) return SCRIPT_STATE_END;
}
return 0;
}
static void bad_usb_hid_state_callback(bool state, void* context) {
furi_assert(context);
BadUsbScript* bad_usb = context;
if(state == true)
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtConnect);
else
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtDisconnect);
}
static int32_t bad_usb_worker(void* context) {
BadUsbScript* bad_usb = context;
BadUsbWorkerState worker_state = BadUsbStateInit;
int32_t delay_val = 0;
FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
FURI_LOG_I(WORKER_TAG, "Init");
File* script_file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
string_init(bad_usb->line);
string_init(bad_usb->line_prev);
furi_hal_hid_set_state_callback(bad_usb_hid_state_callback, bad_usb);
while(1) {
if(worker_state == BadUsbStateInit) { // State: initialization
if(storage_file_open(
script_file,
string_get_cstr(bad_usb->file_path),
FSAM_READ,
FSOM_OPEN_EXISTING)) {
if((ducky_script_preload(bad_usb, script_file)) && (bad_usb->st.line_nb > 0)) {
if(furi_hal_hid_is_connected()) {
worker_state = BadUsbStateIdle; // Ready to run
} else {
worker_state = BadUsbStateNotConnected; // USB not connected
}
} else {
worker_state = BadUsbStateScriptError; // Script preload error
}
} else {
FURI_LOG_E(WORKER_TAG, "File open error");
worker_state = BadUsbStateFileError; // File open error
}
bad_usb->st.state = worker_state;
} else if(worker_state == BadUsbStateNotConnected) { // State: USB not connected
uint32_t flags = furi_thread_flags_wait(
WorkerEvtEnd | WorkerEvtConnect, FuriFlagWaitAny, FuriWaitForever);
furi_check((flags & FuriFlagError) == 0);
if(flags & WorkerEvtEnd) {
break;
} else if(flags & WorkerEvtConnect) {
worker_state = BadUsbStateIdle; // Ready to run
}
bad_usb->st.state = worker_state;
} else if(worker_state == BadUsbStateIdle) { // State: ready to start
uint32_t flags = furi_thread_flags_wait(
WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect,
FuriFlagWaitAny,
FuriWaitForever);
furi_check((flags & FuriFlagError) == 0);
if(flags & WorkerEvtEnd) {
break;
} else if(flags & WorkerEvtToggle) { // Start executing script
DOLPHIN_DEED(DolphinDeedBadUsbPlayScript);
delay_val = 0;
bad_usb->buf_len = 0;
bad_usb->st.line_cur = 0;
bad_usb->defdelay = 0;
bad_usb->repeat_cnt = 0;
bad_usb->file_end = false;
storage_file_seek(script_file, 0, true);
worker_state = BadUsbStateRunning;
} else if(flags & WorkerEvtDisconnect) {
worker_state = BadUsbStateNotConnected; // USB disconnected
}
bad_usb->st.state = worker_state;
} else if(worker_state == BadUsbStateRunning) { // State: running
uint16_t delay_cur = (delay_val > 1000) ? (1000) : (delay_val);
uint32_t flags = furi_thread_flags_wait(
WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect, FuriFlagWaitAny, delay_cur);
delay_val -= delay_cur;
if(!(flags & FuriFlagError)) {
if(flags & WorkerEvtEnd) {
break;
} else if(flags & WorkerEvtToggle) {
worker_state = BadUsbStateIdle; // Stop executing script
furi_hal_hid_kb_release_all();
} else if(flags & WorkerEvtDisconnect) {
worker_state = BadUsbStateNotConnected; // USB disconnected
furi_hal_hid_kb_release_all();
}
bad_usb->st.state = worker_state;
continue;
} else if((flags == FuriFlagErrorTimeout) || (flags == FuriFlagErrorResource)) {
if(delay_val > 0) {
bad_usb->st.delay_remain--;
continue;
}
bad_usb->st.state = BadUsbStateRunning;
delay_val = ducky_script_execute_next(bad_usb, script_file);
if(delay_val == SCRIPT_STATE_ERROR) { // Script error
delay_val = 0;
worker_state = BadUsbStateScriptError;
bad_usb->st.state = worker_state;
} else if(delay_val == SCRIPT_STATE_END) { // End of script
delay_val = 0;
worker_state = BadUsbStateIdle;
bad_usb->st.state = BadUsbStateDone;
furi_hal_hid_kb_release_all();
continue;
} else if(delay_val > 1000) {
bad_usb->st.state = BadUsbStateDelay; // Show long delays
bad_usb->st.delay_remain = delay_val / 1000;
}
} else {
furi_check((flags & FuriFlagError) == 0);
}
} else if(
(worker_state == BadUsbStateFileError) ||
(worker_state == BadUsbStateScriptError)) { // State: error
uint32_t flags = furi_thread_flags_wait(
WorkerEvtEnd, FuriFlagWaitAny, FuriWaitForever); // Waiting for exit command
furi_check((flags & FuriFlagError) == 0);
if(flags & WorkerEvtEnd) {
break;
}
}
}
furi_hal_hid_set_state_callback(NULL, NULL);
furi_hal_usb_set_config(usb_mode_prev, NULL);
storage_file_close(script_file);
storage_file_free(script_file);
string_clear(bad_usb->line);
string_clear(bad_usb->line_prev);
FURI_LOG_I(WORKER_TAG, "End");
return 0;
}
BadUsbScript* bad_usb_script_open(string_t file_path) {
furi_assert(file_path);
BadUsbScript* bad_usb = malloc(sizeof(BadUsbScript));
string_init(bad_usb->file_path);
string_set(bad_usb->file_path, file_path);
bad_usb->st.state = BadUsbStateInit;
bad_usb->thread = furi_thread_alloc();
furi_thread_set_name(bad_usb->thread, "BadUsbWorker");
furi_thread_set_stack_size(bad_usb->thread, 2048);
furi_thread_set_context(bad_usb->thread, bad_usb);
furi_thread_set_callback(bad_usb->thread, bad_usb_worker);
furi_thread_start(bad_usb->thread);
return bad_usb;
}
void bad_usb_script_close(BadUsbScript* bad_usb) {
furi_assert(bad_usb);
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtEnd);
furi_thread_join(bad_usb->thread);
furi_thread_free(bad_usb->thread);
string_clear(bad_usb->file_path);
free(bad_usb);
}
void bad_usb_script_toggle(BadUsbScript* bad_usb) {
furi_assert(bad_usb);
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtToggle);
}
BadUsbState* bad_usb_script_get_state(BadUsbScript* bad_usb) {
furi_assert(bad_usb);
return &(bad_usb->st);
}

View File

@@ -0,0 +1,45 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <furi.h>
#include <m-string.h>
typedef struct BadUsbScript BadUsbScript;
typedef enum {
BadUsbStateInit,
BadUsbStateNotConnected,
BadUsbStateIdle,
BadUsbStateRunning,
BadUsbStateDelay,
BadUsbStateDone,
BadUsbStateScriptError,
BadUsbStateFileError,
} BadUsbWorkerState;
typedef struct {
BadUsbWorkerState state;
uint16_t line_cur;
uint16_t line_nb;
uint32_t delay_remain;
uint16_t error_line;
} BadUsbState;
BadUsbScript* bad_usb_script_open(string_t file_path);
void bad_usb_script_close(BadUsbScript* bad_usb);
void bad_usb_script_start(BadUsbScript* bad_usb);
void bad_usb_script_stop(BadUsbScript* bad_usb);
void bad_usb_script_toggle(BadUsbScript* bad_usb);
BadUsbState* bad_usb_script_get_state(BadUsbScript* bad_usb);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,30 @@
#include "bad_usb_scene.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const bad_usb_scene_on_enter_handlers[])(void*) = {
#include "bad_usb_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const bad_usb_scene_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "bad_usb_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const bad_usb_scene_on_exit_handlers[])(void* context) = {
#include "bad_usb_scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers bad_usb_scene_handlers = {
.on_enter_handlers = bad_usb_scene_on_enter_handlers,
.on_event_handlers = bad_usb_scene_on_event_handlers,
.on_exit_handlers = bad_usb_scene_on_exit_handlers,
.scene_num = BadUsbSceneNum,
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) BadUsbScene##id,
typedef enum {
#include "bad_usb_scene_config.h"
BadUsbSceneNum,
} BadUsbScene;
#undef ADD_SCENE
extern const SceneManagerHandlers bad_usb_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "bad_usb_scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "bad_usb_scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "bad_usb_scene_config.h"
#undef ADD_SCENE

View File

@@ -0,0 +1,3 @@
ADD_SCENE(bad_usb, file_select, FileSelect)
ADD_SCENE(bad_usb, work, Work)
ADD_SCENE(bad_usb, error, Error)

View File

@@ -0,0 +1,65 @@
#include "../bad_usb_app_i.h"
typedef enum {
BadUsbCustomEventErrorBack,
} BadUsbCustomEvent;
static void
bad_usb_scene_error_event_callback(GuiButtonType result, InputType type, void* context) {
furi_assert(context);
BadUsbApp* app = context;
if((result == GuiButtonTypeLeft) && (type == InputTypeShort)) {
view_dispatcher_send_custom_event(app->view_dispatcher, BadUsbCustomEventErrorBack);
}
}
void bad_usb_scene_error_on_enter(void* context) {
BadUsbApp* app = context;
if(app->error == BadUsbAppErrorNoFiles) {
widget_add_icon_element(app->widget, 0, 0, &I_SDQuestion_35x43);
widget_add_string_multiline_element(
app->widget,
81,
4,
AlignCenter,
AlignTop,
FontSecondary,
"No SD card or\napp data found.\nThis app will not\nwork without\nrequired files.");
widget_add_button_element(
app->widget, GuiButtonTypeLeft, "Back", bad_usb_scene_error_event_callback, app);
} else if(app->error == BadUsbAppErrorCloseRpc) {
widget_add_icon_element(app->widget, 78, 0, &I_ActiveConnection_50x64);
widget_add_string_multiline_element(
app->widget, 3, 2, AlignLeft, AlignTop, FontPrimary, "Connection\nis active!");
widget_add_string_multiline_element(
app->widget,
3,
30,
AlignLeft,
AlignTop,
FontSecondary,
"Disconnect from\nPC or phone to\nuse this function.");
}
view_dispatcher_switch_to_view(app->view_dispatcher, BadUsbAppViewError);
}
bool bad_usb_scene_error_on_event(void* context, SceneManagerEvent event) {
BadUsbApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == BadUsbCustomEventErrorBack) {
view_dispatcher_stop(app->view_dispatcher);
consumed = true;
}
}
return consumed;
}
void bad_usb_scene_error_on_exit(void* context) {
BadUsbApp* app = context;
widget_reset(app->widget);
}

View File

@@ -0,0 +1,42 @@
#include "../bad_usb_app_i.h"
#include "furi_hal_power.h"
#include "furi_hal_usb.h"
static bool bad_usb_file_select(BadUsbApp* bad_usb) {
furi_assert(bad_usb);
DialogsFileBrowserOptions browser_options;
dialog_file_browser_set_basic_options(&browser_options, BAD_USB_APP_EXTENSION, &I_badusb_10px);
// Input events and views are managed by file_browser
bool res = dialog_file_browser_show(
bad_usb->dialogs, bad_usb->file_path, bad_usb->file_path, &browser_options);
return res;
}
void bad_usb_scene_file_select_on_enter(void* context) {
BadUsbApp* bad_usb = context;
furi_hal_usb_disable();
if(bad_usb_file_select(bad_usb)) {
scene_manager_next_scene(bad_usb->scene_manager, BadUsbSceneWork);
} else {
furi_hal_usb_enable();
//scene_manager_previous_scene(bad_usb->scene_manager);
view_dispatcher_stop(bad_usb->view_dispatcher);
}
}
bool bad_usb_scene_file_select_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
// BadUsbApp* bad_usb = context;
return false;
}
void bad_usb_scene_file_select_on_exit(void* context) {
UNUSED(context);
// BadUsbApp* bad_usb = context;
}

View File

@@ -0,0 +1,48 @@
#include "../bad_usb_script.h"
#include "../bad_usb_app_i.h"
#include "../views/bad_usb_view.h"
#include "furi_hal.h"
#include "m-string.h"
#include "toolbox/path.h"
void bad_usb_scene_work_ok_callback(InputType type, void* context) {
furi_assert(context);
BadUsbApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, type);
}
bool bad_usb_scene_work_on_event(void* context, SceneManagerEvent event) {
BadUsbApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
bad_usb_script_toggle(app->bad_usb_script);
consumed = true;
} else if(event.type == SceneManagerEventTypeTick) {
bad_usb_set_state(app->bad_usb_view, bad_usb_script_get_state(app->bad_usb_script));
}
return consumed;
}
void bad_usb_scene_work_on_enter(void* context) {
BadUsbApp* app = context;
string_t file_name;
string_init(file_name);
path_extract_filename(app->file_path, file_name, true);
bad_usb_set_file_name(app->bad_usb_view, string_get_cstr(file_name));
app->bad_usb_script = bad_usb_script_open(app->file_path);
string_clear(file_name);
bad_usb_set_state(app->bad_usb_view, bad_usb_script_get_state(app->bad_usb_script));
bad_usb_set_ok_callback(app->bad_usb_view, bad_usb_scene_work_ok_callback, app);
view_dispatcher_switch_to_view(app->view_dispatcher, BadUsbAppViewWork);
}
void bad_usb_scene_work_on_exit(void* context) {
BadUsbApp* app = context;
bad_usb_script_close(app->bad_usb_script);
}

View File

@@ -0,0 +1,171 @@
#include "bad_usb_view.h"
#include "../bad_usb_script.h"
#include <gui/elements.h>
#define MAX_NAME_LEN 64
struct BadUsb {
View* view;
BadUsbOkCallback callback;
void* context;
};
typedef struct {
char file_name[MAX_NAME_LEN];
BadUsbState state;
uint8_t anim_frame;
} BadUsbModel;
static void bad_usb_draw_callback(Canvas* canvas, void* _model) {
BadUsbModel* model = _model;
string_t disp_str;
string_init_set_str(disp_str, model->file_name);
elements_string_fit_width(canvas, disp_str, 128 - 2);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 8, string_get_cstr(disp_str));
string_reset(disp_str);
canvas_draw_icon(canvas, 22, 20, &I_UsbTree_48x22);
if((model->state.state == BadUsbStateIdle) || (model->state.state == BadUsbStateDone)) {
elements_button_center(canvas, "Run");
} else if((model->state.state == BadUsbStateRunning) || (model->state.state == BadUsbStateDelay)) {
elements_button_center(canvas, "Stop");
}
if(model->state.state == BadUsbStateNotConnected) {
canvas_draw_icon(canvas, 4, 22, &I_Clock_18x18);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 127, 27, AlignRight, AlignBottom, "Connect");
canvas_draw_str_aligned(canvas, 127, 39, AlignRight, AlignBottom, "to USB");
} else if(model->state.state == BadUsbStateFileError) {
canvas_draw_icon(canvas, 4, 22, &I_Error_18x18);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 127, 27, AlignRight, AlignBottom, "File");
canvas_draw_str_aligned(canvas, 127, 39, AlignRight, AlignBottom, "ERROR");
} else if(model->state.state == BadUsbStateScriptError) {
canvas_draw_icon(canvas, 4, 22, &I_Error_18x18);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 127, 33, AlignRight, AlignBottom, "ERROR:");
canvas_set_font(canvas, FontSecondary);
string_printf(disp_str, "line %u", model->state.error_line);
canvas_draw_str_aligned(
canvas, 127, 46, AlignRight, AlignBottom, string_get_cstr(disp_str));
string_reset(disp_str);
} else if(model->state.state == BadUsbStateIdle) {
canvas_draw_icon(canvas, 4, 22, &I_Smile_18x18);
canvas_set_font(canvas, FontBigNumbers);
canvas_draw_str_aligned(canvas, 114, 36, AlignRight, AlignBottom, "0");
canvas_draw_icon(canvas, 117, 22, &I_Percent_10x14);
} else if(model->state.state == BadUsbStateRunning) {
if(model->anim_frame == 0) {
canvas_draw_icon(canvas, 4, 19, &I_EviSmile1_18x21);
} else {
canvas_draw_icon(canvas, 4, 19, &I_EviSmile2_18x21);
}
canvas_set_font(canvas, FontBigNumbers);
string_printf(disp_str, "%u", ((model->state.line_cur - 1) * 100) / model->state.line_nb);
canvas_draw_str_aligned(
canvas, 114, 36, AlignRight, AlignBottom, string_get_cstr(disp_str));
string_reset(disp_str);
canvas_draw_icon(canvas, 117, 22, &I_Percent_10x14);
} else if(model->state.state == BadUsbStateDone) {
canvas_draw_icon(canvas, 4, 19, &I_EviSmile1_18x21);
canvas_set_font(canvas, FontBigNumbers);
canvas_draw_str_aligned(canvas, 114, 36, AlignRight, AlignBottom, "100");
string_reset(disp_str);
canvas_draw_icon(canvas, 117, 22, &I_Percent_10x14);
} else if(model->state.state == BadUsbStateDelay) {
if(model->anim_frame == 0) {
canvas_draw_icon(canvas, 4, 19, &I_EviWaiting1_18x21);
} else {
canvas_draw_icon(canvas, 4, 19, &I_EviWaiting2_18x21);
}
canvas_set_font(canvas, FontBigNumbers);
string_printf(disp_str, "%u", ((model->state.line_cur - 1) * 100) / model->state.line_nb);
canvas_draw_str_aligned(
canvas, 114, 36, AlignRight, AlignBottom, string_get_cstr(disp_str));
string_reset(disp_str);
canvas_draw_icon(canvas, 117, 22, &I_Percent_10x14);
canvas_set_font(canvas, FontSecondary);
string_printf(disp_str, "delay %us", model->state.delay_remain);
canvas_draw_str_aligned(
canvas, 127, 46, AlignRight, AlignBottom, string_get_cstr(disp_str));
string_reset(disp_str);
} else {
canvas_draw_icon(canvas, 4, 22, &I_Clock_18x18);
}
string_clear(disp_str);
}
static bool bad_usb_input_callback(InputEvent* event, void* context) {
furi_assert(context);
BadUsb* bad_usb = context;
bool consumed = false;
if(event->type == InputTypeShort) {
if(event->key == InputKeyOk) {
consumed = true;
furi_assert(bad_usb->callback);
bad_usb->callback(InputTypeShort, bad_usb->context);
}
}
return consumed;
}
BadUsb* bad_usb_alloc() {
BadUsb* bad_usb = malloc(sizeof(BadUsb));
bad_usb->view = view_alloc();
view_allocate_model(bad_usb->view, ViewModelTypeLocking, sizeof(BadUsbModel));
view_set_context(bad_usb->view, bad_usb);
view_set_draw_callback(bad_usb->view, bad_usb_draw_callback);
view_set_input_callback(bad_usb->view, bad_usb_input_callback);
return bad_usb;
}
void bad_usb_free(BadUsb* bad_usb) {
furi_assert(bad_usb);
view_free(bad_usb->view);
free(bad_usb);
}
View* bad_usb_get_view(BadUsb* bad_usb) {
furi_assert(bad_usb);
return bad_usb->view;
}
void bad_usb_set_ok_callback(BadUsb* bad_usb, BadUsbOkCallback callback, void* context) {
furi_assert(bad_usb);
furi_assert(callback);
with_view_model(
bad_usb->view, (BadUsbModel * model) {
UNUSED(model);
bad_usb->callback = callback;
bad_usb->context = context;
return true;
});
}
void bad_usb_set_file_name(BadUsb* bad_usb, const char* name) {
furi_assert(name);
with_view_model(
bad_usb->view, (BadUsbModel * model) {
strlcpy(model->file_name, name, MAX_NAME_LEN);
return true;
});
}
void bad_usb_set_state(BadUsb* bad_usb, BadUsbState* st) {
furi_assert(st);
with_view_model(
bad_usb->view, (BadUsbModel * model) {
memcpy(&(model->state), st, sizeof(BadUsbState));
model->anim_frame ^= 1;
return true;
});
}

View File

@@ -0,0 +1,19 @@
#pragma once
#include <gui/view.h>
#include "../bad_usb_script.h"
typedef struct BadUsb BadUsb;
typedef void (*BadUsbOkCallback)(InputType type, void* context);
BadUsb* bad_usb_alloc();
void bad_usb_free(BadUsb* bad_usb);
View* bad_usb_get_view(BadUsb* bad_usb);
void bad_usb_set_ok_callback(BadUsb* bad_usb, BadUsbOkCallback callback, void* context);
void bad_usb_set_file_name(BadUsb* bad_usb, const char* name);
void bad_usb_set_state(BadUsb* bad_usb, BadUsbState* st);

View File

@@ -0,0 +1,13 @@
App(
appid="fap_loader",
name="Applications",
apptype=FlipperAppType.APP,
entry_point="fap_loader_app",
requires=[
"gui",
"storage",
],
stack_size=int(1.5 * 1024),
icon="A_Plugins_14",
order=90,
)

View File

@@ -0,0 +1,111 @@
/**
* Implementation of compile-time sort for symbol table entries.
*/
#pragma once
#include <iterator>
#include <array>
namespace cstd {
template <typename RAIt>
constexpr RAIt next(RAIt it, typename std::iterator_traits<RAIt>::difference_type n = 1) {
return it + n;
}
template <typename RAIt>
constexpr auto distance(RAIt first, RAIt last) {
return last - first;
}
template <class ForwardIt1, class ForwardIt2>
constexpr void iter_swap(ForwardIt1 a, ForwardIt2 b) {
auto temp = std::move(*a);
*a = std::move(*b);
*b = std::move(temp);
}
template <class InputIt, class UnaryPredicate>
constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q) {
for(; first != last; ++first) {
if(!q(*first)) {
return first;
}
}
return last;
}
template <class ForwardIt, class UnaryPredicate>
constexpr ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPredicate p) {
first = cstd::find_if_not(first, last, p);
if(first == last) return first;
for(ForwardIt i = cstd::next(first); i != last; ++i) {
if(p(*i)) {
cstd::iter_swap(i, first);
++first;
}
}
return first;
}
}
template <class RAIt, class Compare = std::less<> >
constexpr void quick_sort(RAIt first, RAIt last, Compare cmp = Compare{}) {
auto const N = cstd::distance(first, last);
if(N <= 1) return;
auto const pivot = *cstd::next(first, N / 2);
auto const middle1 =
cstd::partition(first, last, [=](auto const& elem) { return cmp(elem, pivot); });
auto const middle2 =
cstd::partition(middle1, last, [=](auto const& elem) { return !cmp(pivot, elem); });
quick_sort(first, middle1, cmp); // assert(std::is_sorted(first, middle1, cmp));
quick_sort(middle2, last, cmp); // assert(std::is_sorted(middle2, last, cmp));
}
template <typename Range>
constexpr auto sort(Range&& range) {
quick_sort(std::begin(range), std::end(range));
return range;
}
template <typename V, typename... T>
constexpr auto array_of(T&&... t) -> std::array<V, sizeof...(T)> {
return {{std::forward<T>(t)...}};
}
template <typename T, typename... N>
constexpr auto my_make_array(N&&... args) -> std::array<T, sizeof...(args)> {
return {std::forward<N>(args)...};
}
namespace traits {
template <typename T, typename... Ts>
struct array_type {
using type = T;
};
template <typename T, typename... Ts>
static constexpr bool are_same_type() {
return std::conjunction_v<std::is_same<T, Ts>...>;
}
}
template <typename... T>
constexpr auto create_array(const T&&... values) {
using array_type = typename traits::array_type<T...>::type;
static_assert(sizeof...(T) > 0, "an array must have at least one element");
static_assert(traits::are_same_type<T...>(), "all elements must have same type");
return std::array<array_type, sizeof...(T)>{values...};
}
template <typename T, typename... Ts>
constexpr auto create_array_t(const Ts&&... values) {
using array_type = T;
static_assert(sizeof...(Ts) > 0, "an array must have at least one element");
static_assert(traits::are_same_type<Ts...>(), "all elements must have same type");
return std::array<array_type, sizeof...(Ts)>{static_cast<T>(values)...};
}

View File

@@ -0,0 +1,48 @@
#include "compilesort.hpp"
#include "elf_hashtable.h"
#include "elf_hashtable_entry.h"
#include "elf_hashtable_checks.hpp"
#include <array>
#include <algorithm>
/* Generated table */
#include <symbols.h>
#define TAG "elf_hashtable"
static_assert(!has_hash_collisions(elf_api_table), "Detected API method hash collision!");
/**
* Get function address by function name
* @param name function name
* @param address output for function address
* @return true if the table contains a function
*/
bool elf_resolve_from_hashtable(const char* name, Elf32_Addr* address) {
bool result = false;
uint32_t gnu_sym_hash = elf_gnu_hash(name);
sym_entry key = {
.hash = gnu_sym_hash,
.address = 0,
};
auto find_res = std::lower_bound(elf_api_table.cbegin(), elf_api_table.cend(), key);
if((find_res == elf_api_table.cend() || (find_res->hash != gnu_sym_hash))) {
FURI_LOG_W(TAG, "Cant find symbol '%s' (hash %x)!", name, gnu_sym_hash);
result = false;
} else {
result = true;
*address = find_res->address;
}
return result;
}
const ElfApiInterface hashtable_api_interface = {
.api_version_major = (elf_api_version >> 16),
.api_version_minor = (elf_api_version & 0xFFFF),
.resolver_callback = &elf_resolve_from_hashtable,
};

View File

@@ -0,0 +1,14 @@
#pragma once
#include <stdint.h>
#include <flipper_application/elf/elf_api_interface.h>
#ifdef __cplusplus
extern "C" {
#endif
extern const ElfApiInterface hashtable_api_interface;
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,18 @@
/**
* Check for multiple entries with the same hash value at compilation time.
*/
#pragma once
#include <array>
#include "elf_hashtable_entry.h"
template <std::size_t N>
constexpr bool has_hash_collisions(const std::array<sym_entry, N> api_methods) {
for(std::size_t i = 0; i < (N - 1); ++i) {
if(api_methods[i].hash == api_methods[i + 1].hash) {
return true;
}
}
return false;
}

View File

@@ -0,0 +1,41 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
struct sym_entry {
uint32_t hash;
uint32_t address;
};
#ifdef __cplusplus
}
#include <array>
#include <algorithm>
#define API_METHOD(x, ret_type, args_type) \
sym_entry { \
.hash = elf_gnu_hash(#x), .address = (uint32_t)(static_cast<ret_type(*) args_type>(x)) \
}
#define API_VARIABLE(x, var_type) \
sym_entry { \
.hash = elf_gnu_hash(#x), .address = (uint32_t)(&(x)), \
}
constexpr bool operator<(const sym_entry& k1, const sym_entry& k2) {
return k1.hash < k2.hash;
}
constexpr uint32_t elf_gnu_hash(const char* s) {
uint32_t h = 0x1505;
for(unsigned char c = *s; c != '\0'; c = *++s) {
h = (h << 5) + h + c;
}
return h;
}
#endif

View File

@@ -0,0 +1,176 @@
#include <furi.h>
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/modules/loading.h>
#include <storage/storage.h>
#include <dialogs/dialogs.h>
#include "elf_cpp/elf_hashtable.h"
#include <flipper_application/flipper_application.h>
#define TAG "fap_loader_app"
typedef struct {
FlipperApplication* app;
Storage* storage;
DialogsApp* dialogs;
Gui* gui;
string_t fap_path;
} FapLoader;
static bool
fap_loader_item_callback(string_t path, void* context, uint8_t** icon_ptr, string_t item_name) {
FapLoader* loader = context;
furi_assert(loader);
FlipperApplication* app = flipper_application_alloc(loader->storage, &hashtable_api_interface);
FlipperApplicationPreloadStatus preload_res =
flipper_application_preload(app, string_get_cstr(path));
bool load_success = false;
if(preload_res == FlipperApplicationPreloadStatusSuccess) {
const FlipperApplicationManifest* manifest = flipper_application_get_manifest(app);
if(manifest->has_icon) {
memcpy(*icon_ptr, manifest->icon, FAP_MANIFEST_MAX_ICON_SIZE);
}
string_set_str(item_name, manifest->name);
load_success = true;
} else {
FURI_LOG_E(TAG, "FAP Loader failed to preload %s", string_get_cstr(path));
load_success = false;
}
flipper_application_free(app);
return load_success;
}
static bool fap_loader_run_selected_app(FapLoader* loader) {
furi_assert(loader);
string_t error_message;
string_init_set(error_message, "unknown error");
bool file_selected = false;
bool show_error = true;
do {
file_selected = true;
loader->app = flipper_application_alloc(loader->storage, &hashtable_api_interface);
FURI_LOG_I(TAG, "FAP Loader is loading %s", string_get_cstr(loader->fap_path));
FlipperApplicationPreloadStatus preload_res =
flipper_application_preload(loader->app, string_get_cstr(loader->fap_path));
if(preload_res != FlipperApplicationPreloadStatusSuccess) {
const char* err_msg = flipper_application_preload_status_to_string(preload_res);
string_printf(error_message, "Preload failed: %s", err_msg);
FURI_LOG_E(
TAG,
"FAP Loader failed to preload %s: %s",
string_get_cstr(loader->fap_path),
err_msg);
break;
}
FURI_LOG_I(TAG, "FAP Loader is mapping");
FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(loader->app);
if(load_status != FlipperApplicationLoadStatusSuccess) {
const char* err_msg = flipper_application_load_status_to_string(load_status);
string_printf(error_message, "Load failed: %s", err_msg);
FURI_LOG_E(
TAG,
"FAP Loader failed to map to memory %s: %s",
string_get_cstr(loader->fap_path),
err_msg);
break;
}
FURI_LOG_I(TAG, "FAP Loader is staring app");
FuriThread* thread = flipper_application_spawn(loader->app, NULL);
furi_thread_start(thread);
furi_thread_join(thread);
show_error = false;
int ret = furi_thread_get_return_code(thread);
FURI_LOG_I(TAG, "FAP app returned: %i", ret);
} while(0);
if(show_error) {
DialogMessage* message = dialog_message_alloc();
dialog_message_set_header(message, "Error", 64, 0, AlignCenter, AlignTop);
dialog_message_set_buttons(message, NULL, NULL, NULL);
string_t buffer;
string_init(buffer);
string_printf(buffer, "%s", string_get_cstr(error_message));
string_replace_str(buffer, ":", "\n");
dialog_message_set_text(
message, string_get_cstr(buffer), 64, 32, AlignCenter, AlignCenter);
dialog_message_show(loader->dialogs, message);
dialog_message_free(message);
string_clear(buffer);
}
string_clear(error_message);
if(file_selected) {
flipper_application_free(loader->app);
}
return file_selected;
}
static bool fap_loader_select_app(FapLoader* loader) {
const DialogsFileBrowserOptions browser_options = {
.extension = ".fap",
.skip_assets = true,
.icon = &I_badusb_10px,
.hide_ext = true,
.item_loader_callback = fap_loader_item_callback,
.item_loader_context = loader,
};
return dialog_file_browser_show(
loader->dialogs, loader->fap_path, loader->fap_path, &browser_options);
}
int32_t fap_loader_app(void* p) {
FapLoader* loader = malloc(sizeof(FapLoader));
loader->storage = furi_record_open(RECORD_STORAGE);
loader->dialogs = furi_record_open(RECORD_DIALOGS);
loader->gui = furi_record_open(RECORD_GUI);
ViewDispatcher* view_dispatcher = view_dispatcher_alloc();
Loading* loading = loading_alloc();
view_dispatcher_enable_queue(view_dispatcher);
view_dispatcher_attach_to_gui(view_dispatcher, loader->gui, ViewDispatcherTypeFullscreen);
view_dispatcher_add_view(view_dispatcher, 0, loading_get_view(loading));
if(p) {
string_init_set(loader->fap_path, (const char*)p);
fap_loader_run_selected_app(loader);
} else {
string_init_set(loader->fap_path, EXT_PATH("apps"));
while(fap_loader_select_app(loader)) {
view_dispatcher_switch_to_view(view_dispatcher, 0);
fap_loader_run_selected_app(loader);
};
}
view_dispatcher_remove_view(view_dispatcher, 0);
loading_free(loading);
view_dispatcher_free(view_dispatcher);
string_clear(loader->fap_path);
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_DIALOGS);
furi_record_close(RECORD_STORAGE);
free(loader);
return 0;
}

View File

@@ -0,0 +1,11 @@
App(
appid="gpio",
name="GPIO",
apptype=FlipperAppType.APP,
entry_point="gpio_app",
cdefines=["APP_GPIO"],
requires=["gui"],
stack_size=1 * 1024,
icon="A_GPIO_14",
order=50,
)

View File

@@ -0,0 +1,106 @@
#include "gpio_app_i.h"
#include <furi.h>
#include <furi_hal.h>
static bool gpio_app_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
GpioApp* app = context;
return scene_manager_handle_custom_event(app->scene_manager, event);
}
static bool gpio_app_back_event_callback(void* context) {
furi_assert(context);
GpioApp* app = context;
return scene_manager_handle_back_event(app->scene_manager);
}
static void gpio_app_tick_event_callback(void* context) {
furi_assert(context);
GpioApp* app = context;
scene_manager_handle_tick_event(app->scene_manager);
}
GpioApp* gpio_app_alloc() {
GpioApp* app = malloc(sizeof(GpioApp));
app->gui = furi_record_open(RECORD_GUI);
app->view_dispatcher = view_dispatcher_alloc();
app->scene_manager = scene_manager_alloc(&gpio_scene_handlers, app);
view_dispatcher_enable_queue(app->view_dispatcher);
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
view_dispatcher_set_custom_event_callback(
app->view_dispatcher, gpio_app_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
app->view_dispatcher, gpio_app_back_event_callback);
view_dispatcher_set_tick_event_callback(
app->view_dispatcher, gpio_app_tick_event_callback, 100);
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
app->notifications = furi_record_open(RECORD_NOTIFICATION);
app->var_item_list = variable_item_list_alloc();
view_dispatcher_add_view(
app->view_dispatcher,
GpioAppViewVarItemList,
variable_item_list_get_view(app->var_item_list));
app->gpio_test = gpio_test_alloc();
view_dispatcher_add_view(
app->view_dispatcher, GpioAppViewGpioTest, gpio_test_get_view(app->gpio_test));
app->widget = widget_alloc();
view_dispatcher_add_view(
app->view_dispatcher, GpioAppViewUsbUartCloseRpc, widget_get_view(app->widget));
app->gpio_usb_uart = gpio_usb_uart_alloc();
view_dispatcher_add_view(
app->view_dispatcher, GpioAppViewUsbUart, gpio_usb_uart_get_view(app->gpio_usb_uart));
view_dispatcher_add_view(
app->view_dispatcher,
GpioAppViewUsbUartCfg,
variable_item_list_get_view(app->var_item_list));
scene_manager_next_scene(app->scene_manager, GpioSceneStart);
return app;
}
void gpio_app_free(GpioApp* app) {
furi_assert(app);
// Views
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewVarItemList);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewGpioTest);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewUsbUart);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewUsbUartCfg);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewUsbUartCloseRpc);
variable_item_list_free(app->var_item_list);
widget_free(app->widget);
gpio_test_free(app->gpio_test);
gpio_usb_uart_free(app->gpio_usb_uart);
// View dispatcher
view_dispatcher_free(app->view_dispatcher);
scene_manager_free(app->scene_manager);
// Close records
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_NOTIFICATION);
free(app);
}
int32_t gpio_app(void* p) {
UNUSED(p);
GpioApp* gpio_app = gpio_app_alloc();
view_dispatcher_run(gpio_app->view_dispatcher);
gpio_app_free(gpio_app);
return 0;
}

View File

@@ -0,0 +1,11 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef struct GpioApp GpioApp;
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,38 @@
#pragma once
#include "gpio_app.h"
#include "gpio_item.h"
#include "scenes/gpio_scene.h"
#include "gpio_custom_event.h"
#include "usb_uart_bridge.h"
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/submenu.h>
#include <notification/notification_messages.h>
#include <gui/modules/variable_item_list.h>
#include <gui/modules/widget.h>
#include "views/gpio_test.h"
#include "views/gpio_usb_uart.h"
struct GpioApp {
Gui* gui;
NotificationApp* notifications;
ViewDispatcher* view_dispatcher;
SceneManager* scene_manager;
Widget* widget;
VariableItemList* var_item_list;
GpioTest* gpio_test;
GpioUsbUart* gpio_usb_uart;
UsbUartBridge* usb_uart_bridge;
};
typedef enum {
GpioAppViewVarItemList,
GpioAppViewGpioTest,
GpioAppViewUsbUart,
GpioAppViewUsbUartCfg,
GpioAppViewUsbUartCloseRpc,
} GpioAppView;

View File

@@ -0,0 +1,12 @@
#pragma once
typedef enum {
GpioStartEventOtgOff = 0,
GpioStartEventOtgOn,
GpioStartEventManualControl,
GpioStartEventUsbUart,
GpioCustomEventErrorBack,
GpioUsbUartEventConfig,
} GpioCustomEvent;

View File

@@ -0,0 +1,51 @@
#include "gpio_item.h"
#include <furi_hal_resources.h>
typedef struct {
const char* name;
const GpioPin* pin;
} GpioItem;
static const GpioItem gpio_item[GPIO_ITEM_COUNT] = {
{"1.2: PA7", &gpio_ext_pa7},
{"1.3: PA6", &gpio_ext_pa6},
{"1.4: PA4", &gpio_ext_pa4},
{"1.5: PB3", &gpio_ext_pb3},
{"1.6: PB2", &gpio_ext_pb2},
{"1.7: PC3", &gpio_ext_pc3},
{"2.7: PC1", &gpio_ext_pc1},
{"2.8: PC0", &gpio_ext_pc0},
};
void gpio_item_configure_pin(uint8_t index, GpioMode mode) {
furi_assert(index < GPIO_ITEM_COUNT);
furi_hal_gpio_write(gpio_item[index].pin, false);
furi_hal_gpio_init(gpio_item[index].pin, mode, GpioPullNo, GpioSpeedVeryHigh);
}
void gpio_item_configure_all_pins(GpioMode mode) {
for(uint8_t i = 0; i < GPIO_ITEM_COUNT; i++) {
gpio_item_configure_pin(i, mode);
}
}
void gpio_item_set_pin(uint8_t index, bool level) {
furi_assert(index < GPIO_ITEM_COUNT);
furi_hal_gpio_write(gpio_item[index].pin, level);
}
void gpio_item_set_all_pins(bool level) {
for(uint8_t i = 0; i < GPIO_ITEM_COUNT; i++) {
gpio_item_set_pin(i, level);
}
}
const char* gpio_item_get_pin_name(uint8_t index) {
furi_assert(index < GPIO_ITEM_COUNT + 1);
if(index == GPIO_ITEM_COUNT) {
return "ALL";
} else {
return gpio_item[index].name;
}
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <furi_hal_gpio.h>
#define GPIO_ITEM_COUNT 8
void gpio_item_configure_pin(uint8_t index, GpioMode mode);
void gpio_item_configure_all_pins(GpioMode mode);
void gpio_item_set_pin(uint8_t index, bool level);
void gpio_item_set_all_pins(bool level);
const char* gpio_item_get_pin_name(uint8_t index);

View File

@@ -0,0 +1,30 @@
#include "gpio_scene.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const gpio_scene_on_enter_handlers[])(void*) = {
#include "gpio_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const gpio_scene_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "gpio_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const gpio_scene_on_exit_handlers[])(void* context) = {
#include "gpio_scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers gpio_scene_handlers = {
.on_enter_handlers = gpio_scene_on_enter_handlers,
.on_event_handlers = gpio_scene_on_event_handlers,
.on_exit_handlers = gpio_scene_on_exit_handlers,
.scene_num = GpioSceneNum,
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) GpioScene##id,
typedef enum {
#include "gpio_scene_config.h"
GpioSceneNum,
} GpioScene;
#undef ADD_SCENE
extern const SceneManagerHandlers gpio_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "gpio_scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "gpio_scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "gpio_scene_config.h"
#undef ADD_SCENE

View File

@@ -0,0 +1,5 @@
ADD_SCENE(gpio, start, Start)
ADD_SCENE(gpio, test, Test)
ADD_SCENE(gpio, usb_uart, UsbUart)
ADD_SCENE(gpio, usb_uart_cfg, UsbUartCfg)
ADD_SCENE(gpio, usb_uart_close_rpc, UsbUartCloseRpc)

View File

@@ -0,0 +1,104 @@
#include "../gpio_app_i.h"
#include "furi_hal_power.h"
#include "furi_hal_usb.h"
enum GpioItem {
GpioItemUsbUart,
GpioItemTest,
GpioItemOtg,
};
enum GpioOtg {
GpioOtgOff,
GpioOtgOn,
GpioOtgSettingsNum,
};
const char* const gpio_otg_text[GpioOtgSettingsNum] = {
"OFF",
"ON",
};
static void gpio_scene_start_var_list_enter_callback(void* context, uint32_t index) {
furi_assert(context);
GpioApp* app = context;
if(index == GpioItemTest) {
view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventManualControl);
} else if(index == GpioItemUsbUart) {
view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventUsbUart);
}
}
static void gpio_scene_start_var_list_change_callback(VariableItem* item) {
GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, gpio_otg_text[index]);
if(index == GpioOtgOff) {
view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventOtgOff);
} else if(index == GpioOtgOn) {
view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventOtgOn);
}
}
void gpio_scene_start_on_enter(void* context) {
GpioApp* app = context;
VariableItemList* var_item_list = app->var_item_list;
VariableItem* item;
variable_item_list_set_enter_callback(
var_item_list, gpio_scene_start_var_list_enter_callback, app);
variable_item_list_add(var_item_list, "USB-UART Bridge", 0, NULL, NULL);
variable_item_list_add(var_item_list, "GPIO Manual Control", 0, NULL, NULL);
item = variable_item_list_add(
var_item_list,
"5V on GPIO",
GpioOtgSettingsNum,
gpio_scene_start_var_list_change_callback,
app);
if(furi_hal_power_is_otg_enabled()) {
variable_item_set_current_value_index(item, GpioOtgOn);
variable_item_set_current_value_text(item, gpio_otg_text[GpioOtgOn]);
} else {
variable_item_set_current_value_index(item, GpioOtgOff);
variable_item_set_current_value_text(item, gpio_otg_text[GpioOtgOff]);
}
variable_item_list_set_selected_item(
var_item_list, scene_manager_get_scene_state(app->scene_manager, GpioSceneStart));
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewVarItemList);
}
bool gpio_scene_start_on_event(void* context, SceneManagerEvent event) {
GpioApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GpioStartEventOtgOn) {
furi_hal_power_enable_otg();
} else if(event.event == GpioStartEventOtgOff) {
furi_hal_power_disable_otg();
} else if(event.event == GpioStartEventManualControl) {
scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, GpioItemTest);
scene_manager_next_scene(app->scene_manager, GpioSceneTest);
} else if(event.event == GpioStartEventUsbUart) {
scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, GpioItemUsbUart);
if(!furi_hal_usb_is_locked()) {
scene_manager_next_scene(app->scene_manager, GpioSceneUsbUart);
} else {
scene_manager_next_scene(app->scene_manager, GpioSceneUsbUartCloseRpc);
}
}
consumed = true;
}
return consumed;
}
void gpio_scene_start_on_exit(void* context) {
GpioApp* app = context;
variable_item_list_reset(app->var_item_list);
}

View File

@@ -0,0 +1,30 @@
#include "../gpio_app_i.h"
void gpio_scene_test_ok_callback(InputType type, void* context) {
furi_assert(context);
GpioApp* app = context;
if(type == InputTypePress) {
notification_message(app->notifications, &sequence_set_green_255);
} else if(type == InputTypeRelease) {
notification_message(app->notifications, &sequence_reset_green);
}
}
void gpio_scene_test_on_enter(void* context) {
GpioApp* app = context;
gpio_item_configure_all_pins(GpioModeOutputPushPull);
gpio_test_set_ok_callback(app->gpio_test, gpio_scene_test_ok_callback, app);
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewGpioTest);
}
bool gpio_scene_test_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
return false;
}
void gpio_scene_test_on_exit(void* context) {
UNUSED(context);
gpio_item_configure_all_pins(GpioModeAnalog);
}

View File

@@ -0,0 +1,67 @@
#include "../gpio_app_i.h"
#include "../usb_uart_bridge.h"
typedef struct {
UsbUartConfig cfg;
UsbUartState state;
} SceneUsbUartBridge;
static SceneUsbUartBridge* scene_usb_uart;
void gpio_scene_usb_uart_callback(GpioCustomEvent event, void* context) {
furi_assert(context);
GpioApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, event);
}
void gpio_scene_usb_uart_on_enter(void* context) {
GpioApp* app = context;
uint32_t prev_state = scene_manager_get_scene_state(app->scene_manager, GpioAppViewUsbUart);
if(prev_state == 0) {
scene_usb_uart = malloc(sizeof(SceneUsbUartBridge));
scene_usb_uart->cfg.vcp_ch = 0; // TODO: settings load
scene_usb_uart->cfg.uart_ch = 0;
scene_usb_uart->cfg.flow_pins = 0;
scene_usb_uart->cfg.baudrate_mode = 0;
scene_usb_uart->cfg.baudrate = 0;
app->usb_uart_bridge = usb_uart_enable(&scene_usb_uart->cfg);
}
usb_uart_get_config(app->usb_uart_bridge, &scene_usb_uart->cfg);
usb_uart_get_state(app->usb_uart_bridge, &scene_usb_uart->state);
gpio_usb_uart_set_callback(app->gpio_usb_uart, gpio_scene_usb_uart_callback, app);
scene_manager_set_scene_state(app->scene_manager, GpioSceneUsbUart, 0);
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewUsbUart);
notification_message(app->notifications, &sequence_display_backlight_enforce_on);
}
bool gpio_scene_usb_uart_on_event(void* context, SceneManagerEvent event) {
GpioApp* app = context;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_set_scene_state(app->scene_manager, GpioSceneUsbUart, 1);
scene_manager_next_scene(app->scene_manager, GpioSceneUsbUartCfg);
return true;
} else if(event.type == SceneManagerEventTypeTick) {
uint32_t tx_cnt_last = scene_usb_uart->state.tx_cnt;
uint32_t rx_cnt_last = scene_usb_uart->state.rx_cnt;
usb_uart_get_state(app->usb_uart_bridge, &scene_usb_uart->state);
gpio_usb_uart_update_state(
app->gpio_usb_uart, &scene_usb_uart->cfg, &scene_usb_uart->state);
if(tx_cnt_last != scene_usb_uart->state.tx_cnt)
notification_message(app->notifications, &sequence_blink_blue_10);
if(rx_cnt_last != scene_usb_uart->state.rx_cnt)
notification_message(app->notifications, &sequence_blink_green_10);
}
return false;
}
void gpio_scene_usb_uart_on_exit(void* context) {
GpioApp* app = context;
uint32_t prev_state = scene_manager_get_scene_state(app->scene_manager, GpioSceneUsbUart);
if(prev_state == 0) {
usb_uart_disable(app->usb_uart_bridge);
free(scene_usb_uart);
}
notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
}

View File

@@ -0,0 +1,41 @@
#include "../gpio_app_i.h"
#include "../gpio_custom_event.h"
void gpio_scene_usb_uart_close_rpc_on_enter(void* context) {
GpioApp* app = context;
widget_add_icon_element(app->widget, 78, 0, &I_ActiveConnection_50x64);
widget_add_string_multiline_element(
app->widget, 3, 2, AlignLeft, AlignTop, FontPrimary, "Connection\nis active!");
widget_add_string_multiline_element(
app->widget,
3,
30,
AlignLeft,
AlignTop,
FontSecondary,
"Disconnect from\nPC or phone to\nuse this function.");
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewUsbUartCloseRpc);
}
bool gpio_scene_usb_uart_close_rpc_on_event(void* context, SceneManagerEvent event) {
GpioApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GpioCustomEventErrorBack) {
if(!scene_manager_previous_scene(app->scene_manager)) {
scene_manager_stop(app->scene_manager);
view_dispatcher_stop(app->view_dispatcher);
}
consumed = true;
}
}
return consumed;
}
void gpio_scene_usb_uart_close_rpc_on_exit(void* context) {
GpioApp* app = context;
widget_reset(app->widget);
}

View File

@@ -0,0 +1,137 @@
#include "../usb_uart_bridge.h"
#include "../gpio_app_i.h"
#include "furi_hal.h"
typedef enum {
UsbUartLineIndexVcp,
UsbUartLineIndexBaudrate,
UsbUartLineIndexUart,
UsbUartLineIndexFlow,
} LineIndex;
static UsbUartConfig* cfg_set;
static const char* vcp_ch[] = {"0 (CLI)", "1"};
static const char* uart_ch[] = {"13,14", "15,16"};
static const char* flow_pins[] = {"None", "2,3", "6,7"};
static const char* baudrate_mode[] = {"Host"};
static const uint32_t baudrate_list[] = {
2400,
9600,
19200,
38400,
57600,
115200,
230400,
460800,
921600,
};
bool gpio_scene_usb_uart_cfg_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
return false;
}
static void line_vcp_cb(VariableItem* item) {
GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, vcp_ch[index]);
cfg_set->vcp_ch = index;
usb_uart_set_config(app->usb_uart_bridge, cfg_set);
}
static void line_port_cb(VariableItem* item) {
GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, uart_ch[index]);
if(index == 0)
cfg_set->uart_ch = FuriHalUartIdUSART1;
else if(index == 1)
cfg_set->uart_ch = FuriHalUartIdLPUART1;
usb_uart_set_config(app->usb_uart_bridge, cfg_set);
}
static void line_flow_cb(VariableItem* item) {
GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, flow_pins[index]);
cfg_set->flow_pins = index;
usb_uart_set_config(app->usb_uart_bridge, cfg_set);
}
static void line_baudrate_cb(VariableItem* item) {
GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
char br_text[8];
if(index > 0) {
snprintf(br_text, 7, "%lu", baudrate_list[index - 1]);
variable_item_set_current_value_text(item, br_text);
cfg_set->baudrate = baudrate_list[index - 1];
} else {
variable_item_set_current_value_text(item, baudrate_mode[index]);
cfg_set->baudrate = 0;
}
cfg_set->baudrate_mode = index;
usb_uart_set_config(app->usb_uart_bridge, cfg_set);
}
void gpio_scene_usb_uart_cfg_on_enter(void* context) {
GpioApp* app = context;
VariableItemList* var_item_list = app->var_item_list;
cfg_set = malloc(sizeof(UsbUartConfig));
usb_uart_get_config(app->usb_uart_bridge, cfg_set);
VariableItem* item;
char br_text[8];
item = variable_item_list_add(var_item_list, "USB Channel", 2, line_vcp_cb, app);
variable_item_set_current_value_index(item, cfg_set->vcp_ch);
variable_item_set_current_value_text(item, vcp_ch[cfg_set->vcp_ch]);
item = variable_item_list_add(
var_item_list,
"Baudrate",
sizeof(baudrate_list) / sizeof(baudrate_list[0]) + 1,
line_baudrate_cb,
app);
variable_item_set_current_value_index(item, cfg_set->baudrate_mode);
if(cfg_set->baudrate_mode > 0) {
snprintf(br_text, 7, "%lu", baudrate_list[cfg_set->baudrate_mode - 1]);
variable_item_set_current_value_text(item, br_text);
} else {
variable_item_set_current_value_text(item, baudrate_mode[cfg_set->baudrate_mode]);
}
item = variable_item_list_add(var_item_list, "UART Pins", 2, line_port_cb, app);
variable_item_set_current_value_index(item, cfg_set->uart_ch);
variable_item_set_current_value_text(item, uart_ch[cfg_set->uart_ch]);
item = variable_item_list_add(var_item_list, "RTS/DTR Pins", 3, line_flow_cb, app);
variable_item_set_current_value_index(item, cfg_set->flow_pins);
variable_item_set_current_value_text(item, flow_pins[cfg_set->flow_pins]);
variable_item_list_set_selected_item(
var_item_list, scene_manager_get_scene_state(app->scene_manager, GpioAppViewUsbUartCfg));
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewUsbUartCfg);
}
void gpio_scene_usb_uart_cfg_on_exit(void* context) {
GpioApp* app = context;
scene_manager_set_scene_state(
app->scene_manager,
GpioAppViewUsbUartCfg,
variable_item_list_get_selected_item_index(app->var_item_list));
variable_item_list_reset(app->var_item_list);
free(cfg_set);
}

View File

@@ -0,0 +1,378 @@
#include "usb_uart_bridge.h"
#include "furi_hal.h"
#include <stream_buffer.h>
#include <furi_hal_usb_cdc.h>
#include "usb_cdc.h"
#include "cli/cli_vcp.h"
#include "cli/cli.h"
#define USB_CDC_PKT_LEN CDC_DATA_SZ
#define USB_UART_RX_BUF_SIZE (USB_CDC_PKT_LEN * 5)
#define USB_CDC_BIT_DTR (1 << 0)
#define USB_CDC_BIT_RTS (1 << 1)
static const GpioPin* flow_pins[][2] = {
{&gpio_ext_pa7, &gpio_ext_pa6}, // 2, 3
{&gpio_ext_pb2, &gpio_ext_pc3}, // 6, 7
};
typedef enum {
WorkerEvtStop = (1 << 0),
WorkerEvtRxDone = (1 << 1),
WorkerEvtTxStop = (1 << 2),
WorkerEvtCdcRx = (1 << 3),
WorkerEvtCfgChange = (1 << 4),
WorkerEvtLineCfgSet = (1 << 5),
WorkerEvtCtrlLineSet = (1 << 6),
} WorkerEvtFlags;
#define WORKER_ALL_RX_EVENTS \
(WorkerEvtStop | WorkerEvtRxDone | WorkerEvtCfgChange | WorkerEvtLineCfgSet | \
WorkerEvtCtrlLineSet)
#define WORKER_ALL_TX_EVENTS (WorkerEvtTxStop | WorkerEvtCdcRx)
struct UsbUartBridge {
UsbUartConfig cfg;
UsbUartConfig cfg_new;
FuriThread* thread;
FuriThread* tx_thread;
StreamBufferHandle_t rx_stream;
FuriMutex* usb_mutex;
FuriSemaphore* tx_sem;
UsbUartState st;
uint8_t rx_buf[USB_CDC_PKT_LEN];
};
static void vcp_on_cdc_tx_complete(void* context);
static void vcp_on_cdc_rx(void* context);
static void vcp_state_callback(void* context, uint8_t state);
static void vcp_on_cdc_control_line(void* context, uint8_t state);
static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config);
static const CdcCallbacks cdc_cb = {
vcp_on_cdc_tx_complete,
vcp_on_cdc_rx,
vcp_state_callback,
vcp_on_cdc_control_line,
vcp_on_line_config,
};
/* USB UART worker */
static int32_t usb_uart_tx_thread(void* context);
static void usb_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
if(ev == UartIrqEventRXNE) {
xStreamBufferSendFromISR(usb_uart->rx_stream, &data, 1, &xHigherPriorityTaskWoken);
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtRxDone);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}
static void usb_uart_vcp_init(UsbUartBridge* usb_uart, uint8_t vcp_ch) {
furi_hal_usb_unlock();
if(vcp_ch == 0) {
Cli* cli = furi_record_open(RECORD_CLI);
cli_session_close(cli);
furi_record_close(RECORD_CLI);
furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
} else {
furi_check(furi_hal_usb_set_config(&usb_cdc_dual, NULL) == true);
Cli* cli = furi_record_open(RECORD_CLI);
cli_session_open(cli, &cli_vcp);
furi_record_close(RECORD_CLI);
}
furi_hal_cdc_set_callbacks(vcp_ch, (CdcCallbacks*)&cdc_cb, usb_uart);
}
static void usb_uart_vcp_deinit(UsbUartBridge* usb_uart, uint8_t vcp_ch) {
UNUSED(usb_uart);
furi_hal_cdc_set_callbacks(vcp_ch, NULL, NULL);
if(vcp_ch != 0) {
Cli* cli = furi_record_open(RECORD_CLI);
cli_session_close(cli);
furi_record_close(RECORD_CLI);
}
}
static void usb_uart_serial_init(UsbUartBridge* usb_uart, uint8_t uart_ch) {
if(uart_ch == FuriHalUartIdUSART1) {
furi_hal_console_disable();
} else if(uart_ch == FuriHalUartIdLPUART1) {
furi_hal_uart_init(uart_ch, 115200);
}
furi_hal_uart_set_irq_cb(uart_ch, usb_uart_on_irq_cb, usb_uart);
}
static void usb_uart_serial_deinit(UsbUartBridge* usb_uart, uint8_t uart_ch) {
UNUSED(usb_uart);
furi_hal_uart_set_irq_cb(uart_ch, NULL, NULL);
if(uart_ch == FuriHalUartIdUSART1)
furi_hal_console_enable();
else if(uart_ch == FuriHalUartIdLPUART1)
furi_hal_uart_deinit(uart_ch);
}
static void usb_uart_set_baudrate(UsbUartBridge* usb_uart, uint32_t baudrate) {
if(baudrate != 0) {
furi_hal_uart_set_br(usb_uart->cfg.uart_ch, baudrate);
usb_uart->st.baudrate_cur = baudrate;
} else {
struct usb_cdc_line_coding* line_cfg =
furi_hal_cdc_get_port_settings(usb_uart->cfg.vcp_ch);
if(line_cfg->dwDTERate > 0) {
furi_hal_uart_set_br(usb_uart->cfg.uart_ch, line_cfg->dwDTERate);
usb_uart->st.baudrate_cur = line_cfg->dwDTERate;
}
}
}
static void usb_uart_update_ctrl_lines(UsbUartBridge* usb_uart) {
if(usb_uart->cfg.flow_pins != 0) {
furi_assert((size_t)(usb_uart->cfg.flow_pins - 1) < COUNT_OF(flow_pins));
uint8_t state = furi_hal_cdc_get_ctrl_line_state(usb_uart->cfg.vcp_ch);
furi_hal_gpio_write(flow_pins[usb_uart->cfg.flow_pins - 1][0], !(state & USB_CDC_BIT_RTS));
furi_hal_gpio_write(flow_pins[usb_uart->cfg.flow_pins - 1][1], !(state & USB_CDC_BIT_DTR));
}
}
static int32_t usb_uart_worker(void* context) {
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
memcpy(&usb_uart->cfg, &usb_uart->cfg_new, sizeof(UsbUartConfig));
usb_uart->rx_stream = xStreamBufferCreate(USB_UART_RX_BUF_SIZE, 1);
usb_uart->tx_sem = furi_semaphore_alloc(1, 1);
usb_uart->usb_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
usb_uart->tx_thread = furi_thread_alloc();
furi_thread_set_name(usb_uart->tx_thread, "UsbUartTxWorker");
furi_thread_set_stack_size(usb_uart->tx_thread, 512);
furi_thread_set_context(usb_uart->tx_thread, usb_uart);
furi_thread_set_callback(usb_uart->tx_thread, usb_uart_tx_thread);
usb_uart_vcp_init(usb_uart, usb_uart->cfg.vcp_ch);
usb_uart_serial_init(usb_uart, usb_uart->cfg.uart_ch);
usb_uart_set_baudrate(usb_uart, usb_uart->cfg.baudrate);
if(usb_uart->cfg.flow_pins != 0) {
furi_assert((size_t)(usb_uart->cfg.flow_pins - 1) < COUNT_OF(flow_pins));
furi_hal_gpio_init_simple(
flow_pins[usb_uart->cfg.flow_pins - 1][0], GpioModeOutputPushPull);
furi_hal_gpio_init_simple(
flow_pins[usb_uart->cfg.flow_pins - 1][1], GpioModeOutputPushPull);
usb_uart_update_ctrl_lines(usb_uart);
}
furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtCdcRx);
furi_thread_start(usb_uart->tx_thread);
while(1) {
uint32_t events =
furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
furi_check((events & FuriFlagError) == 0);
if(events & WorkerEvtStop) break;
if(events & WorkerEvtRxDone) {
size_t len =
xStreamBufferReceive(usb_uart->rx_stream, usb_uart->rx_buf, USB_CDC_PKT_LEN, 0);
if(len > 0) {
if(furi_semaphore_acquire(usb_uart->tx_sem, 100) == FuriStatusOk) {
usb_uart->st.rx_cnt += len;
furi_check(
furi_mutex_acquire(usb_uart->usb_mutex, FuriWaitForever) == FuriStatusOk);
furi_hal_cdc_send(usb_uart->cfg.vcp_ch, usb_uart->rx_buf, len);
furi_check(furi_mutex_release(usb_uart->usb_mutex) == FuriStatusOk);
} else {
xStreamBufferReset(usb_uart->rx_stream);
}
}
}
if(events & WorkerEvtCfgChange) {
if(usb_uart->cfg.vcp_ch != usb_uart->cfg_new.vcp_ch) {
furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtTxStop);
furi_thread_join(usb_uart->tx_thread);
usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
usb_uart_vcp_init(usb_uart, usb_uart->cfg_new.vcp_ch);
usb_uart->cfg.vcp_ch = usb_uart->cfg_new.vcp_ch;
furi_thread_start(usb_uart->tx_thread);
events |= WorkerEvtCtrlLineSet;
events |= WorkerEvtLineCfgSet;
}
if(usb_uart->cfg.uart_ch != usb_uart->cfg_new.uart_ch) {
furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtTxStop);
furi_thread_join(usb_uart->tx_thread);
usb_uart_serial_deinit(usb_uart, usb_uart->cfg.uart_ch);
usb_uart_serial_init(usb_uart, usb_uart->cfg_new.uart_ch);
usb_uart->cfg.uart_ch = usb_uart->cfg_new.uart_ch;
usb_uart_set_baudrate(usb_uart, usb_uart->cfg.baudrate);
furi_thread_start(usb_uart->tx_thread);
}
if(usb_uart->cfg.baudrate != usb_uart->cfg_new.baudrate) {
usb_uart_set_baudrate(usb_uart, usb_uart->cfg_new.baudrate);
usb_uart->cfg.baudrate = usb_uart->cfg_new.baudrate;
}
if(usb_uart->cfg.flow_pins != usb_uart->cfg_new.flow_pins) {
if(usb_uart->cfg.flow_pins != 0) {
furi_hal_gpio_init_simple(
flow_pins[usb_uart->cfg.flow_pins - 1][0], GpioModeAnalog);
furi_hal_gpio_init_simple(
flow_pins[usb_uart->cfg.flow_pins - 1][1], GpioModeAnalog);
}
if(usb_uart->cfg_new.flow_pins != 0) {
furi_assert((size_t)(usb_uart->cfg_new.flow_pins - 1) < COUNT_OF(flow_pins));
furi_hal_gpio_init_simple(
flow_pins[usb_uart->cfg_new.flow_pins - 1][0], GpioModeOutputPushPull);
furi_hal_gpio_init_simple(
flow_pins[usb_uart->cfg_new.flow_pins - 1][1], GpioModeOutputPushPull);
}
usb_uart->cfg.flow_pins = usb_uart->cfg_new.flow_pins;
events |= WorkerEvtCtrlLineSet;
}
}
if(events & WorkerEvtLineCfgSet) {
if(usb_uart->cfg.baudrate == 0)
usb_uart_set_baudrate(usb_uart, usb_uart->cfg.baudrate);
}
if(events & WorkerEvtCtrlLineSet) {
usb_uart_update_ctrl_lines(usb_uart);
}
}
usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
usb_uart_serial_deinit(usb_uart, usb_uart->cfg.uart_ch);
if(usb_uart->cfg.flow_pins != 0) {
furi_hal_gpio_init_simple(flow_pins[usb_uart->cfg.flow_pins - 1][0], GpioModeAnalog);
furi_hal_gpio_init_simple(flow_pins[usb_uart->cfg.flow_pins - 1][1], GpioModeAnalog);
}
furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtTxStop);
furi_thread_join(usb_uart->tx_thread);
furi_thread_free(usb_uart->tx_thread);
vStreamBufferDelete(usb_uart->rx_stream);
furi_mutex_free(usb_uart->usb_mutex);
furi_semaphore_free(usb_uart->tx_sem);
furi_hal_usb_unlock();
furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
Cli* cli = furi_record_open(RECORD_CLI);
cli_session_open(cli, &cli_vcp);
furi_record_close(RECORD_CLI);
return 0;
}
static int32_t usb_uart_tx_thread(void* context) {
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
uint8_t data[USB_CDC_PKT_LEN];
while(1) {
uint32_t events =
furi_thread_flags_wait(WORKER_ALL_TX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
furi_check((events & FuriFlagError) == 0);
if(events & WorkerEvtTxStop) break;
if(events & WorkerEvtCdcRx) {
furi_check(furi_mutex_acquire(usb_uart->usb_mutex, FuriWaitForever) == FuriStatusOk);
size_t len = furi_hal_cdc_receive(usb_uart->cfg.vcp_ch, data, USB_CDC_PKT_LEN);
furi_check(furi_mutex_release(usb_uart->usb_mutex) == FuriStatusOk);
if(len > 0) {
usb_uart->st.tx_cnt += len;
furi_hal_uart_tx(usb_uart->cfg.uart_ch, data, len);
}
}
}
return 0;
}
/* VCP callbacks */
static void vcp_on_cdc_tx_complete(void* context) {
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
furi_semaphore_release(usb_uart->tx_sem);
}
static void vcp_on_cdc_rx(void* context) {
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtCdcRx);
}
static void vcp_state_callback(void* context, uint8_t state) {
UNUSED(context);
UNUSED(state);
}
static void vcp_on_cdc_control_line(void* context, uint8_t state) {
UNUSED(state);
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCtrlLineSet);
}
static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config) {
UNUSED(config);
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtLineCfgSet);
}
UsbUartBridge* usb_uart_enable(UsbUartConfig* cfg) {
UsbUartBridge* usb_uart = malloc(sizeof(UsbUartBridge));
memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
usb_uart->thread = furi_thread_alloc();
furi_thread_set_name(usb_uart->thread, "UsbUartWorker");
furi_thread_set_stack_size(usb_uart->thread, 1024);
furi_thread_set_context(usb_uart->thread, usb_uart);
furi_thread_set_callback(usb_uart->thread, usb_uart_worker);
furi_thread_start(usb_uart->thread);
return usb_uart;
}
void usb_uart_disable(UsbUartBridge* usb_uart) {
furi_assert(usb_uart);
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtStop);
furi_thread_join(usb_uart->thread);
furi_thread_free(usb_uart->thread);
free(usb_uart);
}
void usb_uart_set_config(UsbUartBridge* usb_uart, UsbUartConfig* cfg) {
furi_assert(usb_uart);
furi_assert(cfg);
memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCfgChange);
}
void usb_uart_get_config(UsbUartBridge* usb_uart, UsbUartConfig* cfg) {
furi_assert(usb_uart);
furi_assert(cfg);
memcpy(cfg, &(usb_uart->cfg_new), sizeof(UsbUartConfig));
}
void usb_uart_get_state(UsbUartBridge* usb_uart, UsbUartState* st) {
furi_assert(usb_uart);
furi_assert(st);
memcpy(st, &(usb_uart->st), sizeof(UsbUartState));
}

View File

@@ -0,0 +1,30 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
typedef struct UsbUartBridge UsbUartBridge;
typedef struct {
uint8_t vcp_ch;
uint8_t uart_ch;
uint8_t flow_pins;
uint8_t baudrate_mode;
uint32_t baudrate;
} UsbUartConfig;
typedef struct {
uint32_t rx_cnt;
uint32_t tx_cnt;
uint32_t baudrate_cur;
} UsbUartState;
UsbUartBridge* usb_uart_enable(UsbUartConfig* cfg);
void usb_uart_disable(UsbUartBridge* usb_uart);
void usb_uart_set_config(UsbUartBridge* usb_uart, UsbUartConfig* cfg);
void usb_uart_get_config(UsbUartBridge* usb_uart, UsbUartConfig* cfg);
void usb_uart_get_state(UsbUartBridge* usb_uart, UsbUartState* st);

View File

@@ -0,0 +1,131 @@
#include "gpio_test.h"
#include "../gpio_item.h"
#include <gui/elements.h>
struct GpioTest {
View* view;
GpioTestOkCallback callback;
void* context;
};
typedef struct {
uint8_t pin_idx;
} GpioTestModel;
static bool gpio_test_process_left(GpioTest* gpio_test);
static bool gpio_test_process_right(GpioTest* gpio_test);
static bool gpio_test_process_ok(GpioTest* gpio_test, InputEvent* event);
static void gpio_test_draw_callback(Canvas* canvas, void* _model) {
GpioTestModel* model = _model;
canvas_set_font(canvas, FontPrimary);
elements_multiline_text_aligned(canvas, 64, 2, AlignCenter, AlignTop, "GPIO Output Mode Test");
canvas_set_font(canvas, FontSecondary);
elements_multiline_text_aligned(
canvas, 64, 16, AlignCenter, AlignTop, "Press < or > to change pin");
elements_multiline_text_aligned(
canvas, 64, 32, AlignCenter, AlignTop, gpio_item_get_pin_name(model->pin_idx));
}
static bool gpio_test_input_callback(InputEvent* event, void* context) {
furi_assert(context);
GpioTest* gpio_test = context;
bool consumed = false;
if(event->type == InputTypeShort) {
if(event->key == InputKeyRight) {
consumed = gpio_test_process_right(gpio_test);
} else if(event->key == InputKeyLeft) {
consumed = gpio_test_process_left(gpio_test);
}
} else if(event->key == InputKeyOk) {
consumed = gpio_test_process_ok(gpio_test, event);
}
return consumed;
}
static bool gpio_test_process_left(GpioTest* gpio_test) {
with_view_model(
gpio_test->view, (GpioTestModel * model) {
if(model->pin_idx) {
model->pin_idx--;
}
return true;
});
return true;
}
static bool gpio_test_process_right(GpioTest* gpio_test) {
with_view_model(
gpio_test->view, (GpioTestModel * model) {
if(model->pin_idx < GPIO_ITEM_COUNT) {
model->pin_idx++;
}
return true;
});
return true;
}
static bool gpio_test_process_ok(GpioTest* gpio_test, InputEvent* event) {
bool consumed = false;
with_view_model(
gpio_test->view, (GpioTestModel * model) {
if(event->type == InputTypePress) {
if(model->pin_idx < GPIO_ITEM_COUNT) {
gpio_item_set_pin(model->pin_idx, true);
} else {
gpio_item_set_all_pins(true);
}
consumed = true;
} else if(event->type == InputTypeRelease) {
if(model->pin_idx < GPIO_ITEM_COUNT) {
gpio_item_set_pin(model->pin_idx, false);
} else {
gpio_item_set_all_pins(false);
}
consumed = true;
}
gpio_test->callback(event->type, gpio_test->context);
return true;
});
return consumed;
}
GpioTest* gpio_test_alloc() {
GpioTest* gpio_test = malloc(sizeof(GpioTest));
gpio_test->view = view_alloc();
view_allocate_model(gpio_test->view, ViewModelTypeLocking, sizeof(GpioTestModel));
view_set_context(gpio_test->view, gpio_test);
view_set_draw_callback(gpio_test->view, gpio_test_draw_callback);
view_set_input_callback(gpio_test->view, gpio_test_input_callback);
return gpio_test;
}
void gpio_test_free(GpioTest* gpio_test) {
furi_assert(gpio_test);
view_free(gpio_test->view);
free(gpio_test);
}
View* gpio_test_get_view(GpioTest* gpio_test) {
furi_assert(gpio_test);
return gpio_test->view;
}
void gpio_test_set_ok_callback(GpioTest* gpio_test, GpioTestOkCallback callback, void* context) {
furi_assert(gpio_test);
furi_assert(callback);
with_view_model(
gpio_test->view, (GpioTestModel * model) {
UNUSED(model);
gpio_test->callback = callback;
gpio_test->context = context;
return false;
});
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include <gui/view.h>
typedef struct GpioTest GpioTest;
typedef void (*GpioTestOkCallback)(InputType type, void* context);
GpioTest* gpio_test_alloc();
void gpio_test_free(GpioTest* gpio_test);
View* gpio_test_get_view(GpioTest* gpio_test);
void gpio_test_set_ok_callback(GpioTest* gpio_test, GpioTestOkCallback callback, void* context);

View File

@@ -0,0 +1,157 @@
#include "../usb_uart_bridge.h"
#include "../gpio_app_i.h"
#include "furi_hal.h"
#include <gui/elements.h>
struct GpioUsbUart {
View* view;
GpioUsbUartCallback callback;
void* context;
};
typedef struct {
uint32_t baudrate;
uint32_t tx_cnt;
uint32_t rx_cnt;
uint8_t vcp_port;
uint8_t tx_pin;
uint8_t rx_pin;
bool tx_active;
bool rx_active;
} GpioUsbUartModel;
static void gpio_usb_uart_draw_callback(Canvas* canvas, void* _model) {
GpioUsbUartModel* model = _model;
char temp_str[18];
elements_button_left(canvas, "Config");
canvas_draw_line(canvas, 2, 10, 125, 10);
canvas_draw_line(canvas, 44, 52, 123, 52);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 2, 9, "USB Serial");
canvas_draw_str(canvas, 3, 25, "TX:");
canvas_draw_str(canvas, 3, 42, "RX:");
canvas_set_font(canvas, FontSecondary);
snprintf(temp_str, 18, "COM PORT:%u", model->vcp_port);
canvas_draw_str_aligned(canvas, 126, 8, AlignRight, AlignBottom, temp_str);
snprintf(temp_str, 18, "Pin %u", model->tx_pin);
canvas_draw_str(canvas, 22, 25, temp_str);
snprintf(temp_str, 18, "Pin %u", model->rx_pin);
canvas_draw_str(canvas, 22, 42, temp_str);
if(model->baudrate == 0)
snprintf(temp_str, 18, "Baud: ????");
else
snprintf(temp_str, 18, "Baud: %lu", model->baudrate);
canvas_draw_str(canvas, 45, 62, temp_str);
if(model->tx_cnt < 100000000) {
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 127, 24, AlignRight, AlignBottom, "B.");
canvas_set_font(canvas, FontKeyboard);
snprintf(temp_str, 18, "%lu", model->tx_cnt);
canvas_draw_str_aligned(canvas, 116, 24, AlignRight, AlignBottom, temp_str);
} else {
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 127, 24, AlignRight, AlignBottom, "KB.");
canvas_set_font(canvas, FontKeyboard);
snprintf(temp_str, 18, "%lu", model->tx_cnt / 1024);
canvas_draw_str_aligned(canvas, 111, 24, AlignRight, AlignBottom, temp_str);
}
if(model->rx_cnt < 100000000) {
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 127, 41, AlignRight, AlignBottom, "B.");
canvas_set_font(canvas, FontKeyboard);
snprintf(temp_str, 18, "%lu", model->rx_cnt);
canvas_draw_str_aligned(canvas, 116, 41, AlignRight, AlignBottom, temp_str);
} else {
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 127, 41, AlignRight, AlignBottom, "KB.");
canvas_set_font(canvas, FontKeyboard);
snprintf(temp_str, 18, "%lu", model->rx_cnt / 1024);
canvas_draw_str_aligned(canvas, 111, 41, AlignRight, AlignBottom, temp_str);
}
if(model->tx_active)
canvas_draw_icon(canvas, 48, 14, &I_ArrowUpFilled_14x15);
else
canvas_draw_icon(canvas, 48, 14, &I_ArrowUpEmpty_14x15);
if(model->rx_active)
canvas_draw_icon(canvas, 48, 34, &I_ArrowDownFilled_14x15);
else
canvas_draw_icon(canvas, 48, 34, &I_ArrowDownEmpty_14x15);
}
static bool gpio_usb_uart_input_callback(InputEvent* event, void* context) {
furi_assert(context);
GpioUsbUart* usb_uart = context;
bool consumed = false;
if(event->type == InputTypeShort) {
if(event->key == InputKeyLeft) {
consumed = true;
furi_assert(usb_uart->callback);
usb_uart->callback(GpioUsbUartEventConfig, usb_uart->context);
}
}
return consumed;
}
GpioUsbUart* gpio_usb_uart_alloc() {
GpioUsbUart* usb_uart = malloc(sizeof(GpioUsbUart));
usb_uart->view = view_alloc();
view_allocate_model(usb_uart->view, ViewModelTypeLocking, sizeof(GpioUsbUartModel));
view_set_context(usb_uart->view, usb_uart);
view_set_draw_callback(usb_uart->view, gpio_usb_uart_draw_callback);
view_set_input_callback(usb_uart->view, gpio_usb_uart_input_callback);
return usb_uart;
}
void gpio_usb_uart_free(GpioUsbUart* usb_uart) {
furi_assert(usb_uart);
view_free(usb_uart->view);
free(usb_uart);
}
View* gpio_usb_uart_get_view(GpioUsbUart* usb_uart) {
furi_assert(usb_uart);
return usb_uart->view;
}
void gpio_usb_uart_set_callback(GpioUsbUart* usb_uart, GpioUsbUartCallback callback, void* context) {
furi_assert(usb_uart);
furi_assert(callback);
with_view_model(
usb_uart->view, (GpioUsbUartModel * model) {
UNUSED(model);
usb_uart->callback = callback;
usb_uart->context = context;
return false;
});
}
void gpio_usb_uart_update_state(GpioUsbUart* instance, UsbUartConfig* cfg, UsbUartState* st) {
furi_assert(instance);
furi_assert(cfg);
furi_assert(st);
with_view_model(
instance->view, (GpioUsbUartModel * model) {
model->baudrate = st->baudrate_cur;
model->vcp_port = cfg->vcp_ch;
model->tx_pin = (cfg->uart_ch == 0) ? (13) : (15);
model->rx_pin = (cfg->uart_ch == 0) ? (14) : (16);
model->tx_active = (model->tx_cnt != st->tx_cnt);
model->rx_active = (model->rx_cnt != st->rx_cnt);
model->tx_cnt = st->tx_cnt;
model->rx_cnt = st->rx_cnt;
return true;
});
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include <gui/view.h>
#include "../gpio_custom_event.h"
#include "../usb_uart_bridge.h"
typedef struct GpioUsbUart GpioUsbUart;
typedef void (*GpioUsbUartCallback)(GpioCustomEvent event, void* context);
GpioUsbUart* gpio_usb_uart_alloc();
void gpio_usb_uart_free(GpioUsbUart* usb_uart);
View* gpio_usb_uart_get_view(GpioUsbUart* usb_uart);
void gpio_usb_uart_set_callback(GpioUsbUart* usb_uart, GpioUsbUartCallback callback, void* context);
void gpio_usb_uart_update_state(GpioUsbUart* instance, UsbUartConfig* cfg, UsbUartState* st);

View File

@@ -0,0 +1,23 @@
App(
appid="ibutton",
name="iButton",
apptype=FlipperAppType.APP,
entry_point="ibutton_app",
cdefines=["APP_IBUTTON"],
requires=[
"gui",
"dialogs",
],
provides=["ibutton_start"],
icon="A_iButton_14",
stack_size=2 * 1024,
order=60,
)
App(
appid="ibutton_start",
apptype=FlipperAppType.STARTUP,
entry_point="ibutton_on_system_start",
requires=["ibutton"],
order=60,
)

View File

@@ -0,0 +1,359 @@
#include "ibutton.h"
#include "assets_icons.h"
#include "ibutton_i.h"
#include "ibutton/scenes/ibutton_scene.h"
#include "m-string.h"
#include <toolbox/path.h>
#include <flipper_format/flipper_format.h>
#include <rpc/rpc_app.h>
#define TAG "iButtonApp"
static const NotificationSequence sequence_blink_set_yellow = {
&message_blink_set_color_yellow,
NULL,
};
static const NotificationSequence sequence_blink_set_magenta = {
&message_blink_set_color_magenta,
NULL,
};
static const NotificationSequence* ibutton_notification_sequences[] = {
&sequence_error,
&sequence_success,
&sequence_blink_start_cyan,
&sequence_blink_start_magenta,
&sequence_blink_set_yellow,
&sequence_blink_set_magenta,
&sequence_set_red_255,
&sequence_reset_red,
&sequence_set_green_255,
&sequence_reset_green,
&sequence_blink_stop,
};
static void ibutton_make_app_folder(iButton* ibutton) {
if(!storage_simply_mkdir(ibutton->storage, IBUTTON_APP_FOLDER)) {
dialog_message_show_storage_error(ibutton->dialogs, "Cannot create\napp folder");
}
}
bool ibutton_load_key_data(iButton* ibutton, string_t key_path, bool show_dialog) {
FlipperFormat* file = flipper_format_file_alloc(ibutton->storage);
bool result = false;
string_t data;
string_init(data);
do {
if(!flipper_format_file_open_existing(file, string_get_cstr(key_path))) break;
// header
uint32_t version;
if(!flipper_format_read_header(file, data, &version)) break;
if(string_cmp_str(data, IBUTTON_APP_FILE_TYPE) != 0) break;
if(version != 1) break;
// key type
iButtonKeyType type;
if(!flipper_format_read_string(file, "Key type", data)) break;
if(!ibutton_key_get_type_by_string(string_get_cstr(data), &type)) break;
// key data
uint8_t key_data[IBUTTON_KEY_DATA_SIZE] = {0};
if(!flipper_format_read_hex(file, "Data", key_data, ibutton_key_get_size_by_type(type)))
break;
ibutton_key_set_type(ibutton->key, type);
ibutton_key_set_data(ibutton->key, key_data, IBUTTON_KEY_DATA_SIZE);
result = true;
} while(false);
flipper_format_free(file);
string_clear(data);
if((!result) && (show_dialog)) {
dialog_message_show_storage_error(ibutton->dialogs, "Cannot load\nkey file");
}
return result;
}
static void ibutton_rpc_command_callback(RpcAppSystemEvent event, void* context) {
furi_assert(context);
iButton* ibutton = context;
if(event == RpcAppEventSessionClose) {
view_dispatcher_send_custom_event(
ibutton->view_dispatcher, iButtonCustomEventRpcSessionClose);
rpc_system_app_set_callback(ibutton->rpc_ctx, NULL, NULL);
ibutton->rpc_ctx = NULL;
} else if(event == RpcAppEventAppExit) {
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcExit);
} else if(event == RpcAppEventLoadFile) {
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcLoad);
} else {
rpc_system_app_confirm(ibutton->rpc_ctx, event, false);
}
}
bool ibutton_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
iButton* ibutton = context;
return scene_manager_handle_custom_event(ibutton->scene_manager, event);
}
bool ibutton_back_event_callback(void* context) {
furi_assert(context);
iButton* ibutton = context;
return scene_manager_handle_back_event(ibutton->scene_manager);
}
void ibutton_tick_event_callback(void* context) {
furi_assert(context);
iButton* ibutton = context;
scene_manager_handle_tick_event(ibutton->scene_manager);
}
iButton* ibutton_alloc() {
iButton* ibutton = malloc(sizeof(iButton));
string_init(ibutton->file_path);
ibutton->scene_manager = scene_manager_alloc(&ibutton_scene_handlers, ibutton);
ibutton->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(ibutton->view_dispatcher);
view_dispatcher_set_event_callback_context(ibutton->view_dispatcher, ibutton);
view_dispatcher_set_custom_event_callback(
ibutton->view_dispatcher, ibutton_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
ibutton->view_dispatcher, ibutton_back_event_callback);
view_dispatcher_set_tick_event_callback(
ibutton->view_dispatcher, ibutton_tick_event_callback, 100);
ibutton->gui = furi_record_open(RECORD_GUI);
ibutton->storage = furi_record_open(RECORD_STORAGE);
ibutton->dialogs = furi_record_open(RECORD_DIALOGS);
ibutton->notifications = furi_record_open(RECORD_NOTIFICATION);
ibutton->key = ibutton_key_alloc();
ibutton->key_worker = ibutton_worker_alloc();
ibutton_worker_start_thread(ibutton->key_worker);
ibutton->submenu = submenu_alloc();
view_dispatcher_add_view(
ibutton->view_dispatcher, iButtonViewSubmenu, submenu_get_view(ibutton->submenu));
ibutton->byte_input = byte_input_alloc();
view_dispatcher_add_view(
ibutton->view_dispatcher, iButtonViewByteInput, byte_input_get_view(ibutton->byte_input));
ibutton->text_input = text_input_alloc();
view_dispatcher_add_view(
ibutton->view_dispatcher, iButtonViewTextInput, text_input_get_view(ibutton->text_input));
ibutton->popup = popup_alloc();
view_dispatcher_add_view(
ibutton->view_dispatcher, iButtonViewPopup, popup_get_view(ibutton->popup));
ibutton->widget = widget_alloc();
view_dispatcher_add_view(
ibutton->view_dispatcher, iButtonViewWidget, widget_get_view(ibutton->widget));
ibutton->dialog_ex = dialog_ex_alloc();
view_dispatcher_add_view(
ibutton->view_dispatcher, iButtonViewDialogEx, dialog_ex_get_view(ibutton->dialog_ex));
return ibutton;
}
void ibutton_free(iButton* ibutton) {
furi_assert(ibutton);
view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewDialogEx);
dialog_ex_free(ibutton->dialog_ex);
view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewWidget);
widget_free(ibutton->widget);
view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewPopup);
popup_free(ibutton->popup);
view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewTextInput);
text_input_free(ibutton->text_input);
view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewByteInput);
byte_input_free(ibutton->byte_input);
view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewSubmenu);
submenu_free(ibutton->submenu);
view_dispatcher_free(ibutton->view_dispatcher);
scene_manager_free(ibutton->scene_manager);
furi_record_close(RECORD_STORAGE);
ibutton->storage = NULL;
furi_record_close(RECORD_NOTIFICATION);
ibutton->notifications = NULL;
furi_record_close(RECORD_DIALOGS);
ibutton->dialogs = NULL;
furi_record_close(RECORD_GUI);
ibutton->gui = NULL;
ibutton_worker_stop_thread(ibutton->key_worker);
ibutton_worker_free(ibutton->key_worker);
ibutton_key_free(ibutton->key);
string_clear(ibutton->file_path);
free(ibutton);
}
bool ibutton_file_select(iButton* ibutton) {
DialogsFileBrowserOptions browser_options;
dialog_file_browser_set_basic_options(&browser_options, IBUTTON_APP_EXTENSION, &I_ibutt_10px);
bool success = dialog_file_browser_show(
ibutton->dialogs, ibutton->file_path, ibutton->file_path, &browser_options);
if(success) {
success = ibutton_load_key_data(ibutton, ibutton->file_path, true);
}
return success;
}
bool ibutton_save_key(iButton* ibutton, const char* key_name) {
// Create ibutton directory if necessary
ibutton_make_app_folder(ibutton);
FlipperFormat* file = flipper_format_file_alloc(ibutton->storage);
iButtonKey* key = ibutton->key;
bool result = false;
do {
// Check if we has old key
if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
// First remove old key
ibutton_delete_key(ibutton);
// Remove old key name from path
size_t filename_start = string_search_rchar(ibutton->file_path, '/');
string_left(ibutton->file_path, filename_start);
}
string_cat_printf(ibutton->file_path, "/%s%s", key_name, IBUTTON_APP_EXTENSION);
// Open file for write
if(!flipper_format_file_open_always(file, string_get_cstr(ibutton->file_path))) break;
// Write header
if(!flipper_format_write_header_cstr(file, IBUTTON_APP_FILE_TYPE, 1)) break;
// Write key type
if(!flipper_format_write_comment_cstr(file, "Key type can be Cyfral, Dallas or Metakom"))
break;
const char* key_type = ibutton_key_get_string_by_type(ibutton_key_get_type(key));
if(!flipper_format_write_string_cstr(file, "Key type", key_type)) break;
// Write data
if(!flipper_format_write_comment_cstr(
file, "Data size for Cyfral is 2, for Metakom is 4, for Dallas is 8"))
break;
if(!flipper_format_write_hex(
file, "Data", ibutton_key_get_data_p(key), ibutton_key_get_data_size(key)))
break;
result = true;
} while(false);
flipper_format_free(file);
if(!result) {
dialog_message_show_storage_error(ibutton->dialogs, "Cannot save\nkey file");
}
return result;
}
bool ibutton_delete_key(iButton* ibutton) {
bool result = false;
result = storage_simply_remove(ibutton->storage, string_get_cstr(ibutton->file_path));
return result;
}
void ibutton_text_store_set(iButton* ibutton, const char* text, ...) {
va_list args;
va_start(args, text);
vsnprintf(ibutton->text_store, IBUTTON_TEXT_STORE_SIZE, text, args);
va_end(args);
}
void ibutton_text_store_clear(iButton* ibutton) {
memset(ibutton->text_store, 0, IBUTTON_TEXT_STORE_SIZE);
}
void ibutton_notification_message(iButton* ibutton, uint32_t message) {
furi_assert(message < sizeof(ibutton_notification_sequences) / sizeof(NotificationSequence*));
notification_message(ibutton->notifications, ibutton_notification_sequences[message]);
}
int32_t ibutton_app(void* p) {
iButton* ibutton = ibutton_alloc();
ibutton_make_app_folder(ibutton);
bool key_loaded = false;
bool rpc_mode = false;
if(p && strlen(p)) {
uint32_t rpc_ctx = 0;
if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
FURI_LOG_D(TAG, "Running in RPC mode");
ibutton->rpc_ctx = (void*)rpc_ctx;
rpc_mode = true;
rpc_system_app_set_callback(ibutton->rpc_ctx, ibutton_rpc_command_callback, ibutton);
rpc_system_app_send_started(ibutton->rpc_ctx);
} else {
string_set_str(ibutton->file_path, (const char*)p);
if(ibutton_load_key_data(ibutton, ibutton->file_path, true)) {
key_loaded = true;
// TODO: Display an error if the key from p could not be loaded
}
}
}
if(rpc_mode) {
view_dispatcher_attach_to_gui(
ibutton->view_dispatcher, ibutton->gui, ViewDispatcherTypeDesktop);
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneRpc);
} else {
view_dispatcher_attach_to_gui(
ibutton->view_dispatcher, ibutton->gui, ViewDispatcherTypeFullscreen);
if(key_loaded) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneEmulate);
} else {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneStart);
}
}
view_dispatcher_run(ibutton->view_dispatcher);
if(ibutton->rpc_ctx) {
rpc_system_app_set_callback(ibutton->rpc_ctx, NULL, NULL);
rpc_system_app_send_exited(ibutton->rpc_ctx);
}
ibutton_free(ibutton);
return 0;
}

View File

@@ -0,0 +1,3 @@
#pragma once
typedef struct iButton iButton;

View File

@@ -0,0 +1,318 @@
#include <furi.h>
#include <furi_hal.h>
#include <stdarg.h>
#include <cli/cli.h>
#include <lib/toolbox/args.h>
#include <one_wire/ibutton/ibutton_worker.h>
#include <one_wire/one_wire_host.h>
static void ibutton_cli(Cli* cli, string_t args, void* context);
static void onewire_cli(Cli* cli, string_t args, void* context);
// app cli function
void ibutton_on_system_start() {
#ifdef SRV_CLI
Cli* cli = furi_record_open(RECORD_CLI);
cli_add_command(cli, "ikey", CliCommandFlagDefault, ibutton_cli, cli);
cli_add_command(cli, "onewire", CliCommandFlagDefault, onewire_cli, cli);
furi_record_close(RECORD_CLI);
#else
UNUSED(ibutton_cli);
UNUSED(onewire_cli);
#endif
}
void ibutton_cli_print_usage() {
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");
printf("\tMetakom (4 bytes key_data), must contain correct parity\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 = iButtonKeyDS1990;
} else if(string_cmp_str(data, "Cyfral") == 0 || string_cmp_str(data, "cyfral") == 0) {
result = true;
*type = iButtonKeyCyfral;
} else if(string_cmp_str(data, "Metakom") == 0 || string_cmp_str(data, "metakom") == 0) {
result = true;
*type = iButtonKeyMetakom;
}
return result;
}
void ibutton_cli_print_key_data(iButtonKey* key) {
const uint8_t* key_data = ibutton_key_get_data_p(key);
iButtonKeyType type = ibutton_key_get_type(key);
printf("%s ", ibutton_key_get_string_by_type(type));
for(size_t i = 0; i < ibutton_key_get_size_by_type(type); i++) {
printf("%02X", key_data[i]);
}
printf("\r\n");
}
#define EVENT_FLAG_IBUTTON_COMPLETE (1 << 0)
static void ibutton_cli_worker_read_cb(void* context) {
furi_assert(context);
FuriEventFlag* event = context;
furi_event_flag_set(event, EVENT_FLAG_IBUTTON_COMPLETE);
}
void ibutton_cli_read(Cli* cli) {
iButtonKey* key = ibutton_key_alloc();
iButtonWorker* worker = ibutton_worker_alloc();
FuriEventFlag* event = furi_event_flag_alloc();
ibutton_worker_start_thread(worker);
ibutton_worker_read_set_callback(worker, ibutton_cli_worker_read_cb, event);
printf("Reading iButton...\r\nPress Ctrl+C to abort\r\n");
ibutton_worker_read_start(worker, key);
while(true) {
uint32_t flags =
furi_event_flag_wait(event, EVENT_FLAG_IBUTTON_COMPLETE, FuriFlagWaitAny, 100);
if(flags & EVENT_FLAG_IBUTTON_COMPLETE) {
ibutton_cli_print_key_data(key);
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
if(!ibutton_key_dallas_crc_is_valid(key)) {
printf("Warning: invalid CRC\r\n");
}
if(!ibutton_key_dallas_is_1990_key(key)) {
printf("Warning: not a key\r\n");
}
}
break;
}
if(cli_cmd_interrupt_received(cli)) break;
}
ibutton_worker_stop(worker);
ibutton_worker_stop_thread(worker);
ibutton_worker_free(worker);
ibutton_key_free(key);
furi_event_flag_free(event);
};
typedef struct {
FuriEventFlag* event;
iButtonWorkerWriteResult result;
} iButtonWriteContext;
static void ibutton_cli_worker_write_cb(void* context, iButtonWorkerWriteResult result) {
furi_assert(context);
iButtonWriteContext* write_context = (iButtonWriteContext*)context;
write_context->result = result;
furi_event_flag_set(write_context->event, EVENT_FLAG_IBUTTON_COMPLETE);
}
void ibutton_cli_write(Cli* cli, string_t args) {
iButtonKey* key = ibutton_key_alloc();
iButtonWorker* worker = ibutton_worker_alloc();
iButtonKeyType type;
iButtonWriteContext write_context;
uint8_t key_data[IBUTTON_KEY_DATA_SIZE];
string_t data;
write_context.event = furi_event_flag_alloc();
string_init(data);
ibutton_worker_start_thread(worker);
ibutton_worker_write_set_callback(worker, ibutton_cli_worker_write_cb, &write_context);
do {
if(!args_read_string_and_trim(args, data)) {
ibutton_cli_print_usage();
break;
}
if(!ibutton_cli_get_key_type(data, &type)) {
ibutton_cli_print_usage();
break;
}
if(type != iButtonKeyDS1990) {
ibutton_cli_print_usage();
break;
}
if(!args_read_hex_bytes(args, key_data, ibutton_key_get_size_by_type(type))) {
ibutton_cli_print_usage();
break;
}
ibutton_key_set_type(key, type);
ibutton_key_set_data(key, key_data, ibutton_key_get_size_by_type(type));
printf("Writing key ");
ibutton_cli_print_key_data(key);
printf("Press Ctrl+C to abort\r\n");
ibutton_worker_write_start(worker, key);
while(true) {
uint32_t flags = furi_event_flag_wait(
write_context.event, EVENT_FLAG_IBUTTON_COMPLETE, FuriFlagWaitAny, 100);
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;
}
ibutton_worker_stop(worker);
} while(false);
string_clear(data);
ibutton_worker_stop_thread(worker);
ibutton_worker_free(worker);
ibutton_key_free(key);
furi_event_flag_free(write_context.event);
};
void ibutton_cli_emulate(Cli* cli, string_t args) {
iButtonKey* key = ibutton_key_alloc();
iButtonWorker* worker = ibutton_worker_alloc();
iButtonKeyType type;
uint8_t key_data[IBUTTON_KEY_DATA_SIZE];
string_t data;
string_init(data);
ibutton_worker_start_thread(worker);
do {
if(!args_read_string_and_trim(args, data)) {
ibutton_cli_print_usage();
break;
}
if(!ibutton_cli_get_key_type(data, &type)) {
ibutton_cli_print_usage();
break;
}
if(!args_read_hex_bytes(args, key_data, ibutton_key_get_size_by_type(type))) {
ibutton_cli_print_usage();
break;
}
ibutton_key_set_type(key, type);
ibutton_key_set_data(key, key_data, ibutton_key_get_size_by_type(type));
printf("Emulating key ");
ibutton_cli_print_key_data(key);
printf("Press Ctrl+C to abort\r\n");
ibutton_worker_emulate_start(worker, key);
while(!cli_cmd_interrupt_received(cli)) {
furi_delay_ms(100);
};
ibutton_worker_stop(worker);
} while(false);
string_clear(data);
ibutton_worker_stop_thread(worker);
ibutton_worker_free(worker);
ibutton_key_free(key);
};
static void ibutton_cli(Cli* cli, string_t args, void* context) {
UNUSED(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");
};
static void onewire_cli_search(Cli* cli) {
UNUSED(cli);
OneWireHost* onewire = onewire_host_alloc();
uint8_t address[8];
bool done = false;
printf("Search started\r\n");
onewire_host_start(onewire);
furi_hal_power_enable_otg();
while(!done) {
if(onewire_host_search(onewire, address, NORMAL_SEARCH) != 1) {
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");
}
furi_delay_ms(100);
}
furi_hal_power_disable_otg();
onewire_host_free(onewire);
}
void onewire_cli(Cli* cli, string_t args, void* context) {
UNUSED(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);
}

View File

@@ -0,0 +1,16 @@
#pragma once
enum iButtonCustomEvent {
// Reserve first 100 events for button types and indexes, starting from 0
iButtonCustomEventReserved = 100,
iButtonCustomEventBack,
iButtonCustomEventTextEditResult,
iButtonCustomEventByteEditResult,
iButtonCustomEventWorkerEmulated,
iButtonCustomEventWorkerRead,
iButtonCustomEventRpcLoad,
iButtonCustomEventRpcExit,
iButtonCustomEventRpcSessionClose,
};

View File

@@ -0,0 +1,86 @@
#pragma once
#include "ibutton.h"
#include <gui/gui.h>
#include <gui/view.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <notification/notification_messages.h>
#include <one_wire/ibutton/ibutton_worker.h>
#include <storage/storage.h>
#include <dialogs/dialogs.h>
#include <gui/modules/submenu.h>
#include <gui/modules/popup.h>
#include <gui/modules/dialog_ex.h>
#include <gui/modules/text_input.h>
#include <gui/modules/byte_input.h>
#include <gui/modules/widget.h>
#include "ibutton_custom_event.h"
#include "scenes/ibutton_scene.h"
#define IBUTTON_FILE_NAME_SIZE 100
#define IBUTTON_TEXT_STORE_SIZE 128
#define IBUTTON_APP_FOLDER ANY_PATH("ibutton")
#define IBUTTON_APP_EXTENSION ".ibtn"
#define IBUTTON_APP_FILE_TYPE "Flipper iButton key"
struct iButton {
SceneManager* scene_manager;
ViewDispatcher* view_dispatcher;
Gui* gui;
Storage* storage;
DialogsApp* dialogs;
NotificationApp* notifications;
iButtonWorker* key_worker;
iButtonKey* key;
string_t file_path;
char text_store[IBUTTON_TEXT_STORE_SIZE + 1];
Submenu* submenu;
ByteInput* byte_input;
TextInput* text_input;
Popup* popup;
Widget* widget;
DialogEx* dialog_ex;
void* rpc_ctx;
};
typedef enum {
iButtonViewSubmenu,
iButtonViewByteInput,
iButtonViewTextInput,
iButtonViewPopup,
iButtonViewWidget,
iButtonViewDialogEx,
} iButtonView;
typedef enum {
iButtonNotificationMessageError,
iButtonNotificationMessageSuccess,
iButtonNotificationMessageReadStart,
iButtonNotificationMessageEmulateStart,
iButtonNotificationMessageYellowBlink,
iButtonNotificationMessageEmulateBlink,
iButtonNotificationMessageRedOn,
iButtonNotificationMessageRedOff,
iButtonNotificationMessageGreenOn,
iButtonNotificationMessageGreenOff,
iButtonNotificationMessageBlinkStop,
} iButtonNotificationMessage;
bool ibutton_file_select(iButton* ibutton);
bool ibutton_load_key_data(iButton* ibutton, string_t key_path, bool show_dialog);
bool ibutton_save_key(iButton* ibutton, const char* key_name);
bool ibutton_delete_key(iButton* ibutton);
void ibutton_text_store_set(iButton* ibutton, const char* text, ...);
void ibutton_text_store_clear(iButton* ibutton);
void ibutton_notification_message(iButton* ibutton, uint32_t message);

View File

@@ -0,0 +1,30 @@
#include "ibutton_scene.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const ibutton_on_enter_handlers[])(void*) = {
#include "ibutton_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const ibutton_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "ibutton_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const ibutton_on_exit_handlers[])(void* context) = {
#include "ibutton_scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers ibutton_scene_handlers = {
.on_enter_handlers = ibutton_on_enter_handlers,
.on_event_handlers = ibutton_on_event_handlers,
.on_exit_handlers = ibutton_on_exit_handlers,
.scene_num = iButtonSceneNum,
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) iButtonScene##id,
typedef enum {
#include "ibutton_scene_config.h"
iButtonSceneNum,
} iButtonScene;
#undef ADD_SCENE
extern const SceneManagerHandlers ibutton_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "ibutton_scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "ibutton_scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "ibutton_scene_config.h"
#undef ADD_SCENE

View File

@@ -0,0 +1,61 @@
#include "../ibutton_i.h"
#include "m-string.h"
enum SubmenuIndex {
SubmenuIndexCyfral,
SubmenuIndexDallas,
SubmenuIndexMetakom,
};
void ibutton_scene_add_type_submenu_callback(void* context, uint32_t index) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, index);
}
void ibutton_scene_add_type_on_enter(void* context) {
iButton* ibutton = context;
Submenu* submenu = ibutton->submenu;
submenu_add_item(
submenu, "Cyfral", SubmenuIndexCyfral, ibutton_scene_add_type_submenu_callback, ibutton);
submenu_add_item(
submenu, "Dallas", SubmenuIndexDallas, ibutton_scene_add_type_submenu_callback, ibutton);
submenu_add_item(
submenu, "Metakom", SubmenuIndexMetakom, ibutton_scene_add_type_submenu_callback, ibutton);
submenu_set_selected_item(
submenu, scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneAddType));
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewSubmenu);
}
bool ibutton_scene_add_type_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
iButtonKey* key = ibutton->key;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_set_scene_state(ibutton->scene_manager, iButtonSceneAddType, event.event);
consumed = true;
if(event.event == SubmenuIndexCyfral) {
ibutton_key_set_type(key, iButtonKeyCyfral);
} else if(event.event == SubmenuIndexDallas) {
ibutton_key_set_type(key, iButtonKeyDS1990);
} else if(event.event == SubmenuIndexMetakom) {
ibutton_key_set_type(key, iButtonKeyMetakom);
} else {
furi_crash("Unknown key type");
}
string_set_str(ibutton->file_path, IBUTTON_APP_FOLDER);
ibutton_key_clear_data(key);
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneAddValue);
}
return consumed;
}
void ibutton_scene_add_type_on_exit(void* context) {
iButton* ibutton = context;
submenu_reset(ibutton->submenu);
}

View File

@@ -0,0 +1,57 @@
#include "../ibutton_i.h"
#include <dolphin/dolphin.h>
void ibutton_scene_add_type_byte_input_callback(void* context) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventByteEditResult);
}
void ibutton_scene_add_value_on_enter(void* context) {
iButton* ibutton = context;
iButtonKey* key = ibutton->key;
uint8_t* new_key_data = malloc(IBUTTON_KEY_DATA_SIZE);
scene_manager_set_scene_state(
ibutton->scene_manager, iButtonSceneAddValue, (uint32_t)new_key_data);
memcpy(new_key_data, ibutton_key_get_data_p(key), ibutton_key_get_data_size(key));
byte_input_set_result_callback(
ibutton->byte_input,
ibutton_scene_add_type_byte_input_callback,
NULL,
ibutton,
new_key_data,
ibutton_key_get_data_size(key));
byte_input_set_header_text(ibutton->byte_input, "Enter the key");
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewByteInput);
}
bool ibutton_scene_add_value_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
uint8_t* new_key_data =
(uint8_t*)scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneAddValue);
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == iButtonCustomEventByteEditResult) {
ibutton_key_set_data(ibutton->key, new_key_data, IBUTTON_KEY_DATA_SIZE);
DOLPHIN_DEED(DolphinDeedIbuttonAdd);
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneSaveName);
}
}
return consumed;
}
void ibutton_scene_add_value_on_exit(void* context) {
iButton* ibutton = context;
uint8_t* new_key_data =
(uint8_t*)scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneAddValue);
byte_input_set_result_callback(ibutton->byte_input, NULL, NULL, NULL, NULL, 0);
byte_input_set_header_text(ibutton->byte_input, NULL);
free(new_key_data);
}

View File

@@ -0,0 +1,21 @@
ADD_SCENE(ibutton, start, Start)
ADD_SCENE(ibutton, emulate, Emulate)
ADD_SCENE(ibutton, write, Write)
ADD_SCENE(ibutton, write_success, WriteSuccess)
ADD_SCENE(ibutton, info, Info)
ADD_SCENE(ibutton, read, Read)
ADD_SCENE(ibutton, read_key_menu, ReadKeyMenu)
ADD_SCENE(ibutton, read_success, ReadSuccess)
ADD_SCENE(ibutton, read_crc_error, ReadCRCError)
ADD_SCENE(ibutton, read_not_key_error, ReadNotKeyError)
ADD_SCENE(ibutton, select_key, SelectKey)
ADD_SCENE(ibutton, add_type, AddType)
ADD_SCENE(ibutton, add_value, AddValue)
ADD_SCENE(ibutton, saved_key_menu, SavedKeyMenu)
ADD_SCENE(ibutton, save_name, SaveName)
ADD_SCENE(ibutton, save_success, SaveSuccess)
ADD_SCENE(ibutton, delete_confirm, DeleteConfirm)
ADD_SCENE(ibutton, delete_success, DeleteSuccess)
ADD_SCENE(ibutton, retry_confirm, RetryConfirm)
ADD_SCENE(ibutton, exit_confirm, ExitConfirm)
ADD_SCENE(ibutton, rpc, Rpc)

View File

@@ -0,0 +1,98 @@
#include "../ibutton_i.h"
#include <toolbox/path.h>
static void ibutton_scene_delete_confirm_widget_callback(
GuiButtonType result,
InputType type,
void* context) {
iButton* ibutton = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(ibutton->view_dispatcher, result);
}
}
void ibutton_scene_delete_confirm_on_enter(void* context) {
iButton* ibutton = context;
Widget* widget = ibutton->widget;
iButtonKey* key = ibutton->key;
const uint8_t* key_data = ibutton_key_get_data_p(key);
string_t key_name;
string_init(key_name);
path_extract_filename(ibutton->file_path, key_name, true);
ibutton_text_store_set(ibutton, "\e#Delete %s?\e#", string_get_cstr(key_name));
widget_add_text_box_element(
widget, 0, 0, 128, 27, AlignCenter, AlignCenter, ibutton->text_store, false);
widget_add_button_element(
widget, GuiButtonTypeLeft, "Cancel", ibutton_scene_delete_confirm_widget_callback, ibutton);
widget_add_button_element(
widget,
GuiButtonTypeRight,
"Delete",
ibutton_scene_delete_confirm_widget_callback,
ibutton);
switch(ibutton_key_get_type(key)) {
case iButtonKeyDS1990:
ibutton_text_store_set(
ibutton,
"%02X %02X %02X %02X %02X %02X %02X %02X",
key_data[0],
key_data[1],
key_data[2],
key_data[3],
key_data[4],
key_data[5],
key_data[6],
key_data[7]);
widget_add_string_element(
widget, 64, 45, AlignCenter, AlignBottom, FontSecondary, "Dallas");
break;
case iButtonKeyCyfral:
ibutton_text_store_set(ibutton, "%02X %02X", key_data[0], key_data[1]);
widget_add_string_element(
widget, 64, 45, AlignCenter, AlignBottom, FontSecondary, "Cyfral");
break;
case iButtonKeyMetakom:
ibutton_text_store_set(
ibutton, "%02X %02X %02X %02X", key_data[0], key_data[1], key_data[2], key_data[3]);
widget_add_string_element(
widget, 64, 45, AlignCenter, AlignBottom, FontSecondary, "Metakom");
break;
}
widget_add_string_element(
widget, 64, 33, AlignCenter, AlignBottom, FontSecondary, ibutton->text_store);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewWidget);
string_clear(key_name);
}
bool ibutton_scene_delete_confirm_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
SceneManager* scene_manager = ibutton->scene_manager;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == GuiButtonTypeRight) {
if(ibutton_delete_key(ibutton)) {
scene_manager_next_scene(scene_manager, iButtonSceneDeleteSuccess);
}
//TODO: What if the key could not be deleted?
} else if(event.event == GuiButtonTypeLeft) {
scene_manager_previous_scene(scene_manager);
}
}
return consumed;
}
void ibutton_scene_delete_confirm_on_exit(void* context) {
iButton* ibutton = context;
ibutton_text_store_clear(ibutton);
widget_reset(ibutton->widget);
}

View File

@@ -0,0 +1,48 @@
#include "../ibutton_i.h"
static void ibutton_scene_delete_success_popup_callback(void* context) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventBack);
}
void ibutton_scene_delete_success_on_enter(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
popup_set_icon(popup, 0, 2, &I_DolphinMafia_115x62);
popup_set_header(popup, "Deleted", 83, 19, AlignLeft, AlignBottom);
popup_set_callback(popup, ibutton_scene_delete_success_popup_callback);
popup_set_context(popup, ibutton);
popup_set_timeout(popup, 1500);
popup_enable_timeout(popup);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);
}
bool ibutton_scene_delete_success_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == iButtonCustomEventBack) {
scene_manager_search_and_switch_to_previous_scene(
ibutton->scene_manager, iButtonSceneSelectKey);
}
}
return consumed;
}
void ibutton_scene_delete_success_on_exit(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 0, NULL);
popup_disable_timeout(popup);
popup_set_context(popup, NULL);
popup_set_callback(popup, NULL);
}

View File

@@ -0,0 +1,131 @@
#include "../ibutton_i.h"
#include <core/log.h>
#include <dolphin/dolphin.h>
#include <toolbox/path.h>
#define EMULATE_TIMEOUT_TICKS 10
static void ibutton_scene_emulate_callback(void* context, bool emulated) {
iButton* ibutton = context;
if(emulated) {
view_dispatcher_send_custom_event(
ibutton->view_dispatcher, iButtonCustomEventWorkerEmulated);
}
}
void ibutton_scene_emulate_on_enter(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
iButtonKey* key = ibutton->key;
const uint8_t* key_data = ibutton_key_get_data_p(key);
string_t key_name;
string_init(key_name);
if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
path_extract_filename(ibutton->file_path, key_name, true);
}
uint8_t line_count = 2;
DOLPHIN_DEED(DolphinDeedIbuttonEmulate);
// check that stored key has name
if(!string_empty_p(key_name)) {
ibutton_text_store_set(ibutton, "emulating\n%s", string_get_cstr(key_name));
line_count = 2;
} else {
// if not, show key data
switch(ibutton_key_get_type(key)) {
case iButtonKeyDS1990:
ibutton_text_store_set(
ibutton,
"emulating\n%02X %02X %02X %02X\n%02X %02X %02X %02X",
key_data[0],
key_data[1],
key_data[2],
key_data[3],
key_data[4],
key_data[5],
key_data[6],
key_data[7]);
line_count = 3;
break;
case iButtonKeyCyfral:
ibutton_text_store_set(ibutton, "emulating\n%02X %02X", key_data[0], key_data[1]);
line_count = 2;
break;
case iButtonKeyMetakom:
ibutton_text_store_set(
ibutton,
"emulating\n%02X %02X %02X %02X",
key_data[0],
key_data[1],
key_data[2],
key_data[3]);
line_count = 2;
break;
}
}
switch(line_count) {
case 3:
popup_set_header(popup, "iButton", 82, 18, AlignCenter, AlignBottom);
popup_set_text(popup, ibutton->text_store, 82, 22, AlignCenter, AlignTop);
break;
default:
popup_set_header(popup, "iButton", 82, 24, AlignCenter, AlignBottom);
popup_set_text(popup, ibutton->text_store, 82, 28, AlignCenter, AlignTop);
break;
}
popup_set_icon(popup, 2, 10, &I_iButtonKey_49x44);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);
ibutton_worker_emulate_set_callback(
ibutton->key_worker, ibutton_scene_emulate_callback, ibutton);
ibutton_worker_emulate_start(ibutton->key_worker, key);
string_clear(key_name);
ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);
}
bool ibutton_scene_emulate_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeTick) {
uint32_t cnt = scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneEmulate);
if(cnt > 0) {
cnt--;
if(cnt == 0) {
ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateBlink);
}
scene_manager_set_scene_state(ibutton->scene_manager, iButtonSceneEmulate, cnt);
}
consumed = true;
} else if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == iButtonCustomEventWorkerEmulated) {
if(scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneEmulate) == 0) {
ibutton_notification_message(ibutton, iButtonNotificationMessageYellowBlink);
}
scene_manager_set_scene_state(
ibutton->scene_manager, iButtonSceneEmulate, EMULATE_TIMEOUT_TICKS);
}
}
return consumed;
}
void ibutton_scene_emulate_on_exit(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
ibutton_worker_stop(ibutton->key_worker);
popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 0, NULL);
ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
}

View File

@@ -0,0 +1,51 @@
#include "../ibutton_i.h"
static void ibutton_scene_exit_confirm_widget_callback(
GuiButtonType result,
InputType type,
void* context) {
iButton* ibutton = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(ibutton->view_dispatcher, result);
}
}
void ibutton_scene_exit_confirm_on_enter(void* context) {
iButton* ibutton = context;
Widget* widget = ibutton->widget;
widget_add_button_element(
widget, GuiButtonTypeLeft, "Exit", ibutton_scene_exit_confirm_widget_callback, ibutton);
widget_add_button_element(
widget, GuiButtonTypeRight, "Stay", ibutton_scene_exit_confirm_widget_callback, ibutton);
widget_add_string_element(
widget, 64, 19, AlignCenter, AlignBottom, FontPrimary, "Exit to iButton menu?");
widget_add_string_element(
widget, 64, 31, AlignCenter, AlignBottom, FontSecondary, "All unsaved data will be lost!");
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewWidget);
}
bool ibutton_scene_exit_confirm_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
SceneManager* scene_manager = ibutton->scene_manager;
bool consumed = false;
if(event.type == SceneManagerEventTypeBack) {
consumed = true; // Ignore Back button presses
} else if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == GuiButtonTypeLeft) {
scene_manager_search_and_switch_to_previous_scene(scene_manager, iButtonSceneStart);
} else if(event.event == GuiButtonTypeRight) {
scene_manager_previous_scene(scene_manager);
}
}
return consumed;
}
void ibutton_scene_exit_confirm_on_exit(void* context) {
iButton* ibutton = context;
widget_reset(ibutton->widget);
}

View File

@@ -0,0 +1,68 @@
#include "../ibutton_i.h"
#include <toolbox/path.h>
void ibutton_scene_info_on_enter(void* context) {
iButton* ibutton = context;
Widget* widget = ibutton->widget;
iButtonKey* key = ibutton->key;
const uint8_t* key_data = ibutton_key_get_data_p(key);
string_t key_name;
string_init(key_name);
path_extract_filename(ibutton->file_path, key_name, true);
ibutton_text_store_set(ibutton, "%s", string_get_cstr(key_name));
widget_add_text_box_element(
widget, 0, 0, 128, 28, AlignCenter, AlignCenter, ibutton->text_store, false);
switch(ibutton_key_get_type(key)) {
case iButtonKeyDS1990:
ibutton_text_store_set(
ibutton,
"%02X %02X %02X %02X %02X %02X %02X %02X",
key_data[0],
key_data[1],
key_data[2],
key_data[3],
key_data[4],
key_data[5],
key_data[6],
key_data[7]);
widget_add_string_element(
widget, 64, 51, AlignCenter, AlignBottom, FontSecondary, "Dallas");
break;
case iButtonKeyMetakom:
ibutton_text_store_set(
ibutton, "%02X %02X %02X %02X", key_data[0], key_data[1], key_data[2], key_data[3]);
widget_add_string_element(
widget, 64, 51, AlignCenter, AlignBottom, FontSecondary, "Metakom");
break;
case iButtonKeyCyfral:
ibutton_text_store_set(ibutton, "%02X %02X", key_data[0], key_data[1]);
widget_add_string_element(
widget, 64, 51, AlignCenter, AlignBottom, FontSecondary, "Cyfral");
break;
}
widget_add_string_element(
widget, 64, 35, AlignCenter, AlignBottom, FontPrimary, ibutton->text_store);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewWidget);
string_clear(key_name);
}
bool ibutton_scene_info_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
return false;
}
void ibutton_scene_info_on_exit(void* context) {
iButton* ibutton = context;
ibutton_text_store_clear(ibutton);
widget_reset(ibutton->widget);
}

View File

@@ -0,0 +1,75 @@
#include "../ibutton_i.h"
#include <dolphin/dolphin.h>
static void ibutton_scene_read_callback(void* context) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventWorkerRead);
}
void ibutton_scene_read_on_enter(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
iButtonKey* key = ibutton->key;
iButtonWorker* worker = ibutton->key_worker;
DOLPHIN_DEED(DolphinDeedIbuttonRead);
popup_set_header(popup, "iButton", 95, 26, AlignCenter, AlignBottom);
popup_set_text(popup, "Waiting\nfor key ...", 95, 30, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 5, &I_DolphinWait_61x59);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);
string_set_str(ibutton->file_path, IBUTTON_APP_FOLDER);
ibutton_worker_read_set_callback(worker, ibutton_scene_read_callback, ibutton);
ibutton_worker_read_start(worker, key);
ibutton_notification_message(ibutton, iButtonNotificationMessageReadStart);
}
bool ibutton_scene_read_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
SceneManager* scene_manager = ibutton->scene_manager;
bool consumed = false;
if(event.type == SceneManagerEventTypeTick) {
consumed = true;
} else if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == iButtonCustomEventWorkerRead) {
bool success = false;
iButtonKey* key = ibutton->key;
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
if(!ibutton_key_dallas_crc_is_valid(key)) {
scene_manager_next_scene(scene_manager, iButtonSceneReadCRCError);
} else if(!ibutton_key_dallas_is_1990_key(key)) {
scene_manager_next_scene(scene_manager, iButtonSceneReadNotKeyError);
} else {
success = true;
}
} else {
success = true;
}
if(success) {
ibutton_notification_message(ibutton, iButtonNotificationMessageSuccess);
ibutton_notification_message(ibutton, iButtonNotificationMessageGreenOn);
DOLPHIN_DEED(DolphinDeedIbuttonReadSuccess);
scene_manager_next_scene(scene_manager, iButtonSceneReadSuccess);
}
}
}
return consumed;
}
void ibutton_scene_read_on_exit(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
ibutton_worker_stop(ibutton->key_worker);
popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 0, NULL);
ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
}

View File

@@ -0,0 +1,70 @@
#include "../ibutton_i.h"
#include <one_wire/maxim_crc.h>
static void ibutton_scene_read_crc_error_dialog_ex_callback(DialogExResult result, void* context) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, result);
}
void ibutton_scene_read_crc_error_on_enter(void* context) {
iButton* ibutton = context;
DialogEx* dialog_ex = ibutton->dialog_ex;
iButtonKey* key = ibutton->key;
const uint8_t* key_data = ibutton_key_get_data_p(key);
ibutton_text_store_set(
ibutton,
"%02X %02X %02X %02X %02X %02X %02X %02X\nExpected CRC: %X",
key_data[0],
key_data[1],
key_data[2],
key_data[3],
key_data[4],
key_data[5],
key_data[6],
key_data[7],
maxim_crc8(key_data, 7, MAXIM_CRC8_INIT));
dialog_ex_set_header(dialog_ex, "CRC ERROR", 64, 10, AlignCenter, AlignCenter);
dialog_ex_set_text(dialog_ex, ibutton->text_store, 64, 19, AlignCenter, AlignTop);
dialog_ex_set_left_button_text(dialog_ex, "Retry");
dialog_ex_set_right_button_text(dialog_ex, "More");
dialog_ex_set_result_callback(dialog_ex, ibutton_scene_read_crc_error_dialog_ex_callback);
dialog_ex_set_context(dialog_ex, ibutton);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewDialogEx);
ibutton_notification_message(ibutton, iButtonNotificationMessageError);
ibutton_notification_message(ibutton, iButtonNotificationMessageRedOn);
}
bool ibutton_scene_read_crc_error_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
SceneManager* scene_manager = ibutton->scene_manager;
bool consumed = false;
if(event.type == SceneManagerEventTypeBack) {
consumed = true;
scene_manager_next_scene(scene_manager, iButtonSceneExitConfirm);
} else if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == DialogExResultRight) {
scene_manager_next_scene(scene_manager, iButtonSceneReadKeyMenu);
} else if(event.event == DialogExResultLeft) {
scene_manager_previous_scene(scene_manager);
}
}
return consumed;
}
void ibutton_scene_read_crc_error_on_exit(void* context) {
iButton* ibutton = context;
DialogEx* dialog_ex = ibutton->dialog_ex;
ibutton_text_store_clear(ibutton);
dialog_ex_reset(dialog_ex);
ibutton_notification_message(ibutton, iButtonNotificationMessageRedOff);
}

View File

@@ -0,0 +1,63 @@
#include "../ibutton_i.h"
typedef enum {
SubmenuIndexSave,
SubmenuIndexEmulate,
SubmenuIndexWrite,
} SubmenuIndex;
void ibutton_scene_read_key_menu_submenu_callback(void* context, uint32_t index) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, index);
}
void ibutton_scene_read_key_menu_on_enter(void* context) {
iButton* ibutton = context;
Submenu* submenu = ibutton->submenu;
submenu_add_item(
submenu, "Save", SubmenuIndexSave, ibutton_scene_read_key_menu_submenu_callback, ibutton);
submenu_add_item(
submenu,
"Emulate",
SubmenuIndexEmulate,
ibutton_scene_read_key_menu_submenu_callback,
ibutton);
if(ibutton_key_get_type(ibutton->key) == iButtonKeyDS1990) {
submenu_add_item(
submenu,
"Write",
SubmenuIndexWrite,
ibutton_scene_read_key_menu_submenu_callback,
ibutton);
}
submenu_set_selected_item(
submenu, scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneReadKeyMenu));
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewSubmenu);
}
bool ibutton_scene_read_key_menu_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_set_scene_state(
ibutton->scene_manager, iButtonSceneReadKeyMenu, event.event);
consumed = true;
if(event.event == SubmenuIndexSave) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneSaveName);
} else if(event.event == SubmenuIndexEmulate) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneEmulate);
} else if(event.event == SubmenuIndexWrite) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneWrite);
}
}
return consumed;
}
void ibutton_scene_read_key_menu_on_exit(void* context) {
iButton* ibutton = context;
submenu_reset(ibutton->submenu);
}

View File

@@ -0,0 +1,71 @@
#include "../ibutton_i.h"
#include <one_wire/maxim_crc.h>
static void
ibutton_scene_read_not_key_error_dialog_ex_callback(DialogExResult result, void* context) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, result);
}
void ibutton_scene_read_not_key_error_on_enter(void* context) {
iButton* ibutton = context;
DialogEx* dialog_ex = ibutton->dialog_ex;
iButtonKey* key = ibutton->key;
const uint8_t* key_data = ibutton_key_get_data_p(key);
ibutton_text_store_set(
ibutton,
"THIS IS NOT A KEY\n%02X %02X %02X %02X %02X %02X %02X %02X",
key_data[0],
key_data[1],
key_data[2],
key_data[3],
key_data[4],
key_data[5],
key_data[6],
key_data[7],
maxim_crc8(key_data, 7, MAXIM_CRC8_INIT));
dialog_ex_set_header(dialog_ex, "CRC ERROR", 64, 10, AlignCenter, AlignCenter);
dialog_ex_set_text(dialog_ex, ibutton->text_store, 64, 19, AlignCenter, AlignTop);
dialog_ex_set_left_button_text(dialog_ex, "Retry");
dialog_ex_set_right_button_text(dialog_ex, "More");
dialog_ex_set_result_callback(dialog_ex, ibutton_scene_read_not_key_error_dialog_ex_callback);
dialog_ex_set_context(dialog_ex, ibutton);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewDialogEx);
ibutton_notification_message(ibutton, iButtonNotificationMessageError);
ibutton_notification_message(ibutton, iButtonNotificationMessageRedOn);
}
bool ibutton_scene_read_not_key_error_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
SceneManager* scene_manager = ibutton->scene_manager;
bool consumed = false;
if(event.type == SceneManagerEventTypeBack) {
consumed = true;
scene_manager_next_scene(scene_manager, iButtonSceneExitConfirm);
} else if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == DialogExResultRight) {
scene_manager_next_scene(scene_manager, iButtonSceneReadKeyMenu);
} else if(event.event == DialogExResultLeft) {
scene_manager_previous_scene(scene_manager);
}
}
return consumed;
}
void ibutton_scene_read_not_key_error_on_exit(void* context) {
iButton* ibutton = context;
DialogEx* dialog_ex = ibutton->dialog_ex;
ibutton_text_store_clear(ibutton);
dialog_ex_reset(dialog_ex);
ibutton_notification_message(ibutton, iButtonNotificationMessageRedOff);
}

View File

@@ -0,0 +1,82 @@
#include "../ibutton_i.h"
#include <dolphin/dolphin.h>
static void ibutton_scene_read_success_dialog_ex_callback(DialogExResult result, void* context) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, result);
}
void ibutton_scene_read_success_on_enter(void* context) {
iButton* ibutton = context;
DialogEx* dialog_ex = ibutton->dialog_ex;
iButtonKey* key = ibutton->key;
const uint8_t* key_data = ibutton_key_get_data_p(key);
switch(ibutton_key_get_type(key)) {
case iButtonKeyDS1990:
ibutton_text_store_set(
ibutton,
"Dallas\n%02X %02X %02X %02X\n%02X %02X %02X %02X",
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 iButtonKeyCyfral:
ibutton_text_store_set(ibutton, "Cyfral\n%02X %02X", key_data[0], key_data[1]);
break;
case iButtonKeyMetakom:
ibutton_text_store_set(
ibutton,
"Metakom\n%02X %02X %02X %02X",
key_data[0],
key_data[1],
key_data[2],
key_data[3]);
break;
}
dialog_ex_set_text(dialog_ex, ibutton->text_store, 95, 30, AlignCenter, AlignCenter);
dialog_ex_set_left_button_text(dialog_ex, "Retry");
dialog_ex_set_right_button_text(dialog_ex, "More");
dialog_ex_set_icon(dialog_ex, 0, 1, &I_DolphinReadingSuccess_59x63);
dialog_ex_set_result_callback(dialog_ex, ibutton_scene_read_success_dialog_ex_callback);
dialog_ex_set_context(dialog_ex, ibutton);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewDialogEx);
}
bool ibutton_scene_read_success_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
SceneManager* scene_manager = ibutton->scene_manager;
bool consumed = false;
if(event.type == SceneManagerEventTypeBack) {
consumed = true;
scene_manager_next_scene(scene_manager, iButtonSceneExitConfirm);
} else if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == DialogExResultRight) {
scene_manager_next_scene(scene_manager, iButtonSceneReadKeyMenu);
} else if(event.event == DialogExResultLeft) {
scene_manager_next_scene(scene_manager, iButtonSceneRetryConfirm);
}
}
return consumed;
}
void ibutton_scene_read_success_on_exit(void* context) {
iButton* ibutton = context;
DialogEx* dialog_ex = ibutton->dialog_ex;
ibutton_text_store_clear(ibutton);
dialog_ex_reset(dialog_ex);
ibutton_notification_message(ibutton, iButtonNotificationMessageGreenOff);
}

View File

@@ -0,0 +1,51 @@
#include "../ibutton_i.h"
static void ibutton_scene_retry_confirm_widget_callback(
GuiButtonType result,
InputType type,
void* context) {
iButton* ibutton = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(ibutton->view_dispatcher, result);
}
}
void ibutton_scene_retry_confirm_on_enter(void* context) {
iButton* ibutton = context;
Widget* widget = ibutton->widget;
widget_add_button_element(
widget, GuiButtonTypeLeft, "Exit", ibutton_scene_retry_confirm_widget_callback, ibutton);
widget_add_button_element(
widget, GuiButtonTypeRight, "Stay", ibutton_scene_retry_confirm_widget_callback, ibutton);
widget_add_string_element(
widget, 64, 19, AlignCenter, AlignBottom, FontPrimary, "Return to reading?");
widget_add_string_element(
widget, 64, 29, AlignCenter, AlignBottom, FontSecondary, "All unsaved data will be lost!");
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewWidget);
}
bool ibutton_scene_retry_confirm_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
SceneManager* scene_manager = ibutton->scene_manager;
bool consumed = false;
if(event.type == SceneManagerEventTypeBack) {
consumed = true; // Ignore Back button presses
} else if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == GuiButtonTypeLeft) {
scene_manager_search_and_switch_to_previous_scene(scene_manager, iButtonSceneRead);
} else if(event.event == GuiButtonTypeRight) {
scene_manager_previous_scene(scene_manager);
}
}
return consumed;
}
void ibutton_scene_retry_confirm_on_exit(void* context) {
iButton* ibutton = context;
widget_reset(ibutton->widget);
}

View File

@@ -0,0 +1,81 @@
#include "../ibutton_i.h"
#include <toolbox/path.h>
#include <rpc/rpc_app.h>
void ibutton_scene_rpc_on_enter(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
popup_set_header(popup, "iButton", 82, 28, AlignCenter, AlignBottom);
popup_set_text(popup, "RPC mode", 82, 32, AlignCenter, AlignTop);
popup_set_icon(popup, 2, 14, &I_iButtonKey_49x44);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);
notification_message(ibutton->notifications, &sequence_display_backlight_on);
}
bool ibutton_scene_rpc_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
iButton* ibutton = context;
Popup* popup = ibutton->popup;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == iButtonCustomEventRpcLoad) {
const char* arg = rpc_system_app_get_data(ibutton->rpc_ctx);
bool result = false;
if(arg && (string_empty_p(ibutton->file_path))) {
string_set_str(ibutton->file_path, arg);
if(ibutton_load_key_data(ibutton, ibutton->file_path, false)) {
ibutton_worker_emulate_start(ibutton->key_worker, ibutton->key);
string_t key_name;
string_init(key_name);
if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
path_extract_filename(ibutton->file_path, key_name, true);
}
if(!string_empty_p(key_name)) {
ibutton_text_store_set(
ibutton, "emulating\n%s", string_get_cstr(key_name));
} else {
ibutton_text_store_set(ibutton, "emulating");
}
popup_set_text(popup, ibutton->text_store, 82, 32, AlignCenter, AlignTop);
ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);
string_clear(key_name);
result = true;
} else {
string_reset(ibutton->file_path);
}
}
rpc_system_app_confirm(ibutton->rpc_ctx, RpcAppEventLoadFile, result);
} else if(event.event == iButtonCustomEventRpcExit) {
rpc_system_app_confirm(ibutton->rpc_ctx, RpcAppEventAppExit, true);
scene_manager_stop(ibutton->scene_manager);
view_dispatcher_stop(ibutton->view_dispatcher);
} else if(event.event == iButtonCustomEventRpcSessionClose) {
scene_manager_stop(ibutton->scene_manager);
view_dispatcher_stop(ibutton->view_dispatcher);
}
}
return consumed;
}
void ibutton_scene_rpc_on_exit(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 0, NULL);
ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
}

View File

@@ -0,0 +1,82 @@
#include "../ibutton_i.h"
#include "m-string.h"
#include <lib/toolbox/random_name.h>
#include <toolbox/path.h>
static void ibutton_scene_save_name_text_input_callback(void* context) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventTextEditResult);
}
void ibutton_scene_save_name_on_enter(void* context) {
iButton* ibutton = context;
TextInput* text_input = ibutton->text_input;
string_t key_name;
string_init(key_name);
if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
path_extract_filename(ibutton->file_path, key_name, true);
}
const bool key_name_is_empty = string_empty_p(key_name);
if(key_name_is_empty) {
set_random_name(ibutton->text_store, IBUTTON_TEXT_STORE_SIZE);
} else {
ibutton_text_store_set(ibutton, "%s", string_get_cstr(key_name));
}
text_input_set_header_text(text_input, "Name the key");
text_input_set_result_callback(
text_input,
ibutton_scene_save_name_text_input_callback,
ibutton,
ibutton->text_store,
IBUTTON_KEY_NAME_SIZE,
key_name_is_empty);
string_t folder_path;
string_init(folder_path);
path_extract_dirname(string_get_cstr(ibutton->file_path), folder_path);
ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
string_get_cstr(folder_path), IBUTTON_APP_EXTENSION, string_get_cstr(key_name));
text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewTextInput);
string_clear(key_name);
string_clear(folder_path);
}
bool ibutton_scene_save_name_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == iButtonCustomEventTextEditResult) {
if(ibutton_save_key(ibutton, ibutton->text_store)) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneSaveSuccess);
} else {
const uint32_t possible_scenes[] = {
iButtonSceneReadKeyMenu, iButtonSceneSavedKeyMenu, iButtonSceneAddType};
scene_manager_search_and_switch_to_previous_scene_one_of(
ibutton->scene_manager, possible_scenes, COUNT_OF(possible_scenes));
}
}
}
return consumed;
}
void ibutton_scene_save_name_on_exit(void* context) {
iButton* ibutton = context;
TextInput* text_input = ibutton->text_input;
void* validator_context = text_input_get_validator_callback_context(text_input);
text_input_set_validator(text_input, NULL, NULL);
validator_is_file_free((ValidatorIsFile*)validator_context);
text_input_reset(text_input);
}

View File

@@ -0,0 +1,52 @@
#include "../ibutton_i.h"
#include <dolphin/dolphin.h>
static void ibutton_scene_save_success_popup_callback(void* context) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventBack);
}
void ibutton_scene_save_success_on_enter(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
DOLPHIN_DEED(DolphinDeedIbuttonSave);
popup_set_icon(popup, 32, 5, &I_DolphinNice_96x59);
popup_set_header(popup, "Saved!", 5, 7, AlignLeft, AlignTop);
popup_set_callback(popup, ibutton_scene_save_success_popup_callback);
popup_set_context(popup, ibutton);
popup_set_timeout(popup, 1500);
popup_enable_timeout(popup);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);
}
bool ibutton_scene_save_success_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == iButtonCustomEventBack) {
const uint32_t possible_scenes[] = {
iButtonSceneReadKeyMenu, iButtonSceneSavedKeyMenu, iButtonSceneAddType};
scene_manager_search_and_switch_to_previous_scene_one_of(
ibutton->scene_manager, possible_scenes, COUNT_OF(possible_scenes));
}
}
return consumed;
}
void ibutton_scene_save_success_on_exit(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 0, NULL);
popup_disable_timeout(popup);
popup_set_context(popup, NULL);
popup_set_callback(popup, NULL);
}

View File

@@ -0,0 +1,78 @@
#include "../ibutton_i.h"
enum SubmenuIndex {
SubmenuIndexEmulate,
SubmenuIndexWrite,
SubmenuIndexEdit,
SubmenuIndexDelete,
SubmenuIndexInfo,
};
void ibutton_scene_saved_key_menu_submenu_callback(void* context, uint32_t index) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, index);
}
void ibutton_scene_saved_key_menu_on_enter(void* context) {
iButton* ibutton = context;
Submenu* submenu = ibutton->submenu;
submenu_add_item(
submenu,
"Emulate",
SubmenuIndexEmulate,
ibutton_scene_saved_key_menu_submenu_callback,
ibutton);
if(ibutton_key_get_type(ibutton->key) == iButtonKeyDS1990) {
submenu_add_item(
submenu,
"Write",
SubmenuIndexWrite,
ibutton_scene_saved_key_menu_submenu_callback,
ibutton);
}
submenu_add_item(
submenu, "Edit", SubmenuIndexEdit, ibutton_scene_saved_key_menu_submenu_callback, ibutton);
submenu_add_item(
submenu,
"Delete",
SubmenuIndexDelete,
ibutton_scene_saved_key_menu_submenu_callback,
ibutton);
submenu_add_item(
submenu, "Info", SubmenuIndexInfo, ibutton_scene_saved_key_menu_submenu_callback, ibutton);
submenu_set_selected_item(
submenu, scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneSavedKeyMenu));
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewSubmenu);
}
bool ibutton_scene_saved_key_menu_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_set_scene_state(
ibutton->scene_manager, iButtonSceneSavedKeyMenu, event.event);
consumed = true;
if(event.event == SubmenuIndexEmulate) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneEmulate);
} else if(event.event == SubmenuIndexWrite) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneWrite);
} else if(event.event == SubmenuIndexEdit) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneAddValue);
} else if(event.event == SubmenuIndexDelete) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneDeleteConfirm);
} else if(event.event == SubmenuIndexInfo) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneInfo);
}
}
return consumed;
}
void ibutton_scene_saved_key_menu_on_exit(void* context) {
iButton* ibutton = context;
submenu_reset(ibutton->submenu);
}

View File

@@ -0,0 +1,22 @@
#include "../ibutton_i.h"
void ibutton_scene_select_key_on_enter(void* context) {
iButton* ibutton = context;
if(!ibutton_file_select(ibutton)) {
scene_manager_search_and_switch_to_previous_scene(
ibutton->scene_manager, iButtonSceneStart);
} else {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneSavedKeyMenu);
}
}
bool ibutton_scene_select_key_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
return false;
}
void ibutton_scene_select_key_on_exit(void* context) {
UNUSED(context);
}

View File

@@ -0,0 +1,55 @@
#include "../ibutton_i.h"
#include "ibutton/scenes/ibutton_scene.h"
enum SubmenuIndex {
SubmenuIndexRead,
SubmenuIndexSaved,
SubmenuIndexAdd,
};
void ibutton_scene_start_submenu_callback(void* context, uint32_t index) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, index);
}
void ibutton_scene_start_on_enter(void* context) {
iButton* ibutton = context;
Submenu* submenu = ibutton->submenu;
submenu_add_item(
submenu, "Read", SubmenuIndexRead, ibutton_scene_start_submenu_callback, ibutton);
submenu_add_item(
submenu, "Saved", SubmenuIndexSaved, ibutton_scene_start_submenu_callback, ibutton);
submenu_add_item(
submenu, "Add Manually", SubmenuIndexAdd, ibutton_scene_start_submenu_callback, ibutton);
submenu_set_selected_item(
submenu, scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneStart));
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewSubmenu);
}
bool ibutton_scene_start_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_set_scene_state(ibutton->scene_manager, iButtonSceneStart, event.event);
consumed = true;
if(event.event == SubmenuIndexRead) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneRead);
} else if(event.event == SubmenuIndexSaved) {
string_set_str(ibutton->file_path, IBUTTON_APP_FOLDER);
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneSelectKey);
} else if(event.event == SubmenuIndexAdd) {
scene_manager_next_scene(ibutton->scene_manager, iButtonSceneAddType);
}
}
return consumed;
}
void ibutton_scene_start_on_exit(void* context) {
iButton* ibutton = context;
submenu_reset(ibutton->submenu);
}

View File

@@ -0,0 +1,124 @@
#include "../ibutton_i.h"
#include "m-string.h"
#include "toolbox/path.h"
typedef enum {
iButtonSceneWriteStateDefault,
iButtonSceneWriteStateBlinkYellow,
} iButtonSceneWriteState;
static void ibutton_scene_write_callback(void* context, iButtonWorkerWriteResult result) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, result);
}
void ibutton_scene_write_on_enter(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
iButtonKey* key = ibutton->key;
iButtonWorker* worker = ibutton->key_worker;
const uint8_t* key_data = ibutton_key_get_data_p(key);
string_t key_name;
string_init(key_name);
if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
path_extract_filename(ibutton->file_path, key_name, true);
}
uint8_t line_count = 2;
// check that stored key has name
if(!string_empty_p(key_name)) {
ibutton_text_store_set(ibutton, "writing\n%s", string_get_cstr(key_name));
line_count = 2;
} else {
// if not, show key data
switch(ibutton_key_get_type(key)) {
case iButtonKeyDS1990:
ibutton_text_store_set(
ibutton,
"writing\n%02X %02X %02X %02X\n%02X %02X %02X %02X",
key_data[0],
key_data[1],
key_data[2],
key_data[3],
key_data[4],
key_data[5],
key_data[6],
key_data[7]);
line_count = 3;
break;
case iButtonKeyCyfral:
ibutton_text_store_set(ibutton, "writing\n%02X %02X", key_data[0], key_data[1]);
line_count = 2;
break;
case iButtonKeyMetakom:
ibutton_text_store_set(
ibutton,
"writing\n%02X %02X %02X %02X",
key_data[0],
key_data[1],
key_data[2],
key_data[3]);
line_count = 2;
break;
}
}
switch(line_count) {
case 3:
popup_set_header(popup, "iButton", 82, 18, AlignCenter, AlignBottom);
popup_set_text(popup, ibutton->text_store, 82, 22, AlignCenter, AlignTop);
break;
default:
popup_set_header(popup, "iButton", 82, 24, AlignCenter, AlignBottom);
popup_set_text(popup, ibutton->text_store, 82, 28, AlignCenter, AlignTop);
break;
}
popup_set_icon(popup, 2, 10, &I_iButtonKey_49x44);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);
ibutton_worker_write_set_callback(worker, ibutton_scene_write_callback, ibutton);
ibutton_worker_write_start(worker, key);
string_clear(key_name);
ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);
}
bool ibutton_scene_write_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
SceneManager* scene_manager = ibutton->scene_manager;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if((event.event == iButtonWorkerWriteOK) || (event.event == iButtonWorkerWriteSameKey)) {
scene_manager_next_scene(scene_manager, iButtonSceneWriteSuccess);
} else if(event.event == iButtonWorkerWriteNoDetect) {
ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateBlink);
} else if(event.event == iButtonWorkerWriteCannotWrite) {
ibutton_notification_message(ibutton, iButtonNotificationMessageYellowBlink);
}
} else if(event.type == SceneManagerEventTypeTick) {
consumed = true;
}
return consumed;
}
void ibutton_scene_write_on_exit(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
ibutton_worker_stop(ibutton->key_worker);
popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 0, NULL);
ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
}

View File

@@ -0,0 +1,52 @@
#include "../ibutton_i.h"
static void ibutton_scene_write_success_popup_callback(void* context) {
iButton* ibutton = context;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventBack);
ibutton_notification_message(ibutton, iButtonNotificationMessageGreenOff);
}
void ibutton_scene_write_success_on_enter(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
popup_set_icon(popup, 0, 12, &I_iButtonDolphinVerySuccess_108x52);
popup_set_text(popup, "Successfully written!", 40, 12, AlignLeft, AlignBottom);
popup_set_callback(popup, ibutton_scene_write_success_popup_callback);
popup_set_context(popup, ibutton);
popup_set_timeout(popup, 1500);
popup_enable_timeout(popup);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);
ibutton_notification_message(ibutton, iButtonNotificationMessageSuccess);
ibutton_notification_message(ibutton, iButtonNotificationMessageGreenOn);
}
bool ibutton_scene_write_success_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == iButtonCustomEventBack) {
const uint32_t possible_scenes[] = {iButtonSceneReadKeyMenu, iButtonSceneStart};
scene_manager_search_and_switch_to_previous_scene_one_of(
ibutton->scene_manager, possible_scenes, COUNT_OF(possible_scenes));
}
}
return consumed;
}
void ibutton_scene_write_success_on_exit(void* context) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;
popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 0, NULL);
popup_disable_timeout(popup);
popup_set_context(popup, NULL);
popup_set_callback(popup, NULL);
}

View File

@@ -0,0 +1,23 @@
App(
appid="infrared",
name="Infrared",
apptype=FlipperAppType.APP,
entry_point="infrared_app",
cdefines=["APP_INFRARED"],
requires=[
"gui",
"dialogs",
],
provides=["infrared_start"],
icon="A_Infrared_14",
stack_size=3 * 1024,
order=40,
)
App(
appid="infrared_start",
apptype=FlipperAppType.STARTUP,
entry_point="infrared_on_system_start",
requires=["infrared"],
order=20,
)

View File

@@ -0,0 +1,466 @@
#include "infrared_i.h"
#include <string.h>
#include <dolphin/dolphin.h>
static const NotificationSequence* infrared_notification_sequences[] = {
&sequence_success,
&sequence_set_only_green_255,
&sequence_reset_green,
&sequence_solid_yellow,
&sequence_reset_rgb,
&sequence_blink_start_cyan,
&sequence_blink_start_magenta,
&sequence_blink_stop,
};
static void infrared_make_app_folder(Infrared* infrared) {
if(!storage_simply_mkdir(infrared->storage, INFRARED_APP_FOLDER)) {
dialog_message_show_storage_error(infrared->dialogs, "Cannot create\napp folder");
}
}
static bool infrared_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
Infrared* infrared = context;
return scene_manager_handle_custom_event(infrared->scene_manager, event);
}
static bool infrared_back_event_callback(void* context) {
furi_assert(context);
Infrared* infrared = context;
return scene_manager_handle_back_event(infrared->scene_manager);
}
static void infrared_tick_event_callback(void* context) {
furi_assert(context);
Infrared* infrared = context;
scene_manager_handle_tick_event(infrared->scene_manager);
}
static void infrared_rpc_command_callback(RpcAppSystemEvent event, void* context) {
furi_assert(context);
Infrared* infrared = context;
furi_assert(infrared->rpc_ctx);
if(event == RpcAppEventSessionClose) {
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeRpcSessionClose);
rpc_system_app_set_callback(infrared->rpc_ctx, NULL, NULL);
infrared->rpc_ctx = NULL;
} else if(event == RpcAppEventAppExit) {
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeRpcExit);
} else if(event == RpcAppEventLoadFile) {
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeRpcLoad);
} else if(event == RpcAppEventButtonPress) {
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonPress);
} else if(event == RpcAppEventButtonRelease) {
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonRelease);
} else {
rpc_system_app_confirm(infrared->rpc_ctx, event, false);
}
}
static void infrared_find_vacant_remote_name(string_t name, const char* path) {
Storage* storage = furi_record_open(RECORD_STORAGE);
string_t base_path;
string_init_set_str(base_path, path);
if(string_end_with_str_p(base_path, INFRARED_APP_EXTENSION)) {
size_t filename_start = string_search_rchar(base_path, '/');
string_left(base_path, filename_start);
}
string_printf(base_path, "%s/%s%s", path, string_get_cstr(name), INFRARED_APP_EXTENSION);
FS_Error status = storage_common_stat(storage, string_get_cstr(base_path), NULL);
if(status == FSE_OK) {
/* If the suggested name is occupied, try another one (name2, name3, etc) */
size_t dot = string_search_rchar(base_path, '.');
string_left(base_path, dot);
string_t path_temp;
string_init(path_temp);
uint32_t i = 1;
do {
string_printf(
path_temp, "%s%u%s", string_get_cstr(base_path), ++i, INFRARED_APP_EXTENSION);
status = storage_common_stat(storage, string_get_cstr(path_temp), NULL);
} while(status == FSE_OK);
string_clear(path_temp);
if(status == FSE_NOT_EXIST) {
string_cat_printf(name, "%u", i);
}
}
string_clear(base_path);
furi_record_close(RECORD_STORAGE);
}
static Infrared* infrared_alloc() {
Infrared* infrared = malloc(sizeof(Infrared));
string_init(infrared->file_path);
InfraredAppState* app_state = &infrared->app_state;
app_state->is_learning_new_remote = false;
app_state->is_debug_enabled = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);
app_state->edit_target = InfraredEditTargetNone;
app_state->edit_mode = InfraredEditModeNone;
app_state->current_button_index = InfraredButtonIndexNone;
infrared->scene_manager = scene_manager_alloc(&infrared_scene_handlers, infrared);
infrared->view_dispatcher = view_dispatcher_alloc();
infrared->gui = furi_record_open(RECORD_GUI);
ViewDispatcher* view_dispatcher = infrared->view_dispatcher;
view_dispatcher_enable_queue(view_dispatcher);
view_dispatcher_set_event_callback_context(view_dispatcher, infrared);
view_dispatcher_set_custom_event_callback(view_dispatcher, infrared_custom_event_callback);
view_dispatcher_set_navigation_event_callback(view_dispatcher, infrared_back_event_callback);
view_dispatcher_set_tick_event_callback(view_dispatcher, infrared_tick_event_callback, 100);
infrared->storage = furi_record_open(RECORD_STORAGE);
infrared->dialogs = furi_record_open(RECORD_DIALOGS);
infrared->notifications = furi_record_open(RECORD_NOTIFICATION);
infrared->worker = infrared_worker_alloc();
infrared->remote = infrared_remote_alloc();
infrared->received_signal = infrared_signal_alloc();
infrared->brute_force = infrared_brute_force_alloc();
infrared->submenu = submenu_alloc();
view_dispatcher_add_view(
view_dispatcher, InfraredViewSubmenu, submenu_get_view(infrared->submenu));
infrared->text_input = text_input_alloc();
view_dispatcher_add_view(
view_dispatcher, InfraredViewTextInput, text_input_get_view(infrared->text_input));
infrared->dialog_ex = dialog_ex_alloc();
view_dispatcher_add_view(
view_dispatcher, InfraredViewDialogEx, dialog_ex_get_view(infrared->dialog_ex));
infrared->button_menu = button_menu_alloc();
view_dispatcher_add_view(
view_dispatcher, InfraredViewButtonMenu, button_menu_get_view(infrared->button_menu));
infrared->popup = popup_alloc();
view_dispatcher_add_view(view_dispatcher, InfraredViewPopup, popup_get_view(infrared->popup));
infrared->view_stack = view_stack_alloc();
view_dispatcher_add_view(
view_dispatcher, InfraredViewStack, view_stack_get_view(infrared->view_stack));
if(app_state->is_debug_enabled) {
infrared->debug_view = infrared_debug_view_alloc();
view_dispatcher_add_view(
view_dispatcher,
InfraredViewDebugView,
infrared_debug_view_get_view(infrared->debug_view));
}
infrared->button_panel = button_panel_alloc();
infrared->loading = loading_alloc();
infrared->progress = infrared_progress_view_alloc();
return infrared;
}
static void infrared_free(Infrared* infrared) {
furi_assert(infrared);
ViewDispatcher* view_dispatcher = infrared->view_dispatcher;
InfraredAppState* app_state = &infrared->app_state;
if(infrared->rpc_ctx) {
rpc_system_app_set_callback(infrared->rpc_ctx, NULL, NULL);
rpc_system_app_send_exited(infrared->rpc_ctx);
infrared->rpc_ctx = NULL;
}
view_dispatcher_remove_view(view_dispatcher, InfraredViewSubmenu);
submenu_free(infrared->submenu);
view_dispatcher_remove_view(view_dispatcher, InfraredViewTextInput);
text_input_free(infrared->text_input);
view_dispatcher_remove_view(view_dispatcher, InfraredViewDialogEx);
dialog_ex_free(infrared->dialog_ex);
view_dispatcher_remove_view(view_dispatcher, InfraredViewButtonMenu);
button_menu_free(infrared->button_menu);
view_dispatcher_remove_view(view_dispatcher, InfraredViewPopup);
popup_free(infrared->popup);
view_dispatcher_remove_view(view_dispatcher, InfraredViewStack);
view_stack_free(infrared->view_stack);
if(app_state->is_debug_enabled) {
view_dispatcher_remove_view(view_dispatcher, InfraredViewDebugView);
infrared_debug_view_free(infrared->debug_view);
}
button_panel_free(infrared->button_panel);
loading_free(infrared->loading);
infrared_progress_view_free(infrared->progress);
view_dispatcher_free(view_dispatcher);
scene_manager_free(infrared->scene_manager);
infrared_brute_force_free(infrared->brute_force);
infrared_signal_free(infrared->received_signal);
infrared_remote_free(infrared->remote);
infrared_worker_free(infrared->worker);
furi_record_close(RECORD_NOTIFICATION);
infrared->notifications = NULL;
furi_record_close(RECORD_DIALOGS);
infrared->dialogs = NULL;
furi_record_close(RECORD_GUI);
infrared->gui = NULL;
string_clear(infrared->file_path);
free(infrared);
}
bool infrared_add_remote_with_button(
Infrared* infrared,
const char* button_name,
InfraredSignal* signal) {
InfraredRemote* remote = infrared->remote;
string_t new_name, new_path;
string_init_set_str(new_name, INFRARED_DEFAULT_REMOTE_NAME);
string_init_set_str(new_path, INFRARED_APP_FOLDER);
infrared_find_vacant_remote_name(new_name, string_get_cstr(new_path));
string_cat_printf(new_path, "/%s%s", string_get_cstr(new_name), INFRARED_APP_EXTENSION);
infrared_remote_reset(remote);
infrared_remote_set_name(remote, string_get_cstr(new_name));
infrared_remote_set_path(remote, string_get_cstr(new_path));
string_clear(new_name);
string_clear(new_path);
return infrared_remote_add_button(remote, button_name, signal);
}
bool infrared_rename_current_remote(Infrared* infrared, const char* name) {
InfraredRemote* remote = infrared->remote;
const char* remote_path = infrared_remote_get_path(remote);
if(!strcmp(infrared_remote_get_name(remote), name)) {
return true;
}
string_t new_name;
string_init_set_str(new_name, name);
infrared_find_vacant_remote_name(new_name, remote_path);
string_t new_path;
string_init_set(new_path, infrared_remote_get_path(remote));
if(string_end_with_str_p(new_path, INFRARED_APP_EXTENSION)) {
size_t filename_start = string_search_rchar(new_path, '/');
string_left(new_path, filename_start);
}
string_cat_printf(new_path, "/%s%s", string_get_cstr(new_name), INFRARED_APP_EXTENSION);
Storage* storage = furi_record_open(RECORD_STORAGE);
FS_Error status = storage_common_rename(
storage, infrared_remote_get_path(remote), string_get_cstr(new_path));
infrared_remote_set_name(remote, string_get_cstr(new_name));
infrared_remote_set_path(remote, string_get_cstr(new_path));
string_clear(new_name);
string_clear(new_path);
furi_record_close(RECORD_STORAGE);
return (status == FSE_OK || status == FSE_EXIST);
}
void infrared_tx_start_signal(Infrared* infrared, InfraredSignal* signal) {
if(infrared->app_state.is_transmitting) {
FURI_LOG_D(INFRARED_LOG_TAG, "Transmitter is already active");
return;
} else {
infrared->app_state.is_transmitting = true;
}
if(infrared_signal_is_raw(signal)) {
InfraredRawSignal* raw = infrared_signal_get_raw_signal(signal);
infrared_worker_set_raw_signal(infrared->worker, raw->timings, raw->timings_size);
} else {
InfraredMessage* message = infrared_signal_get_message(signal);
infrared_worker_set_decoded_signal(infrared->worker, message);
}
DOLPHIN_DEED(DolphinDeedIrSend);
infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStartSend);
infrared_worker_tx_set_get_signal_callback(
infrared->worker, infrared_worker_tx_get_signal_steady_callback, infrared);
infrared_worker_tx_start(infrared->worker);
}
void infrared_tx_start_button_index(Infrared* infrared, size_t button_index) {
furi_assert(button_index < infrared_remote_get_button_count(infrared->remote));
InfraredRemoteButton* button = infrared_remote_get_button(infrared->remote, button_index);
InfraredSignal* signal = infrared_remote_button_get_signal(button);
infrared_tx_start_signal(infrared, signal);
infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStartSend);
}
void infrared_tx_start_received(Infrared* infrared) {
infrared_tx_start_signal(infrared, infrared->received_signal);
infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStartSend);
}
void infrared_tx_stop(Infrared* infrared) {
if(!infrared->app_state.is_transmitting) {
FURI_LOG_D(INFRARED_LOG_TAG, "Transmitter is already stopped");
return;
} else {
infrared->app_state.is_transmitting = false;
}
infrared_worker_tx_stop(infrared->worker);
infrared_worker_tx_set_get_signal_callback(infrared->worker, NULL, NULL);
infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStop);
}
void infrared_text_store_set(Infrared* infrared, uint32_t bank, const char* text, ...) {
va_list args;
va_start(args, text);
vsnprintf(infrared->text_store[bank], INFRARED_TEXT_STORE_SIZE, text, args);
va_end(args);
}
void infrared_text_store_clear(Infrared* infrared, uint32_t bank) {
memset(infrared->text_store[bank], 0, INFRARED_TEXT_STORE_SIZE);
}
void infrared_play_notification_message(Infrared* infrared, uint32_t message) {
furi_assert(message < sizeof(infrared_notification_sequences) / sizeof(NotificationSequence*));
notification_message(infrared->notifications, infrared_notification_sequences[message]);
}
void infrared_show_loading_popup(Infrared* infrared, bool show) {
TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
ViewStack* view_stack = infrared->view_stack;
Loading* loading = infrared->loading;
if(show) {
// Raise timer priority so that animations can play
vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
view_stack_add_view(view_stack, loading_get_view(loading));
} else {
view_stack_remove_view(view_stack, loading_get_view(loading));
// Restore default timer priority
vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
}
}
void infrared_signal_received_callback(void* context, InfraredWorkerSignal* received_signal) {
furi_assert(context);
Infrared* infrared = context;
if(infrared_worker_signal_is_decoded(received_signal)) {
infrared_signal_set_message(
infrared->received_signal, infrared_worker_get_decoded_signal(received_signal));
} else {
const uint32_t* timings;
size_t timings_size;
infrared_worker_get_raw_signal(received_signal, &timings, &timings_size);
infrared_signal_set_raw_signal(
infrared->received_signal,
timings,
timings_size,
INFRARED_COMMON_CARRIER_FREQUENCY,
INFRARED_COMMON_DUTY_CYCLE);
}
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeSignalReceived);
}
void infrared_text_input_callback(void* context) {
furi_assert(context);
Infrared* infrared = context;
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeTextEditDone);
}
void infrared_popup_closed_callback(void* context) {
furi_assert(context);
Infrared* infrared = context;
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypePopupClosed);
}
int32_t infrared_app(void* p) {
Infrared* infrared = infrared_alloc();
infrared_make_app_folder(infrared);
bool is_remote_loaded = false;
bool is_rpc_mode = false;
if(p && strlen(p)) {
uint32_t rpc_ctx = 0;
if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
infrared->rpc_ctx = (void*)rpc_ctx;
rpc_system_app_set_callback(
infrared->rpc_ctx, infrared_rpc_command_callback, infrared);
rpc_system_app_send_started(infrared->rpc_ctx);
is_rpc_mode = true;
} else {
string_set_str(infrared->file_path, (const char*)p);
is_remote_loaded = infrared_remote_load(infrared->remote, infrared->file_path);
if(!is_remote_loaded) {
dialog_message_show_storage_error(
infrared->dialogs, "Failed to load\nselected remote");
return -1;
}
}
}
if(is_rpc_mode) {
view_dispatcher_attach_to_gui(
infrared->view_dispatcher, infrared->gui, ViewDispatcherTypeDesktop);
scene_manager_next_scene(infrared->scene_manager, InfraredSceneRpc);
} else {
view_dispatcher_attach_to_gui(
infrared->view_dispatcher, infrared->gui, ViewDispatcherTypeFullscreen);
if(is_remote_loaded) {
scene_manager_next_scene(infrared->scene_manager, InfraredSceneRemote);
} else {
scene_manager_next_scene(infrared->scene_manager, InfraredSceneStart);
}
}
view_dispatcher_run(infrared->view_dispatcher);
infrared_free(infrared);
return 0;
}

View File

@@ -0,0 +1,3 @@
#pragma once
typedef struct Infrared Infrared;

View File

@@ -0,0 +1,155 @@
#include "infrared_brute_force.h"
#include <stdlib.h>
#include <m-dict.h>
#include <m-string.h>
#include <flipper_format/flipper_format.h>
#include "infrared_signal.h"
typedef struct {
uint32_t index;
uint32_t count;
} InfraredBruteForceRecord;
DICT_DEF2(
InfraredBruteForceRecordDict,
string_t,
STRING_OPLIST,
InfraredBruteForceRecord,
M_POD_OPLIST);
struct InfraredBruteForce {
FlipperFormat* ff;
const char* db_filename;
string_t current_record_name;
InfraredBruteForceRecordDict_t records;
};
InfraredBruteForce* infrared_brute_force_alloc() {
InfraredBruteForce* brute_force = malloc(sizeof(InfraredBruteForce));
brute_force->ff = NULL;
brute_force->db_filename = NULL;
string_init(brute_force->current_record_name);
InfraredBruteForceRecordDict_init(brute_force->records);
return brute_force;
}
void infrared_brute_force_free(InfraredBruteForce* brute_force) {
furi_assert(!brute_force->ff);
InfraredBruteForceRecordDict_clear(brute_force->records);
string_clear(brute_force->current_record_name);
free(brute_force);
}
void infrared_brute_force_set_db_filename(InfraredBruteForce* brute_force, const char* db_filename) {
brute_force->db_filename = db_filename;
}
bool infrared_brute_force_calculate_messages(InfraredBruteForce* brute_force) {
furi_assert(brute_force->db_filename);
bool success = false;
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
success = flipper_format_buffered_file_open_existing(ff, brute_force->db_filename);
if(success) {
string_t signal_name;
string_init(signal_name);
while(flipper_format_read_string(ff, "name", signal_name)) {
InfraredBruteForceRecord* record =
InfraredBruteForceRecordDict_get(brute_force->records, signal_name);
if(record) {
++(record->count);
}
}
string_clear(signal_name);
}
flipper_format_free(ff);
furi_record_close(RECORD_STORAGE);
return success;
}
bool infrared_brute_force_start(
InfraredBruteForce* brute_force,
uint32_t index,
uint32_t* record_count) {
bool success = false;
*record_count = 0;
InfraredBruteForceRecordDict_it_t it;
for(InfraredBruteForceRecordDict_it(it, brute_force->records);
!InfraredBruteForceRecordDict_end_p(it);
InfraredBruteForceRecordDict_next(it)) {
const InfraredBruteForceRecordDict_itref_t* record = InfraredBruteForceRecordDict_cref(it);
if(record->value.index == index) {
*record_count = record->value.count;
if(*record_count) {
string_set(brute_force->current_record_name, record->key);
}
break;
}
}
if(*record_count) {
Storage* storage = furi_record_open(RECORD_STORAGE);
brute_force->ff = flipper_format_buffered_file_alloc(storage);
success =
flipper_format_buffered_file_open_existing(brute_force->ff, brute_force->db_filename);
if(!success) {
flipper_format_free(brute_force->ff);
brute_force->ff = NULL;
furi_record_close(RECORD_STORAGE);
}
}
return success;
}
bool infrared_brute_force_is_started(InfraredBruteForce* brute_force) {
return brute_force->ff;
}
void infrared_brute_force_stop(InfraredBruteForce* brute_force) {
furi_assert(string_size(brute_force->current_record_name));
furi_assert(brute_force->ff);
string_reset(brute_force->current_record_name);
flipper_format_free(brute_force->ff);
furi_record_close(RECORD_STORAGE);
brute_force->ff = NULL;
}
bool infrared_brute_force_send_next(InfraredBruteForce* brute_force) {
furi_assert(string_size(brute_force->current_record_name));
furi_assert(brute_force->ff);
bool success = false;
string_t signal_name;
string_init(signal_name);
InfraredSignal* signal = infrared_signal_alloc();
do {
success = infrared_signal_read(signal, brute_force->ff, signal_name);
} while(success && !string_equal_p(brute_force->current_record_name, signal_name));
if(success) {
infrared_signal_transmit(signal);
}
infrared_signal_free(signal);
string_clear(signal_name);
return success;
}
void infrared_brute_force_add_record(
InfraredBruteForce* brute_force,
uint32_t index,
const char* name) {
InfraredBruteForceRecord value = {.index = index, .count = 0};
string_t key;
string_init_set_str(key, name);
InfraredBruteForceRecordDict_set_at(brute_force->records, key, value);
string_clear(key);
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
typedef struct InfraredBruteForce InfraredBruteForce;
InfraredBruteForce* infrared_brute_force_alloc();
void infrared_brute_force_free(InfraredBruteForce* brute_force);
void infrared_brute_force_set_db_filename(InfraredBruteForce* brute_force, const char* db_filename);
bool infrared_brute_force_calculate_messages(InfraredBruteForce* brute_force);
bool infrared_brute_force_start(
InfraredBruteForce* brute_force,
uint32_t index,
uint32_t* record_count);
bool infrared_brute_force_is_started(InfraredBruteForce* brute_force);
void infrared_brute_force_stop(InfraredBruteForce* brute_force);
bool infrared_brute_force_send_next(InfraredBruteForce* brute_force);
void infrared_brute_force_add_record(
InfraredBruteForce* brute_force,
uint32_t index,
const char* name);

View File

@@ -0,0 +1,359 @@
#include <m-string.h>
#include <cli/cli.h>
#include <infrared.h>
#include <infrared_worker.h>
#include <furi_hal_infrared.h>
#include <flipper_format.h>
#include <toolbox/args.h>
#include "infrared_signal.h"
#define INFRARED_CLI_BUF_SIZE 10
static void infrared_cli_start_ir_rx(Cli* cli, string_t args);
static void infrared_cli_start_ir_tx(Cli* cli, string_t args);
static void infrared_cli_process_decode(Cli* cli, string_t args);
static const struct {
const char* cmd;
void (*process_function)(Cli* cli, string_t args);
} infrared_cli_commands[] = {
{.cmd = "rx", .process_function = infrared_cli_start_ir_rx},
{.cmd = "tx", .process_function = infrared_cli_start_ir_tx},
{.cmd = "decode", .process_function = infrared_cli_process_decode},
};
static void signal_received_callback(void* context, InfraredWorkerSignal* received_signal) {
furi_assert(received_signal);
char buf[100];
size_t buf_cnt;
Cli* cli = (Cli*)context;
if(infrared_worker_signal_is_decoded(received_signal)) {
const InfraredMessage* message = infrared_worker_get_decoded_signal(received_signal);
buf_cnt = snprintf(
buf,
sizeof(buf),
"%s, A:0x%0*lX, C:0x%0*lX%s\r\n",
infrared_get_protocol_name(message->protocol),
ROUND_UP_TO(infrared_get_protocol_address_length(message->protocol), 4),
message->address,
ROUND_UP_TO(infrared_get_protocol_command_length(message->protocol), 4),
message->command,
message->repeat ? " R" : "");
cli_write(cli, (uint8_t*)buf, buf_cnt);
} else {
const uint32_t* timings;
size_t timings_cnt;
infrared_worker_get_raw_signal(received_signal, &timings, &timings_cnt);
buf_cnt = snprintf(buf, sizeof(buf), "RAW, %d samples:\r\n", timings_cnt);
cli_write(cli, (uint8_t*)buf, buf_cnt);
for(size_t i = 0; i < timings_cnt; ++i) {
buf_cnt = snprintf(buf, sizeof(buf), "%lu ", timings[i]);
cli_write(cli, (uint8_t*)buf, buf_cnt);
}
buf_cnt = snprintf(buf, sizeof(buf), "\r\n");
cli_write(cli, (uint8_t*)buf, buf_cnt);
}
}
static void infrared_cli_start_ir_rx(Cli* cli, string_t args) {
UNUSED(cli);
UNUSED(args);
InfraredWorker* worker = infrared_worker_alloc();
infrared_worker_rx_start(worker);
infrared_worker_rx_set_received_signal_callback(worker, signal_received_callback, cli);
printf("Receiving INFRARED...\r\nPress Ctrl+C to abort\r\n");
while(!cli_cmd_interrupt_received(cli)) {
furi_delay_ms(50);
}
infrared_worker_rx_stop(worker);
infrared_worker_free(worker);
}
static void infrared_cli_print_usage(void) {
printf("Usage:\r\n");
printf("\tir rx\r\n");
printf("\tir tx <protocol> <address> <command>\r\n");
printf("\t<command> and <address> are hex-formatted\r\n");
printf("\tAvailable protocols:");
for(int i = 0; infrared_is_protocol_valid((InfraredProtocol)i); ++i) {
printf(" %s", infrared_get_protocol_name((InfraredProtocol)i));
}
printf("\r\n");
printf("\tRaw format:\r\n");
printf("\tir tx RAW F:<frequency> DC:<duty_cycle> <sample0> <sample1>...\r\n");
printf(
"\tFrequency (%d - %d), Duty cycle (0 - 100), max 512 samples\r\n",
INFRARED_MIN_FREQUENCY,
INFRARED_MAX_FREQUENCY);
printf("\tir decode <input_file> [<output_file>]\r\n");
}
static bool infrared_cli_parse_message(const char* str, InfraredSignal* signal) {
char protocol_name[32];
InfraredMessage message;
int parsed = sscanf(str, "%31s %lX %lX", protocol_name, &message.address, &message.command);
if(parsed != 3) {
return false;
}
message.protocol = infrared_get_protocol_by_name(protocol_name);
message.repeat = false;
infrared_signal_set_message(signal, &message);
return infrared_signal_is_valid(signal);
}
static bool infrared_cli_parse_raw(const char* str, InfraredSignal* signal) {
char frequency_str[INFRARED_CLI_BUF_SIZE];
char duty_cycle_str[INFRARED_CLI_BUF_SIZE];
int parsed = sscanf(str, "RAW F:%9s DC:%9s", frequency_str, duty_cycle_str);
if(parsed != 2) {
return false;
}
uint32_t* timings = malloc(sizeof(uint32_t) * MAX_TIMINGS_AMOUNT);
uint32_t frequency = atoi(frequency_str);
float duty_cycle = (float)atoi(duty_cycle_str) / 100;
str += strlen(frequency_str) + strlen(duty_cycle_str) + INFRARED_CLI_BUF_SIZE;
size_t timings_size = 0;
while(1) {
while(*str == ' ') {
++str;
}
char timing_str[INFRARED_CLI_BUF_SIZE];
if(sscanf(str, "%9s", timing_str) != 1) {
break;
}
str += strlen(timing_str);
uint32_t timing = atoi(timing_str);
if((timing <= 0) || (timings_size >= MAX_TIMINGS_AMOUNT)) {
break;
}
timings[timings_size] = timing;
++timings_size;
}
infrared_signal_set_raw_signal(signal, timings, timings_size, frequency, duty_cycle);
free(timings);
return infrared_signal_is_valid(signal);
}
static void infrared_cli_start_ir_tx(Cli* cli, string_t args) {
UNUSED(cli);
const char* str = string_get_cstr(args);
InfraredSignal* signal = infrared_signal_alloc();
bool success = infrared_cli_parse_message(str, signal) || infrared_cli_parse_raw(str, signal);
if(success) {
infrared_signal_transmit(signal);
} else {
printf("Wrong arguments.\r\n");
infrared_cli_print_usage();
}
infrared_signal_free(signal);
}
static bool
infrared_cli_save_signal(InfraredSignal* signal, FlipperFormat* file, const char* name) {
bool ret = infrared_signal_save(signal, file, name);
if(!ret) {
printf("Failed to save signal: \"%s\"\r\n", name);
}
return ret;
}
static bool infrared_cli_decode_raw_signal(
InfraredRawSignal* raw_signal,
InfraredDecoderHandler* decoder,
FlipperFormat* output_file,
const char* signal_name) {
InfraredSignal* signal = infrared_signal_alloc();
bool ret = false, level = true, is_decoded = false;
size_t i;
for(i = 0; i < raw_signal->timings_size; ++i) {
// TODO: Any infrared_check_decoder_ready() magic?
const InfraredMessage* message = infrared_decode(decoder, level, raw_signal->timings[i]);
if(message) {
is_decoded = true;
printf(
"Protocol: %s address: 0x%lX command: 0x%lX %s\r\n",
infrared_get_protocol_name(message->protocol),
message->address,
message->command,
(message->repeat ? "R" : ""));
if(output_file && !message->repeat) {
infrared_signal_set_message(signal, message);
if(!infrared_cli_save_signal(signal, output_file, signal_name)) break;
}
}
level = !level;
}
if(i == raw_signal->timings_size) {
if(!is_decoded && output_file) {
infrared_signal_set_raw_signal(
signal,
raw_signal->timings,
raw_signal->timings_size,
raw_signal->frequency,
raw_signal->duty_cycle);
ret = infrared_cli_save_signal(signal, output_file, signal_name);
} else {
ret = true;
}
}
infrared_reset_decoder(decoder);
infrared_signal_free(signal);
return ret;
}
static bool infrared_cli_decode_file(FlipperFormat* input_file, FlipperFormat* output_file) {
bool ret = false;
InfraredSignal* signal = infrared_signal_alloc();
InfraredDecoderHandler* decoder = infrared_alloc_decoder();
string_t tmp;
string_init(tmp);
while(infrared_signal_read(signal, input_file, tmp)) {
ret = false;
if(!infrared_signal_is_valid(signal)) {
printf("Invalid signal\r\n");
break;
}
if(!infrared_signal_is_raw(signal)) {
if(output_file &&
!infrared_cli_save_signal(signal, output_file, string_get_cstr(tmp))) {
break;
} else {
printf("Skipping decoded signal\r\n");
continue;
}
}
InfraredRawSignal* raw_signal = infrared_signal_get_raw_signal(signal);
printf("Raw signal: %s, %u samples\r\n", string_get_cstr(tmp), raw_signal->timings_size);
if(!infrared_cli_decode_raw_signal(raw_signal, decoder, output_file, string_get_cstr(tmp)))
break;
ret = true;
}
infrared_free_decoder(decoder);
infrared_signal_free(signal);
string_clear(tmp);
return ret;
}
static void infrared_cli_process_decode(Cli* cli, string_t args) {
UNUSED(cli);
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* input_file = flipper_format_buffered_file_alloc(storage);
FlipperFormat* output_file = NULL;
uint32_t version;
string_t tmp, header, input_path, output_path;
string_init(tmp);
string_init(header);
string_init(input_path);
string_init(output_path);
do {
if(!args_read_probably_quoted_string_and_trim(args, input_path)) {
printf("Wrong arguments.\r\n");
infrared_cli_print_usage();
break;
}
args_read_probably_quoted_string_and_trim(args, output_path);
if(!flipper_format_buffered_file_open_existing(input_file, string_get_cstr(input_path))) {
printf("Failed to open file for reading: \"%s\"\r\n", string_get_cstr(input_path));
break;
}
if(!flipper_format_read_header(input_file, header, &version) ||
(!string_start_with_str_p(header, "IR")) || version != 1) {
printf("Invalid or corrupted input file: \"%s\"\r\n", string_get_cstr(input_path));
break;
}
if(!string_empty_p(output_path)) {
printf("Writing output to file: \"%s\"\r\n", string_get_cstr(output_path));
output_file = flipper_format_file_alloc(storage);
}
if(output_file &&
!flipper_format_file_open_always(output_file, string_get_cstr(output_path))) {
printf("Failed to open file for writing: \"%s\"\r\n", string_get_cstr(output_path));
break;
}
if(output_file && !flipper_format_write_header(output_file, header, version)) {
printf("Failed to write to the output file: \"%s\"\r\n", string_get_cstr(output_path));
break;
}
if(!infrared_cli_decode_file(input_file, output_file)) {
break;
}
printf("File successfully decoded.\r\n");
} while(false);
string_clear(tmp);
string_clear(header);
string_clear(input_path);
string_clear(output_path);
flipper_format_free(input_file);
if(output_file) flipper_format_free(output_file);
furi_record_close(RECORD_STORAGE);
}
static void infrared_cli_start_ir(Cli* cli, string_t args, void* context) {
UNUSED(context);
if(furi_hal_infrared_is_busy()) {
printf("INFRARED is busy. Exiting.");
return;
}
string_t command;
string_init(command);
args_read_string_and_trim(args, command);
size_t i = 0;
for(; i < COUNT_OF(infrared_cli_commands); ++i) {
size_t cmd_len = strlen(infrared_cli_commands[i].cmd);
if(!strncmp(string_get_cstr(command), infrared_cli_commands[i].cmd, cmd_len)) {
break;
}
}
if(i < COUNT_OF(infrared_cli_commands)) {
infrared_cli_commands[i].process_function(cli, args);
} else {
infrared_cli_print_usage();
}
string_clear(command);
}
void infrared_on_system_start() {
#ifdef SRV_CLI
Cli* cli = (Cli*)furi_record_open(RECORD_CLI);
cli_add_command(cli, "ir", CliCommandFlagDefault, infrared_cli_start_ir, NULL);
furi_record_close(RECORD_CLI);
#else
UNUSED(infrared_cli_start_ir);
#endif
}

View File

@@ -0,0 +1,57 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
enum InfraredCustomEventType {
// Reserve first 100 events for button types and indexes, starting from 0
InfraredCustomEventTypeReserved = 100,
InfraredCustomEventTypeMenuSelected,
InfraredCustomEventTypeTransmitStarted,
InfraredCustomEventTypeTransmitStopped,
InfraredCustomEventTypeSignalReceived,
InfraredCustomEventTypeTextEditDone,
InfraredCustomEventTypePopupClosed,
InfraredCustomEventTypeButtonSelected,
InfraredCustomEventTypeBackPressed,
InfraredCustomEventTypeRpcLoad,
InfraredCustomEventTypeRpcExit,
InfraredCustomEventTypeRpcButtonPress,
InfraredCustomEventTypeRpcButtonRelease,
InfraredCustomEventTypeRpcSessionClose,
};
#pragma pack(push, 1)
typedef union {
uint32_t packed_value;
struct {
uint16_t type;
int16_t value;
} content;
} InfraredCustomEvent;
#pragma pack(pop)
static inline uint32_t infrared_custom_event_pack(uint16_t type, int16_t value) {
InfraredCustomEvent event = {.content = {.type = type, .value = value}};
return event.packed_value;
}
static inline void
infrared_custom_event_unpack(uint32_t packed_value, uint16_t* type, int16_t* value) {
InfraredCustomEvent event = {.packed_value = packed_value};
if(type) *type = event.content.type;
if(value) *value = event.content.value;
}
static inline uint16_t infrared_custom_event_get_type(uint32_t packed_value) {
uint16_t type;
infrared_custom_event_unpack(packed_value, &type, NULL);
return type;
}
static inline int16_t infrared_custom_event_get_value(uint32_t packed_value) {
int16_t value;
infrared_custom_event_unpack(packed_value, NULL, &value);
return value;
}

Some files were not shown because too many files have changed in this diff Show More