19_VBUSIF.pm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. ##############################################
  2. # $Id: 19_VBUSIF.pm 12980 2017-01-06 12:36:39Z Tobias.Faust $
  3. #
  4. # VBUS LAN Adapter Device
  5. # 19_VBUSIF.pm
  6. #
  7. # (c) 2014 Arno Willig <akw@bytefeed.de>
  8. # (c) 2015 Frank Wurdinger <frank@wurdinger.de>
  9. # (c) 2015 Adrian Freihofer <adrian.freihofer gmail com>
  10. # (c) 2016 Tobias Faust <tobias.faust gmx net>
  11. # (c) 2016 Jörg (pejonp)
  12. ##############################################
  13. package main;
  14. use strict;
  15. use warnings;
  16. use POSIX;
  17. use Data::Dumper;
  18. use Device::SerialPort;
  19. sub VBUSIF_Read($@);
  20. sub VBUSIF_Write($$$);
  21. sub VBUSIF_Ready($);
  22. sub VBUSIF_getDevList($$);
  23. sub VBUSIF_Initialize($)
  24. {
  25. my ($hash) = @_;
  26. require "$attr{global}{modpath}/FHEM/DevIo.pm";
  27. # Provider
  28. $hash->{ReadFn} = "VBUSIF_Read";
  29. $hash->{WriteFn} = "VBUSIF_Write";
  30. $hash->{ReadyFn} = "VBUSIF_Ready";
  31. $hash->{UndefFn} = "VBUSIF_Undef";
  32. $hash->{ShutdownFn} = "VBUSIF_Undef";
  33. # Normal devices
  34. $hash->{DefFn} = "VBUSIF_Define";
  35. $hash->{AttrList} = "dummy:1,0"
  36. ."$readingFnAttributes ";
  37. $hash->{AutoCreate} = { "VBUSDEF.*" => { ATTR => "event-min-interval:.*:120 event-on-change-reading:.* ",FILTER => "%NAME"} };
  38. }
  39. ######################################
  40. sub VBUSIF_Define($$)
  41. {
  42. my ($hash, $def) = @_;
  43. my @a = split("[ \t]+", $def);
  44. if(@a != 3) {
  45. my $msg = "wrong syntax: define <name> VBUSIF [<hostname:7053> or <dev>]";
  46. Log3 $hash, 2, $msg;
  47. return $msg;
  48. }
  49. # if(@a != 3) {
  50. # return "wrong syntax: define <name> VBUSIF [<hostname:7053> or <dev>]";
  51. # }
  52. my $name = $a[0];
  53. my $dev = $a[2];
  54. $hash->{Clients} = ":VBUSDEV:";
  55. my %matchList = ( "1:VBUSDEV" => ".*" );
  56. $hash->{MatchList} = \%matchList;
  57. Log3 $hash, 4,"$name: VBUSIF_Define: $hash->{MatchList} ";
  58. DevIo_CloseDev($hash);
  59. $hash->{DeviceName} = $dev;
  60. my @dev_name = split('@', $dev);
  61. if ( -c ${dev_name}[0]) {
  62. $hash->{DeviceType} = "Serial";
  63. } else {
  64. $hash->{DeviceType} = "Net";
  65. }
  66. my $ret = DevIo_OpenDev($hash, 0, "VBUSIF_DoInit");
  67. return $ret;
  68. }
  69. ###############################
  70. sub VBUSIF_DoInit($)
  71. {
  72. my $hash = shift;
  73. if ($hash->{DeviceType} eq "Net" ) {
  74. my $name = $hash->{NAME};
  75. delete $hash->{HANDLE}; # else reregister fails / RELEASE is deadly
  76. my $conn = $hash->{TCPDev};
  77. $conn->autoflush(1);
  78. $conn->getline();
  79. $conn->write("PASS vbus\n");
  80. $conn->getline();
  81. $conn->write("DATA\n");
  82. $conn->getline();
  83. }
  84. Log3 $hash, 4,"VBUSIF_DoInit ";
  85. return undef;
  86. }
  87. sub VBUSIF_Undef($@)
  88. {
  89. my ($hash, $arg) = @_;
  90. if ($hash->{DeviceType} eq "Net" ) {
  91. VBUSIF_Write($hash, "QUIT\n", ""); # RELEASE
  92. }
  93. DevIo_CloseDev($hash);
  94. return undef;
  95. }
  96. sub VBUSIF_Write($$$)
  97. {
  98. my ($hash,$fn,$msg) = @_;
  99. DevIo_SimpleWrite($hash, $msg, 1);
  100. }
  101. sub VBUSIF_Read($@)
  102. {
  103. my ($hash, $local, $regexp) = @_;
  104. my $buf = ($local ? $local : DevIo_SimpleRead($hash));
  105. return "" if(!defined($buf));
  106. my $name = $hash->{NAME};
  107. $buf = unpack('H*', $buf);
  108. my $data = ($hash->{PARTIAL} ? $hash->{PARTIAL} : "");
  109. Log3 $hash->{NAME}, 5, ,"received buffer: $buf";
  110. $data .= $buf;
  111. my $msg;
  112. my $msg2;
  113. my $idx;
  114. my $muster = "aa";
  115. $idx = index($data, $muster);
  116. Log3 $hash->{NAME}, 5,"$name: VBUSIF_Read0: index = $data";
  117. if ($idx>=0) {
  118. $msg2 = $data;
  119. $data = substr($data,$idx); # Cut off beginning
  120. $idx = index($data,$muster,2); # Find next message
  121. if ($idx>0) {
  122. $idx +=1 if (substr($data,$idx,3) eq "aaa"); # Message endet mit a
  123. $msg = substr($data,0,$idx);
  124. $data = substr($data,$idx);
  125. my $protoVersion = substr($msg,10,2);
  126. Log3 $hash->{NAME}, 5,"$name: VBUSIF_Read1: protoVersion : $protoVersion";
  127. if ($protoVersion == "10" && length($msg)>=20) {
  128. my $frameCount = hex(substr($msg,16,2));
  129. my $headerCRC = hex(substr($msg,18,2));
  130. my $crc = 0;
  131. for (my $j = 1; $j<=8;$j++) {
  132. $crc += hex(substr($msg,$j*2,2));
  133. }
  134. $crc = ($crc ^ 0xff) & 0x7f;
  135. if ($headerCRC != $crc) {
  136. Log3 $hash, 3, "$name: VBUSIF_Read2: Wrong checksum: $crc != $headerCRC";
  137. } else {
  138. my $len = 20+12*$frameCount;
  139. Log3 $hash->{NAME}, 5,"$name: VBUSIF_Read2a Len: ".$len." Counter: ".$frameCount;
  140. if ($len != length($msg)) {
  141. # Fehler bei aa1000277310000103414a7f1300071c00001401006a62023000016aaa000021732000050000000000000046a
  142. # ^ hier wird falsch getrennt
  143. $msg = substr($msg2,0,$len);
  144. Log3 $hash->{NAME}, 5,"$name: VBUSIF_Read2b MSG: ".$msg;
  145. }
  146. if ($len != length($msg)) {
  147. #if ($len != length($msg) && length($msg) != 247) {
  148. Log3 $hash->{NAME}, 5,"$name: VBUSIF_Read3: Wrong message length: $len != ".length($msg);
  149. } else {
  150. Log3 $hash->{NAME}, 5,"$name: VBUSIF_Read4: OK message length: $len : ".length($msg);
  151. if(length($msg) == 247) {
  152. $msg = $msg."a";
  153. Log3 $hash->{NAME}, 5,"$name: VBUSIF_Read5: message + a : ".$msg;
  154. }
  155. my $payload = VBUSIF_DecodePayload($hash,$msg);
  156. if (defined $payload) {
  157. $msg = substr($msg,0,20).$payload;
  158. Log3 $hash, 4,"$name: VBUSIF_Read6 MSG: ".$msg." Payload: ".$payload;
  159. $hash->{"${name}_MSGCNT"}++;
  160. $hash->{"${name}_TIME"} = TimeNow();
  161. $hash->{RAWMSG} = $msg;
  162. my %addvals = (RAWMSG => $msg);
  163. Dispatch($hash, $msg, \%addvals) if($init_done);
  164. }
  165. }
  166. }
  167. }
  168. if ($protoVersion == "20") {
  169. my $command = substr($msg,14,2).substr($msg,12,2);
  170. my $dataPntId = substr($msg,16,4);
  171. my $dataPntVal = substr($msg,20,8);
  172. my $septet = substr($msg,28,2);
  173. my $checksum = substr($msg,30,2);
  174. Log3 $hash->{NAME}, 5,"$name: VBUSIF_Read7: protoVersion : $protoVersion";
  175. # TODO use septet
  176. # TODO validate checksum
  177. # TODO Understand protocol
  178. }
  179. Log3 $hash->{NAME}, 5,"$name: VBUSIF_Read8: raus ";
  180. }
  181. }
  182. $hash->{PARTIAL} = $data;
  183. # return $msg if(defined($local));
  184. return undef;
  185. }
  186. sub VBUSIF_Ready($)
  187. {
  188. my ($hash) = @_;
  189. return DevIo_OpenDev($hash, 1, "VBUSIF_DoInit") if($hash->{STATE} eq "disconnected");
  190. return 0;
  191. }
  192. sub VBUSIF_DecodePayload($@)
  193. {
  194. my ($hash, $msg) = @_;
  195. my $name = $hash->{NAME};
  196. my $frameCount = hex(substr($msg,16,2));
  197. my $payload = "";
  198. for (my $i = 0; $i < $frameCount; $i++) {
  199. my $septet = hex(substr($msg,28+$i*12,2));
  200. my $frameCRC = hex(substr($msg,30+$i*12,2));
  201. my $crc = (0x7f - $septet) & 0x7f;
  202. for (my $j = 0; $j<4;$j++) {
  203. my $ch = hex(substr($msg,20+$i*12+$j*2,2));
  204. $ch |= 0x80 if ($septet & (1 << $j));
  205. $crc = ($crc - $ch) & 0x7f;
  206. $payload .= chr($ch);
  207. }
  208. if ($crc != $frameCRC) {
  209. Log3 $hash, 4,"$name: VBUSIF_DecodePayload0: Wrong checksum: $crc != $frameCRC";
  210. return undef;
  211. }
  212. }
  213. return unpack('H*', $payload);
  214. }
  215. 1;
  216. =pod
  217. =item device
  218. =item summary connects to the RESOL VBUS LAN or Serial Port adapter
  219. =item summary_DE verbindet sich mit einem RESOL VBUS LAN oder Seriell Adapter
  220. =begin html
  221. <a name="VBUSIF"></a>
  222. <h3>VBUSIF</h3>
  223. <ul>
  224. This module connects to the RESOL VBUS LAN or Serial Port adapter.
  225. It serves as the "physical" counterpart to the <a href="#VBUSDevice">VBUSDevice</a>
  226. devices.
  227. <br /><br />
  228. <a name="VBUSIF_Define"></a>
  229. <b>Define</b>
  230. <ul>
  231. <code>define &lt;name&gt; VBUS &lt;device&gt;</code>
  232. <br />
  233. <br />
  234. &lt;device&gt; is a &lt;host&gt;:&lt;port&gt; combination, where
  235. &lt;host&gt; is the address of the RESOL LAN Adapter and &lt;port&gt; 7053.
  236. <br />
  237. Please note: the password of RESOL Device must be unchanged at &lt;host&gt;
  238. <br />
  239. Examples:
  240. <ul>
  241. <code>define vbus VBUSIF 192.168.1.69:7053</code>
  242. </ul>
  243. <ul>
  244. <code>define vbus VBUSIF /dev/ttyS0</code>
  245. </ul>
  246. </ul>
  247. <br />
  248. </ul>
  249. =end html
  250. =cut