X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Fwindows%2Fserialport.cpp;h=680b1f0426174729827e1b85c1ca4e75e136922f;hb=642daae3aeaea71ee0e2444ee5ac46fe760a5b2b;hp=b028ff32e5e8810f5bb01bcb70cf8aa2810722db;hpb=56701628bc0683330d679147aa857f4b92403daa;p=deb_libcec.git diff --git a/src/lib/platform/windows/serialport.cpp b/src/lib/platform/windows/serialport.cpp index b028ff3..680b1f0 100644 --- a/src/lib/platform/windows/serialport.cpp +++ b/src/lib/platform/windows/serialport.cpp @@ -1,7 +1,7 @@ /* * This file is part of the libCEC(R) library. * - * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved. + * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. * libCEC(R) is an original work, containing original code. * * libCEC(R) is a trademark of Pulse-Eight Limited. @@ -30,14 +30,14 @@ * http://www.pulse-eight.net/ */ -#include "../serialport.h" -#include "../baudrate.h" -#include "../timeutils.h" +#include "../sockets/serialport.h" +#include "../util/baudrate.h" +#include "../util/timeutils.h" using namespace std; -using namespace CEC; +using namespace PLATFORM; -void FormatWindowsError(int iErrorCode, string &strMessage) +void FormatWindowsError(int iErrorCode, CStdString &strMessage) { if (iErrorCode != ERROR_SUCCESS) { @@ -48,88 +48,15 @@ void FormatWindowsError(int iErrorCode, string &strMessage) } } -CSerialPort::CSerialPort(void) : - m_handle(INVALID_HANDLE_VALUE), - m_bIsOpen(false), - m_iBaudrate(0), - m_iDatabits(0), - m_iStopbits(0), - m_iParity(0) +bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking) { -} - -CSerialPort::~CSerialPort(void) -{ - Close(); -} - -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) - { - m_error = "Unable to open COM port"; - FormatWindowsError(GetLastError(), m_error); - return false; - } - - COMMCONFIG commConfig = {0}; - DWORD dwSize = sizeof(commConfig); - commConfig.dwSize = dwSize; - if (GetDefaultCommConfig(name.c_str(), &commConfig,&dwSize)) - { - if (!SetCommConfig(m_handle, &commConfig,dwSize)) - { - m_error = "unable to set default config"; - FormatWindowsError(GetLastError(), m_error); - } - } - else - { - m_error = "unable to get default config"; - FormatWindowsError(GetLastError(), m_error); - } - - if (!SetupComm(m_handle, 64, 64)) - { - m_error = "unable to set up the com port"; - FormatWindowsError(GetLastError(), m_error); - } - - m_iDatabits = databits; - m_iStopbits = stopbits; - m_iParity = parity; - if (!SetBaudRate(baudrate)) - { - m_error = "unable to set baud rate"; - FormatWindowsError(GetLastError(), m_error); - Close(); - return false; - } - - if (!SetTimeouts(false)) - { - m_error = "unable to set timeouts"; - FormatWindowsError(GetLastError(), m_error); - Close(); - return false; - } - - m_bIsOpen = true; - return m_bIsOpen; -} - -bool CSerialPort::SetTimeouts(bool bBlocking) -{ - if (m_handle == INVALID_HANDLE_VALUE) + if (socket == INVALID_HANDLE_VALUE) return false; COMMTIMEOUTS cto; - if (!GetCommTimeouts(m_handle, &cto)) + if (!GetCommTimeouts(socket, &cto)) { - m_error = "GetCommTimeouts failed"; - FormatWindowsError(GetLastError(), m_error); + *iError = GetLastError(); return false; } @@ -146,69 +73,92 @@ bool CSerialPort::SetTimeouts(bool bBlocking) cto.ReadTotalTimeoutMultiplier = 0; } - if (!SetCommTimeouts(m_handle, &cto)) + if (!SetCommTimeouts(socket, &cto)) { - m_error = "SetCommTimeouts failed"; - FormatWindowsError(GetLastError(), m_error); + *iError = GetLastError(); return false; } return true; } -void CSerialPort::Close(void) +void CSerialSocket::Close(void) { - CLockObject lock(&m_mutex); - if (m_bIsOpen) - { - CloseHandle(m_handle); - m_bIsOpen = false; - } + SerialSocketClose(m_socket); } -int8_t CSerialPort::Write(CCECAdapterMessagePtr data) +void CSerialSocket::Shutdown(void) { - CLockObject lock(&m_mutex); - DWORD iBytesWritten = 0; - if (!m_bIsOpen) - return -1; + SerialSocketClose(m_socket); +} - if (!WriteFile(m_handle, data->packet.data, data->size(), &iBytesWritten, NULL)) - { - m_error = "Error while writing to COM port"; - FormatWindowsError(GetLastError(), m_error); - return -1; - } +ssize_t CSerialSocket::Write(void* data, size_t len) +{ + return SerialSocketWrite(m_socket, &m_iError, data, len); +} - return (int8_t)iBytesWritten; +ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */) +{ + return SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs); } -int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /* = 0 */) +bool CSerialSocket::Open(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 iReturn; + iTimeoutMs = 0; + CStdString strComPath = "\\\\.\\" + m_strName; + CLockObject lock(m_mutex); + m_socket = CreateFile(strComPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); + if (m_socket == INVALID_HANDLE_VALUE) + { + m_strError = "Unable to open COM port"; + FormatWindowsError(GetLastError(), m_strError); + return false; } - if(!ReadFile(m_handle, data, len, &iBytesRead, NULL) != 0) + COMMCONFIG commConfig = {0}; + DWORD dwSize = sizeof(commConfig); + commConfig.dwSize = dwSize; + if (GetDefaultCommConfig(strComPath.c_str(), &commConfig,&dwSize)) { - m_error = "unable to read from device"; - FormatWindowsError(GetLastError(), m_error); - iReturn = -1; + if (!SetCommConfig(m_socket, &commConfig,dwSize)) + { + m_strError = "unable to set default config"; + FormatWindowsError(GetLastError(), m_strError); + } } else { - iReturn = (int32_t) iBytesRead; + m_strError = "unable to get default config"; + FormatWindowsError(GetLastError(), m_strError); + } + + if (!SetupComm(m_socket, 64, 64)) + { + m_strError = "unable to set up the com port"; + FormatWindowsError(GetLastError(), m_strError); + } + + if (!SetBaudRate(m_iBaudrate)) + { + m_strError = "unable to set baud rate"; + FormatWindowsError(GetLastError(), m_strError); + Close(); + return false; + } + + if (!SetTimeouts(m_socket, &m_iError, false)) + { + m_strError = "unable to set timeouts"; + FormatWindowsError(GetLastError(), m_strError); + Close(); + return false; } - return iReturn; + m_bIsOpen = true; + return m_bIsOpen; } -bool CSerialPort::SetBaudRate(uint32_t baudrate) +bool CSerialSocket::SetBaudRate(uint32_t baudrate) { int32_t rate = IntToBaudrate(baudrate); if (rate < 0) @@ -229,32 +179,26 @@ bool CSerialPort::SetBaudRate(uint32_t baudrate) dcb.fInX = false; dcb.fAbortOnError = true; - if (m_iParity == PAR_NONE) + if (m_iParity == SERIAL_PARITY_NONE) dcb.Parity = NOPARITY; - else if (m_iParity == PAR_EVEN) + else if (m_iParity == SERIAL_PARITY_EVEN) dcb.Parity = EVENPARITY; else dcb.Parity = ODDPARITY; - if (m_iStopbits == 2) + if (m_iStopbits == SERIAL_STOP_BITS_TWO) dcb.StopBits = TWOSTOPBITS; else dcb.StopBits = ONESTOPBIT; - dcb.ByteSize = m_iDatabits; + dcb.ByteSize = (BYTE)m_iDatabits; - if(!SetCommState(m_handle,&dcb)) + if(!SetCommState(m_socket,&dcb)) { - m_error = "SetCommState failed"; - FormatWindowsError(GetLastError(), m_error); + m_strError = "SetCommState failed"; + FormatWindowsError(GetLastError(), m_strError); return false; } return true; } - -bool CSerialPort::IsOpen() -{ - CLockObject lock(&m_mutex); - return m_bIsOpen; -}