// OnConnect call back
LOCAL void ICACHE_FLASH_ATTR
on_connect_cb(void* arg)
{
    os_printf("TCP Connection Established!\n");
    espconn_set_opt(&conn0, ESPCONN_REUSEADDR); // Docs say to put this here
    char msg[] = "Hello Internet!";
    espconn_send(arg, msg, sizeof(msg));
}
Example #2
0
static void ICACHE_FLASH_ATTR
at_espconn_demo_connect_cb(void *arg)
{
	os_printf("at demo espconn connected\r\n");
	espconn_set_opt((struct espconn*)arg,ESPCONN_COPY);
	at_espconn_demo_flag = TRUE;
	at_espconn_demo_data_len = 0;
}
Example #3
0
static void
esp8266_cb_connect(void *arg)
{
	struct espconn *cs = arg;
//	struct ip_addr *ipa = (struct ip_addr *)cs->proto.tcp->remote_ip;
	struct lws_vhost *vh = hacky_context->vhost_list;
//	struct ip_info info;
	struct lws *wsi;
	int n;

	lwsl_notice("%s: (wsi coming): %p\n", __func__, cs->reverse);
#if 0
	wifi_get_ip_info(0, &info);
	if (ip_addr_netcmp(ipa, &info.ip, &info.netmask)) {
		/* we are on the same subnet as the AP, ie, connected to AP */
		while (vh && strcmp(vh->name, "ap"))
			vh = vh->vhost_next;
	} else
		while (vh && !strcmp(vh->name, "ap"))
			vh = vh->vhost_next;

	if (!vh)
		goto bail;
#endif
	n = esp8266_find_free_conn(hacky_context);
	if (n < 0)
		goto bail;

	hacky_context->connpool[n] = cs;

	espconn_recv_hold(cs);

	wsi = lws_adopt_socket_vhost(vh, cs);
	if (!wsi)
		goto bail;

	lwsl_err("%s: wsi %p (using free_conn %d): vh %s\n", __func__, wsi, n, vh->name);

	espconn_regist_recvcb(cs, esp8266_cb_rx);
	espconn_regist_reconcb(cs, esp8266_cb_recon);
	espconn_regist_disconcb(cs, esp8266_cb_disconnected);
	espconn_regist_sentcb(cs, esp8266_cb_sent);

	espconn_set_opt(cs, ESPCONN_NODELAY | ESPCONN_REUSEADDR);
	espconn_regist_time(cs, 7200, 1);

	return;

bail:
	lwsl_err("%s: bailed]n", __func__);
	espconn_disconnect(cs);
}
Example #4
0
/**
 * 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);
    }
}
void ICACHE_FLASH_ATTR tfp_connect_callback(void *arg) {
	uint8_t i = 0;

	if(!configuration_current.mesh_enable) {
		for(; i < TFP_MAX_CONNECTIONS; i++) {
			if(tfp_cons[i].state == TFP_CON_STATE_CLOSED) {
				tfp_cons[i].con = arg;
				tfp_cons[i].con->reverse = &tfp_cons[i];
				tfp_cons[i].state = TFP_CON_STATE_OPEN;
				logd("tfp_connect_callback: cid %d\n", tfp_cons[i].cid);
				break;
			}
		}

		if(i == TFP_MAX_CONNECTIONS) {
			logw("Too many open connections, can't handle more\n");
			// TODO: according to the documentation we can not call espconn_disconnect in callback
			// espconn_disconnect(arg);
			return;
		}

		/*
			uint8_t param = 10;
			espconn_set_keepalive(arg, ESPCONN_KEEPIDLE, &param);
			param = 2;
			espconn_set_keepalive(arg, ESPCONN_KEEPINTVL, &param);
			param = 10;
			espconn_set_keepalive(arg, ESPCONN_KEEPCNT, &param);

			espconn_set_opt(arg, ESPCONN_KEEPALIVE);
			espconn_set_opt(arg, ESPCONN_REUSEADDR);
			espconn_set_opt(arg, ESPCONN_COPY);
			espconn_set_opt(arg, ESPCONN_NODELAY);
		*/

		espconn_set_opt(arg, ESPCONN_NODELAY);
		espconn_regist_recvcb(arg, tfp_recv_callback);
		espconn_regist_disconcb(arg, tfp_disconnect_callback);
		espconn_regist_sentcb(arg, tfp_sent_callback);
	}
	else {
		tfp_cons[0].con = arg;
		tfp_cons[0].con->reverse = &tfp_cons[0];
		tfp_cons[0].state = TFP_CON_STATE_OPEN;
	}
}
Example #6
0
//***********************************************************************
void ICACHE_FLASH_ATTR webSocketInit( void ) {
    webSocketConn.type = ESPCONN_TCP;
    webSocketConn.state = ESPCONN_NONE;
    webSocketConn.proto.tcp = &webSocketTcp;
    webSocketConn.proto.tcp->local_port = WEB_SOCKET_PORT;
    espconn_regist_connectcb(&webSocketConn, webSocketConnectCb);
    
    espconn_set_opt( &webSocketConn, ESPCONN_NODELAY );  // remove nagle for low latency
    
    sint8 ret = espconn_accept(&webSocketConn);
    if ( ret == 0 )
        webSocketDebug("webSocket server established on port %d\n", WEB_SOCKET_PORT );
    else
        webSocketDebug("webSocket server on port %d FAILED ret=%d\n", WEB_SOCKET_PORT, ret);
    
    return;
}
// called when a client connects to our server
static void ICACHE_FLASH_ATTR http_ws_connect_callback(void *arg) {

	struct espconn *conn=arg;

	http_connection *c = http_new_connection(1,conn); // get a connection from the pool, signal it's an incomming connection
	c->cgi.execute = http_ws_cgi_execute; 		  // attach our cgi dispatcher	

	//attach app callback to cgi function
	c->cgi.function=app_callback;

	c->handshake_ok=0;

	//let's disable NAGLE alg so TCP outputs faster ( in theory )
	espconn_set_opt(conn, ESPCONN_NODELAY | ESPCONN_REUSEADDR );

	//override timeout to 240s
	espconn_regist_time(conn,240,0);

}
Example #8
0
static void ICACHE_FLASH_ATTR httpdConnectCb(void *arg) {
  debugConn(arg, "httpdConnectCb");
  struct espconn *conn = arg;

  // Find empty conndata in pool
  int i;
  for (i = 0; i<MAX_CONN; i++) if (connData[i].conn == NULL) break;
  //DBG("Con req, conn=%p, pool slot %d\n", conn, i);
  if (i == MAX_CONN) {
    os_printf("%sHTTP: conn pool overflow!\n", connStr);
    espconn_disconnect(conn);
    return;
  }

#if 0
  int num = 0;
  for (int j = 0; j<MAX_CONN; j++) if (connData[j].conn != NULL) num++;
  DBG("%sConnect (%d open)\n", connStr, num + 1);
#endif

  connData[i].priv = &connPrivData[i];
  connData[i].conn = conn;
  conn->reverse = connData+i;
  connData[i].priv->headPos = 0;

  esp_tcp *tcp = conn->proto.tcp;
  os_sprintf(connData[i].priv->from, "%d.%d.%d.%d:%d", tcp->remote_ip[0], tcp->remote_ip[1],
      tcp->remote_ip[2], tcp->remote_ip[3], tcp->remote_port);
  connData[i].post = &connPostData[i];
  connData[i].post->buff = NULL;
  connData[i].post->buffLen = 0;
  connData[i].post->received = 0;
  connData[i].post->len = -1;
  connData[i].startTime = system_get_time();

  espconn_regist_recvcb(conn, httpdRecvCb);
  espconn_regist_reconcb(conn, httpdReconCb);
  espconn_regist_disconcb(conn, httpdDisconCb);
  espconn_regist_sentcb(conn, httpdSentCb);

  espconn_set_opt(conn, ESPCONN_REUSEADDR | ESPCONN_NODELAY);
}
Example #9
0
/**
  @brief incomming connection setup callbacks
  @param[in] *new_connection:
  @return void
*/
MEMSPACE
static void tcp_data_connect_callback(struct espconn *new_connection)
{
	if(esp_data_tcp_connection)
		espconn_disconnect(new_connection);
	else
	{
		esp_data_tcp_connection	= new_connection;
		tcp_data_send_buffer_busy = 0;

		espconn_regist_recvcb(esp_data_tcp_connection, tcp_data_receive_callback);
		espconn_regist_sentcb(esp_data_tcp_connection, tcp_data_sent_callback);
		espconn_regist_disconcb(esp_data_tcp_connection, tcp_data_disconnect_callback);

		espconn_set_opt(esp_data_tcp_connection, ESPCONN_REUSEADDR);

		queue_flush(uart_send_queue);
		queue_flush(uart_receive_queue);
	}
}
Example #10
0
static void ICACHE_FLASH_ATTR httpdConnectCb(void *arg) {
	debugConn(arg, "httpdConnectCb");
	struct espconn *conn=arg;
	int i;
	//Find empty conndata in pool
	for (i=0; i<MAX_CONN; i++) if (connData[i].conn==NULL) break;
	//os_printf("Con req, conn=%p, pool slot %d\n", conn, i);
	if (i==MAX_CONN) {
		os_printf("%s Aiee, conn pool overflow!\n", connStr);
		espconn_disconnect(conn);
		return;
	}

	int num = 0;
	for (int j=0; j<MAX_CONN; j++) if (connData[j].conn != NULL) num++;
	os_printf("%s Connect (%d open)\n", connStr, num+1);

	connData[i].priv=&connPrivData[i];
	connData[i].conn=conn;
	connData[i].remote_port = conn->proto.tcp->remote_port;
	os_memcpy(connData[i].remote_ip, conn->proto.tcp->remote_ip, 4);
	connData[i].priv->headPos=0;
	connData[i].post=&connPostData[i];
	connData[i].post->buff=NULL;
	connData[i].post->buffLen=0;
	connData[i].post->received=0;
	connData[i].post->len=-1;
	connData[i].startTime = system_get_time();

	espconn_regist_recvcb(conn, httpdRecvCb);
	espconn_regist_reconcb(conn, httpdReconCb);
	espconn_regist_disconcb(conn, httpdDisconCb);
	espconn_regist_sentcb(conn, httpdSentCb);

	espconn_set_opt(conn, ESPCONN_REUSEADDR|ESPCONN_NODELAY);
}
Example #11
0
/******************************************************************************
 * FunctionName : espconn_connect
 * Description  : The function given as the connect
 * Parameters   : espconn -- the espconn used to listen the connection
 * Returns      : none
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_connect(struct espconn *espconn)
{
	ip_addr_t ipaddr;
	tcpip_adapter_ip_info_t ipinfo;
	uint8 connect_status = 0;
	sint8 value = ESPCONN_OK;
	espconn_msg *plist = NULL;
	// remot_info *pinfo = NULL;

    if (espconn == NULL) {
        return ESPCONN_ARG;
    } else if (espconn ->type != ESPCONN_TCP)
    	return ESPCONN_ARG;

    /*Check the active node count whether is the limit or not*/
    if (espconn_get_acticve_num(ESPCONN_TCP) >= espconn_tcp_get_max_con())
    {
    	printf("espconn_tcp_get_max_con \n");
    	return ESPCONN_ISCONN;
    }

    /*Check the IP address whether is zero or not in different mode*/
    esp_wifi_get_mode((wifi_mode_t*)&value);
    if (value == WIFI_MODE_STA){
    	wifi_get_ip_info(TCPIP_ADAPTER_IF_STA,&ipinfo);
    	if (ipinfo.ip.addr == 0){
   	 		return ESPCONN_RTE;
   	 	}
    } else if(value == WIFI_MODE_AP){
    	wifi_get_ip_info(TCPIP_ADAPTER_IF_AP,&ipinfo);
    	if (ipinfo.ip.addr == 0){
    		return ESPCONN_RTE;
    	}
    } else if(value == WIFI_MODE_APSTA){
    	IP4_ADDR(&ipaddr.u_addr.ip4, espconn->proto.tcp->remote_ip[0],
    	    	    		espconn->proto.tcp->remote_ip[1],
    	    	    		espconn->proto.tcp->remote_ip[2],
    	    	    		espconn->proto.tcp->remote_ip[3]);
    	ipaddr.u_addr.ip4.addr <<= 8;
    	wifi_get_ip_info(TCPIP_ADAPTER_IF_AP,&ipinfo);
    	ipinfo.ip.addr <<= 8;
    	// espconn_printf("softap_addr = %x, remote_addr = %x\n", ipinfo.ip.addr, ipaddr.u_addr.ip4.addr);

    	if (ipaddr.u_addr.ip4.addr != ipinfo.ip.addr){

    		connect_status = wifi_station_get_connect_status();
    		
			if (connect_status == SYSTEM_EVENT_STA_GOT_IP){
				wifi_get_ip_info(TCPIP_ADAPTER_IF_STA,&ipinfo);
    // espconn_printf("wifi_get_ip_info:(ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR ")\n",
    //         IP2STR(&ipinfo.ip), IP2STR(&ipinfo.netmask), IP2STR(&ipinfo.gw));



				if (ipinfo.ip.addr == 0)
					return ESPCONN_RTE;
			} else if (connect_status != SYSTEM_EVENT_STA_GOT_IP){
				return ESPCONN_RTE;
			} else {
				return connect_status;
			}
    	}
    }
    /*check the active node information whether is the same as the entity or not*/
    for (plist = plink_active; plist != NULL; plist = plist->pnext){
    	if (plist->pespconn && plist->pespconn->type == ESPCONN_TCP){
    		if (espconn->proto.tcp->local_port == plist->pespconn->proto.tcp->local_port){
    			return ESPCONN_ISCONN;
    		}
    	}
    }

	espconn_set_opt(espconn, ESPCONN_COPY|ESPCONN_NODELAY);
    value = espconn_tcp_client(espconn);
    espconn_printf("espconn_connect err value %d \n",value);

    return value;
}