source: source/ariba/utility/addressing/mac_address.hpp@ 7041

Last change on this file since 7041 was 7041, checked in by mies, 14 years ago

added bluetooth check

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