1 /*
2  * Copyright (C) 2016-2019 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 ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSCAMERAENUMERATOR_H
18 #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSCAMERAENUMERATOR_H
19 
20 #include <android/hardware/automotive/evs/1.0/IEvsEnumerator.h>
21 #include <android/hardware/automotive/evs/1.0/IEvsCamera.h>
22 
23 #include <unordered_map>
24 #include <thread>
25 #include <atomic>
26 
27 namespace android {
28 namespace hardware {
29 namespace automotive {
30 namespace evs {
31 namespace V1_0 {
32 namespace implementation {
33 
34 
35 class EvsV4lCamera;    // from EvsCamera.h
36 class EvsGlDisplay;    // from EvsGlDisplay.h
37 
38 class EvsEnumerator : public IEvsEnumerator {
39 public:
40     // Methods from ::android::hardware::automotive::evs::V1_0::IEvsEnumerator follow.
41     Return<void> getCameraList(getCameraList_cb _hidl_cb)  override;
42     Return<sp<IEvsCamera>> openCamera(const hidl_string& cameraId) override;
43     Return<void> closeCamera(const ::android::sp<IEvsCamera>& carCamera)  override;
44     Return<sp<IEvsDisplay>> openDisplay()  override;
45     Return<void> closeDisplay(const ::android::sp<IEvsDisplay>& display)  override;
46     Return<DisplayState> getDisplayState()  override;
47 
48     // Implementation details
49     EvsEnumerator();
50 
51     // Listen to video device uevents
52     static void EvsUeventThread(std::atomic<bool>& running);
53 
54 private:
55     struct CameraRecord {
56         CameraDesc          desc;
57         wp<EvsV4lCamera>    activeInstance;
58 
CameraRecordCameraRecord59         CameraRecord(const char *cameraId) : desc() { desc.cameraId = cameraId; }
60     };
61 
62     bool checkPermission();
63 
64     static bool qualifyCaptureDevice(const char* deviceName);
65     static CameraRecord* findCameraById(const std::string& cameraId);
66     static void enumerateDevices();
67 
68     // NOTE:  All members values are static so that all clients operate on the same state
69     //        That is to say, this is effectively a singleton despite the fact that HIDL
70     //        constructs a new instance for each client.
71     //        Because our server has a single thread in the thread pool, these values are
72     //        never accessed concurrently despite potentially having multiple instance objects
73     //        using them.
74     static std::unordered_map<std::string,
75                               CameraRecord> sCameraList;
76 
77     static wp<EvsGlDisplay>                 sActiveDisplay; // Weak pointer.
78                                                             // Object destructs if client dies.
79 
80     static std::mutex                       sLock;          // Mutex on shared camera device list.
81     static std::condition_variable          sCameraSignal;  // Signal on camera device addition.
82 };
83 
84 } // namespace implementation
85 } // namespace V1_0
86 } // namespace evs
87 } // namespace automotive
88 } // namespace hardware
89 } // namespace android
90 
91 #endif  // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSCAMERAENUMERATOR_H
92