More work on the ESP32C3 Boilerplate
This commit is contained in:
172
include/app.h
Normal file
172
include/app.h
Normal file
@@ -0,0 +1,172 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <DNSServer.h>
|
||||
#include <Preferences.h>
|
||||
#include <WebServer.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#ifndef LED_BUILTIN
|
||||
#define LED_BUILTIN 8
|
||||
#endif
|
||||
|
||||
#ifndef FACTORY_RESET_PIN
|
||||
#define FACTORY_RESET_PIN 4
|
||||
#endif
|
||||
|
||||
#ifndef FACTORY_RESET_ACTIVE_LEVEL
|
||||
#define FACTORY_RESET_ACTIVE_LEVEL LOW
|
||||
#endif
|
||||
|
||||
#ifndef PROJECT_NAME
|
||||
#define PROJECT_NAME "TSL-Embedded"
|
||||
#endif
|
||||
|
||||
extern const char *APP_VERSION;
|
||||
extern const char *PROJECT_NAME_VALUE;
|
||||
extern const char *DEFAULT_ADMIN;
|
||||
extern const char *DEFAULT_UPDATE_URL;
|
||||
extern const uint32_t FACTORY_RESET_HOLD_MS;
|
||||
extern const size_t MAX_USERS;
|
||||
extern const size_t MAX_TOKENS;
|
||||
extern const bool DEFAULT_LED_INVERTED;
|
||||
extern const byte DNS_PORT;
|
||||
extern const IPAddress SETUP_AP_IP;
|
||||
extern const IPAddress SETUP_AP_GATEWAY;
|
||||
extern const IPAddress SETUP_AP_SUBNET;
|
||||
|
||||
enum LogLevel : uint8_t {
|
||||
LOG_ERROR = 0,
|
||||
LOG_WARN = 1,
|
||||
LOG_SECURITY_AUDIT = 2,
|
||||
LOG_INFO = 3,
|
||||
LOG_DEBUG = 4
|
||||
};
|
||||
|
||||
struct User {
|
||||
String name;
|
||||
String passwordHash;
|
||||
String roles;
|
||||
bool active;
|
||||
};
|
||||
|
||||
struct Token {
|
||||
String value;
|
||||
String user;
|
||||
String roles;
|
||||
uint32_t lastSeen;
|
||||
};
|
||||
|
||||
typedef void (*RouteHandler)();
|
||||
|
||||
struct ApiDef {
|
||||
const char *path;
|
||||
const char *method;
|
||||
const char *defaultRole;
|
||||
bool publicByDefault;
|
||||
RouteHandler handler;
|
||||
RouteHandler uploadHandler;
|
||||
};
|
||||
|
||||
extern WebServer server;
|
||||
extern DNSServer dnsServer;
|
||||
extern Preferences prefs;
|
||||
extern Token tokens[];
|
||||
extern ApiDef apiDefs[];
|
||||
extern const size_t API_DEF_COUNT;
|
||||
extern ApiDef customApiDefs[];
|
||||
extern const size_t CUSTOM_API_DEF_COUNT;
|
||||
extern LogLevel currentLogLevel;
|
||||
extern size_t maxLogBytes;
|
||||
extern bool setupMode;
|
||||
extern uint8_t ledBrightness;
|
||||
extern bool ledInverted;
|
||||
extern String updateUrl;
|
||||
extern String networkHostname;
|
||||
extern bool networkDhcp;
|
||||
extern String networkIp;
|
||||
extern String networkGateway;
|
||||
extern String networkSubnet;
|
||||
extern String networkDns1;
|
||||
extern String networkDns2;
|
||||
|
||||
String jsonEscape(const String &s);
|
||||
String jsonOk(const String &payload = "");
|
||||
String jsonError(const String &message);
|
||||
void sendJson(int code, const String &body);
|
||||
String requestBody();
|
||||
String jsonStringValue(const String &json, const char *key, const String &fallback = "");
|
||||
int jsonIntValue(const String &json, const char *key, int fallback = 0);
|
||||
bool jsonBoolValue(const String &json, const char *key, bool fallback = false);
|
||||
String jsonRolesValue(const String &json, const char *key, const String &fallback = "");
|
||||
|
||||
String chipId();
|
||||
String deviceName();
|
||||
String defaultDeviceName();
|
||||
String sha256(const String &input);
|
||||
String passwordHash(const String &password);
|
||||
String prefString(const char *key, const String &fallback = "");
|
||||
void loadSettings();
|
||||
|
||||
bool hasRole(const String &roles, const String &role);
|
||||
bool validName(const String &s);
|
||||
String cleanRoles(const String &roles);
|
||||
bool isKnownRole(const String &role);
|
||||
bool isSystemRole(const String &role);
|
||||
String allRoles();
|
||||
String rolesJson();
|
||||
bool addCustomRole(const String &role);
|
||||
bool deleteCustomRole(const String &role);
|
||||
size_t parseUsers(User *users, size_t maxUsers);
|
||||
void saveUsers(User *users, size_t count);
|
||||
bool findUser(const String &name, User &user);
|
||||
String createToken(const User &user);
|
||||
Token *currentToken();
|
||||
ApiDef *findApi(const String &path, const String &method);
|
||||
String configuredRole(ApiDef *api);
|
||||
bool authorize();
|
||||
|
||||
void appendLog(LogLevel level, const String &message);
|
||||
|
||||
void applyLed();
|
||||
void factoryReset();
|
||||
void checkFactoryResetPin();
|
||||
bool connectWifi();
|
||||
|
||||
void handleSetupPage();
|
||||
bool handleHtmlFileRequest();
|
||||
void redirectToSetupPage();
|
||||
void handleCaptiveProbe();
|
||||
void handleFavicon();
|
||||
void handleAdminPage();
|
||||
|
||||
void handleWifiScan();
|
||||
void handleSetupSubmit();
|
||||
void handleLogin();
|
||||
void handleLogout();
|
||||
void handleMe();
|
||||
void handleUsersGet();
|
||||
void handleUsersPost();
|
||||
void handlePassword();
|
||||
void handleRolesGet();
|
||||
void handleRolesPost();
|
||||
void handleRolesDelete();
|
||||
void handleApisGet();
|
||||
void handleApisPost();
|
||||
void handleSettingsGet();
|
||||
void handleSettingsPost();
|
||||
void handleLogsGet();
|
||||
void handleLogsClear();
|
||||
void handleFilesList();
|
||||
void handleFileDownload();
|
||||
void handleFactoryReset();
|
||||
void handleConfigExport();
|
||||
void handleConfigImport();
|
||||
void handleOtaCheck();
|
||||
void handleOtaRun();
|
||||
void handleUpdateUploadDone();
|
||||
void handleUpdateUploadChunk();
|
||||
void handleCertGet();
|
||||
void handleCertPost();
|
||||
|
||||
void registerRoutes();
|
||||
Reference in New Issue
Block a user