• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                     // Send internal events to stop indefinite wait on prepare
1838                     // snapshot done event.
1839                     result.status = rc;
1840                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
1841                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1842                     m_parent->signalAPIResult(&result);
1843 
1844                     result.status = rc;
1845                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
1846                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1847                     m_parent->signalAPIResult(&result);
1848 
1849                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
1850                                             CAMERA_ERROR_SERVER_DIED,
1851                                             0);
1852                 }
1853                 break;
1854             default:
1855                 LOGE("Invalid internal event %d in state(%d)",
1856                              cam_evt->server_event_type, m_state);
1857                 break;
1858             }
1859         }
1860         break;
1861     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1862         {
1863             // No ops, but need to notify
1864             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1865             result.status = rc;
1866             result.request_api = evt;
1867             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1868             m_parent->signalEvtResult(&result);
1869         }
1870        break;
1871     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1872         {
1873             rc = m_parent->updateThermalLevel(payload);
1874         }
1875         break;
1876     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1877     default:
1878         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1879         break;
1880     }
1881 
1882     return rc;
1883 }
1884 
1885 /*===========================================================================
1886  * FUNCTION   : procEvtPicTakingState
1887  *
1888  * DESCRIPTION: finite state machine function to handle event in state of
1889  *              QCAMERA_SM_STATE_PIC_TAKING.
1890  *
1891  * PARAMETERS :
1892  *   @evt      : event to be processed
1893  *   @payload  : event payload. Can be NULL if not needed.
1894  *
1895  * RETURN     : int32_t type of status
1896  *              NO_ERROR  -- success
1897  *              none-zero failure code
1898  *==========================================================================*/
procEvtPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)1899 int32_t QCameraStateMachine::procEvtPicTakingState(qcamera_sm_evt_enum_t evt,
1900                                                    void *payload)
1901 {
1902     int32_t rc = NO_ERROR;
1903     qcamera_api_result_t result;
1904     memset(&result, 0, sizeof(qcamera_api_result_t));
1905 
1906     LOGL("event (%d)", evt);
1907     switch (evt) {
1908     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1909         {
1910             // Error setting preview window during previewing
1911             LOGE("Error!! cannot set preview window when preview is running");
1912             rc = INVALID_OPERATION;
1913             result.status = rc;
1914             result.request_api = evt;
1915             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1916             m_parent->signalAPIResult(&result);
1917         }
1918         break;
1919     case QCAMERA_SM_EVT_SET_CALLBACKS:
1920         {
1921             qcamera_sm_evt_setcb_payload_t *setcbs =
1922                 (qcamera_sm_evt_setcb_payload_t *)payload;
1923             rc = m_parent->setCallBacks(setcbs->notify_cb,
1924                                         setcbs->data_cb,
1925                                         setcbs->data_cb_timestamp,
1926                                         setcbs->get_memory,
1927                                         setcbs->user);
1928             result.status = rc;
1929             result.request_api = evt;
1930             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1931             m_parent->signalAPIResult(&result);
1932         }
1933         break;
1934     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1935         {
1936             rc = m_parent->enableMsgType(*((int32_t *)payload));
1937             result.status = rc;
1938             result.request_api = evt;
1939             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1940             m_parent->signalAPIResult(&result);
1941         }
1942         break;
1943     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1944         {
1945             rc = m_parent->disableMsgType(*((int32_t *)payload));
1946             result.status = rc;
1947             result.request_api = evt;
1948             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1949             m_parent->signalAPIResult(&result);
1950         }
1951         break;
1952     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1953         {
1954             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
1955             result.status = rc;
1956             result.request_api = evt;
1957             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1958             result.enabled = enabled;
1959             m_parent->signalAPIResult(&result);
1960         }
1961         break;
1962     case QCAMERA_SM_EVT_SET_PARAMS:
1963         {
1964             bool needRestart = false;
1965             rc = m_parent->updateParameters((char*)payload, needRestart);
1966             result.status = rc;
1967             result.request_api = evt;
1968             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1969             m_parent->signalAPIResult(&result);
1970         }
1971         break;
1972     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
1973         {
1974             result.status = rc;
1975             result.request_api = evt;
1976             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1977             m_parent->signalAPIResult(&result);
1978         }
1979         break;
1980     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
1981         {
1982             rc = m_parent->commitParameterChanges();
1983             result.status = rc;
1984             result.request_api = evt;
1985             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1986             m_parent->signalAPIResult(&result);
1987         }
1988         break;
1989     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
1990         {
1991             m_parent->setNeedRestart(false);
1992             result.status           =  rc;
1993             result.request_api      =  evt;
1994             result.result_type      =  QCAMERA_API_RESULT_TYPE_DEF;
1995             m_parent->signalAPIResult(&result);
1996         }
1997         break;
1998     case QCAMERA_SM_EVT_GET_PARAMS:
1999         {
2000             result.params = m_parent->getParameters();
2001             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
2002             result.status = rc;
2003             result.request_api = evt;
2004             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
2005             m_parent->signalAPIResult(&result);
2006         }
2007         break;
2008     case QCAMERA_SM_EVT_PUT_PARAMS:
2009         {
2010             rc = m_parent->putParameters((char*)payload);
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_STOP_PREVIEW:
2018         {
2019             // cancel picture first
2020             rc = m_parent->cancelPicture();
2021             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2022 
2023             result.status = rc;
2024             result.request_api = evt;
2025             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2026             m_parent->signalAPIResult(&result);
2027         }
2028         break;
2029     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2030         {
2031             rc = NO_ERROR;
2032             result.status = rc;
2033             result.request_api = evt;
2034             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2035             result.enabled = 0;
2036             m_parent->signalAPIResult(&result);
2037         }
2038         break;
2039     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2040         {
2041             rc = NO_ERROR;
2042             result.status = rc;
2043             result.request_api = evt;
2044             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2045             result.enabled = 0;
2046             m_parent->signalAPIResult(&result);
2047         }
2048         break;
2049     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2050         {
2051             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
2052             result.status = rc;
2053             result.request_api = evt;
2054             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2055             m_parent->signalAPIResult(&result);
2056         }
2057         break;
2058     case QCAMERA_SM_EVT_DUMP:
2059         {
2060             rc = m_parent->dump(*((int *)payload));
2061             result.status = rc;
2062             result.request_api = evt;
2063             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2064             m_parent->signalAPIResult(&result);
2065         }
2066         break;
2067     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2068         {
2069             rc = m_parent->autoFocus();
2070             result.status = rc;
2071             result.request_api = evt;
2072             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2073             m_parent->signalAPIResult(&result);
2074         }
2075         break;
2076     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2077         {
2078             rc = m_parent->cancelAutoFocus();
2079             result.status = rc;
2080             result.request_api = evt;
2081             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2082             m_parent->signalAPIResult(&result);
2083         }
2084         break;
2085     case QCAMERA_SM_EVT_SEND_COMMAND:
2086         {
2087             qcamera_sm_evt_command_payload_t *cmd_payload =
2088                 (qcamera_sm_evt_command_payload_t *)payload;
2089             rc = m_parent->sendCommand(cmd_payload->cmd,
2090                                        cmd_payload->arg1,
2091                                        cmd_payload->arg2);
2092 #ifndef VANILLA_HAL
2093             if ( CAMERA_CMD_LONGSHOT_OFF == cmd_payload->cmd ) {
2094                 // move state to previewing state
2095                 m_state = QCAMERA_SM_STATE_PREVIEWING;
2096             }
2097 #endif
2098             result.status = rc;
2099             result.request_api = evt;
2100             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2101             m_parent->signalAPIResult(&result);
2102         }
2103         break;
2104     case QCAMERA_SM_EVT_CANCEL_PICTURE:
2105         {
2106             rc = m_parent->cancelPicture();
2107             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2108             result.status = rc;
2109             result.request_api = evt;
2110             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2111             m_parent->signalAPIResult(&result);
2112         }
2113         break;
2114     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2115         {
2116             int32_t faceID = 0;
2117             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2118                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2119             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2120                                              reg_payload->config,
2121                                              faceID);
2122             result.status = rc;
2123             result.request_api = evt;
2124             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2125             result.handle = faceID;
2126             m_parent->signalAPIResult(&result);
2127         }
2128         break;
2129     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
2130         {
2131            if ( m_parent->isLongshotEnabled() ) {
2132                // no ops here, need to singal NO_ERROR
2133                rc = NO_ERROR;
2134             } else {
2135                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2136                 rc = INVALID_OPERATION;
2137             }
2138 
2139             result.status = rc;
2140             result.request_api = evt;
2141             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2142             m_parent->signalAPIResult(&result);
2143         }
2144         break;
2145     case QCAMERA_SM_EVT_TAKE_PICTURE:
2146         {
2147            if ( m_parent->isLongshotEnabled() ) {
2148                rc = m_parent->longShot();
2149             } else {
2150                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2151                 rc = INVALID_OPERATION;
2152             }
2153 
2154             result.status = rc;
2155             result.request_api = evt;
2156             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2157             m_parent->signalAPIResult(&result);
2158         }
2159         break;
2160     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
2161     case QCAMERA_SM_EVT_PRE_START_RECORDING:
2162     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
2163     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
2164     case QCAMERA_SM_EVT_START_RECORDING:
2165     case QCAMERA_SM_EVT_STOP_RECORDING:
2166     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2167     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
2168     case QCAMERA_SM_EVT_START_PREVIEW:
2169     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2170     case QCAMERA_SM_EVT_RELEASE:
2171         {
2172             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2173             rc = INVALID_OPERATION;
2174             result.status = rc;
2175             result.request_api = evt;
2176             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2177             m_parent->signalAPIResult(&result);
2178         }
2179         break;
2180     case QCAMERA_SM_EVT_EVT_INTERNAL:
2181         {
2182             qcamera_sm_internal_evt_payload_t *internal_evt =
2183                 (qcamera_sm_internal_evt_payload_t *)payload;
2184             switch (internal_evt->evt_type) {
2185             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2186                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2187                 break;
2188             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
2189                 break;
2190             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
2191                 break;
2192             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
2193                 break;
2194             case QCAMERA_INTERNAL_EVT_CROP_INFO:
2195                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
2196                 break;
2197             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
2198                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
2199                 break;
2200             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
2201                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2202                 break;
2203             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
2204                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
2205                 break;
2206             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
2207                 rc = m_parent->processAEInfo(internal_evt->ae_data);
2208                 break;
2209             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
2210                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
2211                 break;
2212             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
2213                 rc = m_parent->processHDRData(internal_evt->hdr_data);
2214                 break;
2215             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
2216                 rc = m_parent->processRetroAECUnlock();
2217                 break;
2218             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
2219                 rc = m_parent->processZSLCaptureDone();
2220                 break;
2221             default:
2222                 break;
2223             }
2224         }
2225         break;
2226     case QCAMERA_SM_EVT_EVT_NOTIFY:
2227         {
2228             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2229             switch (cam_evt->server_event_type) {
2230             case CAM_EVENT_TYPE_DAEMON_DIED:
2231                 {
2232                     // Send internal events to stop indefinite wait on prepare
2233                     // snapshot done event.
2234                     result.status = rc;
2235                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
2236                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2237                     m_parent->signalAPIResult(&result);
2238 
2239                     result.status = rc;
2240                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
2241                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2242                     m_parent->signalAPIResult(&result);
2243 
2244                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
2245                                             CAMERA_ERROR_SERVER_DIED,
2246                                             0);
2247                 }
2248                 break;
2249             case CAM_EVENT_TYPE_CAC_DONE:
2250                 if (m_parent->isCACEnabled() || m_parent->mParameters.isOEMFeatEnabled()) {
2251                     LOGD("[LONG_SHOT_DBG] : Received CAC Done");
2252                     if (m_parent->isLongshotEnabled()
2253                             && !m_parent->isCaptureShutterEnabled()) {
2254                         // play shutter sound for longshot
2255                         // after CAC stage is done
2256                         m_parent->playShutter();
2257                     }
2258                     m_parent->mCACDoneReceived = TRUE;
2259                 }
2260                 break;
2261             default:
2262                 LOGH("no handling for server evt (%d) at this state",
2263                        cam_evt->server_event_type);
2264                 break;
2265             }
2266         }
2267         break;
2268     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2269         {
2270             qcamera_jpeg_evt_payload_t *jpeg_job =
2271                 (qcamera_jpeg_evt_payload_t *)payload;
2272             rc = m_parent->processJpegNotify(jpeg_job);
2273         }
2274         break;
2275     case QCAMERA_SM_EVT_STOP_CAPTURE_CHANNEL:
2276         {
2277             bool restartPreview = m_parent->isPreviewRestartEnabled();
2278             rc = m_parent->stopCaptureChannel(restartPreview);
2279 
2280             if (restartPreview && (NO_ERROR == rc)) {
2281                 rc = m_parent->preparePreview();
2282                 if (NO_ERROR == rc) {
2283                     m_parent->m_bPreviewStarted = true;
2284                     applyDelayedMsgs();
2285                     rc = m_parent->startPreview();
2286                 }
2287             }
2288 
2289             result.status = rc;
2290             result.request_api = evt;
2291             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2292             m_parent->signalAPIResult(&result);
2293         }
2294         break;
2295     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2296         {
2297             rc = m_parent->cancelPicture();
2298 
2299             bool restartPreview = m_parent->isPreviewRestartEnabled();
2300             if (restartPreview) {
2301                 if (m_parent->mParameters.getManualCaptureMode()
2302                         >= CAM_MANUAL_CAPTURE_TYPE_3) {
2303                     m_parent->mParameters.updateZSLModeValue(m_RestoreZSL);
2304                     m_RestoreZSL = FALSE;
2305                     rc = m_parent->preparePreview();
2306                     if (NO_ERROR == rc) {
2307                         m_parent->m_bPreviewStarted = true;
2308                         applyDelayedMsgs();
2309                         rc = m_parent->startPreview();
2310                     }
2311                 }
2312                 m_state = QCAMERA_SM_STATE_PREVIEWING;
2313             } else {
2314                 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2315             }
2316 
2317             result.status = rc;
2318             result.request_api = evt;
2319             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2320             m_parent->signalEvtResult(&result);
2321         }
2322         break;
2323     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2324         {
2325             rc = m_parent->updateThermalLevel(payload);
2326         }
2327         break;
2328     default:
2329         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2330         break;
2331     }
2332 
2333     return rc;
2334 }
2335 
2336 /*===========================================================================
2337  * FUNCTION   : procEvtRecordingState
2338  *
2339  * DESCRIPTION: finite state machine function to handle event in state of
2340  *              QCAMERA_SM_STATE_RECORDING.
2341  *
2342  * PARAMETERS :
2343  *   @evt      : event to be processed
2344  *   @payload  : event payload. Can be NULL if not needed.
2345  *
2346  * RETURN     : int32_t type of status
2347  *              NO_ERROR  -- success
2348  *              none-zero failure code
2349  *==========================================================================*/
procEvtRecordingState(qcamera_sm_evt_enum_t evt,void * payload)2350 int32_t QCameraStateMachine::procEvtRecordingState(qcamera_sm_evt_enum_t evt,
2351                                                    void *payload)
2352 {
2353     int32_t rc = NO_ERROR;
2354     qcamera_api_result_t result;
2355     memset(&result, 0, sizeof(qcamera_api_result_t));
2356 
2357     LOGL("event (%d)", evt);
2358     switch (evt) {
2359     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
2360     case QCAMERA_SM_EVT_START_PREVIEW:
2361     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2362         {
2363             // WA: CTS test VideoSnapshot will try to
2364             //     start preview during video recording.
2365             LOGH("CTS video restart op");
2366             rc = NO_ERROR;
2367             result.status = rc;
2368             result.request_api = evt;
2369             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2370             m_parent->signalAPIResult(&result);
2371         }
2372         break;
2373     case QCAMERA_SM_EVT_SET_CALLBACKS:
2374         {
2375             qcamera_sm_evt_setcb_payload_t *setcbs =
2376                 (qcamera_sm_evt_setcb_payload_t *)payload;
2377             rc = m_parent->setCallBacks(setcbs->notify_cb,
2378                                         setcbs->data_cb,
2379                                         setcbs->data_cb_timestamp,
2380                                         setcbs->get_memory,
2381                                         setcbs->user);
2382             result.status = rc;
2383             result.request_api = evt;
2384             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2385             m_parent->signalAPIResult(&result);
2386         }
2387         break;
2388     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
2389         {
2390             rc = m_parent->enableMsgType(*((int32_t *)payload));
2391             result.status = rc;
2392             result.request_api = evt;
2393             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2394             m_parent->signalAPIResult(&result);
2395         }
2396         break;
2397     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
2398         {
2399             rc = m_parent->disableMsgType(*((int32_t *)payload));
2400             result.status = rc;
2401             result.request_api = evt;
2402             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2403             m_parent->signalAPIResult(&result);
2404         }
2405         break;
2406     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
2407         {
2408             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
2409             result.status = rc;
2410             result.request_api = evt;
2411             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2412             result.enabled = enabled;
2413             m_parent->signalAPIResult(&result);
2414         }
2415         break;
2416     case QCAMERA_SM_EVT_SET_PARAMS:
2417         {
2418             bool needRestart = false;
2419             rc = m_parent->updateParameters((char*)payload, needRestart);
2420             if (rc == NO_ERROR) {
2421                 if (needRestart) {
2422                     // cannot set parameters that requires restart during recording
2423                     LOGE("Error!! cannot set parameters that requires restart during recording");
2424                     rc = BAD_VALUE;
2425                 }
2426             }
2427             if (rc != NO_ERROR) {
2428                 m_parent->setNeedRestart(false);
2429             }
2430             result.status = rc;
2431             result.request_api = evt;
2432             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2433             m_parent->signalAPIResult(&result);
2434         }
2435         break;
2436     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
2437         {
2438             rc = m_parent->commitParameterChanges();
2439             result.status = rc;
2440             result.request_api = evt;
2441             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2442             m_parent->signalAPIResult(&result);
2443         }
2444         break;
2445     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
2446     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
2447         {
2448             ALOGE("%s: Error!! cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2449             rc = INVALID_OPERATION;
2450             result.status = rc;
2451             result.request_api = evt;
2452             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2453             m_parent->signalAPIResult(&result);
2454         }
2455         break;
2456     case QCAMERA_SM_EVT_GET_PARAMS:
2457         {
2458             result.params = m_parent->getParameters();
2459             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
2460             result.status = rc;
2461             result.request_api = evt;
2462             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
2463             m_parent->signalAPIResult(&result);
2464         }
2465         break;
2466     case QCAMERA_SM_EVT_PUT_PARAMS:
2467         {
2468             rc = m_parent->putParameters((char*)payload);
2469             result.status = rc;
2470             result.request_api = evt;
2471             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2472             m_parent->signalAPIResult(&result);
2473         }
2474         break;
2475     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2476         {
2477             rc = NO_ERROR;
2478             result.status = rc;
2479             result.request_api = evt;
2480             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2481             result.enabled = 0;
2482             m_parent->signalAPIResult(&result);
2483         }
2484         break;
2485     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2486         {
2487             rc = NO_ERROR;
2488             result.status = rc;
2489             result.request_api = evt;
2490             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2491             result.enabled = 1;
2492             m_parent->signalAPIResult(&result);
2493         }
2494         break;
2495     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2496         {
2497             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
2498             result.status = rc;
2499             result.request_api = evt;
2500             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2501             m_parent->signalAPIResult(&result);
2502         }
2503         break;
2504     case QCAMERA_SM_EVT_DUMP:
2505         {
2506             rc = m_parent->dump(*((int *)payload));
2507             result.status = rc;
2508             result.request_api = evt;
2509             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2510             m_parent->signalAPIResult(&result);
2511         }
2512         break;
2513     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2514         {
2515             rc = m_parent->autoFocus();
2516             result.status = rc;
2517             result.request_api = evt;
2518             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2519             m_parent->signalAPIResult(&result);
2520         }
2521         break;
2522     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2523         {
2524             rc = m_parent->cancelAutoFocus();
2525             result.status = rc;
2526             result.request_api = evt;
2527             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2528             m_parent->signalAPIResult(&result);
2529         }
2530         break;
2531     case QCAMERA_SM_EVT_SEND_COMMAND:
2532         {
2533             qcamera_sm_evt_command_payload_t *cmd_payload =
2534                 (qcamera_sm_evt_command_payload_t *)payload;
2535             rc = m_parent->sendCommand(cmd_payload->cmd,
2536                                        cmd_payload->arg1,
2537                                        cmd_payload->arg2);
2538             result.status = rc;
2539             result.request_api = evt;
2540             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2541             m_parent->signalAPIResult(&result);
2542         }
2543         break;
2544     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
2545         {
2546             // No ops here, send NO_ERROR.
2547             rc = NO_ERROR;
2548             result.status = rc;
2549             result.request_api = evt;
2550             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2551             m_parent->signalAPIResult(&result);
2552         }
2553         break;
2554     case QCAMERA_SM_EVT_TAKE_PICTURE:
2555         {
2556             m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
2557             rc = m_parent->takeLiveSnapshot();
2558             if (rc != NO_ERROR) {
2559                 m_parent->unconfigureAdvancedCapture();
2560                 m_state = QCAMERA_SM_STATE_RECORDING;
2561             }
2562             result.status = rc;
2563             result.request_api = evt;
2564             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2565             m_parent->signalAPIResult(&result);
2566         }
2567         break;
2568     case QCAMERA_SM_EVT_PRE_START_RECORDING:
2569     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
2570     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
2571     case QCAMERA_SM_EVT_START_RECORDING:
2572         {
2573             // no ops here
2574             LOGW("already in recording state, no ops for start_recording");
2575             rc = 0;
2576             result.status = rc;
2577             result.request_api = evt;
2578             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2579             m_parent->signalAPIResult(&result);
2580         }
2581         break;
2582     case QCAMERA_SM_EVT_STOP_RECORDING:
2583         {
2584             rc = m_parent->stopRecording();
2585             m_state = QCAMERA_SM_STATE_PREVIEWING;
2586             result.status = rc;
2587             result.request_api = evt;
2588             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2589             m_parent->signalAPIResult(&result);
2590         }
2591         break;
2592     case QCAMERA_SM_EVT_STOP_PREVIEW:
2593         {
2594             rc = m_parent->stopRecording();
2595             m_state = QCAMERA_SM_STATE_PREVIEWING;
2596 
2597             rc = m_parent->stopPreview();
2598             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2599 
2600             result.status = rc;
2601             result.request_api = evt;
2602             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2603             m_parent->signalAPIResult(&result);
2604         }
2605         break;
2606     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2607         {
2608             rc = m_parent->releaseRecordingFrame((const void *)payload);
2609             result.status = rc;
2610             result.request_api = evt;
2611             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2612             m_parent->signalAPIResult(&result);
2613         }
2614         break;
2615     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2616         {
2617             int32_t faceID = 0;
2618             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2619                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2620             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2621                                              reg_payload->config,
2622                                              faceID);
2623             result.status = rc;
2624             result.request_api = evt;
2625             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2626             result.handle = faceID;
2627             m_parent->signalAPIResult(&result);
2628         }
2629         break;
2630     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
2631         {
2632             //In Video snapshot, prepare hardware is a no-op.
2633             result.status = NO_ERROR;
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_CANCEL_PICTURE:
2640     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2641     case QCAMERA_SM_EVT_RELEASE:
2642         {
2643             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2644             rc = INVALID_OPERATION;
2645             result.status = rc;
2646             result.request_api = evt;
2647             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2648             m_parent->signalAPIResult(&result);
2649         }
2650         break;
2651     case QCAMERA_SM_EVT_EVT_INTERNAL:
2652         {
2653             qcamera_sm_internal_evt_payload_t *internal_evt =
2654                 (qcamera_sm_internal_evt_payload_t *)payload;
2655             switch (internal_evt->evt_type) {
2656             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2657                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2658                 break;
2659             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
2660                 break;
2661             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
2662                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
2663                 break;
2664             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
2665                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
2666                 break;
2667             case QCAMERA_INTERNAL_EVT_CROP_INFO:
2668                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
2669                 break;
2670             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
2671                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
2672                 break;
2673             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
2674                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2675                 break;
2676             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
2677                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
2678                 break;
2679             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
2680                 rc = m_parent->processAEInfo(internal_evt->ae_data);
2681                 break;
2682             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
2683                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
2684                 break;
2685             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
2686                 rc = m_parent->processHDRData(internal_evt->hdr_data);
2687                 break;
2688             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
2689                 rc = m_parent->processRetroAECUnlock();
2690                 break;
2691             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
2692                 rc = m_parent->processZSLCaptureDone();
2693                 break;
2694             default:
2695                 break;
2696             }
2697         }
2698         break;
2699     case QCAMERA_SM_EVT_EVT_NOTIFY:
2700         {
2701             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2702             switch (cam_evt->server_event_type) {
2703             case CAM_EVENT_TYPE_DAEMON_DIED:
2704                 {
2705                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
2706                                             CAMERA_ERROR_SERVER_DIED,
2707                                             0);
2708                 }
2709                 break;
2710             default:
2711                 LOGE("Invalid internal event %d in state(%d)",
2712                              cam_evt->server_event_type, m_state);
2713                 break;
2714             }
2715         }
2716         break;
2717     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2718         {
2719             rc = m_parent->updateThermalLevel(payload);
2720         }
2721         break;
2722     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2723         {
2724             // No ops, but need to notify
2725             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2726             result.status = rc;
2727             result.request_api = evt;
2728             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2729             m_parent->signalEvtResult(&result);
2730         }
2731        break;
2732     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2733     default:
2734         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2735         break;
2736     }
2737 
2738     return rc;
2739 }
2740 
2741 /*===========================================================================
2742  * FUNCTION   : procEvtVideoPicTakingState
2743  *
2744  * DESCRIPTION: finite state machine function to handle event in state of
2745  *              QCAMERA_SM_STATE_VIDEO_PIC_TAKING.
2746  *
2747  * PARAMETERS :
2748  *   @evt      : event to be processed
2749  *   @payload  : event payload. Can be NULL if not needed.
2750  *
2751  * RETURN     : int32_t type of status
2752  *              NO_ERROR  -- success
2753  *              none-zero failure code
2754  *==========================================================================*/
procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)2755 int32_t QCameraStateMachine::procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,
2756                                                         void *payload)
2757 {
2758     int32_t rc = NO_ERROR;
2759     qcamera_api_result_t result;
2760     memset(&result, 0, sizeof(qcamera_api_result_t));
2761 
2762     LOGL("event (%d)", evt);
2763     switch (evt) {
2764     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2765         {
2766             // Error setting preview window during previewing
2767             LOGE("Error!! cannot set preview window when preview is running");
2768             rc = INVALID_OPERATION;
2769             result.status = rc;
2770             result.request_api = evt;
2771             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2772             m_parent->signalAPIResult(&result);
2773         }
2774         break;
2775     case QCAMERA_SM_EVT_SET_CALLBACKS:
2776         {
2777             qcamera_sm_evt_setcb_payload_t *setcbs =
2778                 (qcamera_sm_evt_setcb_payload_t *)payload;
2779             rc = m_parent->setCallBacks(setcbs->notify_cb,
2780                                         setcbs->data_cb,
2781                                         setcbs->data_cb_timestamp,
2782                                         setcbs->get_memory,
2783                                         setcbs->user);
2784             result.status = rc;
2785             result.request_api = evt;
2786             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2787             m_parent->signalAPIResult(&result);
2788         }
2789         break;
2790     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
2791         {
2792             rc = m_parent->enableMsgType(*((int32_t *)payload));
2793             result.status = rc;
2794             result.request_api = evt;
2795             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2796             m_parent->signalAPIResult(&result);
2797         }
2798         break;
2799     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
2800         {
2801             rc = m_parent->disableMsgType(*((int32_t *)payload));
2802             result.status = rc;
2803             result.request_api = evt;
2804             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2805             m_parent->signalAPIResult(&result);
2806         }
2807         break;
2808     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
2809         {
2810             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
2811             result.status = rc;
2812             result.request_api = evt;
2813             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2814             result.enabled = enabled;
2815             m_parent->signalAPIResult(&result);
2816         }
2817         break;
2818     case QCAMERA_SM_EVT_SET_PARAMS:
2819         {
2820             bool needRestart = false;
2821             rc = m_parent->updateParameters((char*)payload, needRestart);
2822             if (rc == NO_ERROR) {
2823                 if (needRestart) {
2824                     // cannot set parameters that requires restart during recording
2825                     LOGE("Error!! cannot set parameters that requires restart during recording");
2826                     rc = BAD_VALUE;
2827                 }
2828             }
2829             if (rc != NO_ERROR) {
2830                 m_parent->setNeedRestart(false);
2831             }
2832             result.status = rc;
2833             result.request_api = evt;
2834             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2835             m_parent->signalAPIResult(&result);
2836         }
2837         break;
2838     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
2839         {
2840             rc = m_parent->commitParameterChanges();
2841             result.status = rc;
2842             result.request_api = evt;
2843             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2844             m_parent->signalAPIResult(&result);
2845         }
2846         break;
2847     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
2848     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
2849         {
2850             ALOGE("%s: Error!! cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2851             rc = INVALID_OPERATION;
2852             result.status = rc;
2853             result.request_api = evt;
2854             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2855             m_parent->signalAPIResult(&result);
2856         }
2857         break;
2858     case QCAMERA_SM_EVT_GET_PARAMS:
2859         {
2860             result.params = m_parent->getParameters();
2861             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
2862             result.status = rc;
2863             result.request_api = evt;
2864             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
2865             m_parent->signalAPIResult(&result);
2866         }
2867         break;
2868     case QCAMERA_SM_EVT_PUT_PARAMS:
2869         {
2870             rc = m_parent->putParameters((char*)payload);
2871             result.status = rc;
2872             result.request_api = evt;
2873             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2874             m_parent->signalAPIResult(&result);
2875         }
2876         break;
2877     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2878         {
2879             rc = NO_ERROR;
2880             result.status = rc;
2881             result.request_api = evt;
2882             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2883             result.enabled = 1;
2884             m_parent->signalAPIResult(&result);
2885         }
2886         break;
2887     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2888         {
2889             rc = NO_ERROR;
2890             result.status = rc;
2891             result.request_api = evt;
2892             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2893             result.enabled = 1;
2894             m_parent->signalAPIResult(&result);
2895         }
2896         break;
2897     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2898         {
2899             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
2900             result.status = rc;
2901             result.request_api = evt;
2902             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2903             m_parent->signalAPIResult(&result);
2904         }
2905         break;
2906     case QCAMERA_SM_EVT_DUMP:
2907         {
2908             rc = m_parent->dump(*((int *)payload));
2909             result.status = rc;
2910             result.request_api = evt;
2911             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2912             m_parent->signalAPIResult(&result);
2913         }
2914         break;
2915     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2916         {
2917             rc = m_parent->autoFocus();
2918             result.status = rc;
2919             result.request_api = evt;
2920             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2921             m_parent->signalAPIResult(&result);
2922         }
2923         break;
2924     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2925         {
2926             rc = m_parent->cancelAutoFocus();
2927             result.status = rc;
2928             result.request_api = evt;
2929             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2930             m_parent->signalAPIResult(&result);
2931         }
2932         break;
2933     case QCAMERA_SM_EVT_SEND_COMMAND:
2934         {
2935             qcamera_sm_evt_command_payload_t *cmd_payload =
2936                 (qcamera_sm_evt_command_payload_t *)payload;
2937             rc = m_parent->sendCommand(cmd_payload->cmd,
2938                                        cmd_payload->arg1,
2939                                        cmd_payload->arg2);
2940             result.status = rc;
2941             result.request_api = evt;
2942             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2943             m_parent->signalAPIResult(&result);
2944         }
2945         break;
2946     case QCAMERA_SM_EVT_STOP_RECORDING:
2947         {
2948             rc = m_parent->cancelLiveSnapshot();
2949             m_state = QCAMERA_SM_STATE_RECORDING;
2950 
2951             rc = m_parent->stopRecording();
2952             m_state = QCAMERA_SM_STATE_PREVIEWING;
2953 
2954             result.status = rc;
2955             result.request_api = evt;
2956             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2957             m_parent->signalAPIResult(&result);
2958         }
2959         break;
2960     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2961         {
2962             rc = m_parent->releaseRecordingFrame((const void *)payload);
2963             result.status = rc;
2964             result.request_api = evt;
2965             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2966             m_parent->signalAPIResult(&result);
2967         }
2968         break;
2969     case QCAMERA_SM_EVT_CANCEL_PICTURE:
2970         {
2971             rc = m_parent->cancelLiveSnapshot();
2972             m_state = QCAMERA_SM_STATE_RECORDING;
2973             result.status = rc;
2974             result.request_api = evt;
2975             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2976             m_parent->signalAPIResult(&result);
2977         }
2978         break;
2979     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2980         {
2981             int32_t faceID = 0;
2982             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2983                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2984             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2985                                              reg_payload->config,
2986                                              faceID);
2987             result.status = rc;
2988             result.request_api = evt;
2989             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2990             result.handle = faceID;
2991             m_parent->signalAPIResult(&result);
2992         }
2993         break;
2994     case QCAMERA_SM_EVT_STOP_PREVIEW:
2995         {
2996             rc = m_parent->cancelLiveSnapshot();
2997             m_state = QCAMERA_SM_STATE_RECORDING;
2998 
2999             rc = m_parent->stopRecording();
3000             m_state = QCAMERA_SM_STATE_PREVIEWING;
3001 
3002             rc = m_parent->stopPreview();
3003             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
3004 
3005             result.status = rc;
3006             result.request_api = evt;
3007             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3008             m_parent->signalAPIResult(&result);
3009         }
3010         break;
3011     case QCAMERA_SM_EVT_PRE_START_RECORDING:
3012     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
3013     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
3014     case QCAMERA_SM_EVT_START_RECORDING:
3015     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
3016     case QCAMERA_SM_EVT_START_PREVIEW:
3017     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
3018     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
3019     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
3020     case QCAMERA_SM_EVT_TAKE_PICTURE:
3021     case QCAMERA_SM_EVT_RELEASE:
3022         {
3023             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3024             rc = INVALID_OPERATION;
3025             result.status = rc;
3026             result.request_api = evt;
3027             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3028             m_parent->signalAPIResult(&result);
3029         }
3030         break;
3031     case QCAMERA_SM_EVT_EVT_INTERNAL:
3032         {
3033             qcamera_sm_internal_evt_payload_t *internal_evt =
3034                 (qcamera_sm_internal_evt_payload_t *)payload;
3035             switch (internal_evt->evt_type) {
3036             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
3037                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
3038                 break;
3039             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
3040                 break;
3041             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
3042                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
3043                 break;
3044             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
3045                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
3046                 break;
3047             case QCAMERA_INTERNAL_EVT_CROP_INFO:
3048                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
3049                 break;
3050             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
3051                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
3052                 break;
3053             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
3054                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3055                 break;
3056             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
3057                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
3058                 break;
3059             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
3060                 rc = m_parent->processAEInfo(internal_evt->ae_data);
3061                 break;
3062             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
3063                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
3064                 break;
3065             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
3066                 rc = m_parent->processHDRData(internal_evt->hdr_data);
3067                 break;
3068             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
3069                 rc = m_parent->processRetroAECUnlock();
3070                 break;
3071             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
3072                 rc = m_parent->processZSLCaptureDone();
3073                 break;
3074             default:
3075                 break;
3076             }
3077         }
3078         break;
3079     case QCAMERA_SM_EVT_EVT_NOTIFY:
3080         {
3081             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
3082             switch (cam_evt->server_event_type) {
3083             case CAM_EVENT_TYPE_DAEMON_DIED:
3084                 {
3085                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
3086                                             CAMERA_ERROR_SERVER_DIED,
3087                                             0);
3088                 }
3089                 break;
3090             default:
3091                 LOGE("Invalid internal event %d in state(%d)",
3092                              cam_evt->server_event_type, m_state);
3093                 break;
3094             }
3095         }
3096         break;
3097     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
3098         {
3099             qcamera_jpeg_evt_payload_t *jpeg_job =
3100                 (qcamera_jpeg_evt_payload_t *)payload;
3101             rc = m_parent->processJpegNotify(jpeg_job);
3102         }
3103         break;
3104     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
3105         {
3106             rc = m_parent->cancelLiveSnapshot();
3107             m_state = QCAMERA_SM_STATE_RECORDING;
3108             result.status = rc;
3109             result.request_api = evt;
3110             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3111             m_parent->signalEvtResult(&result);
3112         }
3113         break;
3114     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
3115         {
3116             rc = m_parent->updateThermalLevel(payload);
3117         }
3118         break;
3119     default:
3120         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3121         break;
3122     }
3123 
3124     return rc;
3125 }
3126 
3127 /*===========================================================================
3128  * FUNCTION   : procEvtPreviewPicTakingState
3129  *
3130  * DESCRIPTION: finite state machine function to handle event in state of
3131  *              QCAMERA_SM_STATE_PREVIEW_PIC_TAKING.
3132  *
3133  * PARAMETERS :
3134  *   @evt      : event to be processed
3135  *   @payload  : event payload. Can be NULL if not needed.
3136  *
3137  * RETURN     : int32_t type of status
3138  *              NO_ERROR  -- success
3139  *              none-zero failure code
3140  *==========================================================================*/
procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)3141 int32_t QCameraStateMachine::procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,
3142                                                           void *payload)
3143 {
3144     int32_t rc = NO_ERROR;
3145     qcamera_api_result_t result;
3146     memset(&result, 0, sizeof(qcamera_api_result_t));
3147 
3148     LOGL("event (%d)", evt);
3149     switch (evt) {
3150     case QCAMERA_SM_EVT_SET_CALLBACKS:
3151         {
3152             qcamera_sm_evt_setcb_payload_t *setcbs =
3153                 (qcamera_sm_evt_setcb_payload_t *)payload;
3154             rc = m_parent->setCallBacks(setcbs->notify_cb,
3155                                         setcbs->data_cb,
3156                                         setcbs->data_cb_timestamp,
3157                                         setcbs->get_memory,
3158                                         setcbs->user);
3159             result.status = rc;
3160             result.request_api = evt;
3161             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3162             m_parent->signalAPIResult(&result);
3163         }
3164         break;
3165     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
3166         {
3167             rc = m_parent->enableMsgType(*((int32_t *)payload));
3168             result.status = rc;
3169             result.request_api = evt;
3170             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3171             m_parent->signalAPIResult(&result);
3172         }
3173         break;
3174     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
3175         {
3176             rc = m_parent->disableMsgType(*((int32_t *)payload));
3177             result.status = rc;
3178             result.request_api = evt;
3179             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3180             m_parent->signalAPIResult(&result);
3181         }
3182         break;
3183     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
3184         {
3185             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
3186             result.status = rc;
3187             result.request_api = evt;
3188             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
3189             result.enabled = enabled;
3190             m_parent->signalAPIResult(&result);
3191         }
3192         break;
3193     case QCAMERA_SM_EVT_SET_PARAMS:
3194         {
3195             bool needRestart = false;
3196             rc = m_parent->updateParameters((char*)payload, needRestart);
3197             result.status = rc;
3198             result.request_api = evt;
3199             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3200             m_parent->signalAPIResult(&result);
3201         }
3202         break;
3203     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
3204         {
3205             // need restart preview for parameters to take effect
3206             LOGD("Stopping preview...");
3207             // stop preview
3208             rc = m_parent->stopPreview();
3209             // Clear memory pools
3210             m_parent->m_memoryPool.clear();
3211             result.status = rc;
3212             result.request_api = evt;
3213             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3214             m_parent->signalAPIResult(&result);
3215         }
3216         break;
3217     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
3218         {
3219             // commit parameter changes to server
3220             rc = m_parent->commitParameterChanges();
3221             result.status = rc;
3222             result.request_api = evt;
3223             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3224             m_parent->signalAPIResult(&result);
3225         }
3226         break;
3227     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
3228         {
3229             // start preview again
3230             rc = m_parent->preparePreview();
3231             if (rc == NO_ERROR) {
3232                 applyDelayedMsgs();
3233                 rc = m_parent->startPreview();
3234                 if (rc != NO_ERROR) {
3235                     m_parent->unpreparePreview();
3236                 }
3237             }
3238             if (rc != NO_ERROR) {
3239                 m_state = QCAMERA_SM_STATE_PIC_TAKING;
3240             }
3241             m_parent->setNeedRestart(false);
3242             result.status           =  rc;
3243             result.request_api      =  evt;
3244             result.result_type      =  QCAMERA_API_RESULT_TYPE_DEF;
3245             m_parent->signalAPIResult(&result);
3246         }
3247         break;
3248     case QCAMERA_SM_EVT_GET_PARAMS:
3249         {
3250             result.params = m_parent->getParameters();
3251             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
3252             result.status = rc;
3253             result.request_api = evt;
3254             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
3255             m_parent->signalAPIResult(&result);
3256         }
3257         break;
3258     case QCAMERA_SM_EVT_PUT_PARAMS:
3259         {
3260             rc = m_parent->putParameters((char*)payload);
3261             result.status = rc;
3262             result.request_api = evt;
3263             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3264             m_parent->signalAPIResult(&result);
3265         }
3266         break;
3267     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
3268         {
3269             rc = NO_ERROR;
3270             result.status = rc;
3271             result.request_api = evt;
3272             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
3273             result.enabled = 1;
3274             m_parent->signalAPIResult(&result);
3275         }
3276         break;
3277     case QCAMERA_SM_EVT_RECORDING_ENABLED:
3278         {
3279             rc = NO_ERROR;
3280             result.status = rc;
3281             result.request_api = evt;
3282             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
3283             result.enabled = 0;
3284             m_parent->signalAPIResult(&result);
3285         }
3286         break;
3287     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
3288         {
3289             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
3290             result.status = rc;
3291             result.request_api = evt;
3292             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3293             m_parent->signalAPIResult(&result);
3294         }
3295         break;
3296     case QCAMERA_SM_EVT_DUMP:
3297         {
3298             rc = m_parent->dump(*((int *)payload));
3299             result.status = rc;
3300             result.request_api = evt;
3301             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3302             m_parent->signalAPIResult(&result);
3303         }
3304         break;
3305     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
3306         {
3307             rc = m_parent->autoFocus();
3308             result.status = rc;
3309             result.request_api = evt;
3310             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3311             m_parent->signalAPIResult(&result);
3312         }
3313         break;
3314     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
3315         {
3316             rc = m_parent->cancelAutoFocus();
3317             result.status = rc;
3318             result.request_api = evt;
3319             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3320             m_parent->signalAPIResult(&result);
3321         }
3322         break;
3323     case QCAMERA_SM_EVT_SEND_COMMAND:
3324         {
3325             qcamera_sm_evt_command_payload_t *cmd_payload =
3326                 (qcamera_sm_evt_command_payload_t *)payload;
3327             rc = m_parent->sendCommand(cmd_payload->cmd,
3328                                        cmd_payload->arg1,
3329                                        cmd_payload->arg2);
3330 #ifndef VANILLA_HAL
3331             if ( CAMERA_CMD_LONGSHOT_OFF == cmd_payload->cmd ) {
3332                 // move state to previewing state
3333                 m_state = QCAMERA_SM_STATE_PREVIEWING;
3334             }
3335 #endif
3336             result.status = rc;
3337             result.request_api = evt;
3338             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3339             m_parent->signalAPIResult(&result);
3340         }
3341         break;
3342     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
3343         {
3344             rc = m_parent->releaseRecordingFrame((const void *)payload);
3345             result.status = rc;
3346             result.request_api = evt;
3347             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3348             m_parent->signalAPIResult(&result);
3349         }
3350         break;
3351     case QCAMERA_SM_EVT_CANCEL_PICTURE:
3352         {
3353             if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
3354                 rc = m_parent->cancelPicture();
3355             } else {
3356                 rc = m_parent->cancelLiveSnapshot();
3357             }
3358             m_state = QCAMERA_SM_STATE_PREVIEWING;
3359             result.status = rc;
3360             result.request_api = evt;
3361             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3362             m_parent->signalAPIResult(&result);
3363         }
3364         break;
3365     case QCAMERA_SM_EVT_STOP_PREVIEW:
3366         {
3367             if (m_parent->isZSLMode()) {
3368                 // cancel picture first
3369                 rc = m_parent->cancelPicture();
3370                 m_parent->stopChannel(QCAMERA_CH_TYPE_ZSL);
3371             } else if (m_parent->isLongshotEnabled()) {
3372                 // just cancel picture
3373                 rc = m_parent->cancelPicture();
3374             } else {
3375                 rc = m_parent->cancelLiveSnapshot();
3376                 m_parent->stopChannel(QCAMERA_CH_TYPE_PREVIEW);
3377             }
3378             // unprepare preview
3379             m_parent->unpreparePreview();
3380             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
3381             result.status = rc;
3382             result.request_api = evt;
3383             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3384             m_parent->signalAPIResult(&result);
3385         }
3386         break;
3387     case QCAMERA_SM_EVT_PRE_START_RECORDING:
3388         {
3389             if (m_parent->isZSLMode()) {
3390                 LOGE("Error!! cannot handle evt(%d) in state(%d) in ZSL mode", evt, m_state);
3391                 rc = INVALID_OPERATION;
3392             } else if (m_parent->isLongshotEnabled()) {
3393                 LOGE("Error!! cannot handle evt(%d) in state(%d) in Longshot mode", evt, m_state);
3394                 rc = INVALID_OPERATION;
3395             } else {
3396                 rc = m_parent->preStartRecording();
3397             }
3398             result.status = rc;
3399             result.request_api = evt;
3400             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3401             m_parent->signalAPIResult(&result);
3402         }
3403         break;
3404     case QCAMERA_SM_EVT_START_RECORDING:
3405         {
3406             if (m_parent->isZSLMode()) {
3407                 LOGE("Error!! cannot handle evt(%d) in state(%d) in ZSL mode",
3408                        evt, m_state);
3409                 rc = INVALID_OPERATION;
3410             } else if (m_parent->isLongshotEnabled()) {
3411                 LOGE("Error!! cannot handle evt(%d) in state(%d) in Longshot mode",
3412                        evt, m_state);
3413                 rc = INVALID_OPERATION;
3414             } else {
3415                 rc = m_parent->startRecording();
3416                 if (rc == NO_ERROR) {
3417                     m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
3418                 }
3419             }
3420             result.status = rc;
3421             result.request_api = evt;
3422             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3423             m_parent->signalAPIResult(&result);
3424         }
3425         break;
3426     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
3427         {
3428             int32_t faceID = 0;
3429             qcamera_sm_evt_reg_face_payload_t *reg_payload =
3430                 (qcamera_sm_evt_reg_face_payload_t *)payload;
3431             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
3432                                              reg_payload->config,
3433                                              faceID);
3434             result.status = rc;
3435             result.request_api = evt;
3436             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
3437             result.handle = faceID;
3438             m_parent->signalAPIResult(&result);
3439         }
3440         break;
3441     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
3442         {
3443            if ( m_parent->isLongshotEnabled() ) {
3444                // no ops here, need to singal NO_ERROR
3445                rc = NO_ERROR;
3446             } else {
3447                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3448                 rc = INVALID_OPERATION;
3449             }
3450 
3451             result.status = rc;
3452             result.request_api = evt;
3453             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3454             m_parent->signalAPIResult(&result);
3455         }
3456         break;
3457     case QCAMERA_SM_EVT_TAKE_PICTURE:
3458         {
3459             if ( m_parent->isLongshotEnabled() ) {
3460                rc = m_parent->longShot();
3461             } else {
3462                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3463                 rc = INVALID_OPERATION;
3464             }
3465 
3466             result.status = rc;
3467             result.request_api = evt;
3468             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3469             m_parent->signalAPIResult(&result);
3470         }
3471         break;
3472 
3473     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
3474         {
3475           LOGD("Prepare Snapshot");
3476           if (m_parent->isRetroPicture()) {
3477               LOGD("Prepare Snapshot in Retro Mode");
3478               rc = m_parent->prepareHardwareForSnapshot(FALSE);
3479               if (rc != NO_ERROR) {
3480                   LOGE("prepareHardwareForSnapshot failed %d",
3481                        rc);
3482                   result.status = rc;
3483                   result.request_api = evt;
3484                   result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3485                   m_parent->signalAPIResult(&result);
3486               }
3487           }
3488           else {
3489               LOGE("Error!! cannot handle evt(%d) in state(%d)",
3490                  evt, m_state);
3491               rc = INVALID_OPERATION;
3492               result.status = rc;
3493               result.request_api = evt;
3494               result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3495               m_parent->signalAPIResult(&result);
3496           }
3497         }
3498         break;
3499     case QCAMERA_SM_EVT_STOP_RECORDING:
3500     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
3501     case QCAMERA_SM_EVT_START_PREVIEW:
3502     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
3503     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
3504     case QCAMERA_SM_EVT_RELEASE:
3505         {
3506             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3507             rc = INVALID_OPERATION;
3508             result.status = rc;
3509             result.request_api = evt;
3510             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3511             m_parent->signalAPIResult(&result);
3512         }
3513         break;
3514     case QCAMERA_SM_EVT_EVT_INTERNAL:
3515         {
3516             qcamera_sm_internal_evt_payload_t *internal_evt =
3517                 (qcamera_sm_internal_evt_payload_t *)payload;
3518             switch (internal_evt->evt_type) {
3519             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
3520                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
3521                 break;
3522             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
3523                 LOGD("Received QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE event");
3524                 if (m_parent->isRetroPicture()) {
3525                     m_parent->processPrepSnapshotDoneEvent(internal_evt->prep_snapshot_state);
3526                     LOGD("Retro picture");
3527                     result.status = NO_ERROR;
3528                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
3529                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3530                     m_parent->signalAPIResult(&result);
3531                 }
3532                 else {
3533                     LOGE("Invalid Case for  "
3534                             "QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event");
3535                 }
3536                 break;
3537             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
3538                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
3539                 break;
3540             case QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT:
3541                 // This is valid only in Retro picture Mode
3542                 if (m_parent->isRetroPicture()) {
3543                     LOGD("Received QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event");
3544                     result.status = NO_ERROR;
3545                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
3546                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3547                     m_parent->signalAPIResult(&result);
3548                 }
3549                 else {
3550                     LOGD("Wrong Case for QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event");
3551                 }
3552                 break;
3553             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
3554                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
3555                 break;
3556             case QCAMERA_INTERNAL_EVT_CROP_INFO:
3557                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
3558                 break;
3559             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
3560                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
3561                 break;
3562             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
3563                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3564                 break;
3565             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
3566                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
3567                 break;
3568             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
3569                 rc = m_parent->processAEInfo(internal_evt->ae_data);
3570                 break;
3571             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
3572                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
3573                 break;
3574             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
3575                 rc = m_parent->processHDRData(internal_evt->hdr_data);
3576                 break;
3577             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
3578                 rc = m_parent->processRetroAECUnlock();
3579                 break;
3580             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
3581                 rc = m_parent->processZSLCaptureDone();
3582                 break;
3583             default:
3584                 break;
3585             }
3586         }
3587         break;
3588     case QCAMERA_SM_EVT_EVT_NOTIFY:
3589         {
3590             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
3591             switch (cam_evt->server_event_type) {
3592             case CAM_EVENT_TYPE_DAEMON_DIED:
3593                 {
3594                     // Send internal events to stop indefinite wait on prepare
3595                     // snapshot done event.
3596                     result.status = rc;
3597                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
3598                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3599                     m_parent->signalAPIResult(&result);
3600 
3601                     result.status = rc;
3602                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
3603                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3604                     m_parent->signalAPIResult(&result);
3605 
3606                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
3607                                             CAMERA_ERROR_SERVER_DIED,
3608                                             0);
3609                 }
3610                 break;
3611             case CAM_EVENT_TYPE_CAC_DONE:
3612                 if (m_parent->isCACEnabled() || m_parent->mParameters.isOEMFeatEnabled()) {
3613                     LOGD("[LONG_SHOT_DBG] : Received CAC Done");
3614                     if ((m_parent->isLongshotEnabled())
3615                             && (!m_parent->isCaptureShutterEnabled())) {
3616                         // play shutter sound for longshot
3617                         // after CAC stage is done
3618                         m_parent->playShutter();
3619                     }
3620                     m_parent->mCACDoneReceived = TRUE;
3621                 }
3622                 break;
3623             default:
3624                 LOGE("Invalid internal event %d in state(%d)",
3625                              cam_evt->server_event_type, m_state);
3626                 break;
3627             }
3628         }
3629         break;
3630     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
3631         {
3632             LOGL("Calling Process Jpeg Notify");
3633             qcamera_jpeg_evt_payload_t *jpeg_job =
3634                 (qcamera_jpeg_evt_payload_t *)payload;
3635             rc = m_parent->processJpegNotify(jpeg_job);
3636         }
3637         break;
3638     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
3639         {
3640             LOGL("Snapshot Done");
3641             if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
3642                 rc = m_parent->cancelPicture();
3643             } else {
3644                 rc = m_parent->cancelLiveSnapshot();
3645             }
3646             m_state = QCAMERA_SM_STATE_PREVIEWING;
3647             if (m_parent->isRetroPicture()){
3648                 result.status = rc;
3649                 result.request_api = evt;
3650                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3651                 LOGL("\n Signalling for JPEG snapshot done!!");
3652                 m_parent->signalAPIResult(&result);
3653 
3654             }
3655             result.status = rc;
3656             result.request_api = evt;
3657             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3658             m_parent->signalEvtResult(&result);
3659         }
3660         break;
3661     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
3662         {
3663             rc = m_parent->updateThermalLevel(payload);
3664         }
3665         break;
3666     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
3667         {
3668             m_parent->stopPreview();
3669             result.status = rc;
3670             result.request_api = evt;
3671             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3672             m_parent->signalAPIResult(&result);
3673         }
3674        break;
3675     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
3676         {
3677             rc = m_parent->preparePreview();
3678             if (rc == NO_ERROR) {
3679                 rc = m_parent->startPreview();
3680             }
3681             result.status = rc;
3682             result.request_api = evt;
3683             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3684             m_parent->signalAPIResult(&result);
3685         }
3686        break;
3687     default:
3688         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3689         break;
3690     }
3691 
3692     return rc;
3693 }
3694 
3695 /*===========================================================================
3696  * FUNCTION   : isRecording
3697  *
3698  * DESCRIPTION: check if recording is in process.
3699  *
3700  * PARAMETERS : None
3701  *
3702  * RETURN     : true -- recording
3703  *              false -- not in recording mode
3704  *==========================================================================*/
isRecording()3705 bool QCameraStateMachine::isRecording()
3706 {
3707     switch (m_state) {
3708     case QCAMERA_SM_STATE_RECORDING:
3709     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3710         return true;
3711     default:
3712         return false;
3713     }
3714 }
3715 
3716 /*===========================================================================
3717  * FUNCTION   : isPreviewRunning
3718  *
3719  * DESCRIPTION: check if preview is in process.
3720  *
3721  * PARAMETERS : None
3722  *
3723  * RETURN     : true -- preview running
3724  *              false -- preview stopped
3725  *==========================================================================*/
isPreviewRunning()3726 bool QCameraStateMachine::isPreviewRunning()
3727 {
3728     switch (m_state) {
3729     case QCAMERA_SM_STATE_PREVIEWING:
3730     case QCAMERA_SM_STATE_RECORDING:
3731     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3732     case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3733     case QCAMERA_SM_STATE_PREPARE_SNAPSHOT:
3734     case QCAMERA_SM_STATE_PREVIEW_READY:
3735         return true;
3736     default:
3737         return false;
3738     }
3739 }
3740 
3741 /*===========================================================================
3742  * FUNCTION   : isPreviewReady
3743  *
3744  * DESCRIPTION: check if preview is in ready state.
3745  *
3746  * PARAMETERS : None
3747  *
3748  * RETURN     : true -- preview is in ready state
3749  *              false -- preview is stopped
3750  *==========================================================================*/
isPreviewReady()3751 bool QCameraStateMachine::isPreviewReady()
3752 {
3753     switch (m_state) {
3754     case QCAMERA_SM_STATE_PREVIEW_READY:
3755         return true;
3756     default:
3757         return false;
3758     }
3759 }
3760 
3761 /*===========================================================================
3762  * FUNCTION   : isCaptureRunning
3763  *
3764  * DESCRIPTION: check if image capture is in process.
3765  *
3766  * PARAMETERS : None
3767  *
3768  * RETURN     : true -- capture running
3769  *              false -- capture stopped
3770  *==========================================================================*/
isCaptureRunning()3771 bool QCameraStateMachine::isCaptureRunning()
3772 {
3773     switch (m_state) {
3774     case QCAMERA_SM_STATE_PIC_TAKING:
3775     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3776     case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3777         return true;
3778     default:
3779         return false;
3780     }
3781 }
3782 /*===========================================================================
3783  * FUNCTION   : isNonZSLCaptureRunning
3784  *
3785  * DESCRIPTION: check if image capture is in process in non ZSL mode.
3786  *
3787  * PARAMETERS : None
3788  *
3789  * RETURN     : true -- capture running in non ZSL mode
3790  *              false -- Either in not capture mode or captur is not in non ZSL mode
3791  *==========================================================================*/
isNonZSLCaptureRunning()3792 bool QCameraStateMachine::isNonZSLCaptureRunning()
3793 {
3794     switch (m_state) {
3795     case QCAMERA_SM_STATE_PIC_TAKING:
3796         return true;
3797     default:
3798         return false;
3799     }
3800 }
3801 
3802 /*===========================================================================
3803  * FUNCTION   : dump
3804  *
3805  * DESCRIPTION: Composes a string based on current configuration
3806  *
3807  * PARAMETERS : none
3808  *
3809  * RETURN     : Formatted string
3810  *==========================================================================*/
dump()3811 String8 QCameraStateMachine::dump()
3812 {
3813     String8 str("\n");
3814     char s[128];
3815 
3816     snprintf(s, 128, "Is Preview Running: %d\n", isPreviewRunning());
3817     str += s;
3818 
3819     snprintf(s, 128, "Is Capture Running: %d\n", isCaptureRunning());
3820     str += s;
3821 
3822     snprintf(s, 128, "Is Non ZSL Capture Running: %d\n",
3823         isNonZSLCaptureRunning());
3824     str += s;
3825 
3826     snprintf(s, 128, "Current State: %d \n", m_state);
3827     str += s;
3828 
3829     switch(m_state){
3830         case QCAMERA_SM_STATE_PREVIEW_STOPPED:
3831         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_STOPPED \n");
3832         break;
3833 
3834         case QCAMERA_SM_STATE_PREVIEW_READY:
3835         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_READY \n");
3836         break;
3837 
3838         case QCAMERA_SM_STATE_PREVIEWING:
3839         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEWING \n");
3840         break;
3841 
3842         case QCAMERA_SM_STATE_PREPARE_SNAPSHOT:
3843         snprintf(s, 128, " QCAMERA_SM_STATE_PREPARE_SNAPSHOT \n");
3844         break;
3845 
3846         case QCAMERA_SM_STATE_PIC_TAKING:
3847         snprintf(s, 128, " QCAMERA_SM_STATE_PIC_TAKING \n");
3848         break;
3849 
3850         case QCAMERA_SM_STATE_RECORDING:
3851         snprintf(s, 128, " QCAMERA_SM_STATE_RECORDING \n");
3852         break;
3853 
3854         case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3855         snprintf(s, 128, " QCAMERA_SM_STATE_VIDEO_PIC_TAKING \n");
3856         break;
3857 
3858         case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3859         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_PIC_TAKING \n");
3860         break;
3861     }
3862     str += s;
3863 
3864     return str;
3865 }
3866 
3867 }; // namespace qcamera
3868