dahdi_maint: Minor name change for the error counters
[dahdi/tools.git] / pattest.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 <stdio.h>
33 #include <linux/types.h>
34 #include <linux/ppp_defs.h> 
35 #include <sys/ioctl.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include "bittest.h"
39
40 #include <dahdi/user.h>
41 #include "dahdi_tools_version.h"
42
43 #define BLOCK_SIZE 2039
44 #define DEVICE    "/dev/dahdi/channel"
45
46 static const char       rcsid[] = "$Id$";
47 char                    *prog_name;
48
49 static void usage(void)
50 {
51         fprintf(stderr, "Usage: %s <dahdi_chan>\n", prog_name);
52         fprintf(stderr, "   e.g.: %s /dev/dahdi/55\n", prog_name);
53         fprintf(stderr, "         %s 455\n", prog_name);
54         fprintf(stderr, "%s version %s\n", prog_name, rcsid);
55         exit(1);
56 }
57
58 void print_packet(unsigned char *buf, int len)
59 {
60         int x;
61         printf("{ ");
62         for (x=0;x<len;x++)
63                 printf("%02x ",buf[x]);
64         printf("}\n");
65 }
66
67 int channel_open(char *name, int *bs)
68 {
69         int                     channo;
70         int                     fd;
71         struct                  dahdi_params tp;
72         char                    *dev;
73
74         channo = atoi(name);
75         /* channo==0: The user passed a file name to be opened. */
76         dev = channo ? DEVICE : name;
77
78         fd = open(dev, O_RDWR, 0600);
79
80         if (fd < 0) {
81                 perror(DEVICE);
82                 return -1;
83         }
84
85         /* If we got a channel number, get it from /dev/dahdi/channel: */
86         if(channo && ioctl(fd, DAHDI_SPECIFY, &channo) < 0) {
87                 perror("SPECIFY");
88                 return -1;
89         }
90         if(ioctl(fd, DAHDI_SET_BLOCKSIZE, bs) < 0) {
91                 perror("SET_BLOCKSIZE");
92                 return -1;
93         }
94
95         if (ioctl(fd, DAHDI_GET_PARAMS, &tp)) {
96                 fprintf(stderr, "Unable to get channel parameters\n");
97                 return -1;
98         }
99
100         return fd;
101 }
102
103 int main(int argc, char *argv[])
104 {
105         int fd;
106         int res, x;
107         int bs = BLOCK_SIZE;
108         unsigned char c=0;
109         unsigned char outbuf[BLOCK_SIZE];
110         int setup=0;
111         int errors=0;
112         int bytes=0;
113
114         prog_name = argv[0];
115
116         if (argc < 2) {
117                 usage();
118         }
119
120         fd = channel_open(argv[1], &bs);
121         if (fd < 0)
122                 exit(1);
123
124         ioctl(fd, DAHDI_GETEVENT);
125         for(;;) {
126                 res = bs;
127                 res = read(fd, outbuf, res);
128                 if (res < bs) {
129                         int e;
130                         struct dahdi_spaninfo zi;
131                         res = ioctl(fd,DAHDI_GETEVENT,&e);
132                         if (res == -1)
133                         {
134                                 perror("DAHDI_GETEVENT");
135                                 exit(1);
136                         }
137                         if (e == DAHDI_EVENT_NOALARM)
138                                 printf("ALARMS CLEARED\n");
139                         if (e == DAHDI_EVENT_ALARM)
140                         {
141                                 zi.spanno = 0;
142                                 res = ioctl(fd,DAHDI_SPANSTAT,&zi);
143                                 if (res == -1)
144                                 {
145                                         perror("DAHDI_SPANSTAT");
146                                         exit(1);
147                                 }
148                                 printf("Alarm mask %x hex\n",zi.alarms);
149                         }
150                         continue;
151                 }
152                 if (!setup) {
153                         c = outbuf[0];
154                         setup++;
155                 }
156                 for (x=0;x<bs;x++)  {
157                         if (outbuf[x] != c) {
158                                 printf("(Error %d): Unexpected result, %d != %d, %d bytes since last error.\n", ++errors, outbuf[x], c, bytes); 
159                                 c = outbuf[x];
160                                 bytes=0;
161                         }
162                         c = bit_next(c);
163                         bytes++;
164                 }
165 #if 0
166                 printf("(%d) Wrote %d bytes\n", packets++, res);
167 #endif
168         }
169         
170 }