/* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "CarPowerManagerNative: " #include #include #include #include #include "CarPowerManager.h" namespace android { namespace car { namespace hardware { namespace power { // Public functions int CarPowerManager::clearListener() { int retVal = -1; if (mIsConnected && (mListenerToService != nullptr)) { mICarPower->unregisterListener(mListenerToService); mListenerToService = nullptr; retVal = 0; } return retVal; } int CarPowerManager::requestShutdownOnNextSuspend() { int retVal = -1; if (connectToCarService()) { mICarPower->requestShutdownOnNextSuspend(); retVal = 0; } return retVal; } int CarPowerManager::setListener(Listener listener) { int retVal = -1; if (connectToCarService()) { if (mListenerToService == nullptr) { mListenerToService = new CarPowerStateListener(this); mICarPower->registerListener(mListenerToService); } // Set the listener mListener = listener; retVal = 0; } return retVal; } // Private functions bool CarPowerManager::connectToCarService() { if (mIsConnected) { // Service is already connected return true; } const String16 ICarName("car_service"); const String16 ICarPowerName("power"); ALOGI(LOG_TAG "Connecting to CarService"); // Get ICar sp serviceManager = defaultServiceManager(); if (serviceManager == nullptr) { ALOGE(LOG_TAG "Cannot get defaultServiceManager"); return(false); } sp binder = (serviceManager->getService(ICarName)); if (binder == nullptr) { ALOGE(LOG_TAG "Cannot get ICar"); return false; } // Get ICarPower sp iCar = interface_cast(binder); if (iCar == nullptr) { ALOGW(LOG_TAG "car service unavailable"); return false; } iCar->getCarService(ICarPowerName, &binder); if (binder == nullptr) { ALOGE(LOG_TAG "Cannot get ICarPower"); return false; } mICarPower = interface_cast(binder); if (mICarPower == nullptr) { ALOGW(LOG_TAG "car power management service unavailable"); return false; } mIsConnected = true; return true; } } // namespace power } // namespace hardware } // namespace car } // namespace android