source: source/ariba/utility/transport/rfcomm/rfcomm.cpp@ 5406

Last change on this file since 5406 was 5406, checked in by mies, 15 years ago
File size: 9.3 KB
Line 
1#include "rfcomm.hpp"
2
3#include "../asio/asio_io_service.h"
4#include "../asio/rfcomm.hpp"
5#include "../asio/bluetooth_endpoint.hpp"
6
7#include <boost/asio.hpp>
8#include <boost/thread.hpp>
9#include <boost/array.hpp>
10#include <memory.h>
11#include <deque>
12
13namespace ariba {
14namespace transport {
15
16use_logging_cpp(rfcomm)
17
18using namespace boost::asio;
19using namespace detail;
20using namespace std;
21
22using boost::system::error_code;
23
24class link_data {
25public:
26 uint8_t size_[4];
27 size_t size;
28 uint8_t* buffer;
29};
30
31class link_info {
32public:
33 link_info(io_service& io ) :
34 up(false), local(), remote(), socket(io), connect_retries(0),
35 size(0), buffer(NULL), sending(false) {
36 }
37
38 // state
39 bool up;
40 rfcomm_endpoint local, remote;
41 bluetooth::rfcomm::socket socket;
42 int connect_retries;
43
44 // read buffer
45 uint8_t size_[4];
46 size_t size;
47 uint8_t* buffer;
48
49 // send buffer
50 bool sending;
51 boost::mutex mutex;
52 std::deque<link_data> send_buffer;
53};
54
55void rfcomm::shutdown(link_info* info) {
56 if (info != NULL && info->up) {
57 info->up = false;
58 info->socket.shutdown( bluetooth::rfcomm::socket::shutdown_both );
59 }
60}
61
62
63inline bluetooth::rfcomm::endpoint convert( const rfcomm_endpoint& endpoint ) {
64 return bluetooth::rfcomm::endpoint(
65 endpoint.mac().bluetooth(), endpoint.channel().value()
66 );
67}
68
69inline rfcomm_endpoint convert( const bluetooth::rfcomm::endpoint& endpoint ) {
70 mac_address mac;
71 mac.bluetooth( endpoint.address() );
72 rfcomm_channel_address channel;
73 channel.value( endpoint.channel() );
74 return rfcomm_endpoint( mac, channel );
75}
76
77
78rfcomm::rfcomm(uint16_t channel) :
79 channel(channel), io(asio_io_service::alloc()) {
80 accept_retries = 0;
81}
82
83rfcomm::~rfcomm() {
84 asio_io_service::free();
85}
86
87void rfcomm::start() {
88
89 // start io service
90 asio_io_service::start();
91
92 // create acceptor
93 logging_info( "Binding to channel " << channel );
94 acceptor = new bluetooth::rfcomm::acceptor(io,
95 bluetooth::rfcomm::endpoint(bluetooth::rfcomm::get(), channel )
96 );
97
98 send_data = new link_data();
99
100 // start accepting
101 start_accept();
102}
103
104void rfcomm::stop() {
105 logging_info( "Stopping asio rfcomm" );
106
107}
108
109void rfcomm::send(const address_v* remote, const uint8_t* data, size_t size) {
110
111 // get end-point
112 rfcomm_endpoint endpoint = *remote;
113 endpoint = convert(convert(endpoint));
114
115 // try to find established connector
116 logging_debug("Trying to find a already existing link.");
117 link_info* info = NULL;
118 for (size_t i = 0; i < links.size(); i++)
119 if (links[i]->remote.mac() == endpoint.mac()) {
120 logging_debug("Using already established link");
121 info = links[i];
122 break;
123 }
124
125 // not found, or not up? ->try to (re-)connect
126 if (info==NULL || !info->up) {
127 logging_debug( "Connecting to " << endpoint.to_string() );
128 if (info != NULL && !info->up) {
129 logging_debug("Old link is down. Trying to re-establish link.");
130 } else {
131 info = new link_info(io);
132 }
133 info->remote = endpoint;
134 info->socket.async_connect( convert(endpoint), boost::bind(
135 &rfcomm::handle_connect, this,
136 boost::asio::placeholders::error, info
137 ));
138 asio_io_service::start();
139 }
140
141 // copy message
142 link_data ldata;
143 ldata.size = size;
144 ldata.size_[0] = (size >> 24) & 0xFF;
145 ldata.size_[1] = (size >> 16) & 0xFF;
146 ldata.size_[2] = (size >> 8) & 0xFF;
147 ldata.size_[3] = (size >> 0) & 0xFF;
148 ldata.buffer = new uint8_t[size];
149 memcpy(ldata.buffer, data, size);
150
151 // enqueue message
152 info->mutex.lock();
153 info->send_buffer.push_back(ldata);
154 info->mutex.unlock();
155
156 // start writing
157 start_write(info);
158}
159
160void rfcomm::send(const endpoint_set& endpoints, const uint8_t* data, size_t size) {
161 // send a message to each combination of mac-address and channel
162 BOOST_FOREACH( const mac_address mac, endpoints.bluetooth ) {
163 BOOST_FOREACH( const rfcomm_channel_address channel, endpoints.rfcomm ) {
164 rfcomm_endpoint endpoint(mac, channel);
165 address_vf vf = endpoint;
166 send(vf,data,size);
167 }
168 }
169}
170
171void rfcomm::terminate(const address_v* local, const address_v* remote) {
172 // get end-point
173 rfcomm_endpoint endpoint = *remote;
174
175 for (size_t i = 0; i < links.size(); i++)
176 if (links[i]->remote.mac() == endpoint.mac()) {
177
178 // close socket
179 links[i]->socket.cancel();
180 links[i]->socket.close();
181 links[i]->up = false;
182 break;
183 }
184}
185
186void rfcomm::register_listener(transport_listener* listener) {
187 this->listener = listener;
188}
189
190void rfcomm::start_accept() {
191
192 logging_info( "Waiting for connections ..." );
193
194 // start accepting a connection
195 link_info* info = new link_info(io);
196 acceptor->async_accept(info->socket, boost::bind(
197 // bind parameters
198 &rfcomm::handle_accept, this,
199
200 // handler parameters
201 boost::asio::placeholders::error, info
202 ));
203 asio_io_service::start();
204}
205
206void rfcomm::handle_accept(const error_code& error, link_info* info) {
207 if (error) {
208 logging_error( "Error waiting for new connections: " << error
209 << ", trying to recover (attempt " << accept_retries << ")");
210 delete info;
211
212 // restart accepting
213 if (accept_retries<3) {
214 accept_retries++;
215 start_accept();
216 }
217 return;
218 }
219
220 // convert endpoints
221 info->up = true;
222 info->local = convert( info->socket.local_endpoint() );
223 info->remote = convert( info->socket.remote_endpoint() );
224
225 logging_debug("Accepting incoming connection from "
226 << info->remote.to_string() );
227
228 // add to list
229 links_mutex.lock();
230 links.push_back(info);
231 links_mutex.unlock();
232
233 // start i/o
234 start_read(info);
235 start_write(info);
236
237 // restart accept
238 start_accept();
239}
240
241void rfcomm::handle_connect( const error_code& error, link_info* info ) {
242 if (error) {
243 logging_error( "Can not connect. Retrying ... "
244 "(attempt " << info->connect_retries << ")" );
245
246 // do we retry this connection? yes->
247 if (info->connect_retries<3) {
248 // increase counter
249 info->connect_retries++;
250
251 // retry connection attempt
252 info->socket.async_connect( convert(info->remote), boost::bind(
253 &rfcomm::handle_connect, this,
254 boost::asio::placeholders::error, info
255 ));
256
257 } else { // no-> delete link and stop
258 return;
259 }
260 }
261
262 // convert endpoints
263 info->up = true;
264 info->local = convert( info->socket.local_endpoint() );
265 info->remote = convert( info->socket.remote_endpoint() );
266
267 logging_debug( "Connected to " << info->remote.to_string() );
268
269 // add to list
270 links_mutex.lock();
271 links.push_back(info);
272 links_mutex.unlock();
273
274 // start i/o
275 start_read(info);
276 start_write(info);
277}
278
279void rfcomm::start_read(link_info* info) {
280 // start read
281 boost::asio::async_read(info->socket,
282
283 // read size of packet
284 boost::asio::buffer(info->size_, 4),
285
286 // bind handler
287 boost::bind(
288
289 // bind parameters
290 &rfcomm::handle_read_header, this,
291
292 // handler parameters
293 placeholders::error, placeholders::bytes_transferred, info
294 )
295 );
296}
297
298void rfcomm::handle_read_header(const error_code& error, size_t bytes,
299 link_info* info) {
300
301 // handle error
302 if (error) {
303 logging_error("Failed to receive message payload.");
304 shutdown(info);
305 return;
306 }
307
308 // ignore errors and wait for all data to be received
309 if (bytes != 4) return;
310
311 // get size
312 info->size = (info->size_[0]<<24) + (info->size_[1] << 16) +
313 (info->size_[2]<< 8) + (info->size_[3] << 0);
314
315 logging_debug( "Message header received -> receive message of size " << info->size );
316
317 // allocate buffer
318 info->buffer = new uint8_t[info->size];
319
320 // start read
321 boost::asio::async_read(info->socket,
322 // read size of packet
323 boost::asio::buffer(info->buffer, info->size),
324 // bind handler
325 boost::bind(
326 // bind parameters
327 &rfcomm::handle_read_data, this,
328 // handler parameters
329 placeholders::error, placeholders::bytes_transferred, info
330 )
331 );
332}
333
334void rfcomm::handle_read_data(const error_code& error, size_t bytes,
335 link_info* info) {
336
337 // check error
338 if (error) {
339 logging_error("Failed to receive message payload.");
340 shutdown(info);
341 return;
342 }
343
344 // wait for all data to be received
345 if (bytes != info->size) return;
346
347 logging_debug( "Received message of size " << info->size );
348
349 // deliver data
350 listener->receive_message(this, info->local, info->remote, info->buffer, info->size );
351
352 // free buffers and reset size buffer
353 delete [] info->buffer;
354 for (size_t i=0; i<4; i++) info->size_[i] = 0;
355
356 start_read(info);
357}
358
359void rfcomm::start_write( link_info* info ) {
360 boost::mutex::scoped_lock(info->mutex);
361
362 // do not start writing if sending is in progress
363 if (info->sending || !info->up || info->send_buffer.size()==0) return;
364
365 cout << "Sending messages..." << endl;
366
367 // safely remove data from deque
368 *send_data = info->send_buffer.front();
369 info->send_buffer.pop_front();
370
371 boost::array<boost::asio::mutable_buffer, 2> buffer;
372 buffer[0] = boost::asio::buffer(send_data->size_,4);
373 buffer[1] = boost::asio::buffer(send_data->buffer,send_data->size);
374
375 // start writing
376 boost::asio::async_write(info->socket,
377 // read size of packet
378 buffer,
379 // bind handler
380 boost::bind(
381 // bind parameters
382 &rfcomm::handle_write_data, this,
383 // handler parameters
384 placeholders::error, placeholders::bytes_transferred,
385 info, send_data->size, send_data->buffer
386 )
387 );
388}
389
390void rfcomm::handle_write_data(const error_code& error, size_t bytes,
391 link_info* info, size_t size, uint8_t* buffer ) {
392
393 // ignore errors and wait for all data to be sent
394 if (error || bytes != (size+4) ) {
395 if (error) {
396 logging_error( "Message sent error" );
397
398 // close socket
399 info->socket.close();
400 info->up = false;
401 }
402 return;
403 }
404 logging_debug( "Message sent" );
405
406 // free buffer
407 delete [] buffer;
408
409 // restart-write
410 start_write(info);
411}
412
413}} // namespace ariba::transport
Note: See TracBrowser for help on using the repository browser.