EtherSia
A minimal IPv6 Ethernet library for Arduino.
MACAddress.h
Go to the documentation of this file.
1 /**
2  * Header file for the MACAddress class
3  * @file MACAddress.h
4  */
5 
6 #ifndef MACAddress_H
7 #define MACAddress_H
8 
9 #include <stdint.h>
10 
11 
12 /**
13  * Class for the storage and manipulation of 48-bit Ethernet addresses.
14  */
15 class MACAddress {
16 private:
17  uint8_t _address[6];
18 
19 public:
20  /**
21  * Constructor for a new / all-zero MAC address (00:00:00:00:00:00)
22  */
23  MACAddress();
24 
25  /**
26  * Constructor for MAC address from a string.
27  * @param macstr a human readable string containing a 17 character MAC address
28  */
29  MACAddress(const char *macstr);
30 
31  /**
32  * Constructor for MAC address from a Flash string (use the F() macro)
33  * @param macstr a human readable string containing a 17 character MAC address
34  */
35  MACAddress(const __FlashStringHelper *macstr);
36 
37  /**
38  * Constructor for MAC address from an array of 6 bytes
39  * @param macaddr a 48-bit (6 byte) MAC address
40  */
41  MACAddress(const byte macaddr[6]);
42 
43  /**
44  * Constructor for MAC address from 6-octets.
45  * @param one The first octet of the address
46  * @param two The second octet of the address
47  * @param three The third octet of the address
48  * @param four The fourth octet of the address
49  * @param five The fifth octet of the address
50  * @param six The sixth octet of the address
51  */
52  MACAddress(uint8_t one, uint8_t two, uint8_t three, uint8_t four, uint8_t five, uint8_t six);
53 
54  /**
55  * Parse a human readable MAC address into a MACAddress object.
56  * @param macstr a human readable string containing a 17 character MAC address
57  * @return true if successful, false if parsing failed
58  */
59  boolean fromString(const char *macstr);
60 
61  /**
62  * Parse a flash string MAC address into a MACAddress object (use the F() macro)
63  * @param macstr a human readable string containing a 17 character MAC address
64  * @return true if successful, false if parsing failed
65  */
66  boolean fromString(const __FlashStringHelper *macstr);
67 
68  /**
69  * Cast the MAC address to an array of octets.
70  */
71  operator uint8_t*();
72 
73  /**
74  * Check if the address equals another MAC address.
75  * @param address the second address to compare to
76  * @return true if the two addresses are the same
77  */
78  boolean operator==(const MACAddress& address) const;
79 
80  /**
81  * Check if the address is not equal to another MAC address.
82  * @param address the second address to compare to
83  * @return true if the two addresses are the same
84  */
85  boolean operator!=(const MACAddress& address) const;
86 
87  /**
88  * Calculate the multicast MAC address for an IPv6 address.
89  * @param address An IPv6 address as an array of 16-bytes
90  */
91  void setIPv6Multicast(const uint8_t* address);
92 
93  /**
94  * Check if the MAC address is an IPv6 multicast address.
95  * An IPv6 multicast MAC looks like the pattern: 33:33:xx:xx:xx:xx
96  * @return true if it is an IPv6 multicast address
97  */
98  boolean isIPv6Multicast();
99 
100  /**
101  * Get an individual octet from the MAC address.
102  * Indexed from 0 to 5.
103  * @param index The byte number from the address to get
104  * @return The requested byte from the address
105  */
106  uint8_t operator[](int index) const;
107 
108  /**
109  * Print a MAC address to a stream as a human readable string.
110  * @param print The stream to print to (defaults to Serial)
111  */
112  void print(Print &print=Serial) const;
113 
114  /**
115  * Print a MAC address to a stream with line ending.
116  * @param print The stream to print to (defaults to Serial)
117  */
118  void println(Print &print=Serial) const;
119 
120 } __attribute__((__packed__));
121 
122 /* Verify that compiler gets the structure size correct */
123 static_assert(sizeof(MACAddress) == 6, "Size is not correct");
124 
125 #endif
MACAddress()
Constructor for a new / all-zero MAC address (00:00:00:00:00:00)
Definition: MACAddress.cpp:5
boolean isIPv6Multicast()
Check if the MAC address is an IPv6 multicast address.
Definition: MACAddress.cpp:61
void println(Print &print=Serial) const
Print a MAC address to a stream with line ending.
Definition: MACAddress.cpp:119
boolean operator!=(const MACAddress &address) const
Check if the address is not equal to another MAC address.
Definition: MACAddress.cpp:45
void print(Print &print=Serial) const
Print a MAC address to a stream as a human readable string.
Definition: MACAddress.cpp:110
uint8_t operator[](int index) const
Get an individual octet from the MAC address.
Definition: MACAddress.cpp:105
void setIPv6Multicast(const uint8_t *address)
Calculate the multicast MAC address for an IPv6 address.
Definition: MACAddress.cpp:51
boolean fromString(const char *macstr)
Parse a human readable MAC address into a MACAddress object.
Definition: MACAddress.cpp:66
Class for the storage and manipulation of 48-bit Ethernet addresses.
Definition: MACAddress.h:15
boolean operator==(const MACAddress &address) const
Check if the address equals another MAC address.
Definition: MACAddress.cpp:40