Commit 9a59ec6c authored by ahoms's avatar ahoms

* added debug messages to Espia::BufferMgr and Frelon::SerialLine



git-svn-id: https://scm.blissgarden.org/svn/lima/trunk@178 45c4679d-1946-429d-baad-37912b19538b
parent 35ccf1e3
......@@ -12,6 +12,8 @@ namespace Frelon
class SerialLine : public HwSerialLine
{
DEB_CLASS_NAMESPC(DebModFrelonSerial, "SerialLine", "Frelon");
public:
enum MsgPart {
MsgSync, MsgCmd, MsgVal, MsgReq, MsgTerm,
......@@ -31,7 +33,8 @@ class SerialLine : public HwSerialLine
TimeoutReset;
SerialLine(Espia::SerialLine& espia_ser_line);
virtual ~SerialLine();
Espia::SerialLine& getEspiaSerialLine();
virtual void write(const std::string& buffer,
......@@ -74,6 +77,7 @@ class SerialLine : public HwSerialLine
enum RegOp {
None, DoCmd, ReadReg, WriteReg, DoReset, MultiRead
};
friend std::ostream& operator <<(std::ostream& os, RegOp op);
typedef std::map<Reg, std::string> RegRespMapType;
......@@ -100,6 +104,9 @@ class SerialLine : public HwSerialLine
std::string m_curr_fmt_resp;
};
std::ostream& operator <<(std::ostream& os, SerialLine::RegOp op);
inline AutoMutex SerialLine::lock(int mode)
{
return AutoMutex(m_cond.mutex(), mode);
......
......@@ -31,6 +31,7 @@ class SerialLine : HwSerialLine
// TimeoutReset;
SerialLine(Espia::SerialLine& espia_ser_line);
virtual ~SerialLine();
Espia::SerialLine& getEspiaSerialLine();
......
......@@ -16,6 +16,11 @@ const double SerialLine::TimeoutReset = 5.0;
SerialLine::SerialLine(Espia::SerialLine& espia_ser_line)
: m_espia_ser_line(espia_ser_line)
{
DEB_CONSTRUCTOR();
ostringstream os;
os << "Serial#" << espia_ser_line.getDev().getDevNb();
DEB_SET_OBJ_NAME(os.str());
m_espia_ser_line.setLineTerm("\r\n");
m_espia_ser_line.setTimeout(TimeoutNormal);
......@@ -28,6 +33,11 @@ SerialLine::SerialLine(Espia::SerialLine& espia_ser_line)
flush();
}
SerialLine::~SerialLine()
{
DEB_DESTRUCTOR();
}
Espia::SerialLine& SerialLine::getEspiaSerialLine()
{
return m_espia_ser_line;
......@@ -35,10 +45,14 @@ Espia::SerialLine& SerialLine::getEspiaSerialLine()
void SerialLine::write(const string& buffer, bool no_wait)
{
DEB_MEMBER_FUNCT();
AutoMutex l = lock(AutoMutex::Locked);
while (m_curr_op != None)
while (m_curr_op != None) {
DEB_TRACE() << "Waiting end of current " << m_curr_op;
m_cond.wait();
}
writeCmd(buffer, no_wait);
......@@ -47,12 +61,16 @@ void SerialLine::write(const string& buffer, bool no_wait)
void SerialLine::writeCmd(const string& buffer, bool no_wait)
{
DEB_MEMBER_FUNCT();
DEB_PARAM_VAR1(no_wait);
MsgPartStrMapType msg_parts;
splitMsg(buffer, msg_parts);
const string& cmd = msg_parts[MsgCmd];
if (cmd == CmdStrMap[Reset]) {
m_curr_op = DoReset;
DEB_TRACE() << "DoReset: clearing reg cache";
m_reg_cache.clear();
} else {
MultiLineCmdStrMapType::const_iterator it, end;
......@@ -83,13 +101,18 @@ void SerialLine::writeCmd(const string& buffer, bool no_wait)
m_curr_resp = msg_parts[MsgVal];
m_curr_cache = (m_curr_resp == cache_val);
}
if (m_curr_cache)
if (m_curr_cache) {
DEB_TRACE() << "Skipping " << m_curr_op
<< ", already in cache";
return;
}
}
if (m_curr_op == None)
m_curr_op = DoCmd;
DEB_TRACE_VAR1(m_curr_op);
string sync = msg_parts[MsgSync].empty() ? ">" : "";
string term = msg_parts[MsgTerm].empty() ? "\r\n" : "";
string msg = sync + buffer + term;
......@@ -99,17 +122,20 @@ void SerialLine::writeCmd(const string& buffer, bool no_wait)
void SerialLine::read(string& buffer, int max_len, double timeout)
{
DEB_MEMBER_FUNCT();
m_espia_ser_line.read(buffer, max_len, timeout);
}
void SerialLine::readStr(string& buffer, int max_len,
const string& term, double timeout)
{
DEB_MEMBER_FUNCT();
m_espia_ser_line.readStr(buffer, max_len, term, timeout);
}
void SerialLine::readLine(string& buffer, int max_len, double timeout)
{
DEB_MEMBER_FUNCT();
AutoMutex l = lock(AutoMutex::PrevLocked);
readResp(buffer, max_len, timeout);
......@@ -117,8 +143,13 @@ void SerialLine::readLine(string& buffer, int max_len, double timeout)
void SerialLine::readResp(string& buffer, int max_len, double timeout)
{
if (m_curr_op == None)
DEB_MEMBER_FUNCT();
DEB_PARAM_VAR3(max_len, timeout, m_curr_op);
if (m_curr_op == None) {
DEB_FATAL() << "readLine without previous write!";
throw LIMA_HW_EXC(Error, "readLine without previous write");
}
if ((m_curr_op == MultiRead) && (timeout == TimeoutDefault))
readMultiLine(buffer, max_len);
......@@ -131,8 +162,11 @@ void SerialLine::readResp(string& buffer, int max_len, double timeout)
void SerialLine::readSingleLine(string& buffer, int max_len, double timeout)
{
DEB_MEMBER_FUNCT();
bool is_req = (m_curr_op == ReadReg);
if (m_curr_cache) {
DEB_TRACE() << "Using cache value";
ostringstream os;
os << "!OK";
if (is_req)
......@@ -140,6 +174,7 @@ void SerialLine::readSingleLine(string& buffer, int max_len, double timeout)
os << "\r\n";
buffer = os.str();
m_curr_fmt_resp = m_curr_resp;
DEB_TRACE_VAR1(m_curr_fmt_resp);
return;
}
......@@ -155,10 +190,14 @@ void SerialLine::readSingleLine(string& buffer, int max_len, double timeout)
string& cache_val = m_reg_cache[m_curr_reg];
cache_val = is_req ? m_curr_fmt_resp : m_curr_resp;
DEB_TRACE_VAR1(cache_val);
}
void SerialLine::readMultiLine(string& buffer, int max_len)
{
DEB_MEMBER_FUNCT();
DEB_PARAM_VAR1(max_len);
Timestamp timeout = Timestamp::now() + Timestamp(TimeoutMultiLine);
buffer.clear();
......@@ -166,6 +205,7 @@ void SerialLine::readMultiLine(string& buffer, int max_len)
while (Timestamp::now() < timeout) {
string ans;
int len = max_len - buffer.size();
DEB_TRACE_VAR1(len);
try {
m_espia_ser_line.readLine(ans, len, TimeoutSingle);
buffer += ans;
......@@ -175,43 +215,61 @@ void SerialLine::readMultiLine(string& buffer, int max_len)
}
}
if (buffer.empty())
if (buffer.empty()) {
DEB_ERROR() << "Timeout reading multi-line";
throw LIMA_HW_EXC(Error, "Timeout reading Frelon multi line");
}
}
bool SerialLine::isRegCacheable(Reg reg)
{
if (!m_cache_act)
DEB_MEMBER_FUNCT();
if (!m_cache_act) {
DEB_TRACE() << "Reg cache is disabled";
return false;
}
const RegListType& list = NonCacheableRegList;
return (find(list.begin(), list.end(), reg) == list.end());
bool cacheable = (find(list.begin(), list.end(), reg) == list.end());
DEB_RETURN_VAR1(cacheable);
return cacheable;
}
void SerialLine::flush()
{
DEB_MEMBER_FUNCT();
m_espia_ser_line.flush();
}
void SerialLine::getNbAvailBytes(int &avail)
{
DEB_MEMBER_FUNCT();
m_espia_ser_line.getNbAvailBytes(avail);
DEB_RETURN_VAR1(avail);
}
void SerialLine::setTimeout(double timeout)
{
DEB_MEMBER_FUNCT();
DEB_PARAM_VAR1(timeout);
m_espia_ser_line.setTimeout(timeout);
}
void SerialLine::getTimeout(double& timeout) const
{
DEB_MEMBER_FUNCT();
m_espia_ser_line.getTimeout(timeout);
DEB_RETURN_VAR1(timeout);
}
void SerialLine::splitMsg(const string& msg,
MsgPartStrMapType& msg_parts) const
{
DEB_MEMBER_FUNCT();
DEB_PARAM_VAR1(msg);
msg_parts.clear();
const static RegEx re("^(?P<sync>>)?"
......@@ -235,10 +293,16 @@ void SerialLine::splitMsg(const string& msg,
const string& grp = it->second;
msg_parts[key] = string(match[grp].start, match[grp].end);
}
DEB_RETURN_VAR2(msg_parts[MsgSync], msg_parts[MsgCmd]);
DEB_RETURN_VAR2(msg_parts[MsgReq], msg_parts[MsgVal]);
}
void SerialLine::decodeFmtResp(const string& ans, string& fmt_resp)
{
DEB_MEMBER_FUNCT();
DEB_PARAM_VAR1(ans);
fmt_resp.clear();
const static RegEx re("!(OK(:(?P<resp>[^\r]+))?|"
......@@ -246,19 +310,23 @@ void SerialLine::decodeFmtResp(const string& ans, string& fmt_resp)
"E\a?:(?P<err>[^\r]+))\r\n");
RegEx::FullNameMatchType match;
if (!re.matchName(ans, match))
throw LIMA_HW_EXC(Error, "Invalid Frelon answer");
if (!re.matchName(ans, match)) {
DEB_ERROR() << "Unexpected Frelon answer";
throw LIMA_HW_EXC(Error, "Unexpected Frelon answer");
}
RegEx::SingleMatchType& err = match["err"];
if (err) {
string err_str(err.start, err.end);
string err_desc = string("Frelon Error: ") + err_str;
DEB_ERROR() << err_desc;
throw LIMA_HW_EXC(Error, err_desc);
}
RegEx::SingleMatchType& warn = match["warn"];
if (warn) {
string warn_str(err.start, err.end);
string warn_str(warn.start, warn.end);
DEB_WARNING() << "Camera warning: " << warn_str;
istringstream is(warn_str);
is >> m_last_warn;
return;
......@@ -266,10 +334,14 @@ void SerialLine::decodeFmtResp(const string& ans, string& fmt_resp)
RegEx::SingleMatchType& resp = match["resp"];
fmt_resp = string(resp.start, resp.end);
if (!fmt_resp.empty())
DEB_RETURN_VAR1(fmt_resp);
}
void SerialLine::sendFmtCmd(const string& cmd, string& resp)
{
DEB_MEMBER_FUNCT();
AutoMutex l = lock(AutoMutex::Locked);
m_curr_fmt_resp.clear();
......@@ -283,27 +355,53 @@ void SerialLine::sendFmtCmd(const string& cmd, string& resp)
int SerialLine::getLastWarning()
{
DEB_MEMBER_FUNCT();
int last_warn = m_last_warn;
m_last_warn = 0;
DEB_RETURN_VAR1(last_warn);
return last_warn;
}
void SerialLine::clearCache()
{
DEB_MEMBER_FUNCT();
AutoMutex l = lock(AutoMutex::Locked);
DEB_TRACE() << "Clearing reg cache";
m_reg_cache.clear();
}
void SerialLine::setCacheActive(bool cache_act)
{
DEB_MEMBER_FUNCT();
DEB_PARAM_VAR2(cache_act, m_cache_act);
AutoMutex l = lock(AutoMutex::Locked);
if (cache_act && !m_cache_act)
if (cache_act && !m_cache_act) {
DEB_TRACE() << "Clearing reg cache";
m_reg_cache.clear();
}
m_cache_act = cache_act;
}
void SerialLine::getCacheActive(bool& cache_act)
{
DEB_MEMBER_FUNCT();
AutoMutex l = lock(AutoMutex::Locked);
cache_act = m_cache_act;
DEB_RETURN_VAR1(cache_act);
}
ostream& lima::Frelon::operator <<(ostream& os, SerialLine::RegOp op)
{
const char *name = "Unknown";
switch (op) {
case SerialLine::None: name = "None"; break;
case SerialLine::DoCmd: name = "DoCmd"; break;
case SerialLine::ReadReg: name = "ReadReg"; break;
case SerialLine::WriteReg: name = "WriteReg"; break;
case SerialLine::DoReset: name = "DoReset"; break;
case SerialLine::MultiRead: name = "MultiRead"; break;
}
return os << name;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment