idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
mac.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
19#include <idfxx/error>
20
21#include <array>
22#include <cstdint>
23#include <esp_mac.h>
24#include <soc/soc_caps.h>
25#include <string>
26
27namespace idfxx {
28
33enum class mac_type : int {
34 // clang-format off
35 wifi_sta = 0,
36 wifi_softap = 1,
37 bt = 2,
38 ethernet = 3,
39#if SOC_IEEE802154_SUPPORTED
40 ieee802154 = 4,
41#endif
42 base = 5,
43 efuse_factory = 6,
44 efuse_custom = 7,
45#if SOC_IEEE802154_SUPPORTED
46 efuse_ext = 8,
47#endif
48 // clang-format on
49};
50
63public:
68
76
89
95 [[nodiscard]] constexpr const std::array<uint8_t, 6>& bytes() const noexcept { return _bytes; }
96
102 [[nodiscard]] constexpr const uint8_t* data() const noexcept { return _bytes.data(); }
103
109 [[nodiscard]] constexpr uint8_t* data() noexcept { return _bytes.data(); }
110
117 [[nodiscard]] constexpr uint8_t operator[](std::size_t i) const noexcept { return _bytes[i]; }
118
124 [[nodiscard]] constexpr bool operator==(const mac_address&) const noexcept = default;
125
126private:
127 std::array<uint8_t, 6> _bytes{};
128};
129
136[[nodiscard]] std::string to_string(const mac_address& addr);
137
138// =============================================================================
139// MAC address reading
140// =============================================================================
141
142#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
154
166#endif
167
177
186
187// =============================================================================
188// MAC address configuration
189// =============================================================================
190
191#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
204
215#endif
216
229
240
241// =============================================================================
242// eFuse MAC helpers
243// =============================================================================
244
251
259
271
// end of idfxx_hw_support_mac // end of idfxx_hw_support
274
275} // namespace idfxx
276
277#include "sdkconfig.h"
278#ifdef CONFIG_IDFXX_STD_FORMAT
280#include <algorithm>
281#include <format>
282namespace std {
283template<>
284struct formatter<idfxx::mac_address> {
285 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
286
287 template<typename FormatContext>
288 auto format(const idfxx::mac_address& addr, FormatContext& ctx) const {
289 auto s = to_string(addr);
290 return std::copy(s.begin(), s.end(), ctx.out());
291 }
292};
293} // namespace std
295#endif // CONFIG_IDFXX_STD_FORMAT
A MAC-48 (6-byte) hardware address.
Definition mac.hpp:62
constexpr uint8_t * data() noexcept
Returns a mutable pointer to the raw byte data.
Definition mac.hpp:109
constexpr mac_address() noexcept=default
Constructs a zero-initialized MAC address (00:00:00:00:00:00).
constexpr const uint8_t * data() const noexcept
Returns a pointer to the raw byte data.
Definition mac.hpp:102
constexpr bool operator==(const mac_address &) const noexcept=default
Equality comparison.
constexpr mac_address(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5) noexcept
Constructs a MAC address from individual bytes.
Definition mac.hpp:87
constexpr uint8_t operator[](std::size_t i) const noexcept
Accesses a byte by index.
Definition mac.hpp:117
constexpr const std::array< uint8_t, 6 > & bytes() const noexcept
Returns a reference to the underlying byte array.
Definition mac.hpp:95
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
Definition cpu.hpp:52
@ ieee802154
Chip has IEEE 802.15.4 (Zigbee/Thread).
result< void > try_set_base_mac_address(const mac_address &addr)
Sets the base MAC address.
result< void > try_set_interface_mac_address(const mac_address &addr, mac_type type)
Sets the MAC address for a specific interface.
mac_address base_mac_address()
Returns the base MAC address.
result< mac_address > try_read_mac(mac_type type)
Reads the MAC address for a specific interface.
result< mac_address > try_derive_local_mac(const mac_address &universal_mac)
Derives a local MAC address from a universal MAC address.
void set_interface_mac_address(const mac_address &addr, mac_type type)
Sets the MAC address for a specific interface.
mac_type
Identifies which network interface a MAC address belongs to.
Definition mac.hpp:33
mac_address read_mac(mac_type type)
Reads the MAC address for a specific interface.
result< mac_address > try_custom_mac()
Returns the custom MAC address from eFuse.
void set_base_mac_address(const mac_address &addr)
Sets the base MAC address.
result< mac_address > try_default_mac()
Returns the factory MAC address from eFuse.
result< mac_address > try_base_mac_address()
Returns the base MAC address.
@ wifi_sta
WiFi station interface.
@ wifi_softap
WiFi soft-AP interface.
@ ethernet
Ethernet interface.
@ base
Base (factory) MAC address.
@ bt
Bluetooth interface.
@ efuse_factory
Factory MAC stored in eFuse.
@ efuse_custom
Custom MAC stored in eFuse.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120