Ruchi Kandoi | 8214a64 | 2016-09-29 13:56:05 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define LOG_TAG "android.hardware.power@1.0-impl" |
| 18 | #include <hardware/hardware.h> |
| 19 | #include <hardware/power.h> |
| 20 | #include "Power.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace hardware { |
| 24 | namespace power { |
| 25 | namespace V1_0 { |
| 26 | namespace implementation { |
| 27 | |
| 28 | Power::Power(power_module_t *module) : mModule(module) { |
| 29 | if (mModule) |
| 30 | mModule->init(mModule); |
| 31 | } |
| 32 | |
| 33 | Power::~Power() { |
| 34 | delete(mModule); |
| 35 | } |
| 36 | |
| 37 | // Methods from ::android::hardware::power::V1_0::IPower follow. |
| 38 | Return<void> Power::setInteractive(bool interactive) { |
| 39 | if (mModule->setInteractive > 0) |
| 40 | mModule->setInteractive(mModule, interactive ? 1 : 0); |
| 41 | return Void(); |
| 42 | } |
| 43 | |
| 44 | Return<void> Power::powerHint(PowerHint hint, int32_t data) { |
| 45 | int32_t param = data; |
| 46 | if (mModule->powerHint > 0) |
| 47 | mModule->powerHint(mModule, static_cast<power_hint_t>(hint), ¶m); |
| 48 | return Void(); |
| 49 | } |
| 50 | |
| 51 | Return<void> Power::setFeature(Feature feature, bool activate) { |
| 52 | if (mModule->setFeature > 0) |
| 53 | mModule->setFeature(mModule, static_cast<feature_t>(feature), |
| 54 | activate ? 1 : 0); |
| 55 | return Void(); |
| 56 | } |
| 57 | |
| 58 | Return<void> Power::getPlatformLowPowerStats(getPlatformLowPowerStats_cb _hidl_cb) { |
| 59 | hidl_vec<PowerStatePlatformSleepState> states; |
| 60 | ssize_t number_platform_modes; |
| 61 | size_t *voters = nullptr; |
| 62 | power_state_platform_sleep_state_t *legacy_states = nullptr; |
| 63 | int ret; |
| 64 | |
| 65 | if (mModule->get_number_of_platform_modes == nullptr || |
| 66 | mModule->get_voter_list == nullptr || |
| 67 | mModule->get_platform_low_power_stats == nullptr) |
| 68 | { |
| 69 | _hidl_cb(states, Status::SUCCESS); |
| 70 | return Void(); |
| 71 | } |
| 72 | |
| 73 | number_platform_modes = mModule->get_number_of_platform_modes(mModule); |
| 74 | if (number_platform_modes > 0) |
| 75 | { |
| 76 | voters = new size_t [number_platform_modes]; |
| 77 | if (voters == nullptr) |
| 78 | goto done; |
| 79 | |
| 80 | ret = mModule->get_voter_list(mModule, voters); |
| 81 | if (ret != 0) |
| 82 | goto done; |
| 83 | |
| 84 | legacy_states = new power_state_platform_sleep_state_t [number_platform_modes]; |
| 85 | if (legacy_states == nullptr) |
| 86 | goto done; |
| 87 | |
| 88 | for (int i = 0; i < number_platform_modes; i++) |
| 89 | { |
| 90 | legacy_states[i].voters = nullptr; |
| 91 | legacy_states[i].voters = new power_state_voter_t [voters[i]]; |
| 92 | if (legacy_states[i].voters == nullptr) |
| 93 | goto done; |
| 94 | } |
| 95 | |
| 96 | ret = mModule->get_platform_low_power_stats(mModule, legacy_states); |
| 97 | if (ret != 0) |
| 98 | goto done; |
| 99 | |
| 100 | states.resize(number_platform_modes); |
| 101 | for (int i = 0; i < number_platform_modes; i++) |
| 102 | { |
| 103 | power_state_platform_sleep_state_t& legacy_state = legacy_states[i]; |
| 104 | PowerStatePlatformSleepState& state = states[i]; |
| 105 | state.name = legacy_state.name; |
| 106 | state.residencyInMsecSinceBoot = legacy_state.residency_in_msec_since_boot; |
| 107 | state.totalTransitions = legacy_state.total_transitions; |
| 108 | state.supportedOnlyInSuspend = legacy_state.supported_only_in_suspend; |
| 109 | state.voters.resize(voters[i]); |
| 110 | for(size_t j = 0; j < voters[i]; j++) |
| 111 | { |
| 112 | state.voters[j].name = legacy_state.voters[j].name; |
| 113 | state.voters[j].totalTimeInMsecVotedForSinceBoot = legacy_state.voters[j].total_time_in_msec_voted_for_since_boot; |
| 114 | state.voters[j].totalNumberOfTimesVotedSinceBoot = legacy_state.voters[j].total_number_of_times_voted_since_boot; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | done: |
| 119 | if (legacy_states) |
| 120 | { |
| 121 | for (int i = 0; i < number_platform_modes; i++) |
| 122 | { |
| 123 | if(legacy_states[i].voters) |
| 124 | delete(legacy_states[i].voters); |
| 125 | } |
| 126 | } |
| 127 | delete[] legacy_states; |
| 128 | delete[] voters; |
| 129 | _hidl_cb(states, Status::SUCCESS); |
| 130 | return Void(); |
| 131 | } |
| 132 | |
| 133 | IPower* HIDL_FETCH_IPower(const char* name) { |
| 134 | int ret = 0; |
| 135 | const hw_module_t* hw_module = NULL; |
| 136 | power_module_t *power_module; |
| 137 | ret = hw_get_module(name, &hw_module); |
| 138 | if (ret == 0 && hw_module->methods->open > 0) { |
| 139 | ret = hw_module->methods->open(hw_module, name, |
| 140 | reinterpret_cast<hw_device_t**>(&power_module)); |
| 141 | if (ret == 0) { |
| 142 | return new Power(power_module); |
| 143 | } |
| 144 | else { |
| 145 | ALOGE("Passthrough failed to load legacy HAL."); |
| 146 | return nullptr; |
| 147 | } |
| 148 | } |
| 149 | else { |
| 150 | ALOGE ("hw_get_module %s failed: %d", name, ret); |
| 151 | return nullptr; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | } // namespace implementation |
| 156 | } // namespace V1_0 |
| 157 | } // namespace power |
| 158 | } // namespace hardware |
| 159 | } // namespace android |