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 ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSCAMERAENUMERATOR_H
18 #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSCAMERAENUMERATOR_H
19 
20 #include "ConfigManager.h"
21 
22 #include <android/frameworks/automotive/display/1.0/IAutomotiveDisplayProxyService.h>
23 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
24 #include <android/hardware/automotive/evs/1.1/IEvsEnumerator.h>
25 #include <android/hardware/camera/device/3.2/ICameraDevice.h>
26 
27 #include <atomic>
28 #include <thread>
29 #include <unordered_map>
30 
31 namespace android {
32 namespace hardware {
33 namespace automotive {
34 namespace evs {
35 namespace V1_1 {
36 namespace implementation {
37 
38 using ::android::frameworks::automotive::display::V1_0::IAutomotiveDisplayProxyService;
39 using ::android::hardware::camera::device::V3_2::Stream;
40 
41 using EvsDisplayState = ::android::hardware::automotive::evs::V1_0::DisplayState;
42 using IEvsCamera_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsCamera;
43 using IEvsCamera_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCamera;
44 using IEvsDisplay_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsDisplay;
45 using IEvsDisplay_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsDisplay;
46 
47 class EvsV4lCamera;  // from EvsCamera.h
48 class EvsGlDisplay;  // from EvsGlDisplay.h
49 
50 class EvsEnumerator : public IEvsEnumerator {
51 public:
52     // Methods from ::android::hardware::automotive::evs::V1_0::IEvsEnumerator follow.
53     Return<void> getCameraList(getCameraList_cb _hidl_cb) override;
54     Return<sp<IEvsCamera_1_0>> openCamera(const hidl_string& cameraId) override;
55     Return<void> closeCamera(const ::android::sp<IEvsCamera_1_0>& pCamera) override;
56     Return<sp<IEvsDisplay_1_0>> openDisplay() override;
57     Return<void> closeDisplay(const ::android::sp<IEvsDisplay_1_0>& display) override;
58     Return<EvsDisplayState> getDisplayState() override;
59 
60     // Methods from ::android::hardware::automotive::evs::V1_1::IEvsEnumerator follow.
61     Return<void> getCameraList_1_1(getCameraList_1_1_cb _hidl_cb) override;
62     Return<sp<IEvsCamera_1_1>> openCamera_1_1(const hidl_string& cameraId,
63                                               const Stream& streamCfg) override;
isHardware()64     Return<bool> isHardware() override { return true; }
65     Return<void> getDisplayIdList(getDisplayIdList_cb _list_cb) override;
66     Return<sp<IEvsDisplay_1_1>> openDisplay_1_1(uint8_t port) override;
67     Return<void> getUltrasonicsArrayList(getUltrasonicsArrayList_cb _hidl_cb) override;
68     Return<sp<IEvsUltrasonicsArray>> openUltrasonicsArray(
69             const hidl_string& ultrasonicsArrayId) override;
70     Return<void> closeUltrasonicsArray(
71             const ::android::sp<IEvsUltrasonicsArray>& evsUltrasonicsArray) override;
72 
73     // Implementation details
74     EvsEnumerator(sp<IAutomotiveDisplayProxyService> proxyService = nullptr);
75 
76     // Monitor the device directory
77     static void EvsHotplugThread(std::atomic<bool>& running);
78 
79     // Methods from ::android.hidl.base::V1_0::IBase follow.
80     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override;
81 
82 private:
83     struct CameraRecord {
84         V1_1::CameraDesc desc;
85         android::wp<EvsV4lCamera> activeInstance;
86 
CameraRecordCameraRecord87         CameraRecord(const char* cameraId) : desc() { desc.v1.cameraId = cameraId; }
88     };
89 
90     bool checkPermission();
91 
92     static bool qualifyCaptureDevice(const char* deviceName);
93     static CameraRecord* findCameraById(const std::string& cameraId);
94     static void enumerateCameras();
95     static void enumerateDisplays();
96 
97     void closeCamera_impl(const sp<IEvsCamera_1_0>& pCamera, const std::string& cameraId);
98 
99     // NOTE:  All members values are static so that all clients operate on the same state
100     //        That is to say, this is effectively a singleton despite the fact that HIDL
101     //        constructs a new instance for each client.
102     //        Because our server has a single thread in the thread pool, these values are
103     //        never accessed concurrently despite potentially having multiple instance objects
104     //        using them.
105     static std::unordered_map<std::string, CameraRecord> sCameraList;
106 
107     static wp<EvsGlDisplay> sActiveDisplay;  // Weak pointer.
108                                              // Object destructs if client dies.
109 
110     static std::mutex sLock;                       // Mutex on shared camera device list.
111     static std::condition_variable sCameraSignal;  // Signal on camera device addition.
112 
113     static std::unique_ptr<ConfigManager> sConfigManager;  // ConfigManager
114 
115     static sp<IAutomotiveDisplayProxyService> sDisplayProxy;
116     static std::unordered_map<uint8_t, uint64_t> sDisplayPortList;
117     static uint64_t sInternalDisplayId;
118 
119     // LSHAL debug command implementations
120     void parseCommand(int fd, const hidl_vec<hidl_string>& options);
121     void cmdHelp(int fd);
122     void cmdDump(int fd, const hidl_vec<hidl_string>& options);
123 };
124 
125 }  // namespace implementation
126 }  // namespace V1_1
127 }  // namespace evs
128 }  // namespace automotive
129 }  // namespace hardware
130 }  // namespace android
131 
132 #endif  // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSCAMERAENUMERATOR_H
133