summaryrefslogtreecommitdiff
path: root/src/components/ble/FSService.cpp
diff options
context:
space:
mode:
authorTim Keller <geekboy1011@gmail.com>2021-10-22 03:34:23 +0000
committerTim Keller <geekboy1011@gmail.com>2021-12-10 01:18:57 +0000
commit8fb99471c38c2efd7af88d4888c5792bdd8deafb (patch)
treee393b84d591a0a1b7296aa46e066207a69cd4005 /src/components/ble/FSService.cpp
parentfaa05eb57b7d6214e53d0b147a796793496a89ae (diff)
Reading Seems to work?
Diffstat (limited to 'src/components/ble/FSService.cpp')
-rw-r--r--src/components/ble/FSService.cpp78
1 files changed, 39 insertions, 39 deletions
diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp
index 0ba3e102..68fd5ea6 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++;
}