-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access-point.h
98 lines (84 loc) · 3.49 KB
/
access-point.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/// @file
/// @brief Captive portal implementation for device setup.
/* noisemeter-device - Firmware for CivicTechTO's Noisemeter Device
* Copyright (C) 2024 Clyne Sullivan, Nick Barnard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef ACCESS_POINT_H
#define ACCESS_POINT_H
#include <DNSServer.h>
#include <WebServer.h>
#include <optional>
/**
* @brief Manages the WiFi access point, captive portal, and user setup form.
*
* The access point is used to provide the user with a way to configure and set
* up their device. After the user connects to the network using the fixed SSID
* and passkey, they will be redirected to a setup form via a captive portal.
* The form collects the user's WiFi credentials and any other needed
* information (e.g. email address for first-time setup), then allows the
* firmware to store the submitted response and connect to the internet.
*
* At the moment, once the access point is started it cannot be exited. The
* firmware needs to do a software reset after handling a form submission to
* begin normal operation.
*/
class AccessPoint : public RequestHandler
{
/** Hard-coded SSID for the access point. */
static constexpr auto SSID = "tRacket_Setup";
/** Hard-coded passkey for the access point. */
static constexpr auto Passkey = "noise123";
public:
/**
* Submission handler receives WebServer for input data and returns an
* error message on failure.
*/
using SubmissionHandler = std::optional<const char *> (*)(String, String, String);
/**
* Starts the WiFi access point using the fixed credentials.
* @param func Callback to handle user setup form submission.
*/
AccessPoint(SubmissionHandler func);
/**
* Enters an infinite loop to handle the access point and web server.
*/
void run();
private:
unsigned long timeout;
/** DNS server object for captive portal redirection. */
DNSServer dns;
/** Web server object for running the setup form. */
WebServer server;
/** Callback for setup form completion. */
SubmissionHandler onCredentialsReceived;
// There variables are used for handling setup form completion.
String ssid, psk, email, finishHtml;
bool finishGood;
bool complete;
bool restarting;
/** Hard-coded IP address for the ESP32 when hosting the access point. */
static const IPAddress IP;
/** Hard-coded netmask for access point configuration. */
static const IPAddress Netmask;
/** Provides HTML for an error page with the given message. */
static String htmlFromMsg(const char *msg, const char *extra = nullptr);
/** Determines which HTTP requests should be handled. */
bool canHandle(HTTPMethod, String) override;
/** Handles requests by redirecting to the setup page. */
bool handle(WebServer&, HTTPMethod, String) override;
static void taskOnCredentialsReceived(void *param);
};
#endif // ACCESS_POINT_H