00_OWX.pm 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585
  1. ########################################################################################
  2. #
  3. # OWX.pm
  4. #
  5. # FHEM module to commmunicate with 1-Wire bus devices
  6. # * via an active DS2480/DS2482/DS2490/DS9097U bus master interface attached to an USB port
  7. # * via a passive DS9097 interface attached to an USB port
  8. # * via a network-attached CUNO
  9. # * via a COC attached to a Raspberry Pi
  10. #
  11. # Prof. Dr. Peter A. Henning
  12. #
  13. # Contributions from: Martin Fischer, Rudolf König, Boris Neubert, Joachim Herold
  14. #
  15. # $Id: 00_OWX.pm 11098 2016-03-20 13:03:21Z pahenning $
  16. #
  17. ########################################################################################
  18. #
  19. # define <name> OWX <serial-device> for USB interfaces or
  20. # define <name> OWX <cuno/coc-device> for a CUNO or COC interface
  21. # define <name> OWX <arduino-pin> for a Arduino/Firmata (10_FRM.pm) interface
  22. #
  23. # where <name> may be replaced by any name string
  24. # <serial-device> is a serial (USB) device
  25. # <cuno/coc-device> is a CUNO or COC device
  26. #
  27. # get <name> alarms => find alarmed 1-Wire devices (not with CUNO)
  28. # get <name> devices => find all 1-Wire devices
  29. #
  30. # set <name> interval <seconds> => set period for temperature conversion and alarm testing
  31. # set <name> followAlarms on/off => determine whether an alarm is followed by a search for
  32. # alarmed devices
  33. #
  34. # attr <name> buspower real/parasitic - whether the 1-Wire bus is really powered or
  35. # the 1-Wire devices take their power from the data wire (parasitic is default !)
  36. #
  37. ########################################################################################
  38. #
  39. # This programm is free software; you can redistribute it and/or modify
  40. # it under the terms of the GNU General Public License as published by
  41. # the Free Software Foundation; either version 2 of the License, or
  42. # (at your option) any later version.
  43. #
  44. # The GNU General Public License can be found at
  45. # http://www.gnu.org/copyleft/gpl.html.
  46. # A copy is found in the textfile GPL.txt and important notices to the license
  47. # from the author is found in LICENSE.txt distributed with these scripts.
  48. #
  49. # This script is distributed in the hope that it will be useful,
  50. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. # GNU General Public License for more details.
  53. #
  54. ########################################################################################
  55. package main;
  56. use strict;
  57. use warnings;
  58. use vars qw{%owg_family %gets %sets $owx_version $owx_debug};
  59. #-- unfortunately some things OS-dependent
  60. my $owgdevregexp;
  61. if( $^O =~ /Win/ ) {
  62. require Win32::SerialPort;
  63. $owgdevregexp= "com";
  64. } else {
  65. require Device::SerialPort;
  66. $owgdevregexp= "/dev/";
  67. }
  68. use vars qw{%attr %defs};
  69. require "$attr{global}{modpath}/FHEM/DevIo.pm";
  70. sub Log($$);
  71. # Line counter
  72. my $cline=0;
  73. # These we may get on request
  74. my %gets = (
  75. "alarms" => "A",
  76. "devices" => "D"
  77. );
  78. # These occur in a pulldown menu as settable values for the bus master
  79. my %sets = (
  80. "interval" => "T",
  81. "followAlarms" => "F"
  82. );
  83. # These are attributes
  84. my %attrs = (
  85. );
  86. #-- some globals needed for the 1-Wire module
  87. #-- baud rate serial interface
  88. my $owx_baud=9600;
  89. my $owx_cmdlen;
  90. #-- Debugging 0,1,2,3
  91. $owx_debug=0;
  92. #-- 8 byte 1-Wire device address
  93. my @owx_ROM_ID =(0,0,0,0 ,0,0,0,0);
  94. #-- 16 byte search string
  95. my @owx_search=(0,0,0,0 ,0,0,0,0, 0,0,0,0, 0,0,0,0);
  96. #-- search state for 1-Wire bus search
  97. my $owx_LastDiscrepancy = 0;
  98. my $owx_LastFamilyDiscrepancy = 0;
  99. my $owx_LastDeviceFlag = 0;
  100. ########################################################################################
  101. #
  102. # The following subroutines are independent of the bus interface
  103. #
  104. ########################################################################################
  105. #
  106. # OWX_Initialize
  107. #
  108. # Parameter hash = hash of device addressed
  109. #
  110. ########################################################################################
  111. sub OWX_Initialize ($) {
  112. my ($hash) = @_;
  113. #-- Provider
  114. $hash->{Clients} = ":OWAD:OWCOUNT:OWID:OWLCD:OWMULTI:OWSWITCH:OWTHERM:OWVAR";
  115. #-- Normal Devices
  116. $hash->{DefFn} = "OWX_Define";
  117. $hash->{UndefFn} = "OWX_Undef";
  118. $hash->{GetFn} = "OWX_Get";
  119. $hash->{SetFn} = "OWX_Set";
  120. $hash->{NotifyFn} = "OWX_Notify";
  121. $hash->{AttrFn} = "OWX_Attr";
  122. $hash->{AttrList}= "loglevel:0,1,2,3,4,5,6 buspower:real,parasitic dokick:1,0 interval IODev";
  123. #-- Adapt to FRM
  124. $hash->{InitFn} = "FRM_OWX_Init";
  125. }
  126. ########################################################################################
  127. #
  128. # OWX_Define - Implements DefFn function
  129. #
  130. # Parameter hash = hash of device addressed, def = definition string
  131. #
  132. ########################################################################################
  133. sub OWX_Define ($$) {
  134. my ($hash, $def) = @_;
  135. my @a = split("[ \t][ \t]*", $def);
  136. my $owx_hwdevice;
  137. #-- check syntax
  138. if(int(@a) < 3){
  139. return "OWX: Syntax error - must be define <name> OWX"
  140. }
  141. #-- check syntax
  142. Log 1,"OWX: Warning - Some parameter(s) ignored, must be define <name> OWX <serial-device>|<cuno/coc-device>|<arduino-pin>"
  143. if(int(@a) > 3);
  144. #-- If this line contains 3 parameters, it is the bus master definition
  145. my $dev = $a[2];
  146. $hash->{NOTIFYDEV} = "global";
  147. #-- Dummy 1-Wire ROM identifier, empty device lists
  148. $hash->{ROM_ID} = "FF";
  149. $hash->{DEVS} = [];
  150. $hash->{ALARMDEVS} = [];
  151. #-- First step: check if we have a directly connected serial interface or a CUNO/COC attached
  152. if ( $dev =~ m|$owgdevregexp|i){
  153. #-- when the specified device name contains @<digits> already, use it as supplied
  154. if ( $dev !~ m/\@\d*/ ){
  155. $hash->{DeviceName} = $dev."\@9600";
  156. }
  157. #-- Second step in case of serial device: open the serial device to test it
  158. my $msg = "OWX: Serial device $dev";
  159. my $ret = DevIo_OpenDev($hash,0,undef);
  160. $owx_hwdevice = $hash->{USBDev};
  161. if(!defined($owx_hwdevice)){
  162. Log 1, $msg." not defined";
  163. return "OWX: Can't open serial device $dev: $!"
  164. } else {
  165. Log 1,$msg." defined";
  166. }
  167. $owx_hwdevice->reset_error();
  168. $owx_hwdevice->baudrate(9600);
  169. $owx_hwdevice->databits(8);
  170. $owx_hwdevice->parity('none');
  171. $owx_hwdevice->stopbits(1);
  172. $owx_hwdevice->handshake('none');
  173. $owx_hwdevice->write_settings;
  174. #-- store with OWX device
  175. $hash->{INTERFACE} = "serial";
  176. $hash->{HWDEVICE} = $owx_hwdevice;
  177. #-- check if we are connecting to Arduino (via FRM):
  178. } elsif ($dev =~ /^\d{1,2}$/) {
  179. $hash->{INTERFACE} = "firmata";
  180. main::LoadModule("FRM");
  181. FRM_Client_Define($hash,$def);
  182. } else {
  183. $hash->{DeviceName} = $dev;
  184. #-- Second step in case of CUNO: See if we can open it
  185. my $msg = "OWX: COC/CUNO device $dev";
  186. #-- hash des COC/CUNO
  187. $owx_hwdevice = $main::defs{$dev};
  188. if($owx_hwdevice){
  189. Log 1,$msg." defined";
  190. #-- store with OWX device
  191. $hash->{INTERFACE} = "COC/CUNO";
  192. $hash->{HWDEVICE} = $owx_hwdevice;
  193. #-- loop for some time until the state is "Initialized"
  194. for(my $i=0;$i<6;$i++){
  195. last if( $owx_hwdevice->{STATE} eq "Initialized");
  196. Log 1,"OWX: Waiting, at t=$i ".$dev." is still ".$owx_hwdevice->{STATE};
  197. select(undef,undef,undef,3);
  198. }
  199. Log 1, "OWX: Can't open ".$dev if( $owx_hwdevice->{STATE} ne "Initialized");
  200. #-- reset the 1-Wire system in COC/CUNO
  201. CUL_SimpleWrite($owx_hwdevice, "Oi");
  202. }else{
  203. Log 1, $msg." not defined";
  204. return $msg." not defined";
  205. }
  206. }
  207. if ($main::init_done) {
  208. return OWX_Start($hash);
  209. }
  210. }
  211. sub OWX_Start ($) {
  212. my ($hash) = @_;
  213. #-- Third step: see, if a bus interface is detected
  214. if (!OWX_Detect($hash)){
  215. $hash->{PRESENT} = 0;
  216. readingsSingleUpdate($hash,"state","failed",1);
  217. return undef;
  218. }
  219. #-- Fourth step: discovering devices on the bus
  220. # in 10 seconds discover all devices on the 1-Wire bus
  221. InternalTimer(gettimeofday()+10, "OWX_Discover", $hash,0);
  222. #-- Default settings
  223. $hash->{interval} = AttrVal($hash->{NAME},"interval",300); # kick every 5 minutes
  224. $hash->{followAlarms} = "off";
  225. $hash->{ALARMED} = "no";
  226. $hash->{PRESENT} = 1;
  227. readingsSingleUpdate($hash,"state","defined",1);
  228. #-- Intiate first alarm detection and eventually conversion in a minute or so
  229. InternalTimer(gettimeofday() + $hash->{interval}, "OWX_Kick", $hash,0);
  230. $hash->{STATE} = "Active";
  231. return undef;
  232. }
  233. sub OWX_Notify {
  234. my ($hash,$dev) = @_;
  235. my $name = $hash->{NAME};
  236. my $type = $hash->{TYPE};
  237. if( grep(m/^(INITIALIZED|REREADCFG)$/, @{$dev->{CHANGED}}) ) {
  238. OWX_Start($hash);
  239. } elsif( grep(m/^SAVE$/, @{$dev->{CHANGED}}) ) {
  240. }
  241. }
  242. #######################################################################################
  243. #
  244. # OWTX_Attr - Set one attribute value for device
  245. #
  246. # Parameter hash = hash of device addressed
  247. # a = argument array
  248. #
  249. ########################################################################################
  250. sub OWX_Attr(@) {
  251. my ($do,$name,$key,$value) = @_;
  252. my $hash = $defs{$name};
  253. my $ret;
  254. if ( $do eq "set") {
  255. ARGUMENT_HANDLER: {
  256. $key eq "buspower" and do {
  257. #-- check value
  258. $attr{$hash->{NAME}}{dokick} = $value eq "real" ? 1 : 0;
  259. last;
  260. };
  261. $key eq "interval" and do {
  262. $hash->{interval} = $value;
  263. if ($main::init_done) {
  264. OWX_Kick($hash);
  265. }
  266. last;
  267. };
  268. }
  269. }
  270. return $ret;
  271. }
  272. ########################################################################################
  273. #
  274. # OWX_Alarms - Find devices on the 1-Wire bus,
  275. # which have the alarm flag set
  276. #
  277. # Parameter hash = hash of bus master
  278. #
  279. # Return 1 : OK
  280. # 0 : no device present
  281. #
  282. ########################################################################################
  283. sub OWX_Alarms ($) {
  284. my ($hash) = @_;
  285. my $name = $hash->{NAME};
  286. my @owx_alarm_names=();
  287. $hash->{ALARMDEVS}=[];
  288. if ($hash->{INTERFACE} eq "firmata") {
  289. FRM_OWX_Alarms($hash);
  290. } else {
  291. #-- Discover all alarmed devices on the 1-Wire bus
  292. my $res = OWX_First_SER($hash,"alarm");
  293. while( $owx_LastDeviceFlag==0 && $res != 0){
  294. $res = $res & OWX_Next_SER($hash,"alarm");
  295. }
  296. }
  297. if( @{$hash->{ALARMDEVS}} == 0){
  298. return "OWX: No alarmed 1-Wire devices found on bus $name";
  299. }
  300. #-- walk through all the devices to get their proper fhem names
  301. foreach my $fhem_dev (sort keys %main::defs) {
  302. #-- skip if busmaster
  303. next if( $name eq $main::defs{$fhem_dev}{NAME} );
  304. #-- all OW types start with OW
  305. next if( substr($main::defs{$fhem_dev}{TYPE},0,2) ne "OW");
  306. foreach my $owx_dev (@{$hash->{ALARMDEVS}}) {
  307. #-- two pieces of the ROM ID found on the bus
  308. my $owx_rnf = substr($owx_dev,3,12);
  309. my $owx_f = substr($owx_dev,0,2);
  310. my $id_owx = $owx_f.".".$owx_rnf;
  311. #-- skip if not in alarm list
  312. if( $owx_dev eq $main::defs{$fhem_dev}{ROM_ID} ){
  313. $main::defs{$fhem_dev}{STATE} = "Alarmed";
  314. push(@owx_alarm_names,$main::defs{$fhem_dev}{NAME});
  315. }
  316. }
  317. }
  318. #-- so far, so good - what do we want to do with this ?
  319. return "OWX: Alarmed 1-Wire devices found on bus $name (".join(",",@owx_alarm_names).")";
  320. }
  321. ########################################################################################
  322. #
  323. # OWX_Complex - Send match ROM, data block and receive bytes as response
  324. #
  325. # Parameter hash = hash of bus master,
  326. # owx_dev = ROM ID of device
  327. # data = string to send
  328. # numread = number of bytes to receive
  329. #
  330. # Return response, if OK
  331. # 0 if not OK
  332. #
  333. ########################################################################################
  334. sub OWX_Complex ($$$$) {
  335. my ($hash,$owx_dev,$data,$numread) =@_;
  336. my $name = $hash->{NAME};
  337. #-- get the interface
  338. my $owx_interface = $hash->{INTERFACE};
  339. my $owx_hwdevice = $hash->{HWDEVICE};
  340. #-- interface error
  341. if( !(defined($owx_interface))){
  342. Log 3,"OWX: Complex called with undefined interface";
  343. return 0;
  344. #-- here we treat the directly connected serial interfaces
  345. }elsif( ($owx_interface eq "DS2480") || ($owx_interface eq "DS9097") ){
  346. return OWX_Complex_SER($hash,$owx_dev,$data,$numread);
  347. #-- here we treat the CUNO/COC devices
  348. }elsif( ($owx_interface eq "COC") || ($owx_interface eq "CUNO") ){
  349. return OWX_Complex_CCC($hash,$owx_dev,$data,$numread);
  350. #-- here we treat Arduino/Firmata devices
  351. }elsif( $owx_interface eq "firmata" ) {
  352. return FRM_OWX_Complex( $hash, $owx_dev, $data, $numread );
  353. #-- interface error
  354. }else{
  355. Log 3,"OWX: Complex called with unknown interface $owx_interface on bus $name";
  356. return 0;
  357. }
  358. }
  359. ########################################################################################
  360. #
  361. # OWX_CRC - Check the CRC8 code of a device address in @owx_ROM_ID
  362. #
  363. # Parameter romid = if not zero, return the CRC8 value instead of checking it
  364. #
  365. ########################################################################################
  366. my @crc8_table = (
  367. 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
  368. 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
  369. 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
  370. 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
  371. 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
  372. 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
  373. 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
  374. 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
  375. 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
  376. 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
  377. 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
  378. 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
  379. 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
  380. 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
  381. 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
  382. 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53);
  383. sub OWX_CRC ($) {
  384. my ($romid) = @_;
  385. my $crc8=0;
  386. if( $romid eq "0" ){
  387. for(my $i=0; $i<8; $i++){
  388. $crc8 = $crc8_table[ $crc8 ^ $owx_ROM_ID[$i] ];
  389. }
  390. } elsif (ref($romid) eq "ARRAY") {
  391. for(my $i=0; $i<8; $i++){
  392. $crc8 = $crc8_table[ $crc8 ^ $romid->[$i] ];
  393. }
  394. } else {
  395. #-- from search string to byte id
  396. $romid=~s/\.//g;
  397. for(my $i=0;$i<8;$i++){
  398. $owx_ROM_ID[$i]=hex(substr($romid,2*$i,2));
  399. }
  400. for(my $i=0; $i<7; $i++){
  401. $crc8 = $crc8_table[ $crc8 ^ $owx_ROM_ID[$i] ];
  402. }
  403. }
  404. return $crc8;
  405. }
  406. ########################################################################################
  407. #
  408. # OWX_CRC - Check the CRC8 code of an a byte string
  409. #
  410. # Parameter string, crc.
  411. # If crc is defined, make a comparison, otherwise output crc8
  412. #
  413. ########################################################################################
  414. sub OWX_CRC8 ($$) {
  415. my ($string,$crc) = @_;
  416. my $crc8=0;
  417. my @strhex;
  418. #Log 1,"CRC8 calculated for string of length ".length($string);
  419. for(my $i=0; $i<length($string); $i++){
  420. $strhex[$i]=ord(substr($string,$i,1));
  421. $crc8 = $crc8_table[ $crc8 ^ $strhex[$i] ];
  422. }
  423. if( defined($crc) ){
  424. my $crcx = ord($crc);
  425. if ( $crcx == $crc8 ){
  426. return 1;
  427. }else{
  428. return 0;
  429. }
  430. }else{
  431. #Log 1,"Returning $crc8";
  432. return $crc8;
  433. }
  434. }
  435. ########################################################################################
  436. #
  437. # OWX_CRC16 - Calculate the CRC16 code of a string
  438. #
  439. # TODO UNFINISHED CODE
  440. #
  441. # Parameter crc - previous CRC code, c next character
  442. #
  443. ########################################################################################
  444. my @crc16_table = (
  445. 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
  446. 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
  447. 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
  448. 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
  449. 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
  450. 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
  451. 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
  452. 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
  453. 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
  454. 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
  455. 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
  456. 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
  457. 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
  458. 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
  459. 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
  460. 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
  461. 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
  462. 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
  463. 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
  464. 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
  465. 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
  466. 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
  467. 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
  468. 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
  469. 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
  470. 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
  471. 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
  472. 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
  473. 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
  474. 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
  475. 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
  476. 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
  477. );
  478. sub OWX_CRC16 ($$$) {
  479. my ($string,$crclo,$crchi) = @_;
  480. my $crc16=0;
  481. my @strhex;
  482. #Log 1,"CRC16 calculated for string of length ".length($string);
  483. for(my $i=0; $i<length($string); $i++){
  484. $strhex[$i]=ord(substr($string,$i,1));
  485. $crc16 = $crc16_table[ ($crc16 ^ $strhex[$i]) & 0xFF ] ^ ($crc16 >> 8);
  486. }
  487. if( defined($crclo) & defined($crchi) ){
  488. my $crc = (255-ord($crclo))+256*(255-ord($crchi));
  489. if ($crc == $crc16 ){
  490. return 1;
  491. }else{
  492. return 0;
  493. }
  494. }else{
  495. return $crc16;
  496. }
  497. }
  498. ########################################################################################
  499. #
  500. # OWX_Detect - Detect 1-Wire interface
  501. # TODO: HAS TO BE SPLIT INTO INTERFACE DEPENDENT AND INDEPENDENT PART
  502. #
  503. # Method rather crude - treated as an 2480, and see whatis returned
  504. #
  505. # Parameter hash = hash of bus master
  506. #
  507. # Return 1 : OK
  508. # 0 : not OK
  509. #
  510. ########################################################################################
  511. sub OWX_Detect ($) {
  512. my ($hash) = @_;
  513. my ($i,$j,$k,$l,$res,$ret,$ress);
  514. my $name = $hash->{NAME};
  515. my $ress0 = "OWX: 1-Wire bus $name: interface ";
  516. $ress = $ress0;
  517. #-- get the interface
  518. my $owx_interface = $hash->{INTERFACE};
  519. my $owx_hwdevice = $hash->{HWDEVICE};
  520. #-- here we treat the directly connected serial interfaces
  521. if($owx_interface eq "serial"){
  522. #-- timing byte for DS2480
  523. OWX_Query_2480($hash,"\xC1",1);
  524. #-- Max 4 tries to detect an interface
  525. for($l=0;$l<100;$l++) {
  526. #-- write 1-Wire bus (Fig. 2 of Maxim AN192)
  527. $res = OWX_Query_2480($hash,"\x17\x45\x5B\x0F\x91",5);
  528. #-- process 4/5-byte string for detection
  529. if( !defined($res)){
  530. $res="";
  531. $ret=0;
  532. }elsif( ($res eq "\x16\x44\x5A\x00\x90") || ($res eq "\x16\x44\x5A\x00\x93")){
  533. $ress .= "master DS2480 detected for the first time";
  534. $owx_interface="DS2480";
  535. $ret=1;
  536. } elsif( $res eq "\x17\x45\x5B\x0F\x91"){
  537. $ress .= "master DS2480 re-detected";
  538. $owx_interface="DS2480";
  539. $ret=1;
  540. } elsif( ($res eq "\x17\x0A\x5B\x0F\x02") || ($res eq "\x00\x17\x0A\x5B\x0F\x02") || ($res eq "\x30\xf8\x00") || ($res eq "\x06\x00\x09\x07\x80") || ($res eq "\x17\x41\xAB\x20\xFC")){
  541. $ress .= "passive DS9097 detected";
  542. $owx_interface="DS9097";
  543. $ret=1;
  544. } else {
  545. $ret=0;
  546. }
  547. last
  548. if( $ret==1 );
  549. $ress .= "not found, answer was ";
  550. for($i=0;$i<length($res);$i++){
  551. $j=int(ord(substr($res,$i,1))/16);
  552. $k=ord(substr($res,$i,1))%16;
  553. $ress.=sprintf "0x%1x%1x ",$j,$k;
  554. }
  555. Log 1, $ress;
  556. $ress = $ress0;
  557. #-- sleeping for some time
  558. select(undef,undef,undef,0.5);
  559. }
  560. if( $ret == 0 ){
  561. $owx_interface=undef;
  562. $ress .= "not detected, answer was ";
  563. for($i=0;$i<length($res);$i++){
  564. $j=int(ord(substr($res,$i,1))/16);
  565. $k=ord(substr($res,$i,1))%16;
  566. $ress.=sprintf "0x%1x%1x ",$j,$k;
  567. }
  568. }
  569. #-- nothing to do for Arduino (already done in FRM)
  570. } elsif($owx_interface eq "firmata") {
  571. eval {
  572. FRM_Client_AssignIOPort($hash);
  573. if (defined $hash->{IODev}) {
  574. $ret=1;
  575. $ress .= "Firmata detected in $hash->{IODev}->{NAME}";
  576. } else {
  577. $ret = 0;
  578. $ress .= "not associated to any FRM device";
  579. }
  580. };
  581. if ($@) {
  582. $ress .= FRM_Catch($@);
  583. $ret = 0;
  584. }
  585. #-- here we treat the COC/CUNO
  586. } else {
  587. select(undef,undef,undef,2);
  588. #-- type of interface
  589. CUL_SimpleWrite($owx_hwdevice, "V");
  590. select(undef,undef,undef,0.01);
  591. my ($err,$ob) = OWX_ReadAnswer_CCC($owx_hwdevice);
  592. #my $ob = CallFn($owx_hwdevice->{NAME}, "GetFn", $owx_hwdevice, (" ", "raw", "V"));
  593. #-- process result for detection
  594. if( !defined($ob)){
  595. $ob="";
  596. $ret=0;
  597. #-- COC
  598. }elsif( $ob =~ m/.*CSM.*/){
  599. $owx_interface="COC";
  600. $ress .= "DS2482 / COC detected in $owx_hwdevice->{NAME} with response $ob";
  601. $ret=1;
  602. #-- CUNO
  603. }elsif( $ob =~ m/.*CUNO.*/){
  604. $owx_interface="CUNO";
  605. $ress .= "DS2482 / CUNO detected in $owx_hwdevice->{NAME} with response $ob";
  606. $ret=1;
  607. #-- something else
  608. } else {
  609. $ret=0;
  610. }
  611. #-- treat the failure cases
  612. if( $ret == 0 ){
  613. $owx_interface=undef;
  614. $ress .= "in $owx_hwdevice->{NAME} could not be addressed, return was $ob";
  615. }
  616. }
  617. #-- store with OWX device
  618. $hash->{INTERFACE} = $owx_interface;
  619. Log3 $hash->{NAME}, 1, $ress;
  620. return $ret;
  621. }
  622. ########################################################################################
  623. #
  624. # OWX_Discover - Discover devices on the 1-Wire bus,
  625. # autocreate devices if not already present
  626. #
  627. # Parameter hash = hash of bus master
  628. #
  629. # Return: List of devices in table format or undef
  630. #
  631. ########################################################################################
  632. sub OWX_Discover ($) {
  633. my ($hash) = @_;
  634. my $res;
  635. my $ret= "";
  636. my $name = $hash->{NAME};
  637. my $exname;
  638. my $acname;
  639. #-- get the interface
  640. my $owx_interface = $hash->{INTERFACE};
  641. my $owx_hwdevice = $hash->{HWDEVICE};
  642. my @owx_names=();
  643. #-- Discover all devices on the 1-Wire bus, they will be found in $hash->{DEVS}
  644. return undef
  645. if( !defined($owx_interface) );
  646. #-- Directly connected serial interface
  647. if( ($owx_interface eq "DS2480") || ($owx_interface eq "DS9097") ){
  648. $res = OWX_First_SER($hash,"discover");
  649. while( $owx_LastDeviceFlag==0 && $res!=0 ){
  650. $res = $res & OWX_Next_SER($hash,"discover");
  651. }
  652. #-- Ask the COC/CUNO
  653. }elsif( ($owx_interface eq "COC" ) || ($owx_interface eq "CUNO") ){
  654. $res = OWX_Discover_CCC($hash);
  655. #-- ask the Arduino
  656. }elsif ( $owx_interface eq "firmata") {
  657. $res = FRM_OWX_Discover($hash);
  658. #-- Something else
  659. } else {
  660. Log 1,"OWX: Discover called with unknown interface";
  661. return undef;
  662. }
  663. #-- Go through all devices found on this bus
  664. foreach my $owx_dev (@{$hash->{DEVS}}) {
  665. #-- ignore those which do not have the proper pattern
  666. if( !($owx_dev =~ m/[0-9A-F]{2}\.[0-9A-F]{12}\.[0-9A-F]{2}/) ){
  667. Log 3,"OWX: Invalid 1-Wire device ID $owx_dev, ignoring it";
  668. next;
  669. }
  670. #-- three pieces of the ROM ID found on the bus
  671. my $owx_rnf = substr($owx_dev,3,12);
  672. my $owx_f = substr($owx_dev,0,2);
  673. my $owx_crc = substr($owx_dev,15,3);
  674. my $id_owx = $owx_f.".".$owx_rnf;
  675. my $match = 0;
  676. #-- Check against all existing devices
  677. foreach my $fhem_dev (sort keys %main::defs) {
  678. #-- skip if busmaster
  679. # next if( $hash->{NAME} eq $main::defs{$fhem_dev}{NAME} );
  680. #-- all OW types start with OW
  681. next if( !defined($main::defs{$fhem_dev}{TYPE}));
  682. next if( substr($main::defs{$fhem_dev}{TYPE},0,2) ne "OW");
  683. my $id_fhem = substr($main::defs{$fhem_dev}{ROM_ID},0,15);
  684. #-- skip interface device
  685. next if( length($id_fhem) != 15 );
  686. #-- testing if equal to the one found here
  687. # even with improper family
  688. # Log 1, " FHEM-Device = ".substr($id_fhem,3,12)." OWX discovered device ".substr($id_owx,3,12);
  689. if( substr($id_fhem,3,12) eq substr($id_owx,3,12) ) {
  690. #-- warn if improper family id
  691. if( substr($id_fhem,0,2) ne substr($id_owx,0,2) ){
  692. Log 1, "OWX: Warning, $fhem_dev is defined with improper family id ".substr($id_fhem,0,2).
  693. ", must enter correct model in configuration";
  694. #$main::defs{$fhem_dev}{OW_FAMILY} = substr($id_owx,0,2);
  695. }
  696. $exname=$main::defs{$fhem_dev}{NAME};
  697. push(@owx_names,$exname);
  698. #-- replace the ROM ID by the proper value including CRC
  699. $main::defs{$fhem_dev}{ROM_ID}=$owx_dev;
  700. $main::defs{$fhem_dev}{PRESENT}=1;
  701. $match = 1;
  702. last;
  703. }
  704. #
  705. }
  706. #-- Determine the device type. This is done manually here
  707. # could be automatic as in OWServer
  708. my $acstring;
  709. my $chip;
  710. #-- Family 10 = Temperature sensor DS1820
  711. if( $owx_f eq "10" ){
  712. $chip = "DS1820";
  713. $acstring = "OWTHERM DS1820";
  714. #-- Family 12 = Switch DS2406
  715. }elsif( $owx_f eq "12" ){
  716. $chip = "DS2406";
  717. $acstring = "OWSWITCH DS2406";
  718. #-- Family 1D = Counter/RAM DS2423
  719. }elsif( $owx_f eq "1D" ){
  720. $chip = "DS2423";
  721. $acstring = "OWCOUNT DS2423";
  722. #-- Family 20 = A/D converter DS2450
  723. } elsif( $owx_f eq "20" ){
  724. $chip = "DS2450";
  725. $acstring = "OWAD DS2450";
  726. #-- Family 22 = Temperature sensor DS1822
  727. }elsif( $owx_f eq "22" ){
  728. $chip = "DS1822";
  729. $acstring = "OWTHERM DS1822";
  730. #-- Family 26 = Multisensor DS2438
  731. }elsif( $owx_f eq "26" ){
  732. $chip = "DS2438";
  733. $acstring = "OWMULTI DS2438";
  734. #-- Family 28 = Temperature sensor DS18B20
  735. }elsif( $owx_f eq "28" ){
  736. $chip = "DS18B20";
  737. $acstring = "OWTHERM DS18B20";
  738. #-- Family 29 = Switch DS2408
  739. }elsif( $owx_f eq "29" ){
  740. $chip = "DS2408";
  741. $acstring = "OWSWITCH DS2408";
  742. #-- Family 3A = Switch DS2413
  743. }elsif( $owx_f eq "3A" ){
  744. $chip = "DS2413";
  745. $acstring = "OWSWITCH DS2413";
  746. #-- Family FF = LCD display
  747. }elsif( $owx_f eq "FF" ){
  748. $chip = "LCD";
  749. $acstring = "OWLCD";
  750. #-- All unknown families are ID only (ID-Chips have family id 09)
  751. } else {
  752. $chip = "unknown";
  753. $acstring = "OWID $owx_f";
  754. }
  755. #Log 1,"###\nfor the following device match=$match, chip=$chip name=$name acstring=$acstring";
  756. #-- device exists
  757. if( $match==1 ){
  758. $ret .= sprintf("%s.%s %-10s %s\n", $owx_f,$owx_rnf, $chip, $exname);
  759. #-- device unknoen, autocreate
  760. }else{
  761. #-- example code for checking global autocreate - do we want this ?
  762. #foreach my $d (keys %defs) {
  763. #next if($defs{$d}{TYPE} ne "autocreate");
  764. #return undef if(AttrVal($defs{$d}{NAME},"disable",undef));
  765. my $acname = sprintf "OWX_%s_%s",$owx_f,$owx_rnf;
  766. #Log 1, "to define $acname $acstring $owx_rnf";
  767. $res = CommandDefine(undef,"$acname $acstring $owx_rnf");
  768. if($res) {
  769. $ret.= "OWX: Error autocreating with $acname $acstring $owx_rnf: $res\n";
  770. } else{
  771. select(undef,undef,undef,0.1);
  772. push(@owx_names,$acname);
  773. $main::defs{$acname}{PRESENT}=1;
  774. #-- THIS IODev, default room (model is set in the device module)
  775. CommandAttr (undef,"$acname IODev $hash->{NAME}");
  776. CommandAttr (undef,"$acname room OWX");
  777. #-- replace the ROM ID by the proper value
  778. $main::defs{$acname}{ROM_ID}=$owx_dev;
  779. $ret .= sprintf("%s.%s %-10s %s\n", $owx_f,$owx_rnf, $chip, $acname);
  780. }
  781. }
  782. }
  783. #-- final step: Undefine all 1-Wire devices which
  784. # are autocreated and
  785. # not discovered on this bus
  786. # but have this IODev
  787. foreach my $fhem_dev (sort keys %main::defs) {
  788. #-- skip if malformed device
  789. #next if( !defined($main::defs{$fhem_dev}{NAME}) );
  790. #-- all OW types start with OW, but safeguard against deletion of other devices
  791. #next if( !defined($main::defs{$fhem_dev}{TYPE}));
  792. next if( substr($main::defs{$fhem_dev}{TYPE},0,2) ne "OW");
  793. next if( uc($main::defs{$fhem_dev}{TYPE}) eq "OWX");
  794. next if( uc($main::defs{$fhem_dev}{TYPE}) eq "OWFS");
  795. next if( uc($main::defs{$fhem_dev}{TYPE}) eq "OWSERVER");
  796. next if( uc($main::defs{$fhem_dev}{TYPE}) eq "OWDEVICE");
  797. #-- restrict to autocreated devices
  798. next if( $main::defs{$fhem_dev}{NAME} !~ m/OWX_[0-9a-fA-F]{2}_/);
  799. #-- skip if the device is present.
  800. next if( $main::defs{$fhem_dev}{PRESENT} == 1);
  801. #-- skip if different IODev, but only if other IODev exists
  802. if ( $main::defs{$fhem_dev}{IODev} ){
  803. next if( $main::defs{$fhem_dev}{IODev}{NAME} ne $hash->{NAME} );
  804. }
  805. Log 1, "OWX: Deleting unused 1-Wire device $main::defs{$fhem_dev}{NAME} of type $main::defs{$fhem_dev}{TYPE}";
  806. CommandDelete(undef,$main::defs{$fhem_dev}{NAME});
  807. #Log 1, "present= ".$main::defs{$fhem_dev}{PRESENT}." iodev=".$main::defs{$fhem_dev}{IODev}{NAME};
  808. }
  809. #-- Log the discovered devices
  810. Log 1, "OWX: 1-Wire devices found on bus $name (".join(",",@owx_names).")";
  811. #-- tabular view as return value
  812. return "OWX: 1-Wire devices found on bus $name \n".$ret;
  813. }
  814. ########################################################################################
  815. #
  816. # OWX_Get - Implements GetFn function
  817. #
  818. # Parameter hash = hash of the bus master a = argument array
  819. #
  820. ########################################################################################
  821. sub OWX_Get($@) {
  822. my ($hash, @a) = @_;
  823. return "OWX: Get needs exactly one parameter" if(@a != 2);
  824. my $name = $hash->{NAME};
  825. my $owx_dev = $hash->{ROM_ID};
  826. if( $a[1] eq "alarms") {
  827. my $res = OWX_Alarms($hash);
  828. #-- process result
  829. return $res
  830. } elsif( $a[1] eq "devices") {
  831. my $res = OWX_Discover($hash);
  832. #-- process result
  833. return $res
  834. } else {
  835. return "OWX: Get with unknown argument $a[1], choose one of ".
  836. join(" ", sort keys %gets);
  837. }
  838. }
  839. ########################################################################################
  840. #
  841. # OWX_Kick - Initiate some processes in all devices
  842. #
  843. # Parameter hash = hash of bus master
  844. #
  845. # Return 1 : OK
  846. # 0 : Not OK
  847. #
  848. ########################################################################################
  849. sub OWX_Kick($) {
  850. my($hash) = @_;
  851. my $ret;
  852. #-- Call us in n seconds again.
  853. InternalTimer(gettimeofday()+ $hash->{interval}, "OWX_Kick", $hash,0);
  854. #-- During reset we see if an alarmed device is present.
  855. OWX_Reset($hash);
  856. #-- Only if we have real power on the bus
  857. if( AttrVal($hash->{NAME},"dokick",0) ) {
  858. #-- issue the skip ROM command \xCC followed by start conversion command \x44
  859. $ret = OWX_Complex($hash,"","\xCC\x44",0);
  860. if( $ret eq 0 ){
  861. Log 3, "OWX: Failure in temperature conversion\n";
  862. return 0;
  863. }
  864. #-- sleeping for some time
  865. select(undef,undef,undef,0.5);
  866. }
  867. return 1;
  868. }
  869. ########################################################################################
  870. #
  871. # OWX_Reset - Reset the 1-Wire bus
  872. #
  873. # Parameter hash = hash of bus master
  874. #
  875. # Return 1 : OK
  876. # 0 : not OK
  877. #
  878. ########################################################################################
  879. sub OWX_Reset ($) {
  880. my ($hash)=@_;
  881. #-- get the interface
  882. my $owx_interface = $hash->{INTERFACE};
  883. my $owx_hwdevice = $hash->{HWDEVICE};
  884. #-- interface error
  885. if( !(defined($owx_interface))){
  886. Log 3,"OWX: Reset called with undefined interface";
  887. return 0;
  888. }elsif( $owx_interface eq "DS2480" ){
  889. return OWX_Reset_2480($hash);
  890. }elsif( $owx_interface eq "DS9097" ){
  891. return OWX_Reset_9097($hash);
  892. }elsif( $owx_interface eq "COC" ){
  893. return OWX_Reset_CCC($hash);
  894. }elsif( $owx_interface eq "CUNO" ){
  895. return OWX_Reset_CCC($hash);
  896. }elsif( $owx_interface eq "firmata" ) {
  897. return FRM_OWX_Reset($hash);
  898. }else{
  899. Log 3,"OWX: Reset called with unknown interface $owx_interface";
  900. return 0;
  901. }
  902. }
  903. ########################################################################################
  904. #
  905. # OWX_Set - Implements SetFn function
  906. #
  907. # Parameter hash , a = argument array
  908. #
  909. ########################################################################################
  910. sub OWX_Set($@) {
  911. my ($hash, @a) = @_;
  912. my $name = shift @a;
  913. my $res;
  914. #-- First we need to find the ROM ID corresponding to the device name
  915. my $owx_romid = $hash->{ROM_ID};
  916. Log 5, "OWX_Set request $name $owx_romid ".join(" ",@a);
  917. #-- for the selector: which values are possible
  918. return join(" ", sort keys %sets) if(@a != 2);
  919. return "OWX_Set: With unknown argument $a[0], choose one of " . join(" ", sort keys %sets)
  920. if(!defined($sets{$a[0]}));
  921. #-- Set timer value
  922. if( $a[0] eq "interval" ){
  923. #-- only values >= 15 secs allowed
  924. if( $a[1] >= 15){
  925. $hash->{interval} = $a[1];
  926. $res = 1;
  927. } else {
  928. $res = 0;
  929. }
  930. }
  931. #-- Set alarm behaviour
  932. if( $a[0] eq "followAlarms" ){
  933. #-- only values >= 15 secs allowed
  934. if( (lc($a[1]) eq "off") && ($hash->{followAlarms} eq "on") ){
  935. $hash->{interval} = "off";
  936. $res = 1;
  937. }elsif( (lc($a[1]) eq "on") && ($hash->{followAlarms} eq "off") ){
  938. $hash->{interval} = "off";
  939. $res = 1;
  940. } else {
  941. $res = 0;
  942. }
  943. }
  944. Log GetLogLevel($name,3), "OWX_Set $name ".join(" ",@a)." => $res";
  945. DoTrigger($name, undef) if($main::init_done);
  946. return "OWX_Set => $name ".join(" ",@a)." => $res";
  947. }
  948. ########################################################################################
  949. #
  950. # OWX_Undef - Implements UndefFn function
  951. #
  952. # Parameter hash = hash of the bus master, name
  953. #
  954. ########################################################################################
  955. sub OWX_Undef ($$) {
  956. my ($hash, $name) = @_;
  957. RemoveInternalTimer($hash);
  958. DevIo_CloseDev($hash);
  959. return undef;
  960. }
  961. ########################################################################################
  962. #
  963. # OWX_Verify - Verify a particular device on the 1-Wire bus
  964. #
  965. # Parameter hash = hash of bus master, dev = 8 Byte ROM ID of device to be tested
  966. #
  967. # Return 1 : device found
  968. # 0 : device not found
  969. #
  970. ########################################################################################
  971. sub OWX_Verify ($$) {
  972. my ($hash,$dev) = @_;
  973. my $i;
  974. #-- get the interface
  975. my $owx_interface = $hash->{INTERFACE};
  976. #-- Verify this device on the 1-Wire bus
  977. return 0
  978. if( !defined($owx_interface) );
  979. #-- Directly connected interface
  980. if( ($owx_interface eq "DS2480") || ($owx_interface eq "DS9097") ){
  981. return OWX_Verify_SER($hash,$dev)
  982. #-- Ask the COC/CUNO
  983. }elsif( ($owx_interface eq "COC" ) || ($owx_interface eq "CUNO") ){
  984. return OWX_Verify_CCC($hash,$dev)
  985. }elsif( $owx_interface eq "firmata" ){
  986. return FRM_OWX_Verify($hash,$dev);
  987. } else {
  988. Log 1,"OWX: Verify called with unknown interface";
  989. return 0;
  990. }
  991. }
  992. #######################################################################################
  993. #
  994. # OWX_WDBG - Write a debug message
  995. #
  996. # Parameter $name= device name
  997. # $msg = string message
  998. # $bin = binary message
  999. #
  1000. ########################################################################################
  1001. sub OWX_WDBG($$$) {
  1002. my ($name,$msg,$bin) = @_;
  1003. my ($i,$j,$k);
  1004. if( $bin ){
  1005. for($i=0;$i<length($bin);$i++){
  1006. $j=int(ord(substr($bin,$i,1))/16);
  1007. $k=ord(substr($bin,$i,1))%16;
  1008. $msg.=sprintf "0x%1x%1x ",$j,$k;
  1009. }
  1010. }
  1011. main::Log3($name, 1, $msg);
  1012. }
  1013. ########################################################################################
  1014. #
  1015. # The following subroutines in alphabetical order are only for direct serial bus interface
  1016. #
  1017. ########################################################################################
  1018. #
  1019. # OWX_Complex_SER - Send match ROM, data block and receive bytes as response
  1020. #
  1021. # Parameter hash = hash of bus master,
  1022. # owx_dev = ROM ID of device
  1023. # data = string to send
  1024. # numread = number of bytes to receive
  1025. #
  1026. # Return response, if OK
  1027. # 0 if not OK
  1028. #
  1029. ########################################################################################
  1030. sub OWX_Complex_SER ($$$$) {
  1031. my ($hash,$owx_dev,$data,$numread) =@_;
  1032. my $select;
  1033. my $res = "";
  1034. my $res2 = "";
  1035. my ($i,$j,$k);
  1036. #-- get the interface
  1037. my $owx_interface = $hash->{INTERFACE};
  1038. my $owx_hwdevice = $hash->{HWDEVICE};
  1039. #-- has match ROM part
  1040. if( $owx_dev ){
  1041. #-- ID of the device
  1042. my $owx_rnf = substr($owx_dev,3,12);
  1043. my $owx_f = substr($owx_dev,0,2);
  1044. #-- 8 byte 1-Wire device address
  1045. my @owx_ROM_ID =(0,0,0,0 ,0,0,0,0);
  1046. #-- from search string to byte id
  1047. $owx_dev=~s/\.//g;
  1048. for(my $i=0;$i<8;$i++){
  1049. $owx_ROM_ID[$i]=hex(substr($owx_dev,2*$i,2));
  1050. }
  1051. $select=sprintf("\x55%c%c%c%c%c%c%c%c",@owx_ROM_ID).$data;
  1052. #-- has no match ROM part
  1053. } else {
  1054. $select=$data;
  1055. }
  1056. #-- has receive data part
  1057. if( $numread >0 ){
  1058. #$numread += length($data);
  1059. for( my $i=0;$i<$numread;$i++){
  1060. $select .= "\xFF";
  1061. };
  1062. }
  1063. #-- for debugging
  1064. if( $owx_debug > 1){
  1065. $res2 = "OWX_Complex_SER: Sending out ";
  1066. for($i=0;$i<length($select);$i++){
  1067. $j=int(ord(substr($select,$i,1))/16);
  1068. $k=ord(substr($select,$i,1))%16;
  1069. $res2.=sprintf "0x%1x%1x ",$j,$k;
  1070. }
  1071. Log 3, $res2;
  1072. }
  1073. if( $owx_interface eq "DS2480" ){
  1074. $res = OWX_Block_2480($hash,$select);
  1075. }elsif( $owx_interface eq "DS9097" ){
  1076. $res = OWX_Block_9097($hash,$select);
  1077. }
  1078. #-- for debugging
  1079. if( $owx_debug > 1){
  1080. $res2 = "OWX_Complex_SER: Receiving ";
  1081. for($i=0;$i<length($res);$i++){
  1082. $j=int(ord(substr($res,$i,1))/16);
  1083. $k=ord(substr($res,$i,1))%16;
  1084. $res2.=sprintf "0x%1x%1x ",$j,$k;
  1085. }
  1086. Log 3, $res2;
  1087. }
  1088. return $res
  1089. }
  1090. ########################################################################################
  1091. #
  1092. # OWX_First_SER - Find the 'first' devices on the 1-Wire bus
  1093. #
  1094. # Parameter hash = hash of bus master, mode
  1095. #
  1096. # Return 1 : device found, ROM number pushed to list
  1097. # 0 : no device present
  1098. #
  1099. ########################################################################################
  1100. sub OWX_First_SER ($$) {
  1101. my ($hash,$mode) = @_;
  1102. #-- clear 16 byte of search data
  1103. @owx_search=(0,0,0,0 ,0,0,0,0, 0,0,0,0, 0,0,0,0);
  1104. #-- clear 8 bytes of romid:
  1105. @owx_ROM_ID = (0,0,0,0,0,0,0,0);
  1106. #-- reset the search state
  1107. $owx_LastDiscrepancy = 0;
  1108. $owx_LastDeviceFlag = 0;
  1109. $owx_LastFamilyDiscrepancy = 0;
  1110. #-- now do the search
  1111. return OWX_Search_SER($hash,$mode);
  1112. }
  1113. ########################################################################################
  1114. #
  1115. # OWX_Next_SER - Find the 'next' devices on the 1-Wire bus
  1116. #
  1117. # Parameter hash = hash of bus master, mode
  1118. #
  1119. # Return 1 : device found, ROM number in owx_ROM_ID and pushed to list (LastDeviceFlag=0)
  1120. # or only in owx_ROM_ID (LastDeviceFlag=1)
  1121. # 0 : device not found, or ot searched at all
  1122. #
  1123. ########################################################################################
  1124. sub OWX_Next_SER ($$) {
  1125. my ($hash,$mode) = @_;
  1126. #-- now do the search
  1127. return OWX_Search_SER($hash,$mode);
  1128. }
  1129. #######################################################################################
  1130. #
  1131. # OWX_Search_SER - Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
  1132. # search state.
  1133. #
  1134. # Parameter hash = hash of bus master, mode=alarm,discover or verify
  1135. #
  1136. # Return 1 : device found, ROM number in owx_ROM_ID and pushed to list (LastDeviceFlag=0)
  1137. # or only in owx_ROM_ID (LastDeviceFlag=1)
  1138. # 0 : device not found, or ot searched at all
  1139. #
  1140. ########################################################################################
  1141. sub OWX_Search_SER ($$) {
  1142. my ($hash,$mode)=@_;
  1143. my @owx_fams=();
  1144. #-- get the interface
  1145. my $owx_interface = $hash->{INTERFACE};
  1146. my $owx_hwdevice = $hash->{HWDEVICE};
  1147. #-- if the last call was the last one, no search
  1148. if ($owx_LastDeviceFlag==1){
  1149. return 0;
  1150. }
  1151. #-- 1-Wire reset
  1152. if (OWX_Reset($hash)==0){
  1153. #-- reset the search
  1154. Log 1, "OWX: Search reset failed";
  1155. $owx_LastDiscrepancy = 0;
  1156. $owx_LastDeviceFlag = 0;
  1157. $owx_LastFamilyDiscrepancy = 0;
  1158. return 0;
  1159. }
  1160. #-- Here we call the device dependent part
  1161. if( $owx_interface eq "DS2480" ){
  1162. OWX_Search_2480($hash,$mode);
  1163. }elsif( $owx_interface eq "DS9097" ){
  1164. OWX_Search_9097($hash,$mode);
  1165. }else{
  1166. Log 1,"OWX: Search called with unknown interface ".$owx_interface;
  1167. return 0;
  1168. }
  1169. #--check if we really found a device
  1170. if( OWX_CRC(0)!= 0){
  1171. #-- reset the search
  1172. Log 1, "OWX: Search CRC failed ";
  1173. $owx_LastDiscrepancy = 0;
  1174. $owx_LastDeviceFlag = 0;
  1175. $owx_LastFamilyDiscrepancy = 0;
  1176. return 0;
  1177. }
  1178. #-- character version of device ROM_ID, first byte = family
  1179. my $dev=sprintf("%02X.%02X%02X%02X%02X%02X%02X.%02X",@owx_ROM_ID);
  1180. #-- for some reason this does not work - replaced by another test, see below
  1181. #if( $owx_LastDiscrepancy==0 ){
  1182. # $owx_LastDeviceFlag=1;
  1183. #}
  1184. #--
  1185. if( $owx_LastDiscrepancy==$owx_LastFamilyDiscrepancy ){
  1186. $owx_LastFamilyDiscrepancy=0;
  1187. }
  1188. #-- mode was to verify presence of a device
  1189. if ($mode eq "verify") {
  1190. Log 5, "OWX: Device verified $dev";
  1191. return 1;
  1192. #-- mode was to discover devices
  1193. } elsif( $mode eq "discover" ){
  1194. #-- check families
  1195. my $famfnd=0;
  1196. foreach (@owx_fams){
  1197. if( substr($dev,0,2) eq $_ ){
  1198. #-- if present, set the fam found flag
  1199. $famfnd=1;
  1200. last;
  1201. }
  1202. }
  1203. push(@owx_fams,substr($dev,0,2)) if( !$famfnd );
  1204. foreach (@{$hash->{DEVS}}){
  1205. if( $dev eq $_ ){
  1206. #-- if present, set the last device found flag
  1207. $owx_LastDeviceFlag=1;
  1208. last;
  1209. }
  1210. }
  1211. if( $owx_LastDeviceFlag!=1 ){
  1212. #-- push to list
  1213. push(@{$hash->{DEVS}},$dev);
  1214. Log 5, "OWX: New device found $dev";
  1215. }
  1216. return 1;
  1217. #-- mode was to discover alarm devices
  1218. } else {
  1219. for(my $i=0;$i<@{$hash->{ALARMDEVS}};$i++){
  1220. if( $dev eq ${$hash->{ALARMDEVS}}[$i] ){
  1221. #-- if present, set the last device found flag
  1222. $owx_LastDeviceFlag=1;
  1223. last;
  1224. }
  1225. }
  1226. if( $owx_LastDeviceFlag!=1 ){
  1227. #--push to list
  1228. push(@{$hash->{ALARMDEVS}},$dev);
  1229. Log 5, "OWX: New alarm device found $dev";
  1230. }
  1231. return 1;
  1232. }
  1233. }
  1234. ########################################################################################
  1235. #
  1236. # OWX_Verify_SER - Verify a particular device on the 1-Wire bus
  1237. #
  1238. # Parameter hash = hash of bus master, dev = 8 Byte ROM ID of device to be tested
  1239. #
  1240. # Return 1 : device found
  1241. # 0 : device not
  1242. #
  1243. ########################################################################################
  1244. sub OWX_Verify_SER ($$) {
  1245. my ($hash,$dev) = @_;
  1246. my $i;
  1247. #-- from search string to byte id
  1248. my $devs=$dev;
  1249. $devs=~s/\.//g;
  1250. for($i=0;$i<8;$i++){
  1251. $owx_ROM_ID[$i]=hex(substr($devs,2*$i,2));
  1252. }
  1253. #-- reset the search state
  1254. $owx_LastDiscrepancy = 64;
  1255. $owx_LastDeviceFlag = 0;
  1256. #-- now do the search
  1257. my $res=OWX_Search_SER($hash,"verify");
  1258. my $dev2=sprintf("%02X.%02X%02X%02X%02X%02X%02X.%02X",@owx_ROM_ID);
  1259. #-- reset the search state
  1260. $owx_LastDiscrepancy = 0;
  1261. $owx_LastDeviceFlag = 0;
  1262. #-- check result
  1263. if ($dev eq $dev2){
  1264. return 1;
  1265. }else{
  1266. return 0;
  1267. }
  1268. }
  1269. ########################################################################################
  1270. #
  1271. # The following subroutines in alphabetical order are only for a DS2480 bus interface
  1272. #
  1273. #########################################################################################
  1274. #
  1275. # OWX_Block_2480 - Send data block (Fig. 6 of Maxim AN192)
  1276. #
  1277. # Parameter hash = hash of bus master, data = string to send
  1278. #
  1279. # Return response, if OK
  1280. # 0 if not OK
  1281. #
  1282. ########################################################################################
  1283. sub OWX_Block_2480 ($$) {
  1284. my ($hash,$data) =@_;
  1285. my $data2="";
  1286. my $retlen = length($data);
  1287. #-- if necessary, prepend E1 character for data mode
  1288. if( substr($data,0,1) ne '\xE1') {
  1289. $data2 = "\xE1";
  1290. }
  1291. #-- all E3 characters have to be duplicated
  1292. for(my $i=0;$i<length($data);$i++){
  1293. my $newchar = substr($data,$i,1);
  1294. $data2=$data2.$newchar;
  1295. if( $newchar eq '\xE3'){
  1296. $data2=$data2.$newchar;
  1297. }
  1298. }
  1299. #-- write 1-Wire bus as a single string
  1300. my $res =OWX_Query_2480($hash,$data2,$retlen);
  1301. return $res;
  1302. }
  1303. ########################################################################################
  1304. #
  1305. # OWX_Level_2480 - Change power level (Fig. 13 of Maxim AN192)
  1306. #
  1307. # Parameter hash = hash of bus master, newlevel = "normal" or something else
  1308. #
  1309. # Return 1 : OK
  1310. # 0 : not OK
  1311. #
  1312. ########################################################################################
  1313. sub OWX_Level_2480 ($$) {
  1314. my ($hash,$newlevel) =@_;
  1315. my $cmd="";
  1316. my $retlen=0;
  1317. #-- if necessary, prepend E3 character for command mode
  1318. $cmd = "\xE3";
  1319. #-- return to normal level
  1320. if( $newlevel eq "normal" ){
  1321. $cmd=$cmd."\xF1\xED\xF1";
  1322. $retlen+=3;
  1323. #-- write 1-Wire bus
  1324. my $res = OWX_Query_2480($hash,$cmd,$retlen);
  1325. #-- process result
  1326. my $r1 = ord(substr($res,0,1)) & 236;
  1327. my $r2 = ord(substr($res,1,1)) & 236;
  1328. if( ($r1 eq 236) && ($r2 eq 236) ){
  1329. Log 5, "OWX: Level change to normal OK";
  1330. return 1;
  1331. } else {
  1332. Log 3, "OWX: Failed to change to normal level";
  1333. return 0;
  1334. }
  1335. #-- start pulse
  1336. } else {
  1337. $cmd=$cmd."\x3F\xED";
  1338. $retlen+=2;
  1339. #-- write 1-Wire bus
  1340. my $res = OWX_Query_2480($hash,$cmd,$retlen);
  1341. #-- process result
  1342. if( $res eq "\x3E" ){
  1343. Log 5, "OWX: Level change OK";
  1344. return 1;
  1345. } else {
  1346. Log 3, "OWX: Failed to change level";
  1347. return 0;
  1348. }
  1349. }
  1350. }
  1351. ########################################################################################
  1352. #
  1353. # OWX_Query_2480 - Write to and read from the 1-Wire bus
  1354. #
  1355. # Parameter: hash = hash of bus master, cmd = string to send to the 1-Wire bus
  1356. #
  1357. # Return: string received from the 1-Wire bus
  1358. #
  1359. ########################################################################################
  1360. sub OWX_Query_2480 ($$$) {
  1361. my ($hash,$cmd,$retlen) = @_;
  1362. my ($i,$j,$k,$l,$m,$n);
  1363. my $string_in = "";
  1364. my $string_part;
  1365. #-- get hardware device
  1366. my $owx_hwdevice = $hash->{HWDEVICE};
  1367. $owx_hwdevice->baudrate($owx_baud);
  1368. $owx_hwdevice->write_settings;
  1369. if( $owx_debug > 2){
  1370. my $res = "OWX: Sending out ";
  1371. for($i=0;$i<length($cmd);$i++){
  1372. $j=int(ord(substr($cmd,$i,1))/16);
  1373. $k=ord(substr($cmd,$i,1))%16;
  1374. $res.=sprintf "0x%1x%1x ",$j,$k;
  1375. }
  1376. Log 3, $res;
  1377. }
  1378. my $count_out = $owx_hwdevice->write($cmd);
  1379. if( !($count_out)){
  1380. Log 3,"OWX_Query_2480: No return value after writing" if( $owx_debug > 0);
  1381. } else {
  1382. Log 3, "OWX_Query_2480: Write incomplete $count_out ne ".(length($cmd))."" if ( ($count_out != length($cmd)) & ($owx_debug > 0));
  1383. }
  1384. #-- sleeping for some time
  1385. select(undef,undef,undef,0.04);
  1386. #-- read the data - looping for slow devices suggested by Joachim Herold
  1387. $n=0;
  1388. for($l=0;$l<$retlen;$l+=$m) {
  1389. my ($count_in, $string_part) = $owx_hwdevice->read(48);
  1390. $string_in .= $string_part;
  1391. $m = $count_in;
  1392. $n++;
  1393. if( $owx_debug > 2){
  1394. Log 3, "Schleifendurchlauf $n";
  1395. }
  1396. if ($n > 100) {
  1397. $m = $retlen;
  1398. }
  1399. select(undef,undef,undef,0.02);
  1400. if( $owx_debug > 2){
  1401. my $res = "OWX: Receiving in loop no. $n ";
  1402. for($i=0;$i<$count_in;$i++){
  1403. $j=int(ord(substr($string_part,$i,1))/16);
  1404. $k=ord(substr($string_part,$i,1))%16;
  1405. $res.=sprintf "0x%1x%1x ",$j,$k;
  1406. }
  1407. Log 3, $res
  1408. if( $count_in > 0);
  1409. }
  1410. }
  1411. #-- sleeping for some time
  1412. select(undef,undef,undef,0.01);
  1413. return($string_in);
  1414. }
  1415. ########################################################################################
  1416. #
  1417. # OWX_Reset_2480 - Reset the 1-Wire bus (Fig. 4 of Maxim AN192)
  1418. #
  1419. # Parameter hash = hash of bus master
  1420. #
  1421. # Return 1 : OK
  1422. # 0 : not OK
  1423. #
  1424. ########################################################################################
  1425. sub OWX_Reset_2480 ($) {
  1426. my ($hash)=@_;
  1427. my $cmd="";
  1428. my $name = $hash->{NAME};
  1429. my ($res,$r1,$r2);
  1430. #-- if necessary, prepend \xE3 character for command mode
  1431. $cmd = "\xE3";
  1432. #-- Reset command \xC5
  1433. $cmd = $cmd."\xC5";
  1434. #-- write 1-Wire bus
  1435. $res =OWX_Query_2480($hash,$cmd,1);
  1436. #-- if not ok, try for max. a second time
  1437. $r1 = ord(substr($res,0,1)) & 192;
  1438. if( $r1 != 192){
  1439. #Log 1, "Trying second reset";
  1440. $res =OWX_Query_2480($hash,$cmd,1);
  1441. }
  1442. #-- process result
  1443. $r1 = ord(substr($res,0,1)) & 192;
  1444. if( $r1 != 192){
  1445. Log 3, "OWX: Reset failure on bus $name";
  1446. return 0;
  1447. }
  1448. $hash->{ALARMED} = "no";
  1449. $r2 = ord(substr($res,0,1)) & 3;
  1450. if( $r2 == 3 ){
  1451. #Log 3, "OWX: No presence detected";
  1452. return 1;
  1453. }elsif( $r2 ==2 ){
  1454. Log 1, "OWX: Alarm presence detected on bus $name";
  1455. $hash->{ALARMED} = "yes";
  1456. }
  1457. return 1;
  1458. }
  1459. ########################################################################################
  1460. #
  1461. # OWX_Search_2480 - Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
  1462. # search state.
  1463. #
  1464. # Parameter hash = hash of bus master, mode=alarm,discover or verify
  1465. #
  1466. # Return 1 : device found, ROM number in owx_ROM_ID and pushed to list (LastDeviceFlag=0)
  1467. # or only in owx_ROM_ID (LastDeviceFlag=1)
  1468. # 0 : device not found, or ot searched at all
  1469. #
  1470. ########################################################################################
  1471. sub OWX_Search_2480 ($$) {
  1472. my ($hash,$mode)=@_;
  1473. my ($sp1,$sp2,$response,$search_direction,$id_bit_number);
  1474. #-- Response search data parsing operates bytewise
  1475. $id_bit_number = 1;
  1476. select(undef,undef,undef,0.5);
  1477. #-- clear 16 byte of search data
  1478. @owx_search=(0,0,0,0 ,0,0,0,0, 0,0,0,0, 0,0,0,0);
  1479. #-- Output search data construction (Fig. 9 of Maxim AN192)
  1480. # operates on a 16 byte search response = 64 pairs of two bits
  1481. while ( $id_bit_number <= 64) {
  1482. #-- address single bits in a 16 byte search string
  1483. my $newcpos = int(($id_bit_number-1)/4);
  1484. my $newimsk = ($id_bit_number-1)%4;
  1485. #-- address single bits in a 8 byte id string
  1486. my $newcpos2 = int(($id_bit_number-1)/8);
  1487. my $newimsk2 = ($id_bit_number-1)%8;
  1488. if( $id_bit_number <= $owx_LastDiscrepancy){
  1489. #-- first use the ROM ID bit to set the search direction
  1490. if( $id_bit_number < $owx_LastDiscrepancy ) {
  1491. $search_direction = ($owx_ROM_ID[$newcpos2]>>$newimsk2) & 1;
  1492. #-- at the last discrepancy search into 1 direction anyhow
  1493. } else {
  1494. $search_direction = 1;
  1495. }
  1496. #-- fill into search data;
  1497. $owx_search[$newcpos]+=$search_direction<<(2*$newimsk+1);
  1498. }
  1499. #--increment number
  1500. $id_bit_number++;
  1501. }
  1502. #-- issue data mode \xE1, the normal search command \xF0 or the alarm search command \xEC
  1503. # and the command mode \xE3 / start accelerator \xB5
  1504. if( $mode ne "alarm" ){
  1505. $sp1 = "\xE1\xF0\xE3\xB5";
  1506. } else {
  1507. $sp1 = "\xE1\xEC\xE3\xB5";
  1508. }
  1509. #-- issue data mode \xE1, device ID, command mode \xE3 / end accelerator \xA5
  1510. $sp2=sprintf("\xE1%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\xE3\xA5",@owx_search);
  1511. $response = OWX_Query_2480($hash,$sp1,1);
  1512. $response = OWX_Query_2480($hash,$sp2,16);
  1513. #-- interpret the return data
  1514. if( length($response)!=16 ) {
  1515. Log 3, "OWX: Search 2nd return has wrong parameter with length = ".length($response)."";
  1516. return 0;
  1517. }
  1518. #-- Response search data parsing (Fig. 11 of Maxim AN192)
  1519. # operates on a 16 byte search response = 64 pairs of two bits
  1520. $id_bit_number = 1;
  1521. #-- clear 8 byte of device id for current search
  1522. @owx_ROM_ID =(0,0,0,0 ,0,0,0,0);
  1523. while ( $id_bit_number <= 64) {
  1524. #-- adress single bits in a 16 byte string
  1525. my $newcpos = int(($id_bit_number-1)/4);
  1526. my $newimsk = ($id_bit_number-1)%4;
  1527. #-- retrieve the new ROM_ID bit
  1528. my $newchar = substr($response,$newcpos,1);
  1529. #-- these are the new bits
  1530. my $newibit = (( ord($newchar) >> (2*$newimsk) ) & 2) / 2;
  1531. my $newdbit = ( ord($newchar) >> (2*$newimsk) ) & 1;
  1532. #-- output for test purpose
  1533. #print "id_bit_number=$id_bit_number => newcpos=$newcpos, newchar=0x".int(ord($newchar)/16).
  1534. # ".".int(ord($newchar)%16)." r$id_bit_number=$newibit d$id_bit_number=$newdbit\n";
  1535. #-- discrepancy=1 and ROM_ID=0
  1536. if( ($newdbit==1) and ($newibit==0) ){
  1537. $owx_LastDiscrepancy=$id_bit_number;
  1538. if( $id_bit_number < 9 ){
  1539. $owx_LastFamilyDiscrepancy=$id_bit_number;
  1540. }
  1541. }
  1542. #-- fill into device data; one char per 8 bits
  1543. $owx_ROM_ID[int(($id_bit_number-1)/8)]+=$newibit<<(($id_bit_number-1)%8);
  1544. #-- increment number
  1545. $id_bit_number++;
  1546. }
  1547. return 1;
  1548. }
  1549. ########################################################################################
  1550. #
  1551. # OWX_WriteBytePower_2480 - Send byte to bus with power increase (Fig. 16 of Maxim AN192)
  1552. #
  1553. # Parameter hash = hash of bus master, dbyte = byte to send
  1554. #
  1555. # Return 1 : OK
  1556. # 0 : not OK
  1557. #
  1558. ########################################################################################
  1559. sub OWX_WriteBytePower_2480 ($$) {
  1560. my ($hash,$dbyte) =@_;
  1561. my $cmd="\x3F";
  1562. my $ret="\x3E";
  1563. #-- if necessary, prepend \xE3 character for command mode
  1564. $cmd = "\xE3".$cmd;
  1565. #-- distribute the bits of data byte over several command bytes
  1566. for (my $i=0;$i<8;$i++){
  1567. my $newbit = (ord($dbyte) >> $i) & 1;
  1568. my $newchar = 133 | ($newbit << 4);
  1569. my $newchar2 = 132 | ($newbit << 4) | ($newbit << 1) | $newbit;
  1570. #-- last command byte still different
  1571. if( $i == 7){
  1572. $newchar = $newchar | 2;
  1573. }
  1574. $cmd = $cmd.chr($newchar);
  1575. $ret = $ret.chr($newchar2);
  1576. }
  1577. #-- write 1-Wire bus
  1578. my $res = OWX_Query($hash,$cmd);
  1579. #-- process result
  1580. if( $res eq $ret ){
  1581. Log 5, "OWX: WriteBytePower OK";
  1582. return 1;
  1583. } else {
  1584. Log 3, "OWX: WriteBytePower failure";
  1585. return 0;
  1586. }
  1587. }
  1588. ########################################################################################
  1589. #
  1590. # The following subroutines in alphabetical order are only for a DS9097 bus interface
  1591. #
  1592. ########################################################################################
  1593. #
  1594. # OWX_Block_9097 - Send data block (
  1595. #
  1596. # Parameter hash = hash of bus master, data = string to send
  1597. #
  1598. # Return response, if OK
  1599. # 0 if not OK
  1600. #
  1601. ########################################################################################
  1602. sub OWX_Block_9097 ($$) {
  1603. my ($hash,$data) =@_;
  1604. my $data2="";
  1605. my $res=0;
  1606. for (my $i=0; $i<length($data);$i++){
  1607. $res = OWX_TouchByte_9097($hash,ord(substr($data,$i,1)));
  1608. $data2 = $data2.chr($res);
  1609. }
  1610. return $data2;
  1611. }
  1612. ########################################################################################
  1613. #
  1614. # OWX_Query_9097 - Write to and read from the 1-Wire bus
  1615. #
  1616. # Parameter: hash = hash of bus master, cmd = string to send to the 1-Wire bus
  1617. #
  1618. # Return: string received from the 1-Wire bus
  1619. #
  1620. ########################################################################################
  1621. sub OWX_Query_9097 ($$) {
  1622. my ($hash,$cmd) = @_;
  1623. my ($i,$j,$k);
  1624. #-- get hardware device
  1625. my $owx_hwdevice = $hash->{HWDEVICE};
  1626. $owx_hwdevice->baudrate($owx_baud);
  1627. $owx_hwdevice->write_settings;
  1628. if( $owx_debug > 2){
  1629. my $res = "OWX: Sending out ";
  1630. for($i=0;$i<length($cmd);$i++){
  1631. $j=int(ord(substr($cmd,$i,1))/16);
  1632. $k=ord(substr($cmd,$i,1))%16;
  1633. $res.=sprintf "0x%1x%1x ",$j,$k;
  1634. }
  1635. Log 3, $res;
  1636. }
  1637. my $count_out = $owx_hwdevice->write($cmd);
  1638. Log 1, "OWX: Write incomplete $count_out ne ".(length($cmd))."" if ( $count_out != length($cmd) );
  1639. #-- sleeping for some time
  1640. select(undef,undef,undef,0.01);
  1641. #-- read the data
  1642. my ($count_in, $string_in) = $owx_hwdevice->read(48);
  1643. if( $owx_debug > 2){
  1644. my $res = "OWX: Receiving ";
  1645. for($i=0;$i<$count_in;$i++){
  1646. $j=int(ord(substr($string_in,$i,1))/16);
  1647. $k=ord(substr($string_in,$i,1))%16;
  1648. $res.=sprintf "0x%1x%1x ",$j,$k;
  1649. }
  1650. Log 3, $res;
  1651. }
  1652. #-- sleeping for some time
  1653. select(undef,undef,undef,0.01);
  1654. return($string_in);
  1655. }
  1656. ########################################################################################
  1657. #
  1658. # OWX_ReadBit_9097 - Read 1 bit from 1-wire bus (Fig. 5/6 from Maxim AN214)
  1659. #
  1660. # Parameter hash = hash of bus master
  1661. #
  1662. # Return bit value
  1663. #
  1664. ########################################################################################
  1665. sub OWX_ReadBit_9097 ($) {
  1666. my ($hash) = @_;
  1667. #-- set baud rate to 115200 and query!!!
  1668. my $sp1="\xFF";
  1669. $owx_baud=115200;
  1670. my $res=OWX_Query_9097($hash,$sp1);
  1671. $owx_baud=9600;
  1672. #-- process result
  1673. if( substr($res,0,1) eq "\xFF" ){
  1674. return 1;
  1675. } else {
  1676. return 0;
  1677. }
  1678. }
  1679. ########################################################################################
  1680. #
  1681. # OWX_Reset_9097 - Reset the 1-Wire bus (Fig. 4 of Maxim AN192)
  1682. #
  1683. # Parameter hash = hash of bus master
  1684. #
  1685. # Return 1 : OK
  1686. # 0 : not OK
  1687. #
  1688. ########################################################################################
  1689. sub OWX_Reset_9097 ($) {
  1690. my ($hash)=@_;
  1691. my $cmd="";
  1692. #-- Reset command \xF0
  1693. $cmd="\xF0";
  1694. #-- write 1-Wire bus
  1695. my $res =OWX_Query_9097($hash,$cmd);
  1696. #-- TODO: process result
  1697. #-- may vary between 0x10, 0x90, 0xe0
  1698. return 1;
  1699. }
  1700. ########################################################################################
  1701. #
  1702. # OWX_Search_9097 - Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
  1703. # search state.
  1704. #
  1705. # Parameter hash = hash of bus master, mode=alarm,discover or verify
  1706. #
  1707. # Return 1 : device found, ROM number in owx_ROM_ID and pushed to list (LastDeviceFlag=0)
  1708. # or only in owx_ROM_ID (LastDeviceFlag=1)
  1709. # 0 : device not found, or ot searched at all
  1710. #
  1711. ########################################################################################
  1712. sub OWX_Search_9097 ($$) {
  1713. my ($hash,$mode)=@_;
  1714. my ($sp1,$sp2,$response,$search_direction,$id_bit_number);
  1715. #-- Response search data parsing operates bitwise
  1716. $id_bit_number = 1;
  1717. my $rom_byte_number = 0;
  1718. my $rom_byte_mask = 1;
  1719. my $last_zero = 0;
  1720. #-- issue search command
  1721. $owx_baud=115200;
  1722. $sp2="\x00\x00\x00\x00\xFF\xFF\xFF\xFF";
  1723. $response = OWX_Query_9097($hash,$sp2);
  1724. $owx_baud=9600;
  1725. #-- issue the normal search command \xF0 or the alarm search command \xEC
  1726. #if( $mode ne "alarm" ){
  1727. # $sp1 = 0xF0;
  1728. #} else {
  1729. # $sp1 = 0xEC;
  1730. #}
  1731. #$response = OWX_TouchByte($hash,$sp1);
  1732. while ( $id_bit_number <= 64) {
  1733. #loop until through all ROM bytes 0-7
  1734. my $id_bit = OWX_TouchBit_9097($hash,1);
  1735. my $cmp_id_bit = OWX_TouchBit_9097($hash,1);
  1736. #print "id_bit = $id_bit, cmp_id_bit = $cmp_id_bit\n";
  1737. if( ($id_bit == 1) && ($cmp_id_bit == 1) ){
  1738. #print "no devices present at id_bit_number=$id_bit_number \n";
  1739. last;
  1740. }
  1741. if ( $id_bit != $cmp_id_bit ){
  1742. $search_direction = $id_bit;
  1743. } else {
  1744. # hä ? if this discrepancy if before the Last Discrepancy
  1745. # on a previous next then pick the same as last time
  1746. if ( $id_bit_number < $owx_LastDiscrepancy ){
  1747. if (($owx_ROM_ID[$rom_byte_number] & $rom_byte_mask) > 0){
  1748. $search_direction = 1;
  1749. } else {
  1750. $search_direction = 0;
  1751. }
  1752. } else {
  1753. # if equal to last pick 1, if not then pick 0
  1754. if ($id_bit_number == $owx_LastDiscrepancy){
  1755. $search_direction = 1;
  1756. } else {
  1757. $search_direction = 0;
  1758. }
  1759. }
  1760. # if 0 was picked then record its position in LastZero
  1761. if ($search_direction == 0){
  1762. $last_zero = $id_bit_number;
  1763. # check for Last discrepancy in family
  1764. if ($last_zero < 9) {
  1765. $owx_LastFamilyDiscrepancy = $last_zero;
  1766. }
  1767. }
  1768. }
  1769. # print "search_direction = $search_direction, last_zero=$last_zero\n";
  1770. # set or clear the bit in the ROM byte rom_byte_number
  1771. # with mask rom_byte_mask
  1772. #print "ROM byte mask = $rom_byte_mask, search_direction = $search_direction\n";
  1773. if ( $search_direction == 1){
  1774. $owx_ROM_ID[$rom_byte_number] |= $rom_byte_mask;
  1775. } else {
  1776. $owx_ROM_ID[$rom_byte_number] &= ~$rom_byte_mask;
  1777. }
  1778. # serial number search direction write bit
  1779. $response = OWX_WriteBit_9097($hash,$search_direction);
  1780. # increment the byte counter id_bit_number
  1781. # and shift the mask rom_byte_mask
  1782. $id_bit_number++;
  1783. $rom_byte_mask <<= 1;
  1784. #-- if the mask is 0 then go to new rom_byte_number and
  1785. if ($rom_byte_mask == 256){
  1786. $rom_byte_number++;
  1787. $rom_byte_mask = 1;
  1788. }
  1789. }
  1790. $owx_LastDiscrepancy = $last_zero;
  1791. return 1;
  1792. }
  1793. ########################################################################################
  1794. #
  1795. # OWX_TouchBit_9097 - Write/Read 1 bit from 1-wire bus (Fig. 5-8 from Maxim AN 214)
  1796. #
  1797. # Parameter hash = hash of bus master
  1798. #
  1799. # Return bit value
  1800. #
  1801. ########################################################################################
  1802. sub OWX_TouchBit_9097 ($$) {
  1803. my ($hash,$bit) = @_;
  1804. my $sp1;
  1805. #-- set baud rate to 115200 and query!!!
  1806. if( $bit == 1 ){
  1807. $sp1="\xFF";
  1808. } else {
  1809. $sp1="\x00";
  1810. }
  1811. $owx_baud=115200;
  1812. my $res=OWX_Query_9097($hash,$sp1);
  1813. $owx_baud=9600;
  1814. #-- process result
  1815. my $sp2=substr($res,0,1);
  1816. if( $sp1 eq $sp2 ){
  1817. return 1;
  1818. }else {
  1819. return 0;
  1820. }
  1821. }
  1822. ########################################################################################
  1823. #
  1824. # OWX_TouchByte_9097 - Write/Read 8 bit from 1-wire bus
  1825. #
  1826. # Parameter hash = hash of bus master
  1827. #
  1828. # Return bit value
  1829. #
  1830. ########################################################################################
  1831. sub OWX_TouchByte_9097 ($$) {
  1832. my ($hash,$byte) = @_;
  1833. my $loop;
  1834. my $result=0;
  1835. my $bytein=$byte;
  1836. for( $loop=0; $loop < 8; $loop++ ){
  1837. #-- shift result to get ready for the next bit
  1838. $result >>=1;
  1839. #-- if sending a 1 then read a bit else write 0
  1840. if( $byte & 0x01 ){
  1841. if( OWX_ReadBit_9097($hash) ){
  1842. $result |= 0x80;
  1843. }
  1844. } else {
  1845. OWX_WriteBit_9097($hash,0);
  1846. }
  1847. $byte >>= 1;
  1848. }
  1849. return $result;
  1850. }
  1851. ########################################################################################
  1852. #
  1853. # OWX_WriteBit_9097 - Write 1 bit to 1-wire bus (Fig. 7/8 from Maxim AN 214)
  1854. #
  1855. # Parameter hash = hash of bus master
  1856. #
  1857. # Return bit value
  1858. #
  1859. ########################################################################################
  1860. sub OWX_WriteBit_9097 ($$) {
  1861. my ($hash,$bit) = @_;
  1862. my $sp1;
  1863. #-- set baud rate to 115200 and query!!!
  1864. if( $bit ==1 ){
  1865. $sp1="\xFF";
  1866. } else {
  1867. $sp1="\x00";
  1868. }
  1869. $owx_baud=115200;
  1870. my $res=OWX_Query_9097($hash,$sp1);
  1871. $owx_baud=9600;
  1872. #-- process result
  1873. if( substr($res,0,1) eq $sp1 ){
  1874. return 1;
  1875. } else {
  1876. return 0;
  1877. }
  1878. }
  1879. ########################################################################################
  1880. #
  1881. # The following subroutines in alphabetical order are only for a COC/CUNO interface
  1882. #
  1883. ########################################################################################
  1884. #
  1885. # OWX_Complex_CCC - Send match ROM, data block and receive bytes as response
  1886. #
  1887. # Parameter hash = hash of bus master,
  1888. # owx_dev = ROM ID of device
  1889. # data = string to send
  1890. # numread = number of bytes to receive
  1891. #
  1892. # Return response, if OK
  1893. # 0 if not OK
  1894. #
  1895. ########################################################################################
  1896. sub OWX_Complex_CCC ($$$$) {
  1897. my ($hash,$owx_dev,$data,$numread) =@_;
  1898. my $select;
  1899. my $res = "";
  1900. #-- get the interface
  1901. my $owx_hwdevice = $hash->{HWDEVICE};
  1902. #-- has match ROM part
  1903. if( $owx_dev ){
  1904. #-- ID of the device
  1905. my $owx_rnf = substr($owx_dev,3,12);
  1906. my $owx_f = substr($owx_dev,0,2);
  1907. #-- 8 byte 1-Wire device address
  1908. my @owx_ROM_ID =(0,0,0,0 ,0,0,0,0);
  1909. #-- from search string to reverse string id
  1910. $owx_dev=~s/\.//g;
  1911. for(my $i=0;$i<8;$i++){
  1912. $owx_ROM_ID[7-$i]=substr($owx_dev,2*$i,2);
  1913. }
  1914. $select=sprintf("Om%s%s%s%s%s%s%s%s",@owx_ROM_ID);
  1915. Log 3,"OWX: Sending match ROM to COC/CUNO ".$select
  1916. if( $owx_debug > 1);
  1917. #--
  1918. CUL_SimpleWrite($owx_hwdevice, $select);
  1919. my ($err,$ob) = OWX_ReadAnswer_CCC($owx_hwdevice);
  1920. #-- padding first 9 bytes into result string, since we have this
  1921. # in the serial interfaces as well
  1922. $res .= "000000000";
  1923. }
  1924. #-- has data part
  1925. if ( $data ){
  1926. OWX_Send_CCC($hash,$data);
  1927. $res .= $data;
  1928. }
  1929. #-- has receive part
  1930. if( $numread > 0 ){
  1931. #$numread += length($data);
  1932. Log 3,"COC/CUNO is expected to deliver $numread bytes"
  1933. if( $owx_debug > 1);
  1934. $res.=OWX_Receive_CCC($hash,$numread);
  1935. }
  1936. Log 3,"OWX: returned from COC/CUNO $res"
  1937. if( $owx_debug > 1);
  1938. return $res;
  1939. }
  1940. ########################################################################################
  1941. #
  1942. # OWX_Discover_CCC - Discover devices on the 1-Wire bus via internal firmware
  1943. #
  1944. # Parameter hash = hash of bus master
  1945. #
  1946. # Return 0 : error
  1947. # 1 : OK
  1948. #
  1949. ########################################################################################
  1950. sub OWX_Discover_CCC ($) {
  1951. my ($hash) = @_;
  1952. my $res;
  1953. #-- get the interface
  1954. my $owx_hwdevice = $hash->{HWDEVICE};
  1955. #-- zero the array
  1956. @{$hash->{DEVS}}=();
  1957. #-- reset the busmaster
  1958. OWX_ReInit_CCC($hash,0);
  1959. #-- get the devices
  1960. CUL_SimpleWrite($owx_hwdevice, "Oc");
  1961. select(undef,undef,undef,0.5);
  1962. my ($err,$ob) = OWX_ReadAnswer_CCC($owx_hwdevice);
  1963. if( $ob ){
  1964. Log 3,"OWX: Answer to ".$owx_hwdevice->{NAME}." device search is ".$ob;
  1965. foreach my $dx (split(/\n/,$ob)){
  1966. next if ($dx !~ /^\d\d?\:[0-9a-fA-F]{16}/);
  1967. $dx =~ s/\d+\://;
  1968. my $ddx = substr($dx,14,2).".";
  1969. #-- reverse data from culfw
  1970. for( my $i=1;$i<7;$i++){
  1971. $ddx .= substr($dx,14-2*$i,2);
  1972. }
  1973. $ddx .= ".".substr($dx,0,2);
  1974. push (@{$hash->{DEVS}},$ddx);
  1975. }
  1976. return 1;
  1977. } else {
  1978. Log 1, "OWX: No answer to ".$owx_hwdevice->{NAME}." device search";
  1979. return 0;
  1980. }
  1981. }
  1982. ########################################################################################
  1983. #
  1984. # OWX_ReadAnswer_CCC - Replacement for CUL_ReadAnswer for better control
  1985. #
  1986. # Parameter: hash = hash of bus master
  1987. #
  1988. # Return: string received
  1989. #
  1990. ########################################################################################
  1991. sub
  1992. OWX_ReadAnswer_CCC($)
  1993. {
  1994. my ($hash) = @_;
  1995. my $type = $hash->{TYPE};
  1996. my $arg ="";
  1997. my $anydata=0;
  1998. my $regexp =undef;
  1999. my ($mculdata, $rin) = ("", '');
  2000. my $buf;
  2001. my $to = 3; # 3 seconds timeout
  2002. $to = $hash->{RA_Timeout} if($hash->{RA_Timeout}); # ...or less
  2003. for(;;) {
  2004. return ("Device lost when reading answer for get $arg", undef)
  2005. if(!$hash->{FD});
  2006. vec($rin, $hash->{FD}, 1) = 1;
  2007. my $nfound = select($rin, undef, undef, $to);
  2008. if($nfound < 0) {
  2009. next if ($! == EAGAIN() || $! == EINTR() || $! == 0);
  2010. my $err = $!;
  2011. DevIo_Disconnected($hash);
  2012. return("OWX_ReadAnswer_CCC $arg: $err", undef);
  2013. }
  2014. return ("Timeout reading answer for get $arg", undef)
  2015. if($nfound == 0);
  2016. $buf = DevIo_SimpleRead($hash);
  2017. return ("No data", undef) if(!defined($buf));
  2018. if($buf) {
  2019. Log 5, "CUL/RAW (ReadAnswer): $buf";
  2020. $mculdata .= $buf;
  2021. }
  2022. # \n\n is socat special
  2023. if($mculdata =~ m/\r\n/ || $anydata || $mculdata =~ m/\n\n/ ) {
  2024. if($regexp && $mculdata !~ m/$regexp/) {
  2025. CUL_Parse($hash, $hash, $hash->{NAME}, $mculdata, $hash->{initString});
  2026. } else {
  2027. return (undef, $mculdata)
  2028. }
  2029. }
  2030. }
  2031. }
  2032. ########################################################################################
  2033. #
  2034. # OWX_Receive_CCC - Read data from the 1-Wire bus
  2035. #
  2036. # Parameter: hash = hash of bus master, numread = number of bytes to read
  2037. #
  2038. # Return: string received
  2039. #
  2040. ########################################################################################
  2041. sub OWX_Receive_CCC ($$) {
  2042. my ($hash,$numread) = @_;
  2043. my $res="";
  2044. my $res2="";
  2045. #-- get the interface
  2046. my $owx_hwdevice = $hash->{HWDEVICE};
  2047. for(
  2048. my $i=0;$i<$numread;$i++){
  2049. #Log 1, "Sending $owx_hwdevice->{NAME}: OrB";
  2050. #my $ob = CallFn($owx_hwdevice->{NAME}, "GetFn", $owx_hwdevice, (" ", "raw", "OrB"));
  2051. CUL_SimpleWrite($owx_hwdevice, "OrB");
  2052. select(undef,undef,undef,0.01);
  2053. my ($err,$ob) = OWX_ReadAnswer_CCC($owx_hwdevice);
  2054. #Log 1, "Answer from $owx_hwdevice->{NAME}:$ob: ";
  2055. #-- process results
  2056. if( !(defined($ob)) ){
  2057. return "";
  2058. #-- four bytes received makes one byte of result
  2059. }elsif( length($ob) == 4 ){
  2060. $res .= sprintf("%c",hex(substr($ob,0,2)));
  2061. $res2 .= "0x".substr($ob,0,2)." ";
  2062. #-- 11 bytes received makes one byte of result
  2063. }elsif( length($ob) == 11 ){
  2064. $res .= sprintf("%c",hex(substr($ob,9,2)));
  2065. $res2 .= "0x".substr($ob,9,2)." ";
  2066. #-- 18 bytes received from CUNO
  2067. }elsif( length($ob) == 18 ){
  2068. my $res = "OWX: Receiving 18 bytes from CUNO: $ob\n";
  2069. for(my $i=0;$i<length($ob);$i++){
  2070. my $j=int(ord(substr($ob,$i,1))/16);
  2071. my $k=ord(substr($ob,$i,1))%16;
  2072. $res.=sprintf "0x%1x%1x ",$j,$k;
  2073. }
  2074. Log 3, $res;
  2075. #$numread++;
  2076. #-- 20 bytes received = leftover from match
  2077. }elsif( length($ob) == 20 ){
  2078. $numread++;
  2079. }else{
  2080. Log 1,"OWX: Received unexpected number of ".length($ob)." bytes on bus ".$owx_hwdevice->{NAME};
  2081. }
  2082. }
  2083. Log 3, "OWX: Received $numread bytes = $res2 on bus ".$owx_hwdevice->{NAME}
  2084. if( $owx_debug > 1);
  2085. return($res);
  2086. }
  2087. ########################################################################################
  2088. #
  2089. # OWX_Reset_CCC - Reset the 1-Wire bus
  2090. #
  2091. # Parameter hash = hash of bus master
  2092. #
  2093. # Return 1 : OK
  2094. # 0 : not OK
  2095. #
  2096. ########################################################################################
  2097. sub OWX_Reset_CCC ($) {
  2098. my ($hash) = @_;
  2099. #-- get the interface
  2100. my $owx_hwdevice = $hash->{HWDEVICE};
  2101. my $ob = CallFn($owx_hwdevice->{NAME}, "GetFn", $owx_hwdevice, (" ", "raw", "ORb"));
  2102. if( substr($ob,9,4) eq "OK:1" ){
  2103. return 1;
  2104. }else{
  2105. return 0
  2106. }
  2107. }
  2108. ########################################################################################
  2109. #
  2110. # OWX_ReInit_CCC - Reset the 1-Wire bus master chip or subsystem
  2111. #
  2112. # Parameter hash = hash of bus master
  2113. # typ = 0 for bus master, 1 for subsystem
  2114. #
  2115. # Return 1 : OK
  2116. # 0 : not OK
  2117. #
  2118. ########################################################################################
  2119. sub OWX_ReInit_CCC ($$) {
  2120. my ($hash,$type) = @_;
  2121. #-- get the interface
  2122. my $owx_hwdevice = $hash->{HWDEVICE};
  2123. if ( $type eq "0") {
  2124. my $ob = CallFn($owx_hwdevice->{NAME}, "GetFn", $owx_hwdevice, (" ", "raw", "ORm"));
  2125. return 0 if( !defined($ob) );
  2126. return 0 if( length($ob) < 13);
  2127. if( substr($ob,9,4) eq "OK" ){
  2128. return 1;
  2129. }else{
  2130. return 0
  2131. }
  2132. }elsif( $type eq "1" ){
  2133. my $ob = CallFn($owx_hwdevice->{NAME}, "GetFn", $owx_hwdevice, (" ", "raw", "Oi"));
  2134. #Log 1, "Answer of sending Oi to ".$owx_hwdevice->{NAME}." was $ob";
  2135. return 1;
  2136. }
  2137. }
  2138. #########################################################################################
  2139. #
  2140. # OWX_Send_CCC - Send data block
  2141. #
  2142. # Parameter hash = hash of bus master, data = string to send
  2143. #
  2144. # Return response, if OK
  2145. # 0 if not OK
  2146. #
  2147. ########################################################################################
  2148. sub OWX_Send_CCC ($$) {
  2149. my ($hash,$data) =@_;
  2150. my ($i,$j,$k);
  2151. my $res = "";
  2152. my $res2 = "";
  2153. #-- get the interface
  2154. my $owx_hwdevice = $hash->{HWDEVICE};
  2155. for( $i=0;$i<length($data);$i++){
  2156. $j=int(ord(substr($data,$i,1))/16);
  2157. $k=ord(substr($data,$i,1))%16;
  2158. $res =sprintf "OwB%1x%1x ",$j,$k;
  2159. $res2.=sprintf "0x%1x%1x ",$j,$k;
  2160. CUL_SimpleWrite($owx_hwdevice, $res);
  2161. }
  2162. Log 3,"OWX: Send to COC/CUNO $res2"
  2163. if( $owx_debug > 1);
  2164. }
  2165. ########################################################################################
  2166. #
  2167. # OWX_Verify_CCC - Verify a particular device on the 1-Wire bus
  2168. #
  2169. # Parameter hash = hash of bus master, dev = 8 Byte ROM ID of device to be tested
  2170. #
  2171. # Return 1 : device found
  2172. # 0 : device not
  2173. #
  2174. ########################################################################################
  2175. sub OWX_Verify_CCC ($$) {
  2176. my ($hash,$dev) = @_;
  2177. my $i;
  2178. #-- get the interface
  2179. my $owx_hwdevice = $hash->{HWDEVICE};
  2180. #-- Ask the COC/CUNO
  2181. CUL_SimpleWrite($owx_hwdevice, "OCf");
  2182. #-- sleeping for some time
  2183. select(undef,undef,undef,3);
  2184. CUL_SimpleWrite($owx_hwdevice, "Oc");
  2185. select(undef,undef,undef,0.5);
  2186. my ($err,$ob) = OWX_ReadAnswer_CCC($owx_hwdevice);
  2187. if( $ob ){
  2188. foreach my $dx (split(/\n/,$ob)){
  2189. next if ($dx !~ /^\d\d?\:[0-9a-fA-F]{16}/);
  2190. $dx =~ s/\d+\://;
  2191. my $ddx = substr($dx,14,2).".";
  2192. #-- reverse data from culfw
  2193. for( my $i=1;$i<7;$i++){
  2194. $ddx .= substr($dx,14-2*$i,2);
  2195. }
  2196. $ddx .= ".".substr($dx,0,2);
  2197. return 1 if( $dev eq $ddx);
  2198. }
  2199. }
  2200. return 0;
  2201. }
  2202. 1;
  2203. =pod
  2204. =begin html
  2205. <a name="OWX"></a>
  2206. <h3>OWX</h3>
  2207. <p> FHEM module to commmunicate with 1-Wire bus devices</p>
  2208. <ul>
  2209. <li>via an active DS2480/DS2482/DS2490/DS9097U bus master interface attached to an USB
  2210. port or </li>
  2211. <li>via a passive DS9097 interface attached to an USB port or</li>
  2212. <li>via a network-attached CUNO or through a COC on the RaspBerry Pi</li>
  2213. <li>via an Arduino running OneWireFirmata attached to USB</li>
  2214. </ul> Internally these interfaces are vastly different, read the corresponding <a
  2215. href="http://fhemwiki.de/wiki/Interfaces_f%C3%BCr_1-Wire"> Wiki pages </a>
  2216. <br />
  2217. <br />
  2218. <h4>Example</h4><br />
  2219. <p>
  2220. <code>define OWio1 OWX /dev/ttyUSB1</code>
  2221. <br />
  2222. <code>define OWio2 OWX COC</code>
  2223. <br />
  2224. <code>define OWio3 OWX 10</code>
  2225. <br />
  2226. </p>
  2227. <br />
  2228. <a name="OWXdefine"></a>
  2229. <h4>Define</h4>
  2230. <p>
  2231. <code>define &lt;name&gt; OWX &lt;serial-device&gt;</code> or <br />
  2232. <code>define &lt;name&gt; OWX &lt;cuno/coc-device&gt;</code> or <br />
  2233. <code>define &lt;name&gt; OWX &lt;arduino-pin&gt;</code>
  2234. <br /><br /> Define a 1-Wire interface to communicate with a 1-Wire bus.<br />
  2235. <br />
  2236. </p>
  2237. <ul>
  2238. <li>
  2239. <code>&lt;serial-device&gt;</code> The serial device (e.g. USB port) to which the
  2240. 1-Wire bus is attached.</li>
  2241. <li>
  2242. <code>&lt;cuno-device&gt;</code> The previously defined CUNO to which the 1-Wire bus
  2243. is attached. </li>
  2244. <li>
  2245. <code>&lt;arduino-pin&gt;</code> The pin of the previous defined <a href="#FRM">FRM</a>
  2246. to which the 1-Wire bus is attached. If there is more than one FRM device defined
  2247. use <a href="#IODev">IODev</a> attribute to select which FRM device to use.</li>
  2248. </ul>
  2249. <br />
  2250. <a name="OWXset"></a>
  2251. <h4>Set</h4>
  2252. <ul>
  2253. <li><a name="owx_interval">
  2254. <code>set &lt;name&gt; interval &lt;value&gt;</code>
  2255. </a>
  2256. <br /><br /> sets the time period in seconds for "kicking" the 1-Wire bus (default
  2257. is 300 seconds). This means: <ul>
  2258. <li>With 1-Wire bus interfaces that do not supply power to the 1-Wire bus (attr
  2259. buspower parasitic), the 1-Wire bus is reset at these intervals. </li>
  2260. <li>With 1-Wire bus interfaces that supply power to the 1-Wire bus (attr
  2261. buspower = real), all temperature measurement devices on the bus receive the
  2262. command to start a temperature conversion (saves a lot of time when reading) </li>
  2263. <li>With 1-Wire bus interfaces that contain a busmaster chip, the response to a
  2264. reset pulse contains information about alarms.</li>
  2265. </ul><br />
  2266. </li>
  2267. <li><a name="owx_followAlarms">
  2268. <code>set &lt;name&gt; followAlarms on|off</code>
  2269. </a>
  2270. <br /><br /> instructs the module to start an alarm search in case a reset pulse
  2271. discovers any 1-Wire device which has the alarm flag set. </li>
  2272. </ul>
  2273. <br />
  2274. <a name="OWXget"></a>
  2275. <h4>Get</h4>
  2276. <ul>
  2277. <li><a name="owx_alarms"></a>
  2278. <code>get &lt;name&gt; alarms</code>
  2279. <br /><br /> performs an "alarm search" for devices on the 1-Wire bus and, if found,
  2280. generates an event in the log (not with CUNO). </li>
  2281. <li><a name="owx_devices"></a>
  2282. <code>get &lt;name&gt; devices</code>
  2283. <br /><br /> redicovers all devices on the 1-Wire bus. If a device found has a
  2284. previous definition, this is automatically used. If a device is found but has no
  2285. definition, it is autocreated. If a defined device is not on the 1-Wire bus, it is
  2286. autodeleted. </li>
  2287. </ul>
  2288. <br />
  2289. <a name="OWXattr"></a>
  2290. <h4>Attributes</h4>
  2291. <ul>
  2292. <li><a name="OWXbuspower"><code>attr &lt;name&gt; buspower real|parasitic</code></a>
  2293. <br />tells FHEM whether power is supplied to the 1-Wire bus or not.</li>
  2294. <li><code>attr &lt;name&gt; IODev <FRM-device></code>
  2295. <br />assignes a specific FRM-device to OWX. Required only if there is more than one FRM defined.</li>
  2296. <li>Standard attributes <a href="#alias">alias</a>, <a href="#comment">comment</a>, <a
  2297. href="#event-on-update-reading">event-on-update-reading</a>, <a
  2298. href="#event-on-change-reading">event-on-change-reading</a>, <a href="#room"
  2299. >room</a>, <a href="#eventMap">eventMap</a>, <a href="#loglevel">loglevel</a>,
  2300. <a href="#webCmd">webCmd</a></li>
  2301. </ul>
  2302. =end html
  2303. =cut