esp_mac.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "esp_err.h"
  8. #include "sdkconfig.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. typedef enum {
  13. ESP_MAC_WIFI_STA,
  14. ESP_MAC_WIFI_SOFTAP,
  15. ESP_MAC_BT,
  16. ESP_MAC_ETH,
  17. ESP_MAC_IEEE802154,
  18. } esp_mac_type_t;
  19. /** @cond */
  20. #define TWO_UNIVERSAL_MAC_ADDR 2
  21. #define FOUR_UNIVERSAL_MAC_ADDR 4
  22. #if CONFIG_IDF_TARGET_ESP32
  23. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES
  24. #elif CONFIG_IDF_TARGET_ESP32S2
  25. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32S2_UNIVERSAL_MAC_ADDRESSES
  26. #elif CONFIG_IDF_TARGET_ESP32S3
  27. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32S3_UNIVERSAL_MAC_ADDRESSES
  28. #elif CONFIG_IDF_TARGET_ESP32C3
  29. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES
  30. #elif CONFIG_IDF_TARGET_ESP32H2
  31. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32H2_UNIVERSAL_MAC_ADDRESSES
  32. #elif CONFIG_IDF_TARGET_ESP8684
  33. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP8684_UNIVERSAL_MAC_ADDRESSES
  34. #endif
  35. /** @endcond */
  36. /**
  37. * @brief Set base MAC address with the MAC address which is stored in BLK3 of EFUSE or
  38. * external storage e.g. flash and EEPROM.
  39. *
  40. * Base MAC address is used to generate the MAC addresses used by network interfaces.
  41. *
  42. * If using a custom base MAC address, call this API before initializing any network interfaces.
  43. * Refer to the ESP-IDF Programming Guide for details about how the Base MAC is used.
  44. *
  45. * @note Base MAC must be a unicast MAC (least significant bit of first byte must be zero).
  46. *
  47. * @note If not using a valid OUI, set the "locally administered" bit
  48. * (bit value 0x02 in the first byte) to avoid collisions.
  49. *
  50. * @param mac base MAC address, length: 6 bytes/8 bytes.
  51. * length: 6 bytes for MAC-48
  52. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  53. *
  54. * @return ESP_OK on success
  55. * ESP_ERR_INVALID_ARG If mac is NULL or is not a unicast MAC
  56. */
  57. esp_err_t esp_base_mac_addr_set(const uint8_t *mac);
  58. /**
  59. * @brief Return base MAC address which is set using esp_base_mac_addr_set.
  60. *
  61. * @note If no custom Base MAC has been set, this returns the pre-programmed Espressif base MAC address.
  62. *
  63. * @param mac base MAC address, length: 6 bytes/8 bytes.
  64. * length: 6 bytes for MAC-48
  65. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  66. *
  67. * @return ESP_OK on success
  68. * ESP_ERR_INVALID_ARG mac is NULL
  69. * ESP_ERR_INVALID_MAC base MAC address has not been set
  70. */
  71. esp_err_t esp_base_mac_addr_get(uint8_t *mac);
  72. /**
  73. * @brief Return base MAC address which was previously written to BLK3 of EFUSE.
  74. *
  75. * Base MAC address is used to generate the MAC addresses used by the networking interfaces.
  76. * This API returns the custom base MAC address which was previously written to EFUSE BLK3 in
  77. * a specified format.
  78. *
  79. * Writing this EFUSE allows setting of a different (non-Espressif) base MAC address. It is also
  80. * possible to store a custom base MAC address elsewhere, see esp_base_mac_addr_set() for details.
  81. *
  82. * @note This function is currently only supported on ESP32.
  83. *
  84. * @param mac base MAC address, length: 6 bytes/8 bytes.
  85. * length: 6 bytes for MAC-48
  86. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  87. *
  88. * @return ESP_OK on success
  89. * ESP_ERR_INVALID_ARG mac is NULL
  90. * ESP_ERR_INVALID_MAC CUSTOM_MAC address has not been set, all zeros (for esp32-xx)
  91. * ESP_ERR_INVALID_VERSION An invalid MAC version field was read from BLK3 of EFUSE (for esp32)
  92. * ESP_ERR_INVALID_CRC An invalid MAC CRC was read from BLK3 of EFUSE (for esp32)
  93. */
  94. esp_err_t esp_efuse_mac_get_custom(uint8_t *mac);
  95. /**
  96. * @brief Return base MAC address which is factory-programmed by Espressif in EFUSE.
  97. *
  98. * @param mac base MAC address, length: 6 bytes/8 bytes.
  99. * length: 6 bytes for MAC-48
  100. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  101. *
  102. * @return ESP_OK on success
  103. * ESP_ERR_INVALID_ARG mac is NULL
  104. */
  105. esp_err_t esp_efuse_mac_get_default(uint8_t *mac);
  106. /**
  107. * @brief Read base MAC address and set MAC address of the interface.
  108. *
  109. * This function first get base MAC address using esp_base_mac_addr_get().
  110. * Then calculates the MAC address of the specific interface requested,
  111. * refer to ESP-IDF Programming Guide for the algorithm.
  112. *
  113. * @param mac base MAC address, length: 6 bytes/8 bytes.
  114. * length: 6 bytes for MAC-48
  115. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  116. * @param type Type of MAC address to return
  117. *
  118. * @return ESP_OK on success
  119. */
  120. esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type);
  121. /**
  122. * @brief Derive local MAC address from universal MAC address.
  123. *
  124. * This function copies a universal MAC address and then sets the "locally
  125. * administered" bit (bit 0x2) in the first octet, creating a locally
  126. * administered MAC address.
  127. *
  128. * If the universal MAC address argument is already a locally administered MAC
  129. * address, then the first octet is XORed with 0x4 in order to create a different
  130. * locally administered MAC address.
  131. *
  132. * @param mac base MAC address, length: 6 bytes/8 bytes.
  133. * length: 6 bytes for MAC-48
  134. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  135. * @param universal_mac Source universal MAC address, length: 6 bytes.
  136. *
  137. * @return ESP_OK on success
  138. */
  139. esp_err_t esp_derive_local_mac(uint8_t *local_mac, const uint8_t *universal_mac);
  140. #ifdef __cplusplus
  141. }
  142. #endif