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