コード例 #1
0
ファイル: at_ipCmd.c プロジェクト: SmbatYeranyan/lemonbox
void ICACHE_FLASH_ATTR
at_setupCmdCipsto(uint8_t id, char *pPara)
{
  char temp[64];
  uint16_t timeOver;

  if(serverEn == FALSE)
  {
    at_backError;
    return;
  }
  pPara++;
  timeOver = atoi(pPara);
  if(timeOver>28800)
  {
    at_backError;
    return;
  }
  if(timeOver != server_timeover)
  {
    server_timeover = timeOver;
    espconn_regist_time(pTcpServer, server_timeover, 0);
  }
  at_backOk;
  return;
}
コード例 #2
0
ファイル: user_main.c プロジェクト: cnlohr/dumbcraft8266
void user_init(void)
{
	//gpio_init();
	uart_init(BIT_RATE_115200, BIT_RATE_115200);
	int at_wifiMode = wifi_get_opmode();

	uart0_sendStr("\r\nCustom Server\r\n");

	wifi_set_opmode( 2 ); //We broadcast our ESSID, wait for peopel to join.

	pTcpServer = (struct espconn *)os_zalloc(sizeof(struct espconn));
	pTcpServer->type = ESPCONN_TCP;
	pTcpServer->state = ESPCONN_NONE;
	pTcpServer->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
	pTcpServer->proto.tcp->local_port = PORT;
	espconn_regist_connectcb(pTcpServer, at_tcpserver_listen);
	espconn_accept(pTcpServer);
	espconn_regist_time(pTcpServer, SERVER_TIMEOUT, 0);

	printf("Hello, world.  Starting server.\n" );
	InitDumbcraft();
	
	os_sprintf( my_server_name, "ESP8266Dumb" );

	system_os_task(at_procTask, at_procTaskPrio, at_procTaskQueue, at_procTaskQueueLen);

	uart0_sendStr("\r\nCustom Server\r\n");
	system_os_post(at_procTaskPrio, 0, 0 );
}
コード例 #3
0
ファイル: bridge.c プロジェクト: lvjh/esp8266_ili9341
/**
  @brief Serial Bridge task initialize
  @param[in] port: network port
*/
MEMSPACE
bridge_task_init(int port)
{
	static struct espconn esp_data_config;
	static esp_tcp esp_data_tcp_config;

	if(!(uart_send_queue = queue_new(BUFFER_SIZE)))
		reset();

	if(!(uart_receive_queue = queue_new(BUFFER_SIZE)))
		reset();

	if(!(tcp_data_send_buffer = malloc(BUFFER_SIZE)))
		reset();

	wifi_set_sleep_type(NONE_SLEEP_T);

	tcp_accept(&esp_data_config, &esp_data_tcp_config, port, tcp_data_connect_callback);
	espconn_regist_time(&esp_data_config, 0, 0);
	esp_data_tcp_connection = 0;

	system_os_task(bridge_task, bridge_task_id, bridge_task_queue, bridge_task_queue_length);
	system_os_post(bridge_task_id, 0, 0);

	printf("\nbridge task init done\n");
}
コード例 #4
0
void
setup_server ( void )
{
    register struct espconn *c = &server_conn;
    char *x;

    c->type = ESPCONN_TCP;
    c->state = ESPCONN_NONE;
    my_tcp_conn.local_port=88;
    c->proto.tcp=&my_tcp_conn;

    espconn_regist_reconcb ( c, tcp_reconnect_cb);
    espconn_regist_connectcb ( c, tcp_connect_cb);
    espconn_regist_disconcb ( c, tcp_disconnect_cb);

    if ( espconn_accept(c) != ESPCONN_OK ) {
	os_printf("Error starting server %d\n", 0);
	return;
    }

    /* Interval in seconds to timeout inactive connections */
    espconn_regist_time(c, 20, 0);

    // x = (char *) os_zalloc ( 4 );
    // os_printf ( "Got mem: %08x\n", x );

    os_printf ( "Server ready\n" );
}
コード例 #5
0
ファイル: server.c プロジェクト: alvieboy/iotpanel
LOCAL ICACHE_FLASH_ATTR void server_conn(void *arg)
{
    struct espconn *pesp_conn = arg;
    DEBUG("Server conn\n");
    clientInfo_init(&clientInfo);

    espconn_regist_time(pesp_conn, 0, 1);

}
コード例 #6
0
ファイル: user_main.c プロジェクト: withmaia/maia-attinytemp
void ICACHE_FLASH_ATTR setup_server() {
    server.type = ESPCONN_TCP;
    server.state = ESPCONN_NONE;
    server.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
    server.proto.tcp->local_port = 80;
    espconn_regist_connectcb(&server, tcpserver_connectcb);
    espconn_accept(&server);
    espconn_regist_time(&server, server_timeover, 0);
}
コード例 #7
0
ファイル: telnet_server.c プロジェクト: nvl1109/esp8266-dev
void ICACHE_FLASH_ATTR serverConnect(void) {
    sint8 res;
    res = espconn_regist_connectcb(serverConn, serverConnectCb);
    DBG_MSG("espconn_regist_connectcb, res: %d\r\n", res);
    res = espconn_accept(serverConn);
    DBG_MSG("espconn_accept, res: %d\r\n", res);
    res = espconn_regist_time(serverConn, SERVER_TIMEOUT, 0);
    DBG_MSG("espconn_regist_time, res: %d\r\n", res);
}
コード例 #8
0
ファイル: httpd-nonos.c プロジェクト: MightyPork/libesphttpd
//Initialize listening socket, do general initialization
void ICACHE_FLASH_ATTR httpdPlatInit(int port, int maxConnCt) {
	httpdConn.type=ESPCONN_TCP;
	httpdConn.state=ESPCONN_NONE;
	httpdTcp.local_port=port;
	httpdConn.proto.tcp=&httpdTcp;
	espconn_regist_connectcb(&httpdConn, platConnCb);
	espconn_accept(&httpdConn);
	espconn_regist_time(&httpdConn, HTTPD_CONN_TIMEOUT, 0); // Configure timeout
	espconn_tcp_set_max_con_allow(&httpdConn, (uint8) maxConnCt);
}
コード例 #9
0
ファイル: Tcp.cpp プロジェクト: vortex314/ea_stm32
void Tcp::registerCb(struct espconn* pconn) {

	pconn->reverse = this;
	espconn_regist_time(pconn, 10000, 1); // disconnect after 10000 sec
	espconn_regist_connectcb(pconn, connectCb);
	espconn_regist_disconcb(pconn, disconnectCb);
	espconn_regist_recvcb(pconn, recvCb);
	espconn_regist_sentcb(pconn, sendCb);
	espconn_regist_write_finish(pconn, writeFinishCb);

}
コード例 #10
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);
}
コード例 #11
0
void ICACHE_FLASH_ATTR websocketdInit(int port, WSOnConnection onConnection) {

	wsOnConnectionCallback = onConnection;

	wsConn.type = ESPCONN_TCP;
	wsConn.state = ESPCONN_NONE;

	wsTcp.local_port = port;
	wsConn.proto.tcp = &wsTcp;

	espconn_regist_connectcb(&wsConn, wsConnectCb);
	espconn_accept(&wsConn);
	espconn_regist_time(&wsConn, CONN_TIMEOUT, 0);
}
コード例 #12
0
ファイル: server.c プロジェクト: alvieboy/iotpanel
void ICACHE_FLASH_ATTR user_server_init(uint32 port)
{
     esp_conn.type = ESPCONN_TCP;
     esp_conn.state = ESPCONN_NONE;
     esp_conn.proto.tcp = &esptcp;
     esp_conn.proto.tcp->local_port = port;
     espconn_regist_connectcb(&esp_conn, server_listen);
     espconn_regist_time(&esp_conn, 65535, 0);



#ifdef SERVER_SSL_ENABLE
     espconn_secure_accept(&esp_conn);
#else
     espconn_accept(&esp_conn);
#endif
}
コード例 #13
0
ファイル: modesp.c プロジェクト: Ga-vin/micropython
// method socket.listen(backlog)
STATIC mp_obj_t esp_socket_listen(mp_obj_t self_in, mp_obj_t backlog) {
    esp_socket_obj_t *s = self_in;

    if (esp_socket_listening != NULL) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
            "only one espconn can listen at a time"));
    }

    esp_socket_listening = s;

    s->connlist = mp_obj_new_list(0, NULL);

    espconn_regist_connectcb(s->espconn, esp_socket_connect_callback_server);
    espconn_accept(s->espconn);
    espconn_regist_time(s->espconn, 1500, 0);

    return mp_const_none;
}
コード例 #14
0
ファイル: user_main.c プロジェクト: neuroradiology/ureq
void ICACHE_FLASH_ATTR ssServerInit() {

    struct espconn * pSimpleServer;

    pSimpleServer = (struct espconn *)os_zalloc(sizeof(struct espconn));
    ets_memset( pSimpleServer, 0, sizeof( struct espconn ) );

    espconn_create( pSimpleServer );
    pSimpleServer->type = ESPCONN_TCP;
    pSimpleServer->state = ESPCONN_NONE;
    pSimpleServer->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
    pSimpleServer->proto.tcp->local_port = 80;

    //espconn_set_opt(pSimpleServer, ESPCONN_REUSEADDR);

    espconn_regist_connectcb(pSimpleServer, ssConnCb);
    espconn_accept(pSimpleServer);
    espconn_regist_time(pSimpleServer, 0, 0);
}
コード例 #15
0
ファイル: modesp.c プロジェクト: Ga-vin/micropython
STATIC void esp_socket_connect_callback_server(void *arg) {
    struct espconn *conn = arg;

    esp_socket_obj_t *s = esp_socket_make_new_base();
    s->espconn = conn;
    s->fromserver = true;
    conn->reverse = s;

    espconn_regist_recvcb(conn, esp_socket_recv_callback);
    espconn_regist_sentcb(conn, esp_socket_sent_callback);
    espconn_regist_disconcb(conn, esp_socket_disconnect_callback);
    espconn_regist_time(conn, 15, 0);

    if (esp_socket_listening->cb_connect != mp_const_none) {
        call_function_1_protected(esp_socket_listening->cb_connect, s);
    } else {
        mp_obj_list_append(esp_socket_listening->connlist, s);
    }
}
コード例 #16
0
// 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);

}
コード例 #17
0
//***********************************************************************
void ICACHE_FLASH_ATTR webSocketConnectCb(void *arg) {
  struct espconn *connection = (espconn *)arg;

  webSocketDebug("\n\nmeshWebSocket received connection !!!\n");

    // set time out for this connection in seconds
    espconn_regist_time( connection, 120, 1);
    
  //find an empty slot
  uint8_t slotId = 0;
  while (wsConnections[slotId].connection != NULL && wsConnections[slotId].status != STATUS_CLOSED && slotId < WS_MAXCONN) {
    slotId++;
  }

  webSocketDebug("websocketConnectCb slotId=%d\n", slotId);


  if (slotId >= WS_MAXCONN) {
    //no more free slots, close the connection
    webSocketDebug("No more free slots for WebSockets!\n");
    espconn_disconnect(connection);
    return;
  }

  //  webSocketDebug("websocketConnectCb2\n");

  WSConnection wsConnection;
  wsConnection.status = STATUS_UNINITIALISED;
  wsConnection.connection = connection;
  wsConnection.onMessage = wsOnMessageCallback;
  wsConnections[slotId] = wsConnection;

  //  webSocketDebug("websocketConnectCb3\n");

  espconn_regist_recvcb(connection, webSocketRecvCb);
  espconn_regist_sentcb(connection, webSocketSentCb);
  espconn_regist_reconcb(connection, webSocketReconCb);
  espconn_regist_disconcb(connection, webSocketDisconCb);

  //  webSocketDebug("leaving websocketConnectCb\n");
}
コード例 #18
0
ファイル: user_tcp.c プロジェクト: kopytkoProg/ESP8266
void ICACHE_FLASH_ATTR
createServer() {

	init_slots();
	fifo_init(msg_queue, SENT_QUEUE_LENGTH);

	pTcpServer = (struct espconn *) os_zalloc(sizeof(struct espconn));
	if (pTcpServer == NULL) {
		debug_print_str_uart("TcpServer Failure\r\n");
		return;
	}
	debug_print_str_uart("createServer \n\r");

	pTcpServer->type = ESPCONN_TCP;
	pTcpServer->state = ESPCONN_NONE;
	pTcpServer->proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp));
	pTcpServer->proto.tcp->local_port = 300;
	espconn_regist_connectcb(pTcpServer, at_tcpserver_listen);
	espconn_accept(pTcpServer);
	espconn_regist_time(pTcpServer, 180, 0);
}
コード例 #19
0
void ICACHE_FLASH_ATTR tfp_open_connection(void) {
	// Common initialisations.
	ringbuffer_init(&tfp_rb, tfp_rb_buffer, TFP_RING_BUFFER_SIZE);
	brickd_init();
	com_init();

	for(uint8_t i = 0; i < TFP_MAX_CONNECTIONS; i++) {
		tfp_init_con(i);
	}

	/*
	 * When mesh mode is enabled all the socket setup is done from mesh specific
	 * callbacks. Existing TFP socket callbacks and implementation are used but as
	 * a layer underneath the mesh layer.
	 */
	if(!configuration_current.mesh_enable) {
		ets_memset(&tfp_con_listen, 0, sizeof(struct espconn));

		// Initialize the ESPConn
		espconn_create(&tfp_con_listen);
		tfp_con_listen.type = ESPCONN_TCP;
		tfp_con_listen.state = ESPCONN_NONE;

		// Make it a TCP connection
		tfp_con_listen.proto.tcp = &tfp_con_listen_tcp;
		tfp_con_listen.proto.tcp->local_port = configuration_current.general_port;

		espconn_regist_reconcb(&tfp_con_listen, tfp_reconnect_callback);
		espconn_regist_connectcb(&tfp_con_listen, tfp_connect_callback);

		// Start listening
		espconn_accept(&tfp_con_listen);

		// Set server timeout (in seconds)
		espconn_regist_time(&tfp_con_listen, 7200, 0);
	}
	else {
		logi("MSH:TFP init\n");
	}
}
コード例 #20
0
ファイル: http.c プロジェクト: Daven005/ESP8266
bool ICACHE_FLASH_ATTR tcp_listen(unsigned int port) {
	int ret;

	httpConn.type = ESPCONN_TCP;
	httpConn.state = ESPCONN_NONE;
	httpConn.proto.tcp = &httpTcp;
	httpConn.proto.tcp->local_port = port;

	espconn_regist_connectcb(&httpConn, tcp_connect_cb);
	espconn_regist_reconcb(&httpConn, tcp_reconnect_cb);
	espconn_regist_disconcb(&httpConn, tcp_disconnect_cb);

	ret = espconn_accept(&httpConn);
	espconn_regist_time(&httpConn, 15, 0); //timeout

	if (ret != ESPCONN_OK) {
		TESTP("Error when starting the listener: %d.\n", ret);
		ets_delay_us(2000);
		return false;
	}
	return true;
}
コード例 #21
0
void ICACHE_FLASH_ATTR server_listen()
{
	os_printf("begin server_listen\n");
	struct espconn* pconn = (struct espconn*)os_zalloc(sizeof(struct espconn));
	esp_tcp* ptcp = (esp_tcp*)os_zalloc(sizeof(esp_tcp));
	heart_timer* pheart = (heart_timer*)os_zalloc(sizeof(heart_timer));
	ETSTimer* ptimer = (ETSTimer*)os_zalloc(sizeof(ETSTimer));

	pconn->state = ESPCONN_NONE;
	pconn->type = ESPCONN_TCP;
	pconn->proto.tcp = ptcp;
	pconn->proto.tcp->local_port = LOCAL_SERVER_PORT;
	pheart->mode = MODE_SERVER;
	pheart->conn = pconn;
	pheart->timer = ptimer;
	pconn->reverse = (void*)pheart;

	espconn_regist_connectcb(pconn, client_connected_cb);

	espconn_accept(pconn);
	espconn_regist_time(pconn, 120, 0);
	os_printf("server_listen ok, conn: [%p]\n", pconn);
}
コード例 #22
0
ファイル: user_tcp.c プロジェクト: kopytkoProg/ESP8266
LOCAL void ICACHE_FLASH_ATTR
at_tcpserver_listen(void *arg) {
	struct espconn *pespconn = (struct espconn *) arg;

	int8_t first_free_slot = get_first_free_slot();
	if (first_free_slot == -1)
		return;

	slot[first_free_slot].free = FALSE;
	slot[first_free_slot].pCon = pespconn;
	slot[first_free_slot].len = 0;
	// slot[first_free_slot].linkId = linkId_counter++;

	pespconn->reverse = &slot[first_free_slot];

	debug_print_str_tcp("at_tcpserver_listen \n\r");
	espconn_regist_time(pespconn, 0, 15);					// 15s timeout
	espconn_regist_recvcb(pespconn, at_tcpclient_recv);
	espconn_regist_reconcb(pespconn, at_tcpserver_recon_cb);
	espconn_regist_disconcb(pespconn, at_tcpserver_discon_cb);
	espconn_regist_sentcb(pespconn, at_tcpclient_sent_cb);

}
コード例 #23
0
// 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");
}
コード例 #24
0
ファイル: http_process.c プロジェクト: andresvidal/esp-ginx
http_connection ICACHE_FLASH_ATTR * http_new_connection(uint8_t in,struct espconn *conn){

	int i;
	//Find empty connection in pool
	for (i=0; i<MAX_CONNECTIONS; i++) if (connection_poll[i].espConnection==NULL) break;
	

	if (i>=MAX_CONNECTIONS) {
		NODE_DBG("Connection pool overflow!");	

		if(conn!=NULL){			
			espconn_disconnect(conn);
		}

		return;
	}	

	NODE_DBG("\nNew connection, conn=%p, pool slot %d", conn, i);

	if(conn!=NULL){		
		connection_poll[i].espConnection=conn;
		connection_poll[i].espConnection->reverse=&connection_poll[i];
	}

	//allocate buffer
	connection_poll[i].output.buffer = (uint8_t *)os_zalloc(HTTP_BUFFER_SIZE);

	//zero headers again- for sanity
	int j=0;
	while(j<MAX_HEADERS){

		if(connection_poll[i].headers[j].value!=NULL
			&& (connection_poll[i].headers[j].value!=connection_poll[i].headers[j].key)){
			os_free(connection_poll[i].headers[j].value);	
			connection_poll[i].headers[j].value=NULL;
		}
		connection_poll[i].headers[j].key=NULL;
		
		j++;
	}

	//mark cgi as not done
	connection_poll[i].cgi.done=0;
	
	//free response buffer again
	http_reset_buffer(&connection_poll[i]);
		
	//init body
	connection_poll[i].body.len=0;
	connection_poll[i].body.save=0;
	connection_poll[i].body.data=NULL;

	//reset parser	
	http_parser_settings_init(&(connection_poll[i].parser_settings));
	connection_poll[i].parser_settings.on_message_begin=on_message_begin;
	connection_poll[i].parser_settings.on_url=on_url;
	connection_poll[i].parser_settings.on_header_field=on_header_field;
	connection_poll[i].parser_settings.on_header_value=on_header_value;
	connection_poll[i].parser_settings.on_headers_complete=on_headers_complete;
	connection_poll[i].parser_settings.on_body=on_body;
	connection_poll[i].parser_settings.on_message_complete=on_message_complete;

	//attach httpd connection to data (socket info) so we may retrieve it easily inside parser callbacks
	connection_poll[i].parser.data=(&connection_poll[i]);

	//init parser
	if(in){		
		http_parser_init(&(connection_poll[i].parser),HTTP_REQUEST);

		//register espconn callbacks
		espconn_regist_recvcb(conn, http_process_received_cb);
		espconn_regist_reconcb(conn, http_process_reconnect_cb);
		espconn_regist_disconcb(conn, http_process_disconnect_cb);
		espconn_regist_sentcb(conn, http_process_sent_cb);
	}
	else{		
		http_parser_init(&(connection_poll[i].parser),HTTP_RESPONSE);

		connection_poll[i].espConnection = &connection_poll[i].client_connection;

		connection_poll[i].espConnection->reverse=&connection_poll[i]; //set reverse object 

		connection_poll[i].espConnection->type=ESPCONN_TCP;
		connection_poll[i].espConnection->state=ESPCONN_NONE;
		connection_poll[i].espConnection->proto.tcp = &connection_poll[i].client_tcp;

		//register espconn callbacks
		espconn_regist_recvcb(connection_poll[i].espConnection, http_process_received_cb);
		espconn_regist_reconcb(connection_poll[i].espConnection, http_process_reconnect_cb);
		espconn_regist_disconcb(connection_poll[i].espConnection, http_process_disconnect_cb);
		espconn_regist_sentcb(connection_poll[i].espConnection, http_process_sent_cb);		
	}
	
	
	espconn_regist_time(conn,30,0);

	return &connection_poll[i];

}
コード例 #25
0
ファイル: at_ipCmd.c プロジェクト: SmbatYeranyan/lemonbox
/**
  * @brief  Setup commad of module as server.
  * @param  id: commad id number
  * @param  pPara: AT input param
  * @retval None
  */
