1 /* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 
30 #define LOG_TAG "QCameraStateMachine"
31 
32 // System dependencies
33 #include <utils/Errors.h>
34 #include <stdio.h>
35 
36 // Camera dependencies
37 #include "QCamera2HWI.h"
38 #include "QCameraStateMachine.h"
39 
40 extern "C" {
41 #include "mm_camera_dbg.h"
42 }
43 
44 namespace qcamera {
45 
46 /*===========================================================================
47  * FUNCTION   : smEvtProcRoutine
48  *
49  * DESCRIPTION: Statemachine process thread routine to handle events
50  *              in different state.
51  *
52  * PARAMETERS :
53  *   @data    : ptr to QCameraStateMachine object
54  *
55  * RETURN     : none
56  *==========================================================================*/
smEvtProcRoutine(void * data)57 void *QCameraStateMachine::smEvtProcRoutine(void *data)
58 {
59     int running = 1, ret;
60     QCameraStateMachine *pme = (QCameraStateMachine *)data;
61 
62     LOGH("E");
63     do {
64         do {
65             ret = cam_sem_wait(&pme->cmd_sem);
66             if (ret != 0 && errno != EINVAL) {
67                 LOGE("cam_sem_wait error (%s)",
68                             strerror(errno));
69                 return NULL;
70             }
71         } while (ret != 0);
72 
73         // we got notified about new cmd avail in cmd queue
74         // first check API cmd queue
75         qcamera_sm_cmd_t *node = (qcamera_sm_cmd_t *)pme->api_queue.dequeue();
76         if (node == NULL) {
77             // no API cmd, then check evt cmd queue
78             node = (qcamera_sm_cmd_t *)pme->evt_queue.dequeue();
79         }
80         if (node != NULL) {
81             switch (node->cmd) {
82             case QCAMERA_SM_CMD_TYPE_API:
83                 pme->stateMachine(node->evt, node->evt_payload);
84                 // API is in a way sync call, so evt_payload is managed by HWI
85                 // no need to free payload for API
86                 break;
87             case QCAMERA_SM_CMD_TYPE_EVT:
88                 pme->stateMachine(node->evt, node->evt_payload);
89 
90                 // EVT is async call, so payload need to be free after use
91                 free(node->evt_payload);
92                 node->evt_payload = NULL;
93                 break;
94             case QCAMERA_SM_CMD_TYPE_EXIT:
95                 running = 0;
96                 break;
97             default:
98                 break;
99             }
100             free(node);
101             node = NULL;
102         }
103     } while (running);
104     LOGH("X");
105     return NULL;
106 }
107 
108 /*===========================================================================
109  * FUNCTION   : QCameraStateMachine
110  *
111  * DESCRIPTION: constructor of QCameraStateMachine. Will start process thread
112  *
113  * PARAMETERS :
114  *   @ctrl    : ptr to HWI object
115  *
116  * RETURN     : none
117  *==========================================================================*/
QCameraStateMachine(QCamera2HardwareInterface * ctrl)118 QCameraStateMachine::QCameraStateMachine(QCamera2HardwareInterface *ctrl) :
119     api_queue(),
120     evt_queue()
121 {
122     m_parent = ctrl;
123     m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
124     cmd_pid = 0;
125     cam_sem_init(&cmd_sem, 0);
126     pthread_create(&cmd_pid,
127                    NULL,
128                    smEvtProcRoutine,
129                    this);
130     pthread_setname_np(cmd_pid, "CAM_stMachine");
131     m_bDelayPreviewMsgs = false;
132     m_DelayedMsgs = 0;
133     m_RestoreZSL = TRUE;
134     m_bPreviewCallbackNeeded = TRUE;
135 }
136 
137 /*===========================================================================
138  * FUNCTION   : ~QCameraStateMachine
139  *
140  * DESCRIPTION: desctructor of QCameraStateMachine. Will stop process thread.
141  *
142  * PARAMETERS : none
143  *
144  * RETURN     : none
145  *==========================================================================*/
~QCameraStateMachine()146 QCameraStateMachine::~QCameraStateMachine()
147 {
148     cam_sem_destroy(&cmd_sem);
149 }
150 
151 /*===========================================================================
152  * FUNCTION   : releaseThread
153  *
154  * DESCRIPTION: Sends an exit command and terminates the state machine thread
155  *
156  * PARAMETERS : none
157  *
158  * RETURN     : none
159  *==========================================================================*/
releaseThread()160 void QCameraStateMachine::releaseThread()
161 {
162     if (cmd_pid != 0) {
163         qcamera_sm_cmd_t *node =
164             (qcamera_sm_cmd_t *)malloc(sizeof(qcamera_sm_cmd_t));
165         if (NULL != node) {
166             memset(node, 0, sizeof(qcamera_sm_cmd_t));
167             node->cmd = QCAMERA_SM_CMD_TYPE_EXIT;
168 
169             if (api_queue.enqueue((void *)node)) {
170                 cam_sem_post(&cmd_sem);
171             } else {
172                 free(node);
173                 node = NULL;
174             }
175 
176             /* wait until cmd thread exits */
177             if (pthread_join(cmd_pid, NULL) != 0) {
178                 LOGW("pthread dead already\n");
179             }
180         }
181         cmd_pid = 0;
182     }
183 }
184 
185 /*===========================================================================
186  * FUNCTION   : applyDelayedMsgs
187  *
188  * DESCRIPTION: Enable if needed any delayed message types
189  *
190  * PARAMETERS : None
191  * RETURN     : int32_t type of status
192  *              NO_ERROR  -- success
193  *              none-zero failure code
194  *==========================================================================*/
applyDelayedMsgs()195 int32_t QCameraStateMachine::applyDelayedMsgs()
196 {
197     int32_t rc = NO_ERROR;
198 
199     if (m_bDelayPreviewMsgs && m_DelayedMsgs) {
200         rc = m_parent->enableMsgType(m_DelayedMsgs);
201         m_bDelayPreviewMsgs = false;
202         m_DelayedMsgs = 0;
203     } else if (m_bDelayPreviewMsgs) {
204         m_bDelayPreviewMsgs = false;
205     }
206 
207     return rc;
208 }
209 
210 /*===========================================================================
211  * FUNCTION   : procAPI
212  *
213  * DESCRIPTION: process incoming API request from framework layer.
214  *
215  * PARAMETERS :
216  *   @evt          : event to be processed
217  *   @api_payload  : API payload. Can be NULL if not needed.
218  *
219  * RETURN     : int32_t type of status
220  *              NO_ERROR  -- success
221  *              none-zero failure code
222  *==========================================================================*/
procAPI(qcamera_sm_evt_enum_t evt,void * api_payload)223 int32_t QCameraStateMachine::procAPI(qcamera_sm_evt_enum_t evt,
224                                      void *api_payload)
225 {
226     qcamera_sm_cmd_t *node =
227         (qcamera_sm_cmd_t *)malloc(sizeof(qcamera_sm_cmd_t));
228     if (NULL == node) {
229         LOGE("No memory for qcamera_sm_cmd_t");
230         return NO_MEMORY;
231     }
232 
233     memset(node, 0, sizeof(qcamera_sm_cmd_t));
234     node->cmd = QCAMERA_SM_CMD_TYPE_API;
235     node->evt = evt;
236     node->evt_payload = api_payload;
237     if (api_queue.enqueue((void *)node)) {
238         cam_sem_post(&cmd_sem);
239         return NO_ERROR;
240     } else {
241         LOGE("API enqueue failed API = %d", evt);
242         free(node);
243         return UNKNOWN_ERROR;
244     }
245 }
246 
247 /*===========================================================================
248  * FUNCTION   : procEvt
249  *
250  * DESCRIPTION: process incoming envent from mm-camera-interface and
251  *              mm-jpeg-interface.
252  *
253  * PARAMETERS :
254  *   @evt          : event to be processed
255  *   @evt_payload  : event payload. Can be NULL if not needed.
256  *
257  * RETURN     : int32_t type of status
258  *              NO_ERROR  -- success
259  *              none-zero failure code
260  *==========================================================================*/
procEvt(qcamera_sm_evt_enum_t evt,void * evt_payload)261 int32_t QCameraStateMachine::procEvt(qcamera_sm_evt_enum_t evt,
262                                      void *evt_payload)
263 {
264     qcamera_sm_cmd_t *node =
265         (qcamera_sm_cmd_t *)malloc(sizeof(qcamera_sm_cmd_t));
266     if (NULL == node) {
267         LOGE("No memory for qcamera_sm_cmd_t");
268         return NO_MEMORY;
269     }
270 
271     memset(node, 0, sizeof(qcamera_sm_cmd_t));
272     node->cmd = QCAMERA_SM_CMD_TYPE_EVT;
273     node->evt = evt;
274     node->evt_payload = evt_payload;
275     if (evt_queue.enqueue((void *)node)) {
276         cam_sem_post(&cmd_sem);
277         return NO_ERROR;
278     } else {
279         LOGE("EVENT enqueue failed Event = %d", evt);
280         free(node);
281         return UNKNOWN_ERROR;
282     }
283 }
284 
285 /*===========================================================================
286  * FUNCTION   : stateMachine
287  *
288  * DESCRIPTION: finite state machine entry function. Depends on state,
289  *              incoming event will be handled differently.
290  *
291  * PARAMETERS :
292  *   @evt      : event to be processed
293  *   @payload  : event payload. Can be NULL if not needed.
294  *
295  * RETURN     : int32_t type of status
296  *              NO_ERROR  -- success
297  *              none-zero failure code
298  *==========================================================================*/
stateMachine(qcamera_sm_evt_enum_t evt,void * payload)299 int32_t QCameraStateMachine::stateMachine(qcamera_sm_evt_enum_t evt, void *payload)
300 {
301     int32_t rc = NO_ERROR;
302     LOGL("m_state %d, event (%d)", m_state, evt);
303     switch (m_state) {
304     case QCAMERA_SM_STATE_PREVIEW_STOPPED:
305         rc = procEvtPreviewStoppedState(evt, payload);
306         break;
307     case QCAMERA_SM_STATE_PREVIEW_READY:
308         rc = procEvtPreviewReadyState(evt, payload);
309         break;
310     case QCAMERA_SM_STATE_PREVIEWING:
311         rc = procEvtPreviewingState(evt, payload);
312         break;
313     case QCAMERA_SM_STATE_PREPARE_SNAPSHOT:
314         rc = procEvtPrepareSnapshotState(evt, payload);
315         break;
316     case QCAMERA_SM_STATE_PIC_TAKING:
317         rc = procEvtPicTakingState(evt, payload);
318         break;
319     case QCAMERA_SM_STATE_RECORDING:
320         rc = procEvtRecordingState(evt, payload);
321         break;
322     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
323         rc = procEvtVideoPicTakingState(evt, payload);
324         break;
325     case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
326         rc = procEvtPreviewPicTakingState(evt, payload);
327         break;
328     default:
329         break;
330     }
331 
332     return rc;
333 }
334 
335 /*===========================================================================
336  * FUNCTION   : procEvtPreviewStoppedState
337  *
338  * DESCRIPTION: finite state machine function to handle event in state of
339  *              QCAMERA_SM_STATE_PREVIEW_STOPPED.
340  *
341  * PARAMETERS :
342  *   @evt      : event to be processed
343  *   @payload  : event payload. Can be NULL if not needed.
344  *
345  * RETURN     : int32_t type of status
346  *              NO_ERROR  -- success
347  *              none-zero failure code
348  *==========================================================================*/
procEvtPreviewStoppedState(qcamera_sm_evt_enum_t evt,void * payload)349 int32_t QCameraStateMachine::procEvtPreviewStoppedState(qcamera_sm_evt_enum_t evt,
350                                                         void *payload)
351 {
352     int32_t rc = NO_ERROR;
353     qcamera_api_result_t result;
354     memset(&result, 0, sizeof(qcamera_api_result_t));
355 
356     LOGL("event (%d)", evt);
357     switch (evt) {
358     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
359         {
360             rc = m_parent->setPreviewWindow((struct preview_stream_ops *)payload);
361             result.status = rc;
362             result.request_api = evt;
363             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
364             m_parent->signalAPIResult(&result);
365         }
366         break;
367     case QCAMERA_SM_EVT_SET_CALLBACKS:
368         {
369             qcamera_sm_evt_setcb_payload_t *setcbs =
370                 (qcamera_sm_evt_setcb_payload_t *)payload;
371             rc = m_parent->setCallBacks(setcbs->notify_cb,
372                                         setcbs->data_cb,
373                                         setcbs->data_cb_timestamp,
374                                         setcbs->get_memory,
375                                         setcbs->user);
376             result.status = rc;
377             result.request_api = evt;
378             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
379             m_parent->signalAPIResult(&result);
380         }
381         break;
382     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
383         {
384             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
385             if (NO_ERROR != rc) {
386                 LOGE("Param init deferred work failed");
387             } else {
388                 rc = m_parent->enableMsgType(*((int32_t *)payload));
389             }
390             result.status = rc;
391             result.request_api = evt;
392             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
393             m_parent->signalAPIResult(&result);
394         }
395         break;
396     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
397         {
398             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
399             if (NO_ERROR != rc) {
400                 LOGE("Param init deferred work failed");
401             } else {
402                 rc = m_parent->disableMsgType(*((int32_t *)payload));
403             }
404             result.status = rc;
405             result.request_api = evt;
406             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
407             m_parent->signalAPIResult(&result);
408         }
409         break;
410     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
411         {
412             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
413             result.status = rc;
414             result.request_api = evt;
415             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
416             result.enabled = enabled;
417             m_parent->signalAPIResult(&result);
418         }
419         break;
420     case QCAMERA_SM_EVT_SET_PARAMS:
421         {
422             bool needRestart = false;
423 
424             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
425             if (NO_ERROR != rc) {
426                 LOGE("Param init deferred work failed");
427             } else {
428                 rc = m_parent->updateParameters((char*)payload, needRestart);
429             }
430             result.status = rc;
431             result.request_api = evt;
432             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
433             m_parent->signalAPIResult(&result);
434         }
435         break;
436     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
437         {
438             m_parent->m_memoryPool.clear();
439             result.status = rc;
440             result.request_api = evt;
441             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
442             m_parent->signalAPIResult(&result);
443         }
444         break;
445     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
446         {
447             rc = m_parent->commitParameterChanges();
448             result.status = rc;
449             result.request_api = evt;
450             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
451             m_parent->signalAPIResult(&result);
452         }
453         break;
454     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
455         {
456             m_parent->setNeedRestart(false);
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_GET_PARAMS:
464         {
465             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
466             if (NO_ERROR != rc) {
467                 LOGE("Param init deferred work failed");
468                 char* nullParams = (char *)malloc(1);
469                 if (nullParams) {
470                     memset(nullParams, 0, 1);
471                 }
472                 result.params = nullParams;
473             } else {
474                 result.params = m_parent->getParameters();
475             }
476             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
477             result.status = rc;
478             result.request_api = evt;
479             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
480             m_parent->signalAPIResult(&result);
481         }
482         break;
483     case QCAMERA_SM_EVT_PUT_PARAMS:
484         {
485             rc = m_parent->putParameters((char*)payload);
486             result.status = rc;
487             result.request_api = evt;
488             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
489             m_parent->signalAPIResult(&result);
490         }
491         break;
492     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
493         {
494             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
495             if (NO_ERROR != rc) {
496                 LOGE("Param init deferred work failed");
497             } else {
498                 rc = m_parent->preparePreview();
499             }
500             if (rc == NO_ERROR) {
501                 //prepare preview success, move to ready state
502                 m_state = QCAMERA_SM_STATE_PREVIEW_READY;
503             }
504             result.status = rc;
505             result.request_api = evt;
506             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
507             m_parent->signalAPIResult(&result);
508         }
509         break;
510     case QCAMERA_SM_EVT_START_PREVIEW:
511         {
512             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
513             if (NO_ERROR != rc) {
514                 LOGE("Param init deferred work failed");
515             } else if (m_parent->mPreviewWindow == NULL) {
516                 rc = m_parent->preparePreview();
517                 if(rc == NO_ERROR) {
518                     // preview window is not set yet, move to previewReady state
519                     m_state = QCAMERA_SM_STATE_PREVIEW_READY;
520                 } else {
521                     LOGE("preparePreview failed");
522                 }
523             } else {
524                 rc = m_parent->preparePreview();
525                 if (rc == NO_ERROR) {
526                     applyDelayedMsgs();
527                     rc = m_parent->startPreview();
528                     if (rc != NO_ERROR) {
529                         m_parent->unpreparePreview();
530                     } else {
531                         // start preview success, move to previewing state
532                         m_state = QCAMERA_SM_STATE_PREVIEWING;
533                     }
534                 }
535             }
536             result.status = rc;
537             result.request_api = evt;
538             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
539             m_parent->signalAPIResult(&result);
540         }
541         break;
542     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
543         {
544             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
545             if (NO_ERROR != rc) {
546                 LOGE("Param init deferred work failed");
547             } else {
548                 rc = m_parent->preparePreview();
549             }
550             if (rc == NO_ERROR) {
551                 applyDelayedMsgs();
552                 rc = m_parent->startPreview();
553                 if (rc != NO_ERROR) {
554                     m_parent->unpreparePreview();
555                 } else {
556                     m_state = QCAMERA_SM_STATE_PREVIEWING;
557                 }
558             }
559             result.status = rc;
560             result.request_api = evt;
561             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
562             m_parent->signalAPIResult(&result);
563         }
564     break;
565     case QCAMERA_SM_EVT_STOP_PREVIEW:
566         {
567             // no op needed here
568             LOGW("already in preview stopped state, do nothing");
569             result.status = NO_ERROR;
570             result.request_api = evt;
571             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
572             m_parent->signalAPIResult(&result);
573         }
574         break;
575     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
576     case QCAMERA_SM_EVT_RECORDING_ENABLED:
577         {
578             result.status = NO_ERROR;
579             result.request_api = evt;
580             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
581             result.enabled = 0;
582             m_parent->signalAPIResult(&result);
583         }
584         break;
585     case QCAMERA_SM_EVT_RELEASE:
586         {
587             rc = m_parent->release();
588             result.status = rc;
589             result.request_api = evt;
590             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
591             m_parent->signalAPIResult(&result);
592         }
593         break;
594     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
595         {
596             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
597             result.status = rc;
598             result.request_api = evt;
599             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
600             m_parent->signalAPIResult(&result);
601         }
602         break;
603     case QCAMERA_SM_EVT_DUMP:
604         {
605             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
606             if (NO_ERROR != rc) {
607                 LOGE("Param init deferred work failed");
608             } else {
609                 rc = m_parent->dump(*((int *)payload));
610             }
611             result.status = rc;
612             result.request_api = evt;
613             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
614             m_parent->signalAPIResult(&result);
615         }
616         break;
617     case QCAMERA_SM_EVT_SEND_COMMAND:
618         {
619             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
620             if (NO_ERROR != rc) {
621                 LOGE("Param init deferred work failed");
622             } else {
623                 qcamera_sm_evt_command_payload_t *cmd_payload =
624                         (qcamera_sm_evt_command_payload_t *)payload;
625                 rc = m_parent->sendCommand(cmd_payload->cmd,
626                         cmd_payload->arg1,
627                         cmd_payload->arg2);
628             }
629             result.status = rc;
630             result.request_api = evt;
631             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
632             m_parent->signalAPIResult(&result);
633         }
634         break;
635     case QCAMERA_SM_EVT_PRE_START_RECORDING:
636     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
637     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
638     case QCAMERA_SM_EVT_START_RECORDING:
639     case QCAMERA_SM_EVT_STOP_RECORDING:
640     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
641     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
642     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
643     case QCAMERA_SM_EVT_TAKE_PICTURE:
644         {
645             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
646             rc = INVALID_OPERATION;
647             result.status = rc;
648             result.request_api = evt;
649             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
650             m_parent->signalAPIResult(&result);
651         }
652         break;
653     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
654     case QCAMERA_SM_EVT_CANCEL_PICTURE:
655         {
656             // no op needed here
657             LOGW("No ops for evt(%d) in state(%d)", evt, m_state);
658             result.status = NO_ERROR;
659             result.request_api = evt;
660             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
661             m_parent->signalAPIResult(&result);
662         }
663         break;
664     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
665         {
666             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
667             if (NO_ERROR != rc) {
668                 LOGE("Param init deferred work failed");
669             } else {
670                 rc = m_parent->cancelAutoFocus();
671             }
672             result.status = rc;
673             result.request_api = evt;
674             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
675             m_parent->signalAPIResult(&result);
676         }
677         break;
678     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
679         {
680             int32_t faceID = 0;
681             qcamera_sm_evt_reg_face_payload_t *reg_payload =
682                 (qcamera_sm_evt_reg_face_payload_t *)payload;
683             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
684                                              reg_payload->config,
685                                              faceID);
686             result.status = rc;
687             result.request_api = evt;
688             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
689             result.handle = faceID;
690             m_parent->signalAPIResult(&result);
691         }
692         break;
693     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
694         {
695             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
696             if (NO_ERROR != rc) {
697                 LOGE("Param init deferred work failed");
698             } else {
699                 rc = m_parent->updateThermalLevel(payload);
700             }
701         }
702         break;
703     case QCAMERA_SM_EVT_EVT_NOTIFY:
704         {
705             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
706             switch (cam_evt->server_event_type) {
707             case CAM_EVENT_TYPE_DAEMON_DIED:
708                 {
709                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
710                                             CAMERA_ERROR_SERVER_DIED,
711                                             0);
712                 }
713                 break;
714             default:
715                 LOGE("Invalid internal event %d in state(%d)",
716                              cam_evt->server_event_type, m_state);
717                 break;
718             }
719         }
720         break;
721     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
722         {
723             // No ops, but need to notify
724             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
725             result.status = rc;
726             result.request_api = evt;
727             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
728             m_parent->signalEvtResult(&result);
729         }
730        break;
731     case QCAMERA_SM_EVT_EVT_INTERNAL:
732        {
733            qcamera_sm_internal_evt_payload_t *internal_evt =
734                (qcamera_sm_internal_evt_payload_t *)payload;
735            switch (internal_evt->evt_type) {
736            case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
737                rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
738                if (NO_ERROR != rc) {
739                    LOGE("Param init deferred work failed");
740                } else {
741                    rc = m_parent->mParameters.updateFlashMode(internal_evt->led_data);
742                }
743                break;
744            default:
745                LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
746                break;
747            }
748        }
749        break;
750     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
751     default:
752         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
753         break;
754     }
755 
756     return rc;
757 }
758 
759 /*===========================================================================
760  * FUNCTION   : procEvtPreviewReadyState
761  *
762  * DESCRIPTION: finite state machine function to handle event in state of
763  *              QCAMERA_SM_STATE_PREVIEW_READY.
764  *
765  * PARAMETERS :
766  *   @evt      : event to be processed
767  *   @payload  : event payload. Can be NULL if not needed.
768  *
769  * RETURN     : int32_t type of status
770  *              NO_ERROR  -- success
771  *              none-zero failure code
772  *==========================================================================*/
procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt,void * payload)773 int32_t QCameraStateMachine::procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt,
774                                                       void *payload)
775 {
776     int32_t rc = NO_ERROR;
777     qcamera_api_result_t result;
778     memset(&result, 0, sizeof(qcamera_api_result_t));
779 
780     LOGL("event (%d)", evt);
781     switch (evt) {
782     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
783         {
784             m_parent->setPreviewWindow((struct preview_stream_ops *)payload);
785             if (m_parent->mPreviewWindow != NULL) {
786                 applyDelayedMsgs();
787                 rc = m_parent->startPreview();
788                 if (rc != NO_ERROR) {
789                     m_parent->unpreparePreview();
790                     m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
791                 } else {
792                     m_state = QCAMERA_SM_STATE_PREVIEWING;
793                 }
794             }
795 
796             result.status = rc;
797             result.request_api = evt;
798             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
799             m_parent->signalAPIResult(&result);
800         }
801         break;
802     case QCAMERA_SM_EVT_SET_CALLBACKS:
803         {
804             qcamera_sm_evt_setcb_payload_t *setcbs =
805                 (qcamera_sm_evt_setcb_payload_t *)payload;
806             rc = m_parent->setCallBacks(setcbs->notify_cb,
807                                         setcbs->data_cb,
808                                         setcbs->data_cb_timestamp,
809                                         setcbs->get_memory,
810                                         setcbs->user);
811             result.status = rc;
812             result.request_api = evt;
813             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
814             m_parent->signalAPIResult(&result);
815         }
816         break;
817     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
818         {
819             rc = m_parent->enableMsgType(*((int32_t *)payload));
820             result.status = rc;
821             result.request_api = evt;
822             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
823             m_parent->signalAPIResult(&result);
824         }
825         break;
826     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
827         {
828             rc = m_parent->disableMsgType(*((int32_t *)payload));
829             result.status = rc;
830             result.request_api = evt;
831             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
832             m_parent->signalAPIResult(&result);
833         }
834         break;
835     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
836         {
837             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
838             result.status = rc;
839             result.request_api = evt;
840             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
841             result.enabled = enabled;
842             m_parent->signalAPIResult(&result);
843         }
844         break;
845     case QCAMERA_SM_EVT_SET_PARAMS:
846         {
847             bool needRestart = false;
848             rc = m_parent->updateParameters((char*)payload, needRestart);
849             result.status = rc;
850             result.request_api = evt;
851             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
852             m_parent->signalAPIResult(&result);
853         }
854         break;
855     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
856         {
857             LOGD("Stopping preview...");
858             // need restart preview for parameters to take effect
859             m_parent->unpreparePreview();
860             // Clear memory pools
861             m_parent->m_memoryPool.clear();
862             result.status = rc;
863             result.request_api = evt;
864             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
865             m_parent->signalAPIResult(&result);
866         }
867         break;
868     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
869         {
870             rc = m_parent->commitParameterChanges();
871             result.status = rc;
872             result.request_api = evt;
873             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
874             m_parent->signalAPIResult(&result);
875         }
876         break;
877     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
878         {
879             // prepare preview again
880             rc = m_parent->preparePreview();
881             if (rc != NO_ERROR) {
882                 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
883             }
884             m_parent->setNeedRestart(false);
885             result.status = rc;
886             result.request_api = evt;
887             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
888             m_parent->signalAPIResult(&result);
889         }
890         break;
891     case QCAMERA_SM_EVT_GET_PARAMS:
892         {
893             result.params = m_parent->getParameters();
894             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
895             result.status = rc;
896             result.request_api = evt;
897             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
898             m_parent->signalAPIResult(&result);
899         }
900         break;
901     case QCAMERA_SM_EVT_PUT_PARAMS:
902         {
903             rc = m_parent->putParameters((char*)payload);
904             result.status = rc;
905             result.request_api = evt;
906             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
907             m_parent->signalAPIResult(&result);
908         }
909         break;
910     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
911         {
912             // no ops here
913             rc = NO_ERROR;
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_START_NODISPLAY_PREVIEW:
921         {
922             rc = m_parent->startPreview();
923             if (rc != NO_ERROR) {
924                 m_parent->unpreparePreview();
925                 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
926             } else {
927                 m_state = QCAMERA_SM_STATE_PREVIEWING;
928             }
929             // no ops here
930             rc = NO_ERROR;
931             result.status = rc;
932             result.request_api = evt;
933             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
934             m_parent->signalAPIResult(&result);
935         }
936         break;
937     case QCAMERA_SM_EVT_START_PREVIEW:
938         {
939             if (m_parent->mPreviewWindow != NULL) {
940                 rc = m_parent->startPreview();
941                 if (rc != NO_ERROR) {
942                     m_parent->unpreparePreview();
943                     m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
944                 } else {
945                     m_state = QCAMERA_SM_STATE_PREVIEWING;
946                 }
947             }
948             // no ops here
949             rc = NO_ERROR;
950             result.status = rc;
951             result.request_api = evt;
952             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
953             m_parent->signalAPIResult(&result);
954         }
955         break;
956     case QCAMERA_SM_EVT_STOP_PREVIEW:
957         {
958             m_parent->unpreparePreview();
959             rc = 0;
960             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
961             result.status = rc;
962             result.request_api = evt;
963             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
964             m_parent->signalAPIResult(&result);
965         }
966         break;
967     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
968         {
969             rc = NO_ERROR;
970             result.status = rc;
971             result.request_api = evt;
972             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
973             result.enabled = 1;
974             m_parent->signalAPIResult(&result);
975         }
976         break;
977     case QCAMERA_SM_EVT_RECORDING_ENABLED:
978         {
979             rc = 0;
980             result.status = rc;
981             result.request_api = evt;
982             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
983             result.enabled = 0;
984             m_parent->signalAPIResult(&result);
985         }
986         break;
987     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
988         {
989             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
990             result.status = rc;
991             result.request_api = evt;
992             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
993             m_parent->signalAPIResult(&result);
994         }
995         break;
996     case QCAMERA_SM_EVT_DUMP:
997         {
998             rc = m_parent->dump(*((int *)payload));
999             result.status = rc;
1000             result.request_api = evt;
1001             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1002             m_parent->signalAPIResult(&result);
1003         }
1004         break;
1005     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1006         {
1007             rc = m_parent->autoFocus();
1008             result.status = rc;
1009             result.request_api = evt;
1010             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1011             m_parent->signalAPIResult(&result);
1012         }
1013         break;
1014     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1015         {
1016             rc = m_parent->cancelAutoFocus();
1017             result.status = rc;
1018             result.request_api = evt;
1019             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1020             m_parent->signalAPIResult(&result);
1021         }
1022         break;
1023     case QCAMERA_SM_EVT_SEND_COMMAND:
1024         {
1025             qcamera_sm_evt_command_payload_t *cmd_payload =
1026                 (qcamera_sm_evt_command_payload_t *)payload;
1027             rc = m_parent->sendCommand(cmd_payload->cmd,
1028                                        cmd_payload->arg1,
1029                                        cmd_payload->arg2);
1030             result.status = rc;
1031             result.request_api = evt;
1032             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1033             m_parent->signalAPIResult(&result);
1034         }
1035         break;
1036     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
1037         {
1038             int32_t faceID = 0;
1039             qcamera_sm_evt_reg_face_payload_t *reg_payload =
1040                 (qcamera_sm_evt_reg_face_payload_t *)payload;
1041             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
1042                                              reg_payload->config,
1043                                              faceID);
1044             result.status = rc;
1045             result.request_api = evt;
1046             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
1047             result.handle = faceID;
1048             m_parent->signalAPIResult(&result);
1049         }
1050         break;
1051     case QCAMERA_SM_EVT_PRE_START_RECORDING:
1052     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
1053     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
1054     case QCAMERA_SM_EVT_START_RECORDING:
1055     case QCAMERA_SM_EVT_STOP_RECORDING:
1056     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
1057     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
1058     case QCAMERA_SM_EVT_TAKE_PICTURE:
1059     case QCAMERA_SM_EVT_CANCEL_PICTURE:
1060     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1061     case QCAMERA_SM_EVT_RELEASE:
1062         {
1063             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
1064             rc = INVALID_OPERATION;
1065             result.status = rc;
1066             result.request_api = evt;
1067             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1068             m_parent->signalAPIResult(&result);
1069         }
1070         break;
1071     case QCAMERA_SM_EVT_EVT_NOTIFY:
1072         {
1073             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1074             switch (cam_evt->server_event_type) {
1075             case CAM_EVENT_TYPE_DAEMON_DIED:
1076                 {
1077                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
1078                                             CAMERA_ERROR_SERVER_DIED,
1079                                             0);
1080                 }
1081                 break;
1082             default:
1083                 LOGE("Invalid internal event %d in state(%d)",
1084                              cam_evt->server_event_type, m_state);
1085                 break;
1086             }
1087         }
1088         break;
1089     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1090         {
1091             // No ops, but need to notify
1092             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1093             result.status = rc;
1094             result.request_api = evt;
1095             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1096             m_parent->signalEvtResult(&result);
1097         }
1098        break;
1099     case QCAMERA_SM_EVT_EVT_INTERNAL:
1100        {
1101            qcamera_sm_internal_evt_payload_t *internal_evt =
1102                    (qcamera_sm_internal_evt_payload_t *)payload;
1103            switch (internal_evt->evt_type) {
1104            case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
1105                rc = m_parent->mParameters.updateFlashMode(internal_evt->led_data);
1106                break;
1107            default:
1108                LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1109                break;
1110            }
1111        }
1112        break;
1113     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1114     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1115     default:
1116         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1117         break;
1118     }
1119 
1120     return rc;
1121 }
1122 
1123 /*===========================================================================
1124  * FUNCTION   : procEvtPreviewingState
1125  *
1126  * DESCRIPTION: finite state machine function to handle event in state of
1127  *              QCAMERA_SM_STATE_PREVIEWING.
1128  *
1129  * PARAMETERS :
1130  *   @evt      : event to be processed
1131  *   @payload  : event payload. Can be NULL if not needed.
1132  *
1133  * RETURN     : int32_t type of status
1134  *              NO_ERROR  -- success
1135  *              none-zero failure code
1136  *==========================================================================*/
procEvtPreviewingState(qcamera_sm_evt_enum_t evt,void * payload)1137 int32_t QCameraStateMachine::procEvtPreviewingState(qcamera_sm_evt_enum_t evt,
1138                                                     void *payload)
1139 {
1140     int32_t rc = NO_ERROR;
1141     qcamera_api_result_t result;
1142     memset(&result, 0, sizeof(qcamera_api_result_t));
1143 
1144     LOGL("event (%d)", evt);
1145     switch (evt) {
1146     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1147         {
1148             // Error setting preview window during previewing
1149             LOGE("Error!! cannot set preview window when preview is running");
1150             rc = INVALID_OPERATION;
1151             result.status = rc;
1152             result.request_api = evt;
1153             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1154             m_parent->signalAPIResult(&result);
1155         }
1156         break;
1157     case QCAMERA_SM_EVT_SET_CALLBACKS:
1158         {
1159             qcamera_sm_evt_setcb_payload_t *setcbs =
1160                 (qcamera_sm_evt_setcb_payload_t *)payload;
1161             rc = m_parent->setCallBacks(setcbs->notify_cb,
1162                                         setcbs->data_cb,
1163                                         setcbs->data_cb_timestamp,
1164                                         setcbs->get_memory,
1165                                         setcbs->user);
1166             result.status = rc;
1167             result.request_api = evt;
1168             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1169             m_parent->signalAPIResult(&result);
1170         }
1171         break;
1172     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1173         {
1174             int32_t enable_msgs = *((int32_t *)payload);
1175             if (m_bDelayPreviewMsgs &&
1176                     (enable_msgs & CAMERA_MSG_PREVIEW_FRAME)) {
1177                 enable_msgs &= ~CAMERA_MSG_PREVIEW_FRAME;
1178                 m_DelayedMsgs = CAMERA_MSG_PREVIEW_FRAME;
1179             }
1180             rc = m_parent->enableMsgType(enable_msgs);
1181             result.status = rc;
1182             result.request_api = evt;
1183             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1184             m_parent->signalAPIResult(&result);
1185         }
1186         break;
1187     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1188         {
1189             int32_t disable_msgs = *((int32_t *)payload);
1190             if (m_bDelayPreviewMsgs && m_DelayedMsgs) {
1191                 m_DelayedMsgs &= ~disable_msgs;
1192                 if (0 == m_DelayedMsgs) {
1193                     m_bDelayPreviewMsgs = false;
1194                 }
1195             }
1196             rc = m_parent->disableMsgType(disable_msgs);
1197             result.status = rc;
1198             result.request_api = evt;
1199             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1200             m_parent->signalAPIResult(&result);
1201         }
1202         break;
1203     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1204         {
1205             int32_t msgs = *((int32_t *)payload);
1206             int enabled = m_parent->msgTypeEnabled(msgs);
1207             if (m_bDelayPreviewMsgs && m_DelayedMsgs) {
1208                 enabled |= (msgs & m_DelayedMsgs);
1209             }
1210             result.status = rc;
1211             result.request_api = evt;
1212             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1213             result.enabled = enabled;
1214             m_parent->signalAPIResult(&result);
1215         }
1216         break;
1217     case QCAMERA_SM_EVT_SET_PARAMS:
1218         {
1219             bool needRestart = false;
1220             rc = m_parent->updateParameters((char*)payload, needRestart);
1221             result.status = rc;
1222             result.request_api = evt;
1223             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1224             m_parent->signalAPIResult(&result);
1225         }
1226         break;
1227     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
1228         {
1229             LOGD("Stopping preview...");
1230             // stop preview
1231             rc = m_parent->stopPreview();
1232             // Clear memory pools
1233             m_parent->m_memoryPool.clear();
1234             result.status = rc;
1235             result.request_api = evt;
1236             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1237             m_parent->signalAPIResult(&result);
1238         }
1239         break;
1240     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
1241         {
1242             rc = m_parent->commitParameterChanges();
1243             result.status = rc;
1244             result.request_api = evt;
1245             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1246             m_parent->signalAPIResult(&result);
1247         }
1248         break;
1249     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
1250         {
1251             // start preview again
1252             rc = m_parent->preparePreview();
1253             if (rc == NO_ERROR) {
1254                 applyDelayedMsgs();
1255                 rc = m_parent->startPreview();
1256                 if (rc != NO_ERROR) {
1257                     m_parent->unpreparePreview();
1258                 }
1259                 if (rc != NO_ERROR) {
1260                     m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1261                 }
1262             }
1263             m_parent->setNeedRestart(false);
1264             result.status            = rc;
1265             result.request_api       = evt;
1266             result.result_type       = QCAMERA_API_RESULT_TYPE_DEF;
1267             m_parent->signalAPIResult(&result);
1268         }
1269         break;
1270     case QCAMERA_SM_EVT_GET_PARAMS:
1271         {
1272             result.params = m_parent->getParameters();
1273             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
1274             result.status = rc;
1275             result.request_api = evt;
1276             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1277             m_parent->signalAPIResult(&result);
1278         }
1279         break;
1280     case QCAMERA_SM_EVT_PUT_PARAMS:
1281         {
1282             rc = m_parent->putParameters((char*)payload);
1283             result.status = rc;
1284             result.request_api = evt;
1285             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1286             m_parent->signalAPIResult(&result);
1287         }
1288         break;
1289     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
1290         {
1291             // no ops here
1292             LOGW("Already in preview ready state, no ops here");
1293             rc = NO_ERROR;
1294             result.status = rc;
1295             result.request_api = evt;
1296             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1297             m_parent->signalAPIResult(&result);
1298         }
1299         break;
1300     case QCAMERA_SM_EVT_START_PREVIEW:
1301     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1302         {
1303             // no ops here
1304             LOGW("Already in previewing, no ops here to start preview");
1305             applyDelayedMsgs();
1306             rc = NO_ERROR;
1307             result.status = rc;
1308             result.request_api = evt;
1309             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1310             m_parent->signalAPIResult(&result);
1311         }
1312         break;
1313     case QCAMERA_SM_EVT_STOP_PREVIEW:
1314         {
1315             rc = m_parent->stopPreview();
1316             applyDelayedMsgs();
1317             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1318             result.status = rc;
1319             result.request_api = evt;
1320             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1321             m_parent->signalAPIResult(&result);
1322         }
1323         break;
1324     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1325         {
1326             applyDelayedMsgs();
1327             rc = NO_ERROR;
1328             result.status = rc;
1329             result.request_api = evt;
1330             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1331             result.enabled = 1;
1332             m_parent->signalAPIResult(&result);
1333         }
1334         break;
1335     case QCAMERA_SM_EVT_RECORDING_ENABLED:
1336         {
1337             rc = NO_ERROR;
1338             result.status = rc;
1339             result.request_api = evt;
1340             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1341             result.enabled = 0;
1342             m_parent->signalAPIResult(&result);
1343         }
1344         break;
1345     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1346         {
1347             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
1348             result.status = rc;
1349             result.request_api = evt;
1350             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1351             m_parent->signalAPIResult(&result);
1352         }
1353         break;
1354     case QCAMERA_SM_EVT_DUMP:
1355         {
1356             rc = m_parent->dump(*((int *)payload));
1357             result.status = rc;
1358             result.request_api = evt;
1359             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1360             m_parent->signalAPIResult(&result);
1361         }
1362         break;
1363     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1364         {
1365             rc = m_parent->autoFocus();
1366             result.status = rc;
1367             result.request_api = evt;
1368             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1369             m_parent->signalAPIResult(&result);
1370         }
1371         break;
1372     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1373         {
1374             rc = m_parent->cancelAutoFocus();
1375             result.status = rc;
1376             result.request_api = evt;
1377             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1378             m_parent->signalAPIResult(&result);
1379         }
1380         break;
1381     case QCAMERA_SM_EVT_PRE_START_RECORDING:
1382         {
1383             rc = m_parent->preStartRecording();
1384             result.status = rc;
1385             result.request_api = evt;
1386             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1387             m_parent->signalAPIResult(&result);
1388         }
1389         break;
1390     case QCAMERA_SM_EVT_START_RECORDING:
1391         {
1392             rc = m_parent->startRecording();
1393             if (rc == NO_ERROR) {
1394                 // move state to recording state
1395                 m_state = QCAMERA_SM_STATE_RECORDING;
1396                 applyDelayedMsgs();
1397             }
1398             result.status = rc;
1399             result.request_api = evt;
1400             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1401             m_parent->signalAPIResult(&result);
1402         }
1403         break;
1404     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
1405         {
1406             rc = m_parent->prepareHardwareForSnapshot(FALSE);
1407             if (rc == NO_ERROR) {
1408                 // Do not signal API result in this case.
1409                 // Need to wait for snapshot done in metadta.
1410                 m_state = QCAMERA_SM_STATE_PREPARE_SNAPSHOT;
1411                 applyDelayedMsgs();
1412             } else {
1413                 // Do not change state in this case.
1414                 LOGE("prepareHardwareForSnapshot failed %d",
1415                      rc);
1416 
1417                 result.status = rc;
1418                 result.request_api = evt;
1419                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1420                 m_parent->signalAPIResult(&result);
1421             }
1422         }
1423         break;
1424     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
1425         {
1426             rc = m_parent->preTakePicture();
1427             result.status = rc;
1428             result.request_api = evt;
1429             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1430             m_parent->signalAPIResult(&result);
1431         }
1432         break;
1433     case QCAMERA_SM_EVT_TAKE_PICTURE:
1434        {
1435            LOGL("QCAMERA_SM_EVT_TAKE_PICTURE ");
1436            if ( m_parent->mParameters.getRecordingHintValue() == true) {
1437                 m_parent->stopPreview();
1438                 m_parent->mParameters.updateRecordingHintValue(FALSE);
1439                 // start preview again
1440                 rc = m_parent->preparePreview();
1441                 if (rc == NO_ERROR) {
1442                     rc = m_parent->startPreview();
1443                     if (rc != NO_ERROR) {
1444                         m_parent->unpreparePreview();
1445                     }
1446                 }
1447            }
1448            if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
1449                bool restartPreview = m_parent->isPreviewRestartEnabled();
1450                if ((restartPreview) && (m_parent->mParameters.getManualCaptureMode()
1451                        >= CAM_MANUAL_CAPTURE_TYPE_3)) {
1452                    /* stop preview and disable ZSL now */
1453                    m_parent->stopPreview();
1454                    m_parent->mParameters.updateZSLModeValue(FALSE);
1455                    m_RestoreZSL = TRUE;
1456                    m_bDelayPreviewMsgs = true;
1457                    m_state = QCAMERA_SM_STATE_PIC_TAKING;
1458                } else {
1459                    m_state = QCAMERA_SM_STATE_PREVIEW_PIC_TAKING;
1460                    m_bDelayPreviewMsgs = true;
1461                }
1462 
1463                rc = m_parent->takePicture();
1464                if (rc != NO_ERROR) {
1465                    // move state to previewing state
1466                    m_parent->unconfigureAdvancedCapture();
1467                    m_state = QCAMERA_SM_STATE_PREVIEWING;
1468                }
1469                if (!(m_parent->isRetroPicture()) || (rc != NO_ERROR)) {
1470                    LOGD("signal API result, m_state = %d",
1471                           m_state);
1472                    result.status = rc;
1473                    result.request_api = evt;
1474                    result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1475                    m_parent->signalAPIResult(&result);
1476                }
1477            } else {
1478                m_state = QCAMERA_SM_STATE_PIC_TAKING;
1479                rc = m_parent->takePicture();
1480                if (rc != NO_ERROR) {
1481                    int32_t temp_rc = NO_ERROR;
1482                    // move state to preview stopped state
1483                    m_parent->unconfigureAdvancedCapture();
1484                    m_parent->stopPreview();
1485                    // start preview again
1486                    temp_rc = m_parent->preparePreview();
1487                    if (temp_rc == NO_ERROR) {
1488                        temp_rc = m_parent->startPreview();
1489                        if (temp_rc != NO_ERROR) {
1490                            m_parent->unpreparePreview();
1491                            m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1492                        } else {
1493                            m_state = QCAMERA_SM_STATE_PREVIEWING;
1494                        }
1495                    } else {
1496                        m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1497                    }
1498                }
1499                result.status = rc;
1500                result.request_api = evt;
1501                result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1502                m_parent->signalAPIResult(&result);
1503            }
1504         }
1505         break;
1506     case QCAMERA_SM_EVT_SEND_COMMAND:
1507         {
1508             qcamera_sm_evt_command_payload_t *cmd_payload =
1509                     (qcamera_sm_evt_command_payload_t *)payload;
1510             rc = m_parent->sendCommand(cmd_payload->cmd,
1511                     cmd_payload->arg1,
1512                     cmd_payload->arg2);
1513             m_bPreviewNeedsRestart =
1514                     (QCAMERA_SM_EVT_RESTART_PERVIEW == cmd_payload->arg1);
1515             m_bPreviewDelayedRestart =
1516                     (QCAMERA_SM_EVT_DELAYED_RESTART == cmd_payload->arg2);
1517 
1518 #ifndef VANILLA_HAL
1519             if ((CAMERA_CMD_LONGSHOT_ON == cmd_payload->cmd) &&
1520                     (m_bPreviewNeedsRestart)) {
1521                 m_parent->stopPreview();
1522                 // Clear memory pools
1523                 m_parent->m_memoryPool.clear();
1524 
1525                 if (!m_bPreviewDelayedRestart) {
1526                     // start preview again
1527                     rc = m_parent->preparePreview();
1528                     if (rc == NO_ERROR) {
1529                         applyDelayedMsgs();
1530                         rc = m_parent->startPreview();
1531                         if (rc != NO_ERROR) {
1532                             m_parent->unpreparePreview();
1533                         }
1534                     }
1535                 }
1536             }
1537 #endif
1538             result.status = rc;
1539             result.request_api = evt;
1540             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1541             m_parent->signalAPIResult(&result);
1542         }
1543         break;
1544     case QCAMERA_SM_EVT_SEND_COMMAND_RESTART:
1545         {
1546 #ifndef VANILLA_HAL
1547             qcamera_sm_evt_command_payload_t *cmd_payload =
1548                     (qcamera_sm_evt_command_payload_t *)payload;
1549             if ((CAMERA_CMD_LONGSHOT_ON == cmd_payload->cmd) &&
1550                     (m_bPreviewNeedsRestart) &&
1551                     (m_bPreviewDelayedRestart)) {
1552                 // start preview again
1553                 rc = m_parent->preparePreview();
1554                 if (rc == NO_ERROR) {
1555                     rc = m_parent->startPreview();
1556                     if (rc != NO_ERROR) {
1557                         m_parent->unpreparePreview();
1558                     }
1559                 }
1560             }
1561 #endif
1562             result.status = rc;
1563             result.request_api = evt;
1564             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1565             m_parent->signalAPIResult(&result);
1566         }
1567         break;
1568     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
1569         {
1570             int32_t faceID = 0;
1571             qcamera_sm_evt_reg_face_payload_t *reg_payload =
1572                 (qcamera_sm_evt_reg_face_payload_t *)payload;
1573             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
1574                                              reg_payload->config,
1575                                              faceID);
1576             result.status = rc;
1577             result.request_api = evt;
1578             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
1579             result.handle = faceID;
1580             m_parent->signalAPIResult(&result);
1581         }
1582         break;
1583     case QCAMERA_SM_EVT_CANCEL_PICTURE:
1584     case QCAMERA_SM_EVT_STOP_RECORDING:
1585     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1586     case QCAMERA_SM_EVT_RELEASE:
1587         {
1588             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
1589             rc = INVALID_OPERATION;
1590             result.status = rc;
1591             result.request_api = evt;
1592             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1593             m_parent->signalAPIResult(&result);
1594         }
1595         break;
1596     case QCAMERA_SM_EVT_EVT_INTERNAL:
1597         {
1598             qcamera_sm_internal_evt_payload_t *internal_evt =
1599                 (qcamera_sm_internal_evt_payload_t *)payload;
1600             switch (internal_evt->evt_type) {
1601             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1602                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1603                 break;
1604             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
1605                 break;
1606             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
1607                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
1608                 break;
1609             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
1610                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
1611                 break;
1612             case QCAMERA_INTERNAL_EVT_CROP_INFO:
1613                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
1614                 break;
1615             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
1616                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
1617                 break;
1618             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
1619                 rc = m_parent->mParameters.updateFlashMode(internal_evt->led_data);
1620                 break;
1621             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
1622                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
1623                 break;
1624             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
1625                 rc = m_parent->processAEInfo(internal_evt->ae_data);
1626                 break;
1627             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
1628                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
1629                 break;
1630             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
1631                 rc = m_parent->processHDRData(internal_evt->hdr_data);
1632                 break;
1633             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
1634                 rc = m_parent->processRetroAECUnlock();
1635                 break;
1636             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
1637                 rc = m_parent->processZSLCaptureDone();
1638                 break;
1639             default:
1640                 LOGE("Invalid internal event %d in state(%d)",
1641                              internal_evt->evt_type, m_state);
1642                 break;
1643             }
1644         }
1645         break;
1646     case QCAMERA_SM_EVT_EVT_NOTIFY:
1647         {
1648             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1649             switch (cam_evt->server_event_type) {
1650             case CAM_EVENT_TYPE_DAEMON_DIED:
1651                 {
1652                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
1653                                             CAMERA_ERROR_SERVER_DIED,
1654                                             0);
1655                 }
1656                 break;
1657             default:
1658                 LOGW("no handling for server evt (%d) at this state",
1659                        cam_evt->server_event_type);
1660                 break;
1661             }
1662         }
1663         break;
1664     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1665         {
1666             rc = m_parent->updateThermalLevel(payload);
1667         }
1668         break;
1669     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1670         {
1671             // No ops, but need to notify
1672             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1673             result.status = rc;
1674             result.request_api = evt;
1675             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1676             m_parent->signalEvtResult(&result);
1677         }
1678        break;
1679     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
1680         {
1681             m_parent->stopPreview();
1682             result.status = rc;
1683             result.request_api = evt;
1684             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1685             m_parent->signalAPIResult(&result);
1686         }
1687        break;
1688     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
1689         {
1690             rc = m_parent->preparePreview();
1691             if (rc == NO_ERROR) {
1692                 rc = m_parent->startPreview();
1693             }
1694             result.status = rc;
1695             result.request_api = evt;
1696             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1697             m_parent->signalAPIResult(&result);
1698         }
1699        break;
1700     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1701     default:
1702         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1703         break;
1704     }
1705 
1706     return rc;
1707 }
1708 
1709 /*===========================================================================
1710  * FUNCTION   : procEvtPrepareSnapshotState
1711  *
1712  * DESCRIPTION: finite state machine function to handle event in state of
1713  *              QCAMERA_SM_STATE_PREPARE_SNAPSHOT.
1714  *
1715  * PARAMETERS :
1716  *   @evt      : event to be processed
1717  *   @payload  : event payload. Can be NULL if not needed.
1718  *
1719  * RETURN     : int32_t type of status
1720  *              NO_ERROR  -- success
1721  *              none-zero failure code
1722  *==========================================================================*/
procEvtPrepareSnapshotState(qcamera_sm_evt_enum_t evt,void * payload)1723 int32_t QCameraStateMachine::procEvtPrepareSnapshotState(qcamera_sm_evt_enum_t evt,
1724                                                     void *payload)
1725 {
1726     int32_t rc = NO_ERROR;
1727     qcamera_api_result_t result;
1728     memset(&result, 0, sizeof(qcamera_api_result_t));
1729 
1730     LOGL("event (%d)", evt);
1731     switch (evt) {
1732     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1733     case QCAMERA_SM_EVT_SET_CALLBACKS:
1734     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1735     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1736     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1737     case QCAMERA_SM_EVT_SET_PARAMS:
1738     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
1739     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
1740     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
1741     case QCAMERA_SM_EVT_GET_PARAMS:
1742     case QCAMERA_SM_EVT_PUT_PARAMS:
1743     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
1744     case QCAMERA_SM_EVT_START_PREVIEW:
1745     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1746     case QCAMERA_SM_EVT_STOP_PREVIEW:
1747     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1748     case QCAMERA_SM_EVT_RECORDING_ENABLED:
1749     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1750     case QCAMERA_SM_EVT_DUMP:
1751     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1752     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1753     case QCAMERA_SM_EVT_PRE_START_RECORDING:
1754     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
1755     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
1756     case QCAMERA_SM_EVT_START_RECORDING:
1757     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
1758     case QCAMERA_SM_EVT_TAKE_PICTURE:
1759     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
1760     case QCAMERA_SM_EVT_SEND_COMMAND:
1761     case QCAMERA_SM_EVT_CANCEL_PICTURE:
1762     case QCAMERA_SM_EVT_STOP_RECORDING:
1763     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1764     case QCAMERA_SM_EVT_RELEASE:
1765         {
1766             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
1767             rc = INVALID_OPERATION;
1768             result.status = rc;
1769             result.request_api = evt;
1770             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1771             m_parent->signalAPIResult(&result);
1772         }
1773         break;
1774     case QCAMERA_SM_EVT_EVT_INTERNAL:
1775         {
1776             qcamera_sm_internal_evt_payload_t *internal_evt =
1777                 (qcamera_sm_internal_evt_payload_t *)payload;
1778             switch (internal_evt->evt_type) {
1779             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1780                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1781                 break;
1782             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
1783                 m_parent->processPrepSnapshotDoneEvent(internal_evt->prep_snapshot_state);
1784                 m_state = QCAMERA_SM_STATE_PREVIEWING;
1785 
1786                 result.status = NO_ERROR;
1787                 result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
1788                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1789                 m_parent->signalAPIResult(&result);
1790                 break;
1791             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
1792                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
1793                 break;
1794             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
1795                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
1796                 break;
1797             case QCAMERA_INTERNAL_EVT_CROP_INFO:
1798                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
1799                 break;
1800             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
1801                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
1802                 break;
1803             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
1804                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1805                 break;
1806             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
1807                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
1808                 break;
1809             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
1810                 rc = m_parent->processAEInfo(internal_evt->ae_data);
1811                 break;
1812             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
1813                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
1814                 break;
1815             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
1816                 rc = m_parent->processHDRData(internal_evt->hdr_data);
1817                 break;
1818             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
1819                 rc = m_parent->processRetroAECUnlock();
1820                 break;
1821             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
1822                 rc = m_parent->processZSLCaptureDone();
1823                 break;
1824             default:
1825                 LOGE("Invalid internal event %d in state(%d)",
1826                              internal_evt->evt_type, m_state);
1827                 break;
1828             }
1829         }
1830         break;
1831     case QCAMERA_SM_EVT_EVT_NOTIFY:
1832         {
1833             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1834             switch (cam_evt->server_event_type) {
1835             case CAM_EVENT_TYPE_DAEMON_DIED:
1836                 {
1837                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
1838                                             CAMERA_ERROR_SERVER_DIED,
1839                                             0);
1840                 }
1841                 break;
1842             default:
1843                 LOGE("Invalid internal event %d in state(%d)",
1844                              cam_evt->server_event_type, m_state);
1845                 break;
1846             }
1847         }
1848         break;
1849     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1850         {
1851             // No ops, but need to notify
1852             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1853             result.status = rc;
1854             result.request_api = evt;
1855             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1856             m_parent->signalEvtResult(&result);
1857         }
1858        break;
1859     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1860         {
1861             rc = m_parent->updateThermalLevel(payload);
1862         }
1863         break;
1864     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1865     default:
1866         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1867         break;
1868     }
1869 
1870     return rc;
1871 }
1872 
1873 /*===========================================================================
1874  * FUNCTION   : procEvtPicTakingState
1875  *
1876  * DESCRIPTION: finite state machine function to handle event in state of
1877  *              QCAMERA_SM_STATE_PIC_TAKING.
1878  *
1879  * PARAMETERS :
1880  *   @evt      : event to be processed
1881  *   @payload  : event payload. Can be NULL if not needed.
1882  *
1883  * RETURN     : int32_t type of status
1884  *              NO_ERROR  -- success
1885  *              none-zero failure code
1886  *==========================================================================*/
procEvtPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)1887 int32_t QCameraStateMachine::procEvtPicTakingState(qcamera_sm_evt_enum_t evt,
1888                                                    void *payload)
1889 {
1890     int32_t rc = NO_ERROR;
1891     qcamera_api_result_t result;
1892     memset(&result, 0, sizeof(qcamera_api_result_t));
1893 
1894     LOGL("event (%d)", evt);
1895     switch (evt) {
1896     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1897         {
1898             // Error setting preview window during previewing
1899             LOGE("Error!! cannot set preview window when preview is running");
1900             rc = INVALID_OPERATION;
1901             result.status = rc;
1902             result.request_api = evt;
1903             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1904             m_parent->signalAPIResult(&result);
1905         }
1906         break;
1907     case QCAMERA_SM_EVT_SET_CALLBACKS:
1908         {
1909             qcamera_sm_evt_setcb_payload_t *setcbs =
1910                 (qcamera_sm_evt_setcb_payload_t *)payload;
1911             rc = m_parent->setCallBacks(setcbs->notify_cb,
1912                                         setcbs->data_cb,
1913                                         setcbs->data_cb_timestamp,
1914                                         setcbs->get_memory,
1915                                         setcbs->user);
1916             result.status = rc;
1917             result.request_api = evt;
1918             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1919             m_parent->signalAPIResult(&result);
1920         }
1921         break;
1922     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1923         {
1924             rc = m_parent->enableMsgType(*((int32_t *)payload));
1925             result.status = rc;
1926             result.request_api = evt;
1927             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1928             m_parent->signalAPIResult(&result);
1929         }
1930         break;
1931     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1932         {
1933             rc = m_parent->disableMsgType(*((int32_t *)payload));
1934             result.status = rc;
1935             result.request_api = evt;
1936             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1937             m_parent->signalAPIResult(&result);
1938         }
1939         break;
1940     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1941         {
1942             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
1943             result.status = rc;
1944             result.request_api = evt;
1945             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1946             result.enabled = enabled;
1947             m_parent->signalAPIResult(&result);
1948         }
1949         break;
1950     case QCAMERA_SM_EVT_SET_PARAMS:
1951         {
1952             bool needRestart = false;
1953             rc = m_parent->updateParameters((char*)payload, needRestart);
1954             result.status = rc;
1955             result.request_api = evt;
1956             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1957             m_parent->signalAPIResult(&result);
1958         }
1959         break;
1960     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
1961         {
1962             result.status = rc;
1963             result.request_api = evt;
1964             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1965             m_parent->signalAPIResult(&result);
1966         }
1967         break;
1968     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
1969         {
1970             rc = m_parent->commitParameterChanges();
1971             result.status = rc;
1972             result.request_api = evt;
1973             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1974             m_parent->signalAPIResult(&result);
1975         }
1976         break;
1977     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
1978         {
1979             m_parent->setNeedRestart(false);
1980             result.status           =  rc;
1981             result.request_api      =  evt;
1982             result.result_type      =  QCAMERA_API_RESULT_TYPE_DEF;
1983             m_parent->signalAPIResult(&result);
1984         }
1985         break;
1986     case QCAMERA_SM_EVT_GET_PARAMS:
1987         {
1988             result.params = m_parent->getParameters();
1989             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
1990             result.status = rc;
1991             result.request_api = evt;
1992             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1993             m_parent->signalAPIResult(&result);
1994         }
1995         break;
1996     case QCAMERA_SM_EVT_PUT_PARAMS:
1997         {
1998             rc = m_parent->putParameters((char*)payload);
1999             result.status = rc;
2000             result.request_api = evt;
2001             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2002             m_parent->signalAPIResult(&result);
2003         }
2004         break;
2005     case QCAMERA_SM_EVT_STOP_PREVIEW:
2006         {
2007             // cancel picture first
2008             rc = m_parent->cancelPicture();
2009             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2010 
2011             result.status = rc;
2012             result.request_api = evt;
2013             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2014             m_parent->signalAPIResult(&result);
2015         }
2016         break;
2017     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2018         {
2019             rc = NO_ERROR;
2020             result.status = rc;
2021             result.request_api = evt;
2022             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2023             result.enabled = 0;
2024             m_parent->signalAPIResult(&result);
2025         }
2026         break;
2027     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2028         {
2029             rc = NO_ERROR;
2030             result.status = rc;
2031             result.request_api = evt;
2032             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2033             result.enabled = 0;
2034             m_parent->signalAPIResult(&result);
2035         }
2036         break;
2037     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2038         {
2039             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
2040             result.status = rc;
2041             result.request_api = evt;
2042             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2043             m_parent->signalAPIResult(&result);
2044         }
2045         break;
2046     case QCAMERA_SM_EVT_DUMP:
2047         {
2048             rc = m_parent->dump(*((int *)payload));
2049             result.status = rc;
2050             result.request_api = evt;
2051             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2052             m_parent->signalAPIResult(&result);
2053         }
2054         break;
2055     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2056         {
2057             rc = m_parent->autoFocus();
2058             result.status = rc;
2059             result.request_api = evt;
2060             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2061             m_parent->signalAPIResult(&result);
2062         }
2063         break;
2064     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2065         {
2066             rc = m_parent->cancelAutoFocus();
2067             result.status = rc;
2068             result.request_api = evt;
2069             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2070             m_parent->signalAPIResult(&result);
2071         }
2072         break;
2073     case QCAMERA_SM_EVT_SEND_COMMAND:
2074         {
2075             qcamera_sm_evt_command_payload_t *cmd_payload =
2076                 (qcamera_sm_evt_command_payload_t *)payload;
2077             rc = m_parent->sendCommand(cmd_payload->cmd,
2078                                        cmd_payload->arg1,
2079                                        cmd_payload->arg2);
2080 #ifndef VANILLA_HAL
2081             if ( CAMERA_CMD_LONGSHOT_OFF == cmd_payload->cmd ) {
2082                 // move state to previewing state
2083                 m_state = QCAMERA_SM_STATE_PREVIEWING;
2084             }
2085 #endif
2086             result.status = rc;
2087             result.request_api = evt;
2088             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2089             m_parent->signalAPIResult(&result);
2090         }
2091         break;
2092     case QCAMERA_SM_EVT_CANCEL_PICTURE:
2093         {
2094             rc = m_parent->cancelPicture();
2095             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2096             result.status = rc;
2097             result.request_api = evt;
2098             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2099             m_parent->signalAPIResult(&result);
2100         }
2101         break;
2102     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2103         {
2104             int32_t faceID = 0;
2105             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2106                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2107             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2108                                              reg_payload->config,
2109                                              faceID);
2110             result.status = rc;
2111             result.request_api = evt;
2112             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2113             result.handle = faceID;
2114             m_parent->signalAPIResult(&result);
2115         }
2116         break;
2117     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
2118         {
2119            if ( m_parent->isLongshotEnabled() ) {
2120                // no ops here, need to singal NO_ERROR
2121                rc = NO_ERROR;
2122             } else {
2123                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2124                 rc = INVALID_OPERATION;
2125             }
2126 
2127             result.status = rc;
2128             result.request_api = evt;
2129             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2130             m_parent->signalAPIResult(&result);
2131         }
2132         break;
2133     case QCAMERA_SM_EVT_TAKE_PICTURE:
2134         {
2135            if ( m_parent->isLongshotEnabled() ) {
2136                rc = m_parent->longShot();
2137             } else {
2138                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2139                 rc = INVALID_OPERATION;
2140             }
2141 
2142             result.status = rc;
2143             result.request_api = evt;
2144             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2145             m_parent->signalAPIResult(&result);
2146         }
2147         break;
2148     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
2149     case QCAMERA_SM_EVT_PRE_START_RECORDING:
2150     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
2151     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
2152     case QCAMERA_SM_EVT_START_RECORDING:
2153     case QCAMERA_SM_EVT_STOP_RECORDING:
2154     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2155     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
2156     case QCAMERA_SM_EVT_START_PREVIEW:
2157     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2158     case QCAMERA_SM_EVT_RELEASE:
2159         {
2160             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2161             rc = INVALID_OPERATION;
2162             result.status = rc;
2163             result.request_api = evt;
2164             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2165             m_parent->signalAPIResult(&result);
2166         }
2167         break;
2168     case QCAMERA_SM_EVT_EVT_INTERNAL:
2169         {
2170             qcamera_sm_internal_evt_payload_t *internal_evt =
2171                 (qcamera_sm_internal_evt_payload_t *)payload;
2172             switch (internal_evt->evt_type) {
2173             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2174                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2175                 break;
2176             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
2177                 break;
2178             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
2179                 break;
2180             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
2181                 break;
2182             case QCAMERA_INTERNAL_EVT_CROP_INFO:
2183                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
2184                 break;
2185             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
2186                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
2187                 break;
2188             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
2189                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2190                 break;
2191             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
2192                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
2193                 break;
2194             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
2195                 rc = m_parent->processAEInfo(internal_evt->ae_data);
2196                 break;
2197             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
2198                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
2199                 break;
2200             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
2201                 rc = m_parent->processHDRData(internal_evt->hdr_data);
2202                 break;
2203             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
2204                 rc = m_parent->processRetroAECUnlock();
2205                 break;
2206             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
2207                 rc = m_parent->processZSLCaptureDone();
2208                 break;
2209             default:
2210                 break;
2211             }
2212         }
2213         break;
2214     case QCAMERA_SM_EVT_EVT_NOTIFY:
2215         {
2216             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2217             switch (cam_evt->server_event_type) {
2218             case CAM_EVENT_TYPE_DAEMON_DIED:
2219                 {
2220                     // Send internal events to release statemachine
2221                     // thread to process CAMERA_ERROR_SERVER_DIED error
2222                     result.status = rc;
2223                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
2224                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2225                     m_parent->signalAPIResult(&result);
2226 
2227                     result.status = rc;
2228                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
2229                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2230                     m_parent->signalAPIResult(&result);
2231 
2232                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
2233                                             CAMERA_ERROR_SERVER_DIED,
2234                                             0);
2235                 }
2236                 break;
2237             case CAM_EVENT_TYPE_CAC_DONE:
2238                 if (m_parent->isCACEnabled() || m_parent->mParameters.isOEMFeatEnabled()) {
2239                     LOGD("[LONG_SHOT_DBG] : Received CAC Done");
2240                     if (m_parent->isLongshotEnabled()
2241                             && !m_parent->isCaptureShutterEnabled()) {
2242                         // play shutter sound for longshot
2243                         // after CAC stage is done
2244                         m_parent->playShutter();
2245                     }
2246                     m_parent->mCACDoneReceived = TRUE;
2247                 }
2248                 break;
2249             default:
2250                 LOGH("no handling for server evt (%d) at this state",
2251                        cam_evt->server_event_type);
2252                 break;
2253             }
2254         }
2255         break;
2256     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2257         {
2258             qcamera_jpeg_evt_payload_t *jpeg_job =
2259                 (qcamera_jpeg_evt_payload_t *)payload;
2260             rc = m_parent->processJpegNotify(jpeg_job);
2261         }
2262         break;
2263     case QCAMERA_SM_EVT_STOP_CAPTURE_CHANNEL:
2264         {
2265             bool restartPreview = m_parent->isPreviewRestartEnabled();
2266             rc = m_parent->stopCaptureChannel(restartPreview);
2267 
2268             if (restartPreview && (NO_ERROR == rc)) {
2269                 rc = m_parent->preparePreview();
2270                 if (NO_ERROR == rc) {
2271                     m_parent->m_bPreviewStarted = true;
2272                     applyDelayedMsgs();
2273                     rc = m_parent->startPreview();
2274                 }
2275             }
2276 
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_SNAPSHOT_DONE:
2284         {
2285             rc = m_parent->cancelPicture();
2286 
2287             bool restartPreview = m_parent->isPreviewRestartEnabled();
2288             if (restartPreview) {
2289                 if (m_parent->mParameters.getManualCaptureMode()
2290                         >= CAM_MANUAL_CAPTURE_TYPE_3) {
2291                     m_parent->mParameters.updateZSLModeValue(m_RestoreZSL);
2292                     m_RestoreZSL = FALSE;
2293                     rc = m_parent->preparePreview();
2294                     if (NO_ERROR == rc) {
2295                         m_parent->m_bPreviewStarted = true;
2296                         applyDelayedMsgs();
2297                         rc = m_parent->startPreview();
2298                     }
2299                 }
2300                 m_state = QCAMERA_SM_STATE_PREVIEWING;
2301             } else {
2302                 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2303             }
2304 
2305             result.status = rc;
2306             result.request_api = evt;
2307             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2308             m_parent->signalEvtResult(&result);
2309         }
2310         break;
2311     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2312         {
2313             rc = m_parent->updateThermalLevel(payload);
2314         }
2315         break;
2316     default:
2317         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2318         break;
2319     }
2320 
2321     return rc;
2322 }
2323 
2324 /*===========================================================================
2325  * FUNCTION   : procEvtRecordingState
2326  *
2327  * DESCRIPTION: finite state machine function to handle event in state of
2328  *              QCAMERA_SM_STATE_RECORDING.
2329  *
2330  * PARAMETERS :
2331  *   @evt      : event to be processed
2332  *   @payload  : event payload. Can be NULL if not needed.
2333  *
2334  * RETURN     : int32_t type of status
2335  *              NO_ERROR  -- success
2336  *              none-zero failure code
2337  *==========================================================================*/
procEvtRecordingState(qcamera_sm_evt_enum_t evt,void * payload)2338 int32_t QCameraStateMachine::procEvtRecordingState(qcamera_sm_evt_enum_t evt,
2339                                                    void *payload)
2340 {
2341     int32_t rc = NO_ERROR;
2342     qcamera_api_result_t result;
2343     memset(&result, 0, sizeof(qcamera_api_result_t));
2344 
2345     LOGL("event (%d)", evt);
2346     switch (evt) {
2347     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
2348     case QCAMERA_SM_EVT_START_PREVIEW:
2349     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2350         {
2351             // WA: CTS test VideoSnapshot will try to
2352             //     start preview during video recording.
2353             LOGH("CTS video restart op");
2354             rc = NO_ERROR;
2355             result.status = rc;
2356             result.request_api = evt;
2357             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2358             m_parent->signalAPIResult(&result);
2359         }
2360         break;
2361     case QCAMERA_SM_EVT_SET_CALLBACKS:
2362         {
2363             qcamera_sm_evt_setcb_payload_t *setcbs =
2364                 (qcamera_sm_evt_setcb_payload_t *)payload;
2365             rc = m_parent->setCallBacks(setcbs->notify_cb,
2366                                         setcbs->data_cb,
2367                                         setcbs->data_cb_timestamp,
2368                                         setcbs->get_memory,
2369                                         setcbs->user);
2370             result.status = rc;
2371             result.request_api = evt;
2372             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2373             m_parent->signalAPIResult(&result);
2374         }
2375         break;
2376     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
2377         {
2378             rc = m_parent->enableMsgType(*((int32_t *)payload));
2379             result.status = rc;
2380             result.request_api = evt;
2381             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2382             m_parent->signalAPIResult(&result);
2383         }
2384         break;
2385     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
2386         {
2387             rc = m_parent->disableMsgType(*((int32_t *)payload));
2388             result.status = rc;
2389             result.request_api = evt;
2390             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2391             m_parent->signalAPIResult(&result);
2392         }
2393         break;
2394     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
2395         {
2396             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
2397             result.status = rc;
2398             result.request_api = evt;
2399             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2400             result.enabled = enabled;
2401             m_parent->signalAPIResult(&result);
2402         }
2403         break;
2404     case QCAMERA_SM_EVT_SET_PARAMS:
2405         {
2406             bool needRestart = false;
2407             rc = m_parent->updateParameters((char*)payload, needRestart);
2408             if (rc == NO_ERROR) {
2409                 if (needRestart) {
2410                     // cannot set parameters that requires restart during recording
2411                     LOGE("Error!! cannot set parameters that requires restart during recording");
2412                     rc = BAD_VALUE;
2413                 }
2414             }
2415             if (rc != NO_ERROR) {
2416                 m_parent->setNeedRestart(false);
2417             }
2418             result.status = rc;
2419             result.request_api = evt;
2420             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2421             m_parent->signalAPIResult(&result);
2422         }
2423         break;
2424     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
2425         {
2426             rc = m_parent->commitParameterChanges();
2427             result.status = rc;
2428             result.request_api = evt;
2429             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2430             m_parent->signalAPIResult(&result);
2431         }
2432         break;
2433     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
2434     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
2435         {
2436             ALOGE("%s: Error!! cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2437             rc = INVALID_OPERATION;
2438             result.status = rc;
2439             result.request_api = evt;
2440             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2441             m_parent->signalAPIResult(&result);
2442         }
2443         break;
2444     case QCAMERA_SM_EVT_GET_PARAMS:
2445         {
2446             result.params = m_parent->getParameters();
2447             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
2448             result.status = rc;
2449             result.request_api = evt;
2450             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
2451             m_parent->signalAPIResult(&result);
2452         }
2453         break;
2454     case QCAMERA_SM_EVT_PUT_PARAMS:
2455         {
2456             rc = m_parent->putParameters((char*)payload);
2457             result.status = rc;
2458             result.request_api = evt;
2459             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2460             m_parent->signalAPIResult(&result);
2461         }
2462         break;
2463     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2464         {
2465             rc = NO_ERROR;
2466             result.status = rc;
2467             result.request_api = evt;
2468             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2469             result.enabled = 0;
2470             m_parent->signalAPIResult(&result);
2471         }
2472         break;
2473     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2474         {
2475             rc = NO_ERROR;
2476             result.status = rc;
2477             result.request_api = evt;
2478             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2479             result.enabled = 1;
2480             m_parent->signalAPIResult(&result);
2481         }
2482         break;
2483     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2484         {
2485             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
2486             result.status = rc;
2487             result.request_api = evt;
2488             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2489             m_parent->signalAPIResult(&result);
2490         }
2491         break;
2492     case QCAMERA_SM_EVT_DUMP:
2493         {
2494             rc = m_parent->dump(*((int *)payload));
2495             result.status = rc;
2496             result.request_api = evt;
2497             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2498             m_parent->signalAPIResult(&result);
2499         }
2500         break;
2501     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2502         {
2503             rc = m_parent->autoFocus();
2504             result.status = rc;
2505             result.request_api = evt;
2506             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2507             m_parent->signalAPIResult(&result);
2508         }
2509         break;
2510     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2511         {
2512             rc = m_parent->cancelAutoFocus();
2513             result.status = rc;
2514             result.request_api = evt;
2515             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2516             m_parent->signalAPIResult(&result);
2517         }
2518         break;
2519     case QCAMERA_SM_EVT_SEND_COMMAND:
2520         {
2521             qcamera_sm_evt_command_payload_t *cmd_payload =
2522                 (qcamera_sm_evt_command_payload_t *)payload;
2523             rc = m_parent->sendCommand(cmd_payload->cmd,
2524                                        cmd_payload->arg1,
2525                                        cmd_payload->arg2);
2526             result.status = rc;
2527             result.request_api = evt;
2528             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2529             m_parent->signalAPIResult(&result);
2530         }
2531         break;
2532     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
2533         {
2534             // No ops here, send NO_ERROR.
2535             rc = NO_ERROR;
2536             result.status = rc;
2537             result.request_api = evt;
2538             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2539             m_parent->signalAPIResult(&result);
2540         }
2541         break;
2542     case QCAMERA_SM_EVT_TAKE_PICTURE:
2543         {
2544             m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
2545             rc = m_parent->takeLiveSnapshot();
2546             if (rc != NO_ERROR) {
2547                 m_parent->unconfigureAdvancedCapture();
2548                 m_state = QCAMERA_SM_STATE_RECORDING;
2549             }
2550             result.status = rc;
2551             result.request_api = evt;
2552             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2553             m_parent->signalAPIResult(&result);
2554         }
2555         break;
2556     case QCAMERA_SM_EVT_PRE_START_RECORDING:
2557     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
2558     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
2559     case QCAMERA_SM_EVT_START_RECORDING:
2560         {
2561             // no ops here
2562             LOGW("already in recording state, no ops for start_recording");
2563             rc = 0;
2564             result.status = rc;
2565             result.request_api = evt;
2566             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2567             m_parent->signalAPIResult(&result);
2568         }
2569         break;
2570     case QCAMERA_SM_EVT_STOP_RECORDING:
2571         {
2572             rc = m_parent->stopRecording();
2573             m_state = QCAMERA_SM_STATE_PREVIEWING;
2574             result.status = rc;
2575             result.request_api = evt;
2576             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2577             m_parent->signalAPIResult(&result);
2578         }
2579         break;
2580     case QCAMERA_SM_EVT_STOP_PREVIEW:
2581         {
2582             rc = m_parent->stopRecording();
2583             m_state = QCAMERA_SM_STATE_PREVIEWING;
2584 
2585             rc = m_parent->stopPreview();
2586             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2587 
2588             result.status = rc;
2589             result.request_api = evt;
2590             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2591             m_parent->signalAPIResult(&result);
2592         }
2593         break;
2594     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2595         {
2596             rc = m_parent->releaseRecordingFrame((const void *)payload);
2597             result.status = rc;
2598             result.request_api = evt;
2599             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2600             m_parent->signalAPIResult(&result);
2601         }
2602         break;
2603     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2604         {
2605             int32_t faceID = 0;
2606             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2607                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2608             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2609                                              reg_payload->config,
2610                                              faceID);
2611             result.status = rc;
2612             result.request_api = evt;
2613             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2614             result.handle = faceID;
2615             m_parent->signalAPIResult(&result);
2616         }
2617         break;
2618     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
2619         {
2620             //In Video snapshot, prepare hardware is a no-op.
2621             result.status = NO_ERROR;
2622             result.request_api = evt;
2623             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2624             m_parent->signalAPIResult(&result);
2625         }
2626         break;
2627     case QCAMERA_SM_EVT_CANCEL_PICTURE:
2628     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2629     case QCAMERA_SM_EVT_RELEASE:
2630         {
2631             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2632             rc = INVALID_OPERATION;
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_EVT_INTERNAL:
2640         {
2641             qcamera_sm_internal_evt_payload_t *internal_evt =
2642                 (qcamera_sm_internal_evt_payload_t *)payload;
2643             switch (internal_evt->evt_type) {
2644             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2645                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2646                 break;
2647             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
2648                 break;
2649             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
2650                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
2651                 break;
2652             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
2653                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
2654                 break;
2655             case QCAMERA_INTERNAL_EVT_CROP_INFO:
2656                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
2657                 break;
2658             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
2659                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
2660                 break;
2661             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
2662                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2663                 break;
2664             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
2665                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
2666                 break;
2667             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
2668                 rc = m_parent->processAEInfo(internal_evt->ae_data);
2669                 break;
2670             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
2671                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
2672                 break;
2673             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
2674                 rc = m_parent->processHDRData(internal_evt->hdr_data);
2675                 break;
2676             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
2677                 rc = m_parent->processRetroAECUnlock();
2678                 break;
2679             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
2680                 rc = m_parent->processZSLCaptureDone();
2681                 break;
2682             default:
2683                 break;
2684             }
2685         }
2686         break;
2687     case QCAMERA_SM_EVT_EVT_NOTIFY:
2688         {
2689             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2690             switch (cam_evt->server_event_type) {
2691             case CAM_EVENT_TYPE_DAEMON_DIED:
2692                 {
2693                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
2694                                             CAMERA_ERROR_SERVER_DIED,
2695                                             0);
2696                 }
2697                 break;
2698             default:
2699                 LOGE("Invalid internal event %d in state(%d)",
2700                              cam_evt->server_event_type, m_state);
2701                 break;
2702             }
2703         }
2704         break;
2705     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2706         {
2707             rc = m_parent->updateThermalLevel(payload);
2708         }
2709         break;
2710     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2711         {
2712             // No ops, but need to notify
2713             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2714             result.status = rc;
2715             result.request_api = evt;
2716             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2717             m_parent->signalEvtResult(&result);
2718         }
2719        break;
2720     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2721     default:
2722         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2723         break;
2724     }
2725 
2726     return rc;
2727 }
2728 
2729 /*===========================================================================
2730  * FUNCTION   : procEvtVideoPicTakingState
2731  *
2732  * DESCRIPTION: finite state machine function to handle event in state of
2733  *              QCAMERA_SM_STATE_VIDEO_PIC_TAKING.
2734  *
2735  * PARAMETERS :
2736  *   @evt      : event to be processed
2737  *   @payload  : event payload. Can be NULL if not needed.
2738  *
2739  * RETURN     : int32_t type of status
2740  *              NO_ERROR  -- success
2741  *              none-zero failure code
2742  *==========================================================================*/
procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)2743 int32_t QCameraStateMachine::procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,
2744                                                         void *payload)
2745 {
2746     int32_t rc = NO_ERROR;
2747     qcamera_api_result_t result;
2748     memset(&result, 0, sizeof(qcamera_api_result_t));
2749 
2750     LOGL("event (%d)", evt);
2751     switch (evt) {
2752     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2753         {
2754             // Error setting preview window during previewing
2755             LOGE("Error!! cannot set preview window when preview is running");
2756             rc = INVALID_OPERATION;
2757             result.status = rc;
2758             result.request_api = evt;
2759             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2760             m_parent->signalAPIResult(&result);
2761         }
2762         break;
2763     case QCAMERA_SM_EVT_SET_CALLBACKS:
2764         {
2765             qcamera_sm_evt_setcb_payload_t *setcbs =
2766                 (qcamera_sm_evt_setcb_payload_t *)payload;
2767             rc = m_parent->setCallBacks(setcbs->notify_cb,
2768                                         setcbs->data_cb,
2769                                         setcbs->data_cb_timestamp,
2770                                         setcbs->get_memory,
2771                                         setcbs->user);
2772             result.status = rc;
2773             result.request_api = evt;
2774             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2775             m_parent->signalAPIResult(&result);
2776         }
2777         break;
2778     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
2779         {
2780             rc = m_parent->enableMsgType(*((int32_t *)payload));
2781             result.status = rc;
2782             result.request_api = evt;
2783             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2784             m_parent->signalAPIResult(&result);
2785         }
2786         break;
2787     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
2788         {
2789             rc = m_parent->disableMsgType(*((int32_t *)payload));
2790             result.status = rc;
2791             result.request_api = evt;
2792             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2793             m_parent->signalAPIResult(&result);
2794         }
2795         break;
2796     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
2797         {
2798             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
2799             result.status = rc;
2800             result.request_api = evt;
2801             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2802             result.enabled = enabled;
2803             m_parent->signalAPIResult(&result);
2804         }
2805         break;
2806     case QCAMERA_SM_EVT_SET_PARAMS:
2807         {
2808             bool needRestart = false;
2809             rc = m_parent->updateParameters((char*)payload, needRestart);
2810             if (rc == NO_ERROR) {
2811                 if (needRestart) {
2812                     // cannot set parameters that requires restart during recording
2813                     LOGE("Error!! cannot set parameters that requires restart during recording");
2814                     rc = BAD_VALUE;
2815                 }
2816             }
2817             if (rc != NO_ERROR) {
2818                 m_parent->setNeedRestart(false);
2819             }
2820             result.status = rc;
2821             result.request_api = evt;
2822             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2823             m_parent->signalAPIResult(&result);
2824         }
2825         break;
2826     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
2827         {
2828             rc = m_parent->commitParameterChanges();
2829             result.status = rc;
2830             result.request_api = evt;
2831             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2832             m_parent->signalAPIResult(&result);
2833         }
2834         break;
2835     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
2836     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
2837         {
2838             ALOGE("%s: Error!! cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2839             rc = INVALID_OPERATION;
2840             result.status = rc;
2841             result.request_api = evt;
2842             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2843             m_parent->signalAPIResult(&result);
2844         }
2845         break;
2846     case QCAMERA_SM_EVT_GET_PARAMS:
2847         {
2848             result.params = m_parent->getParameters();
2849             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
2850             result.status = rc;
2851             result.request_api = evt;
2852             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
2853             m_parent->signalAPIResult(&result);
2854         }
2855         break;
2856     case QCAMERA_SM_EVT_PUT_PARAMS:
2857         {
2858             rc = m_parent->putParameters((char*)payload);
2859             result.status = rc;
2860             result.request_api = evt;
2861             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2862             m_parent->signalAPIResult(&result);
2863         }
2864         break;
2865     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2866         {
2867             rc = NO_ERROR;
2868             result.status = rc;
2869             result.request_api = evt;
2870             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2871             result.enabled = 1;
2872             m_parent->signalAPIResult(&result);
2873         }
2874         break;
2875     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2876         {
2877             rc = NO_ERROR;
2878             result.status = rc;
2879             result.request_api = evt;
2880             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2881             result.enabled = 1;
2882             m_parent->signalAPIResult(&result);
2883         }
2884         break;
2885     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2886         {
2887             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
2888             result.status = rc;
2889             result.request_api = evt;
2890             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2891             m_parent->signalAPIResult(&result);
2892         }
2893         break;
2894     case QCAMERA_SM_EVT_DUMP:
2895         {
2896             rc = m_parent->dump(*((int *)payload));
2897             result.status = rc;
2898             result.request_api = evt;
2899             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2900             m_parent->signalAPIResult(&result);
2901         }
2902         break;
2903     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2904         {
2905             rc = m_parent->autoFocus();
2906             result.status = rc;
2907             result.request_api = evt;
2908             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2909             m_parent->signalAPIResult(&result);
2910         }
2911         break;
2912     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2913         {
2914             rc = m_parent->cancelAutoFocus();
2915             result.status = rc;
2916             result.request_api = evt;
2917             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2918             m_parent->signalAPIResult(&result);
2919         }
2920         break;
2921     case QCAMERA_SM_EVT_SEND_COMMAND:
2922         {
2923             qcamera_sm_evt_command_payload_t *cmd_payload =
2924                 (qcamera_sm_evt_command_payload_t *)payload;
2925             rc = m_parent->sendCommand(cmd_payload->cmd,
2926                                        cmd_payload->arg1,
2927                                        cmd_payload->arg2);
2928             result.status = rc;
2929             result.request_api = evt;
2930             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2931             m_parent->signalAPIResult(&result);
2932         }
2933         break;
2934     case QCAMERA_SM_EVT_STOP_RECORDING:
2935         {
2936             rc = m_parent->cancelLiveSnapshot();
2937             m_state = QCAMERA_SM_STATE_RECORDING;
2938 
2939             rc = m_parent->stopRecording();
2940             m_state = QCAMERA_SM_STATE_PREVIEWING;
2941 
2942             result.status = rc;
2943             result.request_api = evt;
2944             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2945             m_parent->signalAPIResult(&result);
2946         }
2947         break;
2948     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2949         {
2950             rc = m_parent->releaseRecordingFrame((const void *)payload);
2951             result.status = rc;
2952             result.request_api = evt;
2953             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2954             m_parent->signalAPIResult(&result);
2955         }
2956         break;
2957     case QCAMERA_SM_EVT_CANCEL_PICTURE:
2958         {
2959             rc = m_parent->cancelLiveSnapshot();
2960             m_state = QCAMERA_SM_STATE_RECORDING;
2961             result.status = rc;
2962             result.request_api = evt;
2963             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2964             m_parent->signalAPIResult(&result);
2965         }
2966         break;
2967     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2968         {
2969             int32_t faceID = 0;
2970             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2971                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2972             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2973                                              reg_payload->config,
2974                                              faceID);
2975             result.status = rc;
2976             result.request_api = evt;
2977             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2978             result.handle = faceID;
2979             m_parent->signalAPIResult(&result);
2980         }
2981         break;
2982     case QCAMERA_SM_EVT_STOP_PREVIEW:
2983         {
2984             rc = m_parent->cancelLiveSnapshot();
2985             m_state = QCAMERA_SM_STATE_RECORDING;
2986 
2987             rc = m_parent->stopRecording();
2988             m_state = QCAMERA_SM_STATE_PREVIEWING;
2989 
2990             rc = m_parent->stopPreview();
2991             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2992 
2993             result.status = rc;
2994             result.request_api = evt;
2995             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2996             m_parent->signalAPIResult(&result);
2997         }
2998         break;
2999     case QCAMERA_SM_EVT_PRE_START_RECORDING:
3000     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
3001     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
3002     case QCAMERA_SM_EVT_START_RECORDING:
3003     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
3004     case QCAMERA_SM_EVT_START_PREVIEW:
3005     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
3006     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
3007     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
3008     case QCAMERA_SM_EVT_TAKE_PICTURE:
3009     case QCAMERA_SM_EVT_RELEASE:
3010         {
3011             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3012             rc = INVALID_OPERATION;
3013             result.status = rc;
3014             result.request_api = evt;
3015             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3016             m_parent->signalAPIResult(&result);
3017         }
3018         break;
3019     case QCAMERA_SM_EVT_EVT_INTERNAL:
3020         {
3021             qcamera_sm_internal_evt_payload_t *internal_evt =
3022                 (qcamera_sm_internal_evt_payload_t *)payload;
3023             switch (internal_evt->evt_type) {
3024             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
3025                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
3026                 break;
3027             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
3028                 break;
3029             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
3030                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
3031                 break;
3032             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
3033                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
3034                 break;
3035             case QCAMERA_INTERNAL_EVT_CROP_INFO:
3036                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
3037                 break;
3038             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
3039                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
3040                 break;
3041             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
3042                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3043                 break;
3044             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
3045                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
3046                 break;
3047             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
3048                 rc = m_parent->processAEInfo(internal_evt->ae_data);
3049                 break;
3050             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
3051                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
3052                 break;
3053             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
3054                 rc = m_parent->processHDRData(internal_evt->hdr_data);
3055                 break;
3056             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
3057                 rc = m_parent->processRetroAECUnlock();
3058                 break;
3059             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
3060                 rc = m_parent->processZSLCaptureDone();
3061                 break;
3062             default:
3063                 break;
3064             }
3065         }
3066         break;
3067     case QCAMERA_SM_EVT_EVT_NOTIFY:
3068         {
3069             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
3070             switch (cam_evt->server_event_type) {
3071             case CAM_EVENT_TYPE_DAEMON_DIED:
3072                 {
3073                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
3074                                             CAMERA_ERROR_SERVER_DIED,
3075                                             0);
3076                 }
3077                 break;
3078             default:
3079                 LOGE("Invalid internal event %d in state(%d)",
3080                              cam_evt->server_event_type, m_state);
3081                 break;
3082             }
3083         }
3084         break;
3085     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
3086         {
3087             qcamera_jpeg_evt_payload_t *jpeg_job =
3088                 (qcamera_jpeg_evt_payload_t *)payload;
3089             rc = m_parent->processJpegNotify(jpeg_job);
3090         }
3091         break;
3092     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
3093         {
3094             rc = m_parent->cancelLiveSnapshot();
3095             m_state = QCAMERA_SM_STATE_RECORDING;
3096             result.status = rc;
3097             result.request_api = evt;
3098             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3099             m_parent->signalEvtResult(&result);
3100         }
3101         break;
3102     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
3103         {
3104             rc = m_parent->updateThermalLevel(payload);
3105         }
3106         break;
3107     default:
3108         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3109         break;
3110     }
3111 
3112     return rc;
3113 }
3114 
3115 /*===========================================================================
3116  * FUNCTION   : procEvtPreviewPicTakingState
3117  *
3118  * DESCRIPTION: finite state machine function to handle event in state of
3119  *              QCAMERA_SM_STATE_PREVIEW_PIC_TAKING.
3120  *
3121  * PARAMETERS :
3122  *   @evt      : event to be processed
3123  *   @payload  : event payload. Can be NULL if not needed.
3124  *
3125  * RETURN     : int32_t type of status
3126  *              NO_ERROR  -- success
3127  *              none-zero failure code
3128  *==========================================================================*/
procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)3129 int32_t QCameraStateMachine::procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,
3130                                                           void *payload)
3131 {
3132     int32_t rc = NO_ERROR;
3133     qcamera_api_result_t result;
3134     memset(&result, 0, sizeof(qcamera_api_result_t));
3135 
3136     LOGL("event (%d)", evt);
3137     switch (evt) {
3138     case QCAMERA_SM_EVT_SET_CALLBACKS:
3139         {
3140             qcamera_sm_evt_setcb_payload_t *setcbs =
3141                 (qcamera_sm_evt_setcb_payload_t *)payload;
3142             rc = m_parent->setCallBacks(setcbs->notify_cb,
3143                                         setcbs->data_cb,
3144                                         setcbs->data_cb_timestamp,
3145                                         setcbs->get_memory,
3146                                         setcbs->user);
3147             result.status = rc;
3148             result.request_api = evt;
3149             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3150             m_parent->signalAPIResult(&result);
3151         }
3152         break;
3153     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
3154         {
3155             rc = m_parent->enableMsgType(*((int32_t *)payload));
3156             result.status = rc;
3157             result.request_api = evt;
3158             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3159             m_parent->signalAPIResult(&result);
3160         }
3161         break;
3162     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
3163         {
3164             rc = m_parent->disableMsgType(*((int32_t *)payload));
3165             result.status = rc;
3166             result.request_api = evt;
3167             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3168             m_parent->signalAPIResult(&result);
3169         }
3170         break;
3171     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
3172         {
3173             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
3174             result.status = rc;
3175             result.request_api = evt;
3176             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
3177             result.enabled = enabled;
3178             m_parent->signalAPIResult(&result);
3179         }
3180         break;
3181     case QCAMERA_SM_EVT_SET_PARAMS:
3182         {
3183             bool needRestart = false;
3184             rc = m_parent->updateParameters((char*)payload, needRestart);
3185             result.status = rc;
3186             result.request_api = evt;
3187             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3188             m_parent->signalAPIResult(&result);
3189         }
3190         break;
3191     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
3192         {
3193             // need restart preview for parameters to take effect
3194             LOGD("Stopping preview...");
3195             // stop preview
3196             rc = m_parent->stopPreview();
3197             // Clear memory pools
3198             m_parent->m_memoryPool.clear();
3199             result.status = rc;
3200             result.request_api = evt;
3201             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3202             m_parent->signalAPIResult(&result);
3203         }
3204         break;
3205     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
3206         {
3207             // commit parameter changes to server
3208             rc = m_parent->commitParameterChanges();
3209             result.status = rc;
3210             result.request_api = evt;
3211             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3212             m_parent->signalAPIResult(&result);
3213         }
3214         break;
3215     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
3216         {
3217             // start preview again
3218             rc = m_parent->preparePreview();
3219             if (rc == NO_ERROR) {
3220                 applyDelayedMsgs();
3221                 rc = m_parent->startPreview();
3222                 if (rc != NO_ERROR) {
3223                     m_parent->unpreparePreview();
3224                 }
3225             }
3226             if (rc != NO_ERROR) {
3227                 m_state = QCAMERA_SM_STATE_PIC_TAKING;
3228             }
3229             m_parent->setNeedRestart(false);
3230             result.status           =  rc;
3231             result.request_api      =  evt;
3232             result.result_type      =  QCAMERA_API_RESULT_TYPE_DEF;
3233             m_parent->signalAPIResult(&result);
3234         }
3235         break;
3236     case QCAMERA_SM_EVT_GET_PARAMS:
3237         {
3238             result.params = m_parent->getParameters();
3239             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
3240             result.status = rc;
3241             result.request_api = evt;
3242             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
3243             m_parent->signalAPIResult(&result);
3244         }
3245         break;
3246     case QCAMERA_SM_EVT_PUT_PARAMS:
3247         {
3248             rc = m_parent->putParameters((char*)payload);
3249             result.status = rc;
3250             result.request_api = evt;
3251             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3252             m_parent->signalAPIResult(&result);
3253         }
3254         break;
3255     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
3256         {
3257             rc = NO_ERROR;
3258             result.status = rc;
3259             result.request_api = evt;
3260             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
3261             result.enabled = 1;
3262             m_parent->signalAPIResult(&result);
3263         }
3264         break;
3265     case QCAMERA_SM_EVT_RECORDING_ENABLED:
3266         {
3267             rc = NO_ERROR;
3268             result.status = rc;
3269             result.request_api = evt;
3270             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
3271             result.enabled = 0;
3272             m_parent->signalAPIResult(&result);
3273         }
3274         break;
3275     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
3276         {
3277             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
3278             result.status = rc;
3279             result.request_api = evt;
3280             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3281             m_parent->signalAPIResult(&result);
3282         }
3283         break;
3284     case QCAMERA_SM_EVT_DUMP:
3285         {
3286             rc = m_parent->dump(*((int *)payload));
3287             result.status = rc;
3288             result.request_api = evt;
3289             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3290             m_parent->signalAPIResult(&result);
3291         }
3292         break;
3293     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
3294         {
3295             rc = m_parent->autoFocus();
3296             result.status = rc;
3297             result.request_api = evt;
3298             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3299             m_parent->signalAPIResult(&result);
3300         }
3301         break;
3302     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
3303         {
3304             rc = m_parent->cancelAutoFocus();
3305             result.status = rc;
3306             result.request_api = evt;
3307             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3308             m_parent->signalAPIResult(&result);
3309         }
3310         break;
3311     case QCAMERA_SM_EVT_SEND_COMMAND:
3312         {
3313             qcamera_sm_evt_command_payload_t *cmd_payload =
3314                 (qcamera_sm_evt_command_payload_t *)payload;
3315             rc = m_parent->sendCommand(cmd_payload->cmd,
3316                                        cmd_payload->arg1,
3317                                        cmd_payload->arg2);
3318 #ifndef VANILLA_HAL
3319             if ( CAMERA_CMD_LONGSHOT_OFF == cmd_payload->cmd ) {
3320                 // move state to previewing state
3321                 m_state = QCAMERA_SM_STATE_PREVIEWING;
3322             }
3323 #endif
3324             result.status = rc;
3325             result.request_api = evt;
3326             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3327             m_parent->signalAPIResult(&result);
3328         }
3329         break;
3330     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
3331         {
3332             rc = m_parent->releaseRecordingFrame((const void *)payload);
3333             result.status = rc;
3334             result.request_api = evt;
3335             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3336             m_parent->signalAPIResult(&result);
3337         }
3338         break;
3339     case QCAMERA_SM_EVT_CANCEL_PICTURE:
3340         {
3341             if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
3342                 rc = m_parent->cancelPicture();
3343             } else {
3344                 rc = m_parent->cancelLiveSnapshot();
3345             }
3346             m_state = QCAMERA_SM_STATE_PREVIEWING;
3347             result.status = rc;
3348             result.request_api = evt;
3349             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3350             m_parent->signalAPIResult(&result);
3351         }
3352         break;
3353     case QCAMERA_SM_EVT_STOP_PREVIEW:
3354         {
3355             if (m_parent->isZSLMode()) {
3356                 // cancel picture first
3357                 rc = m_parent->cancelPicture();
3358                 m_parent->stopChannel(QCAMERA_CH_TYPE_ZSL);
3359             } else if (m_parent->isLongshotEnabled()) {
3360                 // just cancel picture
3361                 rc = m_parent->cancelPicture();
3362             } else {
3363                 rc = m_parent->cancelLiveSnapshot();
3364                 m_parent->stopChannel(QCAMERA_CH_TYPE_PREVIEW);
3365             }
3366             // unprepare preview
3367             m_parent->unpreparePreview();
3368             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
3369             result.status = rc;
3370             result.request_api = evt;
3371             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3372             m_parent->signalAPIResult(&result);
3373         }
3374         break;
3375     case QCAMERA_SM_EVT_PRE_START_RECORDING:
3376         {
3377             if (m_parent->isZSLMode()) {
3378                 LOGE("Error!! cannot handle evt(%d) in state(%d) in ZSL mode", evt, m_state);
3379                 rc = INVALID_OPERATION;
3380             } else if (m_parent->isLongshotEnabled()) {
3381                 LOGE("Error!! cannot handle evt(%d) in state(%d) in Longshot mode", evt, m_state);
3382                 rc = INVALID_OPERATION;
3383             } else {
3384                 rc = m_parent->preStartRecording();
3385             }
3386             result.status = rc;
3387             result.request_api = evt;
3388             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3389             m_parent->signalAPIResult(&result);
3390         }
3391         break;
3392     case QCAMERA_SM_EVT_START_RECORDING:
3393         {
3394             if (m_parent->isZSLMode()) {
3395                 LOGE("Error!! cannot handle evt(%d) in state(%d) in ZSL mode",
3396                        evt, m_state);
3397                 rc = INVALID_OPERATION;
3398             } else if (m_parent->isLongshotEnabled()) {
3399                 LOGE("Error!! cannot handle evt(%d) in state(%d) in Longshot mode",
3400                        evt, m_state);
3401                 rc = INVALID_OPERATION;
3402             } else {
3403                 rc = m_parent->startRecording();
3404                 if (rc == NO_ERROR) {
3405                     m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
3406                 }
3407             }
3408             result.status = rc;
3409             result.request_api = evt;
3410             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3411             m_parent->signalAPIResult(&result);
3412         }
3413         break;
3414     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
3415         {
3416             int32_t faceID = 0;
3417             qcamera_sm_evt_reg_face_payload_t *reg_payload =
3418                 (qcamera_sm_evt_reg_face_payload_t *)payload;
3419             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
3420                                              reg_payload->config,
3421                                              faceID);
3422             result.status = rc;
3423             result.request_api = evt;
3424             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
3425             result.handle = faceID;
3426             m_parent->signalAPIResult(&result);
3427         }
3428         break;
3429     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
3430         {
3431            if ( m_parent->isLongshotEnabled() ) {
3432                // no ops here, need to singal NO_ERROR
3433                rc = NO_ERROR;
3434             } else {
3435                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3436                 rc = INVALID_OPERATION;
3437             }
3438 
3439             result.status = rc;
3440             result.request_api = evt;
3441             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3442             m_parent->signalAPIResult(&result);
3443         }
3444         break;
3445     case QCAMERA_SM_EVT_TAKE_PICTURE:
3446         {
3447             if ( m_parent->isLongshotEnabled() ) {
3448                rc = m_parent->longShot();
3449             } else {
3450                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3451                 rc = INVALID_OPERATION;
3452             }
3453 
3454             result.status = rc;
3455             result.request_api = evt;
3456             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3457             m_parent->signalAPIResult(&result);
3458         }
3459         break;
3460 
3461     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
3462         {
3463           LOGD("Prepare Snapshot");
3464           if (m_parent->isRetroPicture()) {
3465               LOGD("Prepare Snapshot in Retro Mode");
3466               rc = m_parent->prepareHardwareForSnapshot(FALSE);
3467               if (rc != NO_ERROR) {
3468                   LOGE("prepareHardwareForSnapshot failed %d",
3469                        rc);
3470                   result.status = rc;
3471                   result.request_api = evt;
3472                   result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3473                   m_parent->signalAPIResult(&result);
3474               }
3475           }
3476           else {
3477               LOGE("Error!! cannot handle evt(%d) in state(%d)",
3478                  evt, m_state);
3479               rc = INVALID_OPERATION;
3480               result.status = rc;
3481               result.request_api = evt;
3482               result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3483               m_parent->signalAPIResult(&result);
3484           }
3485         }
3486         break;
3487     case QCAMERA_SM_EVT_STOP_RECORDING:
3488     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
3489     case QCAMERA_SM_EVT_START_PREVIEW:
3490     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
3491     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
3492     case QCAMERA_SM_EVT_RELEASE:
3493         {
3494             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3495             rc = INVALID_OPERATION;
3496             result.status = rc;
3497             result.request_api = evt;
3498             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3499             m_parent->signalAPIResult(&result);
3500         }
3501         break;
3502     case QCAMERA_SM_EVT_EVT_INTERNAL:
3503         {
3504             qcamera_sm_internal_evt_payload_t *internal_evt =
3505                 (qcamera_sm_internal_evt_payload_t *)payload;
3506             switch (internal_evt->evt_type) {
3507             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
3508                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
3509                 break;
3510             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
3511                 LOGD("Received QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE event");
3512                 if (m_parent->isRetroPicture()) {
3513                     m_parent->processPrepSnapshotDoneEvent(internal_evt->prep_snapshot_state);
3514                     LOGD("Retro picture");
3515                     result.status = NO_ERROR;
3516                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
3517                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3518                     m_parent->signalAPIResult(&result);
3519                 }
3520                 else {
3521                     LOGE("Invalid Case for  "
3522                             "QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event");
3523                 }
3524                 break;
3525             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
3526                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
3527                 break;
3528             case QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT:
3529                 // This is valid only in Retro picture Mode
3530                 if (m_parent->isRetroPicture()) {
3531                     LOGD("Received QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event");
3532                     result.status = NO_ERROR;
3533                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
3534                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3535                     m_parent->signalAPIResult(&result);
3536                 }
3537                 else {
3538                     LOGD("Wrong Case for QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event");
3539                 }
3540                 break;
3541             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
3542                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
3543                 break;
3544             case QCAMERA_INTERNAL_EVT_CROP_INFO:
3545                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
3546                 break;
3547             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
3548                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
3549                 break;
3550             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
3551                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3552                 break;
3553             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
3554                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
3555                 break;
3556             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
3557                 rc = m_parent->processAEInfo(internal_evt->ae_data);
3558                 break;
3559             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
3560                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
3561                 break;
3562             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
3563                 rc = m_parent->processHDRData(internal_evt->hdr_data);
3564                 break;
3565             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
3566                 rc = m_parent->processRetroAECUnlock();
3567                 break;
3568             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
3569                 rc = m_parent->processZSLCaptureDone();
3570                 break;
3571             default:
3572                 break;
3573             }
3574         }
3575         break;
3576     case QCAMERA_SM_EVT_EVT_NOTIFY:
3577         {
3578             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
3579             switch (cam_evt->server_event_type) {
3580             case CAM_EVENT_TYPE_DAEMON_DIED:
3581                 {
3582                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
3583                                             CAMERA_ERROR_SERVER_DIED,
3584                                             0);
3585                 }
3586                 break;
3587             case CAM_EVENT_TYPE_CAC_DONE:
3588                 if (m_parent->isCACEnabled() || m_parent->mParameters.isOEMFeatEnabled()) {
3589                     LOGD("[LONG_SHOT_DBG] : Received CAC Done");
3590                     if ((m_parent->isLongshotEnabled())
3591                             && (!m_parent->isCaptureShutterEnabled())) {
3592                         // play shutter sound for longshot
3593                         // after CAC stage is done
3594                         m_parent->playShutter();
3595                     }
3596                     m_parent->mCACDoneReceived = TRUE;
3597                 }
3598                 break;
3599             default:
3600                 LOGE("Invalid internal event %d in state(%d)",
3601                              cam_evt->server_event_type, m_state);
3602                 break;
3603             }
3604         }
3605         break;
3606     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
3607         {
3608             LOGL("Calling Process Jpeg Notify");
3609             qcamera_jpeg_evt_payload_t *jpeg_job =
3610                 (qcamera_jpeg_evt_payload_t *)payload;
3611             rc = m_parent->processJpegNotify(jpeg_job);
3612         }
3613         break;
3614     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
3615         {
3616             LOGL("Snapshot Done");
3617             if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
3618                 rc = m_parent->cancelPicture();
3619             } else {
3620                 rc = m_parent->cancelLiveSnapshot();
3621             }
3622             m_state = QCAMERA_SM_STATE_PREVIEWING;
3623             if (m_parent->isRetroPicture()){
3624                 result.status = rc;
3625                 result.request_api = evt;
3626                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3627                 LOGL("\n Signalling for JPEG snapshot done!!");
3628                 m_parent->signalAPIResult(&result);
3629 
3630             }
3631             result.status = rc;
3632             result.request_api = evt;
3633             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3634             m_parent->signalEvtResult(&result);
3635         }
3636         break;
3637     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
3638         {
3639             rc = m_parent->updateThermalLevel(payload);
3640         }
3641         break;
3642     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
3643         {
3644             m_parent->stopPreview();
3645             result.status = rc;
3646             result.request_api = evt;
3647             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3648             m_parent->signalAPIResult(&result);
3649         }
3650        break;
3651     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
3652         {
3653             rc = m_parent->preparePreview();
3654             if (rc == NO_ERROR) {
3655                 rc = m_parent->startPreview();
3656             }
3657             result.status = rc;
3658             result.request_api = evt;
3659             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3660             m_parent->signalAPIResult(&result);
3661         }
3662        break;
3663     default:
3664         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3665         break;
3666     }
3667 
3668     return rc;
3669 }
3670 
3671 /*===========================================================================
3672  * FUNCTION   : isRecording
3673  *
3674  * DESCRIPTION: check if recording is in process.
3675  *
3676  * PARAMETERS : None
3677  *
3678  * RETURN     : true -- recording
3679  *              false -- not in recording mode
3680  *==========================================================================*/
isRecording()3681 bool QCameraStateMachine::isRecording()
3682 {
3683     switch (m_state) {
3684     case QCAMERA_SM_STATE_RECORDING:
3685     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3686         return true;
3687     default:
3688         return false;
3689     }
3690 }
3691 
3692 /*===========================================================================
3693  * FUNCTION   : isPreviewRunning
3694  *
3695  * DESCRIPTION: check if preview is in process.
3696  *
3697  * PARAMETERS : None
3698  *
3699  * RETURN     : true -- preview running
3700  *              false -- preview stopped
3701  *==========================================================================*/
isPreviewRunning()3702 bool QCameraStateMachine::isPreviewRunning()
3703 {
3704     switch (m_state) {
3705     case QCAMERA_SM_STATE_PREVIEWING:
3706     case QCAMERA_SM_STATE_RECORDING:
3707     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3708     case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3709     case QCAMERA_SM_STATE_PREPARE_SNAPSHOT:
3710     case QCAMERA_SM_STATE_PREVIEW_READY:
3711         return true;
3712     default:
3713         return false;
3714     }
3715 }
3716 
3717 /*===========================================================================
3718  * FUNCTION   : isPreviewReady
3719  *
3720  * DESCRIPTION: check if preview is in ready state.
3721  *
3722  * PARAMETERS : None
3723  *
3724  * RETURN     : true -- preview is in ready state
3725  *              false -- preview is stopped
3726  *==========================================================================*/
isPreviewReady()3727 bool QCameraStateMachine::isPreviewReady()
3728 {
3729     switch (m_state) {
3730     case QCAMERA_SM_STATE_PREVIEW_READY:
3731         return true;
3732     default:
3733         return false;
3734     }
3735 }
3736 
3737 /*===========================================================================
3738  * FUNCTION   : isCaptureRunning
3739  *
3740  * DESCRIPTION: check if image capture is in process.
3741  *
3742  * PARAMETERS : None
3743  *
3744  * RETURN     : true -- capture running
3745  *              false -- capture stopped
3746  *==========================================================================*/
isCaptureRunning()3747 bool QCameraStateMachine::isCaptureRunning()
3748 {
3749     switch (m_state) {
3750     case QCAMERA_SM_STATE_PIC_TAKING:
3751     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3752     case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3753         return true;
3754     default:
3755         return false;
3756     }
3757 }
3758 /*===========================================================================
3759  * FUNCTION   : isNonZSLCaptureRunning
3760  *
3761  * DESCRIPTION: check if image capture is in process in non ZSL mode.
3762  *
3763  * PARAMETERS : None
3764  *
3765  * RETURN     : true -- capture running in non ZSL mode
3766  *              false -- Either in not capture mode or captur is not in non ZSL mode
3767  *==========================================================================*/
isNonZSLCaptureRunning()3768 bool QCameraStateMachine::isNonZSLCaptureRunning()
3769 {
3770     switch (m_state) {
3771     case QCAMERA_SM_STATE_PIC_TAKING:
3772         return true;
3773     default:
3774         return false;
3775     }
3776 }
3777 
3778 /*===========================================================================
3779  * FUNCTION   : dump
3780  *
3781  * DESCRIPTION: Composes a string based on current configuration
3782  *
3783  * PARAMETERS : none
3784  *
3785  * RETURN     : Formatted string
3786  *==========================================================================*/
dump()3787 String8 QCameraStateMachine::dump()
3788 {
3789     String8 str("\n");
3790     char s[128];
3791 
3792     snprintf(s, 128, "Is Preview Running: %d\n", isPreviewRunning());
3793     str += s;
3794 
3795     snprintf(s, 128, "Is Capture Running: %d\n", isCaptureRunning());
3796     str += s;
3797 
3798     snprintf(s, 128, "Is Non ZSL Capture Running: %d\n",
3799         isNonZSLCaptureRunning());
3800     str += s;
3801 
3802     snprintf(s, 128, "Current State: %d \n", m_state);
3803     str += s;
3804 
3805     switch(m_state){
3806         case QCAMERA_SM_STATE_PREVIEW_STOPPED:
3807         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_STOPPED \n");
3808         break;
3809 
3810         case QCAMERA_SM_STATE_PREVIEW_READY:
3811         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_READY \n");
3812         break;
3813 
3814         case QCAMERA_SM_STATE_PREVIEWING:
3815         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEWING \n");
3816         break;
3817 
3818         case QCAMERA_SM_STATE_PREPARE_SNAPSHOT:
3819         snprintf(s, 128, " QCAMERA_SM_STATE_PREPARE_SNAPSHOT \n");
3820         break;
3821 
3822         case QCAMERA_SM_STATE_PIC_TAKING:
3823         snprintf(s, 128, " QCAMERA_SM_STATE_PIC_TAKING \n");
3824         break;
3825 
3826         case QCAMERA_SM_STATE_RECORDING:
3827         snprintf(s, 128, " QCAMERA_SM_STATE_RECORDING \n");
3828         break;
3829 
3830         case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3831         snprintf(s, 128, " QCAMERA_SM_STATE_VIDEO_PIC_TAKING \n");
3832         break;
3833 
3834         case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3835         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_PIC_TAKING \n");
3836         break;
3837     }
3838     str += s;
3839 
3840     return str;
3841 }
3842 
3843 }; // namespace qcamera
3844