Linux4 | 9426065 | 2020-04-20 17:41:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The LineageOS Project |
| 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 | #define LOG_TAG "powershare@1.0-service.samsung" |
| 18 | |
| 19 | #include "PowerShare.h" |
| 20 | #include <android-base/logging.h> |
| 21 | #include <fstream> |
| 22 | #include <iostream> |
| 23 | #include "samsung_powershare.h" |
| 24 | |
| 25 | namespace vendor { |
| 26 | namespace lineage { |
| 27 | namespace powershare { |
| 28 | namespace V1_0 { |
| 29 | namespace implementation { |
| 30 | |
| 31 | /* |
| 32 | * Write value to path and close file. |
| 33 | */ |
| 34 | template <typename T> |
| 35 | static void set(const std::string& path, const T& value) { |
| 36 | std::ofstream file(path); |
| 37 | |
| 38 | if (!file) { |
| 39 | PLOG(ERROR) << "Failed to open: " << path; |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | LOG(DEBUG) << "write: " << path << " value: " << value; |
| 44 | |
| 45 | file << value << std::endl; |
| 46 | |
| 47 | if (!file) { |
| 48 | PLOG(ERROR) << "Failed to write: " << path << " value: " << value; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | template <typename T> |
| 53 | static T get(const std::string& path, const T& def) { |
| 54 | std::ifstream file(path); |
| 55 | |
| 56 | if (!file) { |
| 57 | PLOG(ERROR) << "Failed to open: " << path; |
| 58 | return def; |
| 59 | } |
| 60 | |
| 61 | T result; |
| 62 | |
| 63 | file >> result; |
| 64 | |
| 65 | if (file.fail()) { |
| 66 | PLOG(ERROR) << "Failed to read: " << path; |
| 67 | return def; |
| 68 | } else { |
| 69 | LOG(DEBUG) << "read: " << path << " value: " << result; |
| 70 | return result; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | Return<bool> PowerShare::isEnabled() { |
| 75 | return get(POWERSHARE_PATH, 0) == 1; |
| 76 | } |
| 77 | |
| 78 | Return<bool> PowerShare::setEnabled(bool enable) { |
| 79 | set(POWERSHARE_PATH, enable ? 1 : 0); |
| 80 | |
| 81 | return isEnabled(); |
| 82 | } |
| 83 | |
| 84 | Return<uint32_t> PowerShare::getMinBattery() { |
| 85 | return get(POWERSHARE_STOP_CAPACITY_PATH, 0); |
| 86 | } |
| 87 | |
| 88 | Return<uint32_t> PowerShare::setMinBattery(uint32_t minBattery) { |
| 89 | set(POWERSHARE_STOP_CAPACITY_PATH, minBattery); |
| 90 | |
| 91 | return getMinBattery(); |
| 92 | } |
| 93 | |
| 94 | } // namespace implementation |
| 95 | } // namespace V1_0 |
| 96 | } // namespace powershare |
| 97 | } // namespace lineage |
| 98 | } // namespace vendor |