Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 #ifndef _DATASPEED_CAN_USB_MAC_ADDR_H
00036 #define _DATASPEED_CAN_USB_MAC_ADDR_H
00037 
00038 
00039 #include <stdint.h>
00040 #include <string>
00041 #include <sstream>
00042 #include <iomanip>  
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     
00092     std::transform(mac1.begin(), mac1.end(), mac1.begin(), ::toupper);
00093     std::transform(mac2.begin(), mac2.end(), mac2.begin(), ::toupper);
00094 
00095     
00096     mac1.erase(std::remove(mac1.begin(), mac1.end(), ':'), mac1.end());
00097     mac2.erase(std::remove(mac2.begin(), mac2.end(), ':'), mac2.end());
00098 
00099     
00100     if ((mac1.length() == 12) && (mac2.length() == 12)) {
00101       
00102       if ((mac1 != "000000000000") && (mac2 != "000000000000")) {
00103         
00104         if ((mac1 != "FFFFFFFFFFFF") && (mac2 != "FFFFFFFFFFFF")) {
00105           
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 } 
00125 
00126 #endif // _DATASPEED_CAN_USB_MAC_ADDR_H
00127