• 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 
getBootReason(BootReason * bootReason)43 int CarPowerManager::getBootReason(BootReason *bootReason) {
44     int retVal = -1;
45 
46     if ((bootReason != nullptr) && connectToCarService()) {
47         int reason = -1;
48         mICarPower->getBootReason(&reason);
49 
50         if ((reason >= static_cast<int>(BootReason::kFirst)) &&
51             (reason <= static_cast<int>(BootReason::kLast))) {
52             *bootReason = static_cast<BootReason>(reason);
53             retVal = 0;
54         } else {
55             ALOGE("Received unknown bootReason = %d", reason);
56         }
57     }
58     return retVal;
59 }
60 
requestShutdownOnNextSuspend()61 int CarPowerManager::requestShutdownOnNextSuspend() {
62     int retVal = -1;
63 
64     if (connectToCarService()) {
65         mICarPower->requestShutdownOnNextSuspend();
66         retVal = 0;
67     }
68     return retVal;
69 }
70 
setListener(Listener listener)71 int CarPowerManager::setListener(Listener listener) {
72     int retVal = -1;
73 
74     if (connectToCarService()) {
75         if (mListenerToService == nullptr) {
76             mListenerToService = new CarPowerStateListener(this);
77             mICarPower->registerListener(mListenerToService);
78         }
79 
80         // Set the listener
81         mListener = listener;
82         retVal = 0;
83     }
84     return retVal;
85 }
86 
87 
88 // Private functions
connectToCarService()89 bool CarPowerManager::connectToCarService() {
90     if (mIsConnected) {
91         // Service is already connected
92         return true;
93     }
94 
95     const String16 ICarName("car_service");
96     const String16 ICarPowerName("power");
97 
98     ALOGI("Connecting to CarService" LOG_TAG);
99 
100     // Get ICar
101     sp<IServiceManager> serviceManager = defaultServiceManager();
102     if (serviceManager == nullptr) {
103         ALOGE("Cannot get defaultServiceManager");
104         return(false);
105     }
106 
107     sp<IBinder> binder = (serviceManager->getService(ICarName));
108     if (binder == nullptr) {
109         ALOGE("Cannot get ICar");
110         return false;
111     }
112 
113     // Get ICarPower
114     sp<ICar> iCar = interface_cast<ICar>(binder);
115     if (iCar == nullptr) {
116         ALOGW("car service unavailable");
117         return false;
118     }
119 
120     iCar->getCarService(ICarPowerName, &binder);
121     if (binder == nullptr) {
122         ALOGE("Cannot get ICarPower");
123         return false;
124     }
125 
126     mICarPower = interface_cast<ICarPower>(binder);
127     if (mICarPower == nullptr) {
128         ALOGW("car power management service unavailable");
129         return false;
130     }
131     mIsConnected = true;
132     return true;
133 }
134 
135 
136 } // namespace power
137 } // namespace hardware
138 } // namespace car
139 } // namespace android
140 
141