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.

152 lines
5.2 KiB

2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years 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. uint8_t g_mac[6];
  40. static void ethernet_link_status_updated(struct netif *netif) {
  41. if (netif_is_up(netif)) {
  42. ZLOGI(TAG, "ethernet_link_status_updated: netif_is_up");
  43. } else {
  44. ZLOGI(TAG, "ethernet_link_status_updated: netif_is_down");
  45. }
  46. }
  47. static void ethernet_status_updated(struct netif *netif) {
  48. ZLOGI(TAG, "dhcp success----");
  49. ZLOGI(TAG, " IP address: %s", ip4addr_ntoa(netif_ip4_addr(netif)));
  50. ZLOGI(TAG, " netmask : %s", ip4addr_ntoa(netif_ip4_netmask(netif)));
  51. ZLOGI(TAG, " gateway : %s", ip4addr_ntoa(netif_ip4_gw(netif)));
  52. }
  53. // tcpip_init( NULL, NULL );
  54. // /* IP addresses initialization with DHCP (IPv4) */
  55. // ipaddr.addr = 0;
  56. // netmask.addr = 0;
  57. // gw.addr = 0;
  58. // /* add the network interface (IPv4/IPv6) with RTOS */
  59. // netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  60. // /* Registers the default network interface */
  61. // netif_set_default(&gnetif);
  62. // if (netif_is_link_up(&gnetif))
  63. // {
  64. // /* When the netif is fully configured this function must be called */
  65. // netif_set_up(&gnetif);
  66. // }
  67. // else
  68. // {
  69. // /* When the netif link is down this function must be called */
  70. // netif_set_down(&gnetif);
  71. // }
  72. // /* Set the link callback function, this function is called on change of link status*/
  73. // netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  74. // /* Create the Ethernet link handler thread */
  75. // /* USER CODE BEGIN H7_OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 */
  76. // osThreadDef(EthLink, ethernet_link_thread, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE *2);
  77. // osThreadCreate (osThread(EthLink), &gnetif);
  78. // /* USER CODE END H7_OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 */
  79. // /* Start DHCP negotiation for a network interface (IPv4) */
  80. // dhcp_start(&gnetif);
  81. // typedef enum { obtaining_ip_mode_type_static = 0, obtaining_ip_mode_type_dhcp = 1, obtaining_ip_mode_type_lla = 2 } obtaining_ip_mode_t;
  82. void network_service_init() {
  83. ZLOGI(TAG, "network_service_init");
  84. uint32_t mode = config_get()->obtaining_ip_mode;
  85. *(uint32_t *)(IP_ADDRESS) = config_get()->ip;
  86. *(uint32_t *)(NETMASK_ADDRESS) = config_get()->netmask;
  87. *(uint32_t *)(GATEWAY_ADDRESS) = config_get()->gw;
  88. mode = 1;
  89. if (mode == obtaining_ip_mode_type_static) {
  90. memcpy(g_mac, config_get()->mac, 6);
  91. tcpip_init(NULL, NULL);
  92. IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  93. IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1], NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  94. IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
  95. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  96. netif_set_default(&gnetif);
  97. if (netif_is_link_up(&gnetif)) {
  98. netif_set_up(&gnetif);
  99. } else {
  100. netif_set_down(&gnetif);
  101. }
  102. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  103. osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512);
  104. osThreadCreate(osThread(EthLink), &gnetif);
  105. } else if (mode == obtaining_ip_mode_type_dhcp) {
  106. memcpy(g_mac, config_get()->mac, 6);
  107. tcpip_init(NULL, NULL);
  108. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  109. IP4_ADDR(&netmask, 0, 0, 0, 0);
  110. IP4_ADDR(&gw, 0, 0, 0, 0);
  111. netif_set_hostname(&gnetif, PC_DEVICE_NAME);
  112. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  113. netif_set_default(&gnetif);
  114. if (netif_is_link_up(&gnetif)) {
  115. netif_set_up(&gnetif);
  116. } else {
  117. netif_set_down(&gnetif);
  118. }
  119. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  120. netif_set_status_callback(&gnetif, ethernet_status_updated);
  121. osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512);
  122. osThreadCreate(osThread(EthLink), &gnetif);
  123. while (!netif_is_up(&gnetif)) {
  124. ZLOGI(TAG, "waiting for dhcp");
  125. osDelay(100);
  126. }
  127. dhcp_start(&gnetif);
  128. } else if (mode == obtaining_ip_mode_type_lla) {
  129. }
  130. }