Exemple #1
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void *state)
{
  struct httpd_state *s = (struct httpd_state *)state;

  if(uip_closed() || uip_aborted() || uip_timedout()) {
    if(s != NULL) {
      memb_free(&conns, s);
    }
  } else if(uip_connected()) {
    s = (struct httpd_state *)memb_alloc(&conns);
    if(s == NULL) {
      uip_abort();
      return;
    }
    tcp_markconn(uip_conn, s);
    PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->state = STATE_WAITING;
    timer_set(&s->timer, CLOCK_SECOND * 10);
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      if(timer_expired(&s->timer)) {
	uip_abort();
      }
    } else {
      timer_reset(&s->timer);
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}
Exemple #2
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void)
{
  struct httpd_state *s = (struct httpd_state *)&(uip_conn->appstate);

  if(uip_closed() || uip_aborted() || uip_timedout()) {
  } else if(uip_connected()) {
    PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->state = STATE_WAITING;
    /*    timer_set(&s->timer, CLOCK_SECOND * 100);*/
    s->timer = 0;
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      ++s->timer;
      if(s->timer >= 20) {
	uip_abort();
      }
    } else {
      s->timer = 0;
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}
Exemple #3
0
/*---------------------------------------------------------------------------*/
static struct uip_conn *
connect(u16_t *host, u16_t port)
{
  PSOCK_INIT(&s.sin, s.inputbuf, sizeof(s.inputbuf));
  PSOCK_INIT(&s.sout, s.inputbuf, sizeof(s.inputbuf));
  PT_INIT(&s.inpt);
  PT_INIT(&s.outpt);
  return tcp_connect(host, uip_htons(port), NULL);
}
Exemple #4
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void *state)
{
#if DEBUGLOGIC
  struct httpd_state *s;   //Enter here for debugging with output directed to TCPBUF
  s = sg = (struct httpd_state *)memb_alloc(&conns);
  if (1) {
#else
  struct httpd_state *s = (struct httpd_state *)state;
  if(uip_closed() || uip_aborted() || uip_timedout()) {
    if(s != NULL) {
      memb_free(&conns, s);
    }
  } else if(uip_connected()) {
    s = (struct httpd_state *)memb_alloc(&conns);
    if(s == NULL) {
      uip_abort();
      return;
    }
#endif
    tcp_markconn(uip_conn, s);
    PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->state = STATE_WAITING;
    /*    timer_set(&s->timer, CLOCK_SECOND * 100);*/
    s->timer = 0;
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      ++s->timer;
      if(s->timer >= 20) {
        uip_abort();
        memb_free(&conns, s);
      }
    } else {
      s->timer = 0;
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}
/*---------------------------------------------------------------------------*/
void
httpd_init(void)
{
  tcp_listen(UIP_HTONS(80));
  memb_init(&conns);
  httpd_cgi_init();
}
Exemple #5
0
/*
 * The definition of the process.
 */
PROCESS_THREAD(sensor_psock_server_process, ev, data)
{
    PROCESS_BEGIN();

    tcp_listen(UIP_HTONS(1010));

    while(1) {

        PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

        if(uip_connected()) {

            PSOCK_INIT(&ps, buffer, sizeof(buffer));

            while(!(uip_aborted() || uip_closed() || uip_timedout())) {

                PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
                handle_connection(&ps);
            }
            PSOCK_CLOSE(&ps);
        }
    }

    PSOCK_CLOSE_EXIT(&ps);
    PROCESS_END();
}
Exemple #6
0
static int hello_world_appcall(void)
{
    struct tcp_hello_appstate *s = &(uip_conn->appstate.tcp_hello);

    if(uip_connected()) {
      PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));
    }

    PSOCK_BEGIN(&s->p);
    PSOCK_SEND_STR(&s->p, "Hello. What is your name?\n");
    PSOCK_READTO(&s->p, '\n');
    strncpy(s->name, s->inputbuffer, sizeof(s->name));
    s->name[strlen(s->name)-2] = 0;
    PSOCK_SEND_STR(&s->p, "Hello ");
    PSOCK_SEND_STR(&s->p, s->name);
    PSOCK_SEND_STR(&s->p, " !\n");
    sprintf(s->name, "r %d/%d", nrf_link_get_rx_packets(), nrf_link_get_rx_dropped());
    PSOCK_SEND_STR(&s->p, s->name);
    PSOCK_SEND_STR(&s->p, " ");
    sprintf(s->name, "t %d/%d", nrf_link_get_tx_packets(), nrf_link_get_tx_dropped());
    PSOCK_SEND_STR(&s->p, s->name);
    PSOCK_SEND_STR(&s->p, "\n\r");
    PSOCK_CLOSE(&s->p);
    PSOCK_END(&s->p);
}
static void contiki_appcall(void *data)
{
	contiki_data_t *s = (contiki_data_t *)data;

	if (uip_closed() || uip_aborted() || uip_timedout()) {
		//xprintf("closed or aborted or timedout\n");
		if (s->state == READING || s->state == WRITTING) {
			s->state = ERROR;
		} else {
			s->state = CLOSED;
		}
		process_post_synch(s->process, xively_event, s);
	} else if (uip_connected()) {
		s->state = CONNECTED;
		PSOCK_INIT(&s->p, NULL, 0);
		process_post_synch(s->process, xively_event, s);
	} else if (uip_newdata()) {
		if (s->state == READING) {
			s->state = READ_END;
			process_post_synch(s->process, xively_event, s);
		}
	}
	if (s->state == CLOSING) {
		uip_close();
	}
	handle_output(s);
}
Exemple #8
0
/*
 * In socketapp.h we have defined the UIP_APPCALL macro to
 * socket_app_appcall so that this function is uIP's application
 * function. This function is called whenever an 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 socket_app_appcall()
{
  /*
   * The uip_conn structure has a field called "appstate" that holds
   * the application state of the connection. We make a pointer to
   * this to access it easier.
   */
  struct socket_app_state *s = &(uip_conn->appstate);

