From f57f797ff54d9ee8864d3fc62f0c8662df13aad8 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 01:09:26 +0000 Subject: [PATCH 01/26] Added Blank FSService that exposes only version info --- src/CMakeLists.txt | 3 ++ src/components/ble/FSService.cpp | 55 +++++++++++++++++++++ src/components/ble/FSService.h | 65 +++++++++++++++++++++++++ src/components/ble/NimbleController.cpp | 9 ++-- src/components/ble/NimbleController.h | 3 ++ 5 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 src/components/ble/FSService.cpp create mode 100644 src/components/ble/FSService.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bb9f76fe74..809544c92f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -487,6 +487,7 @@ list(APPEND SOURCE_FILES components/ble/NavigationService.cpp displayapp/fonts/lv_font_navi_80.c components/ble/BatteryInformationService.cpp + components/ble/FSService.cpp components/ble/ImmediateAlertService.cpp components/ble/ServiceDiscovery.cpp components/ble/HeartRateService.cpp @@ -557,6 +558,7 @@ list(APPEND RECOVERY_SOURCE_FILES components/ble/MusicService.cpp components/ble/weather/WeatherService.cpp components/ble/BatteryInformationService.cpp + components/ble/FSService.cpp components/ble/ImmediateAlertService.cpp components/ble/ServiceDiscovery.cpp components/ble/NavigationService.cpp @@ -669,6 +671,7 @@ set(INCLUDE_FILES components/ble/DfuService.h components/firmwarevalidator/FirmwareValidator.h components/ble/BatteryInformationService.h + components/ble/FSService.h components/ble/ImmediateAlertService.h components/ble/ServiceDiscovery.h components/ble/BleClient.h diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp new file mode 100644 index 0000000000..6551c20c93 --- /dev/null +++ b/src/components/ble/FSService.cpp @@ -0,0 +1,55 @@ +#include +#include "FSService.h" + +using namespace Pinetime::Controllers; + +constexpr ble_uuid128_t FSService::fsServiceUuid; +constexpr ble_uuid128_t FSService::fsVersionUuid; +constexpr ble_uuid128_t FSService::fsTransferUuid; + +int FSServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { + auto* fsService = static_cast(arg); + return fsService->OnFSServiceRequested(conn_handle, attr_handle, ctxt); +} + +FSService::FSService(Pinetime::Controllers::FS& fs) + : fs {fs}, + characteristicDefinition {{.uuid = &fsVersionUuid.u, + .access_cb = FSServiceCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + .val_handle = &versionCharacteristicHandle}, + { + .uuid = &fsTransferUuid.u, + .access_cb = FSServiceCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, + .val_handle = nullptr, + }, + {0}}, + serviceDefinition { + {/* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = &fsServiceUuid.u, + .characteristics = characteristicDefinition}, + {0}, + } { +} + +void FSService::Init() { + int res = 0; + res = ble_gatts_count_cfg(serviceDefinition); + ASSERT(res == 0); + + res = ble_gatts_add_svcs(serviceDefinition); + ASSERT(res == 0); +} + +int FSService::OnFSServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) { + if (attributeHandle == versionCharacteristicHandle) { + NRF_LOG_INFO("FS_S : handle = %d", versionCharacteristicHandle); + int res = os_mbuf_append(context->om, &fsVersion, sizeof(fsVersion)); + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + return 0; +} diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h new file mode 100644 index 0000000000..85b484a36f --- /dev/null +++ b/src/components/ble/FSService.h @@ -0,0 +1,65 @@ +#pragma once +#define min // workaround: nimble's min/max macros conflict with libstdc++ +#define max +#include +#undef max +#undef min +#include "components/fs/FS.h" + +namespace Pinetime { + namespace System { + class SystemTask; + } + namespace Controllers { + class Ble; + class FSService { + public: + FSService(Pinetime::Controllers::FS& fs); + void Init(); + + int OnFSServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); + void NotifyFSRaw(uint16_t connectionHandle); + + private: + Pinetime::Controllers::FS& fs; + static constexpr uint16_t FSServiceId {0xFEBB}; + static constexpr uint16_t fsVersionId {0x0100}; + static constexpr uint16_t fsTransferId {0x0200}; + uint16_t fsVersion = {0x0004}; + + static constexpr ble_uuid128_t fsServiceUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x72, 0x65, 0x66, 0x73, 0x6e, 0x61, 0x72, 0x54, 0x65, 0x6c, 0x69, 0x46, 0xBB, 0xFE, 0xAF, 0xAD}}; + + static constexpr ble_uuid128_t fsVersionUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x72, 0x65, 0x66, 0x73, 0x6e, 0x61, 0x72, 0x54, 0x65, 0x6c, 0x69, 0x46, 0x00, 0x01, 0xAF, 0xAD}}; + + static constexpr ble_uuid128_t fsTransferUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x72, 0x65, 0x66, 0x73, 0x6e, 0x61, 0x72, 0x54, 0x65, 0x6c, 0x69, 0x46, 0x00, 0x02, 0xAF, 0xAD}}; + + struct ble_gatt_chr_def characteristicDefinition[3]; + struct ble_gatt_svc_def serviceDefinition[2]; + uint16_t versionCharacteristicHandle; + + enum class commands { + INVALID = 0x00, + READ = 0x10, + READ_DATA = 0x11, + READ_PACING = 0x12, + WRITE = 0x20, + WRITE_PACING = 0x21, + WRITE_DATA = 0x22, + DELETE = 0x30, + DELETE_STATUS = 0x31, + MKDIR = 0x40, + MKDIR_STATUS = 0x41, + LISTDIR = 0x50, + LISTDIR_ENTRY = 0x51, + MOVE = 0x60, + MOVE_STATUS = 0x61, + } + }; + } +} diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index acf4f94b90..b5eb46b87e 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -30,7 +30,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Drivers::SpiNorFlash& spiNorFlash, Controllers::HeartRateController& heartRateController, Controllers::MotionController& motionController, - Pinetime::Controllers::FS& fs) + Controllers::FS& fs) : systemTask {systemTask}, bleController {bleController}, dateTimeController {dateTimeController}, @@ -49,7 +49,9 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, batteryInformationService {batteryController}, immediateAlertService {systemTask, notificationManager}, heartRateService {systemTask, heartRateController}, - motionService {systemTask, motionController}, + motionService{systemTask, motionController}, + fs {fs}, + fsService {fs}, serviceDiscovery({¤tTimeClient, &alertNotificationClient}) { } @@ -97,7 +99,8 @@ void NimbleController::Init() { immediateAlertService.Init(); heartRateService.Init(); motionService.Init(); - + fsService.Init(); + int rc; rc = ble_hs_util_ensure_addr(0); ASSERT(rc == 0); diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index 12bd6924b5..14749b8d3f 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -22,6 +22,7 @@ #include "components/ble/MotionService.h" #include "components/ble/weather/WeatherService.h" #include "components/fs/FS.h" +#include "components/ble/FSService.h" namespace Pinetime { namespace Drivers { @@ -110,6 +111,8 @@ namespace Pinetime { HeartRateService heartRateService; MotionService motionService; ServiceDiscovery serviceDiscovery; + FS fs; + FSService fsService; uint8_t addrType; uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE; From 91c644b43c250b0a03047349182828df31ddcbd2 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 16:04:23 +0000 Subject: [PATCH 02/26] 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(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 6551c20c93..2cfd5ccd29 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 85b484a36f..eb4b34d7b4 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 1cad4f0201..f287c28e91 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 75ba16c813..28d28d3c9e 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); From 1dd71744802b4ae80c8952f73bbf0051fbe12cf3 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 20:48:34 +0000 Subject: [PATCH 03/26] 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(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 2cfd5ccd29..2f02cd965b 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 f287c28e91..353193dc76 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 28d28d3c9e..ccff24091b 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); From 2690c274af72cfbac88a8c83fce311665a917a93 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 21:33:01 +0000 Subject: [PATCH 04/26] Workaround for SPI bus being asleep. This needs to get cherrypicked to another PR as SPI Sleep needs to use a semaphore or something --- src/drivers/SpiMaster.cpp | 16 +++++++++++++--- src/drivers/SpiMaster.h | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp index 747dbc84a4..4492e899cc 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -10,7 +10,7 @@ SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters } bool SpiMaster::Init() { - if(mutex == nullptr) { + if (mutex == nullptr) { mutex = xSemaphoreCreateBinary(); ASSERT(mutex != nullptr); } @@ -179,6 +179,10 @@ void SpiMaster::PrepareRx(const volatile uint32_t cmdAddress, bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) { if (data == nullptr) return false; + + if (!active) { + Wakeup(); + } auto ok = xSemaphoreTake(mutex, portMAX_DELAY); ASSERT(ok == true); taskToNotify = xTaskGetCurrentTaskHandle(); @@ -215,7 +219,9 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) { bool SpiMaster::Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) { xSemaphoreTake(mutex, portMAX_DELAY); - + if (!active) { + Wakeup(); + } taskToNotify = nullptr; this->pinCsn = pinCsn; @@ -253,12 +259,16 @@ void SpiMaster::Sleep() { nrf_gpio_cfg_default(params.pinSCK); nrf_gpio_cfg_default(params.pinMOSI); nrf_gpio_cfg_default(params.pinMISO); - + active = false; NRF_LOG_INFO("[SPIMASTER] sleep") } void SpiMaster::Wakeup() { + if (active) { + return; + } Init(); + active = true; NRF_LOG_INFO("[SPIMASTER] Wakeup"); } diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index 5ea624f255..f5d2b1250b 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -60,6 +60,8 @@ namespace Pinetime { volatile size_t currentBufferSize = 0; volatile TaskHandle_t taskToNotify; SemaphoreHandle_t mutex = nullptr; + + bool active; }; } } From eabbbfa373de04d1dc694618d5a9d475f0918592 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 22:07:43 +0000 Subject: [PATCH 05/26] Fix folder count in output --- src/components/ble/FSService.cpp | 4 +-- src/components/ble/FSService.h | 56 ++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 2f02cd965b..40679e5c99 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -78,7 +78,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { 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); @@ -115,7 +115,6 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.entry++; } fs.DirClose(&dir); - resp.entry++; resp.file_size = 0; resp.path_length = 0; resp.flags = 0; @@ -123,6 +122,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { 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; } } return 0; diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h index eb4b34d7b4..a1c42aa789 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -44,23 +44,8 @@ namespace Pinetime { 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; + + int FSCommandHandler(uint16_t connectionHandle, os_mbuf* om); enum class commands : uint8_t { INVALID = 0x00, @@ -80,7 +65,42 @@ namespace Pinetime { MOVE_STATUS = 0x61 }; - int FSCommandHandler(uint16_t connectionHandle, os_mbuf* om); + using ListDirHeader = struct __attribute__((packed)) { + uint8_t command; + uint8_t padding; + uint16_t pathlen; + char pathstr[70]; + }; + + using ListDirResponse = 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]; + }; + + using MKDirHeader = struct __attribute__((packed)) { + uint8_t command; + uint8_t padding; + uint16_t pathlen; + uint32_t padding2; + uint64_t time; + char pathstr[70]; + }; + + using MKDirResponse = struct __attribute__((packed)) { + uint8_t command; + uint8_t status; + uint32_t padding1; + uint16_t padding2; + uint64_t modification_time; + }; + }; } } From 3a8e66a52fcc6317a7dffa15cb161f37a645d36c Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 23:07:43 +0000 Subject: [PATCH 06/26] 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(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 40679e5c99..0cf2b937a8 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 a1c42aa789..3ca6d934e1 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 353193dc76..d30b7373cd 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 ccff24091b..e50ff10a20 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: From 1b4b422ab6d7588e21e58fcf1d2ba04470abc611 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Mon, 18 Oct 2021 04:20:11 +0000 Subject: [PATCH 07/26] More attempted SPI fixes --- src/drivers/Spi.cpp | 14 ++++++++++++++ src/drivers/Spi.h | 1 + 2 files changed, 15 insertions(+) diff --git a/src/drivers/Spi.cpp b/src/drivers/Spi.cpp index e477622b6f..46552437cb 100644 --- a/src/drivers/Spi.cpp +++ b/src/drivers/Spi.cpp @@ -10,14 +10,24 @@ Spi::Spi(SpiMaster& spiMaster, uint8_t pinCsn) : spiMaster {spiMaster}, pinCsn { } bool Spi::Write(const uint8_t* data, size_t size) { + if(!active){ + Wakeup(); + } return spiMaster.Write(pinCsn, data, size); } bool Spi::Read(uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) { + if(!active){ + Wakeup(); + } return spiMaster.Read(pinCsn, cmd, cmdSize, data, dataSize); } void Spi::Sleep() { + if(!active){ + return; + } + active = false; nrf_gpio_cfg_default(pinCsn); NRF_LOG_INFO("[SPI] Sleep") } @@ -32,7 +42,11 @@ bool Spi::Init() { } void Spi::Wakeup() { + if(active){ + return; + } nrf_gpio_cfg_output(pinCsn); nrf_gpio_pin_set(pinCsn); + active=true; NRF_LOG_INFO("[SPI] Wakeup") } diff --git a/src/drivers/Spi.h b/src/drivers/Spi.h index 9b6a30f43b..51de2b34de 100644 --- a/src/drivers/Spi.h +++ b/src/drivers/Spi.h @@ -23,6 +23,7 @@ namespace Pinetime { private: SpiMaster& spiMaster; uint8_t pinCsn; + bool active; }; } } From 8f6a390c36007eecb875bca6d8be860dbec6bd9c Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Mon, 18 Oct 2021 04:20:26 +0000 Subject: [PATCH 08/26] Start of read command --- src/components/ble/FSService.cpp | 121 +++++++++++++++++++++++++++++-- src/components/ble/FSService.h | 56 +++++++++++--- 2 files changed, 158 insertions(+), 19 deletions(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 0cf2b937a8..52cfafb8c6 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -4,7 +4,7 @@ using namespace Pinetime::Controllers; -constexpr ble_uuid128_t FSService::fsServiceUuid; +constexpr ble_uuid16_t FSService::fsServiceUuid; constexpr ble_uuid128_t FSService::fsVersionUuid; constexpr ble_uuid128_t FSService::fsTransferUuid; @@ -63,9 +63,90 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { NRF_LOG_INFO("[FS_S] -> FSCommandHandler"); switch (command) { + case commands::READ: { + NRF_LOG_INFO("[FS_S] -> Read"); + if (state != FSState::IDLE) { + return -1; + } + state = FSState::READ; + auto* header = (ReadHeader*) om->om_data; + uint16_t plen = header->pathlen; + if (plen > maxpathlen - 1) { + return -1; + } + memcpy(filepath, header->pathstr, plen); + filepath[plen + 1] = 0; // Copy and null teminate string + ReadResponse resp; + resp.command = commands::READ_DATA; + resp.chunkoff = header->chunkoff; + resp.status = 0x01; + struct lfs_info info = {}; + int res = fs.Stat(filepath, &info); + if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) { + resp.status = 0x03; + resp.chunklen = 0; + resp.totallen = 0; + } else { + lfs_file f; + resp.chunklen = std::min(header->chunksize, info.size); + resp.totallen = info.size; + fs.FileOpen(&f, filepath, LFS_O_RDONLY); + fs.FileSeek(&f, header->chunkoff); + resp.chunklen = fs.FileRead(&f, resp.chunk, resp.chunklen); + fs.FileClose(&f); + } + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse)); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + if (header->chunksize >= resp.totallen) { // probably removeable...but then usafe + state = FSState::IDLE; + } + } + case commands::READ_PACING: { + NRF_LOG_INFO("[FS_S] -> ReadPacing"); + if (state != FSState::READ) { + return -1; + } + auto* header = (ReadPacing*) om->om_data; + ReadResponse resp = {}; + + resp.command = commands::READ_DATA; + resp.chunkoff = header->chunkoff; + resp.status = 0x01; + struct lfs_info info = {}; + int res = fs.Stat(filepath, &info); + if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) { + resp.status = 0x03; + resp.chunklen = 0; + resp.totallen = 0; + } else { + lfs_file f; + resp.chunklen = std::min(header->chunksize, info.size); + resp.totallen = info.size; + fs.FileOpen(&f, filepath, LFS_O_RDONLY); + fs.FileSeek(&f, header->chunkoff); + resp.chunklen = fs.FileRead(&f, resp.chunk, resp.chunklen); + fs.FileClose(&f); + } + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse)); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + if (resp.chunklen >= header->chunksize) { // is this right? + state = FSState::IDLE; + } + } + case commands::WRITE: { + if (state != FSState::IDLE) { + return -1; + } + } + case commands::WRITE_PACING: { + if (state != FSState::WRITE) { + return -1; + } + } + case commands::DELETE: { NRF_LOG_INFO("[FS_S] -> Delete"); - auto* header = (DelHeader*)om->om_data; + auto* header = (DelHeader*) om->om_data; uint16_t plen = header->pathlen; char path[plen + 1] = {0}; struct lfs_info info = {}; @@ -116,7 +197,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { } case commands::LISTDIR: { NRF_LOG_INFO("[FS_S] -> ListDir"); - ListDirHeader* header = (ListDirHeader*)om->om_data; + ListDirHeader* header = (ListDirHeader*) om->om_data; uint16_t plen = header->pathlen; char path[plen + 1] = {0}; memcpy(path, header->pathstr, plen); @@ -140,7 +221,11 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { fs.DirRewind(&dir); - while (fs.DirRead(&dir, &info)) { + while (true) { + int res = fs.DirRead(&dir, &info); + if(res <= 0){ + break; + } switch (info.type) { case LFS_TYPE_REG: { resp.flags = 0; @@ -157,9 +242,9 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { 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)); + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)+resp.path_length); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); - vTaskDelay(1); // Allow stuff to actually go out over the BLE conn + vTaskDelay(5); // Allow stuff to actually go out over the BLE conn resp.entry++; } fs.DirClose(&dir); @@ -167,7 +252,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.path_length = 0; resp.flags = 0; // TODO Handle Size of response better. - auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse) - 70 + resp.path_length); + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)+resp.path_length); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); NRF_LOG_INFO("[FS_S] -> done "); break; @@ -175,3 +260,25 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { } return 0; } +// Loads resp with file data given a valid filepath header and resp +void FSService::prepareReadDataResp(ReadHeader* header, ReadResponse* resp) { + uint16_t plen = header->pathlen; + resp->command = commands::READ_DATA; + resp->chunkoff = header->chunkoff; + resp->status = 0x01; + struct lfs_info info = {}; + int res = fs.Stat(filepath, &info); + if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) { + resp->status = 0x03; + resp->chunklen = 0; + resp->totallen = 0; + } else { + lfs_file f; + resp->chunklen = std::min(header->chunksize, info.size); + resp->totallen = info.size; + fs.FileOpen(&f, filepath, LFS_O_RDONLY); + fs.FileSeek(&f, header->chunkoff); + resp->chunklen = fs.FileRead(&f, resp->chunk, resp->chunklen); + fs.FileClose(&f); + } +} diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h index 3ca6d934e1..93205e548a 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -27,10 +27,10 @@ namespace Pinetime { static constexpr uint16_t fsVersionId {0x0100}; static constexpr uint16_t fsTransferId {0x0200}; uint16_t fsVersion = {0x0004}; - - static constexpr ble_uuid128_t fsServiceUuid { - .u {.type = BLE_UUID_TYPE_128}, - .value = {0x72, 0x65, 0x66, 0x73, 0x6e, 0x61, 0x72, 0x54, 0x65, 0x6c, 0x69, 0x46, 0xBB, 0xFE, 0xAF, 0xAD}}; + static constexpr uint8_t maxpathlen = 100; + static constexpr ble_uuid16_t fsServiceUuid { + .u {.type = BLE_UUID_TYPE_16}, + .value = {0xFEBB}};// {0x72, 0x65, 0x66, 0x73, 0x6e, 0x61, 0x72, 0x54, 0x65, 0x6c, 0x69, 0x46, 0xBB, 0xFE, 0xAF, 0xAD}}; static constexpr ble_uuid128_t fsVersionUuid { .u {.type = BLE_UUID_TYPE_128}, @@ -45,8 +45,6 @@ namespace Pinetime { uint16_t versionCharacteristicHandle; uint16_t transferCharacteristicHandle; - int FSCommandHandler(uint16_t connectionHandle, os_mbuf* om); - enum class commands : uint8_t { INVALID = 0x00, READ = 0x10, @@ -64,12 +62,44 @@ namespace Pinetime { MOVE = 0x60, MOVE_STATUS = 0x61 }; + enum class FSState : uint8_t { + IDLE = 0x00, + READ = 0x01, + WRITE = 0x02, + }; + FSState state; + char filepath[maxpathlen]; // TODO ..ugh fixed filepath len + using ReadHeader = struct __attribute__((packed)) { + commands command; + uint8_t padding; + uint16_t pathlen; + uint32_t chunkoff; + uint32_t chunksize; + char pathstr[]; + }; + + using ReadResponse = struct __attribute__((packed)) { + commands command; + uint8_t status; + uint16_t padding; + uint32_t chunkoff; + uint32_t totallen; + uint32_t chunklen; + uint8_t chunk[]; + }; + using ReadPacing = struct __attribute__((packed)) { + commands command; + uint8_t status; + uint16_t padding; + uint32_t chunkoff; + uint32_t chunksize; + }; using ListDirHeader = struct __attribute__((packed)) { commands command; uint8_t padding; uint16_t pathlen; - char pathstr[70]; + char pathstr[]; }; using ListDirResponse = struct __attribute__((packed)) { @@ -81,7 +111,7 @@ namespace Pinetime { uint32_t flags; uint32_t modification_time; uint32_t file_size; - char path[70]; + char path[]; }; using MKDirHeader = struct __attribute__((packed)) { @@ -90,7 +120,7 @@ namespace Pinetime { uint16_t pathlen; uint32_t padding2; uint64_t time; - char pathstr[70]; + char pathstr[]; }; using MKDirResponse = struct __attribute__((packed)) { @@ -100,19 +130,21 @@ namespace Pinetime { uint16_t padding2; uint64_t modification_time; }; - + using DelHeader = struct __attribute__((packed)) { commands command; uint8_t padding; uint16_t pathlen; - char pathstr[70]; + char pathstr[]; }; using DelResponse = struct __attribute__((packed)) { commands command; uint8_t status; }; - + + int FSCommandHandler(uint16_t connectionHandle, os_mbuf* om); + void prepareReadDataResp(ReadHeader *header, ReadResponse *resp); }; } } From d89e38d3bf9e84e69635f0cb10cc42f0071fa038 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 19 Oct 2021 19:03:00 +0000 Subject: [PATCH 09/26] 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(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 52cfafb8c6..0a1fabb7bb 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 93205e548a..114c1e0dbb 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 d30b7373cd..c8a5a2ebbd 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 e50ff10a20..1aa8d5f1c2 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); - }; } } From a5a64800eda02358aebe4d0f23d43ee33f363c56 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 19 Oct 2021 19:15:41 +0000 Subject: [PATCH 10/26] Revert "Workaround for SPI bus being asleep." This reverts commit 1edeb5cb65489707c26b7a65f3b54520086d363a. --- src/drivers/SpiMaster.cpp | 16 +++------------- src/drivers/SpiMaster.h | 2 -- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp index 4492e899cc..747dbc84a4 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -10,7 +10,7 @@ SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters } bool SpiMaster::Init() { - if (mutex == nullptr) { + if(mutex == nullptr) { mutex = xSemaphoreCreateBinary(); ASSERT(mutex != nullptr); } @@ -179,10 +179,6 @@ void SpiMaster::PrepareRx(const volatile uint32_t cmdAddress, bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) { if (data == nullptr) return false; - - if (!active) { - Wakeup(); - } auto ok = xSemaphoreTake(mutex, portMAX_DELAY); ASSERT(ok == true); taskToNotify = xTaskGetCurrentTaskHandle(); @@ -219,9 +215,7 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) { bool SpiMaster::Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) { xSemaphoreTake(mutex, portMAX_DELAY); - if (!active) { - Wakeup(); - } + taskToNotify = nullptr; this->pinCsn = pinCsn; @@ -259,16 +253,12 @@ void SpiMaster::Sleep() { nrf_gpio_cfg_default(params.pinSCK); nrf_gpio_cfg_default(params.pinMOSI); nrf_gpio_cfg_default(params.pinMISO); - active = false; + NRF_LOG_INFO("[SPIMASTER] sleep") } void SpiMaster::Wakeup() { - if (active) { - return; - } Init(); - active = true; NRF_LOG_INFO("[SPIMASTER] Wakeup"); } diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index f5d2b1250b..5ea624f255 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -60,8 +60,6 @@ namespace Pinetime { volatile size_t currentBufferSize = 0; volatile TaskHandle_t taskToNotify; SemaphoreHandle_t mutex = nullptr; - - bool active; }; } } From f841b8c98498a01211036fd0720a1f8c949e28f2 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 19 Oct 2021 19:15:48 +0000 Subject: [PATCH 11/26] Revert "More attempted SPI fixes" This reverts commit bed7e731b206961117b717adb1031ab3011e7db9. --- src/drivers/Spi.cpp | 14 -------------- src/drivers/Spi.h | 1 - 2 files changed, 15 deletions(-) diff --git a/src/drivers/Spi.cpp b/src/drivers/Spi.cpp index 46552437cb..e477622b6f 100644 --- a/src/drivers/Spi.cpp +++ b/src/drivers/Spi.cpp @@ -10,24 +10,14 @@ Spi::Spi(SpiMaster& spiMaster, uint8_t pinCsn) : spiMaster {spiMaster}, pinCsn { } bool Spi::Write(const uint8_t* data, size_t size) { - if(!active){ - Wakeup(); - } return spiMaster.Write(pinCsn, data, size); } bool Spi::Read(uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) { - if(!active){ - Wakeup(); - } return spiMaster.Read(pinCsn, cmd, cmdSize, data, dataSize); } void Spi::Sleep() { - if(!active){ - return; - } - active = false; nrf_gpio_cfg_default(pinCsn); NRF_LOG_INFO("[SPI] Sleep") } @@ -42,11 +32,7 @@ bool Spi::Init() { } void Spi::Wakeup() { - if(active){ - return; - } nrf_gpio_cfg_output(pinCsn); nrf_gpio_pin_set(pinCsn); - active=true; NRF_LOG_INFO("[SPI] Wakeup") } diff --git a/src/drivers/Spi.h b/src/drivers/Spi.h index 51de2b34de..9b6a30f43b 100644 --- a/src/drivers/Spi.h +++ b/src/drivers/Spi.h @@ -23,7 +23,6 @@ namespace Pinetime { private: SpiMaster& spiMaster; uint8_t pinCsn; - bool active; }; } } From 6393a17d7402b92e00cd748bc7e901ba053135de Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Wed, 20 Oct 2021 01:30:04 +0000 Subject: [PATCH 12/26] List Dir works? --- src/components/ble/FSService.cpp | 41 +++++++++++++++++-------- src/components/ble/FSService.h | 8 +++-- src/components/ble/NimbleController.cpp | 2 +- src/systemtask/Messages.h | 2 ++ src/systemtask/SystemTask.cpp | 13 ++++++++ 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 0a1fabb7bb..cd2cc07ad7 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -1,6 +1,7 @@ #include #include "FSService.h" #include "components/ble/BleController.h" +#include "systemtask/SystemTask.h" using namespace Pinetime::Controllers; @@ -13,8 +14,9 @@ int FSServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gat return fsService->OnFSServiceRequested(conn_handle, attr_handle, ctxt); } -FSService::FSService(Pinetime::Controllers::FS& fs) - : fs {fs}, +FSService::FSService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::FS& fs) + : systemTask {systemTask}, + fs {fs}, characteristicDefinition {{.uuid = &fsVersionUuid.u, .access_cb = FSServiceCallback, .arg = this, @@ -60,8 +62,13 @@ 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 %d",command); - fs.Mount(); + NRF_LOG_INFO("[FS_S] -> FSCommandHandler Command %d", command); + // Just always make sure we are awake... + systemTask.PushMessage(Pinetime::System::Messages::StartFileTransfer); + vTaskDelay(10); + while (systemTask.IsSleeping()) { + vTaskDelay(100); // 50ms + } switch (command) { /* case commands::READ: { @@ -203,6 +210,7 @@ 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 = {}; @@ -212,9 +220,11 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.totalentries = 0; resp.entry = 0; - int res = fs.DirOpen(path, &dir); + if (fs.DirOpen(path, &dir)) { + return 0; + } - NRF_LOG_INFO("[FS_S] ->diropen %d ", res); + // NRF_LOG_INFO("[FS_S] ->diropen %d ", res); while (fs.DirRead(&dir, &info)) { resp.totalentries++; } @@ -222,9 +232,11 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { fs.DirRewind(&dir); - while (true) { + // NRF_LOG_INFO("[FS_S] ->diropen %d ", res); + + while (resp.entry < resp.totalentries) { int res = fs.DirRead(&dir, &info); - if(res <= 0){ + if (res <= 0) { break; } switch (info.type) { @@ -243,23 +255,26 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { 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)+resp.path_length); + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse) + resp.path_length); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); - vTaskDelay(10); // Allow stuff to actually go out over the BLE conn + vTaskDelay(100); // Allow stuff to actually go out over the BLE conn resp.entry++; } - fs.DirClose(&dir); + + if (fs.DirClose(&dir)) { + return 0; + } resp.file_size = 0; resp.path_length = 0; resp.flags = 0; // TODO Handle Size of response better. - auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)+resp.path_length); + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse) + resp.path_length); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); NRF_LOG_INFO("[FS_S] -> done "); break; } } - fs.UnMount(); + systemTask.PushMessage(Pinetime::System::Messages::StopFileTransfer); 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 114c1e0dbb..69ed094bc7 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -15,13 +15,15 @@ namespace Pinetime { class Ble; class FSService { public: - FSService(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); void NotifyFSRaw(uint16_t connectionHandle); private: + Pinetime::System::SystemTask& systemTask; Pinetime::Controllers::FS& fs; static constexpr uint16_t FSServiceId {0xFEBB}; static constexpr uint16_t fsVersionId {0x0100}; @@ -30,7 +32,7 @@ namespace Pinetime { static constexpr uint8_t maxpathlen = 100; static constexpr ble_uuid16_t fsServiceUuid { .u {.type = BLE_UUID_TYPE_16}, - .value = {0xFEBB}};// {0x72, 0x65, 0x66, 0x73, 0x6e, 0x61, 0x72, 0x54, 0x65, 0x6c, 0x69, 0x46, 0xBB, 0xFE, 0xAF, 0xAD}}; + .value = {0xFEBB}}; // {0x72, 0x65, 0x66, 0x73, 0x6e, 0x61, 0x72, 0x54, 0x65, 0x6c, 0x69, 0x46, 0xBB, 0xFE, 0xAF, 0xAD}}; static constexpr ble_uuid128_t fsVersionUuid { .u {.type = BLE_UUID_TYPE_128}, @@ -144,7 +146,7 @@ namespace Pinetime { }; int FSCommandHandler(uint16_t connectionHandle, os_mbuf* om); - void prepareReadDataResp(ReadHeader *header, ReadResponse *resp); + void prepareReadDataResp(ReadHeader* header, ReadResponse* resp); }; } } diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index b5eb46b87e..01230661b5 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -51,7 +51,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, heartRateService {systemTask, heartRateController}, motionService{systemTask, motionController}, fs {fs}, - fsService {fs}, + fsService {systemTask,fs}, serviceDiscovery({¤tTimeClient, &alertNotificationClient}) { } diff --git a/src/systemtask/Messages.h b/src/systemtask/Messages.h index 516f6462e4..cc30fdc682 100644 --- a/src/systemtask/Messages.h +++ b/src/systemtask/Messages.h @@ -27,6 +27,8 @@ namespace Pinetime { StopRinging, MeasureBatteryTimerExpired, BatteryPercentageUpdated, + StartFileTransfer, + StopFileTransfer, }; } } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 28f812432a..a95d479dec 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -342,6 +342,19 @@ void SystemTask::Work() { doNotGoToSleep = false; xTimerStart(dimTimer, 0); break; + case Messages::StartFileTransfer: + NRF_LOG_INFO("[systemtask] FS Started"); + doNotGoToSleep = true; + if (isSleeping && !isWakingUp) + GoToRunning(); + //TODO add intent of fs access icon or something + break; + case Messages::StopFileTransfer: + NRF_LOG_INFO("[systemtask] FS Stopped"); + doNotGoToSleep = false; + xTimerStart(dimTimer, 0); + //TODO add intent of fs access icon or something + break; case Messages::OnTouchEvent: if (touchHandler.GetNewTouchInfo()) { touchHandler.UpdateLvglTouchPoint(); From faa05eb57b7d6214e53d0b147a796793496a89ae Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Thu, 21 Oct 2021 04:02:50 +0000 Subject: [PATCH 13/26] Actually fix memory corruption, seems stable now ListDir MKDIR delete all seem to work Co-authored-by: Iambian --- src/components/ble/FSService.cpp | 103 ++++++++++++------------------- src/components/ble/FSService.h | 5 +- 2 files changed, 43 insertions(+), 65 deletions(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index cd2cc07ad7..0ba3e102a8 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -69,8 +69,9 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { while (systemTask.IsSleeping()) { vTaskDelay(100); // 50ms } + lfs_dir_t dir; + lfs_info info; switch (command) { - /* case commands::READ: { NRF_LOG_INFO("[FS_S] -> Read"); if (state != FSState::IDLE) { @@ -151,31 +152,15 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { return -1; } } - 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 = {}; + char path[plen + 1] {0}; + memcpy(path, header->pathstr, plen); + 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; - } + resp.status = fs.FileDelete(path) ? 0x02 : 0x01; auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(DelResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; @@ -184,57 +169,43 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { NRF_LOG_INFO("[FS_S] -> MKDir"); auto* header = (MKDirHeader*) 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] -> MKDIR %.10s", path); - MKDirResponse resp = {}; + 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 + resp.modification_time = 0; + resp.status = fs.DirCreate(path) ? 0x02 : 0x01; 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; 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 {}; - ListDirResponse resp = {}; resp.command = commands::LISTDIR_ENTRY; - resp.status = 1; // TODO actually use res above! + resp.status = 1; resp.totalentries = 0; resp.entry = 0; - - if (fs.DirOpen(path, &dir)) { - return 0; - } - - // NRF_LOG_INFO("[FS_S] ->diropen %d ", res); + resp.modification_time = 0; // TODO Does LFS actually support TS? + 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++; } - NRF_LOG_INFO("[FS_S] -> %d ", resp.totalentries); - fs.DirRewind(&dir); - - // NRF_LOG_INFO("[FS_S] ->diropen %d ", res); - - while (resp.entry < resp.totalentries) { + while (true) { int res = fs.DirRead(&dir, &info); if (res <= 0) { break; @@ -251,35 +222,39 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { 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) + resp.path_length); + NRF_LOG_INFO("[FS_S] -> DIR %.10s", path); + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)); + os_mbuf_append(om,info.name,resp.path_length); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + /* + * Todo Figure out how to know when the previous Notify was TX'd + * For now just delay 100ms to make sure that the data went out... + */ vTaskDelay(100); // Allow stuff to actually go out over the BLE conn resp.entry++; } - - if (fs.DirClose(&dir)) { - return 0; - } + assert(fs.DirClose(&dir) == 0); resp.file_size = 0; resp.path_length = 0; resp.flags = 0; - // TODO Handle Size of response better. - auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse) + resp.path_length); + auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); - NRF_LOG_INFO("[FS_S] -> done "); break; } + default: + break; } + NRF_LOG_INFO("[FS_S] -> done "); systemTask.PushMessage(Pinetime::System::Messages::StopFileTransfer); return 0; } + // Loads resp with file data given a valid filepath header and resp void FSService::prepareReadDataResp(ReadHeader* header, ReadResponse* resp) { - uint16_t plen = header->pathlen; + // uint16_t plen = header->pathlen; resp->command = commands::READ_DATA; resp->chunkoff = header->chunkoff; resp->status = 0x01; diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h index 69ed094bc7..e9c98fb481 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -29,7 +29,7 @@ namespace Pinetime { static constexpr uint16_t fsVersionId {0x0100}; static constexpr uint16_t fsTransferId {0x0200}; uint16_t fsVersion = {0x0004}; - static constexpr uint8_t maxpathlen = 100; + static constexpr uint16_t maxpathlen = 256; static constexpr ble_uuid16_t fsServiceUuid { .u {.type = BLE_UUID_TYPE_16}, .value = {0xFEBB}}; // {0x72, 0x65, 0x66, 0x73, 0x6e, 0x61, 0x72, 0x54, 0x65, 0x6c, 0x69, 0x46, 0xBB, 0xFE, 0xAF, 0xAD}}; @@ -47,6 +47,9 @@ namespace Pinetime { uint16_t versionCharacteristicHandle; uint16_t transferCharacteristicHandle; + // lfs_dir_t dir; + // lfs_info info; + enum class commands : uint8_t { INVALID = 0x00, READ = 0x10, From 8fb99471c38c2efd7af88d4888c5792bdd8deafb Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Fri, 22 Oct 2021 03:34:23 +0000 Subject: [PATCH 14/26] Reading Seems to work? --- src/components/ble/FSService.cpp | 78 ++++++++++++++++---------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 0ba3e102a8..68fd5ea659 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -73,74 +73,76 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { lfs_info info; switch (command) { case commands::READ: { + lfs_file f; NRF_LOG_INFO("[FS_S] -> Read"); - if (state != FSState::IDLE) { - return -1; - } - state = FSState::READ; auto* header = (ReadHeader*) om->om_data; uint16_t plen = header->pathlen; - if (plen > maxpathlen - 1) { + if (plen > maxpathlen) { //> counts for null term return -1; } memcpy(filepath, header->pathstr, plen); filepath[plen + 1] = 0; // Copy and null teminate string ReadResponse resp; resp.command = commands::READ_DATA; - resp.chunkoff = header->chunkoff; resp.status = 0x01; - struct lfs_info info = {}; + resp.chunkoff = header->chunkoff; int res = fs.Stat(filepath, &info); if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) { resp.status = 0x03; resp.chunklen = 0; resp.totallen = 0; } else { - lfs_file f; - resp.chunklen = std::min(header->chunksize, info.size); + 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); - resp.chunklen = fs.FileRead(&f, resp.chunk, resp.chunklen); - fs.FileClose(&f); } - auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse)); - ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); - if (header->chunksize >= resp.totallen) { // probably removeable...but then usafe - state = FSState::IDLE; + 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); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + break; } case commands::READ_PACING: { - NRF_LOG_INFO("[FS_S] -> ReadPacing"); - if (state != FSState::READ) { - return -1; - } - auto* header = (ReadPacing*) om->om_data; - ReadResponse resp = {}; - + lfs_file f; + NRF_LOG_INFO("[FS_S] -> Readpacing"); + auto* header = (ReadHeader*) om->om_data; + ReadResponse resp; resp.command = commands::READ_DATA; - resp.chunkoff = header->chunkoff; resp.status = 0x01; - struct lfs_info info = {}; + resp.chunkoff = header->chunkoff; int res = fs.Stat(filepath, &info); if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) { resp.status = 0x03; resp.chunklen = 0; resp.totallen = 0; } else { - lfs_file f; - resp.chunklen = std::min(header->chunksize, info.size); + 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); - resp.chunklen = fs.FileRead(&f, resp.chunk, resp.chunklen); - fs.FileClose(&f); } - auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse)); - ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); - if (resp.chunklen >= header->chunksize) { // is this right? - state = FSState::IDLE; + 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); + ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); + break; } case commands::WRITE: { if (state != FSState::IDLE) { @@ -185,7 +187,6 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { uint16_t plen = header->pathlen; char path[plen + 1] {0}; memcpy(path, header->pathstr, plen); - NRF_LOG_INFO("[FS_S] -> DIR %.10s", path); ListDirResponse resp {}; @@ -223,16 +224,15 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { } } - //strcpy(resp.path, info.name); + // strcpy(resp.path, info.name); resp.path_length = strlen(info.name); - NRF_LOG_INFO("[FS_S] -> DIR %.10s", path); auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)); - os_mbuf_append(om,info.name,resp.path_length); + os_mbuf_append(om, info.name, resp.path_length); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); /* - * Todo Figure out how to know when the previous Notify was TX'd - * For now just delay 100ms to make sure that the data went out... - */ + * Todo Figure out how to know when the previous Notify was TX'd + * For now just delay 100ms to make sure that the data went out... + */ vTaskDelay(100); // Allow stuff to actually go out over the BLE conn resp.entry++; } From c1aa5a5ea7d5ecde63a786827a866312c04507f9 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Mon, 25 Oct 2021 03:02:02 +0000 Subject: [PATCH 15/26] 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 68fd5ea659..c784a8c4a3 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 e9c98fb481..17f52eb08b 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 c8a5a2ebbd..297706feca 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 1aa8d5f1c2..e4df956618 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; From 2e10b0fe645f74d0ab057ac11b96f4eb3fffc4ae Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 26 Oct 2021 00:10:39 +0000 Subject: [PATCH 16/26] 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(-) diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index 297706feca..8d82c39da7 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 e4df956618..60dd8e5163 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; From f4322841ffc011ea5c541e40e6aa73ab11ebd988 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 26 Oct 2021 00:48:25 +0000 Subject: [PATCH 17/26] 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(-) diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp index 8d82c39da7..79fb2222a2 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 60dd8e5163..da3bd27366 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; } From 8f46908d387cc4fe3f911bc0ca517719238418ed Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 26 Oct 2021 03:42:34 +0000 Subject: [PATCH 18/26] 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(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index c784a8c4a3..0230ea15c0 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 79fb2222a2..78b0f5cb40 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) { From 362a5ef1136399c178cf881f7569e3d291432ec1 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 16 Nov 2021 04:32:53 +0000 Subject: [PATCH 19/26] 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 0230ea15c0..1082d24cd6 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 17f52eb08b..828925a86f 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 78b0f5cb40..8c98ae344a 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 da3bd27366..2b27ae5d3f 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(); From 09b9130244a7d5e448612d4a9a70988c2610d686 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sat, 20 Nov 2021 04:57:02 +0000 Subject: [PATCH 20/26] Fix string nullterminations, Expand error codes. --- src/components/ble/FSService.cpp | 36 ++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 1082d24cd6..e1d5bae05f 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -81,7 +81,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { return -1; } memcpy(filepath, header->pathstr, plen); - filepath[plen + 1] = 0; // Copy and null teminate string + filepath[plen] = 0; // Copy and null teminate string ReadResponse resp; os_mbuf* om; resp.command = commands::READ_DATA; @@ -150,14 +150,14 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { return -1; } memcpy(filepath, header->pathstr, plen); - filepath[plen + 1] = 0; // Copy and null teminate string + filepath[plen] = 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_WRONLY | LFS_O_CREAT); - resp.status = res ? 0x02 : 0x01; + resp.status = (res==0) ? 0x01: (int8_t)res; 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)); @@ -171,11 +171,14 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { 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; + resp.status = 1; + fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT); fs.FileSeek(&f, header->offset); - fs.FileWrite(&f, header->data, header->dataSize); + int res = fs.FileWrite(&f, header->data, header->dataSize); fs.FileClose(&f); + if(res < 0 ){ + resp.status = (int8_t)res; + } 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); @@ -187,9 +190,11 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { uint16_t plen = header->pathlen; char path[plen + 1] {0}; memcpy(path, header->pathstr, plen); + path[plen] = 0; // Copy and null teminate string DelResponse resp {}; resp.command = commands::DELETE_STATUS; - resp.status = fs.FileDelete(path) ? 0x02 : 0x01; + int res = fs.FileDelete(path); + resp.status = (res==0) ? 0x01 : (int8_t)res; auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(DelResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; @@ -200,10 +205,12 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { uint16_t plen = header->pathlen; char path[plen + 1] {0}; memcpy(path, header->pathstr, plen); + path[plen] = 0; // Copy and null teminate string MKDirResponse resp {}; resp.command = commands::MKDIR_STATUS; resp.modification_time = 0; - resp.status = fs.DirCreate(path) ? 0x02 : 0x01; + int res = fs.DirCreate(path); + resp.status = (res==0) ? 0x01 : (int8_t)res; auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(MKDirResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; @@ -213,6 +220,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { ListDirHeader* header = (ListDirHeader*) om->om_data; uint16_t plen = header->pathlen; char path[plen + 1] {0}; + path[plen] = 0; // Copy and null teminate string memcpy(path, header->pathstr, plen); ListDirResponse resp {}; @@ -222,8 +230,9 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.totalentries = 0; resp.entry = 0; resp.modification_time = 0; - if (fs.DirOpen(path, &dir) != 0) { - resp.status = 0x02; + int res = fs.DirOpen(path, &dir); + if (res != 0) { + resp.status = (int8_t)res; auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; @@ -233,7 +242,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { } fs.DirRewind(&dir); while (true) { - int res = fs.DirRead(&dir, &info); + res = fs.DirRead(&dir, &info); if (res <= 0) { break; } @@ -278,10 +287,11 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { header->pathstr[plen] = 0; char path[header->NewPathLength + 1] {0}; memcpy(path, &header->pathstr[plen + 1], header->NewPathLength); + path[header->NewPathLength] = 0; // Copy and null teminate string MoveResponse resp {}; resp.command = commands::MOVE_STATUS; - int res = fs.Rename(header->pathstr, path); - resp.status = (res == 0) ? 1 : 2; + int8_t res = (int8_t)fs.Rename(header->pathstr, path); + resp.status = (res == 0) ? 1 : res; auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(MoveResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); } From b62c62ee1f62d3371bfa29855bf2fd59b4dcecf8 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sat, 20 Nov 2021 20:18:14 +0000 Subject: [PATCH 21/26] Change read to return LFS return values when reading a directory or nonexistant file --- src/components/ble/FSService.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index e1d5bae05f..4cefcdcfee 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -89,7 +89,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.chunkoff = header->chunkoff; int res = fs.Stat(filepath, &info); if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) { - resp.status = 0x03; + resp.status = (int8_t) res; resp.chunklen = 0; resp.totallen = 0; om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse)); @@ -118,7 +118,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.chunkoff = header->chunkoff; int res = fs.Stat(filepath, &info); if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) { - resp.status = 0x03; + resp.status = (int8_t) res; resp.chunklen = 0; resp.totallen = 0; } else { @@ -157,7 +157,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.offset = header->offset; resp.modTime = 0; int res = fs.FileOpen(&f, filepath, LFS_O_WRONLY | LFS_O_CREAT); - resp.status = (res==0) ? 0x01: (int8_t)res; + resp.status = (res == 0) ? 0x01 : (int8_t) res; 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)); @@ -171,13 +171,13 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { WriteResponse resp; resp.command = commands::WRITE_PACING; resp.offset = header->offset; - resp.status = 1; + resp.status = 0x01; fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT); fs.FileSeek(&f, header->offset); - int res = fs.FileWrite(&f, header->data, header->dataSize); + int res = fs.FileWrite(&f, header->data, header->dataSize); fs.FileClose(&f); - if(res < 0 ){ - resp.status = (int8_t)res; + if (res < 0) { + resp.status = (int8_t) res; } resp.freespace = std::min(fs.getSize() - (fs.GetFSSize() * fs.getBlockSize()), fileSize - header->offset); auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(WriteResponse)); @@ -194,7 +194,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { DelResponse resp {}; resp.command = commands::DELETE_STATUS; int res = fs.FileDelete(path); - resp.status = (res==0) ? 0x01 : (int8_t)res; + resp.status = (res == 0) ? 0x01 : (int8_t) res; auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(DelResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; @@ -210,7 +210,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.command = commands::MKDIR_STATUS; resp.modification_time = 0; int res = fs.DirCreate(path); - resp.status = (res==0) ? 0x01 : (int8_t)res; + resp.status = (res == 0) ? 0x01 : (int8_t) res; auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(MKDirResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; @@ -226,13 +226,13 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { ListDirResponse resp {}; resp.command = commands::LISTDIR_ENTRY; - resp.status = 1; + resp.status = 0x01; resp.totalentries = 0; resp.entry = 0; resp.modification_time = 0; int res = fs.DirOpen(path, &dir); if (res != 0) { - resp.status = (int8_t)res; + resp.status = (int8_t) res; auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); break; @@ -290,7 +290,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { path[header->NewPathLength] = 0; // Copy and null teminate string MoveResponse resp {}; resp.command = commands::MOVE_STATUS; - int8_t res = (int8_t)fs.Rename(header->pathstr, path); + int8_t res = (int8_t) fs.Rename(header->pathstr, path); resp.status = (res == 0) ? 1 : res; auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(MoveResponse)); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); From fdb5e27aea99c3a5117661948de75e7853165ff5 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 21 Nov 2021 03:33:00 +0000 Subject: [PATCH 22/26] Attempt at a more robust File handler --- src/components/ble/FSService.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 4cefcdcfee..2222ae9a12 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -147,7 +147,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { auto* header = (WriteHeader*) om->om_data; uint16_t plen = header->pathlen; if (plen > maxpathlen) { //> counts for null term - return -1; + return -1; // TODO make this actually return a BLE notif } memcpy(filepath, header->pathstr, plen); filepath[plen] = 0; // Copy and null teminate string @@ -156,9 +156,12 @@ 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_WRONLY | LFS_O_CREAT); - resp.status = (res == 0) ? 0x01 : (int8_t) res; - fs.FileClose(&f); + if(res == 0){ + fs.FileClose(&f); + resp.status = (res == 0) ? 0x01 : (int8_t) res; + } 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); @@ -171,14 +174,18 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { WriteResponse resp; resp.command = commands::WRITE_PACING; resp.offset = header->offset; - resp.status = 0x01; - fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT); - fs.FileSeek(&f, header->offset); - int res = fs.FileWrite(&f, header->data, header->dataSize); - fs.FileClose(&f); + int res = 0; + + if (!(res = fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT))) { + if (!(res = fs.FileSeek(&f, header->offset))) { + res = fs.FileWrite(&f, header->data, header->dataSize); + } + fs.FileClose(&f); + } if (res < 0) { resp.status = (int8_t) res; } + 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); From d1e9aa107617565b456f05e20e9468ab270d323e Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 21 Nov 2021 06:17:07 +0000 Subject: [PATCH 23/26] Force variable cleanup because LFS makes assumptions about variable initialization state --- src/components/ble/FSService.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 2222ae9a12..03d4673d92 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -71,9 +71,11 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { } lfs_dir_t dir; lfs_info info; + lfs_file f; + memset(&f, 0, sizeof(lfs_file_t)); + memset(&dir, 0, sizeof(lfs_dir_t)); switch (command) { case commands::READ: { - lfs_file f; NRF_LOG_INFO("[FS_S] -> Read"); auto* header = (ReadHeader*) om->om_data; uint16_t plen = header->pathlen; @@ -109,7 +111,6 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { break; } case commands::READ_PACING: { - lfs_file f; NRF_LOG_INFO("[FS_S] -> Readpacing"); auto* header = (ReadHeader*) om->om_data; ReadResponse resp; @@ -142,7 +143,6 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { break; } case commands::WRITE: { - lfs_file f; NRF_LOG_INFO("[FS_S] -> Write"); auto* header = (WriteHeader*) om->om_data; uint16_t plen = header->pathlen; @@ -157,8 +157,8 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.offset = header->offset; resp.modTime = 0; - int res = fs.FileOpen(&f, filepath, LFS_O_WRONLY | LFS_O_CREAT); - if(res == 0){ + int res = fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT); + if (res == 0) { fs.FileClose(&f); resp.status = (res == 0) ? 0x01 : (int8_t) res; } @@ -168,7 +168,6 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { break; } case commands::WRITE_DATA: { - lfs_file f; NRF_LOG_INFO("[FS_S] -> WriteData"); auto* header = (WritePacing*) om->om_data; WriteResponse resp; @@ -185,7 +184,6 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { if (res < 0) { resp.status = (int8_t) res; } - 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); From b9a2db1a49976cbfe92a9941bce17ffdbae7d0b3 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 21 Nov 2021 06:33:24 +0000 Subject: [PATCH 24/26] Fix large file support that broke due to a refactor. --- src/components/ble/FSService.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 03d4673d92..d82596cb89 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -176,7 +176,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { int res = 0; if (!(res = fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT))) { - if (!(res = fs.FileSeek(&f, header->offset))) { + if ((res = fs.FileSeek(&f, header->offset) ) >= 0) { res = fs.FileWrite(&f, header->data, header->dataSize); } fs.FileClose(&f); From 0a0d1f270328001e1e80a2ac6c26feb111bfa27a Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 21 Nov 2021 21:35:41 +0000 Subject: [PATCH 25/26] Fix more initializers --- src/components/ble/FSService.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index d82596cb89..8dc9ed674a 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -69,11 +69,9 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { while (systemTask.IsSleeping()) { vTaskDelay(100); // 50ms } - lfs_dir_t dir; - lfs_info info; - lfs_file f; - memset(&f, 0, sizeof(lfs_file_t)); - memset(&dir, 0, sizeof(lfs_dir_t)); + lfs_dir_t dir = {0}; + lfs_info info = {0}; + lfs_file f = {0}; switch (command) { case commands::READ: { NRF_LOG_INFO("[FS_S] -> Read"); @@ -100,7 +98,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { resp.totallen = info.size; fs.FileOpen(&f, filepath, LFS_O_RDONLY); fs.FileSeek(&f, header->chunkoff); - uint8_t fileData[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); @@ -130,7 +128,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { } os_mbuf* om; if (resp.chunklen > 0) { - uint8_t fileData[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); @@ -176,7 +174,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { int res = 0; if (!(res = fs.FileOpen(&f, filepath, LFS_O_RDWR | LFS_O_CREAT))) { - if ((res = fs.FileSeek(&f, header->offset) ) >= 0) { + if ((res = fs.FileSeek(&f, header->offset)) >= 0) { res = fs.FileWrite(&f, header->data, header->dataSize); } fs.FileClose(&f); @@ -193,7 +191,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { NRF_LOG_INFO("[FS_S] -> Delete"); auto* header = (DelHeader*) om->om_data; uint16_t plen = header->pathlen; - char path[plen + 1] {0}; + char path[plen + 1] = {0}; memcpy(path, header->pathstr, plen); path[plen] = 0; // Copy and null teminate string DelResponse resp {}; @@ -208,7 +206,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { NRF_LOG_INFO("[FS_S] -> MKDir"); auto* header = (MKDirHeader*) om->om_data; uint16_t plen = header->pathlen; - char path[plen + 1] {0}; + char path[plen + 1] = {0}; memcpy(path, header->pathstr, plen); path[plen] = 0; // Copy and null teminate string MKDirResponse resp {}; @@ -224,7 +222,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { NRF_LOG_INFO("[FS_S] -> ListDir"); ListDirHeader* header = (ListDirHeader*) om->om_data; uint16_t plen = header->pathlen; - char path[plen + 1] {0}; + char path[plen + 1] = {0}; path[plen] = 0; // Copy and null teminate string memcpy(path, header->pathstr, plen); @@ -290,7 +288,7 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { uint16_t plen = header->OldPathLength; // Null Terminate string header->pathstr[plen] = 0; - char path[header->NewPathLength + 1] {0}; + char path[header->NewPathLength + 1] = {0}; memcpy(path, &header->pathstr[plen + 1], header->NewPathLength); path[header->NewPathLength] = 0; // Copy and null teminate string MoveResponse resp {}; From 1470489e7b14fdfe4871cdc714c4a3c98917c4bb Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Fri, 10 Dec 2021 01:49:03 +0000 Subject: [PATCH 26/26] Fix Failed rebase. --- src/components/ble/NimbleController.cpp | 7 +++---- src/components/ble/NimbleController.h | 1 - 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index 01230661b5..3bf1ec80c6 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -49,9 +49,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, batteryInformationService {batteryController}, immediateAlertService {systemTask, notificationManager}, heartRateService {systemTask, heartRateController}, - motionService{systemTask, motionController}, - fs {fs}, - fsService {systemTask,fs}, + motionService {systemTask, motionController}, + fsService {systemTask, fs}, serviceDiscovery({¤tTimeClient, &alertNotificationClient}) { } @@ -100,7 +99,7 @@ void NimbleController::Init() { heartRateService.Init(); motionService.Init(); fsService.Init(); - + int rc; rc = ble_hs_util_ensure_addr(0); ASSERT(rc == 0); diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index 14749b8d3f..7a3870373e 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -111,7 +111,6 @@ namespace Pinetime { HeartRateService heartRateService; MotionService motionService; ServiceDiscovery serviceDiscovery; - FS fs; FSService fsService; uint8_t addrType;