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