George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 1 | #include "lamptest.hpp" |
| 2 | |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 3 | #include <phosphor-logging/lg2.hpp> |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 4 | |
George Liu | c5e0f31 | 2021-12-27 15:54:31 +0800 | [diff] [blame] | 5 | #include <algorithm> |
| 6 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 7 | namespace phosphor |
| 8 | { |
| 9 | namespace led |
| 10 | { |
| 11 | |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 12 | using Json = nlohmann::json; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 13 | |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 14 | bool LampTest::processLEDUpdates(const ActionSet& ledsAssert, |
| 15 | const ActionSet& ledsDeAssert) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 16 | { |
| 17 | // If the physical LED status is updated during the lamp test, it should be |
| 18 | // saved to Queue, and the queue will be processed after the lamp test is |
| 19 | // stopped. |
| 20 | if (isLampTestRunning) |
| 21 | { |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 22 | // Physical LEDs will be updated during lamp test |
| 23 | for (const auto& it : ledsDeAssert) |
| 24 | { |
| 25 | std::string path = std::string(PHY_LED_PATH) + it.name; |
| 26 | auto iter = std::find_if( |
| 27 | forceUpdateLEDs.begin(), forceUpdateLEDs.end(), |
| 28 | [&path](const auto& name) { return name == path; }); |
| 29 | |
| 30 | if (iter != forceUpdateLEDs.end()) |
| 31 | { |
| 32 | manager.drivePhysicalLED(path, Layout::Action::Off, it.dutyOn, |
| 33 | it.period); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | for (const auto& it : ledsAssert) |
| 38 | { |
| 39 | std::string path = std::string(PHY_LED_PATH) + it.name; |
| 40 | auto iter = std::find_if( |
| 41 | forceUpdateLEDs.begin(), forceUpdateLEDs.end(), |
| 42 | [&path](const auto& name) { return name == path; }); |
| 43 | |
| 44 | if (iter != forceUpdateLEDs.end()) |
| 45 | { |
| 46 | manager.drivePhysicalLED(path, it.action, it.dutyOn, it.period); |
| 47 | } |
| 48 | } |
| 49 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 50 | updatedLEDsDuringLampTest.emplace( |
| 51 | std::make_pair(ledsAssert, ledsDeAssert)); |
| 52 | return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 57 | void LampTest::stop() |
| 58 | { |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 59 | if (!isLampTestRunning) |
| 60 | { |
| 61 | return; |
| 62 | } |
| 63 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 64 | timer.setEnabled(false); |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 65 | |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 66 | // Stop host lamp test |
| 67 | doHostLampTest(false); |
| 68 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 69 | // Set all the Physical action to Off |
| 70 | for (const auto& path : physicalLEDPaths) |
| 71 | { |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 72 | auto iter = |
| 73 | std::find_if(skipUpdateLEDs.begin(), skipUpdateLEDs.end(), |
| 74 | [&path](const auto& skip) { return skip == path; }); |
| 75 | |
| 76 | if (iter != skipUpdateLEDs.end()) |
| 77 | { |
| 78 | // Skip update physical path |
| 79 | continue; |
| 80 | } |
| 81 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 82 | manager.drivePhysicalLED(path, Layout::Action::Off, 0, 0); |
| 83 | } |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 84 | |
| 85 | isLampTestRunning = false; |
| 86 | restorePhysicalLedStates(); |
| 87 | } |
| 88 | |
| 89 | Layout::Action LampTest::getActionFromString(const std::string& str) |
| 90 | { |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 91 | Layout::Action action = Layout::Action::Off; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 92 | |
| 93 | if (str == "xyz.openbmc_project.Led.Physical.Action.On") |
| 94 | { |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 95 | action = Layout::Action::On; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 96 | } |
| 97 | else if (str == "xyz.openbmc_project.Led.Physical.Action.Blink") |
| 98 | { |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 99 | action = Layout::Action::Blink; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return action; |
| 103 | } |
| 104 | |
| 105 | void LampTest::storePhysicalLEDsStates() |
| 106 | { |
| 107 | physicalLEDStatesPriorToLampTest.clear(); |
| 108 | |
| 109 | for (const auto& path : physicalLEDPaths) |
| 110 | { |
Patrick Williams | 857da38 | 2023-05-10 07:50:20 -0500 | [diff] [blame] | 111 | auto iter = std::find_if(skipUpdateLEDs.begin(), skipUpdateLEDs.end(), |
| 112 | [&path](const auto& skipLed) { |
| 113 | return skipLed == path; |
| 114 | }); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 115 | |
| 116 | if (iter != skipUpdateLEDs.end()) |
| 117 | { |
| 118 | // Physical LEDs will be skipped |
| 119 | continue; |
| 120 | } |
| 121 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 122 | // Reverse intercept path, Get the name of each member of physical led |
| 123 | // e.g: path = /xyz/openbmc_project/led/physical/front_fan |
| 124 | // name = front_fan |
| 125 | sdbusplus::message::object_path object_path(path); |
| 126 | auto name = object_path.filename(); |
| 127 | if (name.empty()) |
| 128 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 129 | lg2::error( |
| 130 | "Failed to get the name of member of physical LED path, PATH = {PATH}, NAME = {NAME}", |
| 131 | "PATH", path, "NAME", name); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 132 | continue; |
| 133 | } |
| 134 | |
| 135 | std::string state{}; |
| 136 | uint16_t period{}; |
| 137 | uint8_t dutyOn{}; |
| 138 | try |
| 139 | { |
| 140 | auto properties = dBusHandler.getAllProperties(path, PHY_LED_IFACE); |
| 141 | |
| 142 | state = std::get<std::string>(properties["State"]); |
| 143 | period = std::get<uint16_t>(properties["Period"]); |
| 144 | dutyOn = std::get<uint8_t>(properties["DutyOn"]); |
| 145 | } |
Patrick Williams | 3e073ba | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 146 | catch (const sdbusplus::exception_t& e) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 147 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 148 | lg2::error( |
| 149 | "Failed to get All properties, ERROR = {ERROR}, PATH = {PATH}", |
| 150 | "ERROR", e, "PATH", path); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 151 | continue; |
| 152 | } |
| 153 | |
| 154 | phosphor::led::Layout::Action action = getActionFromString(state); |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 155 | if (action != phosphor::led::Layout::Action::Off) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 156 | { |
| 157 | phosphor::led::Layout::LedAction ledAction{ |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 158 | name, action, dutyOn, period, |
| 159 | phosphor::led::Layout::Action::On}; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 160 | physicalLEDStatesPriorToLampTest.emplace(ledAction); |
| 161 | } |
| 162 | } |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void LampTest::start() |
| 166 | { |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 167 | if (isLampTestRunning) |
| 168 | { |
| 169 | // reset the timer and then return |
| 170 | timer.restart(std::chrono::seconds(LAMP_TEST_TIMEOUT_IN_SECS)); |
| 171 | return; |
| 172 | } |
| 173 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 174 | // Get paths of all the Physical LED objects |
George Liu | 785f505 | 2023-07-18 09:38:43 +0800 | [diff] [blame^] | 175 | try |
| 176 | { |
| 177 | physicalLEDPaths = dBusHandler.getSubTreePaths(PHY_LED_PATH, |
| 178 | PHY_LED_IFACE); |
| 179 | } |
| 180 | catch (const sdbusplus::exception_t& e) |
| 181 | { |
| 182 | lg2::error( |
| 183 | "Failed to call the SubTreePaths method: {ERROR}, ledPath: {PATH}, ledInterface: {INTERFACE}", |
| 184 | "ERROR", e, "PATH", PHY_LED_PATH, "INTERFACE", PHY_LED_IFACE); |
| 185 | return; |
| 186 | } |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 187 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 188 | // Get physical LEDs states before lamp test |
| 189 | storePhysicalLEDsStates(); |
| 190 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 191 | // restart lamp test, it contains initiate or reset the timer. |
| 192 | timer.restart(std::chrono::seconds(LAMP_TEST_TIMEOUT_IN_SECS)); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 193 | isLampTestRunning = true; |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 194 | |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 195 | // Notify PHYP to start the lamp test |
| 196 | doHostLampTest(true); |
| 197 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 198 | // Set all the Physical action to On for lamp test |
| 199 | for (const auto& path : physicalLEDPaths) |
| 200 | { |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 201 | auto iter = |
| 202 | std::find_if(skipUpdateLEDs.begin(), skipUpdateLEDs.end(), |
| 203 | [&path](const auto& skip) { return skip == path; }); |
| 204 | |
| 205 | if (iter != skipUpdateLEDs.end()) |
| 206 | { |
| 207 | // Skip update physical path |
| 208 | continue; |
| 209 | } |
| 210 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 211 | manager.drivePhysicalLED(path, Layout::Action::On, 0, 0); |
| 212 | } |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void LampTest::timeOutHandler() |
| 216 | { |
| 217 | // set the Asserted property of lamp test to false |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 218 | if (!groupObj) |
| 219 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 220 | lg2::error("the Group object is nullptr"); |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 221 | throw std::runtime_error("the Group object is nullptr"); |
| 222 | } |
| 223 | |
| 224 | groupObj->asserted(false); |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 225 | } |
| 226 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 227 | void LampTest::requestHandler(Group* group, bool value) |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 228 | { |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 229 | if (groupObj == NULL) |
| 230 | { |
| 231 | groupObj = std::move(group); |
| 232 | } |
| 233 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 234 | if (value) |
| 235 | { |
| 236 | start(); |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | stop(); |
| 241 | } |
| 242 | } |
| 243 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 244 | void LampTest::restorePhysicalLedStates() |
| 245 | { |
| 246 | // restore physical LEDs states before lamp test |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 247 | ActionSet ledsDeAssert{}; |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 248 | manager.driveLEDs(physicalLEDStatesPriorToLampTest, ledsDeAssert); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 249 | physicalLEDStatesPriorToLampTest.clear(); |
| 250 | |
| 251 | // restore physical LEDs states during lamp test |
| 252 | while (!updatedLEDsDuringLampTest.empty()) |
| 253 | { |
| 254 | auto& [ledsAssert, ledsDeAssert] = updatedLEDsDuringLampTest.front(); |
| 255 | manager.driveLEDs(ledsAssert, ledsDeAssert); |
| 256 | updatedLEDsDuringLampTest.pop(); |
| 257 | } |
| 258 | } |
| 259 | |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 260 | void LampTest::doHostLampTest(bool value) |
| 261 | { |
| 262 | try |
| 263 | { |
| 264 | PropertyValue assertedValue{value}; |
| 265 | dBusHandler.setProperty(HOST_LAMP_TEST_OBJECT, |
| 266 | "xyz.openbmc_project.Led.Group", "Asserted", |
| 267 | assertedValue); |
| 268 | } |
Patrick Williams | 3e073ba | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 269 | catch (const sdbusplus::exception_t& e) |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 270 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 271 | lg2::error( |
| 272 | "Failed to set Asserted property, ERROR = {ERROR}, PATH = {PATH}", |
| 273 | "ERROR", e, "PATH", std::string(HOST_LAMP_TEST_OBJECT)); |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 277 | void LampTest::getPhysicalLEDNamesFromJson(const fs::path& path) |
| 278 | { |
| 279 | if (!fs::exists(path) || fs::is_empty(path)) |
| 280 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 281 | lg2::info("The file does not exist or is empty, FILE_PATH = {PATH}", |
| 282 | "PATH", path); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | |
| 286 | try |
| 287 | { |
| 288 | std::ifstream jsonFile(path); |
| 289 | auto json = Json::parse(jsonFile); |
| 290 | |
| 291 | // define the default JSON as empty |
| 292 | const std::vector<std::string> empty{}; |
| 293 | auto forceLEDs = json.value("forceLEDs", empty); |
George Liu | c5e0f31 | 2021-12-27 15:54:31 +0800 | [diff] [blame] | 294 | std::ranges::transform(forceLEDs, std::back_inserter(forceUpdateLEDs), |
| 295 | [](const auto& i) { return PHY_LED_PATH + i; }); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 296 | |
| 297 | auto skipLEDs = json.value("skipLEDs", empty); |
George Liu | c5e0f31 | 2021-12-27 15:54:31 +0800 | [diff] [blame] | 298 | std::ranges::transform(skipLEDs, std::back_inserter(skipUpdateLEDs), |
| 299 | [](const auto& i) { return PHY_LED_PATH + i; }); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 300 | } |
| 301 | catch (const std::exception& e) |
| 302 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 303 | lg2::error( |
| 304 | "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}", |
| 305 | "ERROR", e, "PATH", path); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 306 | } |
| 307 | return; |
| 308 | } |
| 309 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 310 | } // namespace led |
| 311 | } // namespace phosphor |