Refactor src/decode.c in terms of patty_print_frame()
This commit is contained in:
parent
cbea4ad7a8
commit
2c3ff15d16
1 changed files with 4 additions and 134 deletions
138
src/decode.c
138
src/decode.c
|
@ -4,8 +4,8 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <patty/kiss.h>
|
||||
#include <patty/ax25.h>
|
||||
#include <patty/print.h>
|
||||
|
||||
static void usage(int argc, char **argv, const char *message, ...) {
|
||||
if (message) {
|
||||
|
@ -22,136 +22,6 @@ static void usage(int argc, char **argv, const char *message, ...) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
static int callsign_fprint(FILE *stream, const patty_ax25_addr *addr) {
|
||||
char buf[7];
|
||||
uint8_t ssid;
|
||||
|
||||
if (patty_ax25_ntop(addr, buf, &ssid, sizeof(buf)) < 0) {
|
||||
goto error_ntop;
|
||||
}
|
||||
|
||||
fprintf(stream, "%s", buf);
|
||||
|
||||
if (ssid) {
|
||||
fprintf(stream, "/%d", ssid);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_ntop:
|
||||
return -1;
|
||||
}
|
||||
|
||||
#define printable(c) \
|
||||
(c >= 0x20 && c < 0x7f)
|
||||
|
||||
static int hexdump_fprint(FILE *stream, void *data, size_t len) {
|
||||
size_t i;
|
||||
|
||||
for (i=0; i<len; i+=16) {
|
||||
size_t x;
|
||||
|
||||
if (fprintf(stream, "%08lx:", i) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
|
||||
for (x=0; x<16; x++) {
|
||||
if (!(x & 1)) {
|
||||
if (fprintf(stream, " ") < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
|
||||
if (i+x < len) {
|
||||
if (fprintf(stream, "%02x", ((uint8_t *)data)[i+x]) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
} else {
|
||||
if (fprintf(stream, " ") < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fprintf(stream, " ") < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
|
||||
for (x=0; x<16 && i+x<len; x++) {
|
||||
uint8_t c = ((uint8_t *)data)[i+x];
|
||||
|
||||
if (fputc(printable(c)? c: '.', stream) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
|
||||
if (fprintf(stream, "\n") < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_io:
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int address_fprint(FILE *stream, const patty_ax25_frame *frame) {
|
||||
int i;
|
||||
|
||||
callsign_fprint(stream, &frame->src);
|
||||
|
||||
fprintf(stream, " > ");
|
||||
|
||||
for (i=0; i<frame->hops; i++) {
|
||||
callsign_fprint(stream, &frame->repeaters[i]);
|
||||
fprintf(stream, " > ");
|
||||
}
|
||||
|
||||
callsign_fprint(stream, &frame->dest);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int frame_fprint(FILE *stream,
|
||||
const patty_ax25_frame *frame,
|
||||
void *data,
|
||||
size_t len) {
|
||||
if (address_fprint(stream, frame) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
|
||||
if (PATTY_AX25_CONTROL_INFO(frame->control)) {
|
||||
if (fprintf(stream, " type I poll %d ns %d nr %d info %zu",
|
||||
PATTY_AX25_CONTROL_POLL(frame->control),
|
||||
PATTY_AX25_CONTROL_SEQ_SEND(frame->control),
|
||||
PATTY_AX25_CONTROL_SEQ_RECV(frame->control),
|
||||
frame->infolen) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
} else if (PATTY_AX25_CONTROL_UNNUMBERED(frame->control)) {
|
||||
if (fprintf(stream, " type U info %zu",
|
||||
frame->infolen) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
|
||||
if (fprintf(stream, " total %zu bytes\n", len) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
|
||||
if (frame->info) {
|
||||
if (hexdump_fprint(stream, frame->info, frame->infolen) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_io:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
patty_kiss_tnc *tnc;
|
||||
void *buf;
|
||||
|
@ -195,10 +65,10 @@ int main(int argc, char **argv) {
|
|||
goto error_ax25_frame_decode;
|
||||
}
|
||||
|
||||
if (frame_fprint(stdout, &frame, buf, len) < 0) {
|
||||
if (patty_print_frame(stdout, &frame, buf, len) < 0) {
|
||||
perror("Unable to print frame");
|
||||
|
||||
goto error_frame_fprint;
|
||||
goto error_print_frame;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +78,7 @@ int main(int argc, char **argv) {
|
|||
|
||||
return 0;
|
||||
|
||||
error_frame_fprint:
|
||||
error_print_frame:
|
||||
error_ax25_frame_decode:
|
||||
error_kiss_tnc_recv:
|
||||
free(buf);
|
||||
|
|
Loading…
Add table
Reference in a new issue