Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERL WARNING - sub ConvLaCrosse robust #1038

Merged
merged 9 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions FHEM/lib/SD_Protocols.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use strict;
use warnings;
use Carp qw(croak carp);
use Digest::CRC;
our $VERSION = '2.04';
our $VERSION = '2.05';
use Storable qw(dclone);
use Scalar::Util qw(blessed);

Expand Down Expand Up @@ -1965,12 +1965,15 @@ sub ConvLaCrosse {
my $self = shift // carp 'Not called within an object';
my $hexData = shift // croak 'Error: called without $hexdata as input';

croak qq[ConvLaCrosse, Usage: Input #1, $hexData is not valid HEX]
if (not $hexData =~ /^[0-9a-fA-F]+$/xms) ; # check valid hexData

return ( 1,'ConvLaCrosse, Usage: Input #1, $hexData needs to be at least 8 chars long' )
if ( length($hexData) < 8 ) ; # check number of length for this sub to not throw an error

my $ctx = Digest::CRC->new( width => 8, poly => 0x31 );
my $calcCrc = $ctx->add( pack 'H*', substr( $hexData, 0, 8 ) )->digest;
my $checksum = sprintf( "%d", hex( substr( $hexData, 8, 2 ) ) ); # Todo: Needs some check to be hexadzimal conform
my $checksum = sprintf( "%d", hex( substr( $hexData, 8, 2 ) ) );
return ( 1, qq[ConvLaCrosse, checksumCalc:$calcCrc != checksum:$checksum] )
if ( $calcCrc != $checksum );

Expand Down
2 changes: 1 addition & 1 deletion controls_signalduino.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ UPD 2020-04-13_23:15:56 14325 FHEM/14_SD_WS_Maverick.pm
UPD 2021-08-11_21:38:30 37904 FHEM/41_OREGON.pm
UPD 2020-12-17_23:16:30 15582 FHEM/90_SIGNALduino_un.pm
UPD 2021-10-16_10:57:13 218872 FHEM/lib/SD_ProtocolData.pm
UPD 2021-08-30_16:54:51 72899 FHEM/lib/SD_Protocols.pm
UPD 2021-11-26_11:33:29 72991 FHEM/lib/SD_Protocols.pm
10 changes: 5 additions & 5 deletions t/SD_Protocols/02_ConvLaCrosse.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use warnings;
use Test2::V0;
use lib::SD_Protocols qw(:ALL);
use Test2::Tools::Compare qw{is like};
use Test2::Tools::Exception qw/dies lives/;

plan(4);

Expand All @@ -22,7 +23,8 @@ subtest 'test ConvLaCrosse, checksum ok' => sub {
is($#ret,0, 'ConvLaCrosse reported no error');
is($ret[0],'OK 9 42 129 4 212 44','check result for right Lacrosse transmission');
};



subtest 'msg 9A05922F8180046818480800 (ID 103)' => sub {
plan(2);
my $hexMsg='9A05922F8180046818480800';
Expand Down Expand Up @@ -63,9 +65,7 @@ subtest 'test ConvLaCrosse, length to short ' => sub {


subtest 'test ConvLaCrosse, not hexadezimal' => sub {
plan(2);
plan(1);
my $hexMsg='010503B7PA1041AAAAAAAAPF';
my @ret=$Protocols->ConvLaCrosse($hexMsg) ;
is($#ret,1, "ConvLaCrosse reported some error");
like($ret[1],qr/!= checksum/,'check error message');
like(dies { lib::SD_Protocols::ConvLaCrosse('TEST',$hexMsg) } ,qr/ConvLaCrosse, Usage: Input #1, 010503B7PA1041AAAAAAAAPF is not valid HEX/,'check not hexadezimal');
};