Joey | c6e0e42 | 2019-01-09 14:05:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include <fstream> |
| 17 | |
| 18 | #include "UsbRestrict.h" |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | |
| 22 | namespace vendor { |
| 23 | namespace lineage { |
| 24 | namespace trust { |
| 25 | namespace V1_0 { |
| 26 | namespace implementation { |
| 27 | |
| 28 | static constexpr const char* kControlPath = "/proc/sys/kernel/deny_new_usb"; |
| 29 | |
| 30 | // Methods from ::vendor::lineage::trust::V1_0::IUsbRestrict follow. |
| 31 | Return<bool> UsbRestrict::isEnabled() { |
| 32 | std::ifstream file(kControlPath); |
| 33 | std::string content; |
| 34 | file >> content; |
| 35 | file.close(); |
| 36 | return !file.fail() && std::stoi(content); |
| 37 | } |
| 38 | |
| 39 | Return<void> UsbRestrict::setEnabled(bool enabled) { |
| 40 | std::ofstream file(kControlPath); |
| 41 | if (file.is_open()) { |
| 42 | file << (enabled ? "1" : "0"); |
| 43 | file.close(); |
| 44 | } else { |
| 45 | LOG(ERROR) << "Failed to open " << kControlPath << ", error=" << errno |
| 46 | << " (" << strerror(errno) << ")"; |
| 47 | } |
| 48 | return Void(); |
| 49 | } |
| 50 | |
| 51 | } // namespace implementation |
| 52 | } // namespace V1_0 |
| 53 | } // namespace trust |
| 54 | } // namespace lineage |
| 55 | } // namespace vendor |