1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2012 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #include <stdint.h>
18*38e8c45fSAndroid Build Coastguard Worker #include <math.h>
19*38e8c45fSAndroid Build Coastguard Worker #include <sys/types.h>
20*38e8c45fSAndroid Build Coastguard Worker
21*38e8c45fSAndroid Build Coastguard Worker #include <cutils/atomic.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <utils/Errors.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <utils/Singleton.h>
24*38e8c45fSAndroid Build Coastguard Worker
25*38e8c45fSAndroid Build Coastguard Worker #include <binder/BinderService.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <binder/Parcel.h>
27*38e8c45fSAndroid Build Coastguard Worker
28*38e8c45fSAndroid Build Coastguard Worker #include "BatteryService.h"
29*38e8c45fSAndroid Build Coastguard Worker
30*38e8c45fSAndroid Build Coastguard Worker namespace android {
31*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
32*38e8c45fSAndroid Build Coastguard Worker
BatteryService()33*38e8c45fSAndroid Build Coastguard Worker BatteryService::BatteryService()
34*38e8c45fSAndroid Build Coastguard Worker : mBatteryStatService(nullptr), mLastWakeupSensorEventReportedMs(0) {}
35*38e8c45fSAndroid Build Coastguard Worker
addSensor(uid_t uid,int handle)36*38e8c45fSAndroid Build Coastguard Worker bool BatteryService::addSensor(uid_t uid, int handle) {
37*38e8c45fSAndroid Build Coastguard Worker Mutex::Autolock _l(mActivationsLock);
38*38e8c45fSAndroid Build Coastguard Worker Info key(uid, handle);
39*38e8c45fSAndroid Build Coastguard Worker ssize_t index = mActivations.indexOf(key);
40*38e8c45fSAndroid Build Coastguard Worker if (index < 0) {
41*38e8c45fSAndroid Build Coastguard Worker index = mActivations.add(key);
42*38e8c45fSAndroid Build Coastguard Worker }
43*38e8c45fSAndroid Build Coastguard Worker Info& info(mActivations.editItemAt(index));
44*38e8c45fSAndroid Build Coastguard Worker info.count++;
45*38e8c45fSAndroid Build Coastguard Worker return info.count == 1;
46*38e8c45fSAndroid Build Coastguard Worker }
47*38e8c45fSAndroid Build Coastguard Worker
removeSensor(uid_t uid,int handle)48*38e8c45fSAndroid Build Coastguard Worker bool BatteryService::removeSensor(uid_t uid, int handle) {
49*38e8c45fSAndroid Build Coastguard Worker Mutex::Autolock _l(mActivationsLock);
50*38e8c45fSAndroid Build Coastguard Worker ssize_t index = mActivations.indexOf(Info(uid, handle));
51*38e8c45fSAndroid Build Coastguard Worker if (index < 0) return false;
52*38e8c45fSAndroid Build Coastguard Worker Info& info(mActivations.editItemAt(index));
53*38e8c45fSAndroid Build Coastguard Worker info.count--;
54*38e8c45fSAndroid Build Coastguard Worker return info.count == 0;
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker
57*38e8c45fSAndroid Build Coastguard Worker
enableSensorImpl(uid_t uid,int handle)58*38e8c45fSAndroid Build Coastguard Worker void BatteryService::enableSensorImpl(uid_t uid, int handle) {
59*38e8c45fSAndroid Build Coastguard Worker if (checkService()) {
60*38e8c45fSAndroid Build Coastguard Worker if (addSensor(uid, handle)) {
61*38e8c45fSAndroid Build Coastguard Worker int64_t identity = IPCThreadState::self()->clearCallingIdentity();
62*38e8c45fSAndroid Build Coastguard Worker mBatteryStatService->noteStartSensor(uid, handle);
63*38e8c45fSAndroid Build Coastguard Worker IPCThreadState::self()->restoreCallingIdentity(identity);
64*38e8c45fSAndroid Build Coastguard Worker }
65*38e8c45fSAndroid Build Coastguard Worker }
66*38e8c45fSAndroid Build Coastguard Worker }
disableSensorImpl(uid_t uid,int handle)67*38e8c45fSAndroid Build Coastguard Worker void BatteryService::disableSensorImpl(uid_t uid, int handle) {
68*38e8c45fSAndroid Build Coastguard Worker if (checkService()) {
69*38e8c45fSAndroid Build Coastguard Worker if (removeSensor(uid, handle)) {
70*38e8c45fSAndroid Build Coastguard Worker int64_t identity = IPCThreadState::self()->clearCallingIdentity();
71*38e8c45fSAndroid Build Coastguard Worker mBatteryStatService->noteStopSensor(uid, handle);
72*38e8c45fSAndroid Build Coastguard Worker IPCThreadState::self()->restoreCallingIdentity(identity);
73*38e8c45fSAndroid Build Coastguard Worker }
74*38e8c45fSAndroid Build Coastguard Worker }
75*38e8c45fSAndroid Build Coastguard Worker }
76*38e8c45fSAndroid Build Coastguard Worker
noteWakeupSensorEventImpl(int64_t elapsedNanos,uid_t uid,int handle)77*38e8c45fSAndroid Build Coastguard Worker void BatteryService::noteWakeupSensorEventImpl(int64_t elapsedNanos, uid_t uid, int handle) {
78*38e8c45fSAndroid Build Coastguard Worker if (checkService()) {
79*38e8c45fSAndroid Build Coastguard Worker int64_t identity = IPCThreadState::self()->clearCallingIdentity();
80*38e8c45fSAndroid Build Coastguard Worker mBatteryStatService->noteWakeupSensorEvent(elapsedNanos, uid, handle);
81*38e8c45fSAndroid Build Coastguard Worker IPCThreadState::self()->restoreCallingIdentity(identity);
82*38e8c45fSAndroid Build Coastguard Worker }
83*38e8c45fSAndroid Build Coastguard Worker }
84*38e8c45fSAndroid Build Coastguard Worker
checkService()85*38e8c45fSAndroid Build Coastguard Worker bool BatteryService::checkService() {
86*38e8c45fSAndroid Build Coastguard Worker if (mBatteryStatService == nullptr) {
87*38e8c45fSAndroid Build Coastguard Worker const sp<IServiceManager> sm(defaultServiceManager());
88*38e8c45fSAndroid Build Coastguard Worker if (sm != nullptr) {
89*38e8c45fSAndroid Build Coastguard Worker const String16 name("batterystats");
90*38e8c45fSAndroid Build Coastguard Worker mBatteryStatService = interface_cast<IBatteryStats>(sm->getService(name));
91*38e8c45fSAndroid Build Coastguard Worker }
92*38e8c45fSAndroid Build Coastguard Worker }
93*38e8c45fSAndroid Build Coastguard Worker return mBatteryStatService != nullptr;
94*38e8c45fSAndroid Build Coastguard Worker }
95*38e8c45fSAndroid Build Coastguard Worker
96*38e8c45fSAndroid Build Coastguard Worker ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
97*38e8c45fSAndroid Build Coastguard Worker
98*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
99*38e8c45fSAndroid Build Coastguard Worker }; // namespace android
100