Method for adding network interface MAC address

Add method set_mac_address to set network interface MAC address.
pull/13902/head
Arto Kinnunen 2020-11-11 12:47:46 +02:00
parent a0a4707151
commit 26b322e3de
4 changed files with 49 additions and 0 deletions

View File

@ -29,6 +29,7 @@ public:
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual char *get_ip_address(char *buf, nsapi_size_t buflen);
virtual char *get_mac_address(char *buf, nsapi_size_t buflen);
virtual nsapi_error_t set_mac_address(uint8_t *buf, nsapi_size_t buflen);
virtual nsapi_error_t get_netmask(SocketAddress *address);
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual char *get_netmask(char *buf, nsapi_size_t buflen);
@ -118,6 +119,9 @@ public:
*/
virtual const char *get_mac_address();
/** @copydoc NetworkInterface::set_mac_address */
virtual nsapi_error_t set_mac_address(uint8_t *mac_addr, size_t addr_len);
/** Register callback for status reporting
*
* The specified status callback function will be called on status changes

View File

@ -61,6 +61,27 @@ char *Nanostack::Interface::get_mac_address(char *buf, nsapi_size_t buflen)
}
}
nsapi_error_t Nanostack::Interface::set_mac_address(uint8_t *buf, nsapi_size_t buflen)
{
if (buflen != 8) {
/* Provided MAC is too short */
return NSAPI_ERROR_PARAMETER;
}
if (_device_id >= 0) {
/* device is already registered, can't set MAC address anymore */
return NSAPI_ERROR_BUSY;
}
NanostackMACPhy *phy = interface_phy.nanostack_mac_phy();
if (phy) {
phy->set_mac_address(buf);
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_UNSUPPORTED;
}
nsapi_error_t Nanostack::Interface::get_netmask(SocketAddress *address)
{
return NSAPI_ERROR_UNSUPPORTED;
@ -237,6 +258,11 @@ const char *InterfaceNanostack::get_mac_address()
return NULL;
}
nsapi_error_t InterfaceNanostack::set_mac_address(uint8_t *mac_addr, size_t addr_len)
{
return _interface->set_mac_address(mac_addr, addr_len);
}
nsapi_connection_status_t InterfaceNanostack::get_connection_status() const
{
if (_interface) {

View File

@ -32,6 +32,11 @@ const char *NetworkInterface::get_mac_address()
return 0;
}
nsapi_error_t NetworkInterface::set_mac_address(uint8_t *mac_addr, size_t addr_len)
{
return NSAPI_ERROR_UNSUPPORTED;
}
nsapi_error_t NetworkInterface::get_ip_address(SocketAddress *)
{
return NSAPI_ERROR_UNSUPPORTED;

View File

@ -100,6 +100,20 @@ public:
*/
virtual const char *get_mac_address();
/** Set the MAC address to the interface.
*
* Provided MAC address is set the the network interface. Address must be
* set before using the interface connect() method.
*
* @param mac_addr Buffer containing the MAC address
* @param addr_len Length of provided buffer in bytes
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if address is not valid
* @retval NSAPI_ERROR_BUSY if address can't be set.
*/
virtual nsapi_error_t set_mac_address(uint8_t *mac_addr, size_t addr_len);
/** Get the local IP address
*
* @param address SocketAddress representation of the local IP address