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