1787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian/* 2787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * Copyright (C) 2012 The Android Open Source Project 3787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * 4787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * you may not use this file except in compliance with the License. 6787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * You may obtain a copy of the License at 7787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * 8787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * 10787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * Unless required by applicable law or agreed to in writing, software 11787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * See the License for the specific language governing permissions and 14787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * limitations under the License. 15787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian */ 16787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 17787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <stdint.h> 18787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <math.h> 19787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <sys/types.h> 20787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 21787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <utils/Atomic.h> 22787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <utils/Errors.h> 23787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <utils/Singleton.h> 24787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 25787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <binder/BinderService.h> 26787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <binder/Parcel.h> 27787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 28787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include "BatteryService.h" 29787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 30787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopiannamespace android { 31787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian// --------------------------------------------------------------------------- 32787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 33787ac1b388f144f5e6dd38f8b807866a5256dafcMathias AgopianBatteryService::BatteryService() { 34787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian const sp<IServiceManager> sm(defaultServiceManager()); 35787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian if (sm != NULL) { 36ee83397bb07ef6807ca3f4d691da7d4fd7d632f0Dianne Hackborn const String16 name("batterystats"); 37787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian mBatteryStatService = sm->getService(name); 38787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian } 39787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian} 40787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 41787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianstatus_t BatteryService::noteStartSensor(int uid, int handle) { 42787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian Parcel data, reply; 43787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian data.writeInterfaceToken(DESCRIPTOR); 44787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian data.writeInt32(uid); 45787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian data.writeInt32(handle); 46787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian status_t err = mBatteryStatService->transact( 47787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian TRANSACTION_noteStartSensor, data, &reply, 0); 48787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian err = reply.readExceptionCode(); 49787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian return err; 50787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian} 51787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 52787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianstatus_t BatteryService::noteStopSensor(int uid, int handle) { 53787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian Parcel data, reply; 54787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian data.writeInterfaceToken(DESCRIPTOR); 55787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian data.writeInt32(uid); 56787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian data.writeInt32(handle); 57787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian status_t err = mBatteryStatService->transact( 58787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian TRANSACTION_noteStopSensor, data, &reply, 0); 59787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian err = reply.readExceptionCode(); 60787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian return err; 61787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian} 62787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 63787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianbool BatteryService::addSensor(uid_t uid, int handle) { 64787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian Mutex::Autolock _l(mActivationsLock); 65787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian Info key(uid, handle); 66787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian ssize_t index = mActivations.indexOf(key); 67787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian if (index < 0) { 68787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian index = mActivations.add(key); 69787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian } 70787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian Info& info(mActivations.editItemAt(index)); 71787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian info.count++; 72787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian return info.count == 1; 73787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian} 74787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 75787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianbool BatteryService::removeSensor(uid_t uid, int handle) { 76787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian Mutex::Autolock _l(mActivationsLock); 77787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian ssize_t index = mActivations.indexOf(Info(uid, handle)); 78787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian if (index < 0) return false; 79787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian Info& info(mActivations.editItemAt(index)); 80787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian info.count--; 81787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian return info.count == 0; 82787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian} 83787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 84787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 85787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianvoid BatteryService::enableSensorImpl(uid_t uid, int handle) { 86787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian if (mBatteryStatService != 0) { 87787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian if (addSensor(uid, handle)) { 88787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian int64_t identity = IPCThreadState::self()->clearCallingIdentity(); 89787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian noteStartSensor(uid, handle); 90787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian IPCThreadState::self()->restoreCallingIdentity(identity); 91787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian } 92787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian } 93787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian} 94787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianvoid BatteryService::disableSensorImpl(uid_t uid, int handle) { 95787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian if (mBatteryStatService != 0) { 96787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian if (removeSensor(uid, handle)) { 97787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian int64_t identity = IPCThreadState::self()->clearCallingIdentity(); 98787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian noteStopSensor(uid, handle); 99787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian IPCThreadState::self()->restoreCallingIdentity(identity); 100787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian } 101787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian } 102787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian} 103787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 104787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianvoid BatteryService::cleanupImpl(uid_t uid) { 105787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian if (mBatteryStatService != 0) { 106787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian Mutex::Autolock _l(mActivationsLock); 107787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian int64_t identity = IPCThreadState::self()->clearCallingIdentity(); 108787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian for (ssize_t i=0 ; i<mActivations.size() ; i++) { 109787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian const Info& info(mActivations[i]); 110787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian if (info.uid == uid) { 111787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian noteStopSensor(info.uid, info.handle); 112787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian mActivations.removeAt(i); 113787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian i--; 114787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian } 115787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian } 116787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian IPCThreadState::self()->restoreCallingIdentity(identity); 117787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian } 118787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian} 119787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 120787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianconst String16 BatteryService::DESCRIPTOR("com.android.internal.app.IBatteryStats"); 121787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 122787ac1b388f144f5e6dd38f8b807866a5256dafcMathias AgopianANDROID_SINGLETON_STATIC_INSTANCE(BatteryService) 123787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 124787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian// --------------------------------------------------------------------------- 125787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian}; // namespace android 126787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian 127