blob: cb22aedbfc4eba0dc6e7fc9c3434beb517caf58b [file] [log] [blame]
Joeyc6e0e422019-01-09 14:05:01 +01001/*
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
22namespace vendor {
23namespace lineage {
24namespace trust {
25namespace V1_0 {
26namespace implementation {
27
28static constexpr const char* kControlPath = "/proc/sys/kernel/deny_new_usb";
29
30// Methods from ::vendor::lineage::trust::V1_0::IUsbRestrict follow.
31Return<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
39Return<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