Reading Seems to work?

This commit is contained in:
Tim Keller 2021-10-22 03:34:23 +00:00
parent faa05eb57b
commit 8fb99471c3

View file

@ -73,74 +73,76 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) {
lfs_info info; lfs_info info;
switch (command) { switch (command) {
case commands::READ: { case commands::READ: {
lfs_file f;
NRF_LOG_INFO("[FS_S] -> Read"); NRF_LOG_INFO("[FS_S] -> Read");
if (state != FSState::IDLE) {
return -1;
}
state = FSState::READ;
auto* header = (ReadHeader*) om->om_data; auto* header = (ReadHeader*) om->om_data;
uint16_t plen = header->pathlen; uint16_t plen = header->pathlen;
if (plen > maxpathlen - 1) { if (plen > maxpathlen) { //> counts for null term
return -1; return -1;
} }
memcpy(filepath, header->pathstr, plen); memcpy(filepath, header->pathstr, plen);
filepath[plen + 1] = 0; // Copy and null teminate string filepath[plen + 1] = 0; // Copy and null teminate string
ReadResponse resp; ReadResponse resp;
resp.command = commands::READ_DATA; resp.command = commands::READ_DATA;
resp.chunkoff = header->chunkoff;
resp.status = 0x01; resp.status = 0x01;
struct lfs_info info = {}; resp.chunkoff = header->chunkoff;
int res = fs.Stat(filepath, &info); int res = fs.Stat(filepath, &info);
if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) { if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) {
resp.status = 0x03; resp.status = 0x03;
resp.chunklen = 0; resp.chunklen = 0;
resp.totallen = 0; resp.totallen = 0;
} else { } else {
lfs_file f; resp.chunklen = std::min(header->chunksize, info.size); // TODO add mtu somehow
resp.chunklen = std::min(header->chunksize, info.size);
resp.totallen = info.size; resp.totallen = info.size;
fs.FileOpen(&f, filepath, LFS_O_RDONLY); fs.FileOpen(&f, filepath, LFS_O_RDONLY);
fs.FileSeek(&f, header->chunkoff); fs.FileSeek(&f, header->chunkoff);
resp.chunklen = fs.FileRead(&f, resp.chunk, resp.chunklen); }
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);
}
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
if (header->chunksize >= resp.totallen) { // probably removeable...but then usafe break;
state = FSState::IDLE;
}
} }
case commands::READ_PACING: { case commands::READ_PACING: {
NRF_LOG_INFO("[FS_S] -> ReadPacing"); lfs_file f;
if (state != FSState::READ) { NRF_LOG_INFO("[FS_S] -> Readpacing");
return -1; auto* header = (ReadHeader*) om->om_data;
} ReadResponse resp;
auto* header = (ReadPacing*) om->om_data;
ReadResponse resp = {};
resp.command = commands::READ_DATA; resp.command = commands::READ_DATA;
resp.chunkoff = header->chunkoff;
resp.status = 0x01; resp.status = 0x01;
struct lfs_info info = {}; resp.chunkoff = header->chunkoff;
int res = fs.Stat(filepath, &info); int res = fs.Stat(filepath, &info);
if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) { if (res == LFS_ERR_NOENT && info.type != LFS_TYPE_DIR) {
resp.status = 0x03; resp.status = 0x03;
resp.chunklen = 0; resp.chunklen = 0;
resp.totallen = 0; resp.totallen = 0;
} else { } else {
lfs_file f; resp.chunklen = std::min(header->chunksize, info.size); // TODO add mtu somehow
resp.chunklen = std::min(header->chunksize, info.size);
resp.totallen = info.size; resp.totallen = info.size;
fs.FileOpen(&f, filepath, LFS_O_RDONLY); fs.FileOpen(&f, filepath, LFS_O_RDONLY);
fs.FileSeek(&f, header->chunkoff); fs.FileSeek(&f, header->chunkoff);
resp.chunklen = fs.FileRead(&f, resp.chunk, resp.chunklen); }
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);
}
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ReadResponse));
ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
if (resp.chunklen >= header->chunksize) { // is this right? break;
state = FSState::IDLE;
}
} }
case commands::WRITE: { case commands::WRITE: {
if (state != FSState::IDLE) { if (state != FSState::IDLE) {
@ -185,7 +187,6 @@ int FSService::FSCommandHandler(uint16_t connectionHandle, os_mbuf* om) {
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);
ListDirResponse resp {}; ListDirResponse resp {};
@ -223,11 +224,10 @@ 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); resp.path_length = strlen(info.name);
NRF_LOG_INFO("[FS_S] -> DIR %.10s", path);
auto* om = ble_hs_mbuf_from_flat(&resp, sizeof(ListDirResponse)); 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); ble_gattc_notify_custom(connectionHandle, transferCharacteristicHandle, om);
/* /*
* Todo Figure out how to know when the previous Notify was TX'd * Todo Figure out how to know when the previous Notify was TX'd