netconf.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "lwip/mem.h"
  3. #include "lwip/memp.h"
  4. #include "lwip/tcp.h"
  5. #include "lwip/udp.h"
  6. #include "netif/etharp.h"
  7. #include "lwip/dhcp.h"
  8. #include "ethernetif.h"
  9. #include "tcpip.h"
  10. #include "main.h"
  11. #include "netconf.h"
  12. #include "common_config.h"
  13. #include "settings_api.h"
  14. #include "tinystdio.h"
  15. #include "lwip/init.h"
  16. #include "lwip/timeouts.h"
  17. #include "lwip/sys.h"
  18. /* Private typedef -----------------------------------------------------------*/
  19. #define MAX_DHCP_TRIES 4
  20. /* Private define ------------------------------------------------------------*/
  21. typedef enum {
  22. DHCP_START = 0,
  23. DHCP_WAIT_ADDRESS,
  24. DHCP_ADDRESS_ASSIGNED,
  25. DHCP_TIMEOUT
  26. }
  27. DHCP_State_TypeDef;
  28. /* Private macro -------------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. struct netif netif;
  31. uint32_t TCPTimer = 0;
  32. uint32_t ARPTimer = 0;
  33. uint32_t DHCPfineTimer = 0;
  34. uint32_t DHCPcoarseTimer = 0;
  35. DHCP_State_TypeDef DHCP_state = DHCP_START;
  36. /**
  37. * @brief Общая структура настроек
  38. */
  39. extern SETTINGS_t sSettings;
  40. /* Private functions ---------------------------------------------------------*/
  41. void LwIP_DHCP_Process_Handle(void);
  42. /**
  43. * @brief Initializes the lwIP stack
  44. * @param None
  45. * @retval None
  46. */
  47. void LwIP_Init(void)
  48. {
  49. struct ip4_addr ipaddr;
  50. struct ip4_addr netmask;
  51. struct ip4_addr gw;
  52. char str[20];
  53. lwip_init();
  54. // /* Initializes the dynamic memory heap defined by MEM_SIZE.*/
  55. // mem_init();
  56. //
  57. // /* Initializes the memory pools defined by MEMP_NUM_x.*/
  58. // memp_init();
  59. if (sSettings.sWebParams.dhcpEnable) {
  60. ipaddr.addr = 0;
  61. netmask.addr = 0;
  62. gw.addr = 0;
  63. } else {
  64. sprintf(str, " %s\n\r", sSettings.sWebTempParams.ip);
  65. PRINT_USART("\n\rStatic IP address \n\r");
  66. PRINT_USART(str);
  67. ipaddr.addr = ipaddr_addr(sSettings.sWebParams.ip);
  68. netmask.addr = ipaddr_addr(sSettings.sWebParams.mask);
  69. gw.addr = ipaddr_addr(sSettings.sWebParams.gate);
  70. }
  71. netif_add(&netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
  72. netif_set_default(&netif);
  73. netif_set_link_up(&netif);
  74. netif_set_up(&netif);
  75. }
  76. /**
  77. * @brief Called when a frame is received
  78. * @param None
  79. * @retval None
  80. */
  81. void LwIP_Pkt_Handle(void)
  82. {
  83. /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
  84. ethernetif_input(&netif);
  85. }
  86. ///**
  87. // * @brief LwIP periodic tasks
  88. // * @param localtime the current LocalTime value
  89. // * @retval None
  90. // */
  91. //void LwIP_Periodic_Handle(__IO uint32_t localtime)
  92. //{
  93. //#if LWIP_TCP
  94. // /* TCP periodic process every 250 ms */
  95. // if (localtime - TCPTimer >= TCP_TMR_INTERVAL) {
  96. // TCPTimer = localtime;
  97. // tcp_tmr();
  98. // }
  99. //#endif
  100. //
  101. // /* ARP periodic process every 5s */
  102. // if ((localtime - ARPTimer) >= ARP_TMR_INTERVAL) {
  103. // ARPTimer = localtime;
  104. // etharp_tmr();
  105. // }
  106. //
  107. // if (sSettings.sWebParams.dhcpEnable)
  108. // {
  109. // /* Fine DHCP periodic process every 500ms */
  110. // if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS) {
  111. // DHCPfineTimer = localtime;
  112. // dhcp_fine_tmr();
  113. // if ((DHCP_state != DHCP_ADDRESS_ASSIGNED)&&(DHCP_state != DHCP_TIMEOUT)) {
  114. // /* process DHCP state machine */
  115. // LwIP_DHCP_Process_Handle();
  116. // }
  117. // }
  118. //
  119. // /* DHCP Coarse periodic process every 60s */
  120. // if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS) {
  121. // DHCPcoarseTimer = localtime;
  122. // dhcp_coarse_tmr();
  123. // }
  124. // }
  125. //
  126. //}
  127. /**
  128. * @brief LwIP periodic tasks
  129. * @param localtime the current LocalTime value
  130. * @retval None
  131. */
  132. void LwIP_Periodic_Handle(__IO uint32_t localtime)
  133. {
  134. if (sSettings.sWebParams.dhcpEnable) {
  135. /* Fine DHCP periodic process every 500ms */
  136. if ((DHCP_state != DHCP_ADDRESS_ASSIGNED) && (DHCP_state != DHCP_TIMEOUT)) {
  137. /* process DHCP state machine */
  138. LwIP_DHCP_Process_Handle();
  139. }
  140. }
  141. sys_check_timeouts();
  142. }
  143. /**
  144. * @brief LwIP_DHCP_Process_Handle
  145. * @param None
  146. * @retval None
  147. */
  148. void LwIP_DHCP_Process_Handle()
  149. {
  150. struct ip4_addr ipaddr;
  151. struct ip4_addr netmask;
  152. struct ip4_addr gw;
  153. struct dhcp *dhcp = netif_dhcp_data(&netif);
  154. switch (DHCP_state) {
  155. case DHCP_START: {
  156. dhcp_start(&netif);
  157. DHCP_state = DHCP_WAIT_ADDRESS;
  158. PRINT_USART("\n\rLooking for DHCP server please wait...\n\r");
  159. }
  160. break;
  161. case DHCP_WAIT_ADDRESS: {
  162. ipaddr = netif.ip_addr;
  163. netmask = netif.netmask;
  164. gw = netif.gw;
  165. if (ipaddr.addr != 0) {
  166. DHCP_state = DHCP_ADDRESS_ASSIGNED;
  167. PRINT_USART("Parameters assigned by a DHCP server:\n\r IP: ");
  168. PRINT_USART(ipaddr_ntoa(&ipaddr));
  169. PRINT_USART("\n\r");
  170. PRINT_USART("Netmask: ");
  171. PRINT_USART(ipaddr_ntoa(&netmask));
  172. PRINT_USART("\n\r");
  173. PRINT_USART("Gateway: ");
  174. PRINT_USART(ipaddr_ntoa(&gw));
  175. PRINT_USART("\n\r");
  176. } else {
  177. /* DHCP timeout */
  178. if (dhcp->tries > MAX_DHCP_TRIES) {
  179. DHCP_state = DHCP_TIMEOUT;
  180. /* Stop DHCP */
  181. dhcp_stop(&netif);
  182. ipaddr.addr = ipaddr_addr(sSettings.sWebTempParams.ip);
  183. netmask.addr = ipaddr_addr(sSettings.sWebTempParams.mask);
  184. gw.addr = ipaddr_addr(sSettings.sWebTempParams.gate);
  185. netif_set_addr(&netif, &ipaddr, &netmask, &gw);
  186. PRINT_USART("DHCP timeout\n\r");
  187. PRINT_USART("\n\rStatic IP address\n\r");
  188. PRINT_USART(ipaddr_ntoa(&ipaddr));
  189. PRINT_USART("\n\r");
  190. }
  191. }
  192. }
  193. break;
  194. default: break;
  195. }
  196. }
  197. /*********** Portions COPYRIGHT 2012 Embest Tech. Co., Ltd.*****END OF FILE****/