KEEN REFACTORING YES VERY MUCH THANK YOU MA'AM

This commit is contained in:
XANTRONIX Development 2015-07-15 21:16:39 -05:00
parent fde6e56d50
commit 1cccfca835
2 changed files with 97 additions and 28 deletions

View file

@ -92,19 +92,10 @@ error_io:
return -1; return -1;
} }
ssize_t patty_kiss_tnc_recv(patty_kiss_tnc *tnc, void **frame, int *port) { static ssize_t tnc_decode(patty_kiss_tnc *tnc, void *frame, size_t *len, int *port) {
size_t i, b; size_t i, b;
int flags = KISS_NONE; int flags = KISS_NONE;
if (tnc_buffer(tnc) < 0) {
goto error_io;
}
/*
* Initialize the frame to be returned to the caller.
*/
memset(tnc->frame, '\0', tnc->bufsz);
/* /*
* The default port is always zero, unless a command byte issued from the * The default port is always zero, unless a command byte issued from the
* TNC in the first byte of the frame body. * TNC in the first byte of the frame body.
@ -144,9 +135,9 @@ ssize_t patty_kiss_tnc_recv(patty_kiss_tnc *tnc, void **frame, int *port) {
if (flags & KISS_ESCAPE) { if (flags & KISS_ESCAPE) {
if (c == PATTY_KISS_TFEND) { if (c == PATTY_KISS_TFEND) {
((unsigned char *)tnc->frame)[b++] = PATTY_KISS_FEND; ((unsigned char *)frame)[b++] = PATTY_KISS_FEND;
} else if (c == PATTY_KISS_TFESC) { } else if (c == PATTY_KISS_TFESC) {
((unsigned char *)tnc->frame)[b++] = PATTY_KISS_FESC; ((unsigned char *)frame)[b++] = PATTY_KISS_FESC;
} else { } else {
errno = EIO; errno = EIO;
@ -167,7 +158,7 @@ ssize_t patty_kiss_tnc_recv(patty_kiss_tnc *tnc, void **frame, int *port) {
break; break;
} }
((unsigned char *)tnc->frame)[b++] = c; ((unsigned char *)frame)[b++] = c;
} }
} }
@ -175,32 +166,61 @@ ssize_t patty_kiss_tnc_recv(patty_kiss_tnc *tnc, void **frame, int *port) {
* Return empty-handed if we do not have a fully book-ended frame. * Return empty-handed if we do not have a fully book-ended frame.
*/ */
if ((flags & KISS_FRAME) == 0) { if ((flags & KISS_FRAME) == 0) {
*frame = NULL; *len = 0;
*port = 0;
return 0; return 0;
} }
if (i) { *len = b;
/*
* As we have indeed found a frame at this point, move everything from
* the buffer not processed up to this point, to the beginning of the
* buffer.
*/
memmove(tnc->buf, ((unsigned char *)tnc->buf) + i, tnc->buflen - i);
/* return i;
* And decrement the buffer length by the number of bytes already
* processed. error_io:
*/ return -1;
tnc->buflen -= i; }
static void tnc_flush(patty_kiss_tnc *tnc, size_t len) {
/*
* Move everything from the buffer not processed up to this point, to the
* beginning of the buffer.
*/
memmove(tnc->buf, ((unsigned char *)tnc->buf) + len, tnc->buflen - len);
/*
* Then, decrement the buffer length by the number of bytes already
* processed.
*/
tnc->buflen -= len;
}
ssize_t patty_kiss_tnc_recv(patty_kiss_tnc *tnc, void **frame, int *port) {
ssize_t decoded;
size_t framelen;
/*
* Fill the buffer with something to parse!
*/
if (tnc_buffer(tnc) < 0) {
goto error_io;
} }
/* /*
* Set the output parameter to the frame buffer. * Initialize the frame to be returned to the caller.
*/ */
memset(tnc->frame, '\0', tnc->bufsz);
if ((decoded = tnc_decode(tnc, tnc->frame, &framelen, port)) < 0) {
goto error_io;
}
if (decoded) {
tnc_flush(tnc, decoded);
}
*frame = tnc->frame; *frame = tnc->frame;
return b; return framelen;
error_io: error_io:
return -1; return -1;

49
src/test.c Normal file
View file

@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <patty/kiss.h>
static void usage(int argc, char **argv, const char *message, ...) {
if (message) {
va_list args;
va_start(args, message);
vfprintf(stderr, message, args);
fprintf(stderr, "\n");
va_end(args);
}
fprintf(stderr, "usage: %s kiss.cap\n", argv[0]);
exit(1);
}
int main(int argc, char **argv) {
patty_kiss_tnc *tnc;
ssize_t len;
void *frame;
int port;
if (argc != 2) {
usage(argc, argv, "No TNC device provided");
}
if ((tnc = patty_kiss_tnc_open(argv[1], 330)) == NULL) {
perror("Unable to open TNC");
exit(127);
}
while ((len = patty_kiss_tnc_recv(tnc, &frame, &port)) > 0) {
write(fileno(stdout), frame, len);
fprintf(stderr, "Got %ld byte frame\n", len);
}
patty_kiss_tnc_close(tnc);
return 0;
}