//  Serial.print("uip_flags:");
//  Serial.println(uip_flags);
//  Serial.print("psock state:");
//  Serial.println((&s->p)->state);

  /*
   * If a new connection was just established, we should initialize
   * the protosocket in our applications' state structure.
   */
  if(uip_connected()) {
	Serial.println("connected");
    PSOCK_INIT(&s->p, (unsigned char*)(s->inputbuffer), sizeof(s->inputbuffer));
//    Serial.println("psock_init");
  }

  /*
   * Finally, we run the protosocket function that actually handles
   * the communication. We pass it a pointer to the application state
   * of the current connection.
   */
  Serial.println("before handle_connections");
  handle_connection(s);
  Serial.println("after handle_connections");

}
Exemple #9
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_psock_client_process, ev, data)
{
  uip_ipaddr_t addr;

  printf("%d\n", TEST);
  
  PROCESS_BEGIN();

  uip_ipaddr(addr, 192,168,2,1);
  tcp_connect(addr, UIP_HTONS(80), NULL);

  printf("Connecting...\n");
  PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

  if(uip_aborted() || uip_timedout() || uip_closed()) {
    printf("Could not establish connection\n");
  } else if(uip_connected()) {
    printf("Connected\n");
    
    PSOCK_INIT(&ps, buffer, sizeof(buffer));

    do {
      handle_connection(&ps);
      PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
    } while(!(uip_closed() || uip_aborted() || uip_timedout()));

    printf("\nConnection closed.\n");
  }
  PROCESS_END();
}
Exemple #10
0
/*
 * In socketapp.h we have defined the UIP_APPCALL macro to
 * socket_app_appcall so that this function is uIP's application
 * function. This function is called whenever an 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 socket_app_appcall(void)
{
  /*
   * The uip_conn structure has a field called "appstate" that holds
   * the application state of the connection. We make a pointer to
   * this to access it easier.
   */
  struct socket_app_state *s = &(uip_conn->appstate);

  /*
   * If a new connection was just established, we should initialize
   * the protosocket in our applications' state structure.
   */
  if(uip_connected()) {
    //memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer));
    PSOCK_INIT(&s->p, (uint8_t*)s->inputbuffer, 
	SOCKET_BUFFER_LENGTH
//	sizeof((uint8_t*)s->inputbuffer)
    );
  }

  /*
   * Finally, we run the protosocket function that actually handles
   * the communication. We pass it a pointer to the application state
   * of the current connection.
   */
  handle_connection(s);
}
Exemple #11
0
 /*
  * In hello-world.h we have defined the UIP_APPCALL macro to
  * hello_world_appcall so that this funcion is uIP's application
  * function. This function is called whenever an 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
 hello_world_appcall(void)
 {
   /*
    * The uip_conn structure has a field called "appstate" that holds
    * the application state of the connection. We make a pointer to
    * this to access it easier.
    */
   struct hello_world_state *s = &(uip_conn->appstate);
 
   /*
    * If a new connection was just established, we should initialize
    * the protosocket in our applications' state structure.
    */
   if(uip_connected()) {
     PSOCK_INIT(&p, s->inputbuffer, sizeof(s->inputbuffer));
   }
 
   /*
    * Finally, we run the protosocket function that actually handles
    * the communication. We pass it a pointer to the application state
    * of the current connection.
    */
   handle_connection(s);
 }
