From 7245be64d037f7dfe6595f1e479f86085315d385 Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Thu, 21 Dec 2023 19:21:37 +0100 Subject: [PATCH 1/7] EthernetClass - add setDnsServerIP as in Arduino Ethernet library --- src/STM32Ethernet.cpp | 5 +++++ src/STM32Ethernet.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/STM32Ethernet.cpp b/src/STM32Ethernet.cpp index a0753de..3925d0a 100644 --- a/src/STM32Ethernet.cpp +++ b/src/STM32Ethernet.cpp @@ -182,4 +182,9 @@ IPAddress EthernetClass::dnsServerIP() return _dnsServerAddress; } +void EthernetClass::setDnsServerIP(const IPAddress dns_server) +{ + _dnsServerAddress = dns_server; +} + EthernetClass Ethernet; diff --git a/src/STM32Ethernet.h b/src/STM32Ethernet.h index 502e256..c567ff6 100644 --- a/src/STM32Ethernet.h +++ b/src/STM32Ethernet.h @@ -50,6 +50,8 @@ class EthernetClass { IPAddress gatewayIP(); IPAddress dnsServerIP(); + void setDnsServerIP(const IPAddress dns_server); + friend class EthernetClient; friend class EthernetServer; }; From 52aba529c005e6e670a8483b090e78ab96a2ee17 Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Wed, 27 Dec 2023 20:04:02 +0100 Subject: [PATCH 2/7] EthernetClient.connect - return only 1 or 0 --- src/EthernetClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EthernetClient.cpp b/src/EthernetClient.cpp index aa2606b..9c71624 100644 --- a/src/EthernetClient.cpp +++ b/src/EthernetClient.cpp @@ -39,7 +39,7 @@ int EthernetClient::connect(const char *host, uint16_t port) if (ret == 1) { return connect(remote_addr, port); } else { - return ret; + return 0; } } From cc099cf6dbcc85171209e5f91c95db5a56238c05 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 10 Sep 2024 14:31:49 +0200 Subject: [PATCH 3/7] fix: typo raised by codespell Signed-off-by: Frederic Pillon --- src/utility/stm32_eth.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/stm32_eth.cpp b/src/utility/stm32_eth.cpp index 7b06955..e1c9669 100644 --- a/src/utility/stm32_eth.cpp +++ b/src/utility/stm32_eth.cpp @@ -117,7 +117,7 @@ static void TIM_scheduler_Config(void); #endif /** -* @brief Configurates the network interface +* @brief Configure the network interface * @param None * @retval None */ From 293b2d9ee3813cde905081f4cb8e08074b792480 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 10 Sep 2024 11:09:07 +0200 Subject: [PATCH 4/7] fix: use default MAC address only if no MAC_ADDRx defined Anyway the MAC address get by the application is always 0 while the real address is correct. Will be fixed by latter commit. Signed-off-by: Frederic Pillon --- src/STM32Ethernet.cpp | 18 ++---------------- src/STM32Ethernet.h | 1 - src/utility/ethernetif.cpp | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/STM32Ethernet.cpp b/src/STM32Ethernet.cpp index 3925d0a..2a9aa3d 100644 --- a/src/STM32Ethernet.cpp +++ b/src/STM32Ethernet.cpp @@ -5,7 +5,7 @@ int EthernetClass::begin(unsigned long timeout, unsigned long responseTimeout) { static DhcpClass s_dhcp; _dhcp = &s_dhcp; - stm32_eth_init(MACAddressDefault(), NULL, NULL, NULL); + stm32_eth_init(NULL, NULL, NULL, NULL); // Now try to get our config info from a DHCP server int ret = _dhcp->beginWithDHCP(mac_address, timeout, responseTimeout); @@ -39,7 +39,7 @@ void EthernetClass::begin(IPAddress local_ip, IPAddress subnet, IPAddress gatewa void EthernetClass::begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway, IPAddress dns_server) { - stm32_eth_init(MACAddressDefault(), local_ip.raw_address(), gateway.raw_address(), subnet.raw_address()); + stm32_eth_init(NULL, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address()); /* If there is a local DHCP informs it of our manual IP configuration to prevent IP conflict */ stm32_DHCP_manual_config(); @@ -133,20 +133,6 @@ void EthernetClass::schedule(void) stm32_eth_scheduler(); } -uint8_t *EthernetClass::MACAddressDefault(void) -{ - if ((mac_address[0] + mac_address[1] + mac_address[2] + mac_address[3] + mac_address[4] + mac_address[5]) == 0) { - uint32_t baseUID = *(uint32_t *)UID_BASE; - mac_address[0] = 0x00; - mac_address[1] = 0x80; - mac_address[2] = 0xE1; - mac_address[3] = (baseUID & 0x00FF0000) >> 16; - mac_address[4] = (baseUID & 0x0000FF00) >> 8; - mac_address[5] = (baseUID & 0x000000FF); - } - return mac_address; -} - void EthernetClass::MACAddress(uint8_t *mac) { mac_address[0] = mac[0]; diff --git a/src/STM32Ethernet.h b/src/STM32Ethernet.h index c567ff6..ad00608 100644 --- a/src/STM32Ethernet.h +++ b/src/STM32Ethernet.h @@ -18,7 +18,6 @@ class EthernetClass { IPAddress _dnsServerAddress; DhcpClass *_dhcp; uint8_t mac_address[6]; - uint8_t *MACAddressDefault(void); public: // Initialise the Ethernet with the internal provided MAC address and gain the rest of the diff --git a/src/utility/ethernetif.cpp b/src/utility/ethernetif.cpp index d254ece..fcb687f 100644 --- a/src/utility/ethernetif.cpp +++ b/src/utility/ethernetif.cpp @@ -96,6 +96,25 @@ __ALIGN_BEGIN uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE] __ALIGN_END; /* Ethe static ETH_HandleTypeDef EthHandle; +/* If default MAC fields is not defined use default values based on UID */ +#if !defined(MAC_ADDR0) +#define MAC_ADDR0 0x00 +#endif +#if !defined(MAC_ADDR1) +#define MAC_ADDR1 0x80 +#endif +#if !defined(MAC_ADDR2) +#define MAC_ADDR2 0xE1 +#endif +#if !defined(MAC_ADDR3) +#define MAC_ADDR3 ((uint8_t)(((*(uint32_t *)UID_BASE) & 0x00FF0000) >> 16)) +#endif +#if !defined(MAC_ADDR4) +#define MAC_ADDR4 ((uint8_t)(((*(uint32_t *)UID_BASE) & 0x0000FF00) >> 8)) +#endif +#if !defined(MAC_ADDR5) +#define MAC_ADDR5 ((uint8_t)((*(uint32_t *)UID_BASE) & 0x000000FF)) +#endif static uint8_t macaddress[6] = { MAC_ADDR0, MAC_ADDR1, MAC_ADDR2, MAC_ADDR3, MAC_ADDR4, MAC_ADDR5 }; #if LWIP_IGMP From 348c968de150003a6e80cb61f43a72bf2f5ced43 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 11 Sep 2024 10:08:17 +0200 Subject: [PATCH 5/7] fix: align MAC address api with Arduino reference References: https://www.arduino.cc/reference/en/libraries/ethernet/ethernet.macaddress/ https://www.arduino.cc/reference/en/libraries/ethernet/ethernet.setmacaddress/ Pay attention that setMACAddress have to be called before Begin(). Else new MAC will be ignored. Fixes #81 Signed-off-by: Frederic Pillon --- README.md | 10 +++++----- keywords.txt | 2 ++ src/Dhcp.cpp | 8 ++++++-- src/STM32Ethernet.cpp | 21 +++++++-------------- src/STM32Ethernet.h | 5 ++--- src/utility/ethernetif.cpp | 14 +++++++++++++- src/utility/ethernetif.h | 1 + src/utility/stm32_eth.cpp | 19 +++++++++++++++++++ src/utility/stm32_eth.h | 2 ++ 9 files changed, 57 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index b87f2b2..0256ceb 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ extend the default one by adding some extra configuration in a file named `lwipo ## New alternative init procedure **!!!** -There are alternative inits of the Ethernetinterface with following orders: +There are alternative inits of the Ethernet interface with following orders: Ethernet.begin(); Ethernet.begin(ip); @@ -39,16 +39,16 @@ There are alternative inits of the Ethernetinterface with following orders: This is more logical. A MAC address is no more needed and will retrieved internally by the mbed MAC address! -You can get the MAC address with following function, this must done after Ethernet.Begin() +You can get the MAC address with following function, this must be done after Ethernet.Begin() uint8_t *mac; Ethernet.begin(); - mac = Ethernet.MACAddress(); + Ethernet.MACAddress(mac); -You can also set a new user based MAC address, this must done before Ethernet.begin() +You can also set a new user based MAC address, this must be done before Ethernet.begin() uint8_t newMAC[] = {0x00, 0x80, 0xE1, 0x01, 0x01, 0x01}; - Ethernet.MACAddress(newMAC); + Ethernet.setMACAddress(newMAC); Ethernet.begin(); ## Note diff --git a/keywords.txt b/keywords.txt index 09e494c..469aa86 100644 --- a/keywords.txt +++ b/keywords.txt @@ -38,9 +38,11 @@ localPort KEYWORD2 maintain KEYWORD2 linkStatus KEYWORD2 MACAddress KEYWORD2 +setMACAddress KEYWORD2 subnetMask KEYWORD2 gatewayIP KEYWORD2 dnsServerIP KEYWORD2 +setDnsServerIP KEYWORD2 setConnectionTimeout KEYWORD2 ####################################### diff --git a/src/Dhcp.cpp b/src/Dhcp.cpp index 1725e4f..19c7705 100644 --- a/src/Dhcp.cpp +++ b/src/Dhcp.cpp @@ -16,8 +16,12 @@ int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long // zero out _dhcpMacAddr memset(_dhcpMacAddr, 0, 6); reset_DHCP_lease(); - - memcpy((void *)_dhcpMacAddr, (void *)mac, 6); + if (mac == NULL) { + // use mac from Ethernet chip + stm32_eth_get_macaddr(_dhcpMacAddr); + } else { + memcpy((void *)_dhcpMacAddr, (void *)mac, 6); + } _dhcp_state = STATE_DHCP_START; stm32_set_DHCP_state(_dhcp_state); return request_DHCP_lease(); diff --git a/src/STM32Ethernet.cpp b/src/STM32Ethernet.cpp index 2a9aa3d..1e98284 100644 --- a/src/STM32Ethernet.cpp +++ b/src/STM32Ethernet.cpp @@ -8,7 +8,7 @@ int EthernetClass::begin(unsigned long timeout, unsigned long responseTimeout) stm32_eth_init(NULL, NULL, NULL, NULL); // Now try to get our config info from a DHCP server - int ret = _dhcp->beginWithDHCP(mac_address, timeout, responseTimeout); + int ret = _dhcp->beginWithDHCP(NULL, timeout, responseTimeout); if (ret == 1) { _dnsServerAddress = _dhcp->getDnsServerIp(); } @@ -58,7 +58,6 @@ int EthernetClass::begin(uint8_t *mac_address, unsigned long timeout, unsigned l if (ret == 1) { _dnsServerAddress = _dhcp->getDnsServerIp(); } - MACAddress(mac_address); return ret; } @@ -86,14 +85,13 @@ void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dn begin(mac_address, local_ip, dns_server, gateway, subnet); } -void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) +void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) { - stm32_eth_init(mac, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address()); + stm32_eth_init(mac_address, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address()); /* If there is a local DHCP informs it of our manual IP configuration to prevent IP conflict */ stm32_DHCP_manual_config(); _dnsServerAddress = dns_server; - MACAddress(mac); } EthernetLinkStatus EthernetClass::linkStatus() @@ -133,19 +131,14 @@ void EthernetClass::schedule(void) stm32_eth_scheduler(); } -void EthernetClass::MACAddress(uint8_t *mac) +void EthernetClass::setMACAddress(const uint8_t *mac_address) { - mac_address[0] = mac[0]; - mac_address[1] = mac[1]; - mac_address[2] = mac[2]; - mac_address[3] = mac[3]; - mac_address[4] = mac[4]; - mac_address[5] = mac[5]; + stm32_eth_set_macaddr(mac_address); } -uint8_t *EthernetClass::MACAddress(void) +void EthernetClass::MACAddress(uint8_t *mac_address) { - return mac_address; + stm32_eth_get_macaddr(mac_address); } IPAddress EthernetClass::localIP() diff --git a/src/STM32Ethernet.h b/src/STM32Ethernet.h index ad00608..f0ecfff 100644 --- a/src/STM32Ethernet.h +++ b/src/STM32Ethernet.h @@ -17,7 +17,6 @@ class EthernetClass { private: IPAddress _dnsServerAddress; DhcpClass *_dhcp; - uint8_t mac_address[6]; public: // Initialise the Ethernet with the internal provided MAC address and gain the rest of the @@ -42,13 +41,13 @@ class EthernetClass { int maintain(); void schedule(void); - void MACAddress(uint8_t *mac); - uint8_t *MACAddress(void); + void MACAddress(uint8_t *mac_address); IPAddress localIP(); IPAddress subnetMask(); IPAddress gatewayIP(); IPAddress dnsServerIP(); + void setMACAddress(const uint8_t *mac_address); void setDnsServerIP(const IPAddress dns_server); friend class EthernetClient; diff --git a/src/utility/ethernetif.cpp b/src/utility/ethernetif.cpp index fcb687f..9cb2695 100644 --- a/src/utility/ethernetif.cpp +++ b/src/utility/ethernetif.cpp @@ -627,11 +627,23 @@ __weak void ethernetif_notify_conn_changed(struct netif *netif) */ void ethernetif_set_mac_addr(const uint8_t *mac) { - if (mac != NULL) { + if ((mac != NULL) && !(ethernetif_is_init())) { memcpy(macaddress, mac, 6); } } +/** + * @brief This function get the current MAC address. + * @param mac: mac address + * @retval None + */ +void ethernetif_get_mac_addr(uint8_t *mac) +{ + if (mac != NULL) { + memcpy(mac, macaddress, 6); + } +} + #if LWIP_IGMP err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *ip4_addr, netif_mac_filter_action action) { diff --git a/src/utility/ethernetif.h b/src/utility/ethernetif.h index bab073e..ec7027b 100644 --- a/src/utility/ethernetif.h +++ b/src/utility/ethernetif.h @@ -61,6 +61,7 @@ void ethernetif_update_config(struct netif *netif); void ethernetif_notify_conn_changed(struct netif *netif); void ethernetif_set_mac_addr(const uint8_t *mac); +void ethernetif_get_mac_addr(uint8_t *mac); #if LWIP_IGMP err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *ip4_addr, netif_mac_filter_action action); diff --git a/src/utility/stm32_eth.cpp b/src/utility/stm32_eth.cpp index e1c9669..3a79595 100644 --- a/src/utility/stm32_eth.cpp +++ b/src/utility/stm32_eth.cpp @@ -268,6 +268,25 @@ uint8_t stm32_eth_is_init(void) return ethernetif_is_init(); } +/** + * @brief Set Ethernet MAC address + * @param mac: mac address + * @retval None + */ +void stm32_eth_set_macaddr(const uint8_t *mac) +{ + ethernetif_set_mac_addr(mac); +} +/** + * @brief Return Ethernet MAC address + * @param mac: mac address + * @retval None + */ +void stm32_eth_get_macaddr(uint8_t *mac) +{ + return ethernetif_get_mac_addr(mac); +} + /** * @brief Return Ethernet link status * @param None diff --git a/src/utility/stm32_eth.h b/src/utility/stm32_eth.h index d4c3ff2..753e06c 100644 --- a/src/utility/stm32_eth.h +++ b/src/utility/stm32_eth.h @@ -119,6 +119,8 @@ struct tcp_struct { /* Exported functions ------------------------------------------------------- */ void stm32_eth_init(const uint8_t *mac, const uint8_t *ip, const uint8_t *gw, const uint8_t *netmask); uint8_t stm32_eth_is_init(void); +void stm32_eth_get_macaddr(uint8_t *mac); +void stm32_eth_set_macaddr(const uint8_t *mac); uint8_t stm32_eth_link_up(void); void stm32_eth_scheduler(void); From cc8310f100c564a587669ba17d56483926780cf0 Mon Sep 17 00:00:00 2001 From: patricklaf Date: Thu, 27 Jun 2024 11:24:27 +0200 Subject: [PATCH 6/7] peek() function always return first byte of data Signed-off-by: patricklaf --- src/EthernetClient.cpp | 2 +- src/EthernetUdp.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EthernetClient.cpp b/src/EthernetClient.cpp index 9c71624..70bb01d 100644 --- a/src/EthernetClient.cpp +++ b/src/EthernetClient.cpp @@ -171,7 +171,7 @@ int EthernetClient::peek() if (!available()) { return -1; } - b = pbuf_get_at(_tcp_client->data.p, 0); + b = pbuf_get_at(_tcp_client->data.p, _tcp_client->data.p->tot_len - _tcp_client->data.available); return b; } diff --git a/src/EthernetUdp.cpp b/src/EthernetUdp.cpp index 78df02e..443f898 100644 --- a/src/EthernetUdp.cpp +++ b/src/EthernetUdp.cpp @@ -247,7 +247,7 @@ int EthernetUDP::peek() if (!_remaining) { return -1; } - b = pbuf_get_at(_udp.data.p, 0); + b = pbuf_get_at(_udp.data.p, _udp.data.p->tot_len - _udp.data.available); return b; } From b44edf68e1bb967b2a9df4b4e71c8d2e3ff70b0f Mon Sep 17 00:00:00 2001 From: TingRua zhang <68175738+q1098401179@users.noreply.github.com> Date: Fri, 7 Feb 2025 18:31:35 +0800 Subject: [PATCH 7/7] Update EthernetClient.cpp (#94) Signed-off-by: TingRua zhang <68175738+q1098401179@users.noreply.github.com> Co-authored-by: Frederic Pillon --- src/EthernetClient.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/EthernetClient.cpp b/src/EthernetClient.cpp index 70bb01d..5941afd 100644 --- a/src/EthernetClient.cpp +++ b/src/EthernetClient.cpp @@ -195,6 +195,7 @@ void EthernetClient::stop() tcp_connection_close(_tcp_client->pcb, _tcp_client); } mem_free(_tcp_client); + _tcp_client = NULL; } uint8_t EthernetClient::connected()