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.

179 lines
6.2 KiB

2 years ago
2 years 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
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
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. uint8_t g_mac[6];
  42. static void ethernet_link_status_updated(struct netif *netif) {
  43. // uint32_t mode = config_cache.obtaining_ip_mode;
  44. // if (netif_is_up(netif)) {
  45. // ZLOGI(TAG, "ethernet_link_status_updated: netif_is_up");
  46. // if (mode == obtaining_ip_mode_type_static) {
  47. // m_getip = true;
  48. // }
  49. // } else {
  50. // ZLOGI(TAG, "ethernet_link_status_updated: netif_is_down");
  51. // m_getip = false;
  52. // }
  53. }
  54. static void ethernet_status_updated(struct netif *netif) {
  55. // if (netif->ip_addr.addr == 0) {
  56. // return;
  57. // }
  58. // ZLOGI(TAG, "---------------------get ip-----------------------");
  59. // ZLOGI(TAG, " IP address: %s", ip4addr_ntoa(netif_ip4_addr(netif)));
  60. // ZLOGI(TAG, " netmask : %s", ip4addr_ntoa(netif_ip4_netmask(netif)));
  61. // ZLOGI(TAG, " gateway : %s", ip4addr_ntoa(netif_ip4_gw(netif)));
  62. // m_getip = true;
  63. }
  64. netif_ext_callback_t extcb;
  65. void netif_ext_callback(struct netif *netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t *args) {
  66. uint32_t mode = config_cache.obtaining_ip_mode;
  67. if (mode == obtaining_ip_mode_type_static) {
  68. if (reason & LWIP_NSC_STATUS_CHANGED) {
  69. ZLOGI(TAG, "STATIC_IP: LWIP_NSC_STATUS_CHANGED %d", args->status_changed.state);
  70. }
  71. if (reason & LWIP_NSC_LINK_CHANGED) {
  72. ZLOGI(TAG, "STATIC_IP: LWIP_NSC_LINK_CHANGED %d", args->link_changed.state);
  73. if (args->link_changed.state) {
  74. m_networkisready = true;
  75. } else {
  76. m_networkisready = false;
  77. }
  78. }
  79. } else if (mode == obtaining_ip_mode_type_dhcp) {
  80. if (reason & LWIP_NSC_STATUS_CHANGED) {
  81. ZLOGI(TAG, "DHCP_MODE: LWIP_NSC_STATUS_CHANGED %d", args->status_changed.state);
  82. }
  83. if (reason & LWIP_NSC_LINK_CHANGED) {
  84. ZLOGI(TAG, "DHCP_MODE: LWIP_NSC_LINK_CHANGED %d", args->link_changed.state);
  85. if (!args->link_changed.state) {
  86. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  87. IP4_ADDR(&netmask, 0, 0, 0, 0);
  88. IP4_ADDR(&gw, 0, 0, 0, 0);
  89. netif_set_addr(netif, &ipaddr, &netmask, &gw);
  90. m_networkisready = false;
  91. }
  92. }
  93. 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) {
  94. if (netif->ip_addr.addr == 0) {
  95. ZLOGI(TAG, "clear ip");
  96. return;
  97. }
  98. ZLOGI(TAG, "DHCP_MODE: LWIP_NSC_IPV4_ADDRESS_CHANGED");
  99. ZLOGI(TAG, " IP address: %s", ip4addr_ntoa(netif_ip4_addr(netif)));
  100. ZLOGI(TAG, " netmask : %s", ip4addr_ntoa(netif_ip4_netmask(netif)));
  101. ZLOGI(TAG, " gateway : %s", ip4addr_ntoa(netif_ip4_gw(netif)));
  102. m_networkisready = true;
  103. }
  104. }
  105. }
  106. void network_service_init() {
  107. ZLOGI(TAG, "network_service_init");
  108. config_cache = *config_get();
  109. // config_cache.obtaining_ip_mode = 0;
  110. uint32_t mode = config_cache.obtaining_ip_mode;
  111. *(uint32_t *)(IP_ADDRESS) = config_cache.ip;
  112. *(uint32_t *)(NETMASK_ADDRESS) = config_cache.netmask;
  113. *(uint32_t *)(GATEWAY_ADDRESS) = config_cache.gw;
  114. if (mode == obtaining_ip_mode_type_static) {
  115. memcpy(g_mac, config_cache.mac, 6);
  116. tcpip_init(NULL, NULL);
  117. IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  118. IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1], NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  119. IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
  120. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  121. netif_set_default(&gnetif);
  122. if (netif_is_link_up(&gnetif)) {
  123. netif_set_up(&gnetif);
  124. } else {
  125. netif_set_down(&gnetif);
  126. }
  127. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  128. netif_set_status_callback(&gnetif, ethernet_status_updated);
  129. netif_add_ext_callback(&extcb, netif_ext_callback);
  130. osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512);
  131. osThreadCreate(osThread(EthLink), &gnetif);
  132. } else {
  133. memcpy(g_mac, config_cache.mac, 6);
  134. tcpip_init(NULL, NULL);
  135. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  136. IP4_ADDR(&netmask, 0, 0, 0, 0);
  137. IP4_ADDR(&gw, 0, 0, 0, 0);
  138. netif_set_hostname(&gnetif, PC_DEVICE_NAME);
  139. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  140. netif_set_default(&gnetif);
  141. if (netif_is_link_up(&gnetif)) {
  142. netif_set_up(&gnetif);
  143. } else {
  144. netif_set_down(&gnetif);
  145. }
  146. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  147. netif_set_status_callback(&gnetif, ethernet_status_updated);
  148. netif_add_ext_callback(&extcb, netif_ext_callback);
  149. osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512);
  150. osThreadCreate(osThread(EthLink), &gnetif);
  151. while (!netif_is_up(&gnetif)) {
  152. ZLOGI(TAG, "waiting for dhcp");
  153. osDelay(100);
  154. }
  155. dhcp_start(&gnetif);
  156. }
  157. }
  158. bool network_service_network_is_ready() { return m_networkisready; }