summaryrefslogtreecommitdiff
path: root/patgen.c
diff options
context:
space:
mode:
authorTzafrir Cohen <tzafrir.cohen@xorcom.com>2009-12-02 19:45:45 +0000
committerTzafrir Cohen <tzafrir.cohen@xorcom.com>2009-12-02 19:45:45 +0000
commitd7cbc587a4bda464ae9336d6f756055b93cf8cca (patch)
treeb62769a8a68dc889b1f91b9cf5e8b0faad0f1255 /patgen.c
parent39fb6f44d7f137a6a9625b1afed557f69e438b9a (diff)
patgen and pattest: channel by number
Allow pattest and patgen to get the DAHDI channel by number rather than with an explicit device file. git-svn-id: http://svn.asterisk.org/svn/dahdi/tools/trunk@7658 a0bf4364-ded3-4de4-8d8a-66a801d63aff
Diffstat (limited to 'patgen.c')
-rw-r--r--patgen.c72
1 files changed, 57 insertions, 15 deletions
diff --git a/patgen.c b/patgen.c
index 9c8ae9f..e816dd9 100644
--- a/patgen.c
+++ b/patgen.c
@@ -42,6 +42,19 @@
/* #define BLOCK_SIZE 2048 */
#define BLOCK_SIZE 2041
+#define DEVICE "/dev/dahdi/channel"
+
+static const char rcsid[] = "$Id$";
+char *prog_name;
+
+static void usage(void)
+{
+ fprintf(stderr, "Usage: %s <dahdi_chan>\n", prog_name);
+ fprintf(stderr, " e.g.: %s /dev/dahdi/55\n", prog_name);
+ fprintf(stderr, " %s 455\n", prog_name);
+ fprintf(stderr, "%s version %s\n", prog_name, rcsid);
+ exit(1);
+}
void print_packet(unsigned char *buf, int len)
{
@@ -52,31 +65,60 @@ void print_packet(unsigned char *buf, int len)
printf("}\n");
}
+int channel_open(char *name, int *bs)
+{
+ int channo;
+ int fd;
+ struct dahdi_params tp;
+ char *dev;
+
+ channo = atoi(name);
+ /* channo==0: The user passed a file name to be opened. */
+ dev = channo ? DEVICE : name;
+
+ fd = open(dev, O_RDWR, 0600);
+
+ if (fd < 0) {
+ perror(DEVICE);
+ return -1;
+ }
+
+ /* If we got a channel number, get it from /dev/dahdi/channel: */
+ if(channo && ioctl(fd, DAHDI_SPECIFY, &channo) < 0) {
+ perror("SPECIFY");
+ return -1;
+ }
+ if(ioctl(fd, DAHDI_SET_BLOCKSIZE, bs) < 0) {
+ perror("SET_BLOCKSIZE");
+ return -1;
+ }
+
+ if (ioctl(fd, DAHDI_GET_PARAMS, &tp)) {
+ fprintf(stderr, "Unable to get channel parameters\n");
+ return -1;
+ }
+
+ return fd;
+}
+
int main(int argc, char *argv[])
{
int fd;
int res, res1, x;
- struct dahdi_params tp;
int bs = BLOCK_SIZE;
unsigned char c=0;
unsigned char outbuf[BLOCK_SIZE];
+
+ prog_name = argv[0];
+
if (argc < 2) {
- fprintf(stderr, "Usage: %s <DAHDI device>\n", argv[0]);
- exit(1);
- }
- fd = open(argv[1], O_RDWR, 0600);
- if (fd < 0) {
- fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
- exit(1);
- }
- if (ioctl(fd, DAHDI_SET_BLOCKSIZE, &bs)) {
- fprintf(stderr, "Unable to set block size to %d: %s\n", bs, strerror(errno));
- exit(1);
+ usage();
}
- if (ioctl(fd, DAHDI_GET_PARAMS, &tp)) {
- fprintf(stderr, "Unable to get channel parameters\n");
+
+ fd = channel_open(argv[1], &bs);
+ if (fd < 0)
exit(1);
- }
+
ioctl(fd, DAHDI_GETEVENT);
#if 0
print_packet(outbuf, res);