1 /*
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"
18 #include "PowerHintSession.h"
19
20 #include <android-base/logging.h>
21 #include <fmq/AidlMessageQueue.h>
22 #include <fmq/EventFlag.h>
23 #include <thread>
24
25 namespace aidl {
26 namespace android {
27 namespace hardware {
28 namespace power {
29 namespace impl {
30 namespace example {
31
32 using namespace std::chrono_literals;
33 using ::aidl::android::hardware::common::fmq::MQDescriptor;
34 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
35 using ::aidl::android::hardware::power::ChannelMessage;
36 using ::android::AidlMessageQueue;
37
38 using ndk::ScopedAStatus;
39
40 const std::vector<Boost> BOOST_RANGE{ndk::enum_range<Boost>().begin(),
41 ndk::enum_range<Boost>().end()};
42 const std::vector<Mode> MODE_RANGE{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()};
43
setMode(Mode type,bool enabled)44 ScopedAStatus Power::setMode(Mode type, bool enabled) {
45 LOG(VERBOSE) << "Power setMode: " << static_cast<int32_t>(type) << " to: " << enabled;
46 return ScopedAStatus::ok();
47 }
48
isModeSupported(Mode type,bool * _aidl_return)49 ScopedAStatus Power::isModeSupported(Mode type, bool* _aidl_return) {
50 LOG(INFO) << "Power isModeSupported: " << static_cast<int32_t>(type);
51 *_aidl_return = type >= MODE_RANGE.front() && type <= MODE_RANGE.back();
52 return ScopedAStatus::ok();
53 }
54
setBoost(Boost type,int32_t durationMs)55 ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) {
56 LOG(VERBOSE) << "Power setBoost: " << static_cast<int32_t>(type)
57 << ", duration: " << durationMs;
58 return ScopedAStatus::ok();
59 }
60
isBoostSupported(Boost type,bool * _aidl_return)61 ScopedAStatus Power::isBoostSupported(Boost type, bool* _aidl_return) {
62 LOG(INFO) << "Power isBoostSupported: " << static_cast<int32_t>(type);
63 *_aidl_return = type >= BOOST_RANGE.front() && type <= BOOST_RANGE.back();
64 return ScopedAStatus::ok();
65 }
66
createHintSession(int32_t,int32_t,const std::vector<int32_t> & tids,int64_t,std::shared_ptr<IPowerHintSession> * _aidl_return)67 ScopedAStatus Power::createHintSession(int32_t, int32_t, const std::vector<int32_t>& tids, int64_t,
68 std::shared_ptr<IPowerHintSession>* _aidl_return) {
69 if (tids.size() == 0) {
70 *_aidl_return = nullptr;
71 return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
72 }
73 std::shared_ptr<IPowerHintSession> powerHintSession =
74 ndk::SharedRefBase::make<PowerHintSession>();
75 mPowerHintSessions.push_back(powerHintSession);
76 *_aidl_return = powerHintSession;
77 return ScopedAStatus::ok();
78 }
79
createHintSessionWithConfig(int32_t tgid,int32_t uid,const std::vector<int32_t> & threadIds,int64_t durationNanos,SessionTag,SessionConfig * config,std::shared_ptr<IPowerHintSession> * _aidl_return)80 ndk::ScopedAStatus Power::createHintSessionWithConfig(
81 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
82 SessionTag, SessionConfig* config, std::shared_ptr<IPowerHintSession>* _aidl_return) {
83 auto out = createHintSession(tgid, uid, threadIds, durationNanos, _aidl_return);
84 static_cast<PowerHintSession*>(_aidl_return->get())->getSessionConfig(config);
85 return out;
86 }
87
getSessionChannel(int32_t,int32_t,ChannelConfig * _aidl_return)88 ndk::ScopedAStatus Power::getSessionChannel(int32_t, int32_t, ChannelConfig* _aidl_return) {
89 static AidlMessageQueue<ChannelMessage, SynchronizedReadWrite> stubQueue{20, true};
90 static std::thread stubThread([&] {
91 ChannelMessage data;
92 // This loop will only run while there is data waiting
93 // to be processed, and blocks on a futex all other times
94 while (stubQueue.readBlocking(&data, 1, 0)) {
95 }
96 });
97 _aidl_return->channelDescriptor = stubQueue.dupeDesc();
98 _aidl_return->readFlagBitmask = 0x01;
99 _aidl_return->writeFlagBitmask = 0x02;
100 _aidl_return->eventFlagDescriptor = std::nullopt;
101 return ndk::ScopedAStatus::ok();
102 }
103
closeSessionChannel(int32_t,int32_t)104 ndk::ScopedAStatus Power::closeSessionChannel(int32_t, int32_t) {
105 return ndk::ScopedAStatus::ok();
106 }
107
getHintSessionPreferredRate(int64_t * outNanoseconds)108 ScopedAStatus Power::getHintSessionPreferredRate(int64_t* outNanoseconds) {
109 *outNanoseconds = std::chrono::nanoseconds(1ms).count();
110 return ScopedAStatus::ok();
111 }
112
113 } // namespace example
114 } // namespace impl
115 } // namespace power
116 } // namespace hardware
117 } // namespace android
118 } // namespace aidl
119