1c5e268c6Sopenharmony_ci/*
2c5e268c6Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3c5e268c6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c5e268c6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c5e268c6Sopenharmony_ci * You may obtain a copy of the License at
6c5e268c6Sopenharmony_ci *
7c5e268c6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c5e268c6Sopenharmony_ci *
9c5e268c6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c5e268c6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c5e268c6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c5e268c6Sopenharmony_ci * See the License for the specific language governing permissions and
13c5e268c6Sopenharmony_ci * limitations under the License.
14c5e268c6Sopenharmony_ci */
15c5e268c6Sopenharmony_ci
16c5e268c6Sopenharmony_ci#include "camera_metadata_info.h"
17c5e268c6Sopenharmony_ci#include <securec.h>
18c5e268c6Sopenharmony_ci#include "metadata_log.h"
19c5e268c6Sopenharmony_ci#include <dlfcn.h>
20c5e268c6Sopenharmony_ci#include <memory>
21c5e268c6Sopenharmony_ci#include <vector>
22c5e268c6Sopenharmony_ci#include <map>
23c5e268c6Sopenharmony_ci#include "camera_metadata_item_info.h"
24c5e268c6Sopenharmony_ci#include "camera_vendor_tag.h"
25c5e268c6Sopenharmony_ci
26c5e268c6Sopenharmony_cinamespace OHOS::Camera {
27c5e268c6Sopenharmony_cistatic CameraVendorTag* g_vendorTagImpl = nullptr;
28c5e268c6Sopenharmony_ciconst char* g_exampleVendorTagLib = "libcamera_example_vendor_tag_impl.z.so";
29c5e268c6Sopenharmony_ciconst char* g_vendorTagLib = "libcamera_vendor_tag_impl.z.so";
30c5e268c6Sopenharmony_ciconst int METADATA_HEADER_DATA_SIZE = 4;
31c5e268c6Sopenharmony_ciconst uint32_t itemLen = sizeof(camera_metadata_item_entry_t);
32c5e268c6Sopenharmony_ciconst std::vector<uint32_t> g_metadataTags = {
33c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_POSITION,
34c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_TYPE,
35c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_CONNECTION_TYPE,
36c5e268c6Sopenharmony_ci    OHOS_ABILITY_MEMORY_TYPE,
37c5e268c6Sopenharmony_ci    OHOS_ABILITY_FPS_RANGES,
38c5e268c6Sopenharmony_ci    OHOS_ABILITY_FLASH_AVAILABLE,
39c5e268c6Sopenharmony_ci    OHOS_CAMERA_STREAM_ID,
40c5e268c6Sopenharmony_ci    OHOS_ABILITY_PRELAUNCH_AVAILABLE,
41c5e268c6Sopenharmony_ci    OHOS_ABILITY_CUSTOM_VIDEO_FPS,
42c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_MODES,
43c5e268c6Sopenharmony_ci    OHOS_ABILITY_SKETCH_ENABLE_RATIO,
44c5e268c6Sopenharmony_ci    OHOS_ABILITY_SKETCH_REFERENCE_FOV_RATIO,
45c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_FOLDSCREEN_TYPE,
46c5e268c6Sopenharmony_ci    OHOS_ABILITY_AVAILABLE_COLOR_SPACES,
47c5e268c6Sopenharmony_ci    OHOS_ABILITY_NIGHT_MODE_SUPPORTED_EXPOSURE_TIME,
48c5e268c6Sopenharmony_ci    OHOS_CAMERA_MESURE_EXPOSURE_TIME,
49c5e268c6Sopenharmony_ci    OHOS_CAMERA_EXPOSURE_MODE_PREVIEW_STATE,
50c5e268c6Sopenharmony_ci    OHOS_CAMERA_USER_ID,
51c5e268c6Sopenharmony_ci    OHOS_ABILITY_MOON_CAPTURE_BOOST,
52c5e268c6Sopenharmony_ci    OHOS_CONTROL_MOON_CAPTURE_BOOST,
53c5e268c6Sopenharmony_ci    OHOS_STATUS_MOON_CAPTURE_DETECTION,
54c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAPTURE_DURATION_SUPPORTED,
55c5e268c6Sopenharmony_ci    OHOS_CAMERA_CUSTOM_SNAPSHOT_DURATION,
56c5e268c6Sopenharmony_ci    OHOS_ABILITY_MOVING_PHOTO,
57c5e268c6Sopenharmony_ci    OHOS_ABILITY_FLASH_SUGGESTION_SUPPORTED,
58c5e268c6Sopenharmony_ci    OHOS_CONTROL_FLASH_SUGGESTION_SWITCH,
59c5e268c6Sopenharmony_ci    OHOS_STATUS_FLASH_SUGGESTION,
60c5e268c6Sopenharmony_ci    OHOS_ABILITY_HIGH_QUALITY_SUPPORT,
61c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_FOLD_STATUS,
62c5e268c6Sopenharmony_ci    OHOS_STATUS_CAMERA_OCCLUSION_DETECTION,
63c5e268c6Sopenharmony_ci    OHOS_ABILITY_STATISTICS_DETECT_TYPE,
64c5e268c6Sopenharmony_ci    OHOS_CONTROL_STATISTICS_DETECT_SETTING,
65c5e268c6Sopenharmony_ci    OHOS_ABILITY_AVAILABLE_EXTENDED_STREAM_INFO_TYPES,
66c5e268c6Sopenharmony_ci    OHOS_ABILITY_AUTO_DEFERRED_VIDEO_ENHANCE,
67c5e268c6Sopenharmony_ci    OHOS_CONTROL_AUTO_DEFERRED_VIDEO_ENHANCE,
68c5e268c6Sopenharmony_ci    OHOS_ABILITY_AUTO_CLOUD_IMAGE_ENHANCE,
69c5e268c6Sopenharmony_ci    OHOS_CONTROL_AUTO_CLOUD_IMAGE_ENHANCE,
70c5e268c6Sopenharmony_ci    OHOS_ABILITY_TRIPOD_DETECTION,
71c5e268c6Sopenharmony_ci    OHOS_CONTROL_TRIPOD_DETECTION,
72c5e268c6Sopenharmony_ci    OHOS_CONTROL_TRIPOD_STABLITATION,
73c5e268c6Sopenharmony_ci    OHOS_STATUS_TRIPOD_DETECTION_STATUS,
74c5e268c6Sopenharmony_ci    OHOS_STATUS_SKETCH_POINT,
75c5e268c6Sopenharmony_ci    OHOS_ABILITY_LOW_LIGHT_BOOST,
76c5e268c6Sopenharmony_ci    OHOS_CONTROL_LOW_LIGHT_DETECT,
77c5e268c6Sopenharmony_ci    OHOS_CONTROL_LOW_LIGHT_BOOST,
78c5e268c6Sopenharmony_ci    OHOS_STATUS_LOW_LIGHT_DETECTION,
79c5e268c6Sopenharmony_ci    OHOS_STATUS_CAMERA_LENS_DIRTY_DETECTION,
80c5e268c6Sopenharmony_ci    OHOS_DEVICE_PROTECTION_STATE,
81c5e268c6Sopenharmony_ci
82c5e268c6Sopenharmony_ci    OHOS_SENSOR_EXPOSURE_TIME,
83c5e268c6Sopenharmony_ci    OHOS_SENSOR_COLOR_CORRECTION_GAINS,
84c5e268c6Sopenharmony_ci    OHOS_SENSOR_ORIENTATION,
85c5e268c6Sopenharmony_ci    OHOS_ABILITY_MUTE_MODES,
86c5e268c6Sopenharmony_ci    OHOS_CONTROL_MUTE_MODE,
87c5e268c6Sopenharmony_ci
88c5e268c6Sopenharmony_ci    OHOS_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
89c5e268c6Sopenharmony_ci    OHOS_SENSOR_INFO_SENSITIVITY_RANGE,
90c5e268c6Sopenharmony_ci    OHOS_SENSOR_INFO_MAX_FRAME_DURATION,
91c5e268c6Sopenharmony_ci    OHOS_SENSOR_INFO_PHYSICAL_SIZE,
92c5e268c6Sopenharmony_ci    OHOS_SENSOR_INFO_PIXEL_ARRAY_SIZE,
93c5e268c6Sopenharmony_ci    OHOS_SENSOR_INFO_TIMESTAMP,
94c5e268c6Sopenharmony_ci
95c5e268c6Sopenharmony_ci    OHOS_STATISTICS_FACE_DETECT_MODE,
96c5e268c6Sopenharmony_ci    OHOS_STATISTICS_FACE_DETECT_SWITCH,
97c5e268c6Sopenharmony_ci    OHOS_STATISTICS_FACE_DETECT_MAX_NUM,
98c5e268c6Sopenharmony_ci    OHOS_STATISTICS_HISTOGRAM_MODE,
99c5e268c6Sopenharmony_ci    OHOS_STATISTICS_FACE_IDS,
100c5e268c6Sopenharmony_ci    OHOS_STATISTICS_FACE_LANDMARKS,
101c5e268c6Sopenharmony_ci    OHOS_STATISTICS_FACE_RECTANGLES,
102c5e268c6Sopenharmony_ci    OHOS_STATISTICS_FACE_SCORES,
103c5e268c6Sopenharmony_ci    OHOS_STATISTICS_DETECT_HUMAN_FACE_INFOS,
104c5e268c6Sopenharmony_ci    OHOS_STATISTICS_DETECT_HUMAN_BODY_INFOS,
105c5e268c6Sopenharmony_ci    OHOS_STATISTICS_DETECT_CAT_FACE_INFOS,
106c5e268c6Sopenharmony_ci    OHOS_STATISTICS_DETECT_CAT_BODY_INFOS,
107c5e268c6Sopenharmony_ci    OHOS_STATISTICS_DETECT_DOG_FACE_INFOS,
108c5e268c6Sopenharmony_ci    OHOS_STATISTICS_DETECT_DOG_BODY_INFOS,
109c5e268c6Sopenharmony_ci    OHOS_STATISTICS_DETECT_SALIENT_INFOS,
110c5e268c6Sopenharmony_ci    OHOS_STATISTICS_DETECT_BAR_CODE_INFOS,
111c5e268c6Sopenharmony_ci    OHOS_STATISTICS_DETECT_BASE_FACE_INFO,
112c5e268c6Sopenharmony_ci
113c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_ANTIBANDING_MODE,
114c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_EXPOSURE_COMPENSATION,
115c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_LOCK,
116c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_MODE,
117c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_REGIONS,
118c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_TARGET_FPS_RANGE,
119c5e268c6Sopenharmony_ci    OHOS_CONTROL_FPS_RANGES,
120c5e268c6Sopenharmony_ci    OHOS_CONTROL_AF_MODE,
121c5e268c6Sopenharmony_ci    OHOS_CONTROL_AF_REGIONS,
122c5e268c6Sopenharmony_ci    OHOS_CONTROL_AF_TRIGGER,
123c5e268c6Sopenharmony_ci    OHOS_CONTROL_AF_TRIGGER_ID,
124c5e268c6Sopenharmony_ci    OHOS_CONTROL_AF_STATE,
125c5e268c6Sopenharmony_ci    OHOS_CONTROL_AWB_LOCK,
126c5e268c6Sopenharmony_ci    OHOS_CONTROL_AWB_MODE,
127c5e268c6Sopenharmony_ci    OHOS_CONTROL_AWB_REGIONS,
128c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
129c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_AVAILABLE_MODES,
130c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
131c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_COMPENSATION_RANGE,
132c5e268c6Sopenharmony_ci    OHOS_CONTROL_AE_COMPENSATION_STEP,
133c5e268c6Sopenharmony_ci    OHOS_ABILITY_AE_COMPENSATION_RANGE,
134c5e268c6Sopenharmony_ci    OHOS_ABILITY_AE_COMPENSATION_STEP,
135c5e268c6Sopenharmony_ci    OHOS_CONTROL_AF_AVAILABLE_MODES,
136c5e268c6Sopenharmony_ci    OHOS_CONTROL_AWB_AVAILABLE_MODES,
137c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAPTURE_MIRROR_SUPPORTED,
138c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAPTURE_MIRROR,
139c5e268c6Sopenharmony_ci    OHOS_CONTROL_FOCUS_STATE,
140c5e268c6Sopenharmony_ci    OHOS_CONTROL_METER_POINT,
141c5e268c6Sopenharmony_ci    OHOS_CONTROL_METER_MODE,
142c5e268c6Sopenharmony_ci    OHOS_CONTROL_EXPOSURE_STATE,
143c5e268c6Sopenharmony_ci    OHOS_CONTROL_FOCUSED_POINT,
144c5e268c6Sopenharmony_ci    OHOS_CONTROL_NIGHT_MODE_TRY_AE,
145c5e268c6Sopenharmony_ci    OHOS_CONTROL_MANUAL_EXPOSURE_TIME,
146c5e268c6Sopenharmony_ci    OHOS_STATUS_CAMERA_CURRENT_FPS,
147c5e268c6Sopenharmony_ci    OHOS_ABILITY_ISO_VALUES,
148c5e268c6Sopenharmony_ci    OHOS_CONTROL_ISO_VALUE,
149c5e268c6Sopenharmony_ci    OHOS_STATUS_ISO_VALUE,
150c5e268c6Sopenharmony_ci    OHOS_ABILITY_SENSOR_EXPOSURE_TIME_RANGE,
151c5e268c6Sopenharmony_ci    OHOS_CONTROL_SENSOR_EXPOSURE_TIME,
152c5e268c6Sopenharmony_ci    OHOS_STATUS_SENSOR_EXPOSURE_TIME,
153c5e268c6Sopenharmony_ci    OHOS_CONTROL_MOVING_PHOTO,
154c5e268c6Sopenharmony_ci    OHOS_ABILITY_SENSOR_WB_VALUES,
155c5e268c6Sopenharmony_ci    OHOS_CONTROL_SENSOR_WB_VALUE,
156c5e268c6Sopenharmony_ci    OHOS_CONTROL_HIGH_QUALITY_MODE,
157c5e268c6Sopenharmony_ci    OHOS_CONTROL_BURST_CAPTURE,
158c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAMERA_USED_AS_POSITION,
159c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAMERA_SESSION_USAGE,
160c5e268c6Sopenharmony_ci    OHOS_CONTROL_EJECT_RETRY,
161c5e268c6Sopenharmony_ci    OHOS_CONTROL_FALL_PROTECTION,
162c5e268c6Sopenharmony_ci
163c5e268c6Sopenharmony_ci    // Camera device image acquisition related
164c5e268c6Sopenharmony_ci    OHOS_ABILITY_DEVICE_AVAILABLE_EXPOSUREMODES,
165c5e268c6Sopenharmony_ci    OHOS_CONTROL_EXPOSUREMODE,
166c5e268c6Sopenharmony_ci    OHOS_ABILITY_EXPOSURE_MODES,
167c5e268c6Sopenharmony_ci    OHOS_CONTROL_EXPOSURE_MODE,
168c5e268c6Sopenharmony_ci    OHOS_ABILITY_METER_MODES,
169c5e268c6Sopenharmony_ci    OHOS_ABILITY_SCENE_EXPOSURE_MODES,
170c5e268c6Sopenharmony_ci    OHOS_ABILITY_EXPOSURE_TIME,
171c5e268c6Sopenharmony_ci    OHOS_ABILITY_AE_LOCK,
172c5e268c6Sopenharmony_ci
173c5e268c6Sopenharmony_ci    OHOS_ABILITY_DEVICE_AVAILABLE_FOCUSMODES,
174c5e268c6Sopenharmony_ci    OHOS_CONTROL_FOCUSMODE,
175c5e268c6Sopenharmony_ci    OHOS_ABILITY_FOCUS_MODES,
176c5e268c6Sopenharmony_ci    OHOS_CONTROL_FOCUS_MODE,
177c5e268c6Sopenharmony_ci    OHOS_ABILITY_FOCAL_LENGTH,
178c5e268c6Sopenharmony_ci    OHOS_ABILITY_SCENE_FOCUS_MODES,
179c5e268c6Sopenharmony_ci    OHOS_ABILITY_FOCUS_ASSIST_FLASH_SUPPORTED_MODES,
180c5e268c6Sopenharmony_ci    OHOS_CONTROL_FOCUS_ASSIST_FLASH_SUPPORTED_MODE,
181c5e268c6Sopenharmony_ci    OHOS_ABILITY_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
182c5e268c6Sopenharmony_ci    OHOS_CONTROL_LENS_FOCUS_DISTANCE,
183c5e268c6Sopenharmony_ci    OHOS_ABILITY_EQUIVALENT_FOCUS,
184c5e268c6Sopenharmony_ci    OHOS_CONTROL_FOCUS_DISTANCE,
185c5e268c6Sopenharmony_ci
186c5e268c6Sopenharmony_ci    OHOS_ABILITY_DEVICE_AVAILABLE_AWBMODES,
187c5e268c6Sopenharmony_ci    OHOS_ABILITY_AWB_MODES,
188c5e268c6Sopenharmony_ci    OHOS_ABILITY_AWB_LOCK,
189c5e268c6Sopenharmony_ci    OHOS_ABILITY_DEVICE_AVAILABLE_FLASHMODES,
190c5e268c6Sopenharmony_ci    OHOS_CONTROL_FLASHMODE,
191c5e268c6Sopenharmony_ci    OHOS_ABILITY_FLASH_MODES,
192c5e268c6Sopenharmony_ci    OHOS_CONTROL_FLASH_MODE,
193c5e268c6Sopenharmony_ci    OHOS_CONTROL_FLASH_STATE,
194c5e268c6Sopenharmony_ci    OHOS_ABILITY_SCENE_FLASH_MODES,
195c5e268c6Sopenharmony_ci
196c5e268c6Sopenharmony_ci    OHOS_ABILITY_ZOOM_RATIO_RANGE,
197c5e268c6Sopenharmony_ci    OHOS_CONTROL_ZOOM_RATIO,
198c5e268c6Sopenharmony_ci    OHOS_CONTROL_ZOOM_CROP_REGION,
199c5e268c6Sopenharmony_ci    OHOS_ABILITY_ZOOM_CAP,
200c5e268c6Sopenharmony_ci    OHOS_ABILITY_SCENE_ZOOM_CAP,
201c5e268c6Sopenharmony_ci    OHOS_STATUS_CAMERA_CURRENT_ZOOM_RATIO,
202c5e268c6Sopenharmony_ci    OHOS_CONTROL_SMOOTH_ZOOM_RATIOS,
203c5e268c6Sopenharmony_ci    OHOS_CONTROL_PREPARE_ZOOM,
204c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_ZOOM_PERFORMANCE,
205c5e268c6Sopenharmony_ci
206c5e268c6Sopenharmony_ci    // Camera flow control related
207c5e268c6Sopenharmony_ci    OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS,
208c5e268c6Sopenharmony_ci    OHOS_STREAM_AVAILABLE_FORMATS,
209c5e268c6Sopenharmony_ci    OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS,
210c5e268c6Sopenharmony_ci    OHOS_ABILITY_STREAM_QUICK_THUMBNAIL_AVAILABLE,
211c5e268c6Sopenharmony_ci    OHOS_ABILITY_PORTRAIT_RESOLUSION,
212c5e268c6Sopenharmony_ci    OHOS_ABILITY_SCENE_STREAM_QUICK_THUMBNAIL_AVAILABLE,
213c5e268c6Sopenharmony_ci    OHOS_ABILITY_DEFERRED_IMAGE_DELIVERY,
214c5e268c6Sopenharmony_ci    OHOS_CONTROL_DEFERRED_IMAGE_DELIVERY,
215c5e268c6Sopenharmony_ci
216c5e268c6Sopenharmony_ci    OHOS_JPEG_GPS_COORDINATES,
217c5e268c6Sopenharmony_ci    OHOS_JPEG_GPS_PROCESSING_METHOD,
218c5e268c6Sopenharmony_ci    OHOS_JPEG_GPS_TIMESTAMP,
219c5e268c6Sopenharmony_ci    OHOS_JPEG_ORIENTATION,
220c5e268c6Sopenharmony_ci    OHOS_JPEG_QUALITY,
221c5e268c6Sopenharmony_ci    OHOS_JPEG_THUMBNAIL_QUALITY,
222c5e268c6Sopenharmony_ci    OHOS_JPEG_THUMBNAIL_SIZE,
223c5e268c6Sopenharmony_ci    OHOS_JPEG_AVAILABLE_THUMBNAIL_SIZES,
224c5e268c6Sopenharmony_ci    OHOS_JPEG_MAX_SIZE,
225c5e268c6Sopenharmony_ci    OHOS_JPEG_SIZE,
226c5e268c6Sopenharmony_ci
227c5e268c6Sopenharmony_ci    OHOS_ABILITY_VIDEO_STABILIZATION_MODES,
228c5e268c6Sopenharmony_ci    OHOS_CONTROL_VIDEO_STABILIZATION_MODE,
229c5e268c6Sopenharmony_ci    OHOS_CONTROL_VIDEO_DEBUG_SWITCH,
230c5e268c6Sopenharmony_ci
231c5e268c6Sopenharmony_ci    // camera effects & algorithms
232c5e268c6Sopenharmony_ci    OHOS_ABILITY_SCENE_FILTER_TYPES,
233c5e268c6Sopenharmony_ci    OHOS_CONTROL_FILTER_TYPE,
234c5e268c6Sopenharmony_ci    OHOS_ABILITY_SCENE_PORTRAIT_EFFECT_TYPES,
235c5e268c6Sopenharmony_ci    OHOS_CONTROL_PORTRAIT_EFFECT_TYPE,
236c5e268c6Sopenharmony_ci    OHOS_ABILITY_SCENE_BEAUTY_TYPES,
237c5e268c6Sopenharmony_ci    OHOS_CONTROL_BEAUTY_TYPE,
238c5e268c6Sopenharmony_ci    OHOS_ABILITY_BEAUTY_AUTO_VALUES,
239c5e268c6Sopenharmony_ci    OHOS_CONTROL_BEAUTY_AUTO_VALUE,
240c5e268c6Sopenharmony_ci    OHOS_ABILITY_BEAUTY_FACE_SLENDER_VALUES,
241c5e268c6Sopenharmony_ci    OHOS_CONTROL_BEAUTY_FACE_SLENDER_VALUE,
242c5e268c6Sopenharmony_ci    OHOS_ABILITY_BEAUTY_SKIN_TONE_VALUES,
243c5e268c6Sopenharmony_ci    OHOS_CONTROL_BEAUTY_SKIN_TONE_VALUE,
244c5e268c6Sopenharmony_ci    OHOS_ABILITY_BEAUTY_SKIN_SMOOTH_VALUES,
245c5e268c6Sopenharmony_ci    OHOS_CONTROL_BEAUTY_SKIN_SMOOTH_VALUE,
246c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_MACRO_SUPPORTED,
247c5e268c6Sopenharmony_ci    OHOS_ABILITY_SCENE_MACRO_CAP,
248c5e268c6Sopenharmony_ci    OHOS_CAMERA_MACRO_STATUS,
249c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAMERA_MACRO,
250c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_VIRTUAL_APERTURE_RANGE,
251c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAMERA_VIRTUAL_APERTURE_VALUE,
252c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_PHYSICAL_APERTURE_RANGE,
253c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAMERA_PHYSICAL_APERTURE_VALUE,
254c5e268c6Sopenharmony_ci    OHOS_STATUS_CAMERA_APERTURE_VALUE,
255c5e268c6Sopenharmony_ci    OHOS_DEVICE_EXITCAMERA_EVENT,
256c5e268c6Sopenharmony_ci    OHOS_CONTROL_MOTION_DETECTION_CHECK_AREA,
257c5e268c6Sopenharmony_ci    OHOS_STATUS_SLOW_MOTION_DETECTION,
258c5e268c6Sopenharmony_ci    OHOS_ABILITY_MOTION_DETECTION_SUPPORT,
259c5e268c6Sopenharmony_ci    OHOS_CONTROL_MOTION_DETECTION,
260c5e268c6Sopenharmony_ci    OHOS_ABILITY_EXPOSURE_HINT_SUPPORTED,
261c5e268c6Sopenharmony_ci    OHOS_CONTROL_EXPOSURE_HINT_MODE,
262c5e268c6Sopenharmony_ci    OHOS_STATUS_ALGO_MEAN_Y,
263c5e268c6Sopenharmony_ci    OHOS_STATUS_PREVIEW_PHYSICAL_CAMERA_ID,
264c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAPTURE_EXPECT_TIME,
265c5e268c6Sopenharmony_ci    OHOS_ABILITY_EFFECT_SUGGESTION_SUPPORTED,
266c5e268c6Sopenharmony_ci    OHOS_CONTROL_EFFECT_SUGGESTION,
267c5e268c6Sopenharmony_ci    OHOS_CONTROL_EFFECT_SUGGESTION_DETECTION,
268c5e268c6Sopenharmony_ci    OHOS_CAMERA_EFFECT_SUGGESTION_TYPE,
269c5e268c6Sopenharmony_ci    OHOS_CONTROL_EFFECT_SUGGESTION_TYPE,
270c5e268c6Sopenharmony_ci    OHOS_ABILITY_AVAILABLE_PROFILE_LEVEL,
271c5e268c6Sopenharmony_ci    OHOS_ABILITY_AVAILABLE_PREVIEW_PROFILE,
272c5e268c6Sopenharmony_ci    OHOS_ABILITY_AVAILABLE_PHOTO_PROFILE,
273c5e268c6Sopenharmony_ci    OHOS_ABILITY_AVAILABLE_VIDEO_PROFILE,
274c5e268c6Sopenharmony_ci    OHOS_ABILITY_AVAILABLE_METADATA_PROFILE,
275c5e268c6Sopenharmony_ci    OHOS_ABILITY_AVAILABLE_CONFIGURATIONS,
276c5e268c6Sopenharmony_ci    OHOS_ABILITY_CONFLICT_CONFIGURATIONS,
277c5e268c6Sopenharmony_ci    OHOS_ABILITY_LIGHT_PAINTING_TYPE,
278c5e268c6Sopenharmony_ci    OHOS_CONTROL_LIGHT_PAINTING_TYPE,
279c5e268c6Sopenharmony_ci    OHOS_CONTROL_LIGHT_PAINTING_FLASH,
280c5e268c6Sopenharmony_ci    OHOS_CONTROL_TIME_LAPSE_INTERVAL,
281c5e268c6Sopenharmony_ci    OHOS_CONTROL_TIME_LAPSE_TRYAE_STATE,
282c5e268c6Sopenharmony_ci    OHOS_STATUS_TIME_LAPSE_PREVIEW_TYPE,
283c5e268c6Sopenharmony_ci    OHOS_STATUS_TIME_LAPSE_TRYAE_HINT,
284c5e268c6Sopenharmony_ci    OHOS_STATUS_TIME_LAPSE_CAPTURE_INTERVAL,
285c5e268c6Sopenharmony_ci    OHOS_STATUS_TIME_LAPSE_TRYAE_DONE,
286c5e268c6Sopenharmony_ci    OHOS_CONTROL_TIME_LAPSE_RECORD_STATE,
287c5e268c6Sopenharmony_ci    OHOS_CONTROL_TIME_LAPSE_PREVIEW_TYPE,
288c5e268c6Sopenharmony_ci    OHOS_ABILITY_TIME_LAPSE_INTERVAL_RANGE,
289c5e268c6Sopenharmony_ci    OHOS_ABILITY_LCD_FLASH,
290c5e268c6Sopenharmony_ci    OHOS_CONTROL_LCD_FLASH_DETECTION,
291c5e268c6Sopenharmony_ci    OHOS_CONTROL_LCD_FLASH,
292c5e268c6Sopenharmony_ci    OHOS_STATUS_LCD_FLASH_STATUS,
293c5e268c6Sopenharmony_ci    OHOS_ABILITY_DEPTH_DATA_DELIVERY,
294c5e268c6Sopenharmony_ci    OHOS_CONTROL_DEPTH_DATA_DELIVERY_SWITCH,
295c5e268c6Sopenharmony_ci    OHOS_ABILITY_DEPTH_DATA_PROFILES,
296c5e268c6Sopenharmony_ci    OHOS_CONTROL_DEPTH_DATA_ACCURACY,
297c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_IS_RETRACTABLE,
298c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAMERA_CLOSE_AFTER_SECONDS,
299c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAPTURE_MACRO_DEPTH_FUSION_SUPPORTED,
300c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAPTURE_MACRO_DEPTH_FUSION_ZOOM_RANGE,
301c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAPTURE_MACRO_DEPTH_FUSION,
302c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_PORTRAIT_THEME_SUPPORTED,
303c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_PORTRAIT_THEME_TYPES,
304c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAMERA_PORTRAIT_THEME_TYPE,
305c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_VIDEO_ROTATION_SUPPORTED,
306c5e268c6Sopenharmony_ci    OHOS_ABILITY_CAMERA_VIDEO_ROTATION,
307c5e268c6Sopenharmony_ci    OHOS_CONTROL_CAMERA_VIDEO_ROTATION,
308c5e268c6Sopenharmony_ci
309c5e268c6Sopenharmony_ci    // camera secure related
310c5e268c6Sopenharmony_ci    OHOS_CONTROL_SECURE_FACE_MODE,
311c5e268c6Sopenharmony_ci    OHOS_CONTROL_SECURE_FACE_INFO,
312c5e268c6Sopenharmony_ci    OHOS_CONTROL_SECURE_FACE_AUTH_RESULT,
313c5e268c6Sopenharmony_ci    OHOS_CONTROL_SECURE_FLASH_SEQ,
314c5e268c6Sopenharmony_ci    OHOS_CONTROL_SECURE_IR_LED_SWITCH,
315c5e268c6Sopenharmony_ci    OHOS_CONTROL_SECURE_IR_LOCKAE_SWITCH,
316c5e268c6Sopenharmony_ci
317c5e268c6Sopenharmony_ci    //XMAGE MODES
318c5e268c6Sopenharmony_ci    OHOS_ABILITY_SUPPORTED_COLOR_MODES,
319c5e268c6Sopenharmony_ci    OHOS_CONTROL_SUPPORTED_COLOR_MODES,
320c5e268c6Sopenharmony_ci};
321c5e268c6Sopenharmony_ci
322c5e268c6Sopenharmony_cistd::map<uint32_t, uint32_t> g_metadataSectionMap = {
323c5e268c6Sopenharmony_ci    {OHOS_CAMERA_PROPERTIES, OHOS_SECTION_CAMERA_PROPERTIES},
324c5e268c6Sopenharmony_ci    {OHOS_CAMERA_SENSOR, OHOS_SECTION_CAMERA_SENSOR},
325c5e268c6Sopenharmony_ci    {OHOS_CAMERA_SENSOR_INFO, OHOS_SECTION_CAMERA_SENSOR_INFO},
326c5e268c6Sopenharmony_ci    {OHOS_CAMERA_STATISTICS, OHOS_SECTION_CAMERA_STATISTICS},
327c5e268c6Sopenharmony_ci    {OHOS_DEVICE_CONTROL, OHOS_SECTION_CAMERA_CONTROL},
328c5e268c6Sopenharmony_ci    {OHOS_DEVICE_EXPOSURE, OHOS_SECTION_DEVICE_EXPOSURE},
329c5e268c6Sopenharmony_ci    {OHOS_DEVICE_FOCUS, OHOS_SECTION_DEVICE_FOCUS},
330c5e268c6Sopenharmony_ci    {OHOS_DEVICE_WHITE_BLANCE, OHOS_SECTION_DEVICE_WHITE},
331c5e268c6Sopenharmony_ci    {OHOS_DEVICE_FLASH, OHOS_SECTION_DEVICE_FLASH},
332c5e268c6Sopenharmony_ci    {OHOS_DEVICE_ZOOM, OHOS_SECTION_DEVICE_ZOOM},
333c5e268c6Sopenharmony_ci    {OHOS_STREAM_ABILITY, OHOS_SECTION_STREAM_ABILITY},
334c5e268c6Sopenharmony_ci    {OHOS_STREAM_JPEG, OHOS_SECTION_STREAM_JPEG},
335c5e268c6Sopenharmony_ci    {OHOS_STREAM_VIDEO, OHOS_SECTION_STREAM_VIDEO},
336c5e268c6Sopenharmony_ci    {OHOS_CAMERA_EFFECT, OHOS_SECTION_CAMERA_EFFECT},
337c5e268c6Sopenharmony_ci    {OHOS_CAMERA_SECURE, OHOS_SECTION_CAMERA_SECURE},
338c5e268c6Sopenharmony_ci    {OHOS_XMAGE_COLOR_ABILITY, OHOS_SECTION_CAMERA_XMAGE}
339c5e268c6Sopenharmony_ci};
340c5e268c6Sopenharmony_ci
341c5e268c6Sopenharmony_ciCameraMetadata::CameraMetadata(size_t itemCapacity, size_t dataCapacity)
342c5e268c6Sopenharmony_ci{
343c5e268c6Sopenharmony_ci    metadata_ = AllocateCameraMetadataBuffer(itemCapacity, AlignTo(dataCapacity, DATA_ALIGNMENT));
344c5e268c6Sopenharmony_ci}
345c5e268c6Sopenharmony_ci
346c5e268c6Sopenharmony_ciCameraMetadata::~CameraMetadata()
347c5e268c6Sopenharmony_ci{
348c5e268c6Sopenharmony_ci    if (metadata_) {
349c5e268c6Sopenharmony_ci        FreeCameraMetadataBuffer(metadata_);
350c5e268c6Sopenharmony_ci        metadata_ = nullptr;
351c5e268c6Sopenharmony_ci    }
352c5e268c6Sopenharmony_ci}
353c5e268c6Sopenharmony_ci
354c5e268c6Sopenharmony_cibool CameraMetadata::addEntry(uint32_t item, const void *data, size_t data_count)
355c5e268c6Sopenharmony_ci{
356c5e268c6Sopenharmony_ci    if (metadata_ == nullptr) {
357c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("metadata_ is null");
358c5e268c6Sopenharmony_ci        return false;
359c5e268c6Sopenharmony_ci    }
360c5e268c6Sopenharmony_ci
361c5e268c6Sopenharmony_ci    auto result = AddCameraMetadataItem(metadata_, item, data, data_count);
362c5e268c6Sopenharmony_ci    if (!result) {
363c5e268c6Sopenharmony_ci        return true;
364c5e268c6Sopenharmony_ci    }
365c5e268c6Sopenharmony_ci
366c5e268c6Sopenharmony_ci    if (result != CAM_META_ITEM_CAP_EXCEED && result != CAM_META_DATA_CAP_EXCEED) {
367c5e268c6Sopenharmony_ci        const char *name = GetCameraMetadataItemName(item);
368c5e268c6Sopenharmony_ci
369c5e268c6Sopenharmony_ci        if (name) {
370c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("Failed to add tag. tagname = %{public}s", name);
371c5e268c6Sopenharmony_ci        } else {
372c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("Failed to add unknown tag");
373c5e268c6Sopenharmony_ci        }
374c5e268c6Sopenharmony_ci        return false;
375c5e268c6Sopenharmony_ci    }
376c5e268c6Sopenharmony_ci
377c5e268c6Sopenharmony_ci    return resize_add_metadata(item, data, data_count);
378c5e268c6Sopenharmony_ci}
379c5e268c6Sopenharmony_ci
380c5e268c6Sopenharmony_cibool CameraMetadata::resize_add_metadata(uint32_t item, const void *data, size_t data_count)
381c5e268c6Sopenharmony_ci{
382c5e268c6Sopenharmony_ci    uint32_t data_type;
383c5e268c6Sopenharmony_ci
384c5e268c6Sopenharmony_ci    auto itemCapacity = GetCameraMetadataItemCapacity(metadata_);
385c5e268c6Sopenharmony_ci    auto data_capacity = GetCameraMetadataDataSize(metadata_);
386c5e268c6Sopenharmony_ci
387c5e268c6Sopenharmony_ci    int32_t ret = GetCameraMetadataItemType(item, &data_type);
388c5e268c6Sopenharmony_ci    if (ret != CAM_META_SUCCESS) {
389c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItemType invalid item type");
390c5e268c6Sopenharmony_ci        return false;
391c5e268c6Sopenharmony_ci    }
392c5e268c6Sopenharmony_ci    int32_t size = CalculateCameraMetadataItemDataSize(data_type, data_count);
393c5e268c6Sopenharmony_ci    if (size == CAM_META_FAILURE) {
394c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("CalculateCameraMetadataItemDataSize invalid datatype:%{public}d", data_type);
395c5e268c6Sopenharmony_ci        return false;
396c5e268c6Sopenharmony_ci    }
397c5e268c6Sopenharmony_ci
398c5e268c6Sopenharmony_ci    common_metadata_header_t *newMetadata = AllocateCameraMetadataBuffer((itemCapacity + 1) * INDEX_COUNTER,
399c5e268c6Sopenharmony_ci        AlignTo((data_capacity + size) * INDEX_COUNTER, DATA_ALIGNMENT));
400c5e268c6Sopenharmony_ci
401c5e268c6Sopenharmony_ci    if (newMetadata == nullptr) {
402c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("Failed to resize the metadata buffer");
403c5e268c6Sopenharmony_ci        return false;
404c5e268c6Sopenharmony_ci    }
405c5e268c6Sopenharmony_ci
406c5e268c6Sopenharmony_ci    auto result = CopyCameraMetadataItems(newMetadata, metadata_);
407c5e268c6Sopenharmony_ci    if (result != CAM_META_SUCCESS) {
408c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("Failed to copy the old metadata to new metadata");
409c5e268c6Sopenharmony_ci        FreeCameraMetadataBuffer(newMetadata);
410c5e268c6Sopenharmony_ci        return false;
411c5e268c6Sopenharmony_ci    }
412c5e268c6Sopenharmony_ci
413c5e268c6Sopenharmony_ci    result = AddCameraMetadataItem(newMetadata, item, data, data_count);
414c5e268c6Sopenharmony_ci    if (result != CAM_META_SUCCESS) {
415c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("Failed to add new entry");
416c5e268c6Sopenharmony_ci        FreeCameraMetadataBuffer(newMetadata);
417c5e268c6Sopenharmony_ci        return false;
418c5e268c6Sopenharmony_ci    }
419c5e268c6Sopenharmony_ci    replace_metadata(newMetadata);
420c5e268c6Sopenharmony_ci
421c5e268c6Sopenharmony_ci    return true;
422c5e268c6Sopenharmony_ci}
423c5e268c6Sopenharmony_ci
424c5e268c6Sopenharmony_civoid CameraMetadata::replace_metadata(common_metadata_header_t *newMetadata)
425c5e268c6Sopenharmony_ci{
426c5e268c6Sopenharmony_ci    if (metadata_ == newMetadata) {
427c5e268c6Sopenharmony_ci        return;
428c5e268c6Sopenharmony_ci    }
429c5e268c6Sopenharmony_ci
430c5e268c6Sopenharmony_ci    FreeCameraMetadataBuffer(metadata_);
431c5e268c6Sopenharmony_ci    metadata_ = newMetadata;
432c5e268c6Sopenharmony_ci}
433c5e268c6Sopenharmony_ci
434c5e268c6Sopenharmony_cibool CameraMetadata::updateEntry(uint32_t tag, const void *data, size_t dataCount)
435c5e268c6Sopenharmony_ci{
436c5e268c6Sopenharmony_ci    if (metadata_ == nullptr) {
437c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("metadata_ is null");
438c5e268c6Sopenharmony_ci        return false;
439c5e268c6Sopenharmony_ci    }
440c5e268c6Sopenharmony_ci
441c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(tag);
442c5e268c6Sopenharmony_ci    (void)name;
443c5e268c6Sopenharmony_ci    camera_metadata_item_t item;
444c5e268c6Sopenharmony_ci    int ret = FindCameraMetadataItem(metadata_, tag, &item);
445c5e268c6Sopenharmony_ci    if (ret) {
446c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("Failed to update tag tagname = %{public}s : not present", (name ? name : "<unknown>"));
447c5e268c6Sopenharmony_ci        return false;
448c5e268c6Sopenharmony_ci    }
449c5e268c6Sopenharmony_ci    METADATA_INFO_LOG("updateEntry item id: %{public}d, name: %{public}s, "
450c5e268c6Sopenharmony_ci                      "dataCount: %{public}zu", tag, name ? name : "<unknown>", dataCount);
451c5e268c6Sopenharmony_ci    ret = UpdateCameraMetadataItemByIndex(metadata_, item.index, data, dataCount, nullptr);
452c5e268c6Sopenharmony_ci    if (ret) {
453c5e268c6Sopenharmony_ci        const char *name_ = GetCameraMetadataItemName(tag);
454c5e268c6Sopenharmony_ci        (void)name_;
455c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("Failed to update tag tagname = %{public}s", (name_ ? name_ : "<unknown>"));
456c5e268c6Sopenharmony_ci        return false;
457c5e268c6Sopenharmony_ci    }
458c5e268c6Sopenharmony_ci
459c5e268c6Sopenharmony_ci    return true;
460c5e268c6Sopenharmony_ci}
461c5e268c6Sopenharmony_ci
462c5e268c6Sopenharmony_cicommon_metadata_header_t *CameraMetadata::get()
463c5e268c6Sopenharmony_ci{
464c5e268c6Sopenharmony_ci    return metadata_;
465c5e268c6Sopenharmony_ci}
466c5e268c6Sopenharmony_ci
467c5e268c6Sopenharmony_ciconst common_metadata_header_t *CameraMetadata::get() const
468c5e268c6Sopenharmony_ci{
469c5e268c6Sopenharmony_ci    return metadata_;
470c5e268c6Sopenharmony_ci}
471c5e268c6Sopenharmony_ci
472c5e268c6Sopenharmony_cibool CameraMetadata::isValid() const
473c5e268c6Sopenharmony_ci{
474c5e268c6Sopenharmony_ci    return metadata_ != nullptr;
475c5e268c6Sopenharmony_ci}
476c5e268c6Sopenharmony_ci
477c5e268c6Sopenharmony_ciuint32_t CameraMetadata::AlignTo(uint32_t val, uint32_t alignment)
478c5e268c6Sopenharmony_ci{
479c5e268c6Sopenharmony_ci    return static_cast<uint32_t>((static_cast<uintptr_t>(val) + ((alignment) - 1)) & ~((alignment) - 1));
480c5e268c6Sopenharmony_ci}
481c5e268c6Sopenharmony_ci
482c5e268c6Sopenharmony_ciuint32_t CameraMetadata::MaxAlignment(uint32_t dataAlignment, uint32_t metadataAlignment)
483c5e268c6Sopenharmony_ci{
484c5e268c6Sopenharmony_ci    return ((dataAlignment > metadataAlignment) ? dataAlignment : metadataAlignment);
485c5e268c6Sopenharmony_ci}
486c5e268c6Sopenharmony_ci
487c5e268c6Sopenharmony_ciuint8_t *CameraMetadata::GetMetadataData(const common_metadata_header_t *metadataHeader)
488c5e268c6Sopenharmony_ci{
489c5e268c6Sopenharmony_ci    if (!metadataHeader) {
490c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetMetadataData metadataHeader is null");
491c5e268c6Sopenharmony_ci        return nullptr;
492c5e268c6Sopenharmony_ci    }
493c5e268c6Sopenharmony_ci    return (uint8_t *)metadataHeader + metadataHeader->data_start;
494c5e268c6Sopenharmony_ci}
495c5e268c6Sopenharmony_ci
496c5e268c6Sopenharmony_cicamera_metadata_item_entry_t *CameraMetadata::GetMetadataItems(const common_metadata_header_t *metadataHeader)
497c5e268c6Sopenharmony_ci{
498c5e268c6Sopenharmony_ci    return reinterpret_cast<camera_metadata_item_entry_t *>(
499c5e268c6Sopenharmony_ci        (reinterpret_cast<uint8_t *>(const_cast<common_metadata_header_t *>(metadataHeader)) +
500c5e268c6Sopenharmony_ci        metadataHeader->items_start));
501c5e268c6Sopenharmony_ci}
502c5e268c6Sopenharmony_ci
503c5e268c6Sopenharmony_cicommon_metadata_header_t *CameraMetadata::FillCameraMetadata(common_metadata_header_t *buffer, size_t memoryRequired,
504c5e268c6Sopenharmony_ci    uint32_t itemCapacity, uint32_t dataCapacity)
505c5e268c6Sopenharmony_ci{
506c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("FillCameraMetadata start");
507c5e268c6Sopenharmony_ci    if (buffer == nullptr) {
508c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("FillCameraMetadata buffer is null");
509c5e268c6Sopenharmony_ci        return nullptr;
510c5e268c6Sopenharmony_ci    }
511c5e268c6Sopenharmony_ci
512c5e268c6Sopenharmony_ci    common_metadata_header_t *metadataHeader = static_cast<common_metadata_header_t *>(buffer);
513c5e268c6Sopenharmony_ci    metadataHeader->version = CURRENT_CAMERA_METADATA_VERSION;
514c5e268c6Sopenharmony_ci    metadataHeader->size = memoryRequired;
515c5e268c6Sopenharmony_ci    metadataHeader->item_count = 0;
516c5e268c6Sopenharmony_ci    metadataHeader->item_capacity = itemCapacity;
517c5e268c6Sopenharmony_ci    metadataHeader->items_start = AlignTo(sizeof(common_metadata_header_t), ITEM_ALIGNMENT);
518c5e268c6Sopenharmony_ci    metadataHeader->data_count = 0;
519c5e268c6Sopenharmony_ci    metadataHeader->data_capacity = dataCapacity;
520c5e268c6Sopenharmony_ci    size_t dataUnaligned = reinterpret_cast<uint8_t *>(GetMetadataItems(metadataHeader) +
521c5e268c6Sopenharmony_ci        metadataHeader->item_capacity) - reinterpret_cast<uint8_t *>(metadataHeader);
522c5e268c6Sopenharmony_ci    metadataHeader->data_start = AlignTo(dataUnaligned, DATA_ALIGNMENT);
523c5e268c6Sopenharmony_ci
524c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("MetadataHeader ItemCapacity Size = %{public}u, DataCapacity Size = %{public}u",
525c5e268c6Sopenharmony_ci        metadataHeader->item_capacity, metadataHeader->data_capacity);
526c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("FillCameraMetadata end");
527c5e268c6Sopenharmony_ci    return metadataHeader;
528c5e268c6Sopenharmony_ci}
529c5e268c6Sopenharmony_ci
530c5e268c6Sopenharmony_cisize_t CameraMetadata::CalculateCameraMetadataMemoryRequired(uint32_t itemCount, uint32_t dataCount)
531c5e268c6Sopenharmony_ci{
532c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("CalculateCameraMetadataMemoryRequired start");
533c5e268c6Sopenharmony_ci    size_t memoryRequired = sizeof(common_metadata_header_t);
534c5e268c6Sopenharmony_ci    memoryRequired = CameraMetadata::AlignTo(memoryRequired, ITEM_ALIGNMENT);
535c5e268c6Sopenharmony_ci
536c5e268c6Sopenharmony_ci    memoryRequired += sizeof(camera_metadata_item_entry_t[itemCount]);
537c5e268c6Sopenharmony_ci    memoryRequired = CameraMetadata::AlignTo(memoryRequired, DATA_ALIGNMENT);
538c5e268c6Sopenharmony_ci
539c5e268c6Sopenharmony_ci    memoryRequired += sizeof(uint8_t[dataCount]);
540c5e268c6Sopenharmony_ci    memoryRequired = CameraMetadata::AlignTo(memoryRequired, METADATA_PACKET_ALIGNMENT);
541c5e268c6Sopenharmony_ci
542c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("CalculateCameraMetadataMemoryRequired memoryRequired: %{public}zu", memoryRequired);
543c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("CalculateCameraMetadataMemoryRequired end");
544c5e268c6Sopenharmony_ci    return memoryRequired;
545c5e268c6Sopenharmony_ci}
546c5e268c6Sopenharmony_ci
547c5e268c6Sopenharmony_cicommon_metadata_header_t *CameraMetadata::AllocateCameraMetadataBuffer(uint32_t item_capacity, uint32_t data_capacity)
548c5e268c6Sopenharmony_ci{
549c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("AllocateCameraMetadataBuffer start");
550c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("AllocateCameraMetadataBuffer item_capacity: %{public}u, data_capacity: %{public}u",
551c5e268c6Sopenharmony_ci        item_capacity, data_capacity);
552c5e268c6Sopenharmony_ci    if (item_capacity > MAX_ITEM_CAPACITY || data_capacity > MAX_DATA_CAPACITY) {
553c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AllocateCameraMetadataBuffer invalid item_capacity or data_capacity");
554c5e268c6Sopenharmony_ci        return nullptr;
555c5e268c6Sopenharmony_ci    }
556c5e268c6Sopenharmony_ci    size_t memoryRequired = CalculateCameraMetadataMemoryRequired(item_capacity, data_capacity);
557c5e268c6Sopenharmony_ci    void *buffer = calloc(1, memoryRequired);
558c5e268c6Sopenharmony_ci    if (buffer == nullptr) {
559c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AllocateCameraMetadataBuffer memory allocation failed");
560c5e268c6Sopenharmony_ci        return reinterpret_cast<common_metadata_header_t *>(buffer);
561c5e268c6Sopenharmony_ci    }
562c5e268c6Sopenharmony_ci
563c5e268c6Sopenharmony_ci    common_metadata_header_t *metadataHeader = FillCameraMetadata(reinterpret_cast<common_metadata_header_t *>(buffer),
564c5e268c6Sopenharmony_ci        memoryRequired, item_capacity, data_capacity);
565c5e268c6Sopenharmony_ci    if (metadataHeader == nullptr) {
566c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AllocateCameraMetadataBuffer metadataHeader is null");
567c5e268c6Sopenharmony_ci        free(buffer);
568c5e268c6Sopenharmony_ci    }
569c5e268c6Sopenharmony_ci
570c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("AllocateCameraMetadataBuffer end");
571c5e268c6Sopenharmony_ci    return metadataHeader;
572c5e268c6Sopenharmony_ci}
573c5e268c6Sopenharmony_ci
574c5e268c6Sopenharmony_ci// Load vendor tag impl
575c5e268c6Sopenharmony_ciint32_t LoadVendorTagImpl()
576c5e268c6Sopenharmony_ci{
577c5e268c6Sopenharmony_ci    if (g_vendorTagImpl == nullptr) {
578c5e268c6Sopenharmony_ci        void* libHandle = nullptr;
579c5e268c6Sopenharmony_ci#ifndef CAMERA_VENDOR_TAG
580c5e268c6Sopenharmony_ci        libHandle = dlopen(g_exampleVendorTagLib, RTLD_LAZY);
581c5e268c6Sopenharmony_ci#else
582c5e268c6Sopenharmony_ci        libHandle = dlopen(g_vendorTagLib, RTLD_LAZY);
583c5e268c6Sopenharmony_ci#endif
584c5e268c6Sopenharmony_ci        if (libHandle == nullptr) {
585c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("dlopen failed %{public}s", __func__);
586c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
587c5e268c6Sopenharmony_ci        }
588c5e268c6Sopenharmony_ci
589c5e268c6Sopenharmony_ci        CreateCameraVendorTag* createVendorTag =
590c5e268c6Sopenharmony_ci            reinterpret_cast<CreateCameraVendorTag*>(dlsym(libHandle, "CreateVendorTagImpl"));
591c5e268c6Sopenharmony_ci        if (createVendorTag == nullptr) {
592c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("CreateCameraVendorTag failed %{public}s", __func__);
593c5e268c6Sopenharmony_ci            dlclose(libHandle);
594c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
595c5e268c6Sopenharmony_ci        }
596c5e268c6Sopenharmony_ci
597c5e268c6Sopenharmony_ci        g_vendorTagImpl = createVendorTag();
598c5e268c6Sopenharmony_ci        if (g_vendorTagImpl == nullptr) {
599c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("createVendorTag failed %{public}s", __func__);
600c5e268c6Sopenharmony_ci            dlclose(libHandle);
601c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
602c5e268c6Sopenharmony_ci        }
603c5e268c6Sopenharmony_ci    }
604c5e268c6Sopenharmony_ci
605c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
606c5e268c6Sopenharmony_ci}
607c5e268c6Sopenharmony_ci
608c5e268c6Sopenharmony_ciint32_t CameraMetadata::GetMetadataSection(uint32_t itemSection, uint32_t *section)
609c5e268c6Sopenharmony_ci{
610c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("GetMetadataSection start");
611c5e268c6Sopenharmony_ci    if (itemSection < OHOS_CAMERA_PROPERTIES ||
612c5e268c6Sopenharmony_ci        itemSection >= OHOS_ABILITY_SECTION_END) {
613c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetMetadataSection itemSection is not valid");
614c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
615c5e268c6Sopenharmony_ci    }
616c5e268c6Sopenharmony_ci
617c5e268c6Sopenharmony_ci    int32_t ret = CAM_META_SUCCESS;
618c5e268c6Sopenharmony_ci    auto iter = g_metadataSectionMap.find(itemSection);
619c5e268c6Sopenharmony_ci    if (iter != g_metadataSectionMap.end()) {
620c5e268c6Sopenharmony_ci        *section = iter->second;
621c5e268c6Sopenharmony_ci    } else {
622c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetMetadataSection item section is not defined");
623c5e268c6Sopenharmony_ci        ret = CAM_META_FAILURE;
624c5e268c6Sopenharmony_ci    }
625c5e268c6Sopenharmony_ci
626c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("GetMetadataSection end");
627c5e268c6Sopenharmony_ci    return ret;
628c5e268c6Sopenharmony_ci}
629c5e268c6Sopenharmony_ci
630c5e268c6Sopenharmony_ciint32_t CameraMetadata::GetCameraMetadataItemType(uint32_t item, uint32_t *dataType)
631c5e268c6Sopenharmony_ci{
632c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("GetCameraMetadataItemType start");
633c5e268c6Sopenharmony_ci    if (dataType == nullptr) {
634c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItemType dataType is null");
635c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
636c5e268c6Sopenharmony_ci    }
637c5e268c6Sopenharmony_ci    uint32_t section;
638c5e268c6Sopenharmony_ci    uint32_t itemTag = item >> BITWISE_SHIFT_16;
639c5e268c6Sopenharmony_ci    if (itemTag >= OHOS_VENDOR_SECTION) {
640c5e268c6Sopenharmony_ci        int32_t ret = LoadVendorTagImpl();
641c5e268c6Sopenharmony_ci        if (ret != CAM_META_SUCCESS) {
642c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("LoadVendorTagImpl failed");
643c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
644c5e268c6Sopenharmony_ci        }
645c5e268c6Sopenharmony_ci        *dataType = g_vendorTagImpl->GetVendorTagType(item);
646c5e268c6Sopenharmony_ci        return CAM_META_SUCCESS;
647c5e268c6Sopenharmony_ci    }
648c5e268c6Sopenharmony_ci    int32_t ret = GetMetadataSection(itemTag, &section);
649c5e268c6Sopenharmony_ci    if (ret != CAM_META_SUCCESS) {
650c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItemType section is not valid");
651c5e268c6Sopenharmony_ci        return ret;
652c5e268c6Sopenharmony_ci    }
653c5e268c6Sopenharmony_ci
654c5e268c6Sopenharmony_ci    if (item >= g_ohosCameraSectionBounds[section][1]) {
655c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItemType item is not in section bound");
656c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
657c5e268c6Sopenharmony_ci    }
658c5e268c6Sopenharmony_ci
659c5e268c6Sopenharmony_ci    uint32_t itemIndex = item & 0xFFFF;
660c5e268c6Sopenharmony_ci    if (g_ohosItemInfo[section][itemIndex].item_type < META_TYPE_BYTE) {
661c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItemType item is not initialized");
662c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
663c5e268c6Sopenharmony_ci    }
664c5e268c6Sopenharmony_ci
665c5e268c6Sopenharmony_ci    *dataType = g_ohosItemInfo[section][itemIndex].item_type;
666c5e268c6Sopenharmony_ci
667c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("GetCameraMetadataItemType end");
668c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
669c5e268c6Sopenharmony_ci}
670c5e268c6Sopenharmony_ci
671c5e268c6Sopenharmony_ciconst char *CameraMetadata::GetCameraMetadataItemName(uint32_t item)
672c5e268c6Sopenharmony_ci{
673c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("GetCameraMetadataItemName start");
674c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("GetCameraMetadataItemName item: %{public}u", item);
675c5e268c6Sopenharmony_ci    uint32_t section;
676c5e268c6Sopenharmony_ci    uint32_t itemTag = item >> BITWISE_SHIFT_16;
677c5e268c6Sopenharmony_ci    if (itemTag >= OHOS_VENDOR_SECTION) {
678c5e268c6Sopenharmony_ci        int32_t ret = LoadVendorTagImpl();
679c5e268c6Sopenharmony_ci        if (ret != CAM_META_SUCCESS) {
680c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("LoadVendorTagImpl failed");
681c5e268c6Sopenharmony_ci            return nullptr;
682c5e268c6Sopenharmony_ci        }
683c5e268c6Sopenharmony_ci        const char* tagName = g_vendorTagImpl->GetVendorTagName(item);
684c5e268c6Sopenharmony_ci        return tagName;
685c5e268c6Sopenharmony_ci    }
686c5e268c6Sopenharmony_ci    int32_t ret = GetMetadataSection(itemTag, &section);
687c5e268c6Sopenharmony_ci    if (ret != CAM_META_SUCCESS) {
688c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItemName section is not valid");
689c5e268c6Sopenharmony_ci        return nullptr;
690c5e268c6Sopenharmony_ci    }
691c5e268c6Sopenharmony_ci
692c5e268c6Sopenharmony_ci    if (item >= g_ohosCameraSectionBounds[section][1]) {
693c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItemName item is not in section bound");
694c5e268c6Sopenharmony_ci        return nullptr;
695c5e268c6Sopenharmony_ci    }
696c5e268c6Sopenharmony_ci
697c5e268c6Sopenharmony_ci    uint32_t itemIndex = item & 0xFFFF;
698c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("GetCameraMetadataItemName end");
699c5e268c6Sopenharmony_ci    return g_ohosItemInfo[section][itemIndex].item_name;
700c5e268c6Sopenharmony_ci}
701c5e268c6Sopenharmony_ci
702c5e268c6Sopenharmony_ciint32_t CameraMetadata::CalculateCameraMetadataItemDataSize(uint32_t type, size_t dataCount)
703c5e268c6Sopenharmony_ci{
704c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("CalculateCameraMetadataItemDataSize start");
705c5e268c6Sopenharmony_ci    if (type < META_TYPE_BYTE || type >= META_NUM_TYPES) {
706c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("CalculateCameraMetadataItemDataSize invalid type");
707c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
708c5e268c6Sopenharmony_ci    }
709c5e268c6Sopenharmony_ci
710c5e268c6Sopenharmony_ci    size_t dataBytes = dataCount * OHOS_CAMERA_METADATA_TYPE_SIZE[type];
711c5e268c6Sopenharmony_ci
712c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("CalculateCameraMetadataItemDataSize end");
713c5e268c6Sopenharmony_ci    return (dataBytes <= METADATA_HEADER_DATA_SIZE) ? 0 : AlignTo(dataBytes, DATA_ALIGNMENT);
714c5e268c6Sopenharmony_ci}
715c5e268c6Sopenharmony_ci
716c5e268c6Sopenharmony_ciint CameraMetadata::AddCameraMetadataItemVerify(common_metadata_header_t *dst,
717c5e268c6Sopenharmony_ci    uint32_t item, const void *data, size_t dataCount, uint32_t *dataType)
718c5e268c6Sopenharmony_ci{
719c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(item);
720c5e268c6Sopenharmony_ci    if (name == nullptr) {
721c5e268c6Sopenharmony_ci        name = "<unknown>";
722c5e268c6Sopenharmony_ci    }
723c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("AddCameraMetadataItemVerify item id: %{public}u, name: %{public}s, "
724c5e268c6Sopenharmony_ci        "dataCount: %{public}zu", item, name, dataCount);
725c5e268c6Sopenharmony_ci
726c5e268c6Sopenharmony_ci    if (dst == nullptr) {
727c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AddCameraMetadataItemVerify common_metadata_header_t is null");
728c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
729c5e268c6Sopenharmony_ci    }
730c5e268c6Sopenharmony_ci
731c5e268c6Sopenharmony_ci    if (!dataCount || data == nullptr) {
732c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AddCameraMetadataItemVerify data is not valid. item: %{public}u, "
733c5e268c6Sopenharmony_ci            "dataCount: %{public}zu", item, dataCount);
734c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
735c5e268c6Sopenharmony_ci    }
736c5e268c6Sopenharmony_ci
737c5e268c6Sopenharmony_ci    if (dst->item_count == dst->item_capacity) {
738c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AddCameraMetadataItemVerify item_capacity limit reached. "
739c5e268c6Sopenharmony_ci            "item_count: %{public}d, item_capacity: %{public}d", dst->item_count, dst->item_capacity);
740c5e268c6Sopenharmony_ci        return CAM_META_ITEM_CAP_EXCEED;
741c5e268c6Sopenharmony_ci    }
742c5e268c6Sopenharmony_ci
743c5e268c6Sopenharmony_ci    int32_t ret = GetCameraMetadataItemType(item, dataType);
744c5e268c6Sopenharmony_ci    if (ret != CAM_META_SUCCESS) {
745c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AddCameraMetadataItemVerify invalid item type");
746c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
747c5e268c6Sopenharmony_ci    }
748c5e268c6Sopenharmony_ci
749c5e268c6Sopenharmony_ci    if (*dataType < META_TYPE_BYTE || *dataType >= META_NUM_TYPES) {
750c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AddCameraMetadataItemVerify invalid type");
751c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
752c5e268c6Sopenharmony_ci    }
753c5e268c6Sopenharmony_ci
754c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
755c5e268c6Sopenharmony_ci}
756c5e268c6Sopenharmony_ci
757c5e268c6Sopenharmony_ciint CameraMetadata::AddCameraMetadataItem(common_metadata_header_t *dst, uint32_t item,
758c5e268c6Sopenharmony_ci    const void *data, size_t dataCount)
759c5e268c6Sopenharmony_ci{
760c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("AddCameraMetadataItem start");
761c5e268c6Sopenharmony_ci    uint32_t dataType;
762c5e268c6Sopenharmony_ci    int32_t ret = AddCameraMetadataItemVerify(dst, item, data, dataCount, &dataType);
763c5e268c6Sopenharmony_ci    if (ret != CAM_META_SUCCESS) return ret;
764c5e268c6Sopenharmony_ci
765c5e268c6Sopenharmony_ci    if ((dst->size - dst->items_start) < (uint64_t)dst->item_count * itemLen ||
766c5e268c6Sopenharmony_ci        (dst->size - dst->data_start) < dst->data_count) {
767c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AddCameraMetadataItem fail: invalid argument.");
768c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
769c5e268c6Sopenharmony_ci    }
770c5e268c6Sopenharmony_ci
771c5e268c6Sopenharmony_ci    int32_t dataBytes = CalculateCameraMetadataItemDataSize(dataType, dataCount);
772c5e268c6Sopenharmony_ci    if (dataBytes + dst->data_count > dst->data_capacity) {
773c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AddCameraMetadataItem data_capacity limit reached");
774c5e268c6Sopenharmony_ci        return CAM_META_DATA_CAP_EXCEED;
775c5e268c6Sopenharmony_ci    }
776c5e268c6Sopenharmony_ci
777c5e268c6Sopenharmony_ci    size_t dataPayloadBytes = dataCount * OHOS_CAMERA_METADATA_TYPE_SIZE[dataType];
778c5e268c6Sopenharmony_ci    camera_metadata_item_entry_t *metadataItem = GetMetadataItems(dst) + dst->item_count;
779c5e268c6Sopenharmony_ci    ret = memset_s(metadataItem, sizeof(camera_metadata_item_entry_t), 0, sizeof(camera_metadata_item_entry_t));
780c5e268c6Sopenharmony_ci    if (ret != EOK) {
781c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("AddCameraMetadataItem: memset_s failed");
782c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
783c5e268c6Sopenharmony_ci    }
784c5e268c6Sopenharmony_ci    metadataItem->item = item;
785c5e268c6Sopenharmony_ci    metadataItem->data_type = dataType;
786c5e268c6Sopenharmony_ci    metadataItem->count = dataCount;
787c5e268c6Sopenharmony_ci
788c5e268c6Sopenharmony_ci    if (dataBytes == 0) {
789c5e268c6Sopenharmony_ci        ret = memcpy_s(metadataItem->data.value, METADATA_HEADER_DATA_SIZE, data, dataPayloadBytes);
790c5e268c6Sopenharmony_ci        if (ret != EOK) {
791c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("AddCameraMetadataItem memory copy failed");
792c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
793c5e268c6Sopenharmony_ci        }
794c5e268c6Sopenharmony_ci    } else {
795c5e268c6Sopenharmony_ci        metadataItem->data.offset = dst->data_count;
796c5e268c6Sopenharmony_ci        uint8_t *dstMetadataData = GetMetadataData(dst);
797c5e268c6Sopenharmony_ci        if (dstMetadataData == nullptr) {
798c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("AddCameraMetadataItem GetMetadataData failed");
799c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
800c5e268c6Sopenharmony_ci        }
801c5e268c6Sopenharmony_ci        ret = memcpy_s(dstMetadataData + metadataItem->data.offset, dst->data_capacity - dst->data_count, data,
802c5e268c6Sopenharmony_ci            dataPayloadBytes);
803c5e268c6Sopenharmony_ci        if (ret != EOK) {
804c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("AddCameraMetadataItem memory copy failed");
805c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
806c5e268c6Sopenharmony_ci        }
807c5e268c6Sopenharmony_ci        dst->data_count += dataBytes;
808c5e268c6Sopenharmony_ci    }
809c5e268c6Sopenharmony_ci    dst->item_count++;
810c5e268c6Sopenharmony_ci
811c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("AddCameraMetadataItem end");
812c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
813c5e268c6Sopenharmony_ci}
814c5e268c6Sopenharmony_ci
815c5e268c6Sopenharmony_ciint CameraMetadata::GetCameraMetadataItem(const common_metadata_header_t *src, uint32_t index,
816c5e268c6Sopenharmony_ci    camera_metadata_item_t *item)
817c5e268c6Sopenharmony_ci{
818c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("GetCameraMetadataItem start");
819c5e268c6Sopenharmony_ci    if (src == nullptr || item == nullptr) {
820c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItem src or item is null");
821c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
822c5e268c6Sopenharmony_ci    }
823c5e268c6Sopenharmony_ci
824c5e268c6Sopenharmony_ci    int32_t ret = memset_s(item, sizeof(camera_metadata_item_t), 0, sizeof(camera_metadata_item_t));
825c5e268c6Sopenharmony_ci    if (ret != EOK) {
826c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItem: memset_s failed");
827c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
828c5e268c6Sopenharmony_ci    }
829c5e268c6Sopenharmony_ci    if (index >= src->item_count) {
830c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItem index is greater than item count");
831c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
832c5e268c6Sopenharmony_ci    }
833c5e268c6Sopenharmony_ci
834c5e268c6Sopenharmony_ci    camera_metadata_item_entry_t *localItem = GetMetadataItems(src) + index;
835c5e268c6Sopenharmony_ci
836c5e268c6Sopenharmony_ci    item->index = index;
837c5e268c6Sopenharmony_ci    item->item = localItem->item;
838c5e268c6Sopenharmony_ci    item->data_type = localItem->data_type;
839c5e268c6Sopenharmony_ci    item->count = localItem->count;
840c5e268c6Sopenharmony_ci
841c5e268c6Sopenharmony_ci    int32_t dataBytes = CalculateCameraMetadataItemDataSize(localItem->data_type, localItem->count);
842c5e268c6Sopenharmony_ci    if (dataBytes == 0) {
843c5e268c6Sopenharmony_ci        item->data.u8 = localItem->data.value;
844c5e268c6Sopenharmony_ci    } else if (dataBytes == CAM_META_FAILURE) {
845c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItem invalid datatype:%{public}d", localItem->data_type);
846c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
847c5e268c6Sopenharmony_ci    } else {
848c5e268c6Sopenharmony_ci        uint8_t *srcMetadataData = GetMetadataData(src);
849c5e268c6Sopenharmony_ci        if (srcMetadataData == nullptr) {
850c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("GetCameraMetadataItem GetMetadataData failed");
851c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
852c5e268c6Sopenharmony_ci        }
853c5e268c6Sopenharmony_ci        item->data.u8 = srcMetadataData + localItem->data.offset;
854c5e268c6Sopenharmony_ci    }
855c5e268c6Sopenharmony_ci
856c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("GetCameraMetadataItem end");
857c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
858c5e268c6Sopenharmony_ci}
859c5e268c6Sopenharmony_ci
860c5e268c6Sopenharmony_ciint CameraMetadata::FindCameraMetadataItemIndex(const common_metadata_header_t *src, uint32_t item, uint32_t *idx,
861c5e268c6Sopenharmony_ci    bool isPrintLog)
862c5e268c6Sopenharmony_ci{
863c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("FindCameraMetadataItemIndex start");
864c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("FindCameraMetadataItemIndex item: %{public}u", item);
865c5e268c6Sopenharmony_ci    if (src == nullptr || idx == nullptr) {
866c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("FindCameraMetadataItemIndex src or index is null");
867c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
868c5e268c6Sopenharmony_ci    }
869c5e268c6Sopenharmony_ci
870c5e268c6Sopenharmony_ci    camera_metadata_item_entry_t *searchItem = GetMetadataItems(src);
871c5e268c6Sopenharmony_ci    uint32_t index;
872c5e268c6Sopenharmony_ci    for (index = 0; index < src->item_count; index++, searchItem++) {
873c5e268c6Sopenharmony_ci        if (searchItem->item == item) {
874c5e268c6Sopenharmony_ci            break;
875c5e268c6Sopenharmony_ci        }
876c5e268c6Sopenharmony_ci    }
877c5e268c6Sopenharmony_ci
878c5e268c6Sopenharmony_ci    if (index == src->item_count) {
879c5e268c6Sopenharmony_ci        if (isPrintLog) {
880c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("FindCameraMetadataItemIndex item: %{public}u not found", item);
881c5e268c6Sopenharmony_ci        }
882c5e268c6Sopenharmony_ci        return CAM_META_ITEM_NOT_FOUND;
883c5e268c6Sopenharmony_ci    }
884c5e268c6Sopenharmony_ci
885c5e268c6Sopenharmony_ci    *idx = index;
886c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("FindCameraMetadataItemIndex index: %{public}u", index);
887c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("FindCameraMetadataItemIndex end");
888c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
889c5e268c6Sopenharmony_ci}
890c5e268c6Sopenharmony_ci
891c5e268c6Sopenharmony_ciint CameraMetadata::FindCameraMetadataItem(const common_metadata_header_t *src, uint32_t item,
892c5e268c6Sopenharmony_ci    camera_metadata_item_t *metadataItem)
893c5e268c6Sopenharmony_ci{
894c5e268c6Sopenharmony_ci    uint32_t index = 0;
895c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(item);
896c5e268c6Sopenharmony_ci    if (name == nullptr) {
897c5e268c6Sopenharmony_ci        name = "<unknown>";
898c5e268c6Sopenharmony_ci    }
899c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("FindCameraMetadataItem item id: %{public}u, name: %{public}s", item, name);
900c5e268c6Sopenharmony_ci    int ret = FindCameraMetadataItemIndex(src, item, &index);
901c5e268c6Sopenharmony_ci    if (ret != CAM_META_SUCCESS) {
902c5e268c6Sopenharmony_ci        return ret;
903c5e268c6Sopenharmony_ci    }
904c5e268c6Sopenharmony_ci
905c5e268c6Sopenharmony_ci    return GetCameraMetadataItem(src, index, metadataItem);
906c5e268c6Sopenharmony_ci}
907c5e268c6Sopenharmony_ci
908c5e268c6Sopenharmony_cibool CameraMetadata::IsCameraMetadataItemExist(const common_metadata_header_t *src, uint32_t item)
909c5e268c6Sopenharmony_ci{
910c5e268c6Sopenharmony_ci    uint32_t index = 0;
911c5e268c6Sopenharmony_ci    int ret = FindCameraMetadataItemIndex(src, item, &index, false);
912c5e268c6Sopenharmony_ci    return ret == CAM_META_SUCCESS;
913c5e268c6Sopenharmony_ci}
914c5e268c6Sopenharmony_ci
915c5e268c6Sopenharmony_civoid SetOffset(camera_metadata_item_entry_t *metadataItems, camera_metadata_item_entry_t *item, size_t oldItemSize)
916c5e268c6Sopenharmony_ci{
917c5e268c6Sopenharmony_ci    if (metadataItems == nullptr) {
918c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("SetOffset metadataItems is null");
919c5e268c6Sopenharmony_ci        return;
920c5e268c6Sopenharmony_ci    }
921c5e268c6Sopenharmony_ci    if (CalculateCameraMetadataItemDataSize(metadataItems->data_type, metadataItems->count) > 0 &&
922c5e268c6Sopenharmony_ci        metadataItems->data.offset > item->data.offset) {
923c5e268c6Sopenharmony_ci        metadataItems->data.offset -= oldItemSize;
924c5e268c6Sopenharmony_ci    }
925c5e268c6Sopenharmony_ci}
926c5e268c6Sopenharmony_ci
927c5e268c6Sopenharmony_ciint CameraMetadata::MetadataExpandItemMem(common_metadata_header_t *dst, camera_metadata_item_entry_t *item,
928c5e268c6Sopenharmony_ci    size_t oldItemSize)
929c5e268c6Sopenharmony_ci{
930c5e268c6Sopenharmony_ci    if (item == nullptr || dst == nullptr) {
931c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("MetadataExpandItemMem item is null or dst is null");
932c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
933c5e268c6Sopenharmony_ci    }
934c5e268c6Sopenharmony_ci    uint8_t *dstMetadataData = GetMetadataData(dst);
935c5e268c6Sopenharmony_ci    if (dstMetadataData == nullptr) {
936c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("MetadataExpandItemMem GetMetadataData failed");
937c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
938c5e268c6Sopenharmony_ci    }
939c5e268c6Sopenharmony_ci    if (item->data.offset > UINT32_MAX - (uint32_t)oldItemSize ||
940c5e268c6Sopenharmony_ci        item->data.offset + (uint32_t)oldItemSize > dst->data_count) {
941c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("MetadataExpandItemMem data.offset:%{public}u, oldItemSize:%{pulic}zu, "
942c5e268c6Sopenharmony_ci            "dst->data_count:%{public}u", item->data.offset, oldItemSize, dst->data_count);
943c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
944c5e268c6Sopenharmony_ci    }
945c5e268c6Sopenharmony_ci    uint8_t *start = dstMetadataData + item->data.offset;
946c5e268c6Sopenharmony_ci    uint8_t *end = start + oldItemSize;
947c5e268c6Sopenharmony_ci    size_t length = dst->data_count - item->data.offset - oldItemSize;
948c5e268c6Sopenharmony_ci    if (length != 0) {
949c5e268c6Sopenharmony_ci        int32_t ret = memmove_s(start, length, end, length);
950c5e268c6Sopenharmony_ci        if (ret != EOK) {
951c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("MetadataExpandItemMem memory move failed");
952c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
953c5e268c6Sopenharmony_ci        }
954c5e268c6Sopenharmony_ci    }
955c5e268c6Sopenharmony_ci    dst->data_count -= oldItemSize;
956c5e268c6Sopenharmony_ci
957c5e268c6Sopenharmony_ci    camera_metadata_item_entry_t *metadataItems = GetMetadataItems(dst);
958c5e268c6Sopenharmony_ci    for (uint32_t i = 0; i < dst->item_count; i++, ++metadataItems) {
959c5e268c6Sopenharmony_ci        SetOffset(metadataItems, item, oldItemSize);
960c5e268c6Sopenharmony_ci    }
961c5e268c6Sopenharmony_ci
962c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
963c5e268c6Sopenharmony_ci}
964c5e268c6Sopenharmony_ci
965c5e268c6Sopenharmony_ciint CameraMetadata::copyMetadataMemory(common_metadata_header_t *dst, camera_metadata_item_entry_t *item,
966c5e268c6Sopenharmony_ci    size_t dataPayloadSize, const void *data)
967c5e268c6Sopenharmony_ci{
968c5e268c6Sopenharmony_ci    uint8_t *dstMetadataData = GetMetadataData(dst);
969c5e268c6Sopenharmony_ci    int32_t ret = CAM_META_SUCCESS;
970c5e268c6Sopenharmony_ci    if (dstMetadataData == nullptr) {
971c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("UpdateameraMetadataItemSize GetMetadataData failed");
972c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
973c5e268c6Sopenharmony_ci    }
974c5e268c6Sopenharmony_ci    ret = memcpy_s(dstMetadataData + item->data.offset, dataPayloadSize, data, dataPayloadSize);
975c5e268c6Sopenharmony_ci    if (ret != EOK) {
976c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex memory copy failed");
977c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
978c5e268c6Sopenharmony_ci    }
979c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
980c5e268c6Sopenharmony_ci}
981c5e268c6Sopenharmony_ci
982c5e268c6Sopenharmony_ciint CameraMetadata::UpdateameraMetadataItemSize(camera_metadata_item_entry_t *item, uint32_t dataCount,
983c5e268c6Sopenharmony_ci    common_metadata_header_t *dst, const void *data)
984c5e268c6Sopenharmony_ci{
985c5e268c6Sopenharmony_ci    if (item == nullptr) {
986c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("UpdateameraMetadataItemSize item is null");
987c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
988c5e268c6Sopenharmony_ci    } else if (item->data_type < META_TYPE_BYTE || item->data_type >= META_NUM_TYPES) {
989c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("UpdateameraMetadataItemSize invalid datatype:%{public}d", item->data_type);
990c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
991c5e268c6Sopenharmony_ci    }
992c5e268c6Sopenharmony_ci    int32_t dataSize = CalculateCameraMetadataItemDataSize(item->data_type, dataCount);
993c5e268c6Sopenharmony_ci    size_t dataPayloadSize = dataCount * OHOS_CAMERA_METADATA_TYPE_SIZE[item->data_type];
994c5e268c6Sopenharmony_ci    int32_t oldItemSize = CalculateCameraMetadataItemDataSize(item->data_type, item->count);
995c5e268c6Sopenharmony_ci    int32_t ret = CAM_META_SUCCESS;
996c5e268c6Sopenharmony_ci    if (item == nullptr || dst == nullptr) {
997c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("UpdateameraMetadataItemSize item is null or dst is null");
998c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
999c5e268c6Sopenharmony_ci    }
1000c5e268c6Sopenharmony_ci    if (dataSize != oldItemSize) {
1001c5e268c6Sopenharmony_ci        if (dst->data_capacity < (dst->data_count + dataSize - oldItemSize)) {
1002c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex data_capacity limit reached");
1003c5e268c6Sopenharmony_ci            return CAM_META_DATA_CAP_EXCEED;
1004c5e268c6Sopenharmony_ci        }
1005c5e268c6Sopenharmony_ci        if (oldItemSize != 0) {
1006c5e268c6Sopenharmony_ci            ret = MetadataExpandItemMem(dst, item, oldItemSize);
1007c5e268c6Sopenharmony_ci            if (ret != CAM_META_SUCCESS) {
1008c5e268c6Sopenharmony_ci                return ret;
1009c5e268c6Sopenharmony_ci            }
1010c5e268c6Sopenharmony_ci        }
1011c5e268c6Sopenharmony_ci        if (dataSize != 0) {
1012c5e268c6Sopenharmony_ci            item->data.offset = dst->data_count;
1013c5e268c6Sopenharmony_ci            ret = copyMetadataMemory(dst, item, dataPayloadSize, data);
1014c5e268c6Sopenharmony_ci            if (ret != CAM_META_SUCCESS) {
1015c5e268c6Sopenharmony_ci                return ret;
1016c5e268c6Sopenharmony_ci            }
1017c5e268c6Sopenharmony_ci            dst->data_count += dataSize;
1018c5e268c6Sopenharmony_ci        }
1019c5e268c6Sopenharmony_ci    } else if (dataSize != 0) {
1020c5e268c6Sopenharmony_ci        ret = copyMetadataMemory(dst, item, dataPayloadSize, data);
1021c5e268c6Sopenharmony_ci        if (ret != CAM_META_SUCCESS) {
1022c5e268c6Sopenharmony_ci            return ret;
1023c5e268c6Sopenharmony_ci        }
1024c5e268c6Sopenharmony_ci    }
1025c5e268c6Sopenharmony_ci    if (dataSize == 0) {
1026c5e268c6Sopenharmony_ci        ret = memcpy_s(item->data.value, dataPayloadSize, data, dataPayloadSize);
1027c5e268c6Sopenharmony_ci        if (ret != EOK) {
1028c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex memory copy failed");
1029c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
1030c5e268c6Sopenharmony_ci        }
1031c5e268c6Sopenharmony_ci    }
1032c5e268c6Sopenharmony_ci    return ret;
1033c5e268c6Sopenharmony_ci}
1034c5e268c6Sopenharmony_ci
1035c5e268c6Sopenharmony_ciint CameraMetadata::UpdateCameraMetadataItemByIndex(common_metadata_header_t *dst, uint32_t index,
1036c5e268c6Sopenharmony_ci    const void *data, uint32_t dataCount, camera_metadata_item_t *updatedItem)
1037c5e268c6Sopenharmony_ci{
1038c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("UpdateCameraMetadataItemByIndex start");
1039c5e268c6Sopenharmony_ci    if ((dst == nullptr) || (index >= dst->item_count)) {
1040c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex dst is null or invalid index");
1041c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
1042c5e268c6Sopenharmony_ci    }
1043c5e268c6Sopenharmony_ci
1044c5e268c6Sopenharmony_ci    if (!dataCount || data == nullptr) {
1045c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex data is not valid. "
1046c5e268c6Sopenharmony_ci            "dataCount: %{public}u", dataCount);
1047c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
1048c5e268c6Sopenharmony_ci    }
1049c5e268c6Sopenharmony_ci
1050c5e268c6Sopenharmony_ci    int32_t ret = CAM_META_SUCCESS;
1051c5e268c6Sopenharmony_ci    camera_metadata_item_entry_t *item = GetMetadataItems(dst) + index;
1052c5e268c6Sopenharmony_ci
1053c5e268c6Sopenharmony_ci    ret = UpdateameraMetadataItemSize(item, dataCount, dst, data);
1054c5e268c6Sopenharmony_ci    if (ret != CAM_META_SUCCESS) {
1055c5e268c6Sopenharmony_ci        return ret;
1056c5e268c6Sopenharmony_ci    }
1057c5e268c6Sopenharmony_ci
1058c5e268c6Sopenharmony_ci    item->count = dataCount;
1059c5e268c6Sopenharmony_ci    if (updatedItem != nullptr) {
1060c5e268c6Sopenharmony_ci        ret = GetCameraMetadataItem(dst, index, updatedItem);
1061c5e268c6Sopenharmony_ci        if (ret != CAM_META_SUCCESS) {
1062c5e268c6Sopenharmony_ci            return ret;
1063c5e268c6Sopenharmony_ci        }
1064c5e268c6Sopenharmony_ci    }
1065c5e268c6Sopenharmony_ci
1066c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("UpdateCameraMetadataItemByIndex end");
1067c5e268c6Sopenharmony_ci    return ret;
1068c5e268c6Sopenharmony_ci}
1069c5e268c6Sopenharmony_ci
1070c5e268c6Sopenharmony_ciint CameraMetadata::UpdateCameraMetadataItem(common_metadata_header_t *dst, uint32_t item, const void *data,
1071c5e268c6Sopenharmony_ci    uint32_t dataCount, camera_metadata_item_t *updatedItem)
1072c5e268c6Sopenharmony_ci{
1073c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("UpdateCameraMetadataItem item id: %{public}u, dataCount: %{public}u", item, dataCount);
1074c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(item);
1075c5e268c6Sopenharmony_ci    if (name == nullptr) {
1076c5e268c6Sopenharmony_ci        name = "<unknown>";
1077c5e268c6Sopenharmony_ci    }
1078c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("UpdateCameraMetadataItem item id: %{public}u, name: %{public}s, "
1079c5e268c6Sopenharmony_ci        "dataCount: %{public}u", item, name, dataCount);
1080c5e268c6Sopenharmony_ci    if (!dataCount || data == nullptr) {
1081c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("UpdateCameraMetadataItem data is not valid. item: %{public}u, "
1082c5e268c6Sopenharmony_ci            "dataCount: %{public}u", item, dataCount);
1083c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
1084c5e268c6Sopenharmony_ci    }
1085c5e268c6Sopenharmony_ci
1086c5e268c6Sopenharmony_ci    uint32_t index = 0;
1087c5e268c6Sopenharmony_ci    int32_t ret = FindCameraMetadataItemIndex(dst, item, &index);
1088c5e268c6Sopenharmony_ci    if (ret != CAM_META_SUCCESS) {
1089c5e268c6Sopenharmony_ci        return ret;
1090c5e268c6Sopenharmony_ci    }
1091c5e268c6Sopenharmony_ci
1092c5e268c6Sopenharmony_ci    return UpdateCameraMetadataItemByIndex(dst, index, data, dataCount, updatedItem);
1093c5e268c6Sopenharmony_ci}
1094c5e268c6Sopenharmony_ci
1095c5e268c6Sopenharmony_ciint CameraMetadata::moveMetadataMemery(common_metadata_header_t *dst, camera_metadata_item_entry_t *itemToDelete,
1096c5e268c6Sopenharmony_ci    size_t dataBytes)
1097c5e268c6Sopenharmony_ci{
1098c5e268c6Sopenharmony_ci    uint8_t *dstMetadataData = GetMetadataData(dst);
1099c5e268c6Sopenharmony_ci    if (dstMetadataData == nullptr) {
1100c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("UpdateameraMetadataItemSize GetMetadataData failed");
1101c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
1102c5e268c6Sopenharmony_ci    }
1103c5e268c6Sopenharmony_ci    int32_t ret = CAM_META_SUCCESS;
1104c5e268c6Sopenharmony_ci    uint8_t *start = dstMetadataData + itemToDelete->data.offset;
1105c5e268c6Sopenharmony_ci    uint8_t *end = start + dataBytes;
1106c5e268c6Sopenharmony_ci    size_t length = dst->data_count - itemToDelete->data.offset - dataBytes;
1107c5e268c6Sopenharmony_ci    if (length != 0) {
1108c5e268c6Sopenharmony_ci        ret = memmove_s(start, length, end, length);
1109c5e268c6Sopenharmony_ci        if (ret != EOK) {
1110c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("DeleteCameraMetadataItemByIndex memory move failed");
1111c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
1112c5e268c6Sopenharmony_ci        }
1113c5e268c6Sopenharmony_ci    }
1114c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
1115c5e268c6Sopenharmony_ci}
1116c5e268c6Sopenharmony_ci
1117c5e268c6Sopenharmony_ciint CameraMetadata::DeleteCameraMetadataItemByIndex(common_metadata_header_t *dst, uint32_t index)
1118c5e268c6Sopenharmony_ci{
1119c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("DeleteCameraMetadataItemByIndex start");
1120c5e268c6Sopenharmony_ci    if (dst == nullptr) {
1121c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("DeleteCameraMetadataItemByIndex dst is null");
1122c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
1123c5e268c6Sopenharmony_ci    }
1124c5e268c6Sopenharmony_ci
1125c5e268c6Sopenharmony_ci    if (index >= dst->item_count) {
1126c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("DeleteCameraMetadataItemByIndex item not valid");
1127c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
1128c5e268c6Sopenharmony_ci    }
1129c5e268c6Sopenharmony_ci
1130c5e268c6Sopenharmony_ci    int32_t ret = CAM_META_SUCCESS;
1131c5e268c6Sopenharmony_ci    camera_metadata_item_entry_t *itemToDelete = GetMetadataItems(dst) + index;
1132c5e268c6Sopenharmony_ci    int32_t dataBytes = CalculateCameraMetadataItemDataSize(itemToDelete->data_type, itemToDelete->count);
1133c5e268c6Sopenharmony_ci    if (dataBytes > 0) {
1134c5e268c6Sopenharmony_ci        ret = moveMetadataMemery(dst, itemToDelete, dataBytes);
1135c5e268c6Sopenharmony_ci        if (ret != CAM_META_SUCCESS) {
1136c5e268c6Sopenharmony_ci            return ret;
1137c5e268c6Sopenharmony_ci        }
1138c5e268c6Sopenharmony_ci        dst->data_count -= dataBytes;
1139c5e268c6Sopenharmony_ci
1140c5e268c6Sopenharmony_ci        camera_metadata_item_entry_t *metadataItems = GetMetadataItems(dst);
1141c5e268c6Sopenharmony_ci        for (uint32_t i = 0; i < dst->item_count; i++, ++metadataItems) {
1142c5e268c6Sopenharmony_ci            if (CalculateCameraMetadataItemDataSize(
1143c5e268c6Sopenharmony_ci                metadataItems->data_type, metadataItems->count) > 0 &&
1144c5e268c6Sopenharmony_ci                metadataItems->data.offset > itemToDelete->data.offset) {
1145c5e268c6Sopenharmony_ci                metadataItems->data.offset -= dataBytes;
1146c5e268c6Sopenharmony_ci            }
1147c5e268c6Sopenharmony_ci        }
1148c5e268c6Sopenharmony_ci    } else if (dataBytes == CAM_META_FAILURE) {
1149c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("DeleteCameraMetadataItemByIndex invalid datatype:%{public}d", itemToDelete->data_type);
1150c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
1151c5e268c6Sopenharmony_ci    }
1152c5e268c6Sopenharmony_ci
1153c5e268c6Sopenharmony_ci    uint64_t length = sizeof(camera_metadata_item_entry_t) * (dst->item_count - index - 1);
1154c5e268c6Sopenharmony_ci    if (length != 0) {
1155c5e268c6Sopenharmony_ci        ret = memmove_s(itemToDelete, length, itemToDelete + 1, length);
1156c5e268c6Sopenharmony_ci        if (ret != EOK) {
1157c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("DeleteCameraMetadataItemByIndex memory move failed");
1158c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
1159c5e268c6Sopenharmony_ci        }
1160c5e268c6Sopenharmony_ci    }
1161c5e268c6Sopenharmony_ci    dst->item_count -= 1;
1162c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("DeleteCameraMetadataItemByIndex end");
1163c5e268c6Sopenharmony_ci    return ret;
1164c5e268c6Sopenharmony_ci}
1165c5e268c6Sopenharmony_ci
1166c5e268c6Sopenharmony_ciint CameraMetadata::DeleteCameraMetadataItem(common_metadata_header_t *dst, uint32_t item)
1167c5e268c6Sopenharmony_ci{
1168c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("DeleteCameraMetadataItem item: %{public}u", item);
1169c5e268c6Sopenharmony_ci    uint32_t index = 0;
1170c5e268c6Sopenharmony_ci    int32_t ret = FindCameraMetadataItemIndex(dst, item, &index);
1171c5e268c6Sopenharmony_ci    if (ret != CAM_META_SUCCESS) {
1172c5e268c6Sopenharmony_ci        return ret;
1173c5e268c6Sopenharmony_ci    }
1174c5e268c6Sopenharmony_ci
1175c5e268c6Sopenharmony_ci    return DeleteCameraMetadataItemByIndex(dst, index);
1176c5e268c6Sopenharmony_ci}
1177c5e268c6Sopenharmony_ci
1178c5e268c6Sopenharmony_civoid CameraMetadata::FreeCameraMetadataBuffer(common_metadata_header_t *dst)
1179c5e268c6Sopenharmony_ci{
1180c5e268c6Sopenharmony_ci    if (dst != nullptr) {
1181c5e268c6Sopenharmony_ci        free(dst);
1182c5e268c6Sopenharmony_ci    }
1183c5e268c6Sopenharmony_ci}
1184c5e268c6Sopenharmony_ci
1185c5e268c6Sopenharmony_ciuint32_t CameraMetadata::GetCameraMetadataItemCount(const common_metadata_header_t *metadataHeader)
1186c5e268c6Sopenharmony_ci{
1187c5e268c6Sopenharmony_ci    if (!metadataHeader) {
1188c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItemCount::metadataHeader is null");
1189c5e268c6Sopenharmony_ci        return 0;
1190c5e268c6Sopenharmony_ci    }
1191c5e268c6Sopenharmony_ci    return metadataHeader->item_count;
1192c5e268c6Sopenharmony_ci}
1193c5e268c6Sopenharmony_ci
1194c5e268c6Sopenharmony_ciuint32_t CameraMetadata::GetCameraMetadataItemCapacity(const common_metadata_header_t *metadataHeader)
1195c5e268c6Sopenharmony_ci{
1196c5e268c6Sopenharmony_ci    if (!metadataHeader) {
1197c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataItemCapacity::metadataHeader is null");
1198c5e268c6Sopenharmony_ci        return 0;
1199c5e268c6Sopenharmony_ci    }
1200c5e268c6Sopenharmony_ci    return metadataHeader->item_capacity;
1201c5e268c6Sopenharmony_ci}
1202c5e268c6Sopenharmony_ci
1203c5e268c6Sopenharmony_ciuint32_t CameraMetadata::GetCameraMetadataDataSize(const common_metadata_header_t *metadataHeader)
1204c5e268c6Sopenharmony_ci{
1205c5e268c6Sopenharmony_ci    if (metadataHeader == nullptr) {
1206c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("GetCameraMetadataDataSize::metadataHeader is null");
1207c5e268c6Sopenharmony_ci        return 0;
1208c5e268c6Sopenharmony_ci    }
1209c5e268c6Sopenharmony_ci    return metadataHeader->data_capacity;
1210c5e268c6Sopenharmony_ci}
1211c5e268c6Sopenharmony_ci
1212c5e268c6Sopenharmony_ciint32_t CameraMetadata::CopyCameraMetadataItems(common_metadata_header_t *newMetadata,
1213c5e268c6Sopenharmony_ci    const common_metadata_header_t *oldMetadata)
1214c5e268c6Sopenharmony_ci{
1215c5e268c6Sopenharmony_ci    if (newMetadata == nullptr || oldMetadata == nullptr) {
1216c5e268c6Sopenharmony_ci        return CAM_META_INVALID_PARAM;
1217c5e268c6Sopenharmony_ci    }
1218c5e268c6Sopenharmony_ci
1219c5e268c6Sopenharmony_ci    int32_t ret;
1220c5e268c6Sopenharmony_ci    if (oldMetadata->item_count != 0) {
1221c5e268c6Sopenharmony_ci        ret = memcpy_s(GetMetadataItems(newMetadata), sizeof(camera_metadata_item_entry_t[newMetadata->item_capacity]),
1222c5e268c6Sopenharmony_ci            GetMetadataItems(oldMetadata), sizeof(camera_metadata_item_entry_t[oldMetadata->item_count]));
1223c5e268c6Sopenharmony_ci        if (ret != EOK) {
1224c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("CopyCameraMetadataItems memory copy failed, ItemCapacity Size = %{public}u,"
1225c5e268c6Sopenharmony_ci                "ItemCount Size = %{public}u", newMetadata->item_capacity, oldMetadata->item_count);
1226c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
1227c5e268c6Sopenharmony_ci        }
1228c5e268c6Sopenharmony_ci    }
1229c5e268c6Sopenharmony_ci
1230c5e268c6Sopenharmony_ci    if (oldMetadata->data_count != 0) {
1231c5e268c6Sopenharmony_ci        uint8_t *newMetadataData = GetMetadataData(newMetadata);
1232c5e268c6Sopenharmony_ci        uint8_t *oldMetadataData = GetMetadataData(oldMetadata);
1233c5e268c6Sopenharmony_ci        if (newMetadataData == nullptr || oldMetadataData == nullptr) {
1234c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("UpdateameraMetadataItemSize GetMetadataData failed");
1235c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
1236c5e268c6Sopenharmony_ci        }
1237c5e268c6Sopenharmony_ci        ret = memcpy_s(newMetadataData, sizeof(uint8_t[newMetadata->data_capacity]), oldMetadataData,
1238c5e268c6Sopenharmony_ci            sizeof(uint8_t[oldMetadata->data_count]));
1239c5e268c6Sopenharmony_ci        if (ret != EOK) {
1240c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("CopyCameraMetadataItems memory copy failed, DataCapacity Size = %{public}u,"
1241c5e268c6Sopenharmony_ci                "DataCount Size = %{public}u", newMetadata->data_capacity, oldMetadata->data_count);
1242c5e268c6Sopenharmony_ci            return CAM_META_FAILURE;
1243c5e268c6Sopenharmony_ci        }
1244c5e268c6Sopenharmony_ci    }
1245c5e268c6Sopenharmony_ci
1246c5e268c6Sopenharmony_ci    newMetadata->item_count = oldMetadata->item_count;
1247c5e268c6Sopenharmony_ci    newMetadata->data_count = oldMetadata->data_count;
1248c5e268c6Sopenharmony_ci
1249c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
1250c5e268c6Sopenharmony_ci}
1251c5e268c6Sopenharmony_ci
1252c5e268c6Sopenharmony_cistd::string U8ItemToString(int32_t item, const camera_metadata_item_t entry)
1253c5e268c6Sopenharmony_ci{
1254c5e268c6Sopenharmony_ci    std::string st = {};
1255c5e268c6Sopenharmony_ci    uint32_t count = entry.count;
1256c5e268c6Sopenharmony_ci    if (entry.data.u8 == nullptr) {
1257c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("U8ItemToString: entry.data.u8 is null");
1258c5e268c6Sopenharmony_ci        return st;
1259c5e268c6Sopenharmony_ci    }
1260c5e268c6Sopenharmony_ci    std::string dataStr = std::to_string(*(entry.data.u8));
1261c5e268c6Sopenharmony_ci    for (uint32_t i = 1; i < count; i++) {
1262c5e268c6Sopenharmony_ci        if ((i % WRAP_LENGTH) == 0) {
1263c5e268c6Sopenharmony_ci            dataStr += "]\n\t[" + std::to_string(*(entry.data.u8 + i));
1264c5e268c6Sopenharmony_ci        } else {
1265c5e268c6Sopenharmony_ci            dataStr += " " + std::to_string(*(entry.data.u8 + i));
1266c5e268c6Sopenharmony_ci        }
1267c5e268c6Sopenharmony_ci    }
1268c5e268c6Sopenharmony_ci
1269c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(item);
1270c5e268c6Sopenharmony_ci    if (name == nullptr) {
1271c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("U8ItemToString: get u8 item name fail");
1272c5e268c6Sopenharmony_ci        return st;
1273c5e268c6Sopenharmony_ci    }
1274c5e268c6Sopenharmony_ci    std::string nameStr(name);
1275c5e268c6Sopenharmony_ci
1276c5e268c6Sopenharmony_ci    st = nameStr + " (" + std::to_string(entry.index) + "): " +
1277c5e268c6Sopenharmony_ci        OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
1278c5e268c6Sopenharmony_ci
1279c5e268c6Sopenharmony_ci    return st;
1280c5e268c6Sopenharmony_ci}
1281c5e268c6Sopenharmony_ci
1282c5e268c6Sopenharmony_cistd::string I32ItemToString(int32_t item, const camera_metadata_item_t entry)
1283c5e268c6Sopenharmony_ci{
1284c5e268c6Sopenharmony_ci    std::string st = {};
1285c5e268c6Sopenharmony_ci    uint32_t count = entry.count;
1286c5e268c6Sopenharmony_ci    if (entry.data.i32 == nullptr) {
1287c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("I32ItemToString: entry.data.i32 is null");
1288c5e268c6Sopenharmony_ci        return st;
1289c5e268c6Sopenharmony_ci    }
1290c5e268c6Sopenharmony_ci    std::string dataStr = std::to_string(*(entry.data.i32));
1291c5e268c6Sopenharmony_ci    for (uint32_t i = 1; i < count; i++) {
1292c5e268c6Sopenharmony_ci        if ((i % WRAP_LENGTH) == 0) {
1293c5e268c6Sopenharmony_ci            dataStr += "]\n\t[" + std::to_string(*(entry.data.i32 + i));
1294c5e268c6Sopenharmony_ci        } else {
1295c5e268c6Sopenharmony_ci            dataStr += " " + std::to_string(*(entry.data.i32 + i));
1296c5e268c6Sopenharmony_ci        }
1297c5e268c6Sopenharmony_ci    }
1298c5e268c6Sopenharmony_ci
1299c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(item);
1300c5e268c6Sopenharmony_ci    if (name == nullptr) {
1301c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("I32ItemToString: get i32 item name fail");
1302c5e268c6Sopenharmony_ci        return st;
1303c5e268c6Sopenharmony_ci    }
1304c5e268c6Sopenharmony_ci    std::string nameStr(name);
1305c5e268c6Sopenharmony_ci
1306c5e268c6Sopenharmony_ci    st = nameStr + " (" + std::to_string(entry.index) + "): " +
1307c5e268c6Sopenharmony_ci        OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
1308c5e268c6Sopenharmony_ci
1309c5e268c6Sopenharmony_ci    return st;
1310c5e268c6Sopenharmony_ci}
1311c5e268c6Sopenharmony_ci
1312c5e268c6Sopenharmony_cistd::string U32ItemToString(int32_t item, const camera_metadata_item_t entry)
1313c5e268c6Sopenharmony_ci{
1314c5e268c6Sopenharmony_ci    std::string st = {};
1315c5e268c6Sopenharmony_ci    uint32_t count = entry.count;
1316c5e268c6Sopenharmony_ci    if (entry.data.ui32 == nullptr) {
1317c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("U32ItemToString: entry.data.ui32 is null");
1318c5e268c6Sopenharmony_ci        return st;
1319c5e268c6Sopenharmony_ci    }
1320c5e268c6Sopenharmony_ci    std::string dataStr = std::to_string(*(entry.data.ui32));
1321c5e268c6Sopenharmony_ci    for (uint32_t i = 1; i < count; i++) {
1322c5e268c6Sopenharmony_ci        if ((i % WRAP_LENGTH) == 0) {
1323c5e268c6Sopenharmony_ci            dataStr += "]\n\t[" + std::to_string(*(entry.data.ui32 + i));
1324c5e268c6Sopenharmony_ci        } else {
1325c5e268c6Sopenharmony_ci            dataStr += " " + std::to_string(*(entry.data.ui32 + i));
1326c5e268c6Sopenharmony_ci        }
1327c5e268c6Sopenharmony_ci    }
1328c5e268c6Sopenharmony_ci
1329c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(item);
1330c5e268c6Sopenharmony_ci    if (name == nullptr) {
1331c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("U32ItemToString: get u32 item name fail");
1332c5e268c6Sopenharmony_ci        return st;
1333c5e268c6Sopenharmony_ci    }
1334c5e268c6Sopenharmony_ci    std::string nameStr(name);
1335c5e268c6Sopenharmony_ci
1336c5e268c6Sopenharmony_ci    st = nameStr + " (" + std::to_string(entry.index) + "): " +
1337c5e268c6Sopenharmony_ci        OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
1338c5e268c6Sopenharmony_ci
1339c5e268c6Sopenharmony_ci    return st;
1340c5e268c6Sopenharmony_ci}
1341c5e268c6Sopenharmony_ci
1342c5e268c6Sopenharmony_cistd::string I64ItemToString(int32_t item, const camera_metadata_item_t entry)
1343c5e268c6Sopenharmony_ci{
1344c5e268c6Sopenharmony_ci    std::string st = {};
1345c5e268c6Sopenharmony_ci    uint32_t count = entry.count;
1346c5e268c6Sopenharmony_ci    if (entry.data.i64 == nullptr) {
1347c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("I64ItemToString: entry.data.i64 is null");
1348c5e268c6Sopenharmony_ci        return st;
1349c5e268c6Sopenharmony_ci    }
1350c5e268c6Sopenharmony_ci    std::string dataStr = std::to_string(*(entry.data.i64));
1351c5e268c6Sopenharmony_ci    for (uint32_t i = 1; i < count; i++) {
1352c5e268c6Sopenharmony_ci        if ((i % WRAP_LENGTH) == 0) {
1353c5e268c6Sopenharmony_ci            dataStr += "]\n\t[" + std::to_string(*(entry.data.i64 + i));
1354c5e268c6Sopenharmony_ci        } else {
1355c5e268c6Sopenharmony_ci            dataStr += " " + std::to_string(*(entry.data.i64 + i));
1356c5e268c6Sopenharmony_ci        }
1357c5e268c6Sopenharmony_ci    }
1358c5e268c6Sopenharmony_ci
1359c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(item);
1360c5e268c6Sopenharmony_ci    if (name == nullptr) {
1361c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("I64ItemToString: get i64 item name fail");
1362c5e268c6Sopenharmony_ci        return st;
1363c5e268c6Sopenharmony_ci    }
1364c5e268c6Sopenharmony_ci    std::string nameStr(name);
1365c5e268c6Sopenharmony_ci
1366c5e268c6Sopenharmony_ci    st = nameStr + " (" + std::to_string(entry.index) + "): " +
1367c5e268c6Sopenharmony_ci        OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
1368c5e268c6Sopenharmony_ci
1369c5e268c6Sopenharmony_ci    return st;
1370c5e268c6Sopenharmony_ci}
1371c5e268c6Sopenharmony_ci
1372c5e268c6Sopenharmony_cistd::string FloatItemToString(int32_t item, const camera_metadata_item_t entry)
1373c5e268c6Sopenharmony_ci{
1374c5e268c6Sopenharmony_ci    std::string st = {};
1375c5e268c6Sopenharmony_ci    uint32_t count = entry.count;
1376c5e268c6Sopenharmony_ci    if (entry.data.f == nullptr) {
1377c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("FloatItemToString: entry.data.f is null");
1378c5e268c6Sopenharmony_ci        return st;
1379c5e268c6Sopenharmony_ci    }
1380c5e268c6Sopenharmony_ci    std::string dataStr = std::to_string(*(entry.data.f));
1381c5e268c6Sopenharmony_ci    for (uint32_t i = 1; i < count; i++) {
1382c5e268c6Sopenharmony_ci        if ((i % WRAP_LENGTH) == 0) {
1383c5e268c6Sopenharmony_ci            dataStr += "]\n\t[" + std::to_string(*(entry.data.f + i));
1384c5e268c6Sopenharmony_ci        } else {
1385c5e268c6Sopenharmony_ci            dataStr += " " + std::to_string(*(entry.data.f + i));
1386c5e268c6Sopenharmony_ci        }
1387c5e268c6Sopenharmony_ci    }
1388c5e268c6Sopenharmony_ci
1389c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(item);
1390c5e268c6Sopenharmony_ci    if (name == nullptr) {
1391c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("FloatItemToString: get float item name fail");
1392c5e268c6Sopenharmony_ci        return st;
1393c5e268c6Sopenharmony_ci    }
1394c5e268c6Sopenharmony_ci    std::string nameStr(name);
1395c5e268c6Sopenharmony_ci
1396c5e268c6Sopenharmony_ci    st = nameStr + " (" + std::to_string(entry.index) + "): " +
1397c5e268c6Sopenharmony_ci        OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
1398c5e268c6Sopenharmony_ci
1399c5e268c6Sopenharmony_ci    return st;
1400c5e268c6Sopenharmony_ci}
1401c5e268c6Sopenharmony_ci
1402c5e268c6Sopenharmony_cistd::string DoubleItemToString(int32_t item, const camera_metadata_item_t entry)
1403c5e268c6Sopenharmony_ci{
1404c5e268c6Sopenharmony_ci    std::string st = {};
1405c5e268c6Sopenharmony_ci    uint32_t count = entry.count;
1406c5e268c6Sopenharmony_ci    if (entry.data.d == nullptr) {
1407c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("DoubleItemToString: entry.data.d is null");
1408c5e268c6Sopenharmony_ci        return st;
1409c5e268c6Sopenharmony_ci    }
1410c5e268c6Sopenharmony_ci    std::string dataStr = std::to_string(*(entry.data.d));
1411c5e268c6Sopenharmony_ci    for (uint32_t i = 1; i < count; i++) {
1412c5e268c6Sopenharmony_ci        if ((i % WRAP_LENGTH) == 0) {
1413c5e268c6Sopenharmony_ci            dataStr += "]\n\t[" + std::to_string(*(entry.data.d + i));
1414c5e268c6Sopenharmony_ci        } else {
1415c5e268c6Sopenharmony_ci            dataStr += " " + std::to_string(*(entry.data.d + i));
1416c5e268c6Sopenharmony_ci        }
1417c5e268c6Sopenharmony_ci    }
1418c5e268c6Sopenharmony_ci
1419c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(item);
1420c5e268c6Sopenharmony_ci    if (name == nullptr) {
1421c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("DoubleItemToString: get double item name fail");
1422c5e268c6Sopenharmony_ci        return st;
1423c5e268c6Sopenharmony_ci    }
1424c5e268c6Sopenharmony_ci    std::string nameStr(name);
1425c5e268c6Sopenharmony_ci
1426c5e268c6Sopenharmony_ci    st = nameStr + " (" + std::to_string(entry.index) + "): " +
1427c5e268c6Sopenharmony_ci        OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
1428c5e268c6Sopenharmony_ci
1429c5e268c6Sopenharmony_ci    return st;
1430c5e268c6Sopenharmony_ci}
1431c5e268c6Sopenharmony_ci
1432c5e268c6Sopenharmony_cistd::string RationalItemToString(int32_t item, const camera_metadata_item_t entry)
1433c5e268c6Sopenharmony_ci{
1434c5e268c6Sopenharmony_ci    std::string st = {};
1435c5e268c6Sopenharmony_ci    uint32_t count = entry.count;
1436c5e268c6Sopenharmony_ci    if (entry.data.r == nullptr) {
1437c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("RationalItemToString: entry.data.r is null");
1438c5e268c6Sopenharmony_ci        return st;
1439c5e268c6Sopenharmony_ci    }
1440c5e268c6Sopenharmony_ci    std::string dataStr = std::to_string((*(entry.data.r)).numerator) + "/" +
1441c5e268c6Sopenharmony_ci        std::to_string((*(entry.data.r)).denominator);
1442c5e268c6Sopenharmony_ci    for (uint32_t i = 1; i < count; i++) {
1443c5e268c6Sopenharmony_ci        if ((i % WRAP_LENGTH) == 0) {
1444c5e268c6Sopenharmony_ci            dataStr += "]\n\t[" + std::to_string((*(entry.data.r + i)).numerator) + "/" +
1445c5e268c6Sopenharmony_ci                std::to_string((*(entry.data.r + i)).denominator);
1446c5e268c6Sopenharmony_ci        } else {
1447c5e268c6Sopenharmony_ci            dataStr += " " + std::to_string((*(entry.data.r + i)).numerator) + "/" +
1448c5e268c6Sopenharmony_ci                std::to_string((*(entry.data.r + i)).denominator);
1449c5e268c6Sopenharmony_ci        }
1450c5e268c6Sopenharmony_ci    }
1451c5e268c6Sopenharmony_ci
1452c5e268c6Sopenharmony_ci    const char *name = GetCameraMetadataItemName(item);
1453c5e268c6Sopenharmony_ci    if (name == nullptr) {
1454c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("RationalItemToString: get rational item name fail");
1455c5e268c6Sopenharmony_ci        return st;
1456c5e268c6Sopenharmony_ci    }
1457c5e268c6Sopenharmony_ci    std::string nameStr(name);
1458c5e268c6Sopenharmony_ci
1459c5e268c6Sopenharmony_ci    st = nameStr + " (" + std::to_string(entry.index) + "): " +
1460c5e268c6Sopenharmony_ci        OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
1461c5e268c6Sopenharmony_ci
1462c5e268c6Sopenharmony_ci    return st;
1463c5e268c6Sopenharmony_ci}
1464c5e268c6Sopenharmony_ci
1465c5e268c6Sopenharmony_cistd::string CameraMetadata::MetadataItemDump(const common_metadata_header_t *metadataHeader, uint32_t item)
1466c5e268c6Sopenharmony_ci{
1467c5e268c6Sopenharmony_ci    camera_metadata_item_t entry;
1468c5e268c6Sopenharmony_ci    std::string st = {};
1469c5e268c6Sopenharmony_ci
1470c5e268c6Sopenharmony_ci    int ret = FindCameraMetadataItem(metadataHeader, item, &entry);
1471c5e268c6Sopenharmony_ci    if (ret != 0) {
1472c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("get item error and item = %{public}d", item);
1473c5e268c6Sopenharmony_ci        return st;
1474c5e268c6Sopenharmony_ci    }
1475c5e268c6Sopenharmony_ci
1476c5e268c6Sopenharmony_ci    switch (entry.data_type) {
1477c5e268c6Sopenharmony_ci        case META_TYPE_BYTE:
1478c5e268c6Sopenharmony_ci            st = U8ItemToString(item, entry);
1479c5e268c6Sopenharmony_ci            break;
1480c5e268c6Sopenharmony_ci        case META_TYPE_INT32:
1481c5e268c6Sopenharmony_ci            st = I32ItemToString(item, entry);
1482c5e268c6Sopenharmony_ci            break;
1483c5e268c6Sopenharmony_ci        case META_TYPE_UINT32:
1484c5e268c6Sopenharmony_ci            st = U32ItemToString(item, entry);
1485c5e268c6Sopenharmony_ci            break;
1486c5e268c6Sopenharmony_ci        case META_TYPE_FLOAT:
1487c5e268c6Sopenharmony_ci            st = FloatItemToString(item, entry);
1488c5e268c6Sopenharmony_ci            break;
1489c5e268c6Sopenharmony_ci        case META_TYPE_INT64:
1490c5e268c6Sopenharmony_ci            st = I64ItemToString(item, entry);
1491c5e268c6Sopenharmony_ci            break;
1492c5e268c6Sopenharmony_ci        case META_TYPE_DOUBLE:
1493c5e268c6Sopenharmony_ci            st = DoubleItemToString(item, entry);
1494c5e268c6Sopenharmony_ci            break;
1495c5e268c6Sopenharmony_ci        case META_TYPE_RATIONAL:
1496c5e268c6Sopenharmony_ci            st = RationalItemToString(item, entry);
1497c5e268c6Sopenharmony_ci            break;
1498c5e268c6Sopenharmony_ci        default:
1499c5e268c6Sopenharmony_ci            METADATA_ERR_LOG("invalid param and item = %{public}d", item);
1500c5e268c6Sopenharmony_ci            break;
1501c5e268c6Sopenharmony_ci    }
1502c5e268c6Sopenharmony_ci
1503c5e268c6Sopenharmony_ci    if (!st.empty()) {
1504c5e268c6Sopenharmony_ci        st += "\n";
1505c5e268c6Sopenharmony_ci    }
1506c5e268c6Sopenharmony_ci    return st;
1507c5e268c6Sopenharmony_ci}
1508c5e268c6Sopenharmony_ci
1509c5e268c6Sopenharmony_cistd::string CameraMetadata::FormatCameraMetadataToString(const common_metadata_header_t *metadataHeader)
1510c5e268c6Sopenharmony_ci{
1511c5e268c6Sopenharmony_ci    std::string metaStr;
1512c5e268c6Sopenharmony_ci    if (metadataHeader == nullptr) {
1513c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("metadataHeader is nullptr");
1514c5e268c6Sopenharmony_ci        return metaStr;
1515c5e268c6Sopenharmony_ci    }
1516c5e268c6Sopenharmony_ci
1517c5e268c6Sopenharmony_ci    for (auto it = g_metadataTags.begin(); it != g_metadataTags.end(); it++) {
1518c5e268c6Sopenharmony_ci        metaStr += MetadataItemDump(metadataHeader, *it);
1519c5e268c6Sopenharmony_ci    }
1520c5e268c6Sopenharmony_ci    METADATA_DEBUG_LOG("metadataHeader item = %{public}s", metaStr.c_str());
1521c5e268c6Sopenharmony_ci    return metaStr;
1522c5e268c6Sopenharmony_ci}
1523c5e268c6Sopenharmony_ci
1524c5e268c6Sopenharmony_ciint32_t CameraMetadata::GetAllVendorTags(std::vector<vendorTag_t>& tagVec)
1525c5e268c6Sopenharmony_ci{
1526c5e268c6Sopenharmony_ci    int32_t ret = LoadVendorTagImpl();
1527c5e268c6Sopenharmony_ci    if (ret != CAM_META_SUCCESS) {
1528c5e268c6Sopenharmony_ci        METADATA_ERR_LOG("LoadVendorTagImpl failed");
1529c5e268c6Sopenharmony_ci        return CAM_META_FAILURE;
1530c5e268c6Sopenharmony_ci    }
1531c5e268c6Sopenharmony_ci    g_vendorTagImpl->GetAllVendorTags(tagVec);
1532c5e268c6Sopenharmony_ci    return CAM_META_SUCCESS;
1533c5e268c6Sopenharmony_ci}
1534c5e268c6Sopenharmony_ci} // Camera
1535