static void irc_main(void) { if (uip_aborted() || uip_timedout()) { IRCDEBUG ("connection aborted\n"); irc_conn = NULL; } if (uip_closed()) { IRCDEBUG ("connection closed\n"); irc_conn = NULL; } if (uip_connected()) { IRCDEBUG ("new connection\n"); STATE->stage = IRC_SEND_USERNICK; STATE->sent = IRC_SEND_INIT; #ifdef ECMD_IRC_SUPPORT STATE->reparse = 0; #endif *STATE->outbuf = 0; } if (STATE->stage == IRC_SEND_JOIN && uip_acked ()) STATE->stage ++; else if (STATE->stage == IRC_CONNECTED && uip_acked ()) { *STATE->outbuf = 0; #ifdef ECMD_IRC_SUPPORT if (STATE->reparse) irc_handle_ecmd (); #endif } else if (uip_newdata() && uip_len) { ((char *) uip_appdata)[uip_len] = 0; IRCDEBUG ("received data: %s\n", uip_appdata); if (irc_parse ()) { uip_close (); /* Parse error */ return; } } if (uip_rexmit ()) irc_send_data (STATE->sent); else if ((STATE->stage > STATE->sent || STATE->stage == IRC_CONNECTED) && (uip_newdata() || uip_acked() || uip_connected())) irc_send_data (STATE->stage); else if (STATE->stage == IRC_CONNECTED && uip_poll() && *STATE->outbuf) irc_send_data (STATE->stage); }
/*-----------------------------------------------------------------------------------*/ void webclient_appcall(void) { if(uip_connected()) { s.timer = 0; s.state = WEBCLIENT_STATE_STATUSLINE; senddata(); webclient_connected(); return; } if(s.state == WEBCLIENT_STATE_CLOSE) { webclient_closed(); uip_abort(); return; } if(uip_aborted()) { webclient_aborted(); } if(uip_timedout()) { webclient_timedout(); } if(uip_acked()) { s.timer = 0; acked(); } if(uip_newdata()) { s.timer = 0; newdata(); } if(uip_rexmit() || uip_newdata() || uip_acked()) { senddata(); } else if(uip_poll()) { ++s.timer; if(s.timer == WEBCLIENT_TIMEOUT) { webclient_timedout(); uip_abort(); return; } /* senddata();*/ } if(uip_closed()) { if(s.httpflag != HTTPFLAG_MOVED) { /* Send NULL data to signal EOF. */ webclient_datahandler(NULL, 0); } else { if(resolv_lookup(s.host) == NULL) { resolv_query(s.host); } webclient_get(s.host, s.port, s.file); } } }
void yport_net_main(void) { if(uip_connected()) { if (yport_conn == NULL) { yport_conn = uip_conn; uip_conn->wnd = YPORT_BUFFER_LEN - 1; } else /* if we have already an connection, send an error */ uip_send("ERROR: Connection blocked\n", 27); } else if (uip_acked()) { /* If the peer is not our connection, close it */ if (yport_conn != uip_conn) uip_close(); else { /* Some data we have sent was acked, jipphie */ /* disable interrupts */ uint8_t sreg = SREG; cli(); yport_recv_buffer.len -= yport_recv_buffer.sent; /* We should use memmove, because the data may overlap */ memmove(yport_recv_buffer.data, yport_recv_buffer.data + yport_recv_buffer.sent, yport_recv_buffer.len); /* enable interrupts again */ SREG = sreg; } } else if (uip_closed() || uip_aborted() || uip_timedout()) { /* if the closed connection was our connection, clean yport_conn */ if (yport_conn == uip_conn) yport_conn = NULL; } else if (uip_newdata()) { if (uip_len <= YPORT_BUFFER_LEN && yport_rxstart(uip_appdata, uip_len) != 0) { /* Prevent the other side from sending more data */ uip_stop(); } } if (uip_poll() && uip_conn == yport_conn && uip_stopped(yport_conn) && yport_send_buffer.sent == yport_send_buffer.len) uip_restart(); /* Send data */ if ((uip_poll() || uip_acked() || uip_rexmit()) && yport_conn == uip_conn && yport_recv_buffer.len > 0) { /* We have recieved data, lets propagade it */ /* disable interrupts */ uint8_t sreg = SREG; cli(); /* Send the data */ uip_send(yport_recv_buffer.data, yport_recv_buffer.len); /* so many data was send */ yport_recv_buffer.sent = yport_recv_buffer.len; /* enable interrupts again */ SREG = sreg; } }
/*---------------------------------------------------------------------------*/ void telnetd_appcall(void *ts) { if(uip_connected()) { if(!connected) { buf_init(&buf); s.bufptr = 0; s.state = STATE_NORMAL; connected = 1; shell_start(); timer_set(&s.silence_timer, MAX_SILENCE_TIME); ts = (char *)0; } else { uip_send(telnetd_reject_text, strlen(telnetd_reject_text)); ts = (char *)1; } tcp_markconn(uip_conn, ts); } if(!ts) { if(s.state == STATE_CLOSE) { s.state = STATE_NORMAL; uip_close(); return; } if(uip_closed() || uip_aborted() || uip_timedout()) { shell_stop(); connected = 0; } if(uip_acked()) { timer_set(&s.silence_timer, MAX_SILENCE_TIME); acked(); } if(uip_newdata()) { timer_set(&s.silence_timer, MAX_SILENCE_TIME); newdata(); } if(uip_rexmit() || uip_newdata() || uip_acked() || uip_connected() || uip_poll()) { senddata(); if(s.numsent > 0) { timer_set(&s.silence_timer, MAX_SILENCE_TIME); } } if(uip_poll()) { if(timer_expired(&s.silence_timer)) { uip_close(); tcp_markconn(uip_conn, NULL); } } } }
static void jabber_main(void) { if (uip_aborted() || uip_timedout()) { JABDEBUG("connection aborted\n"); jabber_conn = NULL; } if (uip_closed()) { JABDEBUG("connection closed\n"); jabber_conn = NULL; } if (uip_connected()) { JABDEBUG("new connection\n"); STATE->stage = JABBER_OPEN_STREAM; STATE->sent = JABBER_INIT; #ifdef JABBER_STARTUP_MESSAGE_SUPPORT strncpy_P(STATE->target, PSTR(CONF_JABBER_BUDDY), sizeof(STATE->target)); strncpy_P(STATE->outbuf, jabber_startup_text, sizeof(STATE->outbuf)); STATE->action = JABBER_ACTION_MESSAGE; #endif /* JABBER_STARTUP_MESSAGE_SUPPORT */ } if (uip_acked() && STATE->stage == JABBER_CONNECTED) { STATE->action = JABBER_ACTION_NONE; *STATE->outbuf = 0; } if (uip_newdata() && uip_len) { /* Zero-terminate */ ((char *) uip_appdata)[uip_len] = 0; JABDEBUG("received data: %s\n", uip_appdata); if (jabber_parse()) { uip_close(); /* Parse error */ return; } } if (uip_rexmit()) jabber_send_data(STATE->stage, STATE->action); else if ((STATE->stage > STATE->sent || STATE->stage == JABBER_CONNECTED) && (uip_newdata() || uip_acked() || uip_connected())) jabber_send_data(STATE->stage, STATE->action); else if (STATE->stage == JABBER_CONNECTED && uip_poll() && STATE->action) jabber_send_data(STATE->stage, STATE->action); }
void netcon_app_call(void) { if(uip_newdata()) { parse_request(); } if(uip_rexmit()) { // Psst no one saw this :D strcpy_P(uip_appdata, PSTR("ERROR\r\n")); uip_send(uip_appdata, 7); } }
void httpd_main(void) { if (uip_aborted() || uip_timedout()) { httpd_cleanup (); printf ("httpd: connection aborted\n"); } if (uip_closed()) { httpd_cleanup (); printf ("httpd: connection closed\n"); } if (uip_connected()) { printf ("httpd: new connection\n"); /* initialize struct */ STATE->handler = NULL; STATE->header_acked = 0; STATE->eof = 0; STATE->header_reparse = 0; #ifdef HTTPD_AUTH_SUPPORT STATE->auth_state = PAM_UNKOWN; #endif } if (uip_newdata() && (!STATE->handler || STATE->header_reparse)) { printf ("httpd: new data\n"); httpd_handle_input (); } #ifdef HTTPD_AUTH_SUPPORT if (STATE->auth_state == PAM_DENIED && STATE->handler != httpd_handle_401) { httpd_cleanup(); STATE->handler = httpd_handle_401; STATE->header_reparse = 0; printf("httpd: auth failed\n"); } else if (STATE->auth_state == PAM_PENDING) return; /* Waiting for the PAM Layer */ #endif if(uip_rexmit() || uip_newdata() || uip_acked() || uip_poll() || uip_connected()) { /* Call associated handler, if set already. */ if (STATE->handler && (!STATE->header_reparse)) STATE->handler (); } }
char send_data_P(register struct psock *s) { if(s->state != STATE_DATA_SENT || uip_rexmit()) { if(s->sendlen > uip_mss()) { uip_send_P((PGM_P)s->sendptr, uip_mss()); } else { uip_send_P((PGM_P)s->sendptr, s->sendlen); } s->state = STATE_DATA_SENT; return 1; } return 0; }
/****************************************************************************** * void exosite_appcall(void) * * This function is uIP's application function. It is called whenever a uIP * event occurs (e.g. when a new connection is established, new data arrives, * sent data is acknowledged, data needs to be retransmitted, etc.). * ******************************************************************************/ void exosite_appcall(void) { if(uip_connected()) { s.timer = 0; senddata(); return; } if(s.state == EXO_STATE_CLOSE) { uip_abort(); return; } if(uip_aborted()) { } if(uip_timedout()) { } if(uip_acked()) { s.timer = 0; acked(); } if(uip_newdata()) { s.timer = 0; newdata(); } if(uip_rexmit() || uip_newdata() || uip_acked()) { senddata(); } else if(uip_poll()) { ++s.timer; if(s.timer == EXO_TIMEOUT) { uip_abort(); return; } } #if 0 if(uip_closed()) { if(s.httpflag != HTTPFLAG_MOVED) { memset(s.rxdata,0,sizeof(s.rxdata)); } else { if(resolv_lookup(s.host) == NULL) { resolv_query(s.host); } } } #endif }
/*---------------------------------------------------------------------------*/ static char send_data(struct psock *s) { if(s->state != STATE_DATA_SENT || uip_rexmit()) { if(s->sendlen > uip_mss()) { uip_send(s->sendptr, uip_mss()); } else { uip_send(s->sendptr, s->sendlen); } s->state = STATE_DATA_SENT; return 1; } return 0; }
/*---------------------------------------------------------------------------*/ static char send_data(register struct psock *s) { if(s->state != STATE_DATA_SENT || uip_rexmit()) { // printf("sd %d\n",s->sendlen ); if(s->sendlen > uip_mss()) { uip_send(s->sendptr, uip_mss()); } else { uip_send(s->sendptr, s->sendlen); } s->state = STATE_DATA_SENT; return 1; } return 0; }
void ustream_main(void) { if(uip_connected() || uip_rexmit()) { uip_send(USTREAM_URI, strlen(USTREAM_URI)); return; } if(uip_newdata()) { // TBD: Push data to VS1053 ((char *) uip_appdata)[uip_len] = 0; printf("Got some data.\n"); } }
static void mysql_main(void) { if (uip_aborted() || uip_timedout()) { MYDEBUG ("connection aborted\n"); mysql_conn = NULL; } if (uip_closed()) { MYDEBUG ("connection closed\n"); mysql_conn = NULL; } if (uip_connected()) { MYDEBUG ("new connection\n"); STATE->stage = MYSQL_WAIT_GREETING; STATE->sent = MYSQL_WAIT_GREETING; STATE->packetid = 0; } if (uip_newdata() && uip_len) { #ifdef DEBUG_MYSQL MYDEBUG ("received data: %s\n", uip_appdata); for (uint16_t i = 0; i < uip_len; i ++) debug_putchar (((unsigned char *) uip_appdata)[i]); debug_putchar (10); #endif if (mysql_parse ()) { uip_close (); /* Parse error */ return; } } if (uip_rexmit()) { STATE->packetid --; mysql_send_data (STATE->sent); } else if ((STATE->stage > STATE->sent || STATE->stage == MYSQL_CONNECTED) && (uip_newdata() || uip_acked() || uip_connected())) mysql_send_data (STATE->stage); else if (STATE->stage == MYSQL_CONNECTED && uip_poll() && *STATE->u.stmtbuf) mysql_send_data(STATE->stage); }
void mqttclient_appcall(void) { if (uip_poll()) { _mqttclient_transfer_buffer(); } else if (uip_connected()) { update_state(MQTTCLIENT_BROKER_CONNECTION_ESTABLISHED); umqtt_connect(&_mqtt, &_connection_config); _mqttclient_transfer_buffer(); } else if (uip_aborted() || uip_timedout() || uip_closed()) { _mqttclient_handle_communication_error(); } else if (uip_newdata()) { _mqttclient_handle_new_data(); } else if (uip_acked()) { _is_sending = false; } else if (uip_rexmit()) { _mqttclient_send(); } }
static void httplog_net_main(void) { if (uip_aborted() || uip_timedout()) { HTTPLOG_DEBUG("connection aborted\n"); goto end; } if (uip_closed()) { HTTPLOG_DEBUG("connection closed\n"); goto end; } if (uip_connected() || uip_rexmit()) { HTTPLOG_DEBUG("new connection or rexmit, sending message\n"); char *p = uip_appdata; p += sprintf_P(p, get_string_head); #ifdef CONF_HTTPLOG_INCLUDE_UUID p += sprintf_P(p, uuid_string); #endif #ifdef CONF_HTTPLOG_INCLUDE_TIMESTAMP p += sprintf_P(p, time_string); p += sprintf(p, "%lu&", clock_get_time()); #endif p += sprintf(p, httplog_tmp_buf); p += sprintf_P(p, get_string_foot); uip_udp_send(p - (char *) uip_appdata); HTTPLOG_DEBUG("send %d bytes\n", p - (char *) uip_appdata); } if (uip_acked()) { uip_close(); end: if (httplog_tmp_buf) { free(httplog_tmp_buf); httplog_tmp_buf = NULL; } } }
/*-----------------------------------------------------------------------------------*/ void telnet_app(void *ts) { struct telnet_state *s = (struct telnet_state *)ts; if(uip_connected()) { s->flags = 0; telnet_connected(s); senddata(s); return; } if(uip_closed()) { telnet_closed(s); } if(uip_aborted()) { telnet_aborted(s); } if(uip_timedout()) { telnet_timedout(s); } if(s->flags & FLAG_CLOSE) { uip_close(); return; } if(s->flags & FLAG_ABORT) { uip_abort(); return; } if(uip_acked()) { acked(s); } if(uip_newdata()) { telnet_newdata(s, (char *)uip_appdata, uip_datalen()); } if(uip_rexmit() || uip_newdata() || uip_acked()) { senddata(s); } else if(uip_poll()) { senddata(s); } }
/*---------------------------------------------------------------------------*/ void telnetd_appcall(void) { static unsigned int i; if(uip_connected()) { /* tcp_markconn(uip_conn, &s);*/ for(i = 0; i < TELNETD_CONF_NUMLINES; ++i) { s.lines[i] = NULL; } s.bufptr = 0; s.state = STATE_NORMAL; shell_start(); } if(s.state == STATE_CLOSE) { s.state = STATE_NORMAL; uip_close(); return; } if(uip_closed() || uip_aborted() || uip_timedout()) { closed(); } if(uip_acked()) { acked(); } if(uip_newdata()) { newdata(); } if(uip_rexmit() || uip_newdata() || uip_acked() || uip_connected() || uip_poll()) { senddata(); } }
static void sms77_net_main(void) { if (uip_aborted() || uip_timedout()) { SMSDEBUG ("connection aborted\n"); if (sms77_tmp_buf) { free(sms77_tmp_buf); sms77_tmp_buf = NULL; } return; } if (uip_closed()) { SMSDEBUG ("connection closed\n"); if (sms77_tmp_buf) { free(sms77_tmp_buf); sms77_tmp_buf = NULL; } return; } if (uip_connected() || uip_rexmit()) { SMSDEBUG ("new connection or rexmit, sending message\n"); char *p = uip_appdata; p += sprintf(p, "GET /?u=%s&p=%s&to=%s&type=%s&text=", sms77_user, sms77_pass, sms77_recv, sms77_type); p += urlencode(sms77_tmp_buf, strlen(sms77_tmp_buf), p); p += sprintf_P(p, sms77_secheader); uip_udp_send(p - (char *)uip_appdata); SMSDEBUG("send %d bytes\n", p - (char *)uip_appdata); } if (uip_acked()) { uip_close(); } if (uip_newdata()) SMSDEBUG("data: %s\n", uip_appdata); }
void httpd_handle_vfs (void) { if (uip_acked ()) { if (STATE->header_acked) STATE->u.vfs.acked = STATE->u.vfs.sent; else { STATE->header_acked = 1; STATE->u.vfs.acked = 0; } } if (!STATE->header_acked) httpd_handle_vfs_send_header (); else if (STATE->eof && !uip_rexmit()) uip_close (); else httpd_handle_vfs_send_body (); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(tcp_server, ev, data) { PROCESS_BEGIN(); tcp_listen(UIP_HTONS(8080)); while(1) { PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); printf("TCP server: receive TCP event\n"); if(uip_closed() || uip_aborted() || uip_timedout()) { printf("TCP server: connection closed\n"); } else if(uip_connected()) { printf("TCP server: connected\n"); } if(uip_newdata()) { printf("TCP server: receive new data\n"); uint16_t len = uip_datalen(); uint8_t *ptr = uip_appdata; while(len--){ printf("%c", *ptr++); } printf("\n"); printf("TCP server: send echo message\n"); uip_send(uip_appdata, uip_datalen()); } if(uip_rexmit() || uip_newdata() || uip_acked() || uip_connected() || uip_poll()) { //senddata(); } } PROCESS_END(); }
static void pachube_net_main(void) { if (uip_aborted() || uip_timedout()) { return; } if (uip_closed()) { return; } if (uip_connected() || uip_rexmit()) { char *p = uip_appdata; p += sprintf_P(p, pachube_header, DS, KEY, strlen(pachube_tmp_buf)); p += sprintf(p, pachube_tmp_buf); uip_udp_send(p - (char *)uip_appdata); } if (uip_acked()) { uip_close(); } }
static void twitter_net_main(void) { if (uip_aborted() || uip_timedout()) { TWDEBUG ("connection aborted\n"); if (twitter_tmp_buf) { free(twitter_tmp_buf); twitter_tmp_buf = NULL; } return; } if (uip_closed()) { TWDEBUG ("connection closed\n"); if (twitter_tmp_buf) { free(twitter_tmp_buf); twitter_tmp_buf = NULL; } return; } if (uip_connected() || uip_rexmit()) { TWDEBUG ("new connection or rexmit, sending message\n"); char *p = uip_appdata; p += sprintf_P(p, twitter_header); p += sprintf(p, "%d", strlen(twitter_tmp_buf) + 7); p += sprintf_P(p, twitter_body); p += sprintf(p, twitter_tmp_buf); uip_udp_send(p - (char *)uip_appdata); TWDEBUG("send %d bytes\n", p - (char *)uip_appdata); } if (uip_acked()) { uip_close(); } }
void httpd_main(void) { if (uip_aborted() || uip_timedout()) { httpd_cleanup (); printf ("httpd: connection aborted\n"); } if (uip_closed()) { httpd_cleanup (); printf ("httpd: connection closed\n"); } if (uip_connected()) { printf ("httpd: new connection\n"); /* initialize struct */ STATE->handler = NULL; STATE->header_acked = 0; STATE->eof = 0; STATE->header_reparse = 0; } if (uip_newdata() && (!STATE->handler || STATE->header_reparse)) { printf ("httpd: new data\n"); httpd_handle_input (); } if(uip_rexmit() || uip_newdata() || uip_acked() || uip_connected()) { /* Call associated handler, if set already. */ if (STATE->handler && (!STATE->header_reparse)) STATE->handler (); } }
void telnetd_appcall(void *ts) { if(uip_connected()) { tcp_markconn(uip_conn, &s); buf_init(&buf); s.bufptr = 0; s.state = STATE_NORMAL; stdout = &telnet_stream; shell_input("", 0); //shell_start(); } if(s.state == STATE_CLOSE) { s.state = STATE_NORMAL; uip_close(); return; } if(uip_closed() || uip_aborted() || uip_timedout()) { closed(); } if(uip_acked()) { acked(); } if(uip_newdata()) { newdata(); } if(uip_rexmit() || uip_newdata() || uip_acked() || uip_connected() || uip_poll()) { senddata(); } }
/*---------------------------------------------------------------------------*/ static char data_is_sent_and_acked(CC_REGISTER_ARG struct psock *s) { /* If data has previously been sent, and the data has been acked, we increase the send pointer and call send_data() to send more data. */ if(s->state != STATE_DATA_SENT || uip_rexmit()) { if(s->sendlen > uip_mss()) { uip_send(s->sendptr, uip_mss()); } else { uip_send(s->sendptr, s->sendlen); } s->state = STATE_DATA_SENT; return 0; } else if(s->state == STATE_DATA_SENT && uip_acked()) { if(s->sendlen > uip_mss()) { s->sendlen -= uip_mss(); s->sendptr += uip_mss(); } else { s->sendptr += s->sendlen; s->sendlen = 0; } s->state = STATE_ACKED; return 1; } return 0; }
/*-----------------------------------------------------------------------------------*/ void specserver_appcall(void) { static uint8_t reqs = 0; static char *data; if( uip_conn->lport != HTONS( SPECIAL_SERVER_PORT ) ) return; specserver_stats.frames++; if( uip_connected() ) { reqs = 0; } if( uip_newdata() ) { specserver_stats.recv++; char *p; // Process response data = (char*)uip_appdata; p = strchr( data, '\r' ); if( p != NULL ) *p = 0; p = strchr( data, '\n' ); if( p != NULL ) *p = 0; data[50] = 0; // Limit max size to one line or 50 char, whicever is smaller reqs++; } if( uip_acked() ) { } if( uip_rexmit() || uip_newdata() || uip_acked()) { // Send response if( reqs > 0 ) { reqs--; uip_send( specserver_resp_data, specserver_resp_data_len ); }else{ uip_close(); } } else if(uip_poll()) { uip_close(); } }
/** uIP stack application callback for the TELNET server. This function must be called each time the * TCP/IP stack needs a TCP packet to be processed. */ void TELNETServerApp_Callback(void) { uip_tcp_appstate_t* const AppState = &uip_conn->appstate; char* const AppData = (char*)uip_appdata; if (uip_connected()) { /* New connection - initialize connection state values */ AppState->TELNETServer.CurrentState = TELNET_STATE_SendHeader; } if (uip_acked()) { /* Progress to the next state once the current state's data has been ACKed */ AppState->TELNETServer.CurrentState = AppState->TELNETServer.NextState; } if (uip_rexmit() || uip_acked() || uip_newdata() || uip_connected() || uip_poll()) { switch (AppState->TELNETServer.CurrentState) { case TELNET_STATE_SendHeader: /* Copy over and send the TELNET welcome message upon first connection */ strcpy_P(AppData, WelcomeHeader); uip_send(AppData, strlen(AppData)); AppState->TELNETServer.NextState = TELNET_STATE_SendMenu; break; case TELNET_STATE_SendMenu: /* Copy over and send the TELNET menu to the client */ strcpy_P(AppData, TELNETMenu); uip_send(AppData, strlen(AppData)); AppState->TELNETServer.NextState = TELNET_STATE_GetCommand; break; case TELNET_STATE_GetCommand: if (!(uip_datalen())) break; /* Save the issued command for later processing */ AppState->TELNETServer.IssuedCommand = AppData[0]; AppState->TELNETServer.CurrentState = TELNET_STATE_SendResponse; break; case TELNET_STATE_SendResponse: /* Determine which command was issued, perform command processing */ switch (AppState->TELNETServer.IssuedCommand) { case 'c': TELNETServerApp_DisplayTCPConnections(); break; default: strcpy_P(AppData, PSTR("Invalid Command.\r\n")); uip_send(AppData, strlen(AppData)); break; } AppState->TELNETServer.NextState = TELNET_STATE_SendMenu; break; } } }
/** * Handle client communications */ void client_task_impl() { uip_tcp_appstate_t *app = &(uip_conn->appstate); GETrequest *req = (GETrequest*)app->request; if (uip_connected()) { if (verbose) { Serial.print("Connected to "); Serial.println(req->hostName); } app->ackedCount = 0; sendRequest(); } // Did we get an ack for the last packet? if (uip_acked()) { // Record the bytes that were successfully sent app->ackedCount += app->sentCount; app->sentCount = 0; // Check if we're done or need to send more content for this // request if (app->ackedCount != (int)app->cursor) { // Generate the post again to send the next packet of data sendRequest(); } } if (uip_rexmit()) { sendRequest(); } if (uip_newdata()) { setRXPin(HIGH); if (verbose) { Serial.print("RX "); Serial.print(uip_datalen()); Serial.print(" bytes from "); Serial.println(req->hostName); } // Check if the sketch cares about the returned data if ((req->returnFunc) && (uip_datalen() > 0)){ // Call the sketch's callback function req->returnFunc((char*)uip_appdata, uip_datalen()); } } if (uip_aborted() || uip_timedout() || uip_closed()) { if (req != NULL) { if (verbose) { Serial.print("Ended connection with "); Serial.println(req->hostName); } if (req->returnFunc) { // Call the sketch's callback function with 0 bytes to indicate End Of Data req->returnFunc((char*)uip_appdata, 0); } // Remove the request from the connection app->request = NULL; // Request is no longer active req->active = false; } } }
/* * Handles high-level server communications */ void server_task_impl() { // Get the connection's app state uip_tcp_appstate_t *app = &(uip_conn->appstate); if (uip_connected()) { if (verbose) { Serial.println("Server connected"); } // Initialize the server request data app->ackedCount = 0; app->request = NULL; } if (uip_newdata()) { setRXPin(HIGH); // Process the received packet and check if a valid GET request had been received if (processPacket((char*)uip_appdata, uip_datalen()) && app->request) { if (verbose) { Serial.print("Processing request for "); Serial.println((char*)app->request); } sendPage(); } } // Did we get an ack for the last packet? if (uip_acked()) { // Record the bytes that were successfully sent app->ackedCount += app->sentCount; app->sentCount = 0; // Check if we're done or need to send more content for this // request if (app->ackedCount == (int)app->cursor) { // Done with the current request and connection uip_close(); } else { // Generate the content again to send the next packet of data sendPage(); } } // Check if we need to retransmit if (uip_rexmit()) { // Send the same data again (same ackedCount value) sendPage(); } if (uip_aborted() || uip_closed() || uip_timedout()) { // Check if a URL was stored for this connection if (app->request != NULL) { if (verbose) { Serial.println("Server connection closed"); } // Free RAM and clear the pointer free(app->request); app->request = NULL; } } }
void elua_uip_appcall() { volatile struct elua_uip_state *s; elua_net_size temp; int sockno; // If uIP is not yet configured (DHCP response not received), do nothing if( !elua_uip_configured ) return; s = ( struct elua_uip_state* )&( uip_conn->appstate ); // Need to find the actual socket location, since UIP doesn't provide this ... for( temp = 0; temp < UIP_CONNS; temp ++ ) if( uip_conns + temp == uip_conn ) break; sockno = ( int )temp; if( uip_connected() ) { #ifdef BUILD_CON_TCP if( uip_conn->lport == HTONS( ELUA_NET_TELNET_PORT ) ) // special case: telnet server { if( elua_uip_telnet_socket != -1 ) { uip_close(); return; } else elua_uip_telnet_socket = sockno; } else #endif if( elua_uip_accept_request ) { elua_uip_accept_sock = sockno; elua_uip_accept_remote.ipwords[ 0 ] = uip_conn->ripaddr[ 0 ]; elua_uip_accept_remote.ipwords[ 1 ] = uip_conn->ripaddr[ 1 ]; elua_uip_accept_request = 0; } else if( s->state == ELUA_UIP_STATE_CONNECT ) s->state = ELUA_UIP_STATE_IDLE; uip_stop(); return; } if( s->state == ELUA_UIP_STATE_IDLE ) return; // Do we need to read? if( s->state == ELUA_UIP_STATE_RECV ) { // Re-enable data transfer on the socket and change state s->state = ELUA_UIP_STATE_RECV_2; uip_restart(); return; } if( uip_aborted() || uip_timedout() || uip_closed() ) { // Signal this error s->res = uip_aborted() ? ELUA_NET_ERR_ABORTED : ( uip_timedout() ? ELUA_NET_ERR_TIMEDOUT : ELUA_NET_ERR_CLOSED ); #ifdef BUILD_CON_TCP if( sockno == elua_uip_telnet_socket ) elua_uip_telnet_socket = -1; #endif s->state = ELUA_UIP_STATE_IDLE; return; } // Handle data send if( ( uip_acked() || uip_rexmit() || uip_poll() ) && ( s->state == ELUA_UIP_STATE_SEND ) ) { // Special translation for TELNET: prepend all '\n' with '\r' // We write directly in UIP's buffer if( uip_acked() ) { elua_net_size minlen = UMIN( s->len, uip_mss() ); s->len -= minlen; s->ptr += minlen; if( s->len == 0 ) s->state = ELUA_UIP_STATE_IDLE; } if( s->len > 0 ) // need to (re)transmit? { #ifdef BUILD_CON_TCP if( sockno == elua_uip_telnet_socket ) { temp = elua_uip_telnet_prep_send( s->ptr, s->len ); uip_send( uip_sappdata, temp ); } else #endif uip_send( s->ptr, UMIN( s->len, uip_mss() ) ); } return; } // Handle close if( s->state == ELUA_UIP_STATE_CLOSE ) { uip_close(); s->state = ELUA_UIP_STATE_IDLE; return; } // Handle data receive if( uip_newdata() ) { if( s->state == ELUA_UIP_STATE_RECV_2 ) { #ifdef BUILD_CON_TCP if( sockno == elua_uip_telnet_socket ) { elua_uip_telnet_handle_input( s ); return; } #endif int lastfound = 0; // Check end of transmission if( uip_datalen() < UIP_RECEIVE_WINDOW ) lastfound = 1; // Check overflow if( s->len < uip_datalen() ) { s->res = ELUA_NET_ERR_OVERFLOW; temp = s->len; } else temp = uip_datalen(); if( s->readto != ELUA_NET_NO_LASTCHAR ) { char *tptr = ( char* )uip_appdata; char *last = ( char* )uip_appdata + temp - 1; luaL_Buffer *pbuf = ( luaL_Buffer* )s->ptr; char* dest = ( char* )s->ptr; while( tptr <= last ) { if( *tptr == s->readto ) { lastfound = 1; break; } if( *tptr != '\r' ) { if( s->res ) luaL_addchar( pbuf, *tptr ); else *dest ++ = *tptr; s->len --; } tptr ++; } } else { if( s->res ) luaL_addlstring( ( luaL_Buffer* )s->ptr, ( const char* )uip_appdata, temp ); else memcpy( ( char* )s->ptr, ( const char* )uip_appdata, temp ); s->len -= temp; } // Do we need to read another packet? if( s->len == 0 || lastfound ) { uip_stop(); s->res = ELUA_NET_ERR_OK; s->state = ELUA_UIP_STATE_IDLE; } } else uip_stop(); } }