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