Open Lighting Architecture  Latest Git
MACAddress.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * MACAddress.h
17  * Represents a MAC Address
18  * Copyright (C) 2013 Peter Newman
19  */
20 
21 #ifndef INCLUDE_OLA_NETWORK_MACADDRESS_H_
22 #define INCLUDE_OLA_NETWORK_MACADDRESS_H_
23 
32 #include <stdint.h>
33 #include <sstream>
34 #include <string>
35 
36 namespace ola {
37 namespace network {
38 
50 class MACAddress {
51  public:
52  enum { LENGTH = 6 };
53 
54  MACAddress();
55 
61  explicit MACAddress(const uint8_t *address);
62 
63  MACAddress(const MACAddress &other);
64 
65  MACAddress& operator=(const MACAddress &other);
66 
67  bool operator==(const MACAddress &other) const;
68 
69  bool operator!=(const MACAddress &other) const {
70  return !(*this == other);
71  }
72 
78  bool operator<(const MACAddress &other) const;
79 
80  bool operator>(const MACAddress &other) const;
81 
82  // copy the address in network byte order to a location. The location
83  // should be at least LENGTH bytes.
84  void Get(uint8_t ptr[LENGTH]) const;
85 
92  bool Pack(uint8_t *buffer, unsigned int length) const {
93  if (length < LENGTH)
94  return false;
95  Get(buffer);
96  return true;
97  }
98 
103  std::string ToString() const;
104 
105  friend std::ostream& operator<< (std::ostream &out,
106  const MACAddress &address) {
107  return out << address.ToString();
108  }
109 
116  static MACAddress* FromString(const std::string &address);
117 
125  static bool FromString(const std::string &address, MACAddress *target);
126 
127  // useful for testing
128  static MACAddress FromStringOrDie(const std::string &address);
129 
130  private:
131  uint8_t m_address[LENGTH];
132 };
136 } // namespace network
137 } // namespace ola
138 #endif // INCLUDE_OLA_NETWORK_MACADDRESS_H_
bool Pack(uint8_t *buffer, unsigned int length) const
Write the binary representation of the MAC address to memory.
Definition: MACAddress.h:92
bool operator<(const MACAddress &other) const
Order addresses. Note that this won&#39;t order how humans expect because ether_addr is in network byte o...
Definition: MACAddress.cpp:106
Represents a MAC Address.
Definition: MACAddress.h:50
static MACAddress * FromString(const std::string &address)
Convert a string to a MACAddress object.
Definition: MACAddress.cpp:161
std::string ToString() const
Convert a mac address to a human readable string.
Definition: MACAddress.cpp:118
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44