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