• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SAMPLEDRIVER_AIDL_INCLUDE_EVSENUMERATOR_H
18 #define CPP_EVS_SAMPLEDRIVER_AIDL_INCLUDE_EVSENUMERATOR_H
19 
20 #include "ConfigManager.h"
21 #include "EvsGlDisplay.h"
22 #include "EvsV4lCamera.h"
23 
24 #include <aidl/android/frameworks/automotive/display/ICarDisplayProxy.h>
25 #include <aidl/android/hardware/automotive/evs/BnEvsEnumerator.h>
26 #include <aidl/android/hardware/automotive/evs/CameraDesc.h>
27 #include <aidl/android/hardware/automotive/evs/DeviceStatusType.h>
28 #include <aidl/android/hardware/automotive/evs/IEvsCamera.h>
29 #include <aidl/android/hardware/automotive/evs/IEvsEnumeratorStatusCallback.h>
30 #include <aidl/android/hardware/automotive/evs/Stream.h>
31 #include <utils/Thread.h>
32 
33 #include <atomic>
34 #include <mutex>
35 #include <optional>
36 #include <thread>
37 #include <unordered_map>
38 
39 namespace aidl::android::hardware::automotive::evs::implementation {
40 
41 namespace aidlevs = ::aidl::android::hardware::automotive::evs;
42 
43 class EvsEnumerator final : public ::aidl::android::hardware::automotive::evs::BnEvsEnumerator {
44 public:
45     // Methods from ::aidl::android::hardware::automotive::evs::IEvsEnumerator
46     ::ndk::ScopedAStatus isHardware(bool* flag) override;
47     ::ndk::ScopedAStatus openCamera(const std::string& cameraId,
48                                     const aidlevs::Stream& streamConfig,
49                                     std::shared_ptr<aidlevs::IEvsCamera>* obj) override;
50     ::ndk::ScopedAStatus closeCamera(const std::shared_ptr<aidlevs::IEvsCamera>& obj) override;
51     ::ndk::ScopedAStatus getCameraList(std::vector<aidlevs::CameraDesc>* _aidl_return) override;
52     ::ndk::ScopedAStatus getStreamList(const aidlevs::CameraDesc& desc,
53                                        std::vector<aidlevs::Stream>* _aidl_return) override;
54     ::ndk::ScopedAStatus openDisplay(int32_t displayId,
55                                      std::shared_ptr<aidlevs::IEvsDisplay>* obj) override;
56     ::ndk::ScopedAStatus closeDisplay(const std::shared_ptr<aidlevs::IEvsDisplay>& obj) override;
57     ::ndk::ScopedAStatus getDisplayIdList(std::vector<uint8_t>* list) override;
58     ::ndk::ScopedAStatus getDisplayState(aidlevs::DisplayState* state) override;
59     ::ndk::ScopedAStatus getDisplayStateById(int32_t displayId,
60                                              aidlevs::DisplayState* state) override;
61     ::ndk::ScopedAStatus registerStatusCallback(
62             const std::shared_ptr<aidlevs::IEvsEnumeratorStatusCallback>& callback) override;
63     ::ndk::ScopedAStatus openUltrasonicsArray(
64             const std::string& id, std::shared_ptr<aidlevs::IEvsUltrasonicsArray>* obj) override;
65     ::ndk::ScopedAStatus closeUltrasonicsArray(
66             const std::shared_ptr<aidlevs::IEvsUltrasonicsArray>& obj) override;
67     ::ndk::ScopedAStatus getUltrasonicsArrayList(
68             std::vector<aidlevs::UltrasonicsArrayDesc>* list) override;
69 
70     // Methods from ndk::ICInterface
71     binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
72 
73     // Handle a shell command.
74     binder_status_t handleShellCommand(int in, int out, int err, const char** args,
75                                        uint32_t numArgs) override;
76 
77     // Implementation details
78     EvsEnumerator(const std::shared_ptr<
79                   ::aidl::android::frameworks::automotive::display::ICarDisplayProxy>&
80                           proxyService);
81 
82     void notifyDeviceStatusChange(const std::string_view& deviceName,
83                                   aidlevs::DeviceStatusType type);
84 
85     // Monitor hotplug devices
86     static void EvsHotplugThread(std::shared_ptr<EvsEnumerator> service,
87                                  std::atomic<bool>& running);
88 
89 private:
90     struct CameraRecord {
91         aidlevs::CameraDesc desc;
92         std::weak_ptr<EvsV4lCamera> activeInstance;
93 
CameraRecordCameraRecord94         CameraRecord(const char* cameraId) : desc() { desc.id = cameraId; }
95     };
96 
97     class ActiveDisplays {
98     public:
99         struct DisplayInfo {
100             int32_t id{-1};
101             std::weak_ptr<EvsGlDisplay> displayWeak;
102             uintptr_t internalDisplayRawAddr;
103         };
104 
105         std::optional<DisplayInfo> popDisplay(int32_t id);
106 
107         std::optional<DisplayInfo> popDisplay(std::shared_ptr<IEvsDisplay> display);
108 
109         std::unordered_map<int32_t, DisplayInfo> getAllDisplays();
110 
111         bool tryInsert(int32_t id, std::shared_ptr<EvsGlDisplay> display);
112 
113     private:
114         std::mutex mMutex;
115         std::unordered_map<int32_t, DisplayInfo> mIdToDisplay GUARDED_BY(mMutex);
116         std::unordered_map<uintptr_t, int32_t> mDisplayToId GUARDED_BY(mMutex);
117     };
118 
119     bool checkPermission();
120     void closeCamera_impl(const std::shared_ptr<aidlevs::IEvsCamera>& pCamera,
121                           const std::string& cameraId);
122     ::ndk::ScopedAStatus getDisplayStateImpl(std::optional<int32_t> displayId,
123                                              aidlevs::DisplayState* state);
124 
125     static bool qualifyCaptureDevice(const char* deviceName);
126     static CameraRecord* findCameraById(const std::string& cameraId);
127     static void enumerateCameras();
128     static bool addCaptureDevice(const std::string& deviceName);
129     static bool removeCaptureDevice(const std::string& deviceName);
130     // Enumerate available displays and return an id of the internal display
131     static uint64_t enumerateDisplays();
132 
133     static ActiveDisplays& mutableActiveDisplays();
134 
135     // NOTE:  All members values are static so that all clients operate on the same state
136     //        That is to say, this is effectively a singleton despite the fact that HIDL
137     //        constructs a new instance for each client.
138     //        Because our server has a single thread in the thread pool, these values are
139     //        never accessed concurrently despite potentially having multiple instance objects
140     //        using them.
141     static std::unordered_map<std::string, CameraRecord> sCameraList;
142     // Object destructs if client dies.
143     static std::mutex sLock;                               // Mutex on shared camera device list.
144     static std::condition_variable sCameraSignal;          // Signal on camera device addition.
145     static std::unique_ptr<ConfigManager> sConfigManager;  // ConfigManager
146     static std::shared_ptr<::aidl::android::frameworks::automotive::display::ICarDisplayProxy>
147             sDisplayProxy;
148     static std::unordered_map<uint8_t, uint64_t> sDisplayPortList;
149 
150     uint64_t mInternalDisplayId;
151     std::shared_ptr<aidlevs::IEvsEnumeratorStatusCallback> mCallback;
152 
153     // Dumpsys commands
154     binder_status_t parseCommand(int fd, const std::vector<std::string>& options);
155     binder_status_t cmdDump(int fd, const std::vector<std::string>& options);
156     void cmdHelp(int fd);
157 };
158 
159 }  // namespace aidl::android::hardware::automotive::evs::implementation
160 
161 #endif  // CPP_EVS_SAMPLEDRIVER_AIDL_INCLUDE_EVSENUMERATOR_H
162