tabby/src/link.c

79 lines
1.6 KiB
C
Raw Normal View History

2016-05-25 21:17:18 -05:00
#include <tabby/link.h>
int tabby_link_open(const char *dev) {
int fd;
struct termios attr;
if ((fd = open(device, O_RDWR | O_NOCTTY)) < 0) {
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);
}
ssize_t tabby_link_send(int fd, char *buf, uint16_t len) {
tabby_packet header = {
.type = htobe16(TABBY_COMMAND_SEND),
.value = htobe16(len)
};
if (write(fd, &header, sizeof(header)) < 0) {
goto error_write_header;
}
return write(fd, buf, (size_t)len);
error_write_header:
return -1;
}
ssize_t tabby_link_recv(int fd, char *buf, uint16_t len) {
return read(fd, buf, (size_t)len);
}
int tabby_link_set_clock_source(int fd, tabby_clock_source source) {
tabby_packet packet = {
.command = htobe16(TABBY_COMMAND_CLOCK_SOURCE),
.value = htobe16(source)
};
return write(fd, &packet, sizeof(packet));
}
int tabby_link_set_clock_speed(int fd, tabby_clock_speed speed) {
tabby_packet packet = {
.command = htobe16(TABBY_COMMAND_CLOCK_SPEED),
.value = htobe16(speed)
};
return write(fd, &packet, sizeof(packet));
}