Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | #include "V4L2Camera.h" |
| 18 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame^] | 19 | #include <fcntl.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 23 | #include <camera/CameraMetadata.h> |
| 24 | #include <hardware/camera3.h> |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame^] | 25 | #include <nativehelper/ScopedFd.h> |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 26 | |
| 27 | #include "Common.h" |
| 28 | |
| 29 | namespace v4l2_camera_hal { |
| 30 | |
| 31 | V4L2Camera::V4L2Camera(int id, std::string path) |
| 32 | : default_camera_hal::Camera(id), mDevicePath(std::move(path)) { |
| 33 | HAL_LOG_ENTER(); |
| 34 | } |
| 35 | |
| 36 | V4L2Camera::~V4L2Camera() { |
| 37 | HAL_LOG_ENTER(); |
| 38 | } |
| 39 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame^] | 40 | int V4L2Camera::connect() { |
| 41 | HAL_LOG_ENTER(); |
| 42 | |
| 43 | if (mDeviceFd.get() >= 0) { |
| 44 | HAL_LOGE("Camera device %s is opened. Close it first", mDevicePath.c_str()); |
| 45 | return -EIO; |
| 46 | } |
| 47 | |
| 48 | int fd = TEMP_FAILURE_RETRY(open(mDevicePath.c_str(), O_RDWR)); |
| 49 | if (fd < 0) { |
| 50 | HAL_LOGE("failed to open %s (%s)", mDevicePath.c_str(), strerror(errno)); |
| 51 | return -errno; |
| 52 | } |
| 53 | mDeviceFd.reset(fd); |
| 54 | |
| 55 | // TODO(b/29185945): confirm this is a supported device. |
| 56 | // This is checked by the HAL, but the device at mDevicePath may |
| 57 | // not be the same one that was there when the HAL was loaded. |
| 58 | // (Alternatively, better hotplugging support may make this unecessary |
| 59 | // by disabling cameras that get disconnected and checking newly connected |
| 60 | // cameras, so connect() is never called on an unsupported camera) |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | void V4L2Camera::disconnect() { |
| 65 | HAL_LOG_ENTER(); |
| 66 | mDeviceFd.reset(); |
| 67 | } |
| 68 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 69 | camera_metadata_t* V4L2Camera::initStaticInfo() { |
| 70 | HAL_LOG_ENTER(); |
| 71 | |
| 72 | android::CameraMetadata metadata(1); |
| 73 | // TODO(b/29214516): fill this in. |
Ari Hausman-Cohen | 63f6982 | 2016-06-10 11:40:35 -0700 | [diff] [blame] | 74 | uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED; |
| 75 | metadata.update(ANDROID_CONTROL_AVAILABLE_SCENE_MODES, &scene_mode, 1); |
| 76 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 77 | return metadata.release(); |
| 78 | } |
| 79 | |
| 80 | void V4L2Camera::initDeviceInfo(camera_info_t* info) { |
| 81 | HAL_LOG_ENTER(); |
| 82 | |
| 83 | // For now, just constants. |
| 84 | info->facing = CAMERA_FACING_EXTERNAL; |
| 85 | info->orientation = 0; |
| 86 | info->resource_cost = 100; |
| 87 | info->conflicting_devices = nullptr; |
| 88 | info->conflicting_devices_length = 0; |
| 89 | } |
| 90 | |
| 91 | int V4L2Camera::initDevice() { |
| 92 | HAL_LOG_ENTER(); |
| 93 | |
| 94 | // TODO(b/29221795): fill in templates, etc. |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) { |
| 99 | HAL_LOG_ENTER(); |
| 100 | |
| 101 | // TODO(b): reject capture settings this camera isn't capable of. |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | } // namespace v4l2_camera_hal |