From 91c644b43c250b0a03047349182828df31ddcbd2 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 16:04:23 +0000 Subject: direcetory listings maybe? Added LISTDIR command and notify responses. --- src/components/ble/FSService.cpp | 73 ++++++++++++++++++++++++++++++++++++++-- src/components/ble/FSService.h | 27 +++++++++++++-- 2 files changed, 94 insertions(+), 6 deletions(-) (limited to 'src/components/ble') diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index 6551c20c..2cfd5ccd 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -1,5 +1,6 @@ #include #include "FSService.h" +#include "components/ble/BleController.h" using namespace Pinetime::Controllers; @@ -13,8 +14,8 @@ int FSServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gat } FSService::FSService(Pinetime::Controllers::FS& fs) - : fs {fs}, - characteristicDefinition {{.uuid = &fsVersionUuid.u, + : fs {fs}, + characteristicDefinition {{.uuid = &fsVersionUuid.u, .access_cb = FSServiceCallback, .arg = this, .flags = BLE_GATT_CHR_F_READ, @@ -24,7 +25,7 @@ FSService::FSService(Pinetime::Controllers::FS& fs) .access_cb = FSServiceCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, - .val_handle = nullptr, + .val_handle = &transferCharacteristicHandle, }, {0}}, serviceDefinition { @@ -51,5 +52,71 @@ int FSService::OnFSServiceRequested(uint16_t connectionHandle, uint16_t attribut int res = os_mbuf_append(context->om, &fsVersion, sizeof(fsVersion)); return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; } + if (attributeHandle == transferCharacteristicHandle) { + return FSCommandHandler(connectionHandle, context->om); + } + return 0; +} + +int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) { + auto command = static_cast(om->om_data[0]); + NRF_LOG_INFO("[FS_S] -> FSCommandHandler"); + + switch (command) { + case commands::LISTDIR: { + NRF_LOG_INFO("[FS_S] -> ListDir"); + ListDirHeader *header = (ListDirHeader *)&om->om_data[0]; + uint16_t plen = header->pathlen; + char path[plen+1] = {0}; + memcpy(path, header->pathstr, plen); + NRF_LOG_INFO("[FS_S] -> DIR %.10s", path); + lfs_dir_t dir; + struct lfs_info info; + + ListDirResponse resp; + resp.command = 0x51; // LISTDIR_ENTRY; + resp.status = 1; // TODO actually use res above! + resp.totalentries = 0; + resp.entry = 0; + int res = fs.DirOpen(path, &dir); + while (fs.DirRead(&dir, &info)) { + resp.totalentries++; + } + NRF_LOG_INFO("[FS_S] -> %d ", resp.totalentries); + fs.DirClose(&dir); + fs.DirOpen(path, &dir); + while (fs.DirRead(&dir, &info)) { + switch(info.type){ + case LFS_TYPE_REG: + { + resp.flags = 0; + resp.file_size = info.size; + break; + } + case LFS_TYPE_DIR: + { + resp.flags = 1; + resp.file_size = 0; + break; + } + } + resp.modification_time = 0; // TODO Does LFS actually support TS? + strcpy(resp.path,info.name); + resp.path_length = strlen(info.name); + NRF_LOG_INFO("[FS_S] ->Path %s ,", info.name); + auto* om = ble_hs_mbuf_from_flat(&resp,sizeof(ListDirResponse)); + ble_gattc_notify_custom(connectionHandle,transferCharacteristicHandle,om); + resp.entry++; + } + resp.entry++; + resp.file_size = 0; + resp.path_length = 0; + resp.flags = 0; + //Todo this better + auto* om = ble_hs_mbuf_from_flat(&resp,sizeof(ListDirResponse)-70+resp.path_length); + ble_gattc_notify_custom(connectionHandle,transferCharacteristicHandle,om); + NRF_LOG_INFO("[FS_S] -> done "); + } + } return 0; } diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h index 85b484a3..eb4b34d7 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -4,6 +4,7 @@ #include #undef max #undef min + #include "components/fs/FS.h" namespace Pinetime { @@ -42,8 +43,26 @@ namespace Pinetime { struct ble_gatt_chr_def characteristicDefinition[3]; struct ble_gatt_svc_def serviceDefinition[2]; uint16_t versionCharacteristicHandle; + uint16_t transferCharacteristicHandle; + typedef struct __attribute__((packed)) { + uint8_t command; + uint8_t padding; + uint16_t pathlen; + char pathstr[70]; + } ListDirHeader; + typedef struct __attribute__((packed)) { + uint8_t command; + uint8_t status; + uint16_t path_length; + uint32_t entry; + uint32_t totalentries; + uint32_t flags; + uint32_t modification_time; + uint32_t file_size; + char path[70]; + } ListDirResponse; - enum class commands { + enum class commands : uint8_t { INVALID = 0x00, READ = 0x10, READ_DATA = 0x11, @@ -58,8 +77,10 @@ namespace Pinetime { LISTDIR = 0x50, LISTDIR_ENTRY = 0x51, MOVE = 0x60, - MOVE_STATUS = 0x61, - } + MOVE_STATUS = 0x61 + }; + + int FSCommandHandler(uint16_t connectionHandle, os_mbuf* om); }; } } -- cgit v1.2.3