aboutsummaryrefslogtreecommitdiffstats
path: root/isdn.c
blob: b6be354c7b8863a46514b059ff53c1c94a2b97fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* $Id: isdn.c,v 1.11 2003/10/05 17:58:50 reinelt Exp $
 *
 * ISDN specific functions
 *
 * Copyright 1999, 2000 Michael Reinelt <reinelt@eunet.at>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * LCD4Linux is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * $Log: isdn.c,v $
 * Revision 1.11  2003/10/05 17:58:50  reinelt
 * libtool junk; copyright messages cleaned up
 *
 * Revision 1.10  2003/06/21 05:46:18  reinelt
 * DVB client integrated
 *
 * Revision 1.9  2001/05/27 07:19:28  reinelt
 *
 * fixed a warning in pixmap.c
 * temporarily fixed a bug in isdn.c (ISDN_MAX_CHANNELS is no longer defined?)
 * fixed a bug in configure.in (--with-drivers=xyz did not work)
 *
 * Revision 1.8  2000/08/10 18:42:20  reinelt
 *
 * fixed some bugs with the new syslog code
 *
 * Revision 1.7  2000/08/10 09:44:09  reinelt
 *
 * new debugging scheme: error(), info(), debug()
 * uses syslog if in daemon mode
 *
 * Revision 1.6  2000/04/15 11:56:35  reinelt
 *
 * more debug messages
 *
 * Revision 1.5  2000/03/13 15:58:24  reinelt
 *
 * release 0.9
 * moved row parsing to parser.c
 * all basic work finished
 *
 * Revision 1.4  2000/03/10 17:36:02  reinelt
 *
 * first unstable but running release
 *
 * Revision 1.3  2000/03/07 11:01:34  reinelt
 *
 * system.c cleanup
 *
 * Revision 1.2  2000/03/06 06:04:06  reinelt
 *
 * minor cleanups
 *
 */

/* 
 * exported functions:
 *
 * Isdn (int *rx, int *tx, int *usage)
 *   returns 0 if ok, -1 if error
 *   sets *usage to all channels USAGE or'ed together
 *   sets received/transmitted bytes in *rx, *tx
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/isdn.h>

// Fixme: ISDN_MAX_CHANNELS seems undefined in recent kernels
#ifndef ISDN_MAX_CHANNELS
#define ISDN_MAX_CHANNELS 64
#endif

#include "debug.h"
#include "isdn.h"
#include "filter.h"

typedef struct {
  unsigned long in;
  unsigned long out;
} CPS;


static int Usage (void)
{
  static int fd=0;
  char buffer[4096], *p;
  int i, usage;

  if (fd==-1) return 0;

  fd=open ("/dev/isdninfo", O_RDONLY | O_NDELAY);
  if (fd==-1) {
    error ("open(/dev/isdninfo) failed: %s", strerror(errno));
    return 0;
  }
  
  if (read (fd, buffer, sizeof(buffer))==-1) {
    error ("read(/dev/isdninfo) failed: %s", strerror(errno));
    fd=-1;
    return 0;
  }

  if (close(fd)==-1) {
    error ("close(/dev/isdninfo) failed: %s", strerror(errno));
    fd=-1;
    return 0;
  }

  p=strstr(buffer, "usage:");
  if (p==NULL) {
    error ("parse(/dev/isdninfo) failed: no usage line");
    fd=-1;
    return 0;
  }
  p+=6;

  usage=0;
  for (i=0; i<ISDN_MAX_CHANNELS; i++) {
    usage|=strtol(p, &p, 10);
  }
  return usage;
}

int Isdn (int *rx, int *tx, int *usage)
{
  static int fd=-2;
  CPS cps[ISDN_MAX_CHANNELS];
  double cps_i, cps_o;
  int i;

  *usage=0;
  *rx=0;
  *tx=0;

  if (fd==-1) return -1;
  
  if (fd==-2) {
    fd = open("/dev/isdninfo", O_RDONLY | O_NDELAY);
    if (fd==-1) {
      error ("open(/dev/isdninfo) failed: %s", strerror(errno));
      return -1;
    }
    debug ("open (/dev/isdninfo)=%d", fd);
  }

  if (ioctl(fd, IIOCGETCPS, &cps)) {
    error("ioctl(IIOCGETCPS) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }

  cps_i=0;
  cps_o=0;
  for (i=0; i<ISDN_MAX_CHANNELS; i++) {
    cps_i+=cps[i].in;
    cps_o+=cps[i].out;
  }

  *rx=(int)smooth("isdn_rx", 1000, cps_i);
  *tx=(int)smooth("isdn_tx", 1000, cps_o);
  *usage=Usage();

  return 0;
}