VendorTagDescriptor.cpp revision f81648ec38ff63f1f35516fa27c1c24d846e9ba5
1/* 2 * Copyright (C) 2014 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_TAG "VendorTagDescriptor" 18 19#include <binder/Parcel.h> 20#include <utils/Errors.h> 21#include <utils/Log.h> 22#include <utils/Mutex.h> 23#include <utils/Vector.h> 24#include <utils/SortedVector.h> 25#include <system/camera_metadata.h> 26#include <camera_metadata_hidden.h> 27 28#include "camera/VendorTagDescriptor.h" 29 30#include <stdio.h> 31#include <string.h> 32 33namespace android { 34 35extern "C" { 36 37static int vendor_tag_descriptor_get_tag_count(const vendor_tag_ops_t* v); 38static void vendor_tag_descriptor_get_all_tags(const vendor_tag_ops_t* v, uint32_t* tagArray); 39static const char* vendor_tag_descriptor_get_section_name(const vendor_tag_ops_t* v, uint32_t tag); 40static const char* vendor_tag_descriptor_get_tag_name(const vendor_tag_ops_t* v, uint32_t tag); 41static int vendor_tag_descriptor_get_tag_type(const vendor_tag_ops_t* v, uint32_t tag); 42 43} /* extern "C" */ 44 45 46static Mutex sLock; 47static sp<VendorTagDescriptor> sGlobalVendorTagDescriptor; 48 49VendorTagDescriptor::VendorTagDescriptor() {} 50 51VendorTagDescriptor::~VendorTagDescriptor() { 52 size_t len = mReverseMapping.size(); 53 for (size_t i = 0; i < len; ++i) { 54 delete mReverseMapping[i]; 55 } 56} 57 58status_t VendorTagDescriptor::createDescriptorFromOps(const vendor_tag_ops_t* vOps, 59 /*out*/ 60 sp<VendorTagDescriptor>& descriptor) { 61 if (vOps == NULL) { 62 ALOGE("%s: vendor_tag_ops argument was NULL.", __FUNCTION__); 63 return BAD_VALUE; 64 } 65 66 int tagCount = vOps->get_tag_count(vOps); 67 if (tagCount < 0 || tagCount > INT32_MAX) { 68 ALOGE("%s: tag count %d from vendor ops is invalid.", __FUNCTION__, tagCount); 69 return BAD_VALUE; 70 } 71 72 Vector<uint32_t> tagArray; 73 LOG_ALWAYS_FATAL_IF(tagArray.resize(tagCount) != tagCount, 74 "%s: too many (%u) vendor tags defined.", __FUNCTION__, tagCount); 75 76 vOps->get_all_tags(vOps, /*out*/tagArray.editArray()); 77 78 sp<VendorTagDescriptor> desc = new VendorTagDescriptor(); 79 desc->mTagCount = tagCount; 80 81 SortedVector<String8> sections; 82 KeyedVector<uint32_t, String8> tagToSectionMap; 83 84 for (size_t i = 0; i < static_cast<size_t>(tagCount); ++i) { 85 uint32_t tag = tagArray[i]; 86 if (tag < CAMERA_METADATA_VENDOR_TAG_BOUNDARY) { 87 ALOGE("%s: vendor tag %d not in vendor tag section.", __FUNCTION__, tag); 88 return BAD_VALUE; 89 } 90 const char *tagName = vOps->get_tag_name(vOps, tag); 91 if (tagName == NULL) { 92 ALOGE("%s: no tag name defined for vendor tag %d.", __FUNCTION__, tag); 93 return BAD_VALUE; 94 } 95 desc->mTagToNameMap.add(tag, String8(tagName)); 96 const char *sectionName = vOps->get_section_name(vOps, tag); 97 if (sectionName == NULL) { 98 ALOGE("%s: no section name defined for vendor tag %d.", __FUNCTION__, tag); 99 return BAD_VALUE; 100 } 101 102 String8 sectionString(sectionName); 103 104 sections.add(sectionString); 105 tagToSectionMap.add(tag, sectionString); 106 107 int tagType = vOps->get_tag_type(vOps, tag); 108 if (tagType < 0 || tagType >= NUM_TYPES) { 109 ALOGE("%s: tag type %d from vendor ops does not exist.", __FUNCTION__, tagType); 110 return BAD_VALUE; 111 } 112 desc->mTagToTypeMap.add(tag, tagType); 113 } 114 115 desc->mSections = sections; 116 117 for (size_t i = 0; i < static_cast<size_t>(tagCount); ++i) { 118 uint32_t tag = tagArray[i]; 119 String8 sectionString = tagToSectionMap.valueFor(tag); 120 121 // Set up tag to section index map 122 ssize_t index = sections.indexOf(sectionString); 123 assert(index >= 0); 124 desc->mTagToSectionMap.add(tag, static_cast<uint32_t>(index)); 125 126 // Set up reverse mapping 127 ssize_t reverseIndex = -1; 128 if ((reverseIndex = desc->mReverseMapping.indexOfKey(sectionString)) < 0) { 129 KeyedVector<String8, uint32_t>* nameMapper = new KeyedVector<String8, uint32_t>(); 130 reverseIndex = desc->mReverseMapping.add(sectionString, nameMapper); 131 } 132 desc->mReverseMapping[reverseIndex]->add(desc->mTagToNameMap.valueFor(tag), tag); 133 } 134 135 descriptor = desc; 136 return OK; 137} 138 139status_t VendorTagDescriptor::createFromParcel(const Parcel* parcel, 140 /*out*/ 141 sp<VendorTagDescriptor>& descriptor) { 142 status_t res = OK; 143 if (parcel == NULL) { 144 ALOGE("%s: parcel argument was NULL.", __FUNCTION__); 145 return BAD_VALUE; 146 } 147 148 int32_t tagCount = 0; 149 if ((res = parcel->readInt32(&tagCount)) != OK) { 150 ALOGE("%s: could not read tag count from parcel", __FUNCTION__); 151 return res; 152 } 153 154 if (tagCount < 0 || tagCount > INT32_MAX) { 155 ALOGE("%s: tag count %d from vendor ops is invalid.", __FUNCTION__, tagCount); 156 return BAD_VALUE; 157 } 158 159 sp<VendorTagDescriptor> desc = new VendorTagDescriptor(); 160 desc->mTagCount = tagCount; 161 162 uint32_t tag, sectionIndex; 163 uint32_t maxSectionIndex = 0; 164 int32_t tagType; 165 Vector<uint32_t> allTags; 166 for (int32_t i = 0; i < tagCount; ++i) { 167 if ((res = parcel->readInt32(reinterpret_cast<int32_t*>(&tag))) != OK) { 168 ALOGE("%s: could not read tag id from parcel for index %d", __FUNCTION__, i); 169 break; 170 } 171 if (tag < CAMERA_METADATA_VENDOR_TAG_BOUNDARY) { 172 ALOGE("%s: vendor tag %d not in vendor tag section.", __FUNCTION__, tag); 173 res = BAD_VALUE; 174 break; 175 } 176 if ((res = parcel->readInt32(&tagType)) != OK) { 177 ALOGE("%s: could not read tag type from parcel for tag %d", __FUNCTION__, tag); 178 break; 179 } 180 if (tagType < 0 || tagType >= NUM_TYPES) { 181 ALOGE("%s: tag type %d from vendor ops does not exist.", __FUNCTION__, tagType); 182 res = BAD_VALUE; 183 break; 184 } 185 String8 tagName = parcel->readString8(); 186 if (tagName.isEmpty()) { 187 ALOGE("%s: parcel tag name was NULL for tag %d.", __FUNCTION__, tag); 188 res = NOT_ENOUGH_DATA; 189 break; 190 } 191 192 if ((res = parcel->readInt32(reinterpret_cast<int32_t*>(§ionIndex))) != OK) { 193 ALOGE("%s: could not read section index for tag %d.", __FUNCTION__, tag); 194 break; 195 } 196 197 maxSectionIndex = (maxSectionIndex >= sectionIndex) ? maxSectionIndex : sectionIndex; 198 199 allTags.add(tag); 200 desc->mTagToNameMap.add(tag, tagName); 201 desc->mTagToSectionMap.add(tag, sectionIndex); 202 desc->mTagToTypeMap.add(tag, tagType); 203 } 204 205 if (res != OK) { 206 return res; 207 } 208 209 size_t sectionCount; 210 if (tagCount > 0) { 211 if ((res = parcel->readInt32(reinterpret_cast<int32_t*>(§ionCount))) != OK) { 212 ALOGE("%s: could not read section count for.", __FUNCTION__); 213 return res; 214 } 215 if (sectionCount < (maxSectionIndex + 1)) { 216 ALOGE("%s: Incorrect number of sections defined, received %d, needs %d.", 217 __FUNCTION__, sectionCount, (maxSectionIndex + 1)); 218 return BAD_VALUE; 219 } 220 assert(desc->mSections.setCapacity(sectionCount) > 0); 221 for (size_t i = 0; i < sectionCount; ++i) { 222 String8 sectionName = parcel->readString8(); 223 if (sectionName.isEmpty()) { 224 ALOGE("%s: parcel section name was NULL for section %d.", __FUNCTION__, i); 225 return NOT_ENOUGH_DATA; 226 } 227 desc->mSections.add(sectionName); 228 } 229 } 230 231 assert(tagCount == allTags.size()); 232 // Set up reverse mapping 233 for (size_t i = 0; i < static_cast<size_t>(tagCount); ++i) { 234 uint32_t tag = allTags[i]; 235 String8 sectionString = desc->mSections[desc->mTagToSectionMap.valueFor(tag)]; 236 237 ssize_t reverseIndex = -1; 238 if ((reverseIndex = desc->mReverseMapping.indexOfKey(sectionString)) < 0) { 239 KeyedVector<String8, uint32_t>* nameMapper = new KeyedVector<String8, uint32_t>(); 240 reverseIndex = desc->mReverseMapping.add(sectionString, nameMapper); 241 } 242 desc->mReverseMapping[reverseIndex]->add(desc->mTagToNameMap.valueFor(tag), tag); 243 } 244 245 descriptor = desc; 246 return res; 247} 248 249int VendorTagDescriptor::getTagCount() const { 250 size_t size = mTagToNameMap.size(); 251 if (size == 0) { 252 return VENDOR_TAG_COUNT_ERR; 253 } 254 return size; 255} 256 257void VendorTagDescriptor::getTagArray(uint32_t* tagArray) const { 258 size_t size = mTagToNameMap.size(); 259 for (size_t i = 0; i < size; ++i) { 260 tagArray[i] = mTagToNameMap.keyAt(i); 261 } 262} 263 264const char* VendorTagDescriptor::getSectionName(uint32_t tag) const { 265 ssize_t index = mTagToSectionMap.indexOfKey(tag); 266 if (index < 0) { 267 return VENDOR_SECTION_NAME_ERR; 268 } 269 return mSections[mTagToSectionMap.valueAt(index)].string(); 270} 271 272const char* VendorTagDescriptor::getTagName(uint32_t tag) const { 273 ssize_t index = mTagToNameMap.indexOfKey(tag); 274 if (index < 0) { 275 return VENDOR_TAG_NAME_ERR; 276 } 277 return mTagToNameMap.valueAt(index).string(); 278} 279 280int VendorTagDescriptor::getTagType(uint32_t tag) const { 281 ssize_t index = mTagToNameMap.indexOfKey(tag); 282 if (index < 0) { 283 return VENDOR_TAG_TYPE_ERR; 284 } 285 return mTagToTypeMap.valueFor(tag); 286} 287 288status_t VendorTagDescriptor::writeToParcel(Parcel* parcel) const { 289 status_t res = OK; 290 if (parcel == NULL) { 291 ALOGE("%s: parcel argument was NULL.", __FUNCTION__); 292 return BAD_VALUE; 293 } 294 295 if ((res = parcel->writeInt32(mTagCount)) != OK) { 296 return res; 297 } 298 299 size_t size = mTagToNameMap.size(); 300 uint32_t tag, sectionIndex; 301 int32_t tagType; 302 for (size_t i = 0; i < size; ++i) { 303 tag = mTagToNameMap.keyAt(i); 304 String8 tagName = mTagToNameMap[i]; 305 sectionIndex = mTagToSectionMap.valueFor(tag); 306 tagType = mTagToTypeMap.valueFor(tag); 307 if ((res = parcel->writeInt32(tag)) != OK) break; 308 if ((res = parcel->writeInt32(tagType)) != OK) break; 309 if ((res = parcel->writeString8(tagName)) != OK) break; 310 if ((res = parcel->writeInt32(sectionIndex)) != OK) break; 311 } 312 313 size_t numSections = mSections.size(); 314 if (numSections > 0) { 315 if ((res = parcel->writeInt32(numSections)) != OK) return res; 316 for (size_t i = 0; i < numSections; ++i) { 317 if ((res = parcel->writeString8(mSections[i])) != OK) return res; 318 } 319 } 320 321 return res; 322} 323 324SortedVector<String8> VendorTagDescriptor::getAllSectionNames() const { 325 return mSections; 326} 327 328status_t VendorTagDescriptor::lookupTag(String8 name, String8 section, /*out*/uint32_t* tag) const { 329 ssize_t index = mReverseMapping.indexOfKey(section); 330 if (index < 0) { 331 ALOGE("%s: Section '%s' does not exist.", __FUNCTION__, section.string()); 332 return BAD_VALUE; 333 } 334 335 ssize_t nameIndex = mReverseMapping[index]->indexOfKey(name); 336 if (nameIndex < 0) { 337 ALOGE("%s: Tag name '%s' does not exist.", __FUNCTION__, name.string()); 338 return BAD_VALUE; 339 } 340 341 if (tag != NULL) { 342 *tag = mReverseMapping[index]->valueAt(nameIndex); 343 } 344 return OK; 345} 346 347void VendorTagDescriptor::dump(int fd, int verbosity, int indentation) const { 348 349 size_t size = mTagToNameMap.size(); 350 if (size == 0) { 351 fdprintf(fd, "%*sDumping configured vendor tag descriptors: None set\n", 352 indentation, ""); 353 return; 354 } 355 356 fdprintf(fd, "%*sDumping configured vendor tag descriptors: %zu entries\n", 357 indentation, "", size); 358 for (size_t i = 0; i < size; ++i) { 359 uint32_t tag = mTagToNameMap.keyAt(i); 360 361 if (verbosity < 1) { 362 fdprintf(fd, "%*s0x%x\n", indentation + 2, "", tag); 363 continue; 364 } 365 String8 name = mTagToNameMap.valueAt(i); 366 uint32_t sectionId = mTagToSectionMap.valueFor(tag); 367 String8 sectionName = mSections[sectionId]; 368 int type = mTagToTypeMap.valueFor(tag); 369 const char* typeName = (type >= 0 && type < NUM_TYPES) ? 370 camera_metadata_type_names[type] : "UNKNOWN"; 371 fdprintf(fd, "%*s0x%x (%s) with type %d (%s) defined in section %s\n", indentation + 2, 372 "", tag, name.string(), type, typeName, sectionName.string()); 373 } 374 375} 376 377status_t VendorTagDescriptor::setAsGlobalVendorTagDescriptor(const sp<VendorTagDescriptor>& desc) { 378 status_t res = OK; 379 Mutex::Autolock al(sLock); 380 sGlobalVendorTagDescriptor = desc; 381 382 vendor_tag_ops_t* opsPtr = NULL; 383 if (desc != NULL) { 384 opsPtr = &(desc->mVendorOps); 385 opsPtr->get_tag_count = vendor_tag_descriptor_get_tag_count; 386 opsPtr->get_all_tags = vendor_tag_descriptor_get_all_tags; 387 opsPtr->get_section_name = vendor_tag_descriptor_get_section_name; 388 opsPtr->get_tag_name = vendor_tag_descriptor_get_tag_name; 389 opsPtr->get_tag_type = vendor_tag_descriptor_get_tag_type; 390 } 391 if((res = set_camera_metadata_vendor_ops(opsPtr)) != OK) { 392 ALOGE("%s: Could not set vendor tag descriptor, received error %s (%d)." 393 , __FUNCTION__, strerror(-res), res); 394 } 395 return res; 396} 397 398void VendorTagDescriptor::clearGlobalVendorTagDescriptor() { 399 Mutex::Autolock al(sLock); 400 set_camera_metadata_vendor_ops(NULL); 401 sGlobalVendorTagDescriptor.clear(); 402} 403 404sp<VendorTagDescriptor> VendorTagDescriptor::getGlobalVendorTagDescriptor() { 405 Mutex::Autolock al(sLock); 406 return sGlobalVendorTagDescriptor; 407} 408 409extern "C" { 410 411int vendor_tag_descriptor_get_tag_count(const vendor_tag_ops_t* v) { 412 Mutex::Autolock al(sLock); 413 if (sGlobalVendorTagDescriptor == NULL) { 414 ALOGE("%s: Vendor tag descriptor not initialized.", __FUNCTION__); 415 return VENDOR_TAG_COUNT_ERR; 416 } 417 return sGlobalVendorTagDescriptor->getTagCount(); 418} 419 420void vendor_tag_descriptor_get_all_tags(const vendor_tag_ops_t* v, uint32_t* tagArray) { 421 Mutex::Autolock al(sLock); 422 if (sGlobalVendorTagDescriptor == NULL) { 423 ALOGE("%s: Vendor tag descriptor not initialized.", __FUNCTION__); 424 return; 425 } 426 sGlobalVendorTagDescriptor->getTagArray(tagArray); 427} 428 429const char* vendor_tag_descriptor_get_section_name(const vendor_tag_ops_t* v, uint32_t tag) { 430 Mutex::Autolock al(sLock); 431 if (sGlobalVendorTagDescriptor == NULL) { 432 ALOGE("%s: Vendor tag descriptor not initialized.", __FUNCTION__); 433 return VENDOR_SECTION_NAME_ERR; 434 } 435 return sGlobalVendorTagDescriptor->getSectionName(tag); 436} 437 438const char* vendor_tag_descriptor_get_tag_name(const vendor_tag_ops_t* v, uint32_t tag) { 439 Mutex::Autolock al(sLock); 440 if (sGlobalVendorTagDescriptor == NULL) { 441 ALOGE("%s: Vendor tag descriptor not initialized.", __FUNCTION__); 442 return VENDOR_TAG_NAME_ERR; 443 } 444 return sGlobalVendorTagDescriptor->getTagName(tag); 445} 446 447int vendor_tag_descriptor_get_tag_type(const vendor_tag_ops_t* v, uint32_t tag) { 448 Mutex::Autolock al(sLock); 449 if (sGlobalVendorTagDescriptor == NULL) { 450 ALOGE("%s: Vendor tag descriptor not initialized.", __FUNCTION__); 451 return VENDOR_TAG_TYPE_ERR; 452 } 453 return sGlobalVendorTagDescriptor->getTagType(tag); 454} 455 456} /* extern "C" */ 457} /* namespace android */ 458