♻️ Replace more arduino functions with native
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
// Make sure that this aligns with socket_message.FSDownloadData.data max_size (and for upload)
|
// Make sure that this aligns with socket_message.FSDownloadData.data max_size (and for upload)
|
||||||
#define FS_MAX_CHUNK_SIZE 16384 // ~= 16 kb
|
#define FS_MAX_CHUNK_SIZE 16384 // ~= 16 kb
|
||||||
#define FS_TRANSFER_TIMEOUT 30000 // 30 seconds
|
#define FS_TRANSFER_TIMEOUT_MS 30000 // 30 seconds
|
||||||
|
|
||||||
namespace FileSystemWS {
|
namespace FileSystemWS {
|
||||||
|
|
||||||
@@ -47,12 +47,8 @@ class FileSystemHandler {
|
|||||||
FileSystemHandler();
|
FileSystemHandler();
|
||||||
|
|
||||||
// Set callbacks for sending streaming data
|
// Set callbacks for sending streaming data
|
||||||
void setSendCallbacks(
|
void setSendCallbacks(SendMetadataCallback sendMetadata, SendCallback sendData, SendCompleteCallback sendComplete,
|
||||||
SendMetadataCallback sendMetadata,
|
SendUploadCompleteCallback sendUploadComplete);
|
||||||
SendCallback sendData,
|
|
||||||
SendCompleteCallback sendComplete,
|
|
||||||
SendUploadCompleteCallback sendUploadComplete
|
|
||||||
);
|
|
||||||
|
|
||||||
// Delete file/directory
|
// Delete file/directory
|
||||||
socket_message_FSDeleteResponse handleDelete(const socket_message_FSDeleteRequest& req);
|
socket_message_FSDeleteResponse handleDelete(const socket_message_FSDeleteRequest& req);
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
#include <platform_shared/message.pb.h>
|
#include <platform_shared/message.pb.h>
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <SPI.h>
|
|
||||||
|
|
||||||
#include <NewPing.h>
|
#include <NewPing.h>
|
||||||
#include <peripherals/i2c_bus.h>
|
#include <peripherals/i2c_bus.h>
|
||||||
|
|||||||
+13
-15
@@ -3,6 +3,7 @@
|
|||||||
#include <pb_encode.h>
|
#include <pb_encode.h>
|
||||||
#include <pb_decode.h>
|
#include <pb_decode.h>
|
||||||
#include <esp_littlefs.h>
|
#include <esp_littlefs.h>
|
||||||
|
#include <esp_timer.h>
|
||||||
|
|
||||||
static const char* TAG = "FileSystemWS";
|
static const char* TAG = "FileSystemWS";
|
||||||
|
|
||||||
@@ -12,12 +13,9 @@ FileSystemHandler fsHandler;
|
|||||||
|
|
||||||
FileSystemHandler::FileSystemHandler() : transferIdCounter_(0) {}
|
FileSystemHandler::FileSystemHandler() : transferIdCounter_(0) {}
|
||||||
|
|
||||||
void FileSystemHandler::setSendCallbacks(
|
void FileSystemHandler::setSendCallbacks(SendMetadataCallback sendMetadata, SendCallback sendData,
|
||||||
SendMetadataCallback sendMetadata,
|
|
||||||
SendCallback sendData,
|
|
||||||
SendCompleteCallback sendComplete,
|
SendCompleteCallback sendComplete,
|
||||||
SendUploadCompleteCallback sendUploadComplete
|
SendUploadCompleteCallback sendUploadComplete) {
|
||||||
) {
|
|
||||||
sendMetadataCallback_ = sendMetadata;
|
sendMetadataCallback_ = sendMetadata;
|
||||||
sendDataCallback_ = sendData;
|
sendDataCallback_ = sendData;
|
||||||
sendCompleteCallback_ = sendComplete;
|
sendCompleteCallback_ = sendComplete;
|
||||||
@@ -25,11 +23,11 @@ void FileSystemHandler::setSendCallbacks(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FileSystemHandler::cleanupExpiredTransfers() {
|
void FileSystemHandler::cleanupExpiredTransfers() {
|
||||||
uint32_t now = millis();
|
uint32_t now = esp_timer_get_time() / 1000;
|
||||||
|
|
||||||
auto dlIt = downloads_.begin();
|
auto dlIt = downloads_.begin();
|
||||||
while (dlIt != downloads_.end()) {
|
while (dlIt != downloads_.end()) {
|
||||||
if (now - dlIt->second.lastActivityTime > FS_TRANSFER_TIMEOUT) {
|
if (now - dlIt->second.lastActivityTime > FS_TRANSFER_TIMEOUT_MS) {
|
||||||
if (dlIt->second.file) {
|
if (dlIt->second.file) {
|
||||||
dlIt->second.file.close();
|
dlIt->second.file.close();
|
||||||
}
|
}
|
||||||
@@ -53,7 +51,7 @@ void FileSystemHandler::cleanupExpiredTransfers() {
|
|||||||
|
|
||||||
auto ulIt = uploads_.begin();
|
auto ulIt = uploads_.begin();
|
||||||
while (ulIt != uploads_.end()) {
|
while (ulIt != uploads_.end()) {
|
||||||
if (now - ulIt->second.lastActivityTime > FS_TRANSFER_TIMEOUT) {
|
if (now - ulIt->second.lastActivityTime > FS_TRANSFER_TIMEOUT_MS) {
|
||||||
if (ulIt->second.file) {
|
if (ulIt->second.file) {
|
||||||
ulIt->second.file.close();
|
ulIt->second.file.close();
|
||||||
}
|
}
|
||||||
@@ -156,7 +154,8 @@ void FileSystemHandler::listDirectory(const std::string& path, socket_message_FS
|
|||||||
while (file && fileCount < 20 && dirCount < 20) { // Limit to match protobuf max_count
|
while (file && fileCount < 20 && dirCount < 20) { // Limit to match protobuf max_count
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
if (dirCount < 20) {
|
if (dirCount < 20) {
|
||||||
strncpy(response.directories[dirCount].name, file.name(), sizeof(response.directories[dirCount].name) - 1);
|
strncpy(response.directories[dirCount].name, file.name(),
|
||||||
|
sizeof(response.directories[dirCount].name) - 1);
|
||||||
dirCount++;
|
dirCount++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -248,13 +247,12 @@ void FileSystemHandler::handleDownloadRequest(const socket_message_FSDownloadReq
|
|||||||
state.chunkSize = chunkSize;
|
state.chunkSize = chunkSize;
|
||||||
state.totalChunks = totalChunks;
|
state.totalChunks = totalChunks;
|
||||||
state.chunksSent = 0;
|
state.chunksSent = 0;
|
||||||
state.lastActivityTime = millis();
|
state.lastActivityTime = esp_timer_get_time() / 1000;
|
||||||
state.clientId = clientId;
|
state.clientId = clientId;
|
||||||
|
|
||||||
downloads_[transferId] = state;
|
downloads_[transferId] = state;
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Download started: %s, size=%u, chunks=%u, id=%u",
|
ESP_LOGI(TAG, "Download started: %s, size=%u, chunks=%u, id=%u", path.c_str(), fileSize, totalChunks, transferId);
|
||||||
path.c_str(), fileSize, totalChunks, transferId);
|
|
||||||
|
|
||||||
// Start streaming chunks immediately
|
// Start streaming chunks immediately
|
||||||
while (sendNextDownloadChunk(transferId)) {
|
while (sendNextDownloadChunk(transferId)) {
|
||||||
@@ -270,7 +268,7 @@ bool FileSystemHandler::sendNextDownloadChunk(uint32_t transferId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DownloadState& state = it->second;
|
DownloadState& state = it->second;
|
||||||
state.lastActivityTime = millis();
|
state.lastActivityTime = esp_timer_get_time() / 1000;
|
||||||
|
|
||||||
if (state.chunksSent >= state.totalChunks) {
|
if (state.chunksSent >= state.totalChunks) {
|
||||||
if (sendCompleteCallback_) {
|
if (sendCompleteCallback_) {
|
||||||
@@ -378,7 +376,7 @@ socket_message_FSUploadStartResponse FileSystemHandler::handleUploadStart(const
|
|||||||
state.totalChunks = req.total_chunks;
|
state.totalChunks = req.total_chunks;
|
||||||
state.chunksReceived = 0;
|
state.chunksReceived = 0;
|
||||||
state.bytesReceived = 0;
|
state.bytesReceived = 0;
|
||||||
state.lastActivityTime = millis();
|
state.lastActivityTime = esp_timer_get_time() / 1000;
|
||||||
state.clientId = clientId;
|
state.clientId = clientId;
|
||||||
state.hasError = false;
|
state.hasError = false;
|
||||||
|
|
||||||
@@ -402,7 +400,7 @@ void FileSystemHandler::handleUploadData(const socket_message_FSUploadData& req)
|
|||||||
}
|
}
|
||||||
|
|
||||||
UploadState& state = it->second;
|
UploadState& state = it->second;
|
||||||
state.lastActivityTime = millis();
|
state.lastActivityTime = esp_timer_get_time() / 1000;
|
||||||
|
|
||||||
// Skip if we already have an error
|
// Skip if we already have an error
|
||||||
if (state.hasError) {
|
if (state.hasError) {
|
||||||
|
|||||||
@@ -315,8 +315,6 @@ void IRAM_ATTR serviceLoopEntry(void *) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
|
||||||
|
|
||||||
ESP_FS.begin();
|
ESP_FS.begin();
|
||||||
|
|
||||||
ESP_LOGI("main", "Booting robot");
|
ESP_LOGI("main", "Booting robot");
|
||||||
|
|||||||
Reference in New Issue
Block a user