hdlcverify.c 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/*
 * Written by Mark Spencer <markster@digium.com>
 * Based on previous works, designs, and architectures conceived and
 * written by Jim Dixon <jim@lambdatel.com>.
 *
 * Copyright (C) 2001 Jim Dixon / Zapata Telephony.
 * Copyright (C) 2001-2008 Digium, Inc.
 *
 * All rights reserved.
 *
 * Primary Author: Mark Spencer <markster@digium.com>
 * Radio Support by Jim Dixon <jim@lambdatel.com>
 */

/*
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2 as published by the
 * Free Software Foundation. See the LICENSE file included with
 * this program for more details.
 */

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>

#define FAST_HDLC_NEED_TABLES
#include <dahdi/fasthdlc.h>

#include "dahdi_tools_version.h"

int myread(int fd, unsigned char *buf, int len)
{
	int sofar;
	int res;
	sofar = 0;
	while(sofar < len) {
		res = read(fd, buf + sofar, len - sofar);
		if (res < 0)
			return res;
		sofar += res;
	}
	return sofar;
}

static inline unsigned char nextchar(int fd)
{
	static unsigned char inbuf[2048];
	static int bytes = 0;
	static int pos = 0;
	if (pos >= bytes) {
		pos = 0;
		bytes = read(fd, inbuf, sizeof(inbuf));
		if (bytes < 0) {
			fprintf(stderr, "Unable to read more data: %s\n", strerror(errno));
			exit(1);
		}
		if (bytes == 0) {
			fprintf(stderr, "-- END OF DATA --\n");
			exit(0);
		}
	}
	return inbuf[pos++];
}

int main(int argc, char *argv[])
{
	unsigned char decbuf[1024];
	unsigned char actual[1024];
	int res;
	int datain;
	int hdlcin;
	int hdlccnt;
	int x;
	struct fasthdlc_state receiver;
	
	fasthdlc_precalc();
	
	fasthdlc_init(&receiver, FASTHDLC_MODE_64);
	
	hdlcin = open("random.hdlc", O_RDONLY);
	if (hdlcin < 0) {
		fprintf(stderr, "Unable to open %s: %s\n", "random.hdlc", strerror(errno));
		exit(1);
	}
	datain = open("random.raw", O_RDONLY);
	if (datain < 0) {
		fprintf(stderr, "Unable to open random.raw: %s\n", strerror(errno));
		exit(1);
	}
	hdlccnt = 0;
	for (;;) {
		/* Feed in some input */
		if (fasthdlc_rx_load(&receiver, nextchar(hdlcin))) {
			fprintf(stderr, "Unable to feed receiver :(\n");
			exit(1);
		}
		res = fasthdlc_rx_run(&receiver);
		if (res & RETURN_EMPTY_FLAG)
			continue;
		if (res & RETURN_COMPLETE_FLAG) {
			if (hdlccnt) {
				if (argc > 1)
					printf("Got message of length %d\n", hdlccnt);
				res = myread(datain, actual, hdlccnt);
				if (res != hdlccnt) {
					fprintf(stderr, "Tried to read %d bytes, but read %d instead\n", hdlccnt, res);
					exit(1);
				}
				for (x=0;x<hdlccnt;x++) {
					if (actual[x] != decbuf[x]) {
						fprintf(stderr, "Found discrepancy at offset %d\n", x);
						exit(1);
					}
				}
				/* Reset message receiver */
				hdlccnt = 0;
			}
		} else if (res & RETURN_DISCARD_FLAG) {
			if (1 || hdlccnt) {
				fprintf(stderr, "Detected abort :(\n");
				exit(1);
			}
		} else {
			decbuf[hdlccnt++] = res;
		}
	}
}