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