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_WRAPPERS_INCLUDE_AIDLENUMERATOR_H
18 #define CPP_EVS_MANAGER_AIDL_WRAPPERS_INCLUDE_AIDLENUMERATOR_H
19 
20 #include <aidl/android/hardware/automotive/evs/BnEvsEnumerator.h>
21 #include <aidl/android/hardware/automotive/evs/IEvsCamera.h>
22 #include <aidl/android/hardware/automotive/evs/IEvsDisplay.h>
23 #include <aidl/android/hardware/automotive/evs/IEvsEnumeratorStatusCallback.h>
24 #include <android/hardware/automotive/evs/1.1/IEvsDisplay.h>
25 #include <android/hardware/automotive/evs/1.1/IEvsEnumerator.h>
26 #include <system/camera_metadata.h>
27 
28 namespace aidl::android::automotive::evs::implementation {
29 
30 namespace aidlevs = ::aidl::android::hardware::automotive::evs;
31 namespace hidlevs = ::android::hardware::automotive::evs;
32 
33 class AidlEnumerator final : public ::aidl::android::hardware::automotive::evs::BnEvsEnumerator {
34 public:
35     // Methods from ::aidl::android::hardware::automotive::evs::IEvsEnumerator
36     ::ndk::ScopedAStatus isHardware(bool* flag) override;
37     ::ndk::ScopedAStatus openCamera(const std::string& cameraId,
38                                     const aidlevs::Stream& streamConfig,
39                                     std::shared_ptr<aidlevs::IEvsCamera>* obj) override;
40     ::ndk::ScopedAStatus closeCamera(const std::shared_ptr<aidlevs::IEvsCamera>& obj) override;
41     ::ndk::ScopedAStatus getCameraList(std::vector<aidlevs::CameraDesc>* _aidl_return) override;
42     ::ndk::ScopedAStatus getStreamList(const aidlevs::CameraDesc& desc,
43                                        std::vector<aidlevs::Stream>* _aidl_return) override;
44     ::ndk::ScopedAStatus openDisplay(int32_t displayId,
45                                      std::shared_ptr<aidlevs::IEvsDisplay>* obj) override;
46     ::ndk::ScopedAStatus closeDisplay(const std::shared_ptr<aidlevs::IEvsDisplay>& obj) override;
47     ::ndk::ScopedAStatus getDisplayIdList(std::vector<uint8_t>* list) override;
48     ::ndk::ScopedAStatus getDisplayState(aidlevs::DisplayState* state) override;
49     ::ndk::ScopedAStatus getDisplayStateById(int32_t displayId,
50                                              aidlevs::DisplayState* state) override;
51     ::ndk::ScopedAStatus openUltrasonicsArray(
52             const std::string& id, std::shared_ptr<aidlevs::IEvsUltrasonicsArray>* obj) override;
53     ::ndk::ScopedAStatus closeUltrasonicsArray(
54             const std::shared_ptr<aidlevs::IEvsUltrasonicsArray>& obj) override;
55     ::ndk::ScopedAStatus getUltrasonicsArrayList(
56             std::vector<aidlevs::UltrasonicsArrayDesc>* list) override;
57     ::ndk::ScopedAStatus registerStatusCallback(
58             const std::shared_ptr<aidlevs::IEvsEnumeratorStatusCallback>& callback) override;
59 
60     explicit AidlEnumerator(const ::android::sp<hidlevs::V1_0::IEvsEnumerator>& svc,
61                             bool forceV1_0 = false);
~AidlEnumerator()62     virtual ~AidlEnumerator() { mImpl = nullptr; }
63 
64     // Implementation details
65     bool init(const char* hardwareServiceName);
66 
67 private:
68     class IHidlEnumerator;
69     class ImplV0;
70     class ImplV1;
71 
72     std::shared_ptr<IHidlEnumerator> mImpl;
73     ::android::wp<hidlevs::V1_0::IEvsDisplay> mHidlDisplay;
74     std::weak_ptr<aidlevs::IEvsDisplay> mAidlDisplay;
75 };
76 
77 class AidlEnumerator::IHidlEnumerator {
78 public:
79     virtual ::ndk::ScopedAStatus closeCamera(
80             const ::android::sp<hidlevs::V1_0::IEvsCamera>& cameraObj) = 0;
81     virtual ::ndk::ScopedAStatus closeDisplay(
82             const ::android::sp<hidlevs::V1_0::IEvsDisplay>& display) = 0;
83     virtual ::ndk::ScopedAStatus getCameraList(std::vector<aidlevs::CameraDesc>* _aidl_return) = 0;
84     virtual ::ndk::ScopedAStatus getDisplayIdList(std::vector<uint8_t>* list) = 0;
85     virtual ::ndk::ScopedAStatus openCamera(const std::string& cameraId,
86                                             const aidlevs::Stream& streamConfig,
87                                             std::shared_ptr<aidlevs::IEvsCamera>* obj) = 0;
88     virtual ::android::sp<hidlevs::V1_0::IEvsDisplay> openDisplay(int32_t displayId) = 0;
89 
IHidlEnumerator(const::android::sp<hidlevs::V1_0::IEvsEnumerator> & svc)90     explicit IHidlEnumerator(const ::android::sp<hidlevs::V1_0::IEvsEnumerator>& svc) :
91           mHidlEnumerator(svc) {}
~IHidlEnumerator()92     virtual ~IHidlEnumerator() { mHidlEnumerator = nullptr; }
93 
94 protected:
95     ::android::sp<hidlevs::V1_0::IEvsEnumerator> mHidlEnumerator;
96     ::android::wp<hidlevs::V1_0::IEvsDisplay> mActiveHidlDisplay;
97 };
98 
99 class AidlEnumerator::ImplV0 final : public IHidlEnumerator {
100 public:
101     virtual ::ndk::ScopedAStatus closeCamera(
102             const ::android::sp<hidlevs::V1_0::IEvsCamera>& cameraObj) override;
103     virtual ::ndk::ScopedAStatus closeDisplay(
104             const ::android::sp<hidlevs::V1_0::IEvsDisplay>& display) override;
105     virtual ::ndk::ScopedAStatus getCameraList(
106             std::vector<aidlevs::CameraDesc>* _aidl_return) override;
107     virtual ::ndk::ScopedAStatus getDisplayIdList(std::vector<uint8_t>* list) override;
108     virtual ::ndk::ScopedAStatus openCamera(const std::string& cameraId,
109                                             const aidlevs::Stream& streamConfig,
110                                             std::shared_ptr<aidlevs::IEvsCamera>* obj) override;
111     virtual ::android::sp<hidlevs::V1_0::IEvsDisplay> openDisplay(int32_t displayId) override;
112 
ImplV0(const::android::sp<hidlevs::V1_0::IEvsEnumerator> & svc)113     explicit ImplV0(const ::android::sp<hidlevs::V1_0::IEvsEnumerator>& svc) :
114           IHidlEnumerator(svc) {}
115 };
116 
117 class AidlEnumerator::ImplV1 final : public IHidlEnumerator {
118 public:
119     virtual ::ndk::ScopedAStatus closeCamera(
120             const ::android::sp<hidlevs::V1_0::IEvsCamera>& cameraObj) override;
121     virtual ::ndk::ScopedAStatus closeDisplay(
122             const ::android::sp<hidlevs::V1_0::IEvsDisplay>& display) override;
123     virtual ::ndk::ScopedAStatus getCameraList(
124             std::vector<aidlevs::CameraDesc>* _aidl_return) override;
125     virtual ::ndk::ScopedAStatus getDisplayIdList(std::vector<uint8_t>* list) override;
126     virtual ::ndk::ScopedAStatus openCamera(const std::string& cameraId,
127                                             const aidlevs::Stream& streamConfig,
128                                             std::shared_ptr<aidlevs::IEvsCamera>* obj) override;
129     virtual ::android::sp<hidlevs::V1_0::IEvsDisplay> openDisplay(int32_t displayId) override;
130 
ImplV1(const::android::sp<hidlevs::V1_1::IEvsEnumerator> & svc)131     explicit ImplV1(const ::android::sp<hidlevs::V1_1::IEvsEnumerator>& svc) :
132           IHidlEnumerator(svc), mHidlEnumerator(svc) {}
~ImplV1()133     virtual ~ImplV1() { mHidlEnumerator = nullptr; }
134 
135 private:
136     ::android::sp<hidlevs::V1_1::IEvsEnumerator> mHidlEnumerator;
137 };
138 
139 }  // namespace aidl::android::automotive::evs::implementation
140 
141 #endif  // CPP_EVS_MANAGER_AIDL_WRAPPERS_INCLUDE_AIDLENUMERATOR_H
142