Skip to content

Commit

Permalink
Add DHCP settings
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed May 23, 2023
1 parent a8309ef commit 923f2c7
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 14 deletions.
56 changes: 46 additions & 10 deletions examples/wifi-router-dashboard/web.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ struct event {
const char *text;
};

// DHCP configuration
struct dhcp {
bool enabled;
uint8_t address_begin;
uint8_t address_end;
unsigned long lease_time_sec;
};

static struct dhcp s_dhcp = {true, 10, 255, 86400};

// Mocked events
static struct event s_events[] = {
{.type = 0, .prio = 0, .text = "here goes event 1"},
Expand All @@ -31,12 +41,6 @@ static struct event s_events[] = {
{.type = 1, .prio = 1, .text = "oops. it happened again"},
};

static int event_next(int no, struct event *e) {
if (no < 0 || no >= (int) (sizeof(s_events) / sizeof(s_events[0]))) return 0;
*e = s_events[no];
return no + 1;
}

static const char *s_json_header =
"Content-Type: application/json\r\n"
"Cache-Control: no-cache\r\n";
Expand All @@ -62,6 +66,12 @@ static const char *s_ssl_key =
"6YbyU/ZGtdGfbaGYYJwatKNMX00OIwtb8A==\n"
"-----END EC PRIVATE KEY-----\n";

static int event_next(int no, struct event *e) {
if (no < 0 || no >= (int) (sizeof(s_events) / sizeof(s_events[0]))) return 0;
*e = s_events[no];
return no + 1;
}

// SNTP connection event handler. When we get a response from an SNTP server,
// adjust s_boot_timestamp. We'll get a valid time from that point on
static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
Expand Down Expand Up @@ -160,10 +170,10 @@ static size_t print_events(void (*out)(char, void *), void *ptr, va_list *ap) {
int no = 0;
while ((no = event_next(no, &e)) != 0) {
len += mg_xprintf(out, ptr, "%s{%m:%lu,%m:%d,%m:%d,%m:%m}", //
len == 0 ? "" : ",", //
MG_ESC("time"), e.timestamp, //
MG_ESC("type"), e.type, //
MG_ESC("prio"), e.prio, //
len == 0 ? "" : ",", //
MG_ESC("time"), e.timestamp, //
MG_ESC("type"), e.type, //
MG_ESC("prio"), e.prio, //
MG_ESC("text"), MG_ESC(e.text));
}
(void) ap;
Expand All @@ -174,6 +184,28 @@ static void handle_events_get(struct mg_connection *c) {
mg_http_reply(c, 200, s_json_header, "[%M]", print_events);
}

static void handle_dhcp_set(struct mg_connection *c, struct mg_str body) {
struct dhcp dhcp = {};

This comment has been minimized.

Copy link
@gvanem

gvanem May 24, 2023

Contributor

MSVC does not like this empty initialiser {}; But a { 0 }; is fine.

This comment has been minimized.

Copy link
@scaprile

scaprile May 24, 2023

Collaborator
mg_json_get_bool(body, "$.enabled", &dhcp.enabled);
dhcp.address_begin = mg_json_get_long(body, "$.address_begin", 0);
dhcp.address_end = mg_json_get_long(body, "$.address_end", 0);
dhcp.lease_time_sec = mg_json_get_long(body, "$.lease_time_sec", 0);
s_dhcp = dhcp; // Save to the device flash, too
bool ok = true;
mg_http_reply(c, 200, s_json_header,
"{%m:%s,%m:%m}", //
MG_ESC("status"), ok ? "true" : "false", //
MG_ESC("message"), MG_ESC(ok ? "Success" : "Failed"));
}

static void handle_dhcp_get(struct mg_connection *c) {
mg_http_reply(c, 200, s_json_header, "{%m:%s,%m:%hhu,%m:%hhu,%m:%lu}", //
MG_ESC("enabled"), s_dhcp.enabled ? "true" : "false", //
MG_ESC("address_begin"), s_dhcp.address_begin, //
MG_ESC("address_end"), s_dhcp.address_end, //
MG_ESC("lease_time_sec"), s_dhcp.lease_time_sec);
}

// HTTP request handler function
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_ACCEPT && fn_data != NULL) {
Expand All @@ -195,6 +227,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
handle_stats_get(c);
} else if (mg_http_match_uri(hm, "/api/events/get")) {
handle_events_get(c);
} else if (mg_http_match_uri(hm, "/api/dhcp/get")) {
handle_dhcp_get(c);
} else if (mg_http_match_uri(hm, "/api/dhcp/set")) {
handle_dhcp_set(c, hm->body);
} else {
struct mg_http_serve_opts opts;
memset(&opts, 0, sizeof(opts));
Expand Down
Loading

0 comments on commit 923f2c7

Please sign in to comment.