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