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 
17 #ifndef CAR_EVS_APP_EVSSTATECONTROL_H
18 #define CAR_EVS_APP_EVSSTATECONTROL_H
19 
20 #include "StreamHandler.h"
21 #include "ConfigManager.h"
22 #include "RenderBase.h"
23 
24 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
25 #include <android/hardware/automotive/evs/1.0/IEvsEnumerator.h>
26 #include <android/hardware/automotive/evs/1.0/IEvsDisplay.h>
27 #include <android/hardware/automotive/evs/1.0/IEvsCamera.h>
28 
29 #include <thread>
30 
31 
32 using namespace ::android::hardware::automotive::evs::V1_0;
33 using namespace ::android::hardware::automotive::vehicle::V2_0;
34 using ::android::hardware::Return;
35 using ::android::hardware::Void;
36 using ::android::hardware::hidl_vec;
37 using ::android::hardware::hidl_handle;
38 using ::android::sp;
39 
40 
41 /*
42  * This class runs the main update loop for the EVS application.  It will sleep when it has
43  * nothing to do.  It provides a thread safe way for other threads to wake it and pass commands
44  * to it.
45  */
46 class EvsStateControl {
47 public:
48     EvsStateControl(android::sp <IVehicle>       pVnet,
49                     android::sp <IEvsEnumerator> pEvs,
50                     android::sp <IEvsDisplay>    pDisplay,
51                     const ConfigManager&         config);
52 
53     enum State {
54         OFF = 0,
55         REVERSE,
56         LEFT,
57         RIGHT,
58         PARKING,
59         NUM_STATES  // Must come last
60     };
61 
62     enum class Op {
63         EXIT,
64         CHECK_VEHICLE_STATE,
65         TOUCH_EVENT,
66     };
67 
68     struct Command {
69         Op          operation;
70         uint32_t    arg1;
71         uint32_t    arg2;
72     };
73 
74     // This spawns a new thread that is expected to run continuously
75     bool startUpdateLoop();
76 
77     // Safe to be called from other threads
78     void postCommand(const Command& cmd);
79 
80 private:
81     void updateLoop();
82     StatusCode invokeGet(VehiclePropValue *pRequestedPropValue);
83     bool selectStateForCurrentConditions();
84     bool configureEvsPipeline(State desiredState);  // Only call from one thread!
85 
86     sp<IVehicle>                mVehicle;
87     sp<IEvsEnumerator>          mEvs;
88     sp<IEvsDisplay>             mDisplay;
89     const ConfigManager&        mConfig;
90 
91     VehiclePropValue            mGearValue;
92     VehiclePropValue            mTurnSignalValue;
93 
94     State                       mCurrentState = OFF;
95 
96     std::vector<ConfigManager::CameraInfo>  mCameraList[NUM_STATES];
97     std::unique_ptr<RenderBase> mCurrentRenderer;
98     std::unique_ptr<RenderBase> mDesiredRenderer;
99 
100     std::thread                 mRenderThread;  // The thread that runs the main rendering loop
101 
102     // Other threads may want to spur us into action, so we provide a thread safe way to do that
103     std::mutex                  mLock;
104     std::condition_variable     mWakeSignal;
105     std::queue<Command>         mCommandQueue;
106 };
107 
108 
109 #endif //CAR_EVS_APP_EVSSTATECONTROL_H
110