blob: 2b404b65eaace6ecca0b727765f18af325c17134 [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
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053019#include <iostream>
20#include <string>
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053021namespace phosphor
22{
23namespace led
24{
25
26/** @brief Populates key parameters */
27void Physical::setInitialState()
28{
Andrew Jeffery42e02d32018-05-24 13:34:05 +093029 auto trigger = led.getTrigger();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053030 if (trigger == "timer")
31 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093032 // LED is blinking. Get the on and off delays and derive percent duty
Andrew Jeffery42e02d32018-05-24 13:34:05 +093033 auto delayOn = led.getDelayOn();
34 auto delayOff = led.getDelayOff();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053035
Andrew Jeffery30552c92018-05-25 15:36:30 +093036 // Calculate duty from millisecond delay values and derive percentage on
Andrew Jefferye5c40fe2018-05-25 15:27:18 +093037 periodMs = delayOn + delayOff;
38 auto factor = periodMs / 100;
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053039 auto dutyOn = delayOn / factor;
40
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053041 this->dutyOn(dutyOn);
42 }
43 else
44 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093045 // TODO: Periodicity is hardcoded for now. This will be changed to
46 // this->period() when configurable periodicity is implemented.
Andrew Jefferye5c40fe2018-05-25 15:27:18 +093047 periodMs = 1000;
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053048
Andrew Jeffery30552c92018-05-25 15:36:30 +093049 // Cache current LED state
Andrew Jeffery42e02d32018-05-24 13:34:05 +093050 auto brightness = led.getBrightness();
51 if (brightness == ASSERT)
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053052 {
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::On);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053055 }
56 else
57 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093058 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093059 Action::Off);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053060 }
61 }
62 return;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053063}
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
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053085 if (request == Action::Blink)
86 {
87 return blinkOperation();
88 }
89
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093090 if (request == Action::On || request == Action::Off)
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053091 {
92 return stableStateOperation(request);
93 }
94 return;
95}
96
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053097void Physical::stableStateOperation(Action action)
98{
99 auto value = (action == Action::On) ? ASSERT : DEASSERT;
100
Andrew Jeffery42e02d32018-05-24 13:34:05 +0930101 led.setTrigger("none");
Andrew Jeffery42e02d32018-05-24 13:34:05 +0930102 led.setBrightness(value);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530103 return;
104}
105
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530106void Physical::blinkOperation()
107{
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530108 auto dutyOn = this->dutyOn();
109
Andrew Jeffery30552c92018-05-25 15:36:30 +0930110 // Convert percent duty to milliseconds for sysfs interface
Andrew Jefferye5c40fe2018-05-25 15:27:18 +0930111 auto factor = periodMs / 100;
112 led.setDelayOn(dutyOn * factor);
Andrew Jefferye5c40fe2018-05-25 15:27:18 +0930113 led.setDelayOff((100 - dutyOn) * factor);
Andrew Jeffery30552c92018-05-25 15:36:30 +0930114
115 led.setTrigger("timer");
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530116 return;
117}
118
119} // namespace led
120} // namespace phosphor