#!/usr/bin/perl #use strict; use vars qw( $OS_win ); use Getopt::Std; use Socket; use Sys::Syslog; use sigtrap; BEGIN { $OS_win = ($^O eq "MSWin32") ? 1 : 0; if ($OS_win) { eval "use Win32::SerialPort 0.11"; die2("$@\n") if ($@); } else { eval "use Device::SerialPort"; die2("$@\n") if ($@); } } #End BEGIN $SIG{TERM} = sub{&die2('Sig Terminate.')}; $SIG{INT} = sub{&die2('Sig Interupt.')}; $SIG{HUP} = sub{&conf_load;syslog('notice', 'Znovu nactena konfigurace');}; $SIG{CHLD} = 'IGNORE'; #\&reaper; $SIG{PIPE} = \&pipe; my ($dev_man) = "/dev/ttyS0"; my ($dev_out)= "/dev/ttyS1"; my ($port) = 8766; my ($conf_file) = "$ENV{HOME}/.ircontrol.cfg"; my ($sock_file) = "/dev/lircd"; my ($code, %conf, %conf_act, $ser, $val); my ($share_key); my ($ident)='ircontrol'; &getopts("divslo:c:"); openlog($ident, '', 'daemon'); if ($opt_c =~ m|^/[\w\-\=\+\/\.]+$|){$conf_file = $opt_c;} if ($opt_d && &init_ir && &init_out()){ &conf_load(); &daemon(); }elsif ($opt_i && &init_ir()){ &conf_load(); &activ(); }else{ die "Usege: -[d|i] [-l] [-s] [-o i|r] [-c /cesta/k/souboru/irxxx.cfg \t -d - daemon mode \t -c - Konfiguracni soubor \t -s - Nasloucha na unix soketu $sock_file \t -i - interaktiv mod \t -l - listen on port $port \t -o dev - povoli vystupni zarizeni - kde dev je 'r'-reg or 'i'-i2c \t -v - debug mode \t verze 0.1.alpha\n"; } &die2("konec programu\n"); ################### --------------------- ################### ################### funkce a podprogramy ################### ################### --------------------- ################### ########################### D AE M O N M O D E ######################### sub daemon { my ($code, $val, $sock, $pid, $proto); ###vytvori novy proces a puvodni ukonci $pid = fork; exit 1 if $pid; die2("Couldn't fork: $!") unless defined($pid); # if ($opt_l){ # &tcp_out; # } if ($opt_s){ &socket_init(); } syslog('notice', 'Ircontrol inicializovan'); # $lir_sock = pack_sockaddr_un ($lir_dev); # socket(OUT, PF_UNIX, SOCK_STREAM, ) || die2("nelze otevrit unix soket $!"); # bind(OUT,/tmp/irman.sock) || die2("nelze pojmenovat soket $!"); # unlink ('$lir_dev'); # $con_stat = connect(OUT,$lir_sock) || die2("no connect $!"); # print "$con_stat\n"; #========= hlavni pracovni smycka programu =========== while(1){ $code = rxd(); if ($opt_v){print "RXd - $code\n";} #debug foreach $val (keys(%conf)){ if ($conf{$val} eq $code){&exe($val)}; } } } ################ S U B rutiny ############################# ########################################################### ############### ke konfiguraci programu ################## sub conf_save{ my $key; open(CONF,">$conf_file") || print("Nelze zapsat do $conf_file !!\n") && return (0); foreach $key(@conf_full){ print(CONF"$key"); } close(CONF); syslog('notice', 'Konfigurace ulozena do %s',$conf_file); } sub conf_load{ my ($key, $value, $radek, $action); open(CONF,"$conf_file") || print("Nelze otevrit $conf_file !!\n") && return (0); @conf_full = ; close(CONF); foreach $radek (@conf_full){ next if ($radek =~ /^\s*#/); ($key,$value,$action) = $radek =~ /\s*(\w+)\s*-\s*([\da-f]*)\s*-\s*(.*)$/; if ($key){ $conf{$key}=$value; $conf_act{$key}= $action ? $action : "ext"; } } } sub test_conf{ #test stejnych kodu my $key; foreach $key(keys(%conf)){ if ($conf{$key} =~/\s*/){next;} foreach $key2(keys(%conf)){ if ($conf{$key} eq $conf{$key2} and $key ne $key2){ print "****** POZOR ******\n$key ma stejny kod jako $key2\n" } } } print "Enter:>"; <>; } ########################## uceni kodu ############################# sub learnig{ my ($key, $i,$k); system ("clear"); foreach $i (sort keys %conf){ print "\n\n\n\nZadani pro:$i\tnyni:"; print $conf{$i} ? $conf{$i} : ' -----'; print "\n"; print "n - Next\n"; print "e - erase (vymazat kod)\n"; print "q - Exit\n"; print "enter - pokracovat zadavanim kodu\n"; print "###>"; $key=; chomp($key); if ($key eq 'q'){ &test_conf;return; }elsif ($key eq 'e'){ $conf{$i}=''; }elsif ($key eq 'n'){ next; }else{ print "Stisknete tlacitko na ovladaci\n"; $new_kode = &rxd(); $conf{$i}=$new_kode; for($k=0;$k<@conf_full;$k++){ if ($conf_full[$k] =~ s/^\s*($i)\s*-\s*[\da-f]*\s*-/$i - $new_kode -/){ system ("clear"); print ("\n\nK $i prirazen kod: $new_kode\n"); } } } } } ########################## ukazat nastaveni ######################### sub show{ my $key; print ("vypis nastaveni\n"); foreach $key(sort keys(%conf)){ print ("$key - $conf{$key} - $conf_act{$key}\n"); } print "Enter pro pokracovani\n"; <>; } ########################## ## sub k interaktiv modu # ## cteni peikazu ######### sub activ{ my $key; while(1){ $key=menu(); if($key eq "1"){ learnig(); }elsif($key eq "2"){ show(); }elsif($key eq "3"){ daemon(); }elsif($key eq "4"){ conf_load(); }elsif($key eq "5"){ conf_save(); }elsif($key eq "q"){ last; } } } ##### interaktiv menu sub menu{ my $key; system ("clear"); print ("1 - Naucit\n"); print ("2 - Ukazat nastaveni \n"); print ("3 - prejit do deamon modu \n"); print ("4 - nacist konfiguraci \n"); print ("5 - ulizit konfikuraci \n"); print ("q - Ukoncit \n"); print ("Volba>"); $key=; chomp($key); return $key; } ########################## ------------------------------ ########## ########################## inicializace zarizeni $dev_man ########## ########################## ------- pro prijem IR ------ ########## sub die2{ undef $ir_dev; undef $io_dev; syslog('notice', 'Ukoncuji: %s',@_); closelog; die "@_"; } sub init_ir{ my $status; if ($OS_win) { $ir_dev = Win32::SerialPort->new ($dev_man, 0, 0); }else { $ir_dev = Device::SerialPort->new ($dev_man, 0, 0); } die2("Can't open serial port from $dev_man: $^E\n") unless ($ir_dev); $ir_dev->rts_active(0); $ir_dev->dtr_active(0); $ir_dev->baudrate(9600); $ir_dev->parity("none"); $ir_dev->databits(8); $ir_dev->stopbits(1); $ir_dev->handshake("none"); $ir_dev->rts_active(1); $ir_dev->dtr_active(1); &delay(0.3); $ir_dev->purge_all; $status = $ir_dev->write(I); &delay(0.1); $status = $ir_dev->write(R); &delay(0.1); $status = $ir_dev->input; print ("precteno $status\n") if ($opt_v); if($status eq 'OK'){return 1}; print "Inicializace Irman selhala\n"; 0; } ########################## prijem z irmanu ################################## sub rxd{ my ($count, $code); $ir_dev->purge_rx; while(1){ ($count,$code) = $ir_dev->read(6); if ($count){ $code = unpack("h*",$code); # print ("code - $code \t delka - $count\n"); last; } &delay(0.05); } return $code; } ##### ##### funkce vyhodnocuje prijaty kod a vykonava akce podle configu ##### nebo posila externim programum (pres local_soket jako lircd nebo pres TCP spojeni) ##### ##### sub exe { my ($key)=@_; my ($out_val, $mode, $addres, $value, $do, $mess,$prikaz); if($opt_v){print ("volam exe klic: $key\t hodnota:$conf_act{$key}\n");} if ($conf_act{$key} =~ m|^(\/[\w\-\=\+\.\s\/]+$)|){ print "Predavam prikaz $1 systemu\n" if ($opt_v); #predani prikazu z configu operacnimu systemu # velice nebezpecne !!!!!!!!!!!!!! system "$1"; ##posilani na soket }elsif ($conf_act{$key} eq "ext"){ if (defined($count) && $count <= '34'){$count++; }else{ $count = '1'; } $mess = unpack(B16,$count). " 0 $key rc-5_whit_irman\n"; print "Zapisuji do share variable: $mess" if ($opt_v); shmwrite($share_key, $mess, 0, 60 ) or die2("shmwrite: $!"); &delay(0.1); #