Exemple #12
0
PROCESS_THREAD(server_process, ev, data)
{
  PROCESS_BEGIN();

  set_global_address();
  leds_init();
  print_local_addresses();
  printf("Starting TCP server on port=%d\n", PORT);
  tcp_listen(UIP_HTONS(PORT));

  while(1) {
    PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

    if(uip_aborted() )
	printf("TCP aborted\n");
    if(uip_timedout() )
	printf("TCP timeoutn\n");
    if(uip_closed() )
	printf("TCP closed\n");

    if(uip_connected()) {
      printf("TCP Connected\n\r");
      PSOCK_INIT(&ps, buf, sizeof(buf));

      while(!(uip_aborted() || uip_closed() || uip_timedout())) {
	PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
	handle_connection(&ps);
      }
    }
  }
  PROCESS_END();
}
Exemple #13
0
/* -------------------------------------------------------------------------- */
PROCESS_THREAD(example_psock_server_process, ev, data)
{
    // The Start...
    PROCESS_BEGIN();

    // So, here we configure the network listening at a TCP port.
    tcp_listen(UIP_HTONS(12345));

    while (1) {
        // Wait a TCP / IP event...
        PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

        // If someone has connected, let's init the connection
        if (uip_connected()) {
            // Bind the buffer to the socket
            PSOCK_INIT(&socket, buffer, sizeof(buffer));

            // So now, let's keep the connection open waiting for something
            while (!(uip_aborted() || uip_closed() || uip_timedout())) {
                // Wait for an TCIP / IP event
                PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

                // And then, let's send to handle the data
                handle_connection(&socket);
            }
        }
    }

    // ... the End
    PROCESS_END();
}
Exemple #14
0
PROCESS_THREAD(tcp_process, ev, data)
{
  static char incoming[10];
  static struct psock ps;

  PROCESS_BEGIN();

  uart0_set_br(1000000);
  tcp_listen(UIP_HTONS(2020));

  while(1) {
    PROCESS_YIELD();
  }

  {
    /* wait for tcp connection */
    PROCESS_WAIT_EVENT_UNTIL(ev==tcpip_event && uip_connected());

    /* start estimator process and init psock connection handler */
    process_start(&ahrs_process, NULL);
    PSOCK_INIT(&ps,incoming,sizeof(incoming));

    /* loop until connection is closed */
    while (!(uip_aborted() || uip_closed() || uip_timedout())) {
      PROCESS_YIELD_UNTIL(ev==tcpip_event);
      handle_connection(&ps);
    }

    /* stop ahrs process */
    process_exit(&ahrs_process);

  }

  PROCESS_END();
}
Exemple #15
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void *state)
{
  struct httpd_state *s = (struct httpd_state *)state;

  if(uip_closed() || uip_aborted() || uip_timedout()) {
    if(s != NULL) {
      if(s->fd >= 0) {
        cfs_close(s->fd);
	s->fd = -1;
      }
      memb_free(&conns, s);
    }
  } else if(uip_connected()) {
    s = (struct httpd_state *)memb_alloc(&conns);
    if(s == NULL) {
      uip_abort();
      webserver_log_file(&uip_conn->ripaddr, "reset (no memory block)");
      return;
    }
    tcp_markconn(uip_conn, s);
    PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->fd = -1;
    s->state = STATE_WAITING;
    timer_set(&s->timer, CLOCK_SECOND * 10);
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      if(timer_expired(&s->timer)) {
	uip_abort();
	if(s->fd >= 0) {
	  cfs_close(s->fd);
	  s->fd = -1;
	}
        memb_free(&conns, s);
        webserver_log_file(&uip_conn->ripaddr, "reset (timeout)");
      }
    } else {
      timer_restart(&s->timer);
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}
Exemple #16
0
void tdtp_appcall(void)
{
        struct tdtp_state *t = &(uip_conn->appstate);
        if(uip_connected()) {
                PSOCK_INIT(&t->p, (uint8_t *)t->inputbuffer, sizeof(t->inputbuffer));
        }
        handle_tdtp_connection(t);
}
Exemple #17
0
void socket_app_appcall(void) {
  struct socket_app_state *s = &(uip_conn->appstate);

  if(uip_connected()) {
    PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));
  }

  handle_connection(s);
}
Exemple #18
0
void webserver_appcall() {
  webserver_state* s = &(uip_conn->appstate);

  if(uip_connected()) {
    PSOCK_INIT(&s->p, (unsigned char*)s->inputbuf, sizeof(s->inputbuf));
  }

  handle_connection(s);
}
Exemple #19
0
void webserver_appcall(void) {
  struct webserver_state *s = &(uip_conn->appstate);
  
  if(uip_connected()) {
    PSOCK_INIT(&s->p, s->inputbuf, sizeof(s->inputbuf));
  }
  
  handle_connection(s);
}
void simple_httpd_appcall(void)
{
	struct simple_httpd_state *s = &(uip_conn->appstate);

	if(uip_connected()) {
		PSOCK_INIT(&s->p, s->inputbuf, sizeof(s->inputbuf));
	}

  handle_connection(s);

}
Exemple #21
0
/**
 * Send an e-mail.
 *
 * \param to The e-mail address of the receiver of the e-mail.
 * \param cc The e-mail address of the CC: receivers of the e-mail.
 * \param from The e-mail address of the sender of the e-mail.
 * \param subject The subject of the e-mail.
 * \param msg The actual e-mail message.
 * \param msglen The length of the e-mail message.
 */
