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