blob: 511d8a109dacbd8d7f4911b33d367017698dc688 [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001/*
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// Modified from hardware/libhardware/modules/camera/CameraHAL.cpp
18
19#include "V4L2CameraHAL.h"
20
21#include <cstdlib>
22
23#include "Common.h"
24#include "V4L2Camera.h"
25
26/*
27 * This file serves as the entry point to the HAL. It is modified from the
28 * example default HAL available in hardware/libhardware/modules/camera.
29 * It contains the module structure and functions used by the framework
30 * to load and interface to this HAL, as well as the handles to the individual
31 * camera devices.
32 */
33
34namespace v4l2_camera_hal {
35
36// Default global camera hal.
37static V4L2CameraHAL gCameraHAL;
38
39// Helper function for converting and validating string name to int id.
40// Returns either a non-negative camera id, or a negative error code.
41static int id_from_name(const char* name) {
42 if (name == NULL || *name == '\0') {
43 HAL_LOGE("Invalid camera id name is NULL");
44 return -EINVAL;
45 }
46 char* nameEnd;
47 int id = strtol(name, &nameEnd, 10);
48 if (*nameEnd != '\0' || id < 0) {
49 HAL_LOGE("Invalid camera id name %s", name);
50 return -EINVAL;
51 }
52 return id;
53}
54
55V4L2CameraHAL::V4L2CameraHAL() : mCameras(), mCallbacks(NULL) {
56 HAL_LOG_ENTER();
57 // TODO(29160300): Populate camera devices.
58}
59
60V4L2CameraHAL::~V4L2CameraHAL() {
61 HAL_LOG_ENTER();
62}
63
64int V4L2CameraHAL::getNumberOfCameras() {
65 HAL_LOGV("returns %d", mCameras.size());
66 return mCameras.size();
67}
68
69int V4L2CameraHAL::getCameraInfo(int id, camera_info_t* info) {
70 HAL_LOG_ENTER();
71 if (id < 0 || id >= mCameras.size()) {
72 return -EINVAL;
73 }
74 // TODO(b/29185945): Hotplugging: return -EINVAL if unplugged.
75 return mCameras[id]->getInfo(info);
76}
77
78int V4L2CameraHAL::setCallbacks(const camera_module_callbacks_t* callbacks) {
79 HAL_LOG_ENTER();
80 mCallbacks = callbacks;
81 return 0;
82}
83
84void V4L2CameraHAL::getVendorTagOps(vendor_tag_ops_t* ops) {
85 HAL_LOG_ENTER();
86 // No vendor ops for this HAL. From <hardware/camera_common.h>:
87 // "leave ops unchanged if no vendor tags are defined."
88}
89
90int V4L2CameraHAL::openLegacy(const hw_module_t* module, const char* id,
91 uint32_t halVersion, hw_device_t** device) {
92 HAL_LOG_ENTER();
93 // Not supported.
94 return -ENOSYS;
95}
96
97int V4L2CameraHAL::setTorchMode(const char* camera_id, bool enabled) {
98 HAL_LOG_ENTER();
99 // TODO(b/29158098): HAL is required to respond appropriately if
100 // the desired camera actually does support flash.
101 return -ENOSYS;
102}
103
104int V4L2CameraHAL::open(const hw_module_t* module, const char* name,
105 hw_device_t** device) {
106 HAL_LOG_ENTER();
107
108 if (module != &HAL_MODULE_INFO_SYM.common) {
109 HAL_LOGE("Invalid module %p expected %p", module,
110 &HAL_MODULE_INFO_SYM.common);
111 return -EINVAL;
112 }
113
114 int id = id_from_name(name);
115 if (id < 0) {
116 return id;
117 } else if (id < 0 || id >= mCameras.size()) {
118 return -EINVAL;
119 }
120 // TODO(b/29185945): Hotplugging: return -EINVAL if unplugged.
121 return mCameras[id]->open(module, device);
122}
123
124/*
125 * The framework calls the following wrappers, which in turn
126 * call the corresponding methods of the global HAL object.
127 */
128
129static int get_number_of_cameras() {
130 return gCameraHAL.getNumberOfCameras();
131}
132
133static int get_camera_info(int id, struct camera_info* info) {
134 return gCameraHAL.getCameraInfo(id, info);
135}
136
137static int set_callbacks(const camera_module_callbacks_t *callbacks) {
138 return gCameraHAL.setCallbacks(callbacks);
139}
140
141static void get_vendor_tag_ops(vendor_tag_ops_t* ops) {
142 return gCameraHAL.getVendorTagOps(ops);
143}
144
145static int open_legacy(const hw_module_t* module, const char* id,
146 uint32_t halVersion, hw_device_t** device) {
147 return gCameraHAL.openLegacy(module, id, halVersion, device);
148}
149
150static int set_torch_mode(const char* camera_id, bool enabled) {
151 return gCameraHAL.setTorchMode(camera_id, enabled);
152}
153
154static int open_dev(const hw_module_t* module, const char* name,
155 hw_device_t** device) {
156 return gCameraHAL.open(module, name, device);
157}
158
159} // namespace v4l2_camera_hal
160
161static hw_module_methods_t v4l2_module_methods = {
162 .open = v4l2_camera_hal::open_dev
163};
164
165camera_module_t HAL_MODULE_INFO_SYM = {
166 .common = {
167 .tag = HARDWARE_MODULE_TAG,
168 .module_api_version = CAMERA_MODULE_API_VERSION_2_4,
169 .hal_api_version = HARDWARE_HAL_API_VERSION,
170 .id = CAMERA_HARDWARE_MODULE_ID,
171 .name = "V4L2 Camera HAL v3",
172 .author = "The Android Open Source Project",
173 .methods = &v4l2_module_methods,
174 .dso = nullptr,
175 .reserved = {0},
176 },
177 .get_number_of_cameras = v4l2_camera_hal::get_number_of_cameras,
178 .get_camera_info = v4l2_camera_hal::get_camera_info,
179 .set_callbacks = v4l2_camera_hal::set_callbacks,
180 .get_vendor_tag_ops = v4l2_camera_hal::get_vendor_tag_ops,
181 .open_legacy = v4l2_camera_hal::open_legacy,
182 .set_torch_mode = v4l2_camera_hal::set_torch_mode,
183 .init = nullptr,
184 .reserved = {nullptr, nullptr, nullptr, nullptr, nullptr}
185};