static void ICACHE_FLASH_ATTR httpdConnectCb(void *arg) { 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("Aiee, conn pool overflow!\n"); espconn_disconnect(conn); return; } connData[i].priv=&connPrivData[i]; connData[i].conn=conn; 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; espconn_regist_recvcb(conn, httpdRecvCb); espconn_regist_reconcb(conn, httpdReconCb); espconn_regist_disconcb(conn, httpdDisconCb); espconn_regist_sentcb(conn, httpdSentCb); }
/** * @brief Tcp client connect success callback function. * @param arg: contain the ip link information * @retval None */ void ICACHE_FLASH_ATTR mqtt_tcpclient_connect_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; MQTT_Client* client = (MQTT_Client *)pCon->reserve; espconn_regist_disconcb(client->pCon, mqtt_tcpclient_discon_cb); espconn_regist_recvcb(client->pCon, mqtt_tcpclient_recv);//////// espconn_regist_sentcb(client->pCon, mqtt_tcpclient_sent_cb);/////// INFO("MQTT: Connected to broker %s:%d\r\n", client->host, client->port); mqtt_msg_init(&client->mqtt_state.mqtt_connection, client->mqtt_state.out_buffer, client->mqtt_state.out_buffer_length); client->mqtt_state.outbound_message = mqtt_msg_connect(&client->mqtt_state.mqtt_connection, client->mqtt_state.connect_info); client->mqtt_state.pending_msg_type = mqtt_get_type(client->mqtt_state.outbound_message->data); client->mqtt_state.pending_msg_id = mqtt_get_id(client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length); client->sendTimeout = MQTT_SEND_TIMOUT; INFO("MQTT: Sending, type: %d, id: %04X\r\n", client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id); if (client->security) { #ifdef MQTT_SSL_ENABLE espconn_secure_send(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length); #else INFO("TCP: Do not support SSL\r\n"); #endif } else { espconn_send(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length); } client->mqtt_state.outbound_message = NULL; client->connState = MQTT_CONNECT_SENDING; xSemaphoreGive(client->mqttTaskSem); }
void ICACHE_FLASH_ATTR connected_cloud_cb(void* param) { uint8 rcvbuf[16]; struct espconn* conn = (struct espconn*)param; os_printf("connected_cloud_cb, conn: [%p]\n", conn); client_status = STATUS_CONNECTED; //注册 断开与接收、发送的回调 espconn_regist_recvcb(conn, data_received_cb); espconn_regist_sentcb(conn, sent_cloud_cb); espconn_regist_disconcb(conn, disconnected_cloud_cb); //注册心跳回调 heart_timer* pheart = (heart_timer*)os_zalloc(sizeof(heart_timer)); ETSTimer* timer = (ETSTimer*)os_zalloc(sizeof(ETSTimer)); pheart->timer = timer; pheart->conn = conn; conn->reverse = (void*)pheart; os_timer_disarm(timer); os_timer_setfn(timer, heart_beat_cbfn, pheart); os_timer_arm(timer, 120000, 1);//两分钟一个心跳包, 重复 uint32* pchipid = (uint32*)(rcvbuf + 2); rcvbuf[0] = 7; rcvbuf[1] = 0; *pchipid = system_get_chip_id(); rcvbuf[6] = 1; espconn_sent(conn, rcvbuf, 7); os_printf("DEV RP OVER\n"); }
static void mqtt_socket_connected(void *arg) { NODE_DBG("enter mqtt_socket_connected.\n"); struct espconn *pesp_conn = arg; if(pesp_conn == NULL) return; lmqtt_userdata *mud = (lmqtt_userdata *)pesp_conn->reverse; if(mud == NULL) return; mud->connected = true; espconn_regist_recvcb(pesp_conn, mqtt_socket_received); espconn_regist_sentcb(pesp_conn, mqtt_socket_sent); espconn_regist_disconcb(pesp_conn, mqtt_socket_disconnected); uint8_t temp_buffer[MQTT_BUF_SIZE]; // call mqtt_connect() to start a mqtt connect stage. mqtt_msg_init(&mud->mqtt_state.mqtt_connection, temp_buffer, MQTT_BUF_SIZE); mqtt_message_t* temp_msg = mqtt_msg_connect(&mud->mqtt_state.mqtt_connection, mud->mqtt_state.connect_info); NODE_DBG("Send MQTT connection infomation, data len: %d, d[0]=%d \r\n", temp_msg->length, temp_msg->data[0]); mud->event_timeout = MQTT_SEND_TIMEOUT; // not queue this message. should send right now. or should enqueue this before head. if(mud->secure) espconn_secure_sent(pesp_conn, temp_msg->data, temp_msg->length); else espconn_sent(pesp_conn, temp_msg->data, temp_msg->length); mud->keep_alive_tick = 0; mud->connState = MQTT_CONNECT_SENDING; NODE_DBG("leave mqtt_socket_connected.\n"); return; }
// ---------------------------------------------------------------------------- // 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); }
/* Called when successfully connected */ static void http_connect_cb(void *arg) { char *buf; struct espconn *conn = (struct espconn *) arg; struct http_ctx *ctx = (struct http_ctx *) conn->proto.tcp; if (strcmp(ctx->method, "GET") == 0) { const char *const reqfmt = "GET %s HTTP/1.0\r\n\r\n"; int buflen = strlen(ctx->path) + strlen(reqfmt) - 2 + 1; buf = (char *) malloc(buflen); snprintf(buf, buflen, reqfmt, ctx->path); } else { if (v7_is_string(ctx->body)) { const char *const reqfmt = "POST %s HTTP/1.0\r\ncontent-length: %d\r\n\r\n%s"; size_t len; const char *body = v7_to_string(v7, &ctx->body, &len); /* some space for content length and zero terminator */ int buflen = strlen(ctx->path) + strlen(reqfmt) + len + 10; buf = (char *) malloc(buflen); snprintf(buf, buflen, reqfmt, ctx->path, (int) len, body); v7_disown(v7, &ctx->body); } else { fprintf(stderr, "body not a string\n"); } } espconn_regist_recvcb(conn, http_recv_cb); espconn_regist_sentcb(conn, http_sent_cb); espconn_sent(conn, buf, strlen(buf)); free(buf); }
/** * @brief Tcp client connect success callback function. * @param arg: contain the ip link information * @retval None */ void ICACHE_FLASH_ATTR mqtt_tcpclient_connect_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; MQTT_Client* client = (MQTT_Client *)pCon->reverse; espconn_regist_disconcb(client->pCon, mqtt_tcpclient_discon_cb); espconn_regist_recvcb(client->pCon, mqtt_tcpclient_recv);//////// espconn_regist_sentcb(client->pCon, mqtt_tcpclient_sent_cb);/////// INFO("MQTT: Connected to broker %s:%d\r\n", client->host, client->port); mqtt_msg_init(&client->mqtt_state.mqtt_connection, client->mqtt_state.out_buffer, client->mqtt_state.out_buffer_length); client->mqtt_state.outbound_message = mqtt_msg_connect(&client->mqtt_state.mqtt_connection, client->mqtt_state.connect_info); client->mqtt_state.pending_msg_type = mqtt_get_type(client->mqtt_state.outbound_message->data); client->mqtt_state.pending_msg_id = mqtt_get_id(client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length); client->sendTimeout = MQTT_SEND_TIMOUT; INFO("MQTT: Sending, type: %d, id: %04X\r\n",client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id); if(client->security){ espconn_secure_sent(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length); } else{ espconn_sent(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length); } client->mqtt_state.outbound_message = NULL; client->connState = MQTT_CONNECT_SENDING; system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client); }
static void ICACHE_FLASH_ATTR dnsLookupCb(const char *name, ip_addr_t *ipaddr, void *arg){ struct espconn* conn = arg; if(ipaddr == NULL){ os_printf("Logger: couldn't resolve IP address for %s;\n", name); return; } os_printf("Successfully resolved %s as %d.%d.%d.%d\n", name, *((uint8 *) &ipaddr->addr), *((uint8 *) &ipaddr->addr + 1), *((uint8 *) &ipaddr->addr + 2), *((uint8 *) &ipaddr->addr + 3)); if(master_addr.addr == 0 && ipaddr->addr != 0){ master_addr.addr = ipaddr->addr; os_memcpy(conn->proto.tcp->remote_ip, &ipaddr->addr, 4); os_printf("Will send to %d.%d.%d.%d\n", (int)conn->proto.tcp->remote_ip[0], (int)conn->proto.tcp->remote_ip[1], (int)conn->proto.tcp->remote_ip[2], (int)conn->proto.tcp->remote_ip[3]); conn->proto.tcp->local_port = espconn_port(); } //espconn_create(conn); 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); os_timer_arm(&broadcastTimer, 60000, 1); }
LOCAL void ICACHE_FLASH_ATTR at_tcpserver_listen(void *arg) { struct espconn *pespconn = (struct espconn *)arg; uint8_t i; printf("get tcpClient:\r\n"); for( i = 0; i < MAX_CONNS; i++ ) { if( connections[i].pespconn == 0 ) { break; } } if( i == MAX_CONNS ) { return; } connections[i].pespconn = pespconn; connections[i].cansend = 1; connections[i].id = i; pespconn->reverse = (void*)&connections[i]; 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); uart0_sendStr("Link\r\n"); AddPlayer( i ); }
static int tls_socket_connect( lua_State *L ) { tls_socket_ud *ud = (tls_socket_ud *)luaL_checkudata(L, 1, "tls.socket"); luaL_argcheck(L, ud, 1, "TLS socket expected"); if(ud==NULL){ NODE_DBG("userdata is nil.\n"); return 0; } if (ud->pesp_conn) { return luaL_error(L, "already connected"); } u16 port = luaL_checkinteger( L, 2 ); size_t il; const char *domain = "127.0.0.1"; if( lua_isstring(L, 3) ) domain = luaL_checklstring( L, 3, &il ); if (port == 0) return luaL_error(L, "invalid port"); if (domain == NULL) return luaL_error(L, "invalid domain"); ud->pesp_conn = (struct espconn*)c_zalloc(sizeof(struct espconn)); if(!ud->pesp_conn) return luaL_error(L, "not enough memory"); ud->pesp_conn->proto.udp = NULL; ud->pesp_conn->proto.tcp = (esp_tcp *)c_zalloc(sizeof(esp_tcp)); if(!ud->pesp_conn->proto.tcp){ c_free(ud->pesp_conn); ud->pesp_conn = NULL; return luaL_error(L, "not enough memory"); } ud->pesp_conn->type = ESPCONN_TCP; ud->pesp_conn->state = ESPCONN_NONE; ud->pesp_conn->reverse = ud; ud->pesp_conn->proto.tcp->remote_port = port; espconn_regist_connectcb(ud->pesp_conn, (espconn_connect_callback)tls_socket_onconnect); espconn_regist_disconcb(ud->pesp_conn, (espconn_connect_callback)tls_socket_ondisconnect); espconn_regist_reconcb(ud->pesp_conn, (espconn_reconnect_callback)tls_socket_onreconnect); espconn_regist_recvcb(ud->pesp_conn, (espconn_recv_callback)tls_socket_onrecv); espconn_regist_sentcb(ud->pesp_conn, (espconn_sent_callback)tls_socket_onsent); if (ud->self_ref == LUA_NOREF) { lua_pushvalue(L, 1); // copy to the top of stack ud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); } ip_addr_t addr; err_t err = dns_gethostbyname(domain, &addr, (dns_found_callback)tls_socket_dns_cb, ud); if (err == ERR_OK) { tls_socket_dns_cb(domain, &addr, ud); } else if (err != ERR_INPROGRESS) { tls_socket_dns_cb(domain, NULL, ud); } return 0; }
void ICACHE_FLASH_ATTR esp8266_resolveCallback(const char* name, ip_addr_t* ip, void* arg) { struct espconn* conn = (struct espconn*)arg; NetConnection* netConn = (NetConnection*)conn->reverse; uart0_tx_buffer("get conn\r\n", 10); HTTPESP8266ConnectionData* driver = esp8266_getConnection(netConn); uart0_tx_buffer("check ip\r\n", 10); if(!ip) { // failed to lookup the hostname uart0_tx_buffer("bad ip\r\n", 8); esp8266_destroyConnection(netConn); netConn->readCallback(netConn->userData, NULL, 0, net_error); return; } char pageBuffer[20]; ets_sprintf(pageBuffer, "r: %d.%d.%d.%d\r\n", IP2STR(ip)); uart0_tx_buffer(pageBuffer, strlen(pageBuffer)); uart0_tx_buffer("set tcp callbacks\r\n", 19); espconn_regist_connectcb(conn, esp8266_connectCallback); espconn_regist_disconcb(conn, esp8266_disconnectCallback); espconn_regist_reconcb(conn, esp8266_reconnectCallback); uart0_tx_buffer("set callbacks\r\n", 15); espconn_regist_recvcb(conn, esp8266_recvCallback); espconn_regist_sentcb(conn, esp8266_sendCallback); uart0_tx_buffer("set ip\r\n", 8); ets_memcpy(&conn->proto.tcp->remote_ip, ip, 4); if(driver->secure) { uart0_tx_buffer("async sconnect\r\n", 16); conn->proto.tcp->remote_port = 443; ets_sprintf(pageBuffer, "port: %d\r\n", conn->proto.tcp->remote_port); uart0_tx_buffer(pageBuffer, strlen(pageBuffer)); sint8 r = espconn_secure_connect(conn); ets_sprintf(pageBuffer, "c_conn: %d\r\n", r); uart0_tx_buffer(pageBuffer, strlen(pageBuffer)); } else { uart0_tx_buffer("async connect\r\n", 15); conn->proto.tcp->remote_port = 80; ets_sprintf(pageBuffer, "port: %d\r\n", conn->proto.tcp->remote_port); uart0_tx_buffer(pageBuffer, strlen(pageBuffer)); sint8 r = espconn_connect(conn); ets_sprintf(pageBuffer, "c_conn: %d\r\n", r); uart0_tx_buffer(pageBuffer, strlen(pageBuffer)); } }
void Tcp::registerCb(struct espconn* pconn) { pconn->reverse = this; // espconn_regist_time(pconn, 10, 1); // disconnect after 1000 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); }
static void ICACHE_FLASH_ATTR platConnCb(void *arg) { ConnTypePtr conn=arg; if (httpdConnectCb(conn, (char*)conn->proto.tcp->remote_ip, conn->proto.tcp->remote_port)) { espconn_regist_recvcb(conn, platRecvCb); espconn_regist_reconcb(conn, platReconCb); espconn_regist_disconcb(conn, platDisconCb); espconn_regist_sentcb(conn, platSentCb); } else { espconn_disconnect(conn); } }
static void server_connect_cb(void *arg) { struct espconn *c = (struct espconn *) arg; if (c == NULL) { printf("got null client, what does it even mean?\n"); return; } espconn_regist_recvcb(c, server_recv); espconn_regist_sentcb(c, server_sent); espconn_regist_disconcb(c, server_disconnect); }
/****************************************************************************** * FunctionName : user_check_ip * Description : check whether get ip addr or not * Parameters : none * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR user_check_ip(void) { struct ip_info ipconfig; //disarm timer first os_timer_disarm(&test_timer); //get ip info of ESP8266 station wifi_get_ip_info(STATION_IF, &ipconfig); if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) { //os_printf("got ip !!! \r\n"); wifi_set_broadcast_if(STATIONAP_MODE); // send UDP broadcast from both station and soft-AP interface user_udp_espconn.type = ESPCONN_UDP; user_udp_espconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); user_udp_espconn.proto.udp->local_port = 1112; // set a available port const char udp_remote_ip[4] = {255, 255, 255, 255}; os_memcpy(user_udp_espconn.proto.udp->remote_ip, udp_remote_ip, 4); // ESP8266 udp remote IP user_udp_espconn.proto.udp->remote_port = 5556; // ESP8266 udp remote port espconn_regist_recvcb(&user_udp_espconn, user_udp_recv_cb); // register a udp packet receiving callback espconn_regist_sentcb(&user_udp_espconn, user_udp_sent_cb); // register a udp packet sent callback espconn_create(&user_udp_espconn); // create udp user_udp_send(); // send udp data os_timer_arm(&send_timer, 40, 0); } else { if ((wifi_station_get_connect_status() == STATION_WRONG_PASSWORD || wifi_station_get_connect_status() == STATION_NO_AP_FOUND || wifi_station_get_connect_status() == STATION_CONNECT_FAIL)) { //os_printf("connect fail !!! \r\n"); } else { //os_printf("still waiting...\n"); //re-arm timer to check ip os_timer_setfn(&test_timer, (os_timer_func_t *)user_check_ip, NULL); os_timer_arm(&test_timer, 500, 0); } } }
void ICACHE_FLASH_ATTR tcp_connect_cb ( void *arg ) { struct espconn *conn = (struct espconn *)arg; char buf[16]; os_printf ( "TCP connect\n" ); os_printf ( " remote ip: %s\n", ip2str ( buf, conn->proto.tcp->remote_ip ) ); os_printf ( " remote port: %d\n", conn->proto.tcp->remote_port ); espconn_regist_recvcb( conn, tcp_receive_data ); espconn_regist_sentcb( conn, tcp_send_data ); }
void ICACHE_FLASH_ATTR client_connected_cb(void* conn) { struct espconn* pconn = (struct espconn*)conn; os_printf("client connected\n"); os_printf("debug: [%p], [" IPSTR ":%d], state, [%d]\n", pconn, IP2STR(pconn->proto.tcp->remote_ip), pconn->proto.tcp->remote_port, pconn->state); espconn_regist_recvcb(pconn, data_received_cb); espconn_regist_sentcb(pconn, sent_cloud_cb); espconn_regist_disconcb(pconn, client_disconnected_cb); espconn_regist_reconcb(pconn, client_reconnect_cb); }
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); }
LOCAL void ICACHE_FLASH_ATTR dhap_httpd_connect_cb(void *arg) { struct espconn *conn = arg; mConnected++; if(mConnected > MAX_CONNECTIONS) { espconn_disconnect(conn); dhdebug("Refuse connection, already %u connections", mConnected); return; } espconn_regist_recvcb(conn, dhap_httpd_recv_cb); espconn_regist_disconcb(conn, dhap_httpd_disconnect_cb); espconn_regist_sentcb(conn, dhap_httpd_sent_cb); dhdebug("Client connected"); }
/** * @brief Tcp client connect success callback function. * @param arg: contain the ip link information * @retval None */ void ICACHE_FLASH_ATTR mqtt_tcpclient_connect_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; MQTT_Client* client = (MQTT_Client *)pCon->reverse; espconn_regist_disconcb(client->pCon, mqtt_tcpclient_discon_cb); espconn_regist_recvcb(client->pCon, mqtt_tcpclient_recv);//////// espconn_regist_sentcb(client->pCon, mqtt_tcpclient_sent_cb);/////// //spam INFO("MQTT: Connected to broker %s:%d\r\n", client->host, client->port); client->connState = MQTT_CONNECT_SEND; system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client); }
static void ICACHE_FLASH_ATTR serverConnectCb(void *arg) { struct espconn *conn = arg; connData->conn=conn; connData->txbufferlen = 0; connData->readytosend = true; DBG_MSG("Client connected!!! txbufferlen=%d\r\n", connData->txbufferlen); espconn_regist_recvcb(conn, serverRecvCb); espconn_regist_reconcb(conn, serverReconCb); espconn_regist_disconcb(conn, serverDisconCb); espconn_regist_sentcb(conn, serverSentCb); config_status = CONFIG_STATUS_TELNET_CONNNECTED; }
static int ICACHE_FLASH_ATTR server_init_udp(uint8 ifnum) { DEBUG("enter server_init_udp"); static struct ip_info info; static struct espconn server_conn; static esp_udp server_udp_sta; int rc; if (!wifi_get_ip_info(ifnum, &info)) { ets_uart_printf("Failed to get ip info.\n"); DEBUG("exit server_init_udp"); return -1; } server_udp_sta.local_port = 1025; os_memcpy(server_udp_sta.local_ip, &(info.ip.addr), 4); server_conn.type = ESPCONN_UDP; server_conn.proto.udp = &server_udp_sta; server_conn.link_cnt = 0; server_conn.reverse = NULL; if (espconn_regist_sentcb(&server_conn, udpserver_sent_cb) != 0) { ets_uart_printf("Failed to register sent callback.\n"); DEBUG("exit server_init_udp"); return -1; } if (espconn_regist_recvcb(&server_conn, udpserver_recv_cb) != 0) { ets_uart_printf("Failed to register recv callback.\n"); DEBUG("exit server_init_udp"); return -1; } if ((rc = espconn_create(&server_conn)) != 0) { if (rc == ESPCONN_ISCONN) { ets_uart_printf("UDP server already connected.\n"); } else { ets_uart_printf("Failed to create UDP server.\n"); DEBUG("exit server_init_udp"); return -1; } } // udpserver_conn = &server_conn; ets_uart_printf("Successfully initialized UDP server.\n\n"); DEBUG("exit server_init_udp"); return 0; }
/****************************************************************************** * 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 }
/****************************************************************************** * FunctionName : upgrade_connect * Description : client connected with a host successfully * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ LOCAL void ICACHE_FLASH_ATTR upgrade_connect_cb(void *arg){ struct espconn *pespconn = arg; UPGRADE_DBG("upgrade_connect_cb\n"); os_timer_disarm(&upgrade_connect_timer); espconn_regist_disconcb(pespconn, upgrade_disconcb); espconn_regist_sentcb(pespconn, upgrade_datasent); if (pbuf != NULL) { UPGRADE_DBG("%s\n", pbuf); espconn_sent(pespconn, pbuf, os_strlen(pbuf)); } }
void ICACHE_FLASH_ATTR ssConnCb(void *arg) { struct espconn *pespconn = (struct espconn *)arg; //printf("HEAP SIZE: %d\n", system_get_free_heap_size()); if (pespconn->link_cnt > MAX_CONNS) { espconn_disconnect(pespconn); os_printf("Too many connections at once.\n"); return; } espconn_regist_recvcb(pespconn, ssRecvCb); espconn_regist_sentcb(pespconn, ssSentCb); espconn_regist_disconcb(pespconn, ssDiscCb); }
/************************************************ * name: httpdConnectCb * parameters: ptArgument - esp connection * return value: none * purpose: assign connection to request ************************************************/ static void ICACHE_FLASH_ATTR httpdConnectCb(void * ptArgument) { /* initialization */ struct espconn * ptEspConnection = ptArgument; int iIndex = 0; /* find empty g_ptConnectionData in pool */ for (iIndex = 0; HTTPD_MAX_CONNECTIONS > iIndex; iIndex++) { if (NULL == g_ptConnectionData[iIndex].ptEspConnection) { break; } #ifdef HTTPD_DEBUG os_printf("httpdConnectCb: connection request: connection %p, pool slot %d\n", ptEspConnection, iIndex); #endif if (HTTPD_MAX_CONNECTIONS == iIndex) { #ifdef HTTPD_DEBUG os_printf("httpdConnectCb: connection pool overflow!\n"); #endif espconn_disconnect(ptEspConnection); goto lblCleanup; } } g_ptConnectionData[iIndex].ptPrivate = &g_ptConnectionPrivateData[iIndex]; g_ptConnectionData[iIndex].ptEspConnection = ptEspConnection; g_ptConnectionData[iIndex].ptPrivate->cbHeaderLength = 0; g_ptConnectionData[iIndex].ptPost = &g_ptConnectionPost[iIndex]; g_ptConnectionData[iIndex].ptPost->pbBuffer = NULL; g_ptConnectionData[iIndex].ptPost->cbBufferLength = 0; g_ptConnectionData[iIndex].ptPost->cbReceived = 0; g_ptConnectionData[iIndex].ptPost->cbProcessed = 0; g_ptConnectionData[iIndex].ptPost->cbPostLength = -1; espconn_regist_recvcb(ptEspConnection, httpdRecvCb); espconn_regist_reconcb(ptEspConnection, httpdReconCb); espconn_regist_disconcb(ptEspConnection, httpdDisconCb); espconn_regist_sentcb(ptEspConnection, httpdSentCb); lblCleanup: return; }
/** * Callback function registered to the ESP8266 environment that is * invoked when a new inbound connection has been formed. * A new connection * can occur when the ESP8266 makes a call out to a partner (in that * case the ESP8266 is acting as a client) or a new connection can * occur when a partner calls into a listening ESP8266. In that case * the ESP8266 is acting as a server. */ static void esp8266_callback_connectCB_inbound( void *arg //!< ) { os_printf(">> connectCB_inbound\n"); struct espconn *pEspconn = (struct espconn *)arg; assert(pEspconn != NULL); 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); espconn_regist_write_finish(pEspconn, esp8266_callback_writeFinishedCB); dumpEspConn(pEspconn); int inboundSocket = getServerSocketByLocalPort(pEspconn->proto.tcp->local_port); assert(inboundSocket != -1); struct socketData *pSocketData = getSocketData(inboundSocket); assert(pSocketData != NULL); esp8266_dumpSocket(pSocketData->socketId); os_printf("** new client has connected to us **\n"); if ((pSocketData->acceptedSocketsHead + 1) % MAX_ACCEPTED_SOCKETS == pSocketData->acceptedSocketsTail) { os_printf("WARNING!! - Discarding inbound client because we have too many accepted clients.\n"); os_printf("<< connectCB_inbound\n"); return; } struct socketData *pClientSocketData = allocateNewSocket(); if (pClientSocketData == NULL) { os_printf("!!! Ran out of sockets !!!\n"); return; } assert(pClientSocketData != NULL); pClientSocketData->pEspconn = pEspconn; pClientSocketData->pEspconn->reverse = pClientSocketData; pClientSocketData->creationType = SOCKET_CREATED_INBOUND; pClientSocketData->isConnected = true; pClientSocketData->state = SOCKET_STATE_IDLE; pSocketData->acceptedSockets[pSocketData->acceptedSocketsHead] = pClientSocketData->socketId; pSocketData->acceptedSocketsHead = (pSocketData->acceptedSocketsHead + 1) % MAX_ACCEPTED_SOCKETS; os_printf("<< connectCB_inbound\n"); }
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, ¶m); param = 2; espconn_set_keepalive(arg, ESPCONN_KEEPINTVL, ¶m); param = 10; espconn_set_keepalive(arg, ESPCONN_KEEPCNT, ¶m); 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; } }
/** * @brief Udp server receive data callback function. * @param arg: contain the ip link information * @retval None */ LOCAL void ICACHE_FLASH_ATTR at_udpserver_recv(void *arg, char *pusrdata, unsigned short len) { struct espconn *pespconn = (struct espconn *)arg; at_linkConType *linkTemp; char temp[32]; uint8_t i; os_printf("get udpClient:\r\n"); if(pespconn->reverse == NULL) { for(i = 0;i < at_linkMax;i++) { if(pLink[i].linkEn == FALSE) { pLink[i].linkEn = TRUE; break; } } if(i >= 5) { return; } pLink[i].teToff = FALSE; pLink[i].linkId = i; pLink[i].teType = teServer; pLink[i].repeaTime = 0; pLink[i].pCon = pespconn; espconn_regist_sentcb(pLink[i].pCon, at_tcpclient_sent_cb); mdState = m_linked; at_linkNum++; pespconn->reverse = &pLink[i]; uart0_sendStr("Link\r\n"); } linkTemp = (at_linkConType *)pespconn->reverse; if(pusrdata == NULL) { return; } os_sprintf(temp, "\r\n+IPD,%d,%d:", linkTemp->linkId, len); uart0_sendStr(temp); uart0_tx_buffer(pusrdata, len); at_backOk; }
void ICACHE_FLASH_ATTR tcpclient_connect_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; REST_CLIENT* client = (REST_CLIENT *)pCon->reverse; espconn_regist_disconcb(client->pCon, tcpclient_discon_cb); espconn_regist_recvcb(client->pCon, tcpclient_recv);//////// espconn_regist_sentcb(client->pCon, tcpclient_sent_cb);/////// if(client->security){ espconn_secure_sent(client->pCon, client->data, client->data_len); } else{ espconn_sent(client->pCon, client->data, client->data_len); } }