unsigned char
smtp_send(char *to, char *cc, char *from,
	  char *subject, char *msg, u16_t msglen)
{
  struct uip_conn *conn;

  conn = uip_connect(smtpserver, HTONS(25));
  if(conn == NULL) {
    return 0;
  }
  s.connected = 1;
  s.to = to;
  s.cc = cc;
  s.from = from;
  s.subject = subject;
  s.msg = msg;
  s.msglen = msglen;

  PSOCK_INIT(&s.psock, s.inputbuffer, sizeof(s.inputbuffer));
  
  return 1;
}
Exemple #22
0
void
UIPClient::uip_callback(uip_tcp_appstate_t *s)
{
  if (uip_connected())
    {

      // We want to store some data in our connections, so allocate some space
      // for it.  The connection_data struct is defined in a separate .h file,
      // due to the way the Arduino IDE works.  (typedefs come after function
      // definitions.)

      uip_userdata_t *data = (uip_userdata_t*) malloc(sizeof(uip_userdata_t));

      data->in_len = 0;
      data->in_pos = 0;
      data->out_len = 0;
      data->out_pos = 0;
      data->close = false;

      s->user = data;

      // The protosocket's read functions need a per-connection buffer to store
      // data they read.  We've got some space for this in our connection_data
      // structure, so we'll tell uIP to use that.
      PSOCK_INIT(&s->p, data->in_buffer, sizeof(data->in_buffer));
    }

  // Call/resume our protosocket handler.
  handle_connection(s);

  // If the connection has been closed, release the data we allocated earlier.
  if (uip_closed())
    {
      if (s->user)
        free(s->user);
      s->user = NULL;
    }
}
Exemple #23
0
/*---------------------------------------------------------------------------*/
static void
tcpip_handler(void)
{

  if(uip_connected()){
    
    PSOCK_INIT(&ps, buf, MAX_PAYLOAD_LEN);    
    
    PRINTF("Created a server connection with remote address ");
    PRINT6ADDR(&uip_conn->ripaddr);
    PRINTF("local/remote port %u/%u\r\n", HTONS(uip_conn->lport),
           HTONS(uip_conn->rport));
  }
  
  if(uip_closed()){
    PRINTF("Connection with remote address ");
    PRINT6ADDR(&uip_conn->ripaddr);
    PRINTF("closed.\r\n");
    return;
  }
  
  if(uip_aborted()){
    PRINTF("Connection with remote address ");
    PRINT6ADDR(&uip_conn->ripaddr);
    PRINTF("aborted.\r\n"); 
    return;
  }
  
  if(uip_timedout()) {
    PRINTF("Connection with remote address ");
    PRINT6ADDR(&uip_conn->ripaddr);
    PRINTF("timed out.\r\n");
    return;
  }
  
  handle_connection(&ps);
}
/*
 * In uip-server we have defined the UIP_APPCALL macro to
 * server_handle_connection so that this funcion is uIP's application
 * function. This function is called whenever an 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 server_handle_connection()
{
	/*
	* The uip_conn structure has a field called "appstate" that holds
	* the application state of the connection. We make a pointer to
	* this to access it easier.
	*/
	uip_tcp_appstate_t* s = &(uip_conn->appstate);

	/*
	* If a new connection was just established, we should initialize
	* the protosocket in our applications' state structure.
	*/
	if (uip_connected())
	{
		PSOCK_INIT(&s->p, s->pwmRgb, sizeof(s->pwmRgb));
	}

	/*
	* Finally, we run the protosocket function that actually handles
	* the communication. We pass it a pointer to the application state
	* of the current connection.
	*/
	PSOCK_BEGIN(&s->p);

	while (1)
	{
		PSOCK_READBUF(&s->p);

		TIM4->CCR2 = s->pwmRgb[0];
		TIM4->CCR3 = s->pwmRgb[1];
		TIM4->CCR4 = s->pwmRgb[2];
	}

	PSOCK_CLOSE(&s->p);
	PSOCK_END(&s->p);
}
Exemple #25
0
PROCESS_THREAD(mqtt_process, ev, data)
{
  mqtt_event_data_t event_data;

  PROCESS_BEGIN();

  while(1)
  {
    printf("mqtt: connecting...\n");
    mqtt_state.tcp_connection = tcp_connect(&mqtt_state.address, 
                                            mqtt_state.port, NULL);
    PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

    if(!uip_connected())
    {
      printf("mqtt: connect failed\n");
      continue;
    }
    else
      printf("mqtt: connected\n");

    // reserve one byte at the end of the buffer so there's space to NULL terminate
    PSOCK_INIT(&mqtt_state.ps, mqtt_state.in_buffer, mqtt_state.in_buffer_length - 1);

    handle_mqtt_connection(&mqtt_state);

    while(1)
    {
      PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

      if(mqtt_flags & MQTT_FLAG_EXIT)
      {
        uip_close();

        event_data.type = MQTT_EVENT_TYPE_EXITED;
        process_post_synch(mqtt_state.calling_process, mqtt_event, &event_data);
        PROCESS_EXIT();
      }

      if(uip_aborted() || uip_timedout() || uip_closed())
      {
        event_data.type = MQTT_EVENT_TYPE_DISCONNECTED;
        process_post_synch(mqtt_state.calling_process, mqtt_event, &event_data);
        printf("mqtt: lost connection: %s\n", uip_aborted() ? "aborted" : 
                                              uip_timedout() ? "timed out" : 
                                              uip_closed() ? "closed" : 
                                              "unknown");
        break;
      }
      else
        handle_mqtt_connection(&mqtt_state);
    }

    if(!mqtt_state.auto_reconnect)
      break;
  }

  event_data.type = MQTT_EVENT_TYPE_EXITED;
  process_post_synch(mqtt_state.calling_process, mqtt_event, &event_data);

  PROCESS_END();
}
Exemple #26
0
/* This is called by contiki/ip/tcpip.c:tcpip_uipcall() when packet
 * is processed.
 */
