| /* |
| * Copyright 2015 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #include "ledstatus.h" |
| |
| #include <string> |
| |
| #include <base/files/file_path.h> |
| #include <base/format_macros.h> |
| #include <base/logging.h> |
| #include <base/strings/stringprintf.h> |
| #include <base/strings/string_number_conversions.h> |
| #include <base/strings/string_util.h> |
| #include <chromeos/streams/file_stream.h> |
| #include <chromeos/streams/stream_utils.h> |
| |
| namespace { |
| |
| chromeos::StreamPtr GetLEDDataStream(size_t index, bool write) { |
| CHECK(index < LedStatus::num_leds); |
| std::string led_path; |
| if( index == 3 ) { |
| led_path = "/sys/class/leds/boot/brightness"; |
| } else { |
| led_path = base::StringPrintf("/sys/class/leds/led%" PRIuS "/brightness", |
| index + 1); |
| } |
| base::FilePath dev_path{led_path}; |
| auto access_mode = chromeos::stream_utils::MakeAccessMode(!write, write); |
| return chromeos::FileStream::Open( |
| dev_path, access_mode, chromeos::FileStream::Disposition::OPEN_EXISTING, |
| nullptr); |
| } |
| |
| } // anonymous namespace |
| |
| std::vector<bool> LedStatus::GetStatus() const { |
| std::vector<bool> leds(num_leds); |
| for (size_t index = 0; index < num_leds; index++) |
| leds[index] = IsLedOn(index); |
| return leds; |
| } |
| |
| bool LedStatus::IsLedOn(size_t index) const { |
| chromeos::StreamPtr stream = GetLEDDataStream(index, false); |
| if (!stream) |
| return false; |
| |
| char buffer[10]; |
| size_t size_read = 0; |
| if (!stream->ReadNonBlocking(buffer, sizeof(buffer), &size_read, nullptr, |
| nullptr)) { |
| return false; |
| } |
| |
| std::string value{buffer, size_read}; |
| base::TrimWhitespaceASCII(value, base::TrimPositions::TRIM_ALL, &value); |
| int brightness = 0; |
| if (!base::StringToInt(value, &brightness)) |
| return false; |
| return brightness > 0; |
| } |
| |
| void LedStatus::SetLedStatus(size_t index, bool on) { |
| chromeos::StreamPtr stream = GetLEDDataStream(index, true); |
| if (!stream) |
| return; |
| |
| std::string brightness = on ? "255" : "0"; |
| stream->WriteAllBlocking(brightness.data(), brightness.size(), nullptr); |
| } |