1 /*
2  * Copyright (C) 2018 The Android Open Source 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 #include "fastboot/Fastboot.h"
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/strings.h>
26 #include <android-base/unique_fd.h>
27 
28 namespace android {
29 namespace hardware {
30 namespace fastboot {
31 namespace V1_0 {
32 namespace implementation {
33 
34 constexpr const char* BRIGHTNESS_FILE = "/sys/class/backlight/panel0-backlight/brightness";
35 constexpr int DISPLAY_BRIGHTNESS_DIM_THRESHOLD = 20;
36 
37 using  OEMCommandHandler = std::function<Result(const std::vector<std::string>&)>;
38 
getPartitionType(const::android::hardware::hidl_string &,getPartitionType_cb _hidl_cb)39 Return<void> Fastboot::getPartitionType(const ::android::hardware::hidl_string& /* partitionName */,
40                                   getPartitionType_cb _hidl_cb) {
41     // For bluecross devices, all partitions need to return raw.
42     _hidl_cb(FileSystemType::RAW, { Status::SUCCESS, "" });
43     return Void();
44 }
45 
getVariant(getVariant_cb _hidl_cb)46 Return<void> Fastboot::getVariant(getVariant_cb _hidl_cb) {
47     _hidl_cb("MSM USF", {Status::SUCCESS, "" });
48     return Void();
49 }
50 
getOffModeChargeState(getOffModeChargeState_cb _hidl_cb)51 Return<void> Fastboot::getOffModeChargeState(getOffModeChargeState_cb _hidl_cb) {
52     constexpr const char* kDevinfoPath = "/dev/block/by-name/devinfo";
53     constexpr int kDevInfoOffModeChargeOffset = 15;
54 
55     uint8_t off_mode_charge_status = 0;
56     android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(kDevinfoPath,
57                                                         O_RDONLY | O_BINARY)));
58     if (!android::base::ReadFullyAtOffset(fd, &off_mode_charge_status, 1 /* byte count */,
59                                           kDevInfoOffModeChargeOffset)) {
60         _hidl_cb(false,
61                  { Status::FAILURE_UNKNOWN, "Unable to read off-mode-charge state" });
62     } else {
63         _hidl_cb(off_mode_charge_status != 0, { Status::SUCCESS, "" });
64     }
65 
66     return Void();
67 }
68 
getBatteryVoltageFlashingThreshold(getBatteryVoltageFlashingThreshold_cb _hidl_cb)69 Return<void> Fastboot::getBatteryVoltageFlashingThreshold(
70         getBatteryVoltageFlashingThreshold_cb _hidl_cb) {
71     constexpr int kMinVoltageForFlashing = 3500;
72     _hidl_cb(kMinVoltageForFlashing, { Status::SUCCESS, "" });
73     return Void();
74 }
75 
SetBrightnessLevel(const std::vector<std::string> & args)76 Result SetBrightnessLevel(const std::vector<std::string>& args) {
77     if (!args.size()) {
78         return { Status::INVALID_ARGUMENT, "Brightness level unspecified" };
79     }
80 
81     auto level = std::stoi(args[0]);
82 
83     if (level < 0 || level > 100)  {
84         return { Status::INVALID_ARGUMENT, "Brighness level must be between 0 and 100" };
85     }
86 
87     // Avoid screen being dimmed too much.
88     if (level < DISPLAY_BRIGHTNESS_DIM_THRESHOLD) {
89         level = DISPLAY_BRIGHTNESS_DIM_THRESHOLD;
90     }
91 
92     if (android::base::WriteStringToFile(std::to_string(level), BRIGHTNESS_FILE)) {
93             return { Status::SUCCESS, "" };
94     }
95 
96     return { Status::FAILURE_UNKNOWN, "Unable to set display brightness" };
97 }
98 
doOemCommand(const::android::hardware::hidl_string & oemCmdArgs,doOemCommand_cb _hidl_cb)99 Return<void> Fastboot::doOemCommand(const ::android::hardware::hidl_string& oemCmdArgs,
100                           doOemCommand_cb _hidl_cb) {
101     const std::unordered_map<std::string, OEMCommandHandler> kOEMCmdMap = {
102         {FB_OEM_SET_BRIGHTNESS, SetBrightnessLevel},
103     };
104 
105     auto args = android::base::Split(oemCmdArgs, " ");
106     if (args.size() < 2) {
107         _hidl_cb({ Status::INVALID_ARGUMENT, "Invalid OEM command" });
108         return Void();
109     }
110 
111     // args[0] will be "oem", args[1] will be the command name
112     auto cmd_handler = kOEMCmdMap.find(args[1]);
113     if (cmd_handler != kOEMCmdMap.end()) {
114         _hidl_cb(cmd_handler->second(std::vector<std::string>(args.begin() + 2, args.end())));
115     } else {
116         _hidl_cb({ Status::FAILURE_UNKNOWN, "Unknown OEM command" });
117     }
118 
119     return Void();
120 }
121 
Fastboot()122 Fastboot::Fastboot() {}
123 
124 // Methods from ::android::hidl::base::V1_0::IBase follow.
125 
HIDL_FETCH_IFastboot(const char *)126 extern "C" IFastboot* HIDL_FETCH_IFastboot(const char* /* name */) {
127     return new Fastboot();
128 }
129 
130 }  // namespace implementation
131 }  // namespace V1_0
132 }  // namespace fastboot
133 }  // namespace hardware
134 }  // namespace android
135