MacAddr.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2015-2020, Dataspeed Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Dataspeed Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #ifndef _DATASPEED_CAN_USB_MAC_ADDR_H
36 #define _DATASPEED_CAN_USB_MAC_ADDR_H
37 
38 // Standard libraries
39 #include <stdint.h>
40 #include <string>
41 #include <sstream>
42 #include <iomanip> // std::setfill, std::setw
43 #include <algorithm>
44 
45 namespace dataspeed_can_usb
46 {
47 
48 class MacAddr {
49 public:
50  MacAddr() { memset(addr_, 0x00, sizeof(addr_)); };
51  MacAddr(const uint8_t *addr) { memcpy(addr_, addr, sizeof(addr_)); };
52  MacAddr(uint8_t mac0, uint8_t mac1, uint8_t mac2, uint8_t mac3, uint8_t mac4, uint8_t mac5) {
53  addr_[0] = mac0;
54  addr_[1] = mac1;
55  addr_[2] = mac2;
56  addr_[3] = mac3;
57  addr_[4] = mac4;
58  addr_[5] = mac5;
59  };
60  bool valid() const {
61  return ((addr_[0] != 0x00) || (addr_[1] != 0x00) || (addr_[2] != 0x00) || (addr_[3] != 0x00) || (addr_[4] != 0x00) || (addr_[5] != 0x00))
62  && ((addr_[0] != 0xFF) || (addr_[1] != 0xFF) || (addr_[2] != 0xFF) || (addr_[3] != 0xFF) || (addr_[4] != 0xFF) || (addr_[5] != 0xFF));
63  }
64  std::string toString(bool upper = false) const {
65  std::stringstream ss;
66  ss << std::setfill('0') << std::hex;
67  if (upper) { ss << std::uppercase; }
68  ss << std::setw(2) << (unsigned int)addr_[0] << ":";
69  ss << std::setw(2) << (unsigned int)addr_[1] << ":";
70  ss << std::setw(2) << (unsigned int)addr_[2] << ":";
71  ss << std::setw(2) << (unsigned int)addr_[3] << ":";
72  ss << std::setw(2) << (unsigned int)addr_[4] << ":";
73  ss << std::setw(2) << (unsigned int)addr_[5];
74  return ss.str();
75  }
76  bool match(const MacAddr& other) const {
77  if (this->valid() && other.valid()) {
78  return (this->mac0() == other.mac0())
79  && (this->mac1() == other.mac1())
80  && (this->mac2() == other.mac2())
81  && (this->mac3() == other.mac3())
82  && (this->mac4() == other.mac4())
83  && (this->mac5() == other.mac5());
84  }
85  return false;
86  }
87  bool match(const std::string& str) const {
88  std::string mac1 = this->toString();
89  std::string mac2 = str;
90 
91  // Convert to upper case
92  std::transform(mac1.begin(), mac1.end(), mac1.begin(), ::toupper);
93  std::transform(mac2.begin(), mac2.end(), mac2.begin(), ::toupper);
94 
95  // Remove the ':' character
96  mac1.erase(std::remove(mac1.begin(), mac1.end(), ':'), mac1.end());
97  mac2.erase(std::remove(mac2.begin(), mac2.end(), ':'), mac2.end());
98 
99  // Verify 12 digits
100  if ((mac1.length() == 12) && (mac2.length() == 12)) {
101  // Disregard all zeros
102  if ((mac1 != "000000000000") && (mac2 != "000000000000")) {
103  // Disregard all ones
104  if ((mac1 != "FFFFFFFFFFFF") && (mac2 != "FFFFFFFFFFFF")) {
105  // Finally, check equality
106  if (mac1 == mac2) {
107  return true;
108  }
109  }
110  }
111  }
112  return false;
113  }
114  uint8_t mac0() const { return addr_[0]; }
115  uint8_t mac1() const { return addr_[1]; }
116  uint8_t mac2() const { return addr_[2]; }
117  uint8_t mac3() const { return addr_[3]; }
118  uint8_t mac4() const { return addr_[4]; }
119  uint8_t mac5() const { return addr_[5]; }
120 private:
121  uint8_t addr_[6];
122 };
123 
124 } // namespace dataspeed_can_usb
125 
126 #endif // _DATASPEED_CAN_USB_MAC_ADDR_H
127 
uint8_t mac2() const
Definition: MacAddr.h:116
bool valid() const
Definition: MacAddr.h:60
uint8_t mac0() const
Definition: MacAddr.h:114
bool match(const MacAddr &other) const
Definition: MacAddr.h:76
uint8_t mac5() const
Definition: MacAddr.h:119
MacAddr(uint8_t mac0, uint8_t mac1, uint8_t mac2, uint8_t mac3, uint8_t mac4, uint8_t mac5)
Definition: MacAddr.h:52
uint8_t mac1() const
Definition: MacAddr.h:115
uint8_t mac3() const
Definition: MacAddr.h:117
bool match(const std::string &str) const
Definition: MacAddr.h:87
MacAddr(const uint8_t *addr)
Definition: MacAddr.h:51
std::string toString(bool upper=false) const
Definition: MacAddr.h:64
uint8_t mac4() const
Definition: MacAddr.h:118


dataspeed_can_usb
Author(s): Kevin Hallenbeck
autogenerated on Thu Jul 9 2020 03:42:00