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> |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 18 | #include <systemd/sd-daemon.h> |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 19 | #include "argument.hpp" |
Matt Spinler | e824f98 | 2017-05-11 10:07:55 -0500 | [diff] [blame] | 20 | #include "event.hpp" |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 21 | #include "fan.hpp" |
| 22 | #include "fan_defs.hpp" |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 23 | #include "trust_manager.hpp" |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 24 | |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 25 | using namespace phosphor::fan::monitor; |
| 26 | using namespace phosphor::logging; |
| 27 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 28 | int main(int argc, char* argv[]) |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 29 | { |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 30 | auto bus = sdbusplus::bus::new_default(); |
| 31 | sd_event* events = nullptr; |
Matt Spinler | 8420111 | 2017-05-12 11:31:53 -0500 | [diff] [blame] | 32 | std::vector<std::unique_ptr<Fan>> fans; |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 33 | phosphor::fan::util::ArgumentParser args(argc, argv); |
| 34 | |
| 35 | if (argc != 2) |
| 36 | { |
| 37 | args.usage(argv); |
| 38 | exit(-1); |
| 39 | } |
| 40 | |
| 41 | Mode mode; |
| 42 | if (args["init"] == "true") |
| 43 | { |
| 44 | mode = Mode::init; |
| 45 | } |
| 46 | else if (args["monitor"] == "true") |
| 47 | { |
| 48 | mode = Mode::monitor; |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | args.usage(argv); |
| 53 | exit(-1); |
| 54 | } |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 55 | |
| 56 | auto r = sd_event_default(&events); |
| 57 | if (r < 0) |
| 58 | { |
| 59 | log<level::ERR>("Failed call to sd_event_default()", |
| 60 | entry("ERROR=%s", strerror(-r))); |
| 61 | return -1; |
| 62 | } |
| 63 | |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 64 | std::unique_ptr<phosphor::fan::trust::Manager> trust = |
| 65 | std::make_unique<phosphor::fan::trust::Manager>(trustGroups); |
| 66 | |
Matt Spinler | e824f98 | 2017-05-11 10:07:55 -0500 | [diff] [blame] | 67 | phosphor::fan::event::EventPtr eventPtr{events}; |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 68 | |
| 69 | //Attach the event object to the bus object so we can |
| 70 | //handle both sd_events (for the timers) and dbus signals. |
| 71 | bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
| 72 | |
| 73 | for (const auto& fanDef : fanDefinitions) |
| 74 | { |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 75 | fans.emplace_back(std::make_unique<Fan>( |
| 76 | mode, bus, eventPtr, trust, fanDef)); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 79 | if (mode == Mode::init) |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 80 | { |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 81 | // Fans were initialized to be functional, exit |
| 82 | return 0; |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 83 | } |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 84 | else |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 85 | { |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 86 | r = sd_event_loop(eventPtr.get()); |
| 87 | if (r < 0) |
| 88 | { |
| 89 | log<level::ERR>("Failed call to sd_event_loop", |
| 90 | entry("ERROR=%s", strerror(-r))); |
| 91 | } |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return -1; |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 95 | } |