X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Fposix%2Fserialport.cpp;h=2237b65b6565e629667b86db49f184e2cfed81a8;hb=f9e70e9ff0cd9ccc3e644453fbea988eda75c8cd;hp=cefc21201ef03ee41d17feea11e5cdc3aac9e529;hpb=7068f331f39c119c8d5ef11860e080f12841f2ce;p=deb_libcec.git diff --git a/src/lib/platform/posix/serialport.cpp b/src/lib/platform/posix/serialport.cpp index cefc212..2237b65 100644 --- a/src/lib/platform/posix/serialport.cpp +++ b/src/lib/platform/posix/serialport.cpp @@ -47,20 +47,33 @@ #ifndef IUCLC #define IUCLC 0 #endif +#else +#include #endif + using namespace std; using namespace PLATFORM; void CSerialSocket::Close(void) { if (IsOpen()) + { SocketClose(m_socket); + #if !defined(__APPLE__) && !defined(__FreeBSD__) + dev_unlock(m_strName.c_str(), m_lockPid); + #endif + } } void CSerialSocket::Shutdown(void) { if (IsOpen()) + { SocketClose(m_socket); + #if !defined(__APPLE__) && !defined(__FreeBSD__) + dev_unlock(m_strName.c_str(), m_lockPid); + #endif + } } ssize_t CSerialSocket::Write(void* data, size_t len) @@ -78,32 +91,51 @@ bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) { iTimeoutMs = 0; if (IsOpen()) + { + m_iError = EINVAL; return false; + } if (m_iDatabits != SERIAL_DATA_BITS_FIVE && m_iDatabits != SERIAL_DATA_BITS_SIX && m_iDatabits != SERIAL_DATA_BITS_SEVEN && m_iDatabits != SERIAL_DATA_BITS_EIGHT) { m_strError = "Databits has to be between 5 and 8"; + m_iError = EINVAL; return false; } if (m_iStopbits != SERIAL_STOP_BITS_ONE && m_iStopbits != SERIAL_STOP_BITS_TWO) { m_strError = "Stopbits has to be 1 or 2"; + m_iError = EINVAL; return false; } if (m_iParity != SERIAL_PARITY_NONE && m_iParity != SERIAL_PARITY_EVEN && m_iParity != SERIAL_PARITY_ODD) { m_strError = "Parity has to be none, even or odd"; + m_iError = EINVAL; + return false; + } + + #if !defined(__APPLE__) && !defined(__FreeBSD__) + m_lockPid = dev_lock(m_strName.c_str()); + if (m_lockPid != 0) + { + m_strError = "Couldn't lock the serial port"; + m_iError = EBUSY; return false; } + #endif m_socket = open(m_strName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); if (m_socket == INVALID_SERIAL_SOCKET_VALUE) { m_strError = strerror(errno); + #if !defined(__APPLE__) && !defined(__FreeBSD__) + m_lockPid = dev_unlock(m_strName.c_str(), m_lockPid); + #endif return false; } @@ -150,6 +182,9 @@ bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) if (tcsetattr(m_socket, TCSANOW, &m_options) != 0) { m_strError = strerror(errno); + #if !defined(__APPLE__) && !defined(__FreeBSD__) + m_lockPid = dev_unlock(m_strName.c_str(), m_lockPid); + #endif return false; }