<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#! /usr/bin/perl
# $Id: TestTarget_LVRT.pm 94447 2011-09-08 08:15:58Z johnnyw $
#
# TestTarget_LVRT - how to manage the test environment on a LabVIEW RT target.
#
# We can FTP files to and from the LabVIEW target, but there's no NFS or
# SMB shares.
# Most information about the target itself is specified via environment
# variables. Environment variables with settings are named using the target's
# config name with a specific suffix. The current environment variables are:
#   &lt;config-name&gt;_IPNAME - the host name/IP of the target.
#   &lt;config-name&gt;_CTLPORT- the TCP port number to connect to for the test
#                          controller. If this is not set, port 8888 is used.
#   &lt;config-name&gt;_FSROOT - the root of the filesystem on the target where
#                          ACE files will be created from (cwd, if you will).
#                          If this is not set, "\ni-rt" is used as the root.
#
# Each of these settings are stored in a member variable of the same name in
# each object. The process objects can access them using, e.g.,
# $self-&gt;{TARGET}-&gt;{IPNAME}.
#
# This class also makes an FTP object available to process objects that are
# created. FTP is set up before creating a process object and can be used to
# transfer files to and from the LVRT target.

package PerlACE::TestTarget_LVRT;
our @ISA = "PerlACE::TestTarget";

### Constructor and Destructor

sub new
{
    my $proto = shift;
    my $config_name = shift;
    my $class = ref ($proto) || $proto;
    my $self = {};
    bless ($self, $class);
    $self-&gt;GetConfigSettings($config_name);
    my $targethost;
    my $env_name = $config_name.'_IPNAME';
    if (exists $ENV{$env_name}) {
        $targethost = $ENV{$env_name};
    }
    else {
        print STDERR "You must define target hostname/IP with $env_name\n";
        undef $self;
        return undef;
    }

    $env_name = $config_name.'_CTLPORT';
    if (exists $ENV{$env_name}) {
        $self-&gt;{CTLPORT} = $ENV{$env_name};
    }
    else {
        print STDERR "Warning: no $env_name variable; falling back to ",
                     "port 8888\n";
        $self-&gt;{CTLPORT} = 8888;
    }

    $env_name = $config_name.'_FSROOT';
    my $fsroot = '\\ni-rt\\system';
    if (exists $ENV{$env_name}) {
        $fsroot = $ENV{$env_name};
    }
    else {
        print STDERR "Warning: no $env_name variable; falling back ",
                     "to $fsroot\n";
    }
    $self-&gt;{FSROOT} = $fsroot;

    $self-&gt;{REBOOT_CMD} = $ENV{'ACE_REBOOT_LVRT_CMD'};
    if (!defined $self-&gt;{REBOOT_CMD}) {
        $self-&gt;{REBOOT_CMD} = 'I_Need_A_Reboot_Command';
    }
    $self-&gt;{REBOOT_TIME} = $ENV{'ACE_LVRT_REBOOT_TIME'};
    if (!defined $self-&gt;{REBOOT_TIME}) {
        $self-&gt;{REBOOT_TIME} = 200;
    }

    $self-&gt;{REBOOT_TIME} = $ENV{'ACE_RUN_LVRT_REBOOT_TIME'};
    if (!defined $self-&gt;{REBOOT_TIME}) {
        $self-&gt;{REBOOT_TIME} = 200;
    }
    $self-&gt;{REBOOT_NEEDED} = undef;

    $self-&gt;{FTP} = new Net::FTP ($targethost);
    $self-&gt;{IPNAME} = $targethost;
    if (!defined $self-&gt;{FTP}) {
        print STDERR "Error opening FTP to $targethost: $@\n";
        $self-&gt;{REBOOT_NEEDED} = 1;
        undef $self;
        return undef;
    }
    $self-&gt;{FTP}-&gt;login("","");

    return $self;
}

sub DESTROY
{
    my $self = shift;

    # Reboot if needed; set up clean for the next test.
    if (defined $self-&gt;{REBOOT_NEEDED} &amp;&amp; $self-&gt;{REBOOT_CMD}) {
        $self-&gt;RebootNow;
    }

    # See if there's a log; should be able to retrieve it from rebooted target.
    if (defined $ENV{'ACE_TEST_VERBOSE'}) {
      print STDERR "LVRT target checking for remaining log...\n";
    }
    $self-&gt;GetStderrLog();
    if (defined $self-&gt;{FTP}) {
        $self-&gt;{FTP}-&gt;close;
        $self-&gt;{FTP} = undef;
    }
}

##################################################################

sub LocalFile ($)
{
    my $self = shift;
    my $file = shift;
    my $newfile = $self-&gt;{FSROOT} . '\\' . $file;
    print STDERR "LVRT LocalFile for $file is $newfile\n";
    return $newfile;
}

sub DeleteFile ($)
{
    my $self = shift;
    $self-&gt;{FTP}-&gt;login("","");
    foreach my $file (@_) {
      my $newfile = $self-&gt;LocalFile($file);
      $self-&gt;{FTP}-&gt;delete($newfile);
    }
}

