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