X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Flib%2FAdapterCommunication.cpp;h=ae1a4e451c288fd4820cc3527efaf4084d67f20b;hb=12027dbe8463aa8dbc3a7b3df60862a867cf94d9;hp=1ee7f883e53e3d9549bc998e289f0883faecb0b9;hpb=828682d35c298ff062422521689258c363ea3579;p=deb_libcec.git diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 1ee7f88..ae1a4e4 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -31,15 +31,17 @@ */ #include "AdapterCommunication.h" -#include "CECParser.h" -#include "libPlatform/serialport.h" + +#include "LibCEC.h" +#include "platform/serialport.h" #include "util/StdString.h" using namespace std; using namespace CEC; -CAdapterCommunication::CAdapterCommunication(CCECParser *parser) : - m_parser(parser), +CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : + m_port(NULL), + m_controller(controller), m_inbuf(NULL), m_iInbufSize(0), m_iInbufUsed(0), @@ -51,43 +53,46 @@ CAdapterCommunication::CAdapterCommunication(CCECParser *parser) : CAdapterCommunication::~CAdapterCommunication(void) { - m_port->Close(); - m_port = NULL; + Close(); + + if (m_port) + { + delete m_port; + m_port = NULL; + } } -bool CAdapterCommunication::Open(const char *strPort, int iBaudRate /* = 38400 */, int iTimeoutMs /* = 10000 */) +bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint64_t iTimeoutMs /* = 10000 */) { CLockObject lock(&m_commMutex); - if (m_bStarted) + if (m_bStarted || !m_port) return false; if (!m_port->Open(strPort, iBaudRate)) { CStdString strError; strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str()); - m_parser->AddLog(CEC_LOG_ERROR, strError); + m_controller->AddLog(CEC_LOG_ERROR, strError); return false; } - m_parser->AddLog(CEC_LOG_DEBUG, "connection opened"); + m_controller->AddLog(CEC_LOG_DEBUG, "connection opened"); //clear any input bytes uint8_t buff[1024]; m_port->Read(buff, sizeof(buff), 50); - CCondition::Sleep(CEC_SETTLE_DOWN_TIME); - - m_bStop = false; - m_bStarted = true; + Sleep(CEC_SETTLE_DOWN_TIME); if (CreateThread()) { - m_parser->AddLog(CEC_LOG_DEBUG, "reader thread created"); + m_controller->AddLog(CEC_LOG_DEBUG, "reader thread created"); + m_bStarted = true; return true; } else { - m_parser->AddLog(CEC_LOG_DEBUG, "could not create a reader thread"); + m_controller->AddLog(CEC_LOG_DEBUG, "could not create a reader thread"); } return false; @@ -95,50 +100,58 @@ bool CAdapterCommunication::Open(const char *strPort, int iBaudRate /* = 38400 * void CAdapterCommunication::Close(void) { + CLockObject lock(&m_commMutex); + if (m_port) + m_port->Close(); + StopThread(); - m_port->Close(); } void *CAdapterCommunication::Process(void) { + m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started"); + while (!m_bStop) { + CLockObject lock(&m_commMutex); if (!ReadFromDevice(250)) { m_bStarted = false; break; } - CCondition::Sleep(50); + if (!m_bStop) + { + lock.Leave(); + Sleep(50); + } } - m_parser->AddLog(CEC_LOG_DEBUG, "reader thread terminated"); - - CLockObject lock(&m_commMutex); m_bStarted = false; return NULL; } -bool CAdapterCommunication::ReadFromDevice(int iTimeout) +bool CAdapterCommunication::ReadFromDevice(uint64_t iTimeout) { uint8_t buff[1024]; - CLockObject lock(&m_commMutex); - int iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); - lock.Leave(); - if (iBytesRead < 0) + if (!m_port) + return false; + + int32_t iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); + if (iBytesRead < 0 || iBytesRead > 256) { CStdString strError; strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); - m_parser->AddLog(CEC_LOG_ERROR, strError); + m_controller->AddLog(CEC_LOG_ERROR, strError); return false; } else if (iBytesRead > 0) - AddData(buff, iBytesRead); + AddData(buff, (uint8_t) iBytesRead); return true; } -void CAdapterCommunication::AddData(uint8_t *data, int iLen) +void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) { CLockObject lock(&m_bufferMutex); if (iLen + m_iInbufUsed > m_iInbufSize) @@ -157,19 +170,22 @@ bool CAdapterCommunication::Write(const cec_frame &data) { CLockObject lock(&m_commMutex); - if (m_port->Write(data) != data.size()) + if (m_port->Write(data) != (int) data.size()) { CStdString strError; strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); - m_parser->AddLog(CEC_LOG_ERROR, strError); + m_controller->AddLog(CEC_LOG_ERROR, strError); return false; } - m_parser->AddLog(CEC_LOG_DEBUG, "command sent"); + m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); + + Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */); + return true; } -bool CAdapterCommunication::Read(cec_frame &msg, int iTimeout) +bool CAdapterCommunication::Read(cec_frame &msg, uint64_t iTimeout) { CLockObject lock(&m_bufferMutex); @@ -222,7 +238,7 @@ bool CAdapterCommunication::Read(cec_frame &msg, int iTimeout) if (startpos > 0) //we found a msgstart before msgend, this is not right, remove { - m_parser->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND"); + m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND"); memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos); m_iInbufUsed -= startpos; return false; @@ -264,3 +280,85 @@ std::string CAdapterCommunication::GetError(void) const { return m_port->GetError(); } + +bool CAdapterCommunication::StartBootloader(void) +{ + if (!IsRunning()) + return false; + + m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); + cec_frame output; + output.push_back(MSGSTART); + PushEscaped(output, MSGCODE_START_BOOTLOADER); + output.push_back(MSGEND); + + if (!Write(output)) + { + m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader"); + return false; + } + m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted"); + return true; +} + +void CAdapterCommunication::PushEscaped(cec_frame &vec, uint8_t byte) +{ + if (byte >= MSGESC && byte != MSGSTART) + { + vec.push_back(MSGESC); + vec.push_back(byte - ESCOFFSET); + } + else + { + vec.push_back(byte); + } +} + +bool CAdapterCommunication::SetAckMask(uint16_t iMask) +{ + if (!IsRunning()) + return false; + + CStdString strLog; + strLog.Format("setting ackmask to %2x", iMask); + m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); + + cec_frame output; + + output.push_back(MSGSTART); + PushEscaped(output, MSGCODE_SET_ACK_MASK); + PushEscaped(output, iMask >> 8); + PushEscaped(output, (uint8_t)iMask); + output.push_back(MSGEND); + + if (!Write(output)) + { + m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask"); + return false; + } + + return true; +} + +bool CAdapterCommunication::PingAdapter(void) +{ + if (!IsRunning()) + return false; + + m_controller->AddLog(CEC_LOG_DEBUG, "sending ping"); + cec_frame output; + output.push_back(MSGSTART); + PushEscaped(output, MSGCODE_PING); + output.push_back(MSGEND); + + if (!Write(output)) + { + m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command"); + return false; + } + + m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted"); + + // TODO check for pong + return true; +}