diff options
author | xengineering <me@xengineering.eu> | 2025-03-21 22:22:51 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2025-03-21 22:22:51 +0100 |
commit | 07d694f3d21ffc1b69638b8732ffd630affc9768 (patch) | |
tree | e841b07e78b694fb2d1c2f34c75aff77c70bd272 | |
parent | b309e027582b656b8a6094e268b495173f30bde7 (diff) | |
download | iot-contact-07d694f3d21ffc1b69638b8732ffd630affc9768.tar iot-contact-07d694f3d21ffc1b69638b8732ffd630affc9768.tar.zst iot-contact-07d694f3d21ffc1b69638b8732ffd630affc9768.zip |
fw: mac: Implement MAC address setting
The used MAC address is from an example range. Later it can easily be
combined with reading from a MAC-providing EEPROM chip to using a unique
hardware MAC on the device.
-rw-r--r-- | fw/CMakeLists.txt | 1 | ||||
-rw-r--r-- | fw/prj.conf | 1 | ||||
-rw-r--r-- | fw/src/mac.c | 56 |
3 files changed, 58 insertions, 0 deletions
diff --git a/fw/CMakeLists.txt b/fw/CMakeLists.txt index 522a4dd..32a4571 100644 --- a/fw/CMakeLists.txt +++ b/fw/CMakeLists.txt @@ -21,4 +21,5 @@ target_sources(app PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/main.c" "${CMAKE_CURRENT_SOURCE_DIR}/src/syslog.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/mac.c" ) diff --git a/fw/prj.conf b/fw/prj.conf index b074671..405e696 100644 --- a/fw/prj.conf +++ b/fw/prj.conf @@ -9,6 +9,7 @@ CONFIG_NETWORKING=y CONFIG_NET_SHELL=y CONFIG_NET_SOCKETS=y CONFIG_NET_CONNECTION_MANAGER=y +CONFIG_NET_L2_ETHERNET_MGMT=y CONFIG_LOG=y CONFIG_LOG_BACKEND_NET=y diff --git a/fw/src/mac.c b/fw/src/mac.c new file mode 100644 index 0000000..e34fa0d --- /dev/null +++ b/fw/src/mac.c @@ -0,0 +1,56 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at https://mozilla.org/MPL/2.0/. + */ + + +#include <stdint.h> +#include <string.h> + +#include <zephyr/init.h> +#include <zephyr/logging/log.h> +#include <zephyr/net/net_if.h> +#include <zephyr/net/net_linkaddr.h> +#include <zephyr/net/net_mgmt.h> +#include <zephyr/net/ethernet.h> +#include <zephyr/net/ethernet_mgmt.h> +#include <zephyr/sys/util.h> + + +LOG_MODULE_REGISTER(mac); + + +/* will be read from an EEPROM chip in the future */ +static const uint8_t mac_address[NET_ETH_ADDR_LEN] = {0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}; + +int init_mac_address(void) +{ + LOG_DBG("Setting custom MAC address"); + + struct net_if *interface = net_if_get_default(); + + int ret = net_if_down(interface); + if (ret < 0) { + LOG_ERR("Failed to set interface down to set MAC address (%d)", ret); + return ret; + } + + struct ethernet_req_params params = {0}; + memcpy(params.mac_address.addr, mac_address, 6); + ret = net_mgmt(NET_REQUEST_ETHERNET_SET_MAC_ADDRESS, interface, ¶ms, sizeof(params)); + if (ret < 0) { + LOG_ERR("Failed to set MAC address (%d)", ret); + return ret; + } + + ret = net_if_up(interface); + if (ret < 0) { + LOG_ERR("Failed to set interface up after setting MAC address (%d)", ret); + return ret; + } + + LOG_INF("Successfully set MAC address"); + return 0; +} +SYS_INIT(init_mac_address, APPLICATION, 0); |