blob: a40aa44a91515656838879983a569a23dc5dc995 [file] [log] [blame]
SunnySrivastava19841356d7e2020-04-24 04:29:35 -05001#include "config.h"
2
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -05003#include "reader_impl.hpp"
4
5#include "utils.hpp"
6
SunnySrivastava19841356d7e2020-04-24 04:29:35 -05007#include <algorithm>
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -05008#include <com/ibm/VPD/error.hpp>
9#include <map>
10#include <phosphor-logging/elog-errors.hpp>
SunnySrivastava19841356d7e2020-04-24 04:29:35 -050011#include <vector>
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -050012#include <xyz/openbmc_project/Common/error.hpp>
13
14namespace openpower
15{
16namespace vpd
17{
18namespace manager
19{
20namespace reader
21{
22
23using namespace phosphor::logging;
24using namespace openpower::vpd::inventory;
25using namespace openpower::vpd::constants;
26
27using InvalidArgument =
28 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
29using Argument = xyz::openbmc_project::Common::InvalidArgument;
30using LocationNotFound = sdbusplus::com::ibm::VPD::Error::LocationNotFound;
31
SunnySrivastava19841356d7e2020-04-24 04:29:35 -050032bool ReaderImpl::isValidLocationCode(const std::string& locationCode) const
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -050033{
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 {
SunnySrivastava19841356d7e2020-04-24 04:29:35 -050038 return false;
39 }
40
41 return true;
42}
43
44std::string ReaderImpl::getExpandedLocationCode(
45 const std::string& locationCode, const uint16_t& nodeNumber,
46 const LocationCodeMap& frusLocationCode) const
47{
48 if (!isValidLocationCode(locationCode))
49 {
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -050050 // 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
SunnySrivastava19841356d7e2020-04-24 04:29:35 -050071ListOfPaths
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
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500105} // namespace reader
106} // namespace manager
107} // namespace vpd
108} // namespace openpower