00001 #include "ariba/config.h"
00002
00003 #ifdef HAVE_LIBBLUETOOTH
00004
00005 #ifndef BOOST_ASIO_BLUETOOTH_BLUETOOTH_ENDPOINT_HPP__
00006 #define BOOST_ASIO_BLUETOOTH_BLUETOOTH_ENDPOINT_HPP__
00007
00008 #include <bluetooth/bluetooth.h>
00009 #include <bluetooth/rfcomm.h>
00010
00011 #include <boost/asio/basic_stream_socket.hpp>
00012
00013 namespace boost {
00014 namespace asio {
00015 namespace bluetooth {
00016
00022 template<typename BluetoothProtocol>
00023 class bluetooth_endpoint {
00024 private:
00025 static bdaddr_t addr_any;
00026
00027 public:
00029 typedef BluetoothProtocol protocol_type;
00030
00033 typedef boost::asio::detail::socket_addr_type data_type;
00034
00035
00037 bluetooth_endpoint() :
00038 data_() {
00039 data_.rc_family = AF_BLUETOOTH;
00040 data_.rc_bdaddr = addr_any;
00041 data_.rc_channel = (uint8_t) 0;
00042 }
00043
00044 bluetooth_endpoint(const BluetoothProtocol& protocol,
00045 unsigned short channel) :
00046 data_() {
00047 data_.rc_family = AF_BLUETOOTH;
00048 data_.rc_bdaddr = addr_any;
00049 data_.rc_channel = channel;
00050 }
00051
00056 bluetooth_endpoint(unsigned short channel) :
00057 data_() {
00058 data_.rc_family = AF_BLUETOOTH;
00059 data_.rc_bdaddr = *BDADDR_ANY;
00060 data_.rc_channel = channel;
00061 }
00062
00065 bluetooth_endpoint(const char *addr, unsigned short channel) :
00066 data_() {
00067 data_.rc_family = AF_BLUETOOTH;
00068 data_.rc_channel = channel;
00069 str2ba(addr, &data_.rc_bdaddr);
00070 }
00071
00074 bluetooth_endpoint(bdaddr_t addr, unsigned short channel) :
00075 data_() {
00076 data_.rc_family = AF_BLUETOOTH;
00077 data_.rc_channel = channel;
00078 data_.rc_bdaddr = addr;
00079 }
00080
00082 bluetooth_endpoint(const bluetooth_endpoint& other) :
00083 data_(other.data_) {
00084 }
00085
00087 bluetooth_endpoint& operator=(const bluetooth_endpoint& other) {
00088 data_ = other.data_;
00089 return *this;
00090 }
00091
00093 protocol_type protocol() const {
00094 return protocol_type::get();
00095 }
00096
00099 data_type* data() {
00100 return (boost::asio::detail::socket_addr_type*) &data_;
00101 }
00102
00104 const data_type* data() const {
00105 return (boost::asio::detail::socket_addr_type*) &data_;
00106 }
00107
00109 std::size_t size() const {
00110 return sizeof(data_type);
00111 }
00112
00114 void resize(std::size_t size) {
00115 if (size > sizeof(data_type)) {
00116 boost::system::system_error e(boost::asio::error::invalid_argument);
00117 boost::throw_exception(e);
00118 }
00119 }
00120
00122 std::size_t capacity() const {
00123 return sizeof(data_type);
00124 }
00125
00128 unsigned short channel() const {
00129 return data_.rc_channel;
00130 }
00131
00134 void channel(unsigned short channel_num) {
00135 data_.rc_channel = channel_num;
00136 }
00137
00139 bdaddr_t address() const {
00140 return data_.rc_bdaddr;
00141 }
00142
00144 void address(const boost::asio::ip::address& addr) {
00145 bluetooth_endpoint<BluetoothProtocol> tmp_endpoint(addr, channel());
00146 data_ = tmp_endpoint.data_;
00147 }
00148
00150 void address_hr(char &buf) {
00151 ba2str(&data_.rc_bdaddr, buf);
00152 }
00153
00155 friend bool operator==(const bluetooth_endpoint& e1,
00156 const bluetooth_endpoint& e2) {
00157 return e1.address() == e2.address() && e1.channel() == e2.channel();
00158 }
00159
00161 friend bool operator!=(const bluetooth_endpoint& e1,
00162 const bluetooth_endpoint& e2) {
00163 return e1.address() != e2.address() || e1.channel() != e2.channel();
00164 }
00165
00167 friend bool operator<(const bluetooth_endpoint<BluetoothProtocol>& e1,
00168 const bluetooth_endpoint<BluetoothProtocol>& e2) {
00169 if (e1.address() < e2.address()) return true;
00170 if (e1.address() != e2.address()) return false;
00171 return e1.channel() < e2.channel();
00172 }
00173
00174 private:
00175
00176
00177 struct sockaddr_rc data_;
00178 };
00179
00180 template<typename X>
00181 bdaddr_t bluetooth_endpoint<X>::addr_any = { {0u, 0u, 0u, 0u, 0u, 0u} };
00182
00183 }}}
00184
00185 #endif
00186 #endif