2021-08-05 15:35:07 +00:00
|
|
|
/*
|
2022-01-12 06:53:47 +00:00
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
2021-08-05 15:35:07 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-10-30 03:19:22 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include "sdkconfig.h"
|
2020-07-13 13:57:24 +00:00
|
|
|
#include "esp_rom_efuse.h"
|
2022-01-12 06:53:47 +00:00
|
|
|
#include "esp_mac.h"
|
2019-10-30 03:19:22 +00:00
|
|
|
#include "esp_efuse.h"
|
|
|
|
#include "esp_efuse_table.h"
|
|
|
|
|
|
|
|
/* esp_system.h APIs relating to MAC addresses */
|
|
|
|
|
2022-11-10 14:21:06 +00:00
|
|
|
#if CONFIG_ESP_MAC_UNIVERSAL_MAC_ADDRESSES_FOUR
|
2021-03-12 06:05:17 +00:00
|
|
|
#define MAC_ADDR_UNIVERSE_BT_OFFSET 2
|
|
|
|
#else
|
|
|
|
#define MAC_ADDR_UNIVERSE_BT_OFFSET 1
|
|
|
|
#endif
|
|
|
|
|
2023-05-30 14:31:22 +00:00
|
|
|
#if CONFIG_SOC_IEEE802154_SUPPORTED
|
2021-07-14 10:00:51 +00:00
|
|
|
#define ESP_MAC_ADDRESS_LEN 8
|
|
|
|
#else
|
|
|
|
#define ESP_MAC_ADDRESS_LEN 6
|
|
|
|
#endif
|
2022-11-10 14:21:06 +00:00
|
|
|
|
2021-07-14 10:00:51 +00:00
|
|
|
static const char *TAG = "system_api";
|
2019-10-30 03:19:22 +00:00
|
|
|
|
2022-11-10 14:21:06 +00:00
|
|
|
typedef enum {
|
|
|
|
STATE_INIT = 0,
|
|
|
|
STATE_SET = (1 << 0),
|
|
|
|
} state_t;
|
2019-10-30 03:19:22 +00:00
|
|
|
|
2022-11-10 14:21:06 +00:00
|
|
|
typedef struct {
|
|
|
|
esp_mac_type_t type: 4;
|
|
|
|
state_t state: 4;
|
|
|
|
uint8_t len;
|
|
|
|
uint8_t mac[ESP_MAC_ADDRESS_LEN];
|
|
|
|
} mac_t;
|
|
|
|
|
|
|
|
static mac_t s_mac_table[] = {
|
2023-05-30 14:31:22 +00:00
|
|
|
#if CONFIG_SOC_WIFI_SUPPORTED
|
|
|
|
{ESP_MAC_WIFI_STA, STATE_INIT, 6, {0}},
|
|
|
|
{ESP_MAC_WIFI_SOFTAP, STATE_INIT, 6, {0}},
|
2022-11-10 14:21:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
|
2023-05-30 14:31:22 +00:00
|
|
|
{ESP_MAC_BT, STATE_INIT, 6, {0}},
|
2022-11-10 14:21:06 +00:00
|
|
|
#endif
|
|
|
|
|
2023-05-30 14:31:22 +00:00
|
|
|
{ESP_MAC_ETH, STATE_INIT, 6, {0}},
|
2022-11-10 14:21:06 +00:00
|
|
|
|
2023-05-30 14:31:22 +00:00
|
|
|
#ifdef CONFIG_SOC_IEEE802154_SUPPORTED
|
|
|
|
{ESP_MAC_IEEE802154, STATE_INIT, ESP_MAC_ADDRESS_LEN, {0}},
|
|
|
|
{ESP_MAC_EFUSE_EXT, STATE_INIT, 2, {0}},
|
2022-11-10 14:21:06 +00:00
|
|
|
#endif
|
|
|
|
|
2023-05-30 14:31:22 +00:00
|
|
|
{ESP_MAC_BASE, STATE_INIT, 6, {0}},
|
|
|
|
{ESP_MAC_EFUSE_FACTORY, STATE_INIT, 6, {0}},
|
|
|
|
{ESP_MAC_EFUSE_CUSTOM, STATE_INIT, 6, {0}},
|
2022-11-10 14:21:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define ITEMS_IN_MAC_TABLE (sizeof(s_mac_table) / sizeof(mac_t))
|
|
|
|
|
|
|
|
static esp_err_t generate_mac(uint8_t *mac, uint8_t *base_mac_addr, esp_mac_type_t type);
|
2023-05-30 14:31:22 +00:00
|
|
|
static esp_err_t get_efuse_mac_get_default(uint8_t *mac);
|
|
|
|
static esp_err_t get_efuse_mac_custom(uint8_t *mac);
|
|
|
|
#if CONFIG_SOC_IEEE802154_SUPPORTED
|
|
|
|
static esp_err_t get_efuse_mac_ext(uint8_t *mac);
|
|
|
|
#endif
|
2022-11-10 14:21:06 +00:00
|
|
|
|
|
|
|
static int get_idx(esp_mac_type_t type)
|
2019-10-30 03:19:22 +00:00
|
|
|
{
|
2022-11-10 14:21:06 +00:00
|
|
|
for (int idx = 0; idx < ITEMS_IN_MAC_TABLE; idx++) {
|
|
|
|
if (s_mac_table[idx].type == type) {
|
|
|
|
return idx;
|
|
|
|
}
|
2019-10-30 03:19:22 +00:00
|
|
|
}
|
2023-05-30 14:31:22 +00:00
|
|
|
ESP_LOGE(TAG, "%d mac type is incorrect (not found)", type);
|
2022-11-10 14:21:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-10-30 03:19:22 +00:00
|
|
|
|
2022-11-18 08:16:55 +00:00
|
|
|
static esp_err_t get_mac_addr_from_mac_table(uint8_t *mac, int idx, bool silent)
|
2022-11-10 14:21:06 +00:00
|
|
|
{
|
2022-11-18 08:16:55 +00:00
|
|
|
if (idx == -1) {
|
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
}
|
2022-11-10 14:21:06 +00:00
|
|
|
if (!(s_mac_table[idx].state & STATE_SET)) {
|
|
|
|
esp_mac_type_t type = s_mac_table[idx].type;
|
2023-05-30 14:31:22 +00:00
|
|
|
if (ESP_MAC_BASE <= type && type <= ESP_MAC_EFUSE_EXT) {
|
2022-11-10 14:21:06 +00:00
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
if (type == ESP_MAC_BASE || type == ESP_MAC_EFUSE_FACTORY) {
|
2023-05-30 14:31:22 +00:00
|
|
|
err = get_efuse_mac_get_default(s_mac_table[idx].mac);
|
2022-11-10 14:21:06 +00:00
|
|
|
} else if (type == ESP_MAC_EFUSE_CUSTOM) {
|
2023-05-30 14:31:22 +00:00
|
|
|
err = get_efuse_mac_custom(s_mac_table[idx].mac);
|
|
|
|
}
|
|
|
|
#if CONFIG_SOC_IEEE802154_SUPPORTED
|
|
|
|
else if (type == ESP_MAC_EFUSE_EXT) {
|
|
|
|
err = get_efuse_mac_ext(s_mac_table[idx].mac);
|
2022-11-10 14:21:06 +00:00
|
|
|
}
|
2023-05-30 14:31:22 +00:00
|
|
|
#endif
|
2022-11-10 14:21:06 +00:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
s_mac_table[idx].state = STATE_SET;
|
|
|
|
} else {
|
|
|
|
if (!silent) {
|
|
|
|
ESP_LOGE(TAG, "MAC address (type %d) is not set in mac table", type);
|
|
|
|
}
|
|
|
|
return ESP_ERR_INVALID_MAC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(mac, s_mac_table[idx].mac, s_mac_table[idx].len);
|
2019-10-30 03:19:22 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2022-11-10 14:21:06 +00:00
|
|
|
size_t esp_mac_addr_len_get(esp_mac_type_t type)
|
|
|
|
{
|
|
|
|
for (int idx = 0; idx < ITEMS_IN_MAC_TABLE; idx++) {
|
|
|
|
if (s_mac_table[idx].type == type) {
|
|
|
|
return s_mac_table[idx].len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_iface_mac_addr_set(const uint8_t *mac, esp_mac_type_t type)
|
2019-10-30 03:19:22 +00:00
|
|
|
{
|
2021-07-19 10:16:42 +00:00
|
|
|
if (mac == NULL) {
|
2022-11-10 14:21:06 +00:00
|
|
|
ESP_LOGE(TAG, "mac address param is NULL");
|
2021-07-19 10:16:42 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2022-11-10 14:21:06 +00:00
|
|
|
int idx = get_idx(type);
|
|
|
|
if (idx == -1) {
|
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == ESP_MAC_EFUSE_FACTORY || type == ESP_MAC_EFUSE_CUSTOM) {
|
|
|
|
ESP_LOGE(TAG, "EFUSE MAC can not be set using this API");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
2019-10-30 03:19:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 14:21:06 +00:00
|
|
|
if (type == ESP_MAC_BASE) {
|
|
|
|
if (mac[0] & 0x01) {
|
|
|
|
ESP_LOGE(TAG, "Base MAC must be a unicast MAC");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 03:19:22 +00:00
|
|
|
|
2022-11-10 14:21:06 +00:00
|
|
|
memcpy(s_mac_table[idx].mac, mac, s_mac_table[idx].len);
|
|
|
|
s_mac_table[idx].state = STATE_SET;
|
2019-10-30 03:19:22 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2022-11-10 14:21:06 +00:00
|
|
|
esp_err_t esp_base_mac_addr_set(const uint8_t *mac)
|
|
|
|
{
|
|
|
|
return esp_iface_mac_addr_set(mac, ESP_MAC_BASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_base_mac_addr_get(uint8_t *mac)
|
|
|
|
{
|
|
|
|
return esp_read_mac(mac, ESP_MAC_BASE);
|
|
|
|
}
|
|
|
|
|
2023-05-30 14:31:22 +00:00
|
|
|
#if CONFIG_SOC_IEEE802154_SUPPORTED
|
|
|
|
static esp_err_t get_efuse_mac_ext(uint8_t *mac)
|
|
|
|
{
|
|
|
|
// ff:fe
|
|
|
|
esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_EXT, mac, 16);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Reading MAC_EXT failed, error=%d", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t insert_mac_ext_into_mac(uint8_t *mac)
|
|
|
|
{
|
|
|
|
uint8_t mac_tmp[3];
|
|
|
|
memcpy(mac_tmp, &mac[3], 3);
|
|
|
|
esp_err_t err = get_efuse_mac_ext(&mac[3]);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
memcpy(&mac[5], mac_tmp, 3);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-10-30 03:19:22 +00:00
|
|
|
esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
|
2023-05-30 14:31:22 +00:00
|
|
|
{
|
|
|
|
esp_err_t err = get_efuse_mac_custom(mac);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#if CONFIG_SOC_IEEE802154_SUPPORTED
|
|
|
|
return insert_mac_ext_into_mac(mac);
|
|
|
|
#else
|
|
|
|
return ESP_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t get_efuse_mac_custom(uint8_t *mac)
|
2019-10-30 03:19:22 +00:00
|
|
|
{
|
2020-07-29 14:03:46 +00:00
|
|
|
#if !CONFIG_IDF_TARGET_ESP32
|
2021-07-19 10:16:42 +00:00
|
|
|
size_t size_bits = esp_efuse_get_field_size(ESP_EFUSE_USER_DATA_MAC_CUSTOM);
|
|
|
|
assert((size_bits % 8) == 0);
|
|
|
|
esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA_MAC_CUSTOM, mac, size_bits);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
size_t size = size_bits / 8;
|
|
|
|
if (mac[0] == 0 && memcmp(mac, &mac[1], size - 1) == 0) {
|
|
|
|
ESP_LOGE(TAG, "eFuse MAC_CUSTOM is empty");
|
|
|
|
return ESP_ERR_INVALID_MAC;
|
|
|
|
}
|
2019-10-30 03:19:22 +00:00
|
|
|
#else
|
|
|
|
uint8_t version;
|
|
|
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_VER, &version, 8);
|
|
|
|
if (version != 1) {
|
2022-02-18 10:40:34 +00:00
|
|
|
// version 0 means has not been setup
|
|
|
|
if (version == 0) {
|
|
|
|
ESP_LOGD(TAG, "No base MAC address in eFuse (version=0)");
|
|
|
|
} else if (version != 1) {
|
|
|
|
ESP_LOGE(TAG, "Base MAC address version error, version = %d", version);
|
|
|
|
}
|
2019-10-30 03:19:22 +00:00
|
|
|
return ESP_ERR_INVALID_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t efuse_crc;
|
|
|
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
|
|
|
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_CRC, &efuse_crc, 8);
|
2020-07-13 13:57:24 +00:00
|
|
|
uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
|
2019-10-30 03:19:22 +00:00
|
|
|
|
|
|
|
if (efuse_crc != calc_crc) {
|
|
|
|
ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
|
2022-12-23 13:31:39 +00:00
|
|
|
#ifdef CONFIG_ESP_MAC_IGNORE_MAC_CRC_ERROR
|
|
|
|
ESP_LOGW(TAG, "Ignore MAC CRC error");
|
|
|
|
#else
|
2019-10-30 03:19:22 +00:00
|
|
|
return ESP_ERR_INVALID_CRC;
|
2022-12-23 13:31:39 +00:00
|
|
|
#endif
|
2019-10-30 03:19:22 +00:00
|
|
|
}
|
2020-07-29 14:03:46 +00:00
|
|
|
#endif
|
2023-05-30 14:31:22 +00:00
|
|
|
return ESP_OK;
|
2019-10-30 03:19:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 10:00:51 +00:00
|
|
|
esp_err_t esp_efuse_mac_get_default(uint8_t *mac)
|
2019-10-30 03:19:22 +00:00
|
|
|
{
|
2023-05-30 14:31:22 +00:00
|
|
|
esp_err_t err = get_efuse_mac_get_default(mac);
|
2021-07-19 10:16:42 +00:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
2021-07-14 10:00:51 +00:00
|
|
|
}
|
2023-05-30 14:31:22 +00:00
|
|
|
#if CONFIG_SOC_IEEE802154_SUPPORTED
|
|
|
|
return insert_mac_ext_into_mac(mac);
|
|
|
|
#else
|
|
|
|
return ESP_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t get_efuse_mac_get_default(uint8_t *mac)
|
|
|
|
{
|
|
|
|
size_t size_bits = esp_efuse_get_field_size(ESP_EFUSE_MAC_FACTORY);
|
|
|
|
assert((size_bits % 8) == 0);
|
|
|
|
esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, size_bits);
|
2019-10-30 03:19:22 +00:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_IDF_TARGET_ESP32
|
2020-07-29 14:03:46 +00:00
|
|
|
// Only ESP32 has MAC CRC in efuse
|
2019-10-30 03:19:22 +00:00
|
|
|
uint8_t efuse_crc;
|
|
|
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY_CRC, &efuse_crc, 8);
|
2020-07-13 13:57:24 +00:00
|
|
|
uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
|
2019-10-30 03:19:22 +00:00
|
|
|
|
|
|
|
if (efuse_crc != calc_crc) {
|
2021-07-14 10:00:51 +00:00
|
|
|
// Small range of MAC addresses are accepted even if CRC is invalid.
|
|
|
|
// These addresses are reserved for Espressif internal use.
|
2019-10-30 03:19:22 +00:00
|
|
|
uint32_t mac_high = ((uint32_t)mac[0] << 8) | mac[1];
|
2020-10-20 14:12:28 +00:00
|
|
|
uint32_t mac_low = ((uint32_t)mac[2] << 24) | ((uint32_t)mac[3] << 16) | ((uint32_t)mac[4] << 8) | mac[5];
|
|
|
|
if (((mac_high & 0xFFFF) == 0x18fe) && (mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
|
|
|
|
return ESP_OK;
|
2019-10-30 03:19:22 +00:00
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "Base MAC address from BLK0 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
|
2022-12-23 13:31:39 +00:00
|
|
|
#ifdef CONFIG_ESP_MAC_IGNORE_MAC_CRC_ERROR
|
|
|
|
ESP_LOGW(TAG, "Ignore MAC CRC error");
|
|
|
|
#else
|
|
|
|
return ESP_ERR_INVALID_CRC;
|
|
|
|
#endif
|
2019-10-30 03:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // CONFIG_IDF_TARGET_ESP32
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:00:51 +00:00
|
|
|
esp_err_t esp_derive_local_mac(uint8_t *local_mac, const uint8_t *universal_mac)
|
2019-10-30 03:19:22 +00:00
|
|
|
{
|
|
|
|
if (local_mac == NULL || universal_mac == NULL) {
|
|
|
|
ESP_LOGE(TAG, "mac address param is NULL");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(local_mac, universal_mac, 6);
|
|
|
|
|
2021-04-22 02:09:54 +00:00
|
|
|
const unsigned UL_BIT = 0x2;
|
|
|
|
local_mac[0] |= UL_BIT;
|
|
|
|
|
|
|
|
if (local_mac[0] == universal_mac[0]) {
|
|
|
|
// universal_mac was already local, so flip this bit instead
|
|
|
|
// (this is kept to be compatible with the previous behaviour of this function)
|
|
|
|
local_mac[0] ^= 0x4;
|
2019-10-30 03:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:00:51 +00:00
|
|
|
esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type)
|
2019-10-30 03:19:22 +00:00
|
|
|
{
|
|
|
|
if (mac == NULL) {
|
|
|
|
ESP_LOGE(TAG, "mac address param is NULL");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2022-11-10 14:21:06 +00:00
|
|
|
int idx = get_idx(type);
|
|
|
|
if (idx == -1) {
|
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_mac_addr_from_mac_table(mac, idx, true) == ESP_OK) {
|
|
|
|
return ESP_OK;
|
2019-10-30 03:19:22 +00:00
|
|
|
}
|
2022-11-10 14:21:06 +00:00
|
|
|
// A MAC with a specific type has not yet been set (or generated)
|
2019-10-30 03:19:22 +00:00
|
|
|
|
2022-11-10 14:21:06 +00:00
|
|
|
// then go ahead and generate it based on the base mac
|
|
|
|
uint8_t base_mac_addr[ESP_MAC_ADDRESS_LEN];
|
|
|
|
esp_err_t err = get_mac_addr_from_mac_table(base_mac_addr, get_idx(ESP_MAC_BASE), false);
|
|
|
|
if (err) {
|
|
|
|
ESP_LOGE(TAG, "Error reading BASE MAC address");
|
|
|
|
return ESP_FAIL;
|
2019-10-30 03:19:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 14:21:06 +00:00
|
|
|
err = generate_mac(mac, base_mac_addr, type);
|
|
|
|
if (err) {
|
|
|
|
ESP_LOGE(TAG, "MAC address generation error");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
// MAC was generated. We write it into the s_mac_table
|
|
|
|
s_mac_table[idx].state = STATE_SET;
|
|
|
|
memcpy(s_mac_table[idx].mac, mac, s_mac_table[idx].len);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t generate_mac(uint8_t *mac, uint8_t *base_mac_addr, esp_mac_type_t type)
|
|
|
|
{
|
2019-10-30 03:19:22 +00:00
|
|
|
switch (type) {
|
|
|
|
case ESP_MAC_WIFI_STA:
|
2022-11-10 14:21:06 +00:00
|
|
|
memcpy(mac, base_mac_addr, 6);
|
2019-10-30 03:19:22 +00:00
|
|
|
break;
|
|
|
|
case ESP_MAC_WIFI_SOFTAP:
|
2020-01-10 09:46:46 +00:00
|
|
|
#if CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
|
2022-11-10 14:21:06 +00:00
|
|
|
memcpy(mac, base_mac_addr, 6);
|
2021-07-14 10:00:51 +00:00
|
|
|
// as a result of some esp32s2 chips burned with one MAC address by mistake,
|
|
|
|
// there are some MAC address are reserved for this bug fix.
|
|
|
|
// related mistake MAC address is 0x7cdfa1003000~0x7cdfa1005fff,
|
|
|
|
// reserved MAC address is 0x7cdfa1020000~0x7cdfa1022fff (MAC address + 0x1d000).
|
2020-03-04 03:15:52 +00:00
|
|
|
#ifdef CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
uint8_t mac_begin[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x30, 0x00 };
|
|
|
|
uint8_t mac_end[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x5f, 0xff };
|
2021-07-14 10:00:51 +00:00
|
|
|
if (memcmp(mac, mac_begin, 6) >= 0 && memcmp(mac_end, mac, 6) >= 0 ) {
|
2020-11-10 07:40:01 +00:00
|
|
|
mac[3] += 0x02; // contain carry bit
|
2020-03-04 03:15:52 +00:00
|
|
|
mac[4] += 0xd0;
|
2020-03-17 13:31:32 +00:00
|
|
|
} else {
|
|
|
|
mac[5] += 1;
|
2020-03-04 03:15:52 +00:00
|
|
|
}
|
|
|
|
#else
|
2020-01-10 09:46:46 +00:00
|
|
|
mac[5] += 1;
|
2020-03-04 03:15:52 +00:00
|
|
|
#endif // IDF_TARGET_ESP32S2
|
2020-01-10 09:46:46 +00:00
|
|
|
#else
|
2022-11-10 14:21:06 +00:00
|
|
|
esp_derive_local_mac(mac, base_mac_addr);
|
2021-04-22 02:11:09 +00:00
|
|
|
#endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
|
2019-10-30 03:19:22 +00:00
|
|
|
break;
|
|
|
|
case ESP_MAC_BT:
|
2020-01-10 09:46:46 +00:00
|
|
|
#if CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
|
2022-11-10 14:21:06 +00:00
|
|
|
memcpy(mac, base_mac_addr, 6);
|
2023-03-01 03:25:38 +00:00
|
|
|
#if SOC_WIFI_SUPPORTED
|
|
|
|
// If the chips do not have wifi module, the mac address do not need to add the BT offset
|
2021-03-12 06:05:17 +00:00
|
|
|
mac[5] += MAC_ADDR_UNIVERSE_BT_OFFSET;
|
2023-03-01 03:25:38 +00:00
|
|
|
#endif //SOC_WIFI_SUPPORTED
|
2021-04-22 02:11:09 +00:00
|
|
|
#else
|
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
#endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
|
2019-10-30 03:19:22 +00:00
|
|
|
break;
|
|
|
|
case ESP_MAC_ETH:
|
2020-01-10 09:46:46 +00:00
|
|
|
#if CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
|
2022-11-10 14:21:06 +00:00
|
|
|
memcpy(mac, base_mac_addr, 6);
|
2020-01-10 09:46:46 +00:00
|
|
|
mac[5] += 3;
|
|
|
|
#else
|
2022-11-10 14:21:06 +00:00
|
|
|
base_mac_addr[5] += 1;
|
|
|
|
esp_derive_local_mac(mac, base_mac_addr);
|
2021-04-22 02:11:09 +00:00
|
|
|
#endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
|
2019-10-30 03:19:22 +00:00
|
|
|
break;
|
2023-05-30 14:31:22 +00:00
|
|
|
#if CONFIG_SOC_IEEE802154_SUPPORTED
|
2021-07-14 10:00:51 +00:00
|
|
|
case ESP_MAC_IEEE802154:
|
2023-05-30 14:31:22 +00:00
|
|
|
// 60:55:f9
|
|
|
|
memcpy(mac, base_mac_addr, 3);
|
|
|
|
// 60:55:f9 + ff:fe
|
|
|
|
esp_read_mac(&mac[3], ESP_MAC_EFUSE_EXT);
|
|
|
|
// 60:55:f9:ff:fe + f7:2c:a2
|
|
|
|
memcpy(&mac[5], &base_mac_addr[3], 3);
|
|
|
|
// 60:55:f9:ff:fe:f7:2c:a2
|
2021-07-14 10:00:51 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2019-10-30 03:19:22 +00:00
|
|
|
default:
|
2020-01-10 09:46:46 +00:00
|
|
|
ESP_LOGE(TAG, "unsupported mac type");
|
2022-11-10 14:21:06 +00:00
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
2019-10-30 03:19:22 +00:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|