hello-world.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * \addtogroup helloworld
  3. * @{
  4. */
  5. /**
  6. * \file
  7. * An example of how to write uIP applications
  8. * with protosockets.
  9. * \author
  10. * Adam Dunkels <adam@sics.se>
  11. */
  12. /*
  13. * This is a short example of how to write uIP applications using
  14. * protosockets.
  15. */
  16. /*
  17. * We define the application state (struct hello_world_state) in the
  18. * hello-world.h file, so we need to include it here. We also include
  19. * uip.h (since this cannot be included in hello-world.h) and
  20. * <string.h>, since we use the memcpy() function in the code.
  21. */
  22. #include "hello-world.h"
  23. #include "uip.h"
  24. #include <string.h>
  25. /*
  26. * Declaration of the protosocket function that handles the connection
  27. * (defined at the end of the code).
  28. */
  29. static int handle_connection(struct hello_world_state *s);
  30. static int rt_show_info(struct hello_world_state *s);
  31. /*---------------------------------------------------------------------------*/
  32. /*
  33. * The initialization function. We must explicitly call this function
  34. * from the system initialization code, some time after uip_init() is
  35. * called.
  36. */
  37. void
  38. hello_world_init(void)
  39. {
  40. /* We start to listen for connections on TCP port 1000. */
  41. uip_listen(HTONS(1000));
  42. }
  43. /*---------------------------------------------------------------------------*/
  44. /*
  45. * In hello-world.h we have defined the UIP_APPCALL macro to
  46. * hello_world_appcall so that this funcion is uIP's application
  47. * function. This function is called whenever an uIP event occurs
  48. * (e.g. when a new connection is established, new data arrives, sent
  49. * data is acknowledged, data needs to be retransmitted, etc.).
  50. */
  51. void
  52. hello_world_appcall(void)
  53. {
  54. /*
  55. * The uip_conn structure has a field called "appstate" that holds
  56. * the application state of the connection. We make a pointer to
  57. * this to access it easier.
  58. */
  59. struct hello_world_state *s = &(uip_conn->appstate);
  60. /*
  61. * If a new connection was just established, we should initialize
  62. * the protosocket in our applications' state structure.
  63. */
  64. if(uip_connected()) {
  65. PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));
  66. rt_show_info(s);
  67. }
  68. /*
  69. * Finally, we run the protosocket function that actually handles
  70. * the communication. We pass it a pointer to the application state
  71. * of the current connection.
  72. */
  73. handle_connection(s);
  74. }
  75. /*---------------------------------------------------------------------------*/
  76. /*
  77. * This is the protosocket function that handles the communication. A
  78. * protosocket function must always return an int, but must never
  79. * explicitly return - all return statements are hidden in the PSOCK
  80. * macros.
  81. */
  82. static int rt_show_info(struct hello_world_state *s)
  83. {
  84. PSOCK_BEGIN(&s->p);
  85. PSOCK_SEND_STR(&s->p,"RT-Thread RTOS");
  86. PSOCK_READTO(&s->p, '\n');
  87. PSOCK_END(&s->p);
  88. }
  89. static int
  90. handle_connection(struct hello_world_state *s)
  91. {
  92. int i;
  93. for (i=0;i<BUF_SIZE;i++)
  94. {
  95. s->name[i] = 0;
  96. s->inputbuffer[i] = 0;
  97. }
  98. PSOCK_BEGIN(&s->p);
  99. //PSOCK_SEND_STR(&s->p, "Hello. What is your name?\n");
  100. PSOCK_READTO(&s->p, '\n');
  101. strncpy(s->name, s->inputbuffer, sizeof(s->name));
  102. //PSOCK_SEND_STR(&s->p, "Hello ");
  103. PSOCK_SEND_STR(&s->p, s->name);
  104. //PSOCK_CLOSE(&s->p);
  105. PSOCK_END(&s->p);
  106. }
  107. /*---------------------------------------------------------------------------*/