PROCESS_THREAD(tcp, ev, data, buf, user_data)
{
	PROCESS_BEGIN();

	while(1) {
		PROCESS_YIELD_UNTIL(ev == tcpip_event);

		if (POINTER_TO_INT(data) == TCP_WRITE_EVENT) {
			/* We want to send data to peer. */
			struct net_context *context = user_data;

			if (!context) {
				continue;
			}

			do {
				context = user_data;
				if (!context || !buf) {
					break;
				}

				if (!context->ps.net_buf ||
				    context->ps.net_buf != buf) {
					NET_DBG("psock init %p buf %p\n",
						&context->ps, buf);
					PSOCK_INIT(&context->ps, buf);
				}

				handle_tcp_connection(&context->ps,
						      POINTER_TO_INT(data),
						      buf);

				PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

				if (POINTER_TO_INT(data) != TCP_WRITE_EVENT) {
					goto read_data;
				}
			} while(!(uip_closed(buf)  ||
				  uip_aborted(buf) ||
				  uip_timedout(buf)));

			context = user_data;

			if (context &&
			    context->tcp_type == NET_TCP_TYPE_CLIENT) {
				NET_DBG("\nConnection closed.\n");
				ip_buf_sent_status(buf) = -ECONNRESET;
			}

			continue;
		}

	read_data:
		/* We are receiving data from peer. */
		if (buf && uip_newdata(buf)) {
			struct net_buf *clone;

			if (!uip_len(buf)) {
				continue;
			}

			/* Note that uIP stack will reuse the buffer when
			 * sending ACK to peer host. The sending will happen
			 * right after this function returns. Because of this
			 * we cannot use the same buffer to pass data to
			 * application.
			 */
			clone = net_buf_clone(buf);
			if (!clone) {
				NET_ERR("No enough RX buffers, "
					"packet %p discarded\n", buf);
				continue;
			}

			ip_buf_appdata(clone) = uip_buf(clone) +
				(ip_buf_appdata(buf) - (void *)uip_buf(buf));
			ip_buf_appdatalen(clone) = uip_len(buf);
			ip_buf_len(clone) = ip_buf_len(buf);
			ip_buf_context(clone) = user_data;
			uip_set_conn(clone) = uip_conn(buf);
			uip_flags(clone) = uip_flags(buf);
			uip_flags(clone) |= UIP_CONNECTED;

			NET_DBG("packet received context %p buf %p len %d "
				"appdata %p appdatalen %d\n",
				ip_buf_context(clone),
				clone,
				ip_buf_len(clone),
				ip_buf_appdata(clone),
				ip_buf_appdatalen(clone));

			nano_fifo_put(net_context_get_queue(user_data), clone);

			/* We let the application to read the data now */
			fiber_yield();
		}
	}

	PROCESS_END();
}
/*
 * The definition of the process.
 */
