MacAddr.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2015-2018, Dataspeed Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Dataspeed Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 #ifndef _DATASPEED_CAN_USB_MAC_ADDR_H
00036 #define _DATASPEED_CAN_USB_MAC_ADDR_H
00037 
00038 // Standard libraries
00039 #include <stdint.h>
00040 #include <string>
00041 #include <sstream>
00042 #include <iomanip>  // std::setfill, std::setw
00043 #include <algorithm>
00044 
00045 namespace dataspeed_can_usb
00046 {
00047 
00048 class MacAddr {
00049 public:
00050   MacAddr() { memset(addr_, 0x00, sizeof(addr_)); };
00051   MacAddr(const uint8_t *addr) { memcpy(addr_, addr, sizeof(addr_)); };
00052   MacAddr(uint8_t mac0, uint8_t mac1, uint8_t mac2, uint8_t mac3, uint8_t mac4, uint8_t mac5) {
00053     addr_[0] = mac0;
00054     addr_[1] = mac1;
00055     addr_[2] = mac2;
00056     addr_[3] = mac3;
00057     addr_[4] = mac4;
00058     addr_[5] = mac5;
00059   };
00060   bool valid() const {
00061     return ((addr_[0] != 0x00) || (addr_[1] != 0x00) || (addr_[2] != 0x00) || (addr_[3] != 0x00) || (addr_[4] != 0x00) || (addr_[5] != 0x00))
00062         && ((addr_[0] != 0xFF) || (addr_[1] != 0xFF) || (addr_[2] != 0xFF) || (addr_[3] != 0xFF) || (addr_[4] != 0xFF) || (addr_[5] != 0xFF));
00063   }
00064   std::string toString(bool upper = false) const {
00065     std::stringstream ss;
00066     ss << std::setfill('0') << std::hex;
00067     if (upper) { ss << std::uppercase; }
00068     ss << std::setw(2) << (unsigned int)addr_[0] << ":";
00069     ss << std::setw(2) << (unsigned int)addr_[1] << ":";
00070     ss << std::setw(2) << (unsigned int)addr_[2] << ":";
00071     ss << std::setw(2) << (unsigned int)addr_[3] << ":";
00072     ss << std::setw(2) << (unsigned int)addr_[4] << ":";
00073     ss << std::setw(2) << (unsigned int)addr_[5];
00074     return ss.str();
00075   }
00076   bool match(const MacAddr& other) const {
00077     if (this->valid() && other.valid()) {
00078       return (this->mac0() == other.mac0())
00079           && (this->mac1() == other.mac1())
00080           && (this->mac2() == other.mac2())
00081           && (this->mac3() == other.mac3())
00082           && (this->mac4() == other.mac4())
00083           && (this->mac5() == other.mac5());
00084     }
00085     return false;
00086   }
00087   bool match(const std::string& str) const {
00088     std::string mac1 = this->toString();
00089     std::string mac2 = str;
00090 
00091     // Convert to upper case
00092     std::transform(mac1.begin(), mac1.end(), mac1.begin(), ::toupper);
00093     std::transform(mac2.begin(), mac2.end(), mac2.begin(), ::toupper);
00094 
00095     // Remove the ':' character
00096     mac1.erase(std::remove(mac1.begin(), mac1.end(), ':'), mac1.end());
00097     mac2.erase(std::remove(mac2.begin(), mac2.end(), ':'), mac2.end());
00098 
00099     // Verify 12 digits
00100     if ((mac1.length() == 12) && (mac2.length() == 12)) {
00101       // Disregard all zeros
00102       if ((mac1 != "000000000000") && (mac2 != "000000000000")) {
00103         // Disregard all ones
00104         if ((mac1 != "FFFFFFFFFFFF") && (mac2 != "FFFFFFFFFFFF")) {
00105           // Finally, check equality
00106           if (mac1 == mac2) {
00107             return true;
00108           }
00109         }
00110       }
00111     }
00112     return false;
00113   }
00114   uint8_t mac0() const { return addr_[0]; }
00115   uint8_t mac1() const { return addr_[1]; }
00116   uint8_t mac2() const { return addr_[2]; }
00117   uint8_t mac3() const { return addr_[3]; }
00118   uint8_t mac4() const { return addr_[4]; }
00119   uint8_t mac5() const { return addr_[5]; }
00120 private:
00121   uint8_t addr_[6];
00122 };
00123 
00124 } // namespace dataspeed_can_usb
00125 
00126 #endif // _DATASPEED_CAN_USB_MAC_ADDR_H
00127 


dataspeed_can_usb
Author(s): Kevin Hallenbeck
autogenerated on Thu Jun 6 2019 21:16:43