00_OWX.pm 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. ########################################################################################
  2. #
  3. # FHEM module (Next Generation) to commmunicate with 1-Wire bus devices
  4. # * via an active DS2480/DS9097U bus master interface attached to an USB port
  5. # * via an active DS2480 bus master interface attached to a TCP/IP-UART device
  6. # * via a network-attached CUNO
  7. # * via a COC attached to a Raspberry Pi
  8. # * via an Arduino running OneWireFirmata
  9. #
  10. # Prof. Dr. Peter A. Henning
  11. #
  12. # $Id: 00_OWX.pm 16217 2018-02-18 16:25:30Z phenning $
  13. #
  14. ########################################################################################
  15. ########################################################################################
  16. #
  17. # This programm is free software; you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation; either version 2 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # The GNU General Public License can be found at
  23. # http://www.gnu.org/copyleft/gpl.html.
  24. # A copy is found in the textfile GPL.txt and important notices to the license
  25. # from the author is found in LICENSE.txt distributed with these scripts.
  26. #
  27. # This script is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. #
  32. ########################################################################################
  33. package main;
  34. use strict;
  35. use warnings;
  36. use Time::HiRes qw(gettimeofday);
  37. use Data::Dumper qw(Dumper);
  38. use DevIo;
  39. #-- unfortunately some things OS-dependent
  40. my $SER_regexp;
  41. if( $^O =~ /Win/ ) {
  42. require Win32::SerialPort;
  43. $SER_regexp= "com";
  44. } else {
  45. require Device::SerialPort;
  46. $SER_regexp= "/dev/";
  47. }
  48. sub Log3($$$);
  49. use vars qw{%owg_family %gets %sets $owx_version $owx_debug};
  50. # 1-Wire devices
  51. # http://owfs.sourceforge.net/family.html
  52. %owg_family = (
  53. "01" => ["DS2401/DS2411/DS1990A","OWID DS2401"],
  54. "05" => ["DS2405","OWID DS2405"],
  55. "09" => ["DS2502","OWID DS2502"],
  56. "10" => ["DS18S20/DS1920","OWTHERM DS1820"],
  57. "12" => ["DS2406/DS2507","OWSWITCH DS2406"],
  58. "1B" => ["DS2436","OWID 1B"],
  59. "1D" => ["DS2423","OWCOUNT DS2423"],
  60. "20" => ["DS2450","OWAD DS2450"],
  61. "22" => ["DS1822","OWTHERM DS1822"],
  62. "23" => ["DS2433","OWID 23"],
  63. "24" => ["DS2415/DS1904","OWID 24"],
  64. "26" => ["DS2438","OWMULTI DS2438"],
  65. "27" => ["DS2417","OWID 27"],
  66. "28" => ["DS18B20","OWTHERM DS18B20"],
  67. "29" => ["DS2408","OWSWITCH DS2408"],
  68. "2C" => ["DS2890","OWVAR DS2890"],
  69. "3A" => ["DS2413","OWSWITCH DS2413"],
  70. "3B" => ["DS1825","OWID 3B"],
  71. "7E" => ["OW-ENV","OWID 7E"], #Environmental sensor
  72. "81" => ["DS1420","OWID 81"],
  73. "FF" => ["LCD","OWLCD"]
  74. );
  75. #-- These we may get on request
  76. %gets = (
  77. "alarms" => "A",
  78. "devices" => "D",
  79. "version" => "V",
  80. "qstatus" => "P"
  81. );
  82. #-- These occur in a pulldown menu as settable values for the bus master
  83. # (expert mode: all, standard mode: only reopen)
  84. %sets = (
  85. "close" => "c",
  86. "open" => "O",
  87. "reopen" => "R",
  88. "discover" => "C",
  89. "detect" => "T",
  90. "disconnected" => "D",
  91. "process" => "P"
  92. );
  93. #-- some globals needed for the 1-Wire module
  94. $owx_version="7.08";
  95. #-- debugging now verbosity, this is just for backward compatibility
  96. $owx_debug=0;
  97. ########################################################################################
  98. #
  99. # The following subroutines are independent of the bus interface
  100. #
  101. ########################################################################################
  102. #
  103. # OWX_Initialize
  104. #
  105. # Parameter hash = hash of device addressed
  106. #
  107. ########################################################################################
  108. sub OWX_Initialize ($) {
  109. my ($hash) = @_;
  110. $hash->{Clients} = ":OWAD:OWCOUNT:OWID:OWLCD:OWMULTI:OWSWITCH:OWTHERM:OWVAR:";
  111. $hash->{WriteFn} = "OWX_Write";
  112. $hash->{ReadFn} = "OWX_Read";
  113. $hash->{ReadyFn} = "OWX_Ready";
  114. $hash->{DefFn} = "OWX_Define";
  115. $hash->{UndefFn} = "OWX_Undef";
  116. $hash->{GetFn} = "OWX_Get";
  117. $hash->{SetFn} = "OWX_Set";
  118. $hash->{AttrFn} = "OWX_Attr";
  119. $hash->{AttrList}= "asynchronous:0,1 dokick:0,1 ".
  120. "interval timeout opendelay expert:0_def,1_detail ".
  121. "IODev ".
  122. $readingFnAttributes;
  123. }
  124. ########################################################################################
  125. #
  126. # OWX_Define - Implements Define function
  127. #
  128. # Parameter hash = hash of device addressed, def = definition string
  129. #
  130. ########################################################################################
  131. sub OWX_Define ($$) {
  132. my ($hash, $def) = @_;
  133. my @a = split("[ \t][ \t]*", $def);
  134. my $hwdevice;
  135. my $ret;
  136. #-- check syntax
  137. if(int(@a) < 3){
  138. return "OWX: Syntax error - must be define <name> OWX <serial-device>|<ip-address>[:<port>]|<i2c-bus>:<i2c-addr>|<cuno/coc-device>|<firmata-device>:<firmata-pin>"
  139. }
  140. Log3 $hash->{NAME},2,"OWX: Warning - Some parameter(s) ignored, must be define <name> OWX <serial-device>|<ip-address>[:<port>]|<i2c-bus>:<i2c-addr>|<cuno/coc-device>|<firmata-device>:<firmata-pin>"
  141. if( int(@a)>3 );
  142. my $dev = $a[2];
  143. #-- Dummy 1-Wire ROM identifier, empty device lists
  144. $hash->{ROM_ID} = "FF";
  145. $hash->{DEVS} = ();
  146. $hash->{DEVHASH}{"$a[0]"}="Busmaster";
  147. $hash->{INITDONE} = 0;
  148. #XXX
  149. $hash->{PARTIAL} = "";
  150. delete $hash->{ALARMDEVS};
  151. delete $hash->{followAlarms};
  152. delete $hash->{version};
  153. #-- Clear from leftovers of ASYNC
  154. delete $hash->{ASYNC};
  155. #-- First step - different methods and parameters for setup
  156. #-- check if we have a serial device attached
  157. if ($dev =~ m|$SER_regexp|i) {
  158. require "$attr{global}{modpath}/FHEM/11_OWX_SER.pm";
  159. $hwdevice = OWX_SER->new($hash);
  160. #-- check if we have a TCP connection
  161. }elsif( $dev =~ m|\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?(\:\d+)?| ){
  162. require "$attr{global}{modpath}/FHEM/11_OWX_TCP.pm";
  163. #$hash->{Protocol} = "telnet"
  164. $hwdevice = OWX_TCP->new($hash);
  165. #-- check if we have an i2c interface attached
  166. }elsif( $dev =~ m|^\d\:\d\d| ){
  167. require "$attr{global}{modpath}/FHEM/11_OWX_I2C.pm";
  168. $hwdevice = OWX_I2C->new($hash);
  169. #-- check if we have a COC/CUNO interface attached
  170. }elsif( (defined( $defs{$dev}->{VERSION} ) ? $defs{$dev}->{VERSION} : "") =~ m/CSM|CUNO|MapleCUN...(4|5|6|7|C|D|E|F)/ ){
  171. require "$attr{global}{modpath}/FHEM/11_OWX_CCC.pm";
  172. $hwdevice = OWX_CCC->new($hash);
  173. #-- check if we are connecting to Arduino (via FRM):
  174. } elsif ($dev =~ /.*\:\d{1,2}$/) {
  175. require "$attr{global}{modpath}/FHEM/11_OWX_FRM.pm";
  176. $hwdevice = OWX_FRM->new($hash);
  177. } else {
  178. return "OWX: Define failed, unable to identify interface type $dev for bus ".$hash->{NAME};
  179. };
  180. #-- Second step: perform low level init of device
  181. #Log 1,"OWX: Performing define and low level init of bus ".$hash->{NAME};
  182. $ret = $hwdevice->Define($def);
  183. #-- cancel definition of OWX if failed
  184. return $ret if $ret;
  185. $hash->{OWX} = $hwdevice;
  186. #-- Default settings
  187. $hash->{interval} = 300; # kick every 5 minutes
  188. $hash->{timeout} = 2; # timeout 2 seconds
  189. $hash->{ALARMED} = 0;
  190. $hash->{PRESENT} = 1;
  191. #-- Third step: perform high level init for 1-Wire Bus in minute or so
  192. ###
  193. InternalTimer(time()+60, "OWX_Init", $hash,0);
  194. return undef;
  195. }
  196. ########################################################################################
  197. #
  198. # OWX_Alarms - Find devices on the 1-Wire bus,
  199. # which have the alarm flag set
  200. #
  201. # Parameter hash = hash of bus master
  202. #
  203. # Return: Message or list of alarmed devices
  204. #
  205. ########################################################################################
  206. sub OWX_Alarms ($) {
  207. my ($hash) = @_;
  208. my @owx_alarm_names=();
  209. #-- get the interface
  210. my $name = $hash->{NAME};
  211. my $owx = $hash->{OWX};
  212. my $devhash = $hash->{DEVHASH};
  213. foreach my $owx_dev (sort keys %{$devhash}){
  214. push(@owx_alarm_names,$owx_dev)
  215. if $defs{$owx_dev}->{ALARM};
  216. }
  217. my $res = int(@owx_alarm_names);
  218. if( $res == 0){
  219. $hash->{ALARMED}=0;
  220. return "OWX: No alarmed 1-Wire devices found on bus $name";
  221. } else{
  222. $hash->{ALARMED}=$res;
  223. return "OWX: $res alarmed 1-Wire devices found on bus $name (".join(",",@owx_alarm_names).")";
  224. }
  225. ############### THE FOLLOWING IST DEAD CODE, BECAUSE ALARM SEARCH DOES NOT WORK PROPERLY - WHY ?
  226. if (defined $owx) {
  227. $res = $owx->Alarms();
  228. } else {
  229. #-- interface error
  230. my $owx_interface = $hash->{INTERFACE};
  231. if( !(defined($owx_interface))){
  232. return undef;
  233. } else {
  234. return "OWX: Alarms called with unknown interface $owx_interface on bus $name";
  235. }
  236. }
  237. if( $res == 0){
  238. return "OWX: No alarmed 1-Wire devices found on bus $name";
  239. }
  240. #-- walk through all the devices to get their proper fhem names
  241. foreach my $fhem_dev (sort keys %main::defs) {
  242. #-- skip if busmaster
  243. next if( $name eq $main::defs{$fhem_dev}{NAME} );
  244. #-- all OW types start with OW
  245. next if( substr($main::defs{$fhem_dev}{TYPE},0,2) ne "OW");
  246. foreach my $owx_dev (@{$hash->{ALARMDEVS}}) {
  247. #-- two pieces of the ROM ID found on the bus
  248. my $owx_rnf = substr($owx_dev,3,12);
  249. my $owx_f = substr($owx_dev,0,2);
  250. my $id_owx = $owx_f.".".$owx_rnf;
  251. #-- skip if not in alarm list
  252. if( $owx_dev eq $main::defs{$fhem_dev}{ROM_ID} ){
  253. $main::defs{$fhem_dev}{STATE} = "Alarmed";
  254. push(@owx_alarm_names,$main::defs{$fhem_dev}{NAME});
  255. }
  256. }
  257. }
  258. #-- so far, so good - what do we want to do with this ?
  259. return "OWX: $res alarmed 1-Wire devices found on bus $name (".join(",",@owx_alarm_names).")";
  260. }
  261. #######################################################################################
  262. #
  263. # OWX_Attr - Set one attribute value for device
  264. #
  265. # Parameter hash = hash of device addressed
  266. # a = argument array
  267. #
  268. ########################################################################################
  269. sub OWX_Attr(@) {
  270. my ($do,$name,$key,$value) = @_;
  271. my $hash = $main::defs{$name};
  272. my $queue = $hash->{QUEUE};
  273. my $ret;
  274. if ( $do eq "set") {
  275. ARGUMENT_HANDLER: {
  276. $key eq "asynchronous" and do {
  277. $hash->{ASYNCHRONOUS} = ($value==1 ? 1 : 0);
  278. #-- stop queue prcoessing and delete it
  279. if(!$value){
  280. RemoveInternalTimer ("queue:$name");
  281. delete($hash->{QUEUE});
  282. readingsSingleUpdate($hash,"queue",0,0);
  283. OWX_Reset($hash);
  284. }
  285. last;
  286. };
  287. $key eq "timeout" and do {
  288. $hash->{timeout} = $value;
  289. last;
  290. };
  291. $key eq "opendelay" and do {
  292. $hash->{opendelay} = $value;
  293. last;
  294. };
  295. $key eq "dokick" and do {
  296. $hash->{dokick} = $value;
  297. last;
  298. };
  299. $key eq "interval" and do {
  300. $hash->{interval} = $value;
  301. if ($main::init_done) {
  302. OWX_Kick($hash);
  303. }
  304. last;
  305. }
  306. }
  307. }
  308. return $ret;
  309. }
  310. ########################################################################################
  311. #
  312. # OWX_CRC - Check the CRC code of a device address in @owx_ROM_ID
  313. #
  314. # Parameter romid = if not reference to array, return the CRC8 value instead of checking it
  315. #
  316. ########################################################################################
  317. my @crc8_table = (
  318. 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
  319. 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
  320. 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
  321. 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
  322. 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
  323. 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
  324. 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
  325. 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
  326. 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
  327. 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
  328. 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
  329. 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
  330. 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
  331. 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
  332. 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
  333. 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53);
  334. sub OWX_CRC ($) {
  335. my ($romid) = @_;
  336. my $crc8=0;
  337. my @owx_ROM_ID;
  338. if( ref ($romid) eq 'ARRAY' ){
  339. @owx_ROM_ID = @$romid;
  340. for(my $i=0; $i<8; $i++){
  341. $crc8 = $crc8_table[ $crc8 ^ $owx_ROM_ID[$i] ];
  342. }
  343. return $crc8;
  344. } else {
  345. #-- from search string to byte id
  346. $romid=~s/\.//g;
  347. for(my $i=0;$i<8;$i++){
  348. $owx_ROM_ID[$i]=hex(substr($romid,2*$i,2));
  349. }
  350. for(my $i=0; $i<7; $i++){
  351. $crc8 = $crc8_table[ $crc8 ^ $owx_ROM_ID[$i] ];
  352. }
  353. return $crc8;
  354. }
  355. }
  356. ########################################################################################
  357. #
  358. # OWX_CRC8 - Check the CRC8 code of an a byte string
  359. #
  360. # Parameter string, crc.
  361. # If crc is defined, make a comparison, otherwise output crc8
  362. #
  363. ########################################################################################
  364. sub OWX_CRC8 ($$) {
  365. my ($string,$crc) = @_;
  366. my $crc8=0;
  367. my @strhex;
  368. for(my $i=0; $i<length($string); $i++){
  369. $strhex[$i]=ord(substr($string,$i,1));
  370. $crc8 = $crc8_table[ $crc8 ^ $strhex[$i] ];
  371. }
  372. if( defined($crc) ){
  373. my $crcx = ord($crc);
  374. if ( $crcx == $crc8 ){
  375. return 1;
  376. }else{
  377. return 0;
  378. }
  379. }else{
  380. return sprintf("%#2x", $crc8);
  381. }
  382. }
  383. ########################################################################################
  384. #
  385. # OWX_CRC16 - Calculate the CRC16 code of a string
  386. #
  387. # TODO UNFINISHED CODE
  388. #
  389. # Parameter crc - previous CRC code, c next character
  390. #
  391. ########################################################################################
  392. my @crc16_table = (
  393. 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
  394. 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
  395. 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
  396. 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
  397. 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
  398. 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
  399. 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
  400. 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
  401. 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
  402. 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
  403. 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
  404. 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
  405. 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
  406. 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
  407. 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
  408. 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
  409. 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
  410. 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
  411. 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
  412. 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
  413. 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
  414. 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
  415. 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
  416. 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
  417. 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
  418. 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
  419. 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
  420. 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
  421. 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
  422. 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
  423. 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
  424. 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
  425. );
  426. sub OWX_CRC16 ($$$) {
  427. my ($string,$crclo,$crchi) = @_;
  428. my $crc16=0;
  429. my @strhex;
  430. #Log3 $name, 1,"CRC16 calculated for string of length ".length($string);
  431. for(my $i=0; $i<length($string); $i++){
  432. $strhex[$i]=ord(substr($string,$i,1));
  433. $crc16 = $crc16_table[ ($crc16 ^ $strhex[$i]) & 0xFF ] ^ ($crc16 >> 8);
  434. }
  435. if( defined($crclo) & defined($crchi) ){
  436. my $crc = (255-ord($crclo))+256*(255-ord($crchi));
  437. if ($crc == $crc16 ){
  438. return 1;
  439. }else{
  440. return 0;
  441. }
  442. }else{
  443. return $crc16;
  444. }
  445. }
  446. ########################################################################################
  447. #
  448. # OWX_Discover - Discover devices on the 1-Wire bus,
  449. # autocreate devices if not already present
  450. #
  451. # Parameter hash = hash of bus master
  452. #
  453. # Return: List of devices in table format or undef
  454. #
  455. ########################################################################################
  456. sub OWX_Discover ($) {
  457. my ($hash) = @_;
  458. my $name = $hash->{NAME};
  459. my $res;
  460. my $ret= "";
  461. my ($chip,$acstring,$acname,$exname);
  462. my $ow_dev;
  463. my $owx_rnf;
  464. my $owx_f;
  465. my $owx_crc;
  466. my $id_owx;
  467. my $match;
  468. #-- get the interface - this should be the hardware device ???
  469. my $owx = $hash->{OWX};
  470. my $owx1 = $hash->{INTERFACE};
  471. my $owx2 = $hash->{DeviceName};
  472. my $owx3 = $hash->{HWDEVICE};
  473. my @owx_names =();
  474. $hash->{DEVHASH}=();
  475. $hash->{DEVHASH}{"$name"} = "Busmaster";
  476. #-- Discover all devices on the 1-Wire bus, they will be found in $hash->{DEVS}
  477. if (defined $owx) {
  478. $res = $owx->Discover();
  479. } else {
  480. my $owx_interface = $hash->{INTERFACE};
  481. if( !defined($owx_interface) ) {
  482. return undef;
  483. } else {
  484. Log3 $name, 1,"OWX_Discover on bus $name called with unknown interface $owx_interface";
  485. return undef;
  486. }
  487. }
  488. if (defined $res and (ref($res) eq "ARRAY")) {
  489. $hash->{DEVS} = $res;
  490. }
  491. #-- Go through all devices found on this bus
  492. foreach my $owx_dev (@{$hash->{DEVS}}) {
  493. #-- ignore those which do not have the proper pattern
  494. if( !($owx_dev =~ m/[0-9A-F]{2}\.[0-9A-F]{12}\.[0-9A-F]{2}/) ){
  495. Log3 $name, 3,"OWX_Discover found invalid 1-Wire device ID $owx_dev on bus $name, ignoring it";
  496. next;
  497. }
  498. #-- three pieces of the ROM ID found on the bus
  499. $owx_rnf = substr($owx_dev,3,12);
  500. $owx_f = substr($owx_dev,0,2);
  501. $owx_crc = substr($owx_dev,15,3);
  502. $id_owx = $owx_f.".".$owx_rnf;
  503. $match = 0;
  504. #-- Check against all existing devices
  505. foreach my $fhem_dev (sort keys %main::defs) {
  506. #-- skip if busmaster
  507. # next if( $hash->{NAME} eq $main::defs{$fhem_dev}{NAME} );
  508. #-- all OW types start with OW
  509. next if( !defined($main::defs{$fhem_dev}{TYPE}));
  510. next if( substr($main::defs{$fhem_dev}{TYPE},0,2) ne "OW");
  511. my $id_fhem = substr($main::defs{$fhem_dev}{ROM_ID},0,15);
  512. #-- skip interface device
  513. next if( length($id_fhem) != 15 );
  514. #-- yes, family id and ROM id are the same
  515. if( $id_fhem eq $id_owx ) {
  516. $exname=$main::defs{$fhem_dev}{NAME};
  517. push(@owx_names,$exname);
  518. #-- replace the ROM ID by the proper value including CRC
  519. $main::defs{$fhem_dev}{ROM_ID} = $owx_dev;
  520. $main::defs{$fhem_dev}{PRESENT} = 1;
  521. $main::defs{$fhem_dev}{ERRCOUNT} = 0;
  522. #-- add to DEVHASH
  523. $hash->{DEVHASH}{"$exname"}=$owx_dev;
  524. $match = 1;
  525. last;
  526. #-- no, only duplicate ROMID
  527. }elsif( substr($id_fhem,3,12) eq substr($id_owx,3,12) ) {
  528. Log3 $name, 1, "OWX_Discover: Warning, $fhem_dev on bus $name is defined with duplicate ROM ID ";
  529. }
  530. }
  531. #-- Determine the device type
  532. if(exists $owg_family{$owx_f}) {
  533. $chip = $owg_family{$owx_f}[0];
  534. $acstring = $owg_family{$owx_f}[1];
  535. }else{
  536. Log3 $name, 2, "OWX_Discover: Device with unknown family code '$owx_f' found on bus $name";
  537. #-- All unknown families are ID only
  538. $chip = "unknown";
  539. $acstring = "OWID $owx_f";
  540. }
  541. #Log3 $name, 1,"###\nfor the following device match=$match, chip=$chip name=$name acstring=$acstring";
  542. #-- device exists
  543. if( $match==1 ){
  544. $ret .= sprintf("%s.%s %-14s %s\n", $owx_f,$owx_rnf, $chip, $exname);
  545. #-- device unknown, autocreate
  546. }else{
  547. #-- example code for checking global autocreate - do we want this ?
  548. #foreach my $d (keys %defs) {
  549. #next if($defs{$d}{TYPE} ne "autocreate");
  550. #return undef if(AttrVal($defs{$d}{NAME},"disable",undef));
  551. $acname = sprintf "OWX_%s_%s",$owx_f,$owx_rnf;
  552. #Log3 $name, 1, "to define $acname $acstring $owx_rnf";
  553. $res = CommandDefine(undef,"$acname $acstring $owx_rnf");
  554. if($res) {
  555. $ret.= "OWX_Discover: Error autocreating device with $acname $acstring $owx_rnf: $res\n";
  556. } else{
  557. select(undef,undef,undef,0.01);
  558. push(@owx_names,$acname);
  559. $main::defs{$acname}{PRESENT}=1;
  560. #-- THIS IODev, default room (model is set in the device module)
  561. CommandAttr (undef,"$acname IODev $hash->{NAME}");
  562. CommandAttr (undef,"$acname room OWX");
  563. #-- replace the ROM ID by the proper value
  564. $main::defs{$acname}{ROM_ID}=$owx_dev;
  565. #-- add to DEVHASH
  566. $hash->{DEVHASH}{"$acname"}=$owx_dev;
  567. $ret .= sprintf("%s.%s %-10s %s\n", $owx_f,$owx_rnf, $chip, $acname);
  568. }
  569. }
  570. }
  571. #-- final step: Undefine all 1-Wire devices which
  572. # are autocreated and
  573. # not discovered on this bus
  574. # but have this IODev
  575. foreach my $fhem_dev (sort keys %main::defs) {
  576. #-- skip if malformed device
  577. #next if( !defined($main::defs{$fhem_dev}{NAME}) );
  578. #-- all OW types start with OW, but safeguard against deletion of other devices
  579. #next if( !defined($main::defs{$fhem_dev}{TYPE}));
  580. next if( substr($main::defs{$fhem_dev}{TYPE},0,2) ne "OW");
  581. next if( uc($main::defs{$fhem_dev}{TYPE}) eq "OWX");
  582. next if( uc($main::defs{$fhem_dev}{TYPE}) eq "OWFS");
  583. next if( uc($main::defs{$fhem_dev}{TYPE}) eq "OWSERVER");
  584. next if( uc($main::defs{$fhem_dev}{TYPE}) eq "OWDEVICE");
  585. #-- restrict to autocreated devices
  586. next if( $main::defs{$fhem_dev}{NAME} !~ m/OWX_[0-9a-fA-F]{2}_/);
  587. #-- skip if the device is present.
  588. next if( $main::defs{$fhem_dev}{PRESENT} == 1);
  589. #-- skip if different IODev, but only if other IODev exists
  590. if ( $main::defs{$fhem_dev}{IODev} ){
  591. next if( $main::defs{$fhem_dev}{IODev}{NAME} ne $hash->{NAME} );
  592. }
  593. Log3 $name, 1, "OWX_Discover: Device $main::defs{$fhem_dev}{NAME} of type $main::defs{$fhem_dev}{TYPE} is unused, consider deletion !";
  594. }
  595. #-- Log the discovered devices
  596. Log3 $name, 1, "OWX_Discover: 1-Wire devices found on bus $name (".join(",",@owx_names).")";
  597. #-- tabular view as return value
  598. return "OWX_Discover: 1-Wire devices found on bus $name \n".$ret;
  599. }
  600. ########################################################################################
  601. #
  602. # OWX_Get - Implements GetFn function
  603. #
  604. # Parameter hash = hash of the bus master a = argument array
  605. #
  606. ########################################################################################
  607. sub OWX_Get($@) {
  608. my ($hash, @a) = @_;
  609. return "OWX: Get needs exactly one parameter" if(@a != 2);
  610. my $name = $hash->{NAME};
  611. my $owx_dev = $hash->{ROM_ID};
  612. if( $a[1] eq "alarms") {
  613. my $res = OWX_Alarms($hash);
  614. #-- process result
  615. return $res
  616. } elsif( $a[1] eq "devices") {
  617. my $res = OWX_Discover($hash);
  618. #-- process result
  619. return $res
  620. } elsif( $a[1] eq "version") {
  621. return $owx_version;
  622. #-- expert mode
  623. } elsif( $a[1] eq "qstatus") {
  624. my $qlen = ($hash->{QUEUE} ? scalar(@{$hash->{QUEUE}}) : 0);
  625. my $state = $hash->{STATE};
  626. my $dev = $hash->{DeviceName};
  627. my $busy = ($hash->{BUSY})? "BUSY" : "NOT BUSY";
  628. my $block = ($hash->{BLOCK})? "BLOCKED" : "NOT BLOCKED";
  629. $hash->{BUSY} = 1;
  630. my $res = "OWX: Queue $name: => dev=$dev length=$qlen, state=$state, $busy, $block\n";
  631. foreach my $diapoint (@{$hash->{QUEUE}}) {
  632. $res .= " => ".$diapoint->{owx_dev}." context ".$diapoint->{context}." expecting ".$diapoint->{numread}." bytes, ".$diapoint->{status}."\n";
  633. }
  634. return $res;
  635. } else {
  636. return "OWX_Get with unknown argument $a[1], choose one of ".
  637. ( (AttrVal($name,"expert","") eq "1_detail") ? join(":noArg ", sort keys %gets).":noArg" : "alarms devices version");
  638. }
  639. }
  640. #######################################################################################
  641. #
  642. # OWX_Init - High Level Init of 1-Wire Bus
  643. #
  644. # Parameter hash = hash of bus master
  645. #
  646. # Return 1 : OK
  647. # 0 : not OK
  648. #
  649. ########################################################################################
  650. sub OWX_Init ($) {
  651. my ($hash)=@_;
  652. #-- get the interface
  653. my $owx = $hash->{OWX};
  654. my $name= $hash->{NAME};
  655. if( defined($attr{$name}{"asynchronous"}) && ($attr{$name}{"asynchronous"}==1) ){
  656. $hash->{ASYNCHRONOUS} = 1;
  657. }else{
  658. $hash->{ASYNCHRONOUS} = 0;
  659. }
  660. return "OWX_Init finds a disconnected interface"
  661. if($hash->{STATE} eq "disconnected");
  662. Log3 $name,1,"OWX_Init called for bus $name with interface state ".$hash->{STATE}.", now going for detect";
  663. if ($owx) {
  664. #-- Fourth step: see, if a bus interface is detected
  665. if (!($owx->Detect())) {
  666. $hash->{PRESENT} = 0;
  667. #$init_done = 1;
  668. Log3 $name,4,"OWX_Init: Detection failed";
  669. return "OWX_Init Detection failed";
  670. }
  671. } else {
  672. #-- interface error
  673. my $owx_interface = $hash->{INTERFACE};
  674. if( !(defined($owx_interface))){
  675. return "OWX_Init called with undefined interface";
  676. } else {
  677. return "OWX_Init called with unknown interface $owx_interface";
  678. }
  679. }
  680. #-- Fifth step: discovering devices on the bus
  681. OWX_Discover($hash);
  682. $hash->{INITDONE} = 1;
  683. #-- Intiate first alarm detection and eventually conversion
  684. OWX_Kick($hash);
  685. return undef;
  686. }
  687. ########################################################################################
  688. #
  689. # OWX_Kick - Initiate some processes in all devices
  690. #
  691. # Parameter hash = hash of bus master
  692. #
  693. # Return 1 : OK
  694. # 0 : Not OK
  695. #
  696. ########################################################################################
  697. sub OWX_Kick($) {
  698. my($hash) = @_;
  699. #-- Call us in n seconds again.
  700. InternalTimer(gettimeofday()+ $hash->{interval}, "OWX_Kick", $hash,1);
  701. #-- Only if we have the dokick attribute set to 1
  702. if( defined($attr{$hash->{NAME}}{dokick}) && ($attr{$hash->{NAME}}{dokick} eq "1") ){
  703. my $name = $hash->{NAME};
  704. my $interface = $hash->{TYPE};
  705. my $asynchronous = $hash->{ASYNCHRONOUS};
  706. #-- issue the skip ROM command \xCC followed by start conversion command \x44
  707. my $cmd = "\xCC\x44";
  708. #-- OWX interface
  709. if( $interface eq "OWX" ){
  710. #-- OLD OWX interface
  711. if( !$asynchronous ){
  712. OWX_Reset($hash);
  713. OWX_Complex($hash,"","\xCC\x44",0);
  714. #-- NEW OWX interface
  715. }else{
  716. #### master slave context proc owx_dev data crcpart numread startread callback delay
  717. OWX_Qomplex($hash, undef, "kick", 1, "", $cmd, 0, -10, undef, undef, undef)
  718. }
  719. }
  720. }
  721. return 1;
  722. }
  723. ########################################################################################
  724. #
  725. # OWX_Reset - Reset the 1-Wire bus
  726. #
  727. # Parameter hash = hash of bus master
  728. #
  729. # Return 1 : OK
  730. # 0 : not OK
  731. #
  732. ########################################################################################
  733. sub OWX_Reset ($) {
  734. my ($hash)=@_;
  735. #-- get the interface
  736. my $owx = $hash->{OWX};
  737. my $name = $hash->{NAME};
  738. my $queue = $hash->{QUEUE};
  739. my $status = (defined( $queue->[0]->{status})) ? $queue->[0]->{status} : "completed";
  740. if (defined $owx) {
  741. #-- reset only when there is no queue or when the status of the current entry is completed
  742. if( $status eq "completed" ){
  743. return $owx->Reset()
  744. } else {
  745. #Log 1,"==================> Reset attempted on waiting interface";
  746. my $i = 1;
  747. while ( (my @call_details = (caller($i++))) ){
  748. Log 1,$call_details[1].":".$call_details[2]." in function ".$call_details[3];
  749. }
  750. return 0;
  751. }
  752. } else {
  753. #-- interface error
  754. my $owx_interface = $hash->{INTERFACE};
  755. if( !(defined($owx_interface))){
  756. return 0;
  757. } else {
  758. Log3 $name, 3,"OWX: Reset called with unknown interface $owx_interface on bus $name";
  759. return 0;
  760. }
  761. }
  762. }
  763. #######################################################################################
  764. #
  765. # OWX_Read - Callback from FHEM main loop - read from the bus
  766. #
  767. # Parameter hash = hash of bus master
  768. #
  769. ########################################################################################
  770. sub OWX_Read(@) {
  771. my ($hash) = @_;
  772. #-- must not be used when acting synchronously
  773. return undef
  774. if( !$hash->{ASYNCHRONOUS} );
  775. my ($buffer, $buffer2, $qlen, $res, $ret);
  776. #-- master data
  777. my $owx = $hash->{OWX};
  778. my $name = $hash->{NAME};
  779. my $queue = $hash->{QUEUE};
  780. my $query = $queue->[0];
  781. my $staexp = $query->{startread};
  782. my $sldev = $query->{hash}->{NAME};
  783. my $slcont = $query->{context};
  784. my $devlevel = (defined($attr{$name}{verbose})) ? $attr{$name}{verbose} : 0;
  785. my $qdebug = ( ($devlevel > 3) || ($attr{global}{verbose}>3) ) ? 1 : 0;
  786. #--safeguard: really called from queue ?
  787. #if( !defined($sldev) ){
  788. #Log 1,"OWX_Read: Erroneous call from somewhere in FHEM ???";
  789. #my $i = 1;
  790. #Log 1, "=============> Empty call of OWX_Read Stack Trace:";
  791. #while ( (my @call_details = (caller($i++))) ){
  792. # Log 1,$call_details[1].":".$call_details[2]." in function ".$call_details[3];
  793. #}
  794. #return;
  795. #}
  796. #-- expected length
  797. my $numget;
  798. my $numexp = $query->{numread};
  799. $numexp = 0
  800. if( (!$numexp) || ($numexp eq "") );
  801. #-- actual read operation
  802. $buffer = $owx->Read($numexp);
  803. #-- actual length
  804. $numget = length($buffer)-10;
  805. #-- partial reception
  806. if( $numget < $numexp ){
  807. #-- empty device buffer
  808. if( length($hash->{PARTIAL}) == 0 ){
  809. $hash->{PARTIAL} = $buffer;
  810. return undef;
  811. #-- some data present already
  812. }else{
  813. $buffer = $hash->{PARTIAL}.$buffer;
  814. $numget = length($buffer)-10;
  815. #-- NOT done with this return to main loop
  816. if( $numget<$numexp ){
  817. $hash->{PARTIAL} = $buffer;
  818. return undef;
  819. }
  820. }
  821. }
  822. #-- IF WE ARE HERE, WE SHOULD HAVE ALL THE BYTES
  823. $hash->{PARTIAL} = "";
  824. $queue->[0]->{status} = "completed";
  825. #Log3 $name, 1, "OWX_Read: queue $name context ".$queue->[0]->{context}." received $numget bytes, expected $numexp. status completed, processing entry";
  826. #-- if necessary perform a reset after the full read operation
  827. if( ($queue->[0]->{proc} & 1)==1 ){
  828. #select(undef,undef,undef,0.01);
  829. $owx->Reset()
  830. };
  831. #-- slave data
  832. my $slave = $query->{hash};
  833. my $context = $query->{context};
  834. my $proc = (defined($query->{proc}))?($query->{proc}):1;
  835. my $owx_dev = $query->{owx_dev};
  836. my $crcpart = $query->{crcpart}; #-- needed for CRC check
  837. #-- successful completion, take off the queue
  838. shift(@{$queue});
  839. $qlen = @{$queue};
  840. #Log3 $name, 4, "OWX_Read: $name queue contains $qlen entries after removal of active entry";
  841. #-- calling callback
  842. if( $query->{callback} ){
  843. #Log3 $name, 1, "OWX_Read: $name received $numget bytes, expected $numexp. Now calling callback for context $context";
  844. $ret = $query->{callback}($slave,$context, $proc, $owx_dev, $crcpart, $numexp, substr($buffer,$staexp,$numexp));
  845. }else{
  846. #Log3 $name, 1, "OWX_Read: $name received $numget bytes, expected $numexp. No callback defined for context $context";
  847. }
  848. #-- we are done - but maybe still have to do a synchronous job
  849. if( $hash->{BLOCK} ){
  850. my $ret2 = $hash->{BLOCKCALL}->();
  851. };
  852. #-- reset busy flag
  853. $hash->{BUSY} = 0;
  854. #-- more items in queue -> schedule next process
  855. #if( $name eq "OWX_TEST"){
  856. # Log3 $name, 1,"----------------------------------------------";
  857. # Log3 $name,1, " queue $name contains ".scalar(@{$hash->{QUEUE}})." entries after read";
  858. # foreach my $diapoint (@{$hash->{QUEUE}}) {
  859. # Log3 $name, 1, " => ".$diapoint->{owx_dev}." context ".$diapoint->{context}." expecting ".$diapoint->{numread}." bytes, ".$diapoint->{status};
  860. # }
  861. # Log3 $name, 1,"----------------------------------------------";
  862. #}
  863. my $now = gettimeofday();
  864. if( ($qlen > 0) ) {
  865. InternalTimer($now+0.01, "OWX_PrQueue", "queue:$name", 0);
  866. }
  867. return $ret;
  868. }
  869. #######################################################################################
  870. #
  871. # OWX_Ready
  872. #
  873. # Parameter hash = hash of bus master
  874. #
  875. # Return 1 : OK
  876. # 0 : not OK
  877. #
  878. ########################################################################################
  879. sub OWX_Ready($) {
  880. my ($hash)=@_;
  881. #-- relevant for Windows/USB only
  882. if ( $hash->{STATE} ne "disconnected" ){
  883. if(defined($hash->{USBDev})) {
  884. my $po = $hash->{USBDev};
  885. my ( $BlockingFlags, $InBytes, $OutBytes, $ErrorFlags ) = $po->status;
  886. return ( $InBytes > 0 );
  887. }
  888. return undef;
  889. }
  890. #-- get the interface
  891. my $owx = $hash->{OWX};
  892. my $name = $hash->{NAME};
  893. my $time = time();
  894. #-- skip this if delay time is not yet over
  895. if($hash->{NEXT_OPEN} && $time < $hash->{NEXT_OPEN}) {
  896. #Log3 $hash, 5, "NEXT_OPEN prevented opening $name";
  897. }else{
  898. #-- call the specific interface if delay time is over
  899. my $success=$owx->Ready();
  900. if($success) {
  901. delete($hash->{NEXT_OPEN});
  902. #-- re-init the bus
  903. Log3 $name,1,"OWX_Ready calling low-level init of bus";
  904. my $ret = OWX_Init($hash);
  905. }else{
  906. $hash->{NEXT_OPEN} = $time+AttrVal($name, "opendelay", 60);
  907. }
  908. }
  909. return undef;
  910. }
  911. ########################################################################################
  912. #
  913. # OWX_Set - Implements SetFn function
  914. #
  915. # Parameter hash , a = argument array
  916. #
  917. ########################################################################################
  918. sub OWX_Set($@) {
  919. my ($hash, @a) = @_;
  920. my $name = shift @a;
  921. my $res = 0;
  922. #-- for the selector: all values are possible for expert, otherwise only reopen
  923. return ( (AttrVal($name,"expert","") eq "1_detail") ? join(":noArg ", sort keys %sets).":noArg" : "reopen:noArg")
  924. if(!defined($sets{$a[0]}));
  925. return "OWX_Set: With unknown argument $a[0], choose one of " .
  926. ( (AttrVal($name,"expert","") eq "1_detail") ? join(" ", sort keys %sets) : "reopen")
  927. if(!defined($sets{$a[0]}));
  928. my $owx = $hash->{OWX};
  929. #-- Set open
  930. if( $a[0] eq "open" ){
  931. $owx->Open();
  932. $res = 0;
  933. }
  934. #-- Set close
  935. if( $a[0] eq "close" ){
  936. $owx->Close();
  937. $res = 0;
  938. }
  939. #-- Set reopen
  940. if( $a[0] eq "reopen" ){
  941. $owx->Reopen();
  942. $res = 0;
  943. }
  944. #-- Set discover
  945. if( $a[0] eq "discover" ){
  946. OWX_Discover($hash);
  947. $res = 0;
  948. }
  949. #-- Set detect
  950. if( $a[0] eq "detect" ){
  951. my $owx = $hash->{OWX};
  952. $owx->Detect();
  953. $res = 0;
  954. }
  955. if( $a[0] eq "process") {
  956. my $res = OWX_PrQueue("queue:$name");
  957. #-- process result
  958. return $res
  959. }
  960. Log3 $name, 3, "OWX_Set $name ".join(" ",@a)." => $res";
  961. }
  962. ########################################################################################
  963. #
  964. # OWX_Undef - Implements UndefFn function
  965. #
  966. # Parameter hash = hash of the bus master, name
  967. #
  968. ########################################################################################
  969. sub OWX_Undef ($$) {
  970. my ($hash, $name) = @_;
  971. RemoveInternalTimer($hash);
  972. RemoveInternalTimer ("queue:$name");
  973. # TODO - THIS IS WRONG. DO NOT DELETE THEM, BUT INVALIDATE THEM
  974. #-- invalidate clients
  975. #foreach my $d (sort keys %defs) {
  976. # if(defined($defs{$d}) &&
  977. # defined($defs{$d}{IODev}) &&
  978. # $defs{$d}{IODev} == $hash) {
  979. # Log3 $hash, 3, "deleting port for $d";
  980. # delete $defs{$d}{IODev};
  981. # }
  982. #}
  983. DevIo_CloseDev($hash);
  984. return undef;
  985. }
  986. ########################################################################################
  987. #
  988. # OWX_Verify - Verify a particular device on the 1-Wire bus
  989. #
  990. # Parameter hash = hash of bus master, dev = 8 Byte ROM ID of device to be tested
  991. #
  992. # Return 1 : device found
  993. # 0 : device not found
  994. #
  995. # Unterbinden
  996. #
  997. ########################################################################################
  998. sub OWX_Verify ($$$$) {
  999. my ($hashorname,$devname,$devid,$type) = @_;
  1000. my $hash;
  1001. my $i;
  1002. if( $type == 1){
  1003. $hash = $defs{$hashorname};
  1004. }else{
  1005. $hash = $hashorname;
  1006. }
  1007. #-- get the interface
  1008. my $name = $hash->{NAME};
  1009. my $owx = $hash->{OWX};
  1010. my $state = $hash->{STATE};
  1011. #-- Do nothing, if devices not yet discovered
  1012. return undef
  1013. if( !$hash->{INITDONE} );
  1014. my $now = gettimeofday();
  1015. my $qlen = ($hash->{QUEUE} ? scalar(@{$hash->{QUEUE}}) : 0);
  1016. #-- if called synchronously, or queue is blocked, or queue empty, do it now
  1017. if( !($hash->{ASYNCHRONOUS}) ||
  1018. ( $qlen == 0) ||
  1019. ( $hash->{ASYNCHRONOUS} && $hash->{BLOCK} ) ){
  1020. if( $state ne "opened" ) {
  1021. Log3 $name, 3,"OWX_Verify called while interface $name not opened";
  1022. return undef;
  1023. }
  1024. Log3 $name, 5,"OWX_Verify called for interface $name";
  1025. $hash->{BLOCK} = 1;
  1026. $hash->{BUSY} = 1;
  1027. my $ret = $owx->Verify($devid);
  1028. #-- remove queue block and restart queue
  1029. $hash->{BLOCK} = 0;
  1030. $hash->{BUSY} = 0;
  1031. my $slave = $defs{$devname};
  1032. #-- generate an event only if presence has changed
  1033. if( $ret == 0 ){
  1034. readingsSingleUpdate($slave,"present",0,$slave->{PRESENT});
  1035. } else {
  1036. readingsSingleUpdate($slave,"present",1,!$slave->{PRESENT});
  1037. }
  1038. $slave->{PRESENT} = $ret;
  1039. OWX_Ready($hash);
  1040. return $ret;
  1041. #-- if called asynchronously, and queue is not blocked, issue queue block and delay.
  1042. }elsif( $hash->{ASYNCHRONOUS} && !($hash->{BLOCK}) ){
  1043. #-- issue queue block
  1044. $hash->{BLOCK} = 1;
  1045. Log3 $name, 5,"OWX_Verify issued a queue block on interface $name";
  1046. $hash->{BLOCKCALL}=sub(){ OWX_Verify($name,$devname,$devid,1); };
  1047. return undef;
  1048. }
  1049. }
  1050. #######################################################################################
  1051. #
  1052. # OWX_Write - Write to the bus
  1053. #
  1054. # Parameter hash = hash of bus master,
  1055. #
  1056. ########################################################################################
  1057. sub OWX_Write($){
  1058. my $hash = shift;
  1059. my $queue = $hash->{QUEUE};
  1060. my $data = $queue->[0]{data};
  1061. my $proc = $queue->[0]{proc};
  1062. my $name = $hash->{NAME};
  1063. my $owx = $hash->{OWX};
  1064. my $buffer = "";
  1065. #-- catch late calls
  1066. return
  1067. if( !$queue );
  1068. #-- perform a send from queue
  1069. if($hash->{STATE} ne "disconnected"){
  1070. #-- empty the receive buffer
  1071. $hash->{PARTIAL} = "";
  1072. $queue->[0]{status} = "active";
  1073. my $reset = 1-(($proc & 2) >> 1);
  1074. $owx->Write($data, $reset);
  1075. }
  1076. #-- in this case we will never get a read operation
  1077. if( $queue->[0]{numread} == -10){
  1078. $hash->{PARTIAL} = "";
  1079. $queue->[0]->{status} = "completed";
  1080. #Log3 $name, 1, "OWX_Write: queue $name gets status completed because donotwait is set";
  1081. #-- if necessary perform a reset after this operation
  1082. if( ($queue->[0]->{proc} & 1)==1 ){
  1083. select(undef,undef,undef,0.01);
  1084. $owx->Reset()
  1085. };
  1086. #-- reset busy flag
  1087. shift(@{$queue});
  1088. $hash->{BUSY} = 0;
  1089. }
  1090. return
  1091. }
  1092. ########################################################################################
  1093. #
  1094. # Complex R/W operations and queue handling
  1095. #
  1096. ########################################################################################
  1097. #
  1098. # OWX_Complex - Synchronously send match ROM, data block and receive bytes as response
  1099. #
  1100. # Parameter hash = hash of bus master,
  1101. # owx_dev = ROM ID of device
  1102. # data = string to send
  1103. # numread = number of bytes to receive
  1104. #
  1105. # Return response, if OK
  1106. # 0 if not OK
  1107. #
  1108. ########################################################################################
  1109. sub OWX_Complex ($$$$) {
  1110. my ($hash,$owx_dev,$data,$numread) =@_;
  1111. #-- get the interface
  1112. my $name = $hash->{NAME};
  1113. my $owx = $hash->{OWX};
  1114. my $state = $hash->{STATE};
  1115. if( $state eq "disconnected" ) {
  1116. Log3 $name, 1,"OWX_Complex called while interface $name disconnected";
  1117. return 0;
  1118. }
  1119. my $ret = $owx->Complex($owx_dev,$data,$numread);
  1120. return (!defined($ret))?0:$ret;
  1121. }
  1122. #######################################################################################
  1123. #
  1124. # OWX_Qomplex - Put into queue:
  1125. # asynchronously send match ROM, data block and receive bytes as response
  1126. # but queued
  1127. #
  1128. # Parameter hash = hash of bus master,
  1129. # slave = hash of slave device
  1130. # context = mode for evaluating the binary data
  1131. # proc = processing instruction, also passed to OWX_Read.
  1132. # bitwise interpretation !!
  1133. # if 0, nothing special
  1134. # if 1 = bit 0, a reset will be performed also after
  1135. # the last operation in OWX_Read
  1136. # if 2 = bit 1, the initial reset of the bus will be suppressed
  1137. # if 4 = do not wait for a response for more than ???
  1138. # if 8 = bit 3, the fillup of the data with 0xff will be suppressed
  1139. # if 16= bit 4, the insertion will be at the top of the queue
  1140. # owx_dev = ROM ID of slave device
  1141. # data = string to be sent
  1142. # crcpart = part of the data that needs to be part of the CRC check
  1143. # numread = number of bytes to receive
  1144. # startread = start index to begin reading in the return value
  1145. # callback = address of callback function
  1146. # mindelay = minimum time until next call to this device
  1147. #
  1148. # Return response, if NOK (died)
  1149. # undef if OK
  1150. #
  1151. ########################################################################################
  1152. sub OWX_Qomplex(@){
  1153. my ( $hash, $slave, $context, $proc, $owx_dev, $data, $crcpart, $numread, $startread, $callback, $mindelay) = @_;
  1154. #### master slave context proc owx_dev data crcpart numread startread callback delay
  1155. my $name = $hash->{NAME};
  1156. my $data2;
  1157. my $res = "";
  1158. my $res2 = "";
  1159. if( !$defs{$name}) {
  1160. return undef;
  1161. }
  1162. #-- get the interface
  1163. my $interface = $hash->{INTERFACE};
  1164. my $owx = $hash->{OWX};
  1165. my $devlevel = (defined($attr{$name}{verbose})) ? $attr{$name}{verbose} : 0;
  1166. my $qdebug = ( ($devlevel > 3) || ($attr{global}{verbose}>3) ) ? 1 : 0;
  1167. #-- has match ROM part, prepend the rom id
  1168. if( $owx_dev ){
  1169. #-- ID of the device
  1170. my $owx_rnf = substr($owx_dev,3,12);
  1171. my $owx_f = substr($owx_dev,0,2);
  1172. #-- 8 byte 1-Wire device address
  1173. my @rom_id =(0,0,0,0 ,0,0,0,0);
  1174. #-- from search string to byte id
  1175. $owx_dev=~s/\.//g;
  1176. for(my $i=0;$i<8;$i++){
  1177. $rom_id[$i]=hex(substr($owx_dev,2*$i,2));
  1178. }
  1179. $data2=sprintf("\x55%c%c%c%c%c%c%c%c",@rom_id).$data;
  1180. #-- has no match ROM part, insert a "skip ROM" command
  1181. } else {
  1182. $data2="\xCC".$data;
  1183. }
  1184. #-- has receive data part, suppress fillup ?
  1185. if( (($proc & 8)>>3) != 1) {
  1186. if( $numread >0 ){
  1187. #$numread += length($data);
  1188. for( my $i=0;$i<$numread;$i++){
  1189. $data2 .= "\xFF";
  1190. }
  1191. }
  1192. }
  1193. my $now = gettimeofday();
  1194. my %dialog;
  1195. $dialog{hash} = $slave;
  1196. $dialog{context} = $context;
  1197. $dialog{proc} = $proc;
  1198. $dialog{owx_dev} = $owx_dev;
  1199. $dialog{data} = $data2;
  1200. $dialog{crcpart} = $crcpart;
  1201. $dialog{numread} = $numread;
  1202. $dialog{startread} = $startread;
  1203. $dialog{callback} = $callback;
  1204. $dialog{delay} = $mindelay;
  1205. $dialog{start} = $now;
  1206. $dialog{status} = "waiting";
  1207. if( !$context ){
  1208. Log3 $name,1,"OWX_Qomplex: context missing in queue $name entry for device $owx_dev";
  1209. $dialog{context} = ""
  1210. }
  1211. my $qlen = ($hash->{QUEUE} ? scalar(@{$hash->{QUEUE}}) : 0);
  1212. readingsSingleUpdate($hash,"queue",$qlen,0);
  1213. #-- single item => new queue, and start processing immediately
  1214. if(!$qlen) {
  1215. $hash->{QUEUE} = [ \%dialog ];
  1216. Log3 $name, 4, "OWX_Qomplex: Added dev $owx_dev to queue $name context=$context";
  1217. OWX_PrQueue("direct:".$name);
  1218. #-- List queue for debugging
  1219. if( $qdebug ){
  1220. Log3 $name,1, " queue $name contains ".scalar(@{$hash->{QUEUE}})." entries after insertion";
  1221. foreach my $diapoint (@{$hash->{QUEUE}}) {
  1222. Log3 $name, 1, " => ".$diapoint->{owx_dev}." context ".$diapoint->{context}." expecting ".$diapoint->{numread}." bytes, ".$diapoint->{status};
  1223. }
  1224. Log3 $name, 1,"----------------------------------------------";
  1225. return;
  1226. }
  1227. #-- priority insert at top of queue, and no immediate processing
  1228. } elsif( (($proc & 16)>>4)==1 ) {
  1229. #if ( $hash->{BUSY}==1 ) {
  1230. # my $dialogtop = shift(@{$hash->{QUEUE}});
  1231. # unshift( @{$hash->{QUEUE}}, \%dialog );
  1232. # unshift( @{$hash->{QUEUE}}, $dialogtop);
  1233. # Log 1,"------------> Prio insert as second task";
  1234. # Log 1, " queue $name contains ".scalar(@{$hash->{QUEUE}})." entries after insertion";
  1235. # foreach my $diapoint (@{$hash->{QUEUE}}) {
  1236. # Log 1, " => ".$diapoint->{owx_dev}." context ".$diapoint->{context}." expecting ".$diapoint->{numread}." bytes, ".$diapoint->{status};
  1237. # }
  1238. # Log 1,"----------------------------------------------";
  1239. #}else{
  1240. unshift( @{$hash->{QUEUE}}, \%dialog );
  1241. #Log 1,"------------> Prio insert as first task";
  1242. #Log 1, " queue $name contains ".scalar(@{$hash->{QUEUE}})." entries after insertion";
  1243. #foreach my $diapoint (@{$hash->{QUEUE}}) {
  1244. # Log 1, " => ".$diapoint->{owx_dev}." context ".$diapoint->{context}." expecting ".$diapoint->{numread}." bytes, ".$diapoint->{status};
  1245. #}
  1246. #Log 1,"----------------------------------------------";
  1247. #}
  1248. return;
  1249. #-- insert into existing queue
  1250. } else {
  1251. if ($qlen > AttrVal($name, "queueMax", 100)) {
  1252. Log 1,"OWX_Qomplex: $name queue too long, dropping data"
  1253. if( $qdebug );
  1254. } else {
  1255. push(@{$hash->{QUEUE}}, \%dialog);
  1256. Log 1, "OWX_Qomplex: Added dev $owx_dev to queue $name numread=$numread"
  1257. if( $qdebug );
  1258. }
  1259. }
  1260. #-- List queue for debugging
  1261. if( $qdebug ){
  1262. Log 1, " queue $name contains ".scalar(@{$hash->{QUEUE}})." entries after insertion";
  1263. foreach my $diapoint (@{$hash->{QUEUE}}) {
  1264. Log 1, " => ".$diapoint->{owx_dev}." context ".$diapoint->{context}." expecting ".$diapoint->{numread}." bytes, ".$diapoint->{status};
  1265. }
  1266. Log 1,"----------------------------------------------";
  1267. }
  1268. }
  1269. #######################################################################################
  1270. #
  1271. # OWX_PrQueue - ProcessQueue.
  1272. # Called by internal timer as"queue:$name"
  1273. # or directly with $direct:$name
  1274. #
  1275. # Flags: BUSY indicates an asynchronous r/w operation
  1276. # BLOCKED stops the asynchronous operations for synchronous r/w
  1277. #
  1278. #
  1279. # Return response, if OK
  1280. # 0 if not OK
  1281. #
  1282. ########################################################################################
  1283. sub OWX_PrQueue($){
  1284. my (undef,$name) = split(':', $_[0]);
  1285. my $hash = $defs{$name};
  1286. my $queue = $hash->{QUEUE};
  1287. my $slave;
  1288. #-- 1 seconds queue delay as default, 2 seconds for timeout
  1289. my $longDelay = 1.0;
  1290. my $shortDelay = 0.05;
  1291. my $maxrunning = ( defined($attr{$name}{timeout})) ? $attr{$name}{timeout} : 2;
  1292. my $devlevel = (defined($attr{$name}{verbose})) ? $attr{$name}{verbose} : 0;
  1293. my $qdebug = ( ($devlevel > 3) || ($attr{global}{verbose}>3) ) ? 1 : 0;
  1294. RemoveInternalTimer ("queue:$name");
  1295. my $now = gettimeofday();
  1296. #-- fhem not initialized, wait with IO || !$hash->{INITDONE}
  1297. if ( !$main::init_done ) {
  1298. Log3 $name,1, "OWX_PrQueue: init not done, delay sending from queue $name";
  1299. InternalTimer($now+$longDelay, "OWX_PrQueue", "queue:$name", 0);
  1300. return;
  1301. }
  1302. #-- Done with the queue
  1303. return
  1304. if( !$queue);
  1305. my $qlen = @{$queue};
  1306. return
  1307. if( $qlen == 0);
  1308. #-- remove top entry if garbage
  1309. if( !defined($queue->[0]{data}) || ($queue->[0]{data} eq "" || $queue->[0]->{start} eq "" )) {
  1310. my $reason = (!defined($queue->[0]{data})) ? "garbage" : (($queue->[0]{data} eq "") ? "no data" :
  1311. (($queue->[0]->{start} eq "") ? "no start" : "unknown"));
  1312. Log3 $name,1, "OWX_PrQueue: removing garbage from queue $name, reason $reason ";
  1313. shift(@{$queue});
  1314. InternalTimer($now+$shortDelay, "OWX_PrQueue", "queue:$name", 0);
  1315. return;
  1316. }
  1317. #-- still waiting for reply to last send
  1318. if ( $hash->{BUSY} && $hash->{BUSY}==1 ) {
  1319. my $running = int(100*($now-$queue->[0]->{start}))/100;
  1320. #-- donotwait flag is set
  1321. if( (($queue->[0]->{proc} & 4)>>2)==1 ){
  1322. #Log 1, "OWX_PrQueue: removing ".$queue->[0]->{owx_dev}." w. context ".$queue->[0]->{context}." from queue $name, because donotwait is set"
  1323. # if( $name eq "OWX_TEST");
  1324. $hash->{BUSY}=0;
  1325. shift(@{$queue});
  1326. InternalTimer($now+$shortDelay, "OWX_PrQueue", "queue:$name", 0);
  1327. return;
  1328. #-- timeout, so kill this entry
  1329. }elsif( $running > $maxrunning ){
  1330. #Log 1, "OWX_PrQueue: removing ".$queue->[0]->{owx_dev}." w. context ".$queue->[0]->{context}." from queue $name. Timeout $running > $maxrunning seconds."
  1331. # if( $name eq "OWX_TEST");
  1332. $hash->{BUSY}=0;
  1333. shift(@{$queue});
  1334. InternalTimer($now+$shortDelay, "OWX_PrQueue", "queue:$name", 0);
  1335. return;
  1336. #-- no timeout, but we have to wait for a reply
  1337. }else{
  1338. my $rout='';
  1339. #my $hash2 = $selectlist{'OWX_WIFI.192.168.0.97:23'};
  1340. #my $fd = ( defined($hash2->{FD}) ) ? $hash2->{FD} : "NO";
  1341. #my $vc = ( defined($hash2->{FD}) ) ? vec($rout, $hash2->{FD}, 1) : "NO";
  1342. #Log 1, "OWX_PrQueue: still waiting for reply, delay sending from queue $name"
  1343. # if( $name eq "OWX_TEST");
  1344. #Log 1, "OWX_PrQueue: still waiting for reply, delay sending from queue $name.";
  1345. # Log 1, " => ".$queue->[0]->{owx_dev}." context ".$queue->[0]->{context}." expecting ".$queue->[0]->{numread}." bytes, ".$queue->[0]->{status};
  1346. InternalTimer($now+$longDelay, "OWX_PrQueue", "queue:$name", 0);
  1347. return;
  1348. }
  1349. }
  1350. #-- if something to send, check if min delay for slave is over
  1351. if( $queue->[0]{data} ) {
  1352. $slave = $queue->[0]{hash}; # hash of 1-Wire device
  1353. #-- no, it is not
  1354. if ( defined($slave->{NEXTSEND})){
  1355. if( $now < $slave->{NEXTSEND} ){
  1356. #-- reschedule, if necessary
  1357. if( ($qlen==1) || (($qlen > 1) && ($queue->[1]{$hash} eq $slave)) ){
  1358. #Log 1, "OWX_PrQueue: device ".$slave->{NAME}." mindelay not over, rescheduling."
  1359. InternalTimer($now+$shortDelay, "OWX_PrQueue", "queue:$name", 0);
  1360. return;
  1361. #-- switch top positions of queue if possible
  1362. }else{
  1363. #Log 1, "OWX_PrQueue: exchanging top positions of queue";
  1364. my $dialogtop = shift(@{$queue});
  1365. my $dialogsecond = shift(@{$queue});
  1366. unshift( @{$queue}, $dialogtop);
  1367. unshift( @{$queue}, $dialogsecond);
  1368. }
  1369. }
  1370. }
  1371. }
  1372. #-- something to send, check min delay of 10 ms
  1373. if( $queue->[0]{data} ) {
  1374. $slave = $queue->[0]{hash}; # may be a different one than above
  1375. #-- no, it is not, reschedule
  1376. if ($hash->{LASTSEND} && $now < $hash->{LASTSEND} + 0.01) {
  1377. #-- List queue for debugging
  1378. #Log 1, "OWX_PrQueue: queue $name mindelay not over, rescheduling.";
  1379. # Log 1, " => ".$queue->[0]->{owx_dev}." context ".$queue->[0]->{context}." expecting ".$queue->[0]->{numread}." bytes, ".$queue->[0]->{status};
  1380. InternalTimer($now+$shortDelay, "OWX_PrQueue", "queue:$name", 0);
  1381. return;
  1382. }
  1383. #-- start write operation
  1384. $hash->{BUSY} = 1; # OWX queue is busy until response is received
  1385. $hash->{LASTSEND} = $now; # remember when last sent on this queue
  1386. #-- set delay for this device if necessary
  1387. if( defined($queue->[0]{delay}) ){
  1388. $slave->{NEXTSEND} = $now + $queue->[0]{delay};
  1389. }else{
  1390. delete $slave->{NEXTSEND};
  1391. }
  1392. # Log 1,"OWX_PrQueue: starting send-receive cycle, queue length $qlen. Setting entry to active";
  1393. # Log 1, " => ".$queue->[0]->{owx_dev}." context ".$queue->[0]->{context}." expecting ".$queue->[0]->{numread}." bytes, ".$queue->[0]->{status};
  1394. # Log 1,"----------------------------------------------";
  1395. #-- REALLY do it
  1396. #Log 1, "OWX_PrQueue: queue $name starting OWX_Write with context ".$queue->[0]->{context}
  1397. # if( $name eq "OWX_TEST");
  1398. $queue->[0]->{start} = $now;
  1399. OWX_Write($hash);
  1400. }
  1401. #-- schedule next processing
  1402. InternalTimer($now+$longDelay, "OWX_PrQueue", "queue:$name", 0);
  1403. return;
  1404. }
  1405. #######################################################################################
  1406. #
  1407. # OWX_WDBG - Write a debug message unconditionally
  1408. #
  1409. # Parameter $name= device name
  1410. # $msg = string message
  1411. # $bin = binary message
  1412. #
  1413. ########################################################################################
  1414. sub OWX_WDBG($$$) {
  1415. my ($name,$msg,$bin) = @_;
  1416. my ($i,$j,$k);
  1417. if( $bin ){
  1418. for($i=0;$i<length($bin);$i++){
  1419. $j=int(ord(substr($bin,$i,1))/16);
  1420. $k=ord(substr($bin,$i,1))%16;
  1421. $msg.=sprintf "0x%1x%1x ",$j,$k;
  1422. }
  1423. }
  1424. main::Log3($name, 1, $msg);
  1425. }
  1426. #######################################################################################
  1427. #
  1428. # OWX_WDBG - Write a debug message according to verbosity level
  1429. #
  1430. # Parameter $name= device name
  1431. # $lvl = verbosity level
  1432. # $msg = string message
  1433. # $bin = binary message
  1434. #
  1435. ########################################################################################
  1436. sub OWX_WDBGL($$$$) {
  1437. my ($name,$lvl,$msg,$bin) = @_;
  1438. if(defined($name) &&
  1439. defined($attr{$name}) &&
  1440. defined (my $devlevel = $attr{$name}{verbose})) {
  1441. return if($lvl > $devlevel);
  1442. } else {
  1443. return if($lvl > $attr{global}{verbose});
  1444. }
  1445. my ($i,$j,$k);
  1446. if( $bin ){
  1447. for($i=0;$i<length($bin);$i++){
  1448. $j=int(ord(substr($bin,$i,1))/16);
  1449. $k=ord(substr($bin,$i,1))%16;
  1450. $msg.=sprintf "0x%1x%1x ",$j,$k;
  1451. }
  1452. }
  1453. main::Log3($name, 1, $msg);
  1454. }
  1455. 1;
  1456. =pod
  1457. =item device
  1458. =item summary to commmunicate with 1-Wire bus devices
  1459. =item summary_DE zur Kommunikation mit 1-Wire Geräten
  1460. =begin html
  1461. <a name="OWX"></a>
  1462. <h3>OWX</h3>
  1463. <ul>
  1464. <p> Backend module to commmunicate with 1-Wire bus devices</p>
  1465. <ul>
  1466. <li>via an active DS2480/DS9097U bus master interface attached to an USB
  1467. port or </li>
  1468. <li>via an active DS2480 bus master interface attached to a TCP/IP-UART interface </li>
  1469. <li>via a network-attached CUNO or through a COC on the RaspBerry Pi</li>
  1470. <li>via an Arduino running OneWireFirmata</li>
  1471. </ul> Internally these interfaces are vastly different, read the corresponding <a
  1472. href="http://fhemwiki.de/wiki/Interfaces_f%C3%BCr_1-Wire"> Wiki pages </a>.
  1473. The passive DS9097 interface is no longer suppoorted.
  1474. <a name="OWXdefine"></a>
  1475. <h4>Define</h4>
  1476. <p>To define a 1-Wire interface to communicate with a 1-Wire bus, several possibilities exist:
  1477. <ul>
  1478. <li><code>define &lt;name&gt; OWX &lt;serial-device&gt;</code>, i.e. specify the serial device (e.g. USB port) to which the
  1479. 1-Wire bus is attached, for example<br/><code>define OWio1 OWX /dev/ttyUSB1</code></li>
  1480. <li><code>define &lt;name&gt; OWX &lt;tcpip&gt;[:&lt;port&gt;]</code>, i.e. specify the IP address and port to which the 1-Wire bus is attached. Attention: no socat program needed.
  1481. Example:<br/><code>define OWio1 OWX 192.168.0.1:23</code></li>
  1482. <li><code>define &lt;name&gt; OWX &lt;cuno/coc-device&gt;</code>, i.e. specify the previously defined COC/CUNO to which the 1-Wire bus
  1483. is attached, for example<br/><code>define OWio2 OWX COC</code></li>
  1484. <li><code>define &lt;name&gt; OWX &lt;firmata-device&gt;:&lt;firmata-pin&gt;</code>, i.e. specify the name and 1-Wire pin of the previously defined <a href="#FRM">FRM</a>
  1485. device to which the 1-Wire bus is attached, for example<br/><code>define OWio3 OWX FIRMATA:10</code></li>
  1486. </ul>
  1487. <a name="OWXset"></a>
  1488. <h4>Set</h4>
  1489. <ul>
  1490. <li><a name="owx_reopen">
  1491. <code>set &lt;name&gt; reopen</code>
  1492. </a>
  1493. <br />re-opens the interface and re-initializes the 1-Wire bus.
  1494. </li>
  1495. </ul>
  1496. <br />
  1497. <a name="OWXget"></a>
  1498. <h4>Get</h4>
  1499. <ul>
  1500. <li><a name="owx_alarms"></a>
  1501. <code>get &lt;name&gt; alarms</code>
  1502. <br />performs an "alarm search" for devices on the 1-Wire bus and, if found,
  1503. generates an event in the log (not with all interface types). </li>
  1504. <li><a name="owx_devices"></a>
  1505. <code>get &lt;name&gt; devices</code>
  1506. <br />redicovers all devices on the 1-Wire bus. If a device found has a
  1507. previous definition, this is automatically used. If a device is found but has no
  1508. definition, it is autocreated. If a defined device is not on the 1-Wire bus, it is
  1509. autodeleted. </li>
  1510. <li><a name="owx_version"></a>
  1511. <code>get &lt;name&gt; version</code>
  1512. <br />internal version number</li>
  1513. </ul>
  1514. <br />
  1515. <a name="OWXattr"></a>
  1516. <h4>Attributes</h4>
  1517. <ul>
  1518. <li><a name="OWXasynchronous"><code>attr &lt;name&gt; asynchronous 0(default)|1</code></a>
  1519. <br />if 1 the interface will run asynchronously;if 0 (default) then not</li>
  1520. <li><a name="OWXtimeout"><code>attr &lt;name&gt; timeout &lt;number&gt;</code></a>
  1521. <br />time in seconds waiting for response of any 1-Wire device, or 1-Wire interface,default 5 s</li>
  1522. <li><a name="OWXopendelay"><code>attr &lt;name&gt; opendelay &lt;number&gt; </code></a>
  1523. <br />time in seconds waiting until a reopen ist attempted, default 60 s</li>
  1524. <li><a name="OWXdokick"><code>attr &lt;name&gt; dokick 0|1</code></a>
  1525. <br />if 1, the interface regularly kicks thermometers on the bus to do a temperature conversion,
  1526. and to make an alarm check; if 0 (default) then not</li>
  1527. <li><a name="OWXinterval"><code>attr &lt;name&gt; interval &lt;number&gt;</code></a>
  1528. <br />time interval in seconds for kicking temperature sensors and checking for alarms, default 300 s</li>
  1529. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  1530. </ul>
  1531. </ul>
  1532. =end html
  1533. =begin html_DE
  1534. <a name="OWX"></a>
  1535. <h3>OWX</h3>
  1536. <ul>
  1537. <a href="http://fhemwiki.de/wiki/Interfaces_f%C3%BCr_1-Wire">Deutsche Dokumentation im Wiki</a> vorhanden, die englische Version gibt es hier: <a href="/fhem/docs/commandref.html#OWX">OWX</a>
  1538. </ul>
  1539. =end html_DE
  1540. =cut