#!/usr/bin/perl

#                                   
# anno '99 by Thomas Koch <no-return@gmx.net>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# bevor dieses programm verwendet werden kann muessen die C header files
# aus /usr/include zu PERL header files konvertiert werden.
#
# cd /usr/include
# h2ph -r -l .
#
# Weitere Infos: man h2ph
#

# needed
require 'ioctl.ph';


# Serielle Schnittstelle zum lesen/schreiben oeffnen
if($ARGV[0]) {

    open(FD, ">$ARGV[0]") || die "Konnte $ARGV[0] nicht oeffnen!\n";

} else {

    print "\nSowas aber auch, du hast vergessen mir zu sagen welche Schnittstelle\n ich ueberwachen soll....\n";
    print "\n./testser serialdevice\n";
    exit(1);

}


# per ioctl die daten vonner seriellen schnittstelle holen
# wobei 0x5415 der definition TIOCMGET aus /usr/include/asm/ioctls.h entspricht
# 
# Warum ich TIOCMGET nicht verwenden kann weis der Deibel, als HEX gehts.
#
ioctl(FD, 0x5415, $ioctlinput);

# ergebniss nach INT wandeln
$test = unpack("I", $ioctlinput);


#
# 0x100 = DSR  (PIN 6, TIOCM_DSR)  -- DATA SEND READY
# 0x080 = RING (PIN 9, TIOCM_RNG)  -- RING (wird auf low(?) gesetzt wenn modem
#					    anruf erkennt)					    
# 0x040 = CAR  (PIN 1, TIOCM_CAR)  -- CARRIER (wenn leitung steht)
# 0x020 = CTS  (PIN 8, TIOCM_CTS)  -- CLEAR TO SEND
# 0x004 = RTS  (PIN 7, TIOCM_RTS)  -- REQUEST TO SEND               *
# 0x002 = DTR  (PIN 4, TIOCM_DTR)  -- DATA TERMINAL READY           *
#
# PIN ist bezogen auf einen 9 poligen stecker/buchse
#
print "RING !!\n" if $test & 0x080;
print "DSR  !!\n" if $test & 0x100;
print "CAR  !!\n" if $test & 0x040;
print "CTS  !!\n" if $test & 0x020;
print "RTS  !!\n" if $test & 0x004;
print "DTR  !!\n" if $test & 0x002;


exit(0);
# end

