netconf.c 6.4 KB

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