Commit | Line | Data |
---|---|---|
abbca718 LOK |
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 | ||
26 | #ifndef __WINDOWS__ | |
27 | #include <termios.h> | |
28 | #else | |
29 | #include "../util/threads.h" | |
30 | #include "../util/buffer.h" | |
31 | ||
32 | class CSerialPort; | |
33 | ||
34 | typedef struct serial_cancel_options | |
35 | { | |
36 | CSerialPort *instance; | |
37 | uint64_t iWaitMs; | |
38 | } serial_cancel_options; | |
39 | ||
40 | #endif | |
41 | ||
42 | #define PAR_NONE 0 | |
43 | #define PAR_EVEN 1 | |
44 | #define PAR_ODD 2 | |
45 | ||
46 | #include "baudrate.h" | |
47 | ||
48 | class CSerialPort | |
49 | { | |
50 | public: | |
51 | CSerialPort(); | |
52 | virtual ~CSerialPort(); | |
53 | ||
54 | bool Open(std::string name, int baudrate, int databits = 8, int stopbits = 1, int parity = PAR_NONE); | |
55 | void Close(); | |
56 | int Write(std::vector<uint8_t> data) | |
57 | { | |
58 | return Write(&data[0], data.size()); | |
59 | } | |
60 | ||
61 | int Write(uint8_t* data, int len); | |
62 | int Read(uint8_t* data, int len, int iTimeoutMs = -1); | |
63 | ||
64 | std::string GetError() { return m_name + ": " + m_error; } | |
65 | std::string GetName() { return m_name; } | |
66 | ||
67 | bool SetBaudRate(int baudrate); | |
68 | ||
69 | int IntToRate(int baudrate) | |
70 | { | |
71 | for (unsigned int i = 0; i < sizeof(baudrates) / sizeof(sbaudrate) - 1; i++) | |
72 | { | |
73 | if (baudrates[i].rate == baudrate) | |
74 | { | |
75 | return baudrates[i].symbol; | |
76 | } | |
77 | } | |
78 | return -1; | |
79 | }; | |
80 | ||
81 | #ifdef __WINDOWS__ | |
82 | bool IsOpen() const { return m_bIsOpen; } | |
83 | #else | |
84 | bool IsOpen() const { return m_fd != -1; } | |
85 | #endif | |
86 | ||
87 | private: | |
88 | std::string m_error; | |
89 | std::string m_name; | |
90 | ||
91 | #ifdef __WINDOWS__ | |
92 | bool SetTimeouts(bool bBlocking); | |
93 | ||
94 | HANDLE m_handle; | |
95 | bool m_bIsOpen; | |
96 | int m_iBaudrate; | |
97 | int m_iDatabits; | |
98 | int m_iStopbits; | |
99 | int m_iParity; | |
100 | int64_t m_iTimeout; | |
101 | CecBuffer<uint8_t> m_buffer; | |
102 | HANDLE m_ovHandle; | |
103 | #else | |
104 | struct termios m_options; | |
105 | int m_fd; | |
106 | #endif | |
107 | }; |