Skip to content

Commit d013e0f

Browse files
authored
Merge pull request #2254 from cesanta/2249-implement-connected-devices-page-for-wifi-router-dashboard
implemented connected devices page
2 parents 7ea2093 + 6f69ad9 commit d013e0f

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

examples/wifi-router-dashboard/net.c

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

23+
// Connected Devices
24+
struct device {
25+
char *dev_name;
26+
char *mac;
27+
char *ip_addr;
28+
int speed;
29+
char *connected_to;
30+
int lease_time_left;
31+
char *last_seen;
32+
};
33+
34+
static struct device s_devices[] = {
35+
{ .dev_name = "espressif",
36+
.mac = "02:11:22:33:44:55",
37+
.ip_addr = "192.168.1.1/24",
38+
.speed = 1000,
39+
.connected_to = "Ethernet",
40+
.lease_time_left = 1000,
41+
.last_seen = "13h20m ago"
42+
},
43+
44+
{ .dev_name = "windows11",
45+
.mac = "01:22:11:44:33:55",
46+
.ip_addr = "192.168.1.2/24",
47+
.speed = 200,
48+
.connected_to = "Wifi 2.4 GHz",
49+
.lease_time_left = 4141,
50+
.last_seen = "23s ago"
51+
},
52+
53+
{ .dev_name = "iRobot-2",
54+
.mac = "01:22:11:44:33:42",
55+
.ip_addr = "192.168.1.3/24",
56+
.speed = 600,
57+
.connected_to = "Wifi 5GHz",
58+
.lease_time_left = 1141,
59+
.last_seen = "20m ago"
60+
}
61+
};
62+
2363
// DHCP configuration
2464
struct dhcp {
2565
bool enabled;
@@ -184,6 +224,33 @@ static void handle_events_get(struct mg_connection *c) {
184224
mg_http_reply(c, 200, s_json_header, "[%M]", print_events);
185225
}
186226

227+
static void handle_devices_get(struct mg_connection *c) {
228+
char test_json[1024];
229+
int nr_devs = sizeof(s_devices) / sizeof(struct device);
230+
memset(test_json, 0, sizeof(test_json));
231+
test_json[0] = '[';
232+
for (int i = 0; i < nr_devs; i++) {
233+
size_t current_length = strlen(test_json);
234+
235+
mg_snprintf(test_json + current_length, sizeof(test_json) - current_length,
236+
"{%m:\"%s\",%m:\"%s\", %m:\"%s\", %m:%d,%m:\"%s\",%m:%d,%m:\"%s\"}", //
237+
MG_ESC("dev_name"), s_devices[i].dev_name, //
238+
MG_ESC("mac"), s_devices[i].mac, //
239+
MG_ESC("ip"), s_devices[i].ip_addr, //
240+
MG_ESC("speed"), s_devices[i].speed, //
241+
MG_ESC("connected_to"), s_devices[i].connected_to, //
242+
MG_ESC("lease_time_left"), s_devices[i].lease_time_left, //
243+
MG_ESC("last_seen"), s_devices[i].last_seen);
244+
245+
if (i < nr_devs - 1) {
246+
strncat(test_json, ",", sizeof(test_json) - strlen(test_json) - 1);
247+
}
248+
}
249+
250+
strncat(test_json, "]", sizeof(test_json) - strlen(test_json) - 1);
251+
mg_http_reply(c, 200, s_json_header, "%s", test_json);
252+
}
253+
187254
static void handle_dhcp_set(struct mg_connection *c, struct mg_str body) {
188255
struct dhcp dhcp;
189256
memset(&dhcp, 0, sizeof(dhcp));
@@ -228,6 +295,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
228295
handle_stats_get(c);
229296
} else if (mg_http_match_uri(hm, "/api/events/get")) {
230297
handle_events_get(c);
298+
} else if (mg_http_match_uri(hm, "/api/devices/get")) {
299+
handle_devices_get(c);
231300
} else if (mg_http_match_uri(hm, "/api/dhcp/get")) {
232301
handle_dhcp_get(c);
233302
} else if (mg_http_match_uri(hm, "/api/dhcp/set")) {

0 commit comments

Comments
 (0)