// is called periodically to check for next version LOCAL void ICACHE_FLASH_ATTR fota_ticktock(fota_client_t *fota_client) { // connection fota_client->conn = (struct espconn *)os_zalloc(sizeof(struct espconn)); fota_client->conn->reverse = (void*)fota_client; fota_client->conn->type = ESPCONN_TCP; fota_client->conn->state = ESPCONN_NONE; // new tcp connection fota_client->conn->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); fota_client->conn->proto.tcp->local_port = espconn_port(); fota_client->conn->proto.tcp->remote_port = fota_client->port; // if ip address is provided, go ahead if (UTILS_StrToIP(fota_client->host, &fota_client->conn->proto.tcp->remote_ip)) { INFO("FOTA client: Connect to ip %s:%d\r\n", fota_client->host, fota_client->port); // check for new version start_esp_connect(fota_client->conn, FOTA_SECURE, get_version_connect_cb, get_version_disconnect_cb); } // else, use dns query to get ip address else { INFO("FOTA client: Connect to domain %s:%d\r\n", fota_client->host, fota_client->port); espconn_gethostbyname(fota_client->conn, fota_client->host, (ip_addr_t *)(fota_client->conn->proto.tcp->remote_ip), fota_dns_found); } }
void ICACHE_FLASH_ATTR GetNetworkTime() { static struct espconn conn; static ip_addr_t ip; os_printf("Looking up server...\n"); os_printf("look"); espconn_gethostbyname(&conn, "www.google.com", &ip, networkTimeFoundCb); }
ICACHE_FLASH_ATTR void ds18b20_run(void) { int temp_low, temp_high; struct Temp temp; if (ds18b20_reset() == ONEWIRE_PRESENT) { ds18b20_putc(Skip_ROM); ds18b20_putc(Convert_T); os_delay_us(DS18B20_CONV_TIME); ds18b20_reset(); ds18b20_putc(Skip_ROM); ds18b20_putc(Read_scratchpad); temp_low = ds18b20_getc(); temp_high = ds18b20_getc(); ConvertTemp(temp_high, temp_low, &temp); os_sprintf(temperature_value, "%c%u.%02u", temp.tsign, temp.tint, temp.tfloat / 100); os_printf("%s: Temperature: %s C.\n", __FUNCTION__, temperature_value); espconn_gethostbyname(&thingspeak_conn, thingspeak_host, &thingspeak_ip, dns_done); } else { os_printf("%s: %s\n", __FUNCTION__, "ds18b20 1-wire signal is absent..."); } }
void ICACHE_FLASH_ATTR http_raw_request(const char * hostname, int port, const char * path, const char * post_data, http_callback user_callback) { PRINTF("DNS request\n"); request_args * req = (request_args *)os_malloc(sizeof(request_args)); req->hostname = esp_strdup(hostname); req->path = esp_strdup(path); req->port = port; req->post_data = esp_strdup(post_data); req->buffer_size = 1; req->buffer = (char *)os_malloc(1); req->buffer[0] = '\0'; // Empty string. req->user_callback = user_callback; ip_addr_t addr; err_t error = espconn_gethostbyname((struct espconn *)req, // It seems we don't need a real espconn pointer here. hostname, &addr, dns_callback); if (error == ESPCONN_INPROGRESS) { PRINTF("DNS pending\n"); } else if (error == ESPCONN_OK) { // Already in the local names table (or hostname was an IP address), execute the callback ourselves. dns_callback(hostname, &addr, req); } else if (error == ESPCONN_ARG) { os_printf("DNS error %s\n", hostname); } else { os_printf("DNS error code %d\n", error); } }
static int do_nslookup(int argc, const char* const* argv) { struct espconn pespconn; ip_addr_t ipaddr; espconn_gethostbyname(&pespconn, argv[1], &ipaddr, nslookup_callback); console_lock(1); return 0; }
sint8 ICACHE_FLASH_ATTR network_start() { static ip_addr_t ip; sint8 result; #ifdef Debug ets_uart_printf("Looking up server...\n"); #endif result = espconn_gethostbyname(&serverConn, "www.asgard.timeenergy.com.br", &ip, serverInit); return result; }
// Perform a TCP command: parse the command and do the right thing. // Returns true on success. bool ICACHE_FLASH_ATTR tcpClientCommand(uint8_t chan, char cmd, char *cmdBuf) { TcpConn *tci; char *hostname; char *port; // copy the command so we can modify it char buf[128]; os_strncpy(buf, cmdBuf, 128); buf[127] = 0; switch (cmd) { //== TCP Connect command case 'T': hostname = buf; port = hostname; while (*port != 0 && *port != ':') port++; if (*port != ':') break; *port = 0; port++; int portInt = atoi(port); if (portInt < 1 || portInt > 65535) break; // allocate a connection tci = tcpConnAlloc(chan); if (tci == NULL) break; tci->state = TCP_dns; tci->tcp->remote_port = portInt; // start the DNS resolution os_printf("TCP %p resolving %s for chan %d (conn=%p)\n", tci, hostname, chan ,tci->conn); ip_addr_t ip; err_t err = espconn_gethostbyname(tci->conn, hostname, &ip, tcpClientHostnameCb); if (err == ESPCONN_OK) { // dns cache hit, got the IP address, fake the callback (sigh) os_printf("TCP DNS hit\n"); tcpClientHostnameCb(hostname, &ip, tci->conn); } else if (err != ESPCONN_INPROGRESS) { tcpConnFree(tci); break; } return true; //== TCP Close/disconnect command case 'C': os_printf("TCP closing chan %d\n", chan); tci = tcpConn+chan; if (tci->state > TCP_idle) { tci->state = TCP_idle; // hackish... espconn_disconnect(tci->conn); } break; } return false; }
void send_discovery_request(uint32_t _ip, char * _name, char * _type){ static struct espconn conn; static ip_addr_t ip; device_ip = _ip; strcpy(device_name, _name); strcpy(device_type, _type); os_printf("Fetching DNS\n"); espconn_gethostbyname(&conn, "local.mirobot.io", &ip, DNSFoundCb); }
/** * @brief Begin connect to MQTT broker * @param client: MQTT_Client reference * @retval None */ void ICACHE_FLASH_ATTR MQTT_Connect(MQTT_Client *mqttClient) { //espconn_secure_set_size(0x01,6*1024); // try to modify memory size 6*1024 if ssl/tls handshake failed if (mqttClient->pCon) { // Clean up the old connection forcefully - using MQTT_Disconnect // does not actually release the old connection until the // disconnection callback is invoked. mqtt_tcpclient_delete(mqttClient); } mqttClient->pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); mqttClient->pCon->type = ESPCONN_TCP; mqttClient->pCon->state = ESPCONN_NONE; mqttClient->pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); mqttClient->pCon->proto.tcp->local_port = espconn_port(); mqttClient->pCon->proto.tcp->remote_port = mqttClient->port; mqttClient->pCon->reverse = mqttClient; espconn_regist_connectcb(mqttClient->pCon, mqtt_tcpclient_connect_cb); espconn_regist_reconcb(mqttClient->pCon, mqtt_tcpclient_recon_cb); mqttClient->keepAliveTick = 0; mqttClient->reconnectTick = 0; os_timer_disarm(&mqttClient->mqttTimer); os_timer_setfn(&mqttClient->mqttTimer, (os_timer_func_t *)mqtt_timer, mqttClient); os_timer_arm(&mqttClient->mqttTimer, 1000, 1); os_printf("your ESP SSL/TLS configuration is %d.[0:NO_TLS\t1:TLS_WITHOUT_AUTHENTICATION\t2ONE_WAY_ANTHENTICATION\t3TWO_WAY_ANTHENTICATION]\n",DEFAULT_SECURITY); if (UTILS_StrToIP(mqttClient->host, &mqttClient->pCon->proto.tcp->remote_ip)) { INFO("TCP: Connect to ip %s:%d\r\n", mqttClient->host, mqttClient->port); if (mqttClient->security) { #ifdef MQTT_SSL_ENABLE if(DEFAULT_SECURITY >= ONE_WAY_ANTHENTICATION ) { espconn_secure_ca_enable(ESPCONN_CLIENT,CA_CERT_FLASH_ADDRESS); } if(DEFAULT_SECURITY >= TWO_WAY_ANTHENTICATION) { espconn_secure_cert_req_enable(ESPCONN_CLIENT,CLIENT_CERT_FLASH_ADDRESS); } espconn_secure_connect(mqttClient->pCon); #else INFO("TCP: Do not support SSL\r\n"); #endif } else { espconn_connect(mqttClient->pCon); } } else { INFO("TCP: Connect to domain %s:%d\r\n", mqttClient->host, mqttClient->port); espconn_gethostbyname(mqttClient->pCon, mqttClient->host, &mqttClient->ip, mqtt_dns_found); } mqttClient->connState = TCP_CONNECTING; }
// Lua: socket:dns( string, function(socket, ip) ) static int net_dns( lua_State* L, const char* mt ) { NODE_DBG("net_dns is called.\n"); bool isserver = false; struct espconn *pesp_conn = NULL; lnet_userdata *nud; size_t l; nud = (lnet_userdata *)luaL_checkudata(L, 1, mt); luaL_argcheck(L, nud, 1, "Server/Socket expected"); if(nud==NULL){ NODE_DBG("userdata is nil.\n"); return 0; } if (mt!=NULL && c_strcmp(mt, "net.server")==0) isserver = true; else if (mt!=NULL && c_strcmp(mt, "net.socket")==0) isserver = false; else { NODE_DBG("wrong metatable for net_send.\n"); return 0; } if(nud->pesp_conn == NULL){ NODE_DBG("nud->pesp_conn is NULL.\n"); return 0; } pesp_conn = nud->pesp_conn; if(!isserver || pesp_conn->type == ESPCONN_UDP){ // self_ref is only needed by socket userdata, or udp server lua_pushvalue(L, 1); // copy to the top of stack if(nud->self_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref); nud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); } const char *domain = luaL_checklstring( L, 2, &l ); if (l>128 || domain == NULL) return luaL_error( L, "need <128 domain" ); if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION){ lua_pushvalue(L, 3); // copy argument (func) to the top of stack if(nud->cb_dns_found_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); nud->cb_dns_found_ref = luaL_ref(L, LUA_REGISTRYINDEX); } host_ip.addr = 0; if(ESPCONN_OK == espconn_gethostbyname(pesp_conn, domain, &host_ip, net_dns_found)) net_dns_found(domain, &host_ip, pesp_conn); // ip is returned in host_ip. return 0; }
LOCAL void ICACHE_FLASH_ATTR start_resolve_dh_server() { static ip_addr_t ip; const char *server = dhrequest_current_server(); char host[os_strlen(server) + 1]; const char *fr = server; while(*fr != ':') { fr++; if(*fr == 0) { fr = 0; break; } } if(fr) { fr++; if(*fr != '/') fr = 0; } if (fr) { while (*fr == '/') fr++; int i = 0; while (*fr != '/' && *fr != ':' && *fr != 0) host[i++] = *fr++; // read port if present int port = 0; if(*fr == ':') { unsigned char d; fr++; while ( (d = *fr - 0x30) < 10) { fr++; port = port*10 + d; if(port > 0xFFFF) break; } } if(port && port < 0xFFFF) mDHConnector.proto.tcp->remote_port = port; else if (os_strncmp(dhrequest_current_server(), "https", 5) == 0) mDHConnector.proto.tcp->remote_port = 443; // HTTPS default port else mDHConnector.proto.tcp->remote_port = 80; //HTTP default port host[i] = 0; dhdebug("Resolving %s", host); err_t r = espconn_gethostbyname(&mDHConnector, host, &ip, resolve_cb); if(r == ESPCONN_OK) { resolve_cb(host, &ip, NULL); } else if(r != ESPCONN_INPROGRESS) { dhesperrors_espconn_result("Resolving failed:", r); arm_repeat_timer(RETRY_CONNECTION_INTERVAL_MS); } } else { dhdebug("Can not find scheme in server url"); } }
static void socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) { NODE_DBG("socket_dns_found is called.\n"); struct espconn *pesp_conn = arg; if(pesp_conn == NULL){ NODE_DBG("pesp_conn null.\n"); return; } lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse; if(nud == NULL) return; if(gL == NULL) return; if(ipaddr == NULL) { dns_reconn_count++; if( dns_reconn_count >= 5 ){ NODE_ERR( "DNS Fail!\n" ); lua_gc(gL, LUA_GCSTOP, 0); if(nud->self_ref != LUA_NOREF){ luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref); nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self } lua_gc(gL, LUA_GCRESTART, 0); return; } NODE_ERR( "DNS retry %d!\n", dns_reconn_count ); host_ip.addr = 0; espconn_gethostbyname(pesp_conn, name, &host_ip, socket_dns_found); return; } // ipaddr->addr is a uint32_t ip if(ipaddr->addr != 0) { dns_reconn_count = 0; if( pesp_conn->type == ESPCONN_TCP ) { c_memcpy(pesp_conn->proto.tcp->remote_ip, &(ipaddr->addr), 4); NODE_DBG("TCP ip is set: "); NODE_DBG(IPSTR, IP2STR(&(ipaddr->addr))); NODE_DBG("\n"); } else if (pesp_conn->type == ESPCONN_UDP) { c_memcpy(pesp_conn->proto.udp->remote_ip, &(ipaddr->addr), 4); NODE_DBG("UDP ip is set: "); NODE_DBG(IPSTR, IP2STR(&(ipaddr->addr))); NODE_DBG("\n"); } socket_connect(pesp_conn); } }
void ICACHE_FLASH_ATTR at_exeCmdUpdate(uint8_t id) { pespconn = (struct espconn *)os_zalloc(sizeof(struct espconn)); pespconn->type = ESPCONN_TCP; pespconn->state = ESPCONN_NONE; pespconn->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); pespconn->proto.tcp->local_port = espconn_port(); pespconn->proto.tcp->remote_port = 80; specialAtState = FALSE; espconn_gethostbyname(pespconn, "iot.espressif.cn", &host_ip, upServer_dns_found); }
static void ICACHE_FLASH_ATTR syslog_gethostbyname() { // // CONSOLE("syslog: IN gethostbyname()"); err_t error = espconn_gethostbyname(&syslog_espconn, syslog_hostname, &syslog_addr, syslog_dns_callback); if (error == ESPCONN_OK) { // Already in the local names table (or hostname was an IP address), execute the callback ourselves. syslog_dns_callback(syslog_hostname, &syslog_addr, NULL); } else if (error != ESPCONN_INPROGRESS) { // CONSOLE("Failed with error: %d", error); } // // CONSOLE("syslog: OUT gethostbyname()"); }
LOCAL void ICACHE_FLASH_ATTR startResolve(const char *domain, dns_found_callback output_mode_cb, dns_found_callback instant_callback) { static ip_addr_t ip; static struct espconn connector = {0}; err_t r = espconn_gethostbyname(&connector, domain, &ip, output_mode_cb); uart_send_line("Resolving..."); if(r == ESPCONN_OK) { instant_callback(domain, &ip, &connector); } else if(r != ESPCONN_INPROGRESS) { uart_send_line("ERROR: illegal request"); } else { mIsCommandWorking = 1; dhserial_set_mode(SM_OUTPUT_MODE, 0, 0); } }
//Init function void ICACHE_FLASH_ATTR user_init() { char ssid[32] = SSID; char password[64] = SSID_PASSWORD; struct station_config stationConf; //Set output debugger bandwidth uart_div_modify(0, UART_CLK_FREQ / 115200); //Set station mode wifi_set_opmode( 0x1 ); //Set ap settings os_memcpy(&stationConf.ssid, ssid, 32); os_memcpy(&stationConf.password, password, 64); if ( wifi_station_set_config(&stationConf) ) { os_printf("Wi-fi configured\n"); } else { os_printf("ERROR:Wi-fi NOT configured\n"); } struct espconn connection; ip_addr_t ipaddr; ipaddr.addr = 3105625166U; // hardcoded IP of golink.besaba.com int rtr_val; if ( (rtr_val = espconn_gethostbyname(&connection, HOSTNAME, &ipaddr, gethost_callback)) != ESPCONN_OK ) { if (rtr_val == ESPCONN_INPROGRESS) { os_printf("ERROR:espconn_gethostbyname INPROGRESS\n"); } else if (rtr_val == ESPCONN_ARG) { os_printf("ERROR:espconn_gethostbyname ARG\n"); } else { os_printf("ERROR:espconn_gethostbyname UNKNOWN ERROR\n"); } } //Start os task system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); system_os_post(user_procTaskPrio, 0, 0 ); }
/** * @brief Begin connect to MQTT broker * @param client: MQTT_Client reference * @retval None */ void ICACHE_FLASH_ATTR MQTT_Connect(MQTT_Client *mqttClient) { if (mqttClient->pCon) { // Clean up the old connection forcefully - using MQTT_Disconnect // does not actually release the old connection until the // disconnection callback is invoked. mqtt_tcpclient_delete(mqttClient); } mqttClient->pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); mqttClient->pCon->type = ESPCONN_TCP; mqttClient->pCon->state = ESPCONN_NONE; mqttClient->pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); mqttClient->pCon->proto.tcp->local_port = espconn_port(); mqttClient->pCon->proto.tcp->remote_port = mqttClient->port; mqttClient->pCon->reverse = mqttClient; espconn_regist_connectcb(mqttClient->pCon, mqtt_tcpclient_connect_cb); espconn_regist_reconcb(mqttClient->pCon, mqtt_tcpclient_recon_cb); mqttClient->keepAliveTick = 0; mqttClient->reconnectTick = 0; os_timer_disarm(&mqttClient->mqttTimer); os_timer_setfn(&mqttClient->mqttTimer, (os_timer_func_t *)mqtt_timer, mqttClient); os_timer_arm(&mqttClient->mqttTimer, 1000, 1); if (UTILS_StrToIP(mqttClient->host, &mqttClient->pCon->proto.tcp->remote_ip)) { MQTT_INFO("TCP: Connect to ip %s:%d\r\n", mqttClient->host, mqttClient->port); if (mqttClient->security) { #ifdef MQTT_SSL_ENABLE espconn_secure_set_size(ESPCONN_CLIENT, MQTT_SSL_SIZE); espconn_secure_connect(mqttClient->pCon); #else MQTT_INFO("TCP: Do not support SSL\r\n"); #endif } else { espconn_connect(mqttClient->pCon); } } else { MQTT_INFO("TCP: Connect to domain %s:%d\r\n", mqttClient->host, mqttClient->port); espconn_gethostbyname(mqttClient->pCon, mqttClient->host, &mqttClient->ip, mqtt_dns_found); } mqttClient->connState = TCP_CONNECTING; }
/****************************************************************************** * FunctionName : initSyslog * Description : Initialize the syslog library * Parameters : syslog_host -- the syslog host (host:port) * host: IP-Addr | hostname * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR syslog_init(char *syslog_host) { if (!*syslog_host) { syslogState = SYSLOG_HALTED; return; } char host[32], *port = &host[0]; syslog_task = register_usr_task(syslog_udp_send_event); syslogHost.min_heap_size = flashConfig.syslog_minheap; syslogHost.port = 514; syslogState = SYSLOG_WAIT; os_strncpy(host, syslog_host, 32); while (*port && *port != ':') // find port delimiter port++; if (*port) { *port++ = '\0'; syslogHost.port = atoi(port); } wifi_set_broadcast_if(STATIONAP_MODE); // send UDP broadcast from both station and soft-AP interface syslog_espconn.type = ESPCONN_UDP; syslog_espconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); syslog_espconn.proto.udp->local_port = espconn_port(); // set a available port #ifdef SYSLOG_UDP_RECV espconn_regist_recvcb(&syslog_espconn, syslog_udp_recv_cb); // register a udp packet receiving callback #endif espconn_regist_sentcb(&syslog_espconn, syslog_udp_sent_cb); // register a udp packet sent callback espconn_create(&syslog_espconn); // create udp if (UTILS_StrToIP((const char *)host, (void*)&syslogHost.addr)) { syslogState = SYSLOG_SENDING; syslog_send_udp(); } else { static struct espconn espconn_ghbn; espconn_gethostbyname(&espconn_ghbn, host, &syslogHost.addr, syslog_gethostbyname_cb); // syslog_send_udp is called by syslog_gethostbyname_cb() } #ifdef SYSLOG_UDP_RECV DBG("syslog_init: host: %s, port: %d, lport: %d, recvcb: %p, sentcb: %p, state: %d\n", host, syslogHost.port, syslog_espconn.proto.udp->local_port, syslog_udp_recv_cb, syslog_udp_sent_cb, syslogState ); #else DBG("syslog_init: host: %s, port: %d, lport: %d, rsentcb: %p, state: %d\n", host, syslogHost.port, syslog_espconn.proto.udp->local_port, syslog_udp_sent_cb, syslogState ); #endif }
/** * Get an IP address from a name. * Sets 'outIp' to 0 on failure and 0xFFFFFFFF on unknown. At some time later, the * IP address will be properly updated. */ void net_ESP8266_BOARD_gethostbyname( JsNetwork *net, //!< The Network we are going to use to create the socket. char *hostName, //!< The string representing the hostname we wish to lookup. uint32_t *outIp //!< The address into which the resolved IP address will be stored. ) { assert(hostName != NULL); assert(outIp != NULL); os_printf("> net_ESP8266_BOARD_gethostbyname: Resolving: %s\n", hostName); int rc = espconn_gethostbyname((struct espconn *)outIp, hostName, (ip_addr_t *)outIp, dnsFoundCallback); // A rc of ESPCONN_OK means that we have an IP and it was stored in outIp. // A rc of ESPCONN_INPROGRESS means that we will get the IP on a callback. if (rc == ESPCONN_INPROGRESS) { *outIp = 0xFFFFFFFF; } }
/** * Create a new socket. * if `ipAddress == 0`, creates a server otherwise creates a client (and automatically connects). * Returns >=0 on success. */ int net_ESP8266_BOARD_createSocket( JsNetwork *net, //!< The Network we are going to use to create the socket. uint32_t ipAddress, //!< The address of the partner of the socket or 0 if we are to be a server. unsigned short port //!< The port number that the partner is listening upon. ) { // allocate a socket data structure struct socketData *pSocketData = allocateNewSocket(); if (pSocketData == NULL) { // No free socket DBG("%s: No free sockets for outbound connection\n", DBG_LIB); return SOCKET_ERR_MAX_SOCK; } // allocate espconn data structure and initialize it struct espconn *pEspconn = os_zalloc(sizeof(struct espconn)); esp_tcp *tcp = os_zalloc(sizeof(esp_tcp)); if (pEspconn == NULL || tcp == NULL) { DBG("%s: Out of memory for outbound connection\n", DBG_LIB); if (pEspconn != NULL) os_free(pEspconn); if (tcp != NULL) os_free(tcp); releaseSocket(pSocketData); return SOCKET_ERR_MEM; } pSocketData->pEspconn = pEspconn; pEspconn->type = ESPCONN_TCP; pEspconn->state = ESPCONN_NONE; pEspconn->proto.tcp = tcp; tcp->remote_port = port; tcp->local_port = espconn_port(); // using 0 doesn't work pEspconn->reverse = pSocketData; espconn_set_opt(pEspconn, ESPCONN_NODELAY); // disable nagle, don't need the extra delay if (ipAddress == (uint32_t)-1) { // We need DNS resolution, kick it off int rc = espconn_gethostbyname(pEspconn, savedHostname, (void*)&pEspconn->proto.tcp->remote_ip, dnsFoundCallback); if (rc < 0) { } DBG("%s: resolving %s\n", DBG_LIB, savedHostname); pSocketData->state = SOCKET_STATE_HOST_RESOLVING; return pSocketData->socketId; } else { // No DNS resolution needed, go right ahead *(uint32_t *)&pEspconn->proto.tcp->remote_ip = ipAddress; return connectSocket(pSocketData); } }
/** * @brief Begin connect to MQTT broker * @param client: MQTT_Client reference * @retval None */ void ICACHE_FLASH_ATTR MQTT_Connect(MQTT_Client *mqttClient) { // Do not connect if this client is already connected otherwise the // two espconn connections may interfere causing unexpected behaviour. if (mqttClient->pCon) { return; } mqttClient->pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); mqttClient->pCon->type = ESPCONN_TCP; mqttClient->pCon->state = ESPCONN_NONE; mqttClient->pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); mqttClient->pCon->proto.tcp->local_port = espconn_port(); mqttClient->pCon->proto.tcp->remote_port = mqttClient->port; mqttClient->pCon->reverse = mqttClient; espconn_regist_connectcb(mqttClient->pCon, mqtt_tcpclient_connect_cb); espconn_regist_reconcb(mqttClient->pCon, mqtt_tcpclient_recon_cb); mqttClient->keepAliveTick = 0; mqttClient->reconnectTick = 0; os_timer_disarm(&mqttClient->mqttTimer); os_timer_setfn(&mqttClient->mqttTimer, (os_timer_func_t *)mqtt_timer, mqttClient); os_timer_arm(&mqttClient->mqttTimer, 1000, 1); if (UTILS_StrToIP(mqttClient->host, &mqttClient->pCon->proto.tcp->remote_ip)) { INFO("TCP: Connect to ip %s:%d\r\n", mqttClient->host, mqttClient->port); if (mqttClient->security) { #ifdef MQTT_SSL_ENABLE espconn_secure_connect(mqttClient->pCon); #else INFO("TCP: Do not support SSL\r\n"); #endif } else { espconn_connect(mqttClient->pCon); } } else { INFO("TCP: Connect to domain %s:%d\r\n", mqttClient->host, mqttClient->port); espconn_gethostbyname(mqttClient->pCon, mqttClient->host, &mqttClient->ip, mqtt_dns_found); } mqttClient->connState = TCP_CONNECTING; }
/** * @brief Begin connect to MQTT broker * @param client: MQTT_Client reference * @retval None */ void ICACHE_FLASH_ATTR MQTT_Connect(MQTT_Client *mqttClient) { if (mqttClient->pCon) { // Clean up the old connection forcefully - using MQTT_Disconnect // does not actually release the old connection until the // disconnection callback is invoked. mqtt_tcpclient_delete(mqttClient); } mqttClient->pCon = (struct espconn *)calloc(1, sizeof(struct espconn)); mqttClient->pCon->type = ESPCONN_TCP; mqttClient->pCon->state = ESPCONN_NONE; mqttClient->pCon->proto.tcp = (esp_tcp *)calloc(1, sizeof(esp_tcp)); mqttClient->pCon->proto.tcp->local_port = espconn_port(); mqttClient->pCon->proto.tcp->remote_port = mqttClient->port; mqttClient->pCon->reserve = mqttClient; espconn_regist_connectcb(mqttClient->pCon, mqtt_tcpclient_connect_cb); espconn_regist_reconcb(mqttClient->pCon, mqtt_tcpclient_recon_cb); mqttClient->keepAliveTick = 0; mqttClient->reconnectTick = 0; xTimerReset(mqttClient->mqttTimer, portMAX_DELAY); xTimerStart(mqttClient->mqttTimer, portMAX_DELAY); if (UTILS_StrToIP(mqttClient->host, &mqttClient->pCon->proto.tcp->remote_ip)) { INFO("TCP: Connect to ip %s:%d\r\n", mqttClient->host, mqttClient->port); if (mqttClient->security) { #ifdef MQTT_SSL_ENABLE espconn_secure_connect(mqttClient->pCon); #else INFO("TCP: Do not support SSL\r\n"); #endif } else { espconn_connect(mqttClient->pCon); } } else { INFO("TCP: Connect to domain %s:%d\r\n", mqttClient->host, mqttClient->port); espconn_gethostbyname(mqttClient->pCon, mqttClient->host, &mqttClient->ip, mqtt_dns_found); } mqttClient->connState = TCP_CONNECTING; }
void MQTT_Connect(MQTT_Client *mqttClient) { if(UTILS_StrToIP(mqttClient->host, &mqttClient->pCon->proto.tcp->remote_ip)) { //spam INFO("TCP: Connect to ip %s:%d\r\n", mqttClient->host, mqttClient->port); if(mqttClient->security){ espconn_secure_connect(mqttClient->pCon); } else { espconn_connect(mqttClient->pCon); } } else { //spam INFO("TCP: Connect to domain %s:%d\r\n", mqttClient->host, mqttClient->port); espconn_gethostbyname(mqttClient->pCon, mqttClient->host, &mqttClient->ip, mqtt_dns_found); } mqttClient->connState = TCP_CONNECTING; }
void FUNCTION_ATTRIBUTE http_raw_request(const char * hostname, int port, const char * path, const char * post_data, http_callback user_callback) { if(http_flag == 1) { PRINTF("http client is running, exit"); return; } http_flag = 1; PRINTF("DNS request\n"); os_timer_disarm(&timeout_timer); os_timer_setfn(&timeout_timer, (os_timer_func_t *)http_exit, HTTP_TIMEOUT); os_timer_arm(&timeout_timer, 20000, 0); http_hostname = my_strdup(hostname); http_path = my_strdup(path); http_port = port; http_post_data = my_strdup(post_data); // respond buf http_buf = (HTTP_BUF*)os_malloc(sizeof(HTTP_BUF)); http_buf->buffer = (char *)os_malloc(1); http_buf->buffer[0] = '\0'; // Empty string. http_buf->buffer_size = 1; user_cb= user_callback; ip_addr_t addr; err_t error = espconn_gethostbyname(NULL, // It seems we don't need a real espconn pointer here. hostname, &addr, dns_callback); if (error == ESPCONN_INPROGRESS) { PRINTF("DNS pending\n"); } else if (error == ESPCONN_OK) { // Already in the local names table (or hostname was an IP address), execute the callback ourselves. dns_callback(hostname, &addr, NULL); } else if (error == ESPCONN_ARG) { PRINTF("DNS error %s\n", hostname); } else { PRINTF("DNS error code %d\n", error); } }
void ICACHE_FLASH_ATTR net_asyncConnect(NetConnection* conn, const char* hostname) { uart0_tx_buffer("connect\r\n", 9); HTTPESP8266ConnectionData* driver = esp8266_createConnection(conn); driver->secure = 1; char pageBuffer[20]; ets_sprintf(pageBuffer, "r: %s\r\n", hostname); uart0_tx_buffer(pageBuffer, strlen(pageBuffer)); err_t e = espconn_gethostbyname(&driver->connection, hostname, &driver->ip, esp8266_resolveCallback); switch(e) { case ESPCONN_OK: // dns cached, no async call esp8266_resolveCallback(hostname, &driver->ip, &driver->connection); case ESPCONN_INPROGRESS: // dns request queued, let callback happen return; default: // error fetching hostname esp8266_resolveCallback(hostname, NULL, &driver->connection); }; }
static void socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) { NODE_DBG("socket_dns_found is called.\n"); struct espconn *pesp_conn = arg; if(pesp_conn == NULL){ NODE_DBG("pesp_conn null.\n"); return; } if(ipaddr == NULL) { dns_reconn_count++; if( dns_reconn_count >= 5 ){ NODE_ERR( "DNS Fail!\n" ); return; } NODE_ERR( "DNS retry %d!\n", dns_reconn_count ); host_ip.addr = 0; espconn_gethostbyname(pesp_conn, name, &host_ip, socket_dns_found); return; } // ipaddr->addr is a uint32_t ip if(ipaddr->addr != 0) { dns_reconn_count = 0; if( pesp_conn->type == ESPCONN_TCP ) { c_memcpy(pesp_conn->proto.tcp->remote_ip, &(ipaddr->addr), 4); NODE_DBG("TCP ip is set: "); NODE_DBG(IPSTR, IP2STR(&(ipaddr->addr))); NODE_DBG("\n"); } else if (pesp_conn->type == ESPCONN_UDP) { c_memcpy(pesp_conn->proto.udp->remote_ip, &(ipaddr->addr), 4); NODE_DBG("UDP ip is set: "); NODE_DBG(IPSTR, IP2STR(&(ipaddr->addr))); NODE_DBG("\n"); } socket_connect(pesp_conn); } }
void ESP_RestClient::m_resolveHostname() { LOG("About to resolve hostname of: %s\n", m_hostname.c_str()); if (m_serverIP.addr != 0) { _connect(); return; } int rc = espconn_gethostbyname(&m_conn, m_hostname.c_str(), &m_serverIP, dnsFoundCallback); if (rc == ESPCONN_OK) { LOG("We immediately have an IP address\n"); _connect(); return; } if (rc != ESPCONN_INPROGRESS) { _handleError(); return; } return; } // End of m_resolveHostname
/** * @brief Begin connect to MQTT broker * @param client: MQTT_Client reference * @retval None */ void ICACHE_FLASH_ATTR MQTT_Connect(MQTT_Client* client) { //MQTT_Disconnect(client); client->pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); client->pCon->type = ESPCONN_TCP; client->pCon->state = ESPCONN_NONE; client->pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); client->pCon->proto.tcp->local_port = espconn_port(); client->pCon->proto.tcp->remote_port = client->port; client->pCon->reverse = client; espconn_regist_connectcb(client->pCon, mqtt_tcpclient_connect_cb); espconn_regist_reconcb(client->pCon, mqtt_tcpclient_recon_cb); // start timer function to tick every second os_timer_disarm(&client->mqttTimer); os_timer_setfn(&client->mqttTimer, (os_timer_func_t *)mqtt_timer, client); os_timer_arm(&client->mqttTimer, 1000, 1); // initiate the TCP connection or DNS lookup os_printf("MQTT: Connect to %s:%d %p\n", client->host, client->port, client->pCon); if (UTILS_StrToIP((const char *)client->host, (void*)&client->pCon->proto.tcp->remote_ip)) { uint8_t err; if (client->security) err = espconn_secure_connect(client->pCon); else err = espconn_connect(client->pCon); if (err != 0) { os_printf("MQTT ERROR: Failed to connect\n"); os_free(client->pCon->proto.tcp); os_free(client->pCon); client->pCon = NULL; return; } } else { espconn_gethostbyname(client->pCon, (const char *)client->host, &client->ip, mqtt_dns_found); } client->connState = TCP_CONNECTING; client->timeoutTick = 20; // generous timeout to allow for DNS, etc client->sending = FALSE; }
/** * @brief Begin connect to MQTT broker * @param client: MQTT_Client reference * @retval None */ void ICACHE_FLASH_ATTR MQTT_Connect(MQTT_Client *mqttClient) { err_t error; MQTT_Disconnect(mqttClient); // Could probably get right of zalloc using static variable and then // checking we don't go into this when already using it. mqttClient->pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); mqttClient->pCon->type = ESPCONN_TCP; mqttClient->pCon->state = ESPCONN_NONE; mqttClient->pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); mqttClient->pCon->proto.tcp->local_port = espconn_port(); mqttClient->pCon->proto.tcp->remote_port = mqttClient->port; mqttClient->pCon->reverse = mqttClient; espconn_regist_connectcb(mqttClient->pCon, mqtt_tcpclient_connect_cb); espconn_regist_reconcb(mqttClient->pCon, mqtt_tcpclient_recon_cb); mqttClient->keepAliveTick = 0; mqttClient->reconnectTick = 0; os_timer_disarm(&mqttClient->mqttTimer); os_timer_setfn(&mqttClient->mqttTimer, (os_timer_func_t *)mqtt_timer, mqttClient); os_timer_arm(&mqttClient->mqttTimer, 1000, 1); if(UTILS_StrToIP(mqttClient->host, &mqttClient->pCon->proto.tcp->remote_ip)) { DEBUG("TCP: Connect to ip %s:%d", mqttClient->host, mqttClient->port); if(mqttClient->security){ espconn_secure_connect(mqttClient->pCon); } else { espconn_connect(mqttClient->pCon); } } else { DEBUG("TCP: Connect to domain %s:%d", mqttClient->host, mqttClient->port); error = espconn_gethostbyname(mqttClient->pCon, mqttClient->host, &mqttClient->ip, mqtt_dns_found); if (error == ESPCONN_ARG) ERROR("MQTT esp: Failed to lookup host"); } mqttClient->connState = TCP_CONNECTING; }
/** * @brief Begin connect to MQTT broker * @param client: MQTT_Client reference * @retval None */ void ICACHE_FLASH_ATTR MQTT_Connect(MQTT_Client *mqttClient) { MQTT_Disconnect(mqttClient); mqttClient->pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); mqttClient->pCon->type = ESPCONN_TCP; mqttClient->pCon->state = ESPCONN_NONE; mqttClient->pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); mqttClient->pCon->proto.tcp->local_port = espconn_port(); mqttClient->pCon->proto.tcp->remote_port = mqttClient->port; mqttClient->pCon->reverse = mqttClient; espconn_regist_connectcb(mqttClient->pCon, mqtt_tcpclient_connect_cb); espconn_regist_reconcb(mqttClient->pCon, mqtt_tcpclient_recon_cb); mqttClient->keepAliveTick = 0; mqttClient->reconnectTick = 0; os_timer_disarm(&mqttClient->mqttTimer); os_timer_setfn(&mqttClient->mqttTimer, (os_timer_func_t *)mqtt_timer, mqttClient); os_timer_arm(&mqttClient->mqttTimer, 1000, 1); if(UTILS_StrToIP(mqttClient->host, &mqttClient->pCon->proto.tcp->remote_ip)) { INFO("TCP: Connect to ip %s:%d\r\n", mqttClient->host, mqttClient->port); #ifdef MQTT_SECURE if(mqttClient->security){ espconn_secure_connect(mqttClient->pCon); } else #endif { espconn_connect(mqttClient->pCon); } } else { INFO("TCP: Connect to domain %s:%d\r\n", mqttClient->host, mqttClient->port); espconn_gethostbyname(mqttClient->pCon, mqttClient->host, &mqttClient->ip, mqtt_dns_found); } mqttClient->connState = TCP_CONNECTING; }