blob: ac8bb255524a37eb602c08b8ce7882ae02a49524 [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();
Andrew Jefferybf0b0a92018-05-25 15:41:12 +093037 periodMs = delayOn + led.getDelayOff();
38 auto percentScale = periodMs / 100;
39 this->dutyOn(delayOn / percentScale);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053040 }
41 else
42 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093043 // TODO: Periodicity is hardcoded for now. This will be changed to
44 // this->period() when configurable periodicity is implemented.
Andrew Jefferye5c40fe2018-05-25 15:27:18 +093045 periodMs = 1000;
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053046
Andrew Jeffery30552c92018-05-25 15:36:30 +093047 // Cache current LED state
Andrew Jeffery42e02d32018-05-24 13:34:05 +093048 auto brightness = led.getBrightness();
Andrew Jeffery5b1417b2019-03-18 17:20:37 +103049 if (brightness == assert)
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053050 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093051 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093052 Action::On);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053053 }
54 else
55 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093056 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093057 Action::Off);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053058 }
59 }
60 return;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053061}
62
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053063auto Physical::state(Action value) -> Action
64{
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093065 auto current =
Andrew Jeffery30552c92018-05-25 15:36:30 +093066 sdbusplus::xyz::openbmc_project::Led::server::Physical::state();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053067
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093068 auto requested =
Andrew Jeffery30552c92018-05-25 15:36:30 +093069 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(value);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053070
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053071 driveLED(current, requested);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053072
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053073 return value;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053074}
75
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053076void Physical::driveLED(Action current, Action request)
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053077{
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053078 if (current == request)
79 {
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053080 return;
81 }
82
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093083 if (request == Action::On || request == Action::Off)
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053084 {
85 return stableStateOperation(request);
86 }
Andrew Jeffery2332e912018-05-25 15:45:38 +093087
88 assert(request == Action::Blink);
89 blinkOperation();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053090}
91
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053092void Physical::stableStateOperation(Action action)
93{
Andrew Jeffery5b1417b2019-03-18 17:20:37 +103094 auto value = (action == Action::On) ? assert : DEASSERT;
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053095
Andrew Jeffery42e02d32018-05-24 13:34:05 +093096 led.setTrigger("none");
Andrew Jeffery42e02d32018-05-24 13:34:05 +093097 led.setBrightness(value);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053098 return;
99}
100
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530101void Physical::blinkOperation()
102{
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530103 auto dutyOn = this->dutyOn();
104
Andrew Jeffery30552c92018-05-25 15:36:30 +0930105 // Convert percent duty to milliseconds for sysfs interface
Andrew Jefferye5c40fe2018-05-25 15:27:18 +0930106 auto factor = periodMs / 100;
107 led.setDelayOn(dutyOn * factor);
Andrew Jefferye5c40fe2018-05-25 15:27:18 +0930108 led.setDelayOff((100 - dutyOn) * factor);
Andrew Jeffery30552c92018-05-25 15:36:30 +0930109
110 led.setTrigger("timer");
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530111 return;
112}
113
Alexander Soldatov97ddb722019-04-16 09:10:00 +0300114/** @brief set led color property in DBus*/
115void Physical::setLedColor(const std::string& color)
116{
117 static const std::string prefix =
118 "xyz.openbmc_project.Led.Physical.Palette.";
119 if (!color.length())
120 return;
121 std::string tmp = color;
122 tmp[0] = toupper(tmp[0]);
123 try
124 {
125 auto palette = convertPaletteFromString(prefix + tmp);
126 setPropertyByName("Color", palette);
127 }
128 catch (const sdbusplus::exception::InvalidEnumString&)
129 {
130 // if color var contains invalid color,
131 // Color property will have default value
132 }
133}
134
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530135} // namespace led
136} // namespace phosphor