From 26b322e3de620dca94faa7863d736295a619af61 Mon Sep 17 00:00:00 2001 From: Arto Kinnunen Date: Wed, 11 Nov 2020 12:47:46 +0200 Subject: [PATCH] Method for adding network interface MAC address Add method set_mac_address to set network interface MAC address. --- .../mbed-mesh-api/MeshInterfaceNanostack.h | 4 +++ .../source/MeshInterfaceNanostack.cpp | 26 +++++++++++++++++++ features/netsocket/NetworkInterface.cpp | 5 ++++ features/netsocket/NetworkInterface.h | 14 ++++++++++ 4 files changed, 49 insertions(+) diff --git a/features/nanostack/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h b/features/nanostack/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h index 51d4ca1431..bc08374a9b 100644 --- a/features/nanostack/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h +++ b/features/nanostack/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h @@ -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 diff --git a/features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp b/features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp index b33b83f84e..cbc7638b89 100644 --- a/features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp +++ b/features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp @@ -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) { diff --git a/features/netsocket/NetworkInterface.cpp b/features/netsocket/NetworkInterface.cpp index 37b49747ab..1e2d93fe26 100644 --- a/features/netsocket/NetworkInterface.cpp +++ b/features/netsocket/NetworkInterface.cpp @@ -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; diff --git a/features/netsocket/NetworkInterface.h b/features/netsocket/NetworkInterface.h index a03909d795..341274e52a 100644 --- a/features/netsocket/NetworkInterface.h +++ b/features/netsocket/NetworkInterface.h @@ -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