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 | |
Alexander Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 19 | #include "phosphor-logging/lg2.hpp" |
| 20 | |
Alexander Hansen | a16bef7 | 2024-08-01 18:31:23 +0200 | [diff] [blame] | 21 | #include <cstring> |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 22 | #include <fstream> |
Alexander Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 23 | #include <optional> |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 24 | #include <string> |
Alexander Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 25 | #include <vector> |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 26 | |
George Liu | 45eba6f | 2021-05-18 14:32:20 +0800 | [diff] [blame] | 27 | namespace fs = std::filesystem; |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 28 | |
| 29 | namespace phosphor |
| 30 | { |
| 31 | namespace led |
| 32 | { |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 33 | template <typename T> |
Andrew Jeffery | 36a8e84 | 2023-02-06 19:37:40 +1030 | [diff] [blame] | 34 | T getSysfsAttr(const fs::path& path); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 35 | |
| 36 | template <> |
Andrew Jeffery | 36a8e84 | 2023-02-06 19:37:40 +1030 | [diff] [blame] | 37 | std::string getSysfsAttr(const fs::path& path) |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 38 | { |
| 39 | std::string content; |
| 40 | std::ifstream file(path); |
George Liu | 001e2a3 | 2023-12-01 17:30:30 +0800 | [diff] [blame] | 41 | std::getline(file, content); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 42 | return content; |
| 43 | } |
| 44 | |
| 45 | template <> |
Andrew Jeffery | 36a8e84 | 2023-02-06 19:37:40 +1030 | [diff] [blame] | 46 | unsigned long getSysfsAttr(const fs::path& path) |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 47 | { |
Andrew Jeffery | cd4e6ca | 2023-02-06 19:43:52 +1030 | [diff] [blame] | 48 | std::string content = getSysfsAttr<std::string>(path); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 49 | return std::strtoul(content.c_str(), nullptr, 0); |
| 50 | } |
| 51 | |
| 52 | template <typename T> |
Andrew Jeffery | 36a8e84 | 2023-02-06 19:37:40 +1030 | [diff] [blame] | 53 | void setSysfsAttr(const fs::path& path, const T& value) |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 54 | { |
| 55 | std::ofstream file(path); |
| 56 | file << value; |
| 57 | } |
| 58 | |
| 59 | unsigned long SysfsLed::getBrightness() |
| 60 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 61 | return getSysfsAttr<unsigned long>(root / attrBrightness); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void SysfsLed::setBrightness(unsigned long brightness) |
| 65 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 66 | setSysfsAttr<unsigned long>(root / attrBrightness, brightness); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | unsigned long SysfsLed::getMaxBrightness() |
| 70 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 71 | return getSysfsAttr<unsigned long>(root / attrMaxBrightness); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | std::string SysfsLed::getTrigger() |
| 75 | { |
George Liu | 001e2a3 | 2023-12-01 17:30:30 +0800 | [diff] [blame] | 76 | // Example content for `/sys/class/leds/<led_name>/trigger`: |
| 77 | // |
| 78 | // * `[none] timer heartbeat default-on` |
| 79 | // * `none [timer] heartbeat default-on` |
| 80 | // |
| 81 | // Refer to: |
| 82 | // |
| 83 | // * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/ABI/testing/sysfs-class-led?h=v6.6#n71 |
| 84 | // * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/ABI/stable/sysfs-block?h=v6.6#n558 |
| 85 | std::string triggerLine = getSysfsAttr<std::string>(root / attrTrigger); |
| 86 | size_t start = triggerLine.find_first_of('['); |
| 87 | size_t end = triggerLine.find_first_of(']'); |
| 88 | if (start >= end || start == std::string::npos || end == std::string::npos) |
| 89 | { |
| 90 | return "none"; |
| 91 | } |
| 92 | |
| 93 | std::string rc = triggerLine.substr(start + 1, end - start - 1); |
| 94 | if (rc.empty()) |
| 95 | { |
| 96 | return "none"; |
| 97 | } |
| 98 | |
| 99 | return rc; |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void SysfsLed::setTrigger(const std::string& trigger) |
| 103 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 104 | setSysfsAttr<std::string>(root / attrTrigger, trigger); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | unsigned long SysfsLed::getDelayOn() |
| 108 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 109 | return getSysfsAttr<unsigned long>(root / attrDelayOn); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void SysfsLed::setDelayOn(unsigned long ms) |
| 113 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 114 | setSysfsAttr<unsigned long>(root / attrDelayOn, ms); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | unsigned long SysfsLed::getDelayOff() |
| 118 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 119 | return getSysfsAttr<unsigned long>(root / attrDelayOff); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void SysfsLed::setDelayOff(unsigned long ms) |
| 123 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 124 | setSysfsAttr<unsigned long>(root / attrDelayOff, ms); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 125 | } |
Alexander Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 126 | |
| 127 | /* LED sysfs name can be any of |
| 128 | * |
| 129 | * - devicename:color:function |
| 130 | * - devicename::function |
| 131 | * - color:function (e.g. "red:fault") |
| 132 | * - label (e.g. "identify") |
| 133 | * - :function (e.g. ":boot") |
| 134 | * - color: (e.g. "green:") |
| 135 | * |
| 136 | * but no one prevents us from making all of this up and creating |
| 137 | * a label with colons inside, e.g. "mylabel:mynoncolorstring:lala". |
| 138 | * |
| 139 | * Reference: https://www.kernel.org/doc/html/latest/leds/leds-class.html |
| 140 | * |
| 141 | * Summary: It's bonkers (not my words, but describes it well) |
| 142 | */ |
| 143 | LedDescr SysfsLed::getLedDescr() |
| 144 | { |
| 145 | std::string name = std::string(root).substr(strlen(devParent)); |
| 146 | LedDescr ledDescr; |
| 147 | |
| 148 | std::vector<std::optional<std::string>> words; |
| 149 | std::stringstream ss(name); |
| 150 | std::string item; |
| 151 | |
| 152 | while (std::getline(ss, item, ':')) |
| 153 | { |
| 154 | if (item.empty()) |
| 155 | { |
| 156 | words.emplace_back(std::nullopt); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | words.emplace_back(item); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if (name.ends_with(":")) |
| 165 | { |
| 166 | words.emplace_back(std::nullopt); |
| 167 | } |
| 168 | |
| 169 | if (name.empty()) |
| 170 | { |
| 171 | lg2::warning("LED description '{DESC}' was empty", "DESC", name); |
| 172 | throw std::out_of_range("expected non-empty LED name"); |
| 173 | } |
| 174 | |
| 175 | if (words.size() != 3) |
| 176 | { |
| 177 | lg2::warning( |
| 178 | "LED description '{DESC}' not well formed, expected 3 parts but got {NPARTS}", |
| 179 | "DESC", name, "NPARTS", words.size()); |
| 180 | } |
| 181 | |
| 182 | switch (words.size()) |
| 183 | { |
| 184 | default: |
| 185 | case 3: |
| 186 | ledDescr.function = words.at(2); |
| 187 | ledDescr.color = words.at(1); |
| 188 | ledDescr.devicename = words.at(0); |
| 189 | break; |
| 190 | case 2: |
| 191 | ledDescr.color = words.at(0); |
| 192 | ledDescr.function = words.at(1); |
| 193 | break; |
| 194 | case 1: |
| 195 | ledDescr.devicename = words.at(0); |
| 196 | break; |
| 197 | case 0: |
| 198 | throw std::out_of_range("expected non-empty LED name"); |
| 199 | } |
| 200 | |
| 201 | // if there is more than 3 parts we ignore the rest |
| 202 | return ledDescr; |
| 203 | } |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 204 | } // namespace led |
| 205 | } // namespace phosphor |