FrelonSerialLine.h 2.71 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

namespace lima
{

namespace Frelon
{

class SerialLine : public HwSerialLine
{
15
16
	DEB_CLASS_NAMESPC(DebModFrelonSerial, "SerialLine", "Frelon");

17
18
19
20
21
 public:
	enum MsgPart {
		MsgSync, MsgCmd, MsgVal, MsgReq, MsgTerm, 
	};
	
22
23
24
25
26
27
28
29
	enum AnsPart {
		AnsResp, AnsWarn, AnsErr,
	};
	
	enum {
		MaxReadLen = 10000,
	};

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

32
	static const double TimeoutSingle, TimeoutNormal, TimeoutMultiLine, 
33
34
35
			    TimeoutReset;
	
	SerialLine(Espia::SerialLine& espia_ser_line);
36
37
	virtual ~SerialLine();

38
39
	Espia::SerialLine& getEspiaSerialLine();

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

59
	virtual void getNbAvailBytes(int &avail);
60
	
61
62
63
	virtual void setTimeout(double timeout);
	virtual void getTimeout(double& timeout) const;

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

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

	int getLastWarning();
71

72
73
74
75
	void clearCache();
	void setCacheActive(bool  cache_act);
	void getCacheActive(bool& cache_act);

76
 private:
77
78
79
	enum RegOp {
		None, DoCmd, ReadReg, WriteReg, DoReset, MultiRead
	};
80
	friend std::ostream& operator <<(std::ostream& os, RegOp op);
81
82
83
84
85
86
87
88
89
90
91

	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);

92
93
	bool isRegCacheable(Reg reg);

94
	Espia::SerialLine& m_espia_ser_line;
95
	Cond m_cond;
96
	int m_last_warn;
97
98
99
100
101
102
103
104

	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;
105
106
};

107
108
109
std::ostream& operator <<(std::ostream& os, SerialLine::RegOp op);


110
111
112
113
114
inline AutoMutex SerialLine::lock(int mode)
{
	return AutoMutex(m_cond.mutex(), mode);
}

115
116
117
118
119
120
121
122
123

} // namespace Frelon



} // namespace lima


#endif // FRELONSERIALLINE_H