blob: 277710d92193cf9eff7d2bc3a6dfde7ce0444be7 [file] [log] [blame]
Andrew Jefferye185efb2018-05-24 12:59:06 +09301/**
2 * Copyright © 2019 IBM Corporation
3 *
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
17#include "sysfs.hpp"
18
19#include <fstream>
20#include <iostream>
21#include <string>
22
George Liu45eba6f2021-05-18 14:32:20 +080023namespace fs = std::filesystem;
Andrew Jefferye185efb2018-05-24 12:59:06 +093024
25namespace phosphor
26{
27namespace led
28{
Andrew Jefferye185efb2018-05-24 12:59:06 +093029template <typename T>
Andrew Jeffery36a8e842023-02-06 19:37:40 +103030T getSysfsAttr(const fs::path& path);
Andrew Jefferye185efb2018-05-24 12:59:06 +093031
32template <>
Andrew Jeffery36a8e842023-02-06 19:37:40 +103033std::string getSysfsAttr(const fs::path& path)
Andrew Jefferye185efb2018-05-24 12:59:06 +093034{
35 std::string content;
36 std::ifstream file(path);
37 file >> content;
38 return content;
39}
40
41template <>
Andrew Jeffery36a8e842023-02-06 19:37:40 +103042unsigned long getSysfsAttr(const fs::path& path)
Andrew Jefferye185efb2018-05-24 12:59:06 +093043{
Andrew Jefferycd4e6ca2023-02-06 19:43:52 +103044 std::string content = getSysfsAttr<std::string>(path);
Andrew Jefferye185efb2018-05-24 12:59:06 +093045 return std::strtoul(content.c_str(), nullptr, 0);
46}
47
48template <typename T>
Andrew Jeffery36a8e842023-02-06 19:37:40 +103049void setSysfsAttr(const fs::path& path, const T& value)
Andrew Jefferye185efb2018-05-24 12:59:06 +093050{
51 std::ofstream file(path);
52 file << value;
53}
54
55unsigned long SysfsLed::getBrightness()
56{
Andrew Jeffery8e852282023-02-06 19:29:43 +103057 return getSysfsAttr<unsigned long>(root / attrBrightness);
Andrew Jefferye185efb2018-05-24 12:59:06 +093058}
59
60void SysfsLed::setBrightness(unsigned long brightness)
61{
Andrew Jeffery8e852282023-02-06 19:29:43 +103062 setSysfsAttr<unsigned long>(root / attrBrightness, brightness);
Andrew Jefferye185efb2018-05-24 12:59:06 +093063}
64
65unsigned long SysfsLed::getMaxBrightness()
66{
Andrew Jeffery8e852282023-02-06 19:29:43 +103067 return getSysfsAttr<unsigned long>(root / attrMaxBrightness);
Andrew Jefferye185efb2018-05-24 12:59:06 +093068}
69
70std::string SysfsLed::getTrigger()
71{
Andrew Jeffery8e852282023-02-06 19:29:43 +103072 return getSysfsAttr<std::string>(root / attrTrigger);
Andrew Jefferye185efb2018-05-24 12:59:06 +093073}
74
75void SysfsLed::setTrigger(const std::string& trigger)
76{
Andrew Jeffery8e852282023-02-06 19:29:43 +103077 setSysfsAttr<std::string>(root / attrTrigger, trigger);
Andrew Jefferye185efb2018-05-24 12:59:06 +093078}
79
80unsigned long SysfsLed::getDelayOn()
81{
Andrew Jeffery8e852282023-02-06 19:29:43 +103082 return getSysfsAttr<unsigned long>(root / attrDelayOn);
Andrew Jefferye185efb2018-05-24 12:59:06 +093083}
84
85void SysfsLed::setDelayOn(unsigned long ms)
86{
Andrew Jeffery8e852282023-02-06 19:29:43 +103087 setSysfsAttr<unsigned long>(root / attrDelayOn, ms);
Andrew Jefferye185efb2018-05-24 12:59:06 +093088}
89
90unsigned long SysfsLed::getDelayOff()
91{
Andrew Jeffery8e852282023-02-06 19:29:43 +103092 return getSysfsAttr<unsigned long>(root / attrDelayOff);
Andrew Jefferye185efb2018-05-24 12:59:06 +093093}
94
95void SysfsLed::setDelayOff(unsigned long ms)
96{
Andrew Jeffery8e852282023-02-06 19:29:43 +103097 setSysfsAttr<unsigned long>(root / attrDelayOff, ms);
Andrew Jefferye185efb2018-05-24 12:59:06 +093098}
99} // namespace led
100} // namespace phosphor