1 /* 2 * Copyright (C) 2012 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 #include <stdint.h> 18 #include <math.h> 19 #include <sys/types.h> 20 21 #include <utils/Atomic.h> 22 #include <utils/Errors.h> 23 #include <utils/Singleton.h> 24 25 #include <binder/BinderService.h> 26 #include <binder/Parcel.h> 27 28 #include "BatteryService.h" 29 30 namespace android { 31 // --------------------------------------------------------------------------- 32 33 BatteryService::BatteryService() { 34 const sp<IServiceManager> sm(defaultServiceManager()); 35 if (sm != NULL) { 36 const String16 name("batterystats"); 37 mBatteryStatService = sm->getService(name); 38 } 39 } 40 41 status_t BatteryService::noteStartSensor(int uid, int handle) { 42 Parcel data, reply; 43 data.writeInterfaceToken(DESCRIPTOR); 44 data.writeInt32(uid); 45 data.writeInt32(handle); 46 status_t err = mBatteryStatService->transact( 47 TRANSACTION_noteStartSensor, data, &reply, 0); 48 err = reply.readExceptionCode(); 49 return err; 50 } 51 52 status_t BatteryService::noteStopSensor(int uid, int handle) { 53 Parcel data, reply; 54 data.writeInterfaceToken(DESCRIPTOR); 55 data.writeInt32(uid); 56 data.writeInt32(handle); 57 status_t err = mBatteryStatService->transact( 58 TRANSACTION_noteStopSensor, data, &reply, 0); 59 err = reply.readExceptionCode(); 60 return err; 61 } 62 63 bool BatteryService::addSensor(uid_t uid, int handle) { 64 Mutex::Autolock _l(mActivationsLock); 65 Info key(uid, handle); 66 ssize_t index = mActivations.indexOf(key); 67 if (index < 0) { 68 index = mActivations.add(key); 69 } 70 Info& info(mActivations.editItemAt(index)); 71 info.count++; 72 return info.count == 1; 73 } 74 75 bool BatteryService::removeSensor(uid_t uid, int handle) { 76 Mutex::Autolock _l(mActivationsLock); 77 ssize_t index = mActivations.indexOf(Info(uid, handle)); 78 if (index < 0) return false; 79 Info& info(mActivations.editItemAt(index)); 80 info.count--; 81 return info.count == 0; 82 } 83 84 85 void BatteryService::enableSensorImpl(uid_t uid, int handle) { 86 if (mBatteryStatService != 0) { 87 if (addSensor(uid, handle)) { 88 int64_t identity = IPCThreadState::self()->clearCallingIdentity(); 89 noteStartSensor(uid, handle); 90 IPCThreadState::self()->restoreCallingIdentity(identity); 91 } 92 } 93 } 94 void BatteryService::disableSensorImpl(uid_t uid, int handle) { 95 if (mBatteryStatService != 0) { 96 if (removeSensor(uid, handle)) { 97 int64_t identity = IPCThreadState::self()->clearCallingIdentity(); 98 noteStopSensor(uid, handle); 99 IPCThreadState::self()->restoreCallingIdentity(identity); 100 } 101 } 102 } 103 104 void BatteryService::cleanupImpl(uid_t uid) { 105 if (mBatteryStatService != 0) { 106 Mutex::Autolock _l(mActivationsLock); 107 int64_t identity = IPCThreadState::self()->clearCallingIdentity(); 108 for (ssize_t i=0 ; i<mActivations.size() ; i++) { 109 const Info& info(mActivations[i]); 110 if (info.uid == uid) { 111 noteStopSensor(info.uid, info.handle); 112 mActivations.removeAt(i); 113 i--; 114 } 115 } 116 IPCThreadState::self()->restoreCallingIdentity(identity); 117 } 118 } 119 120 const String16 BatteryService::DESCRIPTOR("com.android.internal.app.IBatteryStats"); 121 122 ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService) 123 124 // --------------------------------------------------------------------------- 125 }; // namespace android 126 127