Skip to content

Commit

Permalink
Add support for random MAC address
Browse files Browse the repository at this point in the history
Add a build time option to generate random MAC address, this helps in
test deployments.

Used Zephyr API in the BM code to simplify things, as srand() was giving
same MAC address (seed problems).

Signed-off-by: Chaitanya Tata <[email protected]>
  • Loading branch information
krish2718 committed Dec 5, 2024
1 parent c13291a commit 6f39b56
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
9 changes: 8 additions & 1 deletion nrf70_bm_lib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions nrf70_bm_lib/docs/source/nrf70_bm_lib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
#################
Expand Down
5 changes: 5 additions & 0 deletions nrf70_bm_lib/docs/source/nrf70_bm_porting_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 ::
Expand Down
27 changes: 27 additions & 0 deletions nrf70_bm_lib/source/nrf70_bm_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
/** @file
* @brief nRF70 Bare Metal initialization.
*/
#ifdef CONFIG_NRF70_RANDOM_MAC_ADDRESS
#include <zephyr/random/random.h>
#endif /* CONFIG_NRF70_RANDOM_MAC_ADDRESS */

#include "nrf70_bm_core.h"
#include "nrf70_bm_lib.h"

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6f39b56

Please sign in to comment.