blob: e36bd243008fda189e262db8dfbb2ecf690d7ca4 [file] [log] [blame]
Brandon Wyman24e422f2017-07-25 19:40:14 -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 <iostream>
17#include <phosphor-logging/log.hpp>
18#include <systemd/sd-daemon.h>
19#include "argument.hpp"
20#include "event.hpp"
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050021#include "power_supply.hpp"
22#include "device_monitor.hpp"
Brandon Wyman24e422f2017-07-25 19:40:14 -050023
24using namespace witherspoon::power;
25using namespace phosphor::logging;
26
27int main(int argc, char* argv[])
28{
Brandon Wyman24e422f2017-07-25 19:40:14 -050029 auto options = ArgumentParser(argc, argv);
30
31 auto objpath = (options)["path"];
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050032 auto instnum = (options)["instance"];
33 auto invpath = (options)["inventory"];
34 if (argc < 4)
Brandon Wyman24e422f2017-07-25 19:40:14 -050035 {
36 std::cerr << std::endl << "Too few arguments" << std::endl;
37 options.usage(argv);
38 return -1;
39 }
40
41 if (objpath == ArgumentParser::emptyString)
42 {
43 log<level::ERR>("Device monitoring path argument required");
44 return -2;
45 }
46
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050047 if (instnum == ArgumentParser::emptyString)
48 {
49 log<level::ERR>("Device monitoring instance number argument required");
50 return -3;
51 }
52
53 if (invpath == ArgumentParser::emptyString)
54 {
55 log<level::ERR>("Device monitoring inventory path argument required");
56 return -4;
57 }
58
Brandon Wyman442035f2017-08-08 15:58:45 -050059 auto bus = sdbusplus::bus::new_default();
60
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050061 auto objname = "power_supply" + instnum;
62 auto instance = std::stoul(instnum);
63 auto psuDevice = std::make_unique<psu::PowerSupply>(objname,
64 std::move(instance),
65 std::move(objpath),
Brandon Wyman442035f2017-08-08 15:58:45 -050066 std::move(invpath),
67 bus);
68
Brandon Wyman24e422f2017-07-25 19:40:14 -050069 sd_event* events = nullptr;
70
71 auto r = sd_event_default(&events);
72 if (r < 0)
73 {
74 log<level::ERR>("Failed call to sd_event_default()",
75 entry("ERROR=%s", strerror(-r)));
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050076 return -5;
Brandon Wyman24e422f2017-07-25 19:40:14 -050077 }
78
79 witherspoon::power::event::Event eventPtr{events};
80
81 //Attach the event object to the bus object so we can
82 //handle both sd_events (for the timers) and dbus signals.
83 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
84
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050085 // TODO: Use inventory path to subscribe to signal change for power supply presence.
Brandon Wyman24e422f2017-07-25 19:40:14 -050086
Brandon Wyman442035f2017-08-08 15:58:45 -050087 //Attach the event object to the bus object so we can
88 //handle both sd_events (for the timers) and dbus signals.
89 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
90
91 // TODO: Get power state on startup.
92 // TODO: Get presence state on startup and subscribe to presence changes.
93 // TODO - set to vinUVFault to false on presence change & start of poweron
94 // TODO - set readFailLogged to false on presence change and start of poweron
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050095 auto pollInterval = std::chrono::milliseconds(1000);
96 DeviceMonitor mainloop(std::move(psuDevice), eventPtr, pollInterval);
97 mainloop.run();
Brandon Wyman24e422f2017-07-25 19:40:14 -050098
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050099 return 0;
Brandon Wyman24e422f2017-07-25 19:40:14 -0500100}