blob: 24cfe4fbe241945089d0aa0eba9c9fa714fa8aec [file] [log] [blame]
Alex Ray7ee0b7a2012-11-06 00:12:49 -08001/*
2 * Copyright (C) 2012 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 <cstdlib>
18#include <pthread.h>
19
20#define LOG_NDEBUG 0
21#define LOG_TAG "Camera"
22#include <cutils/log.h>
23
24#include "Camera.h"
25
26namespace default_camera_hal {
27
28extern "C" {
29// Shim passed to the framework to close an opened device.
30static int close_device(hw_device_t* dev)
31{
32 camera2_device_t* cam_dev = reinterpret_cast<camera2_device_t*>(dev);
33 Camera* cam = static_cast<Camera*>(cam_dev->priv);
34 return cam->close();
35}
36} // extern "C"
37
38Camera::Camera(int id, const hw_module_t* module)
39 : mId(id),
40 mBusy(false),
41 mMetadata(NULL)
42{
43 pthread_mutex_init(&mMutex,
44 NULL); // No pthread mutex attributes.
45
46 // TODO: fill in device operations function pointers
47 mDevice.common.tag = HARDWARE_DEVICE_TAG;
48 mDevice.common.module = const_cast<hw_module_t*>(module);
49 mDevice.common.close = close_device;
50 mDevice.priv = this;
51}
52
53Camera::~Camera()
54{
55}
56
57int Camera::open()
58{
59 ALOGV("%s: camera id %d", __func__, mId);
60 pthread_mutex_lock(&mMutex);
61 if (mBusy) {
62 pthread_mutex_unlock(&mMutex);
63 ALOGE("%s:id%d: Error, device already in use.", __func__, mId);
64 return -EBUSY;
65 }
66
67 // TODO: open camera dev nodes, etc
68 mBusy = true;
69
70 pthread_mutex_unlock(&mMutex);
71 return 0;
72}
73
74int Camera::close()
75{
76 ALOGV("%s: camera id %d", __func__, mId);
77 pthread_mutex_lock(&mMutex);
78 if (!mBusy) {
79 pthread_mutex_unlock(&mMutex);
80 ALOGE("%s:id%d: Error, close() on not open device.", __func__, mId);
81 return -EINVAL;
82 }
83
84 // TODO: close camera dev nodes, etc
85 mBusy = false;
86
87 pthread_mutex_unlock(&mMutex);
88 return 0;
89}
90
91int Camera::getCameraInfo(struct camera_info* info)
92{
93 ALOGV("%s: camera id %d: info=%p", __func__, mId, info);
94 init();
95 if (mMetadata == NULL) {
96 ALOGE("%s:id%d: unable to initialize metadata.", __func__, mId);
97 return -ENOENT;
98 }
99
100 // TODO: fill remainder of info struct
101 info->static_camera_characteristics = mMetadata;
102
103 return 0;
104}
105
106void Camera::init()
107{
108 pthread_mutex_lock(&mMutex);
109 if (mMetadata != NULL) {
110 pthread_mutex_unlock(&mMutex);
111 return;
112 }
113
114 // TODO: init metadata, dummy value for now
115 mMetadata = allocate_camera_metadata(1,1);
116
117 pthread_mutex_unlock(&mMutex);
118}
119
120} // namespace default_camera_hal