xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

serialmeter.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
3 //
4 // This file may be distributed under terms of the GPL
5 //
6 
7 //
8 // In order to use this new serial meter, xosview needs to be suid root.
9 //
10 #include "serialmeter.h"
11 #include <linux/serial.h>
12 #include <sys/ioctl.h>
13 #include <sys/types.h>
14 #include <fcntl.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <iostream>
18 #include <string>
19 #include <sstream>
20 #include <iomanip>
21 
22 /*
23  * To fetch status information requires ioperm() and inb()
24  * otherwise these meter is largely a no-op.
25  */
26 #if defined(__i386__) || defined(__ia64__) || defined(__amd64__)
27 #include <sys/io.h>
28 #include <sys/perm.h>
29 #define HAVE_IOPERM
30 #endif
31 
33  : BitMeter( parent, getTitle(device), "LSR bits(0-7), MSR bits(0-7)", 16){
34  _device = device;
35  _port = 0;
36 }
37 
39 }
40 
42  getserial();
44 }
45 
48  onColor_ = parent_->allocColor( parent_->getResource( "serialOnColor" ) );
49  offColor_ = parent_->allocColor( parent_->getResource( "serialOffColor" ) );
50  priority_ = atoi (parent_->getResource( "serialPriority" ) );
51 
53  if (!getport(_port + UART_LSR) || !getport(_port + UART_MSR)){
54  std::cerr << "SerialMeter::SerialMeter() : "
55  << "xosview must be suid root to use the serial meter." << std::endl;
56  parent_->done(1);
57  }
58 }
59 
60 bool SerialMeter::getport(unsigned short int port){
61 #ifdef HAVE_IOPERM
62  return ioperm(port, 1, 1) != -1;
63 #else
64  return false;
65 #endif
66 }
67 
69 #ifdef HAVE_IOPERM
70  // get the LSR and MSR
71  unsigned char lsr = inb(_port + UART_LSR);
72  unsigned char msr = inb(_port + UART_MSR);
73 
74  setBits(0, lsr);
75  setBits(8, msr);
76 #endif
77 }
78 
79 const char *SerialMeter::getTitle(Device dev) const {
80  static const char *names[] = { "ttyS0", "ttyS1", "ttyS2", "ttyS3",
81  "ttyS4", "ttyS5", "ttyS6", "ttyS7",
82  "ttyS8", "ttyS9" };
83  return names[dev];
84 }
85 
87  static const char *names[] = { "serial0", "serial1",
88  "serial2", "serial3",
89  "serial4", "serial5",
90  "serial6", "serial7",
91  "serial8", "serial9" };
92 
93  return names[dev];
94 }
95 
96 unsigned short int SerialMeter::getPortBase(Device dev) const {
97  static const char *deviceFile[] = { "/dev/ttyS0",
98  "/dev/ttyS1",
99  "/dev/ttyS2",
100  "/dev/ttyS3",
101  "/dev/ttyS4",
102  "/dev/ttyS5",
103  "/dev/ttyS6",
104  "/dev/ttyS7",
105  "/dev/ttyS8",
106  "/dev/ttyS9"};
107 
108  const char* res = parent_->getResource(getResourceName(dev));
109 
110  if (!strncasecmp(res, "True", 5)){ // Autodetect portbase.
111  int fd;
112  struct serial_struct serinfo;
113 
114  // get the real serial port (code stolen from setserial 2.11)
115  if ((fd = open(deviceFile[dev], O_RDONLY|O_NONBLOCK)) < 0) {
116  std::cerr << "SerialMeter::SerialMeter() : "
117  << "failed to open " << deviceFile[dev] <<"." <<std::endl;
118  exit(1);
119  }
120  if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) {
121  std::cerr << "Failed to detect port base for " << deviceFile[dev]
122  << std::endl;
123  close(fd);
124  exit(1);
125  }
126 
127  close(fd);
128  return serinfo.port;
129  }
130  else { // Use user specified port base.
131  std::string s(res);
132  std::istringstream istrm(s);
133  unsigned short int tmp = 0;
134  istrm >> std::hex >> tmp;
135  return tmp;
136  }
137 
138  return 0;
139 }
unsigned long onColor_
Definition: bitmeter.h:30
unsigned long offColor_
Definition: bitmeter.h:30
void setBits(int startbit, unsigned char values)
Definition: bitmeter.cc:106
void checkResources(void)
Definition: bitmeter.cc:43
void checkevent(void)
Definition: bitmeter.cc:47
int priority_
Definition: meter.h:54
XOSView * parent_
Definition: meter.h:52
unsigned short int getPortBase(Device dev) const
Definition: serialmeter.cc:96
SerialMeter(XOSView *parent, Device device)
Definition: serialmeter.cc:32
unsigned short int _port
Definition: serialmeter.h:33
static const char * getResourceName(Device dev)
Definition: serialmeter.cc:86
void checkResources(void)
Definition: serialmeter.cc:46
void checkevent(void)
Definition: serialmeter.cc:41
Device _device
Definition: serialmeter.h:34
const char * getTitle(Device dev) const
Definition: serialmeter.cc:79
bool getport(unsigned short int port)
Definition: serialmeter.cc:60
~SerialMeter(void)
Definition: serialmeter.cc:38
void getserial(void)
Definition: serialmeter.cc:68
unsigned long allocColor(const char *name)
Definition: xwin.cc:383
int done(void)
Definition: xwin.h:29
const char * getResource(const char *name)
Definition: xwin.cc:362
#define UART_LSR
Definition: serialmeter.h:11
#define UART_MSR
Definition: serialmeter.h:12