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