1/* Copyright (c) 2013-2016, The Linux Foundation. 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// System dependencies
33#include <dlfcn.h>
34#include <utils/Errors.h>
35
36// Camera dependencies
37#include "QCamera2HWI.h"
38#include "QCameraThermalAdapter.h"
39
40extern "C" {
41#include "mm_camera_dbg.h"
42}
43
44using namespace android;
45
46namespace qcamera {
47
48
49QCameraThermalAdapter& QCameraThermalAdapter::getInstance()
50{
51    static QCameraThermalAdapter instance;
52    return instance;
53}
54
55QCameraThermalAdapter::QCameraThermalAdapter() :
56                                        mCallback(NULL),
57                                        mHandle(NULL),
58                                        mRegister(NULL),
59                                        mUnregister(NULL),
60                                        mCameraHandle(0),
61                                        mCamcorderHandle(0)
62{
63}
64
65int QCameraThermalAdapter::init(QCameraThermalCallback *thermalCb)
66{
67    const char *error = NULL;
68    int rc = NO_ERROR;
69
70    LOGD("E");
71    mHandle = dlopen("/vendor/lib/libthermalclient.so", RTLD_NOW);
72    if (!mHandle) {
73        error = dlerror();
74        LOGE("dlopen failed with error %s",
75                     error ? error : "");
76        rc = UNKNOWN_ERROR;
77        goto error;
78    }
79    *(void **)&mRegister = dlsym(mHandle, "thermal_client_register_callback");
80    if (!mRegister) {
81        error = dlerror();
82        LOGE("dlsym failed with error code %s",
83                     error ? error: "");
84        rc = UNKNOWN_ERROR;
85        goto error2;
86    }
87    *(void **)&mUnregister = dlsym(mHandle, "thermal_client_unregister_callback");
88    if (!mUnregister) {
89        error = dlerror();
90        LOGE("dlsym failed with error code %s",
91                     error ? error: "");
92        rc = UNKNOWN_ERROR;
93        goto error2;
94    }
95
96    mCallback = thermalCb;
97
98    // Register camera and camcorder callbacks
99    mCameraHandle = mRegister(mStrCamera, thermalCallback, NULL);
100    if (mCameraHandle < 0) {
101        LOGE("thermal_client_register_callback failed %d",
102                         mCameraHandle);
103        rc = UNKNOWN_ERROR;
104        goto error2;
105    }
106    mCamcorderHandle = mRegister(mStrCamcorder, thermalCallback, NULL);
107    if (mCamcorderHandle < 0) {
108        LOGE("thermal_client_register_callback failed %d",
109                         mCamcorderHandle);
110        rc = UNKNOWN_ERROR;
111        goto error3;
112    }
113
114    LOGD("X");
115    return rc;
116
117error3:
118    mCamcorderHandle = 0;
119    mUnregister(mCameraHandle);
120error2:
121    mCameraHandle = 0;
122    dlclose(mHandle);
123    mHandle = NULL;
124error:
125    LOGD("X");
126    return rc;
127}
128
129void QCameraThermalAdapter::deinit()
130{
131    LOGD("E");
132    if (mUnregister) {
133        if (mCameraHandle) {
134            mUnregister(mCameraHandle);
135            mCameraHandle = 0;
136        }
137        if (mCamcorderHandle) {
138            mUnregister(mCamcorderHandle);
139            mCamcorderHandle = 0;
140        }
141    }
142    if (mHandle)
143        dlclose(mHandle);
144
145    mHandle = NULL;
146    mRegister = NULL;
147    mUnregister = NULL;
148    mCallback = NULL;
149    LOGD("X");
150}
151
152char QCameraThermalAdapter::mStrCamera[] = "camera";
153char QCameraThermalAdapter::mStrCamcorder[] = "camcorder";
154
155int QCameraThermalAdapter::thermalCallback(int level,
156                void *userdata, void *data)
157{
158    int rc = 0;
159    LOGD("E");
160    QCameraThermalCallback *mcb = getInstance().mCallback;
161
162    if (mcb) {
163        mcb->setThermalLevel((qcamera_thermal_level_enum_t) level);
164        rc = mcb->thermalEvtHandle(mcb->getThermalLevel(), userdata, data);
165    }
166    LOGD("X");
167    return rc;
168}
169
170qcamera_thermal_level_enum_t *QCameraThermalCallback::getThermalLevel() {
171    return &mLevel;
172}
173
174void QCameraThermalCallback::setThermalLevel(qcamera_thermal_level_enum_t level) {
175    mLevel = level;
176}
177}; //namespace qcamera
178