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