From b9187cc6999276ce37a5c9852655fd558ea76b8e Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Tue, 4 Oct 2011 19:55:18 +0200 Subject: [PATCH] renamed libPlatform -> platform. stuck everything from libCEC in the CEC namespace to avoid namespace polution --- src/lib/AdapterCommunication.cpp | 2 +- src/lib/AdapterCommunication.h | 5 +- src/lib/AdapterDetection.cpp | 2 +- src/lib/CECProcessor.cpp | 2 +- src/lib/CECProcessor.h | 2 +- src/lib/LibCEC.cpp | 2 +- src/lib/Makefile.am | 18 +- src/lib/libPlatform/baudrate.h | 179 ---------------- src/lib/libPlatform/serialport.h | 107 ---------- src/lib/platform/baudrate.h | 195 ++++++++++++++++++ .../linux/os_posix.h | 27 --- .../linux/serialport.cpp | 37 ++-- .../{libPlatform => platform}/os-dependent.h | 0 .../pthread_win32/pthread.h | 0 .../pthread_win32/pthreadVC2.lib | Bin .../pthread_win32/pthreadVC2d.lib | Bin .../pthread_win32/sched.h | 0 .../pthread_win32/semaphore.h | 0 src/lib/platform/serialport.h | 82 ++++++++ src/lib/{util => platform}/threads.cpp | 2 + src/lib/{util => platform}/threads.h | 101 ++++----- src/lib/{util => platform}/timeutils.h | 49 ++--- .../windows/dlfcn-win32.cpp | 0 .../windows/dlfcn-win32.h | 0 .../windows/os_windows.cpp | 0 .../windows/os_windows.h | 0 .../windows/serialport.cpp | 10 +- src/lib/util/buffer.h | 69 ++++--- src/lib/util/misc.cpp | 81 -------- src/lib/util/misc.h | 177 ---------------- src/testclient/main.cpp | 45 +++- 31 files changed, 477 insertions(+), 717 deletions(-) delete mode 100644 src/lib/libPlatform/baudrate.h delete mode 100644 src/lib/libPlatform/serialport.h create mode 100644 src/lib/platform/baudrate.h rename src/lib/{libPlatform => platform}/linux/os_posix.h (56%) rename src/lib/{libPlatform => platform}/linux/serialport.cpp (92%) rename src/lib/{libPlatform => platform}/os-dependent.h (100%) rename src/lib/{libPlatform => platform}/pthread_win32/pthread.h (100%) rename src/lib/{libPlatform => platform}/pthread_win32/pthreadVC2.lib (100%) rename src/lib/{libPlatform => platform}/pthread_win32/pthreadVC2d.lib (100%) rename src/lib/{libPlatform => platform}/pthread_win32/sched.h (100%) rename src/lib/{libPlatform => platform}/pthread_win32/semaphore.h (100%) create mode 100644 src/lib/platform/serialport.h rename src/lib/{util => platform}/threads.cpp (99%) rename src/lib/{util => platform}/threads.h (53%) rename src/lib/{util => platform}/timeutils.h (55%) rename src/lib/{libPlatform => platform}/windows/dlfcn-win32.cpp (100%) rename src/lib/{libPlatform => platform}/windows/dlfcn-win32.h (100%) rename src/lib/{libPlatform => platform}/windows/os_windows.cpp (100%) rename src/lib/{libPlatform => platform}/windows/os_windows.h (100%) rename src/lib/{libPlatform => platform}/windows/serialport.cpp (97%) delete mode 100644 src/lib/util/misc.cpp delete mode 100644 src/lib/util/misc.h diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 266842f..a7466b7 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; diff --git a/src/lib/AdapterCommunication.h b/src/lib/AdapterCommunication.h index f7dafa0..4266017 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 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..127155a 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; 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..19f34ea 100644 --- a/src/lib/LibCEC.cpp +++ b/src/lib/LibCEC.cpp @@ -36,7 +36,7 @@ #include "AdapterDetection.h" #include "CECProcessor.h" #include "util/StdString.h" -#include "util/timeutils.h" +#include "platform/timeutils.h" using namespace std; using namespace CEC; 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..53f04ac --- /dev/null +++ b/src/lib/platform/baudrate.h @@ -0,0 +1,195 @@ +#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 + +namespace CEC +{ + 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} + }; + + static int IntToBaudrate(int baudrate) + { + for (unsigned int i = 0; i < sizeof(baudrates) / sizeof(CEC::sbaudrate) - 1; i++) + { + if (baudrates[i].rate == 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 92% rename from src/lib/libPlatform/linux/serialport.cpp rename to src/lib/platform/linux/serialport.cpp index 33a7385..e6bfafa 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() @@ -58,14 +54,14 @@ int CSerialPort::Write(uint8_t* data, int len) 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; @@ -122,7 +118,7 @@ int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /*= -1*/) if (returnv == -1) { - m_error = GetErrno(); + m_error = strerror(errno); return -1; } else if (returnv == 0) @@ -133,7 +129,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; } @@ -160,7 +156,7 @@ int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /*= -1*/) bool CSerialPort::Open(string name, int baudrate, int databits/* = 8*/, int stopbits/* = 1*/, int parity/* = PAR_NONE*/) { m_name = name; - m_error = GetErrno(); + m_error = strerror(errno); if (databits < 5 || databits > 8) { @@ -184,7 +180,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 +232,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; } @@ -259,7 +255,7 @@ void CSerialPort::Close() bool CSerialPort::SetBaudRate(int baudrate) { - int rate = IntToRate(baudrate); + int rate = IntToBaudrate(baudrate); if (rate == -1) { char buff[255]; @@ -271,21 +267,26 @@ 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() const +{ + 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..c7e0e6c --- /dev/null +++ b/src/lib/platform/serialport.h @@ -0,0 +1,82 @@ +#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 "baudrate.h" +#include +#include +#include + +#ifndef __WINDOWS__ +#include +#else +#include "../util/threads.h" +#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, int baudrate, int databits = 8, int stopbits = 1, int parity = PAR_NONE); + bool SetBaudRate(int baudrate); + bool IsOpen() const; + 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_error; } + std::string GetName() { return m_name; } + + 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/util/threads.cpp b/src/lib/platform/threads.cpp similarity index 99% rename from src/lib/util/threads.cpp rename to src/lib/platform/threads.cpp index 58aff36..8933a21 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); 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 97% rename from src/lib/libPlatform/windows/serialport.cpp rename to src/lib/platform/windows/serialport.cpp index f4b8c62..bfdc8d5 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) { @@ -205,7 +206,7 @@ bool CSerialPort::SetBaudRate(int baudrate) 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 +239,8 @@ bool CSerialPort::SetBaudRate(int baudrate) return true; } + +bool CSerialPort::IsOpen() const +{ + return m_bIsOpen; +} diff --git a/src/lib/util/buffer.h b/src/lib/util/buffer.h index cafa655..f284131 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(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: + 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..f2f2bf3 100644 --- a/src/testclient/main.cpp +++ b/src/testclient/main.cpp @@ -31,19 +31,60 @@ */ #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, int& value) +{ + return sscanf(data.c_str(), "%x", &value) == 1; +} + + +//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; -- 2.34.1