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 f11733a
Show file tree
Hide file tree
Showing 3 changed files with 39 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
4 changes: 4 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,10 @@ 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_***
* - PRNG
- Used to generate random numbers for the nRF70 Series random MAC address generation.
- 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 f11733a

Please sign in to comment.