00001 #ifndef BOOST_ASIO_BLUETOOTH_BLUETOOTH_ENDPOINT_HPP__
00002 #define BOOST_ASIO_BLUETOOTH_BLUETOOTH_ENDPOINT_HPP__
00003
00004 #include <bluetooth/bluetooth.h>
00005 #include <bluetooth/rfcomm.h>
00006
00007 #include <boost/asio/basic_stream_socket.hpp>
00008
00009 namespace boost {
00010 namespace asio {
00011 namespace bluetooth {
00012
00018 template<typename BluetoothProtocol>
00019 class bluetooth_endpoint {
00020 private:
00021 static bdaddr_t addr_any;
00022
00023 public:
00025 typedef BluetoothProtocol protocol_type;
00026
00029 typedef boost::asio::detail::socket_addr_type data_type;
00030
00031
00033 bluetooth_endpoint() :
00034 data_() {
00035 data_.rc_family = AF_BLUETOOTH;
00036 data_.rc_bdaddr = addr_any;
00037 data_.rc_channel = (uint8_t) 0;
00038 }
00039
00040 bluetooth_endpoint(const BluetoothProtocol& protocol,
00041 unsigned short channel) :
00042 data_() {
00043 data_.rc_family = AF_BLUETOOTH;
00044 data_.rc_bdaddr = addr_any;
00045 data_.rc_channel = channel;
00046 }
00047
00052 bluetooth_endpoint(unsigned short channel) :
00053 data_() {
00054 data_.rc_family = AF_BLUETOOTH;
00055 data_.rc_bdaddr = *BDADDR_ANY;
00056 data_.rc_channel = channel;
00057 }
00058
00061 bluetooth_endpoint(const char *addr, unsigned short channel) :
00062 data_() {
00063 data_.rc_family = AF_BLUETOOTH;
00064 data_.rc_channel = channel;
00065 str2ba(addr, &data_.rc_bdaddr);
00066 }
00067
00070 bluetooth_endpoint(bdaddr_t addr, unsigned short channel) :
00071 data_() {
00072 data_.rc_family = AF_BLUETOOTH;
00073 data_.rc_channel = channel;
00074 data_.rc_bdaddr = addr;
00075 }
00076
00078 bluetooth_endpoint(const bluetooth_endpoint& other) :
00079 data_(other.data_) {
00080 }
00081
00083 bluetooth_endpoint& operator=(const bluetooth_endpoint& other) {
00084 data_ = other.data_;
00085 return *this;
00086 }
00087
00089 protocol_type protocol() const {
00090 return protocol_type::get();
00091 }
00092
00095 data_type* data() {
00096 return (boost::asio::detail::socket_addr_type*) &data_;
00097 }
00098
00100 const data_type* data() const {
00101 return (boost::asio::detail::socket_addr_type*) &data_;
00102 }
00103
00105 std::size_t size() const {
00106 return sizeof(data_type);
00107 }
00108
00110 void resize(std::size_t size) {
00111 if (size > sizeof(data_type)) {
00112 boost::system::system_error e(boost::asio::error::invalid_argument);
00113 boost::throw_exception(e);
00114 }
00115 }
00116
00118 std::size_t capacity() const {
00119 return sizeof(data_type);
00120 }
00121
00124 unsigned short channel() const {
00125 return data_.rc_channel;
00126 }
00127
00130 void channel(unsigned short channel_num) {
00131 data_.rc_channel = channel_num;
00132 }
00133
00135 bdaddr_t address() const {
00136 return data_.rc_bdaddr;
00137 }
00138
00140 void address(const boost::asio::ip::address& addr) {
00141 bluetooth_endpoint<BluetoothProtocol> tmp_endpoint(addr, channel());
00142 data_ = tmp_endpoint.data_;
00143 }
00144
00146 void address_hr(char &buf) {
00147 ba2str(&data_.rc_bdaddr, buf);
00148 }
00149
00151 friend bool operator==(const bluetooth_endpoint& e1,
00152 const bluetooth_endpoint& e2) {
00153 return e1.address() == e2.address() && e1.channel() == e2.channel();
00154 }
00155
00157 friend bool operator!=(const bluetooth_endpoint& e1,
00158 const bluetooth_endpoint& e2) {
00159 return e1.address() != e2.address() || e1.channel() != e2.channel();
00160 }
00161
00163 friend bool operator<(const bluetooth_endpoint<BluetoothProtocol>& e1,
00164 const bluetooth_endpoint<BluetoothProtocol>& e2) {
00165 if (e1.address() < e2.address()) return true;
00166 if (e1.address() != e2.address()) return false;
00167 return e1.channel() < e2.channel();
00168 }
00169
00170 private:
00171
00172
00173 struct sockaddr_rc data_;
00174 };
00175
00176 template<typename X>
00177 bdaddr_t bluetooth_endpoint<X>::addr_any = (const bdaddr_t) { {0u, 0u, 0u, 0u, 0u, 0u} };
00178
00179 }}}
00180
00181 #endif