[FL-2433, FL-2408] Get rid of file_worker in archive and various fixes (#1105)

* Archive: get rid of file_worker and various fixes
* USB init moved to CLI service
This commit is contained in:
Nikolay Minaylov 2022-04-14 14:28:59 +03:00 committed by GitHub
parent fed18f7c42
commit 72a6bbb8ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 173 additions and 84 deletions

View File

@ -21,13 +21,23 @@ bool archive_app_is_available(void* context, const char* path) {
ArchiveAppTypeEnum app = archive_get_app_type(path); ArchiveAppTypeEnum app = archive_get_app_type(path);
if(app == ArchiveAppTypeU2f) { if(app == ArchiveAppTypeU2f) {
FileWorker* file_worker = file_worker_alloc(true);
bool file_exists = false; bool file_exists = false;
file_worker_is_file_exist(file_worker, "/any/u2f/key.u2f", &file_exists); Storage* fs_api = furi_record_open("storage");
File* file = storage_file_alloc(fs_api);
file_exists = storage_file_open(file, "/any/u2f/key.u2f", FSAM_READ, FSOM_OPEN_EXISTING);
if(file_exists) { if(file_exists) {
file_worker_is_file_exist(file_worker, "/any/u2f/cnt.u2f", &file_exists); storage_file_close(file);
file_exists =
storage_file_open(file, "/any/u2f/cnt.u2f", FSAM_READ, FSOM_OPEN_EXISTING);
if(file_exists) {
storage_file_close(file);
}
} }
file_worker_free(file_worker);
storage_file_free(file);
furi_record_close("storage");
return file_exists; return file_exists;
} else { } else {
return false; return false;
@ -60,10 +70,10 @@ void archive_app_delete_file(void* context, const char* path) {
bool res = false; bool res = false;
if(app == ArchiveAppTypeU2f) { if(app == ArchiveAppTypeU2f) {
FileWorker* file_worker = file_worker_alloc(true); Storage* fs_api = furi_record_open("storage");
res = file_worker_remove(file_worker, "/any/u2f/key.u2f"); res = (storage_common_remove(fs_api, "/any/u2f/key.u2f") == FSE_OK);
res |= file_worker_remove(file_worker, "/any/u2f/cnt.u2f"); res |= (storage_common_remove(fs_api, "/any/u2f/cnt.u2f") == FSE_OK);
file_worker_free(file_worker); furi_record_close("storage");
if(archive_is_favorite("/app:u2f/U2F Token")) { if(archive_is_favorite("/app:u2f/U2F Token")) {
archive_favorites_delete("/app:u2f/U2F Token"); archive_favorites_delete("/app:u2f/U2F Token");

View File

@ -392,6 +392,17 @@ void archive_enter_dir(ArchiveBrowserView* browser, string_t name) {
furi_assert(browser); furi_assert(browser);
furi_assert(name); furi_assert(name);
uint8_t browser_depth = 0;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
browser_depth = idx_last_array_size(model->idx_last);
return false;
});
if(browser_depth > BROWSER_DEPTH_MAX) {
return;
}
archive_dir_count_items(browser, string_get_cstr(name)); archive_dir_count_items(browser, string_get_cstr(name));
with_view_model( with_view_model(

View File

@ -4,6 +4,7 @@
#define TAB_RIGHT InputKeyRight //default tab swith direction #define TAB_RIGHT InputKeyRight //default tab swith direction
#define FILE_LIST_BUF_LEN 100 #define FILE_LIST_BUF_LEN 100
#define BROWSER_DEPTH_MAX 8
static const char* tab_default_paths[] = { static const char* tab_default_paths[] = {
[ArchiveTabFavorites] = "/any/favorites", [ArchiveTabFavorites] = "/any/favorites",

View File

@ -4,20 +4,63 @@
#include "archive_apps.h" #include "archive_apps.h"
#include "archive_browser.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) { uint16_t archive_favorites_count(void* context) {
furi_assert(context); furi_assert(context);
FileWorker* file_worker = file_worker_alloc(true); Storage* fs_api = furi_record_open("storage");
File* file = storage_file_alloc(fs_api);
string_t buffer; string_t buffer;
string_init(buffer); string_init(buffer);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING); bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
uint16_t lines = 0; uint16_t lines = 0;
if(result) { if(result) {
while(1) { while(1) {
if(!file_worker_read_until(file_worker, buffer, '\n')) { if(!archive_favorites_read_line(file, buffer)) {
break; break;
} }
if(!string_size(buffer)) { if(!string_size(buffer)) {
@ -27,21 +70,26 @@ uint16_t archive_favorites_count(void* context) {
} }
} }
storage_file_close(file);
string_clear(buffer); string_clear(buffer);
file_worker_close(file_worker); storage_file_free(file);
file_worker_free(file_worker); furi_record_close("storage");
return lines; return lines;
} }
static bool archive_favourites_rescan() { static bool archive_favourites_rescan() {
string_t buffer; string_t buffer;
string_init(buffer); string_init(buffer);
FileWorker* file_worker = file_worker_alloc(true); Storage* fs_api = furi_record_open("storage");
File* file = storage_file_alloc(fs_api);
File* fav_item_file = storage_file_alloc(fs_api);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING); bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) { if(result) {
while(1) { while(1) {
if(!file_worker_read_until(file_worker, buffer, '\n')) { if(!archive_favorites_read_line(file, buffer)) {
break; break;
} }
if(!string_size(buffer)) { if(!string_size(buffer)) {
@ -53,9 +101,10 @@ static bool archive_favourites_rescan() {
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer)); archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
} }
} else { } else {
bool file_exists = false; bool file_exists = storage_file_open(
file_worker_is_file_exist(file_worker, string_get_cstr(buffer), &file_exists); fav_item_file, string_get_cstr(buffer), FSAM_READ, FSOM_OPEN_EXISTING);
if(file_exists) { if(file_exists) {
storage_file_close(fav_item_file);
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer)); archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
} }
} }
@ -64,11 +113,13 @@ static bool archive_favourites_rescan() {
string_clear(buffer); string_clear(buffer);
file_worker_close(file_worker); storage_file_close(file);
file_worker_remove(file_worker, ARCHIVE_FAV_PATH); storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
file_worker_rename(file_worker, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH); storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
file_worker_free(file_worker); storage_file_free(file);
storage_file_free(fav_item_file);
furi_record_close("storage");
return result; return result;
} }
@ -77,7 +128,9 @@ bool archive_favorites_read(void* context) {
furi_assert(context); furi_assert(context);
ArchiveBrowserView* browser = context; ArchiveBrowserView* browser = context;
FileWorker* file_worker = file_worker_alloc(true); Storage* fs_api = furi_record_open("storage");
File* file = storage_file_alloc(fs_api);
File* fav_item_file = storage_file_alloc(fs_api);
string_t buffer; string_t buffer;
FileInfo file_info; FileInfo file_info;
@ -88,11 +141,11 @@ bool archive_favorites_read(void* context) {
archive_file_array_rm_all(browser); archive_file_array_rm_all(browser);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING); bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) { if(result) {
while(1) { while(1) {
if(!file_worker_read_until(file_worker, buffer, '\n')) { if(!archive_favorites_read_line(file, buffer)) {
break; break;
} }
if(!string_size(buffer)) { if(!string_size(buffer)) {
@ -107,10 +160,10 @@ bool archive_favorites_read(void* context) {
need_refresh = true; need_refresh = true;
} }
} else { } else {
bool file_exists = false; bool file_exists = storage_file_open(
file_worker_is_file_exist(file_worker, string_get_cstr(buffer), &file_exists); fav_item_file, string_get_cstr(buffer), FSAM_READ, FSOM_OPEN_EXISTING);
if(file_exists) { if(file_exists) {
storage_file_close(fav_item_file);
archive_add_file_item(browser, &file_info, string_get_cstr(buffer)); archive_add_file_item(browser, &file_info, string_get_cstr(buffer));
file_count++; file_count++;
} else { } else {
@ -121,9 +174,11 @@ bool archive_favorites_read(void* context) {
string_reset(buffer); string_reset(buffer);
} }
} }
storage_file_close(file);
string_clear(buffer); string_clear(buffer);
file_worker_close(file_worker); storage_file_free(file);
file_worker_free(file_worker); storage_file_free(fav_item_file);
furi_record_close("storage");
archive_set_item_count(browser, file_count); archive_set_item_count(browser, file_count);
@ -143,12 +198,14 @@ bool archive_favorites_delete(const char* format, ...) {
va_end(args); va_end(args);
string_init(buffer); string_init(buffer);
FileWorker* file_worker = file_worker_alloc(true); Storage* fs_api = furi_record_open("storage");
File* file = storage_file_alloc(fs_api);
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) { if(result) {
while(1) { while(1) {
if(!file_worker_read_until(file_worker, buffer, '\n')) { if(!archive_favorites_read_line(file, buffer)) {
break; break;
} }
if(!string_size(buffer)) { if(!string_size(buffer)) {
@ -164,11 +221,12 @@ bool archive_favorites_delete(const char* format, ...) {
string_clear(buffer); string_clear(buffer);
string_clear(filename); string_clear(filename);
file_worker_close(file_worker); storage_file_close(file);
file_worker_remove(file_worker, ARCHIVE_FAV_PATH); storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
file_worker_rename(file_worker, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH); storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
file_worker_free(file_worker); storage_file_free(file);
furi_record_close("storage");
return result; return result;
} }
@ -182,14 +240,15 @@ bool archive_is_favorite(const char* format, ...) {
va_end(args); va_end(args);
string_init(buffer); string_init(buffer);
FileWorker* file_worker = file_worker_alloc(true); Storage* fs_api = furi_record_open("storage");
File* file = storage_file_alloc(fs_api);
bool found = false; bool found = false;
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING); bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) { if(result) {
while(1) { while(1) {
if(!file_worker_read_until(file_worker, buffer, '\n')) { if(!archive_favorites_read_line(file, buffer)) {
break; break;
} }
if(!string_size(buffer)) { if(!string_size(buffer)) {
@ -202,10 +261,11 @@ bool archive_is_favorite(const char* format, ...) {
} }
} }
storage_file_close(file);
string_clear(buffer); string_clear(buffer);
string_clear(filename); string_clear(filename);
file_worker_close(file_worker); storage_file_free(file);
file_worker_free(file_worker); furi_record_close("storage");
return found; return found;
} }
@ -214,7 +274,8 @@ bool archive_favorites_rename(const char* src, const char* dst) {
furi_assert(src); furi_assert(src);
furi_assert(dst); furi_assert(dst);
FileWorker* file_worker = file_worker_alloc(true); Storage* fs_api = furi_record_open("storage");
File* file = storage_file_alloc(fs_api);
string_t path; string_t path;
string_t buffer; string_t buffer;
@ -223,11 +284,11 @@ bool archive_favorites_rename(const char* src, const char* dst) {
string_init(path); string_init(path);
string_printf(path, "%s", src); string_printf(path, "%s", src);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING); bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) { if(result) {
while(1) { while(1) {
if(!file_worker_read_until(file_worker, buffer, '\n')) { if(!archive_favorites_read_line(file, buffer)) {
break; break;
} }
if(!string_size(buffer)) { if(!string_size(buffer)) {
@ -244,11 +305,12 @@ bool archive_favorites_rename(const char* src, const char* dst) {
string_clear(buffer); string_clear(buffer);
string_clear(path); string_clear(path);
file_worker_close(file_worker); storage_file_close(file);
file_worker_remove(file_worker, ARCHIVE_FAV_PATH); storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
file_worker_rename(file_worker, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH); storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
file_worker_free(file_worker); storage_file_free(file);
furi_record_close("storage");
return result; return result;
} }
@ -263,15 +325,17 @@ void archive_favorites_save(void* context) {
furi_assert(context); furi_assert(context);
ArchiveBrowserView* browser = context; ArchiveBrowserView* browser = context;
FileWorker* file_worker = file_worker_alloc(true); Storage* fs_api = furi_record_open("storage");
File* file = storage_file_alloc(fs_api);
for(size_t i = 0; i < archive_file_get_array_size(browser); i++) { for(size_t i = 0; i < archive_file_get_array_size(browser); i++) {
ArchiveFile_t* item = archive_get_file_at(browser, i); ArchiveFile_t* item = archive_get_file_at(browser, i);
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(item->name)); archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(item->name));
} }
file_worker_remove(file_worker, ARCHIVE_FAV_PATH); storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
file_worker_rename(file_worker, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH); storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
file_worker_free(file_worker); storage_file_free(file);
} furi_record_close("storage");
}

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "file_worker.h"
#include <storage/storage.h>
#define ARCHIVE_FAV_PATH "/any/favorites.txt" #define ARCHIVE_FAV_PATH "/any/favorites.txt"
#define ARCHIVE_FAV_TEMP_PATH "/any/favorites.tmp" #define ARCHIVE_FAV_TEMP_PATH "/any/favorites.tmp"

View File

@ -201,18 +201,18 @@ void archive_file_append(const char* path, const char* format, ...) {
string_init_vprintf(string, format, args); string_init_vprintf(string, format, args);
va_end(args); va_end(args);
FileWorker* file_worker = file_worker_alloc(false); Storage* fs_api = furi_record_open("storage");
File* file = storage_file_alloc(fs_api);
if(!file_worker_open(file_worker, path, FSAM_WRITE, FSOM_OPEN_APPEND)) { bool res = storage_file_open(file, path, FSAM_WRITE, FSOM_OPEN_APPEND);
FURI_LOG_E(TAG, "Append open error");
if(res) {
storage_file_write(file, string_get_cstr(string), string_size(string));
} }
if(!file_worker_write(file_worker, string_get_cstr(string), string_size(string))) { storage_file_close(file);
FURI_LOG_E(TAG, "Append write error"); storage_file_free(file);
} furi_record_close("storage");
file_worker_close(file_worker);
file_worker_free(file_worker);
} }
void archive_delete_file(void* context, const char* format, ...) { void archive_delete_file(void* context, const char* format, ...) {

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include "file_worker.h"
#include <m-array.h> #include <m-array.h>
#include <m-string.h>
#include <storage/storage.h>
typedef enum { typedef enum {
ArchiveFileTypeIButton, ArchiveFileTypeIButton,

View File

@ -54,6 +54,10 @@ static void render_item_menu(Canvas* canvas, ArchiveBrowserViewModel* model) {
ArchiveFile_t* selected = files_array_get(model->files, model->item_idx - model->array_offset); 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)) { if(!archive_is_known_app(selected->type)) {
string_set_str(menu[0], "---"); string_set_str(menu[0], "---");
string_set_str(menu[1], "---"); string_set_str(menu[1], "---");
@ -67,10 +71,6 @@ static void render_item_menu(Canvas* canvas, ArchiveBrowserViewModel* model) {
} }
} }
if((selected->fav) || (model->tab_idx == ArchiveTabFavorites)) {
string_set_str(menu[1], "Unpin");
}
for(size_t i = 0; i < MENU_ITEMS; i++) { for(size_t i = 0; i < MENU_ITEMS; i++) {
canvas_draw_str(canvas, 82, 27 + i * 11, string_get_cstr(menu[i])); canvas_draw_str(canvas, 82, 27 + i * 11, string_get_cstr(menu[i]));
string_clear(menu[i]); string_clear(menu[i]);

View File

@ -401,6 +401,8 @@ void cli_delete_command(Cli* cli, const char* name) {
int32_t cli_srv(void* p) { int32_t cli_srv(void* p) {
Cli* cli = cli_alloc(); Cli* cli = cli_alloc();
furi_hal_vcp_init();
// Init basic cli commands // Init basic cli commands
cli_commands_init(cli); cli_commands_init(cli);

View File

@ -55,10 +55,9 @@ void furi_hal_init() {
furi_hal_crypto_init(); furi_hal_crypto_init();
furi_hal_crc_init(true); furi_hal_crc_init(true);
// VCP + USB // USB
#ifndef FURI_RAM_EXEC #ifndef FURI_RAM_EXEC
furi_hal_usb_init(); furi_hal_usb_init();
furi_hal_vcp_init();
FURI_LOG_I(TAG, "USB OK"); FURI_LOG_I(TAG, "USB OK");
#endif #endif

View File

@ -268,8 +268,8 @@ static int32_t furi_hal_usb_thread(void* context) {
usbd_reg_event(&udev, usbd_evt_reset, reset_evt); usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
FURI_LOG_I(TAG, "USB Mode change done"); FURI_LOG_I(TAG, "USB Mode change done");
usb.enabled = true; usb.enabled = true;
usb.if_cur = if_new;
} }
usb.if_cur = if_new;
} }
if(flags & EventEnable) { if(flags & EventEnable) {
if((!usb.enabled) && (usb.if_cur != NULL)) { if((!usb.enabled) && (usb.if_cur != NULL)) {

View File

@ -12,15 +12,14 @@
#define VCP_IF_NUM 0 #define VCP_IF_NUM 0
typedef enum { typedef enum {
VcpEvtReserved = (1 << 0), // Reserved for StreamBuffer internal event VcpEvtEnable = (1 << 0),
VcpEvtEnable = (1 << 1), VcpEvtDisable = (1 << 1),
VcpEvtDisable = (1 << 2), VcpEvtConnect = (1 << 2),
VcpEvtConnect = (1 << 3), VcpEvtDisconnect = (1 << 3),
VcpEvtDisconnect = (1 << 4), VcpEvtStreamRx = (1 << 4),
VcpEvtStreamRx = (1 << 5), VcpEvtRx = (1 << 5),
VcpEvtRx = (1 << 6), VcpEvtStreamTx = (1 << 6),
VcpEvtStreamTx = (1 << 7), VcpEvtTx = (1 << 7),
VcpEvtTx = (1 << 8),
} WorkerEvtFlags; } WorkerEvtFlags;
#define VCP_THREAD_FLAG_ALL \ #define VCP_THREAD_FLAG_ALL \

View File

@ -1225,7 +1225,7 @@ osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks) {
stat = osErrorParameter; stat = osErrorParameter;
} }
else { else {
if (xTimerChangePeriod (hTimer, ticks, 0) == pdPASS) { if (xTimerChangePeriod (hTimer, ticks, portMAX_DELAY) == pdPASS) {
stat = osOK; stat = osOK;
} else { } else {
stat = osErrorResource; stat = osErrorResource;
@ -1254,7 +1254,7 @@ osStatus_t osTimerStop (osTimerId_t timer_id) {
stat = osErrorResource; stat = osErrorResource;
} }
else { else {
if (xTimerStop (hTimer, 0) == pdPASS) { if (xTimerStop (hTimer, portMAX_DELAY) == pdPASS) {
stat = osOK; stat = osOK;
} else { } else {
stat = osError; stat = osError;
@ -1305,7 +1305,7 @@ osStatus_t osTimerDelete (osTimerId_t timer_id) {
callb = (TimerCallback_t *)pvTimerGetTimerID (hTimer); callb = (TimerCallback_t *)pvTimerGetTimerID (hTimer);
#endif #endif
if (xTimerDelete (hTimer, 0) == pdPASS) { if (xTimerDelete (hTimer, portMAX_DELAY) == pdPASS) {
#if (configSUPPORT_DYNAMIC_ALLOCATION == 1) #if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
if ((uint32_t)callb & 1U) { if ((uint32_t)callb & 1U) {
/* Callback memory was allocated from dynamic pool, clear flag */ /* Callback memory was allocated from dynamic pool, clear flag */