## Summary Continue my changes to introduce the HAL infrastructure from https://github.com/crosspoint-reader/crosspoint-reader/pull/522 This PR touches quite a lot of files, but most of them are just name changing. It should not have any impacts to the end behavior. ## Additional Context My plan is to firstly add this small shim layer, which sounds useless at first, but then I'll implement an emulated driver which can be helpful for testing and for development. Currently, on my fork, I'm using a FS driver that allow "mounting" a local directory from my computer to the device, much like the `-v` mount option on docker. This allows me to quickly reset `.crosspoint` directory if anything goes wrong. I plan to upstream this feature when this PR get merged. --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? NO
104 lines
2.7 KiB
C++
104 lines
2.7 KiB
C++
#pragma once
|
|
|
|
#include <HalStorage.h>
|
|
#include <WebServer.h>
|
|
#include <WebSocketsServer.h>
|
|
#include <WiFiUdp.h>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// Structure to hold file information
|
|
struct FileInfo {
|
|
String name;
|
|
size_t size;
|
|
bool isEpub;
|
|
bool isDirectory;
|
|
};
|
|
|
|
class CrossPointWebServer {
|
|
public:
|
|
struct WsUploadStatus {
|
|
bool inProgress = false;
|
|
size_t received = 0;
|
|
size_t total = 0;
|
|
std::string filename;
|
|
std::string lastCompleteName;
|
|
size_t lastCompleteSize = 0;
|
|
unsigned long lastCompleteAt = 0;
|
|
};
|
|
|
|
// Used by POST upload handler
|
|
struct UploadState {
|
|
FsFile file;
|
|
String fileName;
|
|
String path = "/";
|
|
size_t size = 0;
|
|
bool success = false;
|
|
String error = "";
|
|
|
|
// Upload write buffer - batches small writes into larger SD card operations
|
|
// 4KB is a good balance: large enough to reduce syscall overhead, small enough
|
|
// to keep individual write times short and avoid watchdog issues
|
|
static constexpr size_t UPLOAD_BUFFER_SIZE = 4096; // 4KB buffer
|
|
std::vector<uint8_t> buffer;
|
|
size_t bufferPos = 0;
|
|
|
|
UploadState() { buffer.resize(UPLOAD_BUFFER_SIZE); }
|
|
} upload;
|
|
|
|
CrossPointWebServer();
|
|
~CrossPointWebServer();
|
|
|
|
// Start the web server (call after WiFi is connected)
|
|
void begin();
|
|
|
|
// Stop the web server
|
|
void stop();
|
|
|
|
// Call this periodically to handle client requests
|
|
void handleClient();
|
|
|
|
// Check if server is running
|
|
bool isRunning() const { return running; }
|
|
|
|
WsUploadStatus getWsUploadStatus() const;
|
|
|
|
// Get the port number
|
|
uint16_t getPort() const { return port; }
|
|
|
|
private:
|
|
std::unique_ptr<WebServer> server = nullptr;
|
|
std::unique_ptr<WebSocketsServer> wsServer = nullptr;
|
|
bool running = false;
|
|
bool apMode = false; // true when running in AP mode, false for STA mode
|
|
uint16_t port = 80;
|
|
uint16_t wsPort = 81; // WebSocket port
|
|
WiFiUDP udp;
|
|
bool udpActive = false;
|
|
|
|
// WebSocket upload state
|
|
void onWebSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length);
|
|
static void wsEventCallback(uint8_t num, WStype_t type, uint8_t* payload, size_t length);
|
|
|
|
// File scanning
|
|
void scanFiles(const char* path, const std::function<void(FileInfo)>& callback) const;
|
|
String formatFileSize(size_t bytes) const;
|
|
bool isEpubFile(const String& filename) const;
|
|
|
|
// Request handlers
|
|
void handleRoot() const;
|
|
void handleNotFound() const;
|
|
void handleStatus() const;
|
|
void handleFileList() const;
|
|
void handleFileListData() const;
|
|
void handleDownload() const;
|
|
void handleUpload(UploadState& state) const;
|
|
void handleUploadPost(UploadState& state) const;
|
|
void handleCreateFolder() const;
|
|
void handleRename() const;
|
|
void handleMove() const;
|
|
void handleDelete() const;
|
|
};
|