1
/* $Id: plugin_isdn.c 840 2007-09-09 12:17:42Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_isdn.c $
4
* plugin for ISDN subsystem
6
* Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* Based on the old isdn client (isdn.c) which is
10
* Copyright (C) 1999, 2000 Michael Reinelt <michael@reinelt.co.at>
12
* This file is part of LCD4Linux.
14
* LCD4Linux is free software; you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation; either version 2, or (at your option)
19
* LCD4Linux is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
24
* You should have received a copy of the GNU General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33
* int plugin_init_isdn (void)
34
* adds functions to access ISDN information
47
#include <sys/ioctl.h>
48
#include <sys/types.h>
50
#ifdef HAVE_LINUX_ISDN_H
51
#include <linux/isdn.h>
53
#warning isdn.h not found. CPS support deactivated.
68
static HASH ISDN_INFO;
72
static void hash_put_info(const char *name, const int channel, const char *val)
76
qprintf(key, sizeof(key), "%s[%d]", name, channel);
77
hash_put(&ISDN_INFO, key, val);
80
static int parse_isdninfo(void)
86
/* reread every 10 msec only */
87
age = hash_age(&ISDN_INFO, NULL);
88
if (age > 0 && age <= 10)
92
stream = fopen("/dev/isdninfo", "r");
94
error("open(/dev/isdninfo) failed: %s", strerror(errno));
99
flags = fcntl(fileno(stream), F_GETFL);
101
error("fcntl(/dev/isdninfo, F_GETFL) failed: %s", strerror(errno));
106
if (fcntl(fileno(stream), F_SETFL, flags | O_NONBLOCK) < 0) {
107
error("fcntl(/dev/isdninfo, F_SETFL, O_NONBLOCK) failed: %s", strerror(errno));
111
while (!feof(stream)) {
114
if (fgets(buffer, sizeof(buffer), stream) == NULL)
116
beg = strchr(buffer, ':');
118
char delim[] = " \t\n";
121
while (*beg && strchr(delim, *beg))
123
while (beg && *beg) {
124
if ((end = strpbrk(beg, delim)))
126
hash_put_info(buffer, i, beg);
127
beg = end ? end + 1 : NULL;
128
while (*beg && strchr(delim, *beg))
133
error("Huh? no colon found in <%s>", buffer);
143
static void my_isdn_info(RESULT * result, RESULT * arg1, RESULT * arg2)
147
if (parse_isdninfo() < 0) {
148
SetResult(&result, R_STRING, "");
152
qprintf(key, sizeof(key), "%s[%d]", R2S(arg1), (int) R2N(arg2));
153
val = hash_get(&ISDN_INFO, key, NULL);
156
SetResult(&result, R_STRING, val);
160
#ifdef HAVE_LINUX_ISDN_H
162
static void hash_put_cps(const int channel, const CPS * cps)
164
char key[16], val[16];
166
qprintf(key, sizeof(key), channel < 0 ? "i" : "i%d", channel);
167
qprintf(val, sizeof(val), "%u", cps->in);
168
hash_put_delta(&ISDN_CPS, key, val);
170
qprintf(key, sizeof(key), channel < 0 ? "o" : "o%d", channel);
171
qprintf(val, sizeof(val), "%u", cps->out);
172
hash_put_delta(&ISDN_CPS, key, val);
176
static int get_cps(void)
180
CPS cps[ISDN_MAX_CHANNELS];
183
/* reread every 10 msec only */
184
age = hash_age(&ISDN_CPS, NULL);
185
if (age > 0 && age <= 10)
192
fd = open("/dev/isdninfo", O_RDONLY | O_NDELAY);
194
error("open(/dev/isdninfo) failed: %s", strerror(errno));
199
if (ioctl(fd, IIOCGETCPS, &cps)) {
200
error("ioctl(IIOCGETCPS) failed: %s", strerror(errno));
207
for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
209
sum.out += cps[i].out;
210
hash_put_cps(i, &cps[i]);
212
hash_put_cps(-1, &sum);
218
static void my_isdn_cps(RESULT * result, RESULT * arg1, RESULT * arg2)
223
SetResult(&result, R_STRING, "");
227
value = hash_get_delta(&ISDN_CPS, R2S(arg1), NULL, R2N(arg2));
228
SetResult(&result, R_NUMBER, &value);
235
int plugin_init_isdn(void)
237
hash_create(&ISDN_INFO);
238
hash_create(&ISDN_CPS);
240
AddFunction("isdn::info", 2, my_isdn_info);
242
#ifdef HAVE_LINUX_ISDN_H
243
AddFunction("isdn::cps", 2, my_isdn_cps);
250
void plugin_exit_isdn(void)
252
hash_destroy(&ISDN_INFO);
253
hash_destroy(&ISDN_CPS);