76 lines
1.5 KiB
C
76 lines
1.5 KiB
C
#include <tabby/link.h>
|
|
#include <tabby/command.h>
|
|
|
|
int tabby_link_open(const char *dev) {
|
|
int fd;
|
|
struct termios attr;
|
|
|
|
if ((fd = open(dev, 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, void *buf, uint16_t len) {
|
|
uint8_t header[3] = {
|
|
TABBY_COMMAND_SEND, (len & 0xff00) >> 8, len & 0xff
|
|
};
|
|
|
|
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, void *buf, uint16_t len) {
|
|
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
|
|
};
|
|
|
|
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
|
|
};
|
|
|
|
return write(fd, &packet, sizeof(packet));
|
|
}
|