SHC_datafields.pm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #!/usr/bin/perl
  2. ##########################################################################
  3. # This file is part of the smarthomatic module for FHEM.
  4. #
  5. # Copyright (c) 2014 Uwe Freese
  6. #
  7. # You can find smarthomatic at www.smarthomatic.org.
  8. # You can find FHEM at www.fhem.de.
  9. #
  10. # This file is free software: you can redistribute it and/or modify it
  11. # under the terms of the GNU General Public License as published by the
  12. # Free Software Foundation, either version 3 of the License, or (at your
  13. # option) any later version.
  14. #
  15. # This file is distributed in the hope that it will be useful, but
  16. # WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  18. # Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
  22. ##########################################################################
  23. # $Id: SHC_datafields.pm 8182 2015-03-09 19:03:15Z rr2000 $
  24. package SHC_util;
  25. # ----------- helper functions -----------
  26. sub max($$)
  27. {
  28. my ($x, $y) = @_;
  29. return $x >= $y ? $x : $y;
  30. }
  31. sub min($$)
  32. {
  33. my ($x, $y) = @_;
  34. return $x <= $y ? $x : $y;
  35. }
  36. # clear some bits within a byte
  37. sub clear_bits($$$)
  38. {
  39. my ($input, $bit, $bits_to_clear) = @_;
  40. my $mask = (~((((1 << $bits_to_clear) - 1)) << (8 - $bits_to_clear - $bit)));
  41. return ($input & $mask);
  42. }
  43. # get some bits from a 32 bit value, counted from the left (MSB) side! The first bit is bit nr. 0.
  44. sub get_bits($$$)
  45. {
  46. my ($input, $bit, $len) = @_;
  47. return ($input >> (32 - $len - $bit)) & ((1 << $len) - 1);
  48. }
  49. sub getUInt($$$)
  50. {
  51. my ($byteArrayRef, $offset, $length_bits) = @_;
  52. my $byte = $offset / 8;
  53. my $bit = $offset % 8;
  54. my $byres_read = 0;
  55. my $val = 0;
  56. my $shiftBits;
  57. # read the bytes one after another, shift them to the correct position and add them
  58. while ($length_bits + $bit > $byres_read * 8) {
  59. $shiftBits = $length_bits + $bit - $byres_read * 8 - 8;
  60. my $zz = @$byteArrayRef[$byte + $byres_read];
  61. if ($shiftBits >= 0) {
  62. $val += $zz << $shiftBits;
  63. } else {
  64. $val += $zz >> -$shiftBits;
  65. }
  66. $byres_read++;
  67. }
  68. # filter out only the wanted bits and clear unwanted upper bits
  69. if ($length_bits < 32) {
  70. $val = $val & ((1 << $length_bits) - 1);
  71. }
  72. return $val;
  73. }
  74. # write some bits to byte array only within one byte
  75. sub setUIntBits($$$$$)
  76. {
  77. my ($byteArrayRef, $byte, $bit, $length_bits, $val8) = @_;
  78. my $b = 0;
  79. # if length is smaller than 8 bits, get the old value from array
  80. if ($length_bits < 8) {
  81. $b = @$byteArrayRef[$byte];
  82. $b = clear_bits($b, $bit, $length_bits);
  83. }
  84. # set bits from given value
  85. $b = $b | ($val8 << (8 - $length_bits - $bit));
  86. @$byteArrayRef[$byte] = $b;
  87. }
  88. # Write UIntValue to data array
  89. sub setUInt($$$$)
  90. {
  91. my ($byteArrayRef, $offset, $length_bits, $value) = @_;
  92. my $byte = int($offset / 8);
  93. my $bit = $offset % 8;
  94. # move bits to the left border
  95. $value = $value << (32 - $length_bits);
  96. # DEBUG print "Moved left: val " . $value . "\r\n";
  97. # 1st byte
  98. my $src_start = 0;
  99. my $dst_start = $bit;
  100. my $len = min($length_bits, 8 - $bit);
  101. my $val8 = get_bits($value, $src_start, $len);
  102. # DEBUG print " Write value " . $val8 . " (" . $len . " bits) to byte " . $byte . ", dst_start " . $dst_start . "\r\n";
  103. setUIntBits($byteArrayRef, $byte, $dst_start, $len, $val8);
  104. $dst_start = 0;
  105. $src_start = $len;
  106. while ($src_start < $length_bits) {
  107. $len = min($length_bits - $src_start, 8);
  108. $val8 = get_bits($value, $src_start, $len);
  109. $byte++;
  110. # DEBUG print " Write value " . $val8 . " (" . $len . " bits) from src_start " . $src_start . " to byte " . $byte . ", dst_start " . $dst_start . "\r\n";
  111. setUIntBits($byteArrayRef, $byte, $dst_start, $len, $val8);
  112. $src_start += $len;
  113. }
  114. }
  115. sub getInt($$$)
  116. {
  117. my ($byteArrayRef, $offset, $length_bits) = @_;
  118. $x = getUInt($byteArrayRef, $offset, $length_bits);
  119. if ($x >= 2 ** ($length_bits - 1))
  120. {
  121. $x = $x - 2 ** $length_bits;
  122. }
  123. # DEBUG print "UInt = " . $x . ", length_bits = " . length_bits . "\r\n";
  124. return $x;
  125. }
  126. # ----------- UIntValue class -----------
  127. package UIntValue;
  128. sub new
  129. {
  130. my $class = shift;
  131. my $self = {
  132. _id => shift,
  133. _offset => shift,
  134. _bits => shift,
  135. _length => shift,
  136. _arrayElementBits => shift
  137. };
  138. bless $self, $class;
  139. return $self;
  140. }
  141. sub getValue
  142. {
  143. my ($self, $byteArrayRef, $index) = @_;
  144. return SHC_util::getUInt($byteArrayRef, $self->{_offset} + $self->{_arrayElementBits} * $index, $self->{_bits});
  145. }
  146. sub setValue
  147. {
  148. my ($self, $byteArrayRef, $value, $index) = @_;
  149. SHC_util::setUInt($byteArrayRef, $self->{_offset} + $self->{_arrayElementBits} * $index, $self->{_bits}, $value);
  150. }
  151. # ----------- IntValue class -----------
  152. package IntValue;
  153. sub new
  154. {
  155. my $class = shift;
  156. my $self = {
  157. _id => shift,
  158. _offset => shift,
  159. _bits => shift,
  160. _length => shift,
  161. _arrayElementBits => shift
  162. };
  163. bless $self, $class;
  164. return $self;
  165. }
  166. sub getValue
  167. {
  168. my ($self, $byteArrayRef, $index) = @_;
  169. return SHC_util::getInt($byteArrayRef, $self->{_offset} + $self->{_arrayElementBits} * $index, $self->{_bits});
  170. }
  171. sub setValue
  172. {
  173. my ($self, $byteArrayRef, $value, $index) = @_;
  174. SHC_util::setInt($byteArrayRef, $self->{_offset} + $self->{_arrayElementBits} * $index, $self->{_bits}, $value);
  175. }
  176. # ----------- BoolValue class -----------
  177. package BoolValue;
  178. sub new
  179. {
  180. my $class = shift;
  181. my $self = {
  182. _id => shift,
  183. _offset => shift,
  184. _length => shift,
  185. _arrayElementBits => shift
  186. };
  187. bless $self, $class;
  188. return $self;
  189. }
  190. sub getValue
  191. {
  192. my ($self, $byteArrayRef, $index) = @_;
  193. return SHC_util::getUInt($byteArrayRef, $self->{_offset} + $self->{_arrayElementBits} * $index, 1) == 1 ? 1 : 0;
  194. }
  195. sub setValue
  196. {
  197. my ($self, $byteArrayRef, $value, $index) = @_;
  198. return SHC_util::setUInt($byteArrayRef, $self->{_offset} + $self->{_arrayElementBits} * $index, 1,
  199. $value == 0 ? 0 : 1);
  200. }
  201. # ----------- EnumValue class -----------
  202. package EnumValue;
  203. my %name2value = ();
  204. my %value2name = ();
  205. sub new
  206. {
  207. my $class = shift;
  208. my $self = {
  209. _id => shift,
  210. _offset => shift,
  211. _bits => shift,
  212. _length => shift,
  213. _arrayElementBits => shift
  214. };
  215. bless $self, $class;
  216. return $self;
  217. }
  218. sub addValue
  219. {
  220. my ($self, $name, $value) = @_;
  221. $name2value{$name} = $value;
  222. $value2name{$value} = $name;
  223. }
  224. sub getValue
  225. {
  226. my ($self, $byteArrayRef, $index) = @_;
  227. my $value = SHC_util::getUInt($byteArrayRef, $self->{_offset} + $self->{_arrayElementBits} * $index, $self->{_bits});
  228. return $value2name{$value};
  229. }
  230. sub setValue
  231. {
  232. my ($self, $byteArrayRef, $name, $index) = @_;
  233. my $value = $name2value{$name};
  234. SHC_util::setUInt($byteArrayRef, $self->{_offset} + $self->{_arrayElementBits} * $index, $self->{_bits}, $value);
  235. }
  236. 1;