test.c (1641B)
1#include "slip.h" 2 3#include <string.h> 4#include <stdarg.h> 5#include <stdio.h> 6 7void 8die(const char *fmt, ...) 9{ 10 va_list ap; 11 12 va_start(ap, fmt); 13 fputs("libslip-test: ", stderr); 14 vfprintf(stderr, fmt, ap); 15 if (*fmt && fmt[strlen(fmt) - 1] == ':') { 16 fputc(' ', stderr); 17 perror(NULL); 18 } else { 19 fputc('\n', stderr); 20 } 21 va_end(ap); 22 23 exit(1); 24} 25 26int 27slip_realloc(void **buf, size_t size) 28{ 29 *buf = realloc(*buf, size); 30 if (!*buf) die("realloc:"); 31 32 return 0; 33} 34 35void 36slip_restart(struct slip *slip, uint8_t b) 37{ 38 printf("RESTART due to '%02x' byte\n", b); 39} 40 41void 42slip_rx(struct slip *slip, uint8_t *data, size_t len, void *user) 43{ 44 printf("PACKET %.*s\n", (int) len, data); 45} 46 47int 48main(int argc, const char **argv) 49{ 50 uint8_t iobuf[BUFSIZ]; 51 uint8_t encbuf[BUFSIZ * 2 + 2]; 52 struct slip slip = { 0 }; 53 size_t enclen, nread; 54 int rc; 55 56 slip.realloc = slip_realloc; 57 slip.rx_packet = slip_rx; 58 slip.rx_restart = slip_restart; 59 slip.esc = '\\'; 60 slip.start = '<'; 61 slip.end = '>'; 62 slip.esc_active['\\'] = true; 63 slip.esc_enc['\\'] = '\\'; 64 slip.esc_active['<'] = true; 65 slip.esc_enc['<'] = '<'; 66 slip.esc_active['>'] = true; 67 slip.esc_enc['>'] = '>'; 68 69 rc = slip_init(&slip); 70 if (rc) die("slip_init"); 71 72 if (argc == 2 && !strcmp(argv[1], "-d")) { 73 while ((nread = fread(iobuf, 1, sizeof(iobuf), stdin))) { 74 rc = slip_decode(&slip, iobuf, nread); 75 if (rc) die("slip_decode"); 76 } 77 } else { 78 while ((nread = fread(iobuf, 1, sizeof(iobuf), stdin))) { 79 rc = slip_encode(&slip, encbuf, &enclen, 80 sizeof(encbuf), iobuf, nread); 81 if (rc) die("slip_encode"); 82 fwrite(encbuf, 1, enclen, stdout); 83 } 84 } 85 86 slip_deinit(&slip); 87}