#include <Debug.h>
#include <Directory.h>
#include <Entry.h>
#include <List.h>
#include <SerialPort.h>
#include <new>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#define SERIAL_DIR "/dev/ports"
static int32
scan_directory(const char *directory, BList *list)
{
BEntry entry;
BDirectory dir(SERIAL_DIR);
char buf[B_OS_NAME_LENGTH];
ASSERT(list != NULL);
while (dir.GetNextEntry(&entry) == B_OK) {
entry.GetName(buf);
list->AddItem(strdup(buf));
};
return list->CountItems();
}
BSerialPort::BSerialPort()
:
ffd(-1),
fBaudRate(B_19200_BPS),
fDataBits(B_DATA_BITS_8),
fStopBits(B_STOP_BIT_1),
fParityMode(B_NO_PARITY),
fFlow(B_HARDWARE_CONTROL),
fTimeout(B_INFINITE_TIMEOUT),
fBlocking(true),
fDevices(new(std::nothrow) BList)
{
_ScanDevices();
}
BSerialPort::~BSerialPort()
{
if (ffd >= 0)
close(ffd);
if (fDevices != NULL) {
for (int32 count = fDevices->CountItems() - 1; count >= 0; count--)
free(fDevices->RemoveItem(count));
delete fDevices;
}
}
status_t
BSerialPort::Open(const char *portName)
{
char buf[64];
if (portName == NULL)
return B_BAD_VALUE;
if (portName[0] != '/')
snprintf(buf, 64, SERIAL_DIR"/%s", portName);
else
snprintf(buf, 64, "%s", portName);
if (ffd >= 0)
close(ffd);
ffd = open(buf, O_RDWR | O_NONBLOCK | O_EXCL);
if (ffd >= 0) {
int flags = fcntl(ffd, F_GETFL);
fcntl(ffd, F_SETFL, flags & ~O_NONBLOCK);
_DriverControl();
}
return (ffd >= 0) ? ffd : errno;
}
void
BSerialPort::Close(void)
{
if (ffd >= 0)
close(ffd);
ffd = -1;
}
ssize_t
BSerialPort::Read(void *buf, size_t count)
{
ssize_t err = read(ffd, buf, count);
return (err >= 0) ? err : errno;
}
ssize_t
BSerialPort::Write(const void *buf, size_t count)
{
ssize_t err = write(ffd, buf, count);
return (err >= 0) ? err : errno;
}
void
BSerialPort::SetBlocking(bool Blocking)
{
fBlocking = Blocking;
_DriverControl();
}
status_t
BSerialPort::SetTimeout(bigtime_t microSeconds)
{
status_t err = B_BAD_VALUE;
if (microSeconds == B_INFINITE_TIMEOUT || microSeconds <= 25000000) {
fTimeout = microSeconds;
_DriverControl();
err = B_OK;
}
return err;
}
status_t
BSerialPort::SetDataRate(data_rate bitsPerSecond)
{
fBaudRate = bitsPerSecond;
return _DriverControl();
}
data_rate
BSerialPort::DataRate(void)
{
return fBaudRate;
}
void
BSerialPort::SetDataBits(data_bits numBits)
{
fDataBits = numBits;
_DriverControl();
}
data_bits
BSerialPort::DataBits(void)
{
return fDataBits;
}
void
BSerialPort::SetStopBits(stop_bits numBits)
{
fStopBits = numBits;
_DriverControl();
}
stop_bits
BSerialPort::StopBits(void)
{
return fStopBits;
}
void
BSerialPort::SetParityMode(parity_mode which)
{
fParityMode = which;
_DriverControl();
}
parity_mode
BSerialPort::ParityMode(void)
{
return fParityMode;
}
void
BSerialPort::ClearInput(void)
{
tcflush(ffd, TCIFLUSH);
}
void
BSerialPort::ClearOutput(void)
{
tcflush(ffd, TCOFLUSH);
}
void
BSerialPort::SetFlowControl(uint32 method)
{
fFlow = method;
_DriverControl();
}
uint32
BSerialPort::FlowControl(void)
{
return fFlow;
}
status_t
BSerialPort::SetDTR(bool asserted)
{
status_t status = ioctl(ffd, TCSETDTR, &asserted, sizeof(asserted));
return (status >= 0) ? status : errno;
}
status_t
BSerialPort::SetRTS(bool asserted)
{
status_t status = ioctl(ffd, TCSETRTS, &asserted, sizeof(asserted));
return (status >= 0) ? status : errno;
}
status_t
BSerialPort::NumCharsAvailable(int32 *numChars)
{
if (ffd < 0)
return B_NO_INIT;
if (numChars)
*numChars = 0;
return B_OK;
}
bool
BSerialPort::IsCTS(void)
{
unsigned int bits = ioctl(ffd, TCGETBITS, 0);
if (bits & TCGB_CTS)
return true;
return false;
}
bool
BSerialPort::IsDSR(void)
{
unsigned int bits = ioctl(ffd, TCGETBITS, 0);
if (bits & TCGB_DSR)
return true;
return false;
}
bool
BSerialPort::IsRI(void)
{
unsigned int bits = ioctl(ffd, TCGETBITS, 0);
if (bits & TCGB_RI)
return true;
return false;
}
bool
BSerialPort::IsDCD(void)
{
unsigned int bits = ioctl(ffd, TCGETBITS, 0);
if (bits & TCGB_DCD)
return true;
return false;
}
ssize_t
BSerialPort::WaitForInput(void)
{
object_wait_info info[1];
info[0].type = B_OBJECT_TYPE_FD;
info[0].object = ffd;
info[0].events = B_EVENT_READ | B_EVENT_ERROR | B_EVENT_DISCONNECTED;
status_t status = wait_for_objects_etc(info, 1, B_RELATIVE_TIMEOUT, fTimeout);
if (status < 0)
return status;
int size;
if (ioctl(ffd, FIONREAD, &size, sizeof(size)) < 0)
return errno;
return size;
}
int32
BSerialPort::CountDevices()
{
int32 count = 0;
_ScanDevices();
if (fDevices != NULL)
count = fDevices->CountItems();
return count;
}
status_t
BSerialPort::GetDeviceName(int32 n, char *name, size_t bufSize)
{
status_t result = B_ERROR;
const char *dev = NULL;
if (fDevices != NULL)
dev = static_cast<char*>(fDevices->ItemAt(n));
if (dev != NULL && name != NULL) {
strncpy(name, dev, bufSize);
name[bufSize - 1] = '\0';
result = B_OK;
}
return result;
}
void
BSerialPort::_ScanDevices()
{
if (fDevices != NULL) {
for (int32 count = fDevices->CountItems() - 1; count >= 0; count--)
free(fDevices->RemoveItem(count));
scan_directory(SERIAL_DIR, fDevices);
}
}
int
BSerialPort::_DriverControl()
{
struct termios options;
int err;
if (ffd < 0)
return B_NO_INIT;
err = tcgetattr(ffd, &options);
if (err < 0)
return errno;
options.c_iflag &= ~(IXON | IXOFF | IXANY | INPCK);
options.c_cflag &= ~(CRTSCTS | CSIZE | CSTOPB | PARODD | PARENB);
options.c_lflag &= ~(ECHO | ECHONL | ISIG | ICANON);
options.c_cflag |= CLOCAL;
if (fFlow & B_HARDWARE_CONTROL)
options.c_cflag |= CRTSCTS;
if (fFlow & B_SOFTWARE_CONTROL)
options.c_iflag |= (IXON | IXOFF);
if (fStopBits & B_STOP_BITS_2)
options.c_cflag |= CSTOPB;
if (fDataBits & B_DATA_BITS_8)
options.c_cflag |= CS8;
if (fParityMode != B_NO_PARITY) {
options.c_cflag |= PARENB;
if (fParityMode == B_ODD_PARITY)
options.c_cflag |= PARODD;
}
cfsetispeed(&options, fBaudRate);
cfsetospeed(&options, fBaudRate);
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 0;
if (fBlocking) {
if (fTimeout == B_INFINITE_TIMEOUT) {
options.c_cc[VMIN] = 1;
} else if (fTimeout != 0) {
int timeout = fTimeout / 100000;
options.c_cc[VTIME] = (timeout == 0) ? 1 : timeout;
}
}
err = tcsetattr(ffd, TCSANOW, &options);
return (err >= 0) ? err : errno;
}
void BSerialPort::_ReservedSerialPort1() {}
void BSerialPort::_ReservedSerialPort2() {}
void BSerialPort::_ReservedSerialPort3() {}
void BSerialPort::_ReservedSerialPort4() {}