Things are shaping up
This commit is contained in:
		
							parent
							
								
									7ace126e02
								
							
						
					
					
						commit
						cc01a283fe
					
				
					 4 changed files with 95 additions and 8 deletions
				
			
		| 
						 | 
				
			
			@ -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 */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,19 +1,24 @@
 | 
			
		|||
#ifndef _TABBY_LINK_H
 | 
			
		||||
#define _TABBY_LINK_H
 | 
			
		||||
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <termios.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/ioctl.h>
 | 
			
		||||
 | 
			
		||||
#include <tabby/clock.h>
 | 
			
		||||
 | 
			
		||||
#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);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +1,13 @@
 | 
			
		|||
#ifndef _TABBY_PACKET_H
 | 
			
		||||
#define _TABBY_PACKET_H
 | 
			
		||||
 | 
			
		||||
#define TABBY_PACKET_MAX_LEN 65535
 | 
			
		||||
 | 
			
		||||
#include <tabby/command.h>
 | 
			
		||||
 | 
			
		||||
typedef struct _tabby_packet {
 | 
			
		||||
    uint16_t type,
 | 
			
		||||
             value;
 | 
			
		||||
             command;
 | 
			
		||||
} tabby_packet;
 | 
			
		||||
 | 
			
		||||
#endif /* _TABBY_PACKET_H */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										78
									
								
								src/link.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								src/link.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,78 @@
 | 
			
		|||
#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));
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue