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