PROCESS_THREAD(example_psock_server_process, ev, data)
{
  /*
   * The process begins here.
   */
  PROCESS_BEGIN();

  /*
   * We start with setting up a listening TCP port. Note how we're
   * using the UIP_HTONS() macro to convert the port number (1010) to
   * network byte order as required by the tcp_listen() function.
   */
  tcp_listen(UIP_HTONS(1010));

  /*
   * We loop for ever, accepting new connections.
   */
  while(1) {

    /*
     * We wait until we get the first TCP/IP event, which probably
     * comes because someone connected to us.
     */
    PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

    /*
     * If a peer connected with us, we'll initialize the protosocket
     * with PSOCK_INIT().
     */
    if(uip_connected()) {
      
      /*
       * The PSOCK_INIT() function initializes the protosocket and
       * binds the input buffer to the protosocket.
       */
      PSOCK_INIT(&ps, buffer, sizeof(buffer));

      /*
       * We loop until the connection is aborted, closed, or times out.
       */
      while(!(uip_aborted() || uip_closed() || uip_timedout())) {

	/*
	 * We wait until we get a TCP/IP event. Remember that we
	 * always need to wait for events inside a process, to let
	 * other processes run while we are waiting.
	 */
	PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

	/*
	 * Here is where the real work is taking place: we call the
	 * handle_connection() protothread that we defined above. This
	 * protothread uses the protosocket to receive the data that
	 * we want it to.
	 */
	handle_connection(&ps);
      }
    }
  }
  
  /*
   * We must always declare the end of a process.
   */
  PROCESS_END();
}
Exemple #28
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void *state)
{
#if DEBUGLOGIC
  struct httpd_state *s;   //Enter here for debugging with output directed to TCPBUF
  s = sg = (struct httpd_state *)memb_alloc(&conns);  //put ram watch on sg
  if (1) {
#else
  struct httpd_state *s = (struct httpd_state *)state;

  if(uip_closed() || uip_aborted() || uip_timedout()) {
    if(s != NULL) {
      s->script = NULL;
      memb_free(&conns, s);
    }
  } else if(uip_connected()) {
    s = (struct httpd_state *)memb_alloc(&conns);
    if(s == NULL) {
      uip_abort();
      webserver_log_file(&uip_conn->ripaddr, "reset (no memory block)");
      return;
    }
#endif
    tcp_markconn(uip_conn, s);
    PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->script = NULL;
    s->state = STATE_WAITING;
    timer_set(&s->timer, CLOCK_SECOND * 10);
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      if(timer_expired(&s->timer)) {
        uip_abort();
        s->script = NULL;
        memb_free(&conns, s);
        webserver_log_file(&uip_conn->ripaddr, "reset (timeout)");
      }
    } else {
      timer_restart(&s->timer);
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}
/*---------------------------------------------------------------------------*/
PROCESS(httpd_process, "httpd");
PROCESS_THREAD(httpd_process, ev, data)
{
  PROCESS_BEGIN();

  httpd_init();

  while(1) {
    PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
    httpd_appcall(data);
  }

  PROCESS_END();
}
Exemple #29
0
/*
 * This function is called whenever an uIP event occurs (e.g. when
 * a new connection is established, new data arrives, sent data is
 * acknowledged, data needs to be retransmitted, etc.), from the 
 * uip_tcp_appcall() callback.
 */
void ftpd_appcall( void )
{


    u16_t i;

    //PRINT_Log("\r\nftpd_appcall\r\n");

    /*
     * First we must check that it is the port we are listening on as
     * this function will be called for all events on all connections.
     */
    if (uip_conn->lport == HTONS(FTPD_CONTROL_PORT))
    {

    	ftpd_appstate_t *s = (ftpd_appstate_t *)uip_conn->appdata;
        /*
         * If a new connection we need to check for a few possibilities.
         */
        if (uip_connected())
        {
        	PRINT_Log("\r\nNew command connection from IP Address %d.%d.%d.%d:%d",
                   uip_ipaddr1(uip_conn->ripaddr), uip_ipaddr2(uip_conn->ripaddr),
                   uip_ipaddr3(uip_conn->ripaddr), uip_ipaddr4(uip_conn->ripaddr),
                   ntohs(uip_conn->rport));


            if (!driveAttached)
            {
            	PRINT_Log("\r\nNo drive attached!");
                // Initialise the ProtoSocket for the 421 message
                PSOCK_INIT(&s->ps, NULL, 0);
                // Set the flag to cause it to be sent
                s->resp421_flag = TRUE;
            }
            else
            {
                // find a new FTP server structure
                for (i = 0; i < NUM_FTP_DAEMONS; i++)
                {
                    if (FTP_UNUSED == ftpd_state[i].state)
                    {
                        break;
                    }
                }
                
                // Check if we found a free ftpd_state
                if (NUM_FTP_DAEMONS == i)
                {
                	PRINT_Log("\r\nNo daemons available!");
                    // Initialise the ProtoSocket for the 421 message
                    PSOCK_INIT(&s->ps, NULL, 0);
                    // Set the flag to cause it to be sent
                    s->resp421_flag = TRUE;
                }
                else
                {
                	PRINT_Log("\r\nFound unused deamon %d", i);
                    s->ftpd_state_ptr = &ftpd_state[i];
                    // Initialise the FTP Daemon state
                    ftpd_state[i].state = LOGGED_OUT;
                    ftpd_state[i].data_conn = NULL;
                    //ftpd_state[i].clientip = uip_conn->ripaddr;
                    memcpy(ftpd_state[i].clientip,uip_conn->ripaddr,sizeof(uip_ipaddr_t));
                    ftpd_state[i].clientport = FTPC_DATA_PORT;
                    // Clear the command index down
                    ftpd_state[i].cmd_index = CMD_INDEX_IDLE;
                    // Reset the directory pointer
                    //ftpd_state[i].pCurrent_dir = NULL;
                    // Clear the current path
                    strcpy(ftpd_state[i].Current_path, "/");
                    // Setup the PASV information
                    ftpd_state[i].pasvflag = FALSE;
                    
                    // Initialise the ProtoSockets and ProtoThread used by the command parser
                    PSOCK_INIT(&ftpd_state[i].cmdpsin, ftpd_state[i].cmdinputbuffer, CMD_BUFFER_SIZE);
                    PT_INIT(&ftpd_state[i].cmdpt);
                    // Clear the 421 response flag
                    s->resp421_flag = FALSE;
                }
            }
        }
        else if (uip_closed() || uip_aborted() || uip_timedout())
        {
        	PRINT_Log("\r\nCommand connection closing");
            if (s->ftpd_state_ptr)
            {
            	PRINT_Log(" for daemon %d", s->ftpd_state_ptr->pasvport - PASV_PORT_OFFSET);
                // Free up the FTP daemon
                s->ftpd_state_ptr->state = FTP_UNUSED;
                s->ftpd_state_ptr = NULL;
            }
        }
      
        if (s->resp421_flag)
        {
            send_421_response(s);
        }
        else if (s->ftpd_state_ptr)
        {
            handle_cmd_input(s->ftpd_state_ptr);
            handle_cmd_output(s->ftpd_state_ptr);
        }
    }
    else
    {
    	//PRINT_Log("\r\nSearch though all the FTPD Structures and see if this one of our data connections.");
        /* 
         * Search though all the FTPD Structures and see if this one of our data connections.
         */
        u16_t i;
        for (i = 0; i < NUM_FTP_DAEMONS; i++)
        {
            if (ftpd_state[i].data_conn == uip_conn)
            {
                if (uip_connected())
                {
                	PRINT_Log("\r\nNew active data connection for daemon %d to IP Address %d.%d.%d.%d:%d", i,
                           uip_ipaddr1(uip_conn->ripaddr), uip_ipaddr2(uip_conn->ripaddr),
                           uip_ipaddr3(uip_conn->ripaddr), uip_ipaddr4(uip_conn->ripaddr),
                           ntohs(uip_conn->rport));
                    PSOCK_INIT(&ftpd_state[i].dataps, ftpd_state[i].datainputbuffer, DATA_BUFFER_SIZE);
                    // Move the state on as required
                    if (WAITING_FOR_CONNECT == ftpd_state[i].state)
                    {
                        ftpd_state[i].state = CONNECT_OK;
                    }
                }
                else if (uip_closed() || uip_aborted() || uip_timedout())
                {
                	PRINT_Log("\r\nData connection closing for daemon %d", i);
                    if (uip_closed() && (TRANSFER_RECV == ftpd_state[i].state))
                    {
                        // There may be data in this last packet
                        handle_data_connection(&ftpd_state[i]);
                        // Closing of the connection in a RECV state indicates end of file
#if 1
                        CloseFile(&ftpd_state[i]);

#endif
                        //close the file in here

                        PRINT_Log("\r\nFinished file receive for daemon %d", i);
                        ftpd_state[i].state = TRANSFER_DONE;
                    }
                    else if((TRANSFER_LIST == ftpd_state[i].state) || (TRANSFER_SEND == ftpd_state[i].state) || (TRANSFER_RECV == ftpd_state[i].state))
                    {
                    	PRINT_Log("\r\nAborting data transfer for daemon %d", i);
                        ftpd_state[i].state = TRANSFER_ABORT;
                    }
                    ftpd_state[i].data_conn = NULL;
                }
                
                if (ftpd_state[i].data_conn)
                {
                    handle_data_connection(&ftpd_state[i]);
                }
                // Leave the loop early
                break;
            }
            else
            {
                // Check for a new connection in Passive mode
                if(ftpd_state[i].pasvflag && uip_connected() && (ntohs(uip_conn->lport) == ftpd_state[i].pasvport))
                {
                	PRINT_Log("\r\nNew passive data connection for daemon %d from IP Address %d.%d.%d.%d:%d", i,
                           uip_ipaddr1(uip_conn->ripaddr), uip_ipaddr2(uip_conn->ripaddr),
                           uip_ipaddr3(uip_conn->ripaddr), uip_ipaddr4(uip_conn->ripaddr),
                           ntohs(uip_conn->rport));
                    ftpd_state[i].data_conn = uip_conn;
                    PSOCK_INIT(&ftpd_state[i].dataps, ftpd_state[i].datainputbuffer, DATA_BUFFER_SIZE);
                    // Move the state on as required
                    if (WAITING_FOR_CONNECT == ftpd_state[i].state)
                    {
                        ftpd_state[i].state = CONNECT_OK;
                    }
                    handle_data_connection(&ftpd_state[i]);
                    // Leave the loop early
                    break;
                }
            }
        }
    }
}
/*
 * The definition of the process.
 */
PROCESS_THREAD(twamp_tcp_control_server, ev, data)
{
  /*
   * The process begins here.
   */
  uip_ipaddr_t *ipaddr;
  
  PROCESS_BEGIN();
  set_global_address();
  /*
   * We start with setting up a listening TCP port. Note how we're
   * using the UIP_HTONS() macro to convert the port number (862) to
   * network byte order as required by the tcp_listen() function.
   */
  tcp_listen(UIP_HTONS(862));

  /*
   * We loop for ever, accepting new connections.
   */
  while(1) {

    /*
     * We wait until we get the first TCP/IP event, which probably
     * comes because someone connected to us.
     */
    PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

    /*
     * If a peer connected with us, we'll initialize the protosocket
     * with PSOCK_INIT().
     */
    if(uip_connected()) {
      
      /*
       * The PSOCK_INIT() function initializes the protosocket and
       * binds the input buffer to the protosocket.
       */
      PSOCK_INIT(&ps, buffer, sizeof(buffer));
      printf("Someone connected!\n");
      
      /*
       * We loop until the connection is aborted, closed, or times out.
       */
      while(!(uip_aborted() || uip_closed() || uip_timedout())) {
	/*
	 * We wait until we get a TCP/IP event. Remember that we
	 * always need to wait for events inside a process, to let
	 * other processes run while we are waiting.
	 */
	PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
	/*
	 * Here is where the real work is taking place: we call the
	 * handle_connection() protothread that we defined above. This
	 * protothread uses the protosocket to receive the data that
	 * we want it to.
	 */
	if(state == 1){
          connection_setup(&ps);
        }
        if(state == 2){
          create_test_session(&ps);
        }
        if(state == 3){
          timesynch(&ps);
        }
        if(state == 4){
          //PT_INIT(&pthread);
          //run_test_session(&ps);
          process_start(&run_test_session,NULL);
          PROCESS_YIELD_UNTIL(!process_is_running(&run_test_session));
        }
      }
    }
  }
  
  /*
   * We must always declare the end of a process.
   */
  PROCESS_END();
}