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