[5284] | 1 | #ifndef MAC_ADDRESS_HPP_
|
---|
| 2 | #define MAC_ADDRESS_HPP_
|
---|
| 3 |
|
---|
| 4 | #include "detail/address_convenience.hpp"
|
---|
| 5 |
|
---|
[7463] | 6 | #include "ariba/config.h"
|
---|
[5284] | 7 | #include<string>
|
---|
| 8 | #include<cassert>
|
---|
[7045] | 9 | #include<stdio.h>
|
---|
[5284] | 10 | #include<boost/tr1/functional.hpp>
|
---|
[7041] | 11 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[5284] | 12 | #include<bluetooth/bluetooth.h>
|
---|
[7041] | 13 | #endif
|
---|
[5284] | 14 |
|
---|
| 15 | namespace ariba {
|
---|
| 16 | namespace addressing {
|
---|
| 17 |
|
---|
| 18 | struct mac_address_info {
|
---|
| 19 | static const uint16_t type_id;
|
---|
| 20 | static const std::string type_name;
|
---|
| 21 | };
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * TODO: Doc
|
---|
| 25 | *
|
---|
| 26 | * @author Sebastian Mies <mies@tm.uka.de>
|
---|
| 27 | */
|
---|
| 28 | template<class AddressInfo = mac_address_info>
|
---|
| 29 | class mac_address_tpl: public detail::address_convenience<mac_address_tpl<AddressInfo> > {
|
---|
| 30 | private:
|
---|
| 31 | uint8_t mac[6];
|
---|
| 32 |
|
---|
| 33 | public:
|
---|
| 34 | mac_address_tpl() {
|
---|
| 35 | for (int i = 0; i < 6; i++)
|
---|
| 36 | mac[i] = 0;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | mac_address_tpl( const mac_address_tpl& copy ) {
|
---|
| 40 | assign(copy);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | mac_address_tpl(const std::string& text) {
|
---|
| 44 | assign(text);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | mac_address_tpl(const char* text) {
|
---|
| 48 | assign(std::string(text));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | mac_address_tpl(const uint8_t* bytes, size_t size) {
|
---|
| 52 | assign(bytes, size);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | //--- compare operations --------------------------------------------------
|
---|
| 56 |
|
---|
| 57 | /// implements comparison operators
|
---|
| 58 | int compare_to(const mac_address_tpl& rhs) const {
|
---|
| 59 | size_t i = 0;
|
---|
| 60 | while (rhs.mac[i] == mac[i] && i < 6)
|
---|
| 61 | i++;
|
---|
| 62 | return (6 - i);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | //--- bytes representation ------------------------------------------------
|
---|
| 66 |
|
---|
| 67 | /// returns true, if this address has a fixed size in bytes
|
---|
| 68 | bool is_bytes_size_static() const {
|
---|
| 69 | return true;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /// returns the number of bytes used for serialization of this address
|
---|
| 73 | size_t to_bytes_size() const {
|
---|
| 74 | return 6;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /// converts this address to a binary representation
|
---|
| 78 | void to_bytes(uint8_t* bytes) const {
|
---|
| 79 | for (size_t i = 0; i < 6; i++)
|
---|
| 80 | bytes[i] = mac[i];
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /// Assigns an address using a bunch of bytes
|
---|
| 84 | bool assign(const uint8_t* bytes, size_t size) {
|
---|
| 85 | assert(size==6);
|
---|
| 86 | for (size_t i = 0; i < 6; i++)
|
---|
| 87 | mac[i] = bytes[i];
|
---|
| 88 | return false;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | //--- string representation -----------------------------------------------
|
---|
| 92 |
|
---|
| 93 | /// convert address to a string that can be used to reconstruct the address
|
---|
| 94 | std::string to_string() const {
|
---|
| 95 | char str[80];
|
---|
| 96 | sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
|
---|
| 97 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
---|
| 98 | return std::string(str);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /// Assigns an address using a human-readable
|
---|
| 102 | bool assign(const std::string& text) {
|
---|
| 103 | unsigned int mac_[6];
|
---|
| 104 | sscanf(text.c_str(), "%x:%x:%x:%x:%x:%x", &mac_[0], &mac_[1], &mac_[2],
|
---|
| 105 | &mac_[3], &mac_[4], &mac_[5]);
|
---|
| 106 | for (size_t i = 0; i < 6; i++)
|
---|
| 107 | mac[i] = (uint8_t) mac_[i];
|
---|
| 108 | return false;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | //--- assignment ----------------------------------------------------------
|
---|
| 112 |
|
---|
| 113 | /// Assigns an address
|
---|
| 114 | bool assign(const mac_address_tpl& rhs) {
|
---|
| 115 | for (size_t i = 0; i < 6; i++)
|
---|
| 116 | mac[i] = rhs.mac[i];
|
---|
| 117 | return false;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | //--- address info --------------------------------------------------------
|
---|
| 121 |
|
---|
| 122 | const std::string& type_name() const {
|
---|
| 123 | return AddressInfo::type_name;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | const uint16_t type_id() const {
|
---|
| 127 | return AddressInfo::type_id;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | //--- conversion ----------------------------------------------------------
|
---|
[7041] | 131 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[5284] | 132 |
|
---|
| 133 | bdaddr_t bluetooth() const {
|
---|
| 134 | bdaddr_t addr;
|
---|
| 135 | for (size_t i=0; i<6; i++) addr.b[i] = mac[5-i];
|
---|
| 136 | return addr;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | void bluetooth( bdaddr_t addr ) {
|
---|
| 140 | for (size_t i=0; i<6; i++) mac[i] = addr.b[5-i];
|
---|
| 141 | }
|
---|
[7041] | 142 | #endif
|
---|
[5284] | 143 | };
|
---|
| 144 |
|
---|
| 145 | typedef mac_address_tpl<> mac_address;
|
---|
| 146 |
|
---|
| 147 | }} // namespace ariba::addressing
|
---|
| 148 |
|
---|
| 149 | namespace boost {
|
---|
| 150 |
|
---|
| 151 | template<class T>
|
---|
| 152 | struct hash<ariba::addressing::mac_address_tpl<T> >: public std::unary_function<ariba::addressing::mac_address_tpl<T>, std::size_t> {
|
---|
| 153 | std::size_t operator()(const ariba::addressing::mac_address_tpl<T>& ep) const {
|
---|
| 154 | return hash_value(ep.to_string());
|
---|
| 155 | }
|
---|
| 156 | };
|
---|
| 157 |
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | #endif /* MAC_ADDRESS_HPP_ */
|
---|