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 | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 18 | #include <fstream> |
| 19 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame^] | 20 | namespace phosphor |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 21 | { |
| 22 | namespace power |
| 23 | { |
| 24 | namespace util |
| 25 | { |
| 26 | |
| 27 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 28 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 29 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 30 | |
| 31 | using namespace phosphor::logging; |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 32 | using json = nlohmann::json; |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 33 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 34 | std::string getService(const std::string& path, const std::string& interface, |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 35 | sdbusplus::bus::bus& bus) |
| 36 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 37 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 38 | MAPPER_INTERFACE, "GetObject"); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 39 | |
| 40 | method.append(path); |
| 41 | method.append(std::vector<std::string>({interface})); |
| 42 | |
| 43 | auto reply = bus.call(method); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 44 | |
| 45 | std::map<std::string, std::vector<std::string>> response; |
| 46 | reply.read(response); |
| 47 | |
| 48 | if (response.empty()) |
| 49 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 50 | log<level::ERR>("Error in mapper response for getting service name", |
| 51 | entry("PATH=%s", path.c_str()), |
| 52 | entry("INTERFACE=%s", interface.c_str())); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 53 | return std::string{}; |
| 54 | } |
| 55 | |
| 56 | return response.begin()->first; |
| 57 | } |
| 58 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 59 | json loadJSONFromFile(const char* path) |
| 60 | { |
| 61 | std::ifstream ifs(path); |
| 62 | if (!ifs.good()) |
| 63 | { |
| 64 | log<level::ERR>("Unable to open file", entry("PATH=%s", path)); |
| 65 | return nullptr; |
| 66 | } |
| 67 | auto data = json::parse(ifs, nullptr, false); |
| 68 | if (data.is_discarded()) |
| 69 | { |
| 70 | log<level::ERR>("Failed to parse json", entry("PATH=%s", path)); |
| 71 | return nullptr; |
| 72 | } |
| 73 | return data; |
| 74 | } |
| 75 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 76 | } // namespace util |
| 77 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame^] | 78 | } // namespace phosphor |