aboutsummaryrefslogtreecommitdiff
path: root/evs/app/EvsStateControl.h
blob: cfb68339848d7d32b967eb998224a21580ef49bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CAR_EVS_APP_EVSSTATECONTROL_H
#define CAR_EVS_APP_EVSSTATECONTROL_H

#include "StreamHandler.h"
#include "ConfigManager.h"
#include "RenderBase.h"

#include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
#include <android/hardware/automotive/evs/1.0/IEvsEnumerator.h>
#include <android/hardware/automotive/evs/1.0/IEvsDisplay.h>
#include <android/hardware/automotive/evs/1.0/IEvsCamera.h>

#include <thread>


using namespace ::android::hardware::automotive::evs::V1_0;
using namespace ::android::hardware::automotive::vehicle::V2_0;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::hidl_vec;
using ::android::hardware::hidl_handle;
using ::android::sp;


/*
 * This class runs the main update loop for the EVS application.  It will sleep when it has
 * nothing to do.  It provides a thread safe way for other threads to wake it and pass commands
 * to it.
 */
class EvsStateControl {
public:
    EvsStateControl(android::sp <IVehicle>       pVnet,
                    android::sp <IEvsEnumerator> pEvs,
                    android::sp <IEvsDisplay>    pDisplay,
                    const ConfigManager&         config);

    enum State {
        OFF = 0,
        REVERSE,
        LEFT,
        RIGHT,
        PARKING,
        NUM_STATES  // Must come last
    };

    enum class Op {
        EXIT,
        CHECK_VEHICLE_STATE,
        TOUCH_EVENT,
    };

    struct Command {
        Op          operation;
        uint32_t    arg1;
        uint32_t    arg2;
    };

    // This spawns a new thread that is expected to run continuously
    bool startUpdateLoop();

    // Safe to be called from other threads
    void postCommand(const Command& cmd);

private:
    void updateLoop();
    StatusCode invokeGet(VehiclePropValue *pRequestedPropValue);
    bool selectStateForCurrentConditions();
    bool configureEvsPipeline(State desiredState);  // Only call from one thread!

    sp<IVehicle>                mVehicle;
    sp<IEvsEnumerator>          mEvs;
    sp<IEvsDisplay>             mDisplay;
    const ConfigManager&        mConfig;

    VehiclePropValue            mGearValue;
    VehiclePropValue            mTurnSignalValue;

    State                       mCurrentState = OFF;

    std::vector<ConfigManager::CameraInfo>  mCameraList[NUM_STATES];
    std::unique_ptr<RenderBase> mCurrentRenderer;

    std::thread                 mRenderThread;  // The thread that runs the main rendering loop

    // Other threads may want to spur us into action, so we provide a thread safe way to do that
    std::mutex                  mLock;
    std::condition_variable     mWakeSignal;
    std::queue<Command>         mCommandQueue;
};


#endif //CAR_EVS_APP_EVSSTATECONTROL_H