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

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