[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>
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | namespace ariba {
|
---|
| 15 | namespace transport {
|
---|
| 16 |
|
---|
| 17 | using namespace boost::asio;
|
---|
| 18 | using namespace detail;
|
---|
| 19 | using namespace std;
|
---|
| 20 |
|
---|
| 21 | using boost::system::error_code;
|
---|
| 22 |
|
---|
| 23 | class link_data {
|
---|
| 24 | public:
|
---|
| 25 | uint8_t size_[4];
|
---|
| 26 | size_t size;
|
---|
| 27 | uint8_t* buffer;
|
---|
| 28 | };
|
---|
| 29 |
|
---|
| 30 | class link_info {
|
---|
| 31 | public:
|
---|
| 32 | link_info(io_service& io ) :
|
---|
| 33 | up(false), local(), remote(), socket(io), size(0), buffer(NULL), sending(false) {
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | // state
|
---|
| 37 | bool up;
|
---|
| 38 | rfcomm_endpoint local, remote;
|
---|
| 39 | bluetooth::rfcomm::socket socket;
|
---|
| 40 |
|
---|
| 41 | // read buffer
|
---|
| 42 | uint8_t size_[4];
|
---|
| 43 | size_t size;
|
---|
| 44 | uint8_t* buffer;
|
---|
| 45 |
|
---|
| 46 | // send buffer
|
---|
| 47 | bool sending;
|
---|
| 48 | boost::mutex mutex;
|
---|
| 49 | std::deque<link_data> send_buffer;
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | inline bluetooth::rfcomm::endpoint convert( const rfcomm_endpoint& endpoint ) {
|
---|
| 53 | return bluetooth::rfcomm::endpoint(
|
---|
| 54 | endpoint.mac().bluetooth(), endpoint.channel().value()
|
---|
| 55 | );
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | inline rfcomm_endpoint convert( const bluetooth::rfcomm::endpoint& endpoint ) {
|
---|
| 59 | mac_address mac;
|
---|
| 60 | mac.bluetooth( endpoint.address() );
|
---|
| 61 | rfcomm_channel_address channel;
|
---|
| 62 | channel.value( endpoint.channel() );
|
---|
| 63 | return rfcomm_endpoint( mac, channel );
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | rfcomm::rfcomm(uint16_t channel) :
|
---|
| 68 | channel(channel), io(asio_io_service::alloc()) {
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | rfcomm::~rfcomm() {
|
---|
| 72 | asio_io_service::free();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | void rfcomm::start() {
|
---|
| 76 |
|
---|
| 77 | // start io service
|
---|
| 78 | asio_io_service::start();
|
---|
| 79 |
|
---|
| 80 | // create acceptor
|
---|
| 81 | cout << "Binding to channel " << channel << endl;
|
---|
| 82 | acceptor = new bluetooth::rfcomm::acceptor(io,
|
---|
| 83 | bluetooth::rfcomm::endpoint(bluetooth::rfcomm::get(), channel )
|
---|
| 84 | );
|
---|
| 85 |
|
---|
| 86 | // start accepting
|
---|
| 87 | start_accept();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void rfcomm::stop() {
|
---|
| 91 |
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | void rfcomm::send(const address_v* remote, const uint8_t* data, size_t size) {
|
---|
| 95 | // get end-point
|
---|
| 96 | rfcomm_endpoint endpoint = *remote;
|
---|
| 97 | endpoint = convert(convert(endpoint));
|
---|
| 98 |
|
---|
| 99 | // try to find established connector
|
---|
| 100 | link_info* info = NULL;
|
---|
| 101 | for (size_t i = 0; i < links.size(); i++)
|
---|
| 102 | if (links[i]->remote.mac() == endpoint.mac()) {
|
---|
| 103 | info = links[i];
|
---|
| 104 | break;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | // not found? ->try to connect
|
---|
| 108 | if (info==NULL) {
|
---|
| 109 | cout << "Connecting to " << endpoint.to_string() << endl;
|
---|
| 110 | info = new link_info(io);
|
---|
| 111 | info->socket.async_connect( convert(endpoint), boost::bind(
|
---|
| 112 | &rfcomm::handle_connect, this,
|
---|
| 113 | boost::asio::placeholders::error, info
|
---|
| 114 | ));
|
---|
| 115 | asio_io_service::start();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | // copy message
|
---|
| 119 | link_data ldata;
|
---|
| 120 | ldata.size = size;
|
---|
| 121 | ldata.size_[0] = (size >> 24) & 0xFF;
|
---|
| 122 | ldata.size_[1] = (size >> 16) & 0xFF;
|
---|
| 123 | ldata.size_[2] = (size >> 8) & 0xFF;
|
---|
| 124 | ldata.size_[3] = (size >> 0) & 0xFF;
|
---|
| 125 | ldata.buffer = new uint8_t[size];
|
---|
| 126 | memcpy(ldata.buffer, data, size);
|
---|
| 127 |
|
---|
| 128 | // enqueue message
|
---|
| 129 | info->mutex.lock();
|
---|
| 130 | info->send_buffer.push_back(ldata);
|
---|
| 131 | info->mutex.unlock();
|
---|
| 132 |
|
---|
| 133 | // start writing
|
---|
| 134 | start_write(info);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | void rfcomm::send(const endpoint_set& endpoints, const uint8_t* data, size_t size) {
|
---|
| 138 | // send a message to each combination of mac-address and channel
|
---|
| 139 | BOOST_FOREACH( const mac_address mac, endpoints.bluetooth ) {
|
---|
| 140 | BOOST_FOREACH( const rfcomm_channel_address channel, endpoints.rfcomm ) {
|
---|
| 141 | rfcomm_endpoint endpoint(mac, channel);
|
---|
| 142 | address_vf vf = endpoint;
|
---|
| 143 | send(vf,data,size);
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | void rfcomm::terminate(const address_v* local, const address_v* remote) {
|
---|
| 149 | // not supported right now!
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | void rfcomm::register_listener(transport_listener* listener) {
|
---|
| 153 | this->listener = listener;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | void rfcomm::start_accept() {
|
---|
| 157 |
|
---|
| 158 | cout << "Waiting for connections ..." << endl;
|
---|
| 159 |
|
---|
| 160 | // start accepting a connection
|
---|
| 161 | link_info* info = new link_info(io);
|
---|
| 162 | acceptor->async_accept(info->socket, boost::bind(
|
---|
| 163 | // bind parameters
|
---|
| 164 | &rfcomm::handle_accept, this,
|
---|
| 165 |
|
---|
| 166 | // handler parameters
|
---|
| 167 | boost::asio::placeholders::error, info
|
---|
| 168 | ));
|
---|
| 169 | asio_io_service::start();
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | void rfcomm::handle_accept(const error_code& error, link_info* info) {
|
---|
| 173 | if (error) {
|
---|
| 174 | cout << "Error accepting" << endl;
|
---|
| 175 | delete info;
|
---|
| 176 | return;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | // convert endpoints
|
---|
| 180 | info->up = true;
|
---|
| 181 | info->local = convert( info->socket.local_endpoint() );
|
---|
| 182 | info->remote = convert( info->socket.remote_endpoint() );
|
---|
| 183 |
|
---|
| 184 | cout << "Accepting incoming connection from " << info->remote.to_string() << endl;
|
---|
| 185 |
|
---|
| 186 | // add to list
|
---|
| 187 | links_mutex.lock();
|
---|
| 188 | links.push_back(info);
|
---|
| 189 | links_mutex.unlock();
|
---|
| 190 |
|
---|
| 191 | // start i/o
|
---|
| 192 | start_read(info);
|
---|
| 193 | start_write(info);
|
---|
| 194 |
|
---|
| 195 | // restart accept
|
---|
| 196 | start_accept();
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | void rfcomm::handle_connect( const error_code& error, link_info* info ) {
|
---|
| 200 | if (error) {
|
---|
| 201 | cout << "Error connecting ..." << endl;
|
---|
| 202 | delete info;
|
---|
| 203 | return;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | // convert endpoints
|
---|
| 207 | info->up = true;
|
---|
| 208 | info->local = convert( info->socket.local_endpoint() );
|
---|
| 209 | info->remote = convert( info->socket.remote_endpoint() );
|
---|
| 210 |
|
---|
| 211 | cout << "Connected to " << info->remote.to_string() << endl;
|
---|
| 212 |
|
---|
| 213 | // add to list
|
---|
| 214 | links_mutex.lock();
|
---|
| 215 | links.push_back(info);
|
---|
| 216 | links_mutex.unlock();
|
---|
| 217 |
|
---|
| 218 | // start i/o
|
---|
| 219 | start_read(info);
|
---|
| 220 | start_write(info);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | void rfcomm::start_read(link_info* info) {
|
---|
| 224 | cout << "Waiting for messages..." << endl;
|
---|
| 225 |
|
---|
| 226 | // start read
|
---|
| 227 | boost::asio::async_read(info->socket,
|
---|
| 228 | // read size of packet
|
---|
| 229 | boost::asio::buffer(info->size_, 4),
|
---|
| 230 | // bind handler
|
---|
| 231 | boost::bind(
|
---|
| 232 | // bind parameters
|
---|
| 233 | &rfcomm::handle_read_header, this,
|
---|
| 234 | // handler parameters
|
---|
| 235 | placeholders::error, placeholders::bytes_transferred, info
|
---|
| 236 | )
|
---|
| 237 | );
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | void rfcomm::handle_read_header(const error_code& error, size_t bytes,
|
---|
| 241 | link_info* info) {
|
---|
| 242 |
|
---|
| 243 | // ignore errors and wait for all data to be received
|
---|
| 244 | if (error || bytes != 4) return;
|
---|
| 245 |
|
---|
| 246 | // get size
|
---|
| 247 | info->size = (info->size_[0]<<24) + (info->size_[1] << 16) +
|
---|
| 248 | (info->size_[2]<< 8) + (info->size_[3] << 0);
|
---|
| 249 |
|
---|
| 250 | cout << "receive message of size " << info->size << endl;
|
---|
| 251 |
|
---|
| 252 | // allocate buffer
|
---|
| 253 | info->buffer = new uint8_t[info->size];
|
---|
| 254 |
|
---|
| 255 | // start read
|
---|
| 256 | boost::asio::async_read(info->socket,
|
---|
| 257 | // read size of packet
|
---|
| 258 | boost::asio::buffer(info->buffer, info->size),
|
---|
| 259 | // bind handler
|
---|
| 260 | boost::bind(
|
---|
| 261 | // bind parameters
|
---|
| 262 | &rfcomm::handle_read_data, this,
|
---|
| 263 | // handler parameters
|
---|
| 264 | placeholders::error, placeholders::bytes_transferred, info
|
---|
| 265 | )
|
---|
| 266 | );
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | void rfcomm::handle_read_data(const error_code& error, size_t bytes,
|
---|
| 270 | link_info* info) {
|
---|
| 271 |
|
---|
| 272 | // ignore errors and wait for all data to be received
|
---|
| 273 | if (error || bytes != info->size) return;
|
---|
| 274 |
|
---|
| 275 | cout << "received message of size " << info->size << endl;
|
---|
| 276 |
|
---|
| 277 | // deliver data
|
---|
| 278 | listener->receive_message(this, info->local, info->remote, info->buffer, info->size );
|
---|
| 279 |
|
---|
| 280 | // free buffers and reset size buffer
|
---|
| 281 | delete [] info->buffer;
|
---|
| 282 | for (size_t i=0; i<4; i++) info->size_[i] = 0;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | void rfcomm::start_write( link_info* info ) {
|
---|
| 286 | // do not start writing if sending is in progress
|
---|
| 287 | if (info->sending || !info->up || info->send_buffer.size()==0) return;
|
---|
| 288 |
|
---|
| 289 | cout << "Sending messages..." << endl;
|
---|
| 290 |
|
---|
| 291 | // safely remove data from deque
|
---|
| 292 | info->mutex.lock();
|
---|
| 293 | link_data data = info->send_buffer.front();
|
---|
| 294 | info->send_buffer.pop_front();
|
---|
| 295 | info->mutex.unlock();
|
---|
| 296 |
|
---|
| 297 | boost::array<boost::asio::mutable_buffer, 2> buffer;
|
---|
| 298 | buffer[0] = boost::asio::buffer(data.size_,4);
|
---|
| 299 | buffer[1] = boost::asio::buffer(data.buffer,data.size);
|
---|
| 300 |
|
---|
| 301 | // start writing
|
---|
| 302 | boost::asio::async_write(info->socket,
|
---|
| 303 | // read size of packet
|
---|
| 304 | buffer,
|
---|
| 305 | // bind handler
|
---|
| 306 | boost::bind(
|
---|
| 307 | // bind parameters
|
---|
| 308 | &rfcomm::handle_write_data, this,
|
---|
| 309 | // handler parameters
|
---|
| 310 | placeholders::error, placeholders::bytes_transferred,
|
---|
| 311 | info, data.size, data.buffer
|
---|
| 312 | )
|
---|
| 313 | );
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | void rfcomm::handle_write_data(const error_code& error, size_t bytes,
|
---|
| 317 | link_info* info, size_t size, uint8_t* buffer ) {
|
---|
| 318 |
|
---|
| 319 | // ignore errors and wait for all data to be sent
|
---|
| 320 | if (error || bytes != (size+4) ) {
|
---|
| 321 | if (error) cout << "Message sent error" << endl;
|
---|
| 322 | return;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | cout << "Message sent" << endl;
|
---|
| 326 |
|
---|
| 327 | // free buffer
|
---|
| 328 | delete [] buffer;
|
---|
| 329 |
|
---|
| 330 | // restart-write
|
---|
| 331 | start_write(info);
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | }} // namespace ariba::transport
|
---|