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

Last change on this file since 5422 was 5422, 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: "<< error.message()
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: "
242 << error.message() << " Retrying ... "
243 "(attempt " << info->connect_retries << ")" );
244
245 // do we retry this connection? yes->
246 if (info->connect_retries<3) {
247 // increase counter
248 info->connect_retries++;
249
250 // retry connection attempt
251 info->socket.async_connect( convert(info->remote), boost::bind(
252 &rfcomm::handle_connect, this,
253 boost::asio::placeholders::error, info
254 ));
255
256 } else { // no-> delete link and stop
257 return;
258 }
259 }
260
261 // convert endpoints
262 info->up = true;
263 info->local = convert( info->socket.local_endpoint() );
264 info->remote = convert( info->socket.remote_endpoint() );
265
266 logging_debug( "Connected to " << info->remote.to_string() );
267
268 // add to list
269 links_mutex.lock();
270 links.push_back(info);
271 links_mutex.unlock();
272
273 // start i/o
274 start_read(info);
275 start_write(info);
276}
277
278void rfcomm::start_read(link_info* info) {
279 // start read
280 boost::asio::async_read(info->socket,
281
282 // read size of packet
283 boost::asio::buffer(info->size_, 4),
284
285 // bind handler
286 boost::bind(
287
288 // bind parameters
289 &rfcomm::handle_read_header, this,
290
291 // handler parameters
292 placeholders::error, placeholders::bytes_transferred, info
293 )
294 );
295}
296
297void rfcomm::handle_read_header(const error_code& error, size_t bytes,
298 link_info* info) {
299
300 // handle error
301 if (error) {
302 logging_error("Failed to receive message payload. Error code: "
303 << error.message() );
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. Error code: "
340 << error.message() );
341 shutdown(info);
342 return;
343 }
344
345 // wait for all data to be received
346 if (bytes != info->size)
347 return;
348
349 logging_debug( "Received message of size " << info->size );
350
351 // deliver data
352 listener->receive_message(this, info->local, info->remote, info->buffer, info->size );
353
354 // free buffers and reset size buffer
355 delete [] info->buffer;
356 for (size_t i=0; i<4; i++) info->size_[i] = 0;
357
358 start_read(info);
359}
360
361void rfcomm::start_write( link_info* info ) {
362 boost::mutex::scoped_lock(info->mutex);
363
364 // do not start writing if sending is in progress
365 if (info->sending || !info->up || info->send_buffer.size()==0) return;
366
367 logging_debug("Sending messages ...");
368
369 // safely remove data from deque
370 *send_data = info->send_buffer.front();
371 info->send_buffer.pop_front();
372
373 boost::array<boost::asio::mutable_buffer, 2> buffer;
374 buffer[0] = boost::asio::buffer(send_data->size_,4);
375 buffer[1] = boost::asio::buffer(send_data->buffer,send_data->size);
376
377 // start writing
378 boost::asio::async_write(info->socket,
379 // read size of packet
380 buffer,
381 // bind handler
382 boost::bind(
383 // bind parameters
384 &rfcomm::handle_write_data, this,
385 // handler parameters
386 placeholders::error, placeholders::bytes_transferred,
387 info, send_data->size, send_data->buffer
388 )
389 );
390}
391
392void rfcomm::handle_write_data(const error_code& error, size_t bytes,
393 link_info* info, size_t size, uint8_t* buffer ) {
394
395 // handle error
396 if (error) {
397 logging_error( "Message sent error. Error code: "<< error.message() );
398 shutdown(info);
399 return;
400 }
401
402 // wait for all data to be sent
403 if (bytes != (size+4) )
404 return;
405
406 logging_debug( "Message sent" );
407
408 // free buffer
409 delete [] buffer;
410
411 // restart-write
412 start_write(info);
413}
414
415}} // namespace ariba::transport
Note: See TracBrowser for help on using the repository browser.