1 /* 2 * Copyright (C) 2017 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 #ifndef CHRE_PLATFORM_SLPI_SEE_ISLAND_VOTE_CLIENT_H_ 18 #define CHRE_PLATFORM_SLPI_SEE_ISLAND_VOTE_CLIENT_H_ 19 20 #include "chre/platform/mutex.h" 21 #include "chre/util/singleton.h" 22 #include "chre/util/time.h" 23 24 extern "C" { 25 26 #include "sns_island_util.h" 27 28 } // extern "C" 29 30 namespace chre { 31 32 class IslandVoteClient : public NonCopyable { 33 public: 34 /** 35 * Constructor for the IslandVoteClient object. 36 * 37 * @param clientName the client name to use when creating an island client 38 */ 39 IslandVoteClient(const char *clientName); 40 41 ~IslandVoteClient(); 42 43 /** 44 * Makes a power mode request. An actual vote to the SLPI power manager may 45 * not be cast depending on current power mode and mBigImageRefCount. 46 * 47 * @param bigImage Whether to request bigImage or not. 48 * 49 * @return true if the vote returned success. 50 */ 51 bool voteBigImage(bool bigImage); 52 53 #ifdef CHRE_SLPI_UIMG_ENABLED 54 /** 55 * Increment the big image reference count when client needs to perform some 56 * big image activity and holds the system in big image. A big image vote is 57 * cast when the count increments from 0. 58 */ 59 void incrementBigImageRefCount(); 60 61 /** 62 * Decrement the big image reference count when client finishes some activity 63 * that has to be performed in big image. A big image vote may be cast or 64 * removed when the count decrements to 0, depending on the system's intended 65 * power state. 66 */ 67 void decrementBigImageRefCount(); 68 69 private: 70 //! The maximum allowed duration to be voted into big image by 71 //! incrementBigImageRefCount before a FATAL_ERROR is triggered. 72 static constexpr Seconds kSeeMaxBigImageDuration = Seconds(300); 73 74 //! Last big image request made through voteBigImage(). 75 bool mLastBigImageRequest = false; 76 77 //! Last big image vote cast to sns_island_aggregator. 78 bool mLastBigImageVote = false; 79 80 //! Client handle for the island aggregator registration. 81 sns_island_client_handle mClientHandle = nullptr; 82 83 //! The system time mBigImageRefCount increments from 0. 84 Milliseconds mRefCountStart = Milliseconds(0); 85 86 //! The count of big image activities. 87 uint32_t mBigImageRefCount = 0; 88 89 //! Used to protect access to member variables from other threads. 90 Mutex mMutex; 91 92 /** 93 * Cast a vote to sns_island_aggregator. 94 * 95 * @param bigImage Whether to vote for bigImage or not. 96 * 97 * @return true if the vote returned success. 98 */ 99 bool voteSnsPowerMode(bool bigImage); 100 101 /** 102 * Check how long the system has been voted into big image due to 103 * incrementBigImageRefCount. If longer than kSeeMaxBigImageDuration, trigger 104 * a crash. 105 * 106 * @return the duration in milliseconds since the system has been voted into 107 * big image due to incrementBigImageRefCount. 108 */ 109 uint64_t checkBigImageDuration() const; 110 #endif // CHRE_SLPI_UIMG_ENABLED 111 }; 112 113 //! Provides an alias to the IslandVoteClient singleton 114 typedef Singleton<IslandVoteClient> IslandVoteClientSingleton; 115 116 extern template class Singleton<IslandVoteClient>; 117 118 } // namespace chre 119 120 #endif // CHRE_PLATFORM_SLPI_SEE_ISLAND_VOTE_CLIENT_H_ 121