From ba65909d0a9c43a1bac71c6182c53f202285cec5 Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Sat, 28 Jan 2012 13:02:01 +0100 Subject: [PATCH] cec: clean ups and only include what we need from lib/platform. --- project/libcec.vcxproj | 3 +- project/libcec.vcxproj.filters | 3 +- src/lib/CECProcessor.cpp | 1 + src/lib/CECProcessor.h | 2 +- src/lib/LibCEC.cpp | 3 +- src/lib/LibCEC.h | 2 +- src/lib/adapter/AdapterCommunication.cpp | 9 +- src/lib/adapter/AdapterCommunication.h | 6 +- src/lib/adapter/AdapterDetection.cpp | 2 +- src/lib/adapter/AdapterMessage.h | 2 + src/lib/devices/CECBusDevice.cpp | 1 + src/lib/devices/CECBusDevice.h | 3 +- src/lib/implementations/CECCommandHandler.h | 3 +- src/lib/platform/os.h | 12 +- src/lib/platform/posix/os-socket.h | 159 ++++++++++++++ src/lib/platform/posix/os-types.h | 4 + src/lib/platform/posix/serialport.cpp | 201 ++---------------- .../{serialport => sockets}/serialport.h | 27 ++- src/lib/platform/sockets/socket.h | 102 +++++++++ src/lib/platform/threads/mutex.h | 8 + src/lib/platform/{ => util}/StdString.h | 0 .../platform/{serialport => util}/baudrate.h | 0 src/lib/platform/{ => util}/buffer.h | 2 +- src/lib/platform/{ => util}/timeutils.h | 5 +- src/lib/platform/windows/os-types.h | 4 + src/lib/platform/windows/serialport.cpp | 6 +- 26 files changed, 342 insertions(+), 228 deletions(-) create mode 100644 src/lib/platform/posix/os-socket.h rename src/lib/platform/{serialport => sockets}/serialport.h (83%) create mode 100644 src/lib/platform/sockets/socket.h rename src/lib/platform/{ => util}/StdString.h (100%) rename src/lib/platform/{serialport => util}/baudrate.h (100%) rename src/lib/platform/{ => util}/buffer.h (98%) rename src/lib/platform/{ => util}/timeutils.h (98%) diff --git a/project/libcec.vcxproj b/project/libcec.vcxproj index 7023193..e0770b4 100644 --- a/project/libcec.vcxproj +++ b/project/libcec.vcxproj @@ -231,4 +231,5 @@ - \ No newline at end of file + + diff --git a/project/libcec.vcxproj.filters b/project/libcec.vcxproj.filters index 37a07ba..5a9f432 100644 --- a/project/libcec.vcxproj.filters +++ b/project/libcec.vcxproj.filters @@ -159,4 +159,5 @@ - \ No newline at end of file + + diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 8c808ae..6a8afd9 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -41,6 +41,7 @@ #include "devices/CECTV.h" #include "implementations/CECCommandHandler.h" #include "LibCEC.h" +#include "platform/util/timeutils.h" using namespace CEC; using namespace std; diff --git a/src/lib/CECProcessor.h b/src/lib/CECProcessor.h index 140911a..2c0f287 100644 --- a/src/lib/CECProcessor.h +++ b/src/lib/CECProcessor.h @@ -34,7 +34,7 @@ #include #include #include "adapter/AdapterCommunication.h" -#include "platform/os.h" +#include "platform/util/buffer.h" class CSerialPort; diff --git a/src/lib/LibCEC.cpp b/src/lib/LibCEC.cpp index 1dd3366..9534fe9 100644 --- a/src/lib/LibCEC.cpp +++ b/src/lib/LibCEC.cpp @@ -36,7 +36,8 @@ #include "adapter/AdapterDetection.h" #include "CECProcessor.h" #include "devices/CECBusDevice.h" -#include "platform/timeutils.h" +#include "platform/util/timeutils.h" +#include "platform/util/StdString.h" using namespace std; using namespace CEC; diff --git a/src/lib/LibCEC.h b/src/lib/LibCEC.h index 752bae6..bb21f02 100644 --- a/src/lib/LibCEC.h +++ b/src/lib/LibCEC.h @@ -33,7 +33,7 @@ #include #include -#include "platform/os.h" +#include "platform/util/buffer.h" namespace CEC { diff --git a/src/lib/adapter/AdapterCommunication.cpp b/src/lib/adapter/AdapterCommunication.cpp index 2bbcb01..ff69a7f 100644 --- a/src/lib/adapter/AdapterCommunication.cpp +++ b/src/lib/adapter/AdapterCommunication.cpp @@ -34,7 +34,8 @@ #include "AdapterMessage.h" #include "../CECProcessor.h" -#include "../platform/serialport/serialport.h" +#include "../platform/sockets/serialport.h" +#include "../platform/util/timeutils.h" #include "../LibCEC.h" using namespace std; @@ -234,9 +235,11 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) return bGotFullMessage; } -std::string CAdapterCommunication::GetError(void) const +CStdString CAdapterCommunication::GetError(void) const { - return m_port->GetError(); + CStdString strError; + strError = m_port->GetError(); + return strError; } bool CAdapterCommunication::StartBootloader(void) diff --git a/src/lib/adapter/AdapterCommunication.h b/src/lib/adapter/AdapterCommunication.h index f797954..76b51df 100644 --- a/src/lib/adapter/AdapterCommunication.h +++ b/src/lib/adapter/AdapterCommunication.h @@ -32,7 +32,9 @@ */ #include -#include "../platform/os.h" +#include "../platform/threads/threads.h" +#include "../platform/util/buffer.h" +#include "../platform/util/StdString.h" namespace PLATFORM { @@ -55,7 +57,7 @@ namespace CEC bool Write(CCECAdapterMessage *data); void Close(void); bool IsOpen(void); - std::string GetError(void) const; + CStdString GetError(void) const; void *Process(void); diff --git a/src/lib/adapter/AdapterDetection.cpp b/src/lib/adapter/AdapterDetection.cpp index 596bfd8..2ed9e49 100644 --- a/src/lib/adapter/AdapterDetection.cpp +++ b/src/lib/adapter/AdapterDetection.cpp @@ -31,7 +31,7 @@ */ #include "AdapterDetection.h" -#include "../platform/os.h" +#include "../platform/util/StdString.h" #if defined(__APPLE__) #include diff --git a/src/lib/adapter/AdapterMessage.h b/src/lib/adapter/AdapterMessage.h index ad81878..4c8b35c 100644 --- a/src/lib/adapter/AdapterMessage.h +++ b/src/lib/adapter/AdapterMessage.h @@ -31,6 +31,8 @@ * http://www.pulse-eight.net/ */ +#include "platform/util/StdString.h" + namespace CEC { typedef enum cec_adapter_message_state diff --git a/src/lib/devices/CECBusDevice.cpp b/src/lib/devices/CECBusDevice.cpp index 48b75d7..be9e887 100644 --- a/src/lib/devices/CECBusDevice.cpp +++ b/src/lib/devices/CECBusDevice.cpp @@ -37,6 +37,7 @@ #include "../implementations/SLCommandHandler.h" #include "../implementations/VLCommandHandler.h" #include "../LibCEC.h" +#include "../platform/util/timeutils.h" using namespace CEC; using namespace PLATFORM; diff --git a/src/lib/devices/CECBusDevice.h b/src/lib/devices/CECBusDevice.h index ba68632..b6f1a7d 100644 --- a/src/lib/devices/CECBusDevice.h +++ b/src/lib/devices/CECBusDevice.h @@ -33,7 +33,8 @@ #include #include -#include "../platform/os.h" +#include "../platform/threads/mutex.h" +#include "../platform/util/StdString.h" namespace CEC { diff --git a/src/lib/implementations/CECCommandHandler.h b/src/lib/implementations/CECCommandHandler.h index f1e0924..97518b3 100644 --- a/src/lib/implementations/CECCommandHandler.h +++ b/src/lib/implementations/CECCommandHandler.h @@ -33,7 +33,8 @@ #include #include -#include "../platform/os.h" +#include "../platform/threads/mutex.h" +#include "../platform/util/StdString.h" namespace CEC { diff --git a/src/lib/platform/os.h b/src/lib/platform/os.h index fbe0903..8f88607 100644 --- a/src/lib/platform/os.h +++ b/src/lib/platform/os.h @@ -31,18 +31,8 @@ * http://www.pulse-eight.net/ */ -#if defined(_WIN32) || defined(_WIN64) -#ifndef __WINDOWS__ -#define __WINDOWS__ -#endif +#if (defined(_WIN32) || defined(_WIN64)) #include "windows/os-types.h" -#include "windows/os-threads.h" #else #include "posix/os-types.h" -#include "posix/os-threads.h" #endif - -#include "timeutils.h" -#include "threads/threads.h" -#include "buffer.h" -#include "StdString.h" diff --git a/src/lib/platform/posix/os-socket.h b/src/lib/platform/posix/os-socket.h new file mode 100644 index 0000000..3cea52b --- /dev/null +++ b/src/lib/platform/posix/os-socket.h @@ -0,0 +1,159 @@ +#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 + * http://www.pulse-eight.com/ + * http://www.pulse-eight.net/ + */ + + +#include "../os.h" +#include "../util/timeutils.h" +#include +#include + +namespace PLATFORM +{ + inline void SocketClose(socket_t socket) + { + if (socket != INVALID_SOCKET) + close(socket); + } + + inline void SocketSetBlocking(socket_t socket, bool bSetTo) + { +// return bSetTo ? +// fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK) == 0 : +// fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK) == 0; + fcntl(socket, F_SETFL, bSetTo ? FNDELAY : 0); + } + + inline int64_t SocketWrite(socket_t socket, int *iError, uint8_t* data, uint32_t len) + { + fd_set port; + + if (socket == -1) + { + *iError = EINVAL; + return -1; + } + + int64_t iBytesWritten = 0; + struct timeval *tv; + + while (iBytesWritten < len) + { + FD_ZERO(&port); + FD_SET(socket, &port); + int returnv = select(socket + 1, NULL, &port, NULL, tv); + if (returnv < 0) + { + *iError = errno; + return -1; + } + else if (returnv == 0) + { + *iError = ETIMEDOUT; + return -1; + } + + returnv = write(socket, data + iBytesWritten, len - iBytesWritten); + if (returnv == -1) + { + *iError = errno; + return -1; + } + iBytesWritten += returnv; + } + + return iBytesWritten; + } + + inline int32_t SocketRead(socket_t socket, int *iError, uint8_t* data, uint32_t len, uint64_t iTimeoutMs /*= 0*/) + { + fd_set port; + struct timeval timeout, *tv; + int64_t iNow(0), iTarget(0); + int32_t iBytesRead = 0; + *iError = 0; + + if (socket == -1) + { + *iError = EINVAL; + return -1; + } + + if (iTimeoutMs > 0) + { + iNow = GetTimeMs(); + iTarget = iNow + (int64_t) iTimeoutMs; + } + + while (iBytesRead < (int32_t) len && (iTimeoutMs == 0 || iTarget > iNow)) + { + if (iTimeoutMs == 0) + { + tv = NULL; + } + else + { + timeout.tv_sec = ((long int)iTarget - (long int)iNow) / (long int)1000.; + timeout.tv_usec = ((long int)iTarget - (long int)iNow) % (long int)1000.; + tv = &timeout; + } + + FD_ZERO(&port); + FD_SET(socket, &port); + int32_t returnv = select(socket + 1, &port, NULL, NULL, tv); + + if (returnv == -1) + { + *iError = errno; + return -1; + } + else if (returnv == 0) + { + break; //nothing to read + } + + returnv = read(socket, data + iBytesRead, len - iBytesRead); + if (returnv == -1) + { + *iError = errno; + return -1; + } + + iBytesRead += returnv; + + if (iTimeoutMs > 0) + iNow = GetTimeMs(); + } + + return iBytesRead; + } +} diff --git a/src/lib/platform/posix/os-types.h b/src/lib/platform/posix/os-types.h index 99bacb3..521c89a 100644 --- a/src/lib/platform/posix/os-types.h +++ b/src/lib/platform/posix/os-types.h @@ -33,3 +33,7 @@ #define LIBTYPE #define DECLSPEC +typedef int socket_t; +#define INVALID_SOCKET (-1) +#define SOCKET_ERROR (-1) + diff --git a/src/lib/platform/posix/serialport.cpp b/src/lib/platform/posix/serialport.cpp index ec8a5c1..71405d4 100644 --- a/src/lib/platform/posix/serialport.cpp +++ b/src/lib/platform/posix/serialport.cpp @@ -30,11 +30,11 @@ * http://www.pulse-eight.net/ */ +#include "../os.h" #include #include -#include "../serialport/serialport.h" -#include "../serialport/baudrate.h" -#include "../timeutils.h" +#include "../sockets/serialport.h" +#include "../util/baudrate.h" #if defined(__APPLE__) #ifndef XCASE @@ -52,191 +52,45 @@ using namespace PLATFORM; CSerialPort::CSerialPort() { - m_fd = -1; m_tostdout = false; } -CSerialPort::~CSerialPort() -{ - Close(); -} - -int64_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; - } - - int64_t byteswritten = 0; - struct timeval *tv; -//TODO -// struct timeval timeout, *tv; -// if (data->transmit_timeout <= 0) -// { - tv = NULL; -// } -// else -// { -// timeout.tv_sec = (long int)data->transmit_timeout / (long int)1000.; -// timeout.tv_usec = (long int)data->transmit_timeout % (long int)1000.; -// tv = &timeout; -// } - - while (byteswritten < len) - { - FD_ZERO(&port); - FD_SET(m_fd, &port); - int returnv = select(m_fd + 1, NULL, &port, NULL, tv); - if (returnv < 0) - { - m_error = strerror(errno); - return -1; - } - else if (returnv == 0) - { - m_error = "timeout"; - return -1; - } - - returnv = write(m_fd, data + byteswritten, len - byteswritten); - if (returnv == -1) - { - m_error = strerror(errno); - return -1; - } - byteswritten += returnv; - } - - //print what's written to stdout for debugging - if (m_tostdout) - { - printf("%s write:", m_name.c_str()); - for (int i = 0; i < byteswritten; i++) - printf(" %02x", data[i]); - - printf("\n"); - } - - return byteswritten; -} - -int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /*= 0*/) -{ - fd_set port; - struct timeval timeout, *tv; - 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) - { - now = GetTimeMs(); - target = now + (int64_t) iTimeoutMs; - } - - while (bytesread < (int32_t) len && (iTimeoutMs == 0 || target > now)) - { - if (iTimeoutMs == 0) - { - tv = NULL; - } - else - { - timeout.tv_sec = ((long int)target - (long int)now) / (long int)1000.; - timeout.tv_usec = ((long int)target - (long int)now) % (long int)1000.; - tv = &timeout; - } - - FD_ZERO(&port); - FD_SET(m_fd, &port); - int32_t returnv = select(m_fd + 1, &port, NULL, NULL, tv); - - if (returnv == -1) - { - m_error = strerror(errno); - return -1; - } - else if (returnv == 0) - { - break; //nothing to read - } - - returnv = read(m_fd, data + bytesread, len - bytesread); - if (returnv == -1) - { - m_error = strerror(errno); - return -1; - } - - bytesread += returnv; - - if (iTimeoutMs > 0) - now = GetTimeMs(); - } - - //print what's read to stdout for debugging - if (m_tostdout && bytesread > 0) - { - printf("%s read:", m_name.c_str()); - for (int i = 0; i < bytesread; i++) - printf(" %02x", data[i]); - - printf("\n"); - } - - return bytesread; -} - //setting all this stuff up is a pain in the ass 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 = strerror(errno); CLockObject lock(m_mutex); if (databits < 5 || databits > 8) { - m_error = "Databits has to be between 5 and 8"; + m_strError = "Databits has to be between 5 and 8"; return false; } if (stopbits != 1 && stopbits != 2) { - m_error = "Stopbits has to be 1 or 2"; + m_strError = "Stopbits has to be 1 or 2"; return false; } if (parity != PAR_NONE && parity != PAR_EVEN && parity != PAR_ODD) { - m_error = "Parity has to be none, even or odd"; + m_strError = "Parity has to be none, even or odd"; return false; } - m_fd = open(name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); + m_socket = open(name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); - if (m_fd == -1) + if (m_socket == INVALID_SOCKET) { - m_error = strerror(errno); + m_strError = strerror(errno); return false; } - fcntl(m_fd, F_SETFL, 0); + SocketSetBlocking(m_socket, false); if (!SetBaudRate(baudrate)) - { return false; - } m_options.c_cflag |= (CLOCAL | CREAD); m_options.c_cflag &= ~HUPCL; @@ -277,30 +131,17 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits /* = 8 * m_options.c_iflag &= ~(IXON | IXOFF | IXANY | BRKINT | INLCR | IGNCR | ICRNL | IUCLC | IMAXBEL); m_options.c_oflag &= ~(OPOST | ONLCR | OCRNL); - if (tcsetattr(m_fd, TCSANOW, &m_options) != 0) + if (tcsetattr(m_socket, TCSANOW, &m_options) != 0) { - m_error = strerror(errno); + m_strError = strerror(errno); return false; } - //non-blocking port - fcntl(m_fd, F_SETFL, FNDELAY); + SocketSetBlocking(m_socket, true); return true; } -void CSerialPort::Close() -{ - CLockObject lock(m_mutex); - if (m_fd != -1) - { - close(m_fd); - m_fd = -1; - m_name = ""; - m_error = ""; - } -} - bool CSerialPort::SetBaudRate(uint32_t baudrate) { int rate = IntToBaudrate(baudrate); @@ -308,34 +149,28 @@ bool CSerialPort::SetBaudRate(uint32_t baudrate) { char buff[255]; sprintf(buff, "%i is not a valid baudrate", baudrate); - m_error = buff; + m_strError = buff; return false; } //get the current port attributes - if (tcgetattr(m_fd, &m_options) != 0) + if (tcgetattr(m_socket, &m_options) != 0) { - m_error = strerror(errno); + m_strError = strerror(errno); return false; } if (cfsetispeed(&m_options, rate) != 0) { - m_error = strerror(errno); + m_strError = strerror(errno); return false; } if (cfsetospeed(&m_options, rate) != 0) { - m_error = strerror(errno); + m_strError = strerror(errno); return false; } return true; } - -bool CSerialPort::IsOpen() -{ - CLockObject lock(m_mutex); - return m_fd != -1; -} diff --git a/src/lib/platform/serialport/serialport.h b/src/lib/platform/sockets/serialport.h similarity index 83% rename from src/lib/platform/serialport/serialport.h rename to src/lib/platform/sockets/serialport.h index 3710bfe..bef3a80 100644 --- a/src/lib/platform/serialport/serialport.h +++ b/src/lib/platform/sockets/serialport.h @@ -32,11 +32,14 @@ */ #include "../os.h" +#include "../util/buffer.h" + #include #include -#ifndef __WINDOWS__ +#if !defined(__WINDOWS__) #include +#include "socket.h" #endif namespace PLATFORM @@ -45,28 +48,25 @@ namespace PLATFORM #define PAR_EVEN 1 #define PAR_ODD 2 - class CSerialPort + class CSerialPort : public CSocket { public: - CSerialPort(); - virtual ~CSerialPort(); + CSerialPort(void); + virtual ~CSerialPort(void) {}; 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(); - - int64_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; } + CStdString GetName(void) const + { + CStdString strName; + strName = m_name; + return strName; + } private: bool SetBaudRate(uint32_t baudrate); - std::string m_error; std::string m_name; - CMutex m_mutex; bool m_tostdout; #ifdef __WINDOWS__ @@ -83,7 +83,6 @@ namespace PLATFORM HANDLE m_ovHandle; #else struct termios m_options; - int m_fd; #endif }; }; diff --git a/src/lib/platform/sockets/socket.h b/src/lib/platform/sockets/socket.h new file mode 100644 index 0000000..aa3625a --- /dev/null +++ b/src/lib/platform/sockets/socket.h @@ -0,0 +1,102 @@ +#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 + * http://www.pulse-eight.com/ + * http://www.pulse-eight.net/ + */ + +#include "../threads/mutex.h" +#include "../util/StdString.h" + +#if defined(__WINDOWS__) +#include "../windows/os-socket.h" +#else +#include "../posix/os-socket.h" +#endif + +// Common socket operations + +namespace PLATFORM +{ + class CSocket : public PreventCopy + { + public: + CSocket(void) : + m_socket(INVALID_SOCKET) {}; + virtual ~CSocket(void) + { + Close(); + } + + virtual bool IsOpen(void) + { + CLockObject lock(m_mutex); + return m_socket != INVALID_SOCKET && + m_socket != SOCKET_ERROR; + } + + virtual void Close(void) + { + CLockObject lock(m_mutex); + SocketClose(m_socket); + m_socket = INVALID_SOCKET; + m_strError = ""; + } + + int64_t Write(uint8_t* data, uint32_t len) + { + CLockObject lock(m_mutex); + int iError(0); + int64_t iReturn = SocketWrite(m_socket, &iError, data, len); + m_strError = strerror(iError); + return iReturn; + } + + int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0) + { + CLockObject lock(m_mutex); + int iError(0); + int32_t iReturn = SocketRead(m_socket, &iError, data, len, iTimeoutMs); + m_strError = strerror(iError); + return iReturn; + } + + virtual CStdString GetError(void) const + { + CStdString strReturn; + strReturn = m_strError; + return strReturn; + } + + protected: + socket_t m_socket; + CStdString m_strError; + CMutex m_mutex; + }; +}; diff --git a/src/lib/platform/threads/mutex.h b/src/lib/platform/threads/mutex.h index 2c2b5c5..38055f9 100644 --- a/src/lib/platform/threads/mutex.h +++ b/src/lib/platform/threads/mutex.h @@ -31,6 +31,14 @@ * http://www.pulse-eight.net/ */ +#include "../os.h" + +#if defined(__WINDOWS__) +#include "../windows/os-threads.h" +#else +#include "../posix/os-threads.h" +#endif + namespace PLATFORM { class PreventCopy diff --git a/src/lib/platform/StdString.h b/src/lib/platform/util/StdString.h similarity index 100% rename from src/lib/platform/StdString.h rename to src/lib/platform/util/StdString.h diff --git a/src/lib/platform/serialport/baudrate.h b/src/lib/platform/util/baudrate.h similarity index 100% rename from src/lib/platform/serialport/baudrate.h rename to src/lib/platform/util/baudrate.h diff --git a/src/lib/platform/buffer.h b/src/lib/platform/util/buffer.h similarity index 98% rename from src/lib/platform/buffer.h rename to src/lib/platform/util/buffer.h index 227b756..1c12e86 100644 --- a/src/lib/platform/buffer.h +++ b/src/lib/platform/util/buffer.h @@ -31,7 +31,7 @@ * http://www.pulse-eight.net/ */ -#include "os.h" +#include "../threads/mutex.h" #include namespace PLATFORM diff --git a/src/lib/platform/timeutils.h b/src/lib/platform/util/timeutils.h similarity index 98% rename from src/lib/platform/timeutils.h rename to src/lib/platform/util/timeutils.h index b2f613a..12c2145 100644 --- a/src/lib/platform/timeutils.h +++ b/src/lib/platform/util/timeutils.h @@ -31,7 +31,8 @@ * http://www.pulse-eight.net/ */ -#include +#include "../os.h" + #if defined(__APPLE__) #include #include @@ -41,8 +42,6 @@ #include #endif -#include "os.h" - namespace PLATFORM { #if defined(__WINDOWS__) diff --git a/src/lib/platform/windows/os-types.h b/src/lib/platform/windows/os-types.h index 3943510..ea00eae 100644 --- a/src/lib/platform/windows/os-types.h +++ b/src/lib/platform/windows/os-types.h @@ -31,6 +31,10 @@ * http://www.pulse-eight.net/ */ +#if !defined(__WINDOWS__) +#define __WINDOWS__ +#endif + #pragma warning(disable:4005) // Disable "warning C4005: '_WINSOCKAPI_' : macro redefinition" #include #pragma warning(default:4005) diff --git a/src/lib/platform/windows/serialport.cpp b/src/lib/platform/windows/serialport.cpp index 8e513fc..22c65d7 100644 --- a/src/lib/platform/windows/serialport.cpp +++ b/src/lib/platform/windows/serialport.cpp @@ -30,9 +30,9 @@ * http://www.pulse-eight.net/ */ -#include "../serialport/serialport.h" -#include "../serialport/baudrate.h" -#include "../timeutils.h" +#include "../sockets/serialport.h" +#include "../util/baudrate.h" +#include "../util/timeutils.h" using namespace std; using namespace PLATFORM; -- 2.34.1