Skip to content

Commit

Permalink
fix: Freezes when reading log files
Browse files Browse the repository at this point in the history
  • Loading branch information
Otrebor671 committed Aug 26, 2024
1 parent b033a77 commit be34b64
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 24 deletions.
7 changes: 2 additions & 5 deletions ModbusApp/Modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Uart* Uart_Alloc(void* context) {
uart->cfg = Config_Alloc();
uart->rxStream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
uart->rxThread = furi_thread_alloc_ex("RxThread", 1024, uart_worker, app);

uart->serial_handle = NULL;
// serial_init(uart, UART_CH);
furi_thread_start(uart->rxThread);

Expand Down Expand Up @@ -147,10 +147,7 @@ void uartFree(void* context) {
furi_thread_flags_set(furi_thread_get_id(app->uart->rxThread), WorkerEvtStop);
furi_thread_join(app->uart->rxThread);
furi_thread_free(app->uart->rxThread);
app->LOGfileReady = false;
if(app->LOGfile && storage_file_is_open(app->LOGfile)) {
storage_file_close(app->LOGfile);
}
close_log_file_stream(app);
free(app->uart->cfg);
free(app->uart);
free(app->modbus);
Expand Down
22 changes: 21 additions & 1 deletion ModbusApp/modbus_storage/modbus_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ void makePaths(App* app) {
dialog_message_show_storage_error(app->dialogs, "Cannot create\nlogs folder");
}
}
void open_log_file_stream(void* context) {
App* app = context;
strcpy(app->logFilePath, sequential_file_resolve_path(app->storage, PATHLOGS, "Log", "log"));
if(app->logFilePath != NULL) {
if(storage_file_open(app->LOGfile, app->logFilePath, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
furi_string_reset(app->text);
app->LOGfileReady = true;
} else {
dialog_message_show_storage_error(app->dialogs, "Cannot open log file");
}
} else {
dialog_message_show_storage_error(app->dialogs, "Cannot resolve log path");
}
}
void close_log_file_stream(void* context) {
App* app = context;
if(app->LOGfile && storage_file_is_open(app->LOGfile)) {
app->LOGfileReady = false;
storage_file_close(app->LOGfile);
}
}
bool OpenLogFile(App* app) {
// browse for files
FuriString* predefined_filepath = furi_string_alloc_set_str(PATHLOGS);
Expand All @@ -45,7 +66,6 @@ bool OpenLogFile(App* app) {
}
if(storage_file_open(
app->LOGfile, furi_string_get_cstr(selected_filepath), FSAM_READ, FSOM_OPEN_EXISTING)) {
app->uart->cfg->saveLOG = false;
furi_string_reset(app->text);
char buf[storage_file_size(app->LOGfile)];
storage_file_read(app->LOGfile, buf, sizeof(buf));
Expand Down
2 changes: 2 additions & 0 deletions ModbusApp/modbus_storage/modbus_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ char* sequential_file_resolve_path(
const char* extension);
bool OpenLogFile(App* app);
void makePaths(App* app);
void open_log_file_stream(void* context);
void close_log_file_stream(void* context);
5 changes: 3 additions & 2 deletions ModbusApp/modbus_uart/modbus_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ void serial_init(Uart* uart, uint8_t uart_ch) {
furi_hal_serial_dma_rx_start(uart->serial_handle, on_rx_cb, uart, false);
}
void serial_deinit(Uart* uart) {
furi_assert(uart->serial_handle);

if(uart->serial_handle == NULL) {
return;
}
furi_hal_serial_dma_rx_stop(uart->serial_handle);
furi_hal_serial_deinit(uart->serial_handle);
furi_hal_serial_control_release(uart->serial_handle);
Expand Down
1 change: 1 addition & 0 deletions ModbusApp/scenes/main_scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void mainOptionsCB(void* context, uint32_t index) {
break;
case Read_LOG_Option:
scene_manager_set_scene_state(app->sceneManager, app_scene_main, Read_LOG_Option);
close_log_file_stream(app);
if(OpenLogFile(app)) {
scene_manager_set_scene_state(app->sceneManager, app_scene_sniffer, Read_LOG_Option);
scene_manager_next_scene(app->sceneManager, app_scene_sniffer);
Expand Down
14 changes: 0 additions & 14 deletions ModbusApp/scenes/settings_scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,6 @@ bool app_scene_settings_on_event(void* context, SceneManagerEvent event) {
}
void app_scene_settings_on_exit(void* context) {
App* app = context;
if(app->uart->cfg->saveLOG) {
strcpy(
app->logFilePath, sequential_file_resolve_path(app->storage, PATHLOGS, "Log", "log"));
if(app->logFilePath != NULL) {
if(storage_file_open(app->LOGfile, app->logFilePath, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
furi_string_reset(app->text);
app->LOGfileReady = true;
} else {
dialog_message_show_storage_error(app->dialogs, "Cannot open log file");
}
} else {
dialog_message_show_storage_error(app->dialogs, "Cannot resolve log path");
}
}
uart_set_config(app);
variable_item_list_reset(app->varList);
}
10 changes: 8 additions & 2 deletions ModbusApp/scenes/sniffer_scene.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include "../Modbus.h"
#include "../modbus_uart/modbus_uart.h"
#include "../modbus_storage/modbus_storage.h"

////////////////////////// Sniffer Scene //////////////////////////
static void sniffer_begin(App* app) {
serial_init(app->uart, UART_CH);
if(app->uart->cfg->saveLOG && !storage_file_is_open(app->LOGfile)) {
open_log_file_stream(app);
}
}
void app_scene_sniffer_on_enter(void* context) {
App* app = context;
serial_init(app->uart, UART_CH);
if(scene_manager_get_scene_state(app->sceneManager, app_scene_sniffer) == Sniffer_Option ||
scene_manager_get_scene_state(app->sceneManager, app_scene_sniffer) == Sender_Option) {
sniffer_begin(app);
text_box_set_font(app->textBox, TextBoxFontText);
text_box_set_focus(app->textBox, TextBoxFocusEnd);
furi_string_cat_printf(
Expand Down

0 comments on commit be34b64

Please sign in to comment.