diff --git a/include/tabby/command.h b/include/tabby/command.h index cf46f13..9829ee3 100644 --- a/include/tabby/command.h +++ b/include/tabby/command.h @@ -2,9 +2,9 @@ #define _TABBY_COMMAND_H typedef enum tabby_command { - TABBY_COMMAND_SEND = 0x01, - TABBY_COMMAND_CLOCK_MODE = 0x02, - TABBY_COMMAND_CLOCK_SPEED = 0x03 + TABBY_COMMAND_SEND = 0x01, + TABBY_COMMAND_CLOCK_SOURCE = 0x02, + TABBY_COMMAND_CLOCK_SPEED = 0x03 }; #endif /* _TABBY_COMMAND_H */ diff --git a/include/tabby/link.h b/include/tabby/link.h index 57f8952..9c9aa79 100644 --- a/include/tabby/link.h +++ b/include/tabby/link.h @@ -1,19 +1,24 @@ #ifndef _TABBY_LINK_H #define _TABBY_LINK_H +#include +#include +#include +#include #include +#include #include -#define TABBY_LINK_BAUD 115200 +#define TABBY_LINK_BAUD B115200 int tabby_link_open(const char *dev); -void tabby_link_close(int fd); +int tabby_link_close(int fd); -int tabby_link_send(int fd, char *buf, size_t len); +ssize_t tabby_link_send(int fd, char *buf, uint16_t len); -ssize_t tabby_link_recv(int fd, char *buf, size_t len); +ssize_t tabby_link_recv(int fd, char *buf, uint16_t len); int tabby_link_set_clock_source(int fd, tabby_clock_source source); diff --git a/include/tabby/packet.h b/include/tabby/packet.h index dc9420a..f94c2ae 100644 --- a/include/tabby/packet.h +++ b/include/tabby/packet.h @@ -1,9 +1,13 @@ #ifndef _TABBY_PACKET_H #define _TABBY_PACKET_H +#define TABBY_PACKET_MAX_LEN 65535 + +#include + typedef struct _tabby_packet { uint16_t type, - value; + command; } tabby_packet; #endif /* _TABBY_PACKET_H */ diff --git a/src/link.c b/src/link.c new file mode 100644 index 0000000..3df40b9 --- /dev/null +++ b/src/link.c @@ -0,0 +1,78 @@ +#include + +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)); +}