hello-world.c

00001 /**
00002  * \addtogroup helloworld
00003  * @{
00004  */
00005 
00006 /**
00007  * \file
00008  *         An example of how to write uIP applications
00009  *         with protosockets.
00010  * \author
00011  *         Adam Dunkels <adam@sics.se>
00012  */
00013 
00014 /*
00015  * This is a short example of how to write uIP applications using
00016  * protosockets.
00017  */
00018 
00019 /*
00020  * We define the application state (struct hello_world_state) in the
00021  * hello-world.h file, so we need to include it here. We also include
00022  * uip.h (since this cannot be included in hello-world.h) and
00023  * <string.h>, since we use the memcpy() function in the code.
00024  */
00025 #include "hello-world.h"
00026 #include "uip.h"
00027 #include <string.h>
00028 
00029 /*
00030  * Declaration of the protosocket function that handles the connection
00031  * (defined at the end of the code).
00032  */
00033 static int handle_connection(struct hello_world_state *s);
00034 /*---------------------------------------------------------------------------*/
00035 /*
00036  * The initialization function. We must explicitly call this function
00037  * from the system initialization code, some time after uip_init() is
00038  * called.
00039  */
00040 void
00041 hello_world_init(void)
00042 {
00043   /* We start to listen for connections on TCP port 1000. */
00044   uip_listen(HTONS(1000));
00045 }
00046 /*---------------------------------------------------------------------------*/
00047 /*
00048  * In hello-world.h we have defined the UIP_APPCALL macro to
00049  * hello_world_appcall so that this funcion is uIP's application
00050  * function. This function is called whenever an uIP event occurs
00051  * (e.g. when a new connection is established, new data arrives, sent
00052  * data is acknowledged, data needs to be retransmitted, etc.).
00053  */
00054 void
00055 hello_world_appcall(void)
00056 {
00057   /*
00058    * The uip_conn structure has a field called "appstate" that holds
00059    * the application state of the connection. We make a pointer to
00060    * this to access it easier.
00061    */
00062   struct hello_world_state *s = &(uip_conn->appstate);
00063 
00064   /*
00065    * If a new connection was just established, we should initialize
00066    * the protosocket in our applications' state structure.
00067    */
00068   if(uip_connected()) {
00069     PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));
00070   }
00071 
00072   /*
00073    * Finally, we run the protosocket function that actually handles
00074    * the communication. We pass it a pointer to the application state
00075    * of the current connection.
00076    */
00077   handle_connection(s);
00078 }
00079 /*---------------------------------------------------------------------------*/
00080 /*
00081  * This is the protosocket function that handles the communication. A
00082  * protosocket function must always return an int, but must never
00083  * explicitly return - all return statements are hidden in the PSOCK
00084  * macros.
00085  */
00086 static int
00087 handle_connection(struct hello_world_state *s)
00088 {
00089   PSOCK_BEGIN(&s->p);
00090 
00091   PSOCK_SEND_STR(&s->p, "Hello. What is your name?\n");
00092   PSOCK_READTO(&s->p, '\n');
00093   strncpy(s->name, s->inputbuffer, sizeof(s->name));
00094   PSOCK_SEND_STR(&s->p, "Hello ");
00095   PSOCK_SEND_STR(&s->p, s->name);
00096   PSOCK_CLOSE(&s->p);
00097   
00098   PSOCK_END(&s->p);
00099 }
00100 /*---------------------------------------------------------------------------*/

Generated on Mon Jun 12 10:23:01 2006 for uIP 1.0 by  doxygen 1.4.6