1#! /usr/bin/perl
2use strict;
3use warnings;
4
5use mrw::Targets;
6use mrw::Inventory;
7use mrw::Util;
8use Getopt::Long; # For parsing command line arguments
9use YAML::Tiny qw(LoadFile);
10
11# Globals
12my $serverwizFile  = "";
13my $debug          = 0;
14my $outputFile     = "";
15my $metaDataFile   = "";
16my $skipBrokenMrw  = 0;
17
18# Command line argument parsing
19GetOptions(
20"i=s" => \$serverwizFile,    # string
21"m=s" => \$metaDataFile,     # string
22"o=s" => \$outputFile,       # string
23"skip-broken-mrw" => \$skipBrokenMrw,
24"d"   => \$debug,
25)
26or printUsage();
27
28if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
29{
30    printUsage();
31}
32
33my $targetObj = Targets->new;
34$targetObj->loadXML($serverwizFile);
35
36# Open the MRW xml and the Metadata file for the sensor.
37# Get the IPMI sensor information based on the Entity ID and Sensor Type.
38# Fetch the Sensor ID, Event/Reading Type and Object Path from MRW.
39# Get the Sensor Type and Offset from the metadata file.
40# Merge and generate an output YAML with inventory object path as the key.
41
42open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
43my $metaDataConfig = LoadFile($metaDataFile);
44
45my @interestedTypes = keys %{$metaDataConfig};
46my %types;
47
48@types{@interestedTypes} = ();
49
50my @inventory = Inventory::getInventory($targetObj);
51#Process all the targets in the XML
52foreach my $target (sort keys %{$targetObj->getAllTargets()})
53{
54    my $sensorID = '';
55    my $sensorType = '';
56    my $eventReadingType = '';
57    my $path = '';
58    my $obmcPath = '';
59    my $entityID = '';
60    my $base = "/xyz/openbmc_project/inventory";
61
62    if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
63
64        $sensorID = getNumeric($targetObj, $target, "IPMI_SENSOR_ID");
65        $sensorType = getNumeric($targetObj, $target, "IPMI_SENSOR_TYPE");
66        $eventReadingType = getNumeric(
67            $targetObj, $target, "IPMI_SENSOR_READING_TYPE");
68        $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
69        $entityID = getNumeric($targetObj, $target, "IPMI_ENTITY_ID");
70
71        # Look only for the interested Entity ID & Sensor Type
72        next if (not exists $types{$entityID});
73        next if ($sensorType ne $metaDataConfig->{$entityID}->{SensorType});
74
75        #if there is ipmi sensor without sensorid or sensorReadingType or
76        #Instance path then die
77
78        if ($sensorID eq '' or $eventReadingType eq '' or $path eq '') {
79            next if $skipBrokenMrw;
80            close $fh;
81            die("sensor without info for target=$target");
82        }
83
84        # Removing the string "instance:" from path
85        $path =~ s/^instance:/\//;
86        $obmcPath = Util::getObmcName(\@inventory, $path);
87
88        # If unable to get the obmc path then die
89        if (not defined $obmcPath) {
90            close $fh;
91            die("Unable to get the obmc path for path=$path");
92        }
93
94        $base .= $obmcPath;
95
96        print $fh $base.":"."\n";
97        print $fh "  sensorID: ".$sensorID."\n";
98        print $fh "  sensorType: ".$sensorType."\n";
99        print $fh "  eventReadingType: ".$eventReadingType."\n";
100        print $fh "  offset: ".$metaDataConfig->{$entityID}->{Offset}."\n";
101
102        printDebug("$sensorID : $sensorType : $eventReadingType : $entityID : $metaDataConfig->{$entityID}->{Offset}")
103    }
104}
105close $fh;
106
107sub getNumeric
108{
109    my $obj = shift;
110    my $target = shift;
111    my $attr = shift;
112    my $val = $obj->getAttribute($target, $attr);
113    $val = oct($val) if $val =~ /^0/;
114    return $val;
115}
116
117# Usage
118sub printUsage
119{
120    print "
121    $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
122Options:
123    -d = debug mode
124    --skip-broken-mrw = Skip broken MRW targets
125        \n";
126    exit(1);
127}
128
129# Helper function to put debug statements.
130sub printDebug
131{
132    my $str = shift;
133    print "DEBUG: ", $str, "\n" if $debug;
134}
135