source: source/ariba/utility/transport/rfcomm/rfcomm_transport.hpp@ 10789

Last change on this file since 10789 was 10653, checked in by Michael Tänzer, 12 years ago

Merge the ASIO branch back into trunk

File size: 4.9 KB
Line 
1#include "ariba/config.h"
2
3#ifdef HAVE_LIBBLUETOOTH
4
5#ifndef RFCOMM_TRANSPORT_HPP_
6#define RFCOMM_TRANSPORT_HPP_
7
8#include "ariba/utility/transport/transport.hpp"
9#include "ariba/utility/transport/asio/unique_io_service.h"
10#include "ariba/utility/transport/transport_connection.hpp"
11#include "ariba/utility/addressing/rfcomm_endpoint.hpp"
12#include <boost/asio.hpp>
13#include <boost/shared_ptr.hpp>
14#include <boost/enable_shared_from_this.hpp>
15#include <queue>
16#include "ariba/utility/transport/messages/buffers.hpp"
17#include "ariba/utility/logging/Logging.h"
18#include "bluetooth_rfcomm.hpp"
19
20namespace ariba {
21namespace transport {
22
23using namespace std;
24using ariba::transport::detail::unique_io_service;
25using ariba::addressing::rfcomm_endpoint;
26using boost::system::error_code;
27using reboost::shared_buffer_t;
28using reboost::message_t;
29
30
31class rfcomm_transport :
32 public transport_protocol,
33 public boost::enable_shared_from_this<rfcomm_transport>
34{
35public:
36 typedef boost::shared_ptr<rfcomm_transport> sptr;
37
38private:
39 typedef rfcomm_transport self;
40 typedef boost::asio::bluetooth::rfcomm rfcomm;
41 use_logging_h(rfcomm_transport)
42
43 class rfcomm_connection :
44 public transport_connection,
45 public boost::enable_shared_from_this<rfcomm_connection>
46 {
47 public:
48 typedef reboost::message_t Packet;
49 typedef std::queue<Packet> OutQueue;
50
51 struct header_t
52 {
53 uint32_t length;
54 uint16_t prot; // XXX protlib
55 } __attribute__((packed));
56
57 rfcomm_connection(boost::asio::io_service& io_service,
58 rfcomm_transport::sptr parent);
59
60 /// Inherited from transport_connection
61 virtual void send(reboost::message_t message, uint8_t priority = 0);
62 virtual address_vf getLocalEndpoint();
63 virtual address_vf getRemoteEndpoint();
64 virtual void terminate();
65
66 void listen();
67
68 void async_connect_handler(const error_code& error);
69
70 void async_read_header_handler(const error_code& error, size_t bytes_transferred);
71 void async_read_data_handler(const error_code& error, size_t bytes_transferred);
72
73 /*
74 * is called from asio when write operation "returns",
75 * calls private function `send_next_package()`
76 */
77 void async_write_handler(
78 reboost::shared_buffer_t packet,
79 const error_code& error,
80 size_t bytes_transferred);
81
82
83 void enqueue_for_sending(Packet packet, uint8_t priority);
84
85 private:
86 /*
87 * is called from `send` or `async_write_handler` to begin/keep sending
88 * sends the next message with the highest priority in this connection
89 */
90 void send_next_package();
91
92
93 public:
94 rfcomm::socket sock;
95 bool valid;
96 rfcomm_transport::sptr parent;
97
98 rfcomm::endpoint partner;
99 rfcomm_endpoint remote;
100 rfcomm_endpoint local;
101
102 vector<OutQueue> out_queues; // to be locked with out_queues_lock
103 boost::mutex out_queues_lock;
104
105 bool sending; // to be locked with out_queues_lock
106
107 header_t header;
108 shared_buffer_t buffy;
109 };
110 typedef boost::shared_ptr<rfcomm_connection> ConnPtr;
111 typedef std::map<rfcomm::endpoint, ConnPtr> ConnectionMap;
112
113public:
114 /* constructor */
115 rfcomm_transport( const rfcomm::endpoint& endp );
116 virtual ~rfcomm_transport();
117
118 virtual void start();
119 virtual void stop();
120
121 /**
122 * enqueues message for sending
123 * create new connection if necessary
124 * starts sending mechanism (if not already running)
125 */
126 void send(
127 const rfcomm::endpoint&,
128 reboost::message_t message,
129 uint8_t priority = 0 );
130
131 /**
132 * Converts address_v to rfcomm::endpoint and calls the real send() function
133 */
134 virtual void send(
135 const address_v* remote,
136 reboost::message_t message,
137 uint8_t priority = 0 );
138
139 /**
140 * calls send for each destination endpoint in `endpoint_set& endpoints`
141 */
142 virtual void send(
143 const endpoint_set& endpoints,
144 reboost::message_t message,
145 uint8_t priority = 0 );
146
147 virtual void terminate( const address_v* remote );
148 virtual void terminate( const rfcomm::endpoint& remote );
149 virtual void register_listener( transport_listener* listener );
150
151
152private:
153 void accept();
154 void async_accept_handler(ConnPtr conn, const error_code& error);
155 rfcomm::endpoint convert_address(const address_v* endpoint);
156 rfcomm_endpoint convert_address(const rfcomm::endpoint& endpoint);
157
158private:
159 transport_listener* listener;
160 unique_io_service u_io_service;
161 rfcomm::acceptor acceptor;
162
163 ConnectionMap connections;
164 boost::mutex connections_lock;
165};
166
167}} // namespace ariba::transport
168
169#endif /* RFCOMM_TRANSPORT_HPP_ */
170#endif /* HAVE_LIBBLUETOOTH */
Note: See TracBrowser for help on using the repository browser.