xref: /nrf52832-nimble/rt-thread/components/net/uip/doc/example-mainloop-with-arp.c (revision 104654410c56c573564690304ae786df310c91fc)
1 #include "uip.h"
2 #include "uip_arp.h"
3 #include "network-device.h"
4 #include "httpd.h"
5 #include "timer.h"
6 
7 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
8 
9 /*---------------------------------------------------------------------------*/
10 int
main(void)11 main(void)
12 {
13   int i;
14   uip_ipaddr_t ipaddr;
15   struct timer periodic_timer, arp_timer;
16 
17   timer_set(&periodic_timer, CLOCK_SECOND / 2);
18   timer_set(&arp_timer, CLOCK_SECOND * 10);
19 
20   network_device_init();
21   uip_init();
22 
23   uip_ipaddr(ipaddr, 192,168,0,2);
24   uip_sethostaddr(ipaddr);
25 
26   httpd_init();
27 
28   while(1) {
29     uip_len = network_device_read();
30     if(uip_len > 0) {
31       if(BUF->type == htons(UIP_ETHTYPE_IP)) {
32 	uip_arp_ipin();
33 	uip_input();
34 	/* If the above function invocation resulted in data that
35 	   should be sent out on the network, the global variable
36 	   uip_len is set to a value > 0. */
37 	if(uip_len > 0) {
38 	  uip_arp_out();
39 	  network_device_send();
40 	}
41       } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
42 	uip_arp_arpin();
43 	/* If the above function invocation resulted in data that
44 	   should be sent out on the network, the global variable
45 	   uip_len is set to a value > 0. */
46 	if(uip_len > 0) {
47 	  network_device_send();
48 	}
49       }
50 
51     } else if(timer_expired(&periodic_timer)) {
52       timer_reset(&periodic_timer);
53       for(i = 0; i < UIP_CONNS; i++) {
54 	uip_periodic(i);
55 	/* If the above function invocation resulted in data that
56 	   should be sent out on the network, the global variable
57 	   uip_len is set to a value > 0. */
58 	if(uip_len > 0) {
59 	  uip_arp_out();
60 	  network_device_send();
61 	}
62       }
63 
64 #if UIP_UDP
65       for(i = 0; i < UIP_UDP_CONNS; i++) {
66 	uip_udp_periodic(i);
67 	/* If the above function invocation resulted in data that
68 	   should be sent out on the network, the global variable
69 	   uip_len is set to a value > 0. */
70 	if(uip_len > 0) {
71 	  uip_arp_out();
72 	  network_device_send();
73 	}
74       }
75 #endif /* UIP_UDP */
76 
77       /* Call the ARP timer function every 10 seconds. */
78       if(timer_expired(&arp_timer)) {
79 	timer_reset(&arp_timer);
80 	uip_arp_timer();
81       }
82     }
83   }
84   return 0;
85 }
86 /*---------------------------------------------------------------------------*/
87