2 * libpri: An implementation of Primary Rate ISDN
4 * Written by Mark Spencer <markster@linux-support.net>
6 * Copyright (C) 2001-2005, Digium, Inc.
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2 as published by the
19 * Free Software Foundation. See the LICENSE file included with
20 * this program for more details.
22 * In addition, when this program is distributed with Asterisk in
23 * any form that would qualify as a 'combined work' or as a
24 * 'derivative work' (but not mere aggregation), you can redistribute
25 * and/or modify the combination under the terms of the license
26 * provided with that copy of Asterisk, instead of the license
31 * This program tests libpri call reception using a zaptel interface.
32 * Its state machines are setup for RECEIVING CALLS ONLY, so if you
33 * are trying to both place and receive calls you have to a bit more.
42 #include <sys/ioctl.h>
43 #include <sys/select.h>
44 #include <sys/types.h>
45 #include <zaptel/zaptel.h>
50 static int pri_open(char *dev
)
55 dfd
= open(dev
, O_RDWR
);
57 fprintf(stderr
, "Failed to open dchannel '%s': %s\n", dev
, strerror(errno
));
60 if (ioctl(dfd
, ZT_GET_PARAMS
, &p
)) {
61 fprintf(stderr
, "Unable to get parameters on '%s': %s\n", dev
, strerror(errno
));
64 if ((p
.sigtype
!= ZT_SIG_HDLCRAW
) && (p
.sigtype
!= ZT_SIG_HDLCFCS
)) {
65 fprintf(stderr
, "%s is in %d signalling, not FCS HDLC or RAW HDLC mode\n", dev
, p
.sigtype
);
71 static void dump_packet(struct pri
*pri
, char *buf
, int len
, int txrx
)
73 q921_h
*h
= (q921_h
*)buf
;
74 q921_dump(pri
, h
, len
, 1, txrx
);
75 if (!((h
->h
.data
[0] & Q921_FRAMETYPE_MASK
) & 0x3)) {
76 q931_dump(pri
, (q931_h
*)(h
->i
.data
), len
- 4 - 2 /* FCS */, txrx
);
83 static int pri_bridge(int d1
, int d2
)
97 ioctl(d1
, ZT_GETEVENT
, &e
);
98 ioctl(d2
, ZT_GETEVENT
, &e
);
99 res
= select(max
+ 1, &fds
, NULL
, NULL
, NULL
);
101 fprintf(stderr
, "Select returned %d: %s\n", res
, strerror(errno
));
104 if (FD_ISSET(d1
, &fds
)) {
105 /* Copy from d1 to d2 */
106 res
= read(d1
, buf
, sizeof(buf
));
107 dump_packet((struct pri
*)NULL
, buf
, res
, 1);
108 res
= write(d2
, buf
, res
);
110 if (FD_ISSET(d2
, &fds
)) {
111 /* Copy from d2 to d1 */
112 res
= read(d2
, buf
, sizeof(buf
));
113 dump_packet((struct pri
*)NULL
, buf
, res
, 0);
114 res
= write(d1
, buf
, res
);
119 static void my_pri_message(struct pri
*pri
, char *stuff
)
121 fprintf(stdout
, "%s", stuff
);
124 static void my_pri_error(struct pri
*pri
, char *stuff
)
126 fprintf(stderr
, "%s", stuff
);
129 int main(int argc
, char *argv
[])
134 fprintf(stderr
, "Usage: pridump <dev1> <dev2>\n");
138 pri_set_message(my_pri_message
);
139 pri_set_error(my_pri_error
);
141 d1
= pri_open(argv
[1]);
144 d2
= pri_open(argv
[2]);