QCameraThermalAdapter.cpp revision 744f5403bebb0e27140d16a5df7a341713c24a13
1/* Copyright (c) 2013, The Linux Foundataion. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#define LOG_TAG "QCameraThermalAdapter"
31
32#include <dlfcn.h>
33#include <stdlib.h>
34#include <utils/Errors.h>
35
36#include "QCamera2HWI.h"
37#include "QCameraThermalAdapter.h"
38
39using namespace android;
40
41namespace qcamera {
42
43
44QCameraThermalAdapter& QCameraThermalAdapter::getInstance()
45{
46    static QCameraThermalAdapter instance;
47    return instance;
48}
49
50QCameraThermalAdapter::QCameraThermalAdapter() :
51                                        mCallback(NULL),
52                                        mHandle(NULL),
53                                        mRegister(NULL),
54                                        mUnregister(NULL),
55                                        mCameraHandle(0),
56                                        mCamcorderHandle(0)
57{
58}
59
60int QCameraThermalAdapter::init(QCameraThermalCallback *thermalCb)
61{
62    const char *error = NULL;
63    int rc = NO_ERROR;
64
65    ALOGV("%s E", __func__);
66    mHandle = dlopen("/vendor/lib/libthermalclient.so", RTLD_NOW);
67    if (!mHandle) {
68        error = dlerror();
69        ALOGE("%s: dlopen failed with error %s",
70                    __func__, error ? error : "");
71        rc = UNKNOWN_ERROR;
72        goto error;
73    }
74    *(void **)&mRegister = dlsym(mHandle, "thermal_client_register_callback");
75    if (!mRegister) {
76        error = dlerror();
77        ALOGE("%s: dlsym failed with error code %s",
78                    __func__, error ? error: "");
79        rc = UNKNOWN_ERROR;
80        goto error2;
81    }
82    *(void **)&mUnregister = dlsym(mHandle, "thermal_client_unregister_callback");
83    if (!mUnregister) {
84        error = dlerror();
85        ALOGE("%s: dlsym failed with error code %s",
86                    __func__, error ? error: "");
87        rc = UNKNOWN_ERROR;
88        goto error2;
89    }
90
91    // Register camera and camcorder callbacks
92    mCameraHandle = mRegister(mStrCamera, thermalCallback, NULL);
93    if (mCameraHandle < 0) {
94        ALOGE("%s: thermal_client_register_callback failed %d",
95                        __func__, mCameraHandle);
96        rc = UNKNOWN_ERROR;
97        goto error2;
98    }
99    mCamcorderHandle = mRegister(mStrCamcorder, thermalCallback, NULL);
100    if (mCamcorderHandle < 0) {
101        ALOGE("%s: thermal_client_register_callback failed %d",
102                        __func__, mCamcorderHandle);
103        rc = UNKNOWN_ERROR;
104        goto error3;
105    }
106
107    mCallback = thermalCb;
108    ALOGV("%s X", __func__);
109    return rc;
110
111error3:
112    mCamcorderHandle = 0;
113    mUnregister(mCameraHandle);
114error2:
115    mCameraHandle = 0;
116    dlclose(mHandle);
117    mHandle = NULL;
118error:
119    ALOGV("%s X", __func__);
120    return rc;
121}
122
123void QCameraThermalAdapter::deinit()
124{
125    ALOGV("%s E", __func__);
126    if (mUnregister) {
127        if (mCameraHandle) {
128            mUnregister(mCameraHandle);
129            mCameraHandle = 0;
130        }
131        if (mCamcorderHandle) {
132            mUnregister(mCamcorderHandle);
133            mCamcorderHandle = 0;
134        }
135    }
136    if (mHandle)
137        dlclose(mHandle);
138
139    mHandle = NULL;
140    mRegister = NULL;
141    mUnregister = NULL;
142    mCallback = NULL;
143    ALOGV("%s X", __func__);
144}
145
146char QCameraThermalAdapter::mStrCamera[] = "camera";
147char QCameraThermalAdapter::mStrCamcorder[] = "camcorder";
148
149int QCameraThermalAdapter::thermalCallback(int level,
150                void *userdata, void *data)
151{
152    int rc = 0;
153    ALOGV("%s E", __func__);
154    QCameraThermalAdapter& instance = getInstance();
155    qcamera_thermal_level_enum_t lvl = (qcamera_thermal_level_enum_t) level;
156    if (instance.mCallback)
157        rc = instance.mCallback->thermalEvtHandle(lvl, userdata, data);
158    ALOGV("%s X", __func__);
159    return rc;
160}
161
162}; //namespace qcamera
163