1 /* Copyright (c) 2013-2015, 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 
39 using namespace android;
40 
41 namespace qcamera {
42 
43 
getInstance()44 QCameraThermalAdapter& QCameraThermalAdapter::getInstance()
45 {
46     static QCameraThermalAdapter instance;
47     return instance;
48 }
49 
QCameraThermalAdapter()50 QCameraThermalAdapter::QCameraThermalAdapter() :
51                                         mCallback(NULL),
52                                         mHandle(NULL),
53                                         mRegister(NULL),
54                                         mUnregister(NULL),
55                                         mCameraHandle(0),
56                                         mCamcorderHandle(0)
57 {
58 }
59 
init(QCameraThermalCallback * thermalCb)60 int QCameraThermalAdapter::init(QCameraThermalCallback *thermalCb)
61 {
62     const char *error = NULL;
63     int rc = NO_ERROR;
64 
65     CDBG("%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     mCallback = thermalCb;
92 
93     // Register camera and camcorder callbacks
94     mCameraHandle = mRegister(mStrCamera, thermalCallback, NULL);
95     if (mCameraHandle < 0) {
96         ALOGE("%s: thermal_client_register_callback failed %d",
97                         __func__, mCameraHandle);
98         rc = UNKNOWN_ERROR;
99         goto error2;
100     }
101     mCamcorderHandle = mRegister(mStrCamcorder, thermalCallback, NULL);
102     if (mCamcorderHandle < 0) {
103         ALOGE("%s: thermal_client_register_callback failed %d",
104                         __func__, mCamcorderHandle);
105         rc = UNKNOWN_ERROR;
106         goto error3;
107     }
108 
109     CDBG("%s X", __func__);
110     return rc;
111 
112 error3:
113     mCamcorderHandle = 0;
114     mUnregister(mCameraHandle);
115 error2:
116     mCameraHandle = 0;
117     dlclose(mHandle);
118     mHandle = NULL;
119 error:
120     CDBG("%s X", __func__);
121     return rc;
122 }
123 
deinit()124 void QCameraThermalAdapter::deinit()
125 {
126     CDBG("%s E", __func__);
127     if (mUnregister) {
128         if (mCameraHandle) {
129             mUnregister(mCameraHandle);
130             mCameraHandle = 0;
131         }
132         if (mCamcorderHandle) {
133             mUnregister(mCamcorderHandle);
134             mCamcorderHandle = 0;
135         }
136     }
137     if (mHandle)
138         dlclose(mHandle);
139 
140     mHandle = NULL;
141     mRegister = NULL;
142     mUnregister = NULL;
143     mCallback = NULL;
144     CDBG("%s X", __func__);
145 }
146 
147 char QCameraThermalAdapter::mStrCamera[] = "camera";
148 char QCameraThermalAdapter::mStrCamcorder[] = "camcorder";
149 
thermalCallback(int level,void * userdata,void * data)150 int QCameraThermalAdapter::thermalCallback(int level,
151                 void *userdata, void *data)
152 {
153     int rc = 0;
154     CDBG("%s E", __func__);
155     QCameraThermalCallback *mcb = getInstance().mCallback;
156 
157     if (mcb) {
158         mcb->setThermalLevel((qcamera_thermal_level_enum_t) level);
159         rc = mcb->thermalEvtHandle(mcb->getThermalLevel(), userdata, data);
160     }
161     CDBG("%s X", __func__);
162     return rc;
163 }
164 
getThermalLevel()165 qcamera_thermal_level_enum_t *QCameraThermalCallback::getThermalLevel() {
166     return &mLevel;
167 }
168 
setThermalLevel(qcamera_thermal_level_enum_t level)169 void QCameraThermalCallback::setThermalLevel(qcamera_thermal_level_enum_t level) {
170     mLevel = level;
171 }
172 }; //namespace qcamera
173