Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 1 | /** |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame] | 2 | * Copyright © 2016,2018 IBM Corporation |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 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 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 17 | #include "physical.hpp" |
| 18 | |
Andrew Jeffery | 2332e91 | 2018-05-25 15:45:38 +0930 | [diff] [blame] | 19 | #include <cassert> |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 20 | #include <cstdlib> |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <string> |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 23 | namespace phosphor |
| 24 | { |
| 25 | namespace led |
| 26 | { |
| 27 | |
| 28 | /** @brief Populates key parameters */ |
| 29 | void Physical::setInitialState() |
| 30 | { |
Jayashree Dhanapal | e48bf95 | 2022-08-17 17:34:32 +0530 | [diff] [blame] | 31 | assert = led->getMaxBrightness(); |
| 32 | auto trigger = led->getTrigger(); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 33 | if (trigger == "timer") |
| 34 | { |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame] | 35 | // LED is blinking. Get the on and off delays and derive percent duty |
Jayashree Dhanapal | e48bf95 | 2022-08-17 17:34:32 +0530 | [diff] [blame] | 36 | auto delayOn = led->getDelayOn(); |
| 37 | uint16_t periodMs = delayOn + led->getDelayOff(); |
Andrew Jeffery | bf0b0a9 | 2018-05-25 15:41:12 +0930 | [diff] [blame] | 38 | auto percentScale = periodMs / 100; |
| 39 | this->dutyOn(delayOn / percentScale); |
tony lee | 9e500aa | 2019-04-23 10:10:24 +0800 | [diff] [blame] | 40 | this->period(periodMs); |
George Liu | 001e2a3 | 2023-12-01 17:30:30 +0800 | [diff] [blame] | 41 | sdbusplus::xyz::openbmc_project::Led::server::Physical::state( |
| 42 | Action::Blink); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 43 | } |
| 44 | else |
| 45 | { |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame] | 46 | // Cache current LED state |
Jayashree Dhanapal | e48bf95 | 2022-08-17 17:34:32 +0530 | [diff] [blame] | 47 | auto brightness = led->getBrightness(); |
Andrew Jeffery | e3719f4 | 2023-02-06 14:28:04 +1030 | [diff] [blame] | 48 | if (brightness != 0U && assert != 0U) |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 49 | { |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame] | 50 | sdbusplus::xyz::openbmc_project::Led::server::Physical::state( |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 51 | Action::On); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 52 | } |
| 53 | else |
| 54 | { |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame] | 55 | sdbusplus::xyz::openbmc_project::Led::server::Physical::state( |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 56 | Action::Off); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 57 | } |
| 58 | } |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 59 | } |
| 60 | |
Vishwanatha Subbanna | db21bc0 | 2021-03-26 00:32:46 -0500 | [diff] [blame] | 61 | auto Physical::state() const -> Action |
| 62 | { |
| 63 | return sdbusplus::xyz::openbmc_project::Led::server::Physical::state(); |
| 64 | } |
| 65 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 66 | auto Physical::state(Action value) -> Action |
| 67 | { |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 68 | auto current = |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame] | 69 | sdbusplus::xyz::openbmc_project::Led::server::Physical::state(); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 70 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 71 | auto requested = |
Andrew Jeffery | 30552c9 | 2018-05-25 15:36:30 +0930 | [diff] [blame] | 72 | sdbusplus::xyz::openbmc_project::Led::server::Physical::state(value); |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 73 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 74 | driveLED(current, requested); |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 75 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 76 | return value; |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 77 | } |
| 78 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 79 | void Physical::driveLED(Action current, Action request) |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 80 | { |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 81 | if (current == request) |
| 82 | { |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 86 | if (request == Action::On || request == Action::Off) |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 87 | { |
| 88 | return stableStateOperation(request); |
| 89 | } |
Andrew Jeffery | 2332e91 | 2018-05-25 15:45:38 +0930 | [diff] [blame] | 90 | |
| 91 | assert(request == Action::Blink); |
| 92 | blinkOperation(); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 93 | } |
| 94 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 95 | void Physical::stableStateOperation(Action action) |
| 96 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 97 | auto value = (action == Action::On) ? assert : deasserted; |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 98 | |
Jayashree Dhanapal | e48bf95 | 2022-08-17 17:34:32 +0530 | [diff] [blame] | 99 | led->setTrigger("none"); |
| 100 | led->setBrightness(value); |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 103 | void Physical::blinkOperation() |
| 104 | { |
tony lee | 9e500aa | 2019-04-23 10:10:24 +0800 | [diff] [blame] | 105 | /* |
| 106 | The configuration of the trigger type must precede the configuration of |
| 107 | the trigger type properties. From the kernel documentation: |
| 108 | "You can change triggers in a similar manner to the way an IO scheduler |
| 109 | is chosen (via /sys/class/leds/<device>/trigger). Trigger specific |
| 110 | parameters can appear in /sys/class/leds/<device> once a given trigger is |
| 111 | selected." |
| 112 | Refer: |
| 113 | https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/leds/leds-class.txt?h=v5.2#n26 |
| 114 | */ |
Andrew Jeffery | 7782f0d | 2023-02-06 14:44:43 +1030 | [diff] [blame] | 115 | auto d = static_cast<unsigned long>(dutyOn()); |
| 116 | if (d > 100) |
| 117 | { |
| 118 | d = 100; |
| 119 | } |
| 120 | |
| 121 | auto p = static_cast<unsigned long>(period()); |
| 122 | |
Jayashree Dhanapal | e48bf95 | 2022-08-17 17:34:32 +0530 | [diff] [blame] | 123 | led->setTrigger("timer"); |
| 124 | led->setDelayOn(p * d / 100UL); |
| 125 | led->setDelayOff(p * (100UL - d) / 100UL); |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 126 | } |
| 127 | |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 128 | /** @brief set led color property in DBus*/ |
| 129 | void Physical::setLedColor(const std::string& color) |
| 130 | { |
| 131 | static const std::string prefix = |
| 132 | "xyz.openbmc_project.Led.Physical.Palette."; |
Andrew Jeffery | 826ba7b | 2023-02-06 14:57:15 +1030 | [diff] [blame] | 133 | |
Andrew Jeffery | 263c056 | 2023-02-06 14:56:16 +1030 | [diff] [blame] | 134 | if (color.empty()) |
Andrew Jeffery | 826ba7b | 2023-02-06 14:57:15 +1030 | [diff] [blame] | 135 | { |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 136 | return; |
Andrew Jeffery | 826ba7b | 2023-02-06 14:57:15 +1030 | [diff] [blame] | 137 | } |
| 138 | |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 139 | std::string tmp = color; |
Andrew Jeffery | dce1e20 | 2023-02-06 17:55:15 +1030 | [diff] [blame] | 140 | tmp[0] = static_cast<char>(toupper(tmp[0])); |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 141 | try |
| 142 | { |
| 143 | auto palette = convertPaletteFromString(prefix + tmp); |
| 144 | setPropertyByName("Color", palette); |
| 145 | } |
| 146 | catch (const sdbusplus::exception::InvalidEnumString&) |
| 147 | { |
| 148 | // if color var contains invalid color, |
| 149 | // Color property will have default value |
| 150 | } |
| 151 | } |
| 152 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 153 | } // namespace led |
| 154 | } // namespace phosphor |