Skip to content

Commit 796f95e

Browse files
committed
shared/netutils/dhcpserver: Add optional callback when client connects.
Signed-off-by: Andrew Leech <[email protected]>
1 parent f9786fb commit 796f95e

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

shared/netutils/dhcpserver.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p,
272272
printf("DHCPS: client connected: MAC=%02x:%02x:%02x:%02x:%02x:%02x IP=%u.%u.%u.%u\n",
273273
dhcp_msg.chaddr[0], dhcp_msg.chaddr[1], dhcp_msg.chaddr[2], dhcp_msg.chaddr[3], dhcp_msg.chaddr[4], dhcp_msg.chaddr[5],
274274
dhcp_msg.yiaddr[0], dhcp_msg.yiaddr[1], dhcp_msg.yiaddr[2], dhcp_msg.yiaddr[3]);
275+
if (d->on_client_connect != NULL) {
276+
d->on_client_connect(dhcp_msg.yiaddr, dhcp_msg.chaddr);
277+
}
275278
break;
276279
}
277280

@@ -296,6 +299,7 @@ void dhcp_server_init(dhcp_server_t *d, ip_addr_t *ip, ip_addr_t *nm) {
296299
ip_addr_copy(d->ip, *ip);
297300
ip_addr_copy(d->nm, *nm);
298301
memset(d->lease, 0, sizeof(d->lease));
302+
d->on_client_connect = NULL;
299303
if (dhcp_socket_new_dgram(&d->udp, d, dhcp_server_process) != 0) {
300304
return;
301305
}
@@ -306,4 +310,8 @@ void dhcp_server_deinit(dhcp_server_t *d) {
306310
dhcp_socket_free(&d->udp);
307311
}
308312

313+
void dhcp_server_register_connect_cb(dhcp_server_t *d, dhcp_client_callback_t on_client_connect) {
314+
d->on_client_connect = on_client_connect;
315+
}
316+
309317
#endif // MICROPY_PY_LWIP

shared/netutils/dhcpserver.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ typedef struct _dhcp_server_lease_t {
3636
uint16_t expiry;
3737
} dhcp_server_lease_t;
3838

39+
typedef void (*dhcp_client_callback_t)(uint8_t[4], uint8_t[16]);
40+
3941
typedef struct _dhcp_server_t {
4042
ip_addr_t ip;
4143
ip_addr_t nm;
4244
dhcp_server_lease_t lease[DHCPS_MAX_IP];
4345
struct udp_pcb *udp;
46+
dhcp_client_callback_t on_client_connect;
4447
} dhcp_server_t;
4548

4649
void dhcp_server_init(dhcp_server_t *d, ip_addr_t *ip, ip_addr_t *nm);
4750
void dhcp_server_deinit(dhcp_server_t *d);
51+
void dhcp_server_register_connect_cb(dhcp_server_t *d, dhcp_client_callback_t on_client_connect);
4852

4953
#endif // MICROPY_INCLUDED_LIB_NETUTILS_DHCPSERVER_H

0 commit comments

Comments
 (0)