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

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