esp_mac.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2021 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #include "esp_err.h"
  15. #include "sdkconfig.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. typedef enum {
  20. ESP_MAC_WIFI_STA,
  21. ESP_MAC_WIFI_SOFTAP,
  22. ESP_MAC_BT,
  23. ESP_MAC_ETH,
  24. ESP_MAC_IEEE802154,
  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_ESP32H2
  38. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32H2_UNIVERSAL_MAC_ADDRESSES
  39. #endif
  40. /** @endcond */
  41. /**
  42. * @brief Set base MAC address with the MAC address which is stored in BLK3 of EFUSE or
  43. * external storage e.g. flash and EEPROM.
  44. *
  45. * Base MAC address is used to generate the MAC addresses used by network interfaces.
  46. *
  47. * If using a custom base MAC address, call this API before initializing any network interfaces.
  48. * Refer to the ESP-IDF Programming Guide for details about how the Base MAC is used.
  49. *
  50. * @note Base MAC must be a unicast MAC (least significant bit of first byte must be zero).
  51. *
  52. * @note If not using a valid OUI, set the "locally administered" bit
  53. * (bit value 0x02 in the first byte) to avoid collisions.
  54. *
  55. * @param mac base MAC address, length: 6 bytes/8 bytes.
  56. * length: 6 bytes for MAC-48
  57. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  58. *
  59. * @return ESP_OK on success
  60. * ESP_ERR_INVALID_ARG If mac is NULL or is not a unicast MAC
  61. */
  62. esp_err_t esp_base_mac_addr_set(const uint8_t *mac);
  63. /**
  64. * @brief Return base MAC address which is set using esp_base_mac_addr_set.
  65. *
  66. * @note If no custom Base MAC has been set, this returns the pre-programmed Espressif base MAC address.
  67. *
  68. * @param mac base MAC address, length: 6 bytes/8 bytes.
  69. * length: 6 bytes for MAC-48
  70. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  71. *
  72. * @return ESP_OK on success
  73. * ESP_ERR_INVALID_ARG mac is NULL
  74. * ESP_ERR_INVALID_MAC base MAC address has not been set
  75. */
  76. esp_err_t esp_base_mac_addr_get(uint8_t *mac);
  77. /**
  78. * @brief Return base MAC address which was previously written to BLK3 of EFUSE.
  79. *
  80. * Base MAC address is used to generate the MAC addresses used by the networking interfaces.
  81. * This API returns the custom base MAC address which was previously written to EFUSE BLK3 in
  82. * a specified format.
  83. *
  84. * Writing this EFUSE allows setting of a different (non-Espressif) base MAC address. It is also
  85. * possible to store a custom base MAC address elsewhere, see esp_base_mac_addr_set() for details.
  86. *
  87. * @note This function is currently only supported on ESP32.
  88. *
  89. * @param mac base MAC address, length: 6 bytes/8 bytes.
  90. * length: 6 bytes for MAC-48
  91. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  92. *
  93. * @return ESP_OK on success
  94. * ESP_ERR_INVALID_ARG mac is NULL
  95. * ESP_ERR_INVALID_MAC CUSTOM_MAC address has not been set, all zeros (for esp32-xx)
  96. * ESP_ERR_INVALID_VERSION An invalid MAC version field was read from BLK3 of EFUSE (for esp32)
  97. * ESP_ERR_INVALID_CRC An invalid MAC CRC was read from BLK3 of EFUSE (for esp32)
  98. */
  99. esp_err_t esp_efuse_mac_get_custom(uint8_t *mac);
  100. /**
  101. * @brief Return base MAC address which is factory-programmed by Espressif in EFUSE.
  102. *
  103. * @param mac base MAC address, length: 6 bytes/8 bytes.
  104. * length: 6 bytes for MAC-48
  105. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  106. *
  107. * @return ESP_OK on success
  108. * ESP_ERR_INVALID_ARG mac is NULL
  109. */
  110. esp_err_t esp_efuse_mac_get_default(uint8_t *mac);
  111. /**
  112. * @brief Read base MAC address and set MAC address of the interface.
  113. *
  114. * This function first get base MAC address using esp_base_mac_addr_get().
  115. * Then calculates the MAC address of the specific interface requested,
  116. * refer to ESP-IDF Programming Guide for the algorithm.
  117. *
  118. * @param mac base MAC address, length: 6 bytes/8 bytes.
  119. * length: 6 bytes for MAC-48
  120. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  121. * @param type Type of MAC address to return
  122. *
  123. * @return ESP_OK on success
  124. */
  125. esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type);
  126. /**
  127. * @brief Derive local MAC address from universal MAC address.
  128. *
  129. * This function copies a universal MAC address and then sets the "locally
  130. * administered" bit (bit 0x2) in the first octet, creating a locally
  131. * administered MAC address.
  132. *
  133. * If the universal MAC address argument is already a locally administered MAC
  134. * address, then the first octet is XORed with 0x4 in order to create a different
  135. * locally administered MAC address.
  136. *
  137. * @param mac base MAC address, length: 6 bytes/8 bytes.
  138. * length: 6 bytes for MAC-48
  139. * 8 bytes for EUI-64(used for IEEE 802.15.4)
  140. * @param universal_mac Source universal MAC address, length: 6 bytes.
  141. *
  142. * @return ESP_OK on success
  143. */
  144. esp_err_t esp_derive_local_mac(uint8_t *local_mac, const uint8_t *universal_mac);
  145. #ifdef __cplusplus
  146. }
  147. #endif