| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 1 | #include "utils.hpp" | 
 | 2 |  | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 4 | #include <sdbusplus/bus.hpp> | 
 | 5 | #include <string> | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 6 | #include <xyz/openbmc_project/Common/error.hpp> | 
 | 7 | namespace open_power | 
 | 8 | { | 
 | 9 | namespace occ | 
 | 10 | { | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 11 | namespace utils | 
 | 12 | { | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 13 | // For throwing exceptions | 
 | 14 | using namespace phosphor::logging; | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 15 | using InternalFailure = | 
 | 16 |     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 17 |  | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 18 | const std::string getService(const std::string& path, | 
 | 19 |                              const std::string& interface) | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 20 | { | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 21 |  | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 22 |     using InterfaceList = std::vector<std::string>; | 
 | 23 |     std::map<std::string, std::vector<std::string>> mapperResponse; | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 24 |  | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 25 |     auto& bus = getBus(); | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 26 |  | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 27 |     auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, | 
 | 28 |                                       MAPPER_IFACE, "GetObject"); | 
 | 29 |     mapper.append(path, InterfaceList({interface})); | 
 | 30 |  | 
 | 31 |     auto mapperResponseMsg = bus.call(mapper); | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 32 |     if (mapperResponseMsg.is_method_error()) | 
 | 33 |     { | 
 | 34 |         log<level::ERR>("ERROR in getting service", | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 35 |                         entry("PATH=%s", path.c_str()), | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 36 |                         entry("INTERFACE=%s", interface.c_str())); | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 37 |  | 
 | 38 |         elog<InternalFailure>(); | 
 | 39 |     } | 
 | 40 |  | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 41 |     mapperResponseMsg.read(mapperResponse); | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 42 |     if (mapperResponse.empty()) | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 43 |     { | 
 | 44 |         log<level::ERR>("ERROR reading mapper response", | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 45 |                         entry("PATH=%s", path.c_str()), | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 46 |                         entry("INTERFACE=%s", interface.c_str())); | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 47 |  | 
 | 48 |         elog<InternalFailure>(); | 
 | 49 |     } | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 50 |  | 
 | 51 |     // the value here will be the service name | 
 | 52 |     return mapperResponse.cbegin()->first; | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 53 | } | 
 | 54 |  | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 55 | const PropertyValue getProperty(const std::string& objectPath, | 
 | 56 |                                 const std::string& interface, | 
 | 57 |                                 const std::string& propertyName) | 
 | 58 | { | 
 | 59 |     PropertyValue value{}; | 
 | 60 |  | 
 | 61 |     auto& bus = getBus(); | 
 | 62 |     auto service = getService(objectPath, interface); | 
 | 63 |     if (service.empty()) | 
 | 64 |     { | 
 | 65 |         return value; | 
 | 66 |     } | 
 | 67 |  | 
 | 68 |     auto method = bus.new_method_call(service.c_str(), objectPath.c_str(), | 
 | 69 |                                       DBUS_PROPERTY_IFACE, "Get"); | 
 | 70 |     method.append(interface, propertyName); | 
 | 71 |  | 
 | 72 |     auto reply = bus.call(method); | 
 | 73 |     reply.read(value); | 
 | 74 |  | 
 | 75 |     return value; | 
 | 76 | } | 
 | 77 |  | 
| George Liu | 1390159 | 2021-06-03 14:13:21 +0800 | [diff] [blame^] | 78 | std::optional<LABELVALUE> checkLabelValue(const std::string& value) | 
 | 79 | { | 
 | 80 |     // The ID 0xD0000002 is only 4 bytes long, but when converted to a string it | 
 | 81 |     // is 8 characters long | 
 | 82 |     // eg: Dimm2, the lable file is `D0000002` so value | 
 | 83 |     // length = 2 byte(type) + 2 byte(reserve) + 4 byte(instace ID) | 
 | 84 |     size_t valueLen = value.length(); | 
 | 85 |     size_t typeLen = 2; | 
 | 86 |     size_t reserveLen = 2; | 
 | 87 |     size_t instanceIDLen = 4; | 
 | 88 |     if (valueLen != typeLen + reserveLen + instanceIDLen) | 
 | 89 |     { | 
 | 90 |         return std::nullopt; | 
 | 91 |     } | 
 | 92 |  | 
 | 93 |     size_t offset = 0; | 
 | 94 |     std::string type = value.substr(offset, typeLen); | 
 | 95 |     offset += typeLen; | 
 | 96 |     std::string reserve = value.substr(offset, reserveLen); | 
 | 97 |     offset += reserveLen; | 
 | 98 |  | 
 | 99 |     if ("00" != reserve) | 
 | 100 |     { | 
 | 101 |         return std::nullopt; | 
 | 102 |     } | 
 | 103 |  | 
 | 104 |     const char* start = value.data() + offset; | 
 | 105 |     uint16_t instanceID = static_cast<uint16_t>(std::strtol(start, NULL, 16)); | 
 | 106 |  | 
 | 107 |     LABELVALUE labelValue{type, instanceID}; | 
 | 108 |  | 
 | 109 |     return std::make_optional(std::move(labelValue)); | 
 | 110 | } | 
 | 111 |  | 
| George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 112 | } // namespace utils | 
| Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 113 | } // namespace occ | 
 | 114 | } // namespace open_power |