1 /*
2  * Copyright (C) 2022 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_AIDL_INCLUDE_ENUMERATOR_H
18 #define CPP_EVS_MANAGER_AIDL_INCLUDE_ENUMERATOR_H
19 
20 #include "HalCamera.h"
21 #include "VirtualCamera.h"
22 #include "stats/include/StatsCollector.h"
23 
24 #include <aidl/android/hardware/automotive/evs/BnEvsEnumerator.h>
25 #include <aidl/android/hardware/automotive/evs/BnEvsEnumeratorStatusCallback.h>
26 #include <aidl/android/hardware/automotive/evs/IEvsDisplay.h>
27 #include <system/camera_metadata.h>
28 
29 #include <list>
30 #include <shared_mutex>
31 #include <unordered_map>
32 #include <unordered_set>
33 
34 namespace aidl::android::automotive::evs::implementation {
35 
36 namespace aidlevs = ::aidl::android::hardware::automotive::evs;
37 
38 class Enumerator final : public ::aidl::android::hardware::automotive::evs::BnEvsEnumerator {
39 public:
40     // Methods from ::aidl::android::hardware::automotive::evs::IEvsEnumerator
41     ::ndk::ScopedAStatus isHardware(bool* flag) override;
42     ::ndk::ScopedAStatus openCamera(const std::string& cameraId,
43                                     const aidlevs::Stream& streamConfig,
44                                     std::shared_ptr<aidlevs::IEvsCamera>* obj) override;
45     ::ndk::ScopedAStatus closeCamera(const std::shared_ptr<aidlevs::IEvsCamera>& obj) override;
46     ::ndk::ScopedAStatus getCameraList(std::vector<aidlevs::CameraDesc>* _aidl_return) override;
47     ::ndk::ScopedAStatus getStreamList(const aidlevs::CameraDesc& desc,
48                                        std::vector<aidlevs::Stream>* _aidl_return) override;
49     ::ndk::ScopedAStatus openDisplay(int32_t displayId,
50                                      std::shared_ptr<aidlevs::IEvsDisplay>* obj) override;
51     ::ndk::ScopedAStatus closeDisplay(const std::shared_ptr<aidlevs::IEvsDisplay>& obj) override;
52     ::ndk::ScopedAStatus getDisplayIdList(std::vector<uint8_t>* list) override;
53     ::ndk::ScopedAStatus getDisplayState(aidlevs::DisplayState* state) override;
54     ::ndk::ScopedAStatus getDisplayStateById(int32_t displayId,
55                                              aidlevs::DisplayState* state) override;
56     ::ndk::ScopedAStatus registerStatusCallback(
57             const std::shared_ptr<aidlevs::IEvsEnumeratorStatusCallback>& callback) override;
58     ::ndk::ScopedAStatus openUltrasonicsArray(
59             const std::string& id, std::shared_ptr<aidlevs::IEvsUltrasonicsArray>* obj) override;
60     ::ndk::ScopedAStatus closeUltrasonicsArray(
61             const std::shared_ptr<aidlevs::IEvsUltrasonicsArray>& obj) override;
62     ::ndk::ScopedAStatus getUltrasonicsArrayList(
63             std::vector<aidlevs::UltrasonicsArrayDesc>* list) override;
64 
65     // Method from ::ndk::ICInterface
66     binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
67 
68     // Implementation details
69     bool init(const std::string_view& hardwareServiceName);
70     void broadcastDeviceStatusChange(const std::vector<aidlevs::DeviceStatus>& list);
71 
72     // Destructor
73     virtual ~Enumerator();
74 
75     // TODO(b/235110887): We may eventually want to remove below two methods and
76     //                    replace their functionality with other methods more
77     //                    elegant.
78     bool init(std::shared_ptr<aidlevs::IEvsEnumerator>& hwEnumerator, bool enableMonitor = false);
79     void enablePermissionCheck(bool enable);
80 
81 private:
82     class EvsDeviceStatusCallbackImpl : public aidlevs::BnEvsEnumeratorStatusCallback {
83     public:
EvsDeviceStatusCallbackImpl(const std::shared_ptr<Enumerator> & enumerator)84         EvsDeviceStatusCallbackImpl(const std::shared_ptr<Enumerator>& enumerator) :
85               mEnumerator(enumerator) {}
86         ::ndk::ScopedAStatus deviceStatusChanged(
87                 const std::vector<aidlevs::DeviceStatus>& status) override;
88 
89     private:
90         std::shared_ptr<Enumerator> mEnumerator;
91     };
92 
93     bool checkPermission() const;
94     bool isLogicalCamera(const camera_metadata_t* metadata) const;
95     std::unordered_set<std::string> getPhysicalCameraIds(const std::string& id);
96     std::shared_ptr<aidlevs::IEvsEnumerator> connectToAidlHal(
97             const std::string_view& hardwareServiceName, bool blocking);
98     std::shared_ptr<aidlevs::IEvsEnumerator> connectToHidlHal(
99             const std::string_view& hardwareServiceName);
100 
101     void cmdDump(int fd, const char** args, uint32_t numArgs);
102     void cmdHelp(int fd);
103     void cmdList(int fd, const char** args, uint32_t numArgs);
104     void cmdDumpDevice(int fd, const char** args, uint32_t numArgs);
105 
106     // Hardware enumerator
107     std::shared_ptr<aidlevs::IEvsEnumerator> mHwEnumerator;
108 
109     // Display proxy object warpping hw display
110     std::weak_ptr<aidlevs::IEvsDisplay> mActiveDisplay;
111 
112     // List of active camera proxy objects that wrap hw cameras
113     std::unordered_map<std::string, std::shared_ptr<HalCamera>> mActiveCameras;
114 
115     // List of camera descriptors of enumerated hw cameras
116     std::unordered_map<std::string, aidlevs::CameraDesc> mCameraDevices;
117 
118     // List of available physical display devices
119     std::vector<uint8_t> mDisplayPorts;
120 
121     // Display port the internal display is connected to.
122     int32_t mInternalDisplayPort;
123 
124     // Collecting camera usage statistics from clients
125     ::android::sp<StatsCollector> mClientsMonitor;
126 
127     // Boolean flag to tell whether the camera usages are being monitored or not
128     bool mMonitorEnabled;
129 
130     // Boolean flag to tell whether EvsDisplay is owned exclusively or not
131     bool mDisplayOwnedExclusively;
132 
133     // Callback to listen to device status changes
134     std::shared_ptr<EvsDeviceStatusCallbackImpl> mDeviceStatusCallback;
135 
136     // Mutex to protect resources related with a device status callback
137     mutable std::shared_mutex mLock;
138 
139     // Clients to forward device status callback messages
140     std::set<std::shared_ptr<aidlevs::IEvsEnumeratorStatusCallback>> mDeviceStatusCallbacks;
141 
142     bool mDisablePermissionCheck = false;
143 
144     std::list<std::weak_ptr<VirtualCamera>> mActiveCameraClients;
145 };
146 
147 }  // namespace aidl::android::automotive::evs::implementation
148 
149 #endif  // CPP_EVS_MANAGER_AIDL_INCLUDE_ENUMERATOR_H
150