From c1aa5a5ea7d5ecde63a786827a866312c04507f9 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Mon, 25 Oct 2021 03:02:02 +0000 Subject: [PATCH] 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(-) 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;