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 <string> 27 #include <variant> 28 #include <vector> 29 30 using GetSubTreeType = std::vector< 31 std::pair<std::string, 32 std::vector<std::pair<std::string, std::vector<std::string>>>>>; 33 using 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>; 36 using Association = std::tuple<std::string, std::string, std::string>; 37 38 constexpr const char* assetTag = 39 "xyz.openbmc_project.Inventory.Decorator.Asset"; 40 41 namespace mapper 42 { 43 constexpr const char* busName = "xyz.openbmc_project.ObjectMapper"; 44 constexpr const char* path = "/xyz/openbmc_project/object_mapper"; 45 constexpr const char* interface = "xyz.openbmc_project.ObjectMapper"; 46 constexpr const char* subtree = "GetSubTree"; 47 } // namespace mapper 48 49 namespace entityManager 50 { 51 constexpr const char* busName = "xyz.openbmc_project.EntityManager"; 52 } // namespace entityManager 53 54 namespace inventory 55 { 56 constexpr const char* interface = "xyz.openbmc_project.Inventory.Item"; 57 } // namespace inventory 58 59 namespace ledGroup 60 { 61 constexpr const char* interface = "xyz.openbmc_project.Led.Group"; 62 constexpr const char* asserted = "Asserted"; 63 } // namespace ledGroup 64 65 namespace properties 66 { 67 constexpr const char* interface = "org.freedesktop.DBus.Properties"; 68 constexpr const char* get = "Get"; 69 } // namespace properties 70 71 namespace power 72 { 73 const static constexpr char* busname = "xyz.openbmc_project.State.Host"; 74 const static constexpr char* interface = "xyz.openbmc_project.State.Host"; 75 const static constexpr char* path = "/xyz/openbmc_project/state/host0"; 76 const static constexpr char* property = "CurrentHostState"; 77 } // namespace power 78 79 namespace association 80 { 81 const static constexpr char* interface = 82 "xyz.openbmc_project.Association.Definitions"; 83 } // namespace association 84 85 namespace hsbp 86 { 87 enum 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 116 117 static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr; 118 static bool powerStatusOn = false; 119 120 bool isPowerOn(void) 121 { 122 if (!powerMatch) 123 { 124 throw std::runtime_error("Power Match Not Created"); 125 } 126 return powerStatusOn; 127 } 128 129 void 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 } 192 193 inline 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 202 inline 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 211 inline 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 } 217