File: wirelessmeter.cc

package info (click to toggle)
xosview 1.9.3-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,056 kB
  • sloc: cpp: 10,117; makefile: 142; ansic: 32; awk: 13; sh: 8
file content (173 lines) | stat: -rw-r--r-- 3,713 bytes parent folder | download
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
//  
//  Copyright (c) 2001 by Tim Ehlers ( tehlers@gwdg.de )
//
//  This file may be distributed under terms of the GPL
//
//
// $Id: wirelessmeter.cc,v 1.3 2001/06/13
//
#include "wirelessmeter.h"
#include "xosview.h"
#include <fstream>
#include <stdlib.h>
#include <string.h>
//#include <strstream>
#include <sstream>
#include <ctype.h>

using namespace std;

static const char WLFILENAME[] = "/proc/net/wireless";

WirelessMeter::WirelessMeter( XOSView *parent, int ID, const char *wlID)
	: FieldMeterGraph ( parent, 2, wlID, "LINK/LEVEL", 1, 1, 0 ), _number(ID) {
  lastqualitystate = -1;
  strcpy(devname, "0");
}

WirelessMeter::~WirelessMeter( void ){
}

void WirelessMeter::checkResources( void ){
  FieldMeterGraph::checkResources();

  poorqualcol_ = parent_->allocColor(parent_->getResource( "PoorQualityColor" ));
  fairqualcol_ = parent_->allocColor(parent_->getResource( "FairQualityColor" ));
  goodqualcol_ = parent_->allocColor(parent_->getResource( "GoodQualityColor" ));

  setfieldcolor( 1, parent_->getResource( "wirelessUsedColor" ) );

  priority_ = atoi (parent_->getResource( "wirelessPriority" ) );
  dodecay_ = parent_->isResourceTrue( "wirelessDecay" );
  SetUsedFormat(parent_->getResource( "wirelessUsedFormat" ) );
}

void WirelessMeter::checkevent( void ){
  getpwrinfo();

  drawfields();
}


void WirelessMeter::getpwrinfo( void ){
  ifstream loadinfo( WLFILENAME );

  if ( !loadinfo ){
    cerr <<"Can not open file : " <<WLFILENAME <<endl;
    parent_->done(1);
    return;
  }

  char buff[256];

  for (int i = 1 ; i < 2 ; i++)
    loadinfo.getline(buff, 256);

  int linkq = 0;

if (strncmp(devname, "0", 1)) {

  while (!loadinfo.eof()){
    loadinfo.getline(buff, 256);
    if (!loadinfo.eof()){
        loadinfo >> buff;
      if (!strncmp(buff, devname, strlen(devname))) {
	loadinfo >> buff >> linkq; }

} } }

if (!strncmp(devname, "0", 1)) {

  for (int i = 0 ; i < _number; ) {
    loadinfo.getline(buff, 256);
	loadinfo >> devname >> buff >> linkq;
	if (!loadinfo.eof()){
          if ( linkq != 0 ) i++; }
		if ( loadinfo.eof() ) break; }

}

  fields_[0] = linkq;

  if ( fields_[0] <  7 ) qualitystate = 0;
  if ( fields_[0] >= 7 ) qualitystate = 1;
  if ( fields_[0] >= 15 ) qualitystate = 2;
  
  if ( qualitystate != lastqualitystate ){
    if ( qualitystate == 0 ) setfieldcolor( 0, poorqualcol_ );
    if ( qualitystate == 1 ) setfieldcolor( 0, fairqualcol_ );
    if ( qualitystate == 2 ) setfieldcolor( 0, goodqualcol_ );
    drawlegend();
    lastqualitystate = qualitystate;
  }

    if ( fields_[0] >= 250 ) { fields_[0] = 0; qualitystate = 0; }

  total_ = 240;

 if ( fields_[0] < 210)
    total_ = 210;

 if ( fields_[0] < 180)
    total_ = 180;

 if ( fields_[0] < 150)
    total_ = 150;

 if ( fields_[0] < 120)
    total_ = 120;

 if ( fields_[0] < 90)
    total_ = 90;

 if ( fields_[0] < 60)
    total_ = 60;

 if ( fields_[0] < 30)
    total_ = 30;

  fields_[1] = fields_[0];

  setUsed (fields_[0], total_);
}

int WirelessMeter::countdevices(void){
  ifstream stats( WLFILENAME );

  if ( !stats ){
    cerr <<"Can not open file : " <<WLFILENAME <<endl;
    exit( 1 );
  }

char buf[1024];

for (int i = 1 ; i < 2 ; i++)
    stats.getline(buf, 1024);

  int wirelessCount = 0;
  int linkq = 0;
  while (!stats.eof()){
    stats.getline(buf, 1024);
	stats >> buf >> buf >> linkq;
if (!stats.eof()){
	  if ( linkq != 0 )
          wirelessCount++;
  }}

  return wirelessCount;
}

const char *WirelessMeter::wirelessStr(int num){
  static char buffer[32];
  std::ostringstream str;
  
  str << "WLAN";
  if (num != 1)
    str << (num);
  str << std::ends;

  strncpy(buffer, str.str().c_str(), 32);
  buffer[31] = '\0';

  return buffer;
}