source: trunk/FACT++/src/ConnectionUSB.h@ 12336

Last change on this file since 12336 was 12236, checked in by tbretz, 13 years ago
Changed signed char to unsigned in output queue.
File size: 3.4 KB
Line 
1#ifndef FACT_Connection
2#define FACT_Connection
3
4#include <array>
5#include <deque>
6#include <string>
7#include <boost/asio.hpp>
8#include <boost/function.hpp>
9#include <boost/asio/deadline_timer.hpp>
10
11#include "MessageImp.h"
12
13class ConnectionUSB : public MessageImp, public boost::asio::serial_port
14{
15private:
16 MessageImp *fLog;
17
18 std::string fAddress;
19
20 boost::asio::serial_port_base::baud_rate fBaudRate; // unisgned int
21 boost::asio::serial_port_base::character_size fCharacterSize; // unisgned int
22 boost::asio::serial_port_base::parity fParity; // unisgned int
23 boost::asio::serial_port_base::stop_bits fStopBits; // unisgned int
24 boost::asio::serial_port_base::flow_control fFlowControl; // unisgned int
25
26 enum ConnectionStatus_t
27 {
28 kDisconnected = 0,
29 kConnecting = 1,
30 kConnected = 2,
31 };
32
33protected:
34 boost::asio::deadline_timer fInTimeout;
35
36private:
37 boost::asio::deadline_timer fOutTimeout;
38 std::deque<std::vector<uint8_t>> fOutQueue;
39
40 ConnectionStatus_t fConnectionStatus;
41
42public:
43 void SetLogStream(MessageImp *log) { fLog = log; }
44 std::ostream &Out() { return fLog ? fLog->Out() : Out(); }
45
46 // -------- Abbreviations for starting async tasks ---------
47
48 void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0, int counter=0);
49 void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
50 void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
51 void (ConnectionUSB::*handler)(const boost::system::error_code&));
52
53protected:
54 void CloseImp(bool restart=true);
55
56private:
57 void ConnectImp(const boost::system::error_code& error,
58 boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
59
60 void HandleWriteTimeout(const boost::system::error_code &error);
61 void HandleSentData(const boost::system::error_code& error, size_t);
62
63 int Write(const Time &t, const std::string &txt, int qos=kInfo);
64
65 virtual void ConnectionEstablished() { }
66
67public:
68 ConnectionUSB(boost::asio::io_service& io_service, std::ostream &out);
69
70 // ------------------------ connect --------------------------
71
72 void SetEndpoint(const std::string &addr);
73
74 void Connect();
75
76 // ------------------------ close --------------------------
77 void PostClose(bool restart=true);
78
79 // ------------------------ write --------------------------
80 void SendMessageImp(const std::vector<uint8_t> msg);
81 void PostMessage(const void *msg, size_t s=0);
82 void PostMessage(const std::string &cmd, size_t s=-1);
83
84 template<typename T, size_t N>
85 void PostMessage(const std::array<T, N> &msg)
86 {
87 PostMessage(msg.begin(), msg.size()*sizeof(T));
88 }
89
90 template<typename T>
91 void PostMessage(const std::vector<T> &msg)
92 {
93 PostMessage(&msg[0], msg.size()*sizeof(T));
94 }
95
96 // ------------------------ others --------------------------
97
98 virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0, int = 0) { }
99 virtual void HandleTransmittedData(size_t) { }
100 virtual void HandleReadTimeout(const boost::system::error_code&) { }
101
102 int IsClosed() const { return !is_open(); }
103
104 bool IsConnected() const { return fConnectionStatus==kConnected; }
105 bool IsConnecting() const { return fConnectionStatus==kConnecting; }
106
107 std::string URL() const { return fAddress; }
108};
109
110#endif
Note: See TracBrowser for help on using the repository browser.