| 2 |
raymond |
1 |
#pragma once
|
|
|
2 |
|
|
|
3 |
#include <Arduino.h>
|
|
|
4 |
#include <DNSServer.h>
|
|
|
5 |
#include <FS.h>
|
|
|
6 |
#include "SerialLog.h"
|
|
|
7 |
#include "CredentialManager.h"
|
|
|
8 |
|
|
|
9 |
#if defined(ESP8266)
|
|
|
10 |
#include <ESP8266WiFi.h>
|
|
|
11 |
#include <ESP8266mDNS.h>
|
|
|
12 |
#elif defined(ESP32)
|
|
|
13 |
#include <WiFi.h>
|
|
|
14 |
#include <ESPmDNS.h>
|
|
|
15 |
#include <esp_wifi.h>
|
|
|
16 |
#include <esp_task_wdt.h>
|
|
|
17 |
#else
|
|
|
18 |
#error Platform not supported
|
|
|
19 |
#endif
|
|
|
20 |
|
|
|
21 |
struct WiFiScanResult {
|
|
|
22 |
bool reload = false;
|
|
|
23 |
String json;
|
|
|
24 |
};
|
|
|
25 |
|
|
|
26 |
enum class WiFiStartAction {
|
|
|
27 |
Connected,
|
|
|
28 |
StartAp,
|
|
|
29 |
Failed
|
|
|
30 |
};
|
|
|
31 |
|
|
|
32 |
struct WiFiStartResult {
|
|
|
33 |
WiFiStartAction action = WiFiStartAction::Failed;
|
|
|
34 |
IPAddress ip = IPAddress(0, 0, 0, 0);
|
|
|
35 |
};
|
|
|
36 |
|
|
|
37 |
struct WiFiConnectParams {
|
|
|
38 |
WiFiCredential creds; // WiFi credentials (ssid, encrypted password, IP config, DNS)
|
|
|
39 |
bool fromApClient = false; // True if the /connect request was initiated by a client
|
|
|
40 |
bool dhcp = true; // True to use DHCP, false for static IP
|
|
|
41 |
String password; // Plaintext password (temporary, not stored in credential)
|
|
|
42 |
String host; // Hostname for mDNS
|
|
|
43 |
uint32_t timeout = 0; // Connection timeout in milliseconds
|
|
|
44 |
uint32_t wdtLongTimeout = 0; // Long WDT timeout in milliseconds
|
|
|
45 |
uint32_t wdtTimeout = 0; // Regular WDT timeout in milliseconds
|
|
|
46 |
};
|
|
|
47 |
|
|
|
48 |
struct WiFiConnectResult {
|
|
|
49 |
bool connected = false;
|
|
|
50 |
int status = 500;
|
|
|
51 |
IPAddress ip = IPAddress(0, 0, 0, 0);
|
|
|
52 |
String body;
|
|
|
53 |
};
|
|
|
54 |
|
|
|
55 |
class WiFiService {
|
|
|
56 |
public:
|
|
|
57 |
static void setTaskWdt(uint32_t timeout);
|
|
|
58 |
static WiFiScanResult scanNetworks();
|
|
|
59 |
static WiFiConnectResult connectWithParams(const WiFiConnectParams& params);
|
|
|
60 |
static WiFiStartResult startWiFi(CredentialManager* credentialManager, fs::FS* filesystem, const char* configFile, uint32_t timeout);
|
|
|
61 |
static bool startAccessPoint(WiFiConnectParams& params, IPAddress& outIp);
|
|
|
62 |
static bool startMDNSResponder(DNSServer*& dnsServer, const String& host, uint16_t port, const IPAddress& serverIp);
|
|
|
63 |
static bool startMDNSOnly(const String& host, uint16_t port);
|
|
|
64 |
};
|