esp_mac.h 5.6 KB

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