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