Преглед изворни кода

[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
Nikolay Minaylov пре 3 година
родитељ
комит
72a6bbb8ad

+ 18 - 8
applications/archive/helpers/archive_apps.c

@@ -21,13 +21,23 @@ bool archive_app_is_available(void* context, const char* path) {
     ArchiveAppTypeEnum app = archive_get_app_type(path);
 
     if(app == ArchiveAppTypeU2f) {
-        FileWorker* file_worker = file_worker_alloc(true);
         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) {
-            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;
     } else {
         return false;
@@ -60,10 +70,10 @@ void archive_app_delete_file(void* context, const char* path) {
     bool res = false;
 
     if(app == ArchiveAppTypeU2f) {
-        FileWorker* file_worker = file_worker_alloc(true);
-        res = file_worker_remove(file_worker, "/any/u2f/key.u2f");
-        res |= file_worker_remove(file_worker, "/any/u2f/cnt.u2f");
-        file_worker_free(file_worker);
+        Storage* fs_api = furi_record_open("storage");
+        res = (storage_common_remove(fs_api, "/any/u2f/key.u2f") == FSE_OK);
+        res |= (storage_common_remove(fs_api, "/any/u2f/cnt.u2f") == FSE_OK);
+        furi_record_close("storage");
 
         if(archive_is_favorite("/app:u2f/U2F Token")) {
             archive_favorites_delete("/app:u2f/U2F Token");

+ 11 - 0
applications/archive/helpers/archive_browser.c

@@ -392,6 +392,17 @@ void archive_enter_dir(ArchiveBrowserView* browser, string_t name) {
     furi_assert(browser);
     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));
 
     with_view_model(

+ 1 - 0
applications/archive/helpers/archive_browser.h

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

+ 110 - 46
applications/archive/helpers/archive_favorites.c

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

+ 2 - 1
applications/archive/helpers/archive_favorites.h

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

+ 8 - 8
applications/archive/helpers/archive_files.c

@@ -201,18 +201,18 @@ void archive_file_append(const char* path, const char* format, ...) {
     string_init_vprintf(string, format, 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)) {
-        FURI_LOG_E(TAG, "Append open error");
-    }
+    bool res = storage_file_open(file, path, FSAM_WRITE, FSOM_OPEN_APPEND);
 
-    if(!file_worker_write(file_worker, string_get_cstr(string), string_size(string))) {
-        FURI_LOG_E(TAG, "Append write error");
+    if(res) {
+        storage_file_write(file, string_get_cstr(string), string_size(string));
     }
 
-    file_worker_close(file_worker);
-    file_worker_free(file_worker);
+    storage_file_close(file);
+    storage_file_free(file);
+    furi_record_close("storage");
 }
 
 void archive_delete_file(void* context, const char* format, ...) {

+ 3 - 1
applications/archive/helpers/archive_files.h

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

+ 4 - 4
applications/archive/views/archive_browser_view.c

@@ -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);
 
+    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], "---");
@@ -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++) {
         canvas_draw_str(canvas, 82, 27 + i * 11, string_get_cstr(menu[i]));
         string_clear(menu[i]);

+ 2 - 0
applications/cli/cli.c

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

+ 1 - 2
firmware/targets/f7/furi_hal/furi_hal.c

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

+ 1 - 1
firmware/targets/f7/furi_hal/furi_hal_usb.c

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

+ 8 - 9
firmware/targets/f7/furi_hal/furi_hal_vcp.c

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

+ 3 - 3
lib/FreeRTOS-glue/cmsis_os2.c

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