From: Lars Op den Kamp Date: Tue, 4 Oct 2011 21:48:29 +0000 (+0200) Subject: Merge branch 'master' into release X-Git-Tag: upstream/2.2.0~1^2~118^2~12 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3424a2f327e569e92c7282c6e643536984a65c64;hp=93f86187ea362ab62f9e0f93b77fb3bcc29166ab;p=deb_libcec.git Merge branch 'master' into release * fixed int parameter sizes and some signed/unsigned warnings * check whether m_port isn't NULL in ReadFromDevice * stop the processor thread and delete the CSerialPort instance on exit * added mutex in CSerialPort * fix segfault on exit * renamed libPlatform -> platform. * stuck everything from libCEC in the CEC namespace to avoid namespace polution --- diff --git a/ChangeLog b/ChangeLog index 10c85f1..7934625 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +libcec (0.4-2) unstable; urgency=low + + * fixed int parameter sizes and some signed/unsigned warnings + * check whether m_port isn't NULL in ReadFromDevice + * stop the processor thread and delete the CSerialPort instance on exit + * added mutex in CSerialPort + * fix segfault on exit + * renamed libPlatform -> platform. + * stuck everything from libCEC in the CEC namespace to avoid namespace + polution + + -- Pulse-Eight Packaging Tue, 04 Oct 2011 23:45:00 +0200 + libcec (0.4-1) unstable; urgency=low * bumped interface version to 4 diff --git a/configure.ac b/configure.ac index bd97334..dffe00b 100644 --- a/configure.ac +++ b/configure.ac @@ -8,5 +8,7 @@ AC_CHECK_LIB([rt], [main],, AC_MSG_ERROR("required library 'rt' is missing")) AC_CHECK_LIB([pthread], [main],, AC_MSG_ERROR("required library 'pthread' is missing")) AC_CHECK_LIB([udev], [main],, AC_MSG_ERROR("required library 'udev' is missing")) +CXXFLAGS="-fPIC -Wall -Wextra $CXXFLAGS" + AC_CONFIG_FILES([src/lib/libcec.pc]) AC_OUTPUT([Makefile src/lib/Makefile src/testclient/Makefile]) diff --git a/debian/changelog b/debian/changelog index 10c85f1..7934625 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,16 @@ +libcec (0.4-2) unstable; urgency=low + + * fixed int parameter sizes and some signed/unsigned warnings + * check whether m_port isn't NULL in ReadFromDevice + * stop the processor thread and delete the CSerialPort instance on exit + * added mutex in CSerialPort + * fix segfault on exit + * renamed libPlatform -> platform. + * stuck everything from libCEC in the CEC namespace to avoid namespace + polution + + -- Pulse-Eight Packaging Tue, 04 Oct 2011 23:45:00 +0200 + libcec (0.4-1) unstable; urgency=low * bumped interface version to 4 diff --git a/include/CECExportsC.h b/include/CECExportsC.h index 61cabd2..b96beba 100644 --- a/include/CECExportsC.h +++ b/include/CECExportsC.h @@ -46,9 +46,9 @@ extern "C" { * @return True when initialised, false otherwise. */ #ifdef __cplusplus -extern DECLSPEC bool cec_init(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, int iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS); +extern DECLSPEC bool cec_init(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, uint8_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS); #else -extern DECLSPEC bool cec_init(const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, int iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS); +extern DECLSPEC bool cec_init(const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, uint8_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS); #endif /*! @@ -62,7 +62,7 @@ extern DECLSPEC void cec_destroy(void); * @param iTimeout Connection timeout in ms. * @return True when connected, false otherwise. */ -extern DECLSPEC bool cec_open(const char *strPort, int iTimeout); +extern DECLSPEC bool cec_open(const char *strPort, uint64_t iTimeout); /*! * @brief Close the connection to the CEC adapter. diff --git a/include/CECExportsCpp.h b/include/CECExportsCpp.h index 54dbcbf..f0a0ac9 100644 --- a/include/CECExportsCpp.h +++ b/include/CECExportsCpp.h @@ -41,7 +41,7 @@ namespace CEC /*! * @see cec_open */ - virtual bool Open(const char *strPort, int iTimeoutMs = 10000) = 0; + virtual bool Open(const char *strPort, uint64_t iTimeoutMs = 10000) = 0; /*! * @see cec_close @@ -122,7 +122,7 @@ namespace CEC }; }; -extern DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, int iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS); +extern DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, uint8_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS); #if !defined(DLL_EXPORT) #if defined(_WIN32) || defined(_WIN64) @@ -135,7 +135,7 @@ static int g_iLibCECInstanceCount = 0; /*! * @see cec_init */ -inline CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, int iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS) +inline CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, uint8_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS) { typedef void* (__cdecl*_CreateLibCec)(const char *, uint8_t, uint8_t); _CreateLibCec CreateLibCec; @@ -149,7 +149,7 @@ inline CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_addres CreateLibCec = (_CreateLibCec) (GetProcAddress(g_libCEC, "CECCreate")); if (!CreateLibCec) return NULL; - return static_cast< CEC::ICECAdapter* > (CreateLibCec(strName, iLogicalAddress, iPhysicalAddress)); + return static_cast< CEC::ICECAdapter* > (CreateLibCec(strName, (uint8_t) iLogicalAddress, iPhysicalAddress)); } /*! @@ -183,6 +183,7 @@ inline CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_addres */ inline void UnloadLibCec(CEC::ICECAdapter *device) { + device->Close(); delete device; }; #endif diff --git a/project/libcec.sln b/project/libcec.sln index 2284eec..2255a1b 100644 --- a/project/libcec.sln +++ b/project/libcec.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 +# Visual C++ Express 2010 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libcec", "libcec.vcxproj", "{C04B0FB1-667D-4F1C-BDAE-A07CDFFAAAA0}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "testclient", "testclient.vcxproj", "{F01222BF-6B3D-43BD-B254-434031CB9887}" @@ -11,12 +11,17 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {C04B0FB1-667D-4F1C-BDAE-A07CDFFAAAA0}.Debug|Win32.ActiveCfg = Debug|Win32 {C04B0FB1-667D-4F1C-BDAE-A07CDFFAAAA0}.Debug|Win32.Build.0 = Debug|Win32 + {C04B0FB1-667D-4F1C-BDAE-A07CDFFAAAA0}.Release|Win32.ActiveCfg = Release|Win32 + {C04B0FB1-667D-4F1C-BDAE-A07CDFFAAAA0}.Release|Win32.Build.0 = Release|Win32 {F01222BF-6B3D-43BD-B254-434031CB9887}.Debug|Win32.ActiveCfg = Debug|Win32 {F01222BF-6B3D-43BD-B254-434031CB9887}.Debug|Win32.Build.0 = Debug|Win32 + {F01222BF-6B3D-43BD-B254-434031CB9887}.Release|Win32.ActiveCfg = Release|Win32 + {F01222BF-6B3D-43BD-B254-434031CB9887}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/project/libcec.vcxproj b/project/libcec.vcxproj index a9edbba..65200d3 100644 --- a/project/libcec.vcxproj +++ b/project/libcec.vcxproj @@ -19,19 +19,18 @@ - - - - - - - - + + + + + + + + + + - - - @@ -40,15 +39,10 @@ - - - - - - - - - + + + + {C04B0FB1-667D-4F1C-BDAE-A07CDFFAAAA0} @@ -98,36 +92,39 @@ - Level3 + Level4 Disabled _CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;_USE_32BIT_TIME_T;_WINSOCKAPI_;__STDC_CONSTANT_MACROS;__WINDOWS__;DLL_EXPORT;%(PreprocessorDefinitions) - $(SolutionDir)\..\src\lib\libPlatform\pthread_win32;%(AdditionalIncludeDirectories) - 4996 + $(SolutionDir)\..\src\lib\platform\pthread_win32;%(AdditionalIncludeDirectories) + 4996;4100;4309 + true true $(SolutionDir)\..\$(TargetName)$(TargetExt) - %(AdditionalDependencies);setupapi.lib + %(AdditionalDependencies);setupapi.lib;..\src\lib\platform\pthread_win32\pthreadVC2d.lib libcmtd 2 - Level3 - MaxSpeed + Level4 + Full true true - $(SolutionDir)\..\src\lib\libPlatform\pthread_win32;%(AdditionalIncludeDirectories) + $(SolutionDir)\..\src\lib\platform\pthread_win32;%(AdditionalIncludeDirectories) _WIN32;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;_USE_32BIT_TIME_T;_WINSOCKAPI_;__STDC_CONSTANT_MACROS;__WINDOWS__;DLL_EXPORT;%(PreprocessorDefinitions) - 4996 + 4100;4309 + true + Speed - true + false true true $(SolutionDir)\..\libcec.dll - ws2_32.lib;$(SolutionDir)\..\src\lib\libPlatform\pthread_win32\pthreadVC2d.lib;%(AdditionalDependencies) + %(AdditionalDependencies);setupapi.lib;..\src\lib\platform\pthread_win32\pthreadVC2.lib libcmt diff --git a/project/libcec.vcxproj.filters b/project/libcec.vcxproj.filters index 2bc976f..7bd7b59 100644 --- a/project/libcec.vcxproj.filters +++ b/project/libcec.vcxproj.filters @@ -15,88 +15,74 @@ util - - util - util - - util + + exports - - platform + + exports - - platform + + exports - - platform + + exports - + + + + + platform - + platform - + platform - - exports + + platform - - exports + + platform - - exports + + platform - + platform - - util + + platform - + platform - - exports + + platform - - - - - - util - - - util - - - platform - - - platform - - - platform - - - - + platform - - + + + platform + + platform - + + + platform + \ No newline at end of file diff --git a/project/testclient.vcxproj b/project/testclient.vcxproj index caa2a03..94c7f24 100644 --- a/project/testclient.vcxproj +++ b/project/testclient.vcxproj @@ -50,54 +50,52 @@ - Level3 + Level4 Disabled WIN32;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;_USE_32BIT_TIME_T;_WINSOCKAPI_;__STDC_CONSTANT_MACROS;__WINDOWS__;%(PreprocessorDefinitions) + true + 4100;4309 Console true - %(AdditionalDependencies) + ..\src\lib\platform\pthread_win32\pthreadVC2d.lib;%(AdditionalDependencies) $(SolutionDir)\..\$(TargetName)$(TargetExt) - Level3 + Level4 - MaxSpeed - true + Full + false true _WIN32;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;_USE_32BIT_TIME_T;_WINSOCKAPI_;__STDC_CONSTANT_MACROS;__WINDOWS__;%(PreprocessorDefinitions) + true + 4100;4309 Console - true + false true true + $(SolutionDir)\..\src\lib\platform\pthread_win32\pthreadVC2.lib;%(AdditionalDependencies) - - - + + + - - - - - + + - - - - diff --git a/project/testclient.vcxproj.filters b/project/testclient.vcxproj.filters index a6fda36..76735fb 100644 --- a/project/testclient.vcxproj.filters +++ b/project/testclient.vcxproj.filters @@ -21,22 +21,16 @@ Header Files - - Header Files - - - Header Files - Header Files - + Header Files - + Header Files - + Header Files @@ -44,22 +38,11 @@ Source Files - + Source Files - + Source Files - - Source Files - - - - - Resource Files - - - Resource Files - \ No newline at end of file diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 266842f..4b0bcdd 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -33,7 +33,7 @@ #include "AdapterCommunication.h" #include "LibCEC.h" -#include "libPlatform/serialport.h" +#include "platform/serialport.h" #include "util/StdString.h" using namespace std; @@ -52,11 +52,13 @@ CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : CAdapterCommunication::~CAdapterCommunication(void) { + StopThread(); m_port->Close(); + 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) @@ -110,23 +112,25 @@ void *CAdapterCommunication::Process(void) break; } - CCondition::Sleep(50); + if (!m_bStop) + CCondition::Sleep(50); } - m_controller->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); + if (!m_port) + return false; + + int32_t iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); lock.Leave(); - if (iBytesRead < 0) + if (iBytesRead < 0 || iBytesRead > 256) { CStdString strError; strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); @@ -134,12 +138,12 @@ bool CAdapterCommunication::ReadFromDevice(int iTimeout) 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) @@ -158,7 +162,7 @@ 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()); @@ -173,7 +177,7 @@ bool CAdapterCommunication::Write(const cec_frame &data) return true; } -bool CAdapterCommunication::Read(cec_frame &msg, int iTimeout) +bool CAdapterCommunication::Read(cec_frame &msg, uint64_t iTimeout) { CLockObject lock(&m_bufferMutex); diff --git a/src/lib/AdapterCommunication.h b/src/lib/AdapterCommunication.h index f7dafa0..c95f729 100644 --- a/src/lib/AdapterCommunication.h +++ b/src/lib/AdapterCommunication.h @@ -32,12 +32,11 @@ */ #include "../../include/CECExports.h" -#include "util/threads.h" - -class CSerialPort; +#include "platform/threads.h" namespace CEC { + class CSerialPort; class CLibCEC; class CAdapterCommunication : CThread @@ -46,8 +45,8 @@ namespace CEC CAdapterCommunication(CLibCEC *controller); virtual ~CAdapterCommunication(); - bool Open(const char *strPort, int iBaudRate = 38400, int iTimeoutMs = 10000); - bool Read(cec_frame &msg, int iTimeout = 1000); + bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint64_t iTimeoutMs = 10000); + bool Read(cec_frame &msg, uint64_t iTimeout = 1000); bool Write(const cec_frame &frame); bool PingAdapter(void); void Close(void); @@ -60,8 +59,8 @@ namespace CEC bool SetAckMask(uint16_t iMask); static void PushEscaped(cec_frame &vec, uint8_t byte); private: - void AddData(uint8_t *data, int iLen); - bool ReadFromDevice(int iTimeout); + void AddData(uint8_t *data, uint8_t iLen); + bool ReadFromDevice(uint64_t iTimeout); CSerialPort * m_port; CLibCEC * m_controller; diff --git a/src/lib/AdapterDetection.cpp b/src/lib/AdapterDetection.cpp index 747ea16..e21d3dc 100644 --- a/src/lib/AdapterDetection.cpp +++ b/src/lib/AdapterDetection.cpp @@ -31,7 +31,7 @@ */ #include "AdapterDetection.h" -#include "libPlatform/os-dependent.h" +#include "platform/os-dependent.h" #include "util/StdString.h" #if !defined(__WINDOWS__) diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index f3b89c3..258e164 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -35,7 +35,7 @@ #include "AdapterCommunication.h" #include "LibCEC.h" #include "util/StdString.h" -#include "util/timeutils.h" +#include "platform/timeutils.h" using namespace CEC; using namespace std; @@ -89,14 +89,16 @@ void *CCECProcessor::Process(void) bParseFrame = ParseMessage(msg); } - if (bParseFrame) + if (!m_bStop && bParseFrame) ParseCurrentFrame(); - m_controller->CheckKeypressTimeout(); - CCondition::Sleep(50); + if (!m_bStop) + { + m_controller->CheckKeypressTimeout(); + CCondition::Sleep(50); + } } - m_controller->AddLog(CEC_LOG_DEBUG, "processor thread terminated"); return NULL; } @@ -110,7 +112,7 @@ bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); cec_frame frame; frame.push_back(GetSourceDestination(address)); - frame.push_back(CEC_OPCODE_TEXT_VIEW_ON); + frame.push_back((uint8_t) CEC_OPCODE_TEXT_VIEW_ON); return Transmit(frame); } @@ -124,7 +126,7 @@ bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BR m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); cec_frame frame; frame.push_back(GetSourceDestination(address)); - frame.push_back(CEC_OPCODE_STANDBY); + frame.push_back((uint8_t) CEC_OPCODE_STANDBY); return Transmit(frame); } @@ -136,7 +138,7 @@ bool CCECProcessor::SetActiveView(void) m_controller->AddLog(CEC_LOG_DEBUG, "setting active view"); cec_frame frame; frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); - frame.push_back(CEC_OPCODE_ACTIVE_SOURCE); + frame.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE); frame.push_back((m_physicaladdress >> 8) & 0xFF); frame.push_back(m_physicaladdress & 0xFF); return Transmit(frame); @@ -150,7 +152,7 @@ bool CCECProcessor::SetInactiveView(void) m_controller->AddLog(CEC_LOG_DEBUG, "setting inactive view"); cec_frame frame; frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); - frame.push_back(CEC_OPCODE_INACTIVE_SOURCE); + frame.push_back((uint8_t) CEC_OPCODE_INACTIVE_SOURCE); frame.push_back((m_physicaladdress >> 8) & 0xFF); frame.push_back(m_physicaladdress & 0xFF); return Transmit(frame); @@ -230,9 +232,9 @@ void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode m_controller->AddLog(CEC_LOG_DEBUG, "transmitting abort message"); cec_frame frame; frame.push_back(GetSourceDestination(address)); - frame.push_back(CEC_OPCODE_FEATURE_ABORT); - frame.push_back(opcode); - frame.push_back(reason); + frame.push_back((uint8_t) CEC_OPCODE_FEATURE_ABORT); + frame.push_back((uint8_t) opcode); + frame.push_back((uint8_t) reason); Transmit(frame); } @@ -241,7 +243,7 @@ void CCECProcessor::ReportCECVersion(cec_logical_address address /* = CECDEVICE_ cec_frame frame; m_controller->AddLog(CEC_LOG_NOTICE, "reporting CEC version as 1.3a"); frame.push_back(GetSourceDestination(address)); - frame.push_back(CEC_OPCODE_CEC_VERSION); + frame.push_back((uint8_t) CEC_OPCODE_CEC_VERSION); frame.push_back(CEC_VERSION_1_3A); Transmit(frame); } @@ -255,8 +257,8 @@ void CCECProcessor::ReportPowerState(cec_logical_address address /*= CECDEVICE_T m_controller->AddLog(CEC_LOG_NOTICE, "reporting \"Off\" power status"); frame.push_back(GetSourceDestination(address)); - frame.push_back(CEC_OPCODE_REPORT_POWER_STATUS); - frame.push_back(bOn ? CEC_POWER_STATUS_ON : CEC_POWER_STATUS_STANDBY); + frame.push_back((uint8_t) CEC_OPCODE_REPORT_POWER_STATUS); + frame.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY); Transmit(frame); } @@ -269,8 +271,8 @@ void CCECProcessor::ReportMenuState(cec_logical_address address /* = CECDEVICE_T m_controller->AddLog(CEC_LOG_NOTICE, "reporting menu state as inactive"); frame.push_back(GetSourceDestination(address)); - frame.push_back(CEC_OPCODE_MENU_STATUS); - frame.push_back(bActive ? CEC_MENU_STATE_ACTIVATED : CEC_MENU_STATE_DEACTIVATED); + frame.push_back((uint8_t) CEC_OPCODE_MENU_STATUS); + frame.push_back(bActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED); Transmit(frame); } @@ -288,7 +290,7 @@ void CCECProcessor::ReportOSDName(cec_logical_address address /* = CECDEVICE_TV strLog.Format("reporting OSD name as %s", osdname); m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); frame.push_back(GetSourceDestination(address)); - frame.push_back(CEC_OPCODE_SET_OSD_NAME); + frame.push_back((uint8_t) CEC_OPCODE_SET_OSD_NAME); for (unsigned int i = 0; i < strlen(osdname); i++) frame.push_back(osdname[i]); @@ -303,7 +305,7 @@ void CCECProcessor::ReportPhysicalAddress(void) strLog.Format("reporting physical address as %04x", m_physicaladdress); m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); - frame.push_back(CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); + frame.push_back((uint8_t) CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); frame.push_back((m_physicaladdress >> 8) & 0xFF); frame.push_back(m_physicaladdress & 0xFF); frame.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); @@ -315,7 +317,7 @@ void CCECProcessor::BroadcastActiveSource(void) cec_frame frame; m_controller->AddLog(CEC_LOG_NOTICE, "broadcasting active source"); frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); - frame.push_back(CEC_OPCODE_ACTIVE_SOURCE); + frame.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE); frame.push_back((m_physicaladdress >> 8) & 0xFF); frame.push_back(m_physicaladdress & 0xFF); Transmit(frame); diff --git a/src/lib/CECProcessor.h b/src/lib/CECProcessor.h index 339653c..aea17c8 100644 --- a/src/lib/CECProcessor.h +++ b/src/lib/CECProcessor.h @@ -33,7 +33,7 @@ #include "../../include/CECExports.h" #include "../../include/CECTypes.h" -#include "util/threads.h" +#include "platform/threads.h" #include "util/buffer.h" class CSerialPort; diff --git a/src/lib/LibCEC.cpp b/src/lib/LibCEC.cpp index 23c4b18..3437e45 100644 --- a/src/lib/LibCEC.cpp +++ b/src/lib/LibCEC.cpp @@ -36,12 +36,12 @@ #include "AdapterDetection.h" #include "CECProcessor.h" #include "util/StdString.h" -#include "util/timeutils.h" +#include "platform/timeutils.h" using namespace std; using namespace CEC; -CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) : +CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint8_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) : m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN), m_buttontime(0) { @@ -58,7 +58,7 @@ CLibCEC::~CLibCEC(void) m_comm = NULL; } -bool CLibCEC::Open(const char *strPort, int iTimeoutMs /* = 10000 */) +bool CLibCEC::Open(const char *strPort, uint64_t iTimeoutMs /* = 10000 */) { if (!m_comm) return false; @@ -87,9 +87,17 @@ bool CLibCEC::Open(const char *strPort, int iTimeoutMs /* = 10000 */) void CLibCEC::Close(void) { if (m_cec) + { m_cec->StopThread(); + delete m_cec; + m_cec = NULL; + } if (m_comm) + { m_comm->Close(); + delete m_comm; + m_comm = NULL; + } } int CLibCEC::FindAdapters(std::vector &deviceList, const char *strDevicePath /* = NULL */) @@ -225,7 +233,7 @@ void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode) m_buttontime = GetTimeMs(); } -DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) +DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint8_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) { return static_cast< void* > (new CLibCEC(strDeviceName, iLogicalAddress, iPhysicalAddress)); } diff --git a/src/lib/LibCEC.h b/src/lib/LibCEC.h index b863cb4..cb9eb8c 100644 --- a/src/lib/LibCEC.h +++ b/src/lib/LibCEC.h @@ -47,10 +47,10 @@ namespace CEC * ICECAdapter implementation */ //@{ - CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, int iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS); + CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, uint8_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS); virtual ~CLibCEC(void); - virtual bool Open(const char *strPort, int iTimeout = 10000); + virtual bool Open(const char *strPort, uint64_t iTimeout = 10000); virtual void Close(void); virtual int FindAdapters(std::vector &deviceList, const char *strDevicePath = NULL); virtual bool PingAdapter(void); diff --git a/src/lib/LibCECC.cpp b/src/lib/LibCECC.cpp index 7f2475b..60ada61 100644 --- a/src/lib/LibCECC.cpp +++ b/src/lib/LibCECC.cpp @@ -41,7 +41,7 @@ using namespace std; //@{ ICECAdapter *cec_parser; -bool cec_init(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) +bool cec_init(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint8_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) { cec_parser = (ICECAdapter *) CECCreate(strDeviceName, iLogicalAddress, iPhysicalAddress); return (cec_parser != NULL); @@ -54,7 +54,7 @@ void cec_destroy(void) cec_parser = NULL; } -bool cec_open(const char *strPort, int iTimeout) +bool cec_open(const char *strPort, uint64_t iTimeout) { if (cec_parser) return cec_parser->Open(strPort, iTimeout); diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 47664b8..3522cb5 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -20,16 +20,14 @@ libcec_la_SOURCES = AdapterCommunication.cpp \ ../../include/CECExports.h \ ../../include/CECExportsCpp.h \ ../../include/CECExportsC.h \ - util/misc.cpp \ - util/misc.h \ util/StdString.h \ - util/threads.cpp \ - util/threads.h \ - util/timeutils.h \ - libPlatform/baudrate.h \ - libPlatform/os-dependent.h \ - libPlatform/linux/os_posix.h \ - libPlatform/linux/serialport.cpp \ - libPlatform/serialport.h + platform/timeutils.h \ + platform/baudrate.h \ + platform/os-dependent.h \ + platform/linux/os_posix.h \ + platform/linux/serialport.cpp \ + platform/serialport.h \ + platform/threads.cpp \ + platform/threads.h libcec_la_LDFLAGS = -lrt -lpthread -ludev -version-info @VERSION@ diff --git a/src/lib/libPlatform/baudrate.h b/src/lib/libPlatform/baudrate.h deleted file mode 100644 index 39a4b1a..0000000 --- a/src/lib/libPlatform/baudrate.h +++ /dev/null @@ -1,179 +0,0 @@ -#pragma once - -/* - * boblight - * Copyright (C) Bob 2009 - * - * boblight is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * boblight is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -//every baudrate I could find is in here in an #ifdef block -//so it should compile on everything - -static struct sbaudrate -{ - int rate; - int symbol; -} -baudrates[] = -{ -#ifdef B50 - { 50, B50 }, -#endif -#ifdef B75 - { 75, B75 }, -#endif -#ifdef B110 - { 110, B110 }, -#endif -#ifdef B134 - { 134, B134 }, -#endif -#ifdef B150 - { 150, B150 }, -#endif -#ifdef B200 - { 200, B200 }, -#endif -#ifdef B300 - { 300, B300 }, -#endif -#ifdef B600 - { 600, B600 }, -#endif -#ifdef B1200 - { 1200, B1200 }, -#endif -#ifdef B1800 - { 1800, B1800 }, -#endif -#ifdef B2400 - { 2400, B2400 }, -#endif -#ifdef B4800 - { 4800, B4800 }, -#endif -#ifdef B9600 - { 9600, B9600 }, -#endif -#ifdef B14400 - { 14400, B14400 }, -#endif -#ifdef B19200 - { 19200, B19200 }, -#endif -#ifdef B28800 - { 28800, B28800 }, -#endif -#ifdef B38400 - { 38400, B38400 }, -#endif -#ifdef B57600 - { 57600, B57600 }, -#endif -#ifdef B76800 - { 76800, B76800 }, -#endif -#ifdef B115200 - { 115200, B115200 }, -#endif -#ifdef B230400 - { 230400, B230400 }, -#endif -#ifdef B250000 - { 250000, B250000 }, -#endif -#ifdef B460800 - { 460800, B460800 }, -#endif -#ifdef B500000 - { 500000, B500000 }, -#endif -#ifdef B576000 - { 576000, B576000 }, -#endif -#ifdef B921600 - { 921600, B921600 }, -#endif -#ifdef B1000000 - { 1000000, B1000000 }, -#endif -#ifdef B1152000 - { 1152000, B1152000 }, -#endif -#ifdef B1500000 - { 1500000, B1500000 }, -#endif -#ifdef B2000000 - { 2000000, B2000000 }, -#endif -#ifdef B2500000 - { 2500000, B2500000 }, -#endif -#ifdef B3000000 - { 3000000, B3000000 }, -#endif -#ifdef B3500000 - { 3500000, B3500000 }, -#endif -#ifdef B4000000 - { 4000000, B4000000 }, -#endif -#ifdef CBR_110 - { 110, CBR_110 }, -#endif -#ifdef CBR_300 - { 300, CBR_300 }, -#endif -#ifdef CBR_600 - { 600, CBR_600 }, -#endif -#ifdef CBR_1200 - { 1200, CBR_1200 }, -#endif -#ifdef CBR_2400 - { 2400, CBR_2400 }, -#endif -#ifdef CBR_4800 - { 4800, CBR_4800 }, -#endif -#ifdef CBR_9600 - { 9600, CBR_9600 }, -#endif -#ifdef CBR_11400 - { 11400, CBR_14400 }, -#endif -#ifdef CBR_19200 - { 19200, CBR_19200 }, -#endif -#ifdef CBR_38400 - { 38400, CBR_38400 }, -#endif -#ifdef CBR_56000 - { 56000, CBR_56000 }, -#endif -#ifdef CBR_57600 - { 57600, CBR_57600 }, -#endif -#ifdef CBR_115200 - { 115200, CBR_115200 }, -#endif -#ifdef CBR_128000 - { 128000, CBR_128000 }, -#endif -#ifdef CBR_256000 - { 256000, CBR_256000 }, -#endif - { -1, -1} -}; diff --git a/src/lib/libPlatform/serialport.h b/src/lib/libPlatform/serialport.h deleted file mode 100644 index f592ee3..0000000 --- a/src/lib/libPlatform/serialport.h +++ /dev/null @@ -1,107 +0,0 @@ -#pragma once - -/* - * boblight - * Copyright (C) Bob 2009 - * - * boblight is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * boblight is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "os-dependent.h" -#include -#include -#include - -#ifndef __WINDOWS__ -#include -#else -#include "../util/threads.h" -#include "../util/buffer.h" - -class CSerialPort; - -typedef struct serial_cancel_options -{ - CSerialPort *instance; - uint64_t iWaitMs; -} serial_cancel_options; - -#endif - -#define PAR_NONE 0 -#define PAR_EVEN 1 -#define PAR_ODD 2 - -#include "baudrate.h" - -class CSerialPort -{ - public: - CSerialPort(); - virtual ~CSerialPort(); - - bool Open(std::string name, int baudrate, int databits = 8, int stopbits = 1, int parity = PAR_NONE); - void Close(); - int Write(std::vector data) - { - return Write(&data[0], data.size()); - } - - int Write(uint8_t* data, int len); - int Read(uint8_t* data, int len, int iTimeoutMs = -1); - - std::string GetError() { return m_name + ": " + m_error; } - std::string GetName() { return m_name; } - - bool SetBaudRate(int baudrate); - - int IntToRate(int baudrate) - { - for (unsigned int i = 0; i < sizeof(baudrates) / sizeof(sbaudrate) - 1; i++) - { - if (baudrates[i].rate == baudrate) - { - return baudrates[i].symbol; - } - } - return -1; - }; - -#ifdef __WINDOWS__ - bool IsOpen() const { return m_bIsOpen; } -#else - bool IsOpen() const { return m_fd != -1; } -#endif - -private: - std::string m_error; - std::string m_name; - -#ifdef __WINDOWS__ - bool SetTimeouts(bool bBlocking); - - HANDLE m_handle; - bool m_bIsOpen; - int m_iBaudrate; - int m_iDatabits; - int m_iStopbits; - int m_iParity; - int64_t m_iTimeout; - CecBuffer m_buffer; - HANDLE m_ovHandle; -#else - struct termios m_options; - int m_fd; -#endif -}; diff --git a/src/lib/platform/baudrate.h b/src/lib/platform/baudrate.h new file mode 100644 index 0000000..759b1f1 --- /dev/null +++ b/src/lib/platform/baudrate.h @@ -0,0 +1,199 @@ +#pragma once + +/* + * boblight + * Copyright (C) Bob 2009 + * + * boblight is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * boblight is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +//every baudrate I could find is in here in an #ifdef block +//so it should compile on everything + +#include "os-dependent.h" + +#ifndef __WINDOWS__ +#include +#endif + +namespace CEC +{ + static struct sbaudrate + { + int32_t rate; + int32_t symbol; + } + baudrates[] = + { + #ifdef B50 + { 50, B50 }, + #endif + #ifdef B75 + { 75, B75 }, + #endif + #ifdef B110 + { 110, B110 }, + #endif + #ifdef B134 + { 134, B134 }, + #endif + #ifdef B150 + { 150, B150 }, + #endif + #ifdef B200 + { 200, B200 }, + #endif + #ifdef B300 + { 300, B300 }, + #endif + #ifdef B600 + { 600, B600 }, + #endif + #ifdef B1200 + { 1200, B1200 }, + #endif + #ifdef B1800 + { 1800, B1800 }, + #endif + #ifdef B2400 + { 2400, B2400 }, + #endif + #ifdef B4800 + { 4800, B4800 }, + #endif + #ifdef B9600 + { 9600, B9600 }, + #endif + #ifdef B14400 + { 14400, B14400 }, + #endif + #ifdef B19200 + { 19200, B19200 }, + #endif + #ifdef B28800 + { 28800, B28800 }, + #endif + #ifdef B38400 + { 38400, B38400 }, + #endif + #ifdef B57600 + { 57600, B57600 }, + #endif + #ifdef B76800 + { 76800, B76800 }, + #endif + #ifdef B115200 + { 115200, B115200 }, + #endif + #ifdef B230400 + { 230400, B230400 }, + #endif + #ifdef B250000 + { 250000, B250000 }, + #endif + #ifdef B460800 + { 460800, B460800 }, + #endif + #ifdef B500000 + { 500000, B500000 }, + #endif + #ifdef B576000 + { 576000, B576000 }, + #endif + #ifdef B921600 + { 921600, B921600 }, + #endif + #ifdef B1000000 + { 1000000, B1000000 }, + #endif + #ifdef B1152000 + { 1152000, B1152000 }, + #endif + #ifdef B1500000 + { 1500000, B1500000 }, + #endif + #ifdef B2000000 + { 2000000, B2000000 }, + #endif + #ifdef B2500000 + { 2500000, B2500000 }, + #endif + #ifdef B3000000 + { 3000000, B3000000 }, + #endif + #ifdef B3500000 + { 3500000, B3500000 }, + #endif + #ifdef B4000000 + { 4000000, B4000000 }, + #endif + #ifdef CBR_110 + { 110, CBR_110 }, + #endif + #ifdef CBR_300 + { 300, CBR_300 }, + #endif + #ifdef CBR_600 + { 600, CBR_600 }, + #endif + #ifdef CBR_1200 + { 1200, CBR_1200 }, + #endif + #ifdef CBR_2400 + { 2400, CBR_2400 }, + #endif + #ifdef CBR_4800 + { 4800, CBR_4800 }, + #endif + #ifdef CBR_9600 + { 9600, CBR_9600 }, + #endif + #ifdef CBR_11400 + { 11400, CBR_14400 }, + #endif + #ifdef CBR_19200 + { 19200, CBR_19200 }, + #endif + #ifdef CBR_38400 + { 38400, CBR_38400 }, + #endif + #ifdef CBR_56000 + { 56000, CBR_56000 }, + #endif + #ifdef CBR_57600 + { 57600, CBR_57600 }, + #endif + #ifdef CBR_115200 + { 115200, CBR_115200 }, + #endif + #ifdef CBR_128000 + { 128000, CBR_128000 }, + #endif + #ifdef CBR_256000 + { 256000, CBR_256000 }, + #endif + { -1, -1} + }; + + static int32_t IntToBaudrate(uint32_t baudrate) + { + for (unsigned int i = 0; i < sizeof(baudrates) / sizeof(CEC::sbaudrate) - 1; i++) + { + if (baudrates[i].rate == (int32_t) baudrate) + return baudrates[i].symbol; + } + + return -1; + }; +}; diff --git a/src/lib/libPlatform/linux/os_posix.h b/src/lib/platform/linux/os_posix.h similarity index 56% rename from src/lib/libPlatform/linux/os_posix.h rename to src/lib/platform/linux/os_posix.h index 1ea8aa5..9cc4d09 100644 --- a/src/lib/libPlatform/linux/os_posix.h +++ b/src/lib/platform/linux/os_posix.h @@ -19,19 +19,9 @@ */ #define _FILE_OFFSET_BITS 64 -#include -#include -#include #include -#include -#include #include -#include -#include #include -#include -#include -#include #ifndef __APPLE__ #include #endif @@ -41,20 +31,3 @@ #define LIBTYPE #define DECLSPEC - -#ifndef _STL_ALGOBASE_H -template inline T min(T a, T b) { return a <= b ? a : b; } -template inline T max(T a, T b) { return a >= b ? a : b; } -template inline int sgn(T a) { return a < 0 ? -1 : a > 0 ? 1 : 0; } -template inline void swap(T &a, T &b) { T t = a; a = b; b = t; } -#endif - -/*! - * @return the current time in seconds since unix epoch. - */ -static inline uint64_t getcurrenttime(void) -{ - struct timeval t; - gettimeofday(&t, NULL); - return ((uint64_t)t.tv_sec * 1000) + (t.tv_usec / 1000); -} diff --git a/src/lib/libPlatform/linux/serialport.cpp b/src/lib/platform/linux/serialport.cpp similarity index 80% rename from src/lib/libPlatform/linux/serialport.cpp rename to src/lib/platform/linux/serialport.cpp index 33a7385..e8fb8e5 100644 --- a/src/lib/libPlatform/linux/serialport.cpp +++ b/src/lib/platform/linux/serialport.cpp @@ -16,22 +16,18 @@ * with this program. If not, see . */ -#include //debug - #include -#include #include - #include "../serialport.h" #include "../baudrate.h" -#include "../../util/misc.h" -#include "../../util/timeutils.h" +#include "../timeutils.h" using namespace std; +using namespace CEC; CSerialPort::CSerialPort() { - m_fd = -1; + m_fd = -1; } CSerialPort::~CSerialPort() @@ -39,33 +35,34 @@ CSerialPort::~CSerialPort() Close(); } -int CSerialPort::Write(uint8_t* data, int len) +int32_t CSerialPort::Write(uint8_t* data, uint32_t len) { fd_set port; - + + CLockObject lock(&m_mutex); if (m_fd == -1) { m_error = "port closed"; return -1; } - int byteswritten = 0; + int32_t byteswritten = 0; - while (byteswritten < len) + while (byteswritten < (int32_t) len) { FD_ZERO(&port); FD_SET(m_fd, &port); int returnv = select(m_fd + 1, NULL, &port, NULL, NULL); if (returnv == -1) { - m_error = GetErrno(); + m_error = strerror(errno); return -1; } returnv = write(m_fd, data + byteswritten, len - byteswritten); if (returnv == -1) { - m_error = GetErrno(); + m_error = strerror(errno); return -1; } byteswritten += returnv; @@ -84,28 +81,29 @@ int CSerialPort::Write(uint8_t* data, int len) return byteswritten; } -int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /*= -1*/) +int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /*= 0*/) { fd_set port; struct timeval timeout, *tv; - int64_t now, target; - int bytesread = 0; + int64_t now(0), target(0); + int32_t bytesread = 0; + CLockObject lock(&m_mutex); if (m_fd == -1) { m_error = "port closed"; return -1; } - if (iTimeoutMs >= 0) + if (iTimeoutMs > 0) { now = GetTimeMs(); target = now + (int64_t) iTimeoutMs; } - while (bytesread < len && (iTimeoutMs < 0 || target > now)) + while (bytesread < (int32_t) len && (iTimeoutMs == 0 || target > now)) { - if (iTimeoutMs < 0) + if (iTimeoutMs == 0) { tv = NULL; } @@ -118,11 +116,11 @@ int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /*= -1*/) FD_ZERO(&port); FD_SET(m_fd, &port); - int returnv = select(m_fd + 1, &port, NULL, NULL, tv); + int32_t returnv = select(m_fd + 1, &port, NULL, NULL, tv); if (returnv == -1) { - m_error = GetErrno(); + m_error = strerror(errno); return -1; } else if (returnv == 0) @@ -133,7 +131,7 @@ int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /*= -1*/) returnv = read(m_fd, data + bytesread, len - bytesread); if (returnv == -1) { - m_error = GetErrno(); + m_error = strerror(errno); return -1; } @@ -157,11 +155,12 @@ int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /*= -1*/) } //setting all this stuff up is a pain in the ass -bool CSerialPort::Open(string name, int baudrate, int databits/* = 8*/, int stopbits/* = 1*/, int parity/* = PAR_NONE*/) +bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits /* = 8 */, uint8_t stopbits /* = 1 */, uint8_t parity /* = PAR_NONE */) { m_name = name; - m_error = GetErrno(); - + m_error = strerror(errno); + CLockObject lock(&m_mutex); + if (databits < 5 || databits > 8) { m_error = "Databits has to be between 5 and 8"; @@ -184,7 +183,7 @@ bool CSerialPort::Open(string name, int baudrate, int databits/* = 8*/, int stop if (m_fd == -1) { - m_error = GetErrno(); + m_error = strerror(errno); return false; } @@ -236,7 +235,7 @@ bool CSerialPort::Open(string name, int baudrate, int databits/* = 8*/, int stop if (tcsetattr(m_fd, TCSANOW, &m_options) != 0) { - m_error = GetErrno(); + m_error = strerror(errno); return false; } @@ -248,6 +247,7 @@ bool CSerialPort::Open(string name, int baudrate, int databits/* = 8*/, int stop void CSerialPort::Close() { + CLockObject lock(&m_mutex); if (m_fd != -1) { close(m_fd); @@ -257,9 +257,9 @@ void CSerialPort::Close() } } -bool CSerialPort::SetBaudRate(int baudrate) +bool CSerialPort::SetBaudRate(uint32_t baudrate) { - int rate = IntToRate(baudrate); + int rate = IntToBaudrate(baudrate); if (rate == -1) { char buff[255]; @@ -271,21 +271,27 @@ bool CSerialPort::SetBaudRate(int baudrate) //get the current port attributes if (tcgetattr(m_fd, &m_options) != 0) { - m_error = GetErrno(); + m_error = strerror(errno); return false; } if (cfsetispeed(&m_options, rate) != 0) { - m_error = GetErrno(); + m_error = strerror(errno); return false; } if (cfsetospeed(&m_options, rate) != 0) { - m_error = GetErrno(); + m_error = strerror(errno); return false; } return true; } + +bool CSerialPort::IsOpen() +{ + CLockObject lock(&m_mutex); + return m_fd != -1; +} diff --git a/src/lib/libPlatform/os-dependent.h b/src/lib/platform/os-dependent.h similarity index 100% rename from src/lib/libPlatform/os-dependent.h rename to src/lib/platform/os-dependent.h diff --git a/src/lib/libPlatform/pthread_win32/pthread.h b/src/lib/platform/pthread_win32/pthread.h similarity index 100% rename from src/lib/libPlatform/pthread_win32/pthread.h rename to src/lib/platform/pthread_win32/pthread.h diff --git a/src/lib/libPlatform/pthread_win32/pthreadVC2.lib b/src/lib/platform/pthread_win32/pthreadVC2.lib similarity index 100% rename from src/lib/libPlatform/pthread_win32/pthreadVC2.lib rename to src/lib/platform/pthread_win32/pthreadVC2.lib diff --git a/src/lib/libPlatform/pthread_win32/pthreadVC2d.lib b/src/lib/platform/pthread_win32/pthreadVC2d.lib similarity index 100% rename from src/lib/libPlatform/pthread_win32/pthreadVC2d.lib rename to src/lib/platform/pthread_win32/pthreadVC2d.lib diff --git a/src/lib/libPlatform/pthread_win32/sched.h b/src/lib/platform/pthread_win32/sched.h similarity index 100% rename from src/lib/libPlatform/pthread_win32/sched.h rename to src/lib/platform/pthread_win32/sched.h diff --git a/src/lib/libPlatform/pthread_win32/semaphore.h b/src/lib/platform/pthread_win32/semaphore.h similarity index 100% rename from src/lib/libPlatform/pthread_win32/semaphore.h rename to src/lib/platform/pthread_win32/semaphore.h diff --git a/src/lib/platform/serialport.h b/src/lib/platform/serialport.h new file mode 100644 index 0000000..ea6cdea --- /dev/null +++ b/src/lib/platform/serialport.h @@ -0,0 +1,83 @@ +#pragma once + +/* + * boblight + * Copyright (C) Bob 2009 + * + * boblight is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * boblight is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "os-dependent.h" +#include +#include +#include +#include "../platform/threads.h" + +#ifndef __WINDOWS__ +#include +#else +#include "../util/buffer.h" +#endif + +namespace CEC +{ + #define PAR_NONE 0 + #define PAR_EVEN 1 + #define PAR_ODD 2 + + class CSerialPort + { + public: + CSerialPort(); + virtual ~CSerialPort(); + + bool Open(std::string name, uint32_t baudrate, uint8_t databits = 8, uint8_t stopbits = 1, uint8_t parity = PAR_NONE); + bool IsOpen(); + void Close(); + + int32_t Write(std::vector data) + { + return Write(&data[0], (uint32_t) data.size()); + } + int32_t Write(uint8_t* data, uint32_t len); + int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0); + + std::string GetError() { return m_error; } + std::string GetName() { return m_name; } + + private: + bool SetBaudRate(uint32_t baudrate); + + std::string m_error; + std::string m_name; + CMutex m_mutex; + + #ifdef __WINDOWS__ + bool SetTimeouts(bool bBlocking); + + HANDLE m_handle; + bool m_bIsOpen; + uint32_t m_iBaudrate; + uint8_t m_iDatabits; + uint8_t m_iStopbits; + uint8_t m_iParity; + int64_t m_iTimeout; + CecBuffer m_buffer; + HANDLE m_ovHandle; + #else + struct termios m_options; + int m_fd; + #endif + }; +}; diff --git a/src/lib/util/threads.cpp b/src/lib/platform/threads.cpp similarity index 98% rename from src/lib/util/threads.cpp rename to src/lib/platform/threads.cpp index 58aff36..7c5b6ce 100644 --- a/src/lib/util/threads.cpp +++ b/src/lib/platform/threads.cpp @@ -33,6 +33,8 @@ #include "threads.h" #include "timeutils.h" +using namespace CEC; + CMutex::CMutex(void) { pthread_mutex_init(&m_mutex, NULL); @@ -118,7 +120,7 @@ void CCondition::Sleep(int iTimeout) CCondition w; CMutex m; CLockObject lock(&m); - w.Wait(&m, iTimeout); + w.Wait(&m, int64_t(iTimeout)); } CThread::CThread(void) : diff --git a/src/lib/util/threads.h b/src/lib/platform/threads.h similarity index 53% rename from src/lib/util/threads.h rename to src/lib/platform/threads.h index d1b6239..4ae5ac7 100644 --- a/src/lib/util/threads.h +++ b/src/lib/platform/threads.h @@ -31,67 +31,70 @@ * http://www.pulse-eight.net/ */ -#include "../libPlatform/os-dependent.h" +#include "os-dependent.h" -class CMutex; - -class CCondition +namespace CEC { -public: - CCondition(void); - virtual ~CCondition(void); + class CMutex; - void Signal(void); - bool Wait(CMutex *mutex, int64_t iTimeout); - static void Sleep(int iTimeout); + class CCondition + { + public: + CCondition(void); + virtual ~CCondition(void); -private: - pthread_cond_t m_cond; -}; + void Signal(void); + bool Wait(CMutex *mutex, int64_t iTimeout); + static void Sleep(int iTimeout); -class CMutex -{ -public: - CMutex(void); - virtual ~CMutex(void); + private: + pthread_cond_t m_cond; + }; - bool TryLock(void); - bool Lock(void); - void Unlock(void); + class CMutex + { + public: + CMutex(void); + virtual ~CMutex(void); - pthread_mutex_t m_mutex; -}; + bool TryLock(void); + bool Lock(void); + void Unlock(void); -class CLockObject -{ -public: - CLockObject(CMutex *mutex); - ~CLockObject(void); + pthread_mutex_t m_mutex; + }; - bool IsLocked(void) const { return m_bLocked; } - void Leave(void); - void Lock(void); + class CLockObject + { + public: + CLockObject(CMutex *mutex); + ~CLockObject(void); -private: - CMutex *m_mutex; - bool m_bLocked; -}; + bool IsLocked(void) const { return m_bLocked; } + void Leave(void); + void Lock(void); -class CThread -{ -public: - CThread(void); - virtual ~CThread(void); + private: + CMutex *m_mutex; + bool m_bLocked; + }; + + class CThread + { + public: + CThread(void); + virtual ~CThread(void); - virtual bool IsRunning(void) const { return m_bRunning; } - virtual bool CreateThread(void); - virtual void StopThread(bool bWaitForExit = true); + virtual bool IsRunning(void) const { return m_bRunning; } + virtual bool CreateThread(void); + virtual void StopThread(bool bWaitForExit = true); - static void *ThreadHandler(CThread *thread); - virtual void *Process(void) = 0; + static void *ThreadHandler(CThread *thread); + virtual void *Process(void) = 0; -protected: - pthread_t m_thread; - bool m_bRunning; - bool m_bStop; + protected: + pthread_t m_thread; + bool m_bRunning; + bool m_bStop; + }; }; diff --git a/src/lib/util/timeutils.h b/src/lib/platform/timeutils.h similarity index 55% rename from src/lib/util/timeutils.h rename to src/lib/platform/timeutils.h index f1c0e63..c414020 100644 --- a/src/lib/util/timeutils.h +++ b/src/lib/platform/timeutils.h @@ -21,32 +21,33 @@ #include #include -inline int64_t GetTimeMs() +namespace CEC { -#ifdef __WINDOWS__ - time_t rawtime; - time(&rawtime); - - LARGE_INTEGER tickPerSecond; - LARGE_INTEGER tick; - if (QueryPerformanceFrequency(&tickPerSecond)) + inline int64_t GetTimeMs() { - QueryPerformanceCounter(&tick); - return (int64_t) (tick.QuadPart / 1000.); - } - return -1; -#else - struct timespec time; - clock_gettime(CLOCK_MONOTONIC, &time); + #ifdef __WINDOWS__ + time_t rawtime; + time(&rawtime); - return ((int64_t)time.tv_sec * (int64_t)1000) + (int64_t)time.tv_nsec / (int64_t)1000; -#endif -} + LARGE_INTEGER tickPerSecond; + LARGE_INTEGER tick; + if (QueryPerformanceFrequency(&tickPerSecond)) + { + QueryPerformanceCounter(&tick); + return (int64_t) (tick.QuadPart / 1000.); + } + return -1; + #else + struct timespec time; + clock_gettime(CLOCK_MONOTONIC, &time); -template -inline T GetTimeSec() -{ - return (T)GetTimeMs() / (T)1000.0; -} + return ((int64_t)time.tv_sec * (int64_t)1000) + (int64_t)time.tv_nsec / (int64_t)1000; + #endif + } -void USleep(int64_t usecs, volatile bool* stop = NULL); + template + inline T GetTimeSec() + { + return (T)GetTimeMs() / (T)1000.0; + } +}; diff --git a/src/lib/libPlatform/windows/dlfcn-win32.cpp b/src/lib/platform/windows/dlfcn-win32.cpp similarity index 100% rename from src/lib/libPlatform/windows/dlfcn-win32.cpp rename to src/lib/platform/windows/dlfcn-win32.cpp diff --git a/src/lib/libPlatform/windows/dlfcn-win32.h b/src/lib/platform/windows/dlfcn-win32.h similarity index 100% rename from src/lib/libPlatform/windows/dlfcn-win32.h rename to src/lib/platform/windows/dlfcn-win32.h diff --git a/src/lib/libPlatform/windows/os_windows.cpp b/src/lib/platform/windows/os_windows.cpp similarity index 100% rename from src/lib/libPlatform/windows/os_windows.cpp rename to src/lib/platform/windows/os_windows.cpp diff --git a/src/lib/libPlatform/windows/os_windows.h b/src/lib/platform/windows/os_windows.h similarity index 100% rename from src/lib/libPlatform/windows/os_windows.h rename to src/lib/platform/windows/os_windows.h diff --git a/src/lib/libPlatform/windows/serialport.cpp b/src/lib/platform/windows/serialport.cpp similarity index 85% rename from src/lib/libPlatform/windows/serialport.cpp rename to src/lib/platform/windows/serialport.cpp index f4b8c62..ce299cf 100644 --- a/src/lib/libPlatform/windows/serialport.cpp +++ b/src/lib/platform/windows/serialport.cpp @@ -32,9 +32,10 @@ #include "../serialport.h" #include "../baudrate.h" -#include "../../util/timeutils.h" +#include "../timeutils.h" using namespace std; +using namespace CEC; void FormatWindowsError(int iErrorCode, string &strMessage) { @@ -62,8 +63,9 @@ CSerialPort::~CSerialPort(void) Close(); } -bool CSerialPort::Open(string name, int baudrate, int databits, int stopbits, int parity) +bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity) { + CLockObject lock(&m_mutex); m_handle = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (m_handle == INVALID_HANDLE_VALUE) { @@ -156,6 +158,7 @@ bool CSerialPort::SetTimeouts(bool bBlocking) void CSerialPort::Close(void) { + CLockObject lock(&m_mutex); if (m_bIsOpen) { CloseHandle(m_handle); @@ -163,8 +166,9 @@ void CSerialPort::Close(void) } } -int CSerialPort::Write(uint8_t* data, int len) +int32_t CSerialPort::Write(uint8_t* data, uint32_t len) { + CLockObject lock(&m_mutex); DWORD iBytesWritten = 0; if (!m_bIsOpen) return -1; @@ -176,36 +180,46 @@ int CSerialPort::Write(uint8_t* data, int len) return -1; } - return (int) iBytesWritten; + return iBytesWritten; } -int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /* = -1 */) +int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /* = 0 */) { + CLockObject lock(&m_mutex); + int32_t iReturn(-1); DWORD iBytesRead = 0; if (m_handle == 0) { m_error = "Error while reading from COM port: invalid handle"; - return -1; + return iReturn; } if(!ReadFile(m_handle, data, len, &iBytesRead, NULL) != 0) { m_error = "unable to read from device"; FormatWindowsError(GetLastError(), m_error); - iBytesRead = -1; + iReturn = -1; + } + else + { + iReturn = (int32_t) iBytesRead; } - return (int) iBytesRead; + return iReturn; } -bool CSerialPort::SetBaudRate(int baudrate) +bool CSerialPort::SetBaudRate(uint32_t baudrate) { - m_iBaudrate = baudrate; + int32_t rate = IntToBaudrate(baudrate); + if (rate < 0) + m_iBaudrate = baudrate > 0 ? baudrate : 0; + else + m_iBaudrate = rate; DCB dcb; memset(&dcb,0,sizeof(dcb)); dcb.DCBlength = sizeof(dcb); - dcb.BaudRate = IntToRate(m_iBaudrate); + dcb.BaudRate = IntToBaudrate(m_iBaudrate); dcb.fBinary = true; dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fRtsControl = RTS_CONTROL_DISABLE; @@ -238,3 +252,9 @@ bool CSerialPort::SetBaudRate(int baudrate) return true; } + +bool CSerialPort::IsOpen() +{ + CLockObject lock(&m_mutex); + return m_bIsOpen; +} diff --git a/src/lib/util/buffer.h b/src/lib/util/buffer.h index cafa655..d49de62 100644 --- a/src/lib/util/buffer.h +++ b/src/lib/util/buffer.h @@ -31,46 +31,49 @@ * http://www.pulse-eight.net/ */ -#include "threads.h" +#include "../platform/threads.h" #include -template - struct CecBuffer - { - public: - CecBuffer(int iMaxSize = 100) +namespace CEC +{ + template + struct CecBuffer { - m_maxSize = iMaxSize; - } - virtual ~CecBuffer(void) {} + public: + CecBuffer(unsigned int iMaxSize = 100) + { + m_maxSize = iMaxSize; + } + virtual ~CecBuffer(void) {} - int Size(void) const { return m_buffer.size(); } + int Size(void) const { return m_buffer.size(); } - bool Push(_BType entry) - { - CLockObject lock(&m_mutex); - if (m_buffer.size() == m_maxSize) - return false; + bool Push(_BType entry) + { + CLockObject lock(&m_mutex); + if (m_buffer.size() == m_maxSize) + return false; - m_buffer.push(entry); - return true; - } + m_buffer.push(entry); + return true; + } - bool Pop(_BType &entry) - { - bool bReturn(false); - CLockObject lock(&m_mutex); - if (m_buffer.size() > 0) + bool Pop(_BType &entry) { - entry = m_buffer.front(); - m_buffer.pop(); - bReturn = true; + bool bReturn(false); + CLockObject lock(&m_mutex); + if (m_buffer.size() > 0) + { + entry = m_buffer.front(); + m_buffer.pop(); + bReturn = true; + } + return bReturn; } - return bReturn; - } - private: - int m_maxSize; - std::queue<_BType> m_buffer; - CMutex m_mutex; - }; + private: + unsigned int m_maxSize; + std::queue<_BType> m_buffer; + CMutex m_mutex; + }; +}; diff --git a/src/lib/util/misc.cpp b/src/lib/util/misc.cpp deleted file mode 100644 index 3cf71bd..0000000 --- a/src/lib/util/misc.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * boblight - * Copyright (C) Bob 2009 - * - * boblight is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * boblight is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include -#include -#include "misc.h" - -using namespace std; - -void PrintError(const std::string& error) -{ - std::cerr << "ERROR: " << error << "\n"; -} - -//get the first word (separated by whitespace) from string data and place that in word -//then remove that word from string data -bool GetWord(string& data, string& word) -{ - stringstream datastream(data); - string end; - - datastream >> word; - if (datastream.fail()) - { - data.clear(); - return false; - } - - size_t pos = data.find(word) + word.length(); - - if (pos >= data.length()) - { - data.clear(); - return true; - } - - data = data.substr(pos); - - datastream.clear(); - datastream.str(data); - - datastream >> end; - if (datastream.fail()) - data.clear(); - - return true; -} - -//convert . or , to the current locale for correct conversion of ascii float -void ConvertFloatLocale(std::string& strfloat) -{ - static struct lconv* locale = localeconv(); - - size_t pos = strfloat.find_first_of(",."); - - while (pos != string::npos) - { - strfloat.replace(pos, 1, 1, *locale->decimal_point); - pos++; - - if (pos >= strfloat.size()) - break; - - pos = strfloat.find_first_of(",.", pos); - } -} diff --git a/src/lib/util/misc.h b/src/lib/util/misc.h deleted file mode 100644 index e3ff857..0000000 --- a/src/lib/util/misc.h +++ /dev/null @@ -1,177 +0,0 @@ -#pragma once - -/* - * boblight - * Copyright (C) Bob 2009 - * - * boblight is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * boblight is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -void PrintError(const std::string& error); -bool GetWord(std::string& data, std::string& word); -void ConvertFloatLocale(std::string& strfloat); - -template -inline std::string ToString(Value value) -{ - std::string data; - std::stringstream valuestream; - valuestream << value; - valuestream >> data; - return data; -} - -inline std::string GetErrno() -{ - return strerror(errno); -} - -inline std::string GetErrno(int err) -{ - return strerror(err); -} - -template -inline A Clamp(A value, B min, C max) -{ - return value < max ? (value > min ? value : min) : max; -} - -template -inline A Max(A value1, B value2) -{ - return value1 > value2 ? value1 : value2; -} - -template -inline A Max(A value1, B value2, C value3) -{ - return (value1 > value2) ? (value1 > value3 ? value1 : value3) : (value2 > value3 ? value2 : value3); -} - -template -inline A Min(A value1, B value2) -{ - return value1 < value2 ? value1 : value2; -} - -template -inline A Min(A value1, B value2, C value3) -{ - return (value1 < value2) ? (value1 < value3 ? value1 : value3) : (value2 < value3 ? value2 : value3); -} - -template -inline T Abs(T value) -{ - return value > 0 ? value : -value; -} - -template -inline A Round(B value) -{ - if (value == 0.0) - { - return 0; - } - else if (value > 0.0) - { - return (A)(value + 0.5); - } - else - { - return (A)(value - 0.5); - } -} - -//inline int32_t Round32(float value) -//{ -// return lroundf(value); -//} -// -//inline int32_t Round32(double value) -//{ -// return lround(value); -//} -// -//inline int64_t Round64(float value) -//{ -// return llroundf(value); -//} -// -//inline int64_t Round64(double value) -//{ -// return llround(value); -//} - -inline bool StrToInt(const std::string& data, int& value) -{ - return sscanf(data.c_str(), "%i", &value) == 1; -} - -inline bool HexStrToInt(const std::string& data, int& value) -{ - return sscanf(data.c_str(), "%x", &value) == 1; -} - -inline bool StrToFloat(const std::string& data, float& value) -{ - return sscanf(data.c_str(), "%f", &value) == 1; -} - -inline bool StrToFloat(const std::string& data, double& value) -{ - return sscanf(data.c_str(), "%lf", &value) == 1; -} - -inline bool StrToBool(const std::string& data, bool& value) -{ - std::string data2 = data; - std::string word; - if (!GetWord(data2, word)) - return false; - - if (word == "1" || word == "true" || word == "on" || word == "yes") - { - value = true; - return true; - } - else if (word == "0" || word == "false" || word == "off" || word == "no") - { - value = false; - return true; - } - else - { - int ivalue; - if (StrToInt(word, ivalue)) - { - value = ivalue != 0; - return true; - } - } - - return false; -} diff --git a/src/testclient/main.cpp b/src/testclient/main.cpp index 90158ed..983ba13 100644 --- a/src/testclient/main.cpp +++ b/src/testclient/main.cpp @@ -31,19 +31,72 @@ */ #include "../../include/CECExports.h" -#include "../lib/util/threads.h" -#include "../lib/util/misc.h" +#include "../lib/platform/threads.h" #include "../lib/util/StdString.h" #include #include #include #include +#include using namespace CEC; using namespace std; #define CEC_TEST_CLIENT_VERSION 3 + +inline bool HexStrToInt(const std::string& data, uint8_t& value) +{ + int iTmp(0); + if (sscanf(data.c_str(), "%x", &iTmp) == 1) + { + if (iTmp > 256) + value = 255; + else if (iTmp < 0) + value = 0; + else + value = (uint8_t) iTmp; + + return true; + } + + return false; +} + +//get the first word (separated by whitespace) from string data and place that in word +//then remove that word from string data +bool GetWord(string& data, string& word) +{ + stringstream datastream(data); + string end; + + datastream >> word; + if (datastream.fail()) + { + data.clear(); + return false; + } + + size_t pos = data.find(word) + word.length(); + + if (pos >= data.length()) + { + data.clear(); + return true; + } + + data = data.substr(pos); + + datastream.clear(); + datastream.str(data); + + datastream >> end; + if (datastream.fail()) + data.clear(); + + return true; +} + void flush_log(ICECAdapter *cecParser) { cec_log_message message; @@ -213,7 +266,7 @@ int main (int argc, char *argv[]) if (command == "tx") { string strvalue; - int ivalue; + uint8_t ivalue; vector bytes; while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue)) bytes.push_back(ivalue);