Home | History | Annotate | Download | only in slpi
      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, nullptr, 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::votePowerMode(
     43     sns_pm_img_mode_e mode) {
     44 #ifdef CHRE_SLPI_UIMG_ENABLED
     45   sns_pm_err_code_e result = sns_pm_vote_img_mode(mClientHandle, mode);
     46   if (result != SNS_PM_SUCCESS) {
     47     LOGE("Failed to vote for power mode %d with result %d", mode, result);
     48   }
     49 
     50   return (result == SNS_PM_SUCCESS);
     51 #else
     52   return true;
     53 #endif // CHRE_SLPI_UIMG_ENABLED
     54 }
     55 
     56 void PowerControlManager::postEventLoopProcess(size_t numPendingEvents) {
     57   if (numPendingEvents == 0 && !slpiInUImage()) {
     58     votePowerMode(SNS_IMG_MODE_NOCLIENT);
     59   }
     60 }
     61 
     62 } // namespace chre
     63