clang format

This commit is contained in:
Brendan O'Leary 2025-12-16 21:42:36 -05:00
parent e9e6982eea
commit 05da79f6ad
9 changed files with 192 additions and 198 deletions

View File

@ -2,6 +2,7 @@
#include <SD.h> #include <SD.h>
#include <WiFi.h> #include <WiFi.h>
#include <algorithm> #include <algorithm>
#include "config.h" #include "config.h"
@ -11,26 +12,35 @@ CrossPointWebServer crossPointWebServer;
// Folders/files to hide from the web interface file browser // Folders/files to hide from the web interface file browser
// Note: Items starting with "." are automatically hidden // Note: Items starting with "." are automatically hidden
static const char* HIDDEN_ITEMS[] = { static const char* HIDDEN_ITEMS[] = {"System Volume Information", "XTCache"};
"System Volume Information",
"XTCache"
};
static const size_t HIDDEN_ITEMS_COUNT = sizeof(HIDDEN_ITEMS) / sizeof(HIDDEN_ITEMS[0]); static const size_t HIDDEN_ITEMS_COUNT = sizeof(HIDDEN_ITEMS) / sizeof(HIDDEN_ITEMS[0]);
// Helper function to escape HTML special characters to prevent XSS // Helper function to escape HTML special characters to prevent XSS
static String escapeHtml(const String& input) { static String escapeHtml(const String& input) {
String output; String output;
output.reserve(input.length() * 1.1); // Pre-allocate with some extra space output.reserve(input.length() * 1.1); // Pre-allocate with some extra space
for (size_t i = 0; i < input.length(); i++) { for (size_t i = 0; i < input.length(); i++) {
char c = input.charAt(i); char c = input.charAt(i);
switch (c) { switch (c) {
case '&': output += "&amp;"; break; case '&':
case '<': output += "&lt;"; break; output += "&amp;";
case '>': output += "&gt;"; break; break;
case '"': output += "&quot;"; break; case '<':
case '\'': output += "&#39;"; break; output += "&lt;";
default: output += c; break; break;
case '>':
output += "&gt;";
break;
case '"':
output += "&quot;";
break;
case '\'':
output += "&#39;";
break;
default:
output += c;
break;
} }
} }
return output; return output;
@ -849,9 +859,7 @@ static const char* FILES_PAGE_FOOTER = R"rawliteral(
CrossPointWebServer::CrossPointWebServer() {} CrossPointWebServer::CrossPointWebServer() {}
CrossPointWebServer::~CrossPointWebServer() { CrossPointWebServer::~CrossPointWebServer() { stop(); }
stop();
}
void CrossPointWebServer::begin() { void CrossPointWebServer::begin() {
if (running) { if (running) {
@ -877,16 +885,16 @@ void CrossPointWebServer::begin() {
server->on("/", HTTP_GET, [this]() { handleRoot(); }); server->on("/", HTTP_GET, [this]() { handleRoot(); });
server->on("/status", HTTP_GET, [this]() { handleStatus(); }); server->on("/status", HTTP_GET, [this]() { handleStatus(); });
server->on("/files", HTTP_GET, [this]() { handleFileList(); }); server->on("/files", HTTP_GET, [this]() { handleFileList(); });
// Upload endpoint with special handling for multipart form data // Upload endpoint with special handling for multipart form data
server->on("/upload", HTTP_POST, [this]() { handleUploadPost(); }, [this]() { handleUpload(); }); server->on("/upload", HTTP_POST, [this]() { handleUploadPost(); }, [this]() { handleUpload(); });
// Create folder endpoint // Create folder endpoint
server->on("/mkdir", HTTP_POST, [this]() { handleCreateFolder(); }); server->on("/mkdir", HTTP_POST, [this]() { handleCreateFolder(); });
// Delete file/folder endpoint // Delete file/folder endpoint
server->on("/delete", HTTP_POST, [this]() { handleDelete(); }); server->on("/delete", HTTP_POST, [this]() { handleDelete(); });
server->onNotFound([this]() { handleNotFound(); }); server->onNotFound([this]() { handleNotFound(); });
server->begin(); server->begin();
@ -953,13 +961,13 @@ void CrossPointWebServer::handleStatus() {
std::vector<FileInfo> CrossPointWebServer::scanFiles(const char* path) { std::vector<FileInfo> CrossPointWebServer::scanFiles(const char* path) {
std::vector<FileInfo> files; std::vector<FileInfo> files;
File root = SD.open(path); File root = SD.open(path);
if (!root) { if (!root) {
Serial.printf("[%lu] [WEB] Failed to open directory: %s\n", millis(), path); Serial.printf("[%lu] [WEB] Failed to open directory: %s\n", millis(), path);
return files; return files;
} }
if (!root.isDirectory()) { if (!root.isDirectory()) {
Serial.printf("[%lu] [WEB] Not a directory: %s\n", millis(), path); Serial.printf("[%lu] [WEB] Not a directory: %s\n", millis(), path);
root.close(); root.close();
@ -967,14 +975,14 @@ std::vector<FileInfo> CrossPointWebServer::scanFiles(const char* path) {
} }
Serial.printf("[%lu] [WEB] Scanning files in: %s\n", millis(), path); Serial.printf("[%lu] [WEB] Scanning files in: %s\n", millis(), path);
File file = root.openNextFile(); File file = root.openNextFile();
while (file) { while (file) {
String fileName = String(file.name()); String fileName = String(file.name());
// Skip hidden items (starting with ".") // Skip hidden items (starting with ".")
bool shouldHide = fileName.startsWith("."); bool shouldHide = fileName.startsWith(".");
// Check against explicitly hidden items list // Check against explicitly hidden items list
if (!shouldHide) { if (!shouldHide) {
for (size_t i = 0; i < HIDDEN_ITEMS_COUNT; i++) { for (size_t i = 0; i < HIDDEN_ITEMS_COUNT; i++) {
@ -984,12 +992,12 @@ std::vector<FileInfo> CrossPointWebServer::scanFiles(const char* path) {
} }
} }
} }
if (!shouldHide) { if (!shouldHide) {
FileInfo info; FileInfo info;
info.name = fileName; info.name = fileName;
info.isDirectory = file.isDirectory(); info.isDirectory = file.isDirectory();
if (info.isDirectory) { if (info.isDirectory) {
info.size = 0; info.size = 0;
info.isEpub = false; info.isEpub = false;
@ -997,15 +1005,15 @@ std::vector<FileInfo> CrossPointWebServer::scanFiles(const char* path) {
info.size = file.size(); info.size = file.size();
info.isEpub = isEpubFile(info.name); info.isEpub = isEpubFile(info.name);
} }
files.push_back(info); files.push_back(info);
} }
file.close(); file.close();
file = root.openNextFile(); file = root.openNextFile();
} }
root.close(); root.close();
Serial.printf("[%lu] [WEB] Found %d items (files and folders)\n", millis(), files.size()); Serial.printf("[%lu] [WEB] Found %d items (files and folders)\n", millis(), files.size());
return files; return files;
} }
@ -1028,7 +1036,7 @@ bool CrossPointWebServer::isEpubFile(const String& filename) {
void CrossPointWebServer::handleFileList() { void CrossPointWebServer::handleFileList() {
String html = FILES_PAGE_HEADER; String html = FILES_PAGE_HEADER;
// Get current path from query string (default to root) // Get current path from query string (default to root)
String currentPath = "/"; String currentPath = "/";
if (server->hasArg("path")) { if (server->hasArg("path")) {
@ -1042,20 +1050,20 @@ void CrossPointWebServer::handleFileList() {
currentPath = currentPath.substring(0, currentPath.length() - 1); currentPath = currentPath.substring(0, currentPath.length() - 1);
} }
} }
// Get message from query string if present // Get message from query string if present
if (server->hasArg("msg")) { if (server->hasArg("msg")) {
String msg = escapeHtml(server->arg("msg")); String msg = escapeHtml(server->arg("msg"));
String msgType = server->hasArg("type") ? escapeHtml(server->arg("type")) : "success"; String msgType = server->hasArg("type") ? escapeHtml(server->arg("type")) : "success";
html += "<div class=\"message " + msgType + "\">" + msg + "</div>"; html += "<div class=\"message " + msgType + "\">" + msg + "</div>";
} }
// Hidden input to store current path for JavaScript // Hidden input to store current path for JavaScript
html += "<input type=\"hidden\" id=\"currentPath\" value=\"" + currentPath + "\">"; html += "<input type=\"hidden\" id=\"currentPath\" value=\"" + currentPath + "\">";
// Scan files in current path first (we need counts for the header) // Scan files in current path first (we need counts for the header)
std::vector<FileInfo> files = scanFiles(currentPath.c_str()); std::vector<FileInfo> files = scanFiles(currentPath.c_str());
// Count items // Count items
int epubCount = 0; int epubCount = 0;
int folderCount = 0; int folderCount = 0;
@ -1068,25 +1076,25 @@ void CrossPointWebServer::handleFileList() {
totalSize += file.size; totalSize += file.size;
} }
} }
// Page header with inline breadcrumb and +Add dropdown // Page header with inline breadcrumb and +Add dropdown
html += "<div class=\"page-header\">"; html += "<div class=\"page-header\">";
html += "<div class=\"page-header-left\">"; html += "<div class=\"page-header-left\">";
html += "<h1>📁 File Manager</h1>"; html += "<h1>📁 File Manager</h1>";
// Inline breadcrumb // Inline breadcrumb
html += "<div class=\"breadcrumb-inline\">"; html += "<div class=\"breadcrumb-inline\">";
html += "<span class=\"sep\">/</span>"; html += "<span class=\"sep\">/</span>";
if (currentPath == "/") { if (currentPath == "/") {
html += "<span class=\"current\">🏠</span>"; html += "<span class=\"current\">🏠</span>";
} else { } else {
html += "<a href=\"/files\">🏠</a>"; html += "<a href=\"/files\">🏠</a>";
String pathParts = currentPath.substring(1); // Remove leading / String pathParts = currentPath.substring(1); // Remove leading /
String buildPath = ""; String buildPath = "";
int start = 0; int start = 0;
int end = pathParts.indexOf('/'); int end = pathParts.indexOf('/');
while (start < (int)pathParts.length()) { while (start < (int)pathParts.length()) {
String part; String part;
if (end == -1) { if (end == -1) {
@ -1105,7 +1113,7 @@ void CrossPointWebServer::handleFileList() {
} }
html += "</div>"; html += "</div>";
html += "</div>"; html += "</div>";
// +Add dropdown button // +Add dropdown button
html += "<div class=\"add-dropdown\" id=\"addDropdown\">"; html += "<div class=\"add-dropdown\" id=\"addDropdown\">";
html += "<button class=\"add-btn\" onclick=\"toggleDropdown()\">"; html += "<button class=\"add-btn\" onclick=\"toggleDropdown()\">";
@ -1121,12 +1129,12 @@ void CrossPointWebServer::handleFileList() {
html += "</button>"; html += "</button>";
html += "</div>"; html += "</div>";
html += "</div>"; html += "</div>";
html += "</div>"; // end page-header html += "</div>"; // end page-header
// Contents card with inline summary // Contents card with inline summary
html += "<div class=\"card\">"; html += "<div class=\"card\">";
// Contents header with inline stats // Contents header with inline stats
html += "<div class=\"contents-header\">"; html += "<div class=\"contents-header\">";
html += "<h2 class=\"contents-title\">Contents</h2>"; html += "<h2 class=\"contents-title\">Contents</h2>";
@ -1136,13 +1144,13 @@ void CrossPointWebServer::handleFileList() {
html += formatFileSize(totalSize); html += formatFileSize(totalSize);
html += "</span>"; html += "</span>";
html += "</div>"; html += "</div>";
if (files.empty()) { if (files.empty()) {
html += "<div class=\"no-files\">This folder is empty</div>"; html += "<div class=\"no-files\">This folder is empty</div>";
} else { } else {
html += "<table class=\"file-table\">"; html += "<table class=\"file-table\">";
html += "<tr><th>Name</th><th>Type</th><th>Size</th><th class=\"actions-col\">Actions</th></tr>"; html += "<tr><th>Name</th><th>Type</th><th>Size</th><th class=\"actions-col\">Actions</th></tr>";
// Sort files: folders first, then epub files, then other files, alphabetically within each group // Sort files: folders first, then epub files, then other files, alphabetically within each group
std::sort(files.begin(), files.end(), [](const FileInfo& a, const FileInfo& b) { std::sort(files.begin(), files.end(), [](const FileInfo& a, const FileInfo& b) {
// Folders come first // Folders come first
@ -1154,29 +1162,30 @@ void CrossPointWebServer::handleFileList() {
// Then alphabetically // Then alphabetically
return a.name < b.name; return a.name < b.name;
}); });
for (const auto& file : files) { for (const auto& file : files) {
String rowClass; String rowClass;
String icon; String icon;
String badge; String badge;
String typeStr; String typeStr;
String sizeStr; String sizeStr;
if (file.isDirectory) { if (file.isDirectory) {
rowClass = "folder-row"; rowClass = "folder-row";
icon = "📁"; icon = "📁";
badge = "<span class=\"folder-badge\">FOLDER</span>"; badge = "<span class=\"folder-badge\">FOLDER</span>";
typeStr = "Folder"; typeStr = "Folder";
sizeStr = "-"; sizeStr = "-";
// Build the path to this folder // Build the path to this folder
String folderPath = currentPath; String folderPath = currentPath;
if (!folderPath.endsWith("/")) folderPath += "/"; if (!folderPath.endsWith("/")) folderPath += "/";
folderPath += file.name; folderPath += file.name;
html += "<tr class=\"" + rowClass + "\">"; html += "<tr class=\"" + rowClass + "\">";
html += "<td><span class=\"file-icon\">" + icon + "</span>"; html += "<td><span class=\"file-icon\">" + icon + "</span>";
html += "<a href=\"/files?path=" + folderPath + "\" class=\"folder-link\">" + escapeHtml(file.name) + "</a>" + badge + "</td>"; html += "<a href=\"/files?path=" + folderPath + "\" class=\"folder-link\">" + escapeHtml(file.name) + "</a>" +
badge + "</td>";
html += "<td>" + typeStr + "</td>"; html += "<td>" + typeStr + "</td>";
html += "<td>" + sizeStr + "</td>"; html += "<td>" + sizeStr + "</td>";
// Escape quotes for JavaScript string // Escape quotes for JavaScript string
@ -1184,7 +1193,8 @@ void CrossPointWebServer::handleFileList() {
escapedName.replace("'", "\\'"); escapedName.replace("'", "\\'");
String escapedPath = folderPath; String escapedPath = folderPath;
escapedPath.replace("'", "\\'"); escapedPath.replace("'", "\\'");
html += "<td class=\"actions-col\"><button class=\"delete-btn\" onclick=\"openDeleteModal('" + escapedName + "', '" + escapedPath + "', true)\" title=\"Delete folder\">🗑️</button></td>"; html += "<td class=\"actions-col\"><button class=\"delete-btn\" onclick=\"openDeleteModal('" + escapedName +
"', '" + escapedPath + "', true)\" title=\"Delete folder\">🗑️</button></td>";
html += "</tr>"; html += "</tr>";
} else { } else {
rowClass = file.isEpub ? "epub-file" : ""; rowClass = file.isEpub ? "epub-file" : "";
@ -1194,12 +1204,12 @@ void CrossPointWebServer::handleFileList() {
ext.toUpperCase(); ext.toUpperCase();
typeStr = ext; typeStr = ext;
sizeStr = formatFileSize(file.size); sizeStr = formatFileSize(file.size);
// Build file path for delete // Build file path for delete
String filePath = currentPath; String filePath = currentPath;
if (!filePath.endsWith("/")) filePath += "/"; if (!filePath.endsWith("/")) filePath += "/";
filePath += file.name; filePath += file.name;
html += "<tr class=\"" + rowClass + "\">"; html += "<tr class=\"" + rowClass + "\">";
html += "<td><span class=\"file-icon\">" + icon + "</span>" + escapeHtml(file.name) + badge + "</td>"; html += "<td><span class=\"file-icon\">" + icon + "</span>" + escapeHtml(file.name) + badge + "</td>";
html += "<td>" + typeStr + "</td>"; html += "<td>" + typeStr + "</td>";
@ -1209,18 +1219,19 @@ void CrossPointWebServer::handleFileList() {
escapedName.replace("'", "\\'"); escapedName.replace("'", "\\'");
String escapedPath = filePath; String escapedPath = filePath;
escapedPath.replace("'", "\\'"); escapedPath.replace("'", "\\'");
html += "<td class=\"actions-col\"><button class=\"delete-btn\" onclick=\"openDeleteModal('" + escapedName + "', '" + escapedPath + "', false)\" title=\"Delete file\">🗑️</button></td>"; html += "<td class=\"actions-col\"><button class=\"delete-btn\" onclick=\"openDeleteModal('" + escapedName +
"', '" + escapedPath + "', false)\" title=\"Delete file\">🗑️</button></td>";
html += "</tr>"; html += "</tr>";
} }
} }
html += "</table>"; html += "</table>";
} }
html += "</div>"; html += "</div>";
html += FILES_PAGE_FOOTER; html += FILES_PAGE_FOOTER;
server->send(200, "text/html", html); server->send(200, "text/html", html);
Serial.printf("[%lu] [WEB] Served file listing page for path: %s\n", millis(), currentPath.c_str()); Serial.printf("[%lu] [WEB] Served file listing page for path: %s\n", millis(), currentPath.c_str());
} }
@ -1235,13 +1246,13 @@ static String uploadError = "";
void CrossPointWebServer::handleUpload() { void CrossPointWebServer::handleUpload() {
HTTPUpload& upload = server->upload(); HTTPUpload& upload = server->upload();
if (upload.status == UPLOAD_FILE_START) { if (upload.status == UPLOAD_FILE_START) {
uploadFileName = upload.filename; uploadFileName = upload.filename;
uploadSize = 0; uploadSize = 0;
uploadSuccess = false; uploadSuccess = false;
uploadError = ""; uploadError = "";
// Get upload path from query parameter (defaults to root if not specified) // Get upload path from query parameter (defaults to root if not specified)
// Note: We use query parameter instead of form data because multipart form // Note: We use query parameter instead of form data because multipart form
// fields aren't available until after file upload completes // fields aren't available until after file upload completes
@ -1258,27 +1269,27 @@ void CrossPointWebServer::handleUpload() {
} else { } else {
uploadPath = "/"; uploadPath = "/";
} }
Serial.printf("[%lu] [WEB] Upload start: %s to path: %s\n", millis(), uploadFileName.c_str(), uploadPath.c_str()); Serial.printf("[%lu] [WEB] Upload start: %s to path: %s\n", millis(), uploadFileName.c_str(), uploadPath.c_str());
// Validate file extension // Validate file extension
if (!isEpubFile(uploadFileName)) { if (!isEpubFile(uploadFileName)) {
uploadError = "Only .epub files are allowed"; uploadError = "Only .epub files are allowed";
Serial.printf("[%lu] [WEB] Upload rejected - not an epub file\n", millis()); Serial.printf("[%lu] [WEB] Upload rejected - not an epub file\n", millis());
return; return;
} }
// Create file path // Create file path
String filePath = uploadPath; String filePath = uploadPath;
if (!filePath.endsWith("/")) filePath += "/"; if (!filePath.endsWith("/")) filePath += "/";
filePath += uploadFileName; filePath += uploadFileName;
// Check if file already exists // Check if file already exists
if (SD.exists(filePath.c_str())) { if (SD.exists(filePath.c_str())) {
Serial.printf("[%lu] [WEB] Overwriting existing file: %s\n", millis(), filePath.c_str()); Serial.printf("[%lu] [WEB] Overwriting existing file: %s\n", millis(), filePath.c_str());
SD.remove(filePath.c_str()); SD.remove(filePath.c_str());
} }
// Open file for writing // Open file for writing
uploadFile = SD.open(filePath.c_str(), FILE_WRITE); uploadFile = SD.open(filePath.c_str(), FILE_WRITE);
if (!uploadFile) { if (!uploadFile) {
@ -1286,10 +1297,9 @@ void CrossPointWebServer::handleUpload() {
Serial.printf("[%lu] [WEB] Failed to create file: %s\n", millis(), filePath.c_str()); Serial.printf("[%lu] [WEB] Failed to create file: %s\n", millis(), filePath.c_str());
return; return;
} }
Serial.printf("[%lu] [WEB] File created: %s\n", millis(), filePath.c_str()); Serial.printf("[%lu] [WEB] File created: %s\n", millis(), filePath.c_str());
} } else if (upload.status == UPLOAD_FILE_WRITE) {
else if (upload.status == UPLOAD_FILE_WRITE) {
if (uploadFile && uploadError.isEmpty()) { if (uploadFile && uploadError.isEmpty()) {
size_t written = uploadFile.write(upload.buf, upload.currentSize); size_t written = uploadFile.write(upload.buf, upload.currentSize);
if (written != upload.currentSize) { if (written != upload.currentSize) {
@ -1300,18 +1310,16 @@ void CrossPointWebServer::handleUpload() {
uploadSize += written; uploadSize += written;
} }
} }
} } else if (upload.status == UPLOAD_FILE_END) {
else if (upload.status == UPLOAD_FILE_END) {
if (uploadFile) { if (uploadFile) {
uploadFile.close(); uploadFile.close();
if (uploadError.isEmpty()) { if (uploadError.isEmpty()) {
uploadSuccess = true; uploadSuccess = true;
Serial.printf("[%lu] [WEB] Upload complete: %s (%d bytes)\n", millis(), uploadFileName.c_str(), uploadSize); Serial.printf("[%lu] [WEB] Upload complete: %s (%d bytes)\n", millis(), uploadFileName.c_str(), uploadSize);
} }
} }
} } else if (upload.status == UPLOAD_FILE_ABORTED) {
else if (upload.status == UPLOAD_FILE_ABORTED) {
if (uploadFile) { if (uploadFile) {
uploadFile.close(); uploadFile.close();
// Try to delete the incomplete file // Try to delete the incomplete file
@ -1340,15 +1348,15 @@ void CrossPointWebServer::handleCreateFolder() {
server->send(400, "text/plain", "Missing folder name"); server->send(400, "text/plain", "Missing folder name");
return; return;
} }
String folderName = server->arg("name"); String folderName = server->arg("name");
// Validate folder name // Validate folder name
if (folderName.isEmpty()) { if (folderName.isEmpty()) {
server->send(400, "text/plain", "Folder name cannot be empty"); server->send(400, "text/plain", "Folder name cannot be empty");
return; return;
} }
// Get parent path // Get parent path
String parentPath = "/"; String parentPath = "/";
if (server->hasArg("path")) { if (server->hasArg("path")) {
@ -1360,20 +1368,20 @@ void CrossPointWebServer::handleCreateFolder() {
parentPath = parentPath.substring(0, parentPath.length() - 1); parentPath = parentPath.substring(0, parentPath.length() - 1);
} }
} }
// Build full folder path // Build full folder path
String folderPath = parentPath; String folderPath = parentPath;
if (!folderPath.endsWith("/")) folderPath += "/"; if (!folderPath.endsWith("/")) folderPath += "/";
folderPath += folderName; folderPath += folderName;
Serial.printf("[%lu] [WEB] Creating folder: %s\n", millis(), folderPath.c_str()); Serial.printf("[%lu] [WEB] Creating folder: %s\n", millis(), folderPath.c_str());
// Check if already exists // Check if already exists
if (SD.exists(folderPath.c_str())) { if (SD.exists(folderPath.c_str())) {
server->send(400, "text/plain", "Folder already exists"); server->send(400, "text/plain", "Folder already exists");
return; return;
} }
// Create the folder // Create the folder
if (SD.mkdir(folderPath.c_str())) { if (SD.mkdir(folderPath.c_str())) {
Serial.printf("[%lu] [WEB] Folder created successfully: %s\n", millis(), folderPath.c_str()); Serial.printf("[%lu] [WEB] Folder created successfully: %s\n", millis(), folderPath.c_str());
@ -1390,31 +1398,31 @@ void CrossPointWebServer::handleDelete() {
server->send(400, "text/plain", "Missing path"); server->send(400, "text/plain", "Missing path");
return; return;
} }
String itemPath = server->arg("path"); String itemPath = server->arg("path");
String itemType = server->hasArg("type") ? server->arg("type") : "file"; String itemType = server->hasArg("type") ? server->arg("type") : "file";
// Validate path // Validate path
if (itemPath.isEmpty() || itemPath == "/") { if (itemPath.isEmpty() || itemPath == "/") {
server->send(400, "text/plain", "Cannot delete root directory"); server->send(400, "text/plain", "Cannot delete root directory");
return; return;
} }
// Ensure path starts with / // Ensure path starts with /
if (!itemPath.startsWith("/")) { if (!itemPath.startsWith("/")) {
itemPath = "/" + itemPath; itemPath = "/" + itemPath;
} }
// Security check: prevent deletion of protected items // Security check: prevent deletion of protected items
String itemName = itemPath.substring(itemPath.lastIndexOf('/') + 1); String itemName = itemPath.substring(itemPath.lastIndexOf('/') + 1);
// Check if item starts with a dot (hidden/system file) // Check if item starts with a dot (hidden/system file)
if (itemName.startsWith(".")) { if (itemName.startsWith(".")) {
Serial.printf("[%lu] [WEB] Delete rejected - hidden/system item: %s\n", millis(), itemPath.c_str()); Serial.printf("[%lu] [WEB] Delete rejected - hidden/system item: %s\n", millis(), itemPath.c_str());
server->send(403, "text/plain", "Cannot delete system files"); server->send(403, "text/plain", "Cannot delete system files");
return; return;
} }
// Check against explicitly protected items // Check against explicitly protected items
for (size_t i = 0; i < HIDDEN_ITEMS_COUNT; i++) { for (size_t i = 0; i < HIDDEN_ITEMS_COUNT; i++) {
if (itemName.equals(HIDDEN_ITEMS[i])) { if (itemName.equals(HIDDEN_ITEMS[i])) {
@ -1423,18 +1431,18 @@ void CrossPointWebServer::handleDelete() {
return; return;
} }
} }
// Check if item exists // Check if item exists
if (!SD.exists(itemPath.c_str())) { if (!SD.exists(itemPath.c_str())) {
Serial.printf("[%lu] [WEB] Delete failed - item not found: %s\n", millis(), itemPath.c_str()); Serial.printf("[%lu] [WEB] Delete failed - item not found: %s\n", millis(), itemPath.c_str());
server->send(404, "text/plain", "Item not found"); server->send(404, "text/plain", "Item not found");
return; return;
} }
Serial.printf("[%lu] [WEB] Attempting to delete %s: %s\n", millis(), itemType.c_str(), itemPath.c_str()); Serial.printf("[%lu] [WEB] Attempting to delete %s: %s\n", millis(), itemType.c_str(), itemPath.c_str());
bool success = false; bool success = false;
if (itemType == "folder") { if (itemType == "folder") {
// For folders, try to remove (will fail if not empty) // For folders, try to remove (will fail if not empty)
File dir = SD.open(itemPath.c_str()); File dir = SD.open(itemPath.c_str());
@ -1456,7 +1464,7 @@ void CrossPointWebServer::handleDelete() {
// For files, use remove // For files, use remove
success = SD.remove(itemPath.c_str()); success = SD.remove(itemPath.c_str());
} }
if (success) { if (success) {
Serial.printf("[%lu] [WEB] Successfully deleted: %s\n", millis(), itemPath.c_str()); Serial.printf("[%lu] [WEB] Successfully deleted: %s\n", millis(), itemPath.c_str());
server->send(200, "text/plain", "Deleted successfully"); server->send(200, "text/plain", "Deleted successfully");

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <WebServer.h> #include <WebServer.h>
#include <functional> #include <functional>
#include <string> #include <string>
#include <vector> #include <vector>

View File

@ -17,8 +17,7 @@ constexpr char WIFI_FILE[] = "/sd/.crosspoint/wifi.bin";
// Obfuscation key - "CrossPoint" in ASCII // Obfuscation key - "CrossPoint" in ASCII
// This is NOT cryptographic security, just prevents casual file reading // This is NOT cryptographic security, just prevents casual file reading
constexpr uint8_t OBFUSCATION_KEY[] = {0x43, 0x72, 0x6F, 0x73, 0x73, constexpr uint8_t OBFUSCATION_KEY[] = {0x43, 0x72, 0x6F, 0x73, 0x73, 0x50, 0x6F, 0x69, 0x6E, 0x74};
0x50, 0x6F, 0x69, 0x6E, 0x74};
constexpr size_t KEY_LENGTH = sizeof(OBFUSCATION_KEY); constexpr size_t KEY_LENGTH = sizeof(OBFUSCATION_KEY);
void WifiCredentialStore::obfuscate(std::string& data) const { void WifiCredentialStore::obfuscate(std::string& data) const {
@ -46,7 +45,8 @@ bool WifiCredentialStore::saveToFile() const {
for (const auto& cred : credentials) { for (const auto& cred : credentials) {
// Write SSID (plaintext - not sensitive) // Write SSID (plaintext - not sensitive)
serialization::writeString(file, cred.ssid); serialization::writeString(file, cred.ssid);
Serial.printf("[%lu] [WCS] Saving SSID: %s, password length: %zu\n", millis(), cred.ssid.c_str(), cred.password.size()); Serial.printf("[%lu] [WCS] Saving SSID: %s, password length: %zu\n", millis(), cred.ssid.c_str(),
cred.password.size());
// Write password (obfuscated) // Write password (obfuscated)
std::string obfuscatedPwd = cred.password; std::string obfuscatedPwd = cred.password;
@ -94,7 +94,8 @@ bool WifiCredentialStore::loadFromFile() {
// Read and deobfuscate password // Read and deobfuscate password
serialization::readString(file, cred.password); serialization::readString(file, cred.password);
Serial.printf("[%lu] [WCS] Loaded SSID: %s, obfuscated password length: %zu\n", millis(), cred.ssid.c_str(), cred.password.size()); Serial.printf("[%lu] [WCS] Loaded SSID: %s, obfuscated password length: %zu\n", millis(), cred.ssid.c_str(),
cred.password.size());
obfuscate(cred.password); // XOR is symmetric, so same function deobfuscates obfuscate(cred.password); // XOR is symmetric, so same function deobfuscates
Serial.printf("[%lu] [WCS] After deobfuscation, password length: %zu\n", millis(), cred.password.size()); Serial.printf("[%lu] [WCS] After deobfuscation, password length: %zu\n", millis(), cred.password.size());
@ -148,9 +149,7 @@ const WifiCredential* WifiCredentialStore::findCredential(const std::string& ssi
return nullptr; return nullptr;
} }
bool WifiCredentialStore::hasSavedCredential(const std::string& ssid) const { bool WifiCredentialStore::hasSavedCredential(const std::string& ssid) const { return findCredential(ssid) != nullptr; }
return findCredential(ssid) != nullptr;
}
void WifiCredentialStore::clearAll() { void WifiCredentialStore::clearAll() {
credentials.clear(); credentials.clear();

View File

@ -4,25 +4,16 @@
// Keyboard layouts - lowercase // Keyboard layouts - lowercase
const char* const OnScreenKeyboard::keyboard[NUM_ROWS] = { const char* const OnScreenKeyboard::keyboard[NUM_ROWS] = {
"`1234567890-=", "`1234567890-=", "qwertyuiop[]\\", "asdfghjkl;'", "zxcvbnm,./",
"qwertyuiop[]\\",
"asdfghjkl;'",
"zxcvbnm,./",
"^ _____<OK" // ^ = shift, _ = space, < = backspace, OK = done "^ _____<OK" // ^ = shift, _ = space, < = backspace, OK = done
}; };
// Keyboard layouts - uppercase/symbols // Keyboard layouts - uppercase/symbols
const char* const OnScreenKeyboard::keyboardShift[NUM_ROWS] = { const char* const OnScreenKeyboard::keyboardShift[NUM_ROWS] = {"~!@#$%^&*()_+", "QWERTYUIOP{}|", "ASDFGHJKL:\"",
"~!@#$%^&*()_+", "ZXCVBNM<>?", "^ _____<OK"};
"QWERTYUIOP{}|",
"ASDFGHJKL:\"",
"ZXCVBNM<>?",
"^ _____<OK"
};
OnScreenKeyboard::OnScreenKeyboard(GfxRenderer& renderer, InputManager& inputManager, OnScreenKeyboard::OnScreenKeyboard(GfxRenderer& renderer, InputManager& inputManager, const std::string& title,
const std::string& title, const std::string& initialText, const std::string& initialText, size_t maxLength, bool isPassword)
size_t maxLength, bool isPassword)
: renderer(renderer), : renderer(renderer),
inputManager(inputManager), inputManager(inputManager),
title(title), title(title),
@ -51,24 +42,30 @@ void OnScreenKeyboard::reset(const std::string& newTitle, const std::string& new
int OnScreenKeyboard::getRowLength(int row) const { int OnScreenKeyboard::getRowLength(int row) const {
if (row < 0 || row >= NUM_ROWS) return 0; if (row < 0 || row >= NUM_ROWS) return 0;
// Return actual length of each row based on keyboard layout // Return actual length of each row based on keyboard layout
switch (row) { switch (row) {
case 0: return 13; // `1234567890-= case 0:
case 1: return 13; // qwertyuiop[]backslash return 13; // `1234567890-=
case 2: return 11; // asdfghjkl;' case 1:
case 3: return 10; // zxcvbnm,./ return 13; // qwertyuiop[]backslash
case 4: return 10; // ^, space (5 wide), backspace, OK (2 wide) case 2:
default: return 0; return 11; // asdfghjkl;'
case 3:
return 10; // zxcvbnm,./
case 4:
return 10; // ^, space (5 wide), backspace, OK (2 wide)
default:
return 0;
} }
} }
char OnScreenKeyboard::getSelectedChar() const { char OnScreenKeyboard::getSelectedChar() const {
const char* const* layout = shiftActive ? keyboardShift : keyboard; const char* const* layout = shiftActive ? keyboardShift : keyboard;
if (selectedRow < 0 || selectedRow >= NUM_ROWS) return '\0'; if (selectedRow < 0 || selectedRow >= NUM_ROWS) return '\0';
if (selectedCol < 0 || selectedCol >= getRowLength(selectedRow)) return '\0'; if (selectedCol < 0 || selectedCol >= getRowLength(selectedRow)) return '\0';
return layout[selectedRow][selectedCol]; return layout[selectedRow][selectedCol];
} }
@ -80,7 +77,7 @@ void OnScreenKeyboard::handleKeyPress() {
shiftActive = !shiftActive; shiftActive = !shiftActive;
return; return;
} }
if (selectedCol >= SPACE_COL && selectedCol < BACKSPACE_COL) { if (selectedCol >= SPACE_COL && selectedCol < BACKSPACE_COL) {
// Space bar // Space bar
if (maxLength == 0 || text.length() < maxLength) { if (maxLength == 0 || text.length() < maxLength) {
@ -88,7 +85,7 @@ void OnScreenKeyboard::handleKeyPress() {
} }
return; return;
} }
if (selectedCol == BACKSPACE_COL) { if (selectedCol == BACKSPACE_COL) {
// Backspace // Backspace
if (!text.empty()) { if (!text.empty()) {
@ -96,7 +93,7 @@ void OnScreenKeyboard::handleKeyPress() {
} }
return; return;
} }
if (selectedCol >= DONE_COL) { if (selectedCol >= DONE_COL) {
// Done button // Done button
complete = true; complete = true;
@ -106,7 +103,7 @@ void OnScreenKeyboard::handleKeyPress() {
return; return;
} }
} }
// Regular character // Regular character
char c = getSelectedChar(); char c = getSelectedChar();
if (c != '\0' && c != '^' && c != '_' && c != '<') { if (c != '\0' && c != '^' && c != '_' && c != '<') {
@ -184,24 +181,24 @@ bool OnScreenKeyboard::handleInput() {
void OnScreenKeyboard::render(int startY) const { void OnScreenKeyboard::render(int startY) const {
const auto pageWidth = GfxRenderer::getScreenWidth(); const auto pageWidth = GfxRenderer::getScreenWidth();
// Draw title // Draw title
renderer.drawCenteredText(UI_FONT_ID, startY, title.c_str(), true, REGULAR); renderer.drawCenteredText(UI_FONT_ID, startY, title.c_str(), true, REGULAR);
// Draw input field // Draw input field
int inputY = startY + 22; int inputY = startY + 22;
renderer.drawText(UI_FONT_ID, 10, inputY, "["); renderer.drawText(UI_FONT_ID, 10, inputY, "[");
std::string displayText; std::string displayText;
if (isPassword) { if (isPassword) {
displayText = std::string(text.length(), '*'); displayText = std::string(text.length(), '*');
} else { } else {
displayText = text; displayText = text;
} }
// Show cursor at end // Show cursor at end
displayText += "_"; displayText += "_";
// Truncate if too long for display - use actual character width from font // Truncate if too long for display - use actual character width from font
int charWidth = renderer.getSpaceWidth(UI_FONT_ID); int charWidth = renderer.getSpaceWidth(UI_FONT_ID);
if (charWidth < 1) charWidth = 8; // Fallback to approximate width if (charWidth < 1) charWidth = 8; // Fallback to approximate width
@ -209,35 +206,35 @@ void OnScreenKeyboard::render(int startY) const {
if (displayText.length() > static_cast<size_t>(maxDisplayLen)) { if (displayText.length() > static_cast<size_t>(maxDisplayLen)) {
displayText = "..." + displayText.substr(displayText.length() - maxDisplayLen + 3); displayText = "..." + displayText.substr(displayText.length() - maxDisplayLen + 3);
} }
renderer.drawText(UI_FONT_ID, 20, inputY, displayText.c_str()); renderer.drawText(UI_FONT_ID, 20, inputY, displayText.c_str());
renderer.drawText(UI_FONT_ID, pageWidth - 15, inputY, "]"); renderer.drawText(UI_FONT_ID, pageWidth - 15, inputY, "]");
// Draw keyboard - use compact spacing to fit 5 rows on screen // Draw keyboard - use compact spacing to fit 5 rows on screen
int keyboardStartY = inputY + 25; int keyboardStartY = inputY + 25;
const int keyWidth = 18; const int keyWidth = 18;
const int keyHeight = 18; const int keyHeight = 18;
const int keySpacing = 3; const int keySpacing = 3;
const char* const* layout = shiftActive ? keyboardShift : keyboard; const char* const* layout = shiftActive ? keyboardShift : keyboard;
// Calculate left margin to center the longest row (13 keys) // Calculate left margin to center the longest row (13 keys)
int maxRowWidth = KEYS_PER_ROW * (keyWidth + keySpacing); int maxRowWidth = KEYS_PER_ROW * (keyWidth + keySpacing);
int leftMargin = (pageWidth - maxRowWidth) / 2; int leftMargin = (pageWidth - maxRowWidth) / 2;
for (int row = 0; row < NUM_ROWS; row++) { for (int row = 0; row < NUM_ROWS; row++) {
int rowY = keyboardStartY + row * (keyHeight + keySpacing); int rowY = keyboardStartY + row * (keyHeight + keySpacing);
// Left-align all rows for consistent navigation // Left-align all rows for consistent navigation
int startX = leftMargin; int startX = leftMargin;
// Handle bottom row (row 4) specially with proper multi-column keys // Handle bottom row (row 4) specially with proper multi-column keys
if (row == 4) { if (row == 4) {
// Bottom row layout: CAPS (2 cols) | SPACE (5 cols) | <- (2 cols) | OK (2 cols) // Bottom row layout: CAPS (2 cols) | SPACE (5 cols) | <- (2 cols) | OK (2 cols)
// Total: 11 visual columns, but we use logical positions for selection // Total: 11 visual columns, but we use logical positions for selection
int currentX = startX; int currentX = startX;
// CAPS key (logical col 0, spans 2 key widths) // CAPS key (logical col 0, spans 2 key widths)
int capsWidth = 2 * keyWidth + keySpacing; int capsWidth = 2 * keyWidth + keySpacing;
bool capsSelected = (selectedRow == 4 && selectedCol == SHIFT_COL); bool capsSelected = (selectedRow == 4 && selectedCol == SHIFT_COL);
@ -247,7 +244,7 @@ void OnScreenKeyboard::render(int startY) const {
} }
renderer.drawText(UI_FONT_ID, currentX + 2, rowY, shiftActive ? "CAPS" : "caps"); renderer.drawText(UI_FONT_ID, currentX + 2, rowY, shiftActive ? "CAPS" : "caps");
currentX += capsWidth + keySpacing; currentX += capsWidth + keySpacing;
// Space bar (logical cols 2-6, spans 5 key widths) // Space bar (logical cols 2-6, spans 5 key widths)
int spaceWidth = 5 * keyWidth + 4 * keySpacing; int spaceWidth = 5 * keyWidth + 4 * keySpacing;
bool spaceSelected = (selectedRow == 4 && selectedCol >= SPACE_COL && selectedCol < BACKSPACE_COL); bool spaceSelected = (selectedRow == 4 && selectedCol >= SPACE_COL && selectedCol < BACKSPACE_COL);
@ -259,7 +256,7 @@ void OnScreenKeyboard::render(int startY) const {
int spaceTextX = currentX + (spaceWidth / 2) - 12; int spaceTextX = currentX + (spaceWidth / 2) - 12;
renderer.drawText(UI_FONT_ID, spaceTextX, rowY, "_____"); renderer.drawText(UI_FONT_ID, spaceTextX, rowY, "_____");
currentX += spaceWidth + keySpacing; currentX += spaceWidth + keySpacing;
// Backspace key (logical col 7, spans 2 key widths) // Backspace key (logical col 7, spans 2 key widths)
int bsWidth = 2 * keyWidth + keySpacing; int bsWidth = 2 * keyWidth + keySpacing;
bool bsSelected = (selectedRow == 4 && selectedCol == BACKSPACE_COL); bool bsSelected = (selectedRow == 4 && selectedCol == BACKSPACE_COL);
@ -269,7 +266,7 @@ void OnScreenKeyboard::render(int startY) const {
} }
renderer.drawText(UI_FONT_ID, currentX + 6, rowY, "<-"); renderer.drawText(UI_FONT_ID, currentX + 6, rowY, "<-");
currentX += bsWidth + keySpacing; currentX += bsWidth + keySpacing;
// OK button (logical col 9, spans 2 key widths) // OK button (logical col 9, spans 2 key widths)
int okWidth = 2 * keyWidth + keySpacing; int okWidth = 2 * keyWidth + keySpacing;
bool okSelected = (selectedRow == 4 && selectedCol >= DONE_COL); bool okSelected = (selectedRow == 4 && selectedCol >= DONE_COL);
@ -278,29 +275,29 @@ void OnScreenKeyboard::render(int startY) const {
renderer.drawText(UI_FONT_ID, currentX + okWidth - 4, rowY, "]"); renderer.drawText(UI_FONT_ID, currentX + okWidth - 4, rowY, "]");
} }
renderer.drawText(UI_FONT_ID, currentX + 8, rowY, "OK"); renderer.drawText(UI_FONT_ID, currentX + 8, rowY, "OK");
} else { } else {
// Regular rows: render each key individually // Regular rows: render each key individually
for (int col = 0; col < getRowLength(row); col++) { for (int col = 0; col < getRowLength(row); col++) {
int keyX = startX + col * (keyWidth + keySpacing); int keyX = startX + col * (keyWidth + keySpacing);
// Get the character to display // Get the character to display
char c = layout[row][col]; char c = layout[row][col];
std::string keyLabel(1, c); std::string keyLabel(1, c);
// Draw selection highlight // Draw selection highlight
bool isSelected = (row == selectedRow && col == selectedCol); bool isSelected = (row == selectedRow && col == selectedCol);
if (isSelected) { if (isSelected) {
renderer.drawText(UI_FONT_ID, keyX - 2, rowY, "["); renderer.drawText(UI_FONT_ID, keyX - 2, rowY, "[");
renderer.drawText(UI_FONT_ID, keyX + keyWidth - 4, rowY, "]"); renderer.drawText(UI_FONT_ID, keyX + keyWidth - 4, rowY, "]");
} }
renderer.drawText(UI_FONT_ID, keyX + 2, rowY, keyLabel.c_str()); renderer.drawText(UI_FONT_ID, keyX + 2, rowY, keyLabel.c_str());
} }
} }
} }
// Draw help text at absolute bottom of screen (consistent with other screens) // Draw help text at absolute bottom of screen (consistent with other screens)
const auto pageHeight = GfxRenderer::getScreenHeight(); const auto pageHeight = GfxRenderer::getScreenHeight();
renderer.drawText(SMALL_FONT_ID, 10, pageHeight - 30, "Navigate: D-pad | Select: OK | Cancel: BACK"); renderer.drawText(SMALL_FONT_ID, 10, pageHeight - 30, "Navigate: D-pad | Select: OK | Cancel: BACK");

View File

@ -8,7 +8,7 @@
/** /**
* Reusable on-screen keyboard component for text input. * Reusable on-screen keyboard component for text input.
* Can be embedded in any screen that needs text entry. * Can be embedded in any screen that needs text entry.
* *
* Usage: * Usage:
* 1. Create an OnScreenKeyboard instance * 1. Create an OnScreenKeyboard instance
* 2. Call render() to draw the keyboard * 2. Call render() to draw the keyboard
@ -31,11 +31,8 @@ class OnScreenKeyboard {
* @param maxLength Maximum length of input text (0 for unlimited) * @param maxLength Maximum length of input text (0 for unlimited)
* @param isPassword If true, display asterisks instead of actual characters * @param isPassword If true, display asterisks instead of actual characters
*/ */
OnScreenKeyboard(GfxRenderer& renderer, InputManager& inputManager, OnScreenKeyboard(GfxRenderer& renderer, InputManager& inputManager, const std::string& title = "Enter Text",
const std::string& title = "Enter Text", const std::string& initialText = "", size_t maxLength = 0, bool isPassword = false);
const std::string& initialText = "",
size_t maxLength = 0,
bool isPassword = false);
/** /**
* Handle button input. Call this in your screen's handleInput(). * Handle button input. Call this in your screen's handleInput().
@ -87,19 +84,19 @@ class OnScreenKeyboard {
private: private:
GfxRenderer& renderer; GfxRenderer& renderer;
InputManager& inputManager; InputManager& inputManager;
std::string title; std::string title;
std::string text; std::string text;
size_t maxLength; size_t maxLength;
bool isPassword; bool isPassword;
// Keyboard state // Keyboard state
int selectedRow = 0; int selectedRow = 0;
int selectedCol = 0; int selectedCol = 0;
bool shiftActive = false; bool shiftActive = false;
bool complete = false; bool complete = false;
bool cancelled = false; bool cancelled = false;
// Callbacks // Callbacks
OnCompleteCallback onComplete; OnCompleteCallback onComplete;
OnCancelCallback onCancel; OnCancelCallback onCancel;
@ -109,7 +106,7 @@ class OnScreenKeyboard {
static constexpr int KEYS_PER_ROW = 13; // Max keys per row (rows 0 and 1 have 13 keys) static constexpr int KEYS_PER_ROW = 13; // Max keys per row (rows 0 and 1 have 13 keys)
static const char* const keyboard[NUM_ROWS]; static const char* const keyboard[NUM_ROWS];
static const char* const keyboardShift[NUM_ROWS]; static const char* const keyboardShift[NUM_ROWS];
// Special key positions (bottom row) // Special key positions (bottom row)
static constexpr int SHIFT_ROW = 4; static constexpr int SHIFT_ROW = 4;
static constexpr int SHIFT_COL = 0; static constexpr int SHIFT_COL = 0;

View File

@ -100,7 +100,7 @@ void SettingsScreen::toggleCurrentSetting() {
} }
const auto& setting = settingsList[selectedSettingIndex]; const auto& setting = settingsList[selectedSettingIndex];
// Only toggle if it's a toggle type and has a value pointer // Only toggle if it's a toggle type and has a value pointer
if (setting.type != SettingType::TOGGLE || setting.valuePtr == nullptr) { if (setting.type != SettingType::TOGGLE || setting.valuePtr == nullptr) {
return; return;

View File

@ -17,9 +17,9 @@ enum class SettingType { TOGGLE, ACTION };
// Structure to hold setting information // Structure to hold setting information
struct SettingInfo { struct SettingInfo {
const char* name; // Display name of the setting const char* name; // Display name of the setting
SettingType type; // Type of setting SettingType type; // Type of setting
uint8_t CrossPointSettings::* valuePtr; // Pointer to member in CrossPointSettings (for TOGGLE) uint8_t CrossPointSettings::* valuePtr; // Pointer to member in CrossPointSettings (for TOGGLE)
}; };
class SettingsScreen final : public Screen { class SettingsScreen final : public Screen {
@ -41,9 +41,8 @@ class SettingsScreen final : public Screen {
void activateCurrentSetting(); void activateCurrentSetting();
public: public:
explicit SettingsScreen(GfxRenderer& renderer, InputManager& inputManager, explicit SettingsScreen(GfxRenderer& renderer, InputManager& inputManager, const std::function<void()>& onGoHome,
const std::function<void()>& onGoHome, const std::function<void()>& onGoWifi)
const std::function<void()>& onGoWifi)
: Screen(renderer, inputManager), onGoHome(onGoHome), onGoWifi(onGoWifi) {} : Screen(renderer, inputManager), onGoHome(onGoHome), onGoWifi(onGoWifi) {}
void onEnter() override; void onEnter() override;
void onExit() override; void onExit() override;

View File

@ -48,10 +48,10 @@ void WifiScreen::onEnter() {
void WifiScreen::onExit() { void WifiScreen::onExit() {
// Stop any ongoing WiFi scan // Stop any ongoing WiFi scan
WiFi.scanDelete(); WiFi.scanDelete();
// Stop the web server to free memory // Stop the web server to free memory
crossPointWebServer.stop(); crossPointWebServer.stop();
// Disconnect WiFi to free memory // Disconnect WiFi to free memory
WiFi.disconnect(true); WiFi.disconnect(true);
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
@ -136,7 +136,8 @@ void WifiScreen::selectNetwork(int index) {
// Use saved password - connect directly // Use saved password - connect directly
enteredPassword = savedCred->password; enteredPassword = savedCred->password;
usedSavedPassword = true; usedSavedPassword = true;
Serial.printf("[%lu] [WiFi] Using saved password for %s, length: %zu\n", millis(), selectedSSID.c_str(), enteredPassword.size()); Serial.printf("[%lu] [WiFi] Using saved password for %s, length: %zu\n", millis(), selectedSSID.c_str(),
enteredPassword.size());
attemptConnection(); attemptConnection();
return; return;
} }
@ -144,13 +145,11 @@ void WifiScreen::selectNetwork(int index) {
if (selectedRequiresPassword) { if (selectedRequiresPassword) {
// Show password entry // Show password entry
state = WifiScreenState::PASSWORD_ENTRY; state = WifiScreenState::PASSWORD_ENTRY;
keyboard.reset(new OnScreenKeyboard( keyboard.reset(new OnScreenKeyboard(renderer, inputManager, "Enter WiFi Password",
renderer, inputManager, "", // No initial text
"Enter WiFi Password", 64, // Max password length
"", // No initial text false // Show password by default (hard keyboard to use)
64, // Max password length ));
false // Show password by default (hard keyboard to use)
));
updateRequired = true; updateRequired = true;
} else { } else {
// Connect directly for open networks // Connect directly for open networks
@ -166,12 +165,12 @@ void WifiScreen::attemptConnection() {
updateRequired = true; updateRequired = true;
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
// Get password from keyboard if we just entered it // Get password from keyboard if we just entered it
if (keyboard && !usedSavedPassword) { if (keyboard && !usedSavedPassword) {
enteredPassword = keyboard->getText(); enteredPassword = keyboard->getText();
} }
if (selectedRequiresPassword && !enteredPassword.empty()) { if (selectedRequiresPassword && !enteredPassword.empty()) {
WiFi.begin(selectedSSID.c_str(), enteredPassword.c_str()); WiFi.begin(selectedSSID.c_str(), enteredPassword.c_str());
} else { } else {
@ -185,17 +184,17 @@ void WifiScreen::checkConnectionStatus() {
} }
wl_status_t status = WiFi.status(); wl_status_t status = WiFi.status();
if (status == WL_CONNECTED) { if (status == WL_CONNECTED) {
// Successfully connected // Successfully connected
IPAddress ip = WiFi.localIP(); IPAddress ip = WiFi.localIP();
char ipStr[16]; char ipStr[16];
snprintf(ipStr, sizeof(ipStr), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); snprintf(ipStr, sizeof(ipStr), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
connectedIP = ipStr; connectedIP = ipStr;
// Start the web server // Start the web server
crossPointWebServer.begin(); crossPointWebServer.begin();
// If we used a saved password, go directly to connected screen // If we used a saved password, go directly to connected screen
// If we entered a new password, ask if user wants to save it // If we entered a new password, ask if user wants to save it
if (usedSavedPassword || enteredPassword.empty()) { if (usedSavedPassword || enteredPassword.empty()) {
@ -244,33 +243,31 @@ void WifiScreen::handleInput() {
// Handle password entry state // Handle password entry state
if (state == WifiScreenState::PASSWORD_ENTRY && keyboard) { if (state == WifiScreenState::PASSWORD_ENTRY && keyboard) {
keyboard->handleInput(); keyboard->handleInput();
if (keyboard->isComplete()) { if (keyboard->isComplete()) {
attemptConnection(); attemptConnection();
return; return;
} }
if (keyboard->isCancelled()) { if (keyboard->isCancelled()) {
state = WifiScreenState::NETWORK_LIST; state = WifiScreenState::NETWORK_LIST;
keyboard.reset(); keyboard.reset();
updateRequired = true; updateRequired = true;
return; return;
} }
updateRequired = true; updateRequired = true;
return; return;
} }
// Handle save prompt state // Handle save prompt state
if (state == WifiScreenState::SAVE_PROMPT) { if (state == WifiScreenState::SAVE_PROMPT) {
if (inputManager.wasPressed(InputManager::BTN_LEFT) || if (inputManager.wasPressed(InputManager::BTN_LEFT) || inputManager.wasPressed(InputManager::BTN_UP)) {
inputManager.wasPressed(InputManager::BTN_UP)) {
if (savePromptSelection > 0) { if (savePromptSelection > 0) {
savePromptSelection--; savePromptSelection--;
updateRequired = true; updateRequired = true;
} }
} else if (inputManager.wasPressed(InputManager::BTN_RIGHT) || } else if (inputManager.wasPressed(InputManager::BTN_RIGHT) || inputManager.wasPressed(InputManager::BTN_DOWN)) {
inputManager.wasPressed(InputManager::BTN_DOWN)) {
if (savePromptSelection < 1) { if (savePromptSelection < 1) {
savePromptSelection++; savePromptSelection++;
updateRequired = true; updateRequired = true;
@ -293,14 +290,12 @@ void WifiScreen::handleInput() {
// Handle forget prompt state (connection failed with saved credentials) // Handle forget prompt state (connection failed with saved credentials)
if (state == WifiScreenState::FORGET_PROMPT) { if (state == WifiScreenState::FORGET_PROMPT) {
if (inputManager.wasPressed(InputManager::BTN_LEFT) || if (inputManager.wasPressed(InputManager::BTN_LEFT) || inputManager.wasPressed(InputManager::BTN_UP)) {
inputManager.wasPressed(InputManager::BTN_UP)) {
if (forgetPromptSelection > 0) { if (forgetPromptSelection > 0) {
forgetPromptSelection--; forgetPromptSelection--;
updateRequired = true; updateRequired = true;
} }
} else if (inputManager.wasPressed(InputManager::BTN_RIGHT) || } else if (inputManager.wasPressed(InputManager::BTN_RIGHT) || inputManager.wasPressed(InputManager::BTN_DOWN)) {
inputManager.wasPressed(InputManager::BTN_DOWN)) {
if (forgetPromptSelection < 1) { if (forgetPromptSelection < 1) {
forgetPromptSelection++; forgetPromptSelection++;
updateRequired = true; updateRequired = true;
@ -330,8 +325,7 @@ void WifiScreen::handleInput() {
// Handle connected state // Handle connected state
if (state == WifiScreenState::CONNECTED) { if (state == WifiScreenState::CONNECTED) {
if (inputManager.wasPressed(InputManager::BTN_BACK) || if (inputManager.wasPressed(InputManager::BTN_BACK) || inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
// Exit screen on success // Exit screen on success
onGoBack(); onGoBack();
return; return;
@ -340,8 +334,7 @@ void WifiScreen::handleInput() {
// Handle connection failed state // Handle connection failed state
if (state == WifiScreenState::CONNECTION_FAILED) { if (state == WifiScreenState::CONNECTION_FAILED) {
if (inputManager.wasPressed(InputManager::BTN_BACK) || if (inputManager.wasPressed(InputManager::BTN_BACK) || inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
// If we used saved credentials, offer to forget the network // If we used saved credentials, offer to forget the network
if (usedSavedPassword) { if (usedSavedPassword) {
state = WifiScreenState::FORGET_PROMPT; state = WifiScreenState::FORGET_PROMPT;
@ -551,7 +544,7 @@ void WifiScreen::renderConnecting() const {
renderer.drawCenteredText(UI_FONT_ID, top, "Scanning...", true, REGULAR); renderer.drawCenteredText(UI_FONT_ID, top, "Scanning...", true, REGULAR);
} else { } else {
renderer.drawCenteredText(READER_FONT_ID, top - 30, "Connecting...", true, BOLD); renderer.drawCenteredText(READER_FONT_ID, top - 30, "Connecting...", true, BOLD);
std::string ssidInfo = "to " + selectedSSID; std::string ssidInfo = "to " + selectedSSID;
if (ssidInfo.length() > 25) { if (ssidInfo.length() > 25) {
ssidInfo = ssidInfo.substr(0, 22) + "..."; ssidInfo = ssidInfo.substr(0, 22) + "...";

View File

@ -54,10 +54,10 @@ class WifiScreen final : public Screen {
// Password to potentially save (from keyboard or saved credentials) // Password to potentially save (from keyboard or saved credentials)
std::string enteredPassword; std::string enteredPassword;
// Whether network was connected using a saved password (skip save prompt) // Whether network was connected using a saved password (skip save prompt)
bool usedSavedPassword = false; bool usedSavedPassword = false;
// Save/forget prompt selection (0 = Yes, 1 = No) // Save/forget prompt selection (0 = Yes, 1 = No)
int savePromptSelection = 0; int savePromptSelection = 0;
int forgetPromptSelection = 0; int forgetPromptSelection = 0;