blob: ae9ff1c3f077ddc9d26928b1cf1a46d62b6a0e64 [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>
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053020#include <iostream>
21#include <string>
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053022namespace phosphor
23{
24namespace led
25{
26
27/** @brief Populates key parameters */
28void Physical::setInitialState()
29{
Andrew Jeffery42e02d32018-05-24 13:34:05 +093030 auto trigger = led.getTrigger();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053031 if (trigger == "timer")
32 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093033 // LED is blinking. Get the on and off delays and derive percent duty
Andrew Jeffery42e02d32018-05-24 13:34:05 +093034 auto delayOn = led.getDelayOn();
Andrew Jefferybf0b0a92018-05-25 15:41:12 +093035 periodMs = delayOn + led.getDelayOff();
36 auto percentScale = periodMs / 100;
37 this->dutyOn(delayOn / percentScale);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053038 }
39 else
40 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093041 // TODO: Periodicity is hardcoded for now. This will be changed to
42 // this->period() when configurable periodicity is implemented.
Andrew Jefferye5c40fe2018-05-25 15:27:18 +093043 periodMs = 1000;
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053044
Andrew Jeffery30552c92018-05-25 15:36:30 +093045 // Cache current LED state
Andrew Jeffery42e02d32018-05-24 13:34:05 +093046 auto brightness = led.getBrightness();
47 if (brightness == ASSERT)
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053048 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093049 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093050 Action::On);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053051 }
52 else
53 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093054 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093055 Action::Off);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053056 }
57 }
58 return;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053059}
60
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053061auto Physical::state(Action value) -> Action
62{
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093063 auto current =
Andrew Jeffery30552c92018-05-25 15:36:30 +093064 sdbusplus::xyz::openbmc_project::Led::server::Physical::state();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053065
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093066 auto requested =
Andrew Jeffery30552c92018-05-25 15:36:30 +093067 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(value);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053068
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053069 driveLED(current, requested);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053070
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053071 return value;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053072}
73
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053074void Physical::driveLED(Action current, Action request)
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053075{
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053076 if (current == request)
77 {
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053078 return;
79 }
80
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093081 if (request == Action::On || request == Action::Off)
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053082 {
83 return stableStateOperation(request);
84 }
Andrew Jeffery2332e912018-05-25 15:45:38 +093085
86 assert(request == Action::Blink);
87 blinkOperation();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053088}
89
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053090void Physical::stableStateOperation(Action action)
91{
92 auto value = (action == Action::On) ? ASSERT : DEASSERT;
93
Andrew Jeffery42e02d32018-05-24 13:34:05 +093094 led.setTrigger("none");
Andrew Jeffery42e02d32018-05-24 13:34:05 +093095 led.setBrightness(value);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053096 return;
97}
98
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053099void Physical::blinkOperation()
100{
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530101 auto dutyOn = this->dutyOn();
102
Andrew Jeffery30552c92018-05-25 15:36:30 +0930103 // Convert percent duty to milliseconds for sysfs interface
Andrew Jefferye5c40fe2018-05-25 15:27:18 +0930104 auto factor = periodMs / 100;
105 led.setDelayOn(dutyOn * factor);
Andrew Jefferye5c40fe2018-05-25 15:27:18 +0930106 led.setDelayOff((100 - dutyOn) * factor);
Andrew Jeffery30552c92018-05-25 15:36:30 +0930107
108 led.setTrigger("timer");
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530109 return;
110}
111
112} // namespace led
113} // namespace phosphor