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

Last change on this file since 5286 was 5286, checked in by mies, 15 years ago
File size: 7.6 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
13
14namespace ariba {
15namespace transport {
16
17using namespace boost::asio;
18using namespace detail;
19using namespace std;
20
21using boost::system::error_code;
22
23class link_data {
24public:
25 uint8_t size_[4];
26 size_t size;
27 uint8_t* buffer;
28};
29
30class link_info {
31public:
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
52inline bluetooth::rfcomm::endpoint convert( const rfcomm_endpoint& endpoint ) {
53 return bluetooth::rfcomm::endpoint(
54 endpoint.mac().bluetooth(), endpoint.channel().value()
55 );
56}
57
58inline 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
67rfcomm::rfcomm(uint16_t channel) :
68 channel(channel), io(asio_io_service::alloc()) {
69}
70
71rfcomm::~rfcomm() {
72 asio_io_service::free();
73}
74
75void 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
90void rfcomm::stop() {
91
92}
93
94void 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
137void 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
148void rfcomm::terminate(const address_v* local, const address_v* remote) {
149 // not supported right now!
150}
151
152void rfcomm::register_listener(transport_listener* listener) {
153 this->listener = listener;
154}
155
156void 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
172void 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
199void 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
223void 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
240void 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
269void 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 start_read(info);
285}
286
287void rfcomm::start_write( link_info* info ) {
288 // do not start writing if sending is in progress
289 if (info->sending || !info->up || info->send_buffer.size()==0) return;
290
291 cout << "Sending messages..." << endl;
292
293 // safely remove data from deque
294 info->mutex.lock();
295 link_data data = info->send_buffer.front();
296 info->send_buffer.pop_front();
297 info->mutex.unlock();
298
299 boost::array<boost::asio::mutable_buffer, 2> buffer;
300 buffer[0] = boost::asio::buffer(data.size_,4);
301 buffer[1] = boost::asio::buffer(data.buffer,data.size);
302
303 // start writing
304 boost::asio::async_write(info->socket,
305 // read size of packet
306 buffer,
307 // bind handler
308 boost::bind(
309 // bind parameters
310 &rfcomm::handle_write_data, this,
311 // handler parameters
312 placeholders::error, placeholders::bytes_transferred,
313 info, data.size, data.buffer
314 )
315 );
316}
317
318void rfcomm::handle_write_data(const error_code& error, size_t bytes,
319 link_info* info, size_t size, uint8_t* buffer ) {
320
321 // ignore errors and wait for all data to be sent
322 if (error || bytes != (size+4) ) {
323 if (error) cout << "Message sent error" << endl;
324 return;
325 }
326
327 cout << "Message sent" << endl;
328
329 // free buffer
330 delete [] buffer;
331
332 // restart-write
333 start_write(info);
334}
335
336}} // namespace ariba::transport
Note: See TracBrowser for help on using the repository browser.