Skip to content

Commit 923f2c7

Browse files
committed
Add DHCP settings
1 parent a8309ef commit 923f2c7

File tree

3 files changed

+164
-14
lines changed

3 files changed

+164
-14
lines changed

examples/wifi-router-dashboard/web.c

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ struct event {
2020
const char *text;
2121
};
2222

23+
// DHCP configuration
24+
struct dhcp {
25+
bool enabled;
26+
uint8_t address_begin;
27+
uint8_t address_end;
28+
unsigned long lease_time_sec;
29+
};
30+
31+
static struct dhcp s_dhcp = {true, 10, 255, 86400};
32+
2333
// Mocked events
2434
static struct event s_events[] = {
2535
{.type = 0, .prio = 0, .text = "here goes event 1"},
@@ -31,12 +41,6 @@ static struct event s_events[] = {
3141
{.type = 1, .prio = 1, .text = "oops. it happened again"},
3242
};
3343

34-
static int event_next(int no, struct event *e) {
35-
if (no < 0 || no >= (int) (sizeof(s_events) / sizeof(s_events[0]))) return 0;
36-
*e = s_events[no];
37-
return no + 1;
38-
}
39-
4044
static const char *s_json_header =
4145
"Content-Type: application/json\r\n"
4246
"Cache-Control: no-cache\r\n";
@@ -62,6 +66,12 @@ static const char *s_ssl_key =
6266
"6YbyU/ZGtdGfbaGYYJwatKNMX00OIwtb8A==\n"
6367
"-----END EC PRIVATE KEY-----\n";
6468

69+
static int event_next(int no, struct event *e) {
70+
if (no < 0 || no >= (int) (sizeof(s_events) / sizeof(s_events[0]))) return 0;
71+
*e = s_events[no];
72+
return no + 1;
73+
}
74+
6575
// SNTP connection event handler. When we get a response from an SNTP server,
6676
// adjust s_boot_timestamp. We'll get a valid time from that point on
6777
static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
@@ -160,10 +170,10 @@ static size_t print_events(void (*out)(char, void *), void *ptr, va_list *ap) {
160170
int no = 0;
161171
while ((no = event_next(no, &e)) != 0) {
162172
len += mg_xprintf(out, ptr, "%s{%m:%lu,%m:%d,%m:%d,%m:%m}", //
163-
len == 0 ? "" : ",", //
164-
MG_ESC("time"), e.timestamp, //
165-
MG_ESC("type"), e.type, //
166-
MG_ESC("prio"), e.prio, //
173+
len == 0 ? "" : ",", //
174+
MG_ESC("time"), e.timestamp, //
175+
MG_ESC("type"), e.type, //
176+
MG_ESC("prio"), e.prio, //
167177
MG_ESC("text"), MG_ESC(e.text));
168178
}
169179
(void) ap;
@@ -174,6 +184,28 @@ static void handle_events_get(struct mg_connection *c) {
174184
mg_http_reply(c, 200, s_json_header, "[%M]", print_events);
175185
}
176186

187+
static void handle_dhcp_set(struct mg_connection *c, struct mg_str body) {
188+
struct dhcp dhcp = {};
189+
mg_json_get_bool(body, "$.enabled", &dhcp.enabled);
190+
dhcp.address_begin = mg_json_get_long(body, "$.address_begin", 0);
191+
dhcp.address_end = mg_json_get_long(body, "$.address_end", 0);
192+
dhcp.lease_time_sec = mg_json_get_long(body, "$.lease_time_sec", 0);
193+
s_dhcp = dhcp; // Save to the device flash, too
194+
bool ok = true;
195+
mg_http_reply(c, 200, s_json_header,
196+
"{%m:%s,%m:%m}", //
197+
MG_ESC("status"), ok ? "true" : "false", //
198+
MG_ESC("message"), MG_ESC(ok ? "Success" : "Failed"));
199+
}
200+
201+
static void handle_dhcp_get(struct mg_connection *c) {
202+
mg_http_reply(c, 200, s_json_header, "{%m:%s,%m:%hhu,%m:%hhu,%m:%lu}", //
203+
MG_ESC("enabled"), s_dhcp.enabled ? "true" : "false", //
204+
MG_ESC("address_begin"), s_dhcp.address_begin, //
205+
MG_ESC("address_end"), s_dhcp.address_end, //
206+
MG_ESC("lease_time_sec"), s_dhcp.lease_time_sec);
207+
}
208+
177209
// HTTP request handler function
178210
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
179211
if (ev == MG_EV_ACCEPT && fn_data != NULL) {
@@ -195,6 +227,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
195227
handle_stats_get(c);
196228
} else if (mg_http_match_uri(hm, "/api/events/get")) {
197229
handle_events_get(c);
230+
} else if (mg_http_match_uri(hm, "/api/dhcp/get")) {
231+
handle_dhcp_get(c);
232+
} else if (mg_http_match_uri(hm, "/api/dhcp/set")) {
233+
handle_dhcp_set(c, hm->body);
198234
} else {
199235
struct mg_http_serve_opts opts;
200236
memset(&opts, 0, sizeof(opts));

0 commit comments

Comments
 (0)