blob: cde71d0899b61d8fdf6221c68f49fef913110abd [file] [log] [blame]
Matt Spinlere73446e2017-04-10 13:55:52 -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 */
16#include <sdbusplus/bus.hpp>
Matthew Barth8600d9a2017-06-23 14:38:05 -050017#include <phosphor-logging/log.hpp>
Matt Spinleree7f6422017-05-09 11:03:14 -050018#include "argument.hpp"
Matt Spinlere10416e2017-04-10 14:15:53 -050019#include "manager.hpp"
Matthew Barth8600d9a2017-06-23 14:38:05 -050020#include "event.hpp"
Matt Spinlere73446e2017-04-10 13:55:52 -050021
Matt Spinleree7f6422017-05-09 11:03:14 -050022using namespace phosphor::fan::control;
Matthew Barth8600d9a2017-06-23 14:38:05 -050023using namespace phosphor::logging;
Matt Spinleree7f6422017-05-09 11:03:14 -050024
Matt Spinlere73446e2017-04-10 13:55:52 -050025int main(int argc, char* argv[])
26{
27 auto bus = sdbusplus::bus::new_default();
Matthew Barth8600d9a2017-06-23 14:38:05 -050028 sd_event* events = nullptr;
Matt Spinleree7f6422017-05-09 11:03:14 -050029 phosphor::fan::util::ArgumentParser args(argc, argv);
Matt Spinlere73446e2017-04-10 13:55:52 -050030
Matt Spinleree7f6422017-05-09 11:03:14 -050031 if (argc != 2)
32 {
33 args.usage(argv);
34 exit(-1);
35 }
36
Matthew Barth14184132017-05-19 14:37:30 -050037 Mode mode;
Matt Spinleree7f6422017-05-09 11:03:14 -050038
39 if (args["init"] == "true")
40 {
Matthew Barth14184132017-05-19 14:37:30 -050041 mode = Mode::init;
Matt Spinleree7f6422017-05-09 11:03:14 -050042 }
43 else if (args["control"] == "true")
44 {
Matthew Barth14184132017-05-19 14:37:30 -050045 mode = Mode::control;
Matt Spinleree7f6422017-05-09 11:03:14 -050046 }
47 else
48 {
49 args.usage(argv);
50 exit(-1);
51 }
52
Matthew Barth8600d9a2017-06-23 14:38:05 -050053 auto r = sd_event_default(&events);
54 if (r < 0)
55 {
56 log<level::ERR>("Failed call to sd_event_default()",
57 entry("ERROR=%s", strerror(-r)));
58 return -1;
59 }
60
61 phosphor::fan::event::EventPtr eventPtr{events};
62
63 //Attach the event object to the bus object so we can
64 //handle both sd_events (for the timers) and dbus signals.
65 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
66
67 Manager manager(bus, eventPtr, mode);
Matt Spinleree7f6422017-05-09 11:03:14 -050068
69 //Init mode will just set fans to max and delay
Matthew Barth14184132017-05-19 14:37:30 -050070 if (mode == Mode::init)
Matt Spinleree7f6422017-05-09 11:03:14 -050071 {
72 manager.doInit();
73 return 0;
74 }
Matthew Barth8600d9a2017-06-23 14:38:05 -050075 else
Matt Spinlere73446e2017-04-10 13:55:52 -050076 {
Matthew Barth8600d9a2017-06-23 14:38:05 -050077 r = sd_event_loop(eventPtr.get());
78 if (r < 0)
79 {
80 log<level::ERR>("Failed call to sd_event_loop",
81 entry("ERROR=%s", strerror(-r)));
82 }
Matt Spinlere73446e2017-04-10 13:55:52 -050083 }
84
Matthew Barth8600d9a2017-06-23 14:38:05 -050085 return -1;
Matt Spinlere73446e2017-04-10 13:55:52 -050086}