blob: ed5626115d225f6585bf7d9596d821b5c56063fd [file] [log] [blame]
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// #define LOG_NDEBUG 0
18
19#define LOG_TAG "CamComm1.0-MD"
Steven Moreland4e7a3072017-04-06 12:15:23 -070020#include <log/log.h>
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070021#include <utils/Errors.h>
22
23#include "CameraMetadata.h"
24#include "VendorTagDescriptor.h"
25
26namespace android {
27namespace hardware {
28namespace camera {
29namespace common {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070030namespace helper {
31
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070032#define ALIGN_TO(val, alignment) (((uintptr_t)(val) + ((alignment)-1)) & ~((alignment)-1))
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070033
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070034CameraMetadata::CameraMetadata() : mBuffer(NULL), mLocked(false) {}
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070035
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070036CameraMetadata::CameraMetadata(size_t entryCapacity, size_t dataCapacity) : mLocked(false) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070037 mBuffer = allocate_camera_metadata(entryCapacity, dataCapacity);
38}
39
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070040CameraMetadata::CameraMetadata(const CameraMetadata& other) : mLocked(false) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070041 mBuffer = clone_camera_metadata(other.mBuffer);
42}
43
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070044CameraMetadata::CameraMetadata(camera_metadata_t* buffer) : mBuffer(NULL), mLocked(false) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070045 acquire(buffer);
46}
47
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070048CameraMetadata& CameraMetadata::operator=(const CameraMetadata& other) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070049 return operator=(other.mBuffer);
50}
51
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070052CameraMetadata& CameraMetadata::operator=(const camera_metadata_t* buffer) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070053 if (mLocked) {
54 ALOGE("%s: Assignment to a locked CameraMetadata!", __FUNCTION__);
55 return *this;
56 }
57
58 if (CC_LIKELY(buffer != mBuffer)) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070059 camera_metadata_t* newBuffer = clone_camera_metadata(buffer);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070060 clear();
61 mBuffer = newBuffer;
62 }
63 return *this;
64}
65
66CameraMetadata::~CameraMetadata() {
67 mLocked = false;
68 clear();
69}
70
71const camera_metadata_t* CameraMetadata::getAndLock() const {
72 mLocked = true;
73 return mBuffer;
74}
75
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070076status_t CameraMetadata::unlock(const camera_metadata_t* buffer) const {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070077 if (!mLocked) {
78 ALOGE("%s: Can't unlock a non-locked CameraMetadata!", __FUNCTION__);
79 return INVALID_OPERATION;
80 }
81 if (buffer != mBuffer) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070082 ALOGE("%s: Can't unlock CameraMetadata with wrong pointer!", __FUNCTION__);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070083 return BAD_VALUE;
84 }
85 mLocked = false;
86 return OK;
87}
88
89camera_metadata_t* CameraMetadata::release() {
90 if (mLocked) {
91 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
92 return NULL;
93 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070094 camera_metadata_t* released = mBuffer;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070095 mBuffer = NULL;
96 return released;
97}
98
99void CameraMetadata::clear() {
100 if (mLocked) {
101 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
102 return;
103 }
104 if (mBuffer) {
105 free_camera_metadata(mBuffer);
106 mBuffer = NULL;
107 }
108}
109
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700110void CameraMetadata::acquire(camera_metadata_t* buffer) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700111 if (mLocked) {
112 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
113 return;
114 }
115 clear();
116 mBuffer = buffer;
117
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700118 ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/ NULL) != OK,
119 "%s: Failed to validate metadata structure %p", __FUNCTION__, buffer);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700120}
121
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700122void CameraMetadata::acquire(CameraMetadata& other) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700123 if (mLocked) {
124 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
125 return;
126 }
127 acquire(other.release());
128}
129
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700130status_t CameraMetadata::append(const CameraMetadata& other) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700131 return append(other.mBuffer);
132}
133
134status_t CameraMetadata::append(const camera_metadata_t* other) {
135 if (mLocked) {
136 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
137 return INVALID_OPERATION;
138 }
139 size_t extraEntries = get_camera_metadata_entry_count(other);
140 size_t extraData = get_camera_metadata_data_count(other);
141 resizeIfNeeded(extraEntries, extraData);
142
143 return append_camera_metadata(mBuffer, other);
144}
145
146size_t CameraMetadata::entryCount() const {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700147 return (mBuffer == NULL) ? 0 : get_camera_metadata_entry_count(mBuffer);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700148}
149
150bool CameraMetadata::isEmpty() const {
151 return entryCount() == 0;
152}
153
154status_t CameraMetadata::sort() {
155 if (mLocked) {
156 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
157 return INVALID_OPERATION;
158 }
159 return sort_camera_metadata(mBuffer);
160}
161
162status_t CameraMetadata::checkType(uint32_t tag, uint8_t expectedType) {
Jayant Chowdhary661f4412019-04-01 10:29:53 -0700163 int tagType = get_local_camera_metadata_tag_type(tag, mBuffer);
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700164 if (CC_UNLIKELY(tagType == -1)) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700165 ALOGE("Update metadata entry: Unknown tag %d", tag);
166 return INVALID_OPERATION;
167 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700168 if (CC_UNLIKELY(tagType != expectedType)) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700169 ALOGE("Mismatched tag type when updating entry %s (%d) of type %s; "
Jayant Chowdhary661f4412019-04-01 10:29:53 -0700170 "got type %s data instead ",
171 get_local_camera_metadata_tag_name(tag, mBuffer), tag,
172 camera_metadata_type_names[tagType], camera_metadata_type_names[expectedType]);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700173 return INVALID_OPERATION;
174 }
175 return OK;
176}
177
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700178status_t CameraMetadata::update(uint32_t tag, const int32_t* data, size_t data_count) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700179 status_t res;
180 if (mLocked) {
181 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
182 return INVALID_OPERATION;
183 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700184 if ((res = checkType(tag, TYPE_INT32)) != OK) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700185 return res;
186 }
187 return updateImpl(tag, (const void*)data, data_count);
188}
189
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700190status_t CameraMetadata::update(uint32_t tag, const uint8_t* data, size_t data_count) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700191 status_t res;
192 if (mLocked) {
193 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
194 return INVALID_OPERATION;
195 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700196 if ((res = checkType(tag, TYPE_BYTE)) != OK) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700197 return res;
198 }
199 return updateImpl(tag, (const void*)data, data_count);
200}
201
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700202status_t CameraMetadata::update(uint32_t tag, const float* data, size_t data_count) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700203 status_t res;
204 if (mLocked) {
205 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
206 return INVALID_OPERATION;
207 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700208 if ((res = checkType(tag, TYPE_FLOAT)) != OK) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700209 return res;
210 }
211 return updateImpl(tag, (const void*)data, data_count);
212}
213
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700214status_t CameraMetadata::update(uint32_t tag, const int64_t* data, size_t data_count) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700215 status_t res;
216 if (mLocked) {
217 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
218 return INVALID_OPERATION;
219 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700220 if ((res = checkType(tag, TYPE_INT64)) != OK) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700221 return res;
222 }
223 return updateImpl(tag, (const void*)data, data_count);
224}
225
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700226status_t CameraMetadata::update(uint32_t tag, const double* data, size_t data_count) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700227 status_t res;
228 if (mLocked) {
229 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
230 return INVALID_OPERATION;
231 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700232 if ((res = checkType(tag, TYPE_DOUBLE)) != OK) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700233 return res;
234 }
235 return updateImpl(tag, (const void*)data, data_count);
236}
237
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700238status_t CameraMetadata::update(uint32_t tag, const camera_metadata_rational_t* data,
239 size_t data_count) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700240 status_t res;
241 if (mLocked) {
242 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
243 return INVALID_OPERATION;
244 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700245 if ((res = checkType(tag, TYPE_RATIONAL)) != OK) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700246 return res;
247 }
248 return updateImpl(tag, (const void*)data, data_count);
249}
250
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700251status_t CameraMetadata::update(uint32_t tag, const String8& string) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700252 status_t res;
253 if (mLocked) {
254 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
255 return INVALID_OPERATION;
256 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700257 if ((res = checkType(tag, TYPE_BYTE)) != OK) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700258 return res;
259 }
260 // string.size() doesn't count the null termination character.
261 return updateImpl(tag, (const void*)string.string(), string.size() + 1);
262}
263
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700264status_t CameraMetadata::update(const camera_metadata_ro_entry& entry) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700265 status_t res;
266 if (mLocked) {
267 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
268 return INVALID_OPERATION;
269 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700270 if ((res = checkType(entry.tag, entry.type)) != OK) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700271 return res;
272 }
273 return updateImpl(entry.tag, (const void*)entry.data.u8, entry.count);
274}
275
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700276status_t CameraMetadata::updateImpl(uint32_t tag, const void* data, size_t data_count) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700277 status_t res;
278 if (mLocked) {
279 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
280 return INVALID_OPERATION;
281 }
Jayant Chowdhary661f4412019-04-01 10:29:53 -0700282 int type = get_local_camera_metadata_tag_type(tag, mBuffer);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700283 if (type == -1) {
284 ALOGE("%s: Tag %d not found", __FUNCTION__, tag);
285 return BAD_VALUE;
286 }
287 // Safety check - ensure that data isn't pointing to this metadata, since
288 // that would get invalidated if a resize is needed
289 size_t bufferSize = get_camera_metadata_size(mBuffer);
290 uintptr_t bufAddr = reinterpret_cast<uintptr_t>(mBuffer);
291 uintptr_t dataAddr = reinterpret_cast<uintptr_t>(data);
292 if (dataAddr > bufAddr && dataAddr < (bufAddr + bufferSize)) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700293 ALOGE("%s: Update attempted with data from the same metadata buffer!", __FUNCTION__);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700294 return INVALID_OPERATION;
295 }
296
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700297 size_t data_size = calculate_camera_metadata_entry_data_size(type, data_count);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700298
299 res = resizeIfNeeded(1, data_size);
300
301 if (res == OK) {
302 camera_metadata_entry_t entry;
303 res = find_camera_metadata_entry(mBuffer, tag, &entry);
304 if (res == NAME_NOT_FOUND) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700305 res = add_camera_metadata_entry(mBuffer, tag, data, data_count);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700306 } else if (res == OK) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700307 res = update_camera_metadata_entry(mBuffer, entry.index, data, data_count, NULL);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700308 }
309 }
310
311 if (res != OK) {
Jayant Chowdhary661f4412019-04-01 10:29:53 -0700312 ALOGE("%s: Unable to update metadata entry %s.%s (%x): %s (%d)", __FUNCTION__,
313 get_local_camera_metadata_section_name(tag, mBuffer),
314 get_local_camera_metadata_tag_name(tag, mBuffer), tag, strerror(-res), res);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700315 }
316
317 IF_ALOGV() {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700318 ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/ NULL) != OK,
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700319
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700320 "%s: Failed to validate metadata structure after update %p", __FUNCTION__,
321 mBuffer);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700322 }
323
324 return res;
325}
326
327bool CameraMetadata::exists(uint32_t tag) const {
328 camera_metadata_ro_entry entry;
329 return find_camera_metadata_ro_entry(mBuffer, tag, &entry) == 0;
330}
331
332camera_metadata_entry_t CameraMetadata::find(uint32_t tag) {
333 status_t res;
334 camera_metadata_entry entry;
335 if (mLocked) {
336 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
337 entry.count = 0;
338 return entry;
339 }
340 res = find_camera_metadata_entry(mBuffer, tag, &entry);
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700341 if (CC_UNLIKELY(res != OK)) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700342 entry.count = 0;
343 entry.data.u8 = NULL;
344 }
345 return entry;
346}
347
348camera_metadata_ro_entry_t CameraMetadata::find(uint32_t tag) const {
349 status_t res;
350 camera_metadata_ro_entry entry;
351 res = find_camera_metadata_ro_entry(mBuffer, tag, &entry);
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700352 if (CC_UNLIKELY(res != OK)) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700353 entry.count = 0;
354 entry.data.u8 = NULL;
355 }
356 return entry;
357}
358
359status_t CameraMetadata::erase(uint32_t tag) {
360 camera_metadata_entry_t entry;
361 status_t res;
362 if (mLocked) {
363 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
364 return INVALID_OPERATION;
365 }
366 res = find_camera_metadata_entry(mBuffer, tag, &entry);
367 if (res == NAME_NOT_FOUND) {
368 return OK;
369 } else if (res != OK) {
Jayant Chowdhary661f4412019-04-01 10:29:53 -0700370 ALOGE("%s: Error looking for entry %s.%s (%x): %s %d", __FUNCTION__,
371 get_local_camera_metadata_section_name(tag, mBuffer),
372 get_local_camera_metadata_tag_name(tag, mBuffer), tag, strerror(-res), res);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700373 return res;
374 }
375 res = delete_camera_metadata_entry(mBuffer, entry.index);
376 if (res != OK) {
Jayant Chowdhary661f4412019-04-01 10:29:53 -0700377 ALOGE("%s: Error deleting entry %s.%s (%x): %s %d", __FUNCTION__,
378 get_local_camera_metadata_section_name(tag, mBuffer),
379 get_local_camera_metadata_tag_name(tag, mBuffer), tag, strerror(-res), res);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700380 }
381 return res;
382}
383
384void CameraMetadata::dump(int fd, int verbosity, int indentation) const {
385 dump_indented_camera_metadata(mBuffer, fd, verbosity, indentation);
386}
387
388status_t CameraMetadata::resizeIfNeeded(size_t extraEntries, size_t extraData) {
389 if (mBuffer == NULL) {
390 mBuffer = allocate_camera_metadata(extraEntries * 2, extraData * 2);
391 if (mBuffer == NULL) {
392 ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__);
393 return NO_MEMORY;
394 }
395 } else {
396 size_t currentEntryCount = get_camera_metadata_entry_count(mBuffer);
397 size_t currentEntryCap = get_camera_metadata_entry_capacity(mBuffer);
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700398 size_t newEntryCount = currentEntryCount + extraEntries;
399 newEntryCount = (newEntryCount > currentEntryCap) ? newEntryCount * 2 : currentEntryCap;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700400
401 size_t currentDataCount = get_camera_metadata_data_count(mBuffer);
402 size_t currentDataCap = get_camera_metadata_data_capacity(mBuffer);
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700403 size_t newDataCount = currentDataCount + extraData;
404 newDataCount = (newDataCount > currentDataCap) ? newDataCount * 2 : currentDataCap;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700405
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700406 if (newEntryCount > currentEntryCap || newDataCount > currentDataCap) {
407 camera_metadata_t* oldBuffer = mBuffer;
408 mBuffer = allocate_camera_metadata(newEntryCount, newDataCount);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700409 if (mBuffer == NULL) {
410 ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__);
411 return NO_MEMORY;
412 }
413 append_camera_metadata(mBuffer, oldBuffer);
414 free_camera_metadata(oldBuffer);
415 }
416 }
417 return OK;
418}
419
420void CameraMetadata::swap(CameraMetadata& other) {
421 if (mLocked) {
422 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
423 return;
424 } else if (other.mLocked) {
425 ALOGE("%s: Other CameraMetadata is locked", __FUNCTION__);
426 return;
427 }
428
429 camera_metadata* thisBuf = mBuffer;
430 camera_metadata* otherBuf = other.mBuffer;
431
432 other.mBuffer = thisBuf;
433 mBuffer = otherBuf;
434}
435
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700436status_t CameraMetadata::getTagFromName(const char* name, const VendorTagDescriptor* vTags,
437 uint32_t* tag) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700438 if (name == nullptr || tag == nullptr) return BAD_VALUE;
439
440 size_t nameLength = strlen(name);
441
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700442 const SortedVector<String8>* vendorSections;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700443 size_t vendorSectionCount = 0;
444
445 if (vTags != NULL) {
446 vendorSections = vTags->getAllSectionNames();
447 vendorSectionCount = vendorSections->size();
448 }
449
450 // First, find the section by the longest string match
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700451 const char* section = NULL;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700452 size_t sectionIndex = 0;
453 size_t sectionLength = 0;
454 size_t totalSectionCount = ANDROID_SECTION_COUNT + vendorSectionCount;
455 for (size_t i = 0; i < totalSectionCount; ++i) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700456 const char* str = (i < ANDROID_SECTION_COUNT)
457 ? camera_metadata_section_names[i]
458 : (*vendorSections)[i - ANDROID_SECTION_COUNT].string();
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700459
460 ALOGV("%s: Trying to match against section '%s'", __FUNCTION__, str);
461
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700462 if (strstr(name, str) == name) { // name begins with the section name
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700463 size_t strLength = strlen(str);
464
465 ALOGV("%s: Name begins with section name", __FUNCTION__);
466
467 // section name is the longest we've found so far
468 if (section == NULL || sectionLength < strLength) {
469 section = str;
470 sectionIndex = i;
471 sectionLength = strLength;
472
473 ALOGV("%s: Found new best section (%s)", __FUNCTION__, section);
474 }
475 }
476 }
477
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700478 if (section == NULL) {
479 return NAME_NOT_FOUND;
480 } else {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700481 ALOGV("%s: Found matched section '%s' (%zu)", __FUNCTION__, section, sectionIndex);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700482 }
483
484 // Get the tag name component of the name
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700485 const char* nameTagName = name + sectionLength + 1; // x.y.z -> z
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700486 if (sectionLength + 1 >= nameLength) {
487 return BAD_VALUE;
488 }
489
490 // Match rest of name against the tag names in that section only
491 uint32_t candidateTag = 0;
492 if (sectionIndex < ANDROID_SECTION_COUNT) {
493 // Match built-in tags (typically android.*)
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700494 uint32_t tagBegin, tagEnd; // [tagBegin, tagEnd)
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700495 tagBegin = camera_metadata_section_bounds[sectionIndex][0];
496 tagEnd = camera_metadata_section_bounds[sectionIndex][1];
497
498 for (candidateTag = tagBegin; candidateTag < tagEnd; ++candidateTag) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700499 const char* tagName = get_camera_metadata_tag_name(candidateTag);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700500
501 if (strcmp(nameTagName, tagName) == 0) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700502 ALOGV("%s: Found matched tag '%s' (%d)", __FUNCTION__, tagName, candidateTag);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700503 break;
504 }
505 }
506
507 if (candidateTag == tagEnd) {
508 return NAME_NOT_FOUND;
509 }
510 } else if (vTags != NULL) {
511 // Match vendor tags (typically com.*)
512 const String8 sectionName(section);
513 const String8 tagName(nameTagName);
514
515 status_t res = OK;
516 if ((res = vTags->lookupTag(tagName, sectionName, &candidateTag)) != OK) {
517 return NAME_NOT_FOUND;
518 }
519 }
520
521 *tag = candidateTag;
522 return OK;
523}
524
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700525} // namespace helper
526} // namespace common
527} // namespace camera
528} // namespace hardware
529} // namespace android