1 /*
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
17 #include <systemd/sd-journal.h>
18
19 #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>
23 #include <cstdint>
24 #include <iostream>
25 #include <sdbusplus/asio/connection.hpp>
26 #include <sdbusplus/bus.hpp>
27 #include <sdbusplus/bus/match.hpp>
28 #include <string>
29 #include <variant>
30 #include <vector>
31
32 using GetSubTreeType = std::vector<
33 std::pair<std::string,
34 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
35 using 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>;
38 using Association = std::tuple<std::string, std::string, std::string>;
39
40 constexpr const char* assetTag =
41 "xyz.openbmc_project.Inventory.Decorator.Asset";
42
43 namespace mapper
44 {
45 constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
46 constexpr const char* path = "/xyz/openbmc_project/object_mapper";
47 constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
48 constexpr const char* subtree = "GetSubTree";
49 } // namespace mapper
50
51 namespace entityManager
52 {
53 constexpr const char* busName = "xyz.openbmc_project.EntityManager";
54 } // namespace entityManager
55
56 namespace inventory
57 {
58 constexpr const char* interface = "xyz.openbmc_project.Inventory.Item";
59 } // namespace inventory
60
61 namespace ledGroup
62 {
63 constexpr const char* interface = "xyz.openbmc_project.Led.Group";
64 constexpr const char* asserted = "Asserted";
65 } // namespace ledGroup
66
67 namespace properties
68 {
69 constexpr const char* interface = "org.freedesktop.DBus.Properties";
70 constexpr const char* get = "Get";
71 } // namespace properties
72
73 namespace power
74 {
75 const static constexpr char* busname = "xyz.openbmc_project.State.Host";
76 const static constexpr char* interface = "xyz.openbmc_project.State.Host";
77 const static constexpr char* path = "/xyz/openbmc_project/state/host0";
78 const static constexpr char* property = "CurrentHostState";
79 } // namespace power
80
81 namespace association
82 {
83 const static constexpr char* interface =
84 "xyz.openbmc_project.Association.Definitions";
85 } // namespace association
86
87 namespace hsbp
88 {
89 enum 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
118
119 static std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr;
120 static bool powerStatusOn = false;
121
isPowerOn(void)122 bool isPowerOn(void)
123 {
124 if (!powerMatch)
125 {
126 throw std::runtime_error("Power Match Not Created");
127 }
128 return powerStatusOn;
129 }
130
setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection> & conn)131 void 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
141 powerMatch = std::make_unique<sdbusplus::bus::match_t>(
142 static_cast<sdbusplus::bus_t&>(*conn),
143 "type='signal',interface='" + std::string(properties::interface) +
144 "',path='" + std::string(power::path) + "',arg0='" +
145 std::string(power::interface) + "'",
146 [](sdbusplus::message_t& message) {
147 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 }
194
logDeviceAdded(const std::string & model,const std::string & type,const std::string & sn)195 inline 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
logDeviceRemoved(const std::string & model,const std::string & type,const std::string & sn)204 inline 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
logDriveError(const std::string & name)213 inline 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 }
219