00001 #ifndef RFCOMM_ENDPOINT_HPP_
00002 #define RFCOMM_ENDPOINT_HPP_
00003
00004 #include<string>
00005
00006 #include<boost/tr1/functional.hpp>
00007 #include<boost/asio.hpp>
00008
00009 #include "detail/address_convenience.hpp"
00010
00011 #include "port_address.hpp"
00012 #include "mac_address.hpp"
00013
00014 namespace ariba {
00015 namespace addressing {
00016
00017 using namespace std;
00018
00019 struct rfcomm_channel_address_info {
00020 static const uint16_t type_id;
00021 static const std::string type_name;
00022 };
00023 typedef port_address_tpl<rfcomm_channel_address_info> rfcomm_channel_address;
00024
00030 class rfcomm_endpoint : public detail::address_convenience<rfcomm_endpoint> {
00031 private:
00032 mac_address mac_;
00033 rfcomm_channel_address channel_;
00034 static const std::string protocol_name;
00035
00036 public:
00037 typedef rfcomm_endpoint self;
00038
00039 public:
00040 rfcomm_endpoint() :
00041 mac_(), channel_() {
00042 }
00043
00044 rfcomm_endpoint( const rfcomm_endpoint& copy ) {
00045 assign(copy);
00046 }
00047
00048 rfcomm_endpoint( const mac_address& mac, const rfcomm_channel_address& channel) :
00049 mac_(mac), channel_(channel) {
00050 }
00051
00052 rfcomm_endpoint(const std::string& mac, const std::string& channel) :
00053 mac_(mac), channel_(channel) {
00054 }
00055
00056 rfcomm_endpoint(const std::string& mac, uint16_t channel) :
00057 mac_(mac), channel_(channel) {
00058 }
00059
00060 rfcomm_endpoint(const std::string& text) {
00061 assign( text );
00062 }
00063
00064 rfcomm_endpoint(const char* text) {
00065 assign( std::string(text) );
00066 }
00067
00068 rfcomm_endpoint(const uint8_t* bytes, size_t size) {
00069 assign( bytes, size );
00070 }
00071
00072 bool assign( const self& copy ) {
00073 this->mac_ = copy.mac_;
00074 this->channel_ = copy.channel_;
00075 return false;
00076 }
00077
00078
00079
00081 int compare_to(const self& rhs) const {
00082 if (mac_.compare_to(rhs.mac_)==0 && channel_.compare_to(rhs.channel_)==0) return 0;
00083 return 1;
00084 }
00085
00086
00087
00089 bool is_bytes_size_static() const {
00090 return true;
00091 }
00092
00094 size_t to_bytes_size() const {
00095 return mac_.to_bytes_size() + channel_.to_bytes_size();
00096 }
00097
00099 void to_bytes(uint8_t* bytes) const {
00100 mac_.to_bytes(bytes);
00101 channel_.to_bytes(bytes+mac_.to_bytes_size());
00102 }
00103
00105 bool assign(const uint8_t* bytes, size_t size) {
00106 assert(size==8);
00107 return false;
00108 }
00109
00110
00111
00113 std::string to_string() const {
00114 return std::string("[")+mac_.to_string()+std::string("]:")+channel_.to_string();
00115 }
00116
00118 bool assign(const std::string& text) {
00119 std::string mac_str;
00120 std::string channel_str;
00121 if (text.at(0)=='[') {
00122 int i = text.find(']',1);
00123 mac_str = text.substr(1,i-1);
00124 channel_str = text.substr(i+2, text.size()-i-1);
00125 return mac_.assign(mac_str) || channel_.assign(channel_str);
00126 }
00127 return true;
00128 }
00129
00130
00131
00133 const string& type_name() const {
00134 return protocol_name;
00135 }
00136
00138 uint16_t type_id() const {
00139 return 0xFE05;
00140 }
00141
00142
00143
00144 mac_address& mac() {
00145 return mac_;
00146 }
00147
00148 const mac_address& mac() const {
00149 return mac_;
00150 }
00151
00152 rfcomm_channel_address& channel() {
00153 return channel_;
00154 }
00155
00156 const rfcomm_channel_address& channel() const {
00157 return channel_;
00158 }
00159 };
00160
00161 }}
00162
00163 namespace boost {
00164
00165 template<>
00166 struct hash<ariba::addressing::rfcomm_endpoint>: public std::unary_function<ariba::addressing::rfcomm_endpoint, std::size_t> {
00167 std::size_t operator()(const ariba::addressing::rfcomm_endpoint& ep) const {
00168 return hash_value(ep.to_string());
00169 }
00170 };
00171
00172 }
00173
00174
00175 #endif