MacAddress.h
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2012 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 
22  http://www.hyperrealm.com/oss_commoncpp.shtml
23 */
24 
25 #pragma once
26 
27 #include "WString.h"
28 #include "Print.h"
29 #include <cstdint>
30 
38 class MacAddress : public Printable
39 {
40  // https://www.artima.com/cppsource/safebool.html
41  typedef void (MacAddress::*bool_type)() const;
42  void Testable() const
43  {
44  }
45 
46 public:
47  typedef uint8_t Octets[6];
48 
49  MacAddress() = default;
50 
51  MacAddress(const Octets& octets)
52  {
53  setOctets(octets);
54  }
55 
59  void getOctets(Octets& octets) const
60  {
61  memcpy(octets, this->octets, 6);
62  }
63 
67  void setOctets(const Octets& octets)
68  {
69  memcpy(this->octets, octets, 6);
70  }
71 
77  uint8_t operator[](unsigned index) const;
78 
84  uint8_t& operator[](unsigned index);
85 
94  String toString(char sep = ':') const;
95 
99  bool operator==(const MacAddress& other) const
100  {
101  return memcmp(octets, other.octets, sizeof(octets)) == 0;
102  }
103 
107  inline bool operator!=(const MacAddress& other) const
108  {
109  return !operator==(other);
110  }
111 
115  bool operator!() const;
116 
120  operator bool_type() const
121  {
122  return operator!() ? nullptr : &MacAddress::Testable;
123  }
124 
128  void clear()
129  {
130  memset(octets, 0, sizeof(octets));
131  }
132 
138  uint32_t getHash() const;
139 
140  size_t printTo(Print& p) const override
141  {
142  return p.print(toString());
143  }
144 
145 private:
146  Octets octets = {0};
147 };
148 
149 #define MACADDR_NONE MacAddress()
size_t printTo(Print &p) const override
Definition: MacAddress.h:140
MacAddress()=default
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:93
uint32_t getHash() const
Generate a simple 32-bit hash from the MAC address.
uint8_t operator[](unsigned index) const
Get the octet at the given index in the MAC address.
bool operator==(const MacAddress &other) const
Equality operator.
Definition: MacAddress.h:99
MacAddress(const Octets &octets)
Definition: MacAddress.h:51
The String class.
Definition: WString.h:136
A network hardware (MAC) address.
Definition: MacAddress.h:38
Provides formatted output to stream.
Definition: Print.h:36
String toString(char sep=':') const
Return a String representation of the MacAddress.
void getOctets(Octets &octets) const
Get the octets of the MAC address.
Definition: MacAddress.h:59
void clear()
Clear address to null value.
Definition: MacAddress.h:128
void setOctets(const Octets &octets)
Set the octets of the MAC address.
Definition: MacAddress.h:67
Definition: Printable.h:42
uint8_t Octets[6]
Definition: MacAddress.h:47
bool operator!() const
Test if this is a null address (00:00:00:00:00:00).
bool operator!=(const MacAddress &other) const
Inequality operator.
Definition: MacAddress.h:107