hello-world.c

Go to the documentation of this file.
00001 
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 
00031 /*
00032   * Declaration of the protosocket function that handles the connection
00033   * (defined at the end of the code).
00034   */
00035  static int handle_connection(struct hello_world_state *s);
00036  /*---------------------------------------------------------------------------*/
00037  /*
00038   * The initialization function. We must explicitly call this function
00039   * from the system initialization code, some time after uip_init() is
00040   * called.
00041   */
00042  void
00043  hello_world_init(void)
00044  {
00045    /* We start to listen for connections on TCP port 1000. */
00046    uip_listen(HTONS(1000));
00047  }
00048  /*---------------------------------------------------------------------------*/
00049  /*
00050   * In hello-world.h we have defined the UIP_APPCALL macro to
00051   * hello_world_appcall so that this funcion is uIP's application
00052   * function. This function is called whenever an uIP event occurs
00053   * (e.g. when a new connection is established, new data arrives, sent
00054   * data is acknowledged, data needs to be retransmitted, etc.).
00055   */
00056  void
00057  hello_world_appcall(void)
00058  {
00059    /*
00060     * The uip_conn structure has a field called "appstate" that holds
00061     * the application state of the connection. We make a pointer to
00062     * this to access it easier.
00063     */
00064    struct hello_world_state *s = &(uip_conn->appstate);
00065  
00066    /*
00067     * If a new connection was just established, we should initialize
00068     * the protosocket in our applications' state structure.
00069     */
00070    if(uip_connected()) {
00071      PSOCK_INIT(&p, s->inputbuffer, sizeof(s->inputbuffer));
00072    }
00073  
00074    /*
00075     * Finally, we run the protosocket function that actually handles
00076     * the communication. We pass it a pointer to the application state
00077     * of the current connection.
00078     */
00079    handle_connection(s);
00080  }
00081  /*---------------------------------------------------------------------------*/
00082  /*
00083   * This is the protosocket function that handles the communication. A
00084   * protosocket function must always return an int, but must never
00085   * explicitly return - all return statements are hidden in the PSOCK
00086   * macros.
00087   */
00088  static int
00089  handle_connection(struct hello_world_state *s)
00090  {
00091    PSOCK_BEGIN(&p);
00092  
00093    PSOCK_SEND_STR(&p, "Hello. What is your name?\n");
00094    PSOCK_READTO(&p, '\n');
00095    strncpy(s->name, s->inputbuffer, sizeof(s->name));
00096    PSOCK_SEND_STR(&p, "Hello ");
00097    PSOCK_SEND_STR(&p, s->name);
00098    PSOCK_CLOSE(&p);
00099    
00100    PSOCK_END(&p);
00101  }
00102  /*---------------------------------------------------------------------------*/