blob: f887232c2fb31448e14b4dfa419dd37a92a648e4 [file] [log] [blame]
Matt Spinlere567dd22017-04-27 12:27:17 -05001/**
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 Spinlerc36168a2017-04-27 14:32:43 -050016#include <phosphor-logging/log.hpp>
17#include <sdbusplus/bus.hpp>
Matt Spinlera5763ff2017-06-14 15:54:12 -050018#include <systemd/sd-daemon.h>
Matthew Barth6ad28432017-08-22 11:18:19 -050019#include "argument.hpp"
Matt Spinlere824f982017-05-11 10:07:55 -050020#include "event.hpp"
Matt Spinlerc36168a2017-04-27 14:32:43 -050021#include "fan.hpp"
22#include "fan_defs.hpp"
Matt Spinlere567dd22017-04-27 12:27:17 -050023
Matt Spinlerc36168a2017-04-27 14:32:43 -050024using namespace phosphor::fan::monitor;
25using namespace phosphor::logging;
26
Matthew Barth6ad28432017-08-22 11:18:19 -050027int main(int argc, char* argv[])
Matt Spinlere567dd22017-04-27 12:27:17 -050028{
Matt Spinlerc36168a2017-04-27 14:32:43 -050029 auto bus = sdbusplus::bus::new_default();
30 sd_event* events = nullptr;
Matt Spinler84201112017-05-12 11:31:53 -050031 std::vector<std::unique_ptr<Fan>> fans;
Matthew Barth6ad28432017-08-22 11:18:19 -050032 phosphor::fan::util::ArgumentParser args(argc, argv);
33
34 if (argc != 2)
35 {
36 args.usage(argv);
37 exit(-1);
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);
52 exit(-1);
53 }
Matt Spinlerc36168a2017-04-27 14:32:43 -050054
55 auto r = sd_event_default(&events);
56 if (r < 0)
57 {
58 log<level::ERR>("Failed call to sd_event_default()",
59 entry("ERROR=%s", strerror(-r)));
60 return -1;
61 }
62
Matt Spinlere824f982017-05-11 10:07:55 -050063 phosphor::fan::event::EventPtr eventPtr{events};
Matt Spinlerc36168a2017-04-27 14:32:43 -050064
65 //Attach the event object to the bus object so we can
66 //handle both sd_events (for the timers) and dbus signals.
67 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
68
69 for (const auto& fanDef : fanDefinitions)
70 {
Matthew Barth6ad28432017-08-22 11:18:19 -050071 fans.emplace_back(std::make_unique<Fan>(mode, bus, eventPtr, fanDef));
Matt Spinlerc36168a2017-04-27 14:32:43 -050072 }
73
Matthew Barth6ad28432017-08-22 11:18:19 -050074 if (mode == Mode::init)
Matt Spinlera5763ff2017-06-14 15:54:12 -050075 {
Matthew Barth6ad28432017-08-22 11:18:19 -050076 // Fans were initialized to be functional, exit
77 return 0;
Matt Spinlera5763ff2017-06-14 15:54:12 -050078 }
Matthew Barth6ad28432017-08-22 11:18:19 -050079 else
Matt Spinlerc36168a2017-04-27 14:32:43 -050080 {
Matthew Barth6ad28432017-08-22 11:18:19 -050081 r = sd_event_loop(eventPtr.get());
82 if (r < 0)
83 {
84 log<level::ERR>("Failed call to sd_event_loop",
85 entry("ERROR=%s", strerror(-r)));
86 }
Matt Spinlerc36168a2017-04-27 14:32:43 -050087 }
88
89 return -1;
Matt Spinlere567dd22017-04-27 12:27:17 -050090}