void ICACHE_FLASH_ATTR
at_setupCmdCipserver(uint8_t id, char *pPara)
{
  BOOL serverEnTemp;
  int32_t port;
  char temp[32];

  if(at_ipMux == FALSE)
  {
    at_backError;
    return;
  }
  pPara++;
  serverEnTemp = atoi(pPara);
  pPara++;
  if(serverEnTemp == 0)
  {
    if(*pPara != '\r')
    {
      at_backError;
      return;
    }
  }
  else if(serverEnTemp == 1)
  {
    if(*pPara == ',')
    {
      pPara++;
      port = atoi(pPara);
    }
    else
    {
      port = 333;
    }
  }
  else
  {
    at_backError;
    return;
  }
  if(serverEnTemp == serverEn)
  {
    uart0_sendStr("no change\r\n");
    return;
  }

  if(serverEnTemp)
  {
    pTcpServer = (struct espconn *)os_zalloc(sizeof(struct espconn));
    if (pTcpServer == NULL)
    {
      uart0_sendStr("TcpServer Failure\r\n");
      return;
    }
    pTcpServer->type = ESPCONN_TCP;
    pTcpServer->state = ESPCONN_NONE;
    pTcpServer->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
    pTcpServer->proto.tcp->local_port = port;
    espconn_regist_connectcb(pTcpServer, at_tcpserver_listen);
    espconn_accept(pTcpServer);
    espconn_regist_time(pTcpServer, server_timeover, 0);

    pUdpServer = (struct espconn *)os_zalloc(sizeof(struct espconn));
    if (pUdpServer == NULL)
    {
      uart0_sendStr("UdpServer Failure\r\n");
      return;
    }
    pUdpServer->type = ESPCONN_UDP;
    pUdpServer->state = ESPCONN_NONE;
    pUdpServer->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
    pUdpServer->proto.udp->local_port = port;
    pUdpServer->reverse = NULL;
    espconn_regist_recvcb(pUdpServer, at_udpserver_recv);
    espconn_create(pUdpServer);

//    if(pLink[0].linkEn)
//    {
//      uart0_sendStr("Link is builded\r\n");
//      return;
//    }
//    pLink[0].pCon = (struct espconn *)os_zalloc(sizeof(struct espconn));
//    if (pLink[0].pCon == NULL)
//    {
//      uart0_sendStr("Link buile Failure\r\n");
//      return;
//    }
//    pLink[0].pCon->type = ESPCONN_TCP;
//    pLink[0].pCon->state = ESPCONN_NONE;
//    pLink[0].linkId = 0;
//    pLink[0].linkEn = TRUE;
//
//    pLink[0].pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
//    pLink[0].pCon->proto.tcp->local_port = port;
//
//    pLink[0].pCon->reverse = &pLink[0];
//
//    espconn_regist_connectcb(pLink[0].pCon, user_test_tcpserver_listen);
//    espconn_accept(pLink[0].pCon);
//    at_linkNum++;
  }
  else
  {
    /* restart */
    uart0_sendStr("we must restart\r\n");
    return;
  }
  serverEn = serverEnTemp;
  at_backOk;
}
コード例 #26
0
ファイル: net.c プロジェクト: aeickho/nodemcu-firmware
// 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;  
}
コード例 #27
0
ファイル: httpd-nonos.c プロジェクト: MightyPork/libesphttpd
void ICACHE_FLASH_ATTR httpdPlatDisableTimeout(ConnTypePtr conn) {
	//Can't disable timeout; set to 2 hours instead.
	espconn_regist_time(conn, 7199, 1);
}
コード例 #28
0
static int ICACHE_FLASH_ATTR server_init_tcp(uint8 ifnum)
{
	DEBUG("enter server_init_tcp");
	static struct ip_info info;
	static struct espconn server_conn;
	static esp_tcp server_tcp;
	int rc;

	if (!wifi_get_ip_info(ifnum, &info)) {
		ets_uart_printf("Failed to get ip info.\n");
		DEBUG("exit server_init_tcp");
		return -1;
	}

	MessageQueue_clear(&sendq);
	MessageQueue_clear(&recvq);
	system_os_task(server_task, SERVER_TASK_PRIO, server_task_queue, SERVER_TASK_QUEUE_LEN);

	server_tcp.local_port = 80;
	os_memcpy(server_tcp.local_ip, &(info.ip.addr), 4);

	server_conn.type = ESPCONN_TCP;
	server_conn.proto.tcp = &server_tcp;

	if (espconn_regist_sentcb(&server_conn, tcpserver_sent_cb) != 0) {
		ets_uart_printf("Failed to register sent callback.\n");
		DEBUG("exit server_init_tcp");
		return -1;
	}

	if (espconn_regist_recvcb(&server_conn, tcpserver_recv_cb) != 0) {
		ets_uart_printf("Failed to register recv callback.\n");
		DEBUG("exit server_init_tcp");
		return -1;
	}

	if (espconn_regist_connectcb(&server_conn, tcpserver_connect_cb) != 0) {
		ets_uart_printf("Failed to register connect callback.\n");
		DEBUG("exit server_init_tcp");
		return -1;
	}

	if (espconn_regist_reconcb(&server_conn, tcpserver_reconnect_cb) != 0) {
		ets_uart_printf("Failed to register reconnect callback.\n");
		DEBUG("exit server_init_tcp");
		return -1;
	}

	if (espconn_regist_disconcb(&server_conn, tcpserver_disconnect_cb) != 0) {
		ets_uart_printf("Failed to register disconnect callback.\n");
		DEBUG("exit server_init_tcp");
		return -1;
	}

	server_conn.link_cnt = 0;
	server_conn.reverse = NULL;
	
	espconn_disconnect(&server_conn);

	if ((rc = espconn_accept(&server_conn)) != 0) {
		if (rc == ESPCONN_ISCONN) {
			ets_uart_printf("TCP server already connected.\n");
		} else {
			ets_uart_printf("Failed to accept. %d\n", rc);
			DEBUG("exit server_init_tcp");
			return -1;
		}
	} else {
		/* Set to 0 for unlimited TCP connection time (no timeout) */
		if (espconn_regist_time(&server_conn, 0, 0) != 0) {
			ets_uart_printf("Failed to set timeout interval.\n");
			DEBUG("exit server_init_tcp");
			return -1;
		}
	}

//	tcpserver_conn = &server_conn;
	ets_uart_printf("Successfully initialized TCP server.\n\n");
	DEBUG("exit server_init_tcp");
	return 0;
}
コード例 #29
0
/**
 * Continue creating a socket, the name resolution having completed
 */
