1 /*
2 * Copyright (C) 2016 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 #define LOG_TAG "VehicleMonitor.Lib"
17
18 #include <assert.h>
19 #include <binder/IServiceManager.h>
20 #include <binder/ProcessState.h>
21
22 #include <VehicleMonitor.h>
23
24 namespace android {
25 // ----------------------------------------------------------------------------
26
27 static const int MAX_SERVICE_RETRY = 4;
28
createVehicleMonitor()29 sp<VehicleMonitor> VehicleMonitor::createVehicleMonitor() {
30 sp<IBinder> binder;
31 int retry = 0;
32 while (true) {
33 binder = defaultServiceManager()->getService(String16(IVehicleMonitor::SERVICE_NAME));
34 if (binder.get() != NULL) {
35 break;
36 }
37 retry++;
38 if (retry > MAX_SERVICE_RETRY) {
39 ALOGE("cannot get VMS, will crash");
40 break;
41 }
42 }
43 assert(binder.get() != NULL);
44 sp<IVehicleMonitor> ivm(interface_cast<IVehicleMonitor>(binder));
45 sp<VehicleMonitor> vm;
46 vm = new VehicleMonitor(ivm);
47 assert(vm.get() != NULL);
48 // in case thread pool is not started, start it.
49 ProcessState::self()->startThreadPool();
50 return vm;
51 }
52
VehicleMonitor(sp<IVehicleMonitor> & vehicleMonitor)53 VehicleMonitor::VehicleMonitor(sp<IVehicleMonitor>& vehicleMonitor) :
54 mService(vehicleMonitor) {
55 }
56
~VehicleMonitor()57 VehicleMonitor::~VehicleMonitor() {
58 sp<IVehicleMonitor> service = getService();
59 IInterface::asBinder(service)->unlinkToDeath(this);
60 }
61
onFirstRef()62 void VehicleMonitor::onFirstRef() {
63 sp<IVehicleMonitor> service = getService();
64 IInterface::asBinder(service)->linkToDeath(this);
65 }
66
setAppPriority(uint32_t pid,uint32_t uid,vehicle_app_priority priority)67 status_t VehicleMonitor::setAppPriority(
68 uint32_t pid, uint32_t uid, vehicle_app_priority priority) {
69 return getService()->setAppPriority(pid, uid, priority);
70 }
71
binderDied(const wp<IBinder> & who)72 void VehicleMonitor::binderDied(const wp<IBinder>& who) {
73 ALOGE("service died");
74 {
75 Mutex::Autolock autoLock(mLock);
76 sp<IBinder> ibinder = who.promote();
77 ibinder->unlinkToDeath(this);
78 sp<IBinder> binder = defaultServiceManager()->getService(
79 String16(IVehicleMonitor::SERVICE_NAME));
80 mService = interface_cast<IVehicleMonitor>(binder);
81 IInterface::asBinder(mService)->linkToDeath(this);
82 };
83 }
84
getService()85 sp<IVehicleMonitor> VehicleMonitor::getService() {
86 Mutex::Autolock autoLock(mLock);
87 return mService;
88 }
89
90 }; // namespace android
91