Skip to content

Commit

Permalink
fix: align MAC address api with Arduino reference
Browse files Browse the repository at this point in the history
  • Loading branch information
fpistm committed Sep 11, 2024
1 parent 293b2d9 commit 5a50a4e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 24 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ localPort KEYWORD2
maintain KEYWORD2
linkStatus KEYWORD2
MACAddress KEYWORD2
setMACAddress KEYWORD2
subnetMask KEYWORD2
gatewayIP KEYWORD2
dnsServerIP KEYWORD2
setDnsServerIP KEYWORD2
setConnectionTimeout KEYWORD2

#######################################
Expand Down
8 changes: 6 additions & 2 deletions src/Dhcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 7 additions & 14 deletions src/STM32Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions src/STM32Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/utility/ethernetif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,18 @@ void ethernetif_set_mac_addr(const uint8_t *mac)
}
}

/**
* @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)
{
Expand Down
1 change: 1 addition & 0 deletions src/utility/ethernetif.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions src/utility/stm32_eth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/utility/stm32_eth.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 5a50a4e

Please sign in to comment.