SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame^] | 1 | #include "config.h" |
| 2 | |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 3 | #include "reader_impl.hpp" |
| 4 | |
| 5 | #include "utils.hpp" |
| 6 | |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame^] | 7 | #include <algorithm> |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 8 | #include <com/ibm/VPD/error.hpp> |
| 9 | #include <map> |
| 10 | #include <phosphor-logging/elog-errors.hpp> |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame^] | 11 | #include <vector> |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Common/error.hpp> |
| 13 | |
| 14 | namespace openpower |
| 15 | { |
| 16 | namespace vpd |
| 17 | { |
| 18 | namespace manager |
| 19 | { |
| 20 | namespace reader |
| 21 | { |
| 22 | |
| 23 | using namespace phosphor::logging; |
| 24 | using namespace openpower::vpd::inventory; |
| 25 | using namespace openpower::vpd::constants; |
| 26 | |
| 27 | using InvalidArgument = |
| 28 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 29 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 30 | using LocationNotFound = sdbusplus::com::ibm::VPD::Error::LocationNotFound; |
| 31 | |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame^] | 32 | bool ReaderImpl::isValidLocationCode(const std::string& locationCode) const |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 33 | { |
| 34 | if ((locationCode.length() < UNEXP_LOCATION_CODE_MIN_LENGTH) || |
| 35 | ((locationCode.find("fcs", 1, 3) == std::string::npos) && |
| 36 | (locationCode.find("mts", 1, 3) == std::string::npos))) |
| 37 | { |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame^] | 38 | return false; |
| 39 | } |
| 40 | |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | std::string ReaderImpl::getExpandedLocationCode( |
| 45 | const std::string& locationCode, const uint16_t& nodeNumber, |
| 46 | const LocationCodeMap& frusLocationCode) const |
| 47 | { |
| 48 | if (!isValidLocationCode(locationCode)) |
| 49 | { |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 50 | // argument is not valid |
| 51 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LOCATIONCODE"), |
| 52 | Argument::ARGUMENT_VALUE(locationCode.c_str())); |
| 53 | } |
| 54 | |
| 55 | auto iterator = frusLocationCode.find(locationCode); |
| 56 | if (iterator == frusLocationCode.end()) |
| 57 | { |
| 58 | // TODO: Implementation of error logic till then throwing invalid |
| 59 | // argument |
| 60 | // the location code was not found in the system |
| 61 | // elog<LocationNotFound>(); |
| 62 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LOCATIONCODE"), |
| 63 | Argument::ARGUMENT_VALUE(locationCode.c_str())); |
| 64 | } |
| 65 | |
| 66 | std::string expandedLocationCode = |
| 67 | readBusProperty(iterator->second, LOCATION_CODE_INF, "LocationCode"); |
| 68 | return expandedLocationCode; |
| 69 | } |
| 70 | |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame^] | 71 | ListOfPaths |
| 72 | ReaderImpl::getFrusAtLocation(const std::string& locationCode, |
| 73 | const uint16_t& nodeNumber, |
| 74 | const LocationCodeMap& frusLocationCode) const |
| 75 | { |
| 76 | if (!isValidLocationCode(locationCode)) |
| 77 | { |
| 78 | // argument is not valid |
| 79 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LOCATIONCODE"), |
| 80 | Argument::ARGUMENT_VALUE(locationCode.c_str())); |
| 81 | } |
| 82 | |
| 83 | auto range = frusLocationCode.equal_range(locationCode); |
| 84 | |
| 85 | if (range.first == frusLocationCode.end()) |
| 86 | { |
| 87 | // TODO: Implementation of error logic till then throwing invalid |
| 88 | // argument |
| 89 | // the location code was not found in the system |
| 90 | // elog<LocationNotFound>(); |
| 91 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LOCATIONCODE"), |
| 92 | Argument::ARGUMENT_VALUE(locationCode.c_str())); |
| 93 | } |
| 94 | |
| 95 | ListOfPaths inventoryPaths; |
| 96 | |
| 97 | for_each(range.first, range.second, |
| 98 | [&inventoryPaths]( |
| 99 | const inventory::LocationCodeMap::value_type& mappedItem) { |
| 100 | inventoryPaths.push_back(INVENTORY_PATH + mappedItem.second); |
| 101 | }); |
| 102 | return inventoryPaths; |
| 103 | } |
| 104 | |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 105 | } // namespace reader |
| 106 | } // namespace manager |
| 107 | } // namespace vpd |
| 108 | } // namespace openpower |