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"
17 
18 #include <assert.h>
19 
20 #include <binder/PermissionCache.h>
21 #include <utils/Errors.h>
22 #include <utils/SystemClock.h>
23 
24 #include "VehicleMonitorService.h"
25 
26 //#define DBG_VERBOSE
27 #ifdef DBG_VERBOSE
28 #define LOG_VERBOSE(x...) ALOGD(x)
29 #else
30 #define LOG_VERBOSE(x...)
31 #endif
32 
33 namespace android {
34 
35 const nsecs_t monitorInterval = 15000000000; // 15s
36 
VehicleMonitorMessageHandler(const sp<Looper> & looper,VehicleMonitorService & service)37 VehicleMonitorMessageHandler::VehicleMonitorMessageHandler(const sp<Looper>& looper,
38         VehicleMonitorService& service)
39     : mLooper(looper),
40       mService(service),
41       mLastDispatchTime(0) {
42     mLooper->sendMessageDelayed(monitorInterval, this, Message(COLLECT_DATA));
43 }
44 
~VehicleMonitorMessageHandler()45 VehicleMonitorMessageHandler::~VehicleMonitorMessageHandler() {
46 }
47 
dump(String8 & msg)48 void VehicleMonitorMessageHandler::dump(String8& msg) {
49     msg.appendFormat("mLastDispatchTime:%" PRId64 "\n", mLastDispatchTime);
50     mProcessMonitor.dump(msg);
51 }
52 
doHandleCollectData()53 void VehicleMonitorMessageHandler::doHandleCollectData() {
54     {
55         std::lock_guard<std::mutex> autoLock(mLock);
56         mLastDispatchTime = elapsedRealtime();
57         mProcessMonitor.process();
58     }
59 
60     // TODO: do better timing for sendMessage
61     mLooper->sendMessageDelayed(monitorInterval, this, Message(COLLECT_DATA));
62 }
63 
handleMessage(const Message & message)64 void VehicleMonitorMessageHandler::handleMessage(const Message& message) {
65     switch (message.what) {
66     case COLLECT_DATA:
67         doHandleCollectData();
68         break;
69     default:
70         // TODO?
71         break;
72     }
73 }
74 
75 // ----------------------------------------------------
76 VehicleMonitorService* VehicleMonitorService::sInstance = NULL;
77 
dump(int fd,const Vector<String16> &)78 status_t VehicleMonitorService::dump(int fd, const Vector<String16>& /*args*/) {
79     static const String16 sDump("android.permission.DUMP");
80     String8 msg;
81     if (!PermissionCache::checkCallingPermission(sDump)) {
82         msg.appendFormat("Permission Denial: "
83                          "can't dump VMS from pid=%d, uid=%d\n",
84                          IPCThreadState::self()->getCallingPid(),
85                          IPCThreadState::self()->getCallingUid());
86         write(fd, msg.string(), msg.size());
87         return NO_ERROR;
88     }
89     msg.appendFormat("*Handler, now in ms:%" PRId64 "\n", elapsedRealtime());
90     mHandler->dump(msg);
91     write(fd, msg.string(), msg.size());
92     return NO_ERROR;
93 }
94 
VehicleMonitorService()95 VehicleMonitorService::VehicleMonitorService() {
96     sInstance = this;
97 }
98 
~VehicleMonitorService()99 VehicleMonitorService::~VehicleMonitorService() {
100     sInstance = NULL;
101 }
102 
binderDied(const wp<IBinder> & who)103 void VehicleMonitorService::binderDied(const wp<IBinder>& who) {
104     std::lock_guard<std::mutex> autoLock(mLock);
105     sp<IBinder> ibinder = who.promote();
106     ibinder->unlinkToDeath(this);
107     // TODO: reset all priorities set by CarService.
108 }
109 
release()110 void VehicleMonitorService::release() {
111     std::lock_guard<std::mutex> autoLock(mLock);
112     mHandlerThread->quit();
113 }
114 
onFirstRef()115 void VehicleMonitorService::onFirstRef() {
116     std::lock_guard<std::mutex> autoLock(mLock);
117     mHandlerThread = new HandlerThread();
118     status_t r = mHandlerThread->start("VMS.NATIVE_LOOP");
119     if (r != NO_ERROR) {
120         ALOGE("cannot start handler thread, error:%d", r);
121         return;
122     }
123     sp<VehicleMonitorMessageHandler> handler(
124             new VehicleMonitorMessageHandler(mHandlerThread->getLooper(), *this));
125     assert(handler.get() != NULL);
126     mHandler = handler;
127 }
128 
setAppPriority(uint32_t,uint32_t,vehicle_app_priority)129 status_t VehicleMonitorService::setAppPriority(uint32_t, uint32_t, vehicle_app_priority) {
130     //TODO
131     return NO_ERROR;
132 }
133 
setMonitorListener(const sp<IVehicleMonitorListener> & listener)134 status_t VehicleMonitorService::setMonitorListener(
135         const sp<IVehicleMonitorListener> &listener) {
136     sp<IBinder> ibinder = IInterface::asBinder(listener);
137     LOG_VERBOSE("setMonitorListener, binder 0x%x", ibinder.get());
138     //TODO
139     return NO_ERROR;
140 }
141 
142 }; // namespace android
143