PPP: Fix an incorrect function call that was causing dahdi.so to fail to load
[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 #ifdef DAHDI_ALARM_LFA
99                                 if (s.alarms & DAHDI_ALARM_LFA)
100                                         strcat(alarms, "LFA/");
101                                 if (s.alarms & DAHDI_ALARM_LMFA)
102                                         strcat(alarms, "LMFA/");
103 #endif /* ifdef DAHDI_ALARM_LFA */
104                         }
105                         if (s.alarms & DAHDI_ALARM_LOOPBACK)
106                                 strcat(alarms,"LB/");
107                         if (s.alarms & DAHDI_ALARM_RECOVER)
108                                 strcat(alarms,"REC/");
109                         if (s.alarms & DAHDI_ALARM_NOTOPEN)
110                                 strcat(alarms, "NOP/");
111                         if (!strlen(alarms))
112                                 strcat(alarms, "UUU/");
113                         if (strlen(alarms)) {
114                                 /* Strip trailing / */
115                                 alarms[strlen(alarms)-1]='\0';
116                         }
117                 } else {
118                         if (s.numchans)
119                                 strcpy(alarms, "OK");
120                         else
121                                 strcpy(alarms, "UNCONFIGURED");
122                 }
123
124                 fprintf(stdout, "[%d]\n", x);
125                 fprintf(stdout, "active=yes\n");
126                 fprintf(stdout, "alarms=%s\n", alarms);
127                 fprintf(stdout, "description=%s\n", s.desc);
128                 fprintf(stdout, "name=%s\n", s.name);
129                 fprintf(stdout, "manufacturer=%s\n", s.manufacturer);
130                 fprintf(stdout, "devicetype=%s\n", s.devicetype);
131                 fprintf(stdout, "location=%s\n", s.location);
132                 fprintf(stdout, "basechan=%d\n", basechan);
133                 fprintf(stdout, "totchans=%d\n", s.totalchans);
134                 fprintf(stdout, "irq=%d\n", s.irq);
135                 y = basechan;
136                 memset(&params, 0, sizeof(params));
137                 params.channo = y;
138                 if (ioctl(ctl, DAHDI_GET_PARAMS, &params)) {
139                         basechan += s.totalchans;
140                         continue;
141                 }
142
143                 if (is_digital_span(&s)) {
144                         /* this is a digital span */
145                         fprintf(stdout, "type=digital-%s\n", s.spantype);
146                         fprintf(stdout, "syncsrc=%d\n", s.syncsrc);
147                         fprintf(stdout, "lbo=%s\n", s.lboname);
148                         fprintf(stdout, "coding_opts=");
149                         buf[0] = '\0';
150                         if (s.linecompat & DAHDI_CONFIG_B8ZS) strcat(buf, "B8ZS,");
151                         if (s.linecompat & DAHDI_CONFIG_AMI) strcat(buf, "AMI,");
152                         if (s.linecompat & DAHDI_CONFIG_HDB3) strcat(buf, "HDB3,");
153                         buf[strlen(buf) - 1] = '\0';
154                         fprintf(stdout, "%s\n", buf);
155                         fprintf(stdout, "framing_opts=");
156                         buf[0] = '\0';
157                         if (s.linecompat & DAHDI_CONFIG_ESF) strcat(buf, "ESF,");
158                         if (s.linecompat & DAHDI_CONFIG_D4) strcat(buf, "D4,");
159                         if (s.linecompat & DAHDI_CONFIG_CCS) strcat(buf, "CCS,");
160                         if (s.linecompat & DAHDI_CONFIG_CRC4) strcat(buf, "CRC4,");
161                         buf[strlen(buf) - 1] = '\0';
162                         fprintf(stdout, "%s\n", buf);
163                         fprintf(stdout, "coding=");
164                         if (s.lineconfig & DAHDI_CONFIG_B8ZS) fprintf(stdout, "B8ZS");
165                         else if (s.lineconfig & DAHDI_CONFIG_AMI) fprintf(stdout, "AMI");
166                         else if (s.lineconfig & DAHDI_CONFIG_HDB3) fprintf(stdout, "HDB3");
167                         fprintf(stdout, "\n");
168                         fprintf(stdout, "framing=");
169                         if (s.lineconfig & DAHDI_CONFIG_ESF) fprintf(stdout, "ESF");
170                         else if (s.lineconfig & DAHDI_CONFIG_D4) fprintf(stdout, "D4");
171                         else if (s.lineconfig & DAHDI_CONFIG_CCS) fprintf(stdout, "CCS");
172                         else if (s.lineconfig & DAHDI_CONFIG_CRC4) fprintf(stdout, "/CRC4");
173                         fprintf(stdout, "\n");
174                 } else {
175                         /* this is an analog span */
176                         fprintf(stdout, "type=analog\n");
177                         for (y = basechan; y < (basechan + s.totalchans); y++) {
178                                 memset(&params, 0, sizeof(params));
179                                 params.channo = y;
180                                 if (ioctl(ctl, DAHDI_GET_PARAMS, &params)) {
181                                         fprintf(stdout, "port=%d,unknown\n", y);
182                                         continue;
183                                 };
184                                 fprintf(stdout, "port=%d,", y);
185                                 switch (params.sigcap & (__DAHDI_SIG_FXO | __DAHDI_SIG_FXS)) {
186                                 case __DAHDI_SIG_FXO:
187                                         fprintf(stdout, "FXS");
188                                         break;
189                                 case __DAHDI_SIG_FXS:
190                                         fprintf(stdout, "FXO");
191                                         break;
192                                 default:
193                                         fprintf(stdout, "none");
194                                 }
195                                 if (params.sigcap & DAHDI_SIG_BROKEN)
196                                         fprintf(stdout, " FAILED");
197                                 fprintf(stdout, "\n");
198                         }
199                 }
200           
201                 basechan += s.totalchans;
202         }
203
204         exit(0);
205 }