dahdi_span_assignments: matched and unmatched
[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 <sys/stat.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include "bittest.h"
40
41 #include <dahdi/user.h>
42 #include "dahdi_tools_version.h"
43
44 #define BLOCK_SIZE 2039
45 #define DEVICE    "/dev/dahdi/channel"
46
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         exit(1);
55 }
56
57 void print_packet(unsigned char *buf, int len)
58 {
59         int x;
60         printf("{ ");
61         for (x=0;x<len;x++)
62                 printf("%02x ",buf[x]);
63         printf("}\n");
64 }
65
66 int channel_open(const char *name, int *bs)
67 {
68         int     channo, fd;
69         struct  dahdi_params tp;
70         struct  stat filestat;
71
72         /* stat file, if character device, open it */
73         channo = strtoul(name, NULL, 10);
74         fd = stat(name, &filestat);
75         if (!fd && S_ISCHR(filestat.st_mode)) {
76                 fd = open(name, O_RDWR, 0600);
77                 if (fd < 0) {
78                         perror(name);
79                         return -1;
80                 }
81         /* try out the dahdi_specify interface */
82         } else if (channo > 0) {
83                 fd = open(DEVICE, O_RDWR, 0600);
84                 if (fd < 0) {
85                         perror(DEVICE);
86                         return -1;
87                 }
88                 if (ioctl(fd, DAHDI_SPECIFY, &channo) < 0) {
89                         perror("DAHDI_SPECIFY ioctl failed");
90                         return -1;
91                 }
92         /* die */
93         } else {
94                 fprintf(stderr, "Specified channel is not a valid character "
95                         "device or channel number");
96                 return -1;
97         }
98
99         if (ioctl(fd, DAHDI_SET_BLOCKSIZE, bs) < 0) {
100                 perror("SET_BLOCKSIZE");
101                 return -1;
102         }
103
104         if (ioctl(fd, DAHDI_GET_PARAMS, &tp)) {
105                 fprintf(stderr, "Unable to get channel parameters\n");
106                 return -1;
107         }
108
109         return fd;
110 }
111
112 int main(int argc, char *argv[])
113 {
114         int fd;
115         int res, x;
116         int bs = BLOCK_SIZE;
117         unsigned char c=0;
118         unsigned char outbuf[BLOCK_SIZE];
119         int setup=0;
120         int errors=0;
121         int bytes=0;
122
123         prog_name = argv[0];
124
125         if (argc < 2) {
126                 usage();
127         }
128
129         fd = channel_open(argv[1], &bs);
130         if (fd < 0)
131                 exit(1);
132
133         ioctl(fd, DAHDI_GETEVENT);
134         for(;;) {
135                 res = bs;
136                 res = read(fd, outbuf, res);
137                 if (res < bs) {
138                         int e;
139                         struct dahdi_spaninfo zi;
140                         res = ioctl(fd,DAHDI_GETEVENT,&e);
141                         if (res == -1)
142                         {
143                                 perror("DAHDI_GETEVENT");
144                                 exit(1);
145                         }
146                         if (e == DAHDI_EVENT_NOALARM)
147                                 printf("ALARMS CLEARED\n");
148                         if (e == DAHDI_EVENT_ALARM)
149                         {
150                                 zi.spanno = 0;
151                                 res = ioctl(fd,DAHDI_SPANSTAT,&zi);
152                                 if (res == -1)
153                                 {
154                                         perror("DAHDI_SPANSTAT");
155                                         exit(1);
156                                 }
157                                 printf("Alarm mask %x hex\n",zi.alarms);
158                         }
159                         continue;
160                 }
161                 if (!setup) {
162                         c = outbuf[0];
163                         setup++;
164                 }
165                 for (x=0;x<bs;x++)  {
166                         if (outbuf[x] != c) {
167                                 printf("(Error %d): Unexpected result, %d != %d, %d bytes since last error.\n", ++errors, outbuf[x], c, bytes); 
168                                 c = outbuf[x];
169                                 bytes=0;
170                         }
171                         c = bit_next(c);
172                         bytes++;
173                 }
174 #if 0
175                 printf("(%d) Wrote %d bytes\n", packets++, res);
176 #endif
177         }
178         
179 }