tabby/src/link.c

77 lines
1.5 KiB
C
Raw Normal View History

2016-05-25 21:17:18 -05:00
#include <tabby/link.h>
2016-05-28 19:07:14 -05:00
#include <tabby/command.h>
2016-05-25 21:17:18 -05:00
int tabby_link_open(const char *dev) {
int fd;
struct termios attr;
2016-05-28 19:07:14 -05:00
if ((fd = open(dev, O_RDWR | O_NOCTTY)) < 0) {
2016-05-25 21:17:18 -05:00
goto error_open;
}
if (fcntl(fd, F_SETFL) < 0) {
goto error_io;
}
attr.c_cflag = CS8 | CREAD;
attr.c_ispeed = TABBY_LINK_BAUD;
attr.c_ospeed = TABBY_LINK_BAUD;
attr.c_iflag = IGNPAR;
attr.c_oflag = 0;
attr.c_lflag = 0;
attr.c_cc[VTIME] = 0;
attr.c_cc[VMIN] = 1;
if (tcsetattr(fd, TCSANOW, &attr) < 0) {
goto error_io;
}
return fd;
error_io:
close(fd);
error_open:
return -1;
}
int tabby_link_close(int fd) {
return close(fd);
}
2016-05-28 19:07:14 -05:00
ssize_t tabby_link_send(int fd, void *buf, uint16_t len) {
uint8_t header[3] = {
2016-05-28 19:07:14 -05:00
TABBY_COMMAND_SEND, (len & 0xff00) >> 8, len & 0xff
2016-05-25 21:17:18 -05:00
};
if (write(fd, &header, sizeof(header)) < 0) {
goto error_write_header;
}
return write(fd, buf, (size_t)len);
error_write_header:
return -1;
}
2016-05-28 19:07:14 -05:00
ssize_t tabby_link_recv(int fd, void *buf, uint16_t len) {
2016-05-25 21:17:18 -05:00
return read(fd, buf, (size_t)len);
}
int tabby_link_set_clock_source(int fd, tabby_clock_source source) {
uint8_t packet[2] = {
TABBY_COMMAND_CLOCK_SOURCE, source
2016-05-25 21:17:18 -05:00
};
return write(fd, &packet, sizeof(packet));
}
int tabby_link_set_clock_speed(int fd, tabby_clock_speed speed) {
uint8_t packet[2] = {
TABBY_COMMAND_CLOCK_SPEED, speed
2016-05-25 21:17:18 -05:00
};
return write(fd, &packet, sizeof(packet));
}