netconf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "lwip/mem.h"
  3. #include "lwip/memp.h"
  4. #include "lwip/tcp.h"
  5. #include "lwip/tcp_impl.h"
  6. #include "lwip/udp.h"
  7. #include "netif/etharp.h"
  8. #include "lwip/dhcp.h"
  9. #include "lwip/ip_addr.h"
  10. #include "ethernetif.h"
  11. #include "main.h"
  12. #include "netconf.h"
  13. #include "common_config.h"
  14. #include "settings_api.h"
  15. #include "tinystdio.h"
  16. #include "lwip/timers.h"
  17. #include "lwip/init.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 ip_addr ipaddr;
  51. struct ip_addr netmask;
  52. struct ip_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_up(&netif);
  78. }
  79. /**
  80. * @brief Called when a frame is received
  81. * @param None
  82. * @retval None
  83. */
  84. void LwIP_Pkt_Handle(void)
  85. {
  86. /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
  87. ethernetif_input(&netif);
  88. }
  89. ///**
  90. // * @brief LwIP periodic tasks
  91. // * @param localtime the current LocalTime value
  92. // * @retval None
  93. // */
  94. //void LwIP_Periodic_Handle(__IO uint32_t localtime)
  95. //{
  96. //#if LWIP_TCP
  97. // /* TCP periodic process every 250 ms */
  98. // if (localtime - TCPTimer >= TCP_TMR_INTERVAL) {
  99. // TCPTimer = localtime;
  100. // tcp_tmr();
  101. // }
  102. //#endif
  103. //
  104. // /* ARP periodic process every 5s */
  105. // if ((localtime - ARPTimer) >= ARP_TMR_INTERVAL) {
  106. // ARPTimer = localtime;
  107. // etharp_tmr();
  108. // }
  109. //
  110. // if (sSettings.sWebParams.dhcpEnable)
  111. // {
  112. // /* Fine DHCP periodic process every 500ms */
  113. // if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS) {
  114. // DHCPfineTimer = localtime;
  115. // dhcp_fine_tmr();
  116. // if ((DHCP_state != DHCP_ADDRESS_ASSIGNED)&&(DHCP_state != DHCP_TIMEOUT)) {
  117. // /* process DHCP state machine */
  118. // LwIP_DHCP_Process_Handle();
  119. // }
  120. // }
  121. //
  122. // /* DHCP Coarse periodic process every 60s */
  123. // if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS) {
  124. // DHCPcoarseTimer = localtime;
  125. // dhcp_coarse_tmr();
  126. // }
  127. // }
  128. //
  129. //}
  130. /**
  131. * @brief LwIP periodic tasks
  132. * @param localtime the current LocalTime value
  133. * @retval None
  134. */
  135. void LwIP_Periodic_Handle(__IO uint32_t localtime)
  136. {
  137. if (sSettings.sWebParams.dhcpEnable)
  138. {
  139. /* Fine DHCP periodic process every 500ms */
  140. if ((DHCP_state != DHCP_ADDRESS_ASSIGNED)&&(DHCP_state != DHCP_TIMEOUT)) {
  141. /* process DHCP state machine */
  142. LwIP_DHCP_Process_Handle();
  143. }
  144. }
  145. sys_check_timeouts();
  146. }
  147. /**
  148. * @brief LwIP_DHCP_Process_Handle
  149. * @param None
  150. * @retval None
  151. */
  152. void LwIP_DHCP_Process_Handle()
  153. {
  154. struct ip_addr ipaddr;
  155. struct ip_addr netmask;
  156. struct ip_addr gw;
  157. switch (DHCP_state)
  158. {
  159. case DHCP_START:
  160. {
  161. dhcp_start(&netif);
  162. DHCP_state = DHCP_WAIT_ADDRESS;
  163. PRINT_USART("\n\rLooking for DHCP server please wait...\n\r");
  164. }
  165. break;
  166. case DHCP_WAIT_ADDRESS:
  167. {
  168. ipaddr = netif.ip_addr;
  169. netmask = netif.netmask;
  170. gw = netif.gw;
  171. if (ipaddr.addr != 0)
  172. {
  173. DHCP_state = DHCP_ADDRESS_ASSIGNED;
  174. /* Stop DHCP */
  175. dhcp_stop(&netif);
  176. PRINT_USART("Parameters assigned by a DHCP server:\n\r IP: ");
  177. PRINT_USART(ipaddr_ntoa(&ipaddr));
  178. PRINT_USART("\n\r");
  179. PRINT_USART("Netmask: ");
  180. PRINT_USART(ipaddr_ntoa(&netmask));
  181. PRINT_USART("\n\r");
  182. PRINT_USART("Gateway: ");
  183. PRINT_USART(ipaddr_ntoa(&gw));
  184. PRINT_USART("\n\r");
  185. } else {
  186. /* DHCP timeout */
  187. if (netif.dhcp->tries > MAX_DHCP_TRIES)
  188. {
  189. DHCP_state = DHCP_TIMEOUT;
  190. /* Stop DHCP */
  191. dhcp_stop(&netif);
  192. // ipaddr.addr = ipaddr_addr(sSettings.sWebTempParams.ip);
  193. // netmask.addr = ipaddr_addr(sSettings.sWebTempParams.mask);
  194. // gw.addr = ipaddr_addr(sSettings.sWebTempParams.gate);
  195. ipaddr.addr = ipaddr_addr("192.168.14.48");
  196. netmask.addr = ipaddr_addr("255.255.255.0");
  197. gw.addr = ipaddr_addr("192.168.14.1");
  198. netif_set_addr(&netif, &ipaddr , &netmask, &gw);
  199. PRINT_USART("DHCP timeout\n\r");
  200. PRINT_USART("\n\rStatic IP address\n\r");
  201. PRINT_USART(ipaddr_ntoa(&ipaddr));
  202. PRINT_USART("\n\r");
  203. }
  204. }
  205. }
  206. break;
  207. default: break;
  208. }
  209. }
  210. /*********** Portions COPYRIGHT 2012 Embest Tech. Co., Ltd.*****END OF FILE****/