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