2cdc10890c9c49396dcdcea56435c5b51bcc0e01
[deb_libcec.git] / src / lib / platform / serialport.h
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 #include "../platform/threads.h"
27
28 #ifndef __WINDOWS__
29 #include <termios.h>
30 #else
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 IsOpen();
48 void Close();
49
50 int Write(std::vector<uint8_t> data)
51 {
52 return Write(&data[0], data.size());
53 }
54 int Write(uint8_t* data, int len);
55 int Read(uint8_t* data, int len, int iTimeoutMs = -1);
56
57 std::string GetError() { return m_error; }
58 std::string GetName() { return m_name; }
59
60 private:
61 bool SetBaudRate(int baudrate);
62
63 std::string m_error;
64 std::string m_name;
65 CMutex m_mutex;
66
67 #ifdef __WINDOWS__
68 bool SetTimeouts(bool bBlocking);
69
70 HANDLE m_handle;
71 bool m_bIsOpen;
72 int m_iBaudrate;
73 int m_iDatabits;
74 int m_iStopbits;
75 int m_iParity;
76 int64_t m_iTimeout;
77 CecBuffer<uint8_t> m_buffer;
78 HANDLE m_ovHandle;
79 #else
80 struct termios m_options;
81 int m_fd;
82 #endif
83 };
84 };