From ed0d76f444dc4161c383f2959b35f9f89aa4d1d8 Mon Sep 17 00:00:00 2001 From: mfr69bs <> Date: Sun, 17 Jun 2012 13:55:00 +0000 Subject: [PATCH] CULflash separated from updatefhem to a new module git-svn-id: https://svn.fhem.de/fhem/trunk@1624 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/99_CULflash.pm | 146 +++++++++++++++++++++++++++++++++++++ fhem/FHEM/99_updatefhem.pm | 63 ---------------- fhem/HISTORY | 3 + 4 files changed, 150 insertions(+), 63 deletions(-) create mode 100644 fhem/FHEM/99_CULflash.pm diff --git a/fhem/CHANGED b/fhem/CHANGED index 531f633d6..cf82951f3 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -44,6 +44,7 @@ new global attribute added (M. Fischer) - feature: optional telnet password added / telnet port is optional - feature: holiday returns all matches, not only the first. + - change: CULflash separated from updatefhem to a new module (M. Fischer) - 2011-12-31 (5.2) - bugfix: applying smallscreen attributes to firefox/opera diff --git a/fhem/FHEM/99_CULflash.pm b/fhem/FHEM/99_CULflash.pm new file mode 100644 index 000000000..eb7dafb91 --- /dev/null +++ b/fhem/FHEM/99_CULflash.pm @@ -0,0 +1,146 @@ +############################################## +# $Id: $ +# modified by M. Fischer +package main; +use strict; +use warnings; +use IO::Socket; + +sub CommandCULflash($$); +sub GetHttpFile($$@); +sub SplitNewFiletimes($); + +my $server = "fhem.de:80"; +my $sdir = "/fhemupdate2"; +my $ftime = "filetimes.txt"; +my $dfu = "dfu-programmer"; + +##################################### +sub +CULflash_Initialize($$) +{ + my %chash = ( Fn=>"CommandCULflash", + Hlp=>" ,flash the CUL from the nightly SVN" ); + $cmds{CULflash} = \%chash; + +} + +##################################### +sub +CommandCULflash($$) +{ + my ($cl, $param) = @_; + my $modpath = (-d "update" ? "update" : $attr{global}{modpath}); + my $moddir = "$modpath/FHEM"; + + my %ctypes = ( + CUL_V2 => "at90usb162", + CUL_V2_HM => "at90usb162", + CUL_V3 => "atmega32u4", + CUL_V4 => "atmega32u2", + ); + my @a = split("[ \t]+", $param); + return "Usage: CULflash , ". + "where is one of ". join(" ", sort keys %ctypes) + if(!(int(@a) == 2 && + ($a[0] eq "none" || ($defs{$a[0]} && $defs{$a[0]}{TYPE} eq "CUL")) && + $ctypes{$a[1]})); + + my $cul = $a[0]; + my $target = $a[1]; + + ################################ + # First get the index file to prove the file size + my $filetimes = GetHttpFile($server, "$sdir/$ftime"); + return "Can't get $ftime from $server" if(!$filetimes); + + # split filetime and filesize + my ($ret, $filetime, $filesize) = SplitNewFiletimes($filetimes); + return $ret if($ret); + + ################################ + # Now get the firmware file: + my $content = GetHttpFile($server, "$sdir/FHEM/$target.hex"); + return "File size for $target.hex does not correspond to filetimes.txt entry" + if(length($content) ne $filesize->{"FHEM/$target.hex"}); + my $localfile = "$moddir/$target.hex"; + open(FH,">$localfile") || return "Can't write $localfile"; + print FH $content; + close(FH); + + my $cmd = "($dfu MCU erase && $dfu MCU flash TARGET && $dfu MCU start) 2>&1"; + my $mcu = $ctypes{$target}; + $cmd =~ s/MCU/$mcu/g; + $cmd =~ s/TARGET/$localfile/g; + + if($cul ne "none") { + CUL_SimpleWrite($defs{$cul}, "B01"); + sleep(4); # B01 needs 2 seconds for the reset + } + Log 1, "CULflash $cmd"; + my $result = `$cmd`; + Log 1, "CULflash $result"; + return $result; +} + +sub +GetHttpFile($$@) +{ + my ($host, $filename, $timeout) = @_; + $timeout = 2.0 if(!defined($timeout)); + + $filename =~ s/%/%25/g; + my $conn = IO::Socket::INET->new(PeerAddr => $host); + if(!$conn) { + Log 1, "CULflash Can't connect to $host\n"; + undef $conn; + return undef; + } + $host =~ s/:.*//; + my $req = "GET $filename HTTP/1.0\r\nHost: $host\r\n\r\n\r\n"; + syswrite $conn, $req; + shutdown $conn, 1; # stopped writing data + my ($buf, $ret) = ("", ""); + + $conn->timeout($timeout); + for(;;) { + my ($rout, $rin) = ('', ''); + vec($rin, $conn->fileno(), 1) = 1; + my $nfound = select($rout=$rin, undef, undef, $timeout); + if($nfound <= 0) { + Log 1, "CULflash GetHttpFile: Select timeout/error: $!"; + undef $conn; + return undef; + } + + my $len = sysread($conn,$buf,65536); + last if(!defined($len) || $len <= 0); + $ret .= $buf; + } + + $ret=~ s/(.*?)\r\n\r\n//s; # Not greedy: switch off the header. + Log 4, "CULflash Got http://$host$filename, length: ".length($ret); + undef $conn; + return $ret; +} + +sub +SplitNewFiletimes($) +{ + my $filetimes = shift; + my $ret; + my (%filetime, %filesize) = (); + foreach my $l (split("[\r\n]", $filetimes)) { + chomp($l); + $ret = "Corrupted filetimes.txt file" + if($l !~ m/^20\d\d-\d\d-\d\d_\d\d:\d\d:\d\d /); + last if($ret); + my ($ts, $fs, $file) = split(" ", $l, 3); + $filetime{$file} = $ts; + $filesize{$file} = $fs; + } + return ($ret, \%filetime, \%filesize); +} + +# vim: ts=2:et +1; diff --git a/fhem/FHEM/99_updatefhem.pm b/fhem/FHEM/99_updatefhem.pm index b67034e1d..7afb50e4c 100644 --- a/fhem/FHEM/99_updatefhem.pm +++ b/fhem/FHEM/99_updatefhem.pm @@ -7,7 +7,6 @@ use warnings; use IO::Socket; sub CommandUpdatefhem($$); -sub CommandCULflash($$); sub GetHttpFile($$@); sub ParseChanges($); sub ReadOldFiletimes($); @@ -17,8 +16,6 @@ sub FileList($); my $server = "fhem.de:80"; my $sdir = "/fhemupdate2"; my $ftime = "filetimes.txt"; -my $dfu = "dfu-programmer"; - ##################################### sub @@ -28,9 +25,6 @@ updatefhem_Initialize($$) Hlp=>",update fhem from the nightly SVN" ); $cmds{updatefhem} = \%fhash; - my %chash = ( Fn=>"CommandCULflash", - Hlp=>" ,flash the CUL from the nightly SVN" ); - $cmds{CULflash} = \%chash; } ##################################### @@ -316,63 +310,6 @@ CommandUpdatefhem($$) return $ret; } -sub -CommandCULflash($$) -{ - my ($cl, $param) = @_; - my $modpath = (-d "update" ? "update" : $attr{global}{modpath}); - my $moddir = "$modpath/FHEM"; - - my %ctypes = ( - CUL_V2 => "at90usb162", - CUL_V2_HM => "at90usb162", - CUL_V3 => "atmega32u4", - CUL_V4 => "atmega32u2", - ); - my @a = split("[ \t]+", $param); - return "Usage: CULflash , ". - "where is one of ". join(" ", sort keys %ctypes) - if(!(int(@a) == 2 && - ($a[0] eq "none" || ($defs{$a[0]} && $defs{$a[0]}{TYPE} eq "CUL")) && - $ctypes{$a[1]})); - - my $cul = $a[0]; - my $target = $a[1]; - - ################################ - # First get the index file to prove the file size - my $filetimes = GetHttpFile($server, "$sdir/$ftime"); - return "Can't get $ftime from $server" if(!$filetimes); - - # split filetime and filesize - my ($ret, $filetime, $filesize) = SplitNewFiletimes($filetimes); - return $ret if($ret); - - ################################ - # Now get the firmware file: - my $content = GetHttpFile($server, "$sdir/FHEM/$target.hex"); - return "File size for $target.hex does not correspond to filetimes.txt entry" - if(length($content) ne $filesize->{"FHEM/$target.hex"}); - my $localfile = "$moddir/$target.hex"; - open(FH,">$localfile") || return "Can't write $localfile"; - print FH $content; - close(FH); - - my $cmd = "($dfu MCU erase && $dfu MCU flash TARGET && $dfu MCU start) 2>&1"; - my $mcu = $ctypes{$target}; - $cmd =~ s/MCU/$mcu/g; - $cmd =~ s/TARGET/$localfile/g; - - if($cul ne "none") { - CUL_SimpleWrite($defs{$cul}, "B01"); - sleep(4); # B01 needs 2 seconds for the reset - } - Log 1, "updatefhem $cmd"; - my $result = `$cmd`; - Log 1, "updatefhem $result"; - return $result; -} - sub GetHttpFile($$@) { diff --git a/fhem/HISTORY b/fhem/HISTORY index ca39ace74..e22be066f 100644 --- a/fhem/HISTORY +++ b/fhem/HISTORY @@ -513,3 +513,6 @@ - Sun Jun 03 2012 (M. Fischer) - Added new global attribute - Added new parameter to updatefhem + +- Sun Jun 17 2012 (M. Fischer) + - CULflash routine from updatefhem removed. CULflash is a standalone module from now.