dahdi_maint: Minor name change for the error counters
[dahdi/tools.git] / dahdi_scan.c
1 /*
2  * Scan and output information about DAHDI spans and ports.
3  * 
4  * Written by Brandon Kruse <bkruse@digium.com>
5  * and Kevin P. Fleming <kpfleming@digium.com>
6  * Copyright (C) 2007 Digium, Inc.
7  *
8  * Based on zttool written by Mark Spencer <markster@digium.com>
9  *
10  * All rights reserved.
11  *
12  */
13
14 /*
15  * See http://www.asterisk.org for more information about
16  * the Asterisk project. Please do not directly contact
17  * any of the maintainers of this project for assistance;
18  * the project provides a web site, mailing lists and IRC
19  * channels for your use.
20  *
21  * This program is free software, distributed under the terms of
22  * the GNU General Public License Version 2 as published by the
23  * Free Software Foundation. See the LICENSE file included with
24  * this program for more details.
25  */
26
27 #include <stdio.h> 
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <sys/ioctl.h>
33 #include <fcntl.h>
34 #include <errno.h>
35
36 #include <dahdi/user.h>
37
38 #include "dahdi_tools_version.h"
39
40 static inline int is_digital_span(struct dahdi_spaninfo *s)
41 {
42         return (s->linecompat > 0);
43 }
44
45 int main(int argc, char *argv[])
46 {
47         int ctl;
48         int x, y, z;
49         struct dahdi_params params;
50         unsigned int basechan = 1;
51         struct dahdi_spaninfo s;
52         char buf[100];
53         char alarms[50];
54         int filter_count = 0;
55         int span_filter[DAHDI_MAX_SPANS];
56
57         if ((ctl = open("/dev/dahdi/ctl", O_RDWR)) < 0) {
58                 fprintf(stderr, "Unable to open /dev/dahdi/ctl: %s\n", strerror(errno));
59                 exit(1);
60         }
61
62         for (x = 1; x < argc && filter_count < DAHDI_MAX_SPANS; x++) {
63                 int s = atoi(argv[x]);
64                 if (s > 0) {
65                         span_filter[filter_count++] = s;
66                 }
67         }
68
69         for (x = 1; x < DAHDI_MAX_SPANS; x++) {
70
71                 memset(&s, 0, sizeof(s));
72                 s.spanno = x;
73                 if (ioctl(ctl, DAHDI_SPANSTAT, &s))
74                         continue;
75
76                 if (filter_count > 0) {
77                         int match = 0;
78                         for (z = 0; z < filter_count; z++) {
79                                 if (x == span_filter[z]) {
80                                         match = 1;
81                                         break;
82                                 }
83                         }
84                         if (!match) {
85                                 basechan += s.totalchans;
86                                 continue;
87                         }
88                 }
89
90                 alarms[0] = '\0';
91                 if (s.alarms) {
92                         if (s.alarms & DAHDI_ALARM_BLUE)
93                                 strcat(alarms,"BLU/");
94                         if (s.alarms & DAHDI_ALARM_YELLOW)
95                                 strcat(alarms, "YEL/");
96                         if (s.alarms & DAHDI_ALARM_RED) {
97                                 strcat(alarms, "RED/");
98
99 /* Extended alarm feature test. Allows compilation with
100  * versions of dahdi-linux prior to 2.4
101  */
102 #ifdef DAHDI_ALARM_LFA
103                                 if (s.alarms & DAHDI_ALARM_LFA)
104                                         strcat(alarms, "LFA/");
105 #endif /* ifdef DAHDI_ALARM_LFA */
106                         }
107                         if (s.alarms & DAHDI_ALARM_LOOPBACK)
108                                 strcat(alarms,"LB/");
109                         if (s.alarms & DAHDI_ALARM_RECOVER)
110                                 strcat(alarms,"REC/");
111                         if (s.alarms & DAHDI_ALARM_NOTOPEN)
112                                 strcat(alarms, "NOP/");
113                         if (!strlen(alarms))
114                                 strcat(alarms, "UUU/");
115                         if (strlen(alarms)) {
116                                 /* Strip trailing / */
117                                 alarms[strlen(alarms)-1]='\0';
118                         }
119                 } else {
120                         if (s.numchans) {
121 #ifdef DAHDI_ALARM_LFA
122                                 /* If we continuously receive framing errors
123                                  * but our span is still in service, and we
124                                  * are configured for E1 & crc4. We've lost
125                                  * crc4-multiframe alignment
126                                  */
127                                 if ((s.linecompat & DAHDI_CONFIG_CRC4) &&
128                                     (s.fecount > 0)) {
129                                         struct dahdi_spaninfo t;
130                                         memset(&t, 0, sizeof(t));
131                                         t.spanno = x;
132                                         sleep(1);
133                                         if (ioctl(ctl, DAHDI_SPANSTAT, &t))
134                                                 continue;
135
136                                         /* Test fecount at two separate time
137                                          * intervals, if they differ, throw LMFA
138                                          */
139                                         if ((t.fecount > s.fecount) &&
140                                                 !t.alarms) {
141                                                 strcat(alarms, "LMFA/");
142                                         }
143                                 }
144 #endif /* ifdef DAHDI_ALARM_LFA */
145                                 strcat(alarms, "OK");
146                         } else {
147                                 strcpy(alarms, "UNCONFIGURED");
148                         }
149                 }
150
151                 fprintf(stdout, "[%d]\n", x);
152                 fprintf(stdout, "active=yes\n");
153                 fprintf(stdout, "alarms=%s\n", alarms);
154                 fprintf(stdout, "description=%s\n", s.desc);
155                 fprintf(stdout, "name=%s\n", s.name);
156                 fprintf(stdout, "manufacturer=%s\n", s.manufacturer);
157                 fprintf(stdout, "devicetype=%s\n", s.devicetype);
158                 fprintf(stdout, "location=%s\n", s.location);
159                 fprintf(stdout, "basechan=%d\n", basechan);
160                 fprintf(stdout, "totchans=%d\n", s.totalchans);
161                 fprintf(stdout, "irq=%d\n", s.irq);
162                 y = basechan;
163                 memset(&params, 0, sizeof(params));
164                 params.channo = y;
165                 if (ioctl(ctl, DAHDI_GET_PARAMS, &params)) {
166                         basechan += s.totalchans;
167                         continue;
168                 }
169
170                 if (is_digital_span(&s)) {
171                         /* this is a digital span */
172                         fprintf(stdout, "type=digital-%s\n", s.spantype);
173                         fprintf(stdout, "syncsrc=%d\n", s.syncsrc);
174                         fprintf(stdout, "lbo=%s\n", s.lboname);
175                         fprintf(stdout, "coding_opts=");
176                         buf[0] = '\0';
177                         if (s.linecompat & DAHDI_CONFIG_B8ZS) strcat(buf, "B8ZS,");
178                         if (s.linecompat & DAHDI_CONFIG_AMI) strcat(buf, "AMI,");
179                         if (s.linecompat & DAHDI_CONFIG_HDB3) strcat(buf, "HDB3,");
180                         buf[strlen(buf) - 1] = '\0';
181                         fprintf(stdout, "%s\n", buf);
182                         fprintf(stdout, "framing_opts=");
183                         buf[0] = '\0';
184                         if (s.linecompat & DAHDI_CONFIG_ESF) strcat(buf, "ESF,");
185                         if (s.linecompat & DAHDI_CONFIG_D4) strcat(buf, "D4,");
186                         if (s.linecompat & DAHDI_CONFIG_CCS) strcat(buf, "CCS,");
187                         if (s.linecompat & DAHDI_CONFIG_CRC4) strcat(buf, "CRC4,");
188                         buf[strlen(buf) - 1] = '\0';
189                         fprintf(stdout, "%s\n", buf);
190                         fprintf(stdout, "coding=");
191                         if (s.lineconfig & DAHDI_CONFIG_B8ZS) fprintf(stdout, "B8ZS");
192                         else if (s.lineconfig & DAHDI_CONFIG_AMI) fprintf(stdout, "AMI");
193                         else if (s.lineconfig & DAHDI_CONFIG_HDB3) fprintf(stdout, "HDB3");
194                         fprintf(stdout, "\n");
195                         fprintf(stdout, "framing=");
196                         if (s.lineconfig & DAHDI_CONFIG_ESF) fprintf(stdout, "ESF");
197                         else if (s.lineconfig & DAHDI_CONFIG_D4) fprintf(stdout, "D4");
198                         else if (s.lineconfig & DAHDI_CONFIG_CCS) fprintf(stdout, "CCS");
199                         else fprintf(stdout, "CAS");
200                         if (s.lineconfig & DAHDI_CONFIG_CRC4) fprintf(stdout, "/CRC4");
201                         fprintf(stdout, "\n");
202                 } else {
203                         /* this is an analog span */
204                         fprintf(stdout, "type=analog\n");
205                         for (y = basechan; y < (basechan + s.totalchans); y++) {
206                                 memset(&params, 0, sizeof(params));
207                                 params.channo = y;
208                                 if (ioctl(ctl, DAHDI_GET_PARAMS, &params)) {
209                                         fprintf(stdout, "port=%d,unknown\n", y);
210                                         continue;
211                                 };
212                                 fprintf(stdout, "port=%d,", y);
213                                 switch (params.sigcap & (__DAHDI_SIG_FXO | __DAHDI_SIG_FXS)) {
214                                 case __DAHDI_SIG_FXO:
215                                         fprintf(stdout, "FXS");
216                                         break;
217                                 case __DAHDI_SIG_FXS:
218                                         fprintf(stdout, "FXO");
219                                         break;
220                                 default:
221                                         fprintf(stdout, "none");
222                                 }
223                                 if (params.sigcap & DAHDI_SIG_BROKEN)
224                                         fprintf(stdout, " FAILED");
225                                 fprintf(stdout, "\n");
226                         }
227                 }
228           
229                 basechan += s.totalchans;
230         }
231
232         exit(0);
233 }