00_ZWDongle.pm 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. ##############################################
  2. # $Id: 00_ZWDongle.pm 16293 2018-02-28 21:33:57Z rudolfkoenig $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use Time::HiRes qw(gettimeofday);
  7. use ZWLib;
  8. use vars qw($FW_ME);
  9. sub ZWDongle_Parse($$$);
  10. sub ZWDongle_Read($@);
  11. sub ZWDongle_ReadAnswer($$$);
  12. sub ZWDongle_Ready($);
  13. sub ZWDongle_Write($$$);
  14. sub ZWDongle_ProcessSendStack($);
  15. sub ZWDongle_NUCheck($$$$);
  16. # See also:
  17. # http://www.digiwave.dk/en/programming/an-introduction-to-the-z-wave-protocol/
  18. # http://open-zwave.googlecode.com/svn-history/r426/trunk/cpp/src/Driver.cpp
  19. # http://buzzdavidson.com/?p=68
  20. # https://bitbucket.org/bradsjm/aeonzstickdriver
  21. my %sets = (
  22. "addNode" => { cmd => "4a%02x@", # ZW_ADD_NODE_TO_NETWORK
  23. param => { onNw =>0xc1, on =>0x81, off=>0x05,
  24. onNwSec=>0xc1, onSec=>0x81 } },
  25. "backupCreate" => { cmd => "" },
  26. "backupRestore" => { cmd => "" },
  27. "controllerChange" => { cmd => "4d%02x@", # ZW_CONTROLLER_CHANGE
  28. param => { on =>0x02, stop =>0x05,
  29. stopFailed =>0x06 } },
  30. "createNewPrimary" => { cmd => "4c%02x@", # ZW_CREATE_NEW_PRIMARY
  31. param => { on =>0x02, stop =>0x05,
  32. stopFailed =>0x06 } },
  33. "createNode" => { cmd => "60%02x" }, # ZW_REQUEST_NODE_INFO
  34. "createNodeSec" => { cmd => "60%02x" }, # ZW_REQUEST_NODE_INFO
  35. "factoryReset" => { cmd => "" }, # ZW_SET_DEFAULT
  36. "learnMode" => { cmd => "50%02x@", # ZW_SET_LEARN_MODE
  37. param => { onNw =>0x02, on =>0x01,
  38. disable=>0x00 } },
  39. "removeFailedNode" => { cmd => "61%02x@" }, # ZW_REMOVE_FAILED_NODE_ID
  40. "removeNode" => { cmd => "4b%02x@", # ZW_REMOVE_NODE_FROM_NETWORK
  41. param => {onNw=>0xc1, on=>0x81, off=>0x05 } },
  42. "reopen" => { cmd => "" },
  43. "replaceFailedNode"=> { cmd => "63%02x@" }, # ZW_REPLACE_FAILED_NODE
  44. "routeFor" => { cmd => "93%02x%02x%02x%02x%02x%02x" },
  45. # ZW_SET_PRIORITY_ROUTE
  46. "sendNIF" => { cmd => "12%02x05@" },# ZW_SEND_NODE_INFORMATION
  47. "setNIF" => { cmd => "03%02x%02x%02x%02x" },
  48. # SERIAL_API_APPL_NODE_INFORMATION
  49. "sucNodeId" => { cmd => "54%02x%02x00%02x@"},
  50. # ZW_SET_SUC_NODE_ID
  51. "sucRequestUpdate" => { cmd => "53%02x@"}, # ZW_REQUEST_NETWORK_UPDATE
  52. "sucSendNodeId" => { cmd => "57%02x25@"}, # ZW_SEND_SUC_ID
  53. "timeouts" => { cmd => "06%02x%02x" }, # SERIAL_API_SET_TIMEOUTS
  54. );
  55. my %gets = (
  56. "backgroundRSSI" => "3b", # GET_BACKGROUND_RSSI
  57. "caps" => "07", # SERIAL_API_GET_CAPABILITIES
  58. "ctrlCaps" => "05", # ZW_GET_CONTROLLER_CAPS
  59. "homeId" => "20", # MEMORY_GET_ID
  60. "isFailedNode" => "62%02x", # ZW_IS_FAILED_NODE
  61. "neighborList" => "80%02x", # GET_ROUTING_TABLE_LINE
  62. "nodeInfo" => "41%02x", # ZW_GET_NODE_PROTOCOL_INFO
  63. "nodeList" => "02", # SERIAL_API_GET_INIT_DATA
  64. "random" => "1c%02x", # ZW_GET_RANDOM
  65. "raw" => "%s", # hex
  66. "routeFor" => "92%02x", # hex
  67. "sucNodeId" => "56", # ZW_GET_SUC_NODE_ID
  68. # "timeouts" => "06", # Forum #71333
  69. "version" => "15", # ZW_GET_VERSION
  70. );
  71. sub
  72. ZWDongle_Initialize($)
  73. {
  74. my ($hash) = @_;
  75. require "$attr{global}{modpath}/FHEM/DevIo.pm";
  76. # Provider
  77. $hash->{ReadFn} = "ZWDongle_Read";
  78. $hash->{WriteFn} = "ZWDongle_Write";
  79. $hash->{ReadyFn} = "ZWDongle_Ready";
  80. $hash->{ReadAnswerFn} = "ZWDongle_ReadAnswer";
  81. # Normal devices
  82. $hash->{DefFn} = "ZWDongle_Define";
  83. $hash->{SetFn} = "ZWDongle_Set";
  84. $hash->{GetFn} = "ZWDongle_Get";
  85. $hash->{AttrFn} = "ZWDongle_Attr";
  86. $hash->{UndefFn} = "ZWDongle_Undef";
  87. no warnings 'qw';
  88. my @attrList = qw(
  89. do_not_notify:1,0
  90. dummy:1,0
  91. model:ZWDongle
  92. disable:0,1
  93. helpSites:multiple,pepper,alliance
  94. homeId
  95. networkKey
  96. neighborListPos
  97. neighborListFmt
  98. showSetInState:1,0
  99. );
  100. use warnings 'qw';
  101. $hash->{AttrList} = join(" ", @attrList);
  102. $hash->{FW_detailFn} = "ZWDongle_fhemwebFn";
  103. }
  104. #####################################
  105. sub
  106. ZWDongle_Define($$)
  107. {
  108. my ($hash, $def) = @_;
  109. my @a = split("[ \t][ \t]*", $def);
  110. if(@a != 3) {
  111. my $msg = "wrong syntax: define <name> ZWDongle {none[:homeId] | ".
  112. "devicename[\@baudrate] | ".
  113. "devicename\@directio | ".
  114. "hostname:port}";
  115. return $msg;
  116. }
  117. DevIo_CloseDev($hash);
  118. my $name = $a[0];
  119. my $dev = $a[2];
  120. $hash->{Clients} = ":ZWave:";
  121. my %matchList = ( "1:ZWave" => ".*" );
  122. $hash->{MatchList} = \%matchList;
  123. if($dev =~ m/none:(.*)/) {
  124. $hash->{homeId} = $1;
  125. Log3 $name, 1,
  126. "$name device is none (homeId:$1), commands will be echoed only";
  127. $attr{$name}{dummy} = 1;
  128. readingsSingleUpdate($hash, "state", "dummy", 1);
  129. return undef;
  130. } elsif($dev !~ m/@/ && $dev !~ m/:/) {
  131. $dev .= "\@115200"; # default baudrate
  132. }
  133. $hash->{DeviceName} = $dev;
  134. $hash->{CallbackNr} = 0;
  135. $hash->{nrNAck} = 0;
  136. my @empty;
  137. $hash->{SendStack} = \@empty;
  138. ZWDongle_shiftSendStack($hash, 0, 5, undef); # Init variables
  139. my $ret = DevIo_OpenDev($hash, 0, "ZWDongle_DoInit");
  140. return $ret;
  141. }
  142. #####################################
  143. sub
  144. ZWDongle_fhemwebFn($$$$)
  145. {
  146. my ($FW_wname, $d, $room, $pageHash) = @_; # pageHash is set for summaryFn.
  147. my $js = "$FW_ME/pgm2/zwave_neighborlist.js";
  148. return
  149. "<div id='ZWDongleNr'><a id='zw_snm' href='#'>Show neighbor map</a></div>".
  150. "<div id='ZWDongleNrSVG'></div>".
  151. "<script type='text/javascript' src='$js'></script>".
  152. '<script type="text/javascript">'.<<"JSEND"
  153. \$(document).ready(function() {
  154. \$("div#ZWDongleNr a#zw_snm")
  155. .click(function(e){
  156. e.preventDefault();
  157. zw_nl('ZWDongle_nlData("$d")');
  158. });
  159. });
  160. </script>
  161. JSEND
  162. }
  163. sub
  164. ZWDongle_nlData($)
  165. {
  166. my ($d) = @_;
  167. my @a = devspec2array("TYPE=ZWave,FILTER=IODev=$d");
  168. my (@dn, %nb, @ret);
  169. my $fmt = eval AttrVal($d, "neighborListFmt",
  170. '{ txt=>"NAME", img=>"IMAGE", title=>"Time to ack: timeToAck" }');
  171. for my $e (@a) {
  172. my $h = $defs{$e};
  173. next if($h->{ZWaveSubDevice} ne "no");
  174. $h->{IMAGE} = ZWave_getPic($d, ReadingsVal($e, "modelId", ""));
  175. my $nl = ReadingsVal($e, "neighborList", "");
  176. $nl = ReadingsVal($d, "neighborList_".hex($h->{nodeIdHex}), "")
  177. if(!$nl);
  178. $nl =~ s/,/ /g; $nl =~ s/\bempty\b//g;
  179. push @dn, $e if($nl =~ m/\b$d\b/);
  180. $nl = '"'.join('","',split(" ", $nl)).'"' if($nl);
  181. my %line = (
  182. pos => '['.AttrVal($e, "neighborListPos", "").']',
  183. class => '"zwBox col_link col_oddrow"',
  184. neighbors => '['.$nl.']'
  185. );
  186. my $r = $h->{READINGS};
  187. my $a = $attr{$e};
  188. for my $key (keys %{$fmt}) {
  189. my $val = $fmt->{$key};
  190. $val =~ s/\b(\w+)\b/{ $h->{$1} ? $h->{$1} :
  191. $r->{$1} ? $r->{$1}{VAL} :
  192. $a->{$1} ? $a->{$1} : $1 }/ge;
  193. $line{$key} = "\"$val\"" if($val ne $fmt->{$key}); # Skip unchanged
  194. }
  195. push @ret, "\"$e\":{". join(',',map({"\"$_\":$line{$_}" } keys %line)) ."}";
  196. $nb{$e} = $nl;
  197. }
  198. my $pos = AttrVal($d, "neighborListPos", "");
  199. my $nl = (@dn ? '"'.join('","',@dn).'"' : '');
  200. push @ret, "\"$d\":{\"txt\":\"$d\", \"pos\":[$pos],".
  201. "\"class\":\"zwDongle col_oddrow col_link\",\"neighbors\":[$nl] }";
  202. return "{ \"saveFn\":\"attr {1} neighborListPos {2}\",".
  203. "\"firstObj\":\"$d\",".
  204. "\"el\":{".join(",",@ret)."} }";
  205. }
  206. #####################################
  207. sub
  208. ZWDongle_Undef($$)
  209. {
  210. my ($hash,$arg) = @_;
  211. DevIo_CloseDev($hash);
  212. return undef;
  213. }
  214. #####################################
  215. sub
  216. ZWDongle_Set($@)
  217. {
  218. my ($hash, @a) = @_;
  219. my $name = shift @a;
  220. return "\"set ZWDongle\" needs at least one parameter" if(@a < 1);
  221. my $type = shift @a;
  222. if(!defined($sets{$type})) {
  223. my @r;
  224. map { my $p = $sets{$_}{param};
  225. push @r,($p ? "$_:".join(",",sort keys %{$p}) : $_)} sort keys %sets;
  226. return "Unknown argument $type, choose one of " . join(" ",@r);
  227. }
  228. Log3 $hash, 4, "ZWDongle *** set $name $type ".join(" ",@a);
  229. if($type eq "reopen") {
  230. return if(AttrVal($name, "dummy",undef) || AttrVal($name, "disable",undef));
  231. delete $hash->{NEXT_OPEN};
  232. DevIo_CloseDev($hash);
  233. sleep(1);
  234. DevIo_OpenDev($hash, 0, "ZWDongle_DoInit");
  235. return;
  236. }
  237. if($type eq "backupCreate") {
  238. my $caps = ReadingsVal($name, "caps","");
  239. my $is4 = ($caps =~ m/MEMORY_GET_BUFFER/);
  240. my $is5 = ($caps =~ m/NVM_EXT_READ_LONG_BUFFER/);
  241. return "Creating a backup is not supported by this device"
  242. if(!$is4 && !$is5);
  243. return "Usage: set $name backupCreate [16k|32k|64k|128k|256k]"
  244. if(int(@a) != 1 || $a[0] !~ m/^(16|32|64|128|256)k$/);
  245. my $fn = ($is5 ? "NVM_EXT_READ_LONG_BUFFER" : "MEMORY_GET_BUFFER");
  246. my $cmdFormat = ($is5 ? "002a%06x0040" : "0023%04x20");
  247. my $cmdRe = ($is5 ? "^012a" : "^0123");
  248. my $inc = ($is5 ? 64 : 32);
  249. my $l = $1 * 1024;
  250. my $fName = "$attr{global}{modpath}/$name.bin";
  251. open(OUT, ">$fName") || return "Cant open $fName: $!";
  252. binmode(OUT);
  253. for(my $off = 0; $off < $l;) {
  254. ZWDongle_Write($hash, "", sprintf($cmdFormat, $off));
  255. my ($err, $ret) = ZWDongle_ReadAnswer($hash, $fn, $cmdRe);
  256. return $err if($err);
  257. print OUT pack('H*', substr($ret, 4));
  258. $off += $inc;
  259. Log 3, "$name backupCreate at $off bytes" if($off % 16384 == 0);
  260. }
  261. close(OUT);
  262. return "Wrote $l bytes to $fName";
  263. }
  264. if($type eq "backupRestore") {
  265. my $caps = ReadingsVal($name, "caps","");
  266. my $is4 = ($caps =~ m/MEMORY_PUT_BUFFER/);
  267. my $is5 = ($caps =~ m/NVM_EXT_WRITE_LONG_BUFFER/);
  268. return "Restoring a backup is not supported by this device"
  269. if(!$is4 && !$is5);
  270. my $fn = ($is5 ? "NVM_EXT_WRITE_LONG_BUFFER" : "MEMORY_PUT_BUFFER");
  271. my $cmdFormat = ($is5 ? "002b%06x0040%s" : "0024%04x40%s");
  272. my $cmdRe = ($is5 ? "^012b" : "^0124");
  273. my $cmdRet = ($is5 ? "^012b01" : "^012401");
  274. my $inc = ($is5 ? 64 : 32);
  275. return "Usage: set $name backupRestore" if(int(@a) != 0);
  276. my $fName = "$attr{global}{modpath}/$name.bin";
  277. my $l = -s $fName;
  278. return "$fName does not exists, or is empty" if(!$l);
  279. open(IN, $fName) || return "Cant open $fName: $!";
  280. binmode(IN);
  281. my $buf;
  282. for(my $off = 0; $off < $l;) {
  283. if(sysread(IN, $buf, $inc) != $inc) {
  284. return "Cant read $inc bytes from $fName";
  285. }
  286. ZWDongle_Write($hash, "", sprintf($cmdFormat, $off, unpack('H*',$buf)));
  287. my ($err, $ret) = ZWDongle_ReadAnswer($hash, $fn, $cmdRe);
  288. return $err if($err);
  289. return "Unexpected $fn return value $ret"
  290. if($ret !~ m/$cmdRet/);
  291. $off += $inc;
  292. Log 3, "$name backupRestore at $off bytes" if($off % 16384 == 0);
  293. }
  294. close(IN);
  295. return "Restored $l bytes from $fName";
  296. }
  297. if($type eq "factoryReset") {
  298. return "Reset to default is not supported by this device"
  299. if(ReadingsVal($name, "caps","") !~ m/ZW_SET_DEFAULT/);
  300. return "Read commandref before use! -> Usage: set $name factoryReset yes"
  301. if(int(@a) != 1 || $a[0] !~ m/^(yes)$/);
  302. ZWDongle_Write($hash,"","0042");
  303. return "Reseted $name to factory default and assigned new random HomeId";
  304. }
  305. if($type eq "removeFailedNode" ||
  306. $type eq "replaceFailedNode" ||
  307. $type =~ m/^createNode/ ||
  308. $type eq "sendNIF") {
  309. $a[0] =~ s/^UNKNOWN_//;
  310. $a[0] = hex($defs{$a[0]}{nodeIdHex})
  311. if($defs{$a[0]} && $defs{$a[0]}{nodeIdHex});
  312. }
  313. my $cmd = $sets{$type}{cmd};
  314. my $fb = substr($cmd, 0, 2);
  315. if($fb =~ m/^[0-8A-F]+$/i &&
  316. ReadingsVal($name, "caps","") !~ m/\b$zw_func_id{$fb}\b/) {
  317. return "$type is unsupported by this controller";
  318. }
  319. delete($hash->{addSecure});
  320. $hash->{addSecure} = 1 if($type eq "createNodeSec");
  321. if($type eq "addNode") {
  322. $hash->{addSecure} = 1 if($a[0] && $a[0] =~ m/sec/i);
  323. if($a[0]) { # Remember the client for the failed message
  324. if($a[0] eq "off") {
  325. delete($hash->{addCL});
  326. } elsif($hash->{CL}) {
  327. $hash->{addCL} = $hash->{CL};
  328. }
  329. }
  330. }
  331. if($type eq "routeFor") {
  332. for(@a = @a) {
  333. $_ =~ s/^UNKNOWN_//;
  334. $_ = hex($defs{$_}{nodeIdHex})
  335. if($defs{$_} && $defs{$_}{nodeIdHex});
  336. return "$_ is neither a device nor a decimal id" if($_ !~ m/\d+/);
  337. }
  338. }
  339. my $par = $sets{$type}{param};
  340. if($par && !$par->{noArg}) {
  341. return "Unknown argument for $type, choose one of ".join(" ",keys %{$par})
  342. if(!$a[0] || !defined($par->{$a[0]}));
  343. $a[0] = $par->{$a[0]};
  344. }
  345. if($cmd =~ m/\@/) {
  346. my $c = $hash->{CallbackNr}+1;
  347. $c = 1 if($c > 255);
  348. $hash->{CallbackNr} = $c;
  349. $c = sprintf("%02x", $c);
  350. $cmd =~ s/\@/$c/g;
  351. }
  352. my @ca = split("%", $cmd, -1);
  353. my $nargs = int(@ca)-1;
  354. return "set $name $type needs $nargs arguments" if($nargs != int(@a));
  355. ZWDongle_Write($hash, "", "00".sprintf($cmd, @a));
  356. return undef;
  357. }
  358. #####################################
  359. sub
  360. ZWDongle_Get($@)
  361. {
  362. my ($hash, @a) = @_;
  363. my $name = shift @a;
  364. return "\"get $name\" needs at least one parameter" if(@a < 1);
  365. my $cmd = shift @a;
  366. return "Unknown argument $cmd, choose one of " .
  367. join(" ", map { $gets{$_} =~ m/%/ ? $_ : "$_:noArg" } sort keys %gets)
  368. if(!defined($gets{$cmd}));
  369. my $fb = substr($gets{$cmd}, 0, 2);
  370. if($fb =~ m/^[0-8A-F]+$/i && $cmd ne "caps" &&
  371. ReadingsVal($name, "caps","") !~ m/\b$zw_func_id{$fb}\b/) {
  372. return "$cmd is unsupported by this controller";
  373. }
  374. if($cmd eq "raw") {
  375. if($a[0] =~ s/^42//) {
  376. Log3 $hash, 4, "ZWDongle *** get $name $cmd 42".join(" ",@a)." blocked";
  377. return "raw 0x42 (ZW_SET_DEFAULT) blocked. Read commandref first and ".
  378. "use instead: set $name factoryReset";
  379. }
  380. }
  381. Log3 $hash, 4, "ZWDongle *** get $name $cmd ".join(" ",@a);
  382. if($cmd eq "neighborList") {
  383. my @b;
  384. @b = grep(!/onlyRep/i, @a); my $onlyRep = (@b != @a); @a = @b;
  385. @b = grep(!/excludeDead/i, @a); my $exclDead = (@b != @a); @a = @b;
  386. $gets{neighborList} = "80%02x".($exclDead ?"00":"01").($onlyRep ?"01":"00");
  387. return "Usage: get $name $cmd [excludeDead] [onlyRep] nodeId"
  388. if(int(@a) != 1);
  389. }
  390. my @ga = split("%", $gets{$cmd}, -1);
  391. my $nargs = int(@ga)-1;
  392. return "get $name $cmd needs $nargs arguments" if($nargs != int(@a));
  393. return "No $cmd for dummies" if(IsDummy($name));
  394. my $a0 = $a[0];
  395. if($cmd eq "neighborList" ||
  396. $cmd eq "nodeInfo" ||
  397. $cmd eq "routeFor" ||
  398. $cmd eq "isFailedNode") {
  399. $a[0] =~ s/^UNKNOWN_//;
  400. $a[0] = hex($defs{$a[0]}{nodeIdHex})
  401. if($defs{$a[0]} && $defs{$a[0]}{nodeIdHex});
  402. }
  403. my $out = sprintf($gets{$cmd}, @a);
  404. ZWDongle_Write($hash, "", "00".$out);
  405. my $re = "^01".substr($out,0,2); # Start with <01><len><01><CMD>
  406. my ($err, $ret) = ZWDongle_ReadAnswer($hash, $cmd, $re);
  407. return $err if($err);
  408. my $msg="";
  409. $a[0] = $a0 if(defined($a0));
  410. $msg = $ret if($ret);
  411. my @r = map { ord($_) } split("", pack('H*', $ret)) if(defined($ret));
  412. if($cmd eq "nodeList") { ############################
  413. $msg =~ s/^.{10}(.{58}).*/$1/;
  414. $msg = zwlib_parseNeighborList($hash, $msg);
  415. } elsif($cmd eq "caps") { ############################
  416. $msg = sprintf("Vers:%d Rev:%d ", $r[2], $r[3]);
  417. $msg .= sprintf("ManufID:%02x%02x ", $r[4], $r[5]);
  418. $msg .= sprintf("ProductType:%02x%02x ", $r[6], $r[7]);
  419. $msg .= sprintf("ProductID:%02x%02x", $r[8], $r[9]);
  420. my @list;
  421. for my $byte (0..31) {
  422. my $bits = $r[10+$byte];
  423. for my $bit (0..7) {
  424. my $id = sprintf("%02x", $byte*8+$bit+1);
  425. push @list, ($zw_func_id{$id} ? $zw_func_id{$id} : "UNKNOWN_$id")
  426. if($bits & (1<<$bit));
  427. }
  428. }
  429. $msg .= " ".join(" ",@list);
  430. } elsif($cmd eq "homeId") { ############################
  431. $msg = sprintf("HomeId:%s CtrlNodeIdHex:%s",
  432. substr($ret,4,8), substr($ret,12,2));
  433. $hash->{homeId} = substr($ret,4,8);
  434. $hash->{nodeIdHex} = substr($ret,12,2);
  435. $attr{$name}{homeId} = substr($ret,4,8);
  436. } elsif($cmd eq "version") { ############################
  437. $msg = join("", map { chr($_) } @r[2..13]);
  438. my @type = qw( STATIC_CONTROLLER CONTROLLER ENHANCED_SLAVE
  439. SLAVE INSTALLER NO_INTELLIGENT_LIFE BRIDGE_CONTROLLER);
  440. my $idx = $r[14]-1;
  441. $msg .= " $type[$idx]" if($idx >= 0 && $idx <= $#type);
  442. } elsif($cmd eq "ctrlCaps") { ############################
  443. my @type = qw(SECONDARY OTHER MEMBER PRIMARY SUC);
  444. my @list;
  445. for my $bit (0..7) {
  446. push @list, $type[$bit] if(($r[2] & (1<<$bit)) && $bit < @type);
  447. }
  448. $msg = join(" ", @list);
  449. } elsif($cmd eq "nodeInfo") { ############################
  450. if($r[6] == 0) {
  451. $msg = "node $a[0] is not present";
  452. } else {
  453. $msg = zwlib_parseNodeInfo(@r);
  454. }
  455. } elsif($cmd eq "random") { ############################
  456. return "$name: Cannot generate" if($ret !~ m/^011c01(..)(.*)$/);
  457. $msg = $2; @a = ();
  458. } elsif($cmd eq "isFailedNode") { ############################
  459. $msg = ($r[2]==1)?"yes":"no";
  460. } elsif($cmd eq "neighborList") { ############################
  461. $msg =~ s/^....//;
  462. $msg = zwlib_parseNeighborList($hash, $msg);
  463. } elsif($cmd eq "sucNodeId") { ############################
  464. $msg = ($r[2]==0)?"no":$r[2];
  465. } elsif($cmd eq "routeFor") { ############################
  466. my $homeId = $hash->{homeId};
  467. my @list;
  468. my $e = hex(substr($msg, 6, 2));
  469. push @list, ($e==1 ? "last": ($e==2 ? "next":"application"))
  470. if($e !=0);
  471. for(my $off=8; $off<16; $off+=2) {
  472. my $dec = hex(substr($msg, $off, 2));
  473. my $hex = sprintf("%02x", $dec);
  474. my $h = ($hex eq $hash->{nodeIdHex} ?
  475. $hash : $modules{ZWave}{defptr}{"$homeId $hex"});
  476. push @list, ($h ? $h->{NAME} : "UNKNOWN_$dec") if($dec);
  477. }
  478. my $f = substr($msg, 17, 1);
  479. push @list, ("at ".($f==1 ? "9.6": ($f==2 ? "40":"100"))."kbps")
  480. if(@list && $f =~ m/[123]/);
  481. $msg = (@list ? join(" ", @list) : "N/A");
  482. } elsif($cmd eq "backgroundRSSI") { ############################
  483. my @list;
  484. my $i=0;
  485. my $maxlen = (length($msg) >= 10 ? 10 : length($msg));
  486. for(my $off=4; $off<$maxlen; $off+=2) {
  487. my $dec = hex(substr($msg, $off, 2));
  488. if($dec == 127) {
  489. push @list, ("ch".($i+1).":N/A");
  490. } elsif($dec == 126) {
  491. push @list, ("ch".($i+1).":aboveMaxPower");
  492. } elsif($dec == 125) {
  493. push @list, ("ch".($i+1).":belowReceiverSensitivity");
  494. } elsif($dec > 161 && $dec < 225) {
  495. push @list, ("ch".($i+1).":".unpack('c', pack('C', $dec))." dBm");
  496. } else {
  497. push @list, ("ch".($i+1).":reservedValue");
  498. }
  499. $i++
  500. }
  501. $msg = join(" ", @list);
  502. }
  503. $cmd .= "_".join("_", @a) if(@a);
  504. readingsSingleUpdate($hash, $cmd, $msg, 0);
  505. return "$name $cmd => $msg";
  506. }
  507. #####################################
  508. sub
  509. ZWDongle_Clear($)
  510. {
  511. my $hash = shift;
  512. # Clear the pipe
  513. for(;;) {
  514. my ($err, undef) = ZWDongle_ReadAnswer($hash, "Clear", "wontmatch");
  515. last if($err && ($err =~ m/^Timeout/ || $err =~ m/No FD/));
  516. }
  517. $hash->{PARTIAL} = "";
  518. }
  519. #####################################
  520. sub
  521. ZWDongle_DoInit($)
  522. {
  523. my $hash = shift;
  524. my $name = $hash->{NAME};
  525. DevIo_SetHwHandshake($hash) if($hash->{USBDev});
  526. $hash->{PARTIAL} = "";
  527. ZWDongle_Clear($hash);
  528. ZWDongle_Get($hash, $name, "caps");
  529. ZWDongle_Get($hash, $name, "ctrlCaps");
  530. ZWDongle_Get($hash, $name, "homeId");
  531. ZWDongle_Get($hash, $name, "sucNodeId");
  532. ZWDongle_Get($hash, $name, ("random", 32)); # Sec relevant
  533. ZWDongle_Set($hash, $name, ("timeouts", 100, 15)); # Sec relevant
  534. ZWDongle_ReadAnswer($hash, "timeouts", "^0106");
  535. # NODEINFO_LISTENING, Generic Static controller, Specific Static Controller, 0
  536. ZWDongle_Set($hash, $name, ("setNIF", 1, 2, 1, 0)); # Sec relevant (?)
  537. readingsSingleUpdate($hash, "state", "Initialized", 1);
  538. return undef;
  539. }
  540. #####################################
  541. # neighborUpdate special: have to serialize. Forum #54574
  542. my @nuStack;
  543. sub
  544. ZWDongle_NUCheck($$$$)
  545. {
  546. my($hash, $fn, $msg, $isWrite) = @_;
  547. if($isWrite) {
  548. return 0 if($msg !~ m/^0048/ || $hash->{calledFromNuCheck});
  549. push @nuStack, "$fn/$msg";
  550. if(@nuStack == 1) {
  551. InternalTimer(gettimeofday+20, sub { # ZME timeout is 9-11s
  552. ZWDongle_NUCheck($hash, undef, "0048xx23", 0); # simulate fail
  553. }, \@nuStack, 0);
  554. }
  555. return (@nuStack > 1);
  556. } else {
  557. return if($msg !~ m/^0048..(..)$/ || $1 eq "21"); # 21: started
  558. shift @nuStack;
  559. RemoveInternalTimer(\@nuStack);
  560. return if(@nuStack == 0);
  561. my @a = split("/", $nuStack[0]);
  562. $hash->{calledFromNuCheck} = 1;
  563. ZWDongle_Write($hash, $a[0], $a[1]);
  564. delete($hash->{calledFromNuCheck});
  565. InternalTimer(gettimeofday+20, sub {
  566. ZWDongle_NUCheck($hash, undef, "0048xx23", 0); # simulate fail
  567. }, \@nuStack, 0);
  568. }
  569. }
  570. #####################################
  571. sub
  572. ZWDongle_Write($$$)
  573. {
  574. my ($hash,$fn,$msg) = @_;
  575. return if(ZWDongle_NUCheck($hash, $fn, $msg, 1));
  576. Log3 $hash, 5, "ZWDongle_Write $msg ($fn)";
  577. # assemble complete message
  578. $msg = sprintf("%02x%s", length($msg)/2+1, $msg);
  579. $msg = "01$msg" . zwlib_checkSum_8($msg);
  580. push @{$hash->{SendStack}}, $msg;
  581. ZWDongle_ProcessSendStack($hash);
  582. }
  583. # Flags:
  584. # - WaitForAck: 0:Written, 1:SerialACK received, 2:RF-Sent
  585. # - SendRetries < MaxSendRetries(3, up to 7 when receiving CAN)
  586. sub
  587. ZWDongle_shiftSendStack($$$$;$)
  588. {
  589. my ($hash, $reason, $loglevel, $txt, $cbId) = @_;
  590. my $ss = $hash->{SendStack};
  591. my $cmd = $ss->[0];
  592. if($cmd && $reason==0 && $cmd =~ m/^01..0013/) { # ACK for SEND_DATA
  593. Log3 $hash, $loglevel, "$txt, WaitForAck=>2 for $cmd"
  594. if($txt);
  595. $hash->{WaitForAck}=2;
  596. } else {
  597. return if($cbId && $cmd && $cbId ne substr($cmd,-4,2));
  598. shift @{$ss};
  599. Log3 $hash, $loglevel, "$txt, removing $cmd from dongle sendstack"
  600. if($txt && $cmd);
  601. $hash->{WaitForAck}=0;
  602. $hash->{SendRetries}=0;
  603. $hash->{MaxSendRetries}=3;
  604. delete($hash->{GotCAN});
  605. }
  606. }
  607. sub
  608. ZWDongle_ProcessSendStack($)
  609. {
  610. my ($hash) = @_;
  611. #Log3 $hash, 1, "ZWDongle_ProcessSendStack: ".@{$hash->{SendStack}}.
  612. # " items on stack, waitForAck ".$hash->{WaitForAck};
  613. RemoveInternalTimer($hash);
  614. my $ts = gettimeofday();
  615. if($hash->{WaitForAck}){
  616. if($hash->{WaitForAck} == 1 && $ts-$hash->{SendTime} >= 1) {
  617. Log3 $hash, 2, "ZWDongle_ProcessSendStack: no ACK, resending message ".
  618. $hash->{SendStack}->[0];
  619. $hash->{SendRetries}++;
  620. $hash->{WaitForAck} = 0;
  621. } elsif($hash->{WaitForAck} == 2 && $ts-$hash->{SendTime} >= 2) {
  622. ZWDongle_shiftSendStack($hash, 1, 4, "no response from device");
  623. } else {
  624. InternalTimer($ts+1, "ZWDongle_ProcessSendStack", $hash, 0);
  625. return;
  626. }
  627. }
  628. if($hash->{SendRetries} > $hash->{MaxSendRetries}){
  629. ZWDongle_shiftSendStack($hash, 1, 1, "ERROR: max send retries reached");
  630. }
  631. return if(!@{$hash->{SendStack}} ||
  632. $hash->{WaitForAck} ||
  633. !DevIo_IsOpen($hash));
  634. my $msg = $hash->{SendStack}->[0];
  635. DevIo_SimpleWrite($hash, $msg, 1);
  636. $hash->{WaitForAck} = 1;
  637. $hash->{SendTime} = $ts;
  638. delete($hash->{GotCAN});
  639. InternalTimer($ts+1, "ZWDongle_ProcessSendStack", $hash, 0);
  640. }
  641. #####################################
  642. # called from the global loop, when the select for hash->{FD} reports data
  643. sub
  644. ZWDongle_Read($@)
  645. {
  646. my ($hash, $local, $regexp) = @_;
  647. my $buf = (defined($local) ? $local : DevIo_SimpleRead($hash));
  648. return "" if(!defined($buf));
  649. my $name = $hash->{NAME};
  650. $buf = unpack('H*', $buf);
  651. # The dongle looses data over USB for some commands(?), and dropping the old
  652. # buffer after a timeout is my only idea of solving this problem.
  653. my $ts = gettimeofday();
  654. my $data = ($hash->{ReadTime} && $ts-$hash->{ReadTime} > 1) ?
  655. $buf : $hash->{PARTIAL}.$buf;
  656. $hash->{ReadTime} = $ts;
  657. #Log3 $name, 5, "ZWDongle RAW buffer: $data";
  658. my $msg;
  659. while(length($data) >= 2) {
  660. my $fb = substr($data, 0, 2);
  661. if($fb eq "06") { # ACK
  662. ZWDongle_shiftSendStack($hash, 0, 5, "ACK received");
  663. $data = substr($data, 2);
  664. next;
  665. }
  666. if($fb eq "15") { # NACK
  667. Log3 $name, 4, "ZWDongle_Read $name: NACK received";
  668. $hash->{WaitForAck} = 0;
  669. $hash->{SendRetries}++;
  670. $data = substr($data, 2);
  671. next;
  672. }
  673. if($fb eq "18") { # CAN
  674. Log3 $name, 4, "ZWDongle_Read $name: CAN received";
  675. $hash->{MaxSendRetries}++ if($hash->{MaxSendRetries}<7);
  676. $data = substr($data, 2);
  677. $hash->{GotCAN} = 1;
  678. if(!$init_done) { # InternalTimer wont work
  679. $hash->{WaitForAck} = 0;
  680. $hash->{SendRetries}++;
  681. select(undef, undef, undef, 0.1);
  682. }
  683. next;
  684. }
  685. if($fb ne "01") { # SOF
  686. Log3 $name, 1, "$name: SOF missing (got $fb instead of 01)";
  687. if(++$hash->{nrNAck} < 5){
  688. Log3 $name, 5, "ZWDongle_Read SOF Error -> sending NACK";
  689. DevIo_SimpleWrite($hash, "15", 1); # Send NACK
  690. }
  691. $data="";
  692. last;
  693. }
  694. last if(length($data) < 4);
  695. my $len = substr($data, 2, 2);
  696. my $l = hex($len)*2;
  697. last if(length($data) < $l+4); # Message not yet complete
  698. if($l < 4) { # Bogus messages, forget the rest
  699. $data = "";
  700. last;
  701. }
  702. $msg = substr($data, 4, $l-2);
  703. my $rcs = substr($data, $l+2, 2); # Received Checksum
  704. $data = substr($data, $l+4);
  705. my $ccs = zwlib_checkSum_8("$len$msg"); # Computed Checksum
  706. if($rcs ne $ccs) {
  707. Log3 $name, 1,
  708. "$name: wrong checksum: received $rcs, computed $ccs for $len$msg";
  709. if(++$hash->{nrNAck} < 5) {
  710. Log3 $name, 5, "ZWDongle_Read wrong checksum -> sending NACK";
  711. DevIo_SimpleWrite($hash, "15", 1);
  712. }
  713. $msg = undef;
  714. $data="";
  715. next;
  716. }
  717. $hash->{nrNAck} = 0;
  718. next if($msg !~ m/^(..)(..)/);
  719. my $ztp = ($1 eq "00" ? "request" : ($1 eq "01" ? "answer" : "unknown $1"));
  720. my $zfi = $zw_func_id{$2} ? $zw_func_id{$2} : "unknown $2";
  721. Log3 $name, 4, "ZWDongle_Read $name: rcvd $msg ($ztp $zfi), sending ACK";
  722. DevIo_SimpleWrite($hash, "06", 1);
  723. ZWDongle_shiftSendStack($hash, 1, 5, "device ack reveived", $1)
  724. if($msg =~ m/^0013(..)/);
  725. last if(defined($local) && (!defined($regexp) || ($msg =~ m/$regexp/)));
  726. $hash->{PARTIAL} = $data; # Recursive call by ZWave get, Forum #37418
  727. ZWDongle_Parse($hash, $name, $msg) if($init_done);
  728. $data = $hash->{PARTIAL};
  729. $msg = undef;
  730. }
  731. $hash->{PARTIAL} = $data;
  732. # trigger sending of next message
  733. ZWDongle_ProcessSendStack($hash) if(length($data) == 0);
  734. return $msg if(defined($local));
  735. return undef;
  736. }
  737. #####################################
  738. # This is a direct read for commands like get
  739. sub
  740. ZWDongle_ReadAnswer($$$)
  741. {
  742. my ($hash, $arg, $regexp) = @_;
  743. Log3 $hash, 4, "ZWDongle_ReadAnswer arg:$arg regexp:".($regexp ? $regexp:"");
  744. return ("No FD (dummy device?)", undef)
  745. if(!$hash || ($^O !~ /Win/ && !defined($hash->{FD})));
  746. my $to = ($hash->{RA_Timeout} ? $hash->{RA_Timeout} : 1);
  747. for(;;) {
  748. my $buf;
  749. if($^O =~ m/Win/ && $hash->{USBDev}) {
  750. $hash->{USBDev}->read_const_time($to*1000); # set timeout (ms)
  751. # Read anstatt input sonst funzt read_const_time nicht.
  752. $buf = $hash->{USBDev}->read(999);
  753. if(length($buf) == 0) {
  754. if($hash->{GotCAN}) {
  755. ZWDongle_ProcessSendStack($hash);
  756. next;
  757. }
  758. return ("Timeout reading answer for get $arg", undef);
  759. }
  760. } else {
  761. if(!$hash->{FD}) {
  762. Log3 $hash, 1, "ZWDongle_ReadAnswer: device lost";
  763. return ("Device lost when reading answer for get $arg", undef);
  764. }
  765. my $rin = '';
  766. vec($rin, $hash->{FD}, 1) = 1;
  767. my $nfound = select($rin, undef, undef, $to);
  768. if($nfound < 0) {
  769. my $err = $!;
  770. Log3 $hash, 5, "ZWDongle_ReadAnswer: nfound < 0 / err:$err";
  771. next if ($err == EAGAIN() || $err == EINTR() || $err == 0);
  772. DevIo_Disconnected($hash);
  773. return("ZWDongle_ReadAnswer $arg: $err", undef);
  774. }
  775. if($nfound == 0){
  776. Log3 $hash, 5, "ZWDongle_ReadAnswer: select timeout";
  777. if($hash->{GotCAN}) {
  778. ZWDongle_ProcessSendStack($hash);
  779. next;
  780. }
  781. return ("Timeout reading answer for get $arg", undef);
  782. }
  783. $buf = DevIo_SimpleRead($hash);
  784. if(!defined($buf)){
  785. Log3 $hash, 1,"ZWDongle_ReadAnswer: no data read";
  786. return ("No data", undef);
  787. }
  788. }
  789. my $ret = ZWDongle_Read($hash, $buf, $regexp);
  790. if(defined($ret)){
  791. Log3 $hash, 4, "ZWDongle_ReadAnswer for $arg: $ret";
  792. return (undef, $ret);
  793. }
  794. }
  795. }
  796. sub
  797. ZWDongle_Parse($$$)
  798. {
  799. my ($hash, $name, $rmsg) = @_;
  800. if(!defined($hash->{STATE}) ||
  801. ReadingsVal($name, "state", "") ne "Initialized"){
  802. Log3 $hash, 4,"ZWDongle_Parse $rmsg: dongle not yet initialized";
  803. return;
  804. }
  805. $hash->{"${name}_MSGCNT"}++;
  806. $hash->{"${name}_TIME"} = TimeNow();
  807. $hash->{RAWMSG} = $rmsg;
  808. $hash->{SendTime}-- # Retry sending after a "real" msg from the dongle
  809. if($hash->{GotCAN} && $rmsg !~ m/^(0113|0013)/);
  810. my %addvals = (RAWMSG => $rmsg);
  811. ZWDongle_NUCheck($hash, undef, $rmsg, 0);
  812. Dispatch($hash, $rmsg, \%addvals);
  813. }
  814. #####################################
  815. sub
  816. ZWDongle_Attr($$$$)
  817. {
  818. my ($cmd, $name, $attr, $value) = @_;
  819. my $hash = $defs{$name};
  820. $attr = "" if(!$attr);
  821. if($attr eq "disable") {
  822. if($cmd eq "set" && ($value || !defined($value))) {
  823. DevIo_CloseDev($hash) if(!AttrVal($name,"dummy",undef));
  824. readingsSingleUpdate($hash, "state", "disabled", 1);
  825. } else {
  826. if(AttrVal($name,"dummy",undef)) {
  827. readingsSingleUpdate($hash, "state", "dummy", 1);
  828. return;
  829. }
  830. DevIo_OpenDev($hash, 0, "ZWDongle_DoInit");
  831. }
  832. } elsif($attr eq "homeId" && $cmd eq "set") {
  833. $hash->{homeId} = $value;
  834. } elsif($attr eq "networkKey" && $cmd eq "set") {
  835. if(!$value || $value !~ m/^[0-9A-F]{32}$/i) {
  836. return "attr $name networkKey: not a hex string with a length of 32";
  837. }
  838. return;
  839. } elsif($attr eq "showSetInState") {
  840. $hash->{showSetInState} = ($cmd eq "set" ? (defined($value) ? $value:1) :0);
  841. }
  842. return undef;
  843. }
  844. #####################################
  845. sub
  846. ZWDongle_Ready($)
  847. {
  848. my ($hash) = @_;
  849. return undef if (IsDisabled($hash->{NAME}));
  850. return DevIo_OpenDev($hash, 1, "ZWDongle_DoInit")
  851. if(ReadingsVal($hash->{NAME}, "state","") eq "disconnected");
  852. # This is relevant for windows/USB only
  853. my $po = $hash->{USBDev};
  854. if($po) {
  855. my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status;
  856. if(!defined($InBytes)) {
  857. DevIo_Disconnected($hash);
  858. return 0;
  859. }
  860. return ($InBytes>0);
  861. }
  862. return 0;
  863. }
  864. 1;
  865. =pod
  866. =item summary connection to standard ZWave controller
  867. =item summary_DE Anbindung von standard ZWave Controller
  868. =begin html
  869. <a name="ZWDongle"></a>
  870. <h3>ZWDongle</h3>
  871. <ul>
  872. This module serves a ZWave dongle, which is attached via USB or TCP/IP, and
  873. enables the use of ZWave devices (see also the <a href="#ZWave">ZWave</a>
  874. module). It was tested wit a Goodway WD6001, but since the protocol is
  875. standardized, it should work with other devices too. A notable exception is
  876. the USB device from Merten.
  877. <br><br>
  878. <a name="ZWDongledefine"></a>
  879. <b>Define</b>
  880. <ul>
  881. <code>define &lt;name&gt; ZWDongle &lt;device&gt;</code>
  882. <br>
  883. <br>
  884. Upon initial connection the module will get the homeId of the attached
  885. device. Since the DevIo module is used to open the device, you can also use
  886. devices connected via TCP/IP. See <a href="#CULdefine">this</a> paragraph on
  887. device naming details.
  888. <br>
  889. Example:
  890. <ul>
  891. <code>define zwdongle_1 ZWDongle /dev/cu.PL2303-000014FA@115200</code><br>
  892. </ul>
  893. </ul>
  894. <br>
  895. <a name="ZWDongleset"></a>
  896. <b>Set</b>
  897. <ul>
  898. <li>addNode on|onNw|onSec|onNwSec|off<br>
  899. Activate (or deactivate) inclusion mode. The controller (i.e. the dongle)
  900. will accept inclusion (i.e. pairing/learning) requests only while in this
  901. mode. After activating inclusion mode usually you have to press a switch
  902. three times within 1.5 seconds on the node to be included into the network
  903. of the controller. If autocreate is active, a fhem device will be created
  904. after inclusion. "on" activates standard inclusion. "onNw" activates network
  905. wide inclusion (only SDK 4.5-4.9, SDK 6.x and above).<br>
  906. If onSec/onNwSec is specified, the ZWDongle networkKey ist set, and the
  907. device supports the SECURITY class, then a secure inclusion is attempted.
  908. </li>
  909. <li>backupCreate 16k|32k|64k|128k|256k<br>
  910. read out the NVRAM of the ZWDongle, and store it in a file called
  911. &lt;ZWDongle_Name&gt;.bin in the modpath folder. Since the size of the
  912. NVRAM is currently unknown to FHEM, you have to specify the size. The
  913. ZWave.me ZME_UZB1 Stick seems to have 256k of NVRAM. Note: writing the file
  914. takes some time, usually about 10s for each 64k (and significantly longer
  915. on Windows), and FHEM is blocked during this time.
  916. </li>
  917. <li>backupRestore<br>
  918. Restore the file created by backupCreate. Restoring the file takes about
  919. the same time as saving it, and FHEM is blocked during this time.
  920. Note / Important: this function is not yet tested for older devices using
  921. the MEMORY functions.
  922. </li>
  923. <li>controllerChange on|stop|stopFailed<br>
  924. Add a controller to the current network and transfer role as primary to it.
  925. Invoking controller is converted to secondary.<br>
  926. stop: stop controllerChange<br>
  927. stopFailed: stop controllerChange and report an error
  928. </li>
  929. <li>createNewPrimary on|stop|stopFailed<br>
  930. Add a controller to the current network as a replacement for an old
  931. primary. Command can be invoked only by a secondary configured as basic
  932. SUC<br>
  933. stop: stop createNewPrimary<br>
  934. stopFailed: stop createNewPrimary and report an error
  935. </li>
  936. <li>createNode &lt;device&gt;<br>
  937. createNodeSec &lt;device&gt;<br>
  938. Request the class information for the specified node, and create
  939. a FHEM device upon reception of the answer. Used to create FHEM devices for
  940. nodes included with another software or if the fhem.cfg got lost. For the
  941. node id see the get nodeList command below. Note: the node must be "alive",
  942. i.e. for battery based devices you have to press the "wakeup" button 1-2
  943. seconds before entering this command in FHEM.<br>
  944. &lt;device&gt; is either device name or decimal nodeId.<br>
  945. createNodeSec assumes a secure inclusion, see the comments for "addNode
  946. onSec" for details.
  947. </li>
  948. <li>factoryReset yes<br>
  949. Reset controller to default state.
  950. Erase all node and routing infos, assign a new random homeId.
  951. To control a device it must be re-included and re-configured.<br>
  952. !Use this with care AND only if You know what You do!<br>
  953. Note: the corresponding FHEM devices have to be deleted manually.
  954. </li>
  955. <li>learnMode on|onNw|disable<br>
  956. Add or remove controller to/from an other network.
  957. Assign a homeId, nodeId and receive/store nodeList and routing infos.
  958. </li>
  959. <li>removeFailedNode &lt;device&gt;<br>
  960. Remove non-responding node -that must be on the failed node list-
  961. from the routing table in controller. Instead, always use removeNode if
  962. possible. Note: the corresponding FHEM device have to be deleted
  963. manually.<br>
  964. &lt;device&gt; is either device name or decimal nodeId.
  965. </li>
  966. <li>removeNode onNw|on|off<br>
  967. Activate (or deactivate) exclusion mode. "on" activates standard exclusion.
  968. "onNw" activates network wide exclusion (only SDK 4.5-4.9, SDK 6.x and
  969. above). Note: the corresponding FHEM device have to be deleted
  970. manually.
  971. </li>
  972. <li>reopen<br>
  973. First close and then open the device. Used for debugging purposes.
  974. </li>
  975. <li>replaceFailedNode &lt;device&gt;<br>
  976. Replace a non-responding node with a new one. The non-responding node
  977. must be on the failed node list.<br>
  978. &lt;device&gt; is either device name or decimal nodeId.
  979. </li>
  980. <li>routeFor &lt;device&gt; &lt;hop1&gt; &lt;hop2&gt; &lt;hop3&gt;
  981. &lt;hop4&gt; &lt;speed&gt;<br>
  982. set priority routing for &lt;device&gt;. &lt;device&gt; and &lt;hopN&gt are
  983. either device name or decimal nodeId or 0 for unused.<br>
  984. &lt;speed&gt;: 1=9,6kbps; 2=40kbps; 3=100kbps
  985. </li>
  986. <li>sendNIF &lt;device&gt;<br>
  987. Send NIF to the specified &lt;device&gt;.
  988. &lt;device&gt; is either device name or decimal nodeId.
  989. </li>
  990. <li>sucNodeId &lt;decimal nodeId&gt; &lt;sucState&gt;
  991. &lt;capabilities&gt;<br>
  992. Configure a controller node to be a SUC/SIS or not.<br>
  993. &lt;nodeId&gt;: decimal nodeId to be SUC/SIS<br>
  994. &lt;sucState&gt;: 0 = deactivate; 1 = activate<br>
  995. &lt;capabilities&gt;: 0 = basic SUC; 1 = SIS
  996. </li>
  997. <li>sucRequestUpdate &lt;decimal nodeId of SUC/SIS&gt;<br>
  998. Request network updates from SUC/SIS. Primary do not need it.
  999. </li>
  1000. <li>sucSendNodeId &lt;decimal nodeId&gt;<br>
  1001. Send SUC/SIS nodeId to the specified decimal controller nodeId.
  1002. </li>
  1003. </ul>
  1004. <br>
  1005. <a name="ZWDongleget"></a>
  1006. <b>Get</b>
  1007. <ul>
  1008. <li>homeId<br>
  1009. return the six hex-digit homeId of the controller.
  1010. </li>
  1011. <li>backgroundRSSI<br>
  1012. query the measured RSSI on the Z-Wave network
  1013. </li>
  1014. <li>caps, ctrlCaps, version<br>
  1015. return different controller specific information. Needed by developers
  1016. only.
  1017. </li>
  1018. <li>isFailedNode &lt;device&gt;<br>
  1019. return if a node is stored in the failed node list. &lt;device&gt; is
  1020. either device name or decimal nodeId.
  1021. </li>
  1022. <li>neighborList [excludeDead] [onlyRep] &lt;device&gt;<br>
  1023. return neighborList of the &lt;device&gt;.<br>
  1024. &lt;device&gt; is either device name or decimal nodeId.<br>
  1025. With onlyRep the result will include only nodes with repeater
  1026. functionality.
  1027. </li>
  1028. <li>nodeInfo &lt;device&gt;<br>
  1029. return node specific information. &lt;device&gt; is either device name or
  1030. decimal nodeId.
  1031. </li>
  1032. <li>nodeList<br>
  1033. return the list of included nodenames or UNKNOWN_id (decimal id), if there
  1034. is no corresponding device in FHEM. Can be used to recreate FHEM-nodes with
  1035. the createNode command.
  1036. </li>
  1037. <li>random &lt;N&gt;<br>
  1038. request &lt;N&gt; random bytes from the controller.
  1039. </li>
  1040. <li>raw &lt;hex&gt;<br>
  1041. Send raw data &lt;hex&gt; to the controller. Developer only.
  1042. </li>
  1043. <li>routeFor &lt;device&gt;<br>
  1044. request priority routing for &lt;device&gt;. &lt;device&gt; is either
  1045. device name or decimal nodeId.</li>
  1046. <li>sucNodeId<br>
  1047. return the currently registered decimal SUC nodeId.
  1048. </li>
  1049. </ul>
  1050. <br>
  1051. <a name="ZWDongleattr"></a>
  1052. <b>Attributes</b>
  1053. <ul>
  1054. <li><a href="#dummy">dummy</a></li>
  1055. <li><a href="#do_not_notify">do_not_notify</a></li>
  1056. <li><a href="#model">model</a></li>
  1057. <li><a href="#disable">disable</a></li>
  1058. <li><a name="helpSites">helpSites</a><br>
  1059. Comma separated list of Help Sites to get device pictures from or to
  1060. show a link to in the detailed window. Valid values are pepper
  1061. and alliance.
  1062. </li>
  1063. <li><a name="homeId">homeId</a><br>
  1064. Stores the homeId of the dongle. Is a workaround for some buggy dongles,
  1065. wich sometimes report a wrong/nonexisten homeId (Forum #35126)</li>
  1066. <li><a name="networkKey">networkKey</a><br>
  1067. Needed for secure inclusion, hex string with length of 32
  1068. </li>
  1069. <li><a name="neighborListPos">neighborListPos</a><br>
  1070. Used by the "Show neighbor map" function in the FHEMWEB ZWDongle detail
  1071. screen to store the position of the box.
  1072. </li>
  1073. <li><a name="neighborListFmt">neighborListFmt</a><br>
  1074. Used by the "Show neighbor map" function in the FHEMWEB ZWDongle detail
  1075. screen. The value is a perl hash, specifiying the values for the keys
  1076. txt, img and title. In the value each word is replaced by the
  1077. corresponding Internal, Reading or Attribute of the device, if there is
  1078. one to replace. Default is
  1079. <ul><code>
  1080. { txt=>"NAME", img=>"IMAGE", title=>"Time to ack: timeToAck" }
  1081. </code></ul>
  1082. </li>
  1083. <li><a name="showSetInState">showSetInState</a><br>
  1084. If the attribute is set to 1, and a user issues a set command to a ZWave
  1085. device, then the state of the ZWave device will be changed to
  1086. set_&lt;cmd&gt; first, and after the ACK from the device is received, to
  1087. &lt;cmd&gt;. E.g.: Issuing the command on changes the state first to
  1088. set_on, and after the device ack is received, to on. This is analoguos
  1089. to the CUL_HM module. Default for this attribute is 0.
  1090. </li>
  1091. </ul>
  1092. <br>
  1093. <a name="ZWDongleevents"></a>
  1094. <b>Generated events:</b>
  1095. <ul>
  1096. <br><b>General</b>
  1097. <li>UNDEFINED ZWave_${type6}_$id ZWave $homeId $id $classes</li>
  1098. <li>ZW_APPLICATION_UPDATE addDone $nodeId</li>
  1099. <li>ZW_APPLICATION_UPDATE deleteDone $nodeId</li>
  1100. <li>ZW_APPLICATION_UPDATE sudId $nodeId</li>
  1101. <br><b>addNode</b>
  1102. <li>ZW_ADD_NODE_TO_NETWORK [learnReady|nodeFound|slave|controller|
  1103. done|failed]</li>
  1104. <br><b>controllerChange</b>
  1105. <li>ZW_CONTROLLER_CHANGE [learnReady|nodeFound|controller|done|failed]</li>
  1106. <br><b>createNewPrimary</b>
  1107. <li>ZW_CREATE_NEW_PRIMARY [learnReady|nodeFound|controller|done|failed]</li>
  1108. <br><b>factoryReset</b>
  1109. <li>ZW_SET_DEFAULT [done]</li>
  1110. <br><b>learnMode</b>
  1111. <li>ZW_SET_LEARN_MODE [started|done|failed|deleted]</li>
  1112. <br><b>neighborUpdate</b>
  1113. <li>ZW_REQUEST_NODE_NEIGHBOR_UPDATE [started|done|failed]</li>
  1114. <br><b>removeFailedNode</b>
  1115. <li>ZW_REMOVE_FAILED_NODE_ID
  1116. [failedNodeRemoveStarted|notPrimaryController|noCallbackFunction|
  1117. failedNodeNotFound|failedNodeRemoveProcessBusy|
  1118. failedNodeRemoveFail|nodeOk|nodeRemoved|nodeNotRemoved]</li>
  1119. <br><b>removeNode</b>
  1120. <li>ZW_REMOVE_NODE_FROM_NETWORK
  1121. [learnReady|nodeFound|slave|controller|done|failed]</li>
  1122. <br><b>replaceFailedNode</b>
  1123. <li>ZW_REPLACE_FAILED_NODE
  1124. [failedNodeRemoveStarted|notPrimaryController|noCallbackFunction|
  1125. failedNodeNotFound|failedNodeRemoveProcessBusy|
  1126. failedNodeRemoveFail|nodeOk|failedNodeReplace|
  1127. failedNodeReplaceDone|failedNodeRemoveFailed]</li>
  1128. <br><b>routeFor</b>
  1129. <li>ZW_SET_PRIORITY_ROUTE node $nodeId result $nr</li>
  1130. <br><b>sucNetworkUpdate</b>
  1131. <li>ZW_REQUEST_NETWORK_UPDATE [started|selfOrNoSUC|done|abort|wait|diabled|
  1132. overflow]</li>
  1133. <br><b>sucNodeId</b>
  1134. <li>ZW_SET_SUC_NODE_ID [ok|failed|callbackSucceeded|callbackFailed]</li>
  1135. <br><b>sucRouteAdd</b>
  1136. <li>ZW_ASSIGN_SUC_RETURN_ROUTE [started|alreadyActive|transmitOk|
  1137. transmitNoAck|transmitFail|transmitNotIdle|
  1138. transmitNoRoute]</li>
  1139. <br><b>sucRouteDel</b>
  1140. <li>ZW_DELETE_SUC_RETURN_ROUTE [started|alreadyActive|transmitOk|
  1141. transmitNoAck|transmitFail|transmitNotIdle|
  1142. transmitNoRoute]</li>
  1143. <br><b>sucSendNodeId</b>
  1144. <li>ZW_SEND_SUC_ID [started|alreadyActive|transmitOk|
  1145. transmitNoAck|transmitFail|transmitNotIdle|
  1146. transmitNoRoute]</li>
  1147. </ul>
  1148. </ul>
  1149. =end html
  1150. =cut