lwip/dhcps: Add dhcps callback argument for associated netif

This commit is contained in:
David Cermak 2022-02-02 17:17:39 +01:00
parent 775c3a6253
commit 3d1c05aefb
4 changed files with 15 additions and 9 deletions

View File

@ -135,6 +135,7 @@ typedef struct {
/** Event structure for IP_EVENT_AP_STAIPASSIGNED event */ /** Event structure for IP_EVENT_AP_STAIPASSIGNED event */
typedef struct { 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 */ esp_ip4_addr_t ip; /*!< IP address which was assigned to the station */
uint8_t mac[6]; /*!< MAC address of the connected client */ uint8_t mac[6]; /*!< MAC address of the connected client */
} ip_event_ap_staipassigned_t; } ip_event_ap_staipassigned_t;

View File

@ -793,12 +793,14 @@ esp_err_t esp_netif_recv_hook_detach(esp_netif_t *esp_netif)
#endif // CONFIG_ESP_NETIF_L2_TAP #endif // CONFIG_ESP_NETIF_L2_TAP
#if ESP_DHCPS #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.ip.addr, (char *)ip, sizeof(evt.ip.addr));
memcpy((char *)&evt.mac, mac, sizeof(evt.mac)); 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]); 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); 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; ip4_addr_t lwip_netmask;
memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr)); memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr));
memcpy(&lwip_netmask, &default_ip->netmask, 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_set_option_info(esp_netif->dhcps, SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask));
dhcps_start(esp_netif->dhcps, p_netif, lwip_ip); dhcps_start(esp_netif->dhcps, p_netif, lwip_ip);
esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; 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; ip4_addr_t lwip_netmask;
memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr)); memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr));
memcpy(&lwip_netmask, &default_ip->netmask, 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_set_option_info(esp_netif->dhcps, SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask));
dhcps_start(esp_netif->dhcps, p_netif, lwip_ip); dhcps_start(esp_netif->dhcps, p_netif, lwip_ip);
esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED;

View File

@ -101,6 +101,7 @@ struct dhcps_t {
dhcps_offer_t dhcps_offer; dhcps_offer_t dhcps_offer;
dhcps_offer_t dhcps_dns; dhcps_offer_t dhcps_dns;
dhcps_cb_t dhcps_cb; dhcps_cb_t dhcps_cb;
void* dhcps_cb_arg;
struct udp_pcb *dhcps_pcb; struct udp_pcb *dhcps_pcb;
dhcps_handle_state state; dhcps_handle_state state;
}; };
@ -741,7 +742,7 @@ static void send_ack(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len)
#endif #endif
if (SendAck_err_t == ERR_OK) { 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) { 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 * Parameters : cb -- callback for dhcp server
* Returns : ERR_OK on success * 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) { if (dhcps == NULL) {
return ERR_ARG; return ERR_ARG;
} }
dhcps->dhcps_cb = cb; dhcps->dhcps_cb = cb;
dhcps->dhcps_cb_arg = cb_arg;
return ERR_OK; return ERR_OK;
} }

View File

@ -82,7 +82,7 @@ typedef struct {
dhcps_lease_t dhcps_poll; dhcps_lease_t dhcps_poll;
} dhcps_options_t; } 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) 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 * @brief Sets callback on assigning an IP to the connected client
* @param dhcps Pointer to the DHCP handle * @param dhcps Pointer to the DHCP handle
* @param cb Callback for dhcp server * @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 * @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 #ifdef __cplusplus
} }