esp_mac.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.
  56. *
  57. * @return ESP_OK on success
  58. * ESP_ERR_INVALID_ARG If mac is NULL or is not a unicast MAC
  59. */
  60. esp_err_t esp_base_mac_addr_set(const uint8_t *mac);
  61. /**
  62. * @brief Return base MAC address which is set using esp_base_mac_addr_set.
  63. *
  64. * @note If no custom Base MAC has been set, this returns the pre-programmed Espressif base MAC address.
  65. *
  66. * @param mac base MAC address, length: 6 bytes.
  67. *
  68. * @return ESP_OK on success
  69. * ESP_ERR_INVALID_MAC base MAC address has not been set
  70. */
  71. esp_err_t esp_base_mac_addr_get(uint8_t *mac);
  72. /**
  73. * @brief Return base MAC address which was previously written to BLK3 of EFUSE.
  74. *
  75. * Base MAC address is used to generate the MAC addresses used by the networking interfaces.
  76. * This API returns the custom base MAC address which was previously written to EFUSE BLK3 in
  77. * a specified format.
  78. *
  79. * Writing this EFUSE allows setting of a different (non-Espressif) base MAC address. It is also
  80. * possible to store a custom base MAC address elsewhere, see esp_base_mac_addr_set() for details.
  81. *
  82. * @note This function is currently only supported on ESP32.
  83. *
  84. * @param mac base MAC address, length: 6 bytes.
  85. *
  86. * @return ESP_OK on success
  87. * ESP_ERR_INVALID_VERSION An invalid MAC version field was read from BLK3 of EFUSE
  88. * ESP_ERR_INVALID_CRC An invalid MAC CRC was read from BLK3 of EFUSE
  89. */
  90. esp_err_t esp_efuse_mac_get_custom(uint8_t *mac);
  91. /**
  92. * @brief Return base MAC address which is factory-programmed by Espressif in EFUSE.
  93. *
  94. * @param mac base MAC address, length: 6 bytes.
  95. *
  96. * @return ESP_OK on success
  97. */
  98. esp_err_t esp_efuse_mac_get_default(uint8_t *mac);
  99. /**
  100. * @brief Read base MAC address and set MAC address of the interface.
  101. *
  102. * This function first get base MAC address using esp_base_mac_addr_get().
  103. * Then calculates the MAC address of the specific interface requested,
  104. * refer to ESP-IDF Programming Guide for the algorithm.
  105. *
  106. * @param mac MAC address of the interface, length: 6 bytes.
  107. * @param type Type of MAC address to return
  108. *
  109. * @return ESP_OK on success
  110. */
  111. esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type);
  112. /**
  113. * @brief Derive local MAC address from universal MAC address.
  114. *
  115. * This function copies a universal MAC address and then sets the "locally
  116. * administered" bit (bit 0x2) in the first octet, creating a locally
  117. * administered MAC address.
  118. *
  119. * If the universal MAC address argument is already a locally administered MAC
  120. * address, then the first octet is XORed with 0x4 in order to create a different
  121. * locally administered MAC address.
  122. *
  123. * @param local_mac Derived local MAC address, length: 6 bytes.
  124. * @param universal_mac Source universal MAC address, length: 6 bytes.
  125. *
  126. * @return ESP_OK on success
  127. */
  128. esp_err_t esp_derive_local_mac(uint8_t* local_mac, const uint8_t* universal_mac);
  129. #ifdef __cplusplus
  130. }
  131. #endif