"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.22/linux/serialmeter.cc" (22 Feb 2020, 4736 Bytes) of package /linux/misc/xosview-1.22.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.21_vs_1.22.

    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 <sys/ioctl.h>
   12 #include <sys/types.h>
   13 #include <fcntl.h>
   14 #include <stdlib.h>
   15 #include <unistd.h>
   16 #include <iostream>
   17 #include <string>
   18 #include <sstream>
   19 #include <iomanip>
   20 
   21 // This should go away after types.h gets fixed in the kernel.
   22 #ifdef __alpha__
   23 typedef unsigned char u8;
   24 typedef signed short s16;
   25 typedef unsigned short u16;
   26 typedef signed int s32;
   27 typedef unsigned int u32;
   28 typedef signed long long s64;
   29 typedef unsigned long long u64;
   30 #endif
   31 
   32 #if defined(GNULIBC) || defined(__GLIBC__)
   33 #if !defined(__powerpc__) && !defined(__hppa__) && !defined(__mips__) && !defined(__sparc__) && !defined(__sh__) && !defined(__s390__) && !defined(__s390x__) && !defined(__m68k__) && !defined(__aarch64__) && !defined(__arm__)
   34 #include <sys/io.h>
   35 #endif
   36 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__powerpc__) && !defined(__ia64__) && !defined(__hppa__) && !defined(__arm__) && !defined(__mips__) && !defined(__sh__) && !defined(__s390__) && !defined (__s390x__) && !defined(__m68k__) && !defined(__aarch64__)
   37 #include <sys/perm.h>
   38 #define HAVE_IOPERM
   39 #endif
   40 #else
   41 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__powerpc__) && !defined(__ia64__) && !defined(__m68k__)
   42 #include <asm/io.h>
   43 #endif
   44 #endif
   45 #include <linux/serial.h>
   46 
   47 
   48 SerialMeter::SerialMeter( XOSView *parent, Device device )
   49   : BitMeter( parent, getTitle(device), "LSR bits(0-7), MSR bits(0-7)", 16){
   50   _device = device;
   51   _port = 0;
   52 }
   53 
   54 SerialMeter::~SerialMeter( void ){
   55 }
   56 
   57 void SerialMeter::checkevent( void ){
   58   getserial();
   59   BitMeter::checkevent();
   60 }
   61 
   62 void SerialMeter::checkResources( void ){
   63   BitMeter::checkResources();
   64   onColor_  = parent_->allocColor( parent_->getResource( "serialOnColor" ) );
   65   offColor_ = parent_->allocColor( parent_->getResource( "serialOffColor" ) );
   66   priority_ = atoi (parent_->getResource( "serialPriority" ) );
   67 
   68   _port = getPortBase(_device);
   69   if (!getport(_port + UART_LSR) || !getport(_port + UART_MSR)){
   70     std::cerr << "SerialMeter::SerialMeter() : "
   71          << "xosview must be suid root to use the serial meter." << std::endl;
   72     parent_->done(1);
   73   }
   74 }
   75 
   76 bool SerialMeter::getport(unsigned short int port){
   77 #ifdef HAVE_IOPERM
   78   return ioperm(port, 1, 1) != -1;
   79 #else
   80   return -1 != -1;
   81 #endif
   82 }
   83 
   84 void SerialMeter::getserial( void ){
   85 #ifdef HAVE_IOPERM
   86   // get the LSR and MSR
   87   unsigned char lsr = inb(_port + UART_LSR);
   88   unsigned char msr = inb(_port + UART_MSR);
   89 
   90   setBits(0, lsr);
   91   setBits(8, msr);
   92 #endif
   93 }
   94 
   95 const char *SerialMeter::getTitle(Device dev) const {
   96   static const char *names[] = { "ttyS0", "ttyS1", "ttyS2", "ttyS3",
   97                                  "ttyS4", "ttyS5", "ttyS6", "ttyS7",
   98                                  "ttyS8", "ttyS9" };
   99   return names[dev];
  100 }
  101 
  102 const char *SerialMeter::getResourceName(Device dev){
  103   static const char *names[] = { "serial0", "serial1",
  104                                  "serial2", "serial3",
  105                                  "serial4", "serial5",
  106                                  "serial6", "serial7",
  107                                  "serial8", "serial9" };
  108 
  109   return names[dev];
  110 }
  111 
  112 unsigned short int SerialMeter::getPortBase(Device dev) const {
  113   static const char *deviceFile[] = { "/dev/ttyS0",
  114                                 "/dev/ttyS1",
  115                                 "/dev/ttyS2",
  116                                 "/dev/ttyS3",
  117                                 "/dev/ttyS4",
  118                                 "/dev/ttyS5",
  119                                 "/dev/ttyS6",
  120                                 "/dev/ttyS7",
  121                                 "/dev/ttyS8",
  122                                 "/dev/ttyS9"};
  123 
  124   const char* res = parent_->getResource(getResourceName(dev));
  125 
  126   if (!strncasecmp(res, "True", 5)){ // Autodetect portbase.
  127     int fd;
  128     struct serial_struct serinfo;
  129 
  130     // get the real serial port (code stolen from setserial 2.11)
  131     if ((fd = open(deviceFile[dev], O_RDONLY|O_NONBLOCK)) < 0) {
  132       std::cerr << "SerialMeter::SerialMeter() : "
  133            << "failed to open " << deviceFile[dev] <<"." <<std::endl;
  134       exit(1);
  135     }
  136     if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) {
  137       std::cerr << "Failed to detect port base for " << deviceFile[dev]
  138         << std::endl;
  139       close(fd);
  140       exit(1);
  141     }
  142 
  143     close(fd);
  144     return serinfo.port;
  145   }
  146   else { // Use user specified port base.
  147     std::string s(res);
  148     std::istringstream istrm(s);
  149     unsigned short int tmp = 0;
  150     istrm >> std::hex >> tmp;
  151     return tmp;
  152   }
  153 
  154   return 0;
  155 }