xref: /aosp_15_r20/hardware/interfaces/automotive/evs/1.1/default/ConfigManager.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 #ifndef ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_CONFIGMANAGER_H
17 #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_CONFIGMANAGER_H
18 
19 #include "ConfigManagerUtil.h"
20 
21 #include <android/hardware/automotive/evs/1.1/types.h>
22 #include <log/log.h>
23 #include <system/camera_metadata.h>
24 #include <tinyxml2.h>
25 
26 #include <string>
27 #include <unordered_map>
28 #include <unordered_set>
29 #include <vector>
30 
31 namespace android::hardware::automotive::evs::V1_1::implementation {
32 
33 using hardware::hidl_vec;
34 using hardware::automotive::evs::V1_1::CameraParam;
35 using hardware::camera::device::V3_2::Stream;
36 
37 /*
38  * Plese note that this is different from what is defined in
39  * libhardware/modules/camera/3_4/metadata/types.h; this has one additional
40  * field to store a framerate.
41  */
42 const size_t kStreamCfgSz = 6;
43 typedef std::array<int32_t, kStreamCfgSz> RawStreamConfiguration;
44 
45 class ConfigManager {
46   public:
47     static std::unique_ptr<ConfigManager> Create(const char* path = "");
48     ConfigManager(const ConfigManager&) = delete;
49     ConfigManager& operator=(const ConfigManager&) = delete;
50 
51     virtual ~ConfigManager();
52 
53     /* Camera device's capabilities and metadata */
54     class CameraInfo {
55       public:
CameraInfo()56         CameraInfo() : characteristics(nullptr) { /* Nothing to do */
57         }
58 
~CameraInfo()59         virtual ~CameraInfo() { free_camera_metadata(characteristics); }
60 
61         /* Allocate memory for camera_metadata_t */
allocate(size_t entry_cap,size_t data_cap)62         bool allocate(size_t entry_cap, size_t data_cap) {
63             if (characteristics != nullptr) {
64                 ALOGE("Camera metadata is already allocated");
65                 return false;
66             }
67 
68             characteristics = allocate_camera_metadata(entry_cap, data_cap);
69             return characteristics != nullptr;
70         }
71 
72         /*
73          * List of supported controls that the primary client can program.
74          * Paraemters are stored with its valid range
75          */
76         std::unordered_map<CameraParam, std::tuple<int32_t, int32_t, int32_t>> controls;
77 
78         /*
79          * List of supported output stream configurations; each array stores
80          * format, width, height, and direction values in the order.
81          */
82         std::unordered_map<int32_t, RawStreamConfiguration> streamConfigurations;
83 
84         /*
85          * Internal storage for camera metadata.  Each entry holds a pointer to
86          * data and number of elements
87          */
88         std::unordered_map<camera_metadata_tag_t, std::pair<std::unique_ptr<void*>, size_t>>
89                 cameraMetadata;
90 
91         /* Camera module characteristics */
92         camera_metadata_t* characteristics;
93     };
94 
95     class CameraGroupInfo : public CameraInfo {
96       public:
CameraGroupInfo()97         CameraGroupInfo() {}
98 
99         /* ID of member camera devices */
100         std::unordered_set<std::string> devices;
101 
102         /* The capture operation of member camera devices are synchronized */
103         bool synchronized = false;
104     };
105 
106     class SystemInfo {
107       public:
108         /* number of available cameras */
109         int32_t numCameras = 0;
110     };
111 
112     class DisplayInfo {
113       public:
114         /*
115          * List of supported input stream configurations; each array stores
116          * format, width, height, and direction values in the order.
117          */
118         std::unordered_map<int32_t, RawStreamConfiguration> streamConfigurations;
119     };
120 
121     /*
122      * Return system information
123      *
124      * @return SystemInfo
125      *         Constant reference of SystemInfo.
126      */
getSystemInfo()127     const SystemInfo& getSystemInfo() { return mSystemInfo; }
128 
129     /*
130      * Return a list of cameras
131      *
132      * This function assumes that it is not being called frequently.
133      *
134      * @return std::vector<std::string>
135      *         A vector that contains unique camera device identifiers.
136      */
getCameraList()137     std::vector<std::string> getCameraList() {
138         std::vector<std::string> aList;
139         for (auto& v : mCameraInfo) {
140             aList.push_back(v.first);
141         }
142 
143         return aList;
144     }
145 
146     /*
147      * Return a list of cameras
148      *
149      * @return CameraGroupInfo
150      *         A pointer to a camera group identified by a given id.
151      */
getCameraGroupInfo(const std::string & gid)152     std::unique_ptr<CameraGroupInfo>& getCameraGroupInfo(const std::string& gid) {
153         return mCameraGroupInfos[gid];
154     }
155 
156     /*
157      * Return a camera metadata
158      *
159      * @param  cameraId
160      *         Unique camera node identifier in std::string
161      *
162      * @return std::unique_ptr<CameraInfo>
163      *         A pointer to CameraInfo that is associated with a given camera
164      *         ID.  This returns a null pointer if this does not recognize a
165      *         given camera identifier.
166      */
getCameraInfo(const std::string cameraId)167     std::unique_ptr<CameraInfo>& getCameraInfo(const std::string cameraId) noexcept {
168         return mCameraInfo[cameraId];
169     }
170 
171   private:
172     /* Constructors */
ConfigManager(const char * xmlPath)173     ConfigManager(const char* xmlPath) : mConfigFilePath(xmlPath) {}
174 
175     /* System configuration */
176     SystemInfo mSystemInfo;
177 
178     /* Internal data structure for camera device information */
179     std::unordered_map<std::string, std::unique_ptr<CameraInfo>> mCameraInfo;
180 
181     /* Internal data structure for camera device information */
182     std::unordered_map<std::string, std::unique_ptr<DisplayInfo>> mDisplayInfo;
183 
184     /* Camera groups are stored in <groud id, CameraGroupInfo> hash map */
185     std::unordered_map<std::string, std::unique_ptr<CameraGroupInfo>> mCameraGroupInfos;
186 
187     /*
188      * Camera positions are stored in <position, camera id set> hash map.
189      * The position must be one of front, rear, left, and right.
190      */
191     std::unordered_map<std::string, std::unordered_set<std::string>> mCameraPosition;
192 
193     /* A path to XML configuration file */
194     const char* mConfigFilePath;
195 
196     /*
197      * Parse a given EVS configuration file and store the information
198      * internally.
199      *
200      * @return bool
201      *         True if it completes parsing a file successfully.
202      */
203     bool readConfigDataFromXML() noexcept;
204 
205     /*
206      * read the information of the vehicle
207      *
208      * @param  aSysElem
209      *         A pointer to "system" XML element.
210      */
211     void readSystemInfo(const tinyxml2::XMLElement* const aSysElem);
212 
213     /*
214      * read the information of camera devices
215      *
216      * @param  aCameraElem
217      *         A pointer to "camera" XML element that may contain multiple
218      *         "device" elements.
219      */
220     void readCameraInfo(const tinyxml2::XMLElement* const aCameraElem);
221 
222     /*
223      * read display device information
224      *
225      * @param  aDisplayElem
226      *         A pointer to "display" XML element that may contain multiple
227      *         "device" elements.
228      */
229     void readDisplayInfo(const tinyxml2::XMLElement* const aDisplayElem);
230 
231     /*
232      * read camera device information
233      *
234      * @param  aCamera
235      *         A pointer to CameraInfo that will be completed by this
236      *         method.
237      *         aDeviceElem
238      *         A pointer to "device" XML element that contains camera module
239      *         capability info and its characteristics.
240      *
241      * @return bool
242      *         Return false upon any failure in reading and processing camera
243      *         device information.
244      */
245     bool readCameraDeviceInfo(CameraInfo* aCamera, const tinyxml2::XMLElement* aDeviceElem);
246 
247     /*
248      * read camera metadata
249      *
250      * @param  aCapElem
251      *         A pointer to "cap" XML element.
252      * @param  aCamera
253      *         A pointer to CameraInfo that is being filled by this method.
254      * @param  dataSize
255      *         Required size of memory to store camera metadata found in this
256      *         method.  This is calculated in this method and returned to the
257      *         caller for camera_metadata allocation.
258      *
259      * @return size_t
260      *         Number of camera metadata entries
261      */
262     size_t readCameraCapabilities(const tinyxml2::XMLElement* const aCapElem, CameraInfo* aCamera,
263                                   size_t& dataSize);
264 
265     /*
266      * read camera metadata
267      *
268      * @param  aParamElem
269      *         A pointer to "characteristics" XML element.
270      * @param  aCamera
271      *         A pointer to CameraInfo that is being filled by this method.
272      * @param  dataSize
273      *         Required size of memory to store camera metadata found in this
274      *         method.
275      *
276      * @return size_t
277      *         Number of camera metadata entries
278      */
279     size_t readCameraMetadata(const tinyxml2::XMLElement* const aParamElem, CameraInfo* aCamera,
280                               size_t& dataSize);
281 
282     /*
283      * construct camera_metadata_t from camera capabilities and metadata
284      *
285      * @param  aCamera
286      *         A pointer to CameraInfo that is being filled by this method.
287      * @param  totalEntries
288      *         Number of camera metadata entries to be added.
289      * @param  totalDataSize
290      *         Sum of sizes of camera metadata entries to be added.
291      *
292      * @return bool
293      *         False if either it fails to allocate memory for camera metadata
294      *         or its size is not large enough to add all found camera metadata
295      *         entries.
296      */
297     bool constructCameraMetadata(CameraInfo* aCamera, const size_t totalEntries,
298                                  const size_t totalDataSize);
299 };
300 
301 }  // namespace android::hardware::automotive::evs::V1_1::implementation
302 #endif  // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_CONFIGMANAGER_H
303