An Overlay-based
Virtual Network Substrate
SpoVNet

source: source/ariba/utility/transport/asio/bluetooth_endpoint.hpp @ 6185

Last change on this file since 6185 was 6185, checked in by huebsch, 14 years ago

fix in ariba

File size: 5.1 KB
Line 
1#ifndef BOOST_ASIO_BLUETOOTH_BLUETOOTH_ENDPOINT_HPP__
2#define BOOST_ASIO_BLUETOOTH_BLUETOOTH_ENDPOINT_HPP__
3
4#include <bluetooth/bluetooth.h>
5#include <bluetooth/rfcomm.h>
6
7#include <boost/asio/basic_stream_socket.hpp>
8
9namespace boost {
10namespace asio {
11namespace bluetooth {
12
13/**
14 * Describes an endpoint for a RFCOMM Bluetooth socket.
15 *
16 * @author Martin Florian <mflorian@lafka.net>
17 */
18template<typename BluetoothProtocol>
19class bluetooth_endpoint {
20private:
21        static bdaddr_t addr_any;
22
23public:
24        /// The protocol type associated with the endpoint.
25        typedef BluetoothProtocol protocol_type;
26
27        /// The type of the endpoint structure. This type is dependent on the
28        /// underlying implementation of the socket layer.
29        typedef boost::asio::detail::socket_addr_type data_type; // <-- Do I need this?
30        //typedef sockaddr_rc data_type;
31
32        /// Default constructor.
33        bluetooth_endpoint() :
34                data_() {
35                data_.rc_family = AF_BLUETOOTH;
36                data_.rc_bdaddr = addr_any;
37                data_.rc_channel = (uint8_t) 0;
38        }
39
40        bluetooth_endpoint(const BluetoothProtocol& protocol,
41                        unsigned short channel) :
42                data_() {
43                data_.rc_family = AF_BLUETOOTH;
44                data_.rc_bdaddr = addr_any;
45                data_.rc_channel = channel;
46        }
47
48        /// Construct an endpoint using a port number, specified in the host's byte
49        /// order. The IP address will be the any address (i.e. INADDR_ANY or
50        /// in6addr_any). This constructor would typically be used for accepting new
51        /// connections.
52        bluetooth_endpoint(unsigned short channel) :
53                data_() {
54                data_.rc_family = AF_BLUETOOTH;
55                data_.rc_bdaddr = *BDADDR_ANY;
56                data_.rc_channel = channel;
57        }
58
59        /// Construct an endpoint using a port number and an BT address.
60        /// The address is in human readable form as a string.
61        bluetooth_endpoint(const char *addr, unsigned short channel) :
62                data_() {
63                data_.rc_family = AF_BLUETOOTH;
64                data_.rc_channel = channel;
65                str2ba(addr, &data_.rc_bdaddr);
66        }
67
68        /// Construct an endpoint using a port number and an BT address.
69        /// The address is given in the bluetooth-internal format.
70        bluetooth_endpoint(bdaddr_t addr, unsigned short channel) :
71                data_() {
72                data_.rc_family = AF_BLUETOOTH;
73                data_.rc_channel = channel;
74                data_.rc_bdaddr = addr;
75        }
76
77        /// Copy constructor.
78        bluetooth_endpoint(const bluetooth_endpoint& other) :
79                data_(other.data_) {
80        }
81
82        /// Assign from another endpoint.
83        bluetooth_endpoint& operator=(const bluetooth_endpoint& other) {
84                data_ = other.data_;
85                return *this;
86        }
87
88        /// The protocol associated with the endpoint.
89        protocol_type protocol() const {
90                return protocol_type::get();
91        }
92
93        /// Get the underlying endpoint in the native type.
94        /// TODO: make this nice and generic -> union like in tcp
95        data_type* data() {
96                return (boost::asio::detail::socket_addr_type*) &data_;
97        }
98
99        /// Get the underlying endpoint in the native type.
100        const data_type* data() const {
101                return (boost::asio::detail::socket_addr_type*) &data_;
102        }
103
104        /// Get the underlying size of the endpoint in the native type.
105        std::size_t size() const {
106                return sizeof(data_type);
107        }
108
109        /// Set the underlying size of the endpoint in the native type.
110        void resize(std::size_t size) {
111                if (size > sizeof(data_type)) {
112                        boost::system::system_error e(boost::asio::error::invalid_argument);
113                        boost::throw_exception(e);
114                }
115        }
116
117        /// Get the capacity of the endpoint in the native type.
118        std::size_t capacity() const {
119                return sizeof(data_type);
120        }
121
122        /// Get the channel associated with the endpoint. The port number is always in
123        /// the host's byte order.
124        unsigned short channel() const {
125                return data_.rc_channel;
126        }
127
128        /// Set the channel associated with the endpoint. The port number is always in
129        /// the host's byte order.
130        void channel(unsigned short channel_num) {
131                data_.rc_channel = channel_num;
132        }
133
134        /// Get the Bluetooth address associated with the endpoint.
135        bdaddr_t address() const {
136                return data_.rc_bdaddr;
137        }
138
139        /// Set the Bluetooth address associated with the endpoint.
140        void address(const boost::asio::ip::address& addr) {
141                bluetooth_endpoint<BluetoothProtocol> tmp_endpoint(addr, channel());
142                data_ = tmp_endpoint.data_;
143        }
144
145        /// Get the Bluetooth address in human readable form and write it to buf.
146        void address_hr(char &buf) {
147                ba2str(&data_.rc_bdaddr, buf);
148        }
149
150        /// Compare two endpoints for equality.
151        friend bool operator==(const bluetooth_endpoint& e1,
152                        const bluetooth_endpoint& e2) {
153                return e1.address() == e2.address() && e1.channel() == e2.channel();
154        }
155
156        /// Compare two endpoints for inequality.
157        friend bool operator!=(const bluetooth_endpoint& e1,
158                        const bluetooth_endpoint& e2) {
159                return e1.address() != e2.address() || e1.channel() != e2.channel();
160        }
161
162        /// Compare endpoints for ordering.
163        friend bool operator<(const bluetooth_endpoint<BluetoothProtocol>& e1,
164                        const bluetooth_endpoint<BluetoothProtocol>& e2) {
165                if (e1.address() < e2.address()) return true;
166                if (e1.address() != e2.address()) return false;
167                return e1.channel() < e2.channel();
168        }
169
170private:
171        // The underlying rfcomm socket address structure thingy.
172        //struct data_type data_;
173        struct sockaddr_rc data_;
174};
175
176template<typename X>
177bdaddr_t bluetooth_endpoint<X>::addr_any = { {0u, 0u, 0u, 0u, 0u, 0u} };
178
179}}} // namespace boost::asio::bluetooth
180
181#endif
Note: See TracBrowser for help on using the repository browser.