From 269f7fa0e224804fe9469d3f2d5dd42ea3e90c08 Mon Sep 17 00:00:00 2001 From: MarkusF <> Date: Sun, 28 Apr 2019 15:31:59 +0000 Subject: [PATCH] 50_MOBILEALERTSGW: Checksum check added git-svn-id: https://svn.fhem.de/fhem/trunk@19278 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/50_MOBILEALERTSGW.pm | 63 ++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index ecdbfabb3..7c42ef3f7 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - change: 50_MOBILEALERTSGW: Checksum check added - change: 93_DbRep: check index "Report_Idx" during first DB connect - change: 98_RandomTimer: remove 59_Twilight dependency - feature: 93_DbRep: new set "index" command to manage needed indexe for diff --git a/fhem/FHEM/50_MOBILEALERTSGW.pm b/fhem/FHEM/50_MOBILEALERTSGW.pm index 89dabf732..873021ec3 100644 --- a/fhem/FHEM/50_MOBILEALERTSGW.pm +++ b/fhem/FHEM/50_MOBILEALERTSGW.pm @@ -150,7 +150,7 @@ sub MOBILEALERTSGW_Set ($$@) { if ( $cmd eq "clear" ) { if ( $args[0] eq "readings" ) { for ( keys %{ $hash->{READINGS} } ) { - readingsDelete($hash, $_) if ( $_ ne 'state' ); + readingsDelete( $hash, $_ ) if ( $_ ne 'state' ); } return undef; } @@ -237,6 +237,8 @@ sub MOBILEALERTSGW_Set ($$@) { my $data = pack( "H*", $args[0] ); my ( $packageHeader, $timeStamp, $packageLength, $deviceID ) = unpack( "CNCH12", $data ); + my ( $sum, $sumWert ) = unpack( "%C63C", $data ); + $sum &= 0x7F; Log3 $name, 4, "$name MOBILEALERTSGW: Debuginsert PackageHeader: " . $packageHeader @@ -248,6 +250,19 @@ sub MOBILEALERTSGW_Set ($$@) { . $deviceID; Log3 $name, 5, "$name MOBILEALERTSGW: Debuginsert for $deviceID: " . unpack( "H*", $data ); + if ( $sum != $sumWert ) { + Log3 $MA_wname, 4, + "$MA_wname MOBILEALERTSGW: Wrong Checksum expected: 0x" + . sprintf( "%02X", $sum ) + . " got: 0x" + . sprintf( "%02X", $sumWert ) + . "=> ignoring"; + return undef; + } + Log3 $MA_wname, 5, + "$MA_wname MOBILEALERTSGW: Good Checksum got: 0x" + . sprintf( "%02X", $sum ); + Dispatch( $hash, $data, undef ); return undef; } @@ -558,23 +573,35 @@ sub MOBILEALERTSGW_DecodeData($$) { for ( my $pos = 0 ; $pos < length($POSTdata) ; $pos += MA_PACKAGE_LENGTH ) { my $data = substr $POSTdata, $pos, MA_PACKAGE_LENGTH; - my ( $packageHeader, $timeStamp, $packageLength, $deviceID ) = - unpack( "CNCH12", $data ); - Log3 $MA_wname, 4, - "$MA_wname MOBILEALERTSGW: PackageHeader: " - . $packageHeader - . " Timestamp: " - . scalar( FmtDateTimeRFC1123($timeStamp) ) - . " PackageLength: " - . $packageLength - . " DeviceID: " - . $deviceID - if ( $verbose >= 4 ); - Log3 $MA_wname, 5, - "$MA_wname MOBILEALERTSGW: Data for $deviceID: " - . unpack( "H*", $data ) - if ( $verbose >= 5 ); - my $found = Dispatch( $defs{$MA_wname}, $data, undef ); + my ( $sum, $sumWert ) = unpack( "%C63C", $data ); + $sum &= 0x7F; + if ( $sum != $sumWert ) { + Log3 $MA_wname, 4, + "$MA_wname MOBILEALERTSGW: Wrong Checksum expected: 0x" + . sprintf( "%02X", $sum ) + . " got: 0x" + . sprintf( "%02X", $sumWert ) + . "=> ignoring"; + } + else { + my ( $packageHeader, $timeStamp, $packageLength, $deviceID ) = + unpack( "CNCH12", $data ); + Log3 $MA_wname, 4, + "$MA_wname MOBILEALERTSGW: PackageHeader: " + . $packageHeader + . " Timestamp: " + . scalar( FmtDateTimeRFC1123($timeStamp) ) + . " PackageLength: " + . $packageLength + . " DeviceID: " + . $deviceID + if ( $verbose >= 4 ); + Log3 $MA_wname, 5, + "$MA_wname MOBILEALERTSGW: Data for $deviceID: " + . unpack( "H*", $data ) + if ( $verbose >= 5 ); + my $found = Dispatch( $defs{$MA_wname}, $data, undef ); + } } }