From 24048d575419fe909fade9110fab48d17370ee6f Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Sun, 29 Jan 2012 20:58:06 +0100 Subject: [PATCH] cec: sync win32 --- project/libcec.vcxproj | 15 ++++--- project/libcec.vcxproj.filters | 54 ++++++++++++---------- src/lib/adapter/AdapterMessage.h | 2 +- src/lib/platform/sockets/serialport.h | 15 +++++-- src/lib/platform/sockets/socket.h | 4 +- src/lib/platform/util/StdString.h | 1 + src/lib/platform/windows/os-socket.h | 60 +++++++++++++++++++++++++ src/lib/platform/windows/os-types.h | 2 + src/lib/platform/windows/serialport.cpp | 55 +++++++++++------------ 9 files changed, 141 insertions(+), 67 deletions(-) create mode 100644 src/lib/platform/windows/os-socket.h diff --git a/project/libcec.vcxproj b/project/libcec.vcxproj index e0770b4..29d5a77 100644 --- a/project/libcec.vcxproj +++ b/project/libcec.vcxproj @@ -38,14 +38,16 @@ - - - - + + - + + + + + @@ -231,5 +233,4 @@ - - + \ No newline at end of file diff --git a/project/libcec.vcxproj.filters b/project/libcec.vcxproj.filters index 5a9f432..636dafc 100644 --- a/project/libcec.vcxproj.filters +++ b/project/libcec.vcxproj.filters @@ -16,22 +16,22 @@ {fa3d5953-d288-45e9-93f4-8419e6b701c7} - - {e3945145-efa2-4393-b201-f50e1e775008} - {38a27e9e-86ad-46f6-a4fb-e1e524267b74} {51614b77-8a0e-47a8-8500-5beb0fd12d49} + + {4e963de7-5c42-40a0-9c81-bb112d5a24f9} + + + {e3945145-efa2-4393-b201-f50e1e775008} + - - platform - exports @@ -83,24 +83,12 @@ platform\windows - - platform\serialport - - - platform\serialport - platform\threads platform\threads - - platform - - - platform - adapter @@ -110,6 +98,27 @@ adapter + + platform\util + + + platform\util + + + platform\util + + + platform\sockets + + + platform\sockets + + + platform\windows + + + platform\util + @@ -146,18 +155,17 @@ devices - - platform\serialport - adapter adapter + + platform\windows + - - + \ No newline at end of file diff --git a/src/lib/adapter/AdapterMessage.h b/src/lib/adapter/AdapterMessage.h index 4c8b35c..8e8c965 100644 --- a/src/lib/adapter/AdapterMessage.h +++ b/src/lib/adapter/AdapterMessage.h @@ -31,7 +31,7 @@ * http://www.pulse-eight.net/ */ -#include "platform/util/StdString.h" +#include "../platform/util/StdString.h" namespace CEC { diff --git a/src/lib/platform/sockets/serialport.h b/src/lib/platform/sockets/serialport.h index bef3a80..bbe0136 100644 --- a/src/lib/platform/sockets/serialport.h +++ b/src/lib/platform/sockets/serialport.h @@ -39,9 +39,10 @@ #if !defined(__WINDOWS__) #include -#include "socket.h" #endif +#include "socket.h" + namespace PLATFORM { #define PAR_NONE 0 @@ -52,7 +53,7 @@ namespace PLATFORM { public: CSerialPort(void); - virtual ~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); @@ -63,16 +64,22 @@ namespace PLATFORM return strName; } + #ifdef __WINDOWS__ + virtual bool IsOpen(void); + virtual void Close(void); + virtual int64_t Write(uint8_t* data, uint32_t len); + virtual int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0); + private: + void FormatWindowsError(int iErrorCode, CStdString &strMessage); bool SetBaudRate(uint32_t baudrate); std::string m_name; bool m_tostdout; - #ifdef __WINDOWS__ bool SetTimeouts(bool bBlocking); - HANDLE m_handle; + HANDLE m_handle; bool m_bIsOpen; uint32_t m_iBaudrate; uint8_t m_iDatabits; diff --git a/src/lib/platform/sockets/socket.h b/src/lib/platform/sockets/socket.h index aa3625a..9b18665 100644 --- a/src/lib/platform/sockets/socket.h +++ b/src/lib/platform/sockets/socket.h @@ -69,7 +69,7 @@ namespace PLATFORM m_strError = ""; } - int64_t Write(uint8_t* data, uint32_t len) + virtual int64_t Write(uint8_t* data, uint32_t len) { CLockObject lock(m_mutex); int iError(0); @@ -78,7 +78,7 @@ namespace PLATFORM return iReturn; } - int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0) + virtual int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0) { CLockObject lock(m_mutex); int iError(0); diff --git a/src/lib/platform/util/StdString.h b/src/lib/platform/util/StdString.h index 9800f94..496dae7 100644 --- a/src/lib/platform/util/StdString.h +++ b/src/lib/platform/util/StdString.h @@ -1,4 +1,5 @@ #pragma once +#include "../os.h" #include #include #include diff --git a/src/lib/platform/windows/os-socket.h b/src/lib/platform/windows/os-socket.h new file mode 100644 index 0000000..bc6c08c --- /dev/null +++ b/src/lib/platform/windows/os-socket.h @@ -0,0 +1,60 @@ +#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" + +namespace PLATFORM +{ + inline void SocketClose(socket_t socket) + { + //TODO + } + + inline void SocketSetBlocking(socket_t socket, bool bSetTo) + { + //TODO + } + + inline int64_t SocketWrite(socket_t socket, int *iError, uint8_t* data, uint32_t len) + { + //TODO + return -1; + } + + inline int32_t SocketRead(socket_t socket, int *iError, uint8_t* data, uint32_t len, uint64_t iTimeoutMs /*= 0*/) + { + // TODO + return -1; + } +} diff --git a/src/lib/platform/windows/os-types.h b/src/lib/platform/windows/os-types.h index ea00eae..0a61c2e 100644 --- a/src/lib/platform/windows/os-types.h +++ b/src/lib/platform/windows/os-types.h @@ -57,6 +57,8 @@ typedef unsigned __int64 uint64_t; #define snprintf _snprintf +typedef SOCKET socket_t; + #if defined(_MSC_VER) #pragma warning (push) #endif diff --git a/src/lib/platform/windows/serialport.cpp b/src/lib/platform/windows/serialport.cpp index 22c65d7..ade4fb2 100644 --- a/src/lib/platform/windows/serialport.cpp +++ b/src/lib/platform/windows/serialport.cpp @@ -37,7 +37,7 @@ using namespace std; using namespace PLATFORM; -void FormatWindowsError(int iErrorCode, string &strMessage) +void CSerialPort::FormatWindowsError(int iErrorCode, CStdString &strMessage) { if (iErrorCode != ERROR_SUCCESS) { @@ -58,11 +58,6 @@ CSerialPort::CSerialPort(void) : { } -CSerialPort::~CSerialPort(void) -{ - Close(); -} - bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity) { CStdString strComPath = "\\\\.\\" + name; @@ -70,8 +65,8 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t m_handle = CreateFile(strComPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (m_handle == INVALID_HANDLE_VALUE) { - m_error = "Unable to open COM port"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "Unable to open COM port"; + FormatWindowsError(GetLastError(), m_strError); return false; } @@ -82,20 +77,20 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t { if (!SetCommConfig(m_handle, &commConfig,dwSize)) { - m_error = "unable to set default config"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "unable to set default config"; + FormatWindowsError(GetLastError(), m_strError); } } else { - m_error = "unable to get default config"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "unable to get default config"; + FormatWindowsError(GetLastError(), m_strError); } if (!SetupComm(m_handle, 64, 64)) { - m_error = "unable to set up the com port"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "unable to set up the com port"; + FormatWindowsError(GetLastError(), m_strError); } m_iDatabits = databits; @@ -103,16 +98,16 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t m_iParity = parity; if (!SetBaudRate(baudrate)) { - m_error = "unable to set baud rate"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "unable to set baud rate"; + FormatWindowsError(GetLastError(), m_strError); Close(); return false; } if (!SetTimeouts(false)) { - m_error = "unable to set timeouts"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "unable to set timeouts"; + FormatWindowsError(GetLastError(), m_strError); Close(); return false; } @@ -129,8 +124,8 @@ bool CSerialPort::SetTimeouts(bool bBlocking) COMMTIMEOUTS cto; if (!GetCommTimeouts(m_handle, &cto)) { - m_error = "GetCommTimeouts failed"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "GetCommTimeouts failed"; + FormatWindowsError(GetLastError(), m_strError); return false; } @@ -149,8 +144,8 @@ bool CSerialPort::SetTimeouts(bool bBlocking) if (!SetCommTimeouts(m_handle, &cto)) { - m_error = "SetCommTimeouts failed"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "SetCommTimeouts failed"; + FormatWindowsError(GetLastError(), m_strError); return false; } @@ -176,8 +171,8 @@ int64_t CSerialPort::Write(uint8_t* data, uint32_t len) if (!WriteFile(m_handle, data, len, &iBytesWritten, NULL)) { - m_error = "Error while writing to COM port"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "Error while writing to COM port"; + FormatWindowsError(GetLastError(), m_strError); return -1; } @@ -191,14 +186,14 @@ int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /* = DWORD iBytesRead = 0; if (m_handle == 0) { - m_error = "Error while reading from COM port: invalid handle"; + m_strError = "Error while reading from COM port: invalid handle"; return iReturn; } if(!ReadFile(m_handle, data, len, &iBytesRead, NULL) != 0) { - m_error = "unable to read from device"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "unable to read from device"; + FormatWindowsError(GetLastError(), m_strError); iReturn = -1; } else @@ -246,15 +241,15 @@ bool CSerialPort::SetBaudRate(uint32_t baudrate) if(!SetCommState(m_handle,&dcb)) { - m_error = "SetCommState failed"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "SetCommState failed"; + FormatWindowsError(GetLastError(), m_strError); return false; } return true; } -bool CSerialPort::IsOpen() +bool CSerialPort::IsOpen(void) { CLockObject lock(m_mutex); return m_bIsOpen; -- 2.34.1