blob: f4ef8a1eeb7ec57964331d083e6e9d6d5b001b56 [file] [log] [blame]
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +05301/**
Andrew Jeffery30552c92018-05-25 15:36:30 +09302 * Copyright © 2016,2018 IBM Corporation
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +05303 *
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 Jefferyc41bf5b2018-05-25 16:39:22 +093017#include "physical.hpp"
18
Andrew Jeffery2332e912018-05-25 15:45:38 +093019#include <cassert>
Alexander Soldatov97ddb722019-04-16 09:10:00 +030020#include <cstdlib>
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053021#include <iostream>
22#include <string>
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053023namespace phosphor
24{
25namespace led
26{
27
28/** @brief Populates key parameters */
29void Physical::setInitialState()
30{
Andrew Jeffery5b1417b2019-03-18 17:20:37 +103031 assert = led.getMaxBrightness();
Andrew Jeffery42e02d32018-05-24 13:34:05 +093032 auto trigger = led.getTrigger();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053033 if (trigger == "timer")
34 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093035 // LED is blinking. Get the on and off delays and derive percent duty
Andrew Jeffery42e02d32018-05-24 13:34:05 +093036 auto delayOn = led.getDelayOn();
tony lee9e500aa2019-04-23 10:10:24 +080037 uint16_t periodMs = delayOn + led.getDelayOff();
Andrew Jefferybf0b0a92018-05-25 15:41:12 +093038 auto percentScale = periodMs / 100;
39 this->dutyOn(delayOn / percentScale);
tony lee9e500aa2019-04-23 10:10:24 +080040 this->period(periodMs);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053041 }
42 else
43 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093044 // Cache current LED state
Andrew Jeffery42e02d32018-05-24 13:34:05 +093045 auto brightness = led.getBrightness();
Vishwanatha Subbannaa48f76d2021-03-26 00:33:07 -050046 if (brightness && assert)
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053047 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093048 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093049 Action::On);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053050 }
51 else
52 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093053 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093054 Action::Off);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053055 }
56 }
57 return;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053058}
59
Vishwanatha Subbannadb21bc02021-03-26 00:32:46 -050060auto Physical::state() const -> Action
61{
62 return sdbusplus::xyz::openbmc_project::Led::server::Physical::state();
63}
64
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053065auto Physical::state(Action value) -> Action
66{
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093067 auto current =
Andrew Jeffery30552c92018-05-25 15:36:30 +093068 sdbusplus::xyz::openbmc_project::Led::server::Physical::state();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053069
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093070 auto requested =
Andrew Jeffery30552c92018-05-25 15:36:30 +093071 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(value);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053072
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053073 driveLED(current, requested);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053074
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053075 return value;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053076}
77
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053078void Physical::driveLED(Action current, Action request)
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053079{
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053080 if (current == request)
81 {
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053082 return;
83 }
84
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093085 if (request == Action::On || request == Action::Off)
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053086 {
87 return stableStateOperation(request);
88 }
Andrew Jeffery2332e912018-05-25 15:45:38 +093089
90 assert(request == Action::Blink);
91 blinkOperation();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053092}
93
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053094void Physical::stableStateOperation(Action action)
95{
Andrew Jeffery5b1417b2019-03-18 17:20:37 +103096 auto value = (action == Action::On) ? assert : DEASSERT;
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053097
Andrew Jeffery42e02d32018-05-24 13:34:05 +093098 led.setTrigger("none");
Andrew Jeffery42e02d32018-05-24 13:34:05 +093099 led.setBrightness(value);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530100 return;
101}
102
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530103void Physical::blinkOperation()
104{
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530105 auto dutyOn = this->dutyOn();
106
tony lee9e500aa2019-04-23 10:10:24 +0800107 /*
108 The configuration of the trigger type must precede the configuration of
109 the trigger type properties. From the kernel documentation:
110 "You can change triggers in a similar manner to the way an IO scheduler
111 is chosen (via /sys/class/leds/<device>/trigger). Trigger specific
112 parameters can appear in /sys/class/leds/<device> once a given trigger is
113 selected."
114 Refer:
115 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/leds/leds-class.txt?h=v5.2#n26
116 */
117 led.setTrigger("timer");
Andrew Jeffery30552c92018-05-25 15:36:30 +0930118 // Convert percent duty to milliseconds for sysfs interface
tony lee9e500aa2019-04-23 10:10:24 +0800119 auto factor = this->period() / 100.0;
Andrew Jefferye5c40fe2018-05-25 15:27:18 +0930120 led.setDelayOn(dutyOn * factor);
Andrew Jefferye5c40fe2018-05-25 15:27:18 +0930121 led.setDelayOff((100 - dutyOn) * factor);
Andrew Jeffery30552c92018-05-25 15:36:30 +0930122
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530123 return;
124}
125
Alexander Soldatov97ddb722019-04-16 09:10:00 +0300126/** @brief set led color property in DBus*/
127void Physical::setLedColor(const std::string& color)
128{
129 static const std::string prefix =
130 "xyz.openbmc_project.Led.Physical.Palette.";
131 if (!color.length())
132 return;
133 std::string tmp = color;
134 tmp[0] = toupper(tmp[0]);
135 try
136 {
137 auto palette = convertPaletteFromString(prefix + tmp);
138 setPropertyByName("Color", palette);
139 }
140 catch (const sdbusplus::exception::InvalidEnumString&)
141 {
142 // if color var contains invalid color,
143 // Color property will have default value
144 }
145}
146
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530147} // namespace led
148} // namespace phosphor