Hi, yes, I'm aware none of this compiles nor makes sense at this time

This commit is contained in:
XANTRONIX Development 2015-07-14 22:27:42 -05:00
parent 78fcd5dec1
commit 1ef84ca2b4
2 changed files with 56 additions and 10 deletions

View file

@ -32,7 +32,7 @@ patty_kiss_tnc *patty_kiss_tnc_open(const char *device, size_t bufsize);
void patty_kiss_tnc_close(patty_kiss_tnc *tnc); void patty_kiss_tnc_close(patty_kiss_tnc *tnc);
ssize_t patty_kiss_read(patty_kiss_tnc *tnc, ssize_t patty_kiss_read(patty_kiss_tnc *tnc,
void *buf, size_t len, int *port); void **buf, int *port);
ssize_t patty_kiss_write(patty_kiss_tnc *tnc, ssize_t patty_kiss_write(patty_kiss_tnc *tnc,
const void *buf, size_t len, int port); const void *buf, size_t len, int port);

View file

@ -1,6 +1,9 @@
#include <sys/types.h> #include <stdlib.h>
#include <sys/uio.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <errno.h> #include <errno.h>
#include <patty/kiss.h> #include <patty/kiss.h>
@ -11,18 +14,61 @@ enum kiss_flags {
KISS_ESCAPE = 0x02 KISS_ESCAPE = 0x02
}; };
ssize_t patty_kiss_read(int fd, void *buf, size_t len, int *port) { struct _patty_kiss_tnc {
int fd;
size_t size;
size_t len;
void * buf;
};
patty_kiss_tnc *patty_kiss_tnc_open(const char *device, size_t bufsize) {
patty_kiss_tnc *tnc;
if ((tnc = malloc(sizeof(*tnc))) == NULL) {
goto error_malloc_tnc;
}
if ((tnc->buf = malloc(bufsize)) == NULL) {
goto error_malloc_buf;
}
if ((tnc->fd = open(device, O_RDWR)) < 0) {
goto error_open;
}
tnc->size = bufsize;
tnc->len = 0;
return tnc;
error_open:
free(tnc->buf);
error_malloc_buf:
free(tnc);
error_malloc_tnc:
return NULL;
}
void patty_kiss_tnc_close(patty_kiss_tnc *tnc) {
close(tnc->fd);
free(tnc->buf);
free(tnc);
}
ssize_t patty_kiss_read(patty_kiss_tnc *tnc, void **frame, int *port) {
int flags = KISS_NONE; int flags = KISS_NONE;
size_t i, b = 0; size_t i, b = 0;
if (read(fd, buf, len) < 0) { if (read(tnc->fd, tnc->buf, len) < 0) {
goto error_io; goto error_io;
} }
*port = 0; *port = 0;
for (i=0; i<len; i++) { for (i=0; i<len; i++) {
unsigned char c = ((char *)buf)[i]; unsigned char c = ((char *)tnc->buf)[i];
if ((flags & KISS_FRAME) == 0) { if ((flags & KISS_FRAME) == 0) {
if (c == PATTY_KISS_FEND) { if (c == PATTY_KISS_FEND) {
@ -34,9 +80,9 @@ ssize_t patty_kiss_read(int fd, void *buf, size_t len, int *port) {
if (flags & KISS_ESCAPE) { if (flags & KISS_ESCAPE) {
if (c == PATTY_KISS_TFEND) { if (c == PATTY_KISS_TFEND) {
((unsigned char *)buf)[b++] = PATTY_KISS_FEND; ((unsigned char *)tnc->buf)[b++] = PATTY_KISS_FEND;
} else if (c == PATTY_KISS_TFESC) { } else if (c == PATTY_KISS_TFESC) {
((unsigned char *)buf)[b++] = PATTY_KISS_FESC; ((unsigned char *)tnc->buf)[b++] = PATTY_KISS_FESC;
} else { } else {
errno = EIO; errno = EIO;
@ -71,7 +117,7 @@ ssize_t patty_kiss_read(int fd, void *buf, size_t len, int *port) {
continue; continue;
} }
((unsigned char *)buf)[b++] = c; ((unsigned char *)tnc->buf)[b++] = c;
} }
} }
@ -89,7 +135,7 @@ static inline ssize_t write_command(int fd, int command, int port) {
return write_byte(fd, ((port & 0x0f) << 4) | (command & 0x0f)); return write_byte(fd, ((port & 0x0f) << 4) | (command & 0x0f));
} }
ssize_t patty_kiss_write(int fd, const void *buf, size_t len, int port) { ssize_t patty_kiss_write(patty_kiss_tnc *tnc, const void *buf, size_t len, int port) {
size_t i, start = 0, end = 0; size_t i, start = 0, end = 0;
unsigned char escape_fend[2] = { PATTY_KISS_FESC, PATTY_KISS_TFEND }; unsigned char escape_fend[2] = { PATTY_KISS_FESC, PATTY_KISS_TFEND };