1dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph/*
2dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph * Copyright (C) 2016 The Android Open Source Project
3dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph *
4dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph * Licensed under the Apache License, Version 2.0 (the "License");
5dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph * you may not use this file except in compliance with the License.
6dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph * You may obtain a copy of the License at
7dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph *
8dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph *      http://www.apache.org/licenses/LICENSE-2.0
9dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph *
10dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph * Unless required by applicable law or agreed to in writing, software
11dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph * distributed under the License is distributed on an "AS IS" BASIS,
12dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph * See the License for the specific language governing permissions and
14dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph * limitations under the License.
15dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph */
16dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
17dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph#ifndef CAR_EVS_APP_EVSSTATECONTROL_H
18dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph#define CAR_EVS_APP_EVSSTATECONTROL_H
19dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
20150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph#include "StreamHandler.h"
21150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph#include "ConfigManager.h"
22150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph#include "RenderBase.h"
23150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph
24cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsev#include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
25f1b55e7bfee3aeb42436373b4c6ea318c1ee56dfScott Randolph#include <android/hardware/automotive/evs/1.0/IEvsEnumerator.h>
26f1b55e7bfee3aeb42436373b4c6ea318c1ee56dfScott Randolph#include <android/hardware/automotive/evs/1.0/IEvsDisplay.h>
27f1b55e7bfee3aeb42436373b4c6ea318c1ee56dfScott Randolph#include <android/hardware/automotive/evs/1.0/IEvsCamera.h>
28da289be96177f528495cc660f83472ade76c5918Scott Randolph
29150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph#include <thread>
30dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
31dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
32f1b55e7bfee3aeb42436373b4c6ea318c1ee56dfScott Randolphusing namespace ::android::hardware::automotive::evs::V1_0;
33cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevusing namespace ::android::hardware::automotive::vehicle::V2_0;
34dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolphusing ::android::hardware::Return;
35dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolphusing ::android::hardware::Void;
36dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolphusing ::android::hardware::hidl_vec;
37dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolphusing ::android::hardware::hidl_handle;
38dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolphusing ::android::sp;
39dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
40dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
41150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph/*
42150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph * This class runs the main update loop for the EVS application.  It will sleep when it has
43150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph * nothing to do.  It provides a thread safe way for other threads to wake it and pass commands
44150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph * to it.
45150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph */
46da289be96177f528495cc660f83472ade76c5918Scott Randolphclass EvsStateControl {
47dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolphpublic:
48dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph    EvsStateControl(android::sp <IVehicle>       pVnet,
49dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph                    android::sp <IEvsEnumerator> pEvs,
50da289be96177f528495cc660f83472ade76c5918Scott Randolph                    android::sp <IEvsDisplay>    pDisplay,
51da289be96177f528495cc660f83472ade76c5918Scott Randolph                    const ConfigManager&         config);
52dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
53dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph    enum State {
54150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph        OFF = 0,
55150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph        REVERSE,
56dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph        LEFT,
57dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph        RIGHT,
58150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph        PARKING,
59dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph        NUM_STATES  // Must come last
60dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph    };
61dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
62150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    enum class Op {
63150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph        EXIT,
64150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph        CHECK_VEHICLE_STATE,
65150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph        TOUCH_EVENT,
66150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    };
67150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph
68150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    struct Command {
69150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph        Op          operation;
70150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph        uint32_t    arg1;
71150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph        uint32_t    arg2;
72150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    };
73150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph
74150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    // This spawns a new thread that is expected to run continuously
75150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    bool startUpdateLoop();
76150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph
77150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    // Safe to be called from other threads
78150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    void postCommand(const Command& cmd);
79da289be96177f528495cc660f83472ade76c5918Scott Randolph
80dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolphprivate:
81150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    void updateLoop();
82dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph    StatusCode invokeGet(VehiclePropValue *pRequestedPropValue);
83150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    bool selectStateForCurrentConditions();
84150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    bool configureEvsPipeline(State desiredState);  // Only call from one thread!
85dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
86dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph    sp<IVehicle>                mVehicle;
87dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph    sp<IEvsEnumerator>          mEvs;
88dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph    sp<IEvsDisplay>             mDisplay;
894a83b5dba29eceaf43c4ff7d5c184463949174ccScott Randolph    const ConfigManager&        mConfig;
90dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
91dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph    VehiclePropValue            mGearValue;
92dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph    VehiclePropValue            mTurnSignalValue;
93dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
94150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    State                       mCurrentState = OFF;
95150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph
964a83b5dba29eceaf43c4ff7d5c184463949174ccScott Randolph    std::vector<ConfigManager::CameraInfo>  mCameraList[NUM_STATES];
97150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    std::unique_ptr<RenderBase> mCurrentRenderer;
98150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph
99150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    std::thread                 mRenderThread;  // The thread that runs the main rendering loop
100dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
101150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    // Other threads may want to spur us into action, so we provide a thread safe way to do that
102150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    std::mutex                  mLock;
103150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    std::condition_variable     mWakeSignal;
104150c580464a7a4d6b0ad3362f23f790ded65e7abScott Randolph    std::queue<Command>         mCommandQueue;
105dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph};
106dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
107dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph
108dcc3534f0f7523e4ee7285522586d6e2827aa407Scott Randolph#endif //CAR_EVS_APP_EVSSTATECONTROL_H
109