You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.8 KiB

2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
1 year ago
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : LWIP.c
  5. * Description : This file provides initialization code for LWIP
  6. * middleWare.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "lwip.h"
  22. #include "lwip/init.h"
  23. #include "lwip/netif.h"
  24. #if defined(__CC_ARM)
  25. #include "lwip/sio.h"
  26. #endif
  27. #include "ethernetif.h"
  28. //
  29. #include "base_service/base_service.h"
  30. #include "base_service/task_level_config.h"
  31. #define TAG "network"
  32. static struct netif gnetif;
  33. static ip4_addr_t ipaddr;
  34. static ip4_addr_t netmask;
  35. static ip4_addr_t gw;
  36. static uint8_t IP_ADDRESS[4];
  37. static uint8_t NETMASK_ADDRESS[4];
  38. static uint8_t GATEWAY_ADDRESS[4];
  39. static bool m_networkisready;
  40. static config_t config_cache;
  41. static bool dhcp_is_start;
  42. uint8_t g_mac[6];
  43. static void ethernet_link_status_updated(struct netif *netif) {
  44. // uint32_t mode = config_cache.obtaining_ip_mode;
  45. // if (netif_is_up(netif)) {
  46. // ZLOGI(TAG, "ethernet_link_status_updated: netif_is_up");
  47. // if (mode == obtaining_ip_mode_type_static) {
  48. // m_getip = true;
  49. // }
  50. // } else {
  51. // ZLOGI(TAG, "ethernet_link_status_updated: netif_is_down");
  52. // m_getip = false;
  53. // }
  54. }
  55. static void ethernet_status_updated(struct netif *netif) {
  56. // if (netif->ip_addr.addr == 0) {
  57. // return;
  58. // }
  59. // ZLOGI(TAG, "---------------------get ip-----------------------");
  60. // ZLOGI(TAG, " IP address: %s", ip4addr_ntoa(netif_ip4_addr(netif)));
  61. // ZLOGI(TAG, " netmask : %s", ip4addr_ntoa(netif_ip4_netmask(netif)));
  62. // ZLOGI(TAG, " gateway : %s", ip4addr_ntoa(netif_ip4_gw(netif)));
  63. // m_getip = true;
  64. }
  65. netif_ext_callback_t extcb;
  66. void netif_ext_callback(struct netif *netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t *args) {
  67. uint32_t mode = config_cache.obtaining_ip_mode;
  68. if (mode == obtaining_ip_mode_type_static) {
  69. if (reason & LWIP_NSC_STATUS_CHANGED) {
  70. ZLOGI(TAG, "STATIC_IP: LWIP_NSC_STATUS_CHANGED %d", args->status_changed.state);
  71. }
  72. if (reason & LWIP_NSC_LINK_CHANGED) {
  73. ZLOGI(TAG, "STATIC_IP: LWIP_NSC_LINK_CHANGED %d", args->link_changed.state);
  74. if (args->link_changed.state) {
  75. m_networkisready = true;
  76. } else {
  77. m_networkisready = false;
  78. }
  79. }
  80. } else if (mode == obtaining_ip_mode_type_dhcp) {
  81. if (reason & LWIP_NSC_STATUS_CHANGED) {
  82. ZLOGI(TAG, "DHCP_MODE: LWIP_NSC_STATUS_CHANGED %d", args->status_changed.state);
  83. }
  84. if (reason & LWIP_NSC_LINK_CHANGED) {
  85. ZLOGI(TAG, "DHCP_MODE: LWIP_NSC_LINK_CHANGED %d", args->link_changed.state);
  86. // if (!args->link_changed.state) {
  87. // IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  88. // IP4_ADDR(&netmask, 0, 0, 0, 0);
  89. // IP4_ADDR(&gw, 0, 0, 0, 0);
  90. // netif_set_addr(netif, &ipaddr, &netmask, &gw);
  91. // m_networkisready = false;
  92. // }
  93. if (args->link_changed.state) {
  94. // if (!dhcp_is_start) {
  95. // dhcp_start(&gnetif);
  96. // dhcp_is_start = true;
  97. // ZLOGI(TAG, "start dhcp");
  98. // }
  99. if (netif->ip_addr.addr != 0) {
  100. m_networkisready = true;
  101. }
  102. } else {
  103. ZLOGI(TAG, "stop dhcp");
  104. if (dhcp_is_start) {
  105. dhcp_stop(&gnetif);
  106. dhcp_is_start = false;
  107. }
  108. m_networkisready = false;
  109. }
  110. }
  111. if (reason & LWIP_NSC_IPV4_ADDRESS_CHANGED || reason & LWIP_NSC_IPV4_GATEWAY_CHANGED || reason & LWIP_NSC_IPV4_NETMASK_CHANGED || reason & LWIP_NSC_IPV4_SETTINGS_CHANGED) {
  112. if (netif->ip_addr.addr == 0) {
  113. ZLOGI(TAG, "clear ip");
  114. return;
  115. }
  116. ZLOGI(TAG, "DHCP_MODE: LWIP_NSC_IPV4_ADDRESS_CHANGED");
  117. ZLOGI(TAG, " IP address: %s", ip4addr_ntoa(netif_ip4_addr(netif)));
  118. ZLOGI(TAG, " netmask : %s", ip4addr_ntoa(netif_ip4_netmask(netif)));
  119. ZLOGI(TAG, " gateway : %s", ip4addr_ntoa(netif_ip4_gw(netif)));
  120. m_networkisready = true;
  121. }
  122. }
  123. }
  124. void network_service_init() {
  125. ZLOGI(TAG, "network_service_init");
  126. config_cache = *config_get();
  127. // config_cache.obtaining_ip_mode = 0;
  128. uint32_t mode = config_cache.obtaining_ip_mode;
  129. *(uint32_t *)(IP_ADDRESS) = config_cache.ip;
  130. *(uint32_t *)(NETMASK_ADDRESS) = config_cache.netmask;
  131. *(uint32_t *)(GATEWAY_ADDRESS) = config_cache.gw;
  132. if (mode == obtaining_ip_mode_type_static) {
  133. memcpy(g_mac, config_cache.mac, 6);
  134. tcpip_init(NULL, NULL);
  135. IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  136. IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1], NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  137. IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
  138. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  139. netif_set_default(&gnetif);
  140. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  141. netif_set_status_callback(&gnetif, ethernet_status_updated);
  142. netif_add_ext_callback(&extcb, netif_ext_callback);
  143. if (netif_is_link_up(&gnetif)) {
  144. netif_set_up(&gnetif);
  145. } else {
  146. netif_set_down(&gnetif);
  147. }
  148. osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512);
  149. osThreadCreate(osThread(EthLink), &gnetif);
  150. } else {
  151. memcpy(g_mac, config_cache.mac, 6);
  152. tcpip_init(NULL, NULL);
  153. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  154. IP4_ADDR(&netmask, 0, 0, 0, 0);
  155. IP4_ADDR(&gw, 0, 0, 0, 0);
  156. netif_set_hostname(&gnetif, PC_DEVICE_NAME);
  157. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  158. netif_set_default(&gnetif);
  159. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  160. netif_set_status_callback(&gnetif, ethernet_status_updated);
  161. netif_add_ext_callback(&extcb, netif_ext_callback);
  162. if (netif_is_link_up(&gnetif)) {
  163. netif_set_up(&gnetif);
  164. } else {
  165. netif_set_down(&gnetif);
  166. }
  167. osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 1024);
  168. osThreadCreate(osThread(EthLink), &gnetif);
  169. }
  170. }
  171. void network_service_schedule() {
  172. if (!dhcp_is_start && netif_is_up(&gnetif)) {
  173. err_t err = dhcp_start(&gnetif);
  174. if (err != 0) {
  175. return;
  176. }
  177. dhcp_is_start = true;
  178. ZLOGI(TAG, "start dhcp");
  179. }
  180. }
  181. bool network_service_network_is_ready() { return m_networkisready; }