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