usb create added

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@1148 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2011-12-28 19:21:19 +00:00
parent ca492200b9
commit 5a2ca9d88c
8 changed files with 184 additions and 27 deletions

View File

@ -27,7 +27,8 @@
- feature: FHEMWEB save button, smallscreen first screen fix - feature: FHEMWEB save button, smallscreen first screen fix
- feature: FHEMWEB encoding is now UTF-8, alias attribute is respected - feature: FHEMWEB encoding is now UTF-8, alias attribute is respected
- change: HTTPS certs directory moved from cwd into modpath - change: HTTPS certs directory moved from cwd into modpath
- feature: shutdwown parameter restart added - feature: shutdown parameter restart added
- feature: usb scan/create command added (part of autocreate).
- 2011-07-08 (5.1) - 2011-07-08 (5.1)

View File

@ -239,7 +239,7 @@ FHZ_Get($@)
FHZ_Write($hash, $fn, $arg) if(!IsDummy($hash->{NAME})); FHZ_Write($hash, $fn, $arg) if(!IsDummy($hash->{NAME}));
my $msg = FHZ_ReadAnswer($hash, $a[1], 1.0); my $msg = FHZ_ReadAnswer($hash, $a[1], 1.0);
Log 5, "GET Got: $msg"; Log 5, "GET Got: $msg" if(defined($msg));
return $msg if(!$msg || $msg !~ /^81..c9..0102/); return $msg if(!$msg || $msg !~ /^81..c9..0102/);
if($a[1] eq "serial") { if($a[1] eq "serial") {

View File

@ -268,7 +268,7 @@ KM271_Set($@)
my $data = ($val ? sprintf($fmt, $val) : $fmt); my $data = ($val ? sprintf($fmt, $val) : $fmt);
push @{$hash->{SENDBUFFER}}, $data; push @{$hash->{SENDBUFFER}}, $data;
DevIo_SimpleWrite($hash, "02") if(!$hash->{WAITING}); DevIo_SimpleWrite($hash, "02", 1) if(!$hash->{WAITING});
return undef; return undef;
} }
@ -292,31 +292,31 @@ KM271_Read($)
if(@{$hash->{SENDBUFFER}} || $hash->{DATASENT}) { # Send data if(@{$hash->{SENDBUFFER}} || $hash->{DATASENT}) { # Send data
if($buf eq "02") { # KM271 Wants to send, override if($buf eq "02") { # KM271 Wants to send, override
DevIo_SimpleWrite($hash, "02"); DevIo_SimpleWrite($hash, "02", 1);
return; return;
} }
if($buf eq "10") { if($buf eq "10") {
if($hash->{DATASENT}) { if($hash->{DATASENT}) {
delete($hash->{DATASENT}); delete($hash->{DATASENT});
DevIo_SimpleWrite($hash, "02") if(@{$hash->{SENDBUFFER}}); DevIo_SimpleWrite($hash, "02", 1) if(@{$hash->{SENDBUFFER}});
return; return;
} }
$data = pop @{ $hash->{SENDBUFFER} }; $data = pop @{ $hash->{SENDBUFFER} };
$data =~ s/10/1010/g; $data =~ s/10/1010/g;
$crc = KM271_crc($data); $crc = KM271_crc($data);
DevIo_SimpleWrite($hash, $data."1003$crc"); # Send the data DevIo_SimpleWrite($hash, $data."1003$crc", 1); # Send the data
} }
if($buf eq "15") { # NACK from the KM271 if($buf eq "15") { # NACK from the KM271
Log 1, "$name: NACK!"; Log 1, "$name: NACK!";
delete($hash->{DATASENT}); delete($hash->{DATASENT});
DevIo_SimpleWrite($hash, "02") if(@{$hash->{SENDBUFFER}}); DevIo_SimpleWrite($hash, "02", 1) if(@{$hash->{SENDBUFFER}});
return; return;
} }
} elsif($buf eq "02") { # KM271 Wants to send } elsif($buf eq "02") { # KM271 Wants to send
DevIo_SimpleWrite($hash, "10"); # We are ready DevIo_SimpleWrite($hash, "10", 1); # We are ready
$hash->{PARTIAL} = ""; $hash->{PARTIAL} = "";
$hash->{WAITING} = 1; $hash->{WAITING} = 1;
return; return;
@ -333,12 +333,12 @@ KM271_Read($)
if(KM271_crc($data) ne $crc) { if(KM271_crc($data) ne $crc) {
Log 1, "Wrong CRC in $hash->{PARTIAL}: $crc vs. ". KM271_crc($data); Log 1, "Wrong CRC in $hash->{PARTIAL}: $crc vs. ". KM271_crc($data);
DevIo_SimpleWrite($hash, "15"); # NAK DevIo_SimpleWrite($hash, "15", 1); # NAK
DevIo_SimpleWrite($hash, "02") if(@{$hash->{SENDBUFFER}}); # want to send DevIo_SimpleWrite($hash, "02", 1) if(@{$hash->{SENDBUFFER}}); # want to send
return; return;
} }
DevIo_SimpleWrite($hash, "10"); # ACK, Data received ok DevIo_SimpleWrite($hash, "10", 1); # ACK, Data received ok
$data =~ s/1010/10/g; $data =~ s/1010/10/g;
@ -414,7 +414,7 @@ KM271_DoInit($)
{ {
my ($hash) = @_; my ($hash) = @_;
push @{$hash->{SENDBUFFER}}, "EE0000"; push @{$hash->{SENDBUFFER}}, "EE0000";
DevIo_SimpleWrite($hash, "02"); # STX DevIo_SimpleWrite($hash, "02", 1); # STX
return undef; return undef;
} }

View File

@ -113,7 +113,7 @@ TCM_Write($$$)
} }
Log $ll5, "$hash->{NAME} sending $bstring"; Log $ll5, "$hash->{NAME} sending $bstring";
DevIo_SimpleWrite($hash, $bstring); DevIo_SimpleWrite($hash, $bstring, 1);
} }
##################################### #####################################
@ -542,7 +542,7 @@ TCM_Set($@)
############################## ##############################
if($hash->{MODEL} eq "120") { if($hash->{MODEL} eq "120") {
if($cmdHex eq "") { # wake is very special if($cmdHex eq "") { # wake is very special
DevIo_SimpleWrite($hash, "AA"); DevIo_SimpleWrite($hash, "AA", 1);
return ""; return "";
} }

View File

@ -90,6 +90,10 @@ autocreate_Initialize($)
my %ahash = ( Fn=>"CommandCreateLog", my %ahash = ( Fn=>"CommandCreateLog",
Hlp=>"<device>,create log/weblink for <device>" ); Hlp=>"<device>,create log/weblink for <device>" );
$cmds{createlog} = \%ahash; $cmds{createlog} = \%ahash;
my %bhash = ( Fn=>"CommandUsb",
Hlp=>"[scan|create],display or create fhem-entries for USB devices" );
$cmds{usb} = \%bhash;
} }
##################################### #####################################
@ -275,4 +279,115 @@ CommandCreateLog($$)
$attr{$ac}{disable} = 1 if($disabled); $attr{$ac}{disable} = 1 if($disabled);
} }
##########################
# Table for automatically creating IO devices
my @usbtable = (
{ NAME => "CUL",
matchList => ["cu.usbmodem(.*)", "ttyACM(.*)"],
DeviceName=> "DEVICE\@9600",
flush => "\n",
request => "V\n",
response => "V .* CU.*",
define => "CUL_PARAM CUL DEVICE\@9600 1234", },
{ NAME => "TCM310",
matchList => ["cu.usbserial(.*)", "cu.usbmodem(.*)", "ttyUSB(.*)"],
DeviceName=> "DEVICE\@57600",
request => pack("H*", "5500010005700838"), # get idbase
response => "\x00\xFF....",
define => "TCM310_PARAM TCM 310 DEVICE\@57600", },
{ NAME => "TCM120",
matchList => ["ttyUSB(.*)"],
DeviceName=> "DEVICE\@9600",
request => pack("H*", "A55AAB5800000000000000000003"), # get idbase
response => "\xA5\x5A............",
define => "TCM120_PARAM TCM 120 DEVICE\@9600", },
{ NAME => "FHZ",
matchList => ["cu.usbserial(.*)", "ttyUSB(.*)"],
DeviceName=> "DEVICE\@9600",
request => pack("H*", "8105044fc90185"), # get fhtbuf
response => "\x81........",
define => "FHZ_PARAM FHZ DEVICE", },
);
sub
CommandUsb($$)
{
my ($cl, $n) = @_;
return "Usage: usb [scan|create]" if("$n" !~ m/^(scan|create)$/);
my $scan = 1 if($n =~ m/scan/);
my $ret = "";
my $msg;
my $dir = "/dev";
require "$attr{global}{modpath}/FHEM/DevIo.pm";
foreach my $dev (sort split("\n", `ls $dir`)) {
foreach my $thash (@usbtable) {
foreach my $ml (@{$thash->{matchList}}) {
if($dev =~ m/$ml/) {
my $PARAM = $1;
$PARAM =~ s/[^A-Za-z0-9]//g;
my $name = $thash->{NAME};
$msg = "$dev: checking if it is a $name";
Log 4, $msg; $ret .= $msg . "\n";
# Check if it already used
foreach my $d (keys %defs) {
if($defs{$d}{DeviceName} &&
$defs{$d}{DeviceName} =~ m/$dev/ &&
$defs{$d}{FD}) {
$msg = "$dev: already used by the $d fhem device";
Log 4, $msg; $ret .= $msg . "\n";
goto NEXTDEVICE;
}
}
# Open the device
my $dname = $thash->{DeviceName};
$dname =~ s,DEVICE,$dir/$dev,g;
my $hash = { NAME=>$name, DeviceName=>$dname };
DevIo_OpenDev($hash, 0, 0);
if(!defined($hash->{USBDev})) {
$msg = "$dev: Cannot open the device, check the log";
Log 4, $msg; $ret .= $msg . "\n";
goto NEXTDEVICE;
}
# Clear the USB buffer
if($thash->{flush}) {
DevIo_SimpleWrite($hash, $thash->{flush}, 0);
DevIo_TimeoutRead($hash, 0.1);
}
DevIo_SimpleWrite($hash, $thash->{request}, 0);
my $answer = DevIo_TimeoutRead($hash, 0.1);
DevIo_CloseDev($hash);
if($answer !~ m/$thash->{response}/) {
$msg = "$dev: got wrong answer for a $name";
Log 4, $msg; $ret .= $msg . "\n";
next;
}
my $define = $thash->{define};
$define =~ s/PARAM/$PARAM/g;
$define =~ s,DEVICE,$dir/$dev,g;
$msg = "$dev: create as a fhem device with: define $define";
Log 4, $msg; $ret .= $msg . "\n";
CommandDefine($cl, $define);
goto NEXTDEVICE;
}
}
}
NEXTDEVICE:
}
return ($scan ? $ret : undef);
}
1; 1;

