From ac7b2da611fa5ef4cc989a9feb027f66c0ebfc6c Mon Sep 17 00:00:00 2001 From: Reinhold Gschweicher Date: Wed, 13 Oct 2021 22:08:35 +0200 Subject: Update includes to to be relative to src directory Don't use relative imports like `../foo.h` as those depend on the relative position of both files. Rather than that use imports relative to the `src` directory, which explicitly is part of the include directories. --- src/components/fs/FS.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/components/fs') diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index 857e6bf9..1cad4f02 100644 --- a/src/components/fs/FS.cpp +++ b/src/components/fs/FS.cpp @@ -1,4 +1,4 @@ -#include "FS.h" +#include "components/fs/FS.h" #include #include #include -- cgit v1.2.3-70-g09d2 From 91c644b43c250b0a03047349182828df31ddcbd2 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 16:04:23 +0000 Subject: direcetory listings maybe? Added LISTDIR command and notify responses. --- src/components/ble/FSService.cpp | 73 ++++++++++++++++++++++++++++++++++++++-- src/components/ble/FSService.h | 27 +++++++++++++-- src/components/fs/FS.cpp | 63 +++++++++++++++++++--------------- src/components/fs/FS.h | 3 ++ 4 files changed, 132 insertions(+), 34 deletions(-) (limited to 'src/components/fs') diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 6551c20c..2cfd5ccd 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -1,5 +1,6 @@ #include #include "FSService.h" +#include "components/ble/BleController.h" using namespace Pinetime::Controllers; @@ -13,8 +14,8 @@ int FSServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gat } FSService::FSService(Pinetime::Controllers::FS& fs) - : fs {fs}, - characteristicDefinition {{.uuid = &fsVersionUuid.u, + : fs {fs}, + characteristicDefinition {{.uuid = &fsVersionUuid.u, .access_cb = FSServiceCallback, .arg = this, .flags = BLE_GATT_CHR_F_READ, @@ -24,7 +25,7 @@ FSService::FSService(Pinetime::Controllers::FS& fs) .access_cb = FSServiceCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, - .val_handle = nullptr, + .val_handle = &transferCharacteristicHandle, }, {0}}, serviceDefinition { @@ -51,5 +52,71 @@ int FSService::OnFSServiceRequested(uint16_t connectionHandle, uint16_t attribut int res = os_mbuf_append(context->om, &fsVersion, sizeof(fsVersion)); return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; } + if (attributeHandle == transferCharacteristicHandle) { + return FSCommandHandler(connectionHandle, context->om); + } + return 0; +} + +int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { + auto command = static_cast(om->om_data[0]); + NRF_LOG_INFO("[FS_S] -> FSCommandHandler"); + + switch (command) { + case commands::LISTDIR: { + NRF_LOG_INFO("[FS_S] -> ListDir"); + ListDirHeader *header = (ListDirHeader *)&om->om_data[0]; + uint16_t plen = header->pathlen; + char path[plen+1] = {0}; + memcpy(path, header->pathstr, plen); + NRF_LOG_INFO("[FS_S] -> DIR %.10s", path); + lfs_dir_t dir; + struct lfs_info info; + + ListDirResponse resp; + resp.command = 0x51; // LISTDIR_ENTRY; + resp.status = 1; // TODO actually use res above! + resp.totalentries = 0; + resp.entry = 0; + int res = fs.DirOpen(path, &dir); + while (fs.DirRead(&dir, &info)) { + resp.totalentries++; + } + NRF_LOG_INFO("[FS_S] -> %d ", resp.totalentries); + fs.DirClose(&dir); + fs.DirOpen(path, &dir); + while (fs.DirRead(&dir, &info)) { + switch(info.type){ + case LFS_TYPE_REG: + { + resp.flags = 0; + resp.file_size = info.size; + break; + } + case LFS_TYPE_DIR: + { + resp.flags = 1; + resp.file_size = 0; + break; + } + } + resp.modification_time = 0; // TODO Does LFS actually support TS? + strcpy(resp.path,info.name); + resp.path_length = strlen(info.name); + NRF_LOG_INFO("[FS_S] ->Path %s ,", info.name); + auto* om = ble_hs_mbuf_from_flat(&resp,sizeof(ListDirResponse)); + ble_gattc_notify_custom(connectionHandle,transferCharacteristicHandle,om); + resp.entry++; + } + resp.entry++; + resp.file_size = 0; + resp.path_length = 0; + resp.flags = 0; + //Todo this better + auto* om = ble_hs_mbuf_from_flat(&resp,sizeof(ListDirResponse)-70+resp.path_length); + ble_gattc_notify_custom(connectionHandle,transferCharacteristicHandle,om); + NRF_LOG_INFO("[FS_S] -> done "); + } + } return 0; } diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h index 85b484a3..eb4b34d7 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -4,6 +4,7 @@ #include #undef max #undef min + #include "components/fs/FS.h" namespace Pinetime { @@ -42,8 +43,26 @@ namespace Pinetime { struct ble_gatt_chr_def characteristicDefinition[3]; struct ble_gatt_svc_def serviceDefinition[2]; uint16_t versionCharacteristicHandle; + uint16_t transferCharacteristicHandle; + typedef struct __attribute__((packed)) { + uint8_t command; + uint8_t padding; + uint16_t pathlen; + char pathstr[70]; + } ListDirHeader; + typedef struct __attribute__((packed)) { + uint8_t command; + uint8_t status; + uint16_t path_length; + uint32_t entry; + uint32_t totalentries; + uint32_t flags; + uint32_t modification_time; + uint32_t file_size; + char path[70]; + } ListDirResponse; - enum class commands { + enum class commands : uint8_t { INVALID = 0x00, READ = 0x10, READ_DATA = 0x11, @@ -58,8 +77,10 @@ namespace Pinetime { LISTDIR = 0x50, LISTDIR_ENTRY = 0x51, MOVE = 0x60, - MOVE_STATUS = 0x61, - } + MOVE_STATUS = 0x61 + }; + + int FSCommandHandler(uint16_t connectionHandle, os_mbuf* om); }; } } diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index 1cad4f02..f287c28e 100644 --- a/src/components/fs/FS.cpp +++ b/src/components/fs/FS.cpp @@ -5,29 +5,28 @@ using namespace Pinetime::Controllers; -FS::FS(Pinetime::Drivers::SpiNorFlash& driver) : - flashDriver{ driver }, - lfsConfig{ - .context = this, - .read = SectorRead, - .prog = SectorProg, - .erase = SectorErase, - .sync = SectorSync, - - .read_size = 16, - .prog_size = 8, - .block_size = blockSize, - .block_count = size / blockSize, - .block_cycles = 1000u, - - .cache_size = 16, - .lookahead_size = 16, - - .name_max = 50, - .attr_max = 50, - } -{ } - +FS::FS(Pinetime::Drivers::SpiNorFlash& driver) + : flashDriver {driver}, + lfsConfig { + .context = this, + .read = SectorRead, + .prog = SectorProg, + .erase = SectorErase, + .sync = SectorSync, + + .read_size = 16, + .prog_size = 8, + .block_size = blockSize, + .block_count = size / blockSize, + .block_cycles = 1000u, + + .cache_size = 16, + .lookahead_size = 16, + + .name_max = 50, + .attr_max = 50, + } { +} void FS::Init() { @@ -48,7 +47,6 @@ void FS::Init() { VerifyResource(); LVGLFileSystemInit(); #endif - } void FS::VerifyResource() { @@ -56,7 +54,7 @@ void FS::VerifyResource() { resourcesValid = true; } -int FS::FileOpen(lfs_file_t* file_p, const char* fileName, const int flags) { +int FS::FileOpen(lfs_file_t* file_p, const char* fileName, const int flags) { return lfs_file_open(&lfs, file_p, fileName, flags); } @@ -80,6 +78,17 @@ int FS::FileDelete(const char* fileName) { return lfs_remove(&lfs, fileName); } +int FS::DirOpen(const char* path, lfs_dir_t* lfs_dir) { + return lfs_dir_open(&lfs, lfs_dir, path); +} + +int FS::DirClose(lfs_dir_t* lfs_dir) { + return lfs_dir_close(&lfs, lfs_dir); +} + +int FS::DirRead(lfs_dir_t* dir, lfs_info* info) { + return lfs_dir_read(&lfs, dir, info); +} int FS::DirCreate(const char* path) { return lfs_mkdir(&lfs, path); @@ -148,8 +157,7 @@ namespace { if (file->type == 0) { return LV_FS_RES_FS_ERR; - } - else { + } else { return LV_FS_RES_OK; } } @@ -193,5 +201,4 @@ void FS::LVGLFileSystemInit() { fs_drv.user_data = this; lv_fs_drv_register(&fs_drv); - } \ No newline at end of file diff --git a/src/components/fs/FS.h b/src/components/fs/FS.h index 75ba16c8..28d28d3c 100644 --- a/src/components/fs/FS.h +++ b/src/components/fs/FS.h @@ -21,6 +21,9 @@ namespace Pinetime { int FileDelete(const char* fileName); + int DirOpen(const char* path, lfs_dir_t* lfs_dir); + int DirClose(lfs_dir_t* lfs_dir); + int DirRead(lfs_dir_t* dir, lfs_info* info); int DirCreate(const char* path); int DirDelete(const char* path); -- cgit v1.2.3-70-g09d2 From 1dd71744802b4ae80c8952f73bbf0051fbe12cf3 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 20:48:34 +0000 Subject: More reliable FS listing --- src/components/ble/FSService.cpp | 17 ++++++++++++----- src/components/fs/FS.cpp | 4 +++- src/components/fs/FS.h | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) (limited to 'src/components/fs') diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 2cfd5ccd..2f02cd96 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -70,21 +70,26 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { char path[plen+1] = {0}; memcpy(path, header->pathstr, plen); NRF_LOG_INFO("[FS_S] -> DIR %.10s", path); - lfs_dir_t dir; - struct lfs_info info; + lfs_dir_t dir = {}; + struct lfs_info info = {}; - ListDirResponse resp; + ListDirResponse resp = {}; resp.command = 0x51; // LISTDIR_ENTRY; resp.status = 1; // TODO actually use res above! resp.totalentries = 0; resp.entry = 0; + int sr; int res = fs.DirOpen(path, &dir); + + NRF_LOG_INFO("[FS_S] ->diropen %d ", res); while (fs.DirRead(&dir, &info)) { resp.totalentries++; + } NRF_LOG_INFO("[FS_S] -> %d ", resp.totalentries); - fs.DirClose(&dir); - fs.DirOpen(path, &dir); + + fs.DirRewind(&dir); + while (fs.DirRead(&dir, &info)) { switch(info.type){ case LFS_TYPE_REG: @@ -106,8 +111,10 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { NRF_LOG_INFO("[FS_S] ->Path %s ,", info.name); auto* om = ble_hs_mbuf_from_flat(&resp,sizeof(ListDirResponse)); ble_gattc_notify_custom(connectionHandle,transferCharacteristicHandle,om); + vTaskDelay(1); //Allow stuff to actually go out over the BLE conn resp.entry++; } + fs.DirClose(&dir); resp.entry++; resp.file_size = 0; resp.path_length = 0; diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index f287c28e..353193dc 100644 --- a/src/components/fs/FS.cpp +++ b/src/components/fs/FS.cpp @@ -89,7 +89,9 @@ int FS::DirClose(lfs_dir_t* lfs_dir) { int FS::DirRead(lfs_dir_t* dir, lfs_info* info) { return lfs_dir_read(&lfs, dir, info); } - +int FS::DirRewind(lfs_dir_t* dir) { + return lfs_dir_rewind(&lfs, dir); +} int FS::DirCreate(const char* path) { return lfs_mkdir(&lfs, path); } diff --git a/src/components/fs/FS.h b/src/components/fs/FS.h index 28d28d3c..ccff2409 100644 --- a/src/components/fs/FS.h +++ b/src/components/fs/FS.h @@ -24,6 +24,7 @@ namespace Pinetime { int DirOpen(const char* path, lfs_dir_t* lfs_dir); int DirClose(lfs_dir_t* lfs_dir); int DirRead(lfs_dir_t* dir, lfs_info* info); + int DirRewind(lfs_dir_t* dir); int DirCreate(const char* path); int DirDelete(const char* path); -- cgit v1.2.3-70-g09d2 From 3a8e66a52fcc6317a7dffa15cb161f37a645d36c Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 23:07:43 +0000 Subject: Added Delete file Added FS Stat. --- src/components/ble/FSService.cpp | 96 ++++++++++++++++++++++++++++++---------- src/components/ble/FSService.h | 20 +++++++-- src/components/fs/FS.cpp | 4 ++ src/components/fs/FS.h | 1 + 4 files changed, 93 insertions(+), 28 deletions(-) (limited to 'src/components/fs') diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 40679e5c..0cf2b937 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -63,64 +63,112 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { NRF_LOG_INFO("[FS_S] -> FSCommandHandler"); switch (command) { + case commands::DELETE: { + NRF_LOG_INFO("[FS_S] -> Delete"); + auto* header = (DelHeader*)om->om_data; + uint16_t plen = header->pathlen; + char path[plen + 1] = {0}; + struct lfs_info info = {}; + DelResponse resp = {}; + resp.command = commands::DELETE_STATUS; + int res = fs.Stat(path, &info); + // Get Info about path + // branch for DirDel of FileDelete + if (info.type == LFS_TYPE_DIR) { + res = fs.DirDelete(path); + } else { + res = fs.FileDelete(path); + } + switch (res) { + case LFS_ERR_OK: + resp.status = 0x01; + break; + default: + resp.status = 0x02; + break; + } + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(DelResponse)); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + break; + } + case commands::MKDIR: { + NRF_LOG_INFO("[FS_S] -> MKDir"); + auto* header = (MKDirHeader*) om->om_data; + uint16_t plen = header->pathlen; + char path[plen + 1] = {0}; + memcpy(path, header->pathstr, plen); + NRF_LOG_INFO("[FS_S] -> MKDIR %.10s", path); + MKDirResponse resp = {}; + resp.command = commands::MKDIR_STATUS; + int res = fs.DirCreate(path); + switch (res) { + case LFS_ERR_OK: + resp.status = 0x01; + break; + default: + resp.status = 0x02; + break; + } + resp.modification_time = 0; // We should timestamp..but no + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(MKDirResponse)); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + break; + } case commands::LISTDIR: { NRF_LOG_INFO("[FS_S] -> ListDir"); - ListDirHeader *header = (ListDirHeader *)&om->om_data[0]; + ListDirHeader* header = (ListDirHeader*)om->om_data; uint16_t plen = header->pathlen; - char path[plen+1] = {0}; + char path[plen + 1] = {0}; memcpy(path, header->pathstr, plen); NRF_LOG_INFO("[FS_S] -> DIR %.10s", path); lfs_dir_t dir = {}; struct lfs_info info = {}; ListDirResponse resp = {}; - resp.command = 0x51; // LISTDIR_ENTRY; - resp.status = 1; // TODO actually use res above! + resp.command = commands::LISTDIR_ENTRY; + resp.status = 1; // TODO actually use res above! resp.totalentries = 0; resp.entry = 0; int res = fs.DirOpen(path, &dir); - + NRF_LOG_INFO("[FS_S] ->diropen %d ", res); while (fs.DirRead(&dir, &info)) { resp.totalentries++; - } NRF_LOG_INFO("[FS_S] -> %d ", resp.totalentries); - + fs.DirRewind(&dir); - + while (fs.DirRead(&dir, &info)) { - switch(info.type){ - case LFS_TYPE_REG: - { + switch (info.type) { + case LFS_TYPE_REG: { resp.flags = 0; resp.file_size = info.size; break; - } - case LFS_TYPE_DIR: - { - resp.flags = 1; - resp.file_size = 0; - break; + } + case LFS_TYPE_DIR: { + resp.flags = 1; + resp.file_size = 0; + break; } } resp.modification_time = 0; // TODO Does LFS actually support TS? - strcpy(resp.path,info.name); + strcpy(resp.path, info.name); resp.path_length = strlen(info.name); NRF_LOG_INFO("[FS_S] ->Path %s ,", info.name); - auto* om = ble_hs_mbuf_from_flat(&resp,sizeof(ListDirResponse)); - ble_gattc_notify_custom(connectionHandle,transferCharacteristicHandle,om); - vTaskDelay(1); //Allow stuff to actually go out over the BLE conn + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + vTaskDelay(1); // Allow stuff to actually go out over the BLE conn resp.entry++; } fs.DirClose(&dir); resp.file_size = 0; resp.path_length = 0; resp.flags = 0; - //Todo this better - auto* om = ble_hs_mbuf_from_flat(&resp,sizeof(ListDirResponse)-70+resp.path_length); - ble_gattc_notify_custom(connectionHandle,transferCharacteristicHandle,om); + // TODO Handle Size of response better. + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse) - 70 + resp.path_length); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); NRF_LOG_INFO("[FS_S] -> done "); break; } diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h index a1c42aa7..3ca6d934 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -66,14 +66,14 @@ namespace Pinetime { }; using ListDirHeader = struct __attribute__((packed)) { - uint8_t command; + commands command; uint8_t padding; uint16_t pathlen; char pathstr[70]; }; using ListDirResponse = struct __attribute__((packed)) { - uint8_t command; + commands command; uint8_t status; uint16_t path_length; uint32_t entry; @@ -85,7 +85,7 @@ namespace Pinetime { }; using MKDirHeader = struct __attribute__((packed)) { - uint8_t command; + commands command; uint8_t padding; uint16_t pathlen; uint32_t padding2; @@ -94,13 +94,25 @@ namespace Pinetime { }; using MKDirResponse = struct __attribute__((packed)) { - uint8_t command; + commands command; uint8_t status; uint32_t padding1; uint16_t padding2; uint64_t modification_time; }; + using DelHeader = struct __attribute__((packed)) { + commands command; + uint8_t padding; + uint16_t pathlen; + char pathstr[70]; + }; + + using DelResponse = struct __attribute__((packed)) { + commands command; + uint8_t status; + }; + }; } } diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index 353193dc..d30b7373 100644 --- a/src/components/fs/FS.cpp +++ b/src/components/fs/FS.cpp @@ -96,6 +96,10 @@ int FS::DirCreate(const char* path) { return lfs_mkdir(&lfs, path); } +int FS::Stat(const char* path, lfs_info* info){ + return lfs_stat(&lfs,path,info); +} + // Delete directory and all files inside int FS::DirDelete(const char* path) { diff --git a/src/components/fs/FS.h b/src/components/fs/FS.h index ccff2409..e50ff10a 100644 --- a/src/components/fs/FS.h +++ b/src/components/fs/FS.h @@ -28,6 +28,7 @@ namespace Pinetime { int DirCreate(const char* path); int DirDelete(const char* path); + int Stat(const char* path, lfs_info* info); void VerifyResource(); private: -- cgit v1.2.3-70-g09d2 From d89e38d3bf9e84e69635f0cb10cc42f0071fa038 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 19 Oct 2021 19:03:00 +0000 Subject: Focus on getting flash access working properly --- src/components/ble/FSService.cpp | 10 +++++---- src/components/ble/FSService.h | 2 +- src/components/fs/FS.cpp | 12 ++++++++-- src/components/fs/FS.h | 47 ++++++++++++++++++++-------------------- 4 files changed, 41 insertions(+), 30 deletions(-) (limited to 'src/components/fs') diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 52cfafb8..0a1fabb7 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -60,9 +60,10 @@ int FSService::OnFSServiceRequested(uint16_t connectionHandle, uint16_t attribut int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { auto command = static_cast(om->om_data[0]); - NRF_LOG_INFO("[FS_S] -> FSCommandHandler"); - + NRF_LOG_INFO("[FS_S] -> FSCommandHandler %d",command); + fs.Mount(); switch (command) { + /* case commands::READ: { NRF_LOG_INFO("[FS_S] -> Read"); if (state != FSState::IDLE) { @@ -194,7 +195,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(MKDirResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; - } + }*/ case commands::LISTDIR: { NRF_LOG_INFO("[FS_S] -> ListDir"); ListDirHeader* header = (ListDirHeader*) om->om_data; @@ -244,7 +245,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { NRF_LOG_INFO("[FS_S] ->Path %s ,", info.name); auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)+resp.path_length); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); - vTaskDelay(5); // Allow stuff to actually go out over the BLE conn + vTaskDelay(10); // Allow stuff to actually go out over the BLE conn resp.entry++; } fs.DirClose(&dir); @@ -258,6 +259,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { break; } } + fs.UnMount(); return 0; } // Loads resp with file data given a valid filepath header and resp diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h index 93205e54..114c1e0d 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -109,7 +109,7 @@ namespace Pinetime { uint32_t entry; uint32_t totalentries; uint32_t flags; - uint32_t modification_time; + uint64_t modification_time; uint32_t file_size; char path[]; }; diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index d30b7373..c8a5a2eb 100644 --- a/src/components/fs/FS.cpp +++ b/src/components/fs/FS.cpp @@ -54,6 +54,14 @@ void FS::VerifyResource() { resourcesValid = true; } +void FS::Mount() { + flashDriver.Wakeup(); +} + +void FS::UnMount() { + flashDriver.Sleep(); +} + int FS::FileOpen(lfs_file_t* file_p, const char* fileName, const int flags) { return lfs_file_open(&lfs, file_p, fileName, flags); } @@ -96,8 +104,8 @@ int FS::DirCreate(const char* path) { return lfs_mkdir(&lfs, path); } -int FS::Stat(const char* path, lfs_info* info){ - return lfs_stat(&lfs,path,info); +int FS::Stat(const char* path, lfs_info* info) { + return lfs_stat(&lfs, path, info); } // Delete directory and all files inside diff --git a/src/components/fs/FS.h b/src/components/fs/FS.h index e50ff10a..1aa8d5f1 100644 --- a/src/components/fs/FS.h +++ b/src/components/fs/FS.h @@ -13,6 +13,9 @@ namespace Pinetime { void Init(); void LVGLFileSystemInit(); + void Mount(); + void UnMount(); + int FileOpen(lfs_file_t* file_p, const char* fileName, const int flags); int FileClose(lfs_file_t* file_p); int FileRead(lfs_file_t* file_p, uint8_t* buff, uint32_t size); @@ -32,31 +35,30 @@ namespace Pinetime { void VerifyResource(); private: - Pinetime::Drivers::SpiNorFlash& flashDriver; /* - * External Flash MAP (4 MBytes) - * - * 0x000000 +---------------------------------------+ - * | Bootloader Assets | - * | 256 KBytes | - * | | - * 0x040000 +---------------------------------------+ - * | OTA | - * | 464 KBytes | - * | | - * | | - * | | - * 0x0B4000 +---------------------------------------+ - * | File System | - * | | - * | | - * | | - * | | - * 0x400000 +---------------------------------------+ - * - */ + * External Flash MAP (4 MBytes) + * + * 0x000000 +---------------------------------------+ + * | Bootloader Assets | + * | 256 KBytes | + * | | + * 0x040000 +---------------------------------------+ + * | OTA | + * | 464 KBytes | + * | | + * | | + * | | + * 0x0B4000 +---------------------------------------+ + * | File System | + * | | + * | | + * | | + * | | + * 0x400000 +---------------------------------------+ + * + */ static constexpr size_t startAddress = 0x0B4000; static constexpr size_t size = 0x34C000; static constexpr size_t blockSize = 4096; @@ -70,7 +72,6 @@ namespace Pinetime { static int SectorErase(const struct lfs_config* c, lfs_block_t block); static int SectorProg(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, const void* buffer, lfs_size_t size); static int SectorRead(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, void* buffer, lfs_size_t size); - }; } } -- cgit v1.2.3-70-g09d2 From c1aa5a5ea7d5ecde63a786827a866312c04507f9 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Mon, 25 Oct 2021 03:02:02 +0000 Subject: Write works --- src/components/ble/FSService.cpp | 52 ++++++++++++++++++++++++++++++---------- src/components/ble/FSService.h | 31 +++++++++++++++++++++--- src/components/fs/FS.cpp | 4 +++- src/components/fs/FS.h | 8 +++++-- 4 files changed, 77 insertions(+), 18 deletions(-) (limited to 'src/components/fs') diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 68fd5ea6..c784a8c4 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -83,6 +83,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { memcpy(filepath, header->pathstr, plen); filepath[plen + 1] = 0; // Copy and null teminate string ReadResponse resp; + os_mbuf* om; resp.command = commands::READ_DATA; resp.status = 0x01; resp.chunkoff = header->chunkoff; @@ -91,23 +92,19 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.status = 0x03; resp.chunklen = 0; resp.totallen = 0; + om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse)); } else { resp.chunklen = std::min(header->chunksize, info.size); // TODO add mtu somehow resp.totallen = info.size; fs.FileOpen(&f, filepath, LFS_O_RDONLY); fs.FileSeek(&f, header->chunkoff); - } - os_mbuf* om; - if (resp.chunklen > 0) { uint8_t fileData[resp.chunklen] {0}; resp.chunklen = fs.FileRead(&f, fileData, resp.chunklen); om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse)); os_mbuf_append(om, fileData, resp.chunklen); - } else { - resp.chunklen = 0; - om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse)); + fs.FileClose(&f); } - fs.FileClose(&f); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; } @@ -145,14 +142,45 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { break; } case commands::WRITE: { - if (state != FSState::IDLE) { + lfs_file f; + NRF_LOG_INFO("[FS_S] -> Write"); + auto* header = (WriteHeader*) om->om_data; + uint16_t plen = header->pathlen; + if (plen > maxpathlen) { //> counts for null term return -1; } + memcpy(filepath, header->pathstr, plen); + filepath[plen + 1] = 0; // Copy and null teminate string + fileSize = header->totalSize; + WriteResponse resp; + resp.command = commands::WRITE_PACING; + resp.offset = header->offset; + resp.modTime = 0; + int res = fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT); + resp.status = res ? 0x02 : 0x01; + fs.FileClose(&f); + resp.freespace = std::min(fs.getSize() - (fs.GetFSSize() * fs.getBlockSize()), fileSize - header->offset); + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(WriteResponse)); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + break; } - case commands::WRITE_PACING: { - if (state != FSState::WRITE) { - return -1; - } + case commands::WRITE_DATA: { + lfs_file f; + NRF_LOG_INFO("[FS_S] -> WriteData"); + auto* header = (WritePacing*) om->om_data; + WriteResponse resp; + resp.command = commands::WRITE_PACING; + resp.offset = header->offset; + int res = fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT); + resp.status = res ? 0x02 : 0x01; + fs.FileSeek(&f, header->offset); + fs.FileWrite(&f, header->data, header->dataSize); + fs.FileClose(&f); + resp.freespace = std::min(fs.getSize() - (fs.GetFSSize() * fs.getBlockSize()), fileSize - header->offset); + // NRF_LOG_INFO('[FS_S] Used Blocks -> %u',resp.freespace); + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(WriteResponse)); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + break; } case commands::DELETE: { NRF_LOG_INFO("[FS_S] -> Delete"); diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h index e9c98fb4..17f52eb0 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -47,9 +47,6 @@ namespace Pinetime { uint16_t versionCharacteristicHandle; uint16_t transferCharacteristicHandle; - // lfs_dir_t dir; - // lfs_info info; - enum class commands : uint8_t { INVALID = 0x00, READ = 0x10, @@ -74,6 +71,7 @@ namespace Pinetime { }; FSState state; char filepath[maxpathlen]; // TODO ..ugh fixed filepath len + int fileSize; using ReadHeader = struct __attribute__((packed)) { commands command; uint8_t padding; @@ -100,6 +98,33 @@ namespace Pinetime { uint32_t chunksize; }; + using WriteHeader = struct __attribute__((packed)) { + commands command; + uint8_t padding; + uint16_t pathlen; + uint32_t offset; + uint64_t modTime; + uint32_t totalSize; + char pathstr[]; + }; + + using WriteResponse = struct __attribute__((packed)) { + commands command; + uint8_t status; + uint16_t padding; + uint32_t offset; + uint64_t modTime; + uint32_t freespace; + }; + + using WritePacing = struct __attribute__((packed)) { + commands command; + uint8_t status; + uint16_t padding; + uint32_t offset; + uint32_t dataSize; + uint8_t data[]; + }; using ListDirHeader = struct __attribute__((packed)) { commands command; uint8_t padding; diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index c8a5a2eb..297706fe 100644 --- a/src/components/fs/FS.cpp +++ b/src/components/fs/FS.cpp @@ -107,7 +107,9 @@ int FS::DirCreate(const char* path) { int FS::Stat(const char* path, lfs_info* info) { return lfs_stat(&lfs, path, info); } - +lfs_ssize_t FS::GetFSSize(){ + return lfs_fs_size(&lfs); +} // Delete directory and all files inside int FS::DirDelete(const char* path) { diff --git a/src/components/fs/FS.h b/src/components/fs/FS.h index 1aa8d5f1..e4df9566 100644 --- a/src/components/fs/FS.h +++ b/src/components/fs/FS.h @@ -10,6 +10,8 @@ namespace Pinetime { public: FS(Pinetime::Drivers::SpiNorFlash&); + + void Init(); void LVGLFileSystemInit(); @@ -30,10 +32,11 @@ namespace Pinetime { int DirRewind(lfs_dir_t* dir); int DirCreate(const char* path); int DirDelete(const char* path); - + lfs_ssize_t GetFSSize(); int Stat(const char* path, lfs_info* info); void VerifyResource(); - + static size_t getSize(){return size;} + static size_t getBlockSize(){return blockSize;} private: Pinetime::Drivers::SpiNorFlash& flashDriver; @@ -62,6 +65,7 @@ namespace Pinetime { static constexpr size_t startAddress = 0x0B4000; static constexpr size_t size = 0x34C000; static constexpr size_t blockSize = 4096; + bool resourcesValid = false; const struct lfs_config lfsConfig; -- cgit v1.2.3-70-g09d2 From 2e10b0fe645f74d0ab057ac11b96f4eb3fffc4ae Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 26 Oct 2021 00:10:39 +0000 Subject: Remove mount/unmount. No longer needed --- src/components/fs/FS.cpp | 10 +--------- src/components/fs/FS.h | 15 +++++++-------- 2 files changed, 8 insertions(+), 17 deletions(-) (limited to 'src/components/fs') diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index 297706fe..8d82c39d 100644 --- a/src/components/fs/FS.cpp +++ b/src/components/fs/FS.cpp @@ -54,14 +54,6 @@ void FS::VerifyResource() { resourcesValid = true; } -void FS::Mount() { - flashDriver.Wakeup(); -} - -void FS::UnMount() { - flashDriver.Sleep(); -} - int FS::FileOpen(lfs_file_t* file_p, const char* fileName, const int flags) { return lfs_file_open(&lfs, file_p, fileName, flags); } @@ -107,7 +99,7 @@ int FS::DirCreate(const char* path) { int FS::Stat(const char* path, lfs_info* info) { return lfs_stat(&lfs, path, info); } -lfs_ssize_t FS::GetFSSize(){ +lfs_ssize_t FS::GetFSSize() { return lfs_fs_size(&lfs); } // Delete directory and all files inside diff --git a/src/components/fs/FS.h b/src/components/fs/FS.h index e4df9566..60dd8e51 100644 --- a/src/components/fs/FS.h +++ b/src/components/fs/FS.h @@ -10,14 +10,9 @@ namespace Pinetime { public: FS(Pinetime::Drivers::SpiNorFlash&); - - void Init(); void LVGLFileSystemInit(); - void Mount(); - void UnMount(); - int FileOpen(lfs_file_t* file_p, const char* fileName, const int flags); int FileClose(lfs_file_t* file_p); int FileRead(lfs_file_t* file_p, uint8_t* buff, uint32_t size); @@ -35,8 +30,13 @@ namespace Pinetime { lfs_ssize_t GetFSSize(); int Stat(const char* path, lfs_info* info); void VerifyResource(); - static size_t getSize(){return size;} - static size_t getBlockSize(){return blockSize;} + static size_t getSize() { + return size; + } + static size_t getBlockSize() { + return blockSize; + } + private: Pinetime::Drivers::SpiNorFlash& flashDriver; @@ -65,7 +65,6 @@ namespace Pinetime { static constexpr size_t startAddress = 0x0B4000; static constexpr size_t size = 0x34C000; static constexpr size_t blockSize = 4096; - bool resourcesValid = false; const struct lfs_config lfsConfig; -- cgit v1.2.3-70-g09d2 From f4322841ffc011ea5c541e40e6aa73ab11ebd988 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 26 Oct 2021 00:48:25 +0000 Subject: Remove DirDelete, implementation did not work and memory contraints are recursive. Better implemented on client side... --- src/components/fs/FS.cpp | 17 ----------------- src/components/fs/FS.h | 3 ++- 2 files changed, 2 insertions(+), 18 deletions(-) (limited to 'src/components/fs') diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index 8d82c39d..79fb2222 100644 --- a/src/components/fs/FS.cpp +++ b/src/components/fs/FS.cpp @@ -102,23 +102,6 @@ int FS::Stat(const char* path, lfs_info* info) { lfs_ssize_t FS::GetFSSize() { return lfs_fs_size(&lfs); } -// Delete directory and all files inside -int FS::DirDelete(const char* path) { - - lfs_dir_t lfs_dir; - lfs_info entryInfo; - - int err; - err = lfs_dir_open(&lfs, &lfs_dir, path); - if (err) { - return err; - } - while (lfs_dir_read(&lfs, &lfs_dir, &entryInfo)) { - lfs_remove(&lfs, entryInfo.name); - } - lfs_dir_close(&lfs, &lfs_dir); - return LFS_ERR_OK; -} /* diff --git a/src/components/fs/FS.h b/src/components/fs/FS.h index 60dd8e51..da3bd273 100644 --- a/src/components/fs/FS.h +++ b/src/components/fs/FS.h @@ -26,10 +26,11 @@ namespace Pinetime { int DirRead(lfs_dir_t* dir, lfs_info* info); int DirRewind(lfs_dir_t* dir); int DirCreate(const char* path); - int DirDelete(const char* path); + lfs_ssize_t GetFSSize(); int Stat(const char* path, lfs_info* info); void VerifyResource(); + static size_t getSize() { return size; } -- cgit v1.2.3-70-g09d2 From 8f46908d387cc4fe3f911bc0ca517719238418ed Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 26 Oct 2021 03:42:34 +0000 Subject: Fix lvgl_open to respect littlefs open errors --- src/components/ble/FSService.cpp | 6 ++---- src/components/fs/FS.cpp | 15 ++++++++------- 2 files changed, 10 insertions(+), 11 deletions(-) (limited to 'src/components/fs') diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index c784a8c4..0230ea15 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -156,7 +156,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.command = commands::WRITE_PACING; resp.offset = header->offset; resp.modTime = 0; - int res = fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT); + int res = fs.FileOpen(&f, filepath, LFS_O_WRONLY | LFS_O_CREAT); resp.status = res ? 0x02 : 0x01; fs.FileClose(&f); resp.freespace = std::min(fs.getSize() - (fs.GetFSSize() * fs.getBlockSize()), fileSize - header->offset); @@ -177,7 +177,6 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { fs.FileWrite(&f, header->data, header->dataSize); fs.FileClose(&f); resp.freespace = std::min(fs.getSize() - (fs.GetFSSize() * fs.getBlockSize()), fileSize - header->offset); - // NRF_LOG_INFO('[FS_S] Used Blocks -> %u',resp.freespace); auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(WriteResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; @@ -222,14 +221,13 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.status = 1; resp.totalentries = 0; resp.entry = 0; - resp.modification_time = 0; // TODO Does LFS actually support TS? + resp.modification_time = 0; if (fs.DirOpen(path, &dir) != 0) { resp.status = 0x02; auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; }; - // Count Total files in directory. while (fs.DirRead(&dir, &info)) { resp.totalentries++; } diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index 79fb2222..78b0f5cb 100644 --- a/src/components/fs/FS.cpp +++ b/src/components/fs/FS.cpp @@ -141,16 +141,17 @@ int FS::SectorRead(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, namespace { lv_fs_res_t lvglOpen(lv_fs_drv_t* drv, void* file_p, const char* path, lv_fs_mode_t mode) { - lfs_file_t* file = static_cast(file_p); FS* filesys = static_cast(drv->user_data); - filesys->FileOpen(file, path, LFS_O_RDONLY); - - if (file->type == 0) { - return LV_FS_RES_FS_ERR; - } else { - return LV_FS_RES_OK; + int res = filesys->FileOpen(file, path, LFS_O_RDONLY); + if (res == 0) { + if (file->type == 0) { + return LV_FS_RES_FS_ERR; + } else { + return LV_FS_RES_OK; + } } + return LV_FS_RES_NOT_EX; } lv_fs_res_t lvglClose(lv_fs_drv_t* drv, void* file_p) { -- cgit v1.2.3-70-g09d2 From 362a5ef1136399c178cf881f7569e3d291432ec1 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 16 Nov 2021 04:32:53 +0000 Subject: Added move function --- src/components/ble/FSService.cpp | 15 +++++++++++++++ src/components/ble/FSService.h | 17 ++++++++++++++--- src/components/fs/FS.cpp | 4 +++- src/components/fs/FS.h | 1 + 4 files changed, 33 insertions(+), 4 deletions(-) (limited to 'src/components/fs') diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 0230ea15..1082d24c 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -270,6 +270,21 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; } + case commands::MOVE: { + NRF_LOG_INFO("[FS_S] -> Move"); + MoveHeader* header = (MoveHeader*) om->om_data; + uint16_t plen = header->OldPathLength; + // Null Terminate string + header->pathstr[plen] = 0; + char path[header->NewPathLength + 1] {0}; + memcpy(path, &header->pathstr[plen + 1], header->NewPathLength); + MoveResponse resp {}; + resp.command = commands::MOVE_STATUS; + int res = fs.Rename(header->pathstr, path); + resp.status = (res == 0) ? 1 : 2; + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(MoveResponse)); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + } default: break; } diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h index 17f52eb0..828925a8 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -15,8 +15,7 @@ namespace Pinetime { class Ble; class FSService { public: - FSService(Pinetime::System::SystemTask& systemTask, - Pinetime::Controllers::FS& fs); + FSService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::FS& fs); void Init(); int OnFSServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); @@ -116,7 +115,7 @@ namespace Pinetime { uint64_t modTime; uint32_t freespace; }; - + using WritePacing = struct __attribute__((packed)) { commands command; uint8_t status; @@ -172,6 +171,18 @@ namespace Pinetime { commands command; uint8_t status; }; + using MoveHeader = struct __attribute__((packed)) { + commands command; + uint8_t padding; + uint16_t OldPathLength; + uint16_t NewPathLength; + char pathstr[]; + }; + + using MoveResponse = struct __attribute__((packed)) { + commands command; + uint8_t status; + }; int FSCommandHandler(uint16_t connectionHandle, os_mbuf* om); void prepareReadDataResp(ReadHeader* header, ReadResponse* resp); diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index 78b0f5cb..8c98ae34 100644 --- a/src/components/fs/FS.cpp +++ b/src/components/fs/FS.cpp @@ -95,7 +95,9 @@ int FS::DirRewind(lfs_dir_t* dir) { int FS::DirCreate(const char* path) { return lfs_mkdir(&lfs, path); } - +int FS::Rename(const char* oldPath, const char* newPath){ + return lfs_rename(&lfs,oldPath,newPath); +} int FS::Stat(const char* path, lfs_info* info) { return lfs_stat(&lfs, path, info); } diff --git a/src/components/fs/FS.h b/src/components/fs/FS.h index da3bd273..2b27ae5d 100644 --- a/src/components/fs/FS.h +++ b/src/components/fs/FS.h @@ -28,6 +28,7 @@ namespace Pinetime { int DirCreate(const char* path); lfs_ssize_t GetFSSize(); + int Rename(const char* oldPath, const char* newPath); int Stat(const char* path, lfs_info* info); void VerifyResource(); -- cgit v1.2.3-70-g09d2