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_VEHICLELISTENER_H
18 #define CAR_EVS_APP_VEHICLELISTENER_H
19 
20 #include "EvsStateControl.h"
21 
22 
23 class EvsVehicleListener : public IVehicleCallback {
24 public:
25     // Methods from ::android::hardware::automotive::vehicle::V2_0::IVehicleCallback follow.
onPropertyEvent(const hidl_vec<VehiclePropValue> &)26     Return<void> onPropertyEvent(const hidl_vec <VehiclePropValue> & /*values*/) override {
27         {
28             // Our use case is so simple, we don't actually need to update a variable,
29             // but the docs seem to say we have to take the lock anyway to keep
30             // the condition variable implementation happy.
31             std::lock_guard<std::mutex> g(mLock);
32         }
33         mEventCond.notify_one();
34         return Return<void>();
35     }
36 
onPropertySet(const VehiclePropValue &)37     Return<void> onPropertySet(const VehiclePropValue & /*value*/) override {
38         // Ignore the direct set calls (we don't expect to make any anyway)
39         return Return<void>();
40     }
41 
onPropertySetError(StatusCode,int32_t,int32_t)42     Return<void> onPropertySetError(StatusCode      /* errorCode */,
43                                     int32_t         /* propId */,
44                                     int32_t         /* areaId */) override {
45         // We don't set values, so we don't listen for set errors
46         return Return<void>();
47     }
48 
waitForEvents(int timeout_ms)49     bool waitForEvents(int timeout_ms) {
50         std::unique_lock<std::mutex> g(mLock);
51         std::cv_status result = mEventCond.wait_for(g, std::chrono::milliseconds(timeout_ms));
52         return (result == std::cv_status::no_timeout);
53     }
54 
run(EvsStateControl * pStateController)55     void run(EvsStateControl *pStateController) {
56         while (true) {
57             // Wait until we have an event to which to react
58             // (wake up and validate our current state "just in case" every so often)
59             waitForEvents(5000);
60 
61             // If we were delivered an event (or it's been a while) update as necessary
62             pStateController->configureForVehicleState();
63         }
64     }
65 
66 private:
67     std::mutex mLock;
68     std::condition_variable mEventCond;
69 };
70 
71 #endif //CAR_EVS_APP_VEHICLELISTENER_H
72