@@ -20,6 +20,46 @@ struct event {
20
20
const char * text ;
21
21
};
22
22
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
+
23
63
// DHCP configuration
24
64
struct dhcp {
25
65
bool enabled ;
@@ -184,6 +224,33 @@ static void handle_events_get(struct mg_connection *c) {
184
224
mg_http_reply (c , 200 , s_json_header , "[%M]" , print_events );
185
225
}
186
226
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
+
187
254
static void handle_dhcp_set (struct mg_connection * c , struct mg_str body ) {
188
255
struct dhcp dhcp ;
189
256
memset (& dhcp , 0 , sizeof (dhcp ));
@@ -228,6 +295,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
228
295
handle_stats_get (c );
229
296
} else if (mg_http_match_uri (hm , "/api/events/get" )) {
230
297
handle_events_get (c );
298
+ } else if (mg_http_match_uri (hm , "/api/devices/get" )) {
299
+ handle_devices_get (c );
231
300
} else if (mg_http_match_uri (hm , "/api/dhcp/get" )) {
232
301
handle_dhcp_get (c );
233
302
} else if (mg_http_match_uri (hm , "/api/dhcp/set" )) {
0 commit comments