Skip to content

Commit 348c968

Browse files
committed
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 <[email protected]>
1 parent 293b2d9 commit 348c968

9 files changed

+57
-25
lines changed

Diff for: README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extend the default one by adding some extra configuration in a file named `lwipo
2929

3030
## New alternative init procedure **!!!**
3131

32-
There are alternative inits of the Ethernetinterface with following orders:
32+
There are alternative inits of the Ethernet interface with following orders:
3333

3434
Ethernet.begin();
3535
Ethernet.begin(ip);
@@ -39,16 +39,16 @@ There are alternative inits of the Ethernetinterface with following orders:
3939

4040
This is more logical. A MAC address is no more needed and will retrieved internally by the mbed MAC address!
4141

42-
You can get the MAC address with following function, this must done after Ethernet.Begin()
42+
You can get the MAC address with following function, this must be done after Ethernet.Begin()
4343

4444
uint8_t *mac;
4545
Ethernet.begin();
46-
mac = Ethernet.MACAddress();
46+
Ethernet.MACAddress(mac);
4747

48-
You can also set a new user based MAC address, this must done before Ethernet.begin()
48+
You can also set a new user based MAC address, this must be done before Ethernet.begin()
4949

5050
uint8_t newMAC[] = {0x00, 0x80, 0xE1, 0x01, 0x01, 0x01};
51-
Ethernet.MACAddress(newMAC);
51+
Ethernet.setMACAddress(newMAC);
5252
Ethernet.begin();
5353

5454
## Note

Diff for: keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ localPort KEYWORD2
3838
maintain KEYWORD2
3939
linkStatus KEYWORD2
4040
MACAddress KEYWORD2
41+
setMACAddress KEYWORD2
4142
subnetMask KEYWORD2
4243
gatewayIP KEYWORD2
4344
dnsServerIP KEYWORD2
45+
setDnsServerIP KEYWORD2
4446
setConnectionTimeout KEYWORD2
4547

4648
#######################################

Diff for: src/Dhcp.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long
1616
// zero out _dhcpMacAddr
1717
memset(_dhcpMacAddr, 0, 6);
1818
reset_DHCP_lease();
19-
20-
memcpy((void *)_dhcpMacAddr, (void *)mac, 6);
19+
if (mac == NULL) {
20+
// use mac from Ethernet chip
21+
stm32_eth_get_macaddr(_dhcpMacAddr);
22+
} else {
23+
memcpy((void *)_dhcpMacAddr, (void *)mac, 6);
24+
}
2125
_dhcp_state = STATE_DHCP_START;
2226
stm32_set_DHCP_state(_dhcp_state);
2327
return request_DHCP_lease();

Diff for: src/STM32Ethernet.cpp

+7-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int EthernetClass::begin(unsigned long timeout, unsigned long responseTimeout)
88
stm32_eth_init(NULL, NULL, NULL, NULL);
99

1010
// Now try to get our config info from a DHCP server
11-
int ret = _dhcp->beginWithDHCP(mac_address, timeout, responseTimeout);
11+
int ret = _dhcp->beginWithDHCP(NULL, timeout, responseTimeout);
1212
if (ret == 1) {
1313
_dnsServerAddress = _dhcp->getDnsServerIp();
1414
}
@@ -58,7 +58,6 @@ int EthernetClass::begin(uint8_t *mac_address, unsigned long timeout, unsigned l
5858
if (ret == 1) {
5959
_dnsServerAddress = _dhcp->getDnsServerIp();
6060
}
61-
MACAddress(mac_address);
6261
return ret;
6362
}
6463

@@ -86,14 +85,13 @@ void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dn
8685
begin(mac_address, local_ip, dns_server, gateway, subnet);
8786
}
8887

89-
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
88+
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
9089
{
91-
stm32_eth_init(mac, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
90+
stm32_eth_init(mac_address, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
9291
/* If there is a local DHCP informs it of our manual IP configuration to
9392
prevent IP conflict */
9493
stm32_DHCP_manual_config();
9594
_dnsServerAddress = dns_server;
96-
MACAddress(mac);
9795
}
9896

9997
EthernetLinkStatus EthernetClass::linkStatus()
@@ -133,19 +131,14 @@ void EthernetClass::schedule(void)
133131
stm32_eth_scheduler();
134132
}
135133

136-
void EthernetClass::MACAddress(uint8_t *mac)
134+
void EthernetClass::setMACAddress(const uint8_t *mac_address)
137135
{
138-
mac_address[0] = mac[0];
139-
mac_address[1] = mac[1];
140-
mac_address[2] = mac[2];
141-
mac_address[3] = mac[3];
142-
mac_address[4] = mac[4];
143-
mac_address[5] = mac[5];
136+
stm32_eth_set_macaddr(mac_address);
144137
}
145138

146-
uint8_t *EthernetClass::MACAddress(void)
139+
void EthernetClass::MACAddress(uint8_t *mac_address)
147140
{
148-
return mac_address;
141+
stm32_eth_get_macaddr(mac_address);
149142
}
150143

151144
IPAddress EthernetClass::localIP()

Diff for: src/STM32Ethernet.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class EthernetClass {
1717
private:
1818
IPAddress _dnsServerAddress;
1919
DhcpClass *_dhcp;
20-
uint8_t mac_address[6];
2120

2221
public:
2322
// Initialise the Ethernet with the internal provided MAC address and gain the rest of the
@@ -42,13 +41,13 @@ class EthernetClass {
4241
int maintain();
4342
void schedule(void);
4443

45-
void MACAddress(uint8_t *mac);
46-
uint8_t *MACAddress(void);
44+
void MACAddress(uint8_t *mac_address);
4745
IPAddress localIP();
4846
IPAddress subnetMask();
4947
IPAddress gatewayIP();
5048
IPAddress dnsServerIP();
5149

50+
void setMACAddress(const uint8_t *mac_address);
5251
void setDnsServerIP(const IPAddress dns_server);
5352

5453
friend class EthernetClient;

Diff for: src/utility/ethernetif.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,23 @@ __weak void ethernetif_notify_conn_changed(struct netif *netif)
627627
*/
628628
void ethernetif_set_mac_addr(const uint8_t *mac)
629629
{
630-
if (mac != NULL) {
630+
if ((mac != NULL) && !(ethernetif_is_init())) {
631631
memcpy(macaddress, mac, 6);
632632
}
633633
}
634634

635+
/**
636+
* @brief This function get the current MAC address.
637+
* @param mac: mac address
638+
* @retval None
639+
*/
640+
void ethernetif_get_mac_addr(uint8_t *mac)
641+
{
642+
if (mac != NULL) {
643+
memcpy(mac, macaddress, 6);
644+
}
645+
}
646+
635647
#if LWIP_IGMP
636648
err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *ip4_addr, netif_mac_filter_action action)
637649
{

Diff for: src/utility/ethernetif.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void ethernetif_update_config(struct netif *netif);
6161
void ethernetif_notify_conn_changed(struct netif *netif);
6262

6363
void ethernetif_set_mac_addr(const uint8_t *mac);
64+
void ethernetif_get_mac_addr(uint8_t *mac);
6465

6566
#if LWIP_IGMP
6667
err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *ip4_addr, netif_mac_filter_action action);

Diff for: src/utility/stm32_eth.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,25 @@ uint8_t stm32_eth_is_init(void)
268268
return ethernetif_is_init();
269269
}
270270

271+
/**
272+
* @brief Set Ethernet MAC address
273+
* @param mac: mac address
274+
* @retval None
275+
*/
276+
void stm32_eth_set_macaddr(const uint8_t *mac)
277+
{
278+
ethernetif_set_mac_addr(mac);
279+
}
280+
/**
281+
* @brief Return Ethernet MAC address
282+
* @param mac: mac address
283+
* @retval None
284+
*/
285+
void stm32_eth_get_macaddr(uint8_t *mac)
286+
{
287+
return ethernetif_get_mac_addr(mac);
288+
}
289+
271290
/**
272291
* @brief Return Ethernet link status
273292
* @param None

Diff for: src/utility/stm32_eth.h

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ struct tcp_struct {
119119
/* Exported functions ------------------------------------------------------- */
120120
void stm32_eth_init(const uint8_t *mac, const uint8_t *ip, const uint8_t *gw, const uint8_t *netmask);
121121
uint8_t stm32_eth_is_init(void);
122+
void stm32_eth_get_macaddr(uint8_t *mac);
123+
void stm32_eth_set_macaddr(const uint8_t *mac);
122124
uint8_t stm32_eth_link_up(void);
123125
void stm32_eth_scheduler(void);
124126

0 commit comments

Comments
 (0)