00001 #ifndef MAC_ADDRESS_HPP_
00002 #define MAC_ADDRESS_HPP_
00003
00004 #include "detail/address_convenience.hpp"
00005
00006 #include "ariba/config.h"
00007 #include<string>
00008 #include<cassert>
00009 #include<stdio.h>
00010 #include<boost/tr1/functional.hpp>
00011 #ifdef HAVE_LIBBLUETOOTH
00012 #include<bluetooth/bluetooth.h>
00013 #endif
00014
00015 namespace ariba {
00016 namespace addressing {
00017
00018 struct mac_address_info {
00019 static const uint16_t type_id;
00020 static const std::string type_name;
00021 };
00022
00028 template<class AddressInfo = mac_address_info>
00029 class mac_address_tpl: public detail::address_convenience<mac_address_tpl<AddressInfo> > {
00030 private:
00031 uint8_t mac[6];
00032
00033 public:
00034 mac_address_tpl() {
00035 for (int i = 0; i < 6; i++)
00036 mac[i] = 0;
00037 }
00038
00039 mac_address_tpl( const mac_address_tpl& copy ) {
00040 assign(copy);
00041 }
00042
00043 mac_address_tpl(const std::string& text) {
00044 assign(text);
00045 }
00046
00047 mac_address_tpl(const char* text) {
00048 assign(std::string(text));
00049 }
00050
00051 mac_address_tpl(const uint8_t* bytes, size_t size) {
00052 assign(bytes, size);
00053 }
00054
00055
00056
00058 int compare_to(const mac_address_tpl& rhs) const {
00059 size_t i = 0;
00060 while (rhs.mac[i] == mac[i] && i < 6)
00061 i++;
00062 return (6 - i);
00063 }
00064
00065
00066
00068 bool is_bytes_size_static() const {
00069 return true;
00070 }
00071
00073 size_t to_bytes_size() const {
00074 return 6;
00075 }
00076
00078 void to_bytes(uint8_t* bytes) const {
00079 for (size_t i = 0; i < 6; i++)
00080 bytes[i] = mac[i];
00081 }
00082
00084 bool assign(const uint8_t* bytes, size_t size) {
00085 assert(size==6);
00086 for (size_t i = 0; i < 6; i++)
00087 mac[i] = bytes[i];
00088 return false;
00089 }
00090
00091
00092
00094 std::string to_string() const {
00095 char str[80];
00096 sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
00097 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00098 return std::string(str);
00099 }
00100
00102 bool assign(const std::string& text) {
00103 unsigned int mac_[6];
00104 sscanf(text.c_str(), "%x:%x:%x:%x:%x:%x", &mac_[0], &mac_[1], &mac_[2],
00105 &mac_[3], &mac_[4], &mac_[5]);
00106 for (size_t i = 0; i < 6; i++)
00107 mac[i] = (uint8_t) mac_[i];
00108 return false;
00109 }
00110
00111
00112
00114 bool assign(const mac_address_tpl& rhs) {
00115 for (size_t i = 0; i < 6; i++)
00116 mac[i] = rhs.mac[i];
00117 return false;
00118 }
00119
00120
00121
00122 const std::string& type_name() const {
00123 return AddressInfo::type_name;
00124 }
00125
00126 const uint16_t type_id() const {
00127 return AddressInfo::type_id;
00128 }
00129
00130
00131 #ifdef HAVE_LIBBLUETOOTH
00132
00133 bdaddr_t bluetooth() const {
00134 bdaddr_t addr;
00135 for (size_t i=0; i<6; i++) addr.b[i] = mac[5-i];
00136 return addr;
00137 }
00138
00139 void bluetooth( bdaddr_t addr ) {
00140 for (size_t i=0; i<6; i++) mac[i] = addr.b[5-i];
00141 }
00142 #endif
00143 };
00144
00145 typedef mac_address_tpl<> mac_address;
00146
00147 }}
00148
00149 namespace boost {
00150
00151 template<class T>
00152 struct hash<ariba::addressing::mac_address_tpl<T> >: public std::unary_function<ariba::addressing::mac_address_tpl<T>, std::size_t> {
00153 std::size_t operator()(const ariba::addressing::mac_address_tpl<T>& ep) const {
00154 return hash_value(ep.to_string());
00155 }
00156 };
00157
00158 }
00159
00160
00161 #endif