summaryrefslogtreecommitdiff
path: root/ztscan.c
blob: d821092af2584344cc7106217a1202d5cc49062b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Scan and output information about Zaptel spans and ports.
 * 
 * Written by Brandon Kruse <bkruse@digium.com>
 * and Kevin P. Fleming <kpfleming@digium.com>
 * Copyright (C) 2007 Digium, Inc.
 *
 * Based on zttool written by Mark Spencer <markster@digium.com>
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under thet erms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 */

#include <stdio.h> 
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>

#ifdef STANDALONE_ZAPATA
#include "kernel/zaptel.h"
#else
#include <zaptel/zaptel.h>
#endif

void print_span(int spanno, int ctl)
{
	struct zt_spaninfo spaninfo;
	int y;
	struct zt_params params;
	unsigned int basechan = 1;
	char buf[100];
	char alarms[50];

	memset(&spaninfo, 0, sizeof(spaninfo));
	spaninfo.spanno = spanno;
	if (ioctl(ctl, ZT_SPANSTAT, &spaninfo))
		return;

	alarms[0] = '\0';
	if (spaninfo.alarms) {
		if (spaninfo.alarms & ZT_ALARM_BLUE)
			strcat(alarms,"BLU/");
		if (spaninfo.alarms & ZT_ALARM_YELLOW)
			strcat(alarms, "YEL/");
		if (spaninfo.alarms & ZT_ALARM_RED)
			strcat(alarms, "RED/");
		if (spaninfo.alarms & ZT_ALARM_LOOPBACK)
			strcat(alarms,"LB/");
		if (spaninfo.alarms & ZT_ALARM_RECOVER)
			strcat(alarms,"REC/");
		if (spaninfo.alarms & ZT_ALARM_NOTOPEN)
			strcat(alarms, "NOP/");
		if (!strlen(alarms))
			strcat(alarms, "UUU/");
		if (strlen(alarms)) {
			/* Strip trailing / */
			alarms[strlen(alarms)-1]='\0';
		}
	} else {
		if (spaninfo.numchans)
			strcpy(alarms, "OK");
		else
			strcpy(alarms, "UNCONFIGURED");
	}

	fprintf(stdout, "[%d]\n", spanno);
	fprintf(stdout, "active=yes\n");
	fprintf(stdout, "alarms=%s\n", alarms);
	fprintf(stdout, "description=%s\n", spaninfo.desc);
	fprintf(stdout, "name=%s\n", spaninfo.name);
	fprintf(stdout, "manufacturer=%s\n", spaninfo.manufacturer);
	fprintf(stdout, "devicetype=%s\n", spaninfo.devicetype);
	fprintf(stdout, "location=%s\n", spaninfo.location);
	fprintf(stdout, "basechan=%d\n", basechan);
	fprintf(stdout, "totchans=%d\n", spaninfo.totalchans);
	fprintf(stdout, "irq=%d\n", spaninfo.irq);
	y = basechan;
	memset(&params, 0, sizeof(params));
	params.channo = y;
	if (ioctl(ctl, ZT_GET_PARAMS, &params)) {
		basechan += spaninfo.totalchans;
		return;
	}

	if (params.sigcap & (__ZT_SIG_DACS |  ZT_SIG_CAS)) {
		/* this is a digital span */
		fprintf(stdout, "type=digital-%s\n", spaninfo.spantype);
		fprintf(stdout, "syncsrc=%d\n", spaninfo.syncsrc);
		fprintf(stdout, "lbo=%s\n", spaninfo.lboname);
		fprintf(stdout, "coding_opts=");
		buf[0] = '\0';
		if (spaninfo.linecompat & ZT_CONFIG_B8ZS) strcat(buf, "B8ZS,");
		if (spaninfo.linecompat & ZT_CONFIG_AMI) strcat(buf, "AMI,");
		if (spaninfo.linecompat & ZT_CONFIG_HDB3) strcat(buf, "HDB3,");
		buf[strlen(buf) - 1] = '\0';
		fprintf(stdout, "%s\n", buf);
		fprintf(stdout, "framing_opts=");
		buf[0] = '\0';
		if (spaninfo.linecompat & ZT_CONFIG_ESF) strcat(buf, "ESF,");
		if (spaninfo.linecompat & ZT_CONFIG_D4) strcat(buf, "D4,");
		if (spaninfo.linecompat & ZT_CONFIG_CCS) strcat(buf, "CCS,");
		if (spaninfo.linecompat & ZT_CONFIG_CRC4) strcat(buf, "CRC4,");
		buf[strlen(buf) - 1] = '\0';
		fprintf(stdout, "%s\n", buf);
		fprintf(stdout, "coding=");
		if (spaninfo.lineconfig & ZT_CONFIG_B8ZS) fprintf(stdout, "B8ZS");
		else if (spaninfo.lineconfig & ZT_CONFIG_AMI) fprintf(stdout, "AMI");
		else if (spaninfo.lineconfig & ZT_CONFIG_HDB3) fprintf(stdout, "HDB3");
		fprintf(stdout, "\n");
		fprintf(stdout, "framing=");
		if (spaninfo.lineconfig & ZT_CONFIG_ESF) fprintf(stdout, "ESF");
		else if (spaninfo.lineconfig & ZT_CONFIG_D4) fprintf(stdout, "D4");
		else if (spaninfo.lineconfig & ZT_CONFIG_CCS) fprintf(stdout, "CCS");
		else if (spaninfo.lineconfig & ZT_CONFIG_CRC4) fprintf(stdout, "/CRC4");
		fprintf(stdout, "\n");
	} else {
		/* this is an analog span */
		fprintf(stdout, "type=analog\n");
		for (y = basechan; y < (basechan + spaninfo.totalchans); y++) {
			memset(&params, 0, sizeof(params));
			params.channo = y;
			if (ioctl(ctl, ZT_GET_PARAMS, &params)) {
				fprintf(stdout, "port=%d,unknown\n", y);
				return;
			};
			fprintf(stdout, "port=%d,", y);
			switch (params.sigcap & (__ZT_SIG_FXO | __ZT_SIG_FXS)) {
				case __ZT_SIG_FXO:
					fprintf(stdout, "FXS");
					break;
				case __ZT_SIG_FXS:
					fprintf(stdout, "FXO");
					break;
				default:
					fprintf(stdout, "none");
			}
			if (params.sigcap & ZT_SIG_BROKEN)
				fprintf(stdout, " FAILED");
			fprintf(stdout, "\n");
		}
	}

	basechan += spaninfo.totalchans;
}

int main(int argc, char *argv[])
{
	int ctl;
	int x, spanno;

	if ((ctl = open("/dev/zap/ctl", O_RDWR)) < 0) {
		fprintf(stderr, "Unable to open /dev/zap/ctl: %s\n", strerror(errno));
		exit(1);
	}
	
	/* If no parameters: loop over all spans */
	if (argc == 1) {
		for (spanno = 1; spanno < ZT_MAX_SPANS; spanno++)
			print_span(spanno, ctl);
		return 0;
	}

	/* Parameters provided: use them as span numbers */
	for (x=1; argv[x]; x++) {
		/* We can use atoi because span numbers must be > 0 */
		int spanno = atoi(argv[x]);
		if (spanno <= 0) {
			fprintf(stderr, "%s: Error: Invalid span number %d (%s). Aborting\n",
					argv[0], spanno, argv[x]);
			exit(2);
		}
		print_span(spanno, ctl);
	}

	return 0;
}