• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #define LOG_TAG "CarPowerManagerNative: "
18 
19 #include <binder/IServiceManager.h>
20 #include <binder/IBinder.h>
21 #include <binder/IInterface.h>
22 #include <utils/Log.h>
23 
24 #include "CarPowerManager.h"
25 
26 namespace android {
27 namespace car {
28 namespace hardware {
29 namespace power {
30 
31 // Public functions
clearListener()32 int CarPowerManager::clearListener() {
33     int retVal = -1;
34 
35     if (mIsConnected && (mListenerToService != nullptr)) {
36         mICarPower->unregisterListener(mListenerToService);
37         mListenerToService = nullptr;
38         retVal = 0;
39     }
40     return retVal;
41 }
42 
requestShutdownOnNextSuspend()43 int CarPowerManager::requestShutdownOnNextSuspend() {
44     int retVal = -1;
45 
46     if (connectToCarService()) {
47         mICarPower->requestShutdownOnNextSuspend();
48         retVal = 0;
49     }
50     return retVal;
51 }
52 
setListener(Listener listener)53 int CarPowerManager::setListener(Listener listener) {
54     int retVal = -1;
55 
56     if (connectToCarService()) {
57         if (mListenerToService == nullptr) {
58             mListenerToService = new CarPowerStateListener(this);
59             mICarPower->registerListener(mListenerToService);
60         }
61 
62         // Set the listener
63         mListener = listener;
64         retVal = 0;
65     }
66     return retVal;
67 }
68 
69 
70 // Private functions
connectToCarService()71 bool CarPowerManager::connectToCarService() {
72     if (mIsConnected) {
73         // Service is already connected
74         return true;
75     }
76 
77     const String16 ICarName("car_service");
78     const String16 ICarPowerName("power");
79 
80     ALOGI(LOG_TAG "Connecting to CarService");
81 
82     // Get ICar
83     sp<IServiceManager> serviceManager = defaultServiceManager();
84     if (serviceManager == nullptr) {
85         ALOGE(LOG_TAG "Cannot get defaultServiceManager");
86         return(false);
87     }
88 
89     sp<IBinder> binder = (serviceManager->getService(ICarName));
90     if (binder == nullptr) {
91         ALOGE(LOG_TAG "Cannot get ICar");
92         return false;
93     }
94 
95     // Get ICarPower
96     sp<ICar> iCar = interface_cast<ICar>(binder);
97     if (iCar == nullptr) {
98         ALOGW(LOG_TAG "car service unavailable");
99         return false;
100     }
101 
102     iCar->getCarService(ICarPowerName, &binder);
103     if (binder == nullptr) {
104         ALOGE(LOG_TAG "Cannot get ICarPower");
105         return false;
106     }
107 
108     mICarPower = interface_cast<ICarPower>(binder);
109     if (mICarPower == nullptr) {
110         ALOGW(LOG_TAG "car power management service unavailable");
111         return false;
112     }
113     mIsConnected = true;
114     return true;
115 }
116 
117 
118 } // namespace power
119 } // namespace hardware
120 } // namespace car
121 } // namespace android
122 
123