old-nall/serial.hpp

114 lines
2.9 KiB
C++
Raw Normal View History

2016-05-15 10:42:10 +00:00
#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 {
2016-07-03 11:35:22 +00:00
if(!opened) return false;
2016-05-15 10:42:10 +00:00
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(uint8_t* data, uint length) -> int {
2016-07-03 11:35:22 +00:00
if(!opened) return -1;
2016-05-15 10:42:10 +00:00
return ::read(port, (void*)data, length);
}
auto writable() -> bool {
2016-07-03 11:35:22 +00:00
if(!opened) return false;
2016-05-15 10:42:10 +00:00
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 uint8_t* data, uint length) -> int {
2016-07-03 11:35:22 +00:00
if(!opened) return -1;
2016-05-15 10:42:10 +00:00
return ::write(port, (void*)data, length);
}
2016-07-03 11:35:22 +00:00
//rate==0: use flow control (synchronous mode)
//rate!=0: baud-rate (asynchronous mode)
auto open(string device, uint rate = 0) -> bool {
2016-05-15 10:42:10 +00:00
close();
2016-07-03 11:35:22 +00:00
if(!device) device = "/dev/ttyU0"; //note: default device name is for FreeBSD 10+
port = ::open(device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
2016-05-15 10:42:10 +00:00
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);
2016-07-03 11:35:22 +00:00
cfsetspeed(&attr, rate ? rate : 57600); //rate value has no effect in synchronous mode
2016-05-15 10:42:10 +00:00
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);
2016-07-03 11:35:22 +00:00
if(rate) {
2016-05-15 10:42:10 +00:00
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; }
2016-07-03 11:35:22 +00:00
return opened = true;
2016-05-15 10:42:10 +00:00
}
auto close() -> void {
if(port != -1) {
tcdrain(port);
2016-07-03 11:35:22 +00:00
if(opened) {
2016-05-15 10:42:10 +00:00
tcsetattr(port, TCSANOW, &original_attr);
2016-07-03 11:35:22 +00:00
opened = false;
2016-05-15 10:42:10 +00:00
}
::close(port);
port = -1;
}
}
private:
2016-07-03 11:35:22 +00:00
int port = -1;
bool opened = false;
2016-05-15 10:42:10 +00:00
termios original_attr;
};
}