"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/linux/serialmeter.cc" (11 Jul 2020, 3924 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 "serialmeter.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.22_vs_1.23.

    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 
   32 SerialMeter::SerialMeter( XOSView *parent, Device device )
   33   : BitMeter( parent, getTitle(device), "LSR bits(0-7), MSR bits(0-7)", 16){
   34   _device = device;
   35   _port = 0;
   36 }
   37 
   38 SerialMeter::~SerialMeter( void ){
   39 }
   40 
   41 void SerialMeter::checkevent( void ){
   42   getserial();
   43   BitMeter::checkevent();
   44 }
   45 
   46 void SerialMeter::checkResources( void ){
   47   BitMeter::checkResources();
   48   onColor_  = parent_->allocColor( parent_->getResource( "serialOnColor" ) );
   49   offColor_ = parent_->allocColor( parent_->getResource( "serialOffColor" ) );
   50   priority_ = atoi (parent_->getResource( "serialPriority" ) );
   51 
   52   _port = getPortBase(_device);
   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 
   68 void SerialMeter::getserial( void ){
   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 
   86 const char *SerialMeter::getResourceName(Device dev){
   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 }