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  */
16 #include <phosphor-logging/log.hpp>
17 #include <sdbusplus/bus.hpp>
18 #include <sdeventplus/event.hpp>
19 #include "argument.hpp"
20 #include "fan.hpp"
21 #include "fan_defs.hpp"
22 #include "trust_manager.hpp"
23 
24 using namespace phosphor::fan::monitor;
25 using namespace phosphor::logging;
26 
27 int main(int argc, char* argv[])
28 {
29     auto event = sdeventplus::Event::get_default();
30     auto bus = sdbusplus::bus::new_default();
31     std::vector<std::unique_ptr<Fan>> fans;
32     phosphor::fan::util::ArgumentParser args(argc, argv);
33 
34     if (argc != 2)
35     {
36         args.usage(argv);
37         return 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         return 1;
53     }
54 
55     std::unique_ptr<phosphor::fan::trust::Manager> trust =
56             std::make_unique<phosphor::fan::trust::Manager>(trustGroups);
57 
58     //Attach the event object to the bus object so we can
59     //handle both sd_events (for the timers) and dbus signals.
60     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
61 
62     for (const auto& fanDef : fanDefinitions)
63     {
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         }
74         fans.emplace_back(std::make_unique<Fan>(
75                 mode, bus, event, trust, fanDef));
76     }
77 
78     if (mode == Mode::init)
79     {
80         // Fans were initialized to be functional, exit
81         return 0;
82     }
83 
84     return event.loop();
85 }
86