Skip to content

Commit a607be6

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 f227856 commit a607be6

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Nanostack::Interface : public OnboardNetworkStack::Interface, private mbed
2727
public:
2828
nsapi_error_t get_ip_address(SocketAddress *address) final;
2929
char *get_mac_address(char *buf, nsapi_size_t buflen) final;
30+
nsapi_error_t set_mac_address(uint8_t *buf, nsapi_size_t buflen) final;
3031
nsapi_error_t get_netmask(SocketAddress *address) final;
3132
nsapi_error_t get_gateway(SocketAddress *address) override;
3233
void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) final;
@@ -109,6 +110,9 @@ class InterfaceNanostack : public virtual NetworkInterface {
109110
*/
110111
const char *get_mac_address() override;
111112

113+
/** @copydoc NetworkInterface::set_mac_address */
114+
nsapi_error_t set_mac_address(uint8_t *mac_addr, size_t addr_len) override;
115+
112116
/** Register callback for status reporting
113117
*
114118
* The specified status callback function will be called on status changes

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ char *Nanostack::Interface::get_mac_address(char *buf, nsapi_size_t buflen)
4848
}
4949
}
5050

51+
nsapi_error_t Nanostack::Interface::set_mac_address(uint8_t *buf, nsapi_size_t buflen)
52+
{
53+
if (buflen != 8) {
54+
/* Provided MAC is too short */
55+
return NSAPI_ERROR_PARAMETER;
56+
}
57+
58+
if (_device_id >= 0) {
59+
/* device is already registered, can't set MAC address anymore */
60+
return NSAPI_ERROR_BUSY;
61+
}
62+
63+
NanostackMACPhy *phy = interface_phy.nanostack_mac_phy();
64+
if (phy) {
65+
phy->set_mac_address(buf);
66+
return NSAPI_ERROR_OK;
67+
}
68+
69+
return NSAPI_ERROR_UNSUPPORTED;
70+
}
71+
5172
nsapi_error_t Nanostack::Interface::get_netmask(SocketAddress *address)
5273
{
5374
return NSAPI_ERROR_UNSUPPORTED;
@@ -197,6 +218,11 @@ const char *InterfaceNanostack::get_mac_address()
197218
return NULL;
198219
}
199220

221+
nsapi_error_t InterfaceNanostack::set_mac_address(uint8_t *mac_addr, size_t addr_len)
222+
{
223+
return _interface->set_mac_address(mac_addr, addr_len);
224+
}
225+
200226
nsapi_connection_status_t InterfaceNanostack::get_connection_status() const
201227
{
202228
if (_interface) {

connectivity/netsocket/include/netsocket/NetworkInterface.h

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

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

connectivity/netsocket/source/NetworkInterface.cpp

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

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

0 commit comments

Comments
 (0)