netconf.c 5.9 KB

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