Matt Spinler | 8df7be8 | 2017-01-09 15:42:05 -0600 | [diff] [blame] | 1 | package Util; |
| 2 | |
| 3 | #Holds common utility functions for MRW processing. |
| 4 | |
| 5 | use strict; |
| 6 | use warnings; |
| 7 | |
| 8 | use mrw::Targets; |
| 9 | |
| 10 | #Returns the BMC target for a system. |
| 11 | # param[in] $targetObj = The Targets object |
| 12 | sub getBMCTarget |
| 13 | { |
| 14 | my ($targetObj) = @_; |
| 15 | |
| 16 | for my $target (keys %{$targetObj->getAllTargets()}) { |
| 17 | if ($targetObj->getType($target) eq "BMC") { |
| 18 | return $target; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | die "Could not find BMC target in the MRW XML\n"; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | #Returns an array of child units based on their Target Type. |
| 27 | # param[in] $targetObj = The Targets object |
| 28 | # param[in] $unitTargetType = The target type of the units to find |
| 29 | # param[in] $chip = The chip target to find the units on |
| 30 | sub getChildUnitsWithTargetType |
| 31 | { |
| 32 | my ($targetObj, $unitTargetType, $chip) = @_; |
| 33 | my @units; |
| 34 | |
| 35 | my @children = $targetObj->getAllTargetChildren($chip); |
| 36 | |
| 37 | for my $child (@children) { |
| 38 | if ($targetObj->getTargetType($child) eq $unitTargetType) { |
| 39 | push @units, $child; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return @units; |
| 44 | } |
| 45 | |
Deepak Kodihalli | 6225e93 | 2017-02-07 12:14:55 -0600 | [diff] [blame] | 46 | # Returns OBMC name corresponding to a Target name |
| 47 | # param[in] \@inventory = reference to array of inventory items |
| 48 | # param[in] $targetName = A Target name |
| 49 | sub getObmcName |
| 50 | { |
| 51 | my ($inventory_ref, $targetName) = @_; |
| 52 | my @inventory = @{ $inventory_ref }; |
| 53 | |
| 54 | for my $item (@inventory) |
| 55 | { |
| 56 | if($item->{TARGET} eq $targetName) |
| 57 | { |
| 58 | return $item->{OBMC_NAME}; |
| 59 | } |
| 60 | } |
| 61 | return undef; |
| 62 | } |
| 63 | |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 64 | #Returns the array of all the device paths based on the type. |
| 65 | # param[in] \@inventory = reference to array of inventory items. |
| 66 | # param[in] $targetObj = The Targets object. |
| 67 | # param[in] $type = The target type. |
| 68 | sub getDevicePath |
| 69 | { |
| 70 | my ($inventory_ref, $targetObj, $type) = @_; |
| 71 | my @inventory = @{ $inventory_ref }; |
| 72 | my $fruType = ""; |
| 73 | my @devices; |
| 74 | for my $item (@inventory) |
| 75 | { |
| 76 | if (!$targetObj->isBadAttribute($item->{TARGET}, "TYPE")) { |
| 77 | $fruType = $targetObj->getAttribute($item->{TARGET}, "TYPE"); |
| 78 | if ($fruType eq $type) { |
| 79 | push(@devices,$item->{OBMC_NAME}); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | return @devices; |
| 84 | } |
| 85 | |
Deepak Kodihalli | c1b12a4 | 2017-02-22 04:00:54 -0600 | [diff] [blame] | 86 | #Convert the MRW I2C address into the standard 7-bit format |
| 87 | # $addr = the I2C Address |
| 88 | sub adjustI2CAddress |
| 89 | { |
| 90 | my $addr = shift; |
| 91 | |
| 92 | #MRW holds the 8 bit value. We need the 7 bit one. |
| 93 | $addr = $addr >> 1; |
| 94 | $addr = sprintf("0x%X", $addr); |
| 95 | $addr = lc $addr; |
| 96 | |
| 97 | return $addr; |
| 98 | } |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 99 | |
Deepak Kodihalli | 9283add | 2017-02-22 04:14:01 -0600 | [diff] [blame] | 100 | #Return nearest FRU enclosing the input target |
| 101 | # $targets = the Targets object |
| 102 | # $targetName = the target name |
| 103 | sub getEnclosingFru |
| 104 | { |
| 105 | my ($targets, $targetName) = @_; |
| 106 | |
| 107 | if (($targetName eq "") || (!defined $targetName)) |
| 108 | { |
| 109 | return ""; |
| 110 | } |
| 111 | |
| 112 | if (!$targets->isBadAttribute($targetName, "RU_TYPE")) |
| 113 | { |
| 114 | my $ruType = $targets->getAttribute($targetName, "RU_TYPE"); |
| 115 | if (($ruType eq "FRU") || ($ruType eq "CRU")) |
| 116 | { |
| 117 | return $targetName; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | my $parent = $targets->getTargetParent($targetName); |
| 122 | return getEnclosingFru($targets, $parent); |
| 123 | } |
| 124 | |
Deepak Kodihalli | 8f5dec5 | 2017-02-23 02:42:15 -0600 | [diff] [blame] | 125 | #Convert I2C port number from MRW scheme to Linux numbering scheme |
| 126 | # $port = the I2C port number |
| 127 | sub adjustI2CPort |
| 128 | { |
| 129 | my $port = shift; |
| 130 | |
| 131 | return $port - 1; |
| 132 | } |
| 133 | |
Matt Spinler | 8df7be8 | 2017-01-09 15:42:05 -0600 | [diff] [blame] | 134 | 1; |
| 135 | |
| 136 | =head1 NAME |
| 137 | |
| 138 | Util |
| 139 | |
| 140 | =head1 DESCRIPTION |
| 141 | |
| 142 | Contains utility functions for the MRW parsers. |
| 143 | |
| 144 | =head1 METHODS |
| 145 | |
| 146 | =over 4 |
| 147 | |
| 148 | =item getBMCTarget(C<TargetsObj>) |
| 149 | |
| 150 | Returns the target string for the BMC chip. If it can't find one, |
| 151 | it will die. Currently supports single BMC systems. |
| 152 | |
| 153 | =item getChildUnitsWithTargetType(C<TargetsObj>, C<TargetType>, C<ChipTarget>) |
| 154 | |
| 155 | Returns an array of targets that have target-type C<TargetType> |
| 156 | and are children (any level) of target C<ChipTarget>. |
| 157 | |
Deepak Kodihalli | 6225e93 | 2017-02-07 12:14:55 -0600 | [diff] [blame] | 158 | =item getObmcName(C<InventoryItems>, C<TargetName>) |
| 159 | |
| 160 | Returns an OBMC name corresponding to a Target name. Returns |
| 161 | undef if the Target name is not found. |
| 162 | |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 163 | =item getDevicePath(C<InventoryItems>, C<TargetsObj>, C<TargetType>) |
Deepak Kodihalli | 6474db8 | 2017-02-22 04:40:06 -0600 | [diff] [blame] | 164 | |
| 165 | Returns an array of all device paths (OBMC names) based on C<TargetType>. |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 166 | |
Deepak Kodihalli | c1b12a4 | 2017-02-22 04:00:54 -0600 | [diff] [blame] | 167 | =item adjustI2CAddress(C<I2CAddress>) |
| 168 | |
| 169 | Returns C<I2CAddress> converted from MRW format (8-bit) to the standard 7-bit |
| 170 | format. |
| 171 | |
Deepak Kodihalli | 9283add | 2017-02-22 04:14:01 -0600 | [diff] [blame] | 172 | =item getEnclosingFru(C<TargetsObj>, C<Target>) |
| 173 | |
| 174 | Finds the nearest FRU enclosing the input Target. |
| 175 | |
Deepak Kodihalli | 8f5dec5 | 2017-02-23 02:42:15 -0600 | [diff] [blame] | 176 | =item adjustI2CPort(C<I2CPort>) |
| 177 | |
| 178 | Returns C<I2CPort> converted from MRW numering scheme to Linux numbering scheme. |
| 179 | |
Matt Spinler | 8df7be8 | 2017-01-09 15:42:05 -0600 | [diff] [blame] | 180 | =back |
| 181 | |
| 182 | =cut |