blob: 36d00553242382497e1f3dff1a8352d5feb698a4 [file] [log] [blame]
Wei Wang61c2a332020-01-08 16:51:47 -08001/*
2 * Copyright (C) 2020 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 "Power.h"
Peiyong Lin5851c3a2022-10-18 20:47:56 +000018#include "PowerHintSession.h"
Wei Wang61c2a332020-01-08 16:51:47 -080019
20#include <android-base/logging.h>
Matt Buckleycaac1472023-12-12 03:55:50 +000021#include <fmq/AidlMessageQueue.h>
22#include <fmq/EventFlag.h>
Matt Buckleyf7c36d42024-02-27 01:31:43 +000023#include <thread>
Wei Wang61c2a332020-01-08 16:51:47 -080024
25namespace aidl {
26namespace android {
27namespace hardware {
28namespace power {
29namespace impl {
30namespace example {
31
Peiyong Lin5851c3a2022-10-18 20:47:56 +000032using namespace std::chrono_literals;
Matt Buckleycaac1472023-12-12 03:55:50 +000033using ::aidl::android::hardware::common::fmq::MQDescriptor;
34using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
35using ::aidl::android::hardware::power::ChannelMessage;
36using ::android::AidlMessageQueue;
Peiyong Lin5851c3a2022-10-18 20:47:56 +000037
38using ndk::ScopedAStatus;
39
Wei Wang05003412021-04-01 10:44:29 -070040const std::vector<Boost> BOOST_RANGE{ndk::enum_range<Boost>().begin(),
41 ndk::enum_range<Boost>().end()};
42const std::vector<Mode> MODE_RANGE{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()};
43
Matt Buckley607720e2024-10-28 19:21:37 +000044template <class T>
45constexpr size_t enum_size() {
46 return static_cast<size_t>(*(ndk::enum_range<T>().end() - 1)) + 1;
47}
48
Peiyong Lin5851c3a2022-10-18 20:47:56 +000049ScopedAStatus Power::setMode(Mode type, bool enabled) {
Wei Wang61c2a332020-01-08 16:51:47 -080050 LOG(VERBOSE) << "Power setMode: " << static_cast<int32_t>(type) << " to: " << enabled;
Peiyong Lin5851c3a2022-10-18 20:47:56 +000051 return ScopedAStatus::ok();
Wei Wang61c2a332020-01-08 16:51:47 -080052}
53
Peiyong Lin5851c3a2022-10-18 20:47:56 +000054ScopedAStatus Power::isModeSupported(Mode type, bool* _aidl_return) {
Wei Wang61c2a332020-01-08 16:51:47 -080055 LOG(INFO) << "Power isModeSupported: " << static_cast<int32_t>(type);
Wei Wang05003412021-04-01 10:44:29 -070056 *_aidl_return = type >= MODE_RANGE.front() && type <= MODE_RANGE.back();
Peiyong Lin5851c3a2022-10-18 20:47:56 +000057 return ScopedAStatus::ok();
Wei Wang61c2a332020-01-08 16:51:47 -080058}
59
Peiyong Lin5851c3a2022-10-18 20:47:56 +000060ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) {
Wei Wang61c2a332020-01-08 16:51:47 -080061 LOG(VERBOSE) << "Power setBoost: " << static_cast<int32_t>(type)
62 << ", duration: " << durationMs;
Peiyong Lin5851c3a2022-10-18 20:47:56 +000063 return ScopedAStatus::ok();
Wei Wang61c2a332020-01-08 16:51:47 -080064}
65
Peiyong Lin5851c3a2022-10-18 20:47:56 +000066ScopedAStatus Power::isBoostSupported(Boost type, bool* _aidl_return) {
Wei Wang61c2a332020-01-08 16:51:47 -080067 LOG(INFO) << "Power isBoostSupported: " << static_cast<int32_t>(type);
Wei Wang05003412021-04-01 10:44:29 -070068 *_aidl_return = type >= BOOST_RANGE.front() && type <= BOOST_RANGE.back();
Peiyong Lin5851c3a2022-10-18 20:47:56 +000069 return ScopedAStatus::ok();
Wei Wang61c2a332020-01-08 16:51:47 -080070}
71
Peiyong Lin5851c3a2022-10-18 20:47:56 +000072ScopedAStatus Power::createHintSession(int32_t, int32_t, const std::vector<int32_t>& tids, int64_t,
73 std::shared_ptr<IPowerHintSession>* _aidl_return) {
74 if (tids.size() == 0) {
75 *_aidl_return = nullptr;
76 return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
77 }
78 std::shared_ptr<IPowerHintSession> powerHintSession =
79 ndk::SharedRefBase::make<PowerHintSession>();
80 mPowerHintSessions.push_back(powerHintSession);
81 *_aidl_return = powerHintSession;
82 return ScopedAStatus::ok();
Wei Wang05003412021-04-01 10:44:29 -070083}
84
Matt Buckleycaac1472023-12-12 03:55:50 +000085ndk::ScopedAStatus Power::createHintSessionWithConfig(
86 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
87 SessionTag, SessionConfig* config, std::shared_ptr<IPowerHintSession>* _aidl_return) {
88 auto out = createHintSession(tgid, uid, threadIds, durationNanos, _aidl_return);
89 static_cast<PowerHintSession*>(_aidl_return->get())->getSessionConfig(config);
90 return out;
91}
92
93ndk::ScopedAStatus Power::getSessionChannel(int32_t, int32_t, ChannelConfig* _aidl_return) {
Matt Buckleyf7c36d42024-02-27 01:31:43 +000094 static AidlMessageQueue<ChannelMessage, SynchronizedReadWrite> stubQueue{20, true};
95 static std::thread stubThread([&] {
96 ChannelMessage data;
97 // This loop will only run while there is data waiting
98 // to be processed, and blocks on a futex all other times
99 while (stubQueue.readBlocking(&data, 1, 0)) {
100 }
101 });
Matt Buckleycaac1472023-12-12 03:55:50 +0000102 _aidl_return->channelDescriptor = stubQueue.dupeDesc();
Matt Buckleyf7c36d42024-02-27 01:31:43 +0000103 _aidl_return->readFlagBitmask = 0x01;
104 _aidl_return->writeFlagBitmask = 0x02;
Matt Buckleycaac1472023-12-12 03:55:50 +0000105 _aidl_return->eventFlagDescriptor = std::nullopt;
106 return ndk::ScopedAStatus::ok();
107}
108
109ndk::ScopedAStatus Power::closeSessionChannel(int32_t, int32_t) {
110 return ndk::ScopedAStatus::ok();
111}
112
Matt Buckley607720e2024-10-28 19:21:37 +0000113ndk::ScopedAStatus Power::getHintSessionPreferredRate(int64_t* outNanoseconds) {
Peiyong Lin5851c3a2022-10-18 20:47:56 +0000114 *outNanoseconds = std::chrono::nanoseconds(1ms).count();
115 return ScopedAStatus::ok();
Wei Wang05003412021-04-01 10:44:29 -0700116}
117
Matt Buckley607720e2024-10-28 19:21:37 +0000118template <class E>
119int64_t bitsForEnum() {
120 return static_cast<int64_t>(std::bitset<enum_size<E>()>().set().to_ullong());
121}
122
123ndk::ScopedAStatus Power::getSupportInfo(SupportInfo* _aidl_return) {
124 static SupportInfo supportInfo = {
125 .usesSessions = false,
126 .modes = bitsForEnum<Mode>(),
127 .boosts = bitsForEnum<Boost>(),
128 .sessionHints = 0,
129 .sessionModes = 0,
130 .sessionTags = 0,
131 };
132 // Copy the support object into the binder
133 *_aidl_return = supportInfo;
134 return ndk::ScopedAStatus::ok();
135}
136
Wei Wang61c2a332020-01-08 16:51:47 -0800137} // namespace example
138} // namespace impl
139} // namespace power
140} // namespace hardware
141} // namespace android
142} // namespace aidl