kopia lustrzana https://github.com/espressif/esp-idf
system: Simplify esp_derive_local_mac() function
Previously this function included a loop but would never run more than one iteration of it.pull/6974/head
rodzic
f91d6c7a69
commit
c8cb803378
|
@ -116,11 +116,12 @@ esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type);
|
|||
/**
|
||||
* @brief Derive local MAC address from universal MAC address.
|
||||
*
|
||||
* This function derives a local MAC address from an universal MAC address.
|
||||
* A `definition of local vs universal MAC address can be found on Wikipedia
|
||||
* <https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local>`.
|
||||
* In ESP32, universal MAC address is generated from base MAC address in EFUSE or other external storage.
|
||||
* Local MAC address is derived from the universal MAC address.
|
||||
* This function copies a universal MAC address and then sets the U/L bit
|
||||
* (bit 0x2) in the first octet, creating a locally administered MAC address.
|
||||
*
|
||||
* It's not recommended that the universal MAC address argument is already a
|
||||
* locally administered MAC address, but in this case the first octet is XORed
|
||||
* with 0x4 in order to create a different locally administered MAC address.
|
||||
*
|
||||
* @param local_mac Derived local MAC address, length: 6 bytes.
|
||||
* @param universal_mac Source universal MAC address, length: 6 bytes.
|
||||
|
|
|
@ -117,21 +117,20 @@ esp_err_t esp_efuse_mac_get_default(uint8_t* mac)
|
|||
|
||||
esp_err_t esp_derive_local_mac(uint8_t* local_mac, const uint8_t* universal_mac)
|
||||
{
|
||||
uint8_t idx;
|
||||
|
||||
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);
|
||||
for (idx = 0; idx < 64; idx++) {
|
||||
local_mac[0] = universal_mac[0] | 0x02;
|
||||
local_mac[0] ^= idx << 2;
|
||||
|
||||
if (memcmp(local_mac, universal_mac, 6)) {
|
||||
break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
|
|
Ładowanie…
Reference in New Issue