int sj_wifi_setup_sta(const struct sys_config_wifi_sta *cfg) { int res; struct station_config sta_cfg; /* If in AP-only mode, switch to station. If in STA or AP+STA, keep it. */ if (wifi_get_opmode() == SOFTAP_MODE) { wifi_set_opmode_current(STATION_MODE); } wifi_station_disconnect(); memset(&sta_cfg, 0, sizeof(sta_cfg)); sta_cfg.bssid_set = 0; strncpy((char *) &sta_cfg.ssid, cfg->ssid, 32); strncpy((char *) &sta_cfg.password, cfg->pass, 64); res = wifi_station_set_config_current(&sta_cfg); if (!res) { LOG(LL_ERROR, ("Failed to set station config")); return 0; } if (cfg->ip != NULL && cfg->netmask != NULL) { struct ip_info info; memset(&info, 0, sizeof(info)); info.ip.addr = ipaddr_addr(cfg->ip); info.netmask.addr = ipaddr_addr(cfg->netmask); if (cfg->gw != NULL) info.gw.addr = ipaddr_addr(cfg->gw); wifi_station_dhcpc_stop(); wifi_set_ip_info(STATION_IF, &info); LOG(LL_INFO, ("WiFi STA IP config: %s %s %s", cfg->ip, cfg->netmask, (cfg->gw ? cfg->ip : ""))); } LOG(LL_INFO, ("WiFi STA: Joining %s", sta_cfg.ssid)); return wifi_station_connect(); }
static int net_multicastJoinLeave( lua_State *L, int join) { size_t il; ip_addr_t multicast_addr; ip_addr_t if_addr; const char *multicast_ip; const char *if_ip; NODE_DBG("net_multicastJoin is called.\n"); if(! lua_isstring(L,1) ) return luaL_error( L, "wrong arg type" ); if_ip = luaL_checklstring( L, 1, &il ); if (if_ip != NULL) if ( if_ip[0] == '\0' || stricmp(if_ip,"any") == 0) { if_ip = "0.0.0.0"; il = 7; } if (if_ip == NULL || il > 15 || il < 7) return luaL_error( L, "invalid if ip" ); if_addr.addr = ipaddr_addr(if_ip); if(! lua_isstring(L,2) ) return luaL_error( L, "wrong arg type" ); multicast_ip = luaL_checklstring( L, 2, &il ); if (multicast_ip == NULL || il > 15 || il < 7) return luaL_error( L, "invalid multicast ip" ); multicast_addr.addr = ipaddr_addr(multicast_ip); if (join) { igmp_joingroup(&if_addr, &multicast_addr); } else { igmp_leavegroup(&if_addr, &multicast_addr); } return 0; }
void STC_FLASHMEM IP::configureStatic(WiFi::Network network, char const *IP, char const *netmask, char const *gateway) { ip_info info; info.ip.addr = ipaddr_addr(APtr<char>(f_strdup(IP)).get()); info.netmask.addr = ipaddr_addr(APtr<char>(f_strdup(netmask)).get()); info.gw.addr = ipaddr_addr(APtr<char>(f_strdup(gateway)).get()); Critical critical; wifi_station_dhcpc_stop(); wifi_softap_dhcps_stop(); wifi_set_ip_info(network, &info); }
/****************************************************************************** * FunctionName : user_init * Description : entry of user application, init user function here * Parameters : none * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR user_init(void) { printf("SDK version:%d.%d.%d\n", SDK_VERSION_MAJOR, SDK_VERSION_MINOR, SDK_VERSION_REVISION); /* need to set opmode before you set config */ wifi_set_opmode(STATIONAP_MODE); { struct station_config *config = (struct station_config *)zalloc(sizeof(struct station_config)); sprintf(config->ssid, "CVR100W_T"); sprintf(config->password, "justfortest"); /* need to sure that you are in station mode first, * otherwise it will be failed. */ wifi_station_set_config(config); free(config); } { struct ip_info ipinfo; ipinfo.gw.addr = ipaddr_addr("192.168.145.253"); ipinfo.ip.addr = ipaddr_addr("192.168.145.253"); ipinfo.netmask.addr = ipaddr_addr("255.255.255.0"); wifi_set_ip_info(SOFTAP_IF, &ipinfo); } { struct dhcp_info *pdhcp_info = NULL; pdhcp_info = (struct dhcp_info *)zalloc(sizeof(struct dhcp_info)); pdhcp_info->start_ip = ipaddr_addr("192.168.145.100"); pdhcp_info->end_ip = ipaddr_addr("192.168.145.110"); // don't set the range too large, because it will cost memory. pdhcp_info->max_leases = 10; pdhcp_info->auto_time = 60; pdhcp_info->decline_time = 60; pdhcp_info->conflict_time = 60; pdhcp_info->offer_time = 60; pdhcp_info->min_lease_sec = 60; dhcp_set_info(pdhcp_info); free(pdhcp_info); } udhcpd_start(); xTaskCreate(task2, "tsk2", 256, NULL, 2, NULL); xTaskCreate(task3, "tsk3", 256, NULL, 2, NULL); }
// warn: each IP in the range requires memory! void STC_FLASHMEM DHCPServer::configure(char const* startIP, char const* endIP, uint32_t maxLeases) { //udhcpd_stop(); dhcp_info info = {0}; info.start_ip = ipaddr_addr(APtr<char>(f_strdup(startIP)).get()); info.end_ip = ipaddr_addr(APtr<char>(f_strdup(endIP)).get()); info.max_leases = maxLeases; info.auto_time = 60; info.decline_time = 60; info.conflict_time = 60; info.offer_time = 60; info.min_lease_sec = 60; dhcp_set_info(&info); udhcpd_start(); }
void ICACHE_FLASH_ATTR at_setupCmdCifsr(uint8_t id, char *pPara) { struct ip_info pTempIp; int8_t len; char ipTemp[64]; // char temp[64]; if(at_wifiMode == STATION_MODE) { at_backError; return; } pPara = strchr(pPara, '\"'); len = at_dataStrCpy(ipTemp, pPara, 32); if(len == -1) { uart0_sendStr("IP ERROR\r\n"); return; } wifi_get_ip_info(0x01, &pTempIp); pTempIp.ip.addr = ipaddr_addr(ipTemp); os_printf("%d.%d.%d.%d\r\n", IP2STR(&pTempIp.ip)); if(!wifi_set_ip_info(0x01, &pTempIp)) { at_backError; return; } at_backOk; return; }
void MTD_FLASHMEM IPAddress::operator=(char const* str) { if (!str || f_strlen(str) == 0) *this = IPAddress(0, 0, 0, 0); else *this = IPAddress(ipaddr_addr(APtr<char>(f_strdup(str)).get())); }
// ---------------------------------------------------------------------------- // Send data // Parameters: uint8* psent -- Data to send // uint16 length -- Length of data to send // ---------------------------------------------------------------------------- static void create_udp(void) { uint32_t ip; os_timer_disarm(&WiFiLinker); ip = ipaddr_addr(UDPSERVERIP); os_memcpy(ConnUDP.remote_ip, &ip, 4); struct ip_info ipConfig; wifi_get_ip_info(STATION_IF, &ipConfig); os_memcpy(ConnUDP.local_ip, &ipConfig.ip.addr, 4); ConnUDP.local_port = UDPSERVERPORT; ConnUDP.remote_port = UDPSERVERPORT; Conn.proto.udp = &ConnUDP; Conn.type = ESPCONN_UDP; Conn.state = ESPCONN_NONE; espconn_regist_recvcb(&Conn, recv_cb); // register a udp packet receiving callback espconn_regist_sentcb(&Conn, sent_cb); os_printf("Start espconn_connect to " IPSTR ":%d\n", IP2STR(Conn.proto.udp->remote_ip), Conn.proto.udp->remote_port); os_printf("Start espconn_connect from " IPSTR ":%d\n", IP2STR(Conn.proto.udp->local_ip), Conn.proto.udp->local_port); espconn_create(&Conn); // create udp os_timer_setfn(&WiFiLinker, (os_timer_func_t *)wifi_check_ip, NULL); os_timer_arm(&WiFiLinker, 1000, 0); }
static void ICACHE_FLASH_ATTR senddata() { char info[150]; char tcpserverip[15]; struct espconn *pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); if (pCon == NULL) { #ifdef PLATFORM_DEBUG ets_uart_printf("TCP connect failed\r\n"); #endif return; } pCon->type = ESPCONN_TCP; pCon->state = ESPCONN_NONE; os_sprintf(tcpserverip, "%s", TCPSERVERIP); uint32_t ip = ipaddr_addr(tcpserverip); pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); pCon->proto.tcp->local_port = espconn_port(); pCon->proto.tcp->remote_port = TCPSERVERPORT; os_memcpy(pCon->proto.tcp->remote_ip, &ip, 4); espconn_regist_connectcb(pCon, at_tcpclient_connect_cb); //espconn_regist_reconcb(pCon, at_tcpclient_recon_cb); #ifdef PLATFORM_DEBUG os_sprintf(info,"Start espconn_connect to " IPSTR ":%d\r\n", IP2STR(pCon->proto.tcp->remote_ip), pCon->proto.tcp->remote_port); ets_uart_printf(info); #endif espconn_connect(pCon); }
void ICACHE_FLASH_ATTR spi_flash_write_test(enum RUN_MODE mode) { rw_info rw; os_memset(&rw, 0, sizeof(rw)); rw.server_addr = ipaddr_addr("192.168.0.241"); rw.server_port = 10010; os_strcpy(rw.ssid, "useease2"); os_strcpy(rw.ssid_pwd, "1CBE991A14"); os_strcpy(rw.ssid_mine, "iPLUG"); os_strcpy(rw.ssid_pwd_mine, "iPLUG123"); rw.run_mode = mode; rw.dev_type = DEV_PLUG; show_rw(&rw); //写入前,需要擦除 if (spi_flash_erase_sector(FLASH_HEAD_ADDR / (4 * 1024)) != SPI_FLASH_RESULT_OK) { os_printf("SPI FLASH ERASE ERROR\n"); } else { os_printf("SPI FLASH ERASE SUCCESS\n"); } //写入 if (spi_flash_write(FLASH_HEAD_ADDR, (uint32*) &rw, sizeof(rw)) != SPI_FLASH_RESULT_OK) { os_printf("SPI FLASH WRITE ERROR\n"); } else { os_printf("SPI FLASH WRITE SUCCESS\n"); } }
void ICACHE_FLASH_ATTR network_start() { static struct espconn conn; static esp_tcp tcp; uint32_t target = ipaddr_addr(SERVERIP); pconn = &conn; //uart0_tx_buffer("look",4); conn.type=ESPCONN_TCP; conn.state=ESPCONN_NONE; conn.proto.tcp=&tcp; conn.proto.tcp->local_port=espconn_port(); conn.proto.tcp->remote_port=SERVERPORT; //char page_buffer[20]; //os_sprintf(page_buffer,"IP: %d.%d.%d.%d",IP2STR(&target)); //uart0_tx_buffer(page_buffer,strlen(page_buffer)); os_memcpy(conn.proto.tcp->remote_ip, &target, 4); espconn_regist_connectcb(&conn, networkConnectedCb); espconn_regist_disconcb(&conn, networkDisconCb); espconn_regist_reconcb(&conn, networkReconCb); espconn_regist_recvcb(&conn, networkRecvCb); espconn_regist_sentcb(&conn, networkSentCb); int iRet = espconn_connect(&conn); //os_sprintf(page_buffer,"\nConected =0: %d\n\n",iRet); //uart0_tx_buffer(page_buffer,strlen(page_buffer)); }
void ICACHE_FLASH_ATTR set_softap_mode() { os_printf("run in wifi_boardcast mode\n"); if(!wifi_set_opmode(SOFTAP_MODE)) { os_printf("wifi set opmode to softap error\n"); //可以停机了。。。 } os_printf("wifi set opmode softap ok\n"); struct softap_config apconfig; memset(&apconfig, 0, sizeof(struct softap_config)); os_strcpy(apconfig.ssid, DEFAULT_SSID); os_strcpy(apconfig.password, DEFAULT_SSID_PWD); apconfig.ssid_len = 0; apconfig.authmode = AUTH_WPA_WPA2_PSK; apconfig.ssid_hidden = 0; apconfig.max_connection = 5; apconfig.beacon_interval = 100; if (!wifi_softap_set_config(&apconfig)) { os_printf("[%s] [%s] ERROR\n", __func__, "wifi_softap_set_config"); } os_printf("wifi_softap_set_config success\n"); struct ip_info ipinfo; ipinfo.gw.addr = ipaddr_addr(DEFAULT_GWADDR); ipinfo.ip.addr = ipaddr_addr(DEFAULT_GWADDR); ipinfo.netmask.addr = ipaddr_addr("255.255.255.0"); if(!wifi_set_ip_info(SOFTAP_IF, &ipinfo)) { os_printf("wifi_set_ip_info error\n"); //my god... } struct dhcps_lease please; please.start_ip.addr = ipaddr_addr(DHCP_BEGIN_ADDR); please.end_ip.addr = ipaddr_addr(DHCP_END_ADDR); if(!wifi_softap_set_dhcps_lease(&please)) { os_printf("wifi_softap_set_dhcps_lease error\n"); //unknown... } os_printf("wifi_softap_dhcps config lease ok\n"); }
void rwinfo_init(rw_info* prw) { os_memset(prw, 0, sizeof(rw_info)); prw->server_addr = ipaddr_addr(CLOUD_SERVER); prw->server_port = CLOUD_PORT; prw->dev_type = DEV_PLUG; os_strcpy(prw->ssid_mine, DEFAULT_SSID); os_strcpy(prw->ssid_pwd_mine, DEFAULT_SSID_PWD); }
static uint32_t parse_key(lua_State* L, const char * key){ lua_getfield(L, 1, key); if( lua_isstring(L, -1) ) // deal with the ip/netmask/gw string { const char *ip = luaL_checkstring( L, -1 ); return ipaddr_addr(ip); } lua_pop(L, 1); return 0; }
int set_epon_ip(char *onuip, char *oltip) { struct epon_device_st eponp; if (NULL==onuip || NULL==oltip) return FAIL; eponp.onu_ip = ntohl(ipaddr_addr(onuip)); eponp.olt_ip = ntohl(ipaddr_addr(oltip)); SYSCFG_API_DEBUG(("onuip:0x%x, oltip:0x%x,\n", eponp.onu_ip, eponp.olt_ip)); if ((0!=eponp.onu_ip && ~0!=eponp.onu_ip) && (0!=eponp.olt_ip && ~0!=eponp.olt_ip)) write_syscfgdata_tbl(SYSCFGDATA_TBL_EPON_DEV, 0, &eponp); else return FAIL; return SUCC; }
void ICACHE_FLASH_ATTR WIFI_Connect(WifiCallback cb) { struct station_config stationConf; struct ip_info info; INFO("WIFI_INIT\r\n"); os_timer_disarm(&WiFiLinker); //wifi_set_opmode(STATION_MODE); wifi_station_set_auto_connect(FALSE); wifiCb = cb; os_memset(&stationConf, 0, sizeof(struct station_config)); os_sprintf((char *)stationConf.ssid, "%s", sysCfg.sta_ssid); os_sprintf((char *)stationConf.password, "%s", sysCfg.sta_pass); wifi_get_ip_info(STATION_IF, &info); char *dhcp = (char *)sysCfg.sta_mode; char *ip, *mask, *gw; if (!dhcp || os_strcmp(dhcp, "dhcp") != 0) { ip = (char *)sysCfg.sta_ip; mask = (char *)sysCfg.sta_mask; gw = (char *)sysCfg.sta_gw; if (ip) info.ip.addr = ipaddr_addr(ip); if (mask) info.netmask.addr = ipaddr_addr(mask); if (gw) info.gw.addr = ipaddr_addr(gw); wifi_set_ip_info(STATION_IF, &info); } wifi_get_ip_info(SOFTAP_IF, &info); ip = (char *)sysCfg.ap_ip; mask = (char *)sysCfg.ap_mask; gw = (char *)sysCfg.ap_gw; if (ip) info.ip.addr = ipaddr_addr(ip); if (mask) info.netmask.addr = ipaddr_addr(mask); if (gw) info.gw.addr = ipaddr_addr(gw); if (wifi_get_opmode() != STATION_MODE) wifi_set_ip_info(SOFTAP_IF, &info); wifi_station_set_config(&stationConf); os_timer_disarm(&WiFiLinker); os_timer_setfn(&WiFiLinker, (os_timer_func_t *)wifi_check_ip, NULL); os_timer_arm(&WiFiLinker, 1000, 0); wifi_station_set_auto_connect(TRUE); wifi_station_connect(); }
int set_nms_param(int is_trap, char *ip, int port, int protot) { struct nms_if_info_st nms_if; if (NULL == ip) return FAIL; read_syscfgdata_tbl(SYSCFGDATA_TBL_NMS_IF, 0, &nms_if); if (!is_trap) { nms_if.nms_ip = ntohl(ipaddr_addr(ip)); nms_if.port = port; nms_if.prot_type = protot; } else { nms_if.trap_ip = ntohl(ipaddr_addr(ip)); nms_if.trap_port = port; } SYSCFG_API_DEBUG(("ip:%d.%d.%d.%d, port:%d, protot:0x%x\n", IP_DOTTED_DECIMAL_NOTATION(nms_if.nms_ip), nms_if.port, nms_if.prot_type)); write_syscfgdata_tbl(SYSCFGDATA_TBL_NMS_IF, 0, &nms_if); return SUCC; }
static int do_ping(int argc, const char* const* argv) { struct ping_option *pingopts = os_zalloc(sizeof(struct ping_option)); ip_addr_t ipaddr; ipaddr.addr = ipaddr_addr(argv[1]); pingopts->ip = ipaddr.addr; pingopts->count = 3; pingopts->recv_function=ping_recv_callback; pingopts->sent_function=NULL; ping_start(pingopts); console_lock(1); return 0; }
// Lua: s = net.dns.setdnsserver(ip_addr, [index]) static int net_setdnsserver( lua_State* L ) { size_t l; u32_t ip32; const char *server = luaL_checklstring( L, 1, &l ); if (l>16 || server == NULL || (ip32 = ipaddr_addr(server)) == IPADDR_NONE || ip32 == IPADDR_ANY) return luaL_error( L, "invalid dns server ip" ); int numdns = luaL_optint(L, 2, 0); if (numdns >= DNS_MAX_SERVERS) return luaL_error( L, "server index out of range [0-%d]", DNS_MAX_SERVERS - 1); ip_addr_t ipaddr; ip4_addr_set_u32(&ipaddr, ip32); dns_setserver(numdns,&ipaddr); return 0; }
static void ping_host_thread(void *arg) { int s,i=0; int timeout = PING_RCV_TIMEO; ip_addr_t ping_target; char *host = (char *)arg; //LWIP_UNUSED_ARG(arg); if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) { return; } lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); while (1) { //ping_target = PING_TARGET; if (i>4) break; i++; (ip4_addr_set_u32(&ping_target, ipaddr_addr(host))); if (ping_send(s, &ping_target) == ERR_OK) { //LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); //ip_addr_debug_print(PING_DEBUG, &ping_target); //LWIP_DEBUGF( PING_DEBUG, ("\n")); rt_kprintf("ping: send "); ip_addr_debug_print1(&ping_target); rt_kprintf("\n"); ping_time = sys_now(); ping_recv(s); } else { //LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); //ip_addr_debug_print(PING_DEBUG, &ping_target); //LWIP_DEBUGF( PING_DEBUG, (" - error\n")); rt_kprintf("ping: send "); ip_addr_debug_print1(&ping_target); rt_kprintf(" - error\n"); } sys_msleep(PING_DELAY); } }
static void ping_host(char * host) { int s,i=0; int timeout = PING_RCV_TIMEO; ip_addr_t ping_target; if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) { return; } lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); while (1) { if (i>4) break; i++; //ping_target = netif_default->gw; //IP4_ADDR(&ping_target,210,82,5,1); (ip4_addr_set_u32(&ping_target, ipaddr_addr(host))); if (ping_send(s, &ping_target) == ERR_OK) { //LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); rt_kprintf("ping: send "); ip_addr_debug_print1(&ping_target); rt_kprintf("\n"); ping_time = sys_now(); ping_recv(s); } else { //LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); rt_kprintf("ping: send "); ip_addr_debug_print1(&ping_target); rt_kprintf(" - error\n"); //LWIP_DEBUGF( PING_DEBUG, (" - error\n")); } sys_msleep(PING_DELAY); } }
static void ICACHE_FLASH_ATTR at_espconn_demo_init(void) { uint32 ip = 0; at_espconn_demo_espconn_ptr = (struct espconn *)os_zalloc(sizeof(struct espconn)); at_espconn_demo_espconn_ptr->type = ESPCONN_TCP; at_espconn_demo_espconn_ptr->state = ESPCONN_NONE; at_espconn_demo_espconn_ptr->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); at_espconn_demo_espconn_ptr->proto.tcp->local_port = espconn_port(); at_espconn_demo_espconn_ptr->proto.tcp->remote_port = 8999; ip = ipaddr_addr("192.168.1.120"); os_memcpy(at_espconn_demo_espconn_ptr->proto.tcp->remote_ip,&ip,sizeof(ip)); espconn_regist_connectcb(at_espconn_demo_espconn_ptr, at_espconn_demo_connect_cb); espconn_regist_reconcb(at_espconn_demo_espconn_ptr, at_espconn_demo_recon_cb); espconn_regist_disconcb(at_espconn_demo_espconn_ptr, at_espconn_demo_discon_cb); espconn_regist_recvcb(at_espconn_demo_espconn_ptr, at_espconn_demo_recv); espconn_regist_sentcb(at_espconn_demo_espconn_ptr, at_espconn_demo_send_cb); espconn_connect(at_espconn_demo_espconn_ptr); at_fake_uart_enable(TRUE,at_espconn_demo_response); }
void ICACHE_FLASH_ATTR uplink_sendRequest(char* remoteIP, uint16 remotePort, char* message) { txPayload = message; rxPayload[0] = '\0'; rxPayloadSize = 0; connState = TCP_CONNECTING; // define TCP client connection connection.proto.tcp = &tcp; connection.type = ESPCONN_TCP; connection.state = ESPCONN_NONE; uint32 ip = ipaddr_addr(remoteIP); os_memcpy(connection.proto.tcp->remote_ip, &ip, 4); connection.proto.tcp->local_port = espconn_port(); connection.proto.tcp->remote_port = remotePort; // register callbacks espconn_regist_connectcb(&connection, clientConnectedCallback); espconn_regist_disconcb(&connection, clientDisconnectedCallback); espconn_regist_reconcb(&connection, clientErrorCallback); // connect (non blocking) ets_uart_printf("TCP connecting to " IPSTR ":%d\r\n", IP2STR(connection.proto.tcp->remote_ip), connection.proto.tcp->remote_port); sint8 espcon_status = espconn_connect(&connection); switch (espcon_status) { case ESPCONN_OK: // ets_uart_printf("TCP connnection created.\r\n"); break; case ESPCONN_RTE: ets_uart_printf("ERROR: TCP connect - no route to host.\r\n"); break; case ESPCONN_TIMEOUT: ets_uart_printf("ERROR: TCP connect - timeout\r\n"); break; default: ets_uart_printf("ERROR: TCP connect - error %d\r\n", espcon_status); } }
// Connect to Jarvis LOCAL void ICACHE_FLASH_ATTR connect_to_jarvis(os_event_t *events) { os_delay_us(1000000); // Hit Jarvis os_printf("Trying to hit Jarvis...\n"); tcp0.remote_port = SERVER_PORT; *((uint32*)tcp0.remote_ip) = ipaddr_addr(SERVER_IP); // Remote IP tcp0.local_port = espconn_port(); // Local port wifi_get_ip_info(STATION_IF, &ip0); //tcp0.local_ip = ip0.ip; // Local IP conn0.type = ESPCONN_TCP; conn0.state = ESPCONN_NONE; conn0.proto.tcp = &tcp0; espconn_regist_connectcb(&conn0, on_connect_cb); espconn_regist_disconcb(&conn0, on_discon_cb); espconn_regist_reconcb(&conn0, on_recon_cb); espconn_regist_recvcb(&conn0, on_recv_cb); espconn_regist_time(&conn0, 30, 1); // Set a 30 sec time-out for this conn espconn_connect(&conn0); os_printf("Connection request\n"); }
// Lua: server:listen( port, ip, function(con) ) // Lua: socket:connect( port, ip, function(con) ) static int net_start( lua_State* L, const char* mt ) { NODE_DBG("net_start is called.\n"); struct espconn *pesp_conn = NULL; lnet_userdata *nud; unsigned port; size_t il; bool isserver = false; ip_addr_t ipaddr; const char *domain; uint8_t stack = 1; 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_start.\n"); return 0; } nud = (lnet_userdata *)luaL_checkudata(L, stack, mt); luaL_argcheck(L, nud, stack, "Server/Socket expected"); stack++; if(nud==NULL){ NODE_DBG("userdata is nil.\n"); return 0; } pesp_conn = nud->pesp_conn; port = luaL_checkinteger( L, stack ); stack++; if( pesp_conn->type == ESPCONN_TCP ) { if(isserver) pesp_conn->proto.tcp->local_port = port; else{ pesp_conn->proto.tcp->remote_port = port; pesp_conn->proto.tcp->local_port = espconn_port(); } NODE_DBG("TCP port is set: %d.\n", port); } else if (pesp_conn->type == ESPCONN_UDP) { if(isserver) pesp_conn->proto.udp->local_port = port; else{ pesp_conn->proto.udp->remote_port = port; pesp_conn->proto.udp->local_port = espconn_port(); } NODE_DBG("UDP port is set: %d.\n", port); } if( lua_isstring(L,stack) ) // deal with the domain string { domain = luaL_checklstring( L, stack, &il ); stack++; if (domain == NULL) { if(isserver) domain = "0.0.0.0"; else domain = "127.0.0.1"; } ipaddr.addr = ipaddr_addr(domain); if( pesp_conn->type == ESPCONN_TCP ) { if(isserver) c_memcpy(pesp_conn->proto.tcp->local_ip, &ipaddr.addr, 4); else 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) { if(isserver) c_memcpy(pesp_conn->proto.udp->local_ip, &ipaddr.addr, 4); else 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"); } } // call back function when a connection is obtained, tcp only if ( pesp_conn->type == ESPCONN_TCP ) { if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){ lua_pushvalue(L, stack); // copy argument (func) to the top of stack if(isserver) // for tcp server connected callback { if(tcpserver_cb_connect_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref); tcpserver_cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX); } else { if(nud->cb_connect_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_connect_ref); nud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX); } } } 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); } if( pesp_conn->type == ESPCONN_TCP ) { if(isserver){ // no secure server support for now espconn_regist_connectcb(pesp_conn, net_server_connected); // tcp server, SSL is not supported #ifdef CLIENT_SSL_ENABLE // if(nud->secure) // espconn_secure_accept(pesp_conn); // else #endif espconn_accept(pesp_conn); // if it's a server, no need to dns. espconn_regist_time(pesp_conn, tcp_server_timeover, 0); } else{ espconn_regist_connectcb(pesp_conn, net_socket_connected); espconn_regist_reconcb(pesp_conn, net_socket_reconnected); #ifdef CLIENT_SSL_ENABLE if(nud->secure){ if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port) espconn_secure_disconnect(pesp_conn); // espconn_secure_connect(pesp_conn); } else #endif { if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port) espconn_disconnect(pesp_conn); // espconn_connect(pesp_conn); } } } else if (pesp_conn->type == ESPCONN_UDP) { espconn_regist_recvcb(pesp_conn, net_socket_received); espconn_regist_sentcb(pesp_conn, net_socket_sent); if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port) espconn_delete(pesp_conn); if(isserver) espconn_create(pesp_conn); // if it's a server, no need to dns. } if(!isserver){ if((ipaddr.addr == IPADDR_NONE) && (c_memcmp(domain,"255.255.255.255",16) != 0)) { host_ip.addr = 0; dns_reconn_count = 0; if(ESPCONN_OK == espconn_gethostbyname(pesp_conn, domain, &host_ip, socket_dns_found)){ socket_dns_found(domain, &host_ip, pesp_conn); // ip is returned in host_ip. } } else { socket_connect(pesp_conn); } } return 0; }
void http_get_task(void *pvParameters) { coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */ int failures = 0; // create network socket struct addrinfo hints; struct addrinfo *res; // Use an UDP socket hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; // Get host/port from request URL // should call free(uri) somewhere coap_uri_t *uri = coap_new_uri((unsigned char*)URI, strlen(URI)); if (uri == NULL) { COAP_PRINTF("coap_new_uri(): URI failed\n"); vTaskDelete(NULL); } // DNS lookup int err; char port[6]; char host[32]; char path[64]; sprintf(port, "%d", uri->port); sprintf(path, "%s", uri->path.s); memcpy(host, uri->host.s, uri->host.length); host[uri->host.length] = '\0'; COAP_PRINTF("URI Path: %s\n", path); COAP_PRINTF("URI Host: %s\n", host); COAP_PRINTF("URI Port: %s\n", port); printf("Running DNS lookup for %s...\r\n", host); while (1) { err = getaddrinfo(host, port, &hints, &res); if (err == 0) break; freeaddrinfo(res); printf("DNS lookup failed err=%d res=%p\r\n", err, res); vTaskDelay(3000 / portTICK_RATE_MS); } /* Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */ struct in_addr *addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; char *ip = inet_ntoa(*addr); printf("DNS lookup succeeded. HOST=%s, IP=%s\r\n", host, ip); // init a HTTP POST message and set message header coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0); coap_set_header_uri_path(request, path); coap_set_header_uri_host(request, host); // Create a local socket int s = socket(res->ai_family, res->ai_socktype, 0); if(s < 0) { printf("... Failed to allocate socket.\r\n"); freeaddrinfo(res); vTaskDelete(NULL); } printf("... allocated socket\r\n"); int a; int ret; char payload[128]; struct request_state_t state[1]; ip_addr_t ipaddr; ipaddr.addr = ipaddr_addr(ip); while(1) { if(connect(s, res->ai_addr, res->ai_addrlen) != 0) { close(s); freeaddrinfo(res); printf("... socket connect failed.\r\n"); vTaskDelay(3000 / portTICK_RATE_MS); failures++; continue; } printf("... connected\r\n"); freeaddrinfo(res); repeat: vTaskDelay(5000 / portTICK_RATE_MS); // read sensor data from ESP8266 A0 a = sdk_system_adc_read(); sprintf(payload, "{ \"quality\": %d }\n", a); COAP_PRINTF("Payload: %s\n", payload); // CoAP payload coap_set_payload(request, payload, strlen(payload)); // Make CoAP transaction request->mid = coap_get_mid(); if ((state->transaction = coap_new_transaction(request->mid, &ipaddr, uri->port))) { state->transaction->callback = coap_blocking_request_callback; state->transaction->callback_data = state; if (state->block_num > 0) { coap_set_header_block2(request, state->block_num, 0, REST_MAX_CHUNK_SIZE); } // Build CoAP header and Options state->transaction->packet_len = coap_serialize_message(request, state->transaction->packet); COAP_PRINTF("Header dump: [0x%02X %02X %02X %02X]. Size: %d\n", request->buffer[0], request->buffer[1], request->buffer[2], request->buffer[3], state->transaction->packet_len ); COAP_PRINTF("Requested #%u (MID %u)\n", state->block_num, request->mid); // Transmit ret = write(s, state->transaction->packet, state->transaction->packet_len); //ret = sendto(s, state->transaction->packet, state->transaction->packet_len); if (ret < 0) { printf("[RET: %d] CoAP packet send failed.\n", ret); continue; } } else { COAP_PRINTF("Could not allocate transaction buffer"); } goto repeat; } }
int sj_wifi_setup_ap(const struct sys_config_wifi_ap *cfg) { struct softap_config ap_cfg; struct ip_info info; struct dhcps_lease dhcps; size_t pass_len = strlen(cfg->pass); size_t ssid_len = strlen(cfg->ssid); if (ssid_len > sizeof(ap_cfg.ssid) || pass_len > sizeof(ap_cfg.password)) { LOG(LL_ERROR, ("AP SSID or PASS too long")); return 0; } if (pass_len != 0 && pass_len < 8) { /* * If we don't check pwd len here and it will be less than 8 chars * esp will setup _open_ wifi with name ESP_<mac address here> */ LOG(LL_ERROR, ("AP password too short")); return 0; } /* If in STA-only mode, switch to AP. If in AP or AP+STA, keep it. */ if (wifi_get_opmode() == STATION_MODE) { wifi_set_opmode_current(SOFTAP_MODE); } memset(&ap_cfg, 0, sizeof(ap_cfg)); strncpy((char *) ap_cfg.ssid, cfg->ssid, sizeof(ap_cfg.ssid)); strncpy((char *) ap_cfg.password, cfg->pass, sizeof(ap_cfg.password)); ap_cfg.ssid_len = ssid_len; if (pass_len != 0) { ap_cfg.authmode = AUTH_WPA2_PSK; } ap_cfg.channel = cfg->channel; ap_cfg.ssid_hidden = (cfg->hidden != 0); ap_cfg.max_connection = cfg->max_connections; ap_cfg.beacon_interval = 100; /* ms */ LOG(LL_DEBUG, ("Setting up %s on channel %d", ap_cfg.ssid, ap_cfg.channel)); wifi_softap_set_config_current(&ap_cfg); LOG(LL_DEBUG, ("Restarting DHCP server")); wifi_softap_dhcps_stop(); /* * We have to set ESP's IP address explicitly also, GW IP has to be the * same. Using ap_dhcp_start as IP address for ESP */ info.netmask.addr = ipaddr_addr(cfg->netmask); info.ip.addr = ipaddr_addr(cfg->ip); info.gw.addr = ipaddr_addr(cfg->gw); wifi_set_ip_info(SOFTAP_IF, &info); dhcps.enable = 1; dhcps.start_ip.addr = ipaddr_addr(cfg->dhcp_start); dhcps.end_ip.addr = ipaddr_addr(cfg->dhcp_end); wifi_softap_set_dhcps_lease(&dhcps); /* Do not offer self as a router, we're not one. */ { int off = 0; wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &off); } wifi_softap_dhcps_start(); wifi_get_ip_info(SOFTAP_IF, &info); LOG(LL_INFO, ("WiFi AP: SSID %s, channel %d, IP " IPSTR "", ap_cfg.ssid, ap_cfg.channel, IP2STR(&info.ip))); return 1; }
/** The actual mail-sending function, called by smtp_send_mail and * smtp_send_mail_static after setting up the struct smtp_session. */ static err_t smtp_send_mail_alloced(struct smtp_session *s) { err_t err; struct tcp_pcb* pcb; ip_addr_t addr; LWIP_ASSERT("no smtp_session supplied", s != NULL); #if SMTP_CHECK_DATA /* check that body conforms to RFC: * - convert all single-CR or -LF in body to CRLF * - only 7-bit ASCII is allowed */ if (smtp_verify(s->to, s->to_len, 0) != ERR_OK) { return ERR_ARG; } if (smtp_verify(s->from, s->from_len, 0) != ERR_OK) { return ERR_ARG; } if (smtp_verify(s->subject, s->subject_len, 0) != ERR_OK) { return ERR_ARG; } if (smtp_verify(s->body, s->body_len, 0) != ERR_OK) { return ERR_ARG; } #endif /* SMTP_CHECK_DATA */ pcb = tcp_new(); if (pcb == NULL) { err = ERR_MEM; goto leave; } #if SMTP_COPY_AUTHDATA /* copy auth data, ensuring the first byte is always zero */ memcpy(s->auth_plain + 1, smtp_auth_plain + 1, smtp_auth_plain_len - 1); s->auth_plain_len = smtp_auth_plain_len; /* default username and pass is empty string */ s->username = s->auth_plain; s->pass = s->auth_plain; if (smtp_username != NULL) { s->username += smtp_username - smtp_auth_plain; } if (smtp_pass != NULL) { s->pass += smtp_pass - smtp_auth_plain; } #endif /* SMTP_COPY_AUTHDATA */ s->state = SMTP_NULL; s->timer = SMTP_TIMEOUT; tcp_arg(pcb, s); tcp_recv(pcb, smtp_tcp_recv); tcp_err(pcb, smtp_tcp_err); tcp_poll(pcb, smtp_tcp_poll, SMTP_POLL_INTERVAL); tcp_sent(pcb, smtp_tcp_sent); #if LWIP_DNS err = dns_gethostbyname(smtp_server, &addr, smtp_dns_found, pcb); #else /* LWIP_DNS */ addr.addr = ipaddr_addr(smtp_server); err = addr.addr == IPADDR_NONE ? ERR_ARG : ERR_OK; #endif /* LWIP_DNS */ if (err == ERR_OK) { err = tcp_connect(pcb, &addr, smtp_server_port, smtp_tcp_connected); if (err != ERR_OK) { LWIP_DEBUGF(SMTP_DEBUG_WARN_STATE, ("tcp_connect failed: %d\n", (int)err)); goto deallocate_and_leave; } } else if (err != ERR_INPROGRESS) { LWIP_DEBUGF(SMTP_DEBUG_WARN_STATE, ("dns_gethostbyname failed: %d\n", (int)err)); goto deallocate_and_leave; } return ERR_OK; deallocate_and_leave: if (pcb != NULL) { tcp_arg(pcb, NULL); tcp_close(pcb); } leave: mem_free(s); /* no need to call the callback here since we return != ERR_OK */ return err; }
/** * @brief Setup commad of start client. * @param id: commad id number * @param pPara: AT input param * @retval None */ void ICACHE_FLASH_ATTR at_setupCmdCipstart(uint8_t id, char *pPara) { char temp[64]; // enum espconn_type linkType; int8_t len; enum espconn_type linkType = ESPCONN_INVALID; uint32_t ip = 0; char ipTemp[128]; int32_t port; uint8_t linkID; // if(mdState != m_unlink) // { // uart0_sendStr("no ip\r\n"); // return; // } if(at_wifiMode == 1) { if(wifi_station_get_connect_status() != STATION_GOT_IP) { uart0_sendStr("no ip\r\n"); return; } } pPara++; if(at_ipMux) { linkID = atoi(pPara); pPara++; pPara = strchr(pPara, '\"'); } else { linkID = 0; } if(linkID >= at_linkMax) { uart0_sendStr("ID ERROR\r\n"); return; } len = at_dataStrCpy(temp, pPara, 6); if(len == -1) { uart0_sendStr("Link typ ERROR\r\n"); return; } if(os_strcmp(temp, "TCP") == 0) { linkType = ESPCONN_TCP; } else if(os_strcmp(temp, "UDP") == 0) { linkType = ESPCONN_UDP; } else { uart0_sendStr("Link typ ERROR\r\n"); return; } pPara += (len+3); len = at_dataStrCpy(ipTemp, pPara, 64); if(len == -1) { uart0_sendStr("IP ERROR\r\n"); return; } pPara += (len+2); if(*pPara != ',') { uart0_sendStr("ENTRY ERROR\r\n"); return; } pPara += (1); port = atoi(pPara); if(pLink[linkID].linkEn) { uart0_sendStr("ALREAY CONNECT\r\n"); return; } pLink[linkID].pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); if (pLink[linkID].pCon == NULL) { uart0_sendStr("CONNECT FAIL\r\n"); return; } pLink[linkID].pCon->type = linkType; pLink[linkID].pCon->state = ESPCONN_NONE; pLink[linkID].linkId = linkID; ip = ipaddr_addr(ipTemp); switch(linkType) { case ESPCONN_TCP: pLink[linkID].pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); pLink[linkID].pCon->proto.tcp->local_port = espconn_port(); pLink[linkID].pCon->proto.tcp->remote_port = port; os_memcpy(pLink[linkID].pCon->proto.tcp->remote_ip, &ip, 4); pLink[linkID].pCon->reverse = &pLink[linkID]; espconn_regist_connectcb(pLink[linkID].pCon, at_tcpclient_connect_cb); espconn_regist_reconcb(pLink[linkID].pCon, at_tcpclient_recon_cb); specialAtState = FALSE; if((ip == 0xffffffff) && (os_memcmp(ipTemp,"255.255.255.255",16) != 0)) { espconn_gethostbyname(pLink[linkID].pCon, ipTemp, &host_ip, at_dns_found); } else { espconn_connect(pLink[linkID].pCon); at_linkNum++; } break; case ESPCONN_UDP: pLink[linkID].pCon->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); pLink[linkID].pCon->proto.udp->local_port = espconn_port(); pLink[linkID].pCon->proto.udp->remote_port = port; os_memcpy(pLink[linkID].pCon->proto.udp->remote_ip, &ip, 4); pLink[linkID].pCon->reverse = &pLink[linkID]; // os_printf("%d\r\n",pLink[linkID].pCon->proto.udp->local_port);/// pLink[linkID].linkId = linkID; pLink[linkID].linkEn = TRUE; pLink[linkID].teType = teClient; espconn_regist_recvcb(pLink[linkID].pCon, at_tcpclient_recv); espconn_regist_sentcb(pLink[linkID].pCon, at_tcpclient_sent_cb); if((ip == 0xffffffff) && (os_memcmp(ipTemp,"255.255.255.255",16) != 0)) { specialAtState = FALSE; espconn_gethostbyname(pLink[linkID].pCon, ipTemp, &host_ip, at_dns_found); } else { espconn_create(pLink[linkID].pCon); at_linkNum++; at_backOk; } break; default: break; } // os_sprintf(temp, "%d.%d.%d.%d:%d\r\n",///// // IP2STR(&ip), port);///// // uart0_sendStr(temp);//// // at_backOk;///// }
//---------------------------- //---------------------------- // constructor //---------------------------- //---------------------------- ESP8266Client::ESP8266Client(const char* addr, int port, espconn_type type) : ESP8266SocketBase(type) { setPort(port); setAddress(ipaddr_addr(addr)); }