158825916STom Joseph#! /usr/bin/perl
258825916STom Josephuse strict;
358825916STom Josephuse warnings;
458825916STom Joseph
558825916STom Josephuse mrw::Targets;
658825916STom Josephuse mrw::Inventory;
758825916STom Josephuse mrw::Util;
858825916STom Josephuse Getopt::Long; # For parsing command line arguments
958825916STom Josephuse YAML::Tiny qw(LoadFile);
1058825916STom Joseph
1158825916STom Joseph# Globals
1258825916STom Josephmy $serverwizFile  = "";
1358825916STom Josephmy $debug          = 0;
1458825916STom Josephmy $outputFile     = "";
1558825916STom Josephmy $metaDataFile   = "";
1658825916STom Joseph
1758825916STom Joseph# Command line argument parsing
1858825916STom JosephGetOptions(
1958825916STom Joseph"i=s" => \$serverwizFile,    # string
2058825916STom Joseph"m=s" => \$metaDataFile,     # string
2158825916STom Joseph"o=s" => \$outputFile,       # string
2258825916STom Joseph"d"   => \$debug,
2358825916STom Joseph)
2458825916STom Josephor printUsage();
2558825916STom Joseph
2658825916STom Josephif (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
2758825916STom Joseph{
2858825916STom Joseph    printUsage();
2958825916STom Joseph}
3058825916STom Joseph
3158825916STom Josephmy $targetObj = Targets->new;
3258825916STom Joseph$targetObj->loadXML($serverwizFile);
3358825916STom Joseph
3458825916STom Joseph# Open the MRW xml and the Metadata file for the sensor.
3558825916STom Joseph# Get the IPMI sensor information based on the Entity ID and Sensor Type.
3658825916STom Joseph# Fetch the Sensor ID, Event/Reading Type and Object Path from MRW.
3758825916STom Joseph# Get the Sensor Type and Offset from the metadata file.
3858825916STom Joseph# Merge and generate an output YAML with inventory object path as the key.
3958825916STom Joseph
4058825916STom Josephopen(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
4158825916STom Josephmy $metaDataConfig = LoadFile($metaDataFile);
4258825916STom Joseph
4358825916STom Josephmy @interestedTypes = keys %{$metaDataConfig};
4458825916STom Josephmy %types;
4558825916STom Joseph
4658825916STom Joseph@types{@interestedTypes} = ();
4758825916STom Joseph
4858825916STom Josephmy @inventory = Inventory::getInventory($targetObj);
4958825916STom Joseph#Process all the targets in the XML
5058825916STom Josephforeach my $target (sort keys %{$targetObj->getAllTargets()})
5158825916STom Joseph{
5258825916STom Joseph    my $sensorID = '';
5358825916STom Joseph    my $sensorType = '';
5458825916STom Joseph    my $eventReadingType = '';
5558825916STom Joseph    my $path = '';
5658825916STom Joseph    my $obmcPath = '';
5758825916STom Joseph    my $entityID = '';
5858825916STom Joseph    my $base = "/xyz/openbmc_project/inventory";
5958825916STom Joseph
6058825916STom Joseph    if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
6158825916STom Joseph
62*5102e57bSBrad Bishop        $sensorID = getNumeric($targetObj, $target, "IPMI_SENSOR_ID");
63*5102e57bSBrad Bishop        $sensorType = getNumeric($targetObj, $target, "IPMI_SENSOR_TYPE");
64*5102e57bSBrad Bishop        $eventReadingType = getNumeric(
65*5102e57bSBrad Bishop            $targetObj, $target, "IPMI_SENSOR_READING_TYPE");
6658825916STom Joseph        $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
67*5102e57bSBrad Bishop        $entityID = getNumeric($targetObj, $target, "IPMI_ENTITY_ID");
6858825916STom Joseph
6958825916STom Joseph        # Look only for the interested Entity ID & Sensor Type
7058825916STom Joseph        next if (not exists $types{$entityID});
7158825916STom Joseph        next if ($sensorType ne $metaDataConfig->{$entityID}->{SensorType});
7258825916STom Joseph
7358825916STom Joseph        #if there is ipmi sensor without sensorid or sensorReadingType or
7458825916STom Joseph        #Instance path then die
7558825916STom Joseph
7658825916STom Joseph        if ($sensorID eq '' or $eventReadingType eq '' or $path eq '') {
7758825916STom Joseph            close $fh;
7858825916STom Joseph            die("sensor without info for target=$target");
7958825916STom Joseph        }
8058825916STom Joseph
8158825916STom Joseph        # Removing the string "instance:" from path
8258825916STom Joseph        $path =~ s/^instance:/\//;
8358825916STom Joseph        $obmcPath = Util::getObmcName(\@inventory, $path);
8458825916STom Joseph
8558825916STom Joseph        # If unable to get the obmc path then die
8658825916STom Joseph        if (not defined $obmcPath) {
8758825916STom Joseph            close $fh;
8858825916STom Joseph            die("Unable to get the obmc path for path=$path");
8958825916STom Joseph        }
9058825916STom Joseph
9158825916STom Joseph        $base .= $obmcPath;
9258825916STom Joseph
9358825916STom Joseph        print $fh $base.":"."\n";
9458825916STom Joseph        print $fh "  sensorID: ".$sensorID."\n";
9558825916STom Joseph        print $fh "  sensorType: ".$sensorType."\n";
9658825916STom Joseph        print $fh "  eventReadingType: ".$eventReadingType."\n";
9758825916STom Joseph        print $fh "  offset: ".$metaDataConfig->{$entityID}->{Offset}."\n";
9858825916STom Joseph
9958825916STom Joseph        printDebug("$sensorID : $sensorType : $eventReadingType : $entityID : $metaDataConfig->{$entityID}->{Offset}")
10058825916STom Joseph    }
10158825916STom Joseph}
10258825916STom Josephclose $fh;
10358825916STom Joseph
104*5102e57bSBrad Bishopsub getNumeric
105*5102e57bSBrad Bishop{
106*5102e57bSBrad Bishop    my $obj = shift;
107*5102e57bSBrad Bishop    my $target = shift;
108*5102e57bSBrad Bishop    my $attr = shift;
109*5102e57bSBrad Bishop    my $val = $obj->getAttribute($target, $attr);
110*5102e57bSBrad Bishop    $val = oct($val) if $val =~ /^0/;
111*5102e57bSBrad Bishop    return $val;
112*5102e57bSBrad Bishop}
113*5102e57bSBrad Bishop
11458825916STom Joseph# Usage
11558825916STom Josephsub printUsage
11658825916STom Joseph{
11758825916STom Joseph    print "
11858825916STom Joseph    $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
11958825916STom JosephOptions:
12058825916STom Joseph    -d = debug mode
12158825916STom Joseph        \n";
12258825916STom Joseph    exit(1);
12358825916STom Joseph}
12458825916STom Joseph
12558825916STom Joseph# Helper function to put debug statements.
12658825916STom Josephsub printDebug
12758825916STom Joseph{
12858825916STom Joseph    my $str = shift;
12958825916STom Joseph    print "DEBUG: ", $str, "\n" if $debug;
13058825916STom Joseph}
131