blob: e614b997e874dd7150bde4f8c1ddd629461567f9 [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 Jeffery5b1417b2019-03-18 17:20:37 +103030 assert = led.getMaxBrightness();
Andrew Jeffery42e02d32018-05-24 13:34:05 +093031 auto trigger = led.getTrigger();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053032 if (trigger == "timer")
33 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093034 // LED is blinking. Get the on and off delays and derive percent duty
Andrew Jeffery42e02d32018-05-24 13:34:05 +093035 auto delayOn = led.getDelayOn();
Andrew Jefferybf0b0a92018-05-25 15:41:12 +093036 periodMs = delayOn + led.getDelayOff();
37 auto percentScale = periodMs / 100;
38 this->dutyOn(delayOn / percentScale);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053039 }
40 else
41 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093042 // TODO: Periodicity is hardcoded for now. This will be changed to
43 // this->period() when configurable periodicity is implemented.
Andrew Jefferye5c40fe2018-05-25 15:27:18 +093044 periodMs = 1000;
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053045
Andrew Jeffery30552c92018-05-25 15:36:30 +093046 // Cache current LED state
Andrew Jeffery42e02d32018-05-24 13:34:05 +093047 auto brightness = led.getBrightness();
Andrew Jeffery5b1417b2019-03-18 17:20:37 +103048 if (brightness == assert)
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053049 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093050 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093051 Action::On);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053052 }
53 else
54 {
Andrew Jeffery30552c92018-05-25 15:36:30 +093055 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093056 Action::Off);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053057 }
58 }
59 return;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053060}
61
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053062auto Physical::state(Action value) -> Action
63{
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093064 auto current =
Andrew Jeffery30552c92018-05-25 15:36:30 +093065 sdbusplus::xyz::openbmc_project::Led::server::Physical::state();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053066
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093067 auto requested =
Andrew Jeffery30552c92018-05-25 15:36:30 +093068 sdbusplus::xyz::openbmc_project::Led::server::Physical::state(value);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053069
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053070 driveLED(current, requested);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053071
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053072 return value;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053073}
74
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053075void Physical::driveLED(Action current, Action request)
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053076{
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053077 if (current == request)
78 {
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053079 return;
80 }
81
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093082 if (request == Action::On || request == Action::Off)
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053083 {
84 return stableStateOperation(request);
85 }
Andrew Jeffery2332e912018-05-25 15:45:38 +093086
87 assert(request == Action::Blink);
88 blinkOperation();
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053089}
90
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053091void Physical::stableStateOperation(Action action)
92{
Andrew Jeffery5b1417b2019-03-18 17:20:37 +103093 auto value = (action == Action::On) ? assert : DEASSERT;
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053094
Andrew Jeffery42e02d32018-05-24 13:34:05 +093095 led.setTrigger("none");
Andrew Jeffery42e02d32018-05-24 13:34:05 +093096 led.setBrightness(value);
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053097 return;
98}
99
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530100void Physical::blinkOperation()
101{
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530102 auto dutyOn = this->dutyOn();
103
Andrew Jeffery30552c92018-05-25 15:36:30 +0930104 // Convert percent duty to milliseconds for sysfs interface
Andrew Jefferye5c40fe2018-05-25 15:27:18 +0930105 auto factor = periodMs / 100;
106 led.setDelayOn(dutyOn * factor);
Andrew Jefferye5c40fe2018-05-25 15:27:18 +0930107 led.setDelayOff((100 - dutyOn) * factor);
Andrew Jeffery30552c92018-05-25 15:36:30 +0930108
109 led.setTrigger("timer");
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530110 return;
111}
112
113} // namespace led
114} // namespace phosphor