summaryrefslogtreecommitdiffstats
path: root/Util.pm
blob: ea6b9145a18d261b722188c14023c48a79cfb8cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package Util;

#Holds common utility functions for MRW processing.

use strict;
use warnings;

use mrw::Targets;

#Returns the BMC target for a system.
# param[in] $targetObj = The Targets object
sub getBMCTarget
{
    my ($targetObj) = @_;

    for my $target (keys %{$targetObj->getAllTargets()}) {
        if ($targetObj->getType($target) eq "BMC") {
           return $target;
        }
    }

    die "Could not find BMC target in the MRW XML\n";
}


#Returns an array of child units based on their Target Type.
# param[in] $targetObj = The Targets object
# param[in] $unitTargetType = The target type of the units to find
# param[in] $chip = The chip target to find the units on
sub getChildUnitsWithTargetType
{
    my ($targetObj, $unitTargetType, $chip) = @_;
    my @units;

    my @children = $targetObj->getAllTargetChildren($chip);

    for my $child (@children) {
        if ($targetObj->getTargetType($child) eq $unitTargetType) {
            push @units, $child;
        }
    }

    return @units;
}

#Returns size of child units based on their Type.
# param[in] $targetObj = The Targets object
# param[in] $type = The type of the units to find
# param[in] $chip = The chip target to find the units on
sub getSizeOfChildUnitsWithType
{
    my ($targetObj, $type, $chip) = @_;
    my @units;
    my @children = $targetObj->getAllTargetChildren($chip);
    for my $child (@children) {
        if ($targetObj->getType($child) eq $type) {
            push @units, $child;
        }
    }
    my $size = @units;
    return $size;
}

# Returns OBMC name corresponding to a Target name
# param[in] \@inventory = reference to array of inventory items
# param[in] $targetName = A Target name
sub getObmcName
{
    my ($inventory_ref, $targetName) = @_;
    my @inventory = @{ $inventory_ref };

    for my $item (@inventory)
    {
        if($item->{TARGET} eq $targetName)
        {
            return $item->{OBMC_NAME};
        }
    }
    return undef;
}

#Returns the array of all the device paths based on the type.
# param[in] \@inventory = reference to array of inventory items.
# param[in] $targetObj = The Targets object.
# param[in] $type = The target type.
sub getDevicePath
{
    my ($inventory_ref, $targetObj, $type) = @_;
    my @inventory = @{ $inventory_ref };
    my $fruType = "";
    my @devices;
    for my $item (@inventory)
    {
        if (!$targetObj->isBadAttribute($item->{TARGET}, "TYPE")) {
            $fruType = $targetObj->getAttribute($item->{TARGET}, "TYPE");
            if ($fruType eq $type) {
                push(@devices,$item->{OBMC_NAME});
            }
        }
    }
    return @devices;
}

#Convert the MRW I2C address into the standard 7-bit format
# $addr = the I2C Address
sub adjustI2CAddress
{
    my $addr = shift;

    #MRW holds the 8 bit value.  We need the 7 bit one.
    $addr = $addr >> 1;
    $addr = sprintf("0x%X", $addr);
    $addr = lc $addr;

    return $addr;
}

#Return nearest FRU enclosing the input target
# $targets = the Targets object
# $targetName = the target name
sub getEnclosingFru
{
    my ($targets, $targetName) = @_;

    if (($targetName eq "") || (!defined $targetName))
    {
        return "";
    }

    if (!$targets->isBadAttribute($targetName, "RU_TYPE"))
    {
        my $ruType = $targets->getAttribute($targetName, "RU_TYPE");
        if (($ruType eq "FRU") || ($ruType eq "CRU"))
        {
            return $targetName;
        }
    }

    my $parent = $targets->getTargetParent($targetName);
    return getEnclosingFru($targets, $parent);
}

#Convert I2C port number from MRW scheme to Linux numbering scheme
# $port = the I2C port number
sub adjustI2CPort
{
    my $port = shift;

    return $port - 1;
}

1;

=head1 NAME

Util

=head1 DESCRIPTION

Contains utility functions for the MRW parsers.

=head1 METHODS

=over 4

=item getBMCTarget(C<TargetsObj>)

Returns the target string for the BMC chip.  If it can't find one,
it will die.  Currently supports single BMC systems.

=item getChildUnitsWithTargetType(C<TargetsObj>, C<TargetType>, C<ChipTarget>)

Returns an array of targets that have target-type C<TargetType>
and are children (any level) of target C<ChipTarget>.

=item getSizeOfChildUnitsWithType(C<TargetsObj>, C<Type>, C<ChipTarget>)

Returns size of targets that have Type C<Type>
and are children (any level) of target C<ChipTarget>.

=item getObmcName(C<InventoryItems>, C<TargetName>)

Returns an OBMC name corresponding to a Target name. Returns
undef if the Target name is not found.

=item getDevicePath(C<InventoryItems>, C<TargetsObj>, C<TargetType>)

Returns an array of all device paths (OBMC names)  based on C<TargetType>.

=item adjustI2CAddress(C<I2CAddress>)

Returns C<I2CAddress> converted from MRW format (8-bit) to the standard 7-bit
format.

=item getEnclosingFru(C<TargetsObj>, C<Target>)

Finds the nearest FRU enclosing the input Target.

=item adjustI2CPort(C<I2CPort>)

Returns C<I2CPort> converted from MRW numbering scheme to Linux numbering scheme.

=back

=cut
OpenPOWER on IntegriCloud