/*---------------------------------------------------------------------------*/ static PT_THREAD(processes(struct httpd_state *s, char *ptr)) { PSOCK_BEGIN(&s->sout); for(s->u.ptr = PROCESS_LIST(); s->u.ptr != NULL; s->u.ptr = ((struct process *)s->u.ptr)->next) { PSOCK_GENERATOR_SEND(&s->sout, make_processes, s->u.ptr); } PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(addresses(struct httpd_state *s, char *ptr)) { PSOCK_BEGIN(&s->sout); PSOCK_GENERATOR_SEND(&s->sout, make_addresses, s->u.ptr); PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(file_stats(struct httpd_state *s, char *ptr)) { PSOCK_BEGIN(&s->sout); PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, strchr(ptr, ' ') + 1); PSOCK_END(&s->sout); }
static PT_THREAD(send_part_of_file(struct httpd_state *s)) { PSOCK_BEGIN(&s->sout); PSOCK_GENERATOR_SEND(&s->sout, generate_len_part_of_file, s); PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(send_part_of_file(struct httpd_state *s)) { PSOCK_BEGIN(&s->sout); PSOCK_SEND(&s->sout, s->file.data, s->len); PSOCK_END(&s->sout); }
static PT_THREAD(sensorscall(struct httpd_state *s, char *ptr)) { static struct timer t; static int i; static char buf[100]; static unsigned long last_cpu, last_lpm, last_listen, last_transmit; PSOCK_BEGIN(&s->sout); timer_set(&t, CLOCK_SECOND); i = 0; /* while(1)*/ { /* timer_restart(&t); PSOCK_WAIT_UNTIL(&s->sout, timer_expired(&t));*/ #if CONTIKI_TARGET_SKY SENSORS_ACTIVATE(sht11_sensor); SENSORS_ACTIVATE(light_sensor); snprintf(buf, sizeof(buf), "t(%d);h(%d);l1(%d);l2(%d);", sht11_sensor.value(SHT11_SENSOR_TEMP), sht11_sensor.value(SHT11_SENSOR_HUMIDITY), light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC), light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR)); SENSORS_DEACTIVATE(sht11_sensor); SENSORS_DEACTIVATE(light_sensor); #else /* CONTIKI_TARGET_SKY */ snprintf(buf, sizeof(buf), "t(%d);h(%d);l1(%d);l2(%d);", 0, 0, 0, 0); #endif /* CONTIKI_TARGET_SKY */ PSOCK_SEND_STR(&s->sout, buf); /* timer_restart(&t); PSOCK_WAIT_UNTIL(&s->sout, timer_expired(&t));*/ snprintf(buf, sizeof(buf), "p(%lu,%lu,%lu,%lu);i(%d);", energest_type_time(ENERGEST_TYPE_CPU) - last_cpu, energest_type_time(ENERGEST_TYPE_LPM) - last_lpm, energest_type_time(ENERGEST_TYPE_TRANSMIT) - last_transmit, energest_type_time(ENERGEST_TYPE_LISTEN) - last_listen, i++); last_cpu = energest_type_time(ENERGEST_TYPE_CPU); last_lpm = energest_type_time(ENERGEST_TYPE_LPM); last_transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT); last_listen = energest_type_time(ENERGEST_TYPE_LISTEN); PSOCK_SEND_STR(&s->sout, buf); } PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) { PSOCK_BEGIN(&s->sout); SEND_STRING(&s->sout, statushdr); SEND_STRING(&s->sout, get_content_type(s->filename)); PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(file_stats(struct httpd_state *s, char *ptr)) { //vSemaphoreCreateBinary( mapStruct.SEMForTotalDistance); PSOCK_BEGIN(&s->sout); PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, strchr(ptr, ' ') + 1); PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(header(struct httpd_state *s, char *ptr)) { PSOCK_BEGIN(&s->sout); PSOCK_GENERATOR_SEND(&s->sout, generate_header, (void *) ptr); PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(nodeidcall(struct httpd_state *s, char *ptr)) { static char buf[10]; PSOCK_BEGIN(&s->sout); snprintf(buf, sizeof(buf), "%d.%d", linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]); PSOCK_SEND_STR(&s->sout, buf); PSOCK_END(&s->sout); }
static PT_THREAD(handle_output(contiki_data_t *s)) { PSOCK_BEGIN(&s->p); if (s->state == WRITTING) { PSOCK_SEND(&s->p, s->out_buf, s->out_len); s->state = WRITE_END; process_post_synch(s->process, xively_event, s); } PSOCK_END(&s->p); }
/* ======================================================================================================================= ======================================================================================================================= */ static PT_THREAD(handle_input (struct httpd_state *s)) { PSOCK_BEGIN(&s->sin); PSOCK_READTO(&s->sin, ISO_space); if(strncmp(s->inputbuf, http_get, 4) != 0) { PSOCK_CLOSE_EXIT(&s->sin); } PSOCK_READTO(&s->sin, ISO_space); if(s->inputbuf[0] != ISO_slash) { PSOCK_CLOSE_EXIT(&s->sin); } if(s->inputbuf[1] == ISO_space) { strncpy(s->filename, http_index_html, sizeof(s->filename)); } else { s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0; /* Process any form input being sent to the server. */ { /* * extern void vApplicationProcessFormInput( char *pcInputString, long * xInputLength ); £ * vApplicationProcessFormInput( s->inputbuf, PSOCK_DATALEN(&s->sin) ); */ } strncpy(s->filename, &s->inputbuf[0], sizeof(s->filename)); } /* httpd_log_file(uip_conn->ripaddr, s->filename); */ s->state = STATE_OUTPUT; while(1) { PSOCK_READTO(&s->sin, ISO_nl); if(strncmp(s->inputbuf, http_referer, 8) == 0) { s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0; /* httpd_log(&s->inputbuf[9]); */ } } PSOCK_END(&s->sin); }
static PT_THREAD(send_file(struct httpd_state *s)) { PSOCK_BEGIN(&s->sout); do { PSOCK_GENERATOR_SEND(&s->sout, generate_part_of_file, s); s->file.len -= s->len; s->file.data += s->len; } while(s->file.len > 0); PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(file_stats(struct httpd_state *s, char *ptr)) { PSOCK_BEGIN(&s->sout); //while (pgm_read_byte(ptr++)!=' ') {}; //skip to "/filename" after the script invokation PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, (void *) (strchr_P(ptr, ' ') + 1)); PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD( handle_input ( struct httpd_state *s ) ) { PSOCK_BEGIN( &s->sin ); ( void ) PT_YIELD_FLAG; PSOCK_READTO( &s->sin, ISO_space ); if( strncmp(s->inputbuf, http_get, 4) != 0 ) { PSOCK_CLOSE_EXIT( &s->sin ); } PSOCK_READTO( &s->sin, ISO_space ); if( s->inputbuf[0] != ISO_slash ) { PSOCK_CLOSE_EXIT( &s->sin ); } if( s->inputbuf[1] == ISO_space ) { strncpy( s->filename, http_index_html, sizeof(s->filename) ); } else { s->inputbuf[PSOCK_DATALEN( &s->sin ) - 1] = 0; /* Process any form input being sent to the server. */ #if UIP_CONF_PROCESS_HTTPD_FORMS == 1 { extern void vApplicationProcessFormInput( char *pcInputString ); vApplicationProcessFormInput( s->inputbuf ); } #endif strncpy( s->filename, &s->inputbuf[0], sizeof(s->filename) ); } /* httpd_log_file(uip_conn->ripaddr, s->filename);*/ s->state = STATE_OUTPUT; while( 1 ) { PSOCK_READTO( &s->sin, ISO_nl ); if( strncmp(s->inputbuf, http_referer, 8) == 0 ) { s->inputbuf[PSOCK_DATALEN( &s->sin ) - 2] = 0; /* httpd_log(&s->inputbuf[9]);*/ } } PSOCK_END( &s->sin ); }
static PT_THREAD(lights_list(struct httpd_state *s, char *)) { PSOCK_BEGIN(&s->sout); for(s->count = 0; lights[s->count].is_valid_P(); ++s->count) { PSOCK_GENERATOR_SEND(&s->sout, generate_lights_list, s); } PSOCK_END(&s->sout); }
//const char http_content_type_json[] = "Content-type:application/json\n\n"; static PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) { PSOCK_BEGIN(&s->sout); SEND_STRING(&s->sout, statushdr); //SEND_STRING(&s->sout, http_content_type_json); PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(rplreset(struct httpd_state *s, char *ptr)) { PSOCK_BEGIN(&s->sout); if(rpl_repair_root(RPL_DEFAULT_INSTANCE)) { PSOCK_SEND_STR(&s->sout, "Initiating global repair of RPL network..."); } else { PSOCK_SEND_STR(&s->sout, "Could not start global network repair"); } PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD( tcp_stats ( struct httpd_state *s, char *ptr ) ) { PSOCK_BEGIN( &s->sout ); for( s->count = 0; s->count < UIP_CONNS; ++s->count ) { if( (uip_conns[s->count].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED ) { PSOCK_GENERATOR_SEND( &s->sout, generate_tcp_stats, s ); } } PSOCK_END( &s->sout ); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(file_stats(struct httpd_state *s, char *ptr)) { PSOCK_BEGIN(&s->sout); thisfilename=&s->filename[0]; //temporary way to pass filename to generate_file_stats PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, (void *) ptr); PSOCK_END(&s->sout); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_input(struct httpd_state *s)) { char *LEDptr; PSOCK_BEGIN(&s->sin); PSOCK_READTO(&s->sin, ISO_space); if(strncmp(s->inputbuf, http_get, 4) == 0) { PSOCK_READTO(&s->sin, ISO_space); if(s->inputbuf[0] != ISO_slash) { PSOCK_CLOSE_EXIT(&s->sin); } if(s->inputbuf[1] == ISO_space) { strncpy(s->filename, http_index_html, sizeof(s->filename)); } else { s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0; strncpy(s->filename, &s->inputbuf[0], sizeof(s->filename)); } // Renesas ++ LEDptr = strstr(s->inputbuf, "LEDA"); if (LEDptr !=NULL) { strncpy(LEDbuf, (const char *)(LEDptr), sizeof(LEDbuf)); LEDflag = 1; } // End of Renesas ++ /* httpd_log_file(uip_conn->ripaddr, s->filename);*/ s->state = STATE_OUTPUT; while(1) { PSOCK_READTO(&s->sin, ISO_nl); if(strncmp(s->inputbuf, http_referer, 8) == 0) { s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0; /* httpd_log(&s->inputbuf[9]);*/ } } }else { PSOCK_CLOSE_EXIT(&s->sin); } PSOCK_END(&s->sin); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(sensor_readings(struct httpd_state *s, char *ptr)) { PSOCK_BEGIN(&s->sout); PSOCK_GENERATOR_SEND(&s->sout, generate_sensor_readings, s); #if RADIOSTATS PSOCK_GENERATOR_SEND(&s->sout, generate_radio_stats, s); #endif PSOCK_END(&s->sout); }
static int handle_tcp_connection(struct psock *p, enum tcp_event_type type, struct net_buf *buf) { PSOCK_BEGIN(p); if (type == TCP_WRITE_EVENT) { NET_DBG("Trying to send %d bytes data\n", uip_appdatalen(buf)); PSOCK_SEND(p, buf); } PSOCK_END(p); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(file_stats(struct httpd_state *s, char *ptr)) { PSOCK_BEGIN(&s->sout); /* Pass string after cgi invocation to the generator */ s->u.ptr = ptr; PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, s); PSOCK_END(&s->sout); }
static PT_THREAD(handle_input(struct httpd_state *s)) { PSOCK_BEGIN(&s->sin); PSOCK_READTO(&s->sin, ISO_space); if(strncmp(s->inputbuf, http_get, 4) == 0) { s->request_type = REQUEST_TYPE_GET; } else if(strncmp(s->inputbuf, http_put, 4) == 0) { s->request_type = REQUEST_TYPE_PUT; } else if(strncmp(s->inputbuf, http_post, 5) == 0) { s->request_type = REQUEST_TYPE_POST; } else if(strncmp(s->inputbuf, http_delete, 7) == 0) { s->request_type = REQUEST_TYPE_DELETE; } else { PSOCK_CLOSE_EXIT(&s->sin); } PSOCK_READTO(&s->sin, ISO_space); if(s->inputbuf[0] != ISO_slash) { PSOCK_CLOSE_EXIT(&s->sin); } s->query = NULL; #if URLCONV s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0; urlconv_tofilename(s->filename, &s->query, s->inputbuf, sizeof(s->filename)); if(s->filename[1] == 0) { strncpy(s->filename, http_index_html, sizeof(s->filename)); } #else /* URLCONV */ if(s->inputbuf[1] == ISO_space) { strncpy(s->filename, http_index_html, sizeof(s->filename)); } else { s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0; strncpy(s->filename, s->inputbuf, sizeof(s->filename)); } #endif /* URLCONV */ if(s->query) { LOG6LBR_6ADDR(DEBUG, &uip_conn->ripaddr, "Request for '%s?%s' from ", s->filename, s->query); } else { LOG6LBR_6ADDR(DEBUG, &uip_conn->ripaddr, "Request for '%s' from ", s->filename); } s->state = STATE_OUTPUT; while(1) { PSOCK_READTO(&s->sin, ISO_nl); } PSOCK_END(&s->sin); }
/*---------------------------------------------------------------------------*/ static int handle_connection(struct psock *p) { PSOCK_BEGIN(p); struct conn_state *s; s->state = send1; PSOCK_SEND_STR(p, "Connection Request"); while(1) { if(s->state == send1) { memset(buffer,0,sizeof(buffer)); PSOCK_READTO(p, '\n'); printf("Got: %s", buffer); printf("Server is Authenticated \n"); s->state = send2; } if(s->state == send2) { PSOCK_SEND_STR(p,"certificate\n"); s->state = send3; } if(s->state == send3) { memset(buffer,0,sizeof(buffer)); PSOCK_READTO(p, '\n'); printf("Got: %s", buffer); printf("I received servers shared key \n"); s->state = send4; } if(s->state == send4) { printf("I accepted servers shared key \n"); s->state = send5; PSOCK_SEND_STR(p,"shared key accepted\n"); } if(s->state == send5) { memset(buffer,0,sizeof(buffer)); PSOCK_READTO(p, '\n'); printf("Got : %s", buffer); printf("Secure Connection Established\n"); PSOCK_SEND_STR(p,"Connected\n"); printf("Connected Sent\n"); } } PSOCK_END(p); }
/* * A protosocket always requires a protothread. The protothread * contains the code that uses the protosocket. We define the * protothread here. */ static PT_THREAD(handle_connection(struct psock *p)) { /* * A protosocket's protothread must start with a PSOCK_BEGIN(), with * the protosocket as argument. * * Remember that the same rules as for protothreads apply: do NOT * use local variables unless you are very sure what you are doing! * Local (stack) variables are not preserved when the protothread * blocks. */ PSOCK_BEGIN(p); /* * We start by sending out a welcoming message. The message is sent * using the PSOCK_SEND_STR() function that sends a null-terminated * string. */ PSOCK_SEND_STR(p, "Welcome, please type something and press return.\n"); /* * Next, we use the PSOCK_READTO() function to read incoming data * from the TCP connection until we get a newline character. The * number of bytes that we actually keep is dependant of the length * of the input buffer that we use. Since we only have a 10 byte * buffer here (the buffer[] array), we can only remember the first * 10 bytes received. The rest of the line up to the newline simply * is discarded. */ PSOCK_READTO(p, '\n'); /* * And we send back the contents of the buffer. The PSOCK_DATALEN() * function provides us with the length of the data that we've * received. Note that this length will not be longer than the input * buffer we're using. */ PSOCK_SEND_STR(p, "Got the following data: "); PSOCK_SEND(p, buffer, PSOCK_DATALEN(p)); PSOCK_SEND_STR(p, "Good bye!\r\n"); /* * We close the protosocket. */ PSOCK_CLOSE(p); /* * And end the protosocket's protothread. */ PSOCK_END(p); }
static int handle_connection(struct webserver_state *s) { PSOCK_BEGIN(&s->p); /* the incoming GET request will have the following format: * GET / HTTP/1.1 .... * we have to parse this string to determine the resource being requested * if the requested resource is not the root webpage ('/') then, * GET /<resource name> HTTP/1.1 .... * we should parse the specific resource and react appropriately */ // read incoming data until we read a space character PSOCK_READTO(&s->p, ISO_space); // parse the data to determine if it was a GET request if(strncmp(s->inputbuf, http_get, 4) != 0) { PSOCK_CLOSE_EXIT(&s->p); } // continue reading until the next space character PSOCK_READTO(&s->p, ISO_space); // determine the requested resource // in this case, we check if the request was for the '/' root page // AKA index.html if(s->inputbuf[0] != ISO_slash) { // request for unknown webpage, close and exit PSOCK_CLOSE_EXIT(&s->p); } // This is the web page served by the webserver PSOCK_SEND_STR(&s->p, "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n"); PSOCK_SEND_STR(&s->p, "<HTML>\r\n<HEAD>\r\n\t<META http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">\r\n\t<TITLE>Arduino WiShield 2.0</TITLE>\r\n"); PSOCK_SEND_STR(&s->p, "\t<STYLE type=\"text/css\">\r\n\t\tP,H1 {text-align: center;}\r\n\t</STYLE>\r\n</HEAD>\r\n<BODY>\r\n"); PSOCK_SEND_STR(&s->p, "\t<H1>Hello World!! I am an Arduino with a WiShield 2.0</H1>\r\n\t<P><A href=\"T\">Toggle LED</A></P>\r\n"); if(s->inputbuf[1] != ISO_space) { // request for a resource if(s->inputbuf[1] != ISO_T) { // not supported PSOCK_SEND_STR(&s->p, "\t<P>Request not supported</P>\r\n"); } else { PSOCK_SEND_STR(&s->p, "\t<P>TOGGLE LED command received</P>\r\n"); // Toggle WiShield 2.0 Led status digitalWrite(9, !digitalRead(9)); } } PSOCK_SEND_STR(&s->p, "</BODY>\r\n</HTML>"); PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }
static PT_THREAD( net_stats ( struct httpd_state *s, char *ptr ) ) { PSOCK_BEGIN( &s->sout ); #if UIP_STATISTICS for( s->count = 0; s->count < sizeof(uip_stat) / sizeof(uip_stats_t); ++s->count ) { PSOCK_GENERATOR_SEND( &s->sout, generate_net_stats, s ); } #endif /* UIP_STATISTICS */ PSOCK_END( &s->sout ); }
/* * This is the protosocket function that handles the communication. A * protosocket function must always return an int, but must never * explicitly return - all return statements are hidden in the PSOCK * macros. */ static int handle_connection(struct socket_app_state *s) { PSOCK_BEGIN(&s->p); PSOCK_SEND_STR(&s->p, "Hello. What is you name?\n"); PSOCK_READTO(&s->p, '\n'); PSOCK_SEND_STR(&s->p, "Hello "); PSOCK_SEND_STR(&s->p, s->inputbuffer); memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer)); PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }