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 #ifndef CAR_VEHICLE_MONITOR_SERVICE_H_
17 #define CAR_VEHICLE_MONITOR_SERVICE_H_
18 
19 #include <mutex>
20 #include <inttypes.h>
21 #include <cutils/compiler.h>
22 
23 #include <binder/BinderService.h>
24 #include <binder/IBinder.h>
25 #include <utils/String8.h>
26 
27 #include <ProcessMonitor.h>
28 #include <IVehicleMonitor.h>
29 #include <IVehicleMonitorListener.h>
30 #include <HandlerThread.h>
31 
32 namespace android {
33 // ----------------------------------------------------------------------------
34 
35 class VehicleMonitorService;
36 
37 /**
38  * MessageHandler to handle periodic data processing.
39  * Init / release is handled in the handler thread to allow upper layer to
40  * allocate resource for the thread.
41  */
42 class VehicleMonitorMessageHandler : public MessageHandler {
43     enum {
44         COLLECT_DATA = 0,
45     };
46 
47 public:
48     // not passing VMS as sp as this is held by VMS always.
49     VehicleMonitorMessageHandler(
50             const sp<Looper>& mLooper, VehicleMonitorService& service);
51     virtual ~VehicleMonitorMessageHandler();
52 
53     void dump(String8& msg);
54 
55 private:
56     void handleMessage(const Message& message);
57     void doHandleCollectData();
58 
59 private:
60     mutable std::mutex mLock;
61     const sp<Looper> mLooper;
62     ProcessMonitor mProcessMonitor;
63     VehicleMonitorService& mService;
64     int64_t mLastDispatchTime;
65 };
66 
67 // ----------------------------------------------------------------------------
68 class VehicleMonitorService :
69     public BinderService<VehicleMonitorService>,
70     public BnVehicleMonitor,
71     public IBinder::DeathRecipient {
72 public:
getServiceName()73     static const char* getServiceName() ANDROID_API {
74         return IVehicleMonitor::SERVICE_NAME;
75     };
76 
77     VehicleMonitorService();
78     ~VehicleMonitorService();
79     virtual status_t dump(int fd, const Vector<String16>& args);
80     void release();
81     virtual void binderDied(const wp<IBinder>& who);
82     virtual status_t setAppPriority(
83             uint32_t pid, uint32_t uid, vehicle_app_priority priority);
84     virtual status_t setMonitorListener(
85             const sp<IVehicleMonitorListener> &listener);
86 
87 private:
88     // RefBase
89     virtual void onFirstRef();
90 private:
91     static VehicleMonitorService* sInstance;
92     sp<HandlerThread> mHandlerThread;
93     sp<VehicleMonitorMessageHandler> mHandler;
94     mutable std::mutex mLock;
95 };
96 
97 }
98 
99 #endif /* CAR_VEHICLE_MONITOR_SERVICE_H_ */
100