-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.hpp
113 lines (95 loc) · 2.85 KB
/
serial.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once
#include <nall/intrinsics.hpp>
#include <nall/stdint.hpp>
#include <nall/string.hpp>
#if !defined(API_POSIX)
#error "nall/serial: unsupported system"
#endif
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
namespace nall {
struct serial {
~serial() {
close();
}
auto readable() -> bool {
if(!opened) return false;
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(port, &fdset);
timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int result = select(FD_SETSIZE, &fdset, nullptr, nullptr, &timeout);
if(result < 1) return false;
return FD_ISSET(port, &fdset);
}
//-1 on error, otherwise return bytes read
auto read(u8* data, u32 length) -> s32 {
if(!opened) return -1;
return ::read(port, (void*)data, length);
}
auto writable() -> bool {
if(!opened) return false;
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(port, &fdset);
timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int result = select(FD_SETSIZE, nullptr, &fdset, nullptr, &timeout);
if(result < 1) return false;
return FD_ISSET(port, &fdset);
}
//-1 on error, otherwise return bytes written
auto write(const u8* data, u32 length) -> s32 {
if(!opened) return -1;
return ::write(port, (void*)data, length);
}
//rate==0: use flow control (synchronous mode)
//rate!=0: baud-rate (asynchronous mode)
auto open(string device, u32 rate = 0) -> bool {
close();
if(!device) device = "/dev/ttyU0"; //note: default device name is for FreeBSD 10+
port = ::open(device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if(port == -1) return false;
if(ioctl(port, TIOCEXCL) == -1) { close(); return false; }
if(fcntl(port, F_SETFL, 0) == -1) { close(); return false; }
if(tcgetattr(port, &original_attr) == -1) { close(); return false; }
termios attr = original_attr;
cfmakeraw(&attr);
cfsetspeed(&attr, rate ? rate : 57600); //rate value has no effect in synchronous mode
attr.c_lflag &=~ (ECHO | ECHONL | ISIG | ICANON | IEXTEN);
attr.c_iflag &=~ (BRKINT | PARMRK | INPCK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY);
attr.c_iflag |= (IGNBRK | IGNPAR);
attr.c_oflag &=~ (OPOST);
attr.c_cflag &=~ (CSIZE | CSTOPB | PARENB | CLOCAL);
attr.c_cflag |= (CS8 | CREAD);
if(rate) {
attr.c_cflag &= ~CRTSCTS;
} else {
attr.c_cflag |= CRTSCTS;
}
attr.c_cc[VTIME] = attr.c_cc[VMIN] = 0;
if(tcsetattr(port, TCSANOW, &attr) == -1) { close(); return false; }
return opened = true;
}
auto close() -> void {
if(port != -1) {
tcdrain(port);
if(opened) {
tcsetattr(port, TCSANOW, &original_attr);
opened = false;
}
::close(port);
port = -1;
}
}
private:
int port = -1;
bool opened = false;
termios original_attr;
};
}