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 | */ |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 16 | #include <phosphor-logging/log.hpp> |
| 17 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame^] | 18 | #include <sdeventplus/event.hpp> |
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" |
| 21 | #include "fan_defs.hpp" |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 22 | #include "trust_manager.hpp" |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 23 | |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 24 | using namespace phosphor::fan::monitor; |
| 25 | using namespace phosphor::logging; |
| 26 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 27 | int main(int argc, char* argv[]) |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 28 | { |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame^] | 29 | auto event = sdeventplus::Event::get_default(); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 30 | auto bus = sdbusplus::bus::new_default(); |
Matt Spinler | 8420111 | 2017-05-12 11:31:53 -0500 | [diff] [blame] | 31 | std::vector<std::unique_ptr<Fan>> fans; |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 32 | phosphor::fan::util::ArgumentParser args(argc, argv); |
| 33 | |
| 34 | if (argc != 2) |
| 35 | { |
| 36 | args.usage(argv); |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 37 | return 1; |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | Mode mode; |
| 41 | if (args["init"] == "true") |
| 42 | { |
| 43 | mode = Mode::init; |
| 44 | } |
| 45 | else if (args["monitor"] == "true") |
| 46 | { |
| 47 | mode = Mode::monitor; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | args.usage(argv); |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 52 | return 1; |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 53 | } |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 54 | |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 55 | std::unique_ptr<phosphor::fan::trust::Manager> trust = |
| 56 | std::make_unique<phosphor::fan::trust::Manager>(trustGroups); |
| 57 | |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 58 | //Attach the event object to the bus object so we can |
| 59 | //handle both sd_events (for the timers) and dbus signals. |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame^] | 60 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 61 | |
| 62 | for (const auto& fanDef : fanDefinitions) |
| 63 | { |
Matthew Barth | 81748b1 | 2018-05-02 16:03:48 -0500 | [diff] [blame] | 64 | // Check if a condition exists on the fan |
| 65 | auto condition = std::get<conditionField>(fanDef); |
| 66 | if (condition) |
| 67 | { |
| 68 | // Condition exists, skip adding fan if it fails |
| 69 | if (!(*condition)(bus)) |
| 70 | { |
| 71 | continue; |
| 72 | } |
| 73 | } |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 74 | fans.emplace_back(std::make_unique<Fan>( |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame^] | 75 | mode, bus, event, trust, fanDef)); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 78 | if (mode == Mode::init) |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 79 | { |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 80 | // Fans were initialized to be functional, exit |
| 81 | return 0; |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 82 | } |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 83 | |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame^] | 84 | return event.loop(); |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 85 | } |