X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Fposix%2Fserialport.cpp;h=71405d474c3a6734a80095705aad3d4f57982dc0;hb=ba65909d0a9c43a1bac71c6182c53f202285cec5;hp=ec8a5c1d48e2f4ea14160ab5bd51719de318a81b;hpb=102120cb9d6b3c723ed44b22ae1c97cb160e0bcc;p=deb_libcec.git diff --git a/src/lib/platform/posix/serialport.cpp b/src/lib/platform/posix/serialport.cpp index ec8a5c1..71405d4 100644 --- a/src/lib/platform/posix/serialport.cpp +++ b/src/lib/platform/posix/serialport.cpp @@ -30,11 +30,11 @@ * http://www.pulse-eight.net/ */ +#include "../os.h" #include #include -#include "../serialport/serialport.h" -#include "../serialport/baudrate.h" -#include "../timeutils.h" +#include "../sockets/serialport.h" +#include "../util/baudrate.h" #if defined(__APPLE__) #ifndef XCASE @@ -52,191 +52,45 @@ using namespace PLATFORM; CSerialPort::CSerialPort() { - m_fd = -1; m_tostdout = false; } -CSerialPort::~CSerialPort() -{ - Close(); -} - -int64_t CSerialPort::Write(uint8_t* data, uint32_t len) -{ - fd_set port; - - CLockObject lock(m_mutex); - if (m_fd == -1) - { - m_error = "port closed"; - return -1; - } - - int64_t byteswritten = 0; - struct timeval *tv; -//TODO -// 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 < len) - { - 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 + byteswritten, len - byteswritten); - if (returnv == -1) - { - m_error = strerror(errno); - return -1; - } - byteswritten += returnv; - } - - //print what's written to stdout for debugging - if (m_tostdout) - { - printf("%s write:", m_name.c_str()); - for (int i = 0; i < byteswritten; i++) - printf(" %02x", data[i]); - - printf("\n"); - } - - return byteswritten; -} - -int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /*= 0*/) -{ - 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; - } - - if (iTimeoutMs > 0) - { - now = GetTimeMs(); - target = now + (int64_t) iTimeoutMs; - } - - while (bytesread < (int32_t) len && (iTimeoutMs == 0 || target > now)) - { - 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(); - } - - //print what's read to stdout for debugging - if (m_tostdout && bytesread > 0) - { - printf("%s read:", m_name.c_str()); - for (int i = 0; i < bytesread; i++) - printf(" %02x", data[i]); - - printf("\n"); - } - - 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) { - m_error = "Databits has to be between 5 and 8"; + m_strError = "Databits has to be between 5 and 8"; return false; } if (stopbits != 1 && stopbits != 2) { - m_error = "Stopbits has to be 1 or 2"; + m_strError = "Stopbits has to be 1 or 2"; return false; } if (parity != PAR_NONE && parity != PAR_EVEN && parity != PAR_ODD) { - m_error = "Parity has to be none, even or odd"; + m_strError = "Parity has to be none, even or odd"; return false; } - m_fd = open(name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); + m_socket = open(name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); - if (m_fd == -1) + if (m_socket == INVALID_SOCKET) { - m_error = strerror(errno); + m_strError = strerror(errno); return false; } - fcntl(m_fd, F_SETFL, 0); + SocketSetBlocking(m_socket, false); if (!SetBaudRate(baudrate)) - { return false; - } m_options.c_cflag |= (CLOCAL | CREAD); m_options.c_cflag &= ~HUPCL; @@ -277,30 +131,17 @@ bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits /* = 8 * 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); return false; } - //non-blocking port - fcntl(m_fd, F_SETFL, FNDELAY); + SocketSetBlocking(m_socket, 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) { int rate = IntToBaudrate(baudrate); @@ -308,34 +149,28 @@ bool CSerialPort::SetBaudRate(uint32_t baudrate) { 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; -}