From 3bf3ba5e0ceec0ac81c3f0335d7e5d85ed866a32 Mon Sep 17 00:00:00 2001 From: susisstrolch Date: Tue, 12 Jan 2016 02:17:40 +0100 Subject: [PATCH] app_init: check Wifi ready by looking up hostname --- user/user_main.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/user/user_main.c b/user/user_main.c index 5a4331d..e71f4ce 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -1,7 +1,52 @@ #include -#include +#include "config.h" +#include "syslog.h" +#include "ems.h" + +#define APPINIT_DBG +#ifdef APPINIT_DBG +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#else +#define DBG(format, ...) do { } while(0) +#endif + +static struct espconn *pespconn = NULL; +static ip_addr_t addr; +static char host[32]; + +/****************************************************************************** + * + ******************************************************************************/ +static void ICACHE_FLASH_ATTR appinit_gethostbyname_cb(const char *name, ip_addr_t *ipaddr, void *arg) +{ + os_free(((struct espconn *)arg)->proto.udp); + os_free((struct espconn *)arg); + + if (ipaddr != NULL) { + emsInit(); + EMSBusStatus |= EMSBUS_RDY; + DBG("app_init: EMSBus ready...\n"); + } else { + pespconn = (espconn *)os_zalloc(sizeof(espconn)); + pespconn->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); + + espconn_gethostbyname(pespconn, host, &addr, appinit_gethostbyname_cb); + DBG("app_init: retry to resolve %s...\n", host); + } +} // initialize the custom stuff that goes beyond esp-link +// we simply try to resolve void app_init() { + if (pespconn == NULL) + pespconn = (espconn *)os_zalloc(sizeof(espconn)); + + if (pespconn->proto.udp == NULL) + pespconn->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); + + os_sprintf(host, "%s", flashConfig.hostname); + DBG("app_init: resolving %s...\n", host); + + espconn_gethostbyname(pespconn, host, &addr, appinit_gethostbyname_cb); }