Home | History | Annotate | Download | only in smgr
      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 #include "chre/platform/power_control_manager.h"
     18 
     19 #include "chre/platform/fatal_error.h"
     20 #include "chre/platform/log.h"
     21 #include "chre/platform/slpi/power_control_util.h"
     22 
     23 namespace chre {
     24 
     25 PowerControlManagerBase::PowerControlManagerBase() {
     26 #ifdef CHRE_SLPI_UIMG_ENABLED
     27   char kClientName[] = "CHRE";
     28   sns_pm_err_code_e result = sns_pm_client_init(
     29       &mClientHandle, apSuspendCallback, kClientName, SNS_PM_CLIENT_ID_CHRE);
     30   if (result != SNS_PM_SUCCESS) {
     31     FATAL_ERROR("Power manager client init failed");
     32   }
     33 #endif // CHRE_SLPI_UIMG_ENABLED
     34 }
     35 
     36 PowerControlManagerBase::~PowerControlManagerBase() {
     37 #ifdef CHRE_SLPI_UIMG_ENABLED
     38   sns_pm_client_close(mClientHandle);
     39 #endif // CHRE_SLPI_UIMG_ENABLED
     40 }
     41 
     42 bool PowerControlManagerBase::voteBigImage(bool bigImage) {
     43 #ifdef CHRE_SLPI_UIMG_ENABLED
     44   sns_pm_img_mode_e mode = bigImage ? SNS_IMG_MODE_BIG : SNS_IMG_MODE_NOCLIENT;
     45   sns_pm_err_code_e result = sns_pm_vote_img_mode(mClientHandle, mode);
     46   if (result != SNS_PM_SUCCESS) {
     47     // Note that FATAL_ERROR must not be used here, because this can be called
     48     // from preFatalError() (not that we should use it here regardless)
     49     LOGE("Failed to vote for power mode %d with result %d", mode, result);
     50   }
     51 
     52   return (result == SNS_PM_SUCCESS);
     53 #else
     54   return true;
     55 #endif // CHRE_SLPI_UIMG_ENABLED
     56 }
     57 
     58 void PowerControlManager::postEventLoopProcess(size_t numPendingEvents) {
     59   if (numPendingEvents == 0 && !slpiInUImage()) {
     60     voteBigImage(false);
     61   }
     62 }
     63 
     64 void PowerControlManagerBase::apSuspendCallback(bool apSuspended) {
     65   EventLoopManagerSingleton::get()->getEventLoop()
     66       .getPowerControlManager().mHostIsAwake = !apSuspended;
     67   if (apSuspended) {
     68     EventLoopManagerSingleton::get()->getEventLoop()
     69         .postEvent(CHRE_EVENT_HOST_ASLEEP, nullptr, nullptr);
     70   } else {
     71     EventLoopManagerSingleton::get()->getEventLoop()
     72         .postEvent(CHRE_EVENT_HOST_AWAKE, nullptr, nullptr);
     73   }
     74 }
     75 
     76 bool PowerControlManager::hostIsAwake() {
     77   return mHostIsAwake;
     78 }
     79 
     80 } // namespace chre
     81