esp_mac.h 6.9 KB

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