xpp: fix manpage of astribank_hexload
[dahdi/tools.git] / hdlcverify.c
1 /*
2  * Written by Mark Spencer <markster@digium.com>
3  * Based on previous works, designs, and architectures conceived and
4  * written by Jim Dixon <jim@lambdatel.com>.
5  *
6  * Copyright (C) 2001 Jim Dixon / Zapata Telephony.
7  * Copyright (C) 2001-2008 Digium, Inc.
8  *
9  * All rights reserved.
10  *
11  * Primary Author: Mark Spencer <markster@digium.com>
12  * Radio Support by Jim Dixon <jim@lambdatel.com>
13  */
14
15 /*
16  * See http://www.asterisk.org for more information about
17  * the Asterisk project. Please do not directly contact
18  * any of the maintainers of this project for assistance;
19  * the project provides a web site, mailing lists and IRC
20  * channels for your use.
21  *
22  * This program is free software, distributed under the terms of
23  * the GNU General Public License Version 2 as published by the
24  * Free Software Foundation. See the LICENSE file included with
25  * this program for more details.
26  */
27
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34
35 #define FAST_HDLC_NEED_TABLES
36 #include <dahdi/fasthdlc.h>
37
38 #include "dahdi_tools_version.h"
39
40 int myread(int fd, unsigned char *buf, int len)
41 {
42         int sofar;
43         int res;
44         sofar = 0;
45         while(sofar < len) {
46                 res = read(fd, buf + sofar, len - sofar);
47                 if (res < 0)
48                         return res;
49                 sofar += res;
50         }
51         return sofar;
52 }
53
54 static inline unsigned char nextchar(int fd)
55 {
56         static unsigned char inbuf[2048];
57         static int bytes = 0;
58         static int pos = 0;
59         if (pos >= bytes) {
60                 pos = 0;
61                 bytes = read(fd, inbuf, sizeof(inbuf));
62                 if (bytes < 0) {
63                         fprintf(stderr, "Unable to read more data: %s\n", strerror(errno));
64                         exit(1);
65                 }
66                 if (bytes == 0) {
67                         fprintf(stderr, "-- END OF DATA --\n");
68                         exit(0);
69                 }
70         }
71         return inbuf[pos++];
72 }
73
74 int main(int argc, char *argv[])
75 {
76         unsigned char decbuf[1024];
77         unsigned char actual[1024];
78         int res;
79         int datain;
80         int hdlcin;
81         int hdlccnt;
82         int x;
83         struct fasthdlc_state receiver;
84         
85         fasthdlc_precalc();
86         
87         fasthdlc_init(&receiver, FASTHDLC_MODE_64);
88         
89         hdlcin = open("random.hdlc", O_RDONLY);
90         if (hdlcin < 0) {
91                 fprintf(stderr, "Unable to open %s: %s\n", "random.hdlc", strerror(errno));
92                 exit(1);
93         }
94         datain = open("random.raw", O_RDONLY);
95         if (datain < 0) {
96                 fprintf(stderr, "Unable to open random.raw: %s\n", strerror(errno));
97                 exit(1);
98         }
99         hdlccnt = 0;
100         for (;;) {
101                 /* Feed in some input */
102                 if (fasthdlc_rx_load(&receiver, nextchar(hdlcin))) {
103                         fprintf(stderr, "Unable to feed receiver :(\n");
104                         exit(1);
105                 }
106                 res = fasthdlc_rx_run(&receiver);
107                 if (res & RETURN_EMPTY_FLAG)
108                         continue;
109                 if (res & RETURN_COMPLETE_FLAG) {
110                         if (hdlccnt) {
111                                 if (argc > 1)
112                                         printf("Got message of length %d\n", hdlccnt);
113                                 res = myread(datain, actual, hdlccnt);
114                                 if (res != hdlccnt) {
115                                         fprintf(stderr, "Tried to read %d bytes, but read %d instead\n", hdlccnt, res);
116                                         exit(1);
117                                 }
118                                 for (x=0;x<hdlccnt;x++) {
119                                         if (actual[x] != decbuf[x]) {
120                                                 fprintf(stderr, "Found discrepancy at offset %d\n", x);
121                                                 exit(1);
122                                         }
123                                 }
124                                 /* Reset message receiver */
125                                 hdlccnt = 0;
126                         }
127                 } else if (res & RETURN_DISCARD_FLAG) {
128                         if (1 || hdlccnt) {
129                                 fprintf(stderr, "Detected abort :(\n");
130                                 exit(1);
131                         }
132                 } else {
133                         decbuf[hdlccnt++] = res;
134                 }
135         }
136 }