aboutsummaryrefslogtreecommitdiffstats
path: root/isdn.c
blob: fab470ae8c1a8ae9fe5c92f668154779a70f3ed6 (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/isdn.h>

#include "isdn.h"
#include "filter.h"
#include "lcd4linux.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) {
    perror ("open(/dev/isdninfo) failed");
    return 0;
  }
  
  if (read (fd, buffer, sizeof(buffer))==-1) {
    perror ("read(/dev/isdninfo) failed");
    fd=-1;
    return 0;
  }

  if (close(fd)==-1) {
    perror ("close(/dev/isdninfo) failed");
    fd=-1;
    return 0;
  }

  p=strstr(buffer, "usage:");
  if (p==NULL) {
    fprintf (stderr, "parse(/dev/isdninfo) failed: no usage line\n");
    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)
{
  static int fd=-2;
  CPS cps[ISDN_MAX_CHANNELS];
  double cps_i, cps_o;
  int i;

  *rx=0;
  *tx=0;

  if (fd==-1) return 0;
  
  if (fd==-2) {
    fd = open("/dev/isdninfo", O_RDONLY | O_NDELAY);
    if (fd==-1) {
      perror ("open(/dev/isdninfo) failed");
      return 0;
    }
  }
  if (ioctl(fd, IIOCGETCPS, &cps)) {
    perror("ioctl(IIOCGETCPS) failed");
    fd=-1;
    return 0;
  }
  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);

  return Usage();
}