esp_mac.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 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. #ifndef MAC2STR
  10. #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
  11. #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
  12. #endif
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. typedef enum {
  17. ESP_MAC_WIFI_STA, /**< MAC for WiFi Station (6 bytes) */
  18. ESP_MAC_WIFI_SOFTAP, /**< MAC for WiFi Soft-AP (6 bytes) */
  19. ESP_MAC_BT, /**< MAC for Bluetooth (6 bytes) */
  20. ESP_MAC_ETH, /**< MAC for Ethernet (6 bytes) */
  21. ESP_MAC_IEEE802154, /**< if CONFIG_SOC_IEEE802154_SUPPORTED=y, MAC for IEEE802154 (8 bytes) */
  22. ESP_MAC_BASE, /**< Base MAC for that used for other MAC types (6 bytes) */
  23. ESP_MAC_EFUSE_FACTORY, /**< MAC_FACTORY eFuse which was burned by Espressif in production (6 bytes) */
  24. ESP_MAC_EFUSE_CUSTOM, /**< MAC_CUSTOM eFuse which was can be burned by customer (6 bytes) */
  25. ESP_MAC_EFUSE_EXT, /**< if CONFIG_SOC_IEEE802154_SUPPORTED=y, MAC_EXT eFuse which is used as an extender for IEEE802154 MAC (2 bytes) */
  26. } esp_mac_type_t;
  27. /** @cond */
  28. #define TWO_UNIVERSAL_MAC_ADDR 2
  29. #define FOUR_UNIVERSAL_MAC_ADDR 4
  30. #if CONFIG_IDF_TARGET_ESP32
  31. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES
  32. #elif CONFIG_IDF_TARGET_ESP32S2
  33. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32S2_UNIVERSAL_MAC_ADDRESSES
  34. #elif CONFIG_IDF_TARGET_ESP32S3
  35. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32S3_UNIVERSAL_MAC_ADDRESSES
  36. #elif CONFIG_IDF_TARGET_ESP32C3
  37. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES
  38. #elif CONFIG_IDF_TARGET_ESP32C2
  39. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32C2_UNIVERSAL_MAC_ADDRESSES
  40. #elif CONFIG_IDF_TARGET_ESP32C6
  41. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32C6_UNIVERSAL_MAC_ADDRESSES
  42. #endif
  43. /** @endcond */
  44. /**
  45. * @brief Set base MAC address with the MAC address which is stored in BLK3 of EFUSE or
  46. * external storage e.g. flash and EEPROM.
  47. *
  48. * Base MAC address is used to generate the MAC addresses used by network interfaces.
  49. *
  50. * If using a custom base MAC address, call this API before initializing any network interfaces.
  51. * Refer to the ESP-IDF Programming Guide for details about how the Base MAC is used.
  52. *
  53. * @note Base MAC must be a unicast MAC (least significant bit of first byte must be zero).
  54. *
  55. * @note If not using a valid OUI, set the "locally administered" bit
  56. * (bit value 0x02 in the first byte) to avoid collisions.
  57. *
  58. * @param mac base MAC address, length: 6 bytes.
  59. * length: 6 bytes for MAC-48
  60. *
  61. * @return ESP_OK on success
  62. * ESP_ERR_INVALID_ARG If mac is NULL or is not a unicast MAC
  63. */
  64. esp_err_t esp_base_mac_addr_set(const uint8_t *mac);
  65. /**
  66. * @brief Return base MAC address which is set using esp_base_mac_addr_set.
  67. *
  68. * @note If no custom Base MAC has been set, this returns the pre-programmed Espressif base MAC address.
  69. *
  70. * @param mac base MAC address, length: 6 bytes.
  71. * length: 6 bytes for MAC-48
  72. *
  73. * @return ESP_OK on success
  74. * ESP_ERR_INVALID_ARG mac is NULL
  75. * ESP_ERR_INVALID_MAC base MAC address has not been set
  76. */
  77. esp_err_t esp_base_mac_addr_get(uint8_t *mac);
  78. /**
  79. * @brief Return base MAC address which was previously written to BLK3 of EFUSE.
  80. *
  81. * Base MAC address is used to generate the MAC addresses used by the networking interfaces.
  82. * This API returns the custom base MAC address which was previously written to EFUSE BLK3 in
  83. * a specified format.
  84. *
  85. * Writing this EFUSE allows setting of a different (non-Espressif) base MAC address. It is also
  86. * possible to store a custom base MAC address elsewhere, see esp_base_mac_addr_set() for details.
  87. *
  88. * @note This function is currently only supported on ESP32.
  89. *
  90. * @param mac base MAC address, length: 6 bytes/8 bytes.
  91. * length: 6 bytes for MAC-48
  92. * 8 bytes for EUI-64(used for IEEE 802.15.4, if CONFIG_SOC_IEEE802154_SUPPORTED=y)
  93. *
  94. * @return ESP_OK on success
  95. * ESP_ERR_INVALID_ARG mac is NULL
  96. * ESP_ERR_INVALID_MAC CUSTOM_MAC address has not been set, all zeros (for esp32-xx)
  97. * ESP_ERR_INVALID_VERSION An invalid MAC version field was read from BLK3 of EFUSE (for esp32)
  98. * ESP_ERR_INVALID_CRC An invalid MAC CRC was read from BLK3 of EFUSE (for esp32)
  99. */
  100. esp_err_t esp_efuse_mac_get_custom(uint8_t *mac);
  101. /**
  102. * @brief Return base MAC address which is factory-programmed by Espressif in EFUSE.
  103. *
  104. * @param mac base MAC address, length: 6 bytes/8 bytes.
  105. * length: 6 bytes for MAC-48
  106. * 8 bytes for EUI-64(used for IEEE 802.15.4, if CONFIG_SOC_IEEE802154_SUPPORTED=y)
  107. *
  108. * @return ESP_OK on success
  109. * ESP_ERR_INVALID_ARG mac is NULL
  110. */
  111. esp_err_t esp_efuse_mac_get_default(uint8_t *mac);
  112. /**
  113. * @brief Read base MAC address and set MAC address of the interface.
  114. *
  115. * This function first get base MAC address using esp_base_mac_addr_get().
  116. * Then calculates the MAC address of the specific interface requested,
  117. * refer to ESP-IDF Programming Guide for the algorithm.
  118. *
  119. * The MAC address set by the esp_iface_mac_addr_set() function will not depend on the base MAC address.
  120. *
  121. * @param mac base MAC address, length: 6 bytes/8 bytes.
  122. * length: 6 bytes for MAC-48
  123. * 8 bytes for EUI-64(used for IEEE 802.15.4, if CONFIG_SOC_IEEE802154_SUPPORTED=y)
  124. * @param type Type of MAC address to return
  125. *
  126. * @return ESP_OK on success
  127. */
  128. esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type);
  129. /**
  130. * @brief Derive local MAC address from universal MAC address.
  131. *
  132. * This function copies a universal MAC address and then sets the "locally
  133. * administered" bit (bit 0x2) in the first octet, creating a locally
  134. * administered MAC address.
  135. *
  136. * If the universal MAC address argument is already a locally administered MAC
  137. * address, then the first octet is XORed with 0x4 in order to create a different
  138. * locally administered MAC address.
  139. *
  140. * @param local_mac base MAC address, length: 6 bytes.
  141. * length: 6 bytes for MAC-48
  142. * @param universal_mac Source universal MAC address, length: 6 bytes.
  143. *
  144. * @return ESP_OK on success
  145. */
  146. esp_err_t esp_derive_local_mac(uint8_t *local_mac, const uint8_t *universal_mac);
  147. /**
  148. * @brief Set custom MAC address of the interface. This function allows you to overwrite the MAC addresses
  149. * of the interfaces set by the base MAC address.
  150. *
  151. * @param mac MAC address, length: 6 bytes/8 bytes.
  152. * length: 6 bytes for MAC-48
  153. * 8 bytes for EUI-64(used for ESP_MAC_IEEE802154 type, if CONFIG_SOC_IEEE802154_SUPPORTED=y)
  154. * @param type Type of MAC address
  155. *
  156. * @return ESP_OK on success
  157. */
  158. esp_err_t esp_iface_mac_addr_set(const uint8_t *mac, esp_mac_type_t type);
  159. /**
  160. * @brief Return the size of the MAC type in bytes.
  161. *
  162. * If CONFIG_SOC_IEEE802154_SUPPORTED is set then for these types:
  163. * - ESP_MAC_IEEE802154 is 8 bytes.
  164. * - ESP_MAC_BASE, ESP_MAC_EFUSE_FACTORY and ESP_MAC_EFUSE_CUSTOM the MAC size is 6 bytes.
  165. * - ESP_MAC_EFUSE_EXT is 2 bytes.
  166. * If CONFIG_SOC_IEEE802154_SUPPORTED is not set then for all types it returns 6 bytes.
  167. *
  168. * @param type Type of MAC address
  169. *
  170. * @return 0 MAC type not found (not supported)
  171. * 6 bytes for MAC-48.
  172. * 8 bytes for EUI-64.
  173. */
  174. size_t esp_mac_addr_len_get(esp_mac_type_t type);
  175. #ifdef __cplusplus
  176. }
  177. #endif