From 3d1c05aefb1645532c719e930298e2f51ba62c52 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 2 Feb 2022 17:17:39 +0100 Subject: [PATCH] lwip/dhcps: Add dhcps callback argument for associated netif --- components/esp_netif/include/esp_netif_types.h | 1 + components/esp_netif/lwip/esp_netif_lwip.c | 12 +++++++----- components/lwip/apps/dhcpserver/dhcpserver.c | 6 ++++-- components/lwip/include/apps/dhcpserver/dhcpserver.h | 5 +++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 640606a6e9..e00d4eb063 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -135,6 +135,7 @@ typedef struct { /** Event structure for IP_EVENT_AP_STAIPASSIGNED event */ typedef struct { + esp_netif_t *esp_netif; /*!< Pointer to the associated netif handle */ esp_ip4_addr_t ip; /*!< IP address which was assigned to the station */ uint8_t mac[6]; /*!< MAC address of the connected client */ } ip_event_ap_staipassigned_t; diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 464bce971d..22a3d60e15 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -793,12 +793,14 @@ esp_err_t esp_netif_recv_hook_detach(esp_netif_t *esp_netif) #endif // CONFIG_ESP_NETIF_L2_TAP #if ESP_DHCPS -static void esp_netif_dhcps_cb(uint8_t ip[4], uint8_t mac[6]) +static void esp_netif_dhcps_cb(void* arg, uint8_t ip[4], uint8_t mac[6]) { - ip_event_ap_staipassigned_t evt = { 0 }; + esp_netif_t *esp_netif = arg; + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + ip_event_ap_staipassigned_t evt = { .esp_netif = esp_netif }; memcpy((char *)&evt.ip.addr, (char *)ip, sizeof(evt.ip.addr)); memcpy((char *)&evt.mac, mac, sizeof(evt.mac)); - ESP_LOGI(TAG, "DHCP server assigned IP to a station, IP is: " IPSTR, IP2STR(&evt.ip)); + ESP_LOGI(TAG, "DHCP server assigned IP to a client, IP is: " IPSTR, IP2STR(&evt.ip)); ESP_LOGD(TAG, "Client's MAC: %x:%x:%x:%x:%x:%x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); int ret = esp_event_post(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &evt, sizeof(evt), 0); @@ -866,7 +868,7 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) ip4_addr_t lwip_netmask; memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr)); memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr)); - dhcps_set_new_lease_cb(esp_netif->dhcps, esp_netif_dhcps_cb); + dhcps_set_new_lease_cb(esp_netif->dhcps, esp_netif_dhcps_cb, esp_netif); dhcps_set_option_info(esp_netif->dhcps, SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask)); dhcps_start(esp_netif->dhcps, p_netif, lwip_ip); esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; @@ -1313,7 +1315,7 @@ static esp_err_t esp_netif_dhcps_start_api(esp_netif_api_msg_t *msg) ip4_addr_t lwip_netmask; memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr)); memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr)); - dhcps_set_new_lease_cb(esp_netif->dhcps, esp_netif_dhcps_cb); + dhcps_set_new_lease_cb(esp_netif->dhcps, esp_netif_dhcps_cb, esp_netif); dhcps_set_option_info(esp_netif->dhcps, SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask)); dhcps_start(esp_netif->dhcps, p_netif, lwip_ip); esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index 2107db15b3..9ed3a011d9 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -101,6 +101,7 @@ struct dhcps_t { dhcps_offer_t dhcps_offer; dhcps_offer_t dhcps_dns; dhcps_cb_t dhcps_cb; + void* dhcps_cb_arg; struct udp_pcb *dhcps_pcb; dhcps_handle_state state; }; @@ -741,7 +742,7 @@ static void send_ack(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len) #endif if (SendAck_err_t == ERR_OK) { - dhcps->dhcps_cb(m->yiaddr, m->chaddr); + dhcps->dhcps_cb(dhcps->dhcps_cb_arg, m->yiaddr, m->chaddr); } if (p->ref != 0) { @@ -1185,12 +1186,13 @@ static void dhcps_poll_set(dhcps_t *dhcps, u32_t ip) * Parameters : cb -- callback for dhcp server * Returns : ERR_OK on success *******************************************************************************/ -err_t dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb) +err_t dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb, void* cb_arg) { if (dhcps == NULL) { return ERR_ARG; } dhcps->dhcps_cb = cb; + dhcps->dhcps_cb_arg = cb_arg; return ERR_OK; } diff --git a/components/lwip/include/apps/dhcpserver/dhcpserver.h b/components/lwip/include/apps/dhcpserver/dhcpserver.h index 17d9a3c496..ca97cea276 100644 --- a/components/lwip/include/apps/dhcpserver/dhcpserver.h +++ b/components/lwip/include/apps/dhcpserver/dhcpserver.h @@ -82,7 +82,7 @@ typedef struct { dhcps_lease_t dhcps_poll; } dhcps_options_t; -typedef void (*dhcps_cb_t)(u8_t client_ip[4], u8_t client_mac[6]); +typedef void (*dhcps_cb_t)(void* cb_arg, u8_t client_ip[4], u8_t client_mac[6]); static inline bool dhcps_router_enabled (dhcps_offer_t offer) { @@ -179,9 +179,10 @@ err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver); * @brief Sets callback on assigning an IP to the connected client * @param dhcps Pointer to the DHCP handle * @param cb Callback for dhcp server + * @param cb_arg Context pointer to be added to the callback * @return ERR_ARG if invalid handle, ERR_OK on success */ -err_t dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb); +err_t dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb, void* cb_arg); #ifdef __cplusplus }