static int connectSocket(
    struct socketData *pSocketData //!< Allocated socket data structure
) {
    struct espconn *pEspconn = pSocketData->pEspconn;
    bool isServer = *(uint32_t *)&pEspconn->proto.tcp->remote_ip == 0;

    int newSocket = pSocketData->socketId;
    assert(pSocketData->rxBufQ == NULL);
    assert(pSocketData->currentTx == NULL);

    // If we are a client
    if (!isServer) {
        pSocketData->state = SOCKET_STATE_CONNECTING;
        pSocketData->creationType = SOCKET_CREATED_OUTBOUND;

        espconn_regist_connectcb(pEspconn, esp8266_callback_connectCB_outbound);
        espconn_regist_disconcb(pEspconn, esp8266_callback_disconnectCB);
        espconn_regist_reconcb(pEspconn, esp8266_callback_reconnectCB);
        espconn_regist_sentcb(pEspconn, esp8266_callback_sentCB);
        espconn_regist_recvcb(pEspconn, esp8266_callback_recvCB);

        // Make a call to espconn_connect.
#if 0
        DBG("%s: connecting socket %d/%p/%p to %d.%d.%d.%d:%d from :%d\n",
            DBG_LIB, pSocketData->socketId, pSocketData, pEspconn,
            IP2STR(pEspconn->proto.tcp->remote_ip), pEspconn->proto.tcp->remote_port,
            pEspconn->proto.tcp->local_port);
#endif
        int rc = espconn_connect(pEspconn);
        if (rc != 0) {
            DBG("%s: error %d connecting socket %d: %s\n", DBG_LIB,
                rc, pSocketData->socketId, esp8266_errorToString(rc));
            releaseEspconn(pSocketData);
            releaseSocket(pSocketData);
            return rc;
        }
        DBG("%s: connecting socket %d to %d.%d.%d.%d:%d\n", DBG_LIB, pSocketData->socketId,
            IP2STR(pEspconn->proto.tcp->remote_ip), pEspconn->proto.tcp->remote_port);
    }

    // If the ipAddress IS 0 ... then we are a server.
    else {
        // We are going to set ourselves up as a server
        pSocketData->state        = SOCKET_STATE_IDLE;
        pSocketData->creationType = SOCKET_CREATED_SERVER;
        pEspconn->proto.tcp->local_port = pEspconn->proto.tcp->remote_port;
        pEspconn->proto.tcp->remote_port = 0;

        espconn_regist_connectcb(pEspconn, esp8266_callback_connectCB_inbound);

        // Make a call to espconn_accept (this should really be called espconn_listen, sigh)
        int rc = espconn_accept(pEspconn);
        if (rc != 0) {
            DBG("%s: error %d creating listening socket %d: %s\n", DBG_LIB,
                rc, pSocketData->socketId, esp8266_errorToString(rc));
            releaseEspconn(pSocketData);
            releaseSocket(pSocketData);
            return rc;
        }
        espconn_regist_time(pEspconn, 600, 0);
        DBG("%s: listening socket %d on port %d\n", DBG_LIB,
            pSocketData->socketId, pEspconn->proto.tcp->local_port);
    }

    return newSocket;
}