blob: e9588a770cad015d7c38af32a5f6c449a24d52e9 [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"
Emilian Peeve18057b2017-11-13 16:03:44 +000024#include "CameraDevice_3_4.h"
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -070025#include <cutils/properties.h>
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080026#include <string.h>
27#include <utils/Trace.h>
28
29
30namespace android {
31namespace hardware {
32namespace camera {
33namespace provider {
34namespace V2_4 {
35namespace implementation {
36
37namespace {
38const char *kLegacyProviderName = "legacy/0";
39// "device@<version>/legacy/<id>"
40const std::regex kDeviceNameRE("device@([0-9]+\\.[0-9]+)/legacy/(.+)");
41const char *kHAL3_2 = "3.2";
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -070042const char *kHAL3_3 = "3.3";
Emilian Peeve18057b2017-11-13 16:03:44 +000043const char *kHAL3_4 = "3.4";
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080044const char *kHAL1_0 = "1.0";
45const int kMaxCameraDeviceNameLen = 128;
46const int kMaxCameraIdLen = 16;
47
Andreas Gampe0b171f12017-04-04 20:02:25 -070048bool matchDeviceName(const hidl_string& deviceName, std::string* deviceVersion,
49 std::string* cameraId) {
50 std::string deviceNameStd(deviceName.c_str());
51 std::smatch sm;
52 if (std::regex_match(deviceNameStd, sm, kDeviceNameRE)) {
53 if (deviceVersion != nullptr) {
54 *deviceVersion = sm[1];
55 }
56 if (cameraId != nullptr) {
57 *cameraId = sm[2];
58 }
59 return true;
60 }
61 return false;
62}
63
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080064} // anonymous namespace
65
66using ::android::hardware::camera::common::V1_0::CameraMetadataType;
67using ::android::hardware::camera::common::V1_0::Status;
68
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +010069void CameraProvider::addDeviceNames(int camera_id, CameraDeviceStatus status, bool cam_new)
70{
71 char cameraId[kMaxCameraIdLen];
72 snprintf(cameraId, sizeof(cameraId), "%d", camera_id);
73 std::string cameraIdStr(cameraId);
74
75 mCameraIds.add(cameraIdStr);
76
77 // initialize mCameraDeviceNames and mOpenLegacySupported
78 mOpenLegacySupported[cameraIdStr] = false;
79 int deviceVersion = mModule->getDeviceVersion(camera_id);
80 auto deviceNamePair = std::make_pair(cameraIdStr,
81 getHidlDeviceName(cameraIdStr, deviceVersion));
82 mCameraDeviceNames.add(deviceNamePair);
83 if (cam_new) {
84 mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, status);
85 }
86 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
87 mModule->isOpenLegacyDefined()) {
88 // try open_legacy to see if it actually works
89 struct hw_device_t* halDev = nullptr;
90 int ret = mModule->openLegacy(cameraId, CAMERA_DEVICE_API_VERSION_1_0, &halDev);
91 if (ret == 0) {
92 mOpenLegacySupported[cameraIdStr] = true;
93 halDev->close(halDev);
94 deviceNamePair = std::make_pair(cameraIdStr,
95 getHidlDeviceName(cameraIdStr, CAMERA_DEVICE_API_VERSION_1_0));
96 mCameraDeviceNames.add(deviceNamePair);
97 if (cam_new) {
98 mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, status);
99 }
100 } else if (ret == -EBUSY || ret == -EUSERS) {
101 // Looks like this provider instance is not initialized during
102 // system startup and there are other camera users already.
103 // Not a good sign but not fatal.
104 ALOGW("%s: open_legacy try failed!", __FUNCTION__);
105 }
106 }
107}
108
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800109/**
110 * static callback forwarding methods from HAL to instance
111 */
112void CameraProvider::sCameraDeviceStatusChange(
113 const struct camera_module_callbacks* callbacks,
114 int camera_id,
115 int new_status) {
Yin-Chia Yeh6dc9b532017-02-09 18:43:35 -0800116 CameraProvider* cp = const_cast<CameraProvider*>(
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800117 static_cast<const CameraProvider*>(callbacks));
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100118 bool found = false;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800119
120 if (cp == nullptr) {
121 ALOGE("%s: callback ops is null", __FUNCTION__);
122 return;
123 }
124
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800125 Mutex::Autolock _l(cp->mCbLock);
126 char cameraId[kMaxCameraIdLen];
127 snprintf(cameraId, sizeof(cameraId), "%d", camera_id);
128 std::string cameraIdStr(cameraId);
129 cp->mCameraStatusMap[cameraIdStr] = (camera_device_status_t) new_status;
130 if (cp->mCallbacks != nullptr) {
131 CameraDeviceStatus status = (CameraDeviceStatus) new_status;
132 for (auto const& deviceNamePair : cp->mCameraDeviceNames) {
133 if (cameraIdStr.compare(deviceNamePair.first) == 0) {
134 cp->mCallbacks->cameraDeviceStatusChange(
135 deviceNamePair.second, status);
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100136 found = true;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800137 }
138 }
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100139
140 if (!found) {
141 cp->addDeviceNames(camera_id, status, true);
142 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800143 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800144}
145
146void CameraProvider::sTorchModeStatusChange(
147 const struct camera_module_callbacks* callbacks,
148 const char* camera_id,
149 int new_status) {
Yin-Chia Yeh6dc9b532017-02-09 18:43:35 -0800150 CameraProvider* cp = const_cast<CameraProvider*>(
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800151 static_cast<const CameraProvider*>(callbacks));
152
153 if (cp == nullptr) {
154 ALOGE("%s: callback ops is null", __FUNCTION__);
155 return;
156 }
157
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800158 Mutex::Autolock _l(cp->mCbLock);
159 if (cp->mCallbacks != nullptr) {
160 std::string cameraIdStr(camera_id);
161 TorchModeStatus status = (TorchModeStatus) new_status;
162 for (auto const& deviceNamePair : cp->mCameraDeviceNames) {
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800163 if (cameraIdStr.compare(deviceNamePair.first) == 0) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800164 cp->mCallbacks->torchModeStatusChange(
165 deviceNamePair.second, status);
166 }
167 }
168 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800169}
170
171Status CameraProvider::getHidlStatus(int status) {
172 switch (status) {
173 case 0: return Status::OK;
174 case -ENODEV: return Status::INTERNAL_ERROR;
175 case -EINVAL: return Status::ILLEGAL_ARGUMENT;
176 default:
177 ALOGE("%s: unknown HAL status code %d", __FUNCTION__, status);
178 return Status::INTERNAL_ERROR;
179 }
180}
181
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800182std::string CameraProvider::getLegacyCameraId(const hidl_string& deviceName) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700183 std::string cameraId;
184 matchDeviceName(deviceName, nullptr, &cameraId);
185 return cameraId;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800186}
187
188int CameraProvider::getCameraDeviceVersion(const hidl_string& deviceName) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700189 std::string deviceVersion;
190 bool match = matchDeviceName(deviceName, &deviceVersion, nullptr);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800191 if (!match) {
192 return -1;
193 }
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700194 if (deviceVersion == kHAL3_3) {
195 return CAMERA_DEVICE_API_VERSION_3_3;
196 } else if (deviceVersion == kHAL3_2) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800197 return CAMERA_DEVICE_API_VERSION_3_2;
Andreas Gampe0b171f12017-04-04 20:02:25 -0700198 } else if (deviceVersion == kHAL1_0) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800199 return CAMERA_DEVICE_API_VERSION_1_0;
200 }
201 return 0;
202}
203
204std::string CameraProvider::getHidlDeviceName(
205 std::string cameraId, int deviceVersion) {
206 // Maybe consider create a version check method and SortedVec to speed up?
207 if (deviceVersion != CAMERA_DEVICE_API_VERSION_1_0 &&
208 deviceVersion != CAMERA_DEVICE_API_VERSION_3_2 &&
209 deviceVersion != CAMERA_DEVICE_API_VERSION_3_3 &&
Emilian Peeve18057b2017-11-13 16:03:44 +0000210 deviceVersion != CAMERA_DEVICE_API_VERSION_3_4 &&
211 deviceVersion != CAMERA_DEVICE_API_VERSION_3_5) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800212 return hidl_string("");
213 }
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700214 bool isV1 = deviceVersion == CAMERA_DEVICE_API_VERSION_1_0;
215 int versionMajor = isV1 ? 1 : 3;
216 int versionMinor = isV1 ? 0 : mPreferredHal3MinorVersion;
Emilian Peeve18057b2017-11-13 16:03:44 +0000217 if (deviceVersion == CAMERA_DEVICE_API_VERSION_3_5) {
218 versionMinor = 4;
219 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800220 char deviceName[kMaxCameraDeviceNameLen];
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700221 snprintf(deviceName, sizeof(deviceName), "device@%d.%d/legacy/%s",
222 versionMajor, versionMinor, cameraId.c_str());
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800223 return deviceName;
224}
225
226CameraProvider::CameraProvider() :
227 camera_module_callbacks_t({sCameraDeviceStatusChange,
228 sTorchModeStatusChange}) {
229 mInitFailed = initialize();
230}
231
232CameraProvider::~CameraProvider() {}
233
234bool CameraProvider::initialize() {
235 camera_module_t *rawModule;
236 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
237 (const hw_module_t **)&rawModule);
238 if (err < 0) {
239 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
240 return true;
241 }
242
243 mModule = new CameraModule(rawModule);
244 err = mModule->init();
245 if (err != OK) {
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800246 ALOGE("Could not initialize camera HAL module: %d (%s)", err, strerror(-err));
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800247 mModule.clear();
248 return true;
249 }
250 ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
251
Shuzhen Wangefb7bfa2017-03-15 18:26:39 -0700252 // Setup vendor tags here so HAL can setup vendor keys in camera characteristics
253 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
254 if (!setUpVendorTags()) {
255 ALOGE("%s: Vendor tag setup failed, will not be available.", __FUNCTION__);
256 }
257
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800258 // Setup callback now because we are going to try openLegacy next
259 err = mModule->setCallbacks(this);
260 if (err != OK) {
261 ALOGE("Could not set camera module callback: %d (%s)", err, strerror(-err));
262 mModule.clear();
263 return true;
264 }
265
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700266 mPreferredHal3MinorVersion = property_get_int32("ro.camera.wrapper.hal3TrebleMinorVersion", 3);
267 ALOGV("Preferred HAL 3 minor version is %d", mPreferredHal3MinorVersion);
268 switch(mPreferredHal3MinorVersion) {
269 case 2:
270 case 3:
271 // OK
272 break;
273 default:
274 ALOGW("Unknown minor camera device HAL version %d in property "
Emilian Peeve18057b2017-11-13 16:03:44 +0000275 "'camera.wrapper.hal3TrebleMinorVersion', defaulting to 3",
276 mPreferredHal3MinorVersion);
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700277 mPreferredHal3MinorVersion = 3;
278 }
279
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800280 mNumberOfLegacyCameras = mModule->getNumberOfCameras();
281 for (int i = 0; i < mNumberOfLegacyCameras; i++) {
Emilian Peevc9ded512017-04-10 16:12:55 +0100282 struct camera_info info;
283 auto rc = mModule->getCameraInfo(i, &info);
284 if (rc != NO_ERROR) {
285 ALOGE("%s: Camera info query failed!", __func__);
286 mModule.clear();
287 return true;
288 }
289
290 if (checkCameraVersion(i, info) != OK) {
291 ALOGE("%s: Camera version check failed!", __func__);
292 mModule.clear();
293 return true;
294 }
295
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800296 char cameraId[kMaxCameraIdLen];
297 snprintf(cameraId, sizeof(cameraId), "%d", i);
298 std::string cameraIdStr(cameraId);
299 mCameraStatusMap[cameraIdStr] = CAMERA_DEVICE_STATUS_PRESENT;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800300
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100301 addDeviceNames(i);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800302 }
303
Eino-Ville Talvala0f5eb832017-02-09 19:45:31 -0800304 return false; // mInitFailed
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800305}
306
Emilian Peevc9ded512017-04-10 16:12:55 +0100307/**
308 * Check that the device HAL version is still in supported.
309 */
310int CameraProvider::checkCameraVersion(int id, camera_info info) {
311 if (mModule == nullptr) {
312 return NO_INIT;
313 }
314
315 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
316 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
317 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
318 // Verify the device version is in the supported range
319 switch (info.device_version) {
320 case CAMERA_DEVICE_API_VERSION_1_0:
321 case CAMERA_DEVICE_API_VERSION_3_2:
322 case CAMERA_DEVICE_API_VERSION_3_3:
323 case CAMERA_DEVICE_API_VERSION_3_4:
Emilian Peeve18057b2017-11-13 16:03:44 +0000324 case CAMERA_DEVICE_API_VERSION_3_5:
Emilian Peevc9ded512017-04-10 16:12:55 +0100325 // in support
326 break;
327 case CAMERA_DEVICE_API_VERSION_2_0:
328 case CAMERA_DEVICE_API_VERSION_2_1:
329 case CAMERA_DEVICE_API_VERSION_3_0:
330 case CAMERA_DEVICE_API_VERSION_3_1:
331 // no longer supported
332 default:
333 ALOGE("%s: Device %d has HAL version %x, which is not supported",
334 __FUNCTION__, id, info.device_version);
335 return NO_INIT;
336 }
337 }
338
339 return OK;
340}
341
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800342bool CameraProvider::setUpVendorTags() {
343 ATRACE_CALL();
344 vendor_tag_ops_t vOps = vendor_tag_ops_t();
345
346 // Check if vendor operations have been implemented
347 if (!mModule->isVendorTagDefined()) {
348 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
Eino-Ville Talvala0f5eb832017-02-09 19:45:31 -0800349 return true;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800350 }
351
352 mModule->getVendorTagOps(&vOps);
353
354 // Ensure all vendor operations are present
355 if (vOps.get_tag_count == nullptr || vOps.get_all_tags == nullptr ||
356 vOps.get_section_name == nullptr || vOps.get_tag_name == nullptr ||
357 vOps.get_tag_type == nullptr) {
358 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
359 , __FUNCTION__);
360 return false;
361 }
362
363 // Read all vendor tag definitions into a descriptor
364 sp<VendorTagDescriptor> desc;
365 status_t res;
366 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
367 != OK) {
368 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
369 "received error %s (%d). Camera clients will not be able to use"
370 "vendor tags", __FUNCTION__, strerror(res), res);
371 return false;
372 }
373
374 // Set the global descriptor to use with camera metadata
375 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
376 const SortedVector<String8>* sectionNames = desc->getAllSectionNames();
377 size_t numSections = sectionNames->size();
378 std::vector<std::vector<VendorTag>> tagsBySection(numSections);
379 int tagCount = desc->getTagCount();
380 std::vector<uint32_t> tags(tagCount);
381 desc->getTagArray(tags.data());
382 for (int i = 0; i < tagCount; i++) {
383 VendorTag vt;
384 vt.tagId = tags[i];
385 vt.tagName = desc->getTagName(tags[i]);
386 vt.tagType = (CameraMetadataType) desc->getTagType(tags[i]);
387 ssize_t sectionIdx = desc->getSectionIndex(tags[i]);
388 tagsBySection[sectionIdx].push_back(vt);
389 }
390 mVendorTagSections.resize(numSections);
391 for (size_t s = 0; s < numSections; s++) {
392 mVendorTagSections[s].sectionName = (*sectionNames)[s].string();
393 mVendorTagSections[s].tags = tagsBySection[s];
394 }
395 return true;
396}
397
398// Methods from ::android::hardware::camera::provider::V2_4::ICameraProvider follow.
399Return<Status> CameraProvider::setCallback(const sp<ICameraProviderCallback>& callback) {
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800400 Mutex::Autolock _l(mCbLock);
401 mCallbacks = callback;
402 return Status::OK;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800403}
404
405Return<void> CameraProvider::getVendorTags(getVendorTags_cb _hidl_cb) {
406 _hidl_cb(Status::OK, mVendorTagSections);
407 return Void();
408}
409
410Return<void> CameraProvider::getCameraIdList(getCameraIdList_cb _hidl_cb) {
411 std::vector<hidl_string> deviceNameList;
412 for (auto const& deviceNamePair : mCameraDeviceNames) {
413 if (mCameraStatusMap[deviceNamePair.first] == CAMERA_DEVICE_STATUS_PRESENT) {
414 deviceNameList.push_back(deviceNamePair.second);
415 }
416 }
417 hidl_vec<hidl_string> hidlDeviceNameList(deviceNameList);
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800418 _hidl_cb(Status::OK, hidlDeviceNameList);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800419 return Void();
420}
421
422Return<void> CameraProvider::isSetTorchModeSupported(isSetTorchModeSupported_cb _hidl_cb) {
423 bool support = mModule->isSetTorchModeSupported();
424 _hidl_cb (Status::OK, support);
425 return Void();
426}
427
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800428Return<void> CameraProvider::getCameraDeviceInterface_V1_x(
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800429 const hidl_string& cameraDeviceName, getCameraDeviceInterface_V1_x_cb _hidl_cb) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700430 std::string cameraId, deviceVersion;
431 bool match = matchDeviceName(cameraDeviceName, &deviceVersion, &cameraId);
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800432 if (!match) {
433 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
434 return Void();
435 }
436
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800437 std::string deviceName(cameraDeviceName.c_str());
438 ssize_t index = mCameraDeviceNames.indexOf(std::make_pair(cameraId, deviceName));
439 if (index == NAME_NOT_FOUND) { // Either an illegal name or a device version mismatch
440 Status status = Status::OK;
441 ssize_t idx = mCameraIds.indexOf(cameraId);
442 if (idx == NAME_NOT_FOUND) {
443 ALOGE("%s: cannot find camera %s!", __FUNCTION__, cameraId.c_str());
444 status = Status::ILLEGAL_ARGUMENT;
445 } else { // invalid version
446 ALOGE("%s: camera device %s does not support version %s!",
447 __FUNCTION__, cameraId.c_str(), deviceVersion.c_str());
448 status = Status::OPERATION_NOT_SUPPORTED;
449 }
450 _hidl_cb(status, nullptr);
451 return Void();
452 }
453
454 if (mCameraStatusMap.count(cameraId) == 0 ||
455 mCameraStatusMap[cameraId] != CAMERA_DEVICE_STATUS_PRESENT) {
456 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
457 return Void();
458 }
459
460 sp<android::hardware::camera::device::V1_0::implementation::CameraDevice> device =
461 new android::hardware::camera::device::V1_0::implementation::CameraDevice(
462 mModule, cameraId, mCameraDeviceNames);
463
464 if (device == nullptr) {
465 ALOGE("%s: cannot allocate camera device for id %s", __FUNCTION__, cameraId.c_str());
466 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
467 return Void();
468 }
469
470 if (device->isInitFailed()) {
471 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
472 device = nullptr;
473 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
474 return Void();
475 }
476
477 _hidl_cb (Status::OK, device);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800478 return Void();
479}
480
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800481Return<void> CameraProvider::getCameraDeviceInterface_V3_x(
482 const hidl_string& cameraDeviceName, getCameraDeviceInterface_V3_x_cb _hidl_cb) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700483 std::string cameraId, deviceVersion;
484 bool match = matchDeviceName(cameraDeviceName, &deviceVersion, &cameraId);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800485 if (!match) {
486 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
487 return Void();
488 }
489
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800490 std::string deviceName(cameraDeviceName.c_str());
491 ssize_t index = mCameraDeviceNames.indexOf(std::make_pair(cameraId, deviceName));
492 if (index == NAME_NOT_FOUND) { // Either an illegal name or a device version mismatch
493 Status status = Status::OK;
494 ssize_t idx = mCameraIds.indexOf(cameraId);
495 if (idx == NAME_NOT_FOUND) {
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800496 ALOGE("%s: cannot find camera %s!", __FUNCTION__, cameraId.c_str());
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800497 status = Status::ILLEGAL_ARGUMENT;
498 } else { // invalid version
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800499 ALOGE("%s: camera device %s does not support version %s!",
500 __FUNCTION__, cameraId.c_str(), deviceVersion.c_str());
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800501 status = Status::OPERATION_NOT_SUPPORTED;
502 }
503 _hidl_cb(status, nullptr);
504 return Void();
505 }
506
507 if (mCameraStatusMap.count(cameraId) == 0 ||
508 mCameraStatusMap[cameraId] != CAMERA_DEVICE_STATUS_PRESENT) {
509 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
510 return Void();
511 }
512
Emilian Peeve18057b2017-11-13 16:03:44 +0000513 sp<android::hardware::camera::device::V3_2::ICameraDevice> device;
514 if (deviceVersion == kHAL3_4) {
515 ALOGV("Constructing v3.4 camera device");
516 sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl =
517 new android::hardware::camera::device::V3_4::implementation::CameraDevice(
518 mModule, cameraId, mCameraDeviceNames);
519 if (deviceImpl == nullptr || deviceImpl->isInitFailed()) {
520 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
521 device = nullptr;
522 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
523 return Void();
524 }
525
526 device = deviceImpl;
527 _hidl_cb (Status::OK, device);
528 return Void();
529 }
530
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700531 // Since some Treble HAL revisions can map to the same legacy HAL version(s), we default
532 // to the newest possible Treble HAL revision, but allow for override if needed via
533 // system property.
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700534 switch (mPreferredHal3MinorVersion) {
535 case 2: { // Map legacy camera device v3 HAL to Treble camera device HAL v3.2
536 ALOGV("Constructing v3.2 camera device");
537 sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl =
538 new android::hardware::camera::device::V3_2::implementation::CameraDevice(
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800539 mModule, cameraId, mCameraDeviceNames);
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700540 if (deviceImpl == nullptr || deviceImpl->isInitFailed()) {
541 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
542 device = nullptr;
543 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
544 return Void();
545 }
546 device = deviceImpl;
547 break;
548 }
549 case 3: { // Map legacy camera device v3 HAL to Treble camera device HAL v3.3
550 ALOGV("Constructing v3.3 camera device");
551 sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl =
552 new android::hardware::camera::device::V3_3::implementation::CameraDevice(
553 mModule, cameraId, mCameraDeviceNames);
554 if (deviceImpl == nullptr || deviceImpl->isInitFailed()) {
555 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
556 device = nullptr;
557 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
558 return Void();
559 }
560 device = deviceImpl;
561 break;
562 }
563 default:
564 ALOGE("%s: Unknown HAL minor version %d!", __FUNCTION__, mPreferredHal3MinorVersion);
565 device = nullptr;
566 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
567 return Void();
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800568 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800569 _hidl_cb (Status::OK, device);
570 return Void();
571}
572
573ICameraProvider* HIDL_FETCH_ICameraProvider(const char* name) {
574 if (strcmp(name, kLegacyProviderName) != 0) {
575 return nullptr;
576 }
577 CameraProvider* provider = new CameraProvider();
578 if (provider == nullptr) {
579 ALOGE("%s: cannot allocate camera provider!", __FUNCTION__);
580 return nullptr;
581 }
582 if (provider->isInitFailed()) {
583 ALOGE("%s: camera provider init failed!", __FUNCTION__);
584 delete provider;
585 return nullptr;
586 }
587 return provider;
588}
589
590} // namespace implementation
591} // namespace V2_4
592} // namespace provider
593} // namespace camera
594} // namespace hardware
595} // namespace android