X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Fposix%2Fserialport.cpp;h=2237b65b6565e629667b86db49f184e2cfed81a8;hb=cc938f166e50b0c9e4fdccf55e0a8cffb2c56602;hp=7af3d0e822a4bb8adf3a6988a193c87fe96f5a77;hpb=9b53a148ade63466655e6e5d8cb0ca2b64a76ae3;p=deb_libcec.git diff --git a/src/lib/platform/posix/serialport.cpp b/src/lib/platform/posix/serialport.cpp index 7af3d0e..2237b65 100644 --- a/src/lib/platform/posix/serialport.cpp +++ b/src/lib/platform/posix/serialport.cpp @@ -35,8 +35,9 @@ #include #include "../sockets/serialport.h" #include "../util/baudrate.h" +#include "../posix/os-socket.h" -#if defined(__APPLE__) +#if defined(__APPLE__) || defined(__FreeBSD__) #ifndef XCASE #define XCASE 0 #endif @@ -46,65 +47,116 @@ #ifndef IUCLC #define IUCLC 0 #endif +#else +#include #endif + using namespace std; using namespace PLATFORM; -CSerialPort::CSerialPort() +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) { - m_bToStdOut = false; + 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) +{ + return IsOpen() ? SocketWrite(m_socket, &m_iError, data, len) : -1; +} + +ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */) +{ + return IsOpen() ? SocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1; } //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 */) +bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) { - m_strName = name; - CLockObject lock(m_mutex); + iTimeoutMs = 0; + if (IsOpen()) + { + m_iError = EINVAL; + return false; + } - if (databits < 5 || databits > 8) + 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 (stopbits != 1 && stopbits != 2) + 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 (parity != PAR_NONE && parity != PAR_EVEN && parity != PAR_ODD) + 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; } - m_socket = open(name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); + #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_SOCKET) + 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; } SocketSetBlocking(m_socket, false); - if (!SetBaudRate(baudrate)) + if (!SetBaudRate(m_iBaudrate)) return false; m_options.c_cflag |= (CLOCAL | CREAD); m_options.c_cflag &= ~HUPCL; m_options.c_cflag &= ~CSIZE; - if (databits == 5) m_options.c_cflag |= CS5; - if (databits == 6) m_options.c_cflag |= CS6; - if (databits == 7) m_options.c_cflag |= CS7; - if (databits == 8) m_options.c_cflag |= CS8; + if (m_iDatabits == SERIAL_DATA_BITS_FIVE) m_options.c_cflag |= CS5; + if (m_iDatabits == SERIAL_DATA_BITS_SIX) m_options.c_cflag |= CS6; + if (m_iDatabits == SERIAL_DATA_BITS_SEVEN) m_options.c_cflag |= CS7; + if (m_iDatabits == SERIAL_DATA_BITS_EIGHT) m_options.c_cflag |= CS8; m_options.c_cflag &= ~PARENB; - if (parity == PAR_EVEN || parity == PAR_ODD) + if (m_iParity == SERIAL_PARITY_EVEN || m_iParity == SERIAL_PARITY_ODD) m_options.c_cflag |= PARENB; - if (parity == PAR_ODD) + if (m_iParity == SERIAL_PARITY_ODD) m_options.c_cflag |= PARODD; #ifdef CRTSCTS @@ -113,20 +165,16 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits /* = 8 * m_options.c_cflag &= ~CNEW_RTSCTS; #endif - if (stopbits == 1) m_options.c_cflag &= ~CSTOPB; + if (m_iStopbits == SERIAL_STOP_BITS_ONE) m_options.c_cflag &= ~CSTOPB; else m_options.c_cflag |= CSTOPB; //I guessed a little here m_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | XCASE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | TOSTOP); - if (parity == PAR_NONE) - { + if (m_iParity == SERIAL_PARITY_NONE) m_options.c_iflag &= ~INPCK; - } else - { m_options.c_iflag |= INPCK | ISTRIP; - } m_options.c_iflag &= ~(IXON | IXOFF | IXANY | BRKINT | INLCR | IGNCR | ICRNL | IUCLC | IMAXBEL); m_options.c_oflag &= ~(OPOST | ONLCR | OCRNL); @@ -134,15 +182,19 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits /* = 8 * 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; } SocketSetBlocking(m_socket, true); + m_bIsOpen = true; return true; } -bool CSerialPort::SetBaudRate(uint32_t baudrate) +bool CSerialSocket::SetBaudRate(uint32_t baudrate) { int rate = IntToBaudrate(baudrate); if (rate == -1) @@ -152,7 +204,7 @@ bool CSerialPort::SetBaudRate(uint32_t baudrate) m_strError = buff; return false; } - + //get the current port attributes if (tcgetattr(m_socket, &m_options) != 0) { @@ -165,7 +217,7 @@ bool CSerialPort::SetBaudRate(uint32_t baudrate) m_strError = strerror(errno); return false; } - + if (cfsetospeed(&m_options, rate) != 0) { m_strError = strerror(errno);