Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "utility.hpp" |
| 17 | |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 18 | #include "types.hpp" |
| 19 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 20 | #include <fstream> |
| 21 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 22 | namespace phosphor |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 23 | { |
| 24 | namespace power |
| 25 | { |
| 26 | namespace util |
| 27 | { |
| 28 | |
| 29 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 30 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 31 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 32 | |
| 33 | using namespace phosphor::logging; |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 34 | using json = nlohmann::json; |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 35 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 36 | std::string getService(const std::string& path, const std::string& interface, |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 37 | sdbusplus::bus::bus& bus) |
| 38 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 39 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 40 | MAPPER_INTERFACE, "GetObject"); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 41 | |
| 42 | method.append(path); |
| 43 | method.append(std::vector<std::string>({interface})); |
| 44 | |
| 45 | auto reply = bus.call(method); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 46 | |
| 47 | std::map<std::string, std::vector<std::string>> response; |
| 48 | reply.read(response); |
| 49 | |
| 50 | if (response.empty()) |
| 51 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 52 | log<level::ERR>("Error in mapper response for getting service name", |
| 53 | entry("PATH=%s", path.c_str()), |
| 54 | entry("INTERFACE=%s", interface.c_str())); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 55 | return std::string{}; |
| 56 | } |
| 57 | |
| 58 | return response.begin()->first; |
| 59 | } |
| 60 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 61 | json loadJSONFromFile(const char* path) |
| 62 | { |
| 63 | std::ifstream ifs(path); |
| 64 | if (!ifs.good()) |
| 65 | { |
| 66 | log<level::ERR>("Unable to open file", entry("PATH=%s", path)); |
| 67 | return nullptr; |
| 68 | } |
| 69 | auto data = json::parse(ifs, nullptr, false); |
| 70 | if (data.is_discarded()) |
| 71 | { |
| 72 | log<level::ERR>("Failed to parse json", entry("PATH=%s", path)); |
| 73 | return nullptr; |
| 74 | } |
| 75 | return data; |
| 76 | } |
| 77 | |
Lei YU | 4070546 | 2019-10-09 17:07:11 +0800 | [diff] [blame] | 78 | phosphor::pmbus::Type getPMBusAccessType(const json& json) |
| 79 | { |
| 80 | using namespace phosphor::pmbus; |
| 81 | Type type; |
| 82 | |
| 83 | auto typeStr = json.at("inventoryPMBusAccessType"); |
| 84 | |
| 85 | if (typeStr == "Hwmon") |
| 86 | { |
| 87 | type = Type::Hwmon; |
| 88 | } |
| 89 | else if (typeStr == "DeviceDebug") |
| 90 | { |
| 91 | type = Type::DeviceDebug; |
| 92 | } |
| 93 | else if (typeStr == "Debug") |
| 94 | { |
| 95 | type = Type::Debug; |
| 96 | } |
| 97 | else if (typeStr == "HwmonDeviceDebug") |
| 98 | { |
| 99 | type = Type::HwmonDeviceDebug; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | type = Type::Base; |
| 104 | } |
| 105 | return type; |
| 106 | } |
| 107 | |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 108 | bool isPoweredOn(sdbusplus::bus::bus& bus) |
| 109 | { |
| 110 | // When state = 1, system is powered on |
| 111 | int32_t state = 0; |
| 112 | |
| 113 | try |
| 114 | { |
| 115 | auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus); |
| 116 | getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus, |
| 117 | state); |
| 118 | } |
| 119 | catch (std::exception& e) |
| 120 | { |
| 121 | log<level::INFO>("Failed to get power state. Assuming it is off."); |
| 122 | } |
| 123 | return state != 0; |
| 124 | } |
| 125 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 126 | } // namespace util |
| 127 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 128 | } // namespace phosphor |