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