1 /*
2 * Copyright (C) 2022 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 <fastbootshim.h>
18
19 using ::android::sp;
20 using ::android::hardware::hidl_string;
21 using ::android::hardware::Void;
22 using ::android::hardware::fastboot::V1_0::FileSystemType;
23 using ::android::hardware::fastboot::V1_0::Result;
24 using ::android::hardware::fastboot::V1_0::Status;
25
26 using ndk::ScopedAStatus;
27
28 namespace aidl {
29 namespace android {
30 namespace hardware {
31 namespace fastboot {
ResultToAStatus(Result result)32 ScopedAStatus ResultToAStatus(Result result) {
33 switch (result.status) {
34 case Status::SUCCESS:
35 return ScopedAStatus::ok();
36 case Status::NOT_SUPPORTED:
37 return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
38 case Status::INVALID_ARGUMENT:
39 return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
40 case Status::FAILURE_UNKNOWN:
41 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
42 BnFastboot::FAILURE_UNKNOWN, ("Error " + std::string(result.message)).c_str());
43 }
44 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
45 BnFastboot::FAILURE_UNKNOWN,
46 ("Unrecognized status value " + toString(result.status)).c_str());
47 }
FastbootShim(const sp<HidlFastboot> & service)48 FastbootShim::FastbootShim(const sp<HidlFastboot>& service) : service_(service) {}
49
getPartitionType(const std::string & in_partitionName,FileSystemType * _aidl_return)50 ScopedAStatus FastbootShim::getPartitionType(const std::string& in_partitionName,
51 FileSystemType* _aidl_return) {
52 Result out_result = {Status::FAILURE_UNKNOWN, ""};
53 if (in_partitionName.empty()) {
54 return ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
55 "Invalid partition name");
56 }
57 const hidl_string partition = in_partitionName;
58 auto ret = service_->getPartitionType(partition, [&](auto type, auto& result) {
59 out_result = result;
60 if (out_result.status != Status::SUCCESS) return;
61 *_aidl_return = static_cast<aidl::android::hardware::fastboot::FileSystemType>(type);
62 });
63 return ResultToAStatus(out_result);
64 }
65
doOemCommand(const std::string & in_oemCmd,std::string * _aidl_return)66 ScopedAStatus FastbootShim::doOemCommand(const std::string& in_oemCmd, std::string* _aidl_return) {
67 Result out_result = {Status::FAILURE_UNKNOWN, ""};
68 *_aidl_return = "";
69 if (in_oemCmd.empty()) {
70 return ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, "Invalid command");
71 }
72 const hidl_string oemCmdArgs = in_oemCmd;
73 auto ret = service_->doOemCommand(oemCmdArgs, [&](auto& result) {
74 out_result = result;
75 if (out_result.status != Status::SUCCESS) return;
76 *_aidl_return = std::string(result.message.c_str());
77 });
78 return ResultToAStatus(out_result);
79 }
80
getVariant(std::string * _aidl_return)81 ScopedAStatus FastbootShim::getVariant(std::string* _aidl_return) {
82 Result out_result = {Status::FAILURE_UNKNOWN, ""};
83 *_aidl_return = "";
84 auto ret = service_->getVariant([&](auto& variant, auto& result) {
85 out_result = result;
86 if (out_result.status != Status::SUCCESS) return;
87 *_aidl_return = std::string(variant.c_str());
88 });
89 return ResultToAStatus(out_result);
90 }
91
getOffModeChargeState(bool * _aidl_return)92 ScopedAStatus FastbootShim::getOffModeChargeState(bool* _aidl_return) {
93 Result out_result = {Status::FAILURE_UNKNOWN, ""};
94 *_aidl_return = false;
95 auto ret = service_->getOffModeChargeState([&](auto state, auto& result) {
96 out_result = result;
97 if (out_result.status != Status::SUCCESS) return;
98 *_aidl_return = state;
99 });
100 return ResultToAStatus(out_result);
101 }
102
getBatteryVoltageFlashingThreshold(int32_t * _aidl_return)103 ScopedAStatus FastbootShim::getBatteryVoltageFlashingThreshold(int32_t* _aidl_return) {
104 Result out_result = {Status::FAILURE_UNKNOWN, ""};
105 *_aidl_return = 0;
106 auto ret = service_->getBatteryVoltageFlashingThreshold([&](auto batteryVoltage, auto& result) {
107 out_result = result;
108 if (out_result.status != Status::SUCCESS) return;
109 *_aidl_return = batteryVoltage;
110 });
111 return ResultToAStatus(out_result);
112 }
113
doOemSpecificErase()114 ScopedAStatus FastbootShim::doOemSpecificErase() {
115 Result out_result = {Status::FAILURE_UNKNOWN, ""};
116 auto ret = service_->doOemSpecificErase([&](auto& result) { out_result = result; });
117 return ResultToAStatus(out_result);
118 }
119
120 } // namespace fastboot
121 } // namespace hardware
122 } // namespace android
123 } // namespace aidl
124