1 /* Copyright (c) 2012-2014, 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 "QCameraStateMachine"
31 
32 #include <utils/Errors.h>
33 #include "QCamera2HWI.h"
34 #include "QCameraStateMachine.h"
35 
36 namespace qcamera {
37 
38 /*===========================================================================
39  * FUNCTION   : smEvtProcRoutine
40  *
41  * DESCRIPTION: Statemachine process thread routine to handle events
42  *              in different state.
43  *
44  * PARAMETERS :
45  *   @data    : ptr to QCameraStateMachine object
46  *
47  * RETURN     : none
48  *==========================================================================*/
smEvtProcRoutine(void * data)49 void *QCameraStateMachine::smEvtProcRoutine(void *data)
50 {
51     int running = 1, ret;
52     QCameraStateMachine *pme = (QCameraStateMachine *)data;
53 
54     CDBG_HIGH("%s: E", __func__);
55     do {
56         do {
57             ret = cam_sem_wait(&pme->cmd_sem);
58             if (ret != 0 && errno != EINVAL) {
59                 ALOGE("%s: cam_sem_wait error (%s)",
60                            __func__, strerror(errno));
61                 return NULL;
62             }
63         } while (ret != 0);
64 
65         // we got notified about new cmd avail in cmd queue
66         // first check API cmd queue
67         qcamera_sm_cmd_t *node = (qcamera_sm_cmd_t *)pme->api_queue.dequeue();
68         if (node == NULL) {
69             // no API cmd, then check evt cmd queue
70             node = (qcamera_sm_cmd_t *)pme->evt_queue.dequeue();
71         }
72         if (node != NULL) {
73             switch (node->cmd) {
74             case QCAMERA_SM_CMD_TYPE_API:
75                 pme->stateMachine(node->evt, node->evt_payload);
76                 // API is in a way sync call, so evt_payload is managed by HWI
77                 // no need to free payload for API
78                 break;
79             case QCAMERA_SM_CMD_TYPE_EVT:
80                 pme->stateMachine(node->evt, node->evt_payload);
81 
82                 // EVT is async call, so payload need to be free after use
83                 free(node->evt_payload);
84                 node->evt_payload = NULL;
85                 break;
86             case QCAMERA_SM_CMD_TYPE_EXIT:
87                 running = 0;
88                 break;
89             default:
90                 break;
91             }
92             free(node);
93             node = NULL;
94         }
95     } while (running);
96     CDBG_HIGH("%s: X", __func__);
97     return NULL;
98 }
99 
100 /*===========================================================================
101  * FUNCTION   : QCameraStateMachine
102  *
103  * DESCRIPTION: constructor of QCameraStateMachine. Will start process thread
104  *
105  * PARAMETERS :
106  *   @ctrl    : ptr to HWI object
107  *
108  * RETURN     : none
109  *==========================================================================*/
QCameraStateMachine(QCamera2HardwareInterface * ctrl)110 QCameraStateMachine::QCameraStateMachine(QCamera2HardwareInterface *ctrl) :
111     api_queue(),
112     evt_queue()
113 {
114     m_parent = ctrl;
115     m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
116     cmd_pid = 0;
117     cam_sem_init(&cmd_sem, 0);
118     pthread_create(&cmd_pid,
119                    NULL,
120                    smEvtProcRoutine,
121                    this);
122 }
123 
124 /*===========================================================================
125  * FUNCTION   : ~QCameraStateMachine
126  *
127  * DESCRIPTION: desctructor of QCameraStateMachine. Will stop process thread.
128  *
129  * PARAMETERS : none
130  *
131  * RETURN     : none
132  *==========================================================================*/
~QCameraStateMachine()133 QCameraStateMachine::~QCameraStateMachine()
134 {
135     if (cmd_pid != 0) {
136         qcamera_sm_cmd_t *node =
137             (qcamera_sm_cmd_t *)malloc(sizeof(qcamera_sm_cmd_t));
138         if (NULL != node) {
139             memset(node, 0, sizeof(qcamera_sm_cmd_t));
140             node->cmd = QCAMERA_SM_CMD_TYPE_EXIT;
141 
142             api_queue.enqueue((void *)node);
143             cam_sem_post(&cmd_sem);
144 
145             /* wait until cmd thread exits */
146             if (pthread_join(cmd_pid, NULL) != 0) {
147                 CDBG_HIGH("%s: pthread dead already\n", __func__);
148             }
149         }
150         cmd_pid = 0;
151     }
152     cam_sem_destroy(&cmd_sem);
153 }
154 
155 /*===========================================================================
156  * FUNCTION   : procAPI
157  *
158  * DESCRIPTION: process incoming API request from framework layer.
159  *
160  * PARAMETERS :
161  *   @evt          : event to be processed
162  *   @api_payload  : API payload. Can be NULL if not needed.
163  *
164  * RETURN     : int32_t type of status
165  *              NO_ERROR  -- success
166  *              none-zero failure code
167  *==========================================================================*/
procAPI(qcamera_sm_evt_enum_t evt,void * api_payload)168 int32_t QCameraStateMachine::procAPI(qcamera_sm_evt_enum_t evt,
169                                      void *api_payload)
170 {
171     qcamera_sm_cmd_t *node =
172         (qcamera_sm_cmd_t *)malloc(sizeof(qcamera_sm_cmd_t));
173     if (NULL == node) {
174         ALOGE("%s: No memory for qcamera_sm_cmd_t", __func__);
175         return NO_MEMORY;
176     }
177 
178     memset(node, 0, sizeof(qcamera_sm_cmd_t));
179     node->cmd = QCAMERA_SM_CMD_TYPE_API;
180     node->evt = evt;
181     node->evt_payload = api_payload;
182     if (api_queue.enqueue((void *)node)) {
183         cam_sem_post(&cmd_sem);
184         return NO_ERROR;
185     } else {
186         free(node);
187         return UNKNOWN_ERROR;
188     }
189 }
190 
191 /*===========================================================================
192  * FUNCTION   : procEvt
193  *
194  * DESCRIPTION: process incoming envent from mm-camera-interface and
195  *              mm-jpeg-interface.
196  *
197  * PARAMETERS :
198  *   @evt          : event to be processed
199  *   @evt_payload  : event payload. Can be NULL if not needed.
200  *
201  * RETURN     : int32_t type of status
202  *              NO_ERROR  -- success
203  *              none-zero failure code
204  *==========================================================================*/
procEvt(qcamera_sm_evt_enum_t evt,void * evt_payload)205 int32_t QCameraStateMachine::procEvt(qcamera_sm_evt_enum_t evt,
206                                      void *evt_payload)
207 {
208     qcamera_sm_cmd_t *node =
209         (qcamera_sm_cmd_t *)malloc(sizeof(qcamera_sm_cmd_t));
210     if (NULL == node) {
211         ALOGE("%s: No memory for qcamera_sm_cmd_t", __func__);
212         return NO_MEMORY;
213     }
214 
215     memset(node, 0, sizeof(qcamera_sm_cmd_t));
216     node->cmd = QCAMERA_SM_CMD_TYPE_EVT;
217     node->evt = evt;
218     node->evt_payload = evt_payload;
219     if (evt_queue.enqueue((void *)node)) {
220         cam_sem_post(&cmd_sem);
221         return NO_ERROR;
222     } else {
223         free(node);
224         return UNKNOWN_ERROR;
225     }
226 }
227 
228 /*===========================================================================
229  * FUNCTION   : stateMachine
230  *
231  * DESCRIPTION: finite state machine entry function. Depends on state,
232  *              incoming event will be handled differently.
233  *
234  * PARAMETERS :
235  *   @evt      : event to be processed
236  *   @payload  : event payload. Can be NULL if not needed.
237  *
238  * RETURN     : int32_t type of status
239  *              NO_ERROR  -- success
240  *              none-zero failure code
241  *==========================================================================*/
stateMachine(qcamera_sm_evt_enum_t evt,void * payload)242 int32_t QCameraStateMachine::stateMachine(qcamera_sm_evt_enum_t evt, void *payload)
243 {
244     int32_t rc = NO_ERROR;
245     ALOGV("%s: m_state %d, event (%d)", __func__, m_state, evt);
246     switch (m_state) {
247     case QCAMERA_SM_STATE_PREVIEW_STOPPED:
248         rc = procEvtPreviewStoppedState(evt, payload);
249         break;
250     case QCAMERA_SM_STATE_PREVIEW_READY:
251         rc = procEvtPreviewReadyState(evt, payload);
252         break;
253     case QCAMERA_SM_STATE_PREVIEWING:
254         rc = procEvtPreviewingState(evt, payload);
255         break;
256     case QCAMERA_SM_STATE_PREPARE_SNAPSHOT:
257         rc = procEvtPrepareSnapshotState(evt, payload);
258         break;
259     case QCAMERA_SM_STATE_PIC_TAKING:
260         rc = procEvtPicTakingState(evt, payload);
261         break;
262     case QCAMERA_SM_STATE_RECORDING:
263         rc = procEvtRecordingState(evt, payload);
264         break;
265     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
266         rc = procEvtVideoPicTakingState(evt, payload);
267         break;
268     case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
269         rc = procEvtPreviewPicTakingState(evt, payload);
270         break;
271     default:
272         break;
273     }
274 
275     return rc;
276 }
277 
278 /*===========================================================================
279  * FUNCTION   : procEvtPreviewStoppedState
280  *
281  * DESCRIPTION: finite state machine function to handle event in state of
282  *              QCAMERA_SM_STATE_PREVIEW_STOPPED.
283  *
284  * PARAMETERS :
285  *   @evt      : event to be processed
286  *   @payload  : event payload. Can be NULL if not needed.
287  *
288  * RETURN     : int32_t type of status
289  *              NO_ERROR  -- success
290  *              none-zero failure code
291  *==========================================================================*/
procEvtPreviewStoppedState(qcamera_sm_evt_enum_t evt,void * payload)292 int32_t QCameraStateMachine::procEvtPreviewStoppedState(qcamera_sm_evt_enum_t evt,
293                                                         void *payload)
294 {
295     int32_t rc = NO_ERROR;
296     qcamera_api_result_t result;
297     memset(&result, 0, sizeof(qcamera_api_result_t));
298 
299     ALOGV("%s: event (%d)", __func__, evt);
300     switch (evt) {
301     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
302         {
303             rc = m_parent->setPreviewWindow((struct preview_stream_ops *)payload);
304             result.status = rc;
305             result.request_api = evt;
306             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
307             m_parent->signalAPIResult(&result);
308         }
309         break;
310     case QCAMERA_SM_EVT_SET_CALLBACKS:
311         {
312             qcamera_sm_evt_setcb_payload_t *setcbs =
313                 (qcamera_sm_evt_setcb_payload_t *)payload;
314             rc = m_parent->setCallBacks(setcbs->notify_cb,
315                                         setcbs->data_cb,
316                                         setcbs->data_cb_timestamp,
317                                         setcbs->get_memory,
318                                         setcbs->user);
319             result.status = rc;
320             result.request_api = evt;
321             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
322             m_parent->signalAPIResult(&result);
323         }
324         break;
325     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
326         {
327             rc = m_parent->enableMsgType(int32_t(payload));
328             result.status = rc;
329             result.request_api = evt;
330             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
331             m_parent->signalAPIResult(&result);
332         }
333         break;
334     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
335         {
336             rc = m_parent->disableMsgType(int32_t(payload));
337             result.status = rc;
338             result.request_api = evt;
339             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
340             m_parent->signalAPIResult(&result);
341         }
342         break;
343     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
344         {
345             int enabled = m_parent->msgTypeEnabled(int32_t(payload));
346             result.status = rc;
347             result.request_api = evt;
348             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
349             result.enabled = enabled;
350             m_parent->signalAPIResult(&result);
351         }
352         break;
353     case QCAMERA_SM_EVT_SET_PARAMS:
354         {
355             bool needRestart = false;
356             rc = m_parent->updateParameters((char*)payload, needRestart);
357             if (needRestart) {
358                 // Clear memory pools
359                 m_parent->m_memoryPool.clear();
360             }
361             if (rc == NO_ERROR) {
362                 rc = m_parent->commitParameterChanges();
363             }
364             result.status = rc;
365             result.request_api = evt;
366             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
367             m_parent->signalAPIResult(&result);
368         }
369         break;
370     case QCAMERA_SM_EVT_GET_PARAMS:
371         {
372             result.params = m_parent->getParameters();
373             rc = NO_ERROR;
374             result.status = rc;
375             result.request_api = evt;
376             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
377             m_parent->signalAPIResult(&result);
378         }
379         break;
380     case QCAMERA_SM_EVT_PUT_PARAMS:
381         {
382             rc = m_parent->putParameters((char*)payload);
383             result.status = rc;
384             result.request_api = evt;
385             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
386             m_parent->signalAPIResult(&result);
387         }
388         break;
389     case QCAMERA_SM_EVT_START_PREVIEW:
390         {
391             if (m_parent->mPreviewWindow == NULL) {
392                 rc = m_parent->preparePreview();
393                 if(rc == NO_ERROR) {
394                     // preview window is not set yet, move to previewReady state
395                     m_state = QCAMERA_SM_STATE_PREVIEW_READY;
396                 } else {
397                     ALOGE("%s: preparePreview failed",__func__);
398                 }
399             } else {
400                 rc = m_parent->preparePreview();
401                 if (rc == NO_ERROR) {
402                     rc = m_parent->startPreview();
403                     if (rc != NO_ERROR) {
404                         m_parent->unpreparePreview();
405                     } else {
406                         // start preview success, move to previewing state
407                         m_state = QCAMERA_SM_STATE_PREVIEWING;
408                     }
409                 }
410             }
411             result.status = rc;
412             result.request_api = evt;
413             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
414             m_parent->signalAPIResult(&result);
415         }
416         break;
417     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
418         {
419             rc = m_parent->preparePreview();
420             if (rc == NO_ERROR) {
421                 rc = m_parent->startPreview();
422                 if (rc != NO_ERROR) {
423                     m_parent->unpreparePreview();
424                 } else {
425                     m_state = QCAMERA_SM_STATE_PREVIEWING;
426                 }
427             }
428             result.status = rc;
429             result.request_api = evt;
430             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
431             m_parent->signalAPIResult(&result);
432         }
433     break;
434     case QCAMERA_SM_EVT_STOP_PREVIEW:
435         {
436             // no op needed here
437             CDBG_HIGH("%s: already in preview stopped state, do nothing", __func__);
438             result.status = NO_ERROR;
439             result.request_api = evt;
440             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
441             m_parent->signalAPIResult(&result);
442         }
443         break;
444     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
445     case QCAMERA_SM_EVT_RECORDING_ENABLED:
446         {
447             result.status = NO_ERROR;
448             result.request_api = evt;
449             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
450             result.enabled = 0;
451             m_parent->signalAPIResult(&result);
452         }
453         break;
454     case QCAMERA_SM_EVT_RELEASE:
455         {
456             rc = m_parent->release();
457             result.status = rc;
458             result.request_api = evt;
459             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
460             m_parent->signalAPIResult(&result);
461         }
462         break;
463     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
464         {
465             rc = m_parent->storeMetaDataInBuffers(int(payload));
466             result.status = rc;
467             result.request_api = evt;
468             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
469             m_parent->signalAPIResult(&result);
470         }
471         break;
472     case QCAMERA_SM_EVT_DUMP:
473         {
474             rc = m_parent->dump((int)payload);
475             result.status = rc;
476             result.request_api = evt;
477             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
478             m_parent->signalAPIResult(&result);
479         }
480         break;
481     case QCAMERA_SM_EVT_SEND_COMMAND:
482         {
483             qcamera_sm_evt_command_payload_t *cmd_payload =
484                 (qcamera_sm_evt_command_payload_t *)payload;
485             rc = m_parent->sendCommand(cmd_payload->cmd,
486                                        cmd_payload->arg1,
487                                        cmd_payload->arg2);
488             result.status = rc;
489             result.request_api = evt;
490             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
491             m_parent->signalAPIResult(&result);
492         }
493         break;
494     case QCAMERA_SM_EVT_START_RECORDING:
495     case QCAMERA_SM_EVT_STOP_RECORDING:
496     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
497     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
498     case QCAMERA_SM_EVT_TAKE_PICTURE:
499         {
500             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
501             rc = INVALID_OPERATION;
502             result.status = rc;
503             result.request_api = evt;
504             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
505             m_parent->signalAPIResult(&result);
506         }
507         break;
508     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
509     case QCAMERA_SM_EVT_CANCEL_PICTURE:
510         {
511             // no op needed here
512             CDBG_HIGH("%s: No ops for evt(%d) in state(%d)", __func__, evt, m_state);
513             result.status = NO_ERROR;
514             result.request_api = evt;
515             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
516             m_parent->signalAPIResult(&result);
517         }
518         break;
519     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
520         {
521             bool isAFRunning = m_parent->isAFRunning();
522             rc = m_parent->cancelAutoFocus();
523             if (!isAFRunning) {
524                 result.status = rc;
525                 result.request_api = evt;
526                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
527                 m_parent->signalAPIResult(&result);
528             }
529         }
530         break;
531     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
532         {
533             int32_t faceID = 0;
534             qcamera_sm_evt_reg_face_payload_t *reg_payload =
535                 (qcamera_sm_evt_reg_face_payload_t *)payload;
536             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
537                                              reg_payload->config,
538                                              faceID);
539             result.status = rc;
540             result.request_api = evt;
541             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
542             result.handle = faceID;
543             m_parent->signalAPIResult(&result);
544         }
545         break;
546     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
547         {
548             rc = m_parent->updateThermalLevel(
549                     *(qcamera_thermal_level_enum_t *)&payload);
550         }
551         break;
552     case QCAMERA_SM_EVT_EVT_NOTIFY:
553         {
554             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
555             switch (cam_evt->server_event_type) {
556             case CAM_EVENT_TYPE_DAEMON_DIED:
557                 {
558                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
559                                             CAMERA_ERROR_SERVER_DIED,
560                                             0);
561                 }
562                 break;
563             default:
564                 ALOGE("%s: Invalid internal event %d in state(%d)",
565                             __func__, cam_evt->server_event_type, m_state);
566                 break;
567             }
568         }
569         break;
570     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
571         {
572             // No ops, but need to notify
573             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
574             result.status = rc;
575             result.request_api = evt;
576             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
577             m_parent->signalEvtResult(&result);
578         }
579        break;
580     case QCAMERA_SM_EVT_EVT_INTERNAL:
581     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
582     default:
583         ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
584         break;
585     }
586 
587     return rc;
588 }
589 
590 /*===========================================================================
591  * FUNCTION   : procEvtPreviewReadyState
592  *
593  * DESCRIPTION: finite state machine function to handle event in state of
594  *              QCAMERA_SM_STATE_PREVIEW_READY.
595  *
596  * PARAMETERS :
597  *   @evt      : event to be processed
598  *   @payload  : event payload. Can be NULL if not needed.
599  *
600  * RETURN     : int32_t type of status
601  *              NO_ERROR  -- success
602  *              none-zero failure code
603  *==========================================================================*/
procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt,void * payload)604 int32_t QCameraStateMachine::procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt,
605                                                       void *payload)
606 {
607     int32_t rc = NO_ERROR;
608     qcamera_api_result_t result;
609     memset(&result, 0, sizeof(qcamera_api_result_t));
610 
611     ALOGV("%s: event (%d)", __func__, evt);
612     switch (evt) {
613     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
614         {
615             m_parent->setPreviewWindow((struct preview_stream_ops *)payload);
616             if (m_parent->mPreviewWindow != NULL) {
617                 rc = m_parent->startPreview();
618                 if (rc != NO_ERROR) {
619                     m_parent->unpreparePreview();
620                     m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
621                 } else {
622                     m_state = QCAMERA_SM_STATE_PREVIEWING;
623                 }
624             }
625 
626             result.status = rc;
627             result.request_api = evt;
628             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
629             m_parent->signalAPIResult(&result);
630         }
631         break;
632     case QCAMERA_SM_EVT_SET_CALLBACKS:
633         {
634             qcamera_sm_evt_setcb_payload_t *setcbs =
635                 (qcamera_sm_evt_setcb_payload_t *)payload;
636             rc = m_parent->setCallBacks(setcbs->notify_cb,
637                                         setcbs->data_cb,
638                                         setcbs->data_cb_timestamp,
639                                         setcbs->get_memory,
640                                         setcbs->user);
641             result.status = rc;
642             result.request_api = evt;
643             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
644             m_parent->signalAPIResult(&result);
645         }
646         break;
647     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
648         {
649             rc = m_parent->enableMsgType(int32_t(payload));
650             result.status = rc;
651             result.request_api = evt;
652             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
653             m_parent->signalAPIResult(&result);
654         }
655         break;
656     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
657         {
658             rc = m_parent->disableMsgType(int32_t(payload));
659             result.status = rc;
660             result.request_api = evt;
661             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
662             m_parent->signalAPIResult(&result);
663         }
664         break;
665     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
666         {
667             int enabled = m_parent->msgTypeEnabled(int32_t(payload));
668             result.status = rc;
669             result.request_api = evt;
670             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
671             result.enabled = enabled;
672             m_parent->signalAPIResult(&result);
673         }
674         break;
675     case QCAMERA_SM_EVT_SET_PARAMS:
676         {
677             bool needRestart = false;
678             rc = m_parent->updateParameters((char*)payload, needRestart);
679             if (rc == NO_ERROR) {
680                 if (needRestart) {
681                     // need restart preview for parameters to take effect
682                     m_parent->unpreparePreview();
683                     // Clear memory pools
684                     m_parent->m_memoryPool.clear();
685                     // commit parameter changes to server
686                     m_parent->commitParameterChanges();
687                     // prepare preview again
688                     rc = m_parent->preparePreview();
689                     if (rc != NO_ERROR) {
690                         m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
691                     }
692                 } else {
693                     rc = m_parent->commitParameterChanges();
694                 }
695             }
696 
697             result.status = rc;
698             result.request_api = evt;
699             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
700             m_parent->signalAPIResult(&result);
701         }
702         break;
703     case QCAMERA_SM_EVT_GET_PARAMS:
704         {
705             result.params = m_parent->getParameters();
706             rc = NO_ERROR;
707             result.status = rc;
708             result.request_api = evt;
709             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
710             m_parent->signalAPIResult(&result);
711         }
712         break;
713     case QCAMERA_SM_EVT_PUT_PARAMS:
714         {
715             rc = m_parent->putParameters((char*)payload);
716             result.status = rc;
717             result.request_api = evt;
718             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
719             m_parent->signalAPIResult(&result);
720         }
721         break;
722     case QCAMERA_SM_EVT_START_PREVIEW:
723         {
724             // no ops here
725             rc = NO_ERROR;
726             result.status = rc;
727             result.request_api = evt;
728             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
729             m_parent->signalAPIResult(&result);
730         }
731         break;
732     case QCAMERA_SM_EVT_STOP_PREVIEW:
733         {
734             m_parent->unpreparePreview();
735             rc = 0;
736             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
737             result.status = rc;
738             result.request_api = evt;
739             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
740             m_parent->signalAPIResult(&result);
741         }
742         break;
743     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
744         {
745             rc = NO_ERROR;
746             result.status = rc;
747             result.request_api = evt;
748             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
749             result.enabled = 1;
750             m_parent->signalAPIResult(&result);
751         }
752         break;
753     case QCAMERA_SM_EVT_RECORDING_ENABLED:
754         {
755             rc = 0;
756             result.status = rc;
757             result.request_api = evt;
758             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
759             result.enabled = 0;
760             m_parent->signalAPIResult(&result);
761         }
762         break;
763     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
764         {
765             rc = m_parent->storeMetaDataInBuffers(int(payload));
766             result.status = rc;
767             result.request_api = evt;
768             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
769             m_parent->signalAPIResult(&result);
770         }
771         break;
772     case QCAMERA_SM_EVT_DUMP:
773         {
774             rc = m_parent->dump((int)payload);
775             result.status = rc;
776             result.request_api = evt;
777             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
778             m_parent->signalAPIResult(&result);
779         }
780         break;
781     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
782         {
783             rc = m_parent->autoFocus();
784             result.status = rc;
785             result.request_api = evt;
786             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
787             m_parent->signalAPIResult(&result);
788         }
789         break;
790     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
791         {
792             bool isAFRunning = m_parent->isAFRunning();
793             rc = m_parent->cancelAutoFocus();
794             if (!isAFRunning) {
795                 result.status = rc;
796                 result.request_api = evt;
797                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
798                 m_parent->signalAPIResult(&result);
799             }
800         }
801         break;
802     case QCAMERA_SM_EVT_SEND_COMMAND:
803         {
804             qcamera_sm_evt_command_payload_t *cmd_payload =
805                 (qcamera_sm_evt_command_payload_t *)payload;
806             rc = m_parent->sendCommand(cmd_payload->cmd,
807                                        cmd_payload->arg1,
808                                        cmd_payload->arg2);
809             result.status = rc;
810             result.request_api = evt;
811             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
812             m_parent->signalAPIResult(&result);
813         }
814         break;
815     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
816         {
817             int32_t faceID = 0;
818             qcamera_sm_evt_reg_face_payload_t *reg_payload =
819                 (qcamera_sm_evt_reg_face_payload_t *)payload;
820             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
821                                              reg_payload->config,
822                                              faceID);
823             result.status = rc;
824             result.request_api = evt;
825             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
826             result.handle = faceID;
827             m_parent->signalAPIResult(&result);
828         }
829         break;
830     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
831     case QCAMERA_SM_EVT_START_RECORDING:
832     case QCAMERA_SM_EVT_STOP_RECORDING:
833     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
834     case QCAMERA_SM_EVT_TAKE_PICTURE:
835     case QCAMERA_SM_EVT_CANCEL_PICTURE:
836     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
837     case QCAMERA_SM_EVT_RELEASE:
838         {
839             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
840             rc = INVALID_OPERATION;
841             result.status = rc;
842             result.request_api = evt;
843             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
844             m_parent->signalAPIResult(&result);
845         }
846         break;
847     case QCAMERA_SM_EVT_EVT_NOTIFY:
848         {
849             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
850             switch (cam_evt->server_event_type) {
851             case CAM_EVENT_TYPE_DAEMON_DIED:
852                 {
853                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
854                                             CAMERA_ERROR_SERVER_DIED,
855                                             0);
856                 }
857                 break;
858             default:
859                 ALOGE("%s: Invalid internal event %d in state(%d)",
860                             __func__, cam_evt->server_event_type, m_state);
861                 break;
862             }
863         }
864         break;
865     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
866         {
867             // No ops, but need to notify
868             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
869             result.status = rc;
870             result.request_api = evt;
871             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
872             m_parent->signalEvtResult(&result);
873         }
874        break;
875     case QCAMERA_SM_EVT_EVT_INTERNAL:
876     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
877     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
878     default:
879         ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
880         break;
881     }
882 
883     return rc;
884 }
885 
886 /*===========================================================================
887  * FUNCTION   : procEvtPreviewingState
888  *
889  * DESCRIPTION: finite state machine function to handle event in state of
890  *              QCAMERA_SM_STATE_PREVIEWING.
891  *
892  * PARAMETERS :
893  *   @evt      : event to be processed
894  *   @payload  : event payload. Can be NULL if not needed.
895  *
896  * RETURN     : int32_t type of status
897  *              NO_ERROR  -- success
898  *              none-zero failure code
899  *==========================================================================*/
procEvtPreviewingState(qcamera_sm_evt_enum_t evt,void * payload)900 int32_t QCameraStateMachine::procEvtPreviewingState(qcamera_sm_evt_enum_t evt,
901                                                     void *payload)
902 {
903     int32_t rc = NO_ERROR;
904     qcamera_api_result_t result;
905     memset(&result, 0, sizeof(qcamera_api_result_t));
906 
907     ALOGV("%s: event (%d)", __func__, evt);
908     switch (evt) {
909     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
910         {
911             // Error setting preview window during previewing
912             ALOGE("Cannot set preview window when preview is running");
913             rc = INVALID_OPERATION;
914             result.status = rc;
915             result.request_api = evt;
916             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
917             m_parent->signalAPIResult(&result);
918         }
919         break;
920     case QCAMERA_SM_EVT_SET_CALLBACKS:
921         {
922             qcamera_sm_evt_setcb_payload_t *setcbs =
923                 (qcamera_sm_evt_setcb_payload_t *)payload;
924             rc = m_parent->setCallBacks(setcbs->notify_cb,
925                                         setcbs->data_cb,
926                                         setcbs->data_cb_timestamp,
927                                         setcbs->get_memory,
928                                         setcbs->user);
929             result.status = rc;
930             result.request_api = evt;
931             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
932             m_parent->signalAPIResult(&result);
933         }
934         break;
935     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
936         {
937             rc = m_parent->enableMsgType(int32_t(payload));
938             result.status = rc;
939             result.request_api = evt;
940             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
941             m_parent->signalAPIResult(&result);
942         }
943         break;
944     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
945         {
946             rc = m_parent->disableMsgType(int32_t(payload));
947             result.status = rc;
948             result.request_api = evt;
949             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
950             m_parent->signalAPIResult(&result);
951         }
952         break;
953     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
954         {
955             int enabled = m_parent->msgTypeEnabled(int32_t(payload));
956             result.status = rc;
957             result.request_api = evt;
958             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
959             result.enabled = enabled;
960             m_parent->signalAPIResult(&result);
961         }
962         break;
963     case QCAMERA_SM_EVT_SET_PARAMS:
964         {
965             bool needRestart = false;
966             rc = m_parent->updateParameters((char*)payload, needRestart);
967             if (rc == NO_ERROR) {
968                 if (needRestart) {
969                     // need restart preview for parameters to take effect
970                     // stop preview
971                     m_parent->stopPreview();
972                     // Clear memory pools
973                     m_parent->m_memoryPool.clear();
974                     // commit parameter changes to server
975                     m_parent->commitParameterChanges();
976                     // start preview again
977                     rc = m_parent->preparePreview();
978                     if (rc == NO_ERROR) {
979                         rc = m_parent->startPreview();
980                         if (rc != NO_ERROR) {
981                             m_parent->unpreparePreview();
982                         }
983                     }
984                     if (rc != NO_ERROR) {
985                         m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
986                     }
987                 } else {
988                     rc = m_parent->commitParameterChanges();
989                 }
990             }
991             result.status = rc;
992             result.request_api = evt;
993             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
994             m_parent->signalAPIResult(&result);
995         }
996         break;
997     case QCAMERA_SM_EVT_GET_PARAMS:
998         {
999             result.params = m_parent->getParameters();
1000             rc = NO_ERROR;
1001             result.status = rc;
1002             result.request_api = evt;
1003             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1004             m_parent->signalAPIResult(&result);
1005         }
1006         break;
1007     case QCAMERA_SM_EVT_PUT_PARAMS:
1008         {
1009             rc = m_parent->putParameters((char*)payload);
1010             result.status = rc;
1011             result.request_api = evt;
1012             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1013             m_parent->signalAPIResult(&result);
1014         }
1015         break;
1016     case QCAMERA_SM_EVT_START_PREVIEW:
1017     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1018         {
1019             // no ops here
1020             CDBG_HIGH("%s: Already in previewing, no ops here to start preview", __func__);
1021             rc = NO_ERROR;
1022             result.status = rc;
1023             result.request_api = evt;
1024             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1025             m_parent->signalAPIResult(&result);
1026         }
1027         break;
1028     case QCAMERA_SM_EVT_STOP_PREVIEW:
1029         {
1030             rc = m_parent->stopPreview();
1031             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1032             result.status = rc;
1033             result.request_api = evt;
1034             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1035             m_parent->signalAPIResult(&result);
1036         }
1037         break;
1038     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1039         {
1040             rc = NO_ERROR;
1041             result.status = rc;
1042             result.request_api = evt;
1043             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1044             result.enabled = 1;
1045             m_parent->signalAPIResult(&result);
1046         }
1047         break;
1048     case QCAMERA_SM_EVT_RECORDING_ENABLED:
1049         {
1050             rc = NO_ERROR;
1051             result.status = rc;
1052             result.request_api = evt;
1053             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1054             result.enabled = 0;
1055             m_parent->signalAPIResult(&result);
1056         }
1057         break;
1058     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1059         {
1060             rc = m_parent->storeMetaDataInBuffers(int(payload));
1061             result.status = rc;
1062             result.request_api = evt;
1063             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1064             m_parent->signalAPIResult(&result);
1065         }
1066         break;
1067     case QCAMERA_SM_EVT_DUMP:
1068         {
1069             rc = m_parent->dump((int)payload);
1070             result.status = rc;
1071             result.request_api = evt;
1072             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1073             m_parent->signalAPIResult(&result);
1074         }
1075         break;
1076     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1077         {
1078             rc = m_parent->autoFocus();
1079             result.status = rc;
1080             result.request_api = evt;
1081             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1082             m_parent->signalAPIResult(&result);
1083         }
1084         break;
1085     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1086         {
1087             bool isAFRunning = m_parent->isAFRunning();
1088             rc = m_parent->cancelAutoFocus();
1089             if (!isAFRunning) {
1090                 result.status = rc;
1091                 result.request_api = evt;
1092                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1093                 m_parent->signalAPIResult(&result);
1094             }
1095         }
1096         break;
1097     case QCAMERA_SM_EVT_START_RECORDING:
1098         {
1099             rc = m_parent->startRecording();
1100             if (rc == NO_ERROR) {
1101                 // move state to recording state
1102                 m_state = QCAMERA_SM_STATE_RECORDING;
1103             }
1104             result.status = rc;
1105             result.request_api = evt;
1106             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1107             m_parent->signalAPIResult(&result);
1108         }
1109         break;
1110     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
1111         {
1112             rc = m_parent->prepareHardwareForSnapshot(FALSE);
1113             if (rc == NO_ERROR) {
1114                 // Do not signal API result in this case.
1115                 // Need to wait for snapshot done in metadta.
1116                 m_state = QCAMERA_SM_STATE_PREPARE_SNAPSHOT;
1117             } else {
1118                 // Do not change state in this case.
1119                 ALOGE("%s: prepareHardwareForSnapshot failed %d",
1120                     __func__, rc);
1121 
1122                 result.status = rc;
1123                 result.request_api = evt;
1124                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1125                 m_parent->signalAPIResult(&result);
1126             }
1127         }
1128         break;
1129     case QCAMERA_SM_EVT_TAKE_PICTURE:
1130        {
1131 
1132            ALOGV("%s: QCAMERA_SM_EVT_TAKE_PICTURE ", __func__);
1133            if ( m_parent->mParameters.getRecordingHintValue() == false) {
1134                if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
1135                    m_state = QCAMERA_SM_STATE_PREVIEW_PIC_TAKING;
1136                    rc = m_parent->takePicture();
1137                    if (rc != NO_ERROR) {
1138                        // move state to previewing state
1139                        m_state = QCAMERA_SM_STATE_PREVIEWING;
1140                    }
1141                    if (!(m_parent->isRetroPicture()) || (rc != NO_ERROR)) {
1142                        ALOGD("%s: signal API result, m_state = %d",
1143                              __func__, m_state);
1144                        result.status = rc;
1145                        result.request_api = evt;
1146                        result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1147                        m_parent->signalAPIResult(&result);
1148                    }
1149                } else {
1150                    m_state = QCAMERA_SM_STATE_PIC_TAKING;
1151                    rc = m_parent->takePicture();
1152                    if (rc != NO_ERROR) {
1153                        // move state to preview stopped state
1154                        m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1155                    }
1156 
1157                    result.status = rc;
1158                    result.request_api = evt;
1159                    result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1160                    m_parent->signalAPIResult(&result);
1161                }
1162            } else {
1163                m_state = QCAMERA_SM_STATE_PREVIEW_PIC_TAKING;
1164                rc = m_parent->takeLiveSnapshot();
1165                if (rc != NO_ERROR ) {
1166                    m_state = QCAMERA_SM_STATE_PREVIEWING;
1167                }
1168                result.status = rc;
1169                result.request_api = evt;
1170                result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1171                m_parent->signalAPIResult(&result);
1172            }
1173         }
1174         break;
1175     case QCAMERA_SM_EVT_SEND_COMMAND:
1176         {
1177             qcamera_sm_evt_command_payload_t *cmd_payload =
1178                 (qcamera_sm_evt_command_payload_t *)payload;
1179             rc = m_parent->sendCommand(cmd_payload->cmd,
1180                                        cmd_payload->arg1,
1181                                        cmd_payload->arg2);
1182             result.status = rc;
1183             result.request_api = evt;
1184             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1185             m_parent->signalAPIResult(&result);
1186         }
1187         break;
1188     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
1189         {
1190             int32_t faceID = 0;
1191             qcamera_sm_evt_reg_face_payload_t *reg_payload =
1192                 (qcamera_sm_evt_reg_face_payload_t *)payload;
1193             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
1194                                              reg_payload->config,
1195                                              faceID);
1196             result.status = rc;
1197             result.request_api = evt;
1198             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
1199             result.handle = faceID;
1200             m_parent->signalAPIResult(&result);
1201         }
1202         break;
1203     case QCAMERA_SM_EVT_CANCEL_PICTURE:
1204     case QCAMERA_SM_EVT_STOP_RECORDING:
1205     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1206     case QCAMERA_SM_EVT_RELEASE:
1207         {
1208             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1209             rc = INVALID_OPERATION;
1210             result.status = rc;
1211             result.request_api = evt;
1212             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1213             m_parent->signalAPIResult(&result);
1214         }
1215         break;
1216     case QCAMERA_SM_EVT_EVT_INTERNAL:
1217         {
1218             qcamera_sm_internal_evt_payload_t *internal_evt =
1219                 (qcamera_sm_internal_evt_payload_t *)payload;
1220             switch (internal_evt->evt_type) {
1221             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1222                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1223                 break;
1224             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
1225                 break;
1226             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
1227                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
1228                 break;
1229             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
1230                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
1231                 break;
1232             case QCAMERA_INTERNAL_EVT_CROP_INFO:
1233                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
1234                 break;
1235             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
1236                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
1237                 break;
1238             default:
1239                 ALOGE("%s: Invalid internal event %d in state(%d)",
1240                             __func__, internal_evt->evt_type, m_state);
1241                 break;
1242             }
1243         }
1244         break;
1245     case QCAMERA_SM_EVT_EVT_NOTIFY:
1246         {
1247             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1248             switch (cam_evt->server_event_type) {
1249             case CAM_EVENT_TYPE_DAEMON_DIED:
1250                 {
1251                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
1252                                             CAMERA_ERROR_SERVER_DIED,
1253                                             0);
1254                 }
1255                 break;
1256             default:
1257                 CDBG_HIGH("%s: no handling for server evt (%d) at this state",
1258                       __func__, cam_evt->server_event_type);
1259                 break;
1260             }
1261         }
1262         break;
1263     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1264         {
1265             rc = m_parent->updateThermalLevel(
1266                     *(qcamera_thermal_level_enum_t *)&payload);
1267         }
1268         break;
1269     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1270         {
1271             // No ops, but need to notify
1272             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1273             result.status = rc;
1274             result.request_api = evt;
1275             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1276             m_parent->signalEvtResult(&result);
1277         }
1278        break;
1279     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1280     default:
1281         ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1282         break;
1283     }
1284 
1285     return rc;
1286 }
1287 
1288 /*===========================================================================
1289  * FUNCTION   : procEvtPrepareSnapshotState
1290  *
1291  * DESCRIPTION: finite state machine function to handle event in state of
1292  *              QCAMERA_SM_STATE_PREPARE_SNAPSHOT.
1293  *
1294  * PARAMETERS :
1295  *   @evt      : event to be processed
1296  *   @payload  : event payload. Can be NULL if not needed.
1297  *
1298  * RETURN     : int32_t type of status
1299  *              NO_ERROR  -- success
1300  *              none-zero failure code
1301  *==========================================================================*/
procEvtPrepareSnapshotState(qcamera_sm_evt_enum_t evt,void * payload)1302 int32_t QCameraStateMachine::procEvtPrepareSnapshotState(qcamera_sm_evt_enum_t evt,
1303                                                     void *payload)
1304 {
1305     int32_t rc = NO_ERROR;
1306     qcamera_api_result_t result;
1307     memset(&result, 0, sizeof(qcamera_api_result_t));
1308 
1309     ALOGV("%s: event (%d)", __func__, evt);
1310     switch (evt) {
1311     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1312     case QCAMERA_SM_EVT_SET_CALLBACKS:
1313     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1314     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1315     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1316     case QCAMERA_SM_EVT_SET_PARAMS:
1317     case QCAMERA_SM_EVT_GET_PARAMS:
1318     case QCAMERA_SM_EVT_PUT_PARAMS:
1319     case QCAMERA_SM_EVT_START_PREVIEW:
1320     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1321     case QCAMERA_SM_EVT_STOP_PREVIEW:
1322     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1323     case QCAMERA_SM_EVT_RECORDING_ENABLED:
1324     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1325     case QCAMERA_SM_EVT_DUMP:
1326     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1327     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1328     case QCAMERA_SM_EVT_START_RECORDING:
1329     case QCAMERA_SM_EVT_TAKE_PICTURE:
1330     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
1331     case QCAMERA_SM_EVT_SEND_COMMAND:
1332     case QCAMERA_SM_EVT_CANCEL_PICTURE:
1333     case QCAMERA_SM_EVT_STOP_RECORDING:
1334     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1335     case QCAMERA_SM_EVT_RELEASE:
1336         {
1337             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1338             rc = INVALID_OPERATION;
1339             result.status = rc;
1340             result.request_api = evt;
1341             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1342             m_parent->signalAPIResult(&result);
1343         }
1344         break;
1345     case QCAMERA_SM_EVT_EVT_INTERNAL:
1346         {
1347             qcamera_sm_internal_evt_payload_t *internal_evt =
1348                 (qcamera_sm_internal_evt_payload_t *)payload;
1349             switch (internal_evt->evt_type) {
1350             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1351                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1352                 break;
1353             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
1354                 CDBG("%s: Received QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE event",
1355                     __func__);
1356                 m_parent->processPrepSnapshotDoneEvent(internal_evt->prep_snapshot_state);
1357                 m_state = QCAMERA_SM_STATE_PREVIEWING;
1358 
1359                 result.status = NO_ERROR;
1360                 result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
1361                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1362                 m_parent->signalAPIResult(&result);
1363                 break;
1364             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
1365                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
1366                 break;
1367             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
1368                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
1369                 break;
1370             case QCAMERA_INTERNAL_EVT_CROP_INFO:
1371                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
1372                 break;
1373             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
1374                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
1375                 break;
1376             default:
1377                 ALOGE("%s: Invalid internal event %d in state(%d)",
1378                             __func__, internal_evt->evt_type, m_state);
1379                 break;
1380             }
1381         }
1382         break;
1383     case QCAMERA_SM_EVT_EVT_NOTIFY:
1384         {
1385             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1386             switch (cam_evt->server_event_type) {
1387             case CAM_EVENT_TYPE_DAEMON_DIED:
1388                 {
1389                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
1390                                             CAMERA_ERROR_SERVER_DIED,
1391                                             0);
1392                 }
1393                 break;
1394             default:
1395                 ALOGE("%s: Invalid internal event %d in state(%d)",
1396                             __func__, cam_evt->server_event_type, m_state);
1397                 break;
1398             }
1399         }
1400         break;
1401     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1402         {
1403             // No ops, but need to notify
1404             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1405             result.status = rc;
1406             result.request_api = evt;
1407             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1408             m_parent->signalEvtResult(&result);
1409         }
1410        break;
1411     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1412     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1413     default:
1414         ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1415         break;
1416     }
1417 
1418     return rc;
1419 }
1420 
1421 /*===========================================================================
1422  * FUNCTION   : procEvtPicTakingState
1423  *
1424  * DESCRIPTION: finite state machine function to handle event in state of
1425  *              QCAMERA_SM_STATE_PIC_TAKING.
1426  *
1427  * PARAMETERS :
1428  *   @evt      : event to be processed
1429  *   @payload  : event payload. Can be NULL if not needed.
1430  *
1431  * RETURN     : int32_t type of status
1432  *              NO_ERROR  -- success
1433  *              none-zero failure code
1434  *==========================================================================*/
procEvtPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)1435 int32_t QCameraStateMachine::procEvtPicTakingState(qcamera_sm_evt_enum_t evt,
1436                                                    void *payload)
1437 {
1438     int32_t rc = NO_ERROR;
1439     qcamera_api_result_t result;
1440     memset(&result, 0, sizeof(qcamera_api_result_t));
1441 
1442     ALOGV("%s: event (%d)", __func__, evt);
1443     switch (evt) {
1444     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1445         {
1446             // Error setting preview window during previewing
1447             ALOGE("Cannot set preview window when preview is running");
1448             rc = INVALID_OPERATION;
1449             result.status = rc;
1450             result.request_api = evt;
1451             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1452             m_parent->signalAPIResult(&result);
1453         }
1454         break;
1455     case QCAMERA_SM_EVT_SET_CALLBACKS:
1456         {
1457             qcamera_sm_evt_setcb_payload_t *setcbs =
1458                 (qcamera_sm_evt_setcb_payload_t *)payload;
1459             rc = m_parent->setCallBacks(setcbs->notify_cb,
1460                                         setcbs->data_cb,
1461                                         setcbs->data_cb_timestamp,
1462                                         setcbs->get_memory,
1463                                         setcbs->user);
1464             result.status = rc;
1465             result.request_api = evt;
1466             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1467             m_parent->signalAPIResult(&result);
1468         }
1469         break;
1470     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1471         {
1472             rc = m_parent->enableMsgType(int32_t(payload));
1473             result.status = rc;
1474             result.request_api = evt;
1475             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1476             m_parent->signalAPIResult(&result);
1477         }
1478         break;
1479     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1480         {
1481             rc = m_parent->disableMsgType(int32_t(payload));
1482             result.status = rc;
1483             result.request_api = evt;
1484             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1485             m_parent->signalAPIResult(&result);
1486         }
1487         break;
1488     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1489         {
1490             int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1491             result.status = rc;
1492             result.request_api = evt;
1493             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1494             result.enabled = enabled;
1495             m_parent->signalAPIResult(&result);
1496         }
1497         break;
1498     case QCAMERA_SM_EVT_SET_PARAMS:
1499         {
1500             bool needRestart = false;
1501             rc = m_parent->updateParameters((char*)payload, needRestart);
1502             if (rc == NO_ERROR) {
1503                 rc = m_parent->commitParameterChanges();
1504             }
1505             result.status = rc;
1506             result.request_api = evt;
1507             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1508             m_parent->signalAPIResult(&result);
1509         }
1510         break;
1511     case QCAMERA_SM_EVT_GET_PARAMS:
1512         {
1513             result.params = m_parent->getParameters();
1514             rc = NO_ERROR;
1515             result.status = rc;
1516             result.request_api = evt;
1517             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1518             m_parent->signalAPIResult(&result);
1519         }
1520         break;
1521     case QCAMERA_SM_EVT_PUT_PARAMS:
1522         {
1523             rc = m_parent->putParameters((char*)payload);
1524             result.status = rc;
1525             result.request_api = evt;
1526             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1527             m_parent->signalAPIResult(&result);
1528         }
1529         break;
1530     case QCAMERA_SM_EVT_STOP_PREVIEW:
1531         {
1532             // cancel picture first
1533             rc = m_parent->cancelPicture();
1534             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1535 
1536             result.status = rc;
1537             result.request_api = evt;
1538             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1539             m_parent->signalAPIResult(&result);
1540         }
1541         break;
1542     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1543         {
1544             rc = NO_ERROR;
1545             result.status = rc;
1546             result.request_api = evt;
1547             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1548             result.enabled = 0;
1549             m_parent->signalAPIResult(&result);
1550         }
1551         break;
1552     case QCAMERA_SM_EVT_RECORDING_ENABLED:
1553         {
1554             rc = NO_ERROR;
1555             result.status = rc;
1556             result.request_api = evt;
1557             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1558             result.enabled = 0;
1559             m_parent->signalAPIResult(&result);
1560         }
1561         break;
1562     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1563         {
1564             rc = m_parent->storeMetaDataInBuffers(int(payload));
1565             result.status = rc;
1566             result.request_api = evt;
1567             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1568             m_parent->signalAPIResult(&result);
1569         }
1570         break;
1571     case QCAMERA_SM_EVT_DUMP:
1572         {
1573             rc = m_parent->dump((int)payload);
1574             result.status = rc;
1575             result.request_api = evt;
1576             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1577             m_parent->signalAPIResult(&result);
1578         }
1579         break;
1580     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1581         {
1582             rc = m_parent->autoFocus();
1583             result.status = rc;
1584             result.request_api = evt;
1585             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1586             m_parent->signalAPIResult(&result);
1587         }
1588         break;
1589     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1590         {
1591             bool isAFRunning = m_parent->isAFRunning();
1592             rc = m_parent->cancelAutoFocus();
1593             if (!isAFRunning) {
1594                 result.status = rc;
1595                 result.request_api = evt;
1596                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1597                 m_parent->signalAPIResult(&result);
1598             }
1599         }
1600         break;
1601     case QCAMERA_SM_EVT_SEND_COMMAND:
1602         {
1603             qcamera_sm_evt_command_payload_t *cmd_payload =
1604                 (qcamera_sm_evt_command_payload_t *)payload;
1605             rc = m_parent->sendCommand(cmd_payload->cmd,
1606                                        cmd_payload->arg1,
1607                                        cmd_payload->arg2);
1608 #ifndef VANILLA_HAL
1609             if ( CAMERA_CMD_LONGSHOT_OFF == cmd_payload->cmd ) {
1610                 // move state to previewing state
1611                 m_state = QCAMERA_SM_STATE_PREVIEWING;
1612             }
1613 #endif
1614             result.status = rc;
1615             result.request_api = evt;
1616             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1617             m_parent->signalAPIResult(&result);
1618         }
1619         break;
1620     case QCAMERA_SM_EVT_CANCEL_PICTURE:
1621         {
1622             rc = m_parent->cancelPicture();
1623             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1624             result.status = rc;
1625             result.request_api = evt;
1626             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1627             m_parent->signalAPIResult(&result);
1628         }
1629         break;
1630     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
1631         {
1632             int32_t faceID = 0;
1633             qcamera_sm_evt_reg_face_payload_t *reg_payload =
1634                 (qcamera_sm_evt_reg_face_payload_t *)payload;
1635             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
1636                                              reg_payload->config,
1637                                              faceID);
1638             result.status = rc;
1639             result.request_api = evt;
1640             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
1641             result.handle = faceID;
1642             m_parent->signalAPIResult(&result);
1643         }
1644         break;
1645     case QCAMERA_SM_EVT_TAKE_PICTURE:
1646         {
1647            if ( m_parent->isLongshotEnabled() ) {
1648                rc = m_parent->longShot();
1649             } else {
1650                 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1651                 rc = INVALID_OPERATION;
1652             }
1653 
1654             result.status = rc;
1655             result.request_api = evt;
1656             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1657             m_parent->signalAPIResult(&result);
1658         }
1659         break;
1660     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
1661     case QCAMERA_SM_EVT_START_RECORDING:
1662     case QCAMERA_SM_EVT_STOP_RECORDING:
1663     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1664     case QCAMERA_SM_EVT_START_PREVIEW:
1665     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1666     case QCAMERA_SM_EVT_RELEASE:
1667         {
1668             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1669             rc = INVALID_OPERATION;
1670             result.status = rc;
1671             result.request_api = evt;
1672             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1673             m_parent->signalAPIResult(&result);
1674         }
1675         break;
1676     case QCAMERA_SM_EVT_EVT_INTERNAL:
1677         {
1678             qcamera_sm_internal_evt_payload_t *internal_evt =
1679                 (qcamera_sm_internal_evt_payload_t *)payload;
1680             switch (internal_evt->evt_type) {
1681             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1682                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1683                 break;
1684             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
1685                 break;
1686             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
1687                 break;
1688             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
1689                 break;
1690             case QCAMERA_INTERNAL_EVT_CROP_INFO:
1691                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
1692                 break;
1693             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
1694                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
1695                 break;
1696             default:
1697                 break;
1698             }
1699         }
1700         break;
1701     case QCAMERA_SM_EVT_EVT_NOTIFY:
1702         {
1703             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1704             switch (cam_evt->server_event_type) {
1705             case CAM_EVENT_TYPE_DAEMON_DIED:
1706                 {
1707                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
1708                                             CAMERA_ERROR_SERVER_DIED,
1709                                             0);
1710                 }
1711                 break;
1712             default:
1713                 CDBG_HIGH("%s: no handling for server evt (%d) at this state",
1714                       __func__, cam_evt->server_event_type);
1715                 break;
1716             }
1717         }
1718         break;
1719     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1720         {
1721             qcamera_jpeg_evt_payload_t *jpeg_job =
1722                 (qcamera_jpeg_evt_payload_t *)payload;
1723             rc = m_parent->processJpegNotify(jpeg_job);
1724         }
1725         break;
1726     case QCAMERA_SM_EVT_STOP_CAPTURE_CHANNEL:
1727         {
1728             bool restartPreview = m_parent->isPreviewRestartEnabled();
1729             rc = m_parent->stopCaptureChannel(restartPreview);
1730 
1731             if (restartPreview && (NO_ERROR == rc)) {
1732                 rc = m_parent->preparePreview();
1733                 if (NO_ERROR == rc) {
1734                     m_parent->m_bPreviewStarted = true;
1735                     rc = m_parent->startPreview();
1736                 }
1737             }
1738 
1739             result.status = rc;
1740             result.request_api = evt;
1741             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1742             m_parent->signalAPIResult(&result);
1743         }
1744         break;
1745     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1746         {
1747             rc = m_parent->cancelPicture();
1748 
1749             bool restartPreview = m_parent->isPreviewRestartEnabled();
1750             if (restartPreview) {
1751                 m_state = QCAMERA_SM_STATE_PREVIEWING;
1752             } else {
1753                 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1754             }
1755 
1756             result.status = rc;
1757             result.request_api = evt;
1758             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1759             m_parent->signalEvtResult(&result);
1760         }
1761         break;
1762     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1763         {
1764             rc = m_parent->updateThermalLevel(
1765                     *(qcamera_thermal_level_enum_t *)&payload);
1766         }
1767         break;
1768     default:
1769         ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1770         break;
1771     }
1772 
1773     return rc;
1774 }
1775 
1776 /*===========================================================================
1777  * FUNCTION   : procEvtRecordingState
1778  *
1779  * DESCRIPTION: finite state machine function to handle event in state of
1780  *              QCAMERA_SM_STATE_RECORDING.
1781  *
1782  * PARAMETERS :
1783  *   @evt      : event to be processed
1784  *   @payload  : event payload. Can be NULL if not needed.
1785  *
1786  * RETURN     : int32_t type of status
1787  *              NO_ERROR  -- success
1788  *              none-zero failure code
1789  *==========================================================================*/
procEvtRecordingState(qcamera_sm_evt_enum_t evt,void * payload)1790 int32_t QCameraStateMachine::procEvtRecordingState(qcamera_sm_evt_enum_t evt,
1791                                                    void *payload)
1792 {
1793     int32_t rc = NO_ERROR;
1794     qcamera_api_result_t result;
1795     memset(&result, 0, sizeof(qcamera_api_result_t));
1796 
1797     ALOGV("%s: event (%d)", __func__, evt);
1798     switch (evt) {
1799     case QCAMERA_SM_EVT_START_PREVIEW:
1800     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1801         {
1802             // WA: CTS test VideoSnapshot will try to
1803             //     start preview during video recording.
1804             CDBG_HIGH("CTS video restart op");
1805             rc = NO_ERROR;
1806             result.status = rc;
1807             result.request_api = evt;
1808             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1809             m_parent->signalAPIResult(&result);
1810         }
1811         break;
1812     case QCAMERA_SM_EVT_SET_CALLBACKS:
1813         {
1814             qcamera_sm_evt_setcb_payload_t *setcbs =
1815                 (qcamera_sm_evt_setcb_payload_t *)payload;
1816             rc = m_parent->setCallBacks(setcbs->notify_cb,
1817                                         setcbs->data_cb,
1818                                         setcbs->data_cb_timestamp,
1819                                         setcbs->get_memory,
1820                                         setcbs->user);
1821             result.status = rc;
1822             result.request_api = evt;
1823             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1824             m_parent->signalAPIResult(&result);
1825         }
1826         break;
1827     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1828         {
1829             rc = m_parent->enableMsgType(int32_t(payload));
1830             result.status = rc;
1831             result.request_api = evt;
1832             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1833             m_parent->signalAPIResult(&result);
1834         }
1835         break;
1836     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1837         {
1838             rc = m_parent->disableMsgType(int32_t(payload));
1839             result.status = rc;
1840             result.request_api = evt;
1841             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1842             m_parent->signalAPIResult(&result);
1843         }
1844         break;
1845     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1846         {
1847             int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1848             result.status = rc;
1849             result.request_api = evt;
1850             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1851             result.enabled = enabled;
1852             m_parent->signalAPIResult(&result);
1853         }
1854         break;
1855     case QCAMERA_SM_EVT_SET_PARAMS:
1856         {
1857             bool needRestart = false;
1858             rc = m_parent->updateParameters((char*)payload, needRestart);
1859             if (rc == NO_ERROR) {
1860                 if (needRestart) {
1861                     // cannot set parameters that requires restart during recording
1862                     ALOGE("%s: Cannot set parameters that requires restart during recording",
1863                           __func__);
1864                     rc = BAD_VALUE;
1865                 } else {
1866                     rc = m_parent->commitParameterChanges();
1867                 }
1868             }
1869             result.status = rc;
1870             result.request_api = evt;
1871             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1872             m_parent->signalAPIResult(&result);
1873         }
1874         break;
1875     case QCAMERA_SM_EVT_GET_PARAMS:
1876         {
1877             result.params = m_parent->getParameters();
1878             rc = NO_ERROR;
1879             result.status = rc;
1880             result.request_api = evt;
1881             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1882             m_parent->signalAPIResult(&result);
1883         }
1884         break;
1885     case QCAMERA_SM_EVT_PUT_PARAMS:
1886         {
1887             rc = m_parent->putParameters((char*)payload);
1888             result.status = rc;
1889             result.request_api = evt;
1890             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1891             m_parent->signalAPIResult(&result);
1892         }
1893         break;
1894     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1895         {
1896             rc = NO_ERROR;
1897             result.status = rc;
1898             result.request_api = evt;
1899             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1900             result.enabled = 0;
1901             m_parent->signalAPIResult(&result);
1902         }
1903         break;
1904     case QCAMERA_SM_EVT_RECORDING_ENABLED:
1905         {
1906             rc = NO_ERROR;
1907             result.status = rc;
1908             result.request_api = evt;
1909             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1910             result.enabled = 1;
1911             m_parent->signalAPIResult(&result);
1912         }
1913         break;
1914     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1915         {
1916             rc = m_parent->storeMetaDataInBuffers(int(payload));
1917             result.status = rc;
1918             result.request_api = evt;
1919             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1920             m_parent->signalAPIResult(&result);
1921         }
1922         break;
1923     case QCAMERA_SM_EVT_DUMP:
1924         {
1925             rc = m_parent->dump((int)payload);
1926             result.status = rc;
1927             result.request_api = evt;
1928             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1929             m_parent->signalAPIResult(&result);
1930         }
1931         break;
1932     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1933         {
1934             rc = m_parent->autoFocus();
1935             result.status = rc;
1936             result.request_api = evt;
1937             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1938             m_parent->signalAPIResult(&result);
1939         }
1940         break;
1941     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1942         {
1943             bool isAFRunning = m_parent->isAFRunning();
1944             rc = m_parent->cancelAutoFocus();
1945             if (!isAFRunning) {
1946                 result.status = rc;
1947                 result.request_api = evt;
1948                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1949                 m_parent->signalAPIResult(&result);
1950             }
1951         }
1952         break;
1953     case QCAMERA_SM_EVT_SEND_COMMAND:
1954         {
1955             qcamera_sm_evt_command_payload_t *cmd_payload =
1956                 (qcamera_sm_evt_command_payload_t *)payload;
1957             rc = m_parent->sendCommand(cmd_payload->cmd,
1958                                        cmd_payload->arg1,
1959                                        cmd_payload->arg2);
1960             result.status = rc;
1961             result.request_api = evt;
1962             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1963             m_parent->signalAPIResult(&result);
1964         }
1965         break;
1966     case QCAMERA_SM_EVT_TAKE_PICTURE:
1967         {
1968             m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
1969             rc = m_parent->takeLiveSnapshot();
1970             if (rc != NO_ERROR) {
1971                 m_state = QCAMERA_SM_STATE_RECORDING;
1972             }
1973             result.status = rc;
1974             result.request_api = evt;
1975             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1976             m_parent->signalAPIResult(&result);
1977         }
1978         break;
1979     case QCAMERA_SM_EVT_START_RECORDING:
1980         {
1981             // no ops here
1982             CDBG_HIGH("%s: already in recording state, no ops for start_recording", __func__);
1983             rc = 0;
1984             result.status = rc;
1985             result.request_api = evt;
1986             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1987             m_parent->signalAPIResult(&result);
1988         }
1989         break;
1990     case QCAMERA_SM_EVT_STOP_RECORDING:
1991         {
1992             rc = m_parent->stopRecording();
1993             m_state = QCAMERA_SM_STATE_PREVIEWING;
1994             result.status = rc;
1995             result.request_api = evt;
1996             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1997             m_parent->signalAPIResult(&result);
1998         }
1999         break;
2000     case QCAMERA_SM_EVT_STOP_PREVIEW:
2001         {
2002             rc = m_parent->stopRecording();
2003             m_state = QCAMERA_SM_STATE_PREVIEWING;
2004 
2005             rc = m_parent->stopPreview();
2006             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2007 
2008             result.status = rc;
2009             result.request_api = evt;
2010             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2011             m_parent->signalAPIResult(&result);
2012         }
2013         break;
2014     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2015         {
2016             rc = m_parent->releaseRecordingFrame((const void *)payload);
2017             result.status = rc;
2018             result.request_api = evt;
2019             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2020             m_parent->signalAPIResult(&result);
2021         }
2022         break;
2023     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2024         {
2025             int32_t faceID = 0;
2026             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2027                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2028             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2029                                              reg_payload->config,
2030                                              faceID);
2031             result.status = rc;
2032             result.request_api = evt;
2033             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2034             result.handle = faceID;
2035             m_parent->signalAPIResult(&result);
2036         }
2037         break;
2038     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
2039         {
2040             //In Video snapshot, prepare hardware is a no-op.
2041             result.status = NO_ERROR;
2042             result.request_api = evt;
2043             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2044             m_parent->signalAPIResult(&result);
2045         }
2046         break;
2047     case QCAMERA_SM_EVT_CANCEL_PICTURE:
2048     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2049     case QCAMERA_SM_EVT_RELEASE:
2050         {
2051             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2052             rc = INVALID_OPERATION;
2053             result.status = rc;
2054             result.request_api = evt;
2055             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2056             m_parent->signalAPIResult(&result);
2057         }
2058         break;
2059     case QCAMERA_SM_EVT_EVT_INTERNAL:
2060         {
2061             qcamera_sm_internal_evt_payload_t *internal_evt =
2062                 (qcamera_sm_internal_evt_payload_t *)payload;
2063             switch (internal_evt->evt_type) {
2064             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2065                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2066                 break;
2067             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
2068                 break;
2069             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
2070                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
2071                 break;
2072             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
2073                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
2074                 break;
2075             case QCAMERA_INTERNAL_EVT_CROP_INFO:
2076                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
2077                 break;
2078             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
2079                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
2080                 break;
2081             default:
2082                 break;
2083             }
2084         }
2085         break;
2086     case QCAMERA_SM_EVT_EVT_NOTIFY:
2087         {
2088             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2089             switch (cam_evt->server_event_type) {
2090             case CAM_EVENT_TYPE_DAEMON_DIED:
2091                 {
2092                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
2093                                             CAMERA_ERROR_SERVER_DIED,
2094                                             0);
2095                 }
2096                 break;
2097             default:
2098                 ALOGE("%s: Invalid internal event %d in state(%d)",
2099                             __func__, cam_evt->server_event_type, m_state);
2100                 break;
2101             }
2102         }
2103         break;
2104     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2105         {
2106             rc = m_parent->updateThermalLevel(
2107                     *(qcamera_thermal_level_enum_t *)&payload);
2108         }
2109         break;
2110     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2111         {
2112             // No ops, but need to notify
2113             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2114             result.status = rc;
2115             result.request_api = evt;
2116             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2117             m_parent->signalEvtResult(&result);
2118         }
2119        break;
2120     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2121     default:
2122         ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2123         break;
2124     }
2125 
2126     return rc;
2127 }
2128 
2129 /*===========================================================================
2130  * FUNCTION   : procEvtVideoPicTakingState
2131  *
2132  * DESCRIPTION: finite state machine function to handle event in state of
2133  *              QCAMERA_SM_STATE_VIDEO_PIC_TAKING.
2134  *
2135  * PARAMETERS :
2136  *   @evt      : event to be processed
2137  *   @payload  : event payload. Can be NULL if not needed.
2138  *
2139  * RETURN     : int32_t type of status
2140  *              NO_ERROR  -- success
2141  *              none-zero failure code
2142  *==========================================================================*/
procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)2143 int32_t QCameraStateMachine::procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,
2144                                                         void *payload)
2145 {
2146     int32_t rc = NO_ERROR;
2147     qcamera_api_result_t result;
2148     memset(&result, 0, sizeof(qcamera_api_result_t));
2149 
2150     ALOGV("%s: event (%d)", __func__, evt);
2151     switch (evt) {
2152     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2153         {
2154             // Error setting preview window during previewing
2155             ALOGE("Cannot set preview window when preview is running");
2156             rc = INVALID_OPERATION;
2157             result.status = rc;
2158             result.request_api = evt;
2159             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2160             m_parent->signalAPIResult(&result);
2161         }
2162         break;
2163     case QCAMERA_SM_EVT_SET_CALLBACKS:
2164         {
2165             qcamera_sm_evt_setcb_payload_t *setcbs =
2166                 (qcamera_sm_evt_setcb_payload_t *)payload;
2167             rc = m_parent->setCallBacks(setcbs->notify_cb,
2168                                         setcbs->data_cb,
2169                                         setcbs->data_cb_timestamp,
2170                                         setcbs->get_memory,
2171                                         setcbs->user);
2172             result.status = rc;
2173             result.request_api = evt;
2174             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2175             m_parent->signalAPIResult(&result);
2176         }
2177         break;
2178     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
2179         {
2180             rc = m_parent->enableMsgType(int32_t(payload));
2181             result.status = rc;
2182             result.request_api = evt;
2183             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2184             m_parent->signalAPIResult(&result);
2185         }
2186         break;
2187     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
2188         {
2189             rc = m_parent->disableMsgType(int32_t(payload));
2190             result.status = rc;
2191             result.request_api = evt;
2192             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2193             m_parent->signalAPIResult(&result);
2194         }
2195         break;
2196     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
2197         {
2198             int enabled = m_parent->msgTypeEnabled(int32_t(payload));
2199             result.status = rc;
2200             result.request_api = evt;
2201             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2202             result.enabled = enabled;
2203             m_parent->signalAPIResult(&result);
2204         }
2205         break;
2206     case QCAMERA_SM_EVT_SET_PARAMS:
2207         {
2208             bool needRestart = false;
2209             rc = m_parent->updateParameters((char*)payload, needRestart);
2210             if (rc == NO_ERROR) {
2211                 if (needRestart) {
2212                     // cannot set parameters that requires restart during recording
2213                     ALOGE("%s: Cannot set parameters that requires restart during recording",
2214                           __func__);
2215                     rc = BAD_VALUE;
2216                 } else {
2217                     rc = m_parent->commitParameterChanges();
2218                 }
2219             }
2220             result.status = rc;
2221             result.request_api = evt;
2222             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2223             m_parent->signalAPIResult(&result);
2224         }
2225         break;
2226     case QCAMERA_SM_EVT_GET_PARAMS:
2227         {
2228             result.params = m_parent->getParameters();
2229             rc = NO_ERROR;
2230             result.status = rc;
2231             result.request_api = evt;
2232             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
2233             m_parent->signalAPIResult(&result);
2234         }
2235         break;
2236     case QCAMERA_SM_EVT_PUT_PARAMS:
2237         {
2238             rc = m_parent->putParameters((char*)payload);
2239             result.status = rc;
2240             result.request_api = evt;
2241             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2242             m_parent->signalAPIResult(&result);
2243         }
2244         break;
2245     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2246         {
2247             rc = NO_ERROR;
2248             result.status = rc;
2249             result.request_api = evt;
2250             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2251             result.enabled = 1;
2252             m_parent->signalAPIResult(&result);
2253         }
2254         break;
2255     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2256         {
2257             rc = NO_ERROR;
2258             result.status = rc;
2259             result.request_api = evt;
2260             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2261             result.enabled = 1;
2262             m_parent->signalAPIResult(&result);
2263         }
2264         break;
2265     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2266         {
2267             rc = m_parent->storeMetaDataInBuffers(int(payload));
2268             result.status = rc;
2269             result.request_api = evt;
2270             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2271             m_parent->signalAPIResult(&result);
2272         }
2273         break;
2274     case QCAMERA_SM_EVT_DUMP:
2275         {
2276             rc = m_parent->dump((int)payload);
2277             result.status = rc;
2278             result.request_api = evt;
2279             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2280             m_parent->signalAPIResult(&result);
2281         }
2282         break;
2283     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2284         {
2285             rc = m_parent->autoFocus();
2286             result.status = rc;
2287             result.request_api = evt;
2288             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2289             m_parent->signalAPIResult(&result);
2290         }
2291         break;
2292     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2293         {
2294             bool isAFRunning = m_parent->isAFRunning();
2295             rc = m_parent->cancelAutoFocus();
2296             if (!isAFRunning) {
2297                 result.status = rc;
2298                 result.request_api = evt;
2299                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2300                 m_parent->signalAPIResult(&result);
2301             }
2302         }
2303         break;
2304     case QCAMERA_SM_EVT_SEND_COMMAND:
2305         {
2306             qcamera_sm_evt_command_payload_t *cmd_payload =
2307                 (qcamera_sm_evt_command_payload_t *)payload;
2308             rc = m_parent->sendCommand(cmd_payload->cmd,
2309                                        cmd_payload->arg1,
2310                                        cmd_payload->arg2);
2311             result.status = rc;
2312             result.request_api = evt;
2313             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2314             m_parent->signalAPIResult(&result);
2315         }
2316         break;
2317     case QCAMERA_SM_EVT_STOP_RECORDING:
2318         {
2319             rc = m_parent->cancelLiveSnapshot();
2320             m_state = QCAMERA_SM_STATE_RECORDING;
2321 
2322             rc = m_parent->stopRecording();
2323             m_state = QCAMERA_SM_STATE_PREVIEWING;
2324 
2325             result.status = rc;
2326             result.request_api = evt;
2327             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2328             m_parent->signalAPIResult(&result);
2329         }
2330         break;
2331     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2332         {
2333             rc = m_parent->releaseRecordingFrame((const void *)payload);
2334             result.status = rc;
2335             result.request_api = evt;
2336             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2337             m_parent->signalAPIResult(&result);
2338         }
2339         break;
2340     case QCAMERA_SM_EVT_CANCEL_PICTURE:
2341         {
2342             rc = m_parent->cancelLiveSnapshot();
2343             m_state = QCAMERA_SM_STATE_RECORDING;
2344             result.status = rc;
2345             result.request_api = evt;
2346             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2347             m_parent->signalAPIResult(&result);
2348         }
2349         break;
2350     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2351         {
2352             int32_t faceID = 0;
2353             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2354                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2355             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2356                                              reg_payload->config,
2357                                              faceID);
2358             result.status = rc;
2359             result.request_api = evt;
2360             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2361             result.handle = faceID;
2362             m_parent->signalAPIResult(&result);
2363         }
2364         break;
2365     case QCAMERA_SM_EVT_STOP_PREVIEW:
2366         {
2367             rc = m_parent->cancelLiveSnapshot();
2368             m_state = QCAMERA_SM_STATE_RECORDING;
2369 
2370             rc = m_parent->stopRecording();
2371             m_state = QCAMERA_SM_STATE_PREVIEWING;
2372 
2373             rc = m_parent->stopPreview();
2374             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2375 
2376             result.status = rc;
2377             result.request_api = evt;
2378             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2379             m_parent->signalAPIResult(&result);
2380         }
2381         break;
2382     case QCAMERA_SM_EVT_START_RECORDING:
2383     case QCAMERA_SM_EVT_START_PREVIEW:
2384     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2385     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
2386     case QCAMERA_SM_EVT_TAKE_PICTURE:
2387     case QCAMERA_SM_EVT_RELEASE:
2388         {
2389             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2390             rc = INVALID_OPERATION;
2391             result.status = rc;
2392             result.request_api = evt;
2393             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2394             m_parent->signalAPIResult(&result);
2395         }
2396         break;
2397     case QCAMERA_SM_EVT_EVT_INTERNAL:
2398         {
2399             qcamera_sm_internal_evt_payload_t *internal_evt =
2400                 (qcamera_sm_internal_evt_payload_t *)payload;
2401             switch (internal_evt->evt_type) {
2402             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2403                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2404                 break;
2405             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
2406                 break;
2407             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
2408                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
2409                 break;
2410             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
2411                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
2412                 break;
2413             case QCAMERA_INTERNAL_EVT_CROP_INFO:
2414                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
2415                 break;
2416             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
2417                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
2418                 break;
2419             default:
2420                 break;
2421             }
2422         }
2423         break;
2424     case QCAMERA_SM_EVT_EVT_NOTIFY:
2425         {
2426             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2427             switch (cam_evt->server_event_type) {
2428             case CAM_EVENT_TYPE_DAEMON_DIED:
2429                 {
2430                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
2431                                             CAMERA_ERROR_SERVER_DIED,
2432                                             0);
2433                 }
2434                 break;
2435             default:
2436                 ALOGE("%s: Invalid internal event %d in state(%d)",
2437                             __func__, cam_evt->server_event_type, m_state);
2438                 break;
2439             }
2440         }
2441         break;
2442     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2443         {
2444             qcamera_jpeg_evt_payload_t *jpeg_job =
2445                 (qcamera_jpeg_evt_payload_t *)payload;
2446             rc = m_parent->processJpegNotify(jpeg_job);
2447         }
2448         break;
2449     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2450         {
2451             rc = m_parent->cancelLiveSnapshot();
2452             m_state = QCAMERA_SM_STATE_RECORDING;
2453             result.status = rc;
2454             result.request_api = evt;
2455             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2456             m_parent->signalEvtResult(&result);
2457         }
2458         break;
2459     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2460         {
2461             rc = m_parent->updateThermalLevel(
2462                     *(qcamera_thermal_level_enum_t *)&payload);
2463         }
2464         break;
2465     default:
2466         ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2467         break;
2468     }
2469 
2470     return rc;
2471 }
2472 
2473 /*===========================================================================
2474  * FUNCTION   : procEvtPreviewPicTakingState
2475  *
2476  * DESCRIPTION: finite state machine function to handle event in state of
2477  *              QCAMERA_SM_STATE_PREVIEW_PIC_TAKING.
2478  *
2479  * PARAMETERS :
2480  *   @evt      : event to be processed
2481  *   @payload  : event payload. Can be NULL if not needed.
2482  *
2483  * RETURN     : int32_t type of status
2484  *              NO_ERROR  -- success
2485  *              none-zero failure code
2486  *==========================================================================*/
procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)2487 int32_t QCameraStateMachine::procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,
2488                                                           void *payload)
2489 {
2490     int32_t rc = NO_ERROR;
2491     qcamera_api_result_t result;
2492     memset(&result, 0, sizeof(qcamera_api_result_t));
2493 
2494     ALOGV("%s: event (%d)", __func__, evt);
2495     switch (evt) {
2496     case QCAMERA_SM_EVT_SET_CALLBACKS:
2497         {
2498             qcamera_sm_evt_setcb_payload_t *setcbs =
2499                 (qcamera_sm_evt_setcb_payload_t *)payload;
2500             rc = m_parent->setCallBacks(setcbs->notify_cb,
2501                                         setcbs->data_cb,
2502                                         setcbs->data_cb_timestamp,
2503                                         setcbs->get_memory,
2504                                         setcbs->user);
2505             result.status = rc;
2506             result.request_api = evt;
2507             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2508             m_parent->signalAPIResult(&result);
2509         }
2510         break;
2511     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
2512         {
2513             rc = m_parent->enableMsgType(int32_t(payload));
2514             result.status = rc;
2515             result.request_api = evt;
2516             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2517             m_parent->signalAPIResult(&result);
2518         }
2519         break;
2520     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
2521         {
2522             rc = m_parent->disableMsgType(int32_t(payload));
2523             result.status = rc;
2524             result.request_api = evt;
2525             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2526             m_parent->signalAPIResult(&result);
2527         }
2528         break;
2529     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
2530         {
2531             int enabled = m_parent->msgTypeEnabled(int32_t(payload));
2532             result.status = rc;
2533             result.request_api = evt;
2534             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2535             result.enabled = enabled;
2536             m_parent->signalAPIResult(&result);
2537         }
2538         break;
2539     case QCAMERA_SM_EVT_SET_PARAMS:
2540         {
2541             bool needRestart = false;
2542             rc = m_parent->updateParameters((char*)payload, needRestart);
2543             if (rc == NO_ERROR) {
2544                 if (needRestart) {
2545                     // need restart preview for parameters to take effect
2546                     // stop preview
2547                     m_parent->stopPreview();
2548                     // Clear memory pools
2549                     m_parent->m_memoryPool.clear();
2550                     // commit parameter changes to server
2551                     m_parent->commitParameterChanges();
2552                     // start preview again
2553                     rc = m_parent->preparePreview();
2554                     if (rc == NO_ERROR) {
2555                         rc = m_parent->startPreview();
2556                         if (rc != NO_ERROR) {
2557                             m_parent->unpreparePreview();
2558                         }
2559                     }
2560                     if (rc != NO_ERROR) {
2561                         m_state = QCAMERA_SM_STATE_PIC_TAKING;
2562                     }
2563                 } else {
2564                     rc = m_parent->commitParameterChanges();
2565                 }
2566             }
2567             result.status = rc;
2568             result.request_api = evt;
2569             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2570             m_parent->signalAPIResult(&result);
2571         }
2572         break;
2573     case QCAMERA_SM_EVT_GET_PARAMS:
2574         {
2575             result.params = m_parent->getParameters();
2576             rc = NO_ERROR;
2577             result.status = rc;
2578             result.request_api = evt;
2579             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
2580             m_parent->signalAPIResult(&result);
2581         }
2582         break;
2583     case QCAMERA_SM_EVT_PUT_PARAMS:
2584         {
2585             rc = m_parent->putParameters((char*)payload);
2586             result.status = rc;
2587             result.request_api = evt;
2588             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2589             m_parent->signalAPIResult(&result);
2590         }
2591         break;
2592     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2593         {
2594             rc = NO_ERROR;
2595             result.status = rc;
2596             result.request_api = evt;
2597             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2598             result.enabled = 1;
2599             m_parent->signalAPIResult(&result);
2600         }
2601         break;
2602     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2603         {
2604             rc = NO_ERROR;
2605             result.status = rc;
2606             result.request_api = evt;
2607             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2608             result.enabled = 0;
2609             m_parent->signalAPIResult(&result);
2610         }
2611         break;
2612     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2613         {
2614             rc = m_parent->storeMetaDataInBuffers(int(payload));
2615             result.status = rc;
2616             result.request_api = evt;
2617             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2618             m_parent->signalAPIResult(&result);
2619         }
2620         break;
2621     case QCAMERA_SM_EVT_DUMP:
2622         {
2623             rc = m_parent->dump((int)payload);
2624             result.status = rc;
2625             result.request_api = evt;
2626             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2627             m_parent->signalAPIResult(&result);
2628         }
2629         break;
2630     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2631         {
2632             rc = m_parent->autoFocus();
2633             result.status = rc;
2634             result.request_api = evt;
2635             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2636             m_parent->signalAPIResult(&result);
2637         }
2638         break;
2639     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2640         {
2641             bool isAFRunning = m_parent->isAFRunning();
2642             rc = m_parent->cancelAutoFocus();
2643             if (!isAFRunning) {
2644                 result.status = rc;
2645                 result.request_api = evt;
2646                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2647                 m_parent->signalAPIResult(&result);
2648             }
2649         }
2650         break;
2651     case QCAMERA_SM_EVT_SEND_COMMAND:
2652         {
2653             qcamera_sm_evt_command_payload_t *cmd_payload =
2654                 (qcamera_sm_evt_command_payload_t *)payload;
2655             rc = m_parent->sendCommand(cmd_payload->cmd,
2656                                        cmd_payload->arg1,
2657                                        cmd_payload->arg2);
2658 #ifndef VANILLA_HAL
2659             if ( CAMERA_CMD_LONGSHOT_OFF == cmd_payload->cmd ) {
2660                 // move state to previewing state
2661                 m_state = QCAMERA_SM_STATE_PREVIEWING;
2662             }
2663 #endif
2664             result.status = rc;
2665             result.request_api = evt;
2666             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2667             m_parent->signalAPIResult(&result);
2668         }
2669         break;
2670     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2671         {
2672             rc = m_parent->releaseRecordingFrame((const void *)payload);
2673             result.status = rc;
2674             result.request_api = evt;
2675             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2676             m_parent->signalAPIResult(&result);
2677         }
2678         break;
2679     case QCAMERA_SM_EVT_CANCEL_PICTURE:
2680         {
2681             if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
2682                 rc = m_parent->cancelPicture();
2683             } else {
2684                 rc = m_parent->cancelLiveSnapshot();
2685             }
2686             m_state = QCAMERA_SM_STATE_PREVIEWING;
2687             result.status = rc;
2688             result.request_api = evt;
2689             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2690             m_parent->signalAPIResult(&result);
2691         }
2692         break;
2693     case QCAMERA_SM_EVT_STOP_PREVIEW:
2694         {
2695             if (m_parent->isZSLMode()) {
2696                 // cancel picture first
2697                 rc = m_parent->cancelPicture();
2698                 m_parent->stopChannel(QCAMERA_CH_TYPE_ZSL);
2699             } else if (m_parent->isLongshotEnabled()) {
2700                 // just cancel picture
2701                 rc = m_parent->cancelPicture();
2702             } else {
2703                 rc = m_parent->cancelLiveSnapshot();
2704                 m_parent->stopChannel(QCAMERA_CH_TYPE_PREVIEW);
2705             }
2706             // unprepare preview
2707             m_parent->unpreparePreview();
2708             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2709             result.status = rc;
2710             result.request_api = evt;
2711             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2712             m_parent->signalAPIResult(&result);
2713         }
2714         break;
2715     case QCAMERA_SM_EVT_START_RECORDING:
2716         {
2717             if (m_parent->isZSLMode()) {
2718                 ALOGE("%s: cannot handle evt(%d) in state(%d) in ZSL mode",
2719                       __func__, evt, m_state);
2720                 rc = INVALID_OPERATION;
2721             } else if (m_parent->isLongshotEnabled()) {
2722                 ALOGE("%s: cannot handle evt(%d) in state(%d) in Longshot mode",
2723                       __func__, evt, m_state);
2724                 rc = INVALID_OPERATION;
2725             } else {
2726                 rc = m_parent->startRecording();
2727                 if (rc == NO_ERROR) {
2728                     m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
2729                 }
2730             }
2731             result.status = rc;
2732             result.request_api = evt;
2733             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2734             m_parent->signalAPIResult(&result);
2735         }
2736         break;
2737     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2738         {
2739             int32_t faceID = 0;
2740             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2741                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2742             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2743                                              reg_payload->config,
2744                                              faceID);
2745             result.status = rc;
2746             result.request_api = evt;
2747             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2748             result.handle = faceID;
2749             m_parent->signalAPIResult(&result);
2750         }
2751         break;
2752     case QCAMERA_SM_EVT_TAKE_PICTURE:
2753         {
2754             if ( m_parent->isLongshotEnabled() ) {
2755                rc = m_parent->longShot();
2756             } else {
2757                 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2758                 rc = INVALID_OPERATION;
2759             }
2760 
2761             result.status = rc;
2762             result.request_api = evt;
2763             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2764             m_parent->signalAPIResult(&result);
2765         }
2766         break;
2767 
2768     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
2769         {
2770           ALOGD("%s: [ZSL Retro]Prepare Snapshot", __func__);
2771           if (m_parent->isRetroPicture()) {
2772               ALOGD("%s: [ZSL Retro] Prepare Snapshot in Retro Mode", __func__);
2773               rc = m_parent->prepareHardwareForSnapshot(FALSE);
2774               if (rc != NO_ERROR) {
2775                   ALOGE("%s: [ZSL Retro]prepareHardwareForSnapshot failed %d",
2776                       __func__, rc);
2777                   result.status = rc;
2778                   result.request_api = evt;
2779                   result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2780                   m_parent->signalAPIResult(&result);
2781               }
2782           }
2783           else {
2784               ALOGE("%s: cannot handle evt(%d) in state(%d)",
2785                 __func__, evt, m_state);
2786               rc = INVALID_OPERATION;
2787               result.status = rc;
2788               result.request_api = evt;
2789               result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2790               m_parent->signalAPIResult(&result);
2791           }
2792         }
2793         break;
2794     case QCAMERA_SM_EVT_STOP_RECORDING:
2795     case QCAMERA_SM_EVT_START_PREVIEW:
2796     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2797     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2798     case QCAMERA_SM_EVT_RELEASE:
2799         {
2800             ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2801             rc = INVALID_OPERATION;
2802             result.status = rc;
2803             result.request_api = evt;
2804             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2805             m_parent->signalAPIResult(&result);
2806         }
2807         break;
2808     case QCAMERA_SM_EVT_EVT_INTERNAL:
2809         {
2810             qcamera_sm_internal_evt_payload_t *internal_evt =
2811                 (qcamera_sm_internal_evt_payload_t *)payload;
2812             switch (internal_evt->evt_type) {
2813             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2814                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2815                 break;
2816             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
2817                 ALOGD("%s: [ZSL Retro]Received QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE event",
2818                         __func__);
2819                 if (m_parent->isRetroPicture()) {
2820                     m_parent->processPrepSnapshotDoneEvent(internal_evt->prep_snapshot_state);
2821                     ALOGD("%s: [ZSL Retro] Retro picture", __func__);
2822                     result.status = NO_ERROR;
2823                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
2824                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2825                     m_parent->signalAPIResult(&result);
2826                 }
2827                 else {
2828                     ALOGE("%s: [ZSL Retro] Invalid Case for  "
2829                             "QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event", __func__);
2830                 }
2831                 break;
2832             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
2833                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
2834                 break;
2835             case QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT:
2836                 // This is valid only in Retro picture Mode
2837                 if (m_parent->isRetroPicture()) {
2838                     ALOGD("%s: [ZSL Retro] Received QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event",
2839                             __func__);
2840                     result.status = NO_ERROR;
2841                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
2842                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2843                     m_parent->signalAPIResult(&result);
2844                 }
2845                 else {
2846                     ALOGD("%s: [ZSL Retro] Wrong Case for  "
2847                            "QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event", __func__);
2848                 }
2849                 break;
2850             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
2851                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
2852                 break;
2853             case QCAMERA_INTERNAL_EVT_CROP_INFO:
2854                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
2855                 break;
2856             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
2857                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
2858                 break;
2859             default:
2860                 break;
2861             }
2862         }
2863         break;
2864     case QCAMERA_SM_EVT_EVT_NOTIFY:
2865         {
2866             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2867             switch (cam_evt->server_event_type) {
2868             case CAM_EVENT_TYPE_DAEMON_DIED:
2869                 {
2870                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
2871                                             CAMERA_ERROR_SERVER_DIED,
2872                                             0);
2873                 }
2874                 break;
2875             default:
2876                 ALOGE("%s: Invalid internal event %d in state(%d)",
2877                             __func__, cam_evt->server_event_type, m_state);
2878                 break;
2879             }
2880         }
2881         break;
2882     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2883         {
2884             ALOGV("%s: [ZSL Retro] Calling Process Jpeg Notify",
2885             __func__);
2886             qcamera_jpeg_evt_payload_t *jpeg_job =
2887                 (qcamera_jpeg_evt_payload_t *)payload;
2888             rc = m_parent->processJpegNotify(jpeg_job);
2889         }
2890         break;
2891     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2892         {
2893             ALOGV("%s: [ZSL Retro] Snapshot Done", __func__);
2894             if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
2895                 rc = m_parent->cancelPicture();
2896             } else {
2897                 rc = m_parent->cancelLiveSnapshot();
2898             }
2899             m_state = QCAMERA_SM_STATE_PREVIEWING;
2900             if (m_parent->isRetroPicture()){
2901                 result.status = rc;
2902                 result.request_api = evt;
2903                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2904                 ALOGV("\n Signalling for JPEG snapshot done!!");
2905                 m_parent->signalAPIResult(&result);
2906 
2907             }
2908             result.status = rc;
2909             result.request_api = evt;
2910             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2911             m_parent->signalEvtResult(&result);
2912         }
2913         break;
2914     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2915         {
2916             rc = m_parent->updateThermalLevel(
2917                     *(qcamera_thermal_level_enum_t *)&payload);
2918         }
2919         break;
2920     default:
2921         ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2922         break;
2923     }
2924 
2925     return rc;
2926 }
2927 
2928 /*===========================================================================
2929  * FUNCTION   : isRecording
2930  *
2931  * DESCRIPTION: check if recording is in process.
2932  *
2933  * PARAMETERS : None
2934  *
2935  * RETURN     : true -- recording
2936  *              false -- not in recording mode
2937  *==========================================================================*/
isRecording()2938 bool QCameraStateMachine::isRecording()
2939 {
2940     switch (m_state) {
2941     case QCAMERA_SM_STATE_RECORDING:
2942     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
2943         return true;
2944     default:
2945         return false;
2946     }
2947 }
2948 
2949 /*===========================================================================
2950  * FUNCTION   : isPreviewRunning
2951  *
2952  * DESCRIPTION: check if preview is in process.
2953  *
2954  * PARAMETERS : None
2955  *
2956  * RETURN     : true -- preview running
2957  *              false -- preview stopped
2958  *==========================================================================*/
isPreviewRunning()2959 bool QCameraStateMachine::isPreviewRunning()
2960 {
2961     switch (m_state) {
2962     case QCAMERA_SM_STATE_PREVIEWING:
2963     case QCAMERA_SM_STATE_RECORDING:
2964     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
2965     case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
2966     case QCAMERA_SM_STATE_PREPARE_SNAPSHOT:
2967     case QCAMERA_SM_STATE_PREVIEW_READY:
2968         return true;
2969     default:
2970         return false;
2971     }
2972 }
2973 
2974 /*===========================================================================
2975  * FUNCTION   : isPreviewReady
2976  *
2977  * DESCRIPTION: check if preview is in ready state.
2978  *
2979  * PARAMETERS : None
2980  *
2981  * RETURN     : true -- preview is in ready state
2982  *              false -- preview is stopped
2983  *==========================================================================*/
isPreviewReady()2984 bool QCameraStateMachine::isPreviewReady()
2985 {
2986     switch (m_state) {
2987     case QCAMERA_SM_STATE_PREVIEW_READY:
2988         return true;
2989     default:
2990         return false;
2991     }
2992 }
2993 
2994 /*===========================================================================
2995  * FUNCTION   : isCaptureRunning
2996  *
2997  * DESCRIPTION: check if image capture is in process.
2998  *
2999  * PARAMETERS : None
3000  *
3001  * RETURN     : true -- capture running
3002  *              false -- capture stopped
3003  *==========================================================================*/
isCaptureRunning()3004 bool QCameraStateMachine::isCaptureRunning()
3005 {
3006     switch (m_state) {
3007     case QCAMERA_SM_STATE_PIC_TAKING:
3008     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3009     case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3010         return true;
3011     default:
3012         return false;
3013     }
3014 }
3015 /*===========================================================================
3016  * FUNCTION   : isNonZSLCaptureRunning
3017  *
3018  * DESCRIPTION: check if image capture is in process in non ZSL mode.
3019  *
3020  * PARAMETERS : None
3021  *
3022  * RETURN     : true -- capture running in non ZSL mode
3023  *              false -- Either in not capture mode or captur is not in non ZSL mode
3024  *==========================================================================*/
isNonZSLCaptureRunning()3025 bool QCameraStateMachine::isNonZSLCaptureRunning()
3026 {
3027     switch (m_state) {
3028     case QCAMERA_SM_STATE_PIC_TAKING:
3029         return true;
3030     default:
3031         return false;
3032     }
3033 }
3034 
3035 /*===========================================================================
3036  * FUNCTION   : dump
3037  *
3038  * DESCRIPTION: Composes a string based on current configuration
3039  *
3040  * PARAMETERS : none
3041  *
3042  * RETURN     : Formatted string
3043  *==========================================================================*/
dump()3044 String8 QCameraStateMachine::dump()
3045 {
3046     String8 str("\n");
3047     char s[128];
3048 
3049     snprintf(s, 128, "Is Preview Running: %d\n", isPreviewRunning());
3050     str += s;
3051 
3052     snprintf(s, 128, "Is Capture Running: %d\n", isCaptureRunning());
3053     str += s;
3054 
3055     snprintf(s, 128, "Is Non ZSL Capture Running: %d\n",
3056         isNonZSLCaptureRunning());
3057     str += s;
3058 
3059     snprintf(s, 128, "Current State: %d \n", m_state);
3060     str += s;
3061 
3062     switch(m_state){
3063         case QCAMERA_SM_STATE_PREVIEW_STOPPED:
3064         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_STOPPED \n");
3065         break;
3066 
3067         case QCAMERA_SM_STATE_PREVIEW_READY:
3068         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_READY \n");
3069         break;
3070 
3071         case QCAMERA_SM_STATE_PREVIEWING:
3072         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEWING \n");
3073         break;
3074 
3075         case QCAMERA_SM_STATE_PREPARE_SNAPSHOT:
3076         snprintf(s, 128, " QCAMERA_SM_STATE_PREPARE_SNAPSHOT \n");
3077         break;
3078 
3079         case QCAMERA_SM_STATE_PIC_TAKING:
3080         snprintf(s, 128, " QCAMERA_SM_STATE_PIC_TAKING \n");
3081         break;
3082 
3083         case QCAMERA_SM_STATE_RECORDING:
3084         snprintf(s, 128, " QCAMERA_SM_STATE_RECORDING \n");
3085         break;
3086 
3087         case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3088         snprintf(s, 128, " QCAMERA_SM_STATE_VIDEO_PIC_TAKING \n");
3089         break;
3090 
3091         case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3092         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_PIC_TAKING \n");
3093         break;
3094     }
3095     str += s;
3096 
3097     return str;
3098 }
3099 
3100 }; // namespace qcamera
3101