blob: 0b47a547bd2082a3543cd1bda727ee6d8ebd9bcc [file] [log] [blame]
Feist, Jamesc95cf672019-08-29 16:10:35 -07001/*
2// Copyright (c) 2019 Intel 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
James Feist9f6565d2019-10-09 13:15:13 -070017#include <boost/algorithm/string/predicate.hpp>
18#include <boost/asio/io_context.hpp>
19#include <boost/asio/steady_timer.hpp>
20#include <boost/container/flat_map.hpp>
Feist, Jamesc95cf672019-08-29 16:10:35 -070021#include <cstdint>
James Feist9f6565d2019-10-09 13:15:13 -070022#include <iostream>
23#include <sdbusplus/asio/connection.hpp>
Feist, Jamesc95cf672019-08-29 16:10:35 -070024#include <string>
25#include <variant>
26#include <vector>
27
28using GetSubTreeType = std::vector<
29 std::pair<std::string,
30 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
31using BasicVariantType =
32 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
33 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
James Feist0b236ab2019-10-02 09:09:16 -070034using Association = std::tuple<std::string, std::string, std::string>;
Feist, Jamesc95cf672019-08-29 16:10:35 -070035
36namespace mapper
37{
38constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
39constexpr const char* path = "/xyz/openbmc_project/object_mapper";
40constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
41constexpr const char* subtree = "GetSubTree";
42} // namespace mapper
43
44namespace entityManager
45{
46constexpr const char* busName = "xyz.openbmc_project.EntityManager";
47} // namespace entityManager
48
49namespace inventory
50{
51constexpr const char* interface = "xyz.openbmc_project.Inventory.Item";
52} // namespace inventory
53
James Feist09dd2312019-10-09 09:29:03 -070054namespace ledGroup
55{
56constexpr const char* interface = "xyz.openbmc_project.Led.Group";
57constexpr const char* asserted = "Asserted";
58} // namespace ledGroup
59
James Feist9f6565d2019-10-09 13:15:13 -070060namespace properties
61{
62constexpr const char* interface = "org.freedesktop.DBus.Properties";
63constexpr const char* get = "Get";
64} // namespace properties
65
66namespace power
67{
68const static constexpr char* busname = "xyz.openbmc_project.State.Host";
69const static constexpr char* interface = "xyz.openbmc_project.State.Host";
70const static constexpr char* path = "/xyz/openbmc_project/state/host0";
71const static constexpr char* property = "CurrentHostState";
72} // namespace power
73
Feist, Jamesc95cf672019-08-29 16:10:35 -070074namespace hsbp
75{
76enum class registers : uint8_t
77{
78 fpgaIdH = 0x0,
79 fpgaIdL = 0x1,
80 typeId = 0x2,
81 bootVer = 0x3,
82 fpgaVer = 0x4,
83 securityRev = 0x5,
84 funSupported = 0x6,
85 numDisks = 0x7,
86 presence = 0x8,
87 ssdIFDET = 0x9,
88 ifdetPart = 0xA,
89 statusLocate = 0xB,
90 statusFail = 0xC,
91 statusRebuild = 0xD,
92 ledOverride = 0xE,
93 ledStatus = 0xF,
94 ledPattern0 = 0x10,
95 ledPattern1 = 0x11,
96 ledPattern2 = 0x12,
97 ledPattern3 = 0x13,
98 ledPattern4 = 0x14,
99 ledPattern5 = 0x15,
100 ledPattern6 = 0x16,
101 ledPattern7 = 0x17,
102};
103
104} // namespace hsbp
James Feist9f6565d2019-10-09 13:15:13 -0700105
106static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
107static bool powerStatusOn = false;
108
109bool isPowerOn(void)
110{
111 if (!powerMatch)
112 {
113 throw std::runtime_error("Power Match Not Created");
114 }
115 return powerStatusOn;
116}
117
118void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
119{
120 static boost::asio::steady_timer timer(conn->get_io_context());
121 // create a match for powergood changes, first time do a method call to
122 // cache the correct value
123 if (powerMatch)
124 {
125 return;
126 }
127
128 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
129 static_cast<sdbusplus::bus::bus&>(*conn),
130 "type='signal',interface='" + std::string(properties::interface) +
131 "',path='" + std::string(power::path) + "',arg0='" +
132 std::string(power::interface) + "'",
133 [](sdbusplus::message::message& message) {
134 std::string objectName;
135 boost::container::flat_map<std::string, std::variant<std::string>>
136 values;
137 message.read(objectName, values);
138 auto findState = values.find(power::property);
139 if (findState != values.end())
140 {
141 bool on = boost::ends_with(
142 std::get<std::string>(findState->second), "Running");
143 if (!on)
144 {
145 timer.cancel();
146 powerStatusOn = false;
147 return;
148 }
149 // on comes too quickly
150 timer.expires_after(std::chrono::seconds(10));
151 timer.async_wait([](boost::system::error_code ec) {
152 if (ec == boost::asio::error::operation_aborted)
153 {
154 return;
155 }
156 else if (ec)
157 {
158 std::cerr << "Timer error " << ec.message() << "\n";
159 return;
160 }
161 powerStatusOn = true;
162 });
163 }
164 });
165
166 conn->async_method_call(
167 [](boost::system::error_code ec,
168 const std::variant<std::string>& state) {
169 if (ec)
170 {
171 // we commonly come up before power control, we'll capture the
172 // property change later
173 return;
174 }
175 powerStatusOn =
176 boost::ends_with(std::get<std::string>(state), "Running");
177 },
178 power::busname, power::path, properties::interface, properties::get,
179 power::interface, power::property);
180}