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 // Implementation details 74 EvsEnumerator(const std::shared_ptr< 75 ::aidl::android::frameworks::automotive::display::ICarDisplayProxy>& 76 proxyService); 77 78 void notifyDeviceStatusChange(const std::string_view& deviceName, 79 aidlevs::DeviceStatusType type); 80 81 // Monitor hotplug devices 82 static void EvsHotplugThread(std::shared_ptr<EvsEnumerator> service, 83 std::atomic<bool>& running); 84 85 private: 86 struct CameraRecord { 87 aidlevs::CameraDesc desc; 88 std::weak_ptr<EvsV4lCamera> activeInstance; 89 CameraRecordCameraRecord90 CameraRecord(const char* cameraId) : desc() { desc.id = cameraId; } 91 }; 92 93 class ActiveDisplays { 94 public: 95 struct DisplayInfo { 96 int32_t id{-1}; 97 std::weak_ptr<EvsGlDisplay> displayWeak; 98 uintptr_t internalDisplayRawAddr; 99 }; 100 101 std::optional<DisplayInfo> popDisplay(int32_t id); 102 103 std::optional<DisplayInfo> popDisplay(std::shared_ptr<IEvsDisplay> display); 104 105 std::unordered_map<int32_t, DisplayInfo> getAllDisplays(); 106 107 bool tryInsert(int32_t id, std::shared_ptr<EvsGlDisplay> display); 108 109 private: 110 std::mutex mMutex; 111 std::unordered_map<int32_t, DisplayInfo> mIdToDisplay GUARDED_BY(mMutex); 112 std::unordered_map<uintptr_t, int32_t> mDisplayToId GUARDED_BY(mMutex); 113 }; 114 115 bool checkPermission(); 116 void closeCamera_impl(const std::shared_ptr<aidlevs::IEvsCamera>& pCamera, 117 const std::string& cameraId); 118 ::ndk::ScopedAStatus getDisplayStateImpl(std::optional<int32_t> displayId, 119 aidlevs::DisplayState* state); 120 121 static bool qualifyCaptureDevice(const char* deviceName); 122 static CameraRecord* findCameraById(const std::string& cameraId); 123 static void enumerateCameras(); 124 static bool addCaptureDevice(const std::string& deviceName); 125 static bool removeCaptureDevice(const std::string& deviceName); 126 // Enumerate available displays and return an id of the internal display 127 static uint64_t enumerateDisplays(); 128 129 static ActiveDisplays& mutableActiveDisplays(); 130 131 // NOTE: All members values are static so that all clients operate on the same state 132 // That is to say, this is effectively a singleton despite the fact that HIDL 133 // constructs a new instance for each client. 134 // Because our server has a single thread in the thread pool, these values are 135 // never accessed concurrently despite potentially having multiple instance objects 136 // using them. 137 static std::unordered_map<std::string, CameraRecord> sCameraList; 138 // Object destructs if client dies. 139 static std::mutex sLock; // Mutex on shared camera device list. 140 static std::condition_variable sCameraSignal; // Signal on camera device addition. 141 static std::unique_ptr<ConfigManager> sConfigManager; // ConfigManager 142 static std::shared_ptr<::aidl::android::frameworks::automotive::display::ICarDisplayProxy> 143 sDisplayProxy; 144 static std::unordered_map<uint8_t, uint64_t> sDisplayPortList; 145 146 uint64_t mInternalDisplayId; 147 std::shared_ptr<aidlevs::IEvsEnumeratorStatusCallback> mCallback; 148 149 // Dumpsys commands 150 binder_status_t parseCommand(int fd, const std::vector<std::string>& options); 151 binder_status_t cmdDump(int fd, const std::vector<std::string>& options); 152 void cmdHelp(int fd); 153 }; 154 155 } // namespace aidl::android::hardware::automotive::evs::implementation 156 157 #endif // CPP_EVS_SAMPLEDRIVER_AIDL_INCLUDE_EVSENUMERATOR_H 158