diff --git a/nrf70_bm_lib/Kconfig b/nrf70_bm_lib/Kconfig index ff23c6b..8add2ed 100644 --- a/nrf70_bm_lib/Kconfig +++ b/nrf70_bm_lib/Kconfig @@ -48,8 +48,8 @@ config NRF70_FIXED_MAC_ADDRESS choice prompt "Wi-Fi MAC address type" - default NRF70_OTP_MAC_ADDRESS if NRF70_FIXED_MAC_ADDRESS = "" default NRF70_FIXED_MAC_ADDRESS_ENABLED if NRF70_FIXED_MAC_ADDRESS != "" + default NRF70_OTP_MAC_ADDRESS help Select the type of MAC address to be used by the Wi-Fi driver @@ -62,6 +62,13 @@ config NRF70_FIXED_MAC_ADDRESS_ENABLED bool "Enable fixed MAC address" help Enable fixed MAC address + +config NRF70_RANDOM_MAC_ADDRESS + bool "Random MAC address generation at runtime" + select TEST_RANDOM_GENERATOR + help + This option enables random MAC address generation at runtime. + The random MAC address is generated using the libc srand() function. endchoice config NRF700X_LOG_VERBOSE diff --git a/nrf70_bm_lib/docs/source/nrf70_bm_lib.rst b/nrf70_bm_lib/docs/source/nrf70_bm_lib.rst index da4cff5..416c5fe 100644 --- a/nrf70_bm_lib/docs/source/nrf70_bm_lib.rst +++ b/nrf70_bm_lib/docs/source/nrf70_bm_lib.rst @@ -74,6 +74,20 @@ The key components of the Zephyr shim reference implementation are: * ``platform``: Contains the platform specific files, and has an RPU (Radio Processing Unit) abstraction layer that interacts with the nRF70 Series device, either through QSPI or SPI. The platform also uses Zephyr GPIO APIs to manage GPIO pins of the nRF70 Series. +MAC address configuration +######################### + +The nRF70 Series Wi-Fi driver offers various options for configuring the MAC address used by the Wi-Fi driver. +The MAC address can be configured in the following ways: + +* **Use MAC address from OTP**: The MAC address stored in the OTP memory of the nRF70 Series device is used. + This is the default option, controlled by the Kconfig option ``CONFIG_NRF70_OTP_MAC_ADDRESS``. +* **Enable fixed MAC address**: A fixed MAC address can be set in the Kconfig file. + This option is strictly for testing purposes only, and is controlled by the Kconfig option ``CONFIG_NRF70_FIXED_MAC_ADDRESS``. +* **Use Random MAC address**: A random MAC address is generated by the Wi-Fi driver. + Usefull for testing purposes, this option is controlled by the Kconfig option ``CONFIG_NRF70_RANDOM_MAC_ADDRESS``. + This option uses a Zephyr API in the reference implementation to generate a random MAC address. + When porting to a third-party platform, the random MAC address generation can be implemented using a platform-specific pseudo-random number generator (PRNG). Design essentials ################# diff --git a/nrf70_bm_lib/docs/source/nrf70_bm_porting_guide.rst b/nrf70_bm_lib/docs/source/nrf70_bm_porting_guide.rst index 97654ba..dd5d0ba 100644 --- a/nrf70_bm_lib/docs/source/nrf70_bm_porting_guide.rst +++ b/nrf70_bm_lib/docs/source/nrf70_bm_porting_guide.rst @@ -47,6 +47,11 @@ The reference implementation of the BM Driver for the Zephyr RTOS uses build-tim - Used to manage the timers for the nRF70 Series driver, esp. for low power mode. - timer_*** - k_work_delayable_*** + * - Pseudo-Random Number Generator (PRNG) + - Used to generate random numbers for the nRF70 Series random MAC address generation, + (if random MAC address generation support is enabled). + - NA + - sys_rand8_get() .. note :: diff --git a/nrf70_bm_lib/source/nrf70_bm_core.c b/nrf70_bm_lib/source/nrf70_bm_core.c index e56f12a..fb479be 100644 --- a/nrf70_bm_lib/source/nrf70_bm_core.c +++ b/nrf70_bm_lib/source/nrf70_bm_core.c @@ -7,6 +7,10 @@ /** @file * @brief nRF70 Bare Metal initialization. */ +#ifdef CONFIG_NRF70_RANDOM_MAC_ADDRESS +#include +#endif /* CONFIG_NRF70_RANDOM_MAC_ADDRESS */ + #include "nrf70_bm_core.h" #include "nrf70_bm_lib.h" @@ -451,11 +455,28 @@ int nrf70_fmac_init(void) return -1; } +#ifdef CONFIG_NRF70_RANDOM_MAC_ADDRESS +static void generate_random_mac_address(uint8_t *mac_addr) +{ + // For simplicty use Zephyr API to generate random number + for (int i = 0; i < 6; i++) { + mac_addr[i] = sys_rand8_get(); + } + + // Set the locally administered bit (bit 1 of the first byte) + mac_addr[0] |= 0x02; + + // Clear the multicast bit (bit 0 of the first byte) + mac_addr[0] &= 0xFE; +} +#endif /* CONFIG_WIFI_RANDOM_MAC_ADDRESS */ enum nrf_wifi_status nrf_wifi_get_mac_addr(struct nrf70_wifi_vif_bm *vif) { enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL; +#ifndef CONFIG_NRF70_RANDOM_MAC_ADDRESS void *rpu_ctx = nrf70_bm_priv.rpu_ctx_bm.rpu_ctx; +#endif /* CONFIG_NRF70_RANDOM_MAC_ADDRESS */ struct nrf_wifi_fmac_priv *fmac_priv = nrf70_bm_priv.fmac_priv; unsigned char mac_addr_str[18]; #ifdef CONFIG_NRF70_FIXED_MAC_ADDRESS_ENABLED @@ -474,6 +495,8 @@ enum nrf_wifi_status nrf_wifi_get_mac_addr(struct nrf70_wifi_vif_bm *vif) } memcpy(vif->mac_addr, fixed_mac_addr, NR70_MAC_ADDR_LEN); +#elif CONFIG_NRF70_RANDOM_MAC_ADDRESS + generate_random_mac_address(vif->mac_addr); #elif CONFIG_NRF70_OTP_MAC_ADDRESS status = nrf_wifi_fmac_otp_mac_addr_get(rpu_ctx, vif->vif_idx, @@ -536,6 +559,10 @@ int nrf70_fmac_add_vif_sta(void) goto del_vif; } + NRF70_LOG_INF("MAC address set to %02X:%02X:%02X:%02X:%02X:%02X", + vif->mac_addr[0], vif->mac_addr[1], vif->mac_addr[2], + vif->mac_addr[3], vif->mac_addr[4], vif->mac_addr[5]); + memset(&vif_info, 0, sizeof(vif_info)); vif_info.state = NRF_WIFI_FMAC_IF_OP_STATE_UP; vif_info.if_index = vif->vif_idx;