View File

@ -3,7 +3,8 @@
package main; package main;
sub DevIo_SimpleRead($); sub DevIo_SimpleRead($);
sub DevIo_SimpleWrite($$); sub DevIo_TimeoutRead($$);
sub DevIo_SimpleWrite($$$);
sub DevIo_OpenDev($$$); sub DevIo_OpenDev($$$);
sub DevIo_CloseDev($); sub DevIo_CloseDev($);
sub DevIo_Disconnected($); sub DevIo_Disconnected($);
@ -50,20 +51,40 @@ DevIo_SimpleRead($)
return $buf; return $buf;
} }
########################
# Read until you get the timeout. Use it with care
sub
DevIo_TimeoutRead($$)
{
my ($hash, $timeout) = @_;
my $answer = "";
for(;;) {
my $rin = "";
vec($rin, $hash->{FD}, 1) = 1;
my $nfound = select($rin, undef, undef, $timeout);
last if($nfound <= 0);
my $r = DevIo_DoSimpleRead($hash);
last if(!defined($r));
$answer .= $r;
}
return $answer;
}
######################## ########################
# Input is HEX, with header and CRC # Input is HEX, with header and CRC
sub sub
DevIo_SimpleWrite($$) DevIo_SimpleWrite($$$)
{ {
my ($hash, $msg) = @_; my ($hash, $msg, $ishex) = @_;
return if(!$hash); return if(!$hash);
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $ll5 = GetLogLevel($name,5); my $ll5 = GetLogLevel($name,5);
Log $ll5, "SW: $msg"; Log $ll5, "SW: $msg";
$msg = pack('H*', $msg); $msg = pack('H*', $msg) if($ishex);
$hash->{USBDev}->write($msg) if($hash->{USBDev}); $hash->{USBDev}->write($msg) if($hash->{USBDev});
syswrite($hash->{TCPDev}, $msg) if($hash->{TCPDev}); syswrite($hash->{TCPDev}, $msg) if($hash->{TCPDev});
syswrite($hash->{DIODev}, $msg) if($hash->{DIODev}); syswrite($hash->{DIODev}, $msg) if($hash->{DIODev});
@ -82,7 +103,6 @@ DevIo_OpenDev($$$)
my $baudrate; my $baudrate;
($dev, $baudrate) = split("@", $dev); ($dev, $baudrate) = split("@", $dev);
$hash->{PARTIAL} = ""; $hash->{PARTIAL} = "";
Log 3, "Opening $name device $dev" Log 3, "Opening $name device $dev"
if(!$reopen); if(!$reopen);

View File

@ -13,8 +13,8 @@ RDOCDIR=$(ROOT)$(DOCDIR)
RMANDIR=$(ROOT)$(MANDIR) RMANDIR=$(ROOT)$(MANDIR)
RETCDIR=$(ROOT)$(ETCDIR) RETCDIR=$(ROOT)$(ETCDIR)
VERS=5.1 VERS=5.2
DATE=2011-07-08 DATE=2011-12-29
DESTDIR=fhem-$(VERS) DESTDIR=fhem-$(VERS)
all: all:

View File

@ -55,6 +55,7 @@
<a href="#sleep">sleep</a> &nbsp; <a href="#sleep">sleep</a> &nbsp;
<a href="#trigger">trigger</a> &nbsp; <a href="#trigger">trigger</a> &nbsp;
<a href="#updatefhem">updatefhem</a> &nbsp; <a href="#updatefhem">updatefhem</a> &nbsp;
<a href="#usb">usb</a> &nbsp;
<a href="#xmllist">xmllist</a> &nbsp; <a href="#xmllist">xmllist</a> &nbsp;
</ul> </ul>
@ -6979,16 +6980,36 @@ href="http://www.elv.de/output/controller.aspx?cid=74&detail=10&detail2=29870">U
</ul> </ul>
<br> <br>
<a name="createlog"></a> <a name="createlog"></a>
<b>createlog</b> <b>createlog</b>
<ul> <ul>
Use this command to manually add a FileLog and a weblink to an existing Use this command to manually add a FileLog and a weblink to an existing
device. E.g. if a HomeMatic device is created automatically by something device. E.g. if a HomeMatic device is created automatically by something
else then a pairing message, the model is unknown, so no plots will be else then a pairing message, the model is unknown, so no plots will be
generated. You can set the model/subtype attribute manually, and then call generated. You can set the model/subtype attribute manually, and then call
createlog to add the corresponding logs. </ul> <br> createlog to add the corresponding logs.<br><br>
This command is part of the autocreate module.
</ul>
<br>
<a name="usb"></a>
<b>usb</b>
<ul>
Usage:
<ul><code>
usb scan<br>
usb create<br>
</code></ul>
This command will scan the /dev directory for attached USB devices, and
will try to identify them. With the argument scan you'll get back a list
of fhem commands to execute, with the argument create there will be no
feedback, and the devices will be created instead.<br><br> This command
is part of the autocreate module.
</ul>
</ul> <!-- End of autocreate -->
<br>
</ul>
<a name="average"></a> <a name="average"></a>
<h3>average</h3> <h3>average</h3>