1/* $NetBSD: main.c,v 1.9 2008/04/28 20:24:16 martin Exp $ */
2
3/*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Martin Husemann <martin@NetBSD.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36#include <fcntl.h>
37#include <sys/ioctl.h>
38#include <netisdn/i4b_ioctl.h>
39#include "daicctl.h"
40
41__dead static void usage(void);
42static void listall(int);
43
44int
45main(int argc, char **argv)
46{
47	extern char *optarg;
48	extern int optind;
49	int isdndev;
50	int controller = -1;
51	int lflag, dflag, xflag, iflag, sflag, vflag, pthrou, ch;
52	char *dnload = NULL;
53
54	pthrou = lflag = dflag = xflag = iflag = sflag = vflag = 0;
55	while ((ch = getopt(argc, argv, "d:xsvlp")) != -1)
56	    switch (ch) {
57	    case 'd':
58		dnload = optarg;
59		dflag = 1;
60		break;
61	    case 's':
62		sflag = 1;
63		break;
64	    case 'l':
65		lflag = 1;
66		break;
67	    case 'x':
68		xflag = 1;
69		break;
70	    case 'v':
71		vflag = 1;
72		break;
73	    case 'p':
74		pthrou = 1;
75		break;
76	    case '?':
77	    default:
78		usage();
79	    }
80
81	argc -= optind;
82	argv += optind;
83	if (argc == 0)
84		lflag = 1;
85	else if (argc != 1)
86		usage();
87	else
88		controller = (int)strtol(*argv, NULL, 10);
89
90#ifdef __NetBSD__
91	isdndev = open(PATH_ISDN_DEV, O_RDWR|O_EXLOCK, 0);
92#else
93	isdndev = open(PATH_ISDN_DEV, O_RDWR, 0);
94#endif
95	if (isdndev < 0) {
96		perror(PATH_ISDN_DEV);
97		return 1;
98	}
99
100	if (lflag)
101		listall(isdndev);
102
103	if (controller < 0) {
104		if (dflag || vflag || xflag || sflag || pthrou)
105			fprintf(stderr, "\nYou must specify the controller number!\n");
106	} else {
107		if (dflag)
108			download(isdndev, controller, dnload);
109		if (vflag)
110			xversion(isdndev, controller);
111		if (xflag)
112			xlog(isdndev, controller);
113		if (sflag)
114			istat(isdndev, controller);
115		if (pthrou)
116			passthrough(isdndev, controller);
117	}
118
119	close(isdndev);
120	return 0;
121}
122
123void usage()
124{
125	fprintf(stderr, "usage: daicctl [-d (file)] [-xsie] controller\n"
126		" -l          list available controllers\n"
127		" -d (file)   download microcode from (file) to the card\n"
128		" -x          xlog utility: show on card log\n"
129		" -s          statistics (istat utility): show controller statistics\n"
130		" -v          show version of microcode running on controller\n");
131	exit(1);
132}
133
134static void
135listall(int fd)
136{
137	int i, num;
138	msg_ctrl_info_req_t info;
139
140	memset(&info, 0, sizeof info);
141	ioctl(fd, I4B_CTRL_INFO_REQ, &info);
142	num = info.max_isdnif;
143	printf("There are %d controllers available:\n", info.ncontroller);
144	for (i = 0; i <= num; i++) {
145		info.controller = i;
146		if (ioctl(fd, I4B_CTRL_INFO_REQ, &info) == 0) {
147			printf("BRI %d: %s\n", i, info.devname);
148			printf("\t%s\n", info.cardname);
149		}
150	}
151}
152
153void passthrough(int fd, int controller)
154{
155	static u_int8_t in_data[] = {
156		0x70, 0x07, 0x81,
157		'9', '8', '9', '0', '2', '0',
158		0x00
159	};
160	u_int8_t out_data[16];
161
162	struct isdn_diagnostic_request req;
163
164	printf("passing through: hardcoded dial request\n");
165	memset(&req, 0, sizeof(req));
166	req.controller = controller;
167	req.cmd = 0x05;
168	req.in_param_len = sizeof in_data;
169	req.in_param = &in_data;
170	req.out_param_len = sizeof out_data;
171	req.out_param = &out_data;
172
173	if (ioctl(fd, I4B_ACTIVE_DIAGNOSTIC, &req) == -1) {
174		perror("ioctl(I4B_ACTIVE_DIAGNOSTIC)");
175		return;
176	}
177	printf("Return code: 0x%02x\n", out_data[0]);
178}
179