# $Id$ ############################################################################## # # 70_VIERA.pm # # a module to send messages or commands to a Panasonic TV # inspired by Samsung TV Module from Gabriel Bentele # written 2013 by Tobias Vaupel # since version 1.25 modified by mabula # # # Version = 1.31 # # Version History: # - 1.31 - 2020-01-15 Dr. H-J Breymayer # -- new command display web page on TV # # - 1.30 - 2020-01-10 Dr. H-J Breymayer # -- implemented additional commands, changed definition of commands # -- bugfix patch from Nestor # # - 1.28 - 2019-12-14 Dr. H-J Breymayer # -- correction of "Rolling Key" in case of bad command # -- GetStatus call after on_off command # # - 1.27 - 2019-12-14 Dr. H-J Breymayer # -- recognition of orange power LED indicator, new state dormant # -- attribute webCmd changed "webCmd on_off" # -- replaced in all log3 statements fixed name VIERA with module name # -- correction of "define" number of arguments and their test # # - 1.26 - 2019-11-24 Dr. H-J Breymayer # -- problem with unexpected crypted command, correct Session Sequence # # - 1.25 - 2019-11-23 Dr. H-J Breymayer # -- removed Readings "power". Redefined state -> Initialized/on/off # -- removed spaces at remote control layout, problems with images not appearing # -- PERL error line 481 and 516 # -- correction of sub call "sub VIERA_GetStatus($$)" # -- Verschlüsselte Verbindung für neue TV's ab 2019 integriert. # # - 1.24 - 2015-07-08 # -- Using non blocking as default for status update. Use attr to use blocking mode. # -- Replaced when/given with if/elsif # -- Added color buttons for remoteControl Layout # -- Added remoteControl Layout with SVG # -- InternalTimer is deleted at define. Avoid multiple internalTimer running in parallel when redefining device # -- Added TCP-Port to internal PORT instead of fixed coding # -- increased byte length at command read (IO::Socket::INET) from 1 to 1024 # -- in very few cases the TV is answering with HTTP Code 400 BAD REQUEST. This is considered now and is interpreted as device on instead of off. # # - 1.23 - 2014-08-01 # -- Add parameter "HDMI1" - "HDMI4" for command remoteControl to select HDMI input directly # -- Add command "input" to select a HDMI port, TV or SD-Card as source # # - 1.22 - 2013-12-28 # -- fixed set command remoteControl # # - 1.21 - 2013-08-19 # -- Log() deprecated/replaced by Log3() # -- GetStatus() is called after set volume/mute to update readings immediately # # - 1.20 - 2013-08-16 # -- added support according to http://www.fhemwiki.de/wiki/DevelopmentGuidelinesAV # # - 1.11 - 2013-08-13 # -- added "noArg" at get/set-command # -- changed format of return() in VIERA_Get() for get-command drop down menu in FHEMWEB # # - 1.10 - 2013-06-29 # -- Added support for module 95_remotecontrol # -- New functions: sub VIERA_RClayout_TV(); sub VIERA_RCmakenotify($$); # -- Updated VIERA_Initialize for remotecontrol # # - 1.00 - 2013-03-16 # -- First release # ############################################################################## package main; use strict; use warnings; use IO::Socket::INET; use MIME::Base64; use Crypt::Mode::CBC; use Digest::SHA qw(hmac_sha256); use Time::HiRes qw(gettimeofday sleep); use utf8; #use Blocking; # Forward declaration for remotecontrol module sub VIERA_RCmakenotify($$); sub VIERA_RClayout_TV(); sub VIERA_RClayout_TV_SVG(); my %VIERA_remoteControl_args = ( channel_down => "CH_DOWN", channel_up => "CH_UP", volume_up => "VOLUP", volume_down => "VOLDOWN", mute => "MUTE", TV => "TV", AV => "CHG_INPUT", red_button => "RED", green_button => "GREEN", yellow_button => "YELLOW", blue_button => "BLUE", VIERA_tools => "VTOOLS", cancel => "CANCEL", exit => "CANCEL", option => "SUBMENU", return => "RETURN", home => "HOME", program => "PROG", enter => "ENTER", right => "RIGHT", left => "LEFT", up => "UP", down => "DOWN", "3D_button" => "3D", SD_card => "SD_CARD", display_mode => "DISP_MODE", menu => "MENU", VIERA_connect => "INTERNET", VIERA_link => "VIERA_LINK", EPG => "EPG", guide => "GUIDE", videotext => "TEXT", subtitle => "STTL", info => "INFO", index => "INDEX", hold => "HOLD", last_view => "R_TUNE", on_off => "POWER", rewind => "REW", play => "PLAY", fast_forward => "FF", skip_previous => "SKIP_PREV", pause => "PAUSE", skip_next => "SKIP_NEXT", stop => "STOP", record => "REC", apps => "APPS", aspect => "ASPECT", favorite => "FAVORITE", game => "GAME", digit_1 => "D1", digit_2 => "D2", digit_3 => "D3", digit_4 => "D4", digit_5 => "D5", digit_6 => "D6", digit_7 => "D7", digit_8 => "D8", digit_9 => "D9", digit_0 => "D0", noise_reduction => "P_NR", tune => "R_TUNE", HDMI_1 => "HDMI1", HDMI_2 => "HDMI2", HDMI_3 => "HDMI3", HDMI_4 => "HDMI4", ); my %VIERA_remoteControl_apps = ( netflix => "0010000200000001", youtube => "0070000200180001", shoutcast => "0070000400000001", calendar => "0387878700150020", browser => "0077777700160002", amazonprime => "0010000100180001", iplayer => "0020000A00000010", bbc_iplayer => "0020000A00000010", itv => "0387878700000124", all_4 => "0387878700000125", demand_5 => "0387878700000125", recorded_tv => "0387878700000013", multi_window => "0387878700000050", bbc_news => "0020000A00000006", bbc_sport => "0020000A00000007", weather => "0070000C00000001", developer => "0077777777777778", ); # Initialize the module and tell FHEM name of additional functions # Param1: Hash of FHEM-Device # Return: no return code sub VIERA_Initialize($) { my ($hash) = @_; $hash->{DefFn} = "VIERA_Define"; $hash->{SetFn} = "VIERA_Set"; $hash->{GetFn} = "VIERA_Get"; $hash->{UndefFn} = "VIERA_Undefine"; $hash->{AttrList} = "blocking:1,0 $readingFnAttributes"; $data{RC_layout}{VIERA_TV} = "VIERA_RClayout_TV"; $data{RC_layout}{VIERA_TV_SVG} = "VIERA_RClayout_TV_SVG"; $data{RC_makenotify}{VIERA} = "VIERA_RCmakenotify"; } # Callback when 'define' is used at FHEM # Param1: Hash of FHEM-Device # Param2: String of 'define' command # Return: Help text for FHEMWEB sub VIERA_Define($$) { my ($hash, $def) = @_; my @args = split("[ \t][ \t]*", $def); my $name = $args[0]; my $nom = $hash->{TYPE}."_".$hash->{NAME}."_"; my $error = ""; my $value = ""; $hash->{helper}{session_id} = "None"; $hash->{helper}{session_seq_num} = "None"; if(int(@args) < 3 || int(@args) > 6) { my $msg = "wrong syntax: define VIERA [] "; Log3 $name, 2, "$name: \"$msg\""; return $msg; } $hash->{helper}{HOST} = $args[2]; $hash->{helper}{PORT} = 55000; if(defined($args[3]) and $args[3] > 10) { $hash->{helper}{INTERVAL}=$args[3]; } else { $hash->{helper}{INTERVAL}=30; } if(defined($args[4])) { $hash->{helper}{pincode} = $args[4]; } else { $hash->{helper}{pincode} = "0000"; $hash->{helper}{ENCRYPTION} = "no"; } if(defined($args[5])) { $hash->{helper}{ENCRYPTION} = $args[5]; } else { ($error, $value) = getKeyValue("$nom.ENCRYPTION"); $hash->{helper}{ENCRYPTION} = $value if (defined($value)); } # communication can start again if ($hash->{helper}{pincode} eq "0000") { $hash->{helper}{stop} = "no" } else { ($error, $hash->{helper}{stop}) = getKeyValue("$nom.stop"); } readingsBeginUpdate($hash); readingsBulkUpdate($hash, "PinCode", $hash->{helper}{pincode}); readingsBulkUpdate($hash, "Encryption", $hash->{helper}{ENCRYPTION}); readingsBulkUpdate($hash, "session_id", $hash->{helper}{session_id}); readingsBulkUpdate($hash, "Sequence", $hash->{helper}{session_seq_num}); readingsEndUpdate($hash, 1); ($error, $hash->{helper}{app_id}) = getKeyValue("$nom.app_id"); $hash->{helper}{app_id} = "None" if (!defined($hash->{helper}{app_id})); ($error, $value) = getKeyValue("$nom.session_IV"); if (defined($value)) {$hash->{helper}{session_IV} = decode_base64($value)} else {$hash->{helper}{session_IV} = "None"} ($error, $value) = getKeyValue("$nom.session_key"); if (defined($value)) {$hash->{helper}{session_key} = decode_base64($value)} else {$hash->{helper}{session_key} = "None"} ($error, $value) = getKeyValue("$nom.session_hmac_key"); if (defined($value)) {$hash->{helper}{session_hmac_key} = decode_base64($value)} else {$hash->{helper}{session_hmac_key} = "None"} CommandAttr(undef,$name." webCmd on_off") if( !defined( AttrVal($hash->{NAME}, "webCmd", undef)) ); BlockingKill($hash->{helper}{RUNNING_PID_GET}) if(defined($hash->{helper}{RUNNING_PID_GET})); delete($hash->{helper}{RUNNING_PID_GET}); RemoveInternalTimer($hash); InternalTimer(gettimeofday()+5, "VIERA_GetStatus", $hash, 0); Log3 $name, 2, "$name: defined with host: $hash->{helper}{HOST} interval: $hash->{helper}{INTERVAL} PIN: $hash->{helper}{pincode} "; readingsSingleUpdate($hash,"state","Initialized",1); return undef; } # Callback when 'set' is used at FHEM # Param1: Hash of FHEM-Device # Param2: String of 'set' command # Return: Help text for FHEMWEB sub VIERA_Set($@) { my ($hash, $name, @a) = @_; my $host = $hash->{helper}{HOST}; my $key = ""; my $tab = ""; my $usage = "choose one of ". "on_off:noArg ". "mute:on,off ". "volume:slider,0,1,100 ". "channel ". "remoteControl:" . join(",", sort keys %VIERA_remoteControl_args) . " " . "remoteControlApp:" . join(",", sort keys %VIERA_remoteControl_apps) . " " . "input:HDMI_1,HDMI_2,HDMI_3,HDMI_4,SD_card,TV ". "statusRequest:noArg ". "web "; $usage =~ s/(NRC_|-ONOFF)//g; my $what = $a[0]; return "\"set $name\" needs at least one argument" if(!defined($a[0])); return "Unknown value $a[0] Device is not present or reachable (power on or check ethernet connection)" if(ReadingsVal($name,"state","off") eq "off" && $what ne "?"); return "Unknown value $a[0] use set $name on_off, choose one of on_off ?" if(ReadingsVal($name,"state","off") eq "dormant" && $what ne "on_off"); my $state = $a[1] if(defined($a[1])); if ($what eq "mute"){ Log3 $name, 3, "$name: Set mute $state"; if ($state eq "on") {$state = 1;} else {$state = 0;} VIERA_connection($hash, VIERA_BuildXML_RendCtrl($hash, "Set", "Mute", $state)); VIERA_GetStatus($hash, 1); } elsif ($what eq "volume"){ return "$name: Volume range is too high! Use Value 0 till 100 for volume." if($state < 0 || $state > 100); Log3 $name, 3, "$name: Set volume $state"; VIERA_connection($hash, VIERA_BuildXML_RendCtrl($hash, "Set", "Volume", $state)); VIERA_GetStatus($hash, 1); } elsif ($what eq "on_off"){ Log3 $name, 3, "$name: Set on_off"; VIERA_Encrypted_Command($hash, "POWER", "key"); VIERA_Encrypt_Answer($hash); sleep 0.6; VIERA_GetStatus($hash, 1); } elsif ($what eq "channel"){ return "$name: Channel is too high or low!" if($state < 1 || $state > 9999); Log3 $name, 3, "$name: Set channel $state"; for(my $i = 0; $i <= length($state)-1; $i++) { VIERA_Encrypted_Command($hash, "D".substr($state, $i, 1), "key"); sleep 0.1; VIERA_Encrypt_Answer($hash); } VIERA_Encrypted_Command($hash, "ENTER", "key"); VIERA_Encrypt_Answer($hash); } elsif ($what eq "remoteControl"){ Log3 $name, 3, "$name: Set remoteControl $state"; if( defined($VIERA_remoteControl_args{$state}) ) { $state = $VIERA_remoteControl_args{$state}; Log3 $name, 4, "$name: Set remoteControl $state"; } else {return "Unknown argument $what, $usage"} VIERA_Encrypted_Command($hash, $state, "key"); VIERA_Encrypt_Answer($hash); } elsif ($what eq "remoteControlApp"){ Log3 $name, 3, "$name: Set remoteControlApp $state"; if( defined($VIERA_remoteControl_apps{$state}) ) { $state = $VIERA_remoteControl_apps{$state}; Log3 $name, 4, "$name: Set remoteControlApp $state"; } else {return "Unknown argument $what, $usage"} VIERA_Encrypted_Command($hash, $state, "app"); VIERA_Encrypt_Answer($hash); } elsif ($what eq "input"){ Log3 $name, 3, "$name: Set input $state"; if( defined($VIERA_remoteControl_args{$state}) ) { $state = $VIERA_remoteControl_args{$state}; Log3 $name, 4, "$name: Set remoteControl $state"; } else {return "Unknown argument $what, $usage"} VIERA_Encrypted_Command($hash, $state, "key"); VIERA_Encrypt_Answer($hash); } elsif ($what eq "statusRequest"){ Log3 $name, 3, "$name: Set statusRequest"; VIERA_GetStatus($hash, 1); } elsif ($what eq "web"){ Log3 $name, 3, "$name: Set web $state"; VIERA_webpage($hash, $state); } elsif ($what eq "?"){ return "$usage"; } else { Log3 $name, 3, "$name: Unknown argument $what, $usage"; return "Unknown argument $what, $usage"; } return undef; } # Callback when 'get' is used at FHEM # Param1: Hash of FHEM-Device # Param2: String of 'set' command # Return: Help text for FHEMWEB sub VIERA_Get($@) { my ($hash, @a) = @_; my $what; my $usage = "choose one of mute:noArg volume:noArg presence:noArg"; my $name = $hash->{NAME}; return "$name: No argument given, $usage" if(int(@a) != 2); $what = $a[1]; if($what =~ /^(volume|mute|presence)$/) { if (defined($hash->{READINGS}{$what})) { ReadingsVal($name, $what, "undefined"); } else{ return "no such reading: $what"; } } else{ return "Unknown argument $what, $usage"; } } # Callback when 'delete' is used at FHEM or FHEM is restarting/shutdown # Param1: Hash of FHEM-Device # Param2: Name of FHEM-Device # Return: undef sub VIERA_Undefine($$) { my($hash, $name) = @_; # Stop the internal GetStatus-Loop and exit RemoveInternalTimer($hash); BlockingKill($hash->{helper}{RUNNING_PID_GET}) if(defined($hash->{helper}{RUNNING_PID_GET})); delete($hash->{helper}{RUNNING_PID_GET}); return undef; } # Function is called periodically by InternalTimer and fetch informations from device. The decision if blocking or nonBlocking is used is made here. # Param1: Hash of FHEM-Device # Param2: Optional, if set to 1 fetch information from device without interrupting InternalTimer sub VIERA_GetStatus($$) { my ($hash, $local) = @_; my $name = $hash->{NAME}; my $host = $hash->{helper}{HOST}; my $blocking = AttrVal($name, "blocking", 0); #use non-blocking in standard. Just use blocking when set by attr #if $local is set to 1 just fetch informations from device without interrupting InternalTimer $local = 0 unless(defined($local)); InternalTimer(gettimeofday()+$hash->{helper}{INTERVAL}, "VIERA_GetStatus", $hash, 0) unless($local == 1); return "" if(!defined($hash->{helper}{HOST}) or !defined($hash->{helper}{INTERVAL})); return if ($hash->{helper}{stop} eq "yes"); VIERA_CeckEncryption($hash) if ($hash->{helper}{ENCRYPTION} eq "?"); if ($blocking == 0) { Log3 $name, 4, "$name [VIERA_GetStatus]: Using non blocking..."; $hash->{helper}{RUNNING_PID_GET} = BlockingCall("VIERA_GetDoIt", $hash, "VIERA_GetDone", 10, "VIERA_GetAbortFn", $hash) unless(exists($hash->{helper}{RUNNING_PID_GET})); if($hash->{helper}{RUNNING_PID_GET}) { Log3 $name, 4, "$name [VIERA_GetStatus]: VIERA_GetDoIt() BlockingCall process started with PID $hash->{helper}{RUNNING_PID_GET}{pid}"; } else { Log3 $name, 3, "$name [VIERA_GetStatus]: BlockingCall process start failed for VIERA_GetDoIt()"; } return; } Log3 $name, 4, "$name [VIERA_GetStatus]: Using blocking..."; my $returnVol = VIERA_connection($hash, VIERA_BuildXML_RendCtrl($hash, "Get", "Volume", "")); Log3 $name, 5, "$name [VIERA_GetStatus]: Vol-Request returned: $returnVol" if(defined($returnVol)); if ( not defined($returnVol) or $returnVol eq "" ) { Log3 $name, 4, "$name [VIERA_GetStatus]: Vol-Request NO SOCKET!"; if (ReadingsVal($name,"state","off") ne "off") { $hash->{helper}{session_seq_num} = "None"; readingsBeginUpdate($hash); readingsBulkUpdate($hash, "state", "off"); readingsBulkUpdate($hash, "Sequence", $hash->{helper}{session_seq_num}); readingsBulkUpdate($hash, "presence", "absent"); readingsBulkUpdate($hash, "volume", "?"); readingsBulkUpdate($hash, "mute", "?"); readingsEndUpdate($hash, 1); } return; } elsif ( index($returnVol, "Bad Request") != -1) { if (ReadingsVal($name,"state","off") ne "dormant") { Log3 $name, 4, "$name [VIERA_GetStatus()]: TV answered. Seems to be on but delivering no data"; $hash->{helper}{session_seq_num} = "None"; readingsBeginUpdate($hash); readingsBulkUpdate($hash, "state", "dormant"); readingsBulkUpdate($hash, "Sequence", $hash->{helper}{session_seq_num}); readingsBulkUpdate($hash, "presence", "absent"); readingsBulkUpdate($hash, "volume", "?"); readingsBulkUpdate($hash, "mute", "?"); readingsEndUpdate($hash, 1); } return; } my $returnMute = VIERA_connection($hash, VIERA_BuildXML_RendCtrl($hash, "Get", "Mute", "")); Log3 $name, 5, "$name [VIERA_GetStatus]: Mute-Request returned: $returnMute" if(defined($returnMute)); if( not defined($returnMute) or $returnMute eq "" ) { Log3 $name, 4, "$name [VIERA_GetStatus]: Mute-Request NO SOCKET!"; if( ReadingsVal($name,"state","off") ne "off") { $hash->{helper}{session_seq_num} = "None"; readingsBeginUpdate($hash); readingsBulkUpdate($hash, "state", "off"); readingsBulkUpdate($hash, "Sequence", $hash->{helper}{session_seq_num}); readingsBulkUpdate($hash, "presence", "absent"); readingsBulkUpdate($hash, "volume", "?"); readingsBulkUpdate($hash, "mute", "?"); readingsEndUpdate($hash, 1); } return; } elsif ( index($returnMute, "Bad Request") != -1) { if (ReadingsVal($name,"state","off") ne "dormant") { Log3 $name, 4, "$name [VIERA_GetStatus()]: TV answered. Seems to be on but delivering no data"; $hash->{helper}{session_seq_num} = "None"; readingsBeginUpdate($hash); readingsBulkUpdate($hash, "state", "dormant"); readingsBulkUpdate($hash, "Sequence", $hash->{helper}{session_seq_num}); readingsBulkUpdate($hash, "presence", "absent"); readingsBulkUpdate($hash, "volume", "?"); readingsBulkUpdate($hash, "mute", "?"); readingsEndUpdate($hash, 1); } return; } readingsBeginUpdate($hash); if($returnVol =~ /(.+)<\/CurrentVolume>/){ Log3 $name, 4, "$name [VIERA_GetStatus]: Set reading volume to $1"; if( $1 ne ReadingsVal($name, "volume", "0") ) {readingsBulkUpdate($hash, "volume", $1);} } if($returnMute =~ /(.+)<\/CurrentMute>/){ my $myMute = $1; if ($myMute == 0) { $myMute = "off"; } else { $myMute = "on";} Log3 $name, 4, "$name [VIERA_GetStatus]: Set reading mute to $myMute"; if( $myMute ne ReadingsVal($name, "mute", "0") ) {readingsBulkUpdate($hash, "mute", $myMute);} } if( ReadingsVal($name,"state","off") ne "on") { readingsBulkUpdate($hash, "state", "on"); readingsBulkUpdate($hash, "presence", "present"); } readingsEndUpdate($hash, 1); return $hash->{STATE}; } # To sent RAW-Data as TCP Client to device # param1: Hash of FHEM-Device # param2: RAW Data #return: RAW answer when successful or undef if no socket is available. sub VIERA_connection($$) { my ($hash, $data) = @_; my $name = $hash->{NAME}; my $buffer = ""; my $buff = ""; my $blocking = "NonBlocking-VIERA_connection()" if (AttrVal($name, "blocking", 0) == 0); $blocking = "Blocking-VIERA_connection()" if (AttrVal($name, "blocking", 0) == 1); my $sock = new IO::Socket::INET ( PeerAddr => $hash->{helper}{HOST}, PeerPort => $hash->{helper}{PORT}, Proto => "tcp", Timeout => 2 ); if(defined ($sock)) { Log3 $name, 5, "$name [$blocking]: Send Data to $hash->{helper}{HOST}:$hash->{helper}{PORT}:\n$data"; print $sock $data; while (1){ my $res = read $sock, $buff, 1024; last if (not defined($res) or $res == 0); $buffer .= $buff; } Log3 $name, 5, "$name [$blocking]: $hash->{helper}{HOST} buffer response:\n$buffer"; close($sock); $hash->{helper}{BUFFER} = $buffer; return $buffer; } else { Log3 $name, 4, "$name [$blocking]: $hash->{helper}{HOST}: not able to open socket"; return undef; } } # Create RAW Data to send or get volume/mute state # Param1: Hash of FHEM-Device # Param2: get|set # Param3: volume|mute # Param4: value for set command # Return: RAW html request for xml soap sub VIERA_BuildXML_RendCtrl($$$$) { my ($hash, $methode, $command, $value) = @_; my $host = $hash->{helper}{HOST}; my $port = $hash->{helper}{PORT}; my $name = $hash->{NAME}; my $callsoap = ""; my $message = ""; my $head = ""; my $blen = ""; # Log3 $name, 5, "$name: $command with $value to $host"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "0\r\n"; $callsoap .= "Master\r\n"; $callsoap .= "$value\r\n" if(defined($value)); $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $blen = length($callsoap); $head .= "POST /dmr/control_0 HTTP/1.1\r\n"; $head .= "Host: $host:$port\r\n"; $head .= "Content-Type: text/xml; charset=\"utf-8\"\r\n"; $head .= "SOAPAction: \"urn:schemas-upnp-org:service:RenderingControl:1#$methode$command\"\r\n"; $head .= "Content-Length: $blen\r\n"; $head .= "\r\n"; $message .= $head; $message .= $callsoap; Log3 $name, 5, "$name: Building XML SOAP (RenderingControl) for command $command with value $value to host $host:\n$message"; return $message; } # ####################### create any SOAP Request message encrypted or not ################################# sub VIERA_Build_soap_message_Encrypt($$$$) { my ($hash, $action, $params, $body_elem) = @_; my $host = $hash->{helper}{HOST}; my $port = $hash->{helper}{PORT}; my $urn = "panasonic-com:service:p00NetworkControl:1"; my $callsoap = ""; my $soapbody = ""; my $encrypted_command = ""; my $message = ""; my $head = ""; my $blen = ""; my $session_id = $hash->{helper}{session_id}; my $session_seq_num = $hash->{helper}{session_seq_num}; my $session_hmac_key = $hash->{helper}{session_hmac_key}; my $session_IV = $hash->{helper}{session_IV}; my $session_key = $hash->{helper}{session_key}; my $is_encrypted = 0; if ($action ne "X_GetEncryptSessionId" and $action ne "X_DisplayPinCode" and $action ne "X_RequestAuth") { if ($session_key ne "None" and $session_IV ne "None" and $session_hmac_key ne "None" and $session_id ne "None" and $session_seq_num ne "None" ) { $is_encrypted = 1; # Encapsulate URN_REMOTE_CONTROL command in an X_EncryptedCommand if we're using encryption $hash->{helper}{session_seq_num} += 1; readingsBeginUpdate($hash); readingsBulkUpdate($hash, "Sequence", $hash->{helper}{session_seq_num}); readingsEndUpdate($hash, 1); $session_seq_num = sprintf("%08d", $session_seq_num); $encrypted_command .= "$session_id\r\n"; $encrypted_command .= "$session_seq_num\r\n"; $encrypted_command .= "\r\n"; $encrypted_command .= "<$body_elem:$action xmlns:$body_elem=\"urn:$urn\">\r\n"; $encrypted_command .= "$params\r\n"; $encrypted_command .= "\r\n"; $encrypted_command .= "\r\n"; $encrypted_command = VIERA_encrypt_soap_payload($encrypted_command, $session_key, $session_IV, $session_hmac_key); $action = "X_EncryptedCommand"; my $app_id = $hash->{helper}{app_id}; $params = "$app_id\r\n"; $params .= "$encrypted_command"; $body_elem = "u"; } } # Construct SOAP request $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "<$body_elem:$action xmlns:$body_elem=\"urn:$urn\">\r\n"; $callsoap .= "$params\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $blen = length($callsoap); $head .= "POST /nrc/control_0 HTTP/1.1\r\n"; $head .= "Host: $host:$port\r\n"; $head .= "Content-Type: text/xml; charset=\"utf-8\"\r\n"; $head .= "SOAPAction: \"urn:panasonic-com:service:p00NetworkControl:1#$action\"\r\n"; $head .= "Content-Length: $blen\r\n"; $head .= "\r\n"; $message .= $head; $message .= $callsoap; # Log3 $name, 5, "$name: Building XML SOAP (NetworkControl) for command $command to host $host:\n$message"; $hash->{helper}{is_encrypted} = $is_encrypted; return $message; } # Get volume and mute state from device. This function is called non blocking! # Param1: Hash of FHEM-Device # Return: || sub VIERA_GetDoIt($) { my ($hash) = @_; my $name = $hash->{NAME}; my $myVol = ""; my $myMute = ""; Log3 $name, 4, "$name [NonBlocking-VIERA_GetDoIt()]: BlockingCall for ".$hash->{NAME}." start..."; my $returnVol = VIERA_connection($hash, VIERA_BuildXML_RendCtrl($hash, "Get", "Volume", "")); Log3 $name, 5, "$name [NonBlocking-VIERA_GetDoIt()]: GetStatusVol-Request returned: $returnVol" if(defined($returnVol)); if(not defined($returnVol) or $returnVol eq "") { Log3 $name, 4, "$name [NonBlocking-VIERA_GetDoIt()]: GetStatusVol-Request NO SOCKET!"; return $hash->{NAME}. "|". "error-noSocket" . "|". "error-noSocket"; } my $returnMute = VIERA_connection($hash, VIERA_BuildXML_RendCtrl($hash, "Get", "Mute", "")); Log3 $name, 5, "$name [NonBlocking-VIERA_GetDoIt()]: GetStatusMute-Request returned: $returnMute" if(defined($returnMute)); if(not defined($returnMute) or $returnMute eq "") { Log3 $name, 4, "$name [NonBlocking-VIERA_GetDoIt()]: GetStatusMute-Request NO SOCKET!"; return $hash->{NAME}. "|". "error-noSocket" . "|". "error-noSocket"; } if ($returnVol =~ /HTTP\/1\.1 ([\d]{3}) (.*)/) { Log3 $name, 4, "$name [NonBlocking-VIERA_GetDoIt()]: Received HTTP Code $1 $2"; $myVol = "error-$1 $2" if ($1 ne "200"); } if($returnVol =~ /(.+)<\/CurrentVolume>/){ $myVol = $1; Log3 $name, 4, "$name [NonBlocking-VIERA_GetDoIt()]: Received volume with level $myVol"; } if ($returnMute =~ /HTTP\/1\.1 ([\d]{3}) (.*)/) { Log3 $name, 4, "$name [NonBlocking-VIERA_GetDoIt()]: Received HTTP Code $1 $2"; $myMute = "error-$1 $2" if ($1 ne "200"); } if($returnMute =~ /(.+)<\/CurrentMute>/){ $myMute = $1; if ($myMute == 0) { $myMute = "off"; } else { $myMute = "on";} Log3 $name, 4, "$name [NonBlocking-VIERA_GetDoIt()]: Received mute state $myMute"; } return $hash->{NAME}. "|". "$myVol" . "|". "$myMute"; } # Callback of non blocking function VIERA_GetDoIt. Parse the results and set readings at fhem-device # Param1: || # volume level = 0 - 100 # mute state = 1 or 0 # If no socket is available Par1 and Par2 contains "error" # Return: no return code #################################################### sub VIERA_GetDone($) { my ($string) = @_; return unless(defined($string)); my @a = split("\\|",$string); my $name = shift(@a); my $myVol = shift(@a); my $myMute = shift(@a); my $hash = $defs{$name}; Log3 $name, 4, "$name [NonBlocking-VIERA_GetDone()]: Param: $string"; if ($myVol =~ /error-(.*)/ || $myMute =~ /error-(.*)/) { if ($1 eq "noSocket") { Log3 $name, 4, "$name [NonBlocking-VIERA_GetDone()]: Seems to be there is no socket available. Guessing TV is off!"; if (ReadingsVal($name,"state","off") ne "off") { $hash->{helper}{session_seq_num} = "None"; readingsBeginUpdate($hash); readingsBulkUpdate($hash, "state", "off"); readingsBulkUpdate($hash, "Sequence", $hash->{helper}{session_seq_num}); readingsBulkUpdate($hash, "presence", "absent"); readingsBulkUpdate($hash, "volume", "?"); readingsBulkUpdate($hash, "mute", "?"); readingsEndUpdate($hash, 1); } delete($hash->{helper}{RUNNING_PID_GET}); return; } else { if (ReadingsVal($name,"state","off") ne "dormant") { Log3 $name, 4, "$name [NonBlocking-VIERA_GetDone()]: TV answered with $1. Seems to be on but delivering no data"; $hash->{helper}{session_seq_num} = "None"; readingsBeginUpdate($hash); readingsBulkUpdate($hash, "state", "dormant"); readingsBulkUpdate($hash, "Sequence", $hash->{helper}{session_seq_num}); readingsBulkUpdate($hash, "presence", "absent"); readingsBulkUpdate($hash, "volume", "?"); readingsBulkUpdate($hash, "mute", "?"); readingsEndUpdate($hash, 1); } delete($hash->{helper}{RUNNING_PID_GET}); return; } } readingsBeginUpdate($hash); if ($myVol ne ReadingsVal($name, "volume", "0")) { Log3 $name, 4, "$name [NonBlocking-VIERA_GetDone()]: Set reading volume to $myVol"; readingsBulkUpdate($hash, "volume", $myVol); } if ($myMute ne ReadingsVal($name, "mute", "0")) { Log3 $name, 4, "$name [NonBlocking-VIERA_GetDone()]: Set reading mute to $myMute"; readingsBulkUpdate($hash, "mute", $myMute); } if (ReadingsVal($name,"state","off") ne "on") { readingsBulkUpdate($hash, "state", "on"); readingsBulkUpdate($hash, "presence", "present"); } readingsEndUpdate($hash, 1); delete($hash->{helper}{RUNNING_PID_GET}); } # Callback of non blocking when function VIERA_GetDoIt runs into timeout. # Param1: Hash of FHEM-Device # Return: no return code sub VIERA_GetAbortFn($) { my ($hash) = @_; my $name = $hash->{NAME}; delete($hash->{helper}{RUNNING_PID_GET}); Log3 $name, 2, "$name [NonBlocking-VIERA_GetAbortFn()]: BlockingCall for $hash->{NAME} was aborted, timeout reached"; return; } # open web page on the TV sub VIERA_webpage($$) { my ($hash, $url) = @_; my $name = $hash->{NAME}; my $i = 0; my $params = ""; my $message = ""; my $answer = ""; my @msg ; my $iS ; my $iE ; my $sock ; my $clientsock ; # check if Encryption is ready VIERA_Encrypted_Command($hash, "" , "web"); #start Web Browser and open url""" $params = "vc_appresource_id=1063"; $message = VIERA_Build_soap_message_Encrypt($hash, "X_LaunchApp", $params, "s"); $hash->{helper}{BUFFER} = ""; if (exists($hash->{helper}{RUNNING_PID_GET}) and $i < 5) { sleep (0.1); $i += 1; } VIERA_connection($hash, $message); $answer = VIERA_Encrypt_Answer($hash); if (!defined($answer)) { Log3 $name, 3, "$name cannot start web browser"; return; } $iS = index($answer, ""); $iE = index($answer, ""); my $sessionId = substr($answer, $iS+13, $iE-$iS-13); # setup a server socket where URL will be served my $localip = $hash->{helper}{mylocalIP} ; my $localport = 56000 + int(rand(9000)); $sock = new IO::Socket::INET ( LocalAddr => $localip, LocalPort => $localport, Proto => 'tcp', Listen => 5, Reuse => 1 ); if(defined ($sock)) { Log3 $name, 4, "$name: open socket for web page address: $localip:$localport"; } else { Log3 $name, 3, "$name: not able to open socket for web page address"; return undef; } $params = "vc_app$sessionId\r\n"; $params .= "panasonic-viera 0.2\r\n"; $params .= "$localip:$localport"; $message = VIERA_Build_soap_message_Encrypt($hash, "X_ConnectApp", $params, "s"); $hash->{helper}{BUFFER} = ""; if (exists($hash->{helper}{RUNNING_PID_GET}) and $i < 5) { sleep (0.1); $i += 1; } VIERA_connection($hash, $message); $answer = VIERA_Encrypt_Answer($hash); if (!defined($answer)) { Log3 $name, 3, "$name: cannot transmit web address"; return; } $i = 0; $clientsock = $sock -> accept(); if(!defined ($clientsock) and $i < 5) { sleep 0.1; $i += 1; $clientsock = $sock -> accept(); } if(!defined ($clientsock)) { Log3 $name, 3, "$name: not able to open new socket for web page address"; return undef; } my $client_address = $clientsock -> peerhost(); my $client_port = $clientsock -> peerport(); Log3 $name, 4, "$name: connection from $client_address:$client_port established"; my $l1 = length($url) & 0xFF; @msg = (0xf4, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, $l1); my $l2 = @msg; for($i = 0; $i < $l1; $i++) {$msg[$l2+$i] = ord(substr($url, $i, 1))}; # get unicode for characters $msg[$l2+$l1] = 0x00; my $msgH = ""; for ($i = 0; $i <= $l1+$l2; $i++) {$msgH .= chr($msg[$i])}; # print $clientsock $msgH; $clientsock->send($msgH); shutdown($clientsock, 1); close($clientsock); close($sock); return; } sub VIERA_get_local_ip_address ($) { my ($hash) = @_; my $socket = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $hash->{helper}{HOST}, # "google.com", PeerPort => $hash->{helper}{PORT}, # "80", ); # A side-effect of making a socket connection is that our IP address # is available from the 'sockhost' method my $local_ip_address = $socket->sockhost; close($socket); return $local_ip_address; } ################ special encryption ########################################################### ###################################################################################################################### sub VIERA_Encrypted_Command($$$) { my ($hash, $command, $type) = @_; my $i = 0; my $message = ""; my $params = ""; my $task = ""; if ($hash->{helper}{ENCRYPTION} eq "yes") { if ($hash->{helper}{pincode} eq "0000") {return VIERA_request_pin_code($hash)}; if ($hash->{helper}{stop} eq "yes") {return 0 if (!VIERA_authorize_pin_code($hash))}; if ($hash->{helper}{session_seq_num} eq "None") {return 0 if (!VIERA_request_session_id($hash))}; } if ( $type eq "key" ) { $params = "NRC_$command-ONOFF"; $task = "X_SendKey"; } elsif ( $type eq "app" ) { $params = "vc_app"; $params .= "product_id=$command"; $task = "X_LaunchApp"; } elsif ( $type eq "web" ) { return; } else {return}; $message = VIERA_Build_soap_message_Encrypt($hash, $task, $params, "u"); $hash->{helper}{BUFFER} = ""; if (exists($hash->{helper}{RUNNING_PID_GET}) and $i < 5) { sleep (0.1); $i += 1; } VIERA_connection($hash, $message); return; } sub VIERA_Encrypt_Answer($) { my ($hash) = @_; my $name = $hash->{NAME}; my $answer = ""; $answer = $hash->{helper}{BUFFER} if ($hash->{helper}{BUFFER} ne ""); if (index($answer, "HTTP/1.1 200 OK") == -1) { if (index($answer, "Bad Request") != -1) { if ($hash->{helper}{session_seq_num} ne "None") { $hash->{helper}{session_seq_num} -= 1; readingsSingleUpdate($hash, "Sequence", $hash->{helper}{session_seq_num},1); } } elsif (index($answer, "Internal Server Error") != -1) { $hash->{helper}{session_seq_num} = "None"; readingsSingleUpdate($hash, "Sequence", $hash->{helper}{session_seq_num},1); Log3 $name, 4, "$name reset of Rolling Key"; } Log3 $name, 3, "$name wrong encrypted VIERA command: \r\n\"$answer\""; return undef; } if ($hash->{helper}{is_encrypted} == 1) { my $iS = index($answer, ""); my $iE = index($answer, ""); $answer = substr($answer, $iS+13, $iE-$iS-13); $answer = VIERA_decrypt_soap_payload($answer, $hash->{helper}{session_key}, $hash->{helper}{session_IV}); Log3 $name, 5, "$name decrypted VIERA answer: \r\n\"$answer\""; } return $answer; } #check if TV is encrypted sub VIERA_CeckEncryption($) { my ($hash) = @_; my $nom = $hash->{TYPE}."_".$hash->{NAME}."_"; my $answer = ""; my $iS = -1; $hash->{helper}{BUFFER} = ""; VIERA_Encrypted_Command($hash, "INFO", "key"); if ($hash->{helper}{BUFFER} ne "") { $answer = $hash->{helper}{BUFFER} ; $iS = index($answer, "401"); if ( $iS != -1) { $hash->{helper}{ENCRYPTION} = "yes"; } else { $hash->{helper}{ENCRYPTION} = "no"; } } readingsBeginUpdate($hash); readingsBulkUpdate($hash, "Encryption", $hash->{helper}{ENCRYPTION}); readingsEndUpdate($hash, 1); my $error = setKeyValue("$nom.ENCRYPTION", $hash->{helper}{ENCRYPTION}); return; } # Calculate encryption sub VIERA_derive_session_keys($) { my ($hash) = @_; my $iv =""; my @iv_vals ; my @key_vals ; my $session_key = ""; my $session_hmac_key = ""; my $nom = $hash->{TYPE}."_".$hash->{NAME}."_"; my $i = ""; # decode ChallengeKey $iv = decode_base64($hash->{helper}{enc_key}); $hash->{helper}{session_IV} = $iv; for($i = 0; $i < 16; $i++) {$iv_vals[$i] = ord(substr($iv, $i, 1))}; # get unicode for characters for($i = 0; $i < 16; $i++) {$key_vals[$i] = 0}; # Derive key from IV $i = 0 ; while ($i < 16) { $key_vals[$i] = $iv_vals[$i + 2]; $key_vals[$i + 1] = $iv_vals[$i + 3]; $key_vals[$i + 2] = $iv_vals[$i + 0]; $key_vals[$i + 3] = $iv_vals[$i + 1]; $i += 4; } # Convert key character codes to bytes $session_key = ""; for($i = 0; $i < 16; $i++) {$session_key .= chr($key_vals[$i] & 0xFF)}; $hash->{helper}{session_key} = $session_key; $i = length ($session_key); # HMAC key for comms is just the IV repeated twice $session_hmac_key = $iv.$iv; $hash->{helper}{session_hmac_key} = $session_hmac_key; readingsBeginUpdate($hash); readingsBulkUpdate($hash, "app_id", $hash->{helper}{app_id}); readingsEndUpdate($hash, 1); my $error = ""; my $value = ""; $error = setKeyValue("$nom.app_id", $hash->{helper}{app_id}); $value = encode_base64($hash->{helper}{session_IV}, ""); $error = setKeyValue("$nom.session_IV", $value); $value = encode_base64($hash->{helper}{session_key}, ""); $error = setKeyValue("$nom.session_key", $value); $value = encode_base64($hash->{helper}{session_hmac_key}, ""); $error = setKeyValue("$nom.session_hmac_key", $value); return; } # The encrypted payload must begin with a 16-byte header (12 random bytes, and 4 bytes for the payload length in big endian) # Note: the server does not appear to ever send back valid payload lengths in bytes 13-16, so I would assume these can also # be randomized by the client, but we'll set them anyway to be safe. sub VIERA_encrypt_soap_payload($$$$) { my ($message, $key, $IV, $hmac_key) = @_; my $i = ""; my $len = ""; my $range = 255; my $payload = ""; for($i = 0; $i < 12; $i++) {$payload .= chr( int(rand($range)) )}; # for($i = 0; $i < 12; $i++) {$payload .= "A"}; $len = length($message); $payload .= pack("N", $len); # make big endian # $len = (($len & 0x000000FF) + ($len & 0x0000FF00) * 2**8 + ($len & 0x00FF0000) * 2**16 + ($len & 0xFF000000) * 2**24); $payload .= $message; # Initialize AES my $cbc = Crypt::Mode::CBC->new("AES", 4); my $ciphertext = $cbc->encrypt($payload, $key, $IV); my $sig = hmac_sha256($ciphertext, $hmac_key); # Concat HMAC with AES-encrypted payload my $encrypttext = $ciphertext.$sig; $encrypttext = encode_base64($encrypttext, ""); return $encrypttext; } sub VIERA_decrypt_soap_payload($$$) { my ($message, $key, $IV) = @_; # Initialize AES my $cbc = Crypt::Mode::CBC->new("AES", 4); $message = decode_base64($message); # Decrypt my $decrypted = $cbc->decrypt($message, $key, $IV); # Unpad and return my $i = length($decrypted) ; $decrypted = substr($decrypted, 16, $i-16); $i = index($decrypted, chr(0x00)); $decrypted = substr($decrypted, 0, $i); return $decrypted ; } sub VIERA_request_pin_code($) { my ($hash) = @_; my $nom = $hash->{TYPE}."_".$hash->{NAME}."_"; # Stop communication with TV until PIN authorized $hash->{helper}{stop} = "yes"; my $error = setKeyValue("$nom.stop", $hash->{helper}{stop}); readingsBeginUpdate($hash); readingsBulkUpdate($hash, "stop", $hash->{helper}{stop}); readingsEndUpdate($hash, 1); # First let's ask for a pin code and get a challenge key back my $params = "FHEMremote"; my $message = VIERA_Build_soap_message_Encrypt($hash, "X_DisplayPinCode", $params, "u"); $hash->{helper}{BUFFER} = ""; VIERA_connection($hash, $message); my $answer = VIERA_Encrypt_Answer($hash); if (!defined ($answer)) {return 0}; my $iS = index($answer, ""); my $iE = index($answer, ""); if ($iS == -1) {return 0}; $answer = substr($answer, $iS+16, $iE-$iS-16); $hash->{helper}{challenge} = decode_base64($answer); return 1; } sub VIERA_authorize_pin_code($) { my ($hash) = @_; my $i = 0; my $iS = 0; my $iE = 0; my $pincode = $hash->{helper}{pincode}; my $nom = $hash->{TYPE}."_".$hash->{NAME}."_"; # Second, let's encrypt the pin code using the challenge key and send it back to authenticate # Derive key from IV my $iv = $hash->{helper}{challenge}; my $key = ""; $i = 0; while ($i < 16) { $key .= chr(~ord(substr($iv, $i+3, 1)) & 0xFF); $key .= chr(~ord(substr($iv, $i+2, 1)) & 0xFF); $key .= chr(~ord(substr($iv, $i+1, 1)) & 0xFF); $key .= chr(~ord(substr($iv, $i , 1)) & 0xFF); $i += 4; } # Derive HMAC key from IV & HMAC key mask (taken from libtvconnect.so) my @hmac_key_mask_vals = (0x15,0xC9,0x5A,0xC2,0xB0,0x8A,0xA7,0xEB,0x4E,0x22,0x8F,0x81,0x1E,0x34,0xD0,0x4F, 0xA5,0x4B,0xA7,0xDC,0xAC,0x98,0x79,0xFA,0x8A,0xCD,0xA3,0xFC,0x24,0x4F,0x38,0x54); my $hmac_key = ""; $i = 0; while ($i < 32) { $hmac_key .= chr($hmac_key_mask_vals[$i] ^ ord(substr($iv, (($i + 2) & 0x0F), 1))); $hmac_key .= chr($hmac_key_mask_vals[$i + 1] ^ ord(substr($iv, (($i + 3) & 0x0F), 1))); $hmac_key .= chr($hmac_key_mask_vals[$i + 2] ^ ord(substr($iv, (($i ) & 0x0F), 1))); $hmac_key .= chr($hmac_key_mask_vals[$i + 3] ^ ord(substr($iv, (($i + 1) & 0x0F), 1))); $i += 4; } # Encrypt X_PinCode argument and send it within an X_AuthInfo tag my $params = ""; $params .= VIERA_encrypt_soap_payload("$pincode", $key, $iv, $hmac_key); $params .= ""; my $message = VIERA_Build_soap_message_Encrypt($hash, "X_RequestAuth", $params, "u"); $hash->{helper}{BUFFER} = ""; VIERA_connection($hash, $message); # communication can start again $hash->{helper}{stop} = "no"; my $error = setKeyValue("$nom.stop", $hash->{helper}{stop}); readingsBeginUpdate($hash); readingsBulkUpdate($hash, "stop", $hash->{helper}{stop}); readingsEndUpdate($hash, 1); my $answer = VIERA_Encrypt_Answer($hash); if (!defined ($answer)) {return 0}; # Parse and decrypt X_AuthResult $iS = index($answer, ""); $iE = index($answer, ""); $answer = substr($answer, $iS+14, $iE-$iS-14); $answer = VIERA_decrypt_soap_payload($answer, $key, $iv); # Set session application ID and encryption key $iS = index($answer, ""); $iE = index($answer, ""); $hash->{helper}{app_id} = substr($answer, $iS+17, $iE-$iS-17); $iS = index($answer, ""); $iE = index($answer, ""); $hash->{helper}{enc_key} = substr($answer, $iS+11, $iE-$iS-11); # Derive AES & HMAC keys from X_Keyword VIERA_derive_session_keys($hash); return 1; } sub VIERA_request_session_id($) { my ($hash) = @_; # Thirdly, let's ask for a session. We'll need to use a valid session ID for encrypted NRC commands. # We need to send an encrypted version of X_ApplicationId my $app_id = $hash->{helper}{app_id}; my $session_key = $hash->{helper}{session_key}; my $session_IV = $hash->{helper}{session_IV}; my $session_hmac_key = $hash->{helper}{session_hmac_key}; $hash->{helper}{mylocalIP} = VIERA_get_local_ip_address($hash); my $encinfo = VIERA_encrypt_soap_payload("$app_id", $session_key, $session_IV, $session_hmac_key); # Send the encrypted SOAP request along with plain text X_ApplicationId my $params = "$app_id$encinfo"; my $message = VIERA_Build_soap_message_Encrypt($hash, "X_GetEncryptSessionId", $params, "u"); $hash->{helper}{BUFFER} = ""; VIERA_connection($hash, $message); my $answer = VIERA_Encrypt_Answer($hash); if (!defined ($answer)) {return 0}; my $iS = index($answer, ""); my $iE = index($answer, ""); $answer = substr($answer, $iS+13, $iE-$iS-13); $answer = VIERA_decrypt_soap_payload($answer, $session_key, $session_IV); # Set session ID and begin sequence number at 1. We have to increment the sequence number upon each successful NRC command. $iS = index($answer, ""); $iE = index($answer, ""); $hash->{helper}{session_id} = substr($answer, $iS+13, $iE-$iS-13); $hash->{helper}{session_seq_num} = 1; readingsBeginUpdate($hash); readingsBulkUpdate($hash, "session_id", $hash->{helper}{session_id}); readingsBulkUpdate($hash, "Sequence", $hash->{helper}{session_seq_num}); readingsEndUpdate($hash, 1); return 1; } ###################################### end of special encrytion commands ############################# # Callback from 95_remotecontrol for command makenotify. # Param1: Name of remoteControl device # Param2: Name of target FHEM device sub VIERA_RCmakenotify($$) { my ($nam, $ndev) = @_; my $nname="notify_$nam"; fhem("define $nname notify $nam set $ndev remoteControl ".'$EVENT',1); Log3 undef, 2, "[remoteControl:VIERA] Notify created: $nname"; return "Notify created by VIERA: $nname"; } # Callback from 95_remotecontrol for command layout. Creates non svg layout sub VIERA_RClayout_TV() { my @row; my $i = 0; $row[$i++]="on_off:POWEROFF2, TV:TV, AV:HDMI"; $row[$i++]="menu:MENU, display_mode:ASPECT, EPG:GUIDE"; $row[$i++]="VIERA_link:VIERA_LINK, VIERA_tools:VTOOLS, VIERA_connect:INTERNET"; $row[$i++]=":blank, :blank, :blank"; $row[$i++]="info:INFO2, up:UP, cancel:EXIT"; $row[$i++]="left:LEFT, enter:ENTER, right:RIGHT"; $row[$i++]="option:SUBMENU, down:DOWN, return:RETURN"; $row[$i++]="red:RED, :blank, green:GREEN"; $row[$i++]="yellow:YELLOW, :blank, blue:BLUE"; $row[$i++]="digit_1:1, digit_2:2, digit_3:3"; $row[$i++]="digit_4:4, digit_5:5, digit_6:6"; $row[$i++]="digit_7:7, digit_8:8, digit_9:9"; $row[$i++]="mute:MUTE, digit_0:0, tune:PRECH"; $row[$i++]=":blank, :blank, :blank"; $row[$i++]="volume_up:VOLUP, :blank, channel_up:CHUP"; $row[$i++]=":VOL, :blank, :PROG"; $row[$i++]="volume_down:VOLDOWN, :blank, channel_down:CHDOWN"; $row[$i++]=":blank, :blank, :blank"; $row[$i++]="rewind:REWIND, play:PLAY, fast_foreward:FF"; $row[$i++]="stop:STOP, pause:PAUSE, record:REC"; # Replace spaces with no space for (@row) {tr/ //d} $row[$i++]="attr rc_iconpath icons/remotecontrol"; $row[$i++]="attr rc_iconprefix black_btn_"; return @row; } # Callback from 95_remotecontrol for command layout. Creates svg layout sub VIERA_RClayout_TV_SVG() { my @row; my $i = 0; $row[$i++]="on_off:rc_POWER.svg, TV:rc_TV2.svg, AV:rc_AV.svg"; $row[$i++]="menu:rc_MENU.svg, display_mode:rc_ASPECT.svg, EPG:rc_EPG.svg"; $row[$i++]="VIERA_link:rc_VIERA_LINK.svg, VIERA_tools:rc_VIERA_TOOLS.svg, VIERA_connect:rc_WEB.svg"; $row[$i++]=":rc_BLANK.svg, :rc_BLANK.svg, :rc_BLANK.svg"; $row[$i++]="info:rc_INFO2.svg, up:rc_UP.svg, cancel:rc_EXIT.svg"; $row[$i++]="left:rc_LEFT.svg, enter:rc_dot.svg, right:rc_RIGHT.svg"; $row[$i++]="option:rc_OPTIONS.svg, down:rc_DOWN.svg, return:rc_BACK.svg"; $row[$i++]="red:rc_RED.svg, :rc_BLANK.svg, green:rc_GREEN.svg"; $row[$i++]="yellow:rc_YELLOW.svg, :rc_BLANK.svg, blue:rc_BLUE.svg"; $row[$i++]="digit_1:rc_1.svg, digit_2:rc_2.svg, digit_3:rc_3.svg"; $row[$i++]="digit_4:rc_4.svg, digit_5:rc_5.svg, digit_6:rc_6.svg"; $row[$i++]="digit_7:rc_7.svg, digit_8:rc_8.svg, digit_9:rc_9.svg"; $row[$i++]="mute:rc_MUTE.svg, digit_0:rc_0.svg, tune:rc_BACK.svg"; $row[$i++]=":rc_BLANK.svg, :rc_BLANK.svg, :rc_BLANK.svg"; $row[$i++]="volume_up:rc_UP.svg, :rc_BLANK.svg, channel_up:rc_UP.svg"; $row[$i++]=":rc_VOL.svg, :rc_BLANK.svg, :rc_PROG.svg"; $row[$i++]="volume_down:rc_DOWN.svg, :rc_BLANK.svg, channel_down:rc_DOWN.svg"; $row[$i++]=":rc_BLANK.svg, :rc_BLANK.svg, :rc_BLANK.svg"; $row[$i++]="rewind:rc_REW.svg, play:rc_PLAY.svg, fast_foreward:rc_FF.svg"; $row[$i++]="stop:rc_STOP.svg, pause:rc_PAUSE.svg, record:rc_REC.svg"; # Replace spaces with no space for (@row) {tr/ //d} return @row; } 1; =pod =item summary VIERA control Panasonic TV via network =item summary_DE Steuerung von Panasonic TV über Netzwerk =begin html

