example-mainloop-with-arp.c 2.1 KB

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