netconf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "lwip/mem.h"
  3. #include "lwip/memp.h"
  4. #include "lwip/dhcp.h"
  5. #include "ethernetif.h"
  6. #include "tcpip.h"
  7. #include "main.h"
  8. #include "netconf.h"
  9. #include "common_config.h"
  10. #include "settings_api.h"
  11. #include "netconf.h"
  12. #include "tinystdio.h"
  13. #include "lwip/init.h"
  14. #include "lwip/timeouts.h"
  15. #include "netif/etharp.h"
  16. /* Private typedef -----------------------------------------------------------*/
  17. #define MAX_DHCP_TRIES 4
  18. /* Private define ------------------------------------------------------------*/
  19. typedef enum
  20. {
  21. DHCP_START=0,
  22. DHCP_WAIT_ADDRESS,
  23. DHCP_ADDRESS_ASSIGNED,
  24. DHCP_TIMEOUT
  25. }
  26. DHCP_State_TypeDef;
  27. /* Private macro -------------------------------------------------------------*/
  28. /* Private variables ---------------------------------------------------------*/
  29. struct netif netif;
  30. uint32_t TCPTimer = 0;
  31. uint32_t ARPTimer = 0;
  32. uint32_t DHCPfineTimer = 0;
  33. uint32_t DHCPcoarseTimer = 0;
  34. DHCP_State_TypeDef DHCP_state = DHCP_START;
  35. /**
  36. * @brief Общая структура настроек
  37. */
  38. extern SETTINGS_t sSettings;
  39. /* Private functions ---------------------------------------------------------*/
  40. void LwIP_DHCP_Process_Handle(void);
  41. /**
  42. * @brief Initializes the lwIP stack
  43. * @param None
  44. * @retval None
  45. */
  46. void LwIP_Init(void)
  47. {
  48. struct ip4_addr ipaddr;
  49. struct ip4_addr netmask;
  50. struct ip4_addr gw;
  51. char str[20];
  52. lwip_init();
  53. // /* Initializes the dynamic memory heap defined by MEM_SIZE.*/
  54. // mem_init();
  55. //
  56. // /* Initializes the memory pools defined by MEMP_NUM_x.*/
  57. // memp_init();
  58. if (sSettings.sWebParams.dhcpEnable)
  59. {
  60. ipaddr.addr = 0;
  61. netmask.addr = 0;
  62. gw.addr = 0;
  63. }
  64. else
  65. {
  66. sprintf(str, " %s\n\r", sSettings.sWebTempParams.ip);
  67. PRINT_USART("\n\rStatic IP address \n\r");
  68. PRINT_USART(str);
  69. ipaddr.addr = ipaddr_addr(sSettings.sWebParams.ip);
  70. netmask.addr = ipaddr_addr(sSettings.sWebParams.mask);
  71. gw.addr = ipaddr_addr(sSettings.sWebParams.gate);
  72. }
  73. netif_add(&netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
  74. netif_set_default(&netif);
  75. netif_set_link_up(&netif);
  76. netif_set_up(&netif);
  77. }
  78. /**
  79. * @brief Called when a frame is received
  80. * @param None
  81. * @retval None
  82. */
  83. void LwIP_Pkt_Handle(void)
  84. {
  85. /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
  86. ethernetif_input(&netif);
  87. }
  88. ///**
  89. // * @brief LwIP periodic tasks
  90. // * @param localtime the current LocalTime value
  91. // * @retval None
  92. // */
  93. //void LwIP_Periodic_Handle(__IO uint32_t localtime)
  94. //{
  95. //#if LWIP_TCP
  96. // /* TCP periodic process every 250 ms */
  97. // if (localtime - TCPTimer >= TCP_TMR_INTERVAL) {
  98. // TCPTimer = localtime;
  99. // tcp_tmr();
  100. // }
  101. //#endif
  102. //
  103. // /* ARP periodic process every 5s */
  104. // if ((localtime - ARPTimer) >= ARP_TMR_INTERVAL) {
  105. // ARPTimer = localtime;
  106. // etharp_tmr();
  107. // }
  108. //
  109. // if (sSettings.sWebParams.dhcpEnable)
  110. // {
  111. // /* Fine DHCP periodic process every 500ms */
  112. // if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS) {
  113. // DHCPfineTimer = localtime;
  114. // dhcp_fine_tmr();
  115. // if ((DHCP_state != DHCP_ADDRESS_ASSIGNED)&&(DHCP_state != DHCP_TIMEOUT)) {
  116. // /* process DHCP state machine */
  117. // LwIP_DHCP_Process_Handle();
  118. // }
  119. // }
  120. //
  121. // /* DHCP Coarse periodic process every 60s */
  122. // if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS) {
  123. // DHCPcoarseTimer = localtime;
  124. // dhcp_coarse_tmr();
  125. // }
  126. // }
  127. //
  128. //}
  129. /**
  130. * @brief LwIP periodic tasks
  131. * @param localtime the current LocalTime value
  132. * @retval None
  133. */
  134. void LwIP_Periodic_Handle(__IO uint32_t localtime)
  135. {
  136. if (sSettings.sWebParams.dhcpEnable)
  137. {
  138. /* Fine DHCP periodic process every 500ms */
  139. if ((DHCP_state != DHCP_ADDRESS_ASSIGNED)&&(DHCP_state != DHCP_TIMEOUT)) {
  140. /* process DHCP state machine */
  141. LwIP_DHCP_Process_Handle();
  142. }
  143. }
  144. sys_check_timeouts();
  145. }
  146. /**
  147. * @brief LwIP_DHCP_Process_Handle
  148. * @param None
  149. * @retval None
  150. */
  151. void LwIP_DHCP_Process_Handle()
  152. {
  153. struct ip4_addr ipaddr;
  154. struct ip4_addr netmask;
  155. struct ip4_addr gw;
  156. static uint8_t dhcpTry = 0;
  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 (dhcpTry++ > 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****/