1 /* Copyright (c) 2012-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 // System dependencies
31 #include <string.h>
32 #include <utils/Errors.h>
33 #define PRCTL_H <SYSTEM_HEADER_PREFIX/prctl.h>
34 #include PRCTL_H
35
36 // Camera dependencies
37 #include "QCameraCmdThread.h"
38
39 extern "C" {
40 #include "mm_camera_dbg.h"
41 }
42
43 using namespace android;
44
45 namespace qcamera {
46
47 /*===========================================================================
48 * FUNCTION : QCameraCmdThread
49 *
50 * DESCRIPTION: default constructor of QCameraCmdThread
51 *
52 * PARAMETERS : None
53 *
54 * RETURN : None
55 *==========================================================================*/
QCameraCmdThread()56 QCameraCmdThread::QCameraCmdThread() :
57 cmd_queue()
58 {
59 cmd_pid = 0;
60 cam_sem_init(&sync_sem, 0);
61 cam_sem_init(&cmd_sem, 0);
62 }
63
64 /*===========================================================================
65 * FUNCTION : ~QCameraCmdThread
66 *
67 * DESCRIPTION: deconstructor of QCameraCmdThread
68 *
69 * PARAMETERS : None
70 *
71 * RETURN : None
72 *==========================================================================*/
~QCameraCmdThread()73 QCameraCmdThread::~QCameraCmdThread()
74 {
75 cam_sem_destroy(&sync_sem);
76 cam_sem_destroy(&cmd_sem);
77 }
78
79 /*===========================================================================
80 * FUNCTION : launch
81 *
82 * DESCRIPTION: launch Cmd Thread
83 *
84 * PARAMETERS :
85 * @start_routine : thread routine function ptr
86 * @user_data : user data ptr
87 *
88 * RETURN : int32_t type of status
89 * NO_ERROR -- success
90 * none-zero failure code
91 *==========================================================================*/
launch(void * (* start_routine)(void *),void * user_data)92 int32_t QCameraCmdThread::launch(void *(*start_routine)(void *),
93 void* user_data)
94 {
95 /* launch the thread */
96 pthread_create(&cmd_pid,
97 NULL,
98 start_routine,
99 user_data);
100 return NO_ERROR;
101 }
102
103 /*===========================================================================
104 * FUNCTION : setName
105 *
106 * DESCRIPTION: name the cmd thread
107 *
108 * PARAMETERS :
109 * @name : desired name for the thread
110 *
111 * RETURN : int32_t type of status
112 * NO_ERROR -- success
113 * none-zero failure code
114 *==========================================================================*/
setName(const char * name)115 int32_t QCameraCmdThread::setName(const char* name)
116 {
117 /* name the thread */
118 prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
119 return NO_ERROR;
120 }
121
122 /*===========================================================================
123 * FUNCTION : sendCmd
124 *
125 * DESCRIPTION: send a command to the Cmd Thread
126 *
127 * PARAMETERS :
128 * @cmd : command to be executed.
129 * @sync_cmd: flag to indicate if this is a synchorinzed cmd. If true, this call
130 * will wait until signal is set after the command is completed.
131 * @priority: flag to indicate if this is a cmd with priority. If true, the cmd
132 * will be enqueued to the head with priority.
133 *
134 * RETURN : int32_t type of status
135 * NO_ERROR -- success
136 * none-zero failure code
137 *==========================================================================*/
sendCmd(camera_cmd_type_t cmd,uint8_t sync_cmd,uint8_t priority)138 int32_t QCameraCmdThread::sendCmd(camera_cmd_type_t cmd, uint8_t sync_cmd, uint8_t priority)
139 {
140 camera_cmd_t *node = (camera_cmd_t *)malloc(sizeof(camera_cmd_t));
141 if (NULL == node) {
142 LOGE("No memory for camera_cmd_t");
143 return NO_MEMORY;
144 }
145 memset(node, 0, sizeof(camera_cmd_t));
146 node->cmd = cmd;
147
148 if (priority) {
149 if (!cmd_queue.enqueueWithPriority((void *)node)) {
150 free(node);
151 node = NULL;
152 }
153 } else {
154 if (!cmd_queue.enqueue((void *)node)) {
155 free(node);
156 node = NULL;
157 }
158 }
159 cam_sem_post(&cmd_sem);
160
161 /* if is a sync call, need to wait until it returns */
162 if (sync_cmd) {
163 cam_sem_wait(&sync_sem);
164 }
165 return NO_ERROR;
166 }
167
168 /*===========================================================================
169 * FUNCTION : getCmd
170 *
171 * DESCRIPTION: dequeue a cmommand from cmd queue
172 *
173 * PARAMETERS : None
174 *
175 * RETURN : cmd dequeued
176 *==========================================================================*/
getCmd()177 camera_cmd_type_t QCameraCmdThread::getCmd()
178 {
179 camera_cmd_type_t cmd = CAMERA_CMD_TYPE_NONE;
180 camera_cmd_t *node = (camera_cmd_t *)cmd_queue.dequeue();
181 if (NULL == node) {
182 LOGD("No notify avail");
183 return CAMERA_CMD_TYPE_NONE;
184 } else {
185 cmd = node->cmd;
186 free(node);
187 }
188 return cmd;
189 }
190
191 /*===========================================================================
192 * FUNCTION : exit
193 *
194 * DESCRIPTION: exit the CMD thread
195 *
196 * PARAMETERS : None
197 *
198 * RETURN : int32_t type of status
199 * NO_ERROR -- success
200 * none-zero failure code
201 *==========================================================================*/
exit()202 int32_t QCameraCmdThread::exit()
203 {
204 int32_t rc = NO_ERROR;
205
206 if (cmd_pid == 0) {
207 return rc;
208 }
209
210 rc = sendCmd(CAMERA_CMD_TYPE_EXIT, 0, 1);
211 if (NO_ERROR != rc) {
212 LOGE("Error during exit, rc = %d", rc);
213 return rc;
214 }
215
216 /* wait until cmd thread exits */
217 if (pthread_join(cmd_pid, NULL) != 0) {
218 LOGD("pthread dead already\n");
219 }
220 cmd_pid = 0;
221 return rc;
222 }
223
224 }; // namespace qcamera
225