FrelonSerialLine.h 2.46 KB
Newer Older
1
2
3
4
#ifndef FRELONSERIALLINE_H
#define FRELONSERIALLINE_H

#include "Frelon.h"
5
#include "ThreadUtils.h"
6
7
8
9
10
11
12
13
14
15
16
17
18
19

namespace lima
{

namespace Frelon
{

class SerialLine : public HwSerialLine
{
 public:
	enum MsgPart {
		MsgSync, MsgCmd, MsgVal, MsgReq, MsgTerm, 
	};
	
20
21
22
23
24
25
26
27
	enum AnsPart {
		AnsResp, AnsWarn, AnsErr,
	};
	
	enum {
		MaxReadLen = 10000,
	};

ahoms's avatar
ahoms committed
28
29
	typedef std::map<MsgPart, std::string> MsgPartStrMapType;

30
	static const double TimeoutSingle, TimeoutNormal, TimeoutMultiLine, 
31
32
33
34
			    TimeoutReset;
	
	SerialLine(Espia::SerialLine& espia_ser_line);
	
35
36
	Espia::SerialLine& getEspiaSerialLine();

37
38
	virtual void write(const std::string& buffer, 
			   bool no_wait = false);
39
40
	virtual void read(std::string& buffer, 
			  int max_len = MaxReadLen, 
41
			  double timeout = TimeoutDefault);
42
43
	virtual void readStr(std::string& buffer, int max_len, 
			     const std::string& term, 
44
			     double timeout = TimeoutDefault);
45
46
	virtual void readLine(std::string& buffer, 
			      int max_len = MaxReadLen, 
47
			      double timeout = TimeoutDefault);
48
49
	virtual void readSingleLine(std::string& buffer, 
				    int max_len = MaxReadLen, 
50
				    double timeout = TimeoutDefault);
51
52
	virtual void readMultiLine(std::string& buffer, 
				   int max_len = MaxReadLen);
53
	
54
55
	virtual void flush();

56
	virtual void getNbAvailBytes(int &avail);
57
	
58
59
60
	virtual void setTimeout(double timeout);
	virtual void getTimeout(double& timeout) const;

61
	void splitMsg(const std::string& msg, 
ahoms's avatar
ahoms committed
62
		      MsgPartStrMapType& msg_parts) const;
63
64
65
66
67
	void decodeFmtResp(const std::string& ans, std::string& fmt_resp);

	void sendFmtCmd(const std::string& cmd, std::string& resp);

	int getLastWarning();
68

69
70
71
72
	void clearCache();
	void setCacheActive(bool  cache_act);
	void getCacheActive(bool& cache_act);

73
 private:
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	enum RegOp {
		None, DoCmd, ReadReg, WriteReg, DoReset, MultiRead
	};

	typedef std::map<Reg, std::string> RegRespMapType;

	AutoMutex lock(int mode);

	virtual void writeCmd(const std::string& buffer, 
			      bool no_wait = false);
	virtual void readResp(std::string& buffer, 
			      int max_len = MaxReadLen, 
			      double timeout = TimeoutDefault);

88
	Espia::SerialLine& m_espia_ser_line;
89
	Cond m_cond;
90
	int m_last_warn;
91
92
93
94
95
96
97
98

	RegRespMapType m_reg_cache;
	bool m_cache_act;
	RegOp m_curr_op;
	Reg m_curr_reg;
	bool m_curr_cache;
	std::string m_curr_resp;
	std::string m_curr_fmt_resp;
99
100
};

101
102
103
104
105
inline AutoMutex SerialLine::lock(int mode)
{
	return AutoMutex(m_cond.mutex(), mode);
}

106
107
108
109
110
111
112
113
114

} // namespace Frelon



} // namespace lima


#endif // FRELONSERIALLINE_H