#include "discover.h"
LOCAL struct espconn Conn;
static ETSTimer WiFiLinker;
uint8 udpServerIP[] = { 255, 255, 255, 255 };
/******************************************************************************
* FunctionName : user_devicefind_recv
* Description : Processing the received data from the host
* Parameters : arg -- Additional argument to pass to the callback function
* pusrdata -- The received data (or NULL when the connection has been closed!)
* length -- The length of received data
* Returns : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
recvdata(void *arg, char *pusrdata, unsigned short length)
{
os_printf("Recv data callback\n");
char DeviceBuffer[40] = {0};
char Device_mac_buffer[60] = {0};
char hwaddr[6];
struct ip_info ipconfig;
if (wifi_get_opmode() != STATION_MODE) {
wifi_get_ip_info(SOFTAP_IF, &ipconfig);
wifi_get_macaddr(SOFTAP_IF, hwaddr);
if (!ip_addr_netcmp((struct ip_addr *)Conn.proto.udp->remote_ip, &ipconfig.ip, &ipconfig.netmask)) {
wifi_get_ip_info(STATION_IF, &ipconfig);
wifi_get_macaddr(STATION_IF, hwaddr);
}
} else {
wifi_get_ip_info(STATION_IF, &ipconfig);
wifi_get_macaddr(STATION_IF, hwaddr);
}
if (pusrdata == NULL) {
return;
}
else {
os_printf("recv data: %s\n", pusrdata); // only contains received UDP messages, I need the IP address
}
}
void DISCOVER_Init(void) {
os_printf("Entering DISCOVER mode\n");
Conn.type = ESPCONN_UDP;
Conn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
Conn.proto.udp->local_port = 15002;
Conn.proto.udp->remote_port = 15002;
os_memcpy(Conn.proto.udp->remote_ip, udpServerIP, 4);
espconn_regist_recvcb(&Conn, recvdata);
espconn_create(&Conn);
os_timer_disarm(&WiFiLinker);
os_timer_setfn(&WiFiLinker, (os_timer_func_t *)senddata, NULL);
os_timer_arm(&WiFiLinker, 15000, 0);
}
static void ICACHE_FLASH_ATTR senddata()
{
unsigned short length;
os_timer_disarm(&WiFiLinker);
os_printf("Broadcasting UDP DeviceDiscover...\n");
espconn_sent(&Conn, "DeviceDiscover", 14);
}
Anyone have suggestion to get the source IP from these UDP packets?