Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 1 | /** |
| 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 Liu | 45eba6f | 2021-05-18 14:32:20 +0800 | [diff] [blame] | 23 | namespace fs = std::filesystem; |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 24 | |
| 25 | namespace phosphor |
| 26 | { |
| 27 | namespace led |
| 28 | { |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 29 | template <typename T> |
Andrew Jeffery | 36a8e84 | 2023-02-06 19:37:40 +1030 | [diff] [blame] | 30 | T getSysfsAttr(const fs::path& path); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 31 | |
| 32 | template <> |
Andrew Jeffery | 36a8e84 | 2023-02-06 19:37:40 +1030 | [diff] [blame] | 33 | std::string getSysfsAttr(const fs::path& path) |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 34 | { |
| 35 | std::string content; |
| 36 | std::ifstream file(path); |
| 37 | file >> content; |
| 38 | return content; |
| 39 | } |
| 40 | |
| 41 | template <> |
Andrew Jeffery | 36a8e84 | 2023-02-06 19:37:40 +1030 | [diff] [blame] | 42 | unsigned long getSysfsAttr(const fs::path& path) |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 43 | { |
Andrew Jeffery | cd4e6ca | 2023-02-06 19:43:52 +1030 | [diff] [blame] | 44 | std::string content = getSysfsAttr<std::string>(path); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 45 | return std::strtoul(content.c_str(), nullptr, 0); |
| 46 | } |
| 47 | |
| 48 | template <typename T> |
Andrew Jeffery | 36a8e84 | 2023-02-06 19:37:40 +1030 | [diff] [blame] | 49 | void setSysfsAttr(const fs::path& path, const T& value) |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 50 | { |
| 51 | std::ofstream file(path); |
| 52 | file << value; |
| 53 | } |
| 54 | |
| 55 | unsigned long SysfsLed::getBrightness() |
| 56 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 57 | return getSysfsAttr<unsigned long>(root / attrBrightness); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void SysfsLed::setBrightness(unsigned long brightness) |
| 61 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 62 | setSysfsAttr<unsigned long>(root / attrBrightness, brightness); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | unsigned long SysfsLed::getMaxBrightness() |
| 66 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 67 | return getSysfsAttr<unsigned long>(root / attrMaxBrightness); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | std::string SysfsLed::getTrigger() |
| 71 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 72 | return getSysfsAttr<std::string>(root / attrTrigger); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void SysfsLed::setTrigger(const std::string& trigger) |
| 76 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 77 | setSysfsAttr<std::string>(root / attrTrigger, trigger); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | unsigned long SysfsLed::getDelayOn() |
| 81 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 82 | return getSysfsAttr<unsigned long>(root / attrDelayOn); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void SysfsLed::setDelayOn(unsigned long ms) |
| 86 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 87 | setSysfsAttr<unsigned long>(root / attrDelayOn, ms); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | unsigned long SysfsLed::getDelayOff() |
| 91 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 92 | return getSysfsAttr<unsigned long>(root / attrDelayOff); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void SysfsLed::setDelayOff(unsigned long ms) |
| 96 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 97 | setSysfsAttr<unsigned long>(root / attrDelayOff, ms); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 98 | } |
| 99 | } // namespace led |
| 100 | } // namespace phosphor |