[5284] | 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>
|
---|
[5418] | 12 | #include <cerrno>
|
---|
[5284] | 13 |
|
---|
| 14 | namespace ariba {
|
---|
| 15 | namespace transport {
|
---|
| 16 |
|
---|
[5406] | 17 | use_logging_cpp(rfcomm)
|
---|
| 18 |
|
---|
[5284] | 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 ) :
|
---|
[5406] | 35 | up(false), local(), remote(), socket(io), connect_retries(0),
|
---|
| 36 | size(0), buffer(NULL), sending(false) {
|
---|
[5284] | 37 | }
|
---|
| 38 |
|
---|
| 39 | // state
|
---|
| 40 | bool up;
|
---|
| 41 | rfcomm_endpoint local, remote;
|
---|
| 42 | bluetooth::rfcomm::socket socket;
|
---|
[5406] | 43 | int connect_retries;
|
---|
[5284] | 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 |
|
---|
[5406] | 56 | void 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 | }
|
---|
[5289] | 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
[5284] | 64 | inline bluetooth::rfcomm::endpoint convert( const rfcomm_endpoint& endpoint ) {
|
---|
| 65 | return bluetooth::rfcomm::endpoint(
|
---|
| 66 | endpoint.mac().bluetooth(), endpoint.channel().value()
|
---|
| 67 | );
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | inline 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 |
|
---|
| 79 | rfcomm::rfcomm(uint16_t channel) :
|
---|
| 80 | channel(channel), io(asio_io_service::alloc()) {
|
---|
[5406] | 81 | accept_retries = 0;
|
---|
[5284] | 82 | }
|
---|
| 83 |
|
---|
| 84 | rfcomm::~rfcomm() {
|
---|
| 85 | asio_io_service::free();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | void rfcomm::start() {
|
---|
| 89 |
|
---|
| 90 | // start io service
|
---|
| 91 | asio_io_service::start();
|
---|
| 92 |
|
---|
| 93 | // create acceptor
|
---|
[5406] | 94 | logging_info( "Binding to channel " << channel );
|
---|
[5284] | 95 | acceptor = new bluetooth::rfcomm::acceptor(io,
|
---|
| 96 | bluetooth::rfcomm::endpoint(bluetooth::rfcomm::get(), channel )
|
---|
| 97 | );
|
---|
| 98 |
|
---|
[5406] | 99 | send_data = new link_data();
|
---|
| 100 |
|
---|
[5284] | 101 | // start accepting
|
---|
| 102 | start_accept();
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | void rfcomm::stop() {
|
---|
[5406] | 106 | logging_info( "Stopping asio rfcomm" );
|
---|
[5284] | 107 | }
|
---|
| 108 |
|
---|
| 109 | void rfcomm::send(const address_v* remote, const uint8_t* data, size_t size) {
|
---|
[5406] | 110 |
|
---|
[5284] | 111 | // get end-point
|
---|
| 112 | rfcomm_endpoint endpoint = *remote;
|
---|
| 113 | endpoint = convert(convert(endpoint));
|
---|
| 114 |
|
---|
| 115 | // try to find established connector
|
---|
[5406] | 116 | logging_debug("Trying to find a already existing link.");
|
---|
[5284] | 117 | link_info* info = NULL;
|
---|
| 118 | for (size_t i = 0; i < links.size(); i++)
|
---|
| 119 | if (links[i]->remote.mac() == endpoint.mac()) {
|
---|
[5406] | 120 | logging_debug("Using already established link");
|
---|
[5284] | 121 | info = links[i];
|
---|
| 122 | break;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[5406] | 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;
|
---|
[5284] | 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 |
|
---|
| 160 | void 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 |
|
---|
| 171 | void rfcomm::terminate(const address_v* local, const address_v* remote) {
|
---|
[5406] | 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
|
---|
[5410] | 178 | shutdown(links[i]);
|
---|
[5406] | 179 | break;
|
---|
| 180 | }
|
---|
[5284] | 181 | }
|
---|
| 182 |
|
---|
| 183 | void rfcomm::register_listener(transport_listener* listener) {
|
---|
| 184 | this->listener = listener;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | void rfcomm::start_accept() {
|
---|
| 188 |
|
---|
[5406] | 189 | logging_info( "Waiting for connections ..." );
|
---|
[5284] | 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 |
|
---|
| 203 | void rfcomm::handle_accept(const error_code& error, link_info* info) {
|
---|
| 204 | if (error) {
|
---|
[5418] | 205 | logging_error( "Error waiting for new connections. Error code: "<< strerror(errno)
|
---|
[5406] | 206 | << ", trying to recover (attempt " << accept_retries << ")");
|
---|
| 207 |
|
---|
| 208 | // restart accepting
|
---|
| 209 | if (accept_retries<3) {
|
---|
| 210 | accept_retries++;
|
---|
| 211 | start_accept();
|
---|
[5410] | 212 | } else
|
---|
| 213 | delete info;
|
---|
| 214 |
|
---|
[5284] | 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 |
|
---|
[5406] | 223 | logging_debug("Accepting incoming connection from "
|
---|
| 224 | << info->remote.to_string() );
|
---|
[5284] | 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 |
|
---|
| 239 | void rfcomm::handle_connect( const error_code& error, link_info* info ) {
|
---|
| 240 | if (error) {
|
---|
[5418] | 241 | logging_error( "Can not connect. Error code: "<< strerror(errno) << " Retrying ... "
|
---|
[5406] | 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 | }
|
---|
[5284] | 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 |
|
---|
[5406] | 265 | logging_debug( "Connected to " << info->remote.to_string() );
|
---|
[5284] | 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 |
|
---|
| 277 | void rfcomm::start_read(link_info* info) {
|
---|
| 278 | // start read
|
---|
| 279 | boost::asio::async_read(info->socket,
|
---|
[5406] | 280 |
|
---|
[5284] | 281 | // read size of packet
|
---|
| 282 | boost::asio::buffer(info->size_, 4),
|
---|
[5406] | 283 |
|
---|
[5284] | 284 | // bind handler
|
---|
| 285 | boost::bind(
|
---|
[5406] | 286 |
|
---|
[5284] | 287 | // bind parameters
|
---|
| 288 | &rfcomm::handle_read_header, this,
|
---|
[5406] | 289 |
|
---|
[5284] | 290 | // handler parameters
|
---|
| 291 | placeholders::error, placeholders::bytes_transferred, info
|
---|
| 292 | )
|
---|
| 293 | );
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | void rfcomm::handle_read_header(const error_code& error, size_t bytes,
|
---|
| 297 | link_info* info) {
|
---|
| 298 |
|
---|
[5406] | 299 | // handle error
|
---|
| 300 | if (error) {
|
---|
[5418] | 301 | logging_error("Failed to receive message payload. Error code: "<< strerror(errno));
|
---|
[5406] | 302 | shutdown(info);
|
---|
| 303 | return;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[5284] | 306 | // ignore errors and wait for all data to be received
|
---|
[5406] | 307 | if (bytes != 4) return;
|
---|
[5284] | 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 |
|
---|
[5406] | 313 | logging_debug( "Message header received -> receive message of size " << info->size );
|
---|
[5284] | 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 |
|
---|
| 332 | void rfcomm::handle_read_data(const error_code& error, size_t bytes,
|
---|
| 333 | link_info* info) {
|
---|
| 334 |
|
---|
[5406] | 335 | // check error
|
---|
| 336 | if (error) {
|
---|
[5418] | 337 | logging_error("Failed to receive message payload. Error code: "<< strerror(errno));
|
---|
[5406] | 338 | shutdown(info);
|
---|
[5289] | 339 | return;
|
---|
| 340 | }
|
---|
[5284] | 341 |
|
---|
[5406] | 342 | // wait for all data to be received
|
---|
[5410] | 343 | if (bytes != info->size)
|
---|
| 344 | return;
|
---|
[5284] | 345 |
|
---|
[5406] | 346 | logging_debug( "Received message of size " << info->size );
|
---|
| 347 |
|
---|
[5284] | 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;
|
---|
[5286] | 354 |
|
---|
| 355 | start_read(info);
|
---|
[5284] | 356 | }
|
---|
| 357 |
|
---|
| 358 | void rfcomm::start_write( link_info* info ) {
|
---|
[5406] | 359 | boost::mutex::scoped_lock(info->mutex);
|
---|
| 360 |
|
---|
[5284] | 361 | // do not start writing if sending is in progress
|
---|
| 362 | if (info->sending || !info->up || info->send_buffer.size()==0) return;
|
---|
| 363 |
|
---|
[5410] | 364 | logging_debug("Sending messages ...");
|
---|
[5284] | 365 |
|
---|
| 366 | // safely remove data from deque
|
---|
[5406] | 367 | *send_data = info->send_buffer.front();
|
---|
[5284] | 368 | info->send_buffer.pop_front();
|
---|
| 369 |
|
---|
| 370 | boost::array<boost::asio::mutable_buffer, 2> buffer;
|
---|
[5406] | 371 | buffer[0] = boost::asio::buffer(send_data->size_,4);
|
---|
| 372 | buffer[1] = boost::asio::buffer(send_data->buffer,send_data->size);
|
---|
[5284] | 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,
|
---|
[5406] | 384 | info, send_data->size, send_data->buffer
|
---|
[5284] | 385 | )
|
---|
| 386 | );
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | void rfcomm::handle_write_data(const error_code& error, size_t bytes,
|
---|
| 390 | link_info* info, size_t size, uint8_t* buffer ) {
|
---|
| 391 |
|
---|
[5410] | 392 | // handle error
|
---|
| 393 | if (error) {
|
---|
[5418] | 394 | logging_error( "Message sent error. Error code: "<< strerror(errno) );
|
---|
[5410] | 395 | shutdown(info);
|
---|
| 396 | return;
|
---|
| 397 | }
|
---|
[5406] | 398 |
|
---|
[5410] | 399 | // wait for all data to be sent
|
---|
| 400 | if (bytes != (size+4) )
|
---|
[5284] | 401 | return;
|
---|
[5410] | 402 |
|
---|
[5406] | 403 | logging_debug( "Message sent" );
|
---|
[5284] | 404 |
|
---|
| 405 | // free buffer
|
---|
| 406 | delete [] buffer;
|
---|
| 407 |
|
---|
| 408 | // restart-write
|
---|
| 409 | start_write(info);
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | }} // namespace ariba::transport
|
---|