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

Last change on this file since 5774 was 5284, checked in by mies, 15 years ago

+ added new transport modules and adapted ariba to them
+ exchange endpoint descriptors an link establishment
+ clean up of base communication
+ link establishment with in the presence of multiple endpoints
+ local discovery for ipv6, ipv4 and bluetooth mac addresses

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