~vibhavp/ubuntu/raring/dahdi-tools/merge-from-debian

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