Skip to content

Commit

Permalink
Added Selector for HTML Portals
Browse files Browse the repository at this point in the history
  • Loading branch information
David Lee committed Jul 28, 2023
1 parent 42b7ba9 commit 4d88320
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 3 deletions.
3 changes: 2 additions & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ App(
requires=["gui"],
stack_size=1 * 1024,
order=90,
fap_version=1.0,
fap_author="bigbrodude6119",
fap_description="Create an evil captive portal Wi-Fi access point",
fap_libs=["assets"],
fap_icon_assets="icons",
fap_icon="icons/evil_portal_10px.png",
fap_category="GPIO",
)
14 changes: 13 additions & 1 deletion evil_portal_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ Evil_PortalApp *evil_portal_app_alloc() {
app->command_index = 0;
app->portal_logs = furi_string_alloc();


app->dialogs = furi_record_open(RECORD_DIALOGS);
app->file_path = furi_string_alloc();

app->gui = furi_record_open(RECORD_GUI);

app->view_dispatcher = view_dispatcher_alloc();

app->loading = loading_alloc();

app->scene_manager = scene_manager_alloc(&evil_portal_scene_handlers, app);
view_dispatcher_enable_queue(app->view_dispatcher);
Expand All @@ -52,6 +56,8 @@ Evil_PortalApp *evil_portal_app_alloc() {
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui,
ViewDispatcherTypeFullscreen);

app->view_stack = view_stack_alloc();

app->var_item_list = variable_item_list_alloc();
view_dispatcher_add_view(app->view_dispatcher, Evil_PortalAppViewVarItemList,
variable_item_list_get_view(app->var_item_list));
Expand Down Expand Up @@ -100,6 +106,9 @@ void evil_portal_app_free(Evil_PortalApp *app) {
furi_string_free(app->text_box_store);
text_input_free(app->text_input);

view_stack_free(app->view_stack);
loading_free(app->loading);

// View dispatcher
view_dispatcher_free(app->view_dispatcher);
scene_manager_free(app->scene_manager);
Expand All @@ -109,6 +118,9 @@ void evil_portal_app_free(Evil_PortalApp *app) {
// Close records
furi_record_close(RECORD_GUI);

furi_record_close(RECORD_DIALOGS);
furi_string_free(app->file_path);

free(app);
}

Expand Down
13 changes: 12 additions & 1 deletion evil_portal_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
#include "evil_portal_custom_event.h"
#include "evil_portal_uart.h"
#include "scenes/evil_portal_scene.h"
#include "evil_portal_icons.h"

#include <gui/gui.h>
#include <gui/modules/loading.h>
#include <gui/modules/text_box.h>
#include <gui/modules/text_input.h>
#include <gui/modules/variable_item_list.h>
#include <gui/scene_manager.h>
#include <gui/view_dispatcher.h>
#include <gui/view_stack.h>
#include <dialogs/dialogs.h>

#define NUM_MENU_ITEMS (5)
#define NUM_MENU_ITEMS (6)

#define EVIL_PORTAL_TEXT_BOX_STORE_SIZE (4096)
#define UART_CH (FuriHalUartIdUSART1)
Expand All @@ -21,6 +25,9 @@
#define SET_AP_CMD "setap"
#define RESET_CMD "reset"

#define HTML_EXTENSION ".html"
#define HTML_FOLDER ANY_PATH("apps_data/evil_portal/html")

struct Evil_PortalApp {
Gui *gui;
ViewDispatcher *view_dispatcher;
Expand All @@ -38,6 +45,10 @@ struct Evil_PortalApp {
VariableItemList *var_item_list;
Evil_PortalUart *uart;
TextInput* text_input;
DialogsApp* dialogs;
FuriString* file_path;
Loading* loading;
ViewStack* view_stack;

int selected_menu_index;
int selected_option_index[NUM_MENU_ITEMS];
Expand Down
27 changes: 27 additions & 0 deletions helpers/evil_portal_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,33 @@ void evil_portal_read_index_html(void *context) {
evil_portal_close_storage();
}

void evil_portal_replace_index_html(FuriString* path) {
Storage *storage = evil_portal_open_storage();
FS_Error error;
error = storage_common_remove(storage, EVIL_PORTAL_INDEX_SAVE_PATH);
if(error != FSE_OK) {
FURI_LOG_D("EVIL PORTAL", "Error removing file");
} else {
FURI_LOG_D("EVIL PORTAL", "Error removed file");
}
error = storage_common_copy(storage, furi_string_get_cstr(path), EVIL_PORTAL_INDEX_SAVE_PATH);
if(error != FSE_OK) {
FURI_LOG_D("EVIL PORTAL", "Error copying file");
}
evil_portal_close_storage();
}

void evil_portal_create_html_folder_if_not_exists() {
Storage *storage = evil_portal_open_storage();
if(storage_common_stat(storage, HTML_FOLDER, NULL) == FSE_NOT_EXIST) {
FURI_LOG_D("Evil Portal", "Directory %s doesn't exist. Will create new.", HTML_FOLDER);
if(!storage_simply_mkdir(storage, HTML_FOLDER)) {
FURI_LOG_E("Evil Portal", "Error creating directory %s", HTML_FOLDER);
}
}
evil_portal_close_storage();
}

void evil_portal_read_ap_name(void *context) {
Evil_PortalApp *app = context;
Storage *storage = evil_portal_open_storage();
Expand Down
2 changes: 2 additions & 0 deletions helpers/evil_portal_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
void evil_portal_read_index_html(void *context);
void evil_portal_read_ap_name(void *context);
void evil_portal_write_ap_name(void *context);
void evil_portal_replace_index_html(FuriString* path);
void evil_portal_create_html_folder_if_not_exists();
void write_logs(FuriString* portal_logs);
char *sequential_file_resolve_path(Storage *storage, const char *dir,
const char *prefix, const char *extension);
1 change: 1 addition & 0 deletions scenes/evil_portal_scene_config.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ADD_SCENE(evil_portal, start, Start)
ADD_SCENE(evil_portal, console_output, ConsoleOutput)
ADD_SCENE(evil_portal, rename, Rename)
ADD_SCENE(evil_portal, select_html, SelectHtml)
6 changes: 6 additions & 0 deletions scenes/evil_portal_scene_console_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ void evil_portal_scene_console_output_on_enter(void *context) {
return;
}

if (0 == strncmp("selecthtml", app->selected_tx_string, strlen("selecthtml"))) {
scene_manager_next_scene(app->scene_manager,
Evil_PortalSceneSelectHtml);
return;
}

if (0 ==
strncmp(SET_HTML_CMD, app->selected_tx_string, strlen(SET_HTML_CMD))) {
app->command_queue[0] = SET_AP_CMD;
Expand Down
62 changes: 62 additions & 0 deletions scenes/evil_portal_scene_select_html.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "../evil_portal_app_i.h"
#include "../helpers/evil_portal_storage.h"

void evil_portal_show_loading_popup(Evil_PortalApp* app, bool show) {
TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
ViewStack* view_stack = app->view_stack;
Loading* loading = app->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 evil_portal_scene_select_html_on_enter(void *context) {
Evil_PortalApp *app = context;
DialogsFileBrowserOptions browser_options;
evil_portal_create_html_folder_if_not_exists();

dialog_file_browser_set_basic_options(&browser_options, HTML_EXTENSION, &I_evil_portal_10px);
browser_options.base_path = HTML_FOLDER;

FuriString* path;
path = furi_string_alloc();

furi_string_set(path, HTML_FOLDER);



bool success = dialog_file_browser_show(
app->dialogs, app->file_path, path, &browser_options);
furi_string_free(path);

if(success) {
//Replace HTML File
evil_portal_show_loading_popup(app, true);
evil_portal_replace_index_html(app->file_path);
evil_portal_show_loading_popup(app, false);
}

if(success) {
scene_manager_search_and_switch_to_previous_scene(
app->scene_manager, Evil_PortalSceneStart);
} else {
scene_manager_previous_scene(app->scene_manager);
}
}

bool evil_portal_scene_select_html_on_event(void *context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
bool consumed = true;
return consumed;
}

void evil_portal_scene_select_html_on_exit(void *context) {
UNUSED(context);
}
9 changes: 9 additions & 0 deletions scenes/evil_portal_scene_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ const Evil_PortalItem items[NUM_MENU_ITEMS] = {
FOCUS_CONSOLE_START,
SHOW_STOPSCAN_TIP},

// select HTML Portal File
{"Select HTML",
{""},
1,
{"selecthtml"},
NO_ARGS,
FOCUS_CONSOLE_START,
SHOW_STOPSCAN_TIP},

// help
{"Help",
{""},
Expand Down

0 comments on commit 4d88320

Please sign in to comment.