line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
######################################################################################### |
2
|
|
|
|
|
|
|
# Package HiPi::RF::OpenThings::Message |
3
|
|
|
|
|
|
|
# Description : Handle OpenThings protocol message |
4
|
|
|
|
|
|
|
# Copyright : Copyright (c) 2013-2017 Mark Dootson |
5
|
|
|
|
|
|
|
# License : This is free software; you can redistribute it and/or modify it under |
6
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
7
|
|
|
|
|
|
|
######################################################################################### |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package HiPi::RF::OpenThings::Message; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
######################################################################################### |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
14
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
15
|
1
|
|
|
1
|
|
22
|
use parent qw( HiPi::Class ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
16
|
1
|
|
|
1
|
|
67
|
use HiPi qw( :openthings :energenie ); |
|
1
|
|
|
|
|
39
|
|
|
1
|
|
|
|
|
404
|
|
17
|
1
|
|
|
1
|
|
405
|
use HiPi::RF::OpenThings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
18
|
1
|
|
|
1
|
|
575
|
use Try::Tiny; |
|
1
|
|
|
|
|
2140
|
|
|
1
|
|
|
|
|
84
|
|
19
|
1
|
|
|
1
|
|
688
|
use JSON; |
|
1
|
|
|
|
|
10283
|
|
|
1
|
|
|
|
|
17
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION ='0.80'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
__PACKAGE__->create_accessors( qw( |
24
|
|
|
|
|
|
|
cryptseed |
25
|
|
|
|
|
|
|
errorbuffer |
26
|
|
|
|
|
|
|
databuffer |
27
|
|
|
|
|
|
|
epoch |
28
|
|
|
|
|
|
|
length |
29
|
|
|
|
|
|
|
ok |
30
|
|
|
|
|
|
|
configured_name |
31
|
|
|
|
|
|
|
records |
32
|
|
|
|
|
|
|
_mid |
33
|
|
|
|
|
|
|
_pid |
34
|
|
|
|
|
|
|
_sid |
35
|
|
|
|
|
|
|
_pip |
36
|
|
|
|
|
|
|
is_decoded |
37
|
|
|
|
|
|
|
is_encoded |
38
|
|
|
|
|
|
|
has_join_cmd |
39
|
|
|
|
|
|
|
has_join_ack |
40
|
|
|
|
|
|
|
switch_state |
41
|
|
|
|
|
|
|
switch_command |
42
|
|
|
|
|
|
|
has_command |
43
|
|
|
|
|
|
|
)); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub new { |
46
|
0
|
|
|
0
|
0
|
|
my( $class, %params ) = @_; |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my( $sk_mid, $sk_pid, $sk_sid ) = ( 0, 0, 0 ); |
49
|
|
|
|
|
|
|
|
50
|
0
|
0
|
|
|
|
|
if($params{sensor_key}) { |
51
|
0
|
|
|
|
|
|
( $sk_mid, $sk_pid, $sk_sid ) = split(/-/, $params{sensor_key} ); |
52
|
0
|
|
|
|
|
|
for ( $sk_mid, $sk_pid, $sk_sid ) { |
53
|
0
|
|
|
|
|
|
$_ = hex($_); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
$params{epoch} = time(); |
58
|
0
|
|
|
|
|
|
$params{errorbuffer} = []; |
59
|
0
|
|
0
|
|
|
|
$params{databuffer} //= []; |
60
|
0
|
|
0
|
|
|
|
$params{cryptseed} //= 1; |
61
|
0
|
|
|
|
|
|
$params{records} = []; |
62
|
0
|
|
0
|
|
|
|
$params{_mid} = $sk_mid || $params{mid} || $params{manufacturer_id} || 0; |
63
|
0
|
|
0
|
|
|
|
$params{_pid} = $sk_pid || $params{pid} || $params{product_id} || 0; |
64
|
0
|
|
0
|
|
|
|
$params{_sid} = $sk_sid || $params{sid} || $params{sensor_id} || 0; |
65
|
0
|
|
0
|
|
|
|
$params{_pip} = $params{pip} || $params{encrypt_pip} || 0; |
66
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new( %params ); |
67
|
0
|
|
|
|
|
|
return $self; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
0
|
0
|
|
sub manufacturer_id { return $_[0]->_mid; } |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
0
|
0
|
|
sub product_id { return $_[0]->_pid; } |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
0
|
0
|
|
sub sensor_id { return $_[0]->_sid; } |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
0
|
0
|
|
sub encrypt_pip { return $_[0]->_pip; } |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub has_switch_state { |
79
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
80
|
0
|
0
|
|
|
|
|
return ( defined($self->switch_state) ) ? 1 : 0; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub sensor_key { |
84
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
85
|
0
|
|
|
|
|
|
return HiPi::RF::OpenThings->format_sensor_key($self->manufacturer_id, $self->product_id, $self->sensor_id); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub product_name { |
89
|
0
|
|
|
0
|
0
|
|
my( $self ) = @_; |
90
|
0
|
|
|
|
|
|
return HiPi::RF::OpenThings->product_name($self->manufacturer_id, $self->product_id ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub manufacturer_name { |
94
|
0
|
|
|
0
|
0
|
|
my( $self ) = @_; |
95
|
0
|
|
|
|
|
|
return HiPi::RF::OpenThings->manufacturer_name($self->manufacturer_id ); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub value_hash { |
99
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
100
|
0
|
0
|
|
|
|
|
$self->decode_buffer unless $self->is_decoded; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
0
|
|
|
|
my $data = { |
103
|
|
|
|
|
|
|
timestamp => $self->timestamp, |
104
|
|
|
|
|
|
|
manufacturer_id => $self->manufacturer_id, |
105
|
|
|
|
|
|
|
product_id => $self->product_id, |
106
|
|
|
|
|
|
|
product_name => $self->product_name, |
107
|
|
|
|
|
|
|
sensor_id => $self->sensor_id, |
108
|
|
|
|
|
|
|
sensor_key => $self->sensor_key, |
109
|
|
|
|
|
|
|
records => [], |
110
|
|
|
|
|
|
|
manufacturer_name => $self->manufacturer_name, |
111
|
|
|
|
|
|
|
configured_name => $self->configured_name || '', |
112
|
|
|
|
|
|
|
}; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
for my $record ( @{ $self->records } ) { |
|
0
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
push( @{ $data->{records} }, |
|
0
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
{ |
117
|
|
|
|
|
|
|
name => $record->name, |
118
|
|
|
|
|
|
|
value => $record->value, |
119
|
|
|
|
|
|
|
units => $record->units, |
120
|
|
|
|
|
|
|
command => $record->command, |
121
|
|
|
|
|
|
|
id => $record->id, |
122
|
|
|
|
|
|
|
value_type => $record->typeid, |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
); |
125
|
|
|
|
|
|
|
|
126
|
0
|
0
|
|
|
|
|
if( $record->id == OPENTHINGS_PARAM_JOIN ) { |
127
|
0
|
0
|
|
|
|
|
if( $record->command ) { |
128
|
0
|
|
|
|
|
|
$data->{join_command} = 1; |
129
|
|
|
|
|
|
|
} else { |
130
|
0
|
|
|
|
|
|
$data->{join_ack} = 1; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
if( $record->id == OPENTHINGS_PARAM_SWITCH_STATE ) { |
135
|
0
|
|
|
|
|
|
$data->{switch_state} = $record->value; |
136
|
0
|
0
|
|
|
|
|
if( $record->command ) { |
137
|
0
|
|
|
|
|
|
$data->{switch_command} = 1; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
0
|
|
|
|
|
|
return $data; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub json { |
145
|
0
|
|
|
0
|
0
|
|
my ($self, $pretty) = @_; |
146
|
0
|
|
|
|
|
|
my $data = $self->value_hash; |
147
|
|
|
|
|
|
|
my $output = try { |
148
|
0
|
|
|
0
|
|
|
my $j = JSON->new; |
149
|
0
|
0
|
|
|
|
|
my $return = ( $pretty ) ? $j->pretty->canonical->encode( $data ) : $j->encode( $data ); |
150
|
0
|
|
|
|
|
|
return $return; |
151
|
|
|
|
|
|
|
} catch { |
152
|
0
|
|
|
0
|
|
|
my $error = $_; |
153
|
0
|
|
|
|
|
|
$error =~ s/[\n"']+/ /g; |
154
|
0
|
|
|
|
|
|
return qq({"ok":0,"error":"$error"}); |
155
|
0
|
|
|
|
|
|
}; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub encode_buffer { |
159
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
160
|
|
|
|
|
|
|
my $result = try { |
161
|
0
|
|
|
0
|
|
|
my $payload = [ 0 ]; # len we will assign later |
162
|
0
|
|
|
|
|
|
push @$payload, $self->manufacturer_id; |
163
|
0
|
|
|
|
|
|
push @$payload, $self->product_id; |
164
|
0
|
|
|
|
|
|
push @$payload, ( $self->encrypt_pip >> 8 ) & 0xFF; |
165
|
0
|
|
|
|
|
|
push @$payload, $self->encrypt_pip & 0xFF; |
166
|
0
|
|
|
|
|
|
push @$payload, ( $self->sensor_id >> 16 ) & 0xFF; |
167
|
0
|
|
|
|
|
|
push @$payload, ( $self->sensor_id >> 8 ) & 0xFF; |
168
|
0
|
|
|
|
|
|
push @$payload, $self->sensor_id & 0xFF; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
for my $record ( @{ $self->records } ) { |
|
0
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# get some convenience values |
172
|
|
|
|
|
|
|
|
173
|
0
|
0
|
|
|
|
|
$self->has_command(1) if $record->command; |
174
|
|
|
|
|
|
|
|
175
|
0
|
0
|
|
|
|
|
if( $record->id == OPENTHINGS_PARAM_JOIN ) { |
|
|
0
|
|
|
|
|
|
176
|
0
|
0
|
|
|
|
|
if( $record->command ) { |
177
|
0
|
|
|
|
|
|
$self->has_join_cmd(1); |
178
|
|
|
|
|
|
|
} else { |
179
|
0
|
|
|
|
|
|
$self->has_join_ack(1); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
} elsif($record->id == OPENTHINGS_PARAM_SWITCH_STATE ) { |
183
|
0
|
|
|
|
|
|
$self->switch_state( $record->value ); |
184
|
0
|
0
|
|
|
|
|
if( $record->command ) { |
185
|
0
|
|
|
|
|
|
$self->switch_command(1); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
0
|
0
|
|
|
|
|
my $writemask = ( $record->command ) ? OPENTHINGS_WRITE_MASK : 0x0; |
190
|
|
|
|
|
|
|
# ID and R/W |
191
|
0
|
|
|
|
|
|
push @$payload, $record->id | $writemask; |
192
|
|
|
|
|
|
|
# Type (will or/| length later) |
193
|
0
|
|
|
|
|
|
push @$payload, $record->typeid & 0xF0; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# record position of type/length byte |
196
|
0
|
|
|
|
|
|
my $lenpos = ( scalar @$payload ) -1; |
197
|
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
|
my $val = $record->value; |
199
|
0
|
0
|
0
|
|
|
|
if(defined($val) && $val ne '') { |
200
|
0
|
|
|
|
|
|
my @valbytes = $self->encode_value( $record->typeid, $val ); |
201
|
0
|
|
|
|
|
|
my $vallen = scalar( @valbytes ); |
202
|
|
|
|
|
|
|
# max record length is 15 bytes; |
203
|
0
|
0
|
|
|
|
|
$vallen = 15 if $vallen > 15; |
204
|
0
|
|
|
|
|
|
$payload->[$lenpos] = ( $record->typeid & 0xF0 ) | ( $vallen & 0xF ); |
205
|
0
|
|
|
|
|
|
for (my $vindex = 0; $vindex < $vallen; $vindex ++ ) { |
206
|
0
|
|
|
|
|
|
push @$payload, $valbytes[$vindex]; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
# FOOTER |
212
|
0
|
|
|
|
|
|
push @$payload, 0; #NUL |
213
|
|
|
|
|
|
|
|
214
|
0
|
|
|
|
|
|
$self->databuffer( $payload ); |
215
|
0
|
|
|
|
|
|
my $crc = $self->calculate_crc( 1 ); |
216
|
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
push @$payload, ( $crc >> 8 ) & 0xFF; |
218
|
0
|
|
|
|
|
|
push @$payload, $crc & 0xFF; |
219
|
|
|
|
|
|
|
|
220
|
0
|
|
|
|
|
|
$payload->[0] = ( scalar @$payload ) -1; |
221
|
|
|
|
|
|
|
|
222
|
0
|
|
|
|
|
|
$self->crypt_buffer; |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
return 1; |
225
|
|
|
|
|
|
|
} catch { |
226
|
0
|
|
|
0
|
|
|
die $_; |
227
|
0
|
|
|
|
|
|
$self->push_error(q(unexpected error in message encode ) . $_); |
228
|
0
|
|
|
|
|
|
return 0; |
229
|
0
|
|
|
|
|
|
}; |
230
|
0
|
|
|
|
|
|
$self->is_encoded(1); |
231
|
0
|
|
|
|
|
|
$self->ok( $result ); |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
sub inspect_buffer { |
236
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
237
|
|
|
|
|
|
|
my $result = try { |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
# check basic message length |
240
|
|
|
|
|
|
|
{ |
241
|
0
|
|
|
0
|
|
|
my $bytelength = $self->buffer_length; |
|
0
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
|
243
|
0
|
0
|
0
|
|
|
|
if ( ( $bytelength < 11 ) |
244
|
|
|
|
|
|
|
|| ( $self->databuffer->[0] + 1 != $bytelength ) ) { |
245
|
0
|
|
|
|
|
|
$self->push_error(q(invalid message length ) . $bytelength); |
246
|
0
|
|
|
|
|
|
return 0; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
0
|
|
|
|
|
|
$self->_mid( $self->databuffer->[1] ); |
251
|
0
|
|
|
|
|
|
$self->_pid( $self->databuffer->[2] ); |
252
|
0
|
|
|
|
|
|
$self->_pip( ( $self->databuffer->[3] << 8 ) + $self->databuffer->[4] ); |
253
|
|
|
|
|
|
|
|
254
|
0
|
|
|
|
|
|
return 1; |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
} catch { |
257
|
0
|
|
|
0
|
|
|
return 0; |
258
|
0
|
|
|
|
|
|
}; |
259
|
|
|
|
|
|
|
|
260
|
0
|
|
|
|
|
|
return $result; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub decode_buffer { |
264
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
265
|
|
|
|
|
|
|
my $result = try { |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
# clear records |
268
|
0
|
|
|
0
|
|
|
$self->records([]); |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
# check basic message length |
271
|
|
|
|
|
|
|
{ |
272
|
0
|
|
|
|
|
|
my $bytelength = $self->buffer_length; |
|
0
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
|
274
|
0
|
0
|
0
|
|
|
|
if ( ( $bytelength < 11 ) |
275
|
|
|
|
|
|
|
|| ( $self->databuffer->[0] + 1 != $bytelength ) ) { |
276
|
0
|
|
|
|
|
|
$self->push_error(q(invalid message length ) . $bytelength); |
277
|
0
|
|
|
|
|
|
return 0; |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
0
|
|
|
|
|
|
my $payload = $self->databuffer; |
282
|
|
|
|
|
|
|
|
283
|
0
|
|
|
|
|
|
$self->_mid( $payload->[1] ); |
284
|
0
|
|
|
|
|
|
$self->_pid( $payload->[2] ); |
285
|
0
|
|
|
|
|
|
$self->_pip( ( $payload->[3] << 8 ) + $payload->[4] ); |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
# new we have the manufacturer and product id we can optionally |
288
|
|
|
|
|
|
|
# decrypt the message |
289
|
|
|
|
|
|
|
|
290
|
0
|
|
|
|
|
|
$self->crypt_buffer; |
291
|
|
|
|
|
|
|
|
292
|
0
|
|
|
|
|
|
$self->_sid( ( $payload->[5] << 16 ) + ( $payload->[6] << 8 ) + $payload->[7] ); |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
# check CRC to see if this is good message |
295
|
0
|
|
|
|
|
|
my $crc_sent = ( $payload->[-2] << 8 ) + $payload->[-1]; |
296
|
0
|
|
|
|
|
|
my $crc_calculated= $self->calculate_crc; |
297
|
0
|
0
|
|
|
|
|
if ( $crc_sent != $crc_calculated ) { |
298
|
0
|
|
|
|
|
|
$self->push_error(qq(invalid CRC - got $crc_sent, expected $crc_calculated)); |
299
|
0
|
|
|
|
|
|
return 0; |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
# decode the records |
303
|
|
|
|
|
|
|
|
304
|
0
|
|
|
|
|
|
my $index = 8; |
305
|
0
|
|
|
|
|
|
my @records; |
306
|
0
|
|
0
|
|
|
|
while ( ( $index < @$payload -2 ) && $payload->[$index] != 0 ) { |
307
|
0
|
|
|
|
|
|
my $param = $payload->[$index]; |
308
|
0
|
0
|
|
|
|
|
my $command = (( $param & OPENTHINGS_WRITE_MASK ) == OPENTHINGS_WRITE_MASK ) ? 1 : 0; |
309
|
0
|
|
|
|
|
|
my $paramid = $param & 0x7F; |
310
|
0
|
|
|
|
|
|
my ( $paramname, $paramunit ) = HiPi::RF::OpenThings->parameter_map( $paramid ); |
311
|
0
|
|
|
|
|
|
$index ++; |
312
|
0
|
|
|
|
|
|
my $typeid = $payload->[$index] & 0xF0; |
313
|
0
|
|
|
|
|
|
my $paramlen = $payload->[$index] & 0x0F; |
314
|
0
|
|
|
|
|
|
$index ++; |
315
|
|
|
|
|
|
|
|
316
|
0
|
|
|
|
|
|
my $record = { |
317
|
|
|
|
|
|
|
command => $command, |
318
|
|
|
|
|
|
|
id => $paramid, |
319
|
|
|
|
|
|
|
name => $paramname, |
320
|
|
|
|
|
|
|
units => $paramunit, |
321
|
|
|
|
|
|
|
typeid => $typeid, |
322
|
|
|
|
|
|
|
length => $paramlen, |
323
|
|
|
|
|
|
|
value => '', |
324
|
|
|
|
|
|
|
bytes => [], |
325
|
|
|
|
|
|
|
}; |
326
|
|
|
|
|
|
|
|
327
|
0
|
0
|
|
|
|
|
if ( $paramlen != 0 ) { |
328
|
0
|
|
|
|
|
|
my @valuebytes = (); |
329
|
0
|
|
|
|
|
|
for (my $i = 0; $i < $paramlen; $i++ ) { |
330
|
0
|
|
|
|
|
|
push @valuebytes, $payload->[$index]; |
331
|
0
|
|
|
|
|
|
$index ++; |
332
|
|
|
|
|
|
|
} |
333
|
|
|
|
|
|
|
|
334
|
0
|
0
|
|
|
|
|
if ( $paramlen != @valuebytes ) { |
335
|
0
|
|
|
|
|
|
$self->push_error('length of bytes for param incorrect'); |
336
|
0
|
|
|
|
|
|
return 0; |
337
|
|
|
|
|
|
|
} |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
|
340
|
0
|
|
|
|
|
|
my $value = $self->decode_value($typeid, \@valuebytes ); |
341
|
0
|
|
|
|
|
|
$record->{value} = $value; |
342
|
0
|
|
|
|
|
|
$record->{bytes} = \@valuebytes; |
343
|
|
|
|
|
|
|
} |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
# get some convenience values |
346
|
|
|
|
|
|
|
|
347
|
0
|
0
|
|
|
|
|
$self->has_command(1) if $record->{command}; |
348
|
|
|
|
|
|
|
|
349
|
0
|
0
|
|
|
|
|
if( $record->{id} == OPENTHINGS_PARAM_JOIN ) { |
350
|
0
|
0
|
|
|
|
|
if( $record->{command} ) { |
351
|
0
|
|
|
|
|
|
$self->has_join_cmd(1); |
352
|
|
|
|
|
|
|
} else { |
353
|
0
|
|
|
|
|
|
$self->has_join_ack(1); |
354
|
|
|
|
|
|
|
} |
355
|
|
|
|
|
|
|
} |
356
|
|
|
|
|
|
|
|
357
|
0
|
0
|
|
|
|
|
if( $record->{id} == OPENTHINGS_PARAM_SWITCH_STATE ) { |
358
|
0
|
0
|
0
|
|
|
|
if(defined($record->{value}) && $record->{value} ne '' ) { |
359
|
0
|
|
|
|
|
|
$self->switch_state( $record->{value} ); |
360
|
0
|
0
|
|
|
|
|
if( $record->{command} ) { |
361
|
0
|
|
|
|
|
|
$self->switch_command(1); |
362
|
|
|
|
|
|
|
} |
363
|
|
|
|
|
|
|
} |
364
|
|
|
|
|
|
|
} |
365
|
|
|
|
|
|
|
|
366
|
0
|
|
|
|
|
|
push @records, $record; |
367
|
|
|
|
|
|
|
} |
368
|
|
|
|
|
|
|
|
369
|
0
|
|
|
|
|
|
for my $record ( @records ) { |
370
|
0
|
|
|
|
|
|
$self->add_record(%$record); |
371
|
|
|
|
|
|
|
} |
372
|
0
|
|
|
|
|
|
return 1; |
373
|
|
|
|
|
|
|
} catch { |
374
|
0
|
|
|
0
|
|
|
$self->push_error(q(unexpected error in message decode ) . $_); |
375
|
0
|
|
|
|
|
|
return 0; |
376
|
0
|
|
|
|
|
|
}; |
377
|
|
|
|
|
|
|
|
378
|
0
|
|
|
|
|
|
$self->is_decoded(1); |
379
|
0
|
|
|
|
|
|
$self->ok( $result ); |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
sub get_value_type_bits { |
383
|
0
|
|
|
0
|
0
|
|
my($self, $typeid) = @_; |
384
|
|
|
|
|
|
|
|
385
|
0
|
|
|
|
|
|
my $rval = 1; |
386
|
|
|
|
|
|
|
|
387
|
0
|
0
|
|
|
|
|
if ($typeid == OPENTHINGS_UINT_BP4 ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
388
|
0
|
|
|
|
|
|
$rval = 4; |
389
|
|
|
|
|
|
|
} elsif($typeid == OPENTHINGS_UINT_BP8 ) { |
390
|
0
|
|
|
|
|
|
$rval = 8; |
391
|
|
|
|
|
|
|
} elsif($typeid == OPENTHINGS_UINT_BP12 ) { |
392
|
0
|
|
|
|
|
|
$rval = 12; |
393
|
|
|
|
|
|
|
} elsif($typeid == OPENTHINGS_UINT_BP16 ) { |
394
|
0
|
|
|
|
|
|
$rval = 16; |
395
|
|
|
|
|
|
|
} elsif($typeid == OPENTHINGS_UINT_BP20 ) { |
396
|
0
|
|
|
|
|
|
$rval = 20; |
397
|
|
|
|
|
|
|
} elsif($typeid == OPENTHINGS_UINT_BP24 ) { |
398
|
0
|
|
|
|
|
|
$rval = 24; |
399
|
|
|
|
|
|
|
} elsif($typeid == OPENTHINGS_SINT_BP8 ) { |
400
|
0
|
|
|
|
|
|
$rval = 8; |
401
|
|
|
|
|
|
|
} elsif($typeid == OPENTHINGS_SINT_BP16 ) { |
402
|
0
|
|
|
|
|
|
$rval = 16; |
403
|
|
|
|
|
|
|
} elsif($typeid == OPENTHINGS_SINT_BP24 ) { |
404
|
0
|
|
|
|
|
|
$rval = 24; |
405
|
|
|
|
|
|
|
} |
406
|
0
|
|
|
|
|
|
return $rval; |
407
|
|
|
|
|
|
|
} |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
sub get_value_bits { |
410
|
0
|
|
|
0
|
0
|
|
my($self, $value) = @_; |
411
|
0
|
0
|
|
|
|
|
return 2 if $value == -1; |
412
|
|
|
|
|
|
|
# Turn into 2's |
413
|
0
|
|
|
|
|
|
my $maxbytes = 15; |
414
|
0
|
|
|
|
|
|
my $maxbits = 1 << ( $maxbytes * 8 ); |
415
|
0
|
|
|
|
|
|
$value = $value & $maxbits -1; |
416
|
|
|
|
|
|
|
|
417
|
0
|
|
|
|
|
|
my $bits = 2 + $self->get_highest_clear_bit($value, $maxbytes * 8 ); |
418
|
|
|
|
|
|
|
|
419
|
0
|
|
|
|
|
|
return $bits; |
420
|
|
|
|
|
|
|
} |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
sub get_highest_clear_bit { |
423
|
0
|
|
|
0
|
0
|
|
my($self, $value, $maxbits) = @_; |
424
|
|
|
|
|
|
|
|
425
|
0
|
|
|
|
|
|
my $mask = 1 << ( $maxbits -1 ); |
426
|
0
|
|
|
|
|
|
my $bitno = $maxbits - 1; |
427
|
0
|
|
|
|
|
|
while( $mask != 0 ) { |
428
|
0
|
0
|
|
|
|
|
last if( ($value & $mask) == 0); |
429
|
0
|
|
|
|
|
|
$mask >>= 1; |
430
|
0
|
|
|
|
|
|
$bitno -= 1; |
431
|
|
|
|
|
|
|
} |
432
|
0
|
|
|
|
|
|
return $bitno; |
433
|
|
|
|
|
|
|
} |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
sub decode_value { |
436
|
0
|
|
|
0
|
0
|
|
my($self, $typeid, $bytes) = @_; |
437
|
|
|
|
|
|
|
|
438
|
0
|
|
|
|
|
|
my $numbytes = scalar @$bytes; |
439
|
0
|
0
|
|
|
|
|
return undef unless $numbytes; |
440
|
|
|
|
|
|
|
|
441
|
0
|
0
|
0
|
|
|
|
if ( $typeid <= OPENTHINGS_UINT_BP24 ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
442
|
0
|
|
|
|
|
|
my $result = 0; |
443
|
|
|
|
|
|
|
# decode unsigned integer first |
444
|
0
|
|
|
|
|
|
for (my $i = 0; $i < @$bytes; $i++) { |
445
|
0
|
|
|
|
|
|
$result <<= 8; |
446
|
0
|
|
|
|
|
|
$result += $bytes->[$i]; |
447
|
|
|
|
|
|
|
} |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
# process any fixed binary points |
450
|
0
|
0
|
|
|
|
|
if( $typeid == OPENTHINGS_UINT ) { |
451
|
0
|
|
|
|
|
|
return $result; # no BP adjustment |
452
|
|
|
|
|
|
|
} else { |
453
|
0
|
|
|
|
|
|
return $result / ( 2 ** $self->get_value_type_bits( $typeid ) ); |
454
|
|
|
|
|
|
|
} |
455
|
|
|
|
|
|
|
} elsif( $typeid == OPENTHINGS_CHAR ) { |
456
|
0
|
|
|
|
|
|
my $format = 'C*'; |
457
|
0
|
|
|
|
|
|
my $result = pack($format, @$bytes); |
458
|
0
|
|
|
|
|
|
return $result; |
459
|
|
|
|
|
|
|
} elsif( $typeid >= OPENTHINGS_SINT && $typeid <= OPENTHINGS_SINT_BP24 ) { |
460
|
|
|
|
|
|
|
# decode unsigned int first |
461
|
|
|
|
|
|
|
|
462
|
0
|
|
|
|
|
|
my $result = 0; |
463
|
|
|
|
|
|
|
|
464
|
0
|
|
|
|
|
|
for (my $i = 0; $i < @$bytes; $i++) { |
465
|
0
|
|
|
|
|
|
$result <<= 8; |
466
|
0
|
|
|
|
|
|
$result += $bytes->[$i]; |
467
|
|
|
|
|
|
|
} |
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
# turn to signed int based on high bit of MSB |
470
|
|
|
|
|
|
|
# 2's comp is 1's comp plus 1 |
471
|
0
|
0
|
|
|
|
|
if(($bytes->[0] & 0x80) == 0x80) { |
472
|
0
|
|
|
|
|
|
$result = - HiPi->twos_compliment( $result, $numbytes ); |
473
|
|
|
|
|
|
|
} |
474
|
|
|
|
|
|
|
# adjust for binary point |
475
|
0
|
0
|
|
|
|
|
if( $typeid == OPENTHINGS_SINT ) { |
476
|
0
|
|
|
|
|
|
return $result; # no BP, return as int |
477
|
|
|
|
|
|
|
} else { |
478
|
0
|
|
|
|
|
|
return $result / ( 2 ** $self->get_value_type_bits( $typeid ) ); |
479
|
|
|
|
|
|
|
} |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
} elsif( $typeid == OPENTHINGS_FLOAT ) { |
483
|
0
|
|
|
|
|
|
return "TODO_FLOAT_IEEE_754-2008"; |
484
|
|
|
|
|
|
|
} |
485
|
|
|
|
|
|
|
|
486
|
0
|
|
|
|
|
|
return 0; |
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
sub encode_value { |
490
|
0
|
|
|
0
|
0
|
|
my($self, $typeid, $value ) = @_; |
491
|
|
|
|
|
|
|
|
492
|
0
|
|
|
|
|
|
my @result = (); |
493
|
0
|
|
|
|
|
|
my @emptyresult = (); |
494
|
|
|
|
|
|
|
|
495
|
0
|
0
|
|
|
|
|
if( $typeid == OPENTHINGS_CHAR ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
496
|
0
|
0
|
0
|
|
|
|
unless(defined($value) && $value ne '' ) { |
497
|
0
|
|
|
|
|
|
return @emptyresult; |
498
|
|
|
|
|
|
|
} |
499
|
0
|
|
|
|
|
|
@result = unpack('C*', $value ); |
500
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
} elsif( $typeid == OPENTHINGS_FLOAT ) { |
502
|
0
|
|
|
|
|
|
warn "TODO_FLOAT_IEEE_754-2008"; |
503
|
0
|
|
|
|
|
|
return @emptyresult; |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
} elsif ( $typeid <= OPENTHINGS_SINT_BP24 ) { |
506
|
|
|
|
|
|
|
# signed and unsigned integers can be packed the same |
507
|
0
|
0
|
0
|
|
|
|
if ( ( $typeid != OPENTHINGS_UINT ) && ( $typeid != OPENTHINGS_SINT ) ) { |
508
|
|
|
|
|
|
|
# pre-adjust for BP |
509
|
0
|
|
|
|
|
|
$value *= ( 2 ** $self->get_value_type_bits( $typeid ) ); # shifts float into int range using BP |
510
|
0
|
|
|
|
|
|
$value = int($value); |
511
|
|
|
|
|
|
|
} |
512
|
|
|
|
|
|
|
|
513
|
0
|
|
|
|
|
|
my $v = $value; |
514
|
0
|
|
|
|
|
|
unshift(@result, $v & 0xFF ); |
515
|
0
|
|
|
|
|
|
$v >>= 8; |
516
|
|
|
|
|
|
|
|
517
|
0
|
|
|
|
|
|
while( $v != 0.0 ) { |
518
|
0
|
|
|
|
|
|
unshift(@result, $v & 0xFF ); |
519
|
0
|
|
|
|
|
|
$v >>= 8; |
520
|
|
|
|
|
|
|
} |
521
|
|
|
|
|
|
|
} |
522
|
|
|
|
|
|
|
|
523
|
0
|
|
|
|
|
|
return @result; |
524
|
|
|
|
|
|
|
} |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
sub crypt_buffer { |
527
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
528
|
0
|
0
|
|
|
|
|
return unless $self->cryptseed; |
529
|
0
|
|
|
|
|
|
my $payload = $self->databuffer; |
530
|
0
|
|
|
|
|
|
my $pip = ( $payload->[3] << 8 ) + $payload->[4]; |
531
|
0
|
0
|
|
|
|
|
return unless $pip; |
532
|
0
|
|
|
|
|
|
my $pid = $self->cryptseed; |
533
|
0
|
|
|
|
|
|
my $block = ( ( ($pid & 0xFF ) << 8 ) ^ $pip ) & 0xFFFF; |
534
|
|
|
|
|
|
|
|
535
|
0
|
|
|
|
|
|
for ( my $byte = 5; $byte < @$payload; $byte ++ ) { |
536
|
0
|
|
|
|
|
|
for (my $i = 0; $i < 5; $i ++ ) { |
537
|
0
|
0
|
|
|
|
|
$block = ( $block & 1 ) ? (($block >> 1) ^ 0xF5F5 ) & 0xFFFF : ($block >> 1); |
538
|
|
|
|
|
|
|
} |
539
|
0
|
|
|
|
|
|
$payload->[$byte] = ( $block ^ $payload->[$byte] ^ 0x5A ) & 0xFF; |
540
|
|
|
|
|
|
|
} |
541
|
|
|
|
|
|
|
} |
542
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
sub calculate_crc { |
544
|
0
|
|
|
0
|
0
|
|
my ( $self, $nocrcbytes ) = @_; |
545
|
|
|
|
|
|
|
|
546
|
0
|
0
|
|
|
|
|
my $skipbytes = ( $nocrcbytes ) ? 0 : 2; |
547
|
|
|
|
|
|
|
|
548
|
0
|
|
|
|
|
|
my $payload = $self->databuffer; |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
# calculate from 5th byte (start of encrypt pip excluding the two crc bytes at the end if specified) |
551
|
0
|
|
|
|
|
|
my $crc = 0; |
552
|
0
|
|
|
|
|
|
for ( my $i = 5; $i < @$payload - $skipbytes; $i ++ ) { |
553
|
0
|
|
|
|
|
|
my $byte = $payload->[$i]; |
554
|
0
|
|
|
|
|
|
$crc ^= ( $byte << 8 ); |
555
|
0
|
|
|
|
|
|
for ( my $bit = 0; $bit < 8; $bit ++ ) { |
556
|
0
|
0
|
|
|
|
|
if( ( $crc & ( 1 <<15 ) ) != 0 ) { |
557
|
|
|
|
|
|
|
# bit is set |
558
|
0
|
|
|
|
|
|
$crc = (( $crc << 1) ^ 0x1021) & 0xFFFF; |
559
|
|
|
|
|
|
|
} else { |
560
|
|
|
|
|
|
|
# bit is clear |
561
|
0
|
|
|
|
|
|
$crc = ( $crc << 1 ) & 0xFFFF; |
562
|
|
|
|
|
|
|
} |
563
|
|
|
|
|
|
|
} |
564
|
|
|
|
|
|
|
} |
565
|
|
|
|
|
|
|
|
566
|
0
|
|
|
|
|
|
return $crc; |
567
|
|
|
|
|
|
|
} |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
sub buffer_length { |
570
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
571
|
0
|
|
|
|
|
|
my $val = scalar @{ $self->databuffer }; |
|
0
|
|
|
|
|
|
|
572
|
0
|
|
|
|
|
|
return $val; |
573
|
|
|
|
|
|
|
} |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
sub push_error { |
576
|
0
|
|
|
0
|
0
|
|
my( $self, $error) = @_; |
577
|
|
|
|
|
|
|
|
578
|
0
|
0
|
|
|
|
|
if ( $error ) { |
579
|
0
|
|
|
|
|
|
push( @{ $self->errorbuffer }, $error ); |
|
0
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
} |
581
|
0
|
|
|
|
|
|
return; |
582
|
|
|
|
|
|
|
} |
583
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
sub error { |
585
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
586
|
0
|
|
|
|
|
|
return scalar @{ $self->errorbuffer }; |
|
0
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
} |
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
sub shift_error { |
590
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
591
|
0
|
|
|
|
|
|
my $rval = shift @{ $self->errorbuffer }; |
|
0
|
|
|
|
|
|
|
592
|
0
|
|
|
|
|
|
return $rval; |
593
|
|
|
|
|
|
|
} |
594
|
|
|
|
|
|
|
|
595
|
|
|
|
|
|
|
sub add_record { |
596
|
0
|
|
|
0
|
0
|
|
my($self, %params) = @_; |
597
|
0
|
|
|
|
|
|
my $record = HiPi::RF::OpenThings::Message::Record->new( %params ); |
598
|
0
|
|
|
|
|
|
push @{ $self->records }, $record; |
|
0
|
|
|
|
|
|
|
599
|
0
|
|
|
|
|
|
return; |
600
|
|
|
|
|
|
|
} |
601
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
sub timestamp { |
603
|
0
|
|
|
0
|
0
|
|
my($self) = @_; |
604
|
0
|
|
|
|
|
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime( $self->epoch ); |
605
|
0
|
|
|
|
|
|
my $timestamp = sprintf('%u-%02u-%02u %02u:%02u:%02u', |
606
|
|
|
|
|
|
|
$year + 1900, $mon + 1, $mday, $hour, $min, $sec |
607
|
|
|
|
|
|
|
); |
608
|
0
|
|
|
|
|
|
return $timestamp; |
609
|
|
|
|
|
|
|
} |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
sub round_value { |
612
|
0
|
|
|
0
|
0
|
|
my( $self, $value ) = @_; |
613
|
0
|
0
|
|
|
|
|
if($value == 0) { |
|
|
0
|
|
|
|
|
|
614
|
0
|
|
|
|
|
|
return 0; |
615
|
|
|
|
|
|
|
} elsif( $value == int($value) ) { |
616
|
0
|
|
|
|
|
|
return $value; |
617
|
|
|
|
|
|
|
} else { |
618
|
0
|
|
|
|
|
|
return sprintf(qq(%.0f), $value ); |
619
|
|
|
|
|
|
|
} |
620
|
|
|
|
|
|
|
} |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
######################################################################################### |
623
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
package HiPi::RF::OpenThings::Message::Record; |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
######################################################################################### |
627
|
1
|
|
|
1
|
|
2998
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
44
|
|
628
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
61
|
|
629
|
1
|
|
|
1
|
|
23
|
use parent qw( HiPi::Class ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
10
|
|
630
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
__PACKAGE__->create_accessors( qw( command id name units typeid length value bytes ) ); |
632
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
sub new { |
634
|
0
|
|
|
0
|
|
|
my ( $class, %params ) = @_; |
635
|
|
|
|
|
|
|
|
636
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new( %params ); |
637
|
|
|
|
|
|
|
|
638
|
0
|
0
|
0
|
|
|
|
unless($self->name && $self->units) { |
639
|
0
|
|
|
|
|
|
my ( $name, $units ) = HiPi::RF::OpenThings->parameter_map( $self->id ); |
640
|
0
|
0
|
|
|
|
|
unless( $self->name ) { |
641
|
0
|
|
|
|
|
|
$self->name( $name ); |
642
|
|
|
|
|
|
|
} |
643
|
0
|
0
|
|
|
|
|
unless( $self->units ) { |
644
|
0
|
|
|
|
|
|
$self->units( $units ); |
645
|
|
|
|
|
|
|
} |
646
|
|
|
|
|
|
|
} |
647
|
|
|
|
|
|
|
|
648
|
0
|
|
|
|
|
|
return $self; |
649
|
|
|
|
|
|
|
} |
650
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
1; |
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
__END__ |