1 /*
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_TAG "CameraProviderManager"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20
21 #include "CameraProviderManager.h"
22
23 #include <chrono>
24 #include <inttypes.h>
25 #include <hidl/ServiceManagement.h>
26 #include <functional>
27 #include <camera_metadata_hidden.h>
28
29 namespace android {
30
31 using namespace ::android::hardware::camera;
32 using namespace ::android::hardware::camera::common::V1_0;
33
34 namespace {
35 // Hardcoded name for the passthrough HAL implementation, since it can't be discovered via the
36 // service manager
37 const std::string kLegacyProviderName("legacy/0");
38
39 // Slash-separated list of provider types to consider for use via the old camera API
40 const std::string kStandardProviderTypes("internal/legacy");
41
42 } // anonymous namespace
43
44 CameraProviderManager::HardwareServiceInteractionProxy
45 CameraProviderManager::sHardwareServiceInteractionProxy{};
46
~CameraProviderManager()47 CameraProviderManager::~CameraProviderManager() {
48 }
49
initialize(wp<CameraProviderManager::StatusListener> listener,ServiceInteractionProxy * proxy)50 status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
51 ServiceInteractionProxy* proxy) {
52 std::lock_guard<std::mutex> lock(mInterfaceMutex);
53 if (proxy == nullptr) {
54 ALOGE("%s: No valid service interaction proxy provided", __FUNCTION__);
55 return BAD_VALUE;
56 }
57 mListener = listener;
58 mServiceProxy = proxy;
59
60 // Registering will trigger notifications for all already-known providers
61 bool success = mServiceProxy->registerForNotifications(
62 /* instance name, empty means no filter */ "",
63 this);
64 if (!success) {
65 ALOGE("%s: Unable to register with hardware service manager for notifications "
66 "about camera providers", __FUNCTION__);
67 return INVALID_OPERATION;
68 }
69
70 // See if there's a passthrough HAL, but let's not complain if there's not
71 addProviderLocked(kLegacyProviderName, /*expected*/ false);
72
73 return OK;
74 }
75
getCameraCount() const76 int CameraProviderManager::getCameraCount() const {
77 std::lock_guard<std::mutex> lock(mInterfaceMutex);
78 int count = 0;
79 for (auto& provider : mProviders) {
80 count += provider->mUniqueDeviceCount;
81 }
82 return count;
83 }
84
getAPI1CompatibleCameraCount() const85 int CameraProviderManager::getAPI1CompatibleCameraCount() const {
86 std::lock_guard<std::mutex> lock(mInterfaceMutex);
87 int count = 0;
88 for (auto& provider : mProviders) {
89 if (kStandardProviderTypes.find(provider->getType()) != std::string::npos) {
90 count += provider->mUniqueAPI1CompatibleCameraIds.size();
91 }
92 }
93 return count;
94 }
95
getCameraDeviceIds() const96 std::vector<std::string> CameraProviderManager::getCameraDeviceIds() const {
97 std::lock_guard<std::mutex> lock(mInterfaceMutex);
98 std::vector<std::string> deviceIds;
99 for (auto& provider : mProviders) {
100 for (auto& id : provider->mUniqueCameraIds) {
101 deviceIds.push_back(id);
102 }
103 }
104 return deviceIds;
105 }
106
getAPI1CompatibleCameraDeviceIds() const107 std::vector<std::string> CameraProviderManager::getAPI1CompatibleCameraDeviceIds() const {
108 std::lock_guard<std::mutex> lock(mInterfaceMutex);
109 std::vector<std::string> deviceIds;
110 for (auto& provider : mProviders) {
111 if (kStandardProviderTypes.find(provider->getType()) != std::string::npos) {
112 for (auto& id : provider->mUniqueAPI1CompatibleCameraIds) {
113 deviceIds.push_back(id);
114 }
115 }
116 }
117 return deviceIds;
118 }
119
isValidDevice(const std::string & id,uint16_t majorVersion) const120 bool CameraProviderManager::isValidDevice(const std::string &id, uint16_t majorVersion) const {
121 std::lock_guard<std::mutex> lock(mInterfaceMutex);
122 return isValidDeviceLocked(id, majorVersion);
123 }
124
isValidDeviceLocked(const std::string & id,uint16_t majorVersion) const125 bool CameraProviderManager::isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const {
126 for (auto& provider : mProviders) {
127 for (auto& deviceInfo : provider->mDevices) {
128 if (deviceInfo->mId == id && deviceInfo->mVersion.get_major() == majorVersion) {
129 return true;
130 }
131 }
132 }
133 return false;
134 }
135
hasFlashUnit(const std::string & id) const136 bool CameraProviderManager::hasFlashUnit(const std::string &id) const {
137 std::lock_guard<std::mutex> lock(mInterfaceMutex);
138
139 auto deviceInfo = findDeviceInfoLocked(id);
140 if (deviceInfo == nullptr) return false;
141
142 return deviceInfo->hasFlashUnit();
143 }
144
getResourceCost(const std::string & id,CameraResourceCost * cost) const145 status_t CameraProviderManager::getResourceCost(const std::string &id,
146 CameraResourceCost* cost) const {
147 std::lock_guard<std::mutex> lock(mInterfaceMutex);
148
149 auto deviceInfo = findDeviceInfoLocked(id);
150 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
151
152 *cost = deviceInfo->mResourceCost;
153 return OK;
154 }
155
getCameraInfo(const std::string & id,hardware::CameraInfo * info) const156 status_t CameraProviderManager::getCameraInfo(const std::string &id,
157 hardware::CameraInfo* info) const {
158 std::lock_guard<std::mutex> lock(mInterfaceMutex);
159
160 auto deviceInfo = findDeviceInfoLocked(id);
161 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
162
163 return deviceInfo->getCameraInfo(info);
164 }
165
getCameraCharacteristics(const std::string & id,CameraMetadata * characteristics) const166 status_t CameraProviderManager::getCameraCharacteristics(const std::string &id,
167 CameraMetadata* characteristics) const {
168 std::lock_guard<std::mutex> lock(mInterfaceMutex);
169
170 auto deviceInfo = findDeviceInfoLocked(id, /*minVersion*/ {3,0}, /*maxVersion*/ {4,0});
171 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
172
173 return deviceInfo->getCameraCharacteristics(characteristics);
174 }
175
getHighestSupportedVersion(const std::string & id,hardware::hidl_version * v)176 status_t CameraProviderManager::getHighestSupportedVersion(const std::string &id,
177 hardware::hidl_version *v) {
178 std::lock_guard<std::mutex> lock(mInterfaceMutex);
179
180 hardware::hidl_version maxVersion{0,0};
181 bool found = false;
182 for (auto& provider : mProviders) {
183 for (auto& deviceInfo : provider->mDevices) {
184 if (deviceInfo->mId == id) {
185 if (deviceInfo->mVersion > maxVersion) {
186 maxVersion = deviceInfo->mVersion;
187 found = true;
188 }
189 }
190 }
191 }
192 if (!found) {
193 return NAME_NOT_FOUND;
194 }
195 *v = maxVersion;
196 return OK;
197 }
198
supportSetTorchMode(const std::string & id)199 bool CameraProviderManager::supportSetTorchMode(const std::string &id) {
200 std::lock_guard<std::mutex> lock(mInterfaceMutex);
201 bool support = false;
202 for (auto& provider : mProviders) {
203 auto deviceInfo = findDeviceInfoLocked(id);
204 if (deviceInfo != nullptr) {
205 auto ret = provider->mInterface->isSetTorchModeSupported(
206 [&support](auto status, bool supported) {
207 if (status == Status::OK) {
208 support = supported;
209 }
210 });
211 if (!ret.isOk()) {
212 ALOGE("%s: Transaction error checking torch mode support '%s': %s",
213 __FUNCTION__, provider->mProviderName.c_str(), ret.description().c_str());
214 }
215 break;
216 }
217 }
218 return support;
219 }
220
setTorchMode(const std::string & id,bool enabled)221 status_t CameraProviderManager::setTorchMode(const std::string &id, bool enabled) {
222 std::lock_guard<std::mutex> lock(mInterfaceMutex);
223
224 auto deviceInfo = findDeviceInfoLocked(id);
225 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
226
227 return deviceInfo->setTorchMode(enabled);
228 }
229
setUpVendorTags()230 status_t CameraProviderManager::setUpVendorTags() {
231 sp<VendorTagDescriptorCache> tagCache = new VendorTagDescriptorCache();
232
233 for (auto& provider : mProviders) {
234 hardware::hidl_vec<VendorTagSection> vts;
235 Status status;
236 hardware::Return<void> ret;
237 ret = provider->mInterface->getVendorTags(
238 [&](auto s, const auto& vendorTagSecs) {
239 status = s;
240 if (s == Status::OK) {
241 vts = vendorTagSecs;
242 }
243 });
244 if (!ret.isOk()) {
245 ALOGE("%s: Transaction error getting vendor tags from provider '%s': %s",
246 __FUNCTION__, provider->mProviderName.c_str(), ret.description().c_str());
247 return DEAD_OBJECT;
248 }
249 if (status != Status::OK) {
250 return mapToStatusT(status);
251 }
252
253 // Read all vendor tag definitions into a descriptor
254 sp<VendorTagDescriptor> desc;
255 status_t res;
256 if ((res = HidlVendorTagDescriptor::createDescriptorFromHidl(vts, /*out*/desc))
257 != OK) {
258 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
259 "received error %s (%d). Camera clients will not be able to use"
260 "vendor tags", __FUNCTION__, strerror(res), res);
261 return res;
262 }
263
264 tagCache->addVendorDescriptor(provider->mProviderTagid, desc);
265 }
266
267 VendorTagDescriptorCache::setAsGlobalVendorTagCache(tagCache);
268
269 return OK;
270 }
271
openSession(const std::string & id,const sp<hardware::camera::device::V3_2::ICameraDeviceCallback> & callback,sp<hardware::camera::device::V3_2::ICameraDeviceSession> * session)272 status_t CameraProviderManager::openSession(const std::string &id,
273 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
274 /*out*/
275 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session) {
276
277 std::lock_guard<std::mutex> lock(mInterfaceMutex);
278
279 auto deviceInfo = findDeviceInfoLocked(id,
280 /*minVersion*/ {3,0}, /*maxVersion*/ {4,0});
281 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
282
283 auto *deviceInfo3 = static_cast<ProviderInfo::DeviceInfo3*>(deviceInfo);
284
285 Status status;
286 hardware::Return<void> ret;
287 ret = deviceInfo3->mInterface->open(callback, [&status, &session]
288 (Status s, const sp<device::V3_2::ICameraDeviceSession>& cameraSession) {
289 status = s;
290 if (status == Status::OK) {
291 *session = cameraSession;
292 }
293 });
294 if (!ret.isOk()) {
295 ALOGE("%s: Transaction error opening a session for camera device %s: %s",
296 __FUNCTION__, id.c_str(), ret.description().c_str());
297 return DEAD_OBJECT;
298 }
299 return mapToStatusT(status);
300 }
301
openSession(const std::string & id,const sp<hardware::camera::device::V1_0::ICameraDeviceCallback> & callback,sp<hardware::camera::device::V1_0::ICameraDevice> * session)302 status_t CameraProviderManager::openSession(const std::string &id,
303 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
304 /*out*/
305 sp<hardware::camera::device::V1_0::ICameraDevice> *session) {
306
307 std::lock_guard<std::mutex> lock(mInterfaceMutex);
308
309 auto deviceInfo = findDeviceInfoLocked(id,
310 /*minVersion*/ {1,0}, /*maxVersion*/ {2,0});
311 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
312
313 auto *deviceInfo1 = static_cast<ProviderInfo::DeviceInfo1*>(deviceInfo);
314
315 hardware::Return<Status> status = deviceInfo1->mInterface->open(callback);
316 if (!status.isOk()) {
317 ALOGE("%s: Transaction error opening a session for camera device %s: %s",
318 __FUNCTION__, id.c_str(), status.description().c_str());
319 return DEAD_OBJECT;
320 }
321 if (status == Status::OK) {
322 *session = deviceInfo1->mInterface;
323 }
324 return mapToStatusT(status);
325 }
326
327
onRegistration(const hardware::hidl_string &,const hardware::hidl_string & name,bool)328 hardware::Return<void> CameraProviderManager::onRegistration(
329 const hardware::hidl_string& /*fqName*/,
330 const hardware::hidl_string& name,
331 bool /*preexisting*/) {
332 {
333 std::lock_guard<std::mutex> lock(mInterfaceMutex);
334
335 addProviderLocked(name);
336 }
337
338 sp<StatusListener> listener = getStatusListener();
339 if (nullptr != listener.get()) {
340 listener->onNewProviderRegistered();
341 }
342
343 return hardware::Return<void>();
344 }
345
dump(int fd,const Vector<String16> & args)346 status_t CameraProviderManager::dump(int fd, const Vector<String16>& args) {
347 std::lock_guard<std::mutex> lock(mInterfaceMutex);
348
349 for (auto& provider : mProviders) {
350 provider->dump(fd, args);
351 }
352 return OK;
353 }
354
findDeviceInfoLocked(const std::string & id,hardware::hidl_version minVersion,hardware::hidl_version maxVersion) const355 CameraProviderManager::ProviderInfo::DeviceInfo* CameraProviderManager::findDeviceInfoLocked(
356 const std::string& id,
357 hardware::hidl_version minVersion, hardware::hidl_version maxVersion) const {
358 for (auto& provider : mProviders) {
359 for (auto& deviceInfo : provider->mDevices) {
360 if (deviceInfo->mId == id &&
361 minVersion <= deviceInfo->mVersion && maxVersion >= deviceInfo->mVersion) {
362 return deviceInfo.get();
363 }
364 }
365 }
366 return nullptr;
367 }
368
getProviderTagIdLocked(const std::string & id,hardware::hidl_version minVersion,hardware::hidl_version maxVersion) const369 metadata_vendor_id_t CameraProviderManager::getProviderTagIdLocked(
370 const std::string& id, hardware::hidl_version minVersion,
371 hardware::hidl_version maxVersion) const {
372 metadata_vendor_id_t ret = CAMERA_METADATA_INVALID_VENDOR_ID;
373
374 std::lock_guard<std::mutex> lock(mInterfaceMutex);
375 for (auto& provider : mProviders) {
376 for (auto& deviceInfo : provider->mDevices) {
377 if (deviceInfo->mId == id &&
378 minVersion <= deviceInfo->mVersion &&
379 maxVersion >= deviceInfo->mVersion) {
380 return provider->mProviderTagid;
381 }
382 }
383 }
384
385 return ret;
386 }
387
addProviderLocked(const std::string & newProvider,bool expected)388 status_t CameraProviderManager::addProviderLocked(const std::string& newProvider, bool expected) {
389 for (const auto& providerInfo : mProviders) {
390 if (providerInfo->mProviderName == newProvider) {
391 ALOGW("%s: Camera provider HAL with name '%s' already registered", __FUNCTION__,
392 newProvider.c_str());
393 return ALREADY_EXISTS;
394 }
395 }
396
397 sp<provider::V2_4::ICameraProvider> interface;
398 interface = mServiceProxy->getService(newProvider);
399
400 if (interface == nullptr) {
401 if (expected) {
402 ALOGE("%s: Camera provider HAL '%s' is not actually available", __FUNCTION__,
403 newProvider.c_str());
404 return BAD_VALUE;
405 } else {
406 return OK;
407 }
408 }
409
410 sp<ProviderInfo> providerInfo =
411 new ProviderInfo(newProvider, interface, this);
412 status_t res = providerInfo->initialize();
413 if (res != OK) {
414 return res;
415 }
416
417 mProviders.push_back(providerInfo);
418
419 return OK;
420 }
421
removeProvider(const std::string & provider)422 status_t CameraProviderManager::removeProvider(const std::string& provider) {
423 std::unique_lock<std::mutex> lock(mInterfaceMutex);
424 std::vector<String8> removedDeviceIds;
425 status_t res = NAME_NOT_FOUND;
426 for (auto it = mProviders.begin(); it != mProviders.end(); it++) {
427 if ((*it)->mProviderName == provider) {
428 removedDeviceIds.reserve((*it)->mDevices.size());
429 for (auto& deviceInfo : (*it)->mDevices) {
430 removedDeviceIds.push_back(String8(deviceInfo->mId.c_str()));
431 }
432 mProviders.erase(it);
433 res = OK;
434 break;
435 }
436 }
437 if (res != OK) {
438 ALOGW("%s: Camera provider HAL with name '%s' is not registered", __FUNCTION__,
439 provider.c_str());
440 } else {
441 // Inform camera service of loss of presence for all the devices from this provider,
442 // without lock held for reentrancy
443 sp<StatusListener> listener = getStatusListener();
444 if (listener != nullptr) {
445 lock.unlock();
446 for (auto& id : removedDeviceIds) {
447 listener->onDeviceStatusChanged(id, CameraDeviceStatus::NOT_PRESENT);
448 }
449 }
450 }
451 return res;
452 }
453
getStatusListener() const454 sp<CameraProviderManager::StatusListener> CameraProviderManager::getStatusListener() const {
455 return mListener.promote();
456 }
457
458 /**** Methods for ProviderInfo ****/
459
460
ProviderInfo(const std::string & providerName,sp<provider::V2_4::ICameraProvider> & interface,CameraProviderManager * manager)461 CameraProviderManager::ProviderInfo::ProviderInfo(
462 const std::string &providerName,
463 sp<provider::V2_4::ICameraProvider>& interface,
464 CameraProviderManager *manager) :
465 mProviderName(providerName),
466 mInterface(interface),
467 mProviderTagid(generateVendorTagId(providerName)),
468 mUniqueDeviceCount(0),
469 mManager(manager) {
470 (void) mManager;
471 }
472
initialize()473 status_t CameraProviderManager::ProviderInfo::initialize() {
474 status_t res = parseProviderName(mProviderName, &mType, &mId);
475 if (res != OK) {
476 ALOGE("%s: Invalid provider name, ignoring", __FUNCTION__);
477 return BAD_VALUE;
478 }
479 ALOGI("Connecting to new camera provider: %s, isRemote? %d",
480 mProviderName.c_str(), mInterface->isRemote());
481 hardware::Return<Status> status = mInterface->setCallback(this);
482 if (!status.isOk()) {
483 ALOGE("%s: Transaction error setting up callbacks with camera provider '%s': %s",
484 __FUNCTION__, mProviderName.c_str(), status.description().c_str());
485 return DEAD_OBJECT;
486 }
487 if (status != Status::OK) {
488 ALOGE("%s: Unable to register callbacks with camera provider '%s'",
489 __FUNCTION__, mProviderName.c_str());
490 return mapToStatusT(status);
491 }
492
493 hardware::Return<bool> linked = mInterface->linkToDeath(this, /*cookie*/ mId);
494 if (!linked.isOk()) {
495 ALOGE("%s: Transaction error in linking to camera provider '%s' death: %s",
496 __FUNCTION__, mProviderName.c_str(), linked.description().c_str());
497 return DEAD_OBJECT;
498 } else if (!linked) {
499 ALOGW("%s: Unable to link to provider '%s' death notifications",
500 __FUNCTION__, mProviderName.c_str());
501 }
502
503 // Get initial list of camera devices, if any
504 std::vector<std::string> devices;
505 hardware::Return<void> ret = mInterface->getCameraIdList([&status, &devices](
506 Status idStatus,
507 const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames) {
508 status = idStatus;
509 if (status == Status::OK) {
510 for (size_t i = 0; i < cameraDeviceNames.size(); i++) {
511 devices.push_back(cameraDeviceNames[i]);
512 }
513 } });
514 if (!ret.isOk()) {
515 ALOGE("%s: Transaction error in getting camera ID list from provider '%s': %s",
516 __FUNCTION__, mProviderName.c_str(), linked.description().c_str());
517 return DEAD_OBJECT;
518 }
519 if (status != Status::OK) {
520 ALOGE("%s: Unable to query for camera devices from provider '%s'",
521 __FUNCTION__, mProviderName.c_str());
522 return mapToStatusT(status);
523 }
524
525 sp<StatusListener> listener = mManager->getStatusListener();
526 for (auto& device : devices) {
527 std::string id;
528 status_t res = addDevice(device,
529 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT, &id);
530 if (res != OK) {
531 ALOGE("%s: Unable to enumerate camera device '%s': %s (%d)",
532 __FUNCTION__, device.c_str(), strerror(-res), res);
533 continue;
534 }
535 }
536
537 for (auto& device : mDevices) {
538 mUniqueCameraIds.insert(device->mId);
539 if (device->isAPI1Compatible()) {
540 mUniqueAPI1CompatibleCameraIds.insert(device->mId);
541 }
542 }
543 mUniqueDeviceCount = mUniqueCameraIds.size();
544
545 ALOGI("Camera provider %s ready with %zu camera devices",
546 mProviderName.c_str(), mDevices.size());
547
548 return OK;
549 }
550
getType() const551 const std::string& CameraProviderManager::ProviderInfo::getType() const {
552 return mType;
553 }
554
addDevice(const std::string & name,CameraDeviceStatus initialStatus,std::string * parsedId)555 status_t CameraProviderManager::ProviderInfo::addDevice(const std::string& name,
556 CameraDeviceStatus initialStatus, /*out*/ std::string* parsedId) {
557
558 ALOGI("Enumerating new camera device: %s", name.c_str());
559
560 uint16_t major, minor;
561 std::string type, id;
562
563 status_t res = parseDeviceName(name, &major, &minor, &type, &id);
564 if (res != OK) {
565 return res;
566 }
567 if (type != mType) {
568 ALOGE("%s: Device type %s does not match provider type %s", __FUNCTION__,
569 type.c_str(), mType.c_str());
570 return BAD_VALUE;
571 }
572 if (mManager->isValidDeviceLocked(id, major)) {
573 ALOGE("%s: Device %s: ID %s is already in use for device major version %d", __FUNCTION__,
574 name.c_str(), id.c_str(), major);
575 return BAD_VALUE;
576 }
577
578 std::unique_ptr<DeviceInfo> deviceInfo;
579 switch (major) {
580 case 1:
581 deviceInfo = initializeDeviceInfo<DeviceInfo1>(name, mProviderTagid,
582 id, minor);
583 break;
584 case 3:
585 deviceInfo = initializeDeviceInfo<DeviceInfo3>(name, mProviderTagid,
586 id, minor);
587 break;
588 default:
589 ALOGE("%s: Device %s: Unknown HIDL device HAL major version %d:", __FUNCTION__,
590 name.c_str(), major);
591 return BAD_VALUE;
592 }
593 if (deviceInfo == nullptr) return BAD_VALUE;
594 deviceInfo->mStatus = initialStatus;
595
596 mDevices.push_back(std::move(deviceInfo));
597
598 if (parsedId != nullptr) {
599 *parsedId = id;
600 }
601 return OK;
602 }
603
dump(int fd,const Vector<String16> &) const604 status_t CameraProviderManager::ProviderInfo::dump(int fd, const Vector<String16>&) const {
605 dprintf(fd, "== Camera Provider HAL %s (v2.4, %s) static info: %zu devices: ==\n",
606 mProviderName.c_str(), mInterface->isRemote() ? "remote" : "passthrough",
607 mDevices.size());
608
609 for (auto& device : mDevices) {
610 dprintf(fd, "== Camera HAL device %s (v%d.%d) static information: ==\n", device->mName.c_str(),
611 device->mVersion.get_major(), device->mVersion.get_minor());
612 dprintf(fd, " Resource cost: %d\n", device->mResourceCost.resourceCost);
613 if (device->mResourceCost.conflictingDevices.size() == 0) {
614 dprintf(fd, " Conflicting devices: None\n");
615 } else {
616 dprintf(fd, " Conflicting devices:\n");
617 for (size_t i = 0; i < device->mResourceCost.conflictingDevices.size(); i++) {
618 dprintf(fd, " %s\n",
619 device->mResourceCost.conflictingDevices[i].c_str());
620 }
621 }
622 dprintf(fd, " API1 info:\n");
623 dprintf(fd, " Has a flash unit: %s\n",
624 device->hasFlashUnit() ? "true" : "false");
625 hardware::CameraInfo info;
626 status_t res = device->getCameraInfo(&info);
627 if (res != OK) {
628 dprintf(fd, " <Error reading camera info: %s (%d)>\n",
629 strerror(-res), res);
630 } else {
631 dprintf(fd, " Facing: %s\n",
632 info.facing == hardware::CAMERA_FACING_BACK ? "Back" : "Front");
633 dprintf(fd, " Orientation: %d\n", info.orientation);
634 }
635 CameraMetadata info2;
636 res = device->getCameraCharacteristics(&info2);
637 if (res == INVALID_OPERATION) {
638 dprintf(fd, " API2 not directly supported\n");
639 } else if (res != OK) {
640 dprintf(fd, " <Error reading camera characteristics: %s (%d)>\n",
641 strerror(-res), res);
642 } else {
643 dprintf(fd, " API2 camera characteristics:\n");
644 info2.dump(fd, /*verbosity*/ 2, /*indentation*/ 4);
645 }
646 }
647 return OK;
648 }
649
cameraDeviceStatusChange(const hardware::hidl_string & cameraDeviceName,CameraDeviceStatus newStatus)650 hardware::Return<void> CameraProviderManager::ProviderInfo::cameraDeviceStatusChange(
651 const hardware::hidl_string& cameraDeviceName,
652 CameraDeviceStatus newStatus) {
653 sp<StatusListener> listener;
654 std::string id;
655 {
656 std::lock_guard<std::mutex> lock(mLock);
657 bool known = false;
658 for (auto& deviceInfo : mDevices) {
659 if (deviceInfo->mName == cameraDeviceName) {
660 ALOGI("Camera device %s status is now %s, was %s", cameraDeviceName.c_str(),
661 deviceStatusToString(newStatus), deviceStatusToString(deviceInfo->mStatus));
662 deviceInfo->mStatus = newStatus;
663 // TODO: Handle device removal (NOT_PRESENT)
664 id = deviceInfo->mId;
665 known = true;
666 break;
667 }
668 }
669 // Previously unseen device; status must not be NOT_PRESENT
670 if (!known) {
671 if (newStatus == CameraDeviceStatus::NOT_PRESENT) {
672 ALOGW("Camera provider %s says an unknown camera device %s is not present. Curious.",
673 mProviderName.c_str(), cameraDeviceName.c_str());
674 return hardware::Void();
675 }
676 addDevice(cameraDeviceName, newStatus, &id);
677 }
678 listener = mManager->getStatusListener();
679 }
680 // Call without lock held to allow reentrancy into provider manager
681 if (listener != nullptr) {
682 listener->onDeviceStatusChanged(String8(id.c_str()), newStatus);
683 }
684 return hardware::Void();
685 }
686
torchModeStatusChange(const hardware::hidl_string & cameraDeviceName,TorchModeStatus newStatus)687 hardware::Return<void> CameraProviderManager::ProviderInfo::torchModeStatusChange(
688 const hardware::hidl_string& cameraDeviceName,
689 TorchModeStatus newStatus) {
690 sp<StatusListener> listener;
691 std::string id;
692 {
693 std::lock_guard<std::mutex> lock(mManager->mStatusListenerMutex);
694 bool known = false;
695 for (auto& deviceInfo : mDevices) {
696 if (deviceInfo->mName == cameraDeviceName) {
697 ALOGI("Camera device %s torch status is now %s", cameraDeviceName.c_str(),
698 torchStatusToString(newStatus));
699 id = deviceInfo->mId;
700 known = true;
701 break;
702 }
703 }
704 if (!known) {
705 ALOGW("Camera provider %s says an unknown camera %s now has torch status %d. Curious.",
706 mProviderName.c_str(), cameraDeviceName.c_str(), newStatus);
707 return hardware::Void();
708 }
709 listener = mManager->getStatusListener();
710 }
711 // Call without lock held to allow reentrancy into provider manager
712 if (listener != nullptr) {
713 listener->onTorchStatusChanged(String8(id.c_str()), newStatus);
714 }
715 return hardware::Void();
716 }
717
serviceDied(uint64_t cookie,const wp<hidl::base::V1_0::IBase> & who)718 void CameraProviderManager::ProviderInfo::serviceDied(uint64_t cookie,
719 const wp<hidl::base::V1_0::IBase>& who) {
720 (void) who;
721 ALOGI("Camera provider '%s' has died; removing it", mProviderName.c_str());
722 if (cookie != mId) {
723 ALOGW("%s: Unexpected serviceDied cookie %" PRIu64 ", expected %" PRIu32,
724 __FUNCTION__, cookie, mId);
725 }
726 mManager->removeProvider(mProviderName);
727 }
728
729 template<class DeviceInfoT>
730 std::unique_ptr<CameraProviderManager::ProviderInfo::DeviceInfo>
initializeDeviceInfo(const std::string & name,const metadata_vendor_id_t tagId,const std::string & id,uint16_t minorVersion) const731 CameraProviderManager::ProviderInfo::initializeDeviceInfo(
732 const std::string &name, const metadata_vendor_id_t tagId,
733 const std::string &id, uint16_t minorVersion) const {
734 Status status;
735
736 auto cameraInterface =
737 getDeviceInterface<typename DeviceInfoT::InterfaceT>(name);
738 if (cameraInterface == nullptr) return nullptr;
739
740 CameraResourceCost resourceCost;
741 cameraInterface->getResourceCost([&status, &resourceCost](
742 Status s, CameraResourceCost cost) {
743 status = s;
744 resourceCost = cost;
745 });
746 if (status != Status::OK) {
747 ALOGE("%s: Unable to obtain resource costs for camera device %s: %s", __FUNCTION__,
748 name.c_str(), statusToString(status));
749 return nullptr;
750 }
751 return std::unique_ptr<DeviceInfo>(
752 new DeviceInfoT(name, tagId, id, minorVersion, resourceCost,
753 cameraInterface));
754 }
755
756 template<class InterfaceT>
757 sp<InterfaceT>
getDeviceInterface(const std::string & name) const758 CameraProviderManager::ProviderInfo::getDeviceInterface(const std::string &name) const {
759 ALOGE("%s: Device %s: Unknown HIDL device HAL major version %d:", __FUNCTION__,
760 name.c_str(), InterfaceT::version.get_major());
761 return nullptr;
762 }
763
764 template<>
765 sp<device::V1_0::ICameraDevice>
getDeviceInterface(const std::string & name) const766 CameraProviderManager::ProviderInfo::getDeviceInterface
767 <device::V1_0::ICameraDevice>(const std::string &name) const {
768 Status status;
769 sp<device::V1_0::ICameraDevice> cameraInterface;
770 hardware::Return<void> ret;
771 ret = mInterface->getCameraDeviceInterface_V1_x(name, [&status, &cameraInterface](
772 Status s, sp<device::V1_0::ICameraDevice> interface) {
773 status = s;
774 cameraInterface = interface;
775 });
776 if (!ret.isOk()) {
777 ALOGE("%s: Transaction error trying to obtain interface for camera device %s: %s",
778 __FUNCTION__, name.c_str(), ret.description().c_str());
779 return nullptr;
780 }
781 if (status != Status::OK) {
782 ALOGE("%s: Unable to obtain interface for camera device %s: %s", __FUNCTION__,
783 name.c_str(), statusToString(status));
784 return nullptr;
785 }
786 return cameraInterface;
787 }
788
789 template<>
790 sp<device::V3_2::ICameraDevice>
getDeviceInterface(const std::string & name) const791 CameraProviderManager::ProviderInfo::getDeviceInterface
792 <device::V3_2::ICameraDevice>(const std::string &name) const {
793 Status status;
794 sp<device::V3_2::ICameraDevice> cameraInterface;
795 hardware::Return<void> ret;
796 ret = mInterface->getCameraDeviceInterface_V3_x(name, [&status, &cameraInterface](
797 Status s, sp<device::V3_2::ICameraDevice> interface) {
798 status = s;
799 cameraInterface = interface;
800 });
801 if (!ret.isOk()) {
802 ALOGE("%s: Transaction error trying to obtain interface for camera device %s: %s",
803 __FUNCTION__, name.c_str(), ret.description().c_str());
804 return nullptr;
805 }
806 if (status != Status::OK) {
807 ALOGE("%s: Unable to obtain interface for camera device %s: %s", __FUNCTION__,
808 name.c_str(), statusToString(status));
809 return nullptr;
810 }
811 return cameraInterface;
812 }
813
~DeviceInfo()814 CameraProviderManager::ProviderInfo::DeviceInfo::~DeviceInfo() {}
815
816 template<class InterfaceT>
setTorchMode(InterfaceT & interface,bool enabled)817 status_t CameraProviderManager::ProviderInfo::DeviceInfo::setTorchMode(InterfaceT& interface,
818 bool enabled) {
819 Status s = interface->setTorchMode(enabled ? TorchMode::ON : TorchMode::OFF);
820 return mapToStatusT(s);
821 }
822
DeviceInfo1(const std::string & name,const metadata_vendor_id_t tagId,const std::string & id,uint16_t minorVersion,const CameraResourceCost & resourceCost,sp<InterfaceT> interface)823 CameraProviderManager::ProviderInfo::DeviceInfo1::DeviceInfo1(const std::string& name,
824 const metadata_vendor_id_t tagId, const std::string &id,
825 uint16_t minorVersion,
826 const CameraResourceCost& resourceCost,
827 sp<InterfaceT> interface) :
828 DeviceInfo(name, tagId, id, hardware::hidl_version{1, minorVersion},
829 resourceCost),
830 mInterface(interface) {
831 // Get default parameters and initialize flash unit availability
832 // Requires powering on the camera device
833 hardware::Return<Status> status = mInterface->open(nullptr);
834 if (!status.isOk()) {
835 ALOGE("%s: Transaction error opening camera device %s to check for a flash unit: %s",
836 __FUNCTION__, mId.c_str(), status.description().c_str());
837 return;
838 }
839 if (status != Status::OK) {
840 ALOGE("%s: Unable to open camera device %s to check for a flash unit: %s", __FUNCTION__,
841 mId.c_str(), CameraProviderManager::statusToString(status));
842 return;
843 }
844 hardware::Return<void> ret;
__anonb0e2bf370902(const hardware::hidl_string& parms) 845 ret = mInterface->getParameters([this](const hardware::hidl_string& parms) {
846 mDefaultParameters.unflatten(String8(parms.c_str()));
847 });
848 if (!ret.isOk()) {
849 ALOGE("%s: Transaction error reading camera device %s params to check for a flash unit: %s",
850 __FUNCTION__, mId.c_str(), status.description().c_str());
851 return;
852 }
853 const char *flashMode =
854 mDefaultParameters.get(CameraParameters::KEY_SUPPORTED_FLASH_MODES);
855 if (flashMode && strstr(flashMode, CameraParameters::FLASH_MODE_TORCH)) {
856 mHasFlashUnit = true;
857 }
858
859 ret = mInterface->close();
860 if (!ret.isOk()) {
861 ALOGE("%s: Transaction error closing camera device %s after check for a flash unit: %s",
862 __FUNCTION__, mId.c_str(), status.description().c_str());
863 }
864 }
865
~DeviceInfo1()866 CameraProviderManager::ProviderInfo::DeviceInfo1::~DeviceInfo1() {}
867
setTorchMode(bool enabled)868 status_t CameraProviderManager::ProviderInfo::DeviceInfo1::setTorchMode(bool enabled) {
869 return DeviceInfo::setTorchMode(mInterface, enabled);
870 }
871
getCameraInfo(hardware::CameraInfo * info) const872 status_t CameraProviderManager::ProviderInfo::DeviceInfo1::getCameraInfo(
873 hardware::CameraInfo *info) const {
874 if (info == nullptr) return BAD_VALUE;
875
876 Status status;
877 device::V1_0::CameraInfo cInfo;
878 hardware::Return<void> ret;
879 ret = mInterface->getCameraInfo([&status, &cInfo](Status s, device::V1_0::CameraInfo camInfo) {
880 status = s;
881 cInfo = camInfo;
882 });
883 if (!ret.isOk()) {
884 ALOGE("%s: Transaction error reading camera info from device %s: %s",
885 __FUNCTION__, mId.c_str(), ret.description().c_str());
886 return DEAD_OBJECT;
887 }
888 if (status != Status::OK) {
889 return mapToStatusT(status);
890 }
891
892 switch(cInfo.facing) {
893 case device::V1_0::CameraFacing::BACK:
894 info->facing = hardware::CAMERA_FACING_BACK;
895 break;
896 case device::V1_0::CameraFacing::EXTERNAL:
897 // Map external to front for legacy API
898 case device::V1_0::CameraFacing::FRONT:
899 info->facing = hardware::CAMERA_FACING_FRONT;
900 break;
901 default:
902 ALOGW("%s: Device %s: Unknown camera facing: %d",
903 __FUNCTION__, mId.c_str(), cInfo.facing);
904 info->facing = hardware::CAMERA_FACING_BACK;
905 }
906 info->orientation = cInfo.orientation;
907
908 return OK;
909 }
910
DeviceInfo3(const std::string & name,const metadata_vendor_id_t tagId,const std::string & id,uint16_t minorVersion,const CameraResourceCost & resourceCost,sp<InterfaceT> interface)911 CameraProviderManager::ProviderInfo::DeviceInfo3::DeviceInfo3(const std::string& name,
912 const metadata_vendor_id_t tagId, const std::string &id,
913 uint16_t minorVersion,
914 const CameraResourceCost& resourceCost,
915 sp<InterfaceT> interface) :
916 DeviceInfo(name, tagId, id, hardware::hidl_version{3, minorVersion},
917 resourceCost),
918 mInterface(interface) {
919 // Get camera characteristics and initialize flash unit availability
920 Status status;
921 hardware::Return<void> ret;
922 ret = mInterface->getCameraCharacteristics([&status, this](Status s,
__anonb0e2bf370b02(Status s, device::V3_2::CameraMetadata metadata) 923 device::V3_2::CameraMetadata metadata) {
924 status = s;
925 if (s == Status::OK) {
926 camera_metadata_t *buffer =
927 reinterpret_cast<camera_metadata_t*>(metadata.data());
928 size_t expectedSize = metadata.size();
929 int res = validate_camera_metadata_structure(buffer, &expectedSize);
930 if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
931 set_camera_metadata_vendor_id(buffer, mProviderTagid);
932 mCameraCharacteristics = buffer;
933 } else {
934 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
935 status = Status::INTERNAL_ERROR;
936 }
937 }
938 });
939 if (!ret.isOk()) {
940 ALOGE("%s: Transaction error getting camera characteristics for device %s"
941 " to check for a flash unit: %s", __FUNCTION__, mId.c_str(),
942 ret.description().c_str());
943 return;
944 }
945 if (status != Status::OK) {
946 ALOGE("%s: Unable to get camera characteristics for device %s: %s (%d)",
947 __FUNCTION__, mId.c_str(), CameraProviderManager::statusToString(status), status);
948 return;
949 }
950 camera_metadata_entry flashAvailable =
951 mCameraCharacteristics.find(ANDROID_FLASH_INFO_AVAILABLE);
952 if (flashAvailable.count == 1 &&
953 flashAvailable.data.u8[0] == ANDROID_FLASH_INFO_AVAILABLE_TRUE) {
954 mHasFlashUnit = true;
955 } else {
956 mHasFlashUnit = false;
957 }
958 }
959
~DeviceInfo3()960 CameraProviderManager::ProviderInfo::DeviceInfo3::~DeviceInfo3() {}
961
setTorchMode(bool enabled)962 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::setTorchMode(bool enabled) {
963 return DeviceInfo::setTorchMode(mInterface, enabled);
964 }
965
getCameraInfo(hardware::CameraInfo * info) const966 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getCameraInfo(
967 hardware::CameraInfo *info) const {
968 if (info == nullptr) return BAD_VALUE;
969
970 camera_metadata_ro_entry facing =
971 mCameraCharacteristics.find(ANDROID_LENS_FACING);
972 if (facing.count == 1) {
973 switch (facing.data.u8[0]) {
974 case ANDROID_LENS_FACING_BACK:
975 info->facing = hardware::CAMERA_FACING_BACK;
976 break;
977 case ANDROID_LENS_FACING_EXTERNAL:
978 // Map external to front for legacy API
979 case ANDROID_LENS_FACING_FRONT:
980 info->facing = hardware::CAMERA_FACING_FRONT;
981 break;
982 }
983 } else {
984 ALOGE("%s: Unable to find android.lens.facing static metadata", __FUNCTION__);
985 return NAME_NOT_FOUND;
986 }
987
988 camera_metadata_ro_entry orientation =
989 mCameraCharacteristics.find(ANDROID_SENSOR_ORIENTATION);
990 if (orientation.count == 1) {
991 info->orientation = orientation.data.i32[0];
992 } else {
993 ALOGE("%s: Unable to find android.sensor.orientation static metadata", __FUNCTION__);
994 return NAME_NOT_FOUND;
995 }
996
997 return OK;
998 }
isAPI1Compatible() const999 bool CameraProviderManager::ProviderInfo::DeviceInfo3::isAPI1Compatible() const {
1000 bool isBackwardCompatible = false;
1001 camera_metadata_ro_entry_t caps = mCameraCharacteristics.find(
1002 ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
1003 for (size_t i = 0; i < caps.count; i++) {
1004 if (caps.data.u8[i] ==
1005 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
1006 isBackwardCompatible = true;
1007 break;
1008 }
1009 }
1010
1011 return isBackwardCompatible;
1012 }
1013
getCameraCharacteristics(CameraMetadata * characteristics) const1014 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getCameraCharacteristics(
1015 CameraMetadata *characteristics) const {
1016 if (characteristics == nullptr) return BAD_VALUE;
1017
1018 *characteristics = mCameraCharacteristics;
1019 return OK;
1020 }
1021
parseProviderName(const std::string & name,std::string * type,uint32_t * id)1022 status_t CameraProviderManager::ProviderInfo::parseProviderName(const std::string& name,
1023 std::string *type, uint32_t *id) {
1024 // Format must be "<type>/<id>"
1025 #define ERROR_MSG_PREFIX "%s: Invalid provider name '%s'. " \
1026 "Should match '<type>/<id>' - "
1027
1028 if (!type || !id) return INVALID_OPERATION;
1029
1030 std::string::size_type slashIdx = name.find('/');
1031 if (slashIdx == std::string::npos || slashIdx == name.size() - 1) {
1032 ALOGE(ERROR_MSG_PREFIX
1033 "does not have / separator between type and id",
1034 __FUNCTION__, name.c_str());
1035 return BAD_VALUE;
1036 }
1037
1038 std::string typeVal = name.substr(0, slashIdx);
1039
1040 char *endPtr;
1041 errno = 0;
1042 long idVal = strtol(name.c_str() + slashIdx + 1, &endPtr, 10);
1043 if (errno != 0) {
1044 ALOGE(ERROR_MSG_PREFIX
1045 "cannot parse provider id as an integer: %s (%d)",
1046 __FUNCTION__, name.c_str(), strerror(errno), errno);
1047 return BAD_VALUE;
1048 }
1049 if (endPtr != name.c_str() + name.size()) {
1050 ALOGE(ERROR_MSG_PREFIX
1051 "provider id has unexpected length",
1052 __FUNCTION__, name.c_str());
1053 return BAD_VALUE;
1054 }
1055 if (idVal < 0) {
1056 ALOGE(ERROR_MSG_PREFIX
1057 "id is negative: %ld",
1058 __FUNCTION__, name.c_str(), idVal);
1059 return BAD_VALUE;
1060 }
1061
1062 #undef ERROR_MSG_PREFIX
1063
1064 *type = typeVal;
1065 *id = static_cast<uint32_t>(idVal);
1066
1067 return OK;
1068 }
1069
generateVendorTagId(const std::string & name)1070 metadata_vendor_id_t CameraProviderManager::ProviderInfo::generateVendorTagId(
1071 const std::string &name) {
1072 metadata_vendor_id_t ret = std::hash<std::string> {} (name);
1073 // CAMERA_METADATA_INVALID_VENDOR_ID is not a valid hash value
1074 if (CAMERA_METADATA_INVALID_VENDOR_ID == ret) {
1075 ret = 0;
1076 }
1077
1078 return ret;
1079 }
1080
parseDeviceName(const std::string & name,uint16_t * major,uint16_t * minor,std::string * type,std::string * id)1081 status_t CameraProviderManager::ProviderInfo::parseDeviceName(const std::string& name,
1082 uint16_t *major, uint16_t *minor, std::string *type, std::string *id) {
1083
1084 // Format must be "device@<major>.<minor>/<type>/<id>"
1085
1086 #define ERROR_MSG_PREFIX "%s: Invalid device name '%s'. " \
1087 "Should match 'device@<major>.<minor>/<type>/<id>' - "
1088
1089 if (!major || !minor || !type || !id) return INVALID_OPERATION;
1090
1091 // Verify starting prefix
1092 const char expectedPrefix[] = "device@";
1093
1094 if (name.find(expectedPrefix) != 0) {
1095 ALOGE(ERROR_MSG_PREFIX
1096 "does not start with '%s'",
1097 __FUNCTION__, name.c_str(), expectedPrefix);
1098 return BAD_VALUE;
1099 }
1100
1101 // Extract major/minor versions
1102 constexpr std::string::size_type atIdx = sizeof(expectedPrefix) - 2;
1103 std::string::size_type dotIdx = name.find('.', atIdx);
1104 if (dotIdx == std::string::npos) {
1105 ALOGE(ERROR_MSG_PREFIX
1106 "does not have @<major>. version section",
1107 __FUNCTION__, name.c_str());
1108 return BAD_VALUE;
1109 }
1110 std::string::size_type typeSlashIdx = name.find('/', dotIdx);
1111 if (typeSlashIdx == std::string::npos) {
1112 ALOGE(ERROR_MSG_PREFIX
1113 "does not have .<minor>/ version section",
1114 __FUNCTION__, name.c_str());
1115 return BAD_VALUE;
1116 }
1117
1118 char *endPtr;
1119 errno = 0;
1120 long majorVal = strtol(name.c_str() + atIdx + 1, &endPtr, 10);
1121 if (errno != 0) {
1122 ALOGE(ERROR_MSG_PREFIX
1123 "cannot parse major version: %s (%d)",
1124 __FUNCTION__, name.c_str(), strerror(errno), errno);
1125 return BAD_VALUE;
1126 }
1127 if (endPtr != name.c_str() + dotIdx) {
1128 ALOGE(ERROR_MSG_PREFIX
1129 "major version has unexpected length",
1130 __FUNCTION__, name.c_str());
1131 return BAD_VALUE;
1132 }
1133 long minorVal = strtol(name.c_str() + dotIdx + 1, &endPtr, 10);
1134 if (errno != 0) {
1135 ALOGE(ERROR_MSG_PREFIX
1136 "cannot parse minor version: %s (%d)",
1137 __FUNCTION__, name.c_str(), strerror(errno), errno);
1138 return BAD_VALUE;
1139 }
1140 if (endPtr != name.c_str() + typeSlashIdx) {
1141 ALOGE(ERROR_MSG_PREFIX
1142 "minor version has unexpected length",
1143 __FUNCTION__, name.c_str());
1144 return BAD_VALUE;
1145 }
1146 if (majorVal < 0 || majorVal > UINT16_MAX || minorVal < 0 || minorVal > UINT16_MAX) {
1147 ALOGE(ERROR_MSG_PREFIX
1148 "major/minor version is out of range of uint16_t: %ld.%ld",
1149 __FUNCTION__, name.c_str(), majorVal, minorVal);
1150 return BAD_VALUE;
1151 }
1152
1153 // Extract type and id
1154
1155 std::string::size_type instanceSlashIdx = name.find('/', typeSlashIdx + 1);
1156 if (instanceSlashIdx == std::string::npos) {
1157 ALOGE(ERROR_MSG_PREFIX
1158 "does not have /<type>/ component",
1159 __FUNCTION__, name.c_str());
1160 return BAD_VALUE;
1161 }
1162 std::string typeVal = name.substr(typeSlashIdx + 1, instanceSlashIdx - typeSlashIdx - 1);
1163
1164 if (instanceSlashIdx == name.size() - 1) {
1165 ALOGE(ERROR_MSG_PREFIX
1166 "does not have an /<id> component",
1167 __FUNCTION__, name.c_str());
1168 return BAD_VALUE;
1169 }
1170 std::string idVal = name.substr(instanceSlashIdx + 1);
1171
1172 #undef ERROR_MSG_PREFIX
1173
1174 *major = static_cast<uint16_t>(majorVal);
1175 *minor = static_cast<uint16_t>(minorVal);
1176 *type = typeVal;
1177 *id = idVal;
1178
1179 return OK;
1180 }
1181
1182
1183
~ProviderInfo()1184 CameraProviderManager::ProviderInfo::~ProviderInfo() {
1185 // Destruction of ProviderInfo is only supposed to happen when the respective
1186 // CameraProvider interface dies, so do not unregister callbacks.
1187
1188 }
1189
mapToStatusT(const Status & s)1190 status_t CameraProviderManager::mapToStatusT(const Status& s) {
1191 switch(s) {
1192 case Status::OK:
1193 return OK;
1194 case Status::ILLEGAL_ARGUMENT:
1195 return BAD_VALUE;
1196 case Status::CAMERA_IN_USE:
1197 return -EBUSY;
1198 case Status::MAX_CAMERAS_IN_USE:
1199 return -EUSERS;
1200 case Status::METHOD_NOT_SUPPORTED:
1201 return UNKNOWN_TRANSACTION;
1202 case Status::OPERATION_NOT_SUPPORTED:
1203 return INVALID_OPERATION;
1204 case Status::CAMERA_DISCONNECTED:
1205 return DEAD_OBJECT;
1206 case Status::INTERNAL_ERROR:
1207 return INVALID_OPERATION;
1208 }
1209 ALOGW("Unexpected HAL status code %d", s);
1210 return INVALID_OPERATION;
1211 }
1212
statusToString(const Status & s)1213 const char* CameraProviderManager::statusToString(const Status& s) {
1214 switch(s) {
1215 case Status::OK:
1216 return "OK";
1217 case Status::ILLEGAL_ARGUMENT:
1218 return "ILLEGAL_ARGUMENT";
1219 case Status::CAMERA_IN_USE:
1220 return "CAMERA_IN_USE";
1221 case Status::MAX_CAMERAS_IN_USE:
1222 return "MAX_CAMERAS_IN_USE";
1223 case Status::METHOD_NOT_SUPPORTED:
1224 return "METHOD_NOT_SUPPORTED";
1225 case Status::OPERATION_NOT_SUPPORTED:
1226 return "OPERATION_NOT_SUPPORTED";
1227 case Status::CAMERA_DISCONNECTED:
1228 return "CAMERA_DISCONNECTED";
1229 case Status::INTERNAL_ERROR:
1230 return "INTERNAL_ERROR";
1231 }
1232 ALOGW("Unexpected HAL status code %d", s);
1233 return "UNKNOWN_ERROR";
1234 }
1235
deviceStatusToString(const CameraDeviceStatus & s)1236 const char* CameraProviderManager::deviceStatusToString(const CameraDeviceStatus& s) {
1237 switch(s) {
1238 case CameraDeviceStatus::NOT_PRESENT:
1239 return "NOT_PRESENT";
1240 case CameraDeviceStatus::PRESENT:
1241 return "PRESENT";
1242 case CameraDeviceStatus::ENUMERATING:
1243 return "ENUMERATING";
1244 }
1245 ALOGW("Unexpected HAL device status code %d", s);
1246 return "UNKNOWN_STATUS";
1247 }
1248
torchStatusToString(const TorchModeStatus & s)1249 const char* CameraProviderManager::torchStatusToString(const TorchModeStatus& s) {
1250 switch(s) {
1251 case TorchModeStatus::NOT_AVAILABLE:
1252 return "NOT_AVAILABLE";
1253 case TorchModeStatus::AVAILABLE_OFF:
1254 return "AVAILABLE_OFF";
1255 case TorchModeStatus::AVAILABLE_ON:
1256 return "AVAILABLE_ON";
1257 }
1258 ALOGW("Unexpected HAL torch mode status code %d", s);
1259 return "UNKNOWN_STATUS";
1260 }
1261
1262
createDescriptorFromHidl(const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection> & vts,sp<VendorTagDescriptor> & descriptor)1263 status_t HidlVendorTagDescriptor::createDescriptorFromHidl(
1264 const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts,
1265 /*out*/
1266 sp<VendorTagDescriptor>& descriptor) {
1267
1268 int tagCount = 0;
1269
1270 for (size_t s = 0; s < vts.size(); s++) {
1271 tagCount += vts[s].tags.size();
1272 }
1273
1274 if (tagCount < 0 || tagCount > INT32_MAX) {
1275 ALOGE("%s: tag count %d from vendor tag sections is invalid.", __FUNCTION__, tagCount);
1276 return BAD_VALUE;
1277 }
1278
1279 Vector<uint32_t> tagArray;
1280 LOG_ALWAYS_FATAL_IF(tagArray.resize(tagCount) != tagCount,
1281 "%s: too many (%u) vendor tags defined.", __FUNCTION__, tagCount);
1282
1283
1284 sp<HidlVendorTagDescriptor> desc = new HidlVendorTagDescriptor();
1285 desc->mTagCount = tagCount;
1286
1287 SortedVector<String8> sections;
1288 KeyedVector<uint32_t, String8> tagToSectionMap;
1289
1290 int idx = 0;
1291 for (size_t s = 0; s < vts.size(); s++) {
1292 const hardware::camera::common::V1_0::VendorTagSection& section = vts[s];
1293 const char *sectionName = section.sectionName.c_str();
1294 if (sectionName == NULL) {
1295 ALOGE("%s: no section name defined for vendor tag section %zu.", __FUNCTION__, s);
1296 return BAD_VALUE;
1297 }
1298 String8 sectionString(sectionName);
1299 sections.add(sectionString);
1300
1301 for (size_t j = 0; j < section.tags.size(); j++) {
1302 uint32_t tag = section.tags[j].tagId;
1303 if (tag < CAMERA_METADATA_VENDOR_TAG_BOUNDARY) {
1304 ALOGE("%s: vendor tag %d not in vendor tag section.", __FUNCTION__, tag);
1305 return BAD_VALUE;
1306 }
1307
1308 tagArray.editItemAt(idx++) = section.tags[j].tagId;
1309
1310 const char *tagName = section.tags[j].tagName.c_str();
1311 if (tagName == NULL) {
1312 ALOGE("%s: no tag name defined for vendor tag %d.", __FUNCTION__, tag);
1313 return BAD_VALUE;
1314 }
1315 desc->mTagToNameMap.add(tag, String8(tagName));
1316 tagToSectionMap.add(tag, sectionString);
1317
1318 int tagType = (int) section.tags[j].tagType;
1319 if (tagType < 0 || tagType >= NUM_TYPES) {
1320 ALOGE("%s: tag type %d from vendor ops does not exist.", __FUNCTION__, tagType);
1321 return BAD_VALUE;
1322 }
1323 desc->mTagToTypeMap.add(tag, tagType);
1324 }
1325 }
1326
1327 desc->mSections = sections;
1328
1329 for (size_t i = 0; i < tagArray.size(); ++i) {
1330 uint32_t tag = tagArray[i];
1331 String8 sectionString = tagToSectionMap.valueFor(tag);
1332
1333 // Set up tag to section index map
1334 ssize_t index = sections.indexOf(sectionString);
1335 LOG_ALWAYS_FATAL_IF(index < 0, "index %zd must be non-negative", index);
1336 desc->mTagToSectionMap.add(tag, static_cast<uint32_t>(index));
1337
1338 // Set up reverse mapping
1339 ssize_t reverseIndex = -1;
1340 if ((reverseIndex = desc->mReverseMapping.indexOfKey(sectionString)) < 0) {
1341 KeyedVector<String8, uint32_t>* nameMapper = new KeyedVector<String8, uint32_t>();
1342 reverseIndex = desc->mReverseMapping.add(sectionString, nameMapper);
1343 }
1344 desc->mReverseMapping[reverseIndex]->add(desc->mTagToNameMap.valueFor(tag), tag);
1345 }
1346
1347 descriptor = desc;
1348 return OK;
1349 }
1350
1351
1352 } // namespace android
1353