From 362a5ef1136399c178cf881f7569e3d291432ec1 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 16 Nov 2021 04:32:53 +0000 Subject: [PATCH] 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(-) 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();