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.

187 lines
6.4 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
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
2 years 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 (!dhcp_is_start) {
  94. dhcp_start(&gnetif);
  95. dhcp_is_start = true;
  96. }
  97. if (args->link_changed.state) {
  98. if (netif->ip_addr.addr != 0) {
  99. m_networkisready = true;
  100. }
  101. } else {
  102. m_networkisready = false;
  103. }
  104. }
  105. 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) {
  106. if (netif->ip_addr.addr == 0) {
  107. ZLOGI(TAG, "clear ip");
  108. return;
  109. }
  110. ZLOGI(TAG, "DHCP_MODE: LWIP_NSC_IPV4_ADDRESS_CHANGED");
  111. ZLOGI(TAG, " IP address: %s", ip4addr_ntoa(netif_ip4_addr(netif)));
  112. ZLOGI(TAG, " netmask : %s", ip4addr_ntoa(netif_ip4_netmask(netif)));
  113. ZLOGI(TAG, " gateway : %s", ip4addr_ntoa(netif_ip4_gw(netif)));
  114. m_networkisready = true;
  115. }
  116. }
  117. }
  118. void network_service_init() {
  119. ZLOGI(TAG, "network_service_init");
  120. config_cache = *config_get();
  121. // config_cache.obtaining_ip_mode = 0;
  122. uint32_t mode = config_cache.obtaining_ip_mode;
  123. *(uint32_t *)(IP_ADDRESS) = config_cache.ip;
  124. *(uint32_t *)(NETMASK_ADDRESS) = config_cache.netmask;
  125. *(uint32_t *)(GATEWAY_ADDRESS) = config_cache.gw;
  126. if (mode == obtaining_ip_mode_type_static) {
  127. memcpy(g_mac, config_cache.mac, 6);
  128. tcpip_init(NULL, NULL);
  129. IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  130. IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1], NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  131. IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
  132. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  133. netif_set_default(&gnetif);
  134. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  135. netif_set_status_callback(&gnetif, ethernet_status_updated);
  136. netif_add_ext_callback(&extcb, netif_ext_callback);
  137. if (netif_is_link_up(&gnetif)) {
  138. netif_set_up(&gnetif);
  139. } else {
  140. netif_set_down(&gnetif);
  141. }
  142. osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512);
  143. osThreadCreate(osThread(EthLink), &gnetif);
  144. } else {
  145. memcpy(g_mac, config_cache.mac, 6);
  146. tcpip_init(NULL, NULL);
  147. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  148. IP4_ADDR(&netmask, 0, 0, 0, 0);
  149. IP4_ADDR(&gw, 0, 0, 0, 0);
  150. netif_set_hostname(&gnetif, PC_DEVICE_NAME);
  151. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  152. netif_set_default(&gnetif);
  153. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  154. netif_set_status_callback(&gnetif, ethernet_status_updated);
  155. netif_add_ext_callback(&extcb, netif_ext_callback);
  156. if (netif_is_link_up(&gnetif)) {
  157. netif_set_up(&gnetif);
  158. } else {
  159. netif_set_down(&gnetif);
  160. }
  161. osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 1024);
  162. osThreadCreate(osThread(EthLink), &gnetif);
  163. }
  164. }
  165. bool network_service_network_is_ready() { return m_networkisready; }