blob: 3a113def250c37067774ea36c5ddb68ca3606198 [file] [log] [blame]
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -08001/*
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 "CamProvider@2.4-impl"
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -070018//#define LOG_NDEBUG 0
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080019#include <android/log.h>
20
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080021#include "CameraProvider.h"
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080022#include "CameraDevice_1_0.h"
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -070023#include "CameraDevice_3_3.h"
24#include <cutils/properties.h>
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080025#include <string.h>
26#include <utils/Trace.h>
27
28
29namespace android {
30namespace hardware {
31namespace camera {
32namespace provider {
33namespace V2_4 {
34namespace implementation {
35
36namespace {
37const char *kLegacyProviderName = "legacy/0";
38// "device@<version>/legacy/<id>"
39const std::regex kDeviceNameRE("device@([0-9]+\\.[0-9]+)/legacy/(.+)");
40const char *kHAL3_2 = "3.2";
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -070041const char *kHAL3_3 = "3.3";
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080042const char *kHAL1_0 = "1.0";
43const int kMaxCameraDeviceNameLen = 128;
44const int kMaxCameraIdLen = 16;
45
Andreas Gampe0b171f12017-04-04 20:02:25 -070046bool matchDeviceName(const hidl_string& deviceName, std::string* deviceVersion,
47 std::string* cameraId) {
48 std::string deviceNameStd(deviceName.c_str());
49 std::smatch sm;
50 if (std::regex_match(deviceNameStd, sm, kDeviceNameRE)) {
51 if (deviceVersion != nullptr) {
52 *deviceVersion = sm[1];
53 }
54 if (cameraId != nullptr) {
55 *cameraId = sm[2];
56 }
57 return true;
58 }
59 return false;
60}
61
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080062} // anonymous namespace
63
64using ::android::hardware::camera::common::V1_0::CameraMetadataType;
65using ::android::hardware::camera::common::V1_0::Status;
66
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +010067void CameraProvider::addDeviceNames(int camera_id, CameraDeviceStatus status, bool cam_new)
68{
69 char cameraId[kMaxCameraIdLen];
70 snprintf(cameraId, sizeof(cameraId), "%d", camera_id);
71 std::string cameraIdStr(cameraId);
72
73 mCameraIds.add(cameraIdStr);
74
75 // initialize mCameraDeviceNames and mOpenLegacySupported
76 mOpenLegacySupported[cameraIdStr] = false;
77 int deviceVersion = mModule->getDeviceVersion(camera_id);
78 auto deviceNamePair = std::make_pair(cameraIdStr,
79 getHidlDeviceName(cameraIdStr, deviceVersion));
80 mCameraDeviceNames.add(deviceNamePair);
81 if (cam_new) {
82 mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, status);
83 }
84 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
85 mModule->isOpenLegacyDefined()) {
86 // try open_legacy to see if it actually works
87 struct hw_device_t* halDev = nullptr;
88 int ret = mModule->openLegacy(cameraId, CAMERA_DEVICE_API_VERSION_1_0, &halDev);
89 if (ret == 0) {
90 mOpenLegacySupported[cameraIdStr] = true;
91 halDev->close(halDev);
92 deviceNamePair = std::make_pair(cameraIdStr,
93 getHidlDeviceName(cameraIdStr, CAMERA_DEVICE_API_VERSION_1_0));
94 mCameraDeviceNames.add(deviceNamePair);
95 if (cam_new) {
96 mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, status);
97 }
98 } else if (ret == -EBUSY || ret == -EUSERS) {
99 // Looks like this provider instance is not initialized during
100 // system startup and there are other camera users already.
101 // Not a good sign but not fatal.
102 ALOGW("%s: open_legacy try failed!", __FUNCTION__);
103 }
104 }
105}
106
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800107/**
108 * static callback forwarding methods from HAL to instance
109 */
110void CameraProvider::sCameraDeviceStatusChange(
111 const struct camera_module_callbacks* callbacks,
112 int camera_id,
113 int new_status) {
Yin-Chia Yeh6dc9b532017-02-09 18:43:35 -0800114 CameraProvider* cp = const_cast<CameraProvider*>(
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800115 static_cast<const CameraProvider*>(callbacks));
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100116 bool found = false;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800117
118 if (cp == nullptr) {
119 ALOGE("%s: callback ops is null", __FUNCTION__);
120 return;
121 }
122
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800123 Mutex::Autolock _l(cp->mCbLock);
124 char cameraId[kMaxCameraIdLen];
125 snprintf(cameraId, sizeof(cameraId), "%d", camera_id);
126 std::string cameraIdStr(cameraId);
127 cp->mCameraStatusMap[cameraIdStr] = (camera_device_status_t) new_status;
128 if (cp->mCallbacks != nullptr) {
129 CameraDeviceStatus status = (CameraDeviceStatus) new_status;
130 for (auto const& deviceNamePair : cp->mCameraDeviceNames) {
131 if (cameraIdStr.compare(deviceNamePair.first) == 0) {
132 cp->mCallbacks->cameraDeviceStatusChange(
133 deviceNamePair.second, status);
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100134 found = true;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800135 }
136 }
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100137
138 if (!found) {
139 cp->addDeviceNames(camera_id, status, true);
140 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800141 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800142}
143
144void CameraProvider::sTorchModeStatusChange(
145 const struct camera_module_callbacks* callbacks,
146 const char* camera_id,
147 int new_status) {
Yin-Chia Yeh6dc9b532017-02-09 18:43:35 -0800148 CameraProvider* cp = const_cast<CameraProvider*>(
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800149 static_cast<const CameraProvider*>(callbacks));
150
151 if (cp == nullptr) {
152 ALOGE("%s: callback ops is null", __FUNCTION__);
153 return;
154 }
155
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800156 Mutex::Autolock _l(cp->mCbLock);
157 if (cp->mCallbacks != nullptr) {
158 std::string cameraIdStr(camera_id);
159 TorchModeStatus status = (TorchModeStatus) new_status;
160 for (auto const& deviceNamePair : cp->mCameraDeviceNames) {
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800161 if (cameraIdStr.compare(deviceNamePair.first) == 0) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800162 cp->mCallbacks->torchModeStatusChange(
163 deviceNamePair.second, status);
164 }
165 }
166 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800167}
168
169Status CameraProvider::getHidlStatus(int status) {
170 switch (status) {
171 case 0: return Status::OK;
172 case -ENODEV: return Status::INTERNAL_ERROR;
173 case -EINVAL: return Status::ILLEGAL_ARGUMENT;
174 default:
175 ALOGE("%s: unknown HAL status code %d", __FUNCTION__, status);
176 return Status::INTERNAL_ERROR;
177 }
178}
179
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800180std::string CameraProvider::getLegacyCameraId(const hidl_string& deviceName) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700181 std::string cameraId;
182 matchDeviceName(deviceName, nullptr, &cameraId);
183 return cameraId;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800184}
185
186int CameraProvider::getCameraDeviceVersion(const hidl_string& deviceName) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700187 std::string deviceVersion;
188 bool match = matchDeviceName(deviceName, &deviceVersion, nullptr);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800189 if (!match) {
190 return -1;
191 }
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700192 if (deviceVersion == kHAL3_3) {
193 return CAMERA_DEVICE_API_VERSION_3_3;
194 } else if (deviceVersion == kHAL3_2) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800195 return CAMERA_DEVICE_API_VERSION_3_2;
Andreas Gampe0b171f12017-04-04 20:02:25 -0700196 } else if (deviceVersion == kHAL1_0) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800197 return CAMERA_DEVICE_API_VERSION_1_0;
198 }
199 return 0;
200}
201
202std::string CameraProvider::getHidlDeviceName(
203 std::string cameraId, int deviceVersion) {
204 // Maybe consider create a version check method and SortedVec to speed up?
205 if (deviceVersion != CAMERA_DEVICE_API_VERSION_1_0 &&
206 deviceVersion != CAMERA_DEVICE_API_VERSION_3_2 &&
207 deviceVersion != CAMERA_DEVICE_API_VERSION_3_3 &&
208 deviceVersion != CAMERA_DEVICE_API_VERSION_3_4 ) {
209 return hidl_string("");
210 }
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700211 bool isV1 = deviceVersion == CAMERA_DEVICE_API_VERSION_1_0;
212 int versionMajor = isV1 ? 1 : 3;
213 int versionMinor = isV1 ? 0 : mPreferredHal3MinorVersion;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800214 char deviceName[kMaxCameraDeviceNameLen];
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700215 snprintf(deviceName, sizeof(deviceName), "device@%d.%d/legacy/%s",
216 versionMajor, versionMinor, cameraId.c_str());
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800217 return deviceName;
218}
219
220CameraProvider::CameraProvider() :
221 camera_module_callbacks_t({sCameraDeviceStatusChange,
222 sTorchModeStatusChange}) {
223 mInitFailed = initialize();
224}
225
226CameraProvider::~CameraProvider() {}
227
228bool CameraProvider::initialize() {
229 camera_module_t *rawModule;
230 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
231 (const hw_module_t **)&rawModule);
232 if (err < 0) {
233 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
234 return true;
235 }
236
237 mModule = new CameraModule(rawModule);
238 err = mModule->init();
239 if (err != OK) {
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800240 ALOGE("Could not initialize camera HAL module: %d (%s)", err, strerror(-err));
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800241 mModule.clear();
242 return true;
243 }
244 ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
245
Shuzhen Wangefb7bfa2017-03-15 18:26:39 -0700246 // Setup vendor tags here so HAL can setup vendor keys in camera characteristics
247 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
248 if (!setUpVendorTags()) {
249 ALOGE("%s: Vendor tag setup failed, will not be available.", __FUNCTION__);
250 }
251
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800252 // Setup callback now because we are going to try openLegacy next
253 err = mModule->setCallbacks(this);
254 if (err != OK) {
255 ALOGE("Could not set camera module callback: %d (%s)", err, strerror(-err));
256 mModule.clear();
257 return true;
258 }
259
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700260 mPreferredHal3MinorVersion = property_get_int32("ro.camera.wrapper.hal3TrebleMinorVersion", 3);
261 ALOGV("Preferred HAL 3 minor version is %d", mPreferredHal3MinorVersion);
262 switch(mPreferredHal3MinorVersion) {
263 case 2:
264 case 3:
265 // OK
266 break;
267 default:
268 ALOGW("Unknown minor camera device HAL version %d in property "
269 "'camera.wrapper.hal3TrebleMinorVersion', defaulting to 3", mPreferredHal3MinorVersion);
270 mPreferredHal3MinorVersion = 3;
271 }
272
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800273 mNumberOfLegacyCameras = mModule->getNumberOfCameras();
274 for (int i = 0; i < mNumberOfLegacyCameras; i++) {
Emilian Peevc9ded512017-04-10 16:12:55 +0100275 struct camera_info info;
276 auto rc = mModule->getCameraInfo(i, &info);
277 if (rc != NO_ERROR) {
278 ALOGE("%s: Camera info query failed!", __func__);
279 mModule.clear();
280 return true;
281 }
282
283 if (checkCameraVersion(i, info) != OK) {
284 ALOGE("%s: Camera version check failed!", __func__);
285 mModule.clear();
286 return true;
287 }
288
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800289 char cameraId[kMaxCameraIdLen];
290 snprintf(cameraId, sizeof(cameraId), "%d", i);
291 std::string cameraIdStr(cameraId);
292 mCameraStatusMap[cameraIdStr] = CAMERA_DEVICE_STATUS_PRESENT;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800293
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100294 addDeviceNames(i);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800295 }
296
Eino-Ville Talvala0f5eb832017-02-09 19:45:31 -0800297 return false; // mInitFailed
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800298}
299
Emilian Peevc9ded512017-04-10 16:12:55 +0100300/**
301 * Check that the device HAL version is still in supported.
302 */
303int CameraProvider::checkCameraVersion(int id, camera_info info) {
304 if (mModule == nullptr) {
305 return NO_INIT;
306 }
307
308 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
309 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
310 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
311 // Verify the device version is in the supported range
312 switch (info.device_version) {
313 case CAMERA_DEVICE_API_VERSION_1_0:
314 case CAMERA_DEVICE_API_VERSION_3_2:
315 case CAMERA_DEVICE_API_VERSION_3_3:
316 case CAMERA_DEVICE_API_VERSION_3_4:
317 // in support
318 break;
319 case CAMERA_DEVICE_API_VERSION_2_0:
320 case CAMERA_DEVICE_API_VERSION_2_1:
321 case CAMERA_DEVICE_API_VERSION_3_0:
322 case CAMERA_DEVICE_API_VERSION_3_1:
323 // no longer supported
324 default:
325 ALOGE("%s: Device %d has HAL version %x, which is not supported",
326 __FUNCTION__, id, info.device_version);
327 return NO_INIT;
328 }
329 }
330
331 return OK;
332}
333
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800334bool CameraProvider::setUpVendorTags() {
335 ATRACE_CALL();
336 vendor_tag_ops_t vOps = vendor_tag_ops_t();
337
338 // Check if vendor operations have been implemented
339 if (!mModule->isVendorTagDefined()) {
340 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
Eino-Ville Talvala0f5eb832017-02-09 19:45:31 -0800341 return true;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800342 }
343
344 mModule->getVendorTagOps(&vOps);
345
346 // Ensure all vendor operations are present
347 if (vOps.get_tag_count == nullptr || vOps.get_all_tags == nullptr ||
348 vOps.get_section_name == nullptr || vOps.get_tag_name == nullptr ||
349 vOps.get_tag_type == nullptr) {
350 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
351 , __FUNCTION__);
352 return false;
353 }
354
355 // Read all vendor tag definitions into a descriptor
356 sp<VendorTagDescriptor> desc;
357 status_t res;
358 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
359 != OK) {
360 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
361 "received error %s (%d). Camera clients will not be able to use"
362 "vendor tags", __FUNCTION__, strerror(res), res);
363 return false;
364 }
365
366 // Set the global descriptor to use with camera metadata
367 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
368 const SortedVector<String8>* sectionNames = desc->getAllSectionNames();
369 size_t numSections = sectionNames->size();
370 std::vector<std::vector<VendorTag>> tagsBySection(numSections);
371 int tagCount = desc->getTagCount();
372 std::vector<uint32_t> tags(tagCount);
373 desc->getTagArray(tags.data());
374 for (int i = 0; i < tagCount; i++) {
375 VendorTag vt;
376 vt.tagId = tags[i];
377 vt.tagName = desc->getTagName(tags[i]);
378 vt.tagType = (CameraMetadataType) desc->getTagType(tags[i]);
379 ssize_t sectionIdx = desc->getSectionIndex(tags[i]);
380 tagsBySection[sectionIdx].push_back(vt);
381 }
382 mVendorTagSections.resize(numSections);
383 for (size_t s = 0; s < numSections; s++) {
384 mVendorTagSections[s].sectionName = (*sectionNames)[s].string();
385 mVendorTagSections[s].tags = tagsBySection[s];
386 }
387 return true;
388}
389
390// Methods from ::android::hardware::camera::provider::V2_4::ICameraProvider follow.
391Return<Status> CameraProvider::setCallback(const sp<ICameraProviderCallback>& callback) {
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800392 Mutex::Autolock _l(mCbLock);
393 mCallbacks = callback;
394 return Status::OK;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800395}
396
397Return<void> CameraProvider::getVendorTags(getVendorTags_cb _hidl_cb) {
398 _hidl_cb(Status::OK, mVendorTagSections);
399 return Void();
400}
401
402Return<void> CameraProvider::getCameraIdList(getCameraIdList_cb _hidl_cb) {
403 std::vector<hidl_string> deviceNameList;
404 for (auto const& deviceNamePair : mCameraDeviceNames) {
405 if (mCameraStatusMap[deviceNamePair.first] == CAMERA_DEVICE_STATUS_PRESENT) {
406 deviceNameList.push_back(deviceNamePair.second);
407 }
408 }
409 hidl_vec<hidl_string> hidlDeviceNameList(deviceNameList);
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800410 _hidl_cb(Status::OK, hidlDeviceNameList);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800411 return Void();
412}
413
414Return<void> CameraProvider::isSetTorchModeSupported(isSetTorchModeSupported_cb _hidl_cb) {
415 bool support = mModule->isSetTorchModeSupported();
416 _hidl_cb (Status::OK, support);
417 return Void();
418}
419
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800420Return<void> CameraProvider::getCameraDeviceInterface_V1_x(
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800421 const hidl_string& cameraDeviceName, getCameraDeviceInterface_V1_x_cb _hidl_cb) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700422 std::string cameraId, deviceVersion;
423 bool match = matchDeviceName(cameraDeviceName, &deviceVersion, &cameraId);
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800424 if (!match) {
425 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
426 return Void();
427 }
428
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800429 std::string deviceName(cameraDeviceName.c_str());
430 ssize_t index = mCameraDeviceNames.indexOf(std::make_pair(cameraId, deviceName));
431 if (index == NAME_NOT_FOUND) { // Either an illegal name or a device version mismatch
432 Status status = Status::OK;
433 ssize_t idx = mCameraIds.indexOf(cameraId);
434 if (idx == NAME_NOT_FOUND) {
435 ALOGE("%s: cannot find camera %s!", __FUNCTION__, cameraId.c_str());
436 status = Status::ILLEGAL_ARGUMENT;
437 } else { // invalid version
438 ALOGE("%s: camera device %s does not support version %s!",
439 __FUNCTION__, cameraId.c_str(), deviceVersion.c_str());
440 status = Status::OPERATION_NOT_SUPPORTED;
441 }
442 _hidl_cb(status, nullptr);
443 return Void();
444 }
445
446 if (mCameraStatusMap.count(cameraId) == 0 ||
447 mCameraStatusMap[cameraId] != CAMERA_DEVICE_STATUS_PRESENT) {
448 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
449 return Void();
450 }
451
452 sp<android::hardware::camera::device::V1_0::implementation::CameraDevice> device =
453 new android::hardware::camera::device::V1_0::implementation::CameraDevice(
454 mModule, cameraId, mCameraDeviceNames);
455
456 if (device == nullptr) {
457 ALOGE("%s: cannot allocate camera device for id %s", __FUNCTION__, cameraId.c_str());
458 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
459 return Void();
460 }
461
462 if (device->isInitFailed()) {
463 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
464 device = nullptr;
465 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
466 return Void();
467 }
468
469 _hidl_cb (Status::OK, device);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800470 return Void();
471}
472
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800473Return<void> CameraProvider::getCameraDeviceInterface_V3_x(
474 const hidl_string& cameraDeviceName, getCameraDeviceInterface_V3_x_cb _hidl_cb) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700475 std::string cameraId, deviceVersion;
476 bool match = matchDeviceName(cameraDeviceName, &deviceVersion, &cameraId);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800477 if (!match) {
478 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
479 return Void();
480 }
481
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800482 std::string deviceName(cameraDeviceName.c_str());
483 ssize_t index = mCameraDeviceNames.indexOf(std::make_pair(cameraId, deviceName));
484 if (index == NAME_NOT_FOUND) { // Either an illegal name or a device version mismatch
485 Status status = Status::OK;
486 ssize_t idx = mCameraIds.indexOf(cameraId);
487 if (idx == NAME_NOT_FOUND) {
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800488 ALOGE("%s: cannot find camera %s!", __FUNCTION__, cameraId.c_str());
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800489 status = Status::ILLEGAL_ARGUMENT;
490 } else { // invalid version
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800491 ALOGE("%s: camera device %s does not support version %s!",
492 __FUNCTION__, cameraId.c_str(), deviceVersion.c_str());
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800493 status = Status::OPERATION_NOT_SUPPORTED;
494 }
495 _hidl_cb(status, nullptr);
496 return Void();
497 }
498
499 if (mCameraStatusMap.count(cameraId) == 0 ||
500 mCameraStatusMap[cameraId] != CAMERA_DEVICE_STATUS_PRESENT) {
501 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
502 return Void();
503 }
504
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700505 // Since some Treble HAL revisions can map to the same legacy HAL version(s), we default
506 // to the newest possible Treble HAL revision, but allow for override if needed via
507 // system property.
508 sp<android::hardware::camera::device::V3_2::ICameraDevice> device;
509 switch (mPreferredHal3MinorVersion) {
510 case 2: { // Map legacy camera device v3 HAL to Treble camera device HAL v3.2
511 ALOGV("Constructing v3.2 camera device");
512 sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl =
513 new android::hardware::camera::device::V3_2::implementation::CameraDevice(
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800514 mModule, cameraId, mCameraDeviceNames);
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700515 if (deviceImpl == nullptr || deviceImpl->isInitFailed()) {
516 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
517 device = nullptr;
518 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
519 return Void();
520 }
521 device = deviceImpl;
522 break;
523 }
524 case 3: { // Map legacy camera device v3 HAL to Treble camera device HAL v3.3
525 ALOGV("Constructing v3.3 camera device");
526 sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl =
527 new android::hardware::camera::device::V3_3::implementation::CameraDevice(
528 mModule, cameraId, mCameraDeviceNames);
529 if (deviceImpl == nullptr || deviceImpl->isInitFailed()) {
530 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
531 device = nullptr;
532 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
533 return Void();
534 }
535 device = deviceImpl;
536 break;
537 }
538 default:
539 ALOGE("%s: Unknown HAL minor version %d!", __FUNCTION__, mPreferredHal3MinorVersion);
540 device = nullptr;
541 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
542 return Void();
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800543 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800544 _hidl_cb (Status::OK, device);
545 return Void();
546}
547
548ICameraProvider* HIDL_FETCH_ICameraProvider(const char* name) {
549 if (strcmp(name, kLegacyProviderName) != 0) {
550 return nullptr;
551 }
552 CameraProvider* provider = new CameraProvider();
553 if (provider == nullptr) {
554 ALOGE("%s: cannot allocate camera provider!", __FUNCTION__);
555 return nullptr;
556 }
557 if (provider->isInitFailed()) {
558 ALOGE("%s: camera provider init failed!", __FUNCTION__);
559 delete provider;
560 return nullptr;
561 }
562 return provider;
563}
564
565} // namespace implementation
566} // namespace V2_4
567} // namespace provider
568} // namespace camera
569} // namespace hardware
570} // namespace android