1 /*
2  * Copyright (C) 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 CPP_EVS_MANAGER_1_1_ENUMERATOR_H_
18 #define CPP_EVS_MANAGER_1_1_ENUMERATOR_H_
19 
20 #include "HalCamera.h"
21 #include "IEnumeratorManager.h"
22 #include "IPermissionsChecker.h"
23 #include "ServiceFactory.h"
24 #include "VirtualCamera.h"
25 #include "emul/EvsEmulatedCamera.h"
26 #include "stats/IStatsCollector.h"
27 #include "stats/StatsCollector.h"
28 
29 #include <android/hardware/automotive/evs/1.1/IEvsDisplay.h>
30 #include <android/hardware/automotive/evs/1.1/IEvsEnumerator.h>
31 #include <android/hardware/camera/device/3.2/ICameraDevice.h>
32 #include <system/camera_metadata.h>
33 
34 #include <list>
35 #include <memory>
36 #include <unordered_map>
37 #include <unordered_set>
38 
39 namespace android::automotive::evs::V1_1::implementation {
40 
41 using ::android::hardware::automotive::evs::V1_1::IEvsEnumerator;
42 using ::android::hardware::automotive::evs::V1_1::IEvsUltrasonicsArray;
43 
44 // Passthrough to remove static cling and allow for mocking.
45 class ProdServiceFactory : public ServiceFactory {
46 public:
ProdServiceFactory(const char * hardwareServiceName)47     explicit ProdServiceFactory(const char* hardwareServiceName) :
48           mService(IEvsEnumerator::getService(hardwareServiceName)) {}
49     virtual ~ProdServiceFactory() = default;
50 
getService()51     ::android::hardware::automotive::evs::V1_1::IEvsEnumerator* getService() override {
52         return mService.get();
53     }
54 
55 private:
56     sp<::android::hardware::automotive::evs::V1_1::IEvsEnumerator> mService;
57 };
58 
59 class Enumerator : public IEvsEnumerator {
60 public:
61     // For testing.
62     explicit Enumerator(std::unique_ptr<ServiceFactory> serviceFactory,
63                         std::unique_ptr<IStatsCollector> statsCollector,
64                         std::unique_ptr<IPermissionsChecker> permissionChecker);
65 
66     static std::unique_ptr<Enumerator> build(const char* hardwareServiceName);
67     static std::unique_ptr<Enumerator> build(
68             std::unique_ptr<ServiceFactory> serviceFactory,
69             std::unique_ptr<IStatsCollector> statsCollector,
70             std::unique_ptr<IPermissionsChecker> permissionChecker);
71 
72     virtual ~Enumerator() = default;
73 
74     // Methods from hardware::automotive::evs::V1_0::IEvsEnumerator follow.
75     hardware::Return<void> getCameraList(getCameraList_cb _hidl_cb) override;
76     hardware::Return<sp<hardware::automotive::evs::V1_0::IEvsCamera>> openCamera(
77             const hardware::hidl_string& cameraId) override;
78     hardware::Return<void> closeCamera(
79             const ::android::sp<hardware::automotive::evs::V1_0::IEvsCamera>& virtualCamera)
80             override;
81     hardware::Return<sp<hardware::automotive::evs::V1_0::IEvsDisplay>> openDisplay() override;
82     hardware::Return<void> closeDisplay(
83             const ::android::sp<hardware::automotive::evs::V1_0::IEvsDisplay>& display) override;
84     hardware::Return<hardware::automotive::evs::V1_0::DisplayState> getDisplayState() override;
85 
86     // Methods from hardware::automotive::evs::V1_1::IEvsEnumerator follow.
87     hardware::Return<void> getCameraList_1_1(getCameraList_1_1_cb _hidl_cb) override;
88     hardware::Return<sp<hardware::automotive::evs::V1_1::IEvsCamera>> openCamera_1_1(
89             const hardware::hidl_string& cameraId,
90             const hardware::camera::device::V3_2::Stream& streamCfg) override;
isHardware()91     hardware::Return<bool> isHardware() override { return false; }
92     hardware::Return<void> getDisplayIdList(getDisplayIdList_cb _list_cb) override;
93     hardware::Return<sp<hardware::automotive::evs::V1_1::IEvsDisplay>> openDisplay_1_1(
94             uint8_t id) override;
95     hardware::Return<void> getUltrasonicsArrayList(getUltrasonicsArrayList_cb _hidl_cb) override;
96     hardware::Return<sp<IEvsUltrasonicsArray>> openUltrasonicsArray(
97             const hardware::hidl_string& ultrasonicsArrayId) override;
98     hardware::Return<void> closeUltrasonicsArray(
99             const ::android::sp<IEvsUltrasonicsArray>& evsUltrasonicsArray) override;
100 
101     // Methods from ::android.hidl.base::V1_0::IBase follow.
102     hardware::Return<void> debug(const hardware::hidl_handle& fd,
103                                  const hidl_vec<hardware::hidl_string>& options) override;
104 
105 private:
106     bool isLogicalCamera(const camera_metadata_t* metadata);
107     std::unordered_set<std::string> getPhysicalCameraIds(const std::string& id);
108 
109     const std::unique_ptr<ServiceFactory> mServiceFactory;
110     const std::unique_ptr<IStatsCollector> mStatsCollector;
111     const std::unique_ptr<IPermissionsChecker> mPermissionChecker;
112 
113     wp<hardware::automotive::evs::V1_0::IEvsDisplay> mActiveDisplay;
114 
115     // List of active camera proxy objects that wrap hw cameras
116     std::unordered_map<std::string, sp<HalCamera>> mActiveCameras;
117 
118     // List of camera descriptors of enumerated hw cameras
119     std::unordered_map<std::string, CameraDesc> mCameraDevices;
120 
121     // List of available physical display devices
122     std::list<uint8_t> mDisplayPorts;
123 
124     // Display port the internal display is connected to.
125     uint8_t mInternalDisplayPort;
126 
127     // Boolean flag to tell whether the camera usages are being monitored or not.
128     bool mMonitorEnabled = false;
129 
130     // Boolean flag to tell whether EvsDisplay is owned exclusively or not.
131     bool mDisplayOwnedExclusively = false;
132 
133     // LSHAL dump
134     void cmdDump(int fd, const hidl_vec<hardware::hidl_string>& options);
135     void cmdHelp(int fd);
136     void cmdList(int fd, const hidl_vec<hardware::hidl_string>& options);
137     void cmdDumpDevice(int fd, const hidl_vec<hardware::hidl_string>& options);
138 
139     // List of emulated camera devices
140     std::unordered_map<std::string, EmulatedCameraDesc> mEmulatedCameraDevices;
141 
142     // LSHAL command to use emulated camera device
143     void cmdConfigureEmulatedCamera(int fd, const hidl_vec<hardware::hidl_string>& options);
144 };
145 
146 }  // namespace android::automotive::evs::V1_1::implementation
147 
148 #endif  // CPP_EVS_MANAGER_1_1_ENUMERATOR_H_
149