Merge branch 'master' into release
[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 <string>
23 #include <vector>
24 #include <stdint.h>
25 #include "../platform/threads.h"
26
27 #ifndef __WINDOWS__
28 #include <termios.h>
29 #else
30 #include "../util/buffer.h"
31 #endif
32
33 namespace CEC
34 {
35 #define PAR_NONE 0
36 #define PAR_EVEN 1
37 #define PAR_ODD 2
38
39 class CSerialPort
40 {
41 public:
42 CSerialPort();
43 virtual ~CSerialPort();
44
45 bool Open(std::string name, uint32_t baudrate, uint8_t databits = 8, uint8_t stopbits = 1, uint8_t parity = PAR_NONE);
46 bool IsOpen();
47 void Close();
48
49 int32_t Write(std::vector<uint8_t> data)
50 {
51 return Write(&data[0], (uint32_t) data.size());
52 }
53 int32_t Write(uint8_t* data, uint32_t len);
54 int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0);
55
56 std::string GetError() { return m_error; }
57 std::string GetName() { return m_name; }
58
59 private:
60 bool SetBaudRate(uint32_t baudrate);
61
62 std::string m_error;
63 std::string m_name;
64 CMutex m_mutex;
65
66 #ifdef __WINDOWS__
67 bool SetTimeouts(bool bBlocking);
68
69 HANDLE m_handle;
70 bool m_bIsOpen;
71 uint32_t m_iBaudrate;
72 uint8_t m_iDatabits;
73 uint8_t m_iStopbits;
74 uint8_t m_iParity;
75 int64_t m_iTimeout;
76 CecBuffer<uint8_t> m_buffer;
77 HANDLE m_ovHandle;
78 #else
79 struct termios m_options;
80 int m_fd;
81 #endif
82 };
83 };