sub GetFile ($)
{
    # Use FTP to retrieve the file from the target; should still be open.
    # If only one name is given, use it for both local and remote (after
    # properly LocalFile-ing it). If both names are given, assume the caller
    # knows what he wants and don't adjust the paths.
    my $self = shift;
    my $remote_file = shift;
    my $local_file = shift;
    if (!defined $local_file) {
        $local_file = $remote_file;
        $remote_file = $self-&gt;LocalFile($local_file);
    }
    $self-&gt;{FTP}-&gt;ascii();
    if ($self-&gt;{FTP}-&gt;get($remote_file, $local_file)) {
        return 0;
    }
    return -1;
}

sub WaitForFileTimed ($)
{
    my $self = shift;
    my $file = shift;
    my $timeout = shift;
    my $newfile = $self-&gt;LocalFile($file);
    my $targetport = $self-&gt;{CTLPORT};
    my $target = new Net::Telnet(Errmode =&gt; 'return');
    if (!$target-&gt;open(Host =&gt; $self-&gt;{IPNAME}, Port =&gt; $targetport)) {
        print STDERR "ERROR: target $self-&gt;{IPNAME}:$targetport: ",
                      $target-&gt;errmsg(), "\n";
        return -1;
    }
    my $cmdline = "waitforfile $newfile $timeout";
    if (defined $ENV{'ACE_TEST_VERBOSE'}) {
      print "-&gt; $cmdline\n";
    }
    $target-&gt;print("$cmdline");
    my $reply;
    # Add a small comms delay factor to the timeout
    $timeout = $timeout + 2;
    $reply = $target-&gt;getline(Timeout =&gt; $timeout);
    if (defined $ENV{'ACE_TEST_VERBOSE'}) {
      print "&lt;- $reply\n";
    }
    $target-&gt;close();
    if ($reply eq "OK\n") {
        return 0;
    }
    return -1;
}

sub CreateProcess ($)
{
    my $self = shift;
    my $process = new PerlACE::ProcessLVRT ($self, @_);
    return $process;
}

sub GetStderrLog ($)
{
    my $self = shift;
    # Tell the target to snapshot the stderr log; if there is one, copy
    # it up here and put it out to our stderr.
    my $targetport = $self-&gt;{CTLPORT};
    my $target = new Net::Telnet(Errmode =&gt; 'return');
    if (!$target-&gt;open(Host =&gt; $self-&gt;{IPNAME}, Port =&gt; $targetport)) {
        print STDERR "ERROR: target $self-&gt;{IPNAME}:$targetport: ",
                      $target-&gt;errmsg(), "\n";
        return;
    }
    my $cmdline = "snaplog";
    if (defined $ENV{'ACE_TEST_VERBOSE'}) {
        print "-&gt; $cmdline\n";
    }
    $target-&gt;print("$cmdline");
    my $reply;
    $reply = $target-&gt;getline();
    if (defined $ENV{'ACE_TEST_VERBOSE'}) {
        print "&lt;- $reply\n";
    }
    $target-&gt;close();
    if ($reply eq "NONE\n") {
        return;
    }
    chomp $reply;
    if (undef $self-&gt;{FTP}) {
        $self-&gt;{FTP} = new Net::FTP ($self-&gt;{IPNAME});
        if (!defined $self-&gt;{FTP}) {
            print STDERR "$@\n";
            return -1;
        }
        $self-&gt;{FTP}-&gt;login("","");
    }
    $self-&gt;{FTP}-&gt;ascii();
    if ($self-&gt;{FTP}-&gt;get($reply, "stderr.txt")) {
        $self-&gt;{FTP}-&gt;delete($reply);
        open(LOG, "stderr.txt");
        while (&lt;LOG&gt;) {
            print STDERR;
        }
        close LOG;
        unlink "stderr.txt";
    }
    return;
}

# Copy a file to the target. Adjust for different types (DLL, EXE, TEXT)
# and debug/non (for DLLs). Additionally, a file can be removed when this
# object is deleted, or left in place.
sub NeedFile ($)
{
    my $self = shift;
}

# Need a reboot when this target is destroyed.
sub NeedReboot ($)
{
    my $self = shift;
    $self-&gt;{REBOOT_NEEDED} = 1;
}

# Reboot target
sub RebootNow ($)
{
    my $self = shift;
    $self-&gt;{REBOOT_NEEDED} = undef;
    print STDERR "Attempting to reboot target...\n";
    if (defined $self-&gt;{FTP}) {
        $self-&gt;{FTP}-&gt;close;
        $self-&gt;{FTP} = undef;
    }
    system ($self-&gt;{REBOOT_CMD});
    sleep ($self-&gt;{REBOOT_TIME});
}

# Reboot now then try to restore the FTP connection.
sub RebootReset ($)
{
    my $self = shift;
    $self-&gt;RebootNow;
    my $targethost = $self-&gt;{IPNAME};
    $self-&gt;{FTP} = new Net::FTP ($targethost);
    if (!defined $self-&gt;{FTP}) {
        print STDERR "Error reestablishing FTP to $targethost: $@\n";
    }
    else {
        $self-&gt;{FTP}-&gt;login("","");
    }
}

sub KillAll ($)
{
    my $self = shift;
    my $procmask = shift;
    PerlACE::ProcessLVRT::kill_all ($procmask, $self);
}

1;
</pre></body></html>