Actually fix memory corruption, seems stable now

ListDir MKDIR delete all seem to work
Co-authored-by: Iambian <Iambian@users.noreply.github.com>
This commit is contained in:
Tim Keller 2021-10-21 04:02:50 +00:00
parent 6393a17d74
commit faa05eb57b
2 changed files with 43 additions and 65 deletions

View File

@ -69,8 +69,9 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) {
while (systemTask.IsSleeping()) { while (systemTask.IsSleeping()) {
vTaskDelay(100); // 50ms vTaskDelay(100); // 50ms
} }
lfs_dir_t dir;
lfs_info info;
switch (command) { switch (command) {
/*
case commands::READ: { case commands::READ: {
NRF_LOG_INFO("[FS_S] -> Read"); NRF_LOG_INFO("[FS_S] -> Read");
if (state != FSState::IDLE) { if (state != FSState::IDLE) {
@ -151,31 +152,15 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) {
return -1; return -1;
} }
} }
case commands::DELETE: { case commands::DELETE: {
NRF_LOG_INFO("[FS_S] -> Delete"); NRF_LOG_INFO("[FS_S] -> Delete");
auto* header = (DelHeader*) om->om_data; auto* header = (DelHeader*) om->om_data;
uint16_t plen = header->pathlen; uint16_t plen = header->pathlen;
char path[plen + 1] = {0}; char path[plen + 1] {0};
struct lfs_info info = {}; memcpy(path, header->pathstr, plen);
DelResponse resp = {}; DelResponse resp {};
resp.command = commands::DELETE_STATUS; resp.command = commands::DELETE_STATUS;
int res = fs.Stat(path, &info); resp.status = fs.FileDelete(path) ? 0x02 : 0x01;
// 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)); auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(DelResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break; break;
@ -184,57 +169,43 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) {
NRF_LOG_INFO("[FS_S] -> MKDir"); NRF_LOG_INFO("[FS_S] -> MKDir");
auto* header = (MKDirHeader*) om->om_data; auto* header = (MKDirHeader*) 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] -> MKDIR %.10s", path); MKDirResponse resp {};
MKDirResponse resp = {};
resp.command = commands::MKDIR_STATUS; resp.command = commands::MKDIR_STATUS;
int res = fs.DirCreate(path); resp.modification_time = 0;
switch (res) { resp.status = fs.DirCreate(path) ? 0x02 : 0x01;
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)); auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(MKDirResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
break; 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; 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 = {}; ListDirResponse resp {};
struct lfs_info info = {};
ListDirResponse resp = {};
resp.command = commands::LISTDIR_ENTRY; resp.command = commands::LISTDIR_ENTRY;
resp.status = 1; // TODO actually use res above! resp.status = 1;
resp.totalentries = 0; resp.totalentries = 0;
resp.entry = 0; resp.entry = 0;
resp.modification_time = 0; // TODO Does LFS actually support TS?
if (fs.DirOpen(path, &dir)) { if (fs.DirOpen(path, &dir) != 0) {
return 0; resp.status = 0x02;
} auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
// NRF_LOG_INFO("[FS_S] ->diropen %d ", res); break;
};
// Count Total files in directory.
while (fs.DirRead(&dir, &info)) { while (fs.DirRead(&dir, &info)) {
resp.totalentries++; resp.totalentries++;
} }
NRF_LOG_INFO("[FS_S] -> %d ", resp.totalentries);
fs.DirRewind(&dir); fs.DirRewind(&dir);
while (true) {
// NRF_LOG_INFO("[FS_S] ->diropen %d ", res);
while (resp.entry < resp.totalentries) {
int res = fs.DirRead(&dir, &info); int res = fs.DirRead(&dir, &info);
if (res <= 0) { if (res <= 0) {
break; break;
@ -251,35 +222,39 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) {
break; 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); resp.path_length = strlen(info.name);
NRF_LOG_INFO("[FS_S] ->Path %s ,", info.name); NRF_LOG_INFO("[FS_S] -> DIR %.10s", path);
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse) + resp.path_length); 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); 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 vTaskDelay(100); // Allow stuff to actually go out over the BLE conn
resp.entry++; resp.entry++;
} }
assert(fs.DirClose(&dir) == 0);
if (fs.DirClose(&dir)) {
return 0;
}
resp.file_size = 0; resp.file_size = 0;
resp.path_length = 0; resp.path_length = 0;
resp.flags = 0; resp.flags = 0;
// TODO Handle Size of response better. 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); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
NRF_LOG_INFO("[FS_S] -> done ");
break; break;
} }
default:
break;
} }
NRF_LOG_INFO("[FS_S] -> done ");
systemTask.PushMessage(Pinetime::System::Messages::StopFileTransfer); systemTask.PushMessage(Pinetime::System::Messages::StopFileTransfer);
return 0; return 0;
} }
// Loads resp with file data given a valid filepath header and resp // Loads resp with file data given a valid filepath header and resp
void FSService::prepareReadDataResp(ReadHeader* header, ReadResponse* resp) { void FSService::prepareReadDataResp(ReadHeader* header, ReadResponse* resp) {
uint16_t plen = header->pathlen; // uint16_t plen = header->pathlen;
resp->command = commands::READ_DATA; resp->command = commands::READ_DATA;
resp->chunkoff = header->chunkoff; resp->chunkoff = header->chunkoff;
resp->status = 0x01; resp->status = 0x01;

View File

@ -29,7 +29,7 @@ namespace Pinetime {
static constexpr uint16_t fsVersionId {0x0100}; static constexpr uint16_t fsVersionId {0x0100};
static constexpr uint16_t fsTransferId {0x0200}; static constexpr uint16_t fsTransferId {0x0200};
uint16_t fsVersion = {0x0004}; uint16_t fsVersion = {0x0004};
static constexpr uint8_t maxpathlen = 100; static constexpr uint16_t maxpathlen = 256;
static constexpr ble_uuid16_t fsServiceUuid { static constexpr ble_uuid16_t fsServiceUuid {
.u {.type = BLE_UUID_TYPE_16}, .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}};
@ -47,6 +47,9 @@ namespace Pinetime {
uint16_t versionCharacteristicHandle; uint16_t versionCharacteristicHandle;
uint16_t transferCharacteristicHandle; uint16_t transferCharacteristicHandle;
// lfs_dir_t dir;
// lfs_info info;
enum class commands : uint8_t { enum class commands : uint8_t {
INVALID = 0x00, INVALID = 0x00,
READ = 0x10, READ = 0x10,