Skip to content

Commit f6f0286

Browse files
author
Arto Kinnunen
committed
Method for adding network interface MAC address
Add method set_mac_address to set network interface MAC address.
1 parent d02756f commit f6f0286

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

features/nanostack/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Nanostack::Interface : public OnboardNetworkStack::Interface, private mbed
2929
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
3030
virtual char *get_ip_address(char *buf, nsapi_size_t buflen);
3131
virtual char *get_mac_address(char *buf, nsapi_size_t buflen);
32+
virtual nsapi_error_t set_mac_address(uint8_t *buf, nsapi_size_t buflen);
3233
virtual nsapi_error_t get_netmask(SocketAddress *address);
3334
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
3435
virtual char *get_netmask(char *buf, nsapi_size_t buflen);
@@ -118,6 +119,9 @@ class InterfaceNanostack : public virtual NetworkInterface {
118119
*/
119120
virtual const char *get_mac_address();
120121

122+
/** @copydoc NetworkInterface::set_mac_address */
123+
virtual nsapi_error_t set_mac_address(uint8_t *mac_addr, size_t addr_len);
124+
121125
/** Register callback for status reporting
122126
*
123127
* The specified status callback function will be called on status changes

features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ char *Nanostack::Interface::get_mac_address(char *buf, nsapi_size_t buflen)
6161
}
6262
}
6363

64+
nsapi_error_t Nanostack::Interface::set_mac_address(uint8_t *buf, nsapi_size_t buflen)
65+
{
66+
if (buflen != 8) {
67+
/* Provided MAC is too short */
68+
return NSAPI_ERROR_PARAMETER;
69+
}
70+
71+
if (_device_id >= 0) {
72+
/* device is already registered, can't set MAC address anymore */
73+
return NSAPI_ERROR_BUSY;
74+
}
75+
76+
NanostackMACPhy *phy = interface_phy.nanostack_mac_phy();
77+
if (phy) {
78+
phy->set_mac_address(buf);
79+
return NSAPI_ERROR_OK;
80+
}
81+
82+
return NSAPI_ERROR_UNSUPPORTED;
83+
}
84+
6485
nsapi_error_t Nanostack::Interface::get_netmask(SocketAddress *address)
6586
{
6687
return NSAPI_ERROR_UNSUPPORTED;
@@ -237,6 +258,11 @@ const char *InterfaceNanostack::get_mac_address()
237258
return NULL;
238259
}
239260

261+
nsapi_error_t InterfaceNanostack::set_mac_address(uint8_t *mac_addr, size_t addr_len)
262+
{
263+
return _interface->set_mac_address(mac_addr, addr_len);
264+
}
265+
240266
nsapi_connection_status_t InterfaceNanostack::get_connection_status() const
241267
{
242268
if (_interface) {

features/netsocket/NetworkInterface.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ const char *NetworkInterface::get_mac_address()
3232
return 0;
3333
}
3434

35+
nsapi_error_t NetworkInterface::set_mac_address(uint8_t *mac_addr, size_t addr_len)
36+
{
37+
return NSAPI_ERROR_UNSUPPORTED;
38+
}
39+
3540
nsapi_error_t NetworkInterface::get_ip_address(SocketAddress *)
3641
{
3742
return NSAPI_ERROR_UNSUPPORTED;

features/netsocket/NetworkInterface.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ class NetworkInterface: public DNS {
100100
*/
101101
virtual const char *get_mac_address();
102102

103+
/** Set the MAC address to the interface.
104+
*
105+
* Provided MAC address is set the the network interface. Address must be
106+
* set before using the interface connect() method.
107+
*
108+
* @param mac_addr Buffer containing the MAC address
109+
* @param addr_len Length of provided buffer in bytes
110+
* @retval NSAPI_ERROR_OK on success
111+
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
112+
* @retval NSAPI_ERROR_PARAMETER if address is not valid
113+
* @retval NSAPI_ERROR_BUSY if address can't be set.
114+
*/
115+
virtual nsapi_error_t set_mac_address(uint8_t *mac_addr, size_t addr_len);
116+
103117
/** Get the local IP address
104118
*
105119
* @param address SocketAddress representation of the local IP address

0 commit comments

Comments
 (0)