Added Delete file
Added FS Stat.
This commit is contained in:
parent
eabbbfa373
commit
3a8e66a52f
|
@ -63,64 +63,112 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) {
|
||||||
NRF_LOG_INFO("[FS_S] -> FSCommandHandler");
|
NRF_LOG_INFO("[FS_S] -> FSCommandHandler");
|
||||||
|
|
||||||
switch (command) {
|
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: {
|
case commands::LISTDIR: {
|
||||||
NRF_LOG_INFO("[FS_S] -> 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;
|
uint16_t plen = header->pathlen;
|
||||||
char path[plen+1] = {0};
|
char path[plen + 1] = {0};
|
||||||
memcpy(path, header->pathstr, plen);
|
memcpy(path, header->pathstr, plen);
|
||||||
NRF_LOG_INFO("[FS_S] -> DIR %.10s", path);
|
NRF_LOG_INFO("[FS_S] -> DIR %.10s", path);
|
||||||
lfs_dir_t dir = {};
|
lfs_dir_t dir = {};
|
||||||
struct lfs_info info = {};
|
struct lfs_info info = {};
|
||||||
|
|
||||||
ListDirResponse resp = {};
|
ListDirResponse resp = {};
|
||||||
resp.command = 0x51; // LISTDIR_ENTRY;
|
resp.command = commands::LISTDIR_ENTRY;
|
||||||
resp.status = 1; // TODO actually use res above!
|
resp.status = 1; // TODO actually use res above!
|
||||||
resp.totalentries = 0;
|
resp.totalentries = 0;
|
||||||
resp.entry = 0;
|
resp.entry = 0;
|
||||||
|
|
||||||
int res = fs.DirOpen(path, &dir);
|
int res = fs.DirOpen(path, &dir);
|
||||||
|
|
||||||
NRF_LOG_INFO("[FS_S] ->diropen %d ", res);
|
NRF_LOG_INFO("[FS_S] ->diropen %d ", res);
|
||||||
while (fs.DirRead(&dir, &info)) {
|
while (fs.DirRead(&dir, &info)) {
|
||||||
resp.totalentries++;
|
resp.totalentries++;
|
||||||
|
|
||||||
}
|
}
|
||||||
NRF_LOG_INFO("[FS_S] -> %d ", resp.totalentries);
|
NRF_LOG_INFO("[FS_S] -> %d ", resp.totalentries);
|
||||||
|
|
||||||
fs.DirRewind(&dir);
|
fs.DirRewind(&dir);
|
||||||
|
|
||||||
while (fs.DirRead(&dir, &info)) {
|
while (fs.DirRead(&dir, &info)) {
|
||||||
switch(info.type){
|
switch (info.type) {
|
||||||
case LFS_TYPE_REG:
|
case LFS_TYPE_REG: {
|
||||||
{
|
|
||||||
resp.flags = 0;
|
resp.flags = 0;
|
||||||
resp.file_size = info.size;
|
resp.file_size = info.size;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LFS_TYPE_DIR:
|
case LFS_TYPE_DIR: {
|
||||||
{
|
resp.flags = 1;
|
||||||
resp.flags = 1;
|
resp.file_size = 0;
|
||||||
resp.file_size = 0;
|
break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resp.modification_time = 0; // TODO Does LFS actually support TS?
|
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);
|
resp.path_length = strlen(info.name);
|
||||||
NRF_LOG_INFO("[FS_S] ->Path %s ,", 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));
|
||||||
ble_gattc_notify_custom(connectionHandle,transferCharacteristicHandle,om);
|
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
|
||||||
vTaskDelay(1); //Allow stuff to actually go out over the BLE conn
|
vTaskDelay(1); // Allow stuff to actually go out over the BLE conn
|
||||||
resp.entry++;
|
resp.entry++;
|
||||||
}
|
}
|
||||||
fs.DirClose(&dir);
|
fs.DirClose(&dir);
|
||||||
resp.file_size = 0;
|
resp.file_size = 0;
|
||||||
resp.path_length = 0;
|
resp.path_length = 0;
|
||||||
resp.flags = 0;
|
resp.flags = 0;
|
||||||
//Todo this better
|
// 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) - 70 + resp.path_length);
|
||||||
ble_gattc_notify_custom(connectionHandle,transferCharacteristicHandle,om);
|
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
|
||||||
NRF_LOG_INFO("[FS_S] -> done ");
|
NRF_LOG_INFO("[FS_S] -> done ");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,14 +66,14 @@ namespace Pinetime {
|
||||||
};
|
};
|
||||||
|
|
||||||
using ListDirHeader = struct __attribute__((packed)) {
|
using ListDirHeader = struct __attribute__((packed)) {
|
||||||
uint8_t command;
|
commands command;
|
||||||
uint8_t padding;
|
uint8_t padding;
|
||||||
uint16_t pathlen;
|
uint16_t pathlen;
|
||||||
char pathstr[70];
|
char pathstr[70];
|
||||||
};
|
};
|
||||||
|
|
||||||
using ListDirResponse = struct __attribute__((packed)) {
|
using ListDirResponse = struct __attribute__((packed)) {
|
||||||
uint8_t command;
|
commands command;
|
||||||
uint8_t status;
|
uint8_t status;
|
||||||
uint16_t path_length;
|
uint16_t path_length;
|
||||||
uint32_t entry;
|
uint32_t entry;
|
||||||
|
@ -85,7 +85,7 @@ namespace Pinetime {
|
||||||
};
|
};
|
||||||
|
|
||||||
using MKDirHeader = struct __attribute__((packed)) {
|
using MKDirHeader = struct __attribute__((packed)) {
|
||||||
uint8_t command;
|
commands command;
|
||||||
uint8_t padding;
|
uint8_t padding;
|
||||||
uint16_t pathlen;
|
uint16_t pathlen;
|
||||||
uint32_t padding2;
|
uint32_t padding2;
|
||||||
|
@ -94,13 +94,25 @@ namespace Pinetime {
|
||||||
};
|
};
|
||||||
|
|
||||||
using MKDirResponse = struct __attribute__((packed)) {
|
using MKDirResponse = struct __attribute__((packed)) {
|
||||||
uint8_t command;
|
commands command;
|
||||||
uint8_t status;
|
uint8_t status;
|
||||||
uint32_t padding1;
|
uint32_t padding1;
|
||||||
uint16_t padding2;
|
uint16_t padding2;
|
||||||
uint64_t modification_time;
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,6 +96,10 @@ int FS::DirCreate(const char* path) {
|
||||||
return lfs_mkdir(&lfs, 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
|
// Delete directory and all files inside
|
||||||
int FS::DirDelete(const char* path) {
|
int FS::DirDelete(const char* path) {
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ namespace Pinetime {
|
||||||
int DirCreate(const char* path);
|
int DirCreate(const char* path);
|
||||||
int DirDelete(const char* path);
|
int DirDelete(const char* path);
|
||||||
|
|
||||||
|
int Stat(const char* path, lfs_info* info);
|
||||||
void VerifyResource();
|
void VerifyResource();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in a new issue