X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Fposix%2Fserialport.cpp;h=1e0e364fc8eb2dd5a98738117bd44730b34c5f23;hb=58691fb86d4dd7daac8b283a1bf1f125f00847a5;hp=f0408574dad1670238a571ab0dcadfb8acedcff3;hpb=6f14b51266d3af3f215171a4a5fca5be011c023e;p=deb_libcec.git diff --git a/src/lib/platform/posix/serialport.cpp b/src/lib/platform/posix/serialport.cpp index f040857..1e0e364 100644 --- a/src/lib/platform/posix/serialport.cpp +++ b/src/lib/platform/posix/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-2013 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,13 +30,14 @@ * http://www.pulse-eight.net/ */ +#include "env.h" #include #include -#include "../serialport.h" -#include "../baudrate.h" -#include "../timeutils.h" +#include "lib/platform/sockets/serialport.h" +#include "lib/platform/util/baudrate.h" +#include "lib/platform/posix/os-socket.h" -#if defined(__APPLE__) +#if defined(__APPLE__) || defined(__FreeBSD__) #ifndef XCASE #define XCASE 0 #endif @@ -46,210 +47,121 @@ #ifndef IUCLC #define IUCLC 0 #endif +#else +#ifdef HAVE_LOCKDEV +#include +#endif #endif -using namespace std; -using namespace CEC; -CSerialPort::CSerialPort() -{ - m_fd = -1; - m_tostdout = false; -} +using namespace std; +using namespace PLATFORM; -CSerialPort::~CSerialPort() +inline bool RemoveLock(const char *strDeviceName) { - Close(); + #if !defined(__APPLE__) && !defined(__FreeBSD__) && defined(HAVE_LOCKDEV) + return dev_unlock(strDeviceName, 0) == 0; + #else + (void)strDeviceName; // silence unused warning + return true; + #endif } -int8_t CSerialPort::Write(CCECAdapterMessage *data) +void CSerialSocket::Close(void) { - fd_set port; - - CLockObject lock(&m_mutex); - if (m_fd == -1) - { - m_error = "port closed"; - return -1; - } - - int32_t byteswritten = 0; - - 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 < (int32_t) data->size()) + if (IsOpen()) { - 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->packet.data + byteswritten, data->size() - byteswritten); - if (returnv == -1) - { - m_error = strerror(errno); - return -1; - } - byteswritten += returnv; + SocketClose(m_socket); + RemoveLock(m_strName.c_str()); } +} - //print what's written to stdout for debugging - if (m_tostdout) +void CSerialSocket::Shutdown(void) +{ + if (IsOpen()) { - printf("%s write:", m_name.c_str()); - for (int i = 0; i < byteswritten; i++) - printf(" %02x", data->at(i)); - - printf("\n"); + SocketClose(m_socket); + RemoveLock(m_strName.c_str()); } - - return byteswritten; } -int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /*= 0*/) +ssize_t CSerialSocket::Write(void* data, size_t len) { - 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; - } + return IsOpen() ? SocketWrite(m_socket, &m_iError, data, len) : -1; +} - if (iTimeoutMs > 0) - { - now = GetTimeMs(); - target = now + (int64_t) iTimeoutMs; - } +ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */) +{ + return IsOpen() ? SocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1; +} - while (bytesread < (int32_t) len && (iTimeoutMs == 0 || target > now)) +//setting all this stuff up is a pain in the ass +bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) +{ + iTimeoutMs = 0; if (!iTimeoutMs){} // silence unused warning + if (IsOpen()) { - 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(); + m_iError = EINVAL; + return false; } - //print what's read to stdout for debugging - if (m_tostdout && bytesread > 0) + 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) { - printf("%s read:", m_name.c_str()); - for (int i = 0; i < bytesread; i++) - printf(" %02x", data[i]); - - printf("\n"); + m_strError = "Databits has to be between 5 and 8"; + m_iError = EINVAL; + return false; } - 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) + if (m_iStopbits != SERIAL_STOP_BITS_ONE && m_iStopbits != SERIAL_STOP_BITS_TWO) { - m_error = "Databits has to be between 5 and 8"; + m_strError = "Stopbits has to be 1 or 2"; + m_iError = EINVAL; return false; } - if (stopbits != 1 && stopbits != 2) + if (m_iParity != SERIAL_PARITY_NONE && m_iParity != SERIAL_PARITY_EVEN && m_iParity != SERIAL_PARITY_ODD) { - m_error = "Stopbits has to be 1 or 2"; + m_strError = "Parity has to be none, even or odd"; + m_iError = EINVAL; return false; } - if (parity != PAR_NONE && parity != PAR_EVEN && parity != PAR_ODD) + #if !defined(__APPLE__) && !defined(__FreeBSD__) && defined(HAVE_LOCKDEV) + if (dev_lock(m_strName.c_str()) != 0) { - m_error = "Parity has to be none, even or odd"; + m_strError = "Couldn't lock the serial port"; + m_iError = EBUSY; return false; } + #endif - m_fd = open(name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); + m_socket = open(m_strName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); - if (m_fd == -1) + if (m_socket == INVALID_SERIAL_SOCKET_VALUE) { - m_error = strerror(errno); + m_strError = strerror(errno); + RemoveLock(m_strName.c_str()); return false; } - fcntl(m_fd, F_SETFL, 0); + 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 @@ -258,83 +170,62 @@ 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); - if (tcsetattr(m_fd, TCSANOW, &m_options) != 0) + if (tcsetattr(m_socket, TCSANOW, &m_options) != 0) { - m_error = strerror(errno); + m_strError = strerror(errno); + RemoveLock(m_strName.c_str()); return false; } - //non-blocking port - fcntl(m_fd, F_SETFL, FNDELAY); + SocketSetBlocking(m_socket, true); + m_bIsOpen = 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) +bool CSerialSocket::SetBaudRate(uint32_t baudrate) { int rate = IntToBaudrate(baudrate); if (rate == -1) { 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; -}