1 #pragma once 2 /* 3 * This file is part of the libCEC(R) library. 4 * 5 * libCEC(R) is Copyright (C) 2011-2015 Pulse-Eight Limited. All rights reserved. 6 * libCEC(R) is an original work, containing original code. 7 * 8 * libCEC(R) is a trademark of Pulse-Eight Limited. 9 * 10 * This program is dual-licensed; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 23 * 02110-1301 USA 24 * 25 * 26 * Alternatively, you can license this library under a commercial license, 27 * please contact Pulse-Eight Licensing for more information. 28 * 29 * For more information contact: 30 * Pulse-Eight Licensing <license@pulse-eight.com> 31 * http://www.pulse-eight.com/ 32 * http://www.pulse-eight.net/ 33 */ 34 35 #include "env.h" 36 #include "p8-platform/util/buffer.h" 37 38 #include <string> 39 #include <stdint.h> 40 41 #if !defined(__WINDOWS__) 42 #include <termios.h> 43 #endif 44 45 #include "p8-platform/sockets/socket.h" 46 47 namespace P8PLATFORM 48 { 49 enum SerialParity 50 { 51 SERIAL_PARITY_NONE = 0, 52 SERIAL_PARITY_EVEN, 53 SERIAL_PARITY_ODD 54 }; 55 56 enum SerialStopBits 57 { 58 SERIAL_STOP_BITS_ONE = 1, 59 SERIAL_STOP_BITS_TWO = 2 60 }; 61 62 enum SerialDataBits 63 { 64 SERIAL_DATA_BITS_FIVE = 5, 65 SERIAL_DATA_BITS_SIX = 6, 66 SERIAL_DATA_BITS_SEVEN = 7, 67 SERIAL_DATA_BITS_EIGHT = 8 68 }; 69 70 class CSerialSocket : public CCommonSocket<serial_socket_t> 71 { 72 public: 73 CSerialSocket(const std::string &strName, uint32_t iBaudrate, SerialDataBits iDatabits = SERIAL_DATA_BITS_EIGHT, SerialStopBits iStopbits = SERIAL_STOP_BITS_ONE, SerialParity iParity = SERIAL_PARITY_NONE) : 74 CCommonSocket<serial_socket_t>(INVALID_SERIAL_SOCKET_VALUE, strName), 75 #ifdef __WINDOWS__ 76 m_iCurrentReadTimeout(MAXDWORD), 77 #endif 78 m_bIsOpen(false), 79 m_iBaudrate(iBaudrate), 80 m_iDatabits(iDatabits), 81 m_iStopbits(iStopbits), 82 m_iParity(iParity) {} 83 ~CSerialSocket(void)84 virtual ~CSerialSocket(void) { Close(); } 85 86 virtual bool Open(uint64_t iTimeoutMs = 0); 87 virtual void Close(void); 88 virtual void Shutdown(void); 89 virtual ssize_t Write(void* data, size_t len); 90 virtual ssize_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0); 91 IsOpen(void)92 virtual bool IsOpen(void) 93 { 94 return m_socket != INVALID_SERIAL_SOCKET_VALUE && 95 m_bIsOpen; 96 } 97 98 virtual bool SetBaudRate(uint32_t baudrate); 99 100 protected: 101 #ifndef __WINDOWS__ 102 struct termios m_options; 103 #else 104 bool SetTimeouts(serial_socket_t socket, int* iError, DWORD iTimeoutMs); 105 DWORD m_iCurrentReadTimeout; 106 #endif 107 108 bool m_bIsOpen; 109 uint32_t m_iBaudrate; 110 SerialDataBits m_iDatabits; 111 SerialStopBits m_iStopbits; 112 SerialParity m_iParity; 113 }; 114 115 class CSerialPort : public CProtectedSocket<CSerialSocket> 116 { 117 public: 118 CSerialPort(const std::string &strName, uint32_t iBaudrate, SerialDataBits iDatabits = SERIAL_DATA_BITS_EIGHT, SerialStopBits iStopbits = SERIAL_STOP_BITS_ONE, SerialParity iParity = SERIAL_PARITY_NONE) : 119 CProtectedSocket<CSerialSocket> (new CSerialSocket(strName, iBaudrate, iDatabits, iStopbits, iParity)) {} ~CSerialPort(void)120 virtual ~CSerialPort(void) {} 121 }; 122 }; 123