esp_mac.h 5.4 KB

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