| 1 | #pragma once |
| 2 | |
| 3 | /* |
| 4 | * boblight |
| 5 | * Copyright (C) Bob 2009 |
| 6 | * |
| 7 | * boblight is free software: you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * boblight is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "os-dependent.h" |
| 22 | #include "baudrate.h" |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | #ifndef __WINDOWS__ |
| 28 | #include <termios.h> |
| 29 | #else |
| 30 | #include "../util/threads.h" |
| 31 | #include "../util/buffer.h" |
| 32 | #endif |
| 33 | |
| 34 | namespace CEC |
| 35 | { |
| 36 | #define PAR_NONE 0 |
| 37 | #define PAR_EVEN 1 |
| 38 | #define PAR_ODD 2 |
| 39 | |
| 40 | class CSerialPort |
| 41 | { |
| 42 | public: |
| 43 | CSerialPort(); |
| 44 | virtual ~CSerialPort(); |
| 45 | |
| 46 | bool Open(std::string name, int baudrate, int databits = 8, int stopbits = 1, int parity = PAR_NONE); |
| 47 | bool SetBaudRate(int baudrate); |
| 48 | bool IsOpen() const; |
| 49 | void Close(); |
| 50 | |
| 51 | int Write(std::vector<uint8_t> data) |
| 52 | { |
| 53 | return Write(&data[0], data.size()); |
| 54 | } |
| 55 | int Write(uint8_t* data, int len); |
| 56 | int Read(uint8_t* data, int len, int iTimeoutMs = -1); |
| 57 | |
| 58 | std::string GetError() { return m_error; } |
| 59 | std::string GetName() { return m_name; } |
| 60 | |
| 61 | private: |
| 62 | std::string m_error; |
| 63 | std::string m_name; |
| 64 | |
| 65 | #ifdef __WINDOWS__ |
| 66 | bool SetTimeouts(bool bBlocking); |
| 67 | |
| 68 | HANDLE m_handle; |
| 69 | bool m_bIsOpen; |
| 70 | int m_iBaudrate; |
| 71 | int m_iDatabits; |
| 72 | int m_iStopbits; |
| 73 | int m_iParity; |
| 74 | int64_t m_iTimeout; |
| 75 | CecBuffer<uint8_t> m_buffer; |
| 76 | HANDLE m_ovHandle; |
| 77 | #else |
| 78 | struct termios m_options; |
| 79 | int m_fd; |
| 80 | #endif |
| 81 | }; |
| 82 | }; |