Overview

The RI PC Platform implementation provides a mechanism for receiving key events via telnet. This mechanism allows key events to be inserted into the event queues as if the keys were generated from an actual remote. Once submitted to the platform's event queues, the key events will be processed through the RI Stack using the same IO path as regular remote events.

Interactive Telnet Session

To generate key events using a terminal console, simply start up the RI Emulator and run telnet using

telnet locahost 23000

This will open a telnet session to the emulator. The console will show the available commands you can use to communicate with the emulator. Enter 'd' for the Display menu. You should now see a menu with a key input entry. Enter 'k'. You will be prompted for the key value. Enter the key value you want to send to the emulator; valid values are the enumerations you find in the ri_ui_manager.h file (https://community.cablelabs.com/svn/OCAPRI/trunk/ri/RI_Platform/include/ri_ui_manager.h). For example:

  • A value of 0 maps to the 'Enter key
  • A value of 3 maps to the 'Up' arrow key
  • A value of 4 maps to the 'Down' arrow key
  • A value of 5 maps to the 'Left' arrow key
  • A value of 6 maps to the 'Right; arrow key

Scripting Key Events

With a little finesse, the telnet session can be scripted to automate key events. The script below is a Perl script that can be used as a template for doing this. To execute the script, use

genkey.pl key

where genkey.pl is the name of the script. If you need to explicitly specify the host use:

genkey.pl --host=10.0.0.1 key

Be sure to replace the 10.0.0.1 value above with the IP address of the machine you are using.

Here is the Perl script (it is also an attachment below)

#!/usr/bin/perl
#
# This script is used to automate key events. It is an example on how
# to generate keys for the RI PC Platform, as if they were coming from
# an actual remote.
#
# The keys that are automated are defined in the "keys" array below.
# The "loop" variable identifes how many times the key sequence should
# be executed.
#

use strict;
use Cwd;
use Switch;
use Getopt::Long;
use Net::Telnet;

# Default RI process host to connect to
my $host = '127.0.0.1';

# Default RI process port to connect to
my $port = '23000';

# Default location of temporary persistent storage
my $tmpdir = '/tmp';

# Try to reconnect that many times if first connection attempt fails
my $reconnect = 0;

# Delay in seconds between reconnection attempts
my $delay = 1;

# The telnet connection.
my $telnet;

# Flag indicating telnet connected successfully
my $connected = 0;

# The keys to automate.
#  Down-Arrow, Select ten times
#  Right-Arrow, Select five times
#  Up-Arrow, Select five times
#  Left-Arrow, Select ten times
#
my @keys = ( 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
             6, 0, 0, 0, 0, 0,
             3, 0, 0, 0, 0, 0,
             5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

# Number of times to loop through key automation.
my $loop = 100;

#
# Print a usage string.
#
sub Usage()
{
  die
"Usage: $0 [OPTION] [COMMAND]

Options:
  --host=IPADDR   [127.0.0.1]   RI Host IP address to use
  --port=IPPORT   [23000]       RI Host IP port to use
  --tmpdir=DIR    [/tmp]        Directory for temporary files
  --reconnect=NUM [0]           Number or reconnection attempts
  --delay=SECS    [1]           Delay in seconds between attempts
";
}

#
# Open a telnet session.
#
sub OpenTelnet($)
{
  my $result = "";

  print STDERR "Connecting to $host:$port...\n";

  $telnet = new Net::Telnet(Timeout => 1, Errmode => "return", Port => $port);
  $connected = $telnet->open($host);
  if ($connected == 0 && $reconnect > 0)
  {
    while ($reconnect > 0 && $connected == 0)
    {
      $reconnect--;
      print STDERR ".\n";
      sleep $delay;
      $connected = $telnet->open($host);
    }
  }

  if ($connected != 0)
  {
    print STDERR "Connected.\n";
    $telnet->waitfor("/>/");
  }
  else
  {
    die "Unable to connect to $host:$port!\n";
  }
  return $result;
}

#
# Close a telnet session.
#
sub CloseTelnet($)
{
  my $result = "";

  if ($connected != 0)
  {
    print STDERR "Logging off\n";
    $telnet->print("x");
    $result = $telnet->getline();
    $telnet->waitfor("/>/");
    $telnet->close();
  }
  else
  {
    die "Unable to connect to $host:$port!\n";
  }
  return $result;
}

#
# Automate keys.
#
sub AutomateKeys()
{
  my $key;
  my $count = 0;

  do
  {
    print STDERR "Key Event Loop Count: $count\n";
    foreach $key (@keys)
    {
      print STDERR "RI Key Event: $key\n";
      CmdKeyInput($key);
      sleep 2;
    }
    $count++;
  } until ($count >= $loop);
}

#
# Process key input.
#
sub KeyInput($)
{
  my $key = shift;
  my $result = "";

  if ($connected != 0)
  {
    $telnet->print("d");
    $result = $telnet->getline();
    $telnet->waitfor("/>/");
    $telnet->print("k");
    $result = $telnet->getline();
    $telnet->waitfor("/>/");
    $telnet->print("$key");
    $result = $telnet->getline();
    $telnet->waitfor("/>/");
    $telnet->print("x");
    $result = $telnet->getline();
    $telnet->waitfor("/>/");
  }
  else
  {
    die "Unable to connect to $host:$port!\n";
  }
  return $result;
}

#
# Execute a key pressed event.
#
sub CmdKeyInput($)
{
  my $key = shift;
  my $result = KeyInput($key);
  if ($result =~ /ERROR/)
  {
    die "Command key input failed: $result";
  }
}

#
# Send a log message to the emulator.
#
sub SendLog($)
{
  my $logText = shift;
  my $connected = 0;
  my $result    = "";

  if ($connected != 0)
  {
    print STDERR "Logging message.\n";
    $telnet->waitfor("/>/");
    $telnet->print("p");
    $result = $telnet->getline();
    $telnet->waitfor("/>/");
    $telnet->print("l");
    $result = $telnet->getline();
    $telnet->waitfor("/>/");
    $telnet->print("$logText");
    $result = $telnet->getline();
    $telnet->waitfor("/>/");
    $telnet->print("x");
    $result = $telnet->getline();
    $telnet->waitfor("/>/");
  }
  else
  {
    die "Unable to connect to $host:$port!\n";
  }
  return $result;
}

#
# Execute a log command.
#
sub CmdSendLog($)
{
  my $logText = shift;
  my $result = SendLog($logText);
  if ($result =~ /ERROR/)
  {
    die "Command send log failed: $result";
  }
}

#
# main()
#
$SIG{PIPE} = "IGNORE";
GetOptions ('host=s'      => \$host,
            'port=i'      => \$port,
            'tmpdir=s'    => \$tmpdir,
            'reconnect=i' => \$reconnect,
            'delay=i'     => \$delay
           );

#print "RI host:                $host\n";
#print "RI port:                $port\n";
#print "Temporary storage:      $tmpdir\n";
#print "Re-connection attempts: $reconnect\n";
#print "Delay between attempts: $delay\n";

switch (@ARGV[0])
{
  case "key"
  {
    OpenTelnet(@ARGV[1]);
    #CmdKeyInput(@ARGV[1]);
    AutomateKeys();
    CloseTelnet(@ARGV[1]);
  }
  else
  {
    Usage();
  }
}
  File Modified
Text File genkey.txt Nov 29, 2010 by mbyers
  • No labels