esp_mac.h 4.9 KB

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