"Fossies" - the Fresh Open Source Software Archive 
Member "xosview-1.23/linux/wirelessmeter.cc" (11 Jul 2020, 3087 Bytes) of package /linux/misc/xosview-1.23.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "wirelessmeter.cc" see the
Fossies "Dox" file reference documentation.
1 //
2 // Copyright (c) 2001 by Tim Ehlers ( tehlers@gwdg.de )
3 //
4 // This file may be distributed under terms of the GPL
5 //
6 //
7
8 #include "wirelessmeter.h"
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <glob.h>
12 #include <fstream>
13 #include <iostream>
14
15
16 WirelessMeter::WirelessMeter( XOSView *parent, int ID, const char *wlID)
17 : FieldMeterGraph ( parent, 2, wlID, "LINK/LEVEL", 1, 1, 0 ), _number(ID) {
18 _lastquality = -1;
19 _lastlink = true;
20 total_ = 0;
21 }
22
23 WirelessMeter::~WirelessMeter( void ){
24 }
25
26 void WirelessMeter::checkResources( void ){
27 FieldMeterGraph::checkResources();
28
29 _poorqualcol = parent_->allocColor(parent_->getResource( "PoorQualityColor" ));
30 _fairqualcol = parent_->allocColor(parent_->getResource( "FairQualityColor" ));
31 _goodqualcol = parent_->allocColor(parent_->getResource( "GoodQualityColor" ));
32 setfieldcolor( 1, parent_->getResource( "wirelessUsedColor" ) );
33
34 priority_ = atoi(parent_->getResource( "wirelessPriority" ) );
35 dodecay_ = parent_->isResourceTrue( "wirelessDecay" );
36 SetUsedFormat(parent_->getResource( "wirelessUsedFormat" ) );
37 }
38
39 void WirelessMeter::checkevent( void ){
40 getpwrinfo();
41
42 drawfields();
43 }
44
45 void WirelessMeter::getpwrinfo( void ){
46 std::ifstream loadinfo( WLFILENAME );
47 if ( !loadinfo ){
48 std::cerr << "Can not open file : " << WLFILENAME << std::endl;
49 parent_->done(1);
50 return;
51 }
52
53 char buff[16];
54 int linkq = 0, quality = 0;
55 bool link = false;
56
57 // skip the two header rows
58 loadinfo.ignore(1024, '\n');
59 loadinfo.ignore(1024, '\n');
60
61 if ( _devname.empty() ) { // find devname on first run
62 for (int i = 0; i < _number; i++)
63 loadinfo.ignore(1024, '\n');
64 if ( loadinfo.good() )
65 loadinfo >> _devname >> buff >> linkq;
66 }
67 else {
68 while ( !loadinfo.eof() ) {
69 loadinfo >> buff;
70 if ( _devname == buff ) {
71 loadinfo >> buff >> linkq;
72 link = true;
73 break;
74 }
75 loadinfo.ignore(1024, '\n');
76 }
77 }
78
79 if ( linkq >= 250 )
80 linkq = 0;
81
82 fields_[0] = linkq;
83 if ( fields_[0] >= 15 )
84 quality = 2;
85 else if ( fields_[0] >= 7 )
86 quality = 1;
87 else
88 quality = 0;
89
90 if ( link && !_lastlink ) {
91 legend("LINK/LEVEL");
92 drawlegend();
93 _lastlink = link;
94 }
95 else if ( !link && _lastlink ) {
96 legend("NONE/LEVEL");
97 drawlegend();
98 _lastlink = link;
99 }
100
101 if ( quality != _lastquality ){
102 if ( quality == 0 )
103 setfieldcolor( 0, _poorqualcol );
104 else if ( quality == 1 )
105 setfieldcolor( 0, _fairqualcol );
106 else
107 setfieldcolor( 0, _goodqualcol );
108
109 drawlegend();
110 _lastquality = quality;
111 }
112
113 if (fields_[0] >= total_)
114 total_ = 30 * (int)(fields_[0] / 30 + 1);
115 fields_[1] = total_ - fields_[0];
116 setUsed(fields_[0], total_);
117 }
118
119 int WirelessMeter::countdevices(void){
120 glob_t gbuf;
121 glob("/sys/class/net/*/wireless", 0, NULL, &gbuf);
122 int count = gbuf.gl_pathc;
123 globfree(&gbuf);
124 return count;
125 }
126
127 const char *WirelessMeter::wirelessStr(int num) {
128 static char buffer[8] = "WL";
129 snprintf(buffer + 2, 5, "%d", num);
130 buffer[7] = '\0';
131 return buffer;
132 }