VIERA

    Define
      define <name> VIERA <host> <interval> <pin code> <?>

      This module controls Panasonic TV device over ethernet, old TV's and new TV's with crypted communication. It's possible to change volume, switch it off, mute/unmute the TV or send commands like the ones on the remote control.

      Defining a VIERA device will schedule an internal task, which periodically reads the status of volume and mute status and triggers notify/filelog commands.

      To implement the module several steps may be needed. First define the TV with PinCode <0000>, ? for the encryption and any time interval you like (60 is ok). Switch TV on and wait until the module detects the encyption mode yes/no. If encryption is equal no, you are done. If encryption is yes execute any command like "set myTV1 off". A PinCode should be displayed on the TV. Edit the definition delete the "?" and replace 0000 with the PinCode. Execute the command again while the PinCode is still displayed on TV. You are done.
      state: Initialized - on (green) - dormant (orange) - off (red)

      This module may require further PERL libraries. For raspbian you have to enter the following commands in the terminal:
      sudo cpan
      install MIME::Base64
      install Crypt::Mode::CBC
      install Digest::SHA
      q
      for exit.

      Notes:
        Activate volume remotecontrol by DLNA: Menu -> Setup -> Network Setup -> Network Link Settings -> DLNA RemoteVolume -> On

      Example:
        define myTV1 VIERA 192.168.178.20 ## PinCode ?
        define myTV1 VIERA 192.168.178.20 60 0000 ? (with custom interval of 60 seconds and start PinCode)
        define myTV1 VIERA 192.168.178.20 60 1234 (changed definition with PinCode transfered from TV)

    Set
      set <name> <command> [<value>]

      Currently, the following commands are defined.
        on_off
        mute [on|off]
        volume [0-100]
        channel [1-9999]
        input [hdmi1|hdmi2|hdmi3|hdmi4|sdCard|tv]
        statusRequest
        web
      For the web command, please use https:// up front. Example:
      set <name> web https://google.de


      Remote control commands, depending on your TV
      For this application the following commands are available:

      set <name> remoteControl .........

        3D_button => switch to 3D mode
        apps => show apps
        aspect => change aspect
        AV => switch input to AV
        blue_button => blue button
        cancel => cancel or clear
        channel_down => one channel down
        channel_up => one channel up
        digit_0 => Digit 0
        digit_1 => Digit 1
        digit_2 => Digit 2
        digit_3 => Digit 3
        digit_4 => Digit 4
        digit_5 => Digit 5
        digit_6 => Digit 6
        digit_7 => Digit 7
        digit_8 => Digit 8
        digit_9 => Digit 9
        display_mode
        down => cursor down
        enter => enter
        EPG => program guide
        exit => exit
        fast_forward => fast foreward
        favorite => favorites
        game
        green_button => green button
        guide => guide
        HDMI_1 => input HDMI 1
        HDMI_2 => input HDMI 2
        HDMI_3 => input HDMI 3
        HDMI_4 => input HDMI 4
        hold => hold
        home => home
        index => index
        info => info
        last_view => last view
        left => cursor left
        menu => menu
        mute => mute
        noise_reduction => noise reduction
        on_off => on off switch
        option => option
        pause => pause
        play => play
        program => program
        record => record
        red_button => red button
        return => return
        rewind => rewind
        right => cursor right
        SD_card => SD card
        skip_next => skip to next
        skip_previous => skip to previous
        stop => stop
        subtitle => subtitle
        tune => tune
        TV => TV
        up => cursor up
        videotext => videotext
        VIERA_connect
        VIERA_link
        VIERA_tools
        volume_down => volume down
        volume_up => volume up
        yellow_button => yellow button


      set <name> remoteControlApp ......

        netflix
        youtube
        shoutcast
        calendar
        browser
        amazonprime
        iplayer
        bbc_iplayer
        itv
        all_4
        demand_5
        recorded_tv
        multi_window
        bbc_news
        bbc_sport
        weather
        developer

      Example:
        set <name> mute on
        set <name> volume 20

    Get
      get <name> <what>

      Currently, the following commands are defined and return the current state of the TV.
        mute
        volume
        presence

    Attributes
      blocking [0|1]

    Generated events:
    • volume
    • mute
    • presence
    • state
=end html =begin html_DE

VIERA

    Define
      define <name> VIERA <host> <interval> <pin code> <?>

      Dieses Modul steuert einen Panasonic Fernseher (unverschlüsselt oder verschlüsselt) über das Netzwerk. Es ist möglich den Fernseher auszuschalten, die Lautstärke zu ändern oder zu muten bzw. unmuten oder Befehle wie auf der Fernbedinung zu senden,

      Beim definieren des Gerätes in FHEM wird ein interner Timer gestartet, welcher zyklisch den Status der Lautstärke und des Mute-Zustand ausliest. Das Intervall des Timer kann über den Parameter <interval> geändert werden und ein notify wird eingerichtet.

      Um das Modul einzurichten können mehrere Schritte notwendig sein. Zuerst wird das Modul definiert mit dem PinCode <0000>, ? für die Abfrage der Verschlüsselung und einem beliebigen Zeitinterval (60 ist ok). Dann den TV einschalten und warten bis die Verschlüsselung yes/no erkannt wird. Wenn der TV nicht verschlüsselt ist, ist die Einrichtung abgeschlossen. Ist der TV verschlüsselt, dann bitte ein Kommando ausführen (set myTV1 off), danach wird ein PinCode am TV angezeigt. Die Definition editieren den PinCode eintragen und das ? löschen. Das Kommando nochmals ausführen, solange der PinCode angezeigt wird. Das wars.
      state: Initialized - on (grün) - dormant (orange) - off (rot)

      Diese Modul benötigt evtl. weitere PERL Bibliotheken. Für raspbian bitte folgende Kommandos im Terminmal eingeben:
      sudo cpan
      install MIME::Base64
      install Crypt::Mode::CBC
      install Digest::SHA
      q
      für exit.

      Anmerkung:
        Aktivieren von Fernbedienung der Lautstärke per DLNA: Menü -> Setup -> Netzwerk-Setup -> Netzwerkverbindungsein. -> DLNA-Fernbed. Lautst. -> Ein

      Beispiel:
        define myTV1 VIERA 192.168.178.20 ## PinCode ?
        define myTV1 VIERA 192.168.178.20 60 0000 ? (mit einem Interval von 60 Sekunden und dem PinCode für den 1. Schritt)
        define myTV1 VIERA 192.168.178.20 60 1234 (mit geändertem PinCode wie am TV angezeigt)

    Set
      set <name> <command> [<value>]

      Zur Zeit sind die folgenden Befehle implementiert:
        on_off
        mute [on|off]
        volume [0-100]
        channel [1-9999]
        input [hdmi1|hdmi2|hdmi3|hdmi4|sdCard|tv]
        statusRequest
        web
      Beim Kommando web, bitte immer https:// voranstellen. Beispiel:
      set <name> web https://google.de


      Fernbedienung (Kann vielleicht nach Modell variieren).
      Das Modul hat die folgenden Fernbedienbefehle implementiert:

      set <name> remoteControl .........

        3D_button => in 3D Modus schalten
        apps => apps anzeigen
        aspect => Aspekt aendern
        AV => Eingang auf AV umschalten
        blue_button => blaue Taste
        cancel => loeschen
        channel_down => Kanal nach unten schalten
        channel_up => Kanal nach oben schalten
        digit_0 => Zahl 0
        digit_1 => Zahl 1
        digit_2 => Zahl 2
        digit_3 => Zahl 3
        digit_4 => Zahl 4
        digit_5 => Zahl 5
        digit_6 => Zahl 6
        digit_7 => Zahl 7
        digit_8 => Zahl 8
        digit_9 => Zahl 9
        display_mode
        down => Pfeil nach unten
        enter => Eingabe
        EPG => elektronische Programmzeitschrift
        exit => beenden
        fast_forward => vorspulen
        favorite => Favoriten
        game
        green_button => gruene Taste
        guide => Programmzeitschrift
        HDMI_1 => Eingang HDMI 1
        HDMI_2 => Eingang HDMI 2
        HDMI_3 => Eingang HDMI 3
        HDMI_4 => Eingang HDMI 4
        hold =>
        home => Startbildschirm
        index => Index
        info => Info
        last_view => letzte Ansicht
        left => Pfeil nach unten
        menu => Menue
        mute => Ton aus
        noise_reduction => Rauschreduzierung
        on_off => Ein- / Ausschalter
        option => Option
        pause => Pause
        play => Abspielen
        program => Programm
        record => Aufnahme
        red_button => rote Taste
        return => zurueck
        rewind => zurueckspulen
        right => Pfeil nach rechts
        SD_card => => SD Karte
        skip_next => springe vorwaerts
        skip_previous => springe zurueck
        stop => Stop
        subtitle => Untertitel
        tune => Suchlauf
        TV => TV
        up => Pfeil nach oben
        videotext => Videotext
        VIERA_connect
        VIERA_link
        VIERA_tools
        volume_down => Lautstaerke leiser
        volume_up => Lautstaerke lauter
        yellow_button => gelbe Taste


      set <name> remoteControlApp ......

        netflix
        youtube
        shoutcast
        calendar
        browser
        amazonprime
        iplayer
        bbc_iplayer
        itv
        all_4
        demand_5
        recorded_tv
        multi_window
        bbc_news
        bbc_sport
        weather
        developer

      Beispiel:
        set <name> mute on
        set <name> volume 20

    Get
      get <name> <what>

      Die folgenden Befehle sind definiert und geben den entsprechenden Wert zurück, der vom Fernseher zurückgegeben wurde.
        mute
        volume
        presence

    Attribute
      blocking [0|1]

    Generierte events:
    • volume
    • mute
    • presence
    • state
=end html_DE =cut