#include "AdapterCommunication.h"
#include "CECProcessor.h"
+#include "platform/os-dependent.h"
#include "platform/serialport.h"
-#include "util/StdString.h"
-#include "platform/timeutils.h"
using namespace std;
using namespace CEC;
case MSGCODE_HIGH_ERROR:
case MSGCODE_LOW_ERROR:
{
- int iLine = (size() >= 3) ? (at(1) << 8) | at(2) : 0;
+ uint32_t iLine = (size() >= 3) ? (at(1) << 8) | at(2) : 0;
uint32_t iTime = (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0;
- strMsg.AppendFormat(" line:%i", iLine);
+ strMsg.AppendFormat(" line:%u", iLine);
strMsg.AppendFormat(" time:%u", iTime);
}
break;
void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
{
CLockObject lock(&m_mutex);
- for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
+ for (uint8_t iPtr = 0; iPtr < iLen; iPtr++)
m_inBuffer.Push(data[iPtr]);
m_rcvCondition.Signal();
*/
#include <cectypes.h>
-#include "platform/threads.h"
#include "util/buffer.h"
-#include "util/StdString.h"
#include <string>
+#include "util/StdString.h"
namespace CEC
{
#include <string>
#include <cectypes.h>
#include "AdapterCommunication.h"
-#include "platform/threads.h"
+#include "platform/os-dependent.h"
#include "util/buffer.h"
#include "util/StdString.h"
pkgconfig_DATA = libcec.pc
libcec_la_SOURCES = AdapterCommunication.cpp \
- AdapterCommunication.h \
AdapterDetection.cpp \
- AdapterDetection.h \
CECProcessor.cpp \
- CECProcessor.h \
LibCEC.cpp \
- LibCEC.h \
LibCECC.cpp \
- util/StdString.h \
- devices/CECAudioSystem.h \
devices/CECAudioSystem.cpp \
devices/CECBusDevice.cpp \
- devices/CECBusDevice.h \
- devices/CECPlaybackDevice.h \
devices/CECPlaybackDevice.cpp \
- devices/CECRecordingDevice.h \
devices/CECRecordingDevice.cpp \
- devices/CECTuner.h \
devices/CECTuner.cpp \
- devices/CECTV.h \
devices/CECTV.cpp \
implementations/ANCommandHandler.cpp \
- implementations/ANCommandHandler.h \
implementations/CECCommandHandler.cpp \
- implementations/CECCommandHandler.h \
implementations/SLCommandHandler.cpp \
- implementations/SLCommandHandler.h \
implementations/VLCommandHandler.cpp \
- implementations/VLCommandHandler.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
+ platform/posix/serialport.cpp \
+ platform/posix/pthreads.cpp \
+ platform/threads.cpp
libcec_la_LDFLAGS = @LIBS@ -version-info @VERSION@
libcec_la_CPPFLAGS = -I@abs_top_srcdir@/include
#include <cectypes.h>
#include <set>
-#include "../platform/threads.h"
+#include "../platform/os-dependent.h"
#include "../util/StdString.h"
namespace CEC
m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
vector<CCECBusDevice *> devices;
- for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--)
+ for (int iDevicePtr = GetMyDevices(devices) - 1; iDevicePtr >=0; iDevicePtr--)
devices[iDevicePtr]->TransmitActiveSource();
return true;
m_busDevice->AddLog(CEC_LOG_DEBUG, strLog);
}
-unsigned int CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
+size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
{
- unsigned int iReturn(0);
+ size_t iReturn(0);
cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
{
cec_command command;
cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
- for (unsigned int iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
+ for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
command.parameters.PushBack(strDeviceName.at(iPtr));
return Transmit(command, false);
cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
command.parameters.PushBack((uint8_t)duration);
- unsigned int iLen = strlen(strMessage);
+ size_t iLen = strlen(strMessage);
if (iLen > 13) iLen = 13;
- for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
+ for (size_t iPtr = 0; iPtr < iLen; iPtr++)
command.parameters.PushBack(strMessage[iPtr]);
return Transmit(command, false);
#include <cectypes.h>
#include <vector>
#include "../util/StdString.h"
-#include "../platform/threads.h"
+#include "../platform/os-dependent.h"
namespace CEC
{
virtual bool HandleVendorCommand(const cec_command &command);
virtual void UnhandledCommand(const cec_command &command);
- virtual unsigned int GetMyDevices(std::vector<CCECBusDevice *> &devices) const;
+ virtual size_t GetMyDevices(std::vector<CCECBusDevice *> &devices) const;
virtual CCECBusDevice *GetDevice(cec_logical_address iLogicalAddress) const;
virtual CCECBusDevice *GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const;
virtual CCECBusDevice *GetDeviceByType(cec_device_type type) const;
#if defined(__WINDOWS__)
#include "windows/os_windows.h"
#else
-#include "linux/os_posix.h"
+#include "posix/os_posix.h"
#endif
#if !defined(TRUE)
#if !defined(FALSE)
#define FALSE 0
#endif
+
+#include "timeutils.h"
#define LIBTYPE
#define DECLSPEC
+#include "pthreads.h"
--- /dev/null
+/*
+ * This file is part of the libCEC(R) library.
+ *
+ * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
+ * libCEC(R) is an original work, containing original code.
+ *
+ * libCEC(R) is a trademark of Pulse-Eight Limited.
+ *
+ * This program is dual-licensed; 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ *
+ * Alternatively, you can license this library under a commercial license,
+ * please contact Pulse-Eight Licensing for more information.
+ *
+ * For more information contact:
+ * Pulse-Eight Licensing <license@pulse-eight.com>
+ * http://www.pulse-eight.com/
+ * http://www.pulse-eight.net/
+ */
+
+#include "../threads.h"
+#include "../timeutils.h"
+
+using namespace CEC;
+
+CMutex::CMutex(bool bRecursive /* = true */) :
+ IMutex(bRecursive)
+{
+ pthread_mutex_init(&m_mutex, bRecursive ? GetMutexAttribute() : NULL);
+}
+
+CMutex::~CMutex(void)
+{
+ pthread_mutex_destroy(&m_mutex);
+}
+
+bool CMutex::TryLock(void)
+{
+ return (pthread_mutex_trylock(&m_mutex) == 0);
+}
+
+bool CMutex::Lock(void)
+{
+ return (pthread_mutex_lock(&m_mutex) == 0);
+}
+
+void CMutex::Unlock(void)
+{
+ pthread_mutex_unlock(&m_mutex);
+}
+
+static pthread_mutexattr_t g_mutexAttr;
+pthread_mutexattr_t *CMutex::GetMutexAttribute()
+{
+ static bool bAttributeInitialised = false;
+ if (!bAttributeInitialised)
+ {
+ pthread_mutexattr_init(&g_mutexAttr);
+ pthread_mutexattr_settype(&g_mutexAttr, PTHREAD_MUTEX_RECURSIVE);
+ bAttributeInitialised = true;
+ }
+ return &g_mutexAttr;
+}
+
+CCondition::CCondition(void)
+{
+ pthread_cond_init(&m_cond, NULL);
+}
+
+CCondition::~CCondition(void)
+{
+ pthread_cond_broadcast(&m_cond);
+ pthread_cond_destroy(&m_cond);
+}
+
+void CCondition::Broadcast(void)
+{
+ pthread_cond_broadcast(&m_cond);
+}
+
+void CCondition::Signal(void)
+{
+ pthread_cond_signal(&m_cond);
+}
+
+bool CCondition::Wait(IMutex *mutex, uint32_t iTimeout /* = 0 */)
+{
+ bool bReturn(false);
+ sched_yield();
+ CMutex *pmutex = static_cast<CMutex *>(mutex);
+ if (pmutex)
+ {
+ if (iTimeout > 0)
+ {
+ struct timespec abstime;
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ iTimeout += now.tv_usec / 1000;
+ abstime.tv_sec = now.tv_sec + (time_t)(iTimeout / 1000);
+ abstime.tv_nsec = (int32_t)((iTimeout % (uint32_t)1000) * (uint32_t)1000000);
+ bReturn = (pthread_cond_timedwait(&m_cond, &pmutex->m_mutex, &abstime) == 0);
+ }
+ else
+ {
+ bReturn = (pthread_cond_wait(&m_cond, &pmutex->m_mutex) == 0);
+ }
+ }
+ return bReturn;
+}
+
+bool CThread::CreateThread(bool bWait /* = true */)
+{
+ bool bReturn(false);
+ CLockObject lock(m_threadMutex);
+ m_bStop = false;
+ if (!m_bRunning && pthread_create(&m_thread, NULL, (void *(*) (void *))&CThread::ThreadHandler, (void *)this) == 0)
+ {
+ if (bWait)
+ m_threadCondition->Wait(m_threadMutex);
+ bReturn = true;
+ }
+ return bReturn;
+}
+
+bool CThread::StopThread(bool bWaitForExit /* = true */)
+{
+ bool bReturn = IThread::StopThread(bWaitForExit);
+
+ void *retVal;
+ if (bWaitForExit && m_bRunning)
+ bReturn = (pthread_join(m_thread, &retVal) == 0);
+
+ return bReturn;
+}
+
+void *CThread::ThreadHandler(CThread *thread)
+{
+ void *retVal = NULL;
+
+ if (thread)
+ {
+ CLockObject lock(thread->m_threadMutex);
+ thread->m_bRunning = true;
+ lock.Leave();
+ thread->m_threadCondition->Broadcast();
+
+ retVal = thread->Process();
+
+ lock.Lock();
+ thread->m_bRunning = false;
+ lock.Leave();
+ thread->m_threadCondition->Broadcast();
+ }
+
+ return retVal;
+}
--- /dev/null
+#pragma once
+/*
+ * This file is part of the libCEC(R) library.
+ *
+ * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
+ * libCEC(R) is an original work, containing original code.
+ *
+ * libCEC(R) is a trademark of Pulse-Eight Limited.
+ *
+ * This program is dual-licensed; 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ *
+ * Alternatively, you can license this library under a commercial license,
+ * please contact Pulse-Eight Licensing for more information.
+ *
+ * For more information contact:
+ * Pulse-Eight Licensing <license@pulse-eight.com>
+ * http://www.pulse-eight.com/
+ * http://www.pulse-eight.net/
+ */
+
+#include "../threads.h"
+
+namespace CEC
+{
+ class CMutex : public IMutex
+ {
+ public:
+ CMutex(bool bRecursive = true);
+ virtual ~CMutex(void);
+
+ virtual bool TryLock(void);
+ virtual bool Lock(void);
+ virtual void Unlock(void);
+
+ pthread_mutex_t m_mutex;
+
+ private:
+ static pthread_mutexattr_t *GetMutexAttribute();
+ };
+
+ class CCondition : public ICondition
+ {
+ public:
+ CCondition(void);
+ virtual ~CCondition(void);
+
+ virtual void Broadcast(void);
+ virtual void Signal(void);
+ virtual bool Wait(IMutex *mutex, uint32_t iTimeout = 0);
+
+ private:
+ pthread_cond_t m_cond;
+ };
+
+ class CThread : public IThread
+ {
+ public:
+ CThread(void);
+ virtual ~CThread(void);
+
+ virtual bool CreateThread(bool bWait = true);
+ virtual bool StopThread(bool bWaitForExit = true);
+
+ static void *ThreadHandler(CThread *thread);
+
+ private:
+ pthread_t m_thread;
+ };
+};
#include "threads.h"
#include "timeutils.h"
+#include "os-dependent.h"
using namespace CEC;
-CMutex::CMutex(bool bRecursive /* = true */)
-{
- pthread_mutex_init(&m_mutex, bRecursive ? GetMutexAttribute() : NULL);
-}
-
-CMutex::~CMutex(void)
-{
- pthread_mutex_destroy(&m_mutex);
-}
-
-bool CMutex::TryLock(void)
-{
- return (pthread_mutex_trylock(&m_mutex) == 0);
-}
-
-bool CMutex::Lock(void)
-{
- return (pthread_mutex_lock(&m_mutex) == 0);
-}
-
-void CMutex::Unlock(void)
-{
- pthread_mutex_unlock(&m_mutex);
-}
-
-static pthread_mutexattr_t g_mutexAttr;
-pthread_mutexattr_t *CMutex::GetMutexAttribute()
-{
- static bool bAttributeInitialised = false;
- if (!bAttributeInitialised)
- {
- pthread_mutexattr_init(&g_mutexAttr);
- pthread_mutexattr_settype(&g_mutexAttr, PTHREAD_MUTEX_RECURSIVE);
- bAttributeInitialised = true;
- }
- return &g_mutexAttr;
-}
-
-CLockObject::CLockObject(CMutex *mutex, bool bTryLock /* = false */) :
+CLockObject::CLockObject(IMutex *mutex, bool bTryLock /* = false */) :
m_mutex(mutex)
{
if (m_mutex)
m_bLocked = m_mutex->Lock();
}
-CCondition::CCondition(void)
-{
- pthread_cond_init(&m_cond, NULL);
-}
-
-CCondition::~CCondition(void)
-{
- pthread_cond_broadcast(&m_cond);
- pthread_cond_destroy(&m_cond);
-}
-
-void CCondition::Broadcast(void)
-{
- pthread_cond_broadcast(&m_cond);
-}
-
-void CCondition::Signal(void)
-{
- pthread_cond_signal(&m_cond);
-}
-
-bool CCondition::Wait(CMutex *mutex, uint32_t iTimeout /* = 0 */)
-{
- bool bReturn(false);
- sched_yield();
- if (mutex)
- {
- if (iTimeout > 0)
- {
- struct timespec abstime;
- struct timeval now;
- gettimeofday(&now, NULL);
- iTimeout += now.tv_usec / 1000;
- abstime.tv_sec = now.tv_sec + (time_t)(iTimeout / 1000);
- abstime.tv_nsec = (int32_t)((iTimeout % (uint32_t)1000) * (uint32_t)1000000);
- bReturn = (pthread_cond_timedwait(&m_cond, &mutex->m_mutex, &abstime) == 0);
- }
- else
- {
- bReturn = (pthread_cond_wait(&m_cond, &mutex->m_mutex) == 0);
- }
- }
-
- return bReturn;
-}
-
-void CCondition::Sleep(uint32_t iTimeout)
+void ICondition::Sleep(uint32_t iTimeout)
{
CCondition w;
CMutex m;
w.Wait(&m, iTimeout);
}
-CThread::CThread(void) :
+IThread::IThread(void) :
m_bStop(false),
m_bRunning(false)
{
+ m_threadCondition = new CCondition();
+ m_threadMutex = new CMutex();
}
-CThread::~CThread(void)
+IThread::~IThread(void)
{
StopThread();
+ delete m_threadCondition;
+ delete m_threadMutex;
}
-bool CThread::CreateThread(bool bWait /* = true */)
-{
- bool bReturn(false);
-
- CLockObject lock(&m_threadMutex);
- m_bStop = false;
- if (!m_bRunning && pthread_create(&m_thread, NULL, (void *(*) (void *))&CThread::ThreadHandler, (void *)this) == 0)
- {
- if (bWait)
- m_threadCondition.Wait(&m_threadMutex);
- bReturn = true;
- }
-
- return bReturn;
-}
-
-void *CThread::ThreadHandler(CThread *thread)
-{
- void *retVal = NULL;
-
- if (thread)
- {
- CLockObject lock(&thread->m_threadMutex);
- thread->m_bRunning = true;
- lock.Leave();
- thread->m_threadCondition.Broadcast();
-
- retVal = thread->Process();
-
- lock.Lock();
- thread->m_bRunning = false;
- lock.Leave();
- thread->m_threadCondition.Broadcast();
- }
-
- return retVal;
-}
-
-bool CThread::StopThread(bool bWaitForExit /* = true */)
+bool IThread::StopThread(bool bWaitForExit /* = true */)
{
- bool bReturn(true);
m_bStop = true;
+ m_threadCondition->Broadcast();
+ bWaitForExit = true;
- m_threadCondition.Broadcast();
-
- void *retVal;
- if (bWaitForExit && m_bRunning)
- bReturn = (pthread_join(m_thread, &retVal) == 0);
-
- return bReturn;
+ return false;
}
-bool CThread::Sleep(uint32_t iTimeout)
+bool IThread::Sleep(uint32_t iTimeout)
{
- CLockObject lock(&m_threadMutex);
- return m_bStop ? false : m_threadCondition.Wait(&m_threadMutex, iTimeout);
+ CLockObject lock(m_threadMutex);
+ return m_bStop ? false : m_threadCondition->Wait(m_threadMutex, iTimeout);
}
* http://www.pulse-eight.net/
*/
-#include "os-dependent.h"
#include <stdint.h>
namespace CEC
{
- class CMutex;
+ class IMutex
+ {
+ public:
+ IMutex(bool bRecursive = true) { m_bRecursive = bRecursive ; };
+ virtual ~IMutex(void) {};
+
+ virtual bool TryLock(void) = 0;
+ virtual bool Lock(void) = 0;
+ virtual void Unlock(void) = 0;
+
+ protected:
+ bool m_bRecursive;
+ };
- class CCondition
+ class ICondition
{
public:
- CCondition(void);
- virtual ~CCondition(void);
+ virtual void Broadcast(void) = 0;
+ virtual void Signal(void) = 0;
+ virtual bool Wait(IMutex *mutex, uint32_t iTimeout = 0) = 0;
- void Broadcast(void);
- void Signal(void);
- bool Wait(CMutex *mutex, uint32_t iTimeout = 0);
static void Sleep(uint32_t iTimeout);
-
- private:
- pthread_cond_t m_cond;
};
- class CMutex
+ class IThread
{
public:
- CMutex(bool bRecursive = true);
- virtual ~CMutex(void);
+ IThread(void);
+ virtual ~IThread(void);
- bool TryLock(void);
- bool Lock(void);
- void Unlock(void);
+ virtual bool IsRunning(void) const { return m_bRunning; };
+ virtual bool CreateThread(bool bWait = true) = 0;
+ virtual bool IsStopped(void) const { return m_bStop; };
- pthread_mutex_t m_mutex;
+ virtual bool StopThread(bool bWaitForExit = true);
+ virtual bool Sleep(uint32_t iTimeout);
- private:
- static pthread_mutexattr_t *GetMutexAttribute();
+ virtual void *Process(void) = 0;
+
+ protected:
+ bool m_bStop;
+ bool m_bRunning;
+ ICondition *m_threadCondition;
+ IMutex *m_threadMutex;
};
class CLockObject
{
public:
- CLockObject(CMutex *mutex, bool bTryLock = false);
+ CLockObject(IMutex *mutex, bool bTryLock = false);
~CLockObject(void);
bool IsLocked(void) const { return m_bLocked; }
void Lock(void);
private:
- CMutex *m_mutex;
+ IMutex *m_mutex;
bool m_bLocked;
};
-
- class CThread
- {
- public:
- CThread(void);
- virtual ~CThread(void);
-
- virtual bool IsRunning(void) const { return m_bRunning; }
- virtual bool CreateThread(bool bWait = true);
- virtual bool StopThread(bool bWaitForExit = true);
- virtual bool IsStopped(void) const { return m_bStop; };
- virtual bool Sleep(uint32_t iTimeout);
-
- static void *ThreadHandler(CThread *thread);
- virtual void *Process(void) = 0;
-
- protected:
- CCondition m_threadCondition;
-
- private:
- pthread_t m_thread;
- CMutex m_threadMutex;
- bool m_bStop;
- bool m_bRunning;
- };
};
#include <sys/time.h>
#endif
+#include "os-dependent.h"
+
namespace CEC
{
inline int64_t GetTimeMs()
#include <winsock2.h>
#pragma warning(default:4005)
+#if defined(_WIN32)
#include "../pthread_win32/pthread.h"
+#include "../pthreads.h"
+#endif
#if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64
# define __USE_FILE_OFFSET64 1
* http://www.pulse-eight.net/
*/
-#include "../platform/threads.h"
+#include "../platform/os-dependent.h"
#include <queue>
namespace CEC
struct CecBuffer
{
public:
- CecBuffer(unsigned int iMaxSize = 100)
+ CecBuffer(size_t iMaxSize = 100)
{
m_maxSize = iMaxSize;
}
m_buffer.pop();
}
- int Size(void) const { return m_buffer.size(); }
+ size_t Size(void) const { return m_buffer.size(); }
bool Push(_BType entry)
{
}
private:
- unsigned int m_maxSize;
+ size_t m_maxSize;
std::queue<_BType> m_buffer;
CMutex m_mutex;
};
bin_PROGRAMS = cec-client
cec_client_SOURCES = main.cpp \
- ../lib/platform/threads.cpp
+ ../lib/platform/threads.cpp \
+ ../lib/platform/posix/pthreads.cpp
cec_client_CPPFLAGS = -I@abs_top_srcdir@/include
cec_client_LDFLAGS = @LIBS_DL@
\ No newline at end of file