2015-07-14 22:27:42 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2015-07-14 16:45:03 +00:00
|
|
|
#include <sys/types.h>
|
2015-07-14 22:27:42 -05:00
|
|
|
#include <sys/stat.h>
|
2015-07-14 16:45:03 +00:00
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2015-07-12 02:16:26 -05:00
|
|
|
#include <patty/kiss.h>
|
|
|
|
|
|
|
|
enum kiss_flags {
|
|
|
|
KISS_NONE = 0x00,
|
|
|
|
KISS_FRAME = 0x01,
|
|
|
|
KISS_ESCAPE = 0x02
|
|
|
|
};
|
|
|
|
|
2015-07-14 22:27:42 -05:00
|
|
|
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) {
|
2015-07-12 02:16:26 -05:00
|
|
|
int flags = KISS_NONE;
|
|
|
|
size_t i, b = 0;
|
|
|
|
|
2015-07-14 22:27:42 -05:00
|
|
|
if (read(tnc->fd, tnc->buf, len) < 0) {
|
2015-07-12 12:00:35 -05:00
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
2015-07-12 02:28:31 -05:00
|
|
|
*port = 0;
|
2015-07-12 02:16:26 -05:00
|
|
|
|
2015-07-12 02:28:31 -05:00
|
|
|
for (i=0; i<len; i++) {
|
2015-07-14 22:27:42 -05:00
|
|
|
unsigned char c = ((char *)tnc->buf)[i];
|
2015-07-12 02:16:26 -05:00
|
|
|
|
2015-07-14 16:45:03 +00:00
|
|
|
if ((flags & KISS_FRAME) == 0) {
|
2015-07-12 02:16:26 -05:00
|
|
|
if (c == PATTY_KISS_FEND) {
|
|
|
|
flags |= KISS_FRAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & KISS_ESCAPE) {
|
|
|
|
if (c == PATTY_KISS_TFEND) {
|
2015-07-14 22:27:42 -05:00
|
|
|
((unsigned char *)tnc->buf)[b++] = PATTY_KISS_FEND;
|
2015-07-12 02:16:26 -05:00
|
|
|
} else if (c == PATTY_KISS_TFESC) {
|
2015-07-14 22:27:42 -05:00
|
|
|
((unsigned char *)tnc->buf)[b++] = PATTY_KISS_FESC;
|
2015-07-12 02:18:08 -05:00
|
|
|
} else {
|
2015-07-12 12:00:35 -05:00
|
|
|
errno = EIO;
|
|
|
|
|
2015-07-12 02:18:08 -05:00
|
|
|
goto error_io;
|
2015-07-12 02:16:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
flags &= ~KISS_ESCAPE;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & KISS_FRAME) {
|
|
|
|
if (c == PATTY_KISS_FESC) {
|
|
|
|
flags |= KISS_ESCAPE;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
} else if (c == PATTY_KISS_FEND) {
|
|
|
|
flags &= ~KISS_FRAME;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-12 02:28:31 -05:00
|
|
|
/*
|
|
|
|
* Not all KISS TNCs will emit a type byte at the start of a frame
|
|
|
|
* to the host; if the low nybble has a value of zero, then presume
|
|
|
|
* the high nybble to be the radio port number from which the
|
|
|
|
* packet originated.
|
|
|
|
*/
|
2015-07-14 16:45:03 +00:00
|
|
|
if ((c & 0x0f) == 0) {
|
2015-07-12 02:28:31 -05:00
|
|
|
*port = (c & 0xf0) >> 4;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-14 22:27:42 -05:00
|
|
|
((unsigned char *)tnc->buf)[b++] = c;
|
2015-07-12 02:16:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 02:28:31 -05:00
|
|
|
return b + 1;
|
2015-07-12 02:18:08 -05:00
|
|
|
|
|
|
|
error_io:
|
|
|
|
return -1;
|
2015-07-12 02:16:26 -05:00
|
|
|
}
|
2015-07-12 15:34:40 -05:00
|
|
|
|
2015-07-12 23:48:12 -05:00
|
|
|
static inline ssize_t write_byte(int fd, unsigned char c) {
|
|
|
|
return write(fd, &c, sizeof(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ssize_t write_command(int fd, int command, int port) {
|
|
|
|
return write_byte(fd, ((port & 0x0f) << 4) | (command & 0x0f));
|
|
|
|
}
|
|
|
|
|
2015-07-14 22:27:42 -05:00
|
|
|
ssize_t patty_kiss_write(patty_kiss_tnc *tnc, const void *buf, size_t len, int port) {
|
2015-07-12 23:48:12 -05:00
|
|
|
size_t i, start = 0, end = 0;
|
2015-07-12 15:34:40 -05:00
|
|
|
|
|
|
|
unsigned char escape_fend[2] = { PATTY_KISS_FESC, PATTY_KISS_TFEND };
|
|
|
|
unsigned char escape_fesc[2] = { PATTY_KISS_FESC, PATTY_KISS_TFESC };
|
|
|
|
|
2015-07-12 23:48:12 -05:00
|
|
|
if (write_byte(fd, PATTY_KISS_FEND) < 0) {
|
|
|
|
goto error_io;
|
|
|
|
}
|
2015-07-12 15:34:40 -05:00
|
|
|
|
2015-07-14 16:45:03 +00:00
|
|
|
if (write_command(fd, PATTY_KISS_DATA, port) < 0) {
|
2015-07-12 15:34:40 -05:00
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
unsigned char c = ((unsigned char *)buf)[i];
|
2015-07-12 23:48:12 -05:00
|
|
|
unsigned char *escape = NULL;
|
2015-07-12 15:34:40 -05:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case PATTY_KISS_FEND: {
|
2015-07-12 23:48:12 -05:00
|
|
|
escape = escape_fend; break;
|
2015-07-12 15:34:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case PATTY_KISS_FESC: {
|
2015-07-12 23:48:12 -05:00
|
|
|
escape = escape_fesc; break;
|
2015-07-12 15:34:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
2015-07-12 23:48:12 -05:00
|
|
|
end = i;
|
2015-07-14 16:45:03 +00:00
|
|
|
|
|
|
|
if (write(fd, &c, 1) < 0) {
|
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2015-07-12 15:34:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 23:48:12 -05:00
|
|
|
if (escape) {
|
|
|
|
if (write(fd, ((unsigned char *)buf) + start, end - start) < 0) {
|
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, escape, 2) < 0) {
|
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
escape = NULL;
|
|
|
|
start = i + 1;
|
|
|
|
end = start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end - start) {
|
|
|
|
if (write(fd, ((unsigned char *)buf) + start, end - start) < 0) {
|
2015-07-12 15:34:40 -05:00
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 23:48:12 -05:00
|
|
|
if (write_byte(fd, PATTY_KISS_FEND) < 0) {
|
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
2015-07-12 15:34:40 -05:00
|
|
|
|
|
|
|
error_io:
|
|
|
|
return -1;
|
|
|
|
}
|