Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -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 | */ |
Matthew Barth | ccaf2db | 2020-06-04 13:09:35 -0500 | [diff] [blame] | 16 | |
| 17 | #include "config.h" |
| 18 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 19 | #include "argument.hpp" |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 20 | #include "fan.hpp" |
Matthew Barth | ccaf2db | 2020-06-04 13:09:35 -0500 | [diff] [blame] | 21 | #ifdef MONITOR_USE_JSON |
| 22 | #include "json_parser.hpp" |
| 23 | #endif |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 24 | #include "fan_defs.hpp" |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 25 | #include "trust_manager.hpp" |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 26 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 27 | #include <phosphor-logging/log.hpp> |
| 28 | #include <sdbusplus/bus.hpp> |
| 29 | #include <sdeventplus/event.hpp> |
| 30 | |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 31 | using namespace phosphor::fan::monitor; |
| 32 | using namespace phosphor::logging; |
| 33 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 34 | int main(int argc, char* argv[]) |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 35 | { |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 36 | auto event = sdeventplus::Event::get_default(); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 37 | auto bus = sdbusplus::bus::new_default(); |
Matt Spinler | 8420111 | 2017-05-12 11:31:53 -0500 | [diff] [blame] | 38 | std::vector<std::unique_ptr<Fan>> fans; |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 39 | phosphor::fan::util::ArgumentParser args(argc, argv); |
| 40 | |
| 41 | if (argc != 2) |
| 42 | { |
| 43 | args.usage(argv); |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 44 | return 1; |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | Mode mode; |
| 48 | if (args["init"] == "true") |
| 49 | { |
| 50 | mode = Mode::init; |
| 51 | } |
| 52 | else if (args["monitor"] == "true") |
| 53 | { |
| 54 | mode = Mode::monitor; |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | args.usage(argv); |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 59 | return 1; |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 60 | } |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 61 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 62 | // Attach the event object to the bus object so we can |
| 63 | // handle both sd_events (for the timers) and dbus signals. |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 64 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 65 | |
Matthew Barth | ccaf2db | 2020-06-04 13:09:35 -0500 | [diff] [blame] | 66 | #ifdef MONITOR_USE_JSON |
| 67 | // Get JSON object from monitor JSON config file |
| 68 | const auto& jsonObj = getJsonObj(bus); |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 69 | |
| 70 | // Retrieve and set trust groups within the trust manager |
| 71 | auto trust = |
| 72 | std::make_unique<phosphor::fan::trust::Manager>(getTrustGrps(jsonObj)); |
Matthew Barth | 3ad1434 | 2020-06-08 16:17:42 -0500 | [diff] [blame] | 73 | |
| 74 | // Retrieve fan definitions and create fan objects to be monitored |
| 75 | for (const auto& fanDef : getFanDefs(jsonObj)) |
| 76 | { |
| 77 | // Check if a condition exists on the fan |
| 78 | auto condition = std::get<conditionField>(fanDef); |
| 79 | if (condition) |
| 80 | { |
| 81 | // Condition exists, skip adding fan if it fails |
| 82 | if (!(*condition)(bus)) |
| 83 | { |
| 84 | continue; |
| 85 | } |
| 86 | } |
| 87 | fans.emplace_back( |
| 88 | std::make_unique<Fan>(mode, bus, event, trust, fanDef)); |
| 89 | } |
Matthew Barth | ccaf2db | 2020-06-04 13:09:35 -0500 | [diff] [blame] | 90 | #else |
| 91 | auto trust = std::make_unique<phosphor::fan::trust::Manager>(trustGroups); |
| 92 | |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 93 | for (const auto& fanDef : fanDefinitions) |
| 94 | { |
Matthew Barth | 81748b1 | 2018-05-02 16:03:48 -0500 | [diff] [blame] | 95 | // Check if a condition exists on the fan |
| 96 | auto condition = std::get<conditionField>(fanDef); |
| 97 | if (condition) |
| 98 | { |
| 99 | // Condition exists, skip adding fan if it fails |
| 100 | if (!(*condition)(bus)) |
| 101 | { |
| 102 | continue; |
| 103 | } |
| 104 | } |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 105 | fans.emplace_back( |
| 106 | std::make_unique<Fan>(mode, bus, event, trust, fanDef)); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 107 | } |
Matthew Barth | ccaf2db | 2020-06-04 13:09:35 -0500 | [diff] [blame] | 108 | #endif |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 109 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 110 | if (mode == Mode::init) |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 111 | { |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 112 | // Fans were initialized to be functional, exit |
| 113 | return 0; |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 114 | } |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 115 | |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 116 | return event.loop(); |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 117 | } |