blob: c333089bf03e243c5931be8101e53fef87fd1752 [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 Feiste8818522019-11-04 13:36:10 -080017#include <systemd/sd-journal.h>
18
James Feist9f6565d2019-10-09 13:15:13 -070019#include <boost/algorithm/string/predicate.hpp>
20#include <boost/asio/io_context.hpp>
21#include <boost/asio/steady_timer.hpp>
22#include <boost/container/flat_map.hpp>
Feist, Jamesc95cf672019-08-29 16:10:35 -070023#include <cstdint>
James Feist9f6565d2019-10-09 13:15:13 -070024#include <iostream>
25#include <sdbusplus/asio/connection.hpp>
Feist, Jamesc95cf672019-08-29 16:10:35 -070026#include <string>
27#include <variant>
28#include <vector>
29
30using GetSubTreeType = std::vector<
31 std::pair<std::string,
32 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
33using BasicVariantType =
34 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
35 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
James Feist0b236ab2019-10-02 09:09:16 -070036using Association = std::tuple<std::string, std::string, std::string>;
Feist, Jamesc95cf672019-08-29 16:10:35 -070037
James Feistd0d36f12019-11-21 10:19:44 -080038constexpr const char* assetTag =
39 "xyz.openbmc_project.Inventory.Decorator.Asset";
40
Feist, Jamesc95cf672019-08-29 16:10:35 -070041namespace mapper
42{
43constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
44constexpr const char* path = "/xyz/openbmc_project/object_mapper";
45constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
46constexpr const char* subtree = "GetSubTree";
47} // namespace mapper
48
49namespace entityManager
50{
51constexpr const char* busName = "xyz.openbmc_project.EntityManager";
52} // namespace entityManager
53
54namespace inventory
55{
56constexpr const char* interface = "xyz.openbmc_project.Inventory.Item";
57} // namespace inventory
58
James Feist09dd2312019-10-09 09:29:03 -070059namespace ledGroup
60{
61constexpr const char* interface = "xyz.openbmc_project.Led.Group";
62constexpr const char* asserted = "Asserted";
63} // namespace ledGroup
64
James Feist9f6565d2019-10-09 13:15:13 -070065namespace properties
66{
67constexpr const char* interface = "org.freedesktop.DBus.Properties";
68constexpr const char* get = "Get";
69} // namespace properties
70
71namespace power
72{
73const static constexpr char* busname = "xyz.openbmc_project.State.Host";
74const static constexpr char* interface = "xyz.openbmc_project.State.Host";
75const static constexpr char* path = "/xyz/openbmc_project/state/host0";
76const static constexpr char* property = "CurrentHostState";
77} // namespace power
78
James Feist42b49c12019-10-29 15:18:43 -070079namespace association
80{
81const static constexpr char* interface =
82 "xyz.openbmc_project.Association.Definitions";
83} // namespace association
84
Feist, Jamesc95cf672019-08-29 16:10:35 -070085namespace hsbp
86{
87enum class registers : uint8_t
88{
89 fpgaIdH = 0x0,
90 fpgaIdL = 0x1,
91 typeId = 0x2,
92 bootVer = 0x3,
93 fpgaVer = 0x4,
94 securityRev = 0x5,
95 funSupported = 0x6,
96 numDisks = 0x7,
97 presence = 0x8,
98 ssdIFDET = 0x9,
99 ifdetPart = 0xA,
100 statusLocate = 0xB,
101 statusFail = 0xC,
102 statusRebuild = 0xD,
103 ledOverride = 0xE,
104 ledStatus = 0xF,
105 ledPattern0 = 0x10,
106 ledPattern1 = 0x11,
107 ledPattern2 = 0x12,
108 ledPattern3 = 0x13,
109 ledPattern4 = 0x14,
110 ledPattern5 = 0x15,
111 ledPattern6 = 0x16,
112 ledPattern7 = 0x17,
113};
114
115} // namespace hsbp
James Feist9f6565d2019-10-09 13:15:13 -0700116
117static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
118static bool powerStatusOn = false;
119
120bool isPowerOn(void)
121{
122 if (!powerMatch)
123 {
124 throw std::runtime_error("Power Match Not Created");
125 }
126 return powerStatusOn;
127}
128
129void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
130{
131 static boost::asio::steady_timer timer(conn->get_io_context());
132 // create a match for powergood changes, first time do a method call to
133 // cache the correct value
134 if (powerMatch)
135 {
136 return;
137 }
138
139 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
140 static_cast<sdbusplus::bus::bus&>(*conn),
141 "type='signal',interface='" + std::string(properties::interface) +
142 "',path='" + std::string(power::path) + "',arg0='" +
143 std::string(power::interface) + "'",
144 [](sdbusplus::message::message& message) {
145 std::string objectName;
146 boost::container::flat_map<std::string, std::variant<std::string>>
147 values;
148 message.read(objectName, values);
149 auto findState = values.find(power::property);
150 if (findState != values.end())
151 {
152 bool on = boost::ends_with(
153 std::get<std::string>(findState->second), "Running");
154 if (!on)
155 {
156 timer.cancel();
157 powerStatusOn = false;
158 return;
159 }
160 // on comes too quickly
161 timer.expires_after(std::chrono::seconds(10));
162 timer.async_wait([](boost::system::error_code ec) {
163 if (ec == boost::asio::error::operation_aborted)
164 {
165 return;
166 }
167 else if (ec)
168 {
169 std::cerr << "Timer error " << ec.message() << "\n";
170 return;
171 }
172 powerStatusOn = true;
173 });
174 }
175 });
176
177 conn->async_method_call(
178 [](boost::system::error_code ec,
179 const std::variant<std::string>& state) {
180 if (ec)
181 {
182 // we commonly come up before power control, we'll capture the
183 // property change later
184 return;
185 }
186 powerStatusOn =
187 boost::ends_with(std::get<std::string>(state), "Running");
188 },
189 power::busname, power::path, properties::interface, properties::get,
190 power::interface, power::property);
191}
James Feiste8818522019-11-04 13:36:10 -0800192
193inline void logDeviceAdded(const std::string& model, const std::string& type,
194 const std::string& sn)
195{
196 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR,
197 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
198 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
199 type.c_str(), sn.c_str(), NULL);
200}
201
202inline void logDeviceRemoved(const std::string& model, const std::string& type,
203 const std::string& sn)
204{
205 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR,
206 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
207 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
208 type.c_str(), sn.c_str(), NULL);
209}
210
211inline void logDriveError(const std::string& name)
212{
213 sd_journal_send("MESSAGE=%s", "Drive Error", "PRIORITY=%i", LOG_ERR,
214 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.DriveError",
215 "REDFISH_MESSAGE_ARGS=%s", name.c_str(), NULL);
216}