• 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_RELEASE_RECORIDNG_FRAME:
636         {
637             LOGW("Free video handle %d %d", evt, m_state);
638             QCameraVideoMemory::closeNativeHandle((const void *)payload);
639         }
640     case QCAMERA_SM_EVT_PRE_START_RECORDING:
641     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
642     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
643     case QCAMERA_SM_EVT_START_RECORDING:
644     case QCAMERA_SM_EVT_STOP_RECORDING:
645     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
646     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
647     case QCAMERA_SM_EVT_TAKE_PICTURE:
648         {
649             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
650             rc = INVALID_OPERATION;
651             result.status = rc;
652             result.request_api = evt;
653             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
654             m_parent->signalAPIResult(&result);
655         }
656         break;
657     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
658     case QCAMERA_SM_EVT_CANCEL_PICTURE:
659         {
660             // no op needed here
661             LOGW("No ops for evt(%d) in state(%d)", evt, m_state);
662             result.status = NO_ERROR;
663             result.request_api = evt;
664             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
665             m_parent->signalAPIResult(&result);
666         }
667         break;
668     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
669         {
670             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
671             if (NO_ERROR != rc) {
672                 LOGE("Param init deferred work failed");
673             } else {
674                 rc = m_parent->cancelAutoFocus();
675             }
676             result.status = rc;
677             result.request_api = evt;
678             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
679             m_parent->signalAPIResult(&result);
680         }
681         break;
682     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
683         {
684             int32_t faceID = 0;
685             qcamera_sm_evt_reg_face_payload_t *reg_payload =
686                 (qcamera_sm_evt_reg_face_payload_t *)payload;
687             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
688                                              reg_payload->config,
689                                              faceID);
690             result.status = rc;
691             result.request_api = evt;
692             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
693             result.handle = faceID;
694             m_parent->signalAPIResult(&result);
695         }
696         break;
697     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
698         {
699             rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
700             if (NO_ERROR != rc) {
701                 LOGE("Param init deferred work failed");
702             } else {
703                 rc = m_parent->updateThermalLevel(payload);
704             }
705         }
706         break;
707     case QCAMERA_SM_EVT_EVT_NOTIFY:
708         {
709             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
710             switch (cam_evt->server_event_type) {
711             case CAM_EVENT_TYPE_DAEMON_DIED:
712                 {
713                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
714                                             CAMERA_ERROR_SERVER_DIED,
715                                             0);
716                 }
717                 break;
718             default:
719                 LOGE("Invalid internal event %d in state(%d)",
720                              cam_evt->server_event_type, m_state);
721                 break;
722             }
723         }
724         break;
725     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
726         {
727             // No ops, but need to notify
728             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
729             result.status = rc;
730             result.request_api = evt;
731             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
732             m_parent->signalEvtResult(&result);
733         }
734        break;
735     case QCAMERA_SM_EVT_EVT_INTERNAL:
736        {
737            qcamera_sm_internal_evt_payload_t *internal_evt =
738                (qcamera_sm_internal_evt_payload_t *)payload;
739            switch (internal_evt->evt_type) {
740            case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
741                rc = m_parent->waitDeferredWork(m_parent->mParamInitJob);
742                if (NO_ERROR != rc) {
743                    LOGE("Param init deferred work failed");
744                } else {
745                    rc = m_parent->mParameters.updateFlashMode(internal_evt->led_data);
746                }
747                break;
748            default:
749                LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
750                break;
751            }
752        }
753        break;
754     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
755     default:
756         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
757         break;
758     }
759 
760     return rc;
761 }
762 
763 /*===========================================================================
764  * FUNCTION   : procEvtPreviewReadyState
765  *
766  * DESCRIPTION: finite state machine function to handle event in state of
767  *              QCAMERA_SM_STATE_PREVIEW_READY.
768  *
769  * PARAMETERS :
770  *   @evt      : event to be processed
771  *   @payload  : event payload. Can be NULL if not needed.
772  *
773  * RETURN     : int32_t type of status
774  *              NO_ERROR  -- success
775  *              none-zero failure code
776  *==========================================================================*/
procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt,void * payload)777 int32_t QCameraStateMachine::procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt,
778                                                       void *payload)
779 {
780     int32_t rc = NO_ERROR;
781     qcamera_api_result_t result;
782     memset(&result, 0, sizeof(qcamera_api_result_t));
783 
784     LOGL("event (%d)", evt);
785     switch (evt) {
786     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
787         {
788             m_parent->setPreviewWindow((struct preview_stream_ops *)payload);
789             if (m_parent->mPreviewWindow != NULL) {
790                 applyDelayedMsgs();
791                 rc = m_parent->startPreview();
792                 if (rc != NO_ERROR) {
793                     m_parent->unpreparePreview();
794                     m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
795                 } else {
796                     m_state = QCAMERA_SM_STATE_PREVIEWING;
797                 }
798             }
799 
800             result.status = rc;
801             result.request_api = evt;
802             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
803             m_parent->signalAPIResult(&result);
804         }
805         break;
806     case QCAMERA_SM_EVT_SET_CALLBACKS:
807         {
808             qcamera_sm_evt_setcb_payload_t *setcbs =
809                 (qcamera_sm_evt_setcb_payload_t *)payload;
810             rc = m_parent->setCallBacks(setcbs->notify_cb,
811                                         setcbs->data_cb,
812                                         setcbs->data_cb_timestamp,
813                                         setcbs->get_memory,
814                                         setcbs->user);
815             result.status = rc;
816             result.request_api = evt;
817             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
818             m_parent->signalAPIResult(&result);
819         }
820         break;
821     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
822         {
823             rc = m_parent->enableMsgType(*((int32_t *)payload));
824             result.status = rc;
825             result.request_api = evt;
826             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
827             m_parent->signalAPIResult(&result);
828         }
829         break;
830     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
831         {
832             rc = m_parent->disableMsgType(*((int32_t *)payload));
833             result.status = rc;
834             result.request_api = evt;
835             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
836             m_parent->signalAPIResult(&result);
837         }
838         break;
839     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
840         {
841             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
842             result.status = rc;
843             result.request_api = evt;
844             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
845             result.enabled = enabled;
846             m_parent->signalAPIResult(&result);
847         }
848         break;
849     case QCAMERA_SM_EVT_SET_PARAMS:
850         {
851             bool needRestart = false;
852             rc = m_parent->updateParameters((char*)payload, needRestart);
853             result.status = rc;
854             result.request_api = evt;
855             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
856             m_parent->signalAPIResult(&result);
857         }
858         break;
859     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
860         {
861             LOGD("Stopping preview...");
862             // need restart preview for parameters to take effect
863             m_parent->unpreparePreview();
864             // Clear memory pools
865             m_parent->m_memoryPool.clear();
866             result.status = rc;
867             result.request_api = evt;
868             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
869             m_parent->signalAPIResult(&result);
870         }
871         break;
872     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
873         {
874             rc = m_parent->commitParameterChanges();
875             result.status = rc;
876             result.request_api = evt;
877             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
878             m_parent->signalAPIResult(&result);
879         }
880         break;
881     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
882         {
883             // prepare preview again
884             rc = m_parent->preparePreview();
885             if (rc != NO_ERROR) {
886                 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
887             }
888             m_parent->setNeedRestart(false);
889             result.status = rc;
890             result.request_api = evt;
891             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
892             m_parent->signalAPIResult(&result);
893         }
894         break;
895     case QCAMERA_SM_EVT_GET_PARAMS:
896         {
897             result.params = m_parent->getParameters();
898             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
899             result.status = rc;
900             result.request_api = evt;
901             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
902             m_parent->signalAPIResult(&result);
903         }
904         break;
905     case QCAMERA_SM_EVT_PUT_PARAMS:
906         {
907             rc = m_parent->putParameters((char*)payload);
908             result.status = rc;
909             result.request_api = evt;
910             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
911             m_parent->signalAPIResult(&result);
912         }
913         break;
914     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
915         {
916             // no ops here
917             rc = NO_ERROR;
918             result.status = rc;
919             result.request_api = evt;
920             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
921             m_parent->signalAPIResult(&result);
922         }
923         break;
924     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
925         {
926             rc = m_parent->startPreview();
927             if (rc != NO_ERROR) {
928                 m_parent->unpreparePreview();
929                 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
930             } else {
931                 m_state = QCAMERA_SM_STATE_PREVIEWING;
932             }
933             // no ops here
934             rc = NO_ERROR;
935             result.status = rc;
936             result.request_api = evt;
937             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
938             m_parent->signalAPIResult(&result);
939         }
940         break;
941     case QCAMERA_SM_EVT_START_PREVIEW:
942         {
943             if (m_parent->mPreviewWindow != NULL) {
944                 rc = m_parent->startPreview();
945                 if (rc != NO_ERROR) {
946                     m_parent->unpreparePreview();
947                     m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
948                 } else {
949                     m_state = QCAMERA_SM_STATE_PREVIEWING;
950                 }
951             }
952             // no ops here
953             rc = NO_ERROR;
954             result.status = rc;
955             result.request_api = evt;
956             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
957             m_parent->signalAPIResult(&result);
958         }
959         break;
960     case QCAMERA_SM_EVT_STOP_PREVIEW:
961         {
962             m_parent->unpreparePreview();
963             rc = 0;
964             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
965             result.status = rc;
966             result.request_api = evt;
967             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
968             m_parent->signalAPIResult(&result);
969         }
970         break;
971     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
972         {
973             rc = NO_ERROR;
974             result.status = rc;
975             result.request_api = evt;
976             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
977             result.enabled = 1;
978             m_parent->signalAPIResult(&result);
979         }
980         break;
981     case QCAMERA_SM_EVT_RECORDING_ENABLED:
982         {
983             rc = 0;
984             result.status = rc;
985             result.request_api = evt;
986             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
987             result.enabled = 0;
988             m_parent->signalAPIResult(&result);
989         }
990         break;
991     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
992         {
993             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
994             result.status = rc;
995             result.request_api = evt;
996             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
997             m_parent->signalAPIResult(&result);
998         }
999         break;
1000     case QCAMERA_SM_EVT_DUMP:
1001         {
1002             rc = m_parent->dump(*((int *)payload));
1003             result.status = rc;
1004             result.request_api = evt;
1005             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1006             m_parent->signalAPIResult(&result);
1007         }
1008         break;
1009     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1010         {
1011             rc = m_parent->autoFocus();
1012             result.status = rc;
1013             result.request_api = evt;
1014             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1015             m_parent->signalAPIResult(&result);
1016         }
1017         break;
1018     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1019         {
1020             rc = m_parent->cancelAutoFocus();
1021             result.status = rc;
1022             result.request_api = evt;
1023             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1024             m_parent->signalAPIResult(&result);
1025         }
1026         break;
1027     case QCAMERA_SM_EVT_SEND_COMMAND:
1028         {
1029             qcamera_sm_evt_command_payload_t *cmd_payload =
1030                 (qcamera_sm_evt_command_payload_t *)payload;
1031             rc = m_parent->sendCommand(cmd_payload->cmd,
1032                                        cmd_payload->arg1,
1033                                        cmd_payload->arg2);
1034             result.status = rc;
1035             result.request_api = evt;
1036             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1037             m_parent->signalAPIResult(&result);
1038         }
1039         break;
1040     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
1041         {
1042             int32_t faceID = 0;
1043             qcamera_sm_evt_reg_face_payload_t *reg_payload =
1044                 (qcamera_sm_evt_reg_face_payload_t *)payload;
1045             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
1046                                              reg_payload->config,
1047                                              faceID);
1048             result.status = rc;
1049             result.request_api = evt;
1050             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
1051             result.handle = faceID;
1052             m_parent->signalAPIResult(&result);
1053         }
1054         break;
1055     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1056         {
1057             LOGW("Free video handle %d %d", evt, m_state);
1058             QCameraVideoMemory::closeNativeHandle((const void *)payload);
1059         }
1060     case QCAMERA_SM_EVT_PRE_START_RECORDING:
1061     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
1062     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
1063     case QCAMERA_SM_EVT_START_RECORDING:
1064     case QCAMERA_SM_EVT_STOP_RECORDING:
1065     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
1066     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
1067     case QCAMERA_SM_EVT_TAKE_PICTURE:
1068     case QCAMERA_SM_EVT_CANCEL_PICTURE:
1069     case QCAMERA_SM_EVT_RELEASE:
1070         {
1071             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
1072             rc = INVALID_OPERATION;
1073             result.status = rc;
1074             result.request_api = evt;
1075             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1076             m_parent->signalAPIResult(&result);
1077         }
1078         break;
1079     case QCAMERA_SM_EVT_EVT_NOTIFY:
1080         {
1081             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1082             switch (cam_evt->server_event_type) {
1083             case CAM_EVENT_TYPE_DAEMON_DIED:
1084                 {
1085                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
1086                                             CAMERA_ERROR_SERVER_DIED,
1087                                             0);
1088                 }
1089                 break;
1090             default:
1091                 LOGE("Invalid internal event %d in state(%d)",
1092                              cam_evt->server_event_type, m_state);
1093                 break;
1094             }
1095         }
1096         break;
1097     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1098         {
1099             // No ops, but need to notify
1100             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1101             result.status = rc;
1102             result.request_api = evt;
1103             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1104             m_parent->signalEvtResult(&result);
1105         }
1106        break;
1107     case QCAMERA_SM_EVT_EVT_INTERNAL:
1108        {
1109            qcamera_sm_internal_evt_payload_t *internal_evt =
1110                    (qcamera_sm_internal_evt_payload_t *)payload;
1111            switch (internal_evt->evt_type) {
1112            case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
1113                rc = m_parent->mParameters.updateFlashMode(internal_evt->led_data);
1114                break;
1115            default:
1116                LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1117                break;
1118            }
1119        }
1120        break;
1121     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1122     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1123     default:
1124         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1125         break;
1126     }
1127 
1128     return rc;
1129 }
1130 
1131 /*===========================================================================
1132  * FUNCTION   : procEvtPreviewingState
1133  *
1134  * DESCRIPTION: finite state machine function to handle event in state of
1135  *              QCAMERA_SM_STATE_PREVIEWING.
1136  *
1137  * PARAMETERS :
1138  *   @evt      : event to be processed
1139  *   @payload  : event payload. Can be NULL if not needed.
1140  *
1141  * RETURN     : int32_t type of status
1142  *              NO_ERROR  -- success
1143  *              none-zero failure code
1144  *==========================================================================*/
procEvtPreviewingState(qcamera_sm_evt_enum_t evt,void * payload)1145 int32_t QCameraStateMachine::procEvtPreviewingState(qcamera_sm_evt_enum_t evt,
1146                                                     void *payload)
1147 {
1148     int32_t rc = NO_ERROR;
1149     qcamera_api_result_t result;
1150     memset(&result, 0, sizeof(qcamera_api_result_t));
1151 
1152     LOGL("event (%d)", evt);
1153     switch (evt) {
1154     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1155         {
1156             // Error setting preview window during previewing
1157             LOGE("Error!! cannot set preview window when preview is running");
1158             rc = INVALID_OPERATION;
1159             result.status = rc;
1160             result.request_api = evt;
1161             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1162             m_parent->signalAPIResult(&result);
1163         }
1164         break;
1165     case QCAMERA_SM_EVT_SET_CALLBACKS:
1166         {
1167             qcamera_sm_evt_setcb_payload_t *setcbs =
1168                 (qcamera_sm_evt_setcb_payload_t *)payload;
1169             rc = m_parent->setCallBacks(setcbs->notify_cb,
1170                                         setcbs->data_cb,
1171                                         setcbs->data_cb_timestamp,
1172                                         setcbs->get_memory,
1173                                         setcbs->user);
1174             result.status = rc;
1175             result.request_api = evt;
1176             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1177             m_parent->signalAPIResult(&result);
1178         }
1179         break;
1180     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1181         {
1182             int32_t enable_msgs = *((int32_t *)payload);
1183             if (m_bDelayPreviewMsgs &&
1184                     (enable_msgs & CAMERA_MSG_PREVIEW_FRAME)) {
1185                 enable_msgs &= ~CAMERA_MSG_PREVIEW_FRAME;
1186                 m_DelayedMsgs = CAMERA_MSG_PREVIEW_FRAME;
1187             }
1188             rc = m_parent->enableMsgType(enable_msgs);
1189             result.status = rc;
1190             result.request_api = evt;
1191             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1192             m_parent->signalAPIResult(&result);
1193         }
1194         break;
1195     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1196         {
1197             int32_t disable_msgs = *((int32_t *)payload);
1198             if (m_bDelayPreviewMsgs && m_DelayedMsgs) {
1199                 m_DelayedMsgs &= ~disable_msgs;
1200                 if (0 == m_DelayedMsgs) {
1201                     m_bDelayPreviewMsgs = false;
1202                 }
1203             }
1204             rc = m_parent->disableMsgType(disable_msgs);
1205             result.status = rc;
1206             result.request_api = evt;
1207             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1208             m_parent->signalAPIResult(&result);
1209         }
1210         break;
1211     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1212         {
1213             int32_t msgs = *((int32_t *)payload);
1214             int enabled = m_parent->msgTypeEnabled(msgs);
1215             if (m_bDelayPreviewMsgs && m_DelayedMsgs) {
1216                 enabled |= (msgs & m_DelayedMsgs);
1217             }
1218             result.status = rc;
1219             result.request_api = evt;
1220             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1221             result.enabled = enabled;
1222             m_parent->signalAPIResult(&result);
1223         }
1224         break;
1225     case QCAMERA_SM_EVT_SET_PARAMS:
1226         {
1227             bool needRestart = false;
1228             rc = m_parent->updateParameters((char*)payload, needRestart);
1229             result.status = rc;
1230             result.request_api = evt;
1231             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1232             m_parent->signalAPIResult(&result);
1233         }
1234         break;
1235     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
1236         {
1237             LOGD("Stopping preview...");
1238             // stop preview
1239             rc = m_parent->stopPreview();
1240             // Clear memory pools
1241             m_parent->m_memoryPool.clear();
1242             result.status = rc;
1243             result.request_api = evt;
1244             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1245             m_parent->signalAPIResult(&result);
1246         }
1247         break;
1248     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
1249         {
1250             rc = m_parent->commitParameterChanges();
1251             result.status = rc;
1252             result.request_api = evt;
1253             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1254             m_parent->signalAPIResult(&result);
1255         }
1256         break;
1257     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
1258         {
1259             // start preview again
1260             rc = m_parent->preparePreview();
1261             if (rc == NO_ERROR) {
1262                 applyDelayedMsgs();
1263                 rc = m_parent->startPreview();
1264                 if (rc != NO_ERROR) {
1265                     m_parent->unpreparePreview();
1266                 }
1267                 if (rc != NO_ERROR) {
1268                     m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1269                 }
1270             }
1271             m_parent->setNeedRestart(false);
1272             result.status            = rc;
1273             result.request_api       = evt;
1274             result.result_type       = QCAMERA_API_RESULT_TYPE_DEF;
1275             m_parent->signalAPIResult(&result);
1276         }
1277         break;
1278     case QCAMERA_SM_EVT_GET_PARAMS:
1279         {
1280             result.params = m_parent->getParameters();
1281             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
1282             result.status = rc;
1283             result.request_api = evt;
1284             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1285             m_parent->signalAPIResult(&result);
1286         }
1287         break;
1288     case QCAMERA_SM_EVT_PUT_PARAMS:
1289         {
1290             rc = m_parent->putParameters((char*)payload);
1291             result.status = rc;
1292             result.request_api = evt;
1293             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1294             m_parent->signalAPIResult(&result);
1295         }
1296         break;
1297     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
1298         {
1299             // no ops here
1300             LOGW("Already in preview ready state, no ops here");
1301             rc = NO_ERROR;
1302             result.status = rc;
1303             result.request_api = evt;
1304             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1305             m_parent->signalAPIResult(&result);
1306         }
1307         break;
1308     case QCAMERA_SM_EVT_START_PREVIEW:
1309     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1310         {
1311             // no ops here
1312             LOGW("Already in previewing, no ops here to start preview");
1313             applyDelayedMsgs();
1314             rc = NO_ERROR;
1315             result.status = rc;
1316             result.request_api = evt;
1317             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1318             m_parent->signalAPIResult(&result);
1319         }
1320         break;
1321     case QCAMERA_SM_EVT_STOP_PREVIEW:
1322         {
1323             rc = m_parent->stopPreview();
1324             applyDelayedMsgs();
1325             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1326             result.status = rc;
1327             result.request_api = evt;
1328             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1329             m_parent->signalAPIResult(&result);
1330         }
1331         break;
1332     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1333         {
1334             applyDelayedMsgs();
1335             rc = NO_ERROR;
1336             result.status = rc;
1337             result.request_api = evt;
1338             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1339             result.enabled = 1;
1340             m_parent->signalAPIResult(&result);
1341         }
1342         break;
1343     case QCAMERA_SM_EVT_RECORDING_ENABLED:
1344         {
1345             rc = NO_ERROR;
1346             result.status = rc;
1347             result.request_api = evt;
1348             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1349             result.enabled = 0;
1350             m_parent->signalAPIResult(&result);
1351         }
1352         break;
1353     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1354         {
1355             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
1356             result.status = rc;
1357             result.request_api = evt;
1358             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1359             m_parent->signalAPIResult(&result);
1360         }
1361         break;
1362     case QCAMERA_SM_EVT_DUMP:
1363         {
1364             rc = m_parent->dump(*((int *)payload));
1365             result.status = rc;
1366             result.request_api = evt;
1367             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1368             m_parent->signalAPIResult(&result);
1369         }
1370         break;
1371     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1372         {
1373             rc = m_parent->autoFocus();
1374             result.status = rc;
1375             result.request_api = evt;
1376             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1377             m_parent->signalAPIResult(&result);
1378         }
1379         break;
1380     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1381         {
1382             rc = m_parent->cancelAutoFocus();
1383             result.status = rc;
1384             result.request_api = evt;
1385             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1386             m_parent->signalAPIResult(&result);
1387         }
1388         break;
1389     case QCAMERA_SM_EVT_PRE_START_RECORDING:
1390         {
1391             rc = m_parent->preStartRecording();
1392             result.status = rc;
1393             result.request_api = evt;
1394             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1395             m_parent->signalAPIResult(&result);
1396         }
1397         break;
1398     case QCAMERA_SM_EVT_START_RECORDING:
1399         {
1400             rc = m_parent->startRecording();
1401             if (rc == NO_ERROR) {
1402                 // move state to recording state
1403                 m_state = QCAMERA_SM_STATE_RECORDING;
1404                 applyDelayedMsgs();
1405             }
1406             result.status = rc;
1407             result.request_api = evt;
1408             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1409             m_parent->signalAPIResult(&result);
1410         }
1411         break;
1412     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
1413         {
1414             rc = m_parent->prepareHardwareForSnapshot(FALSE);
1415             if (rc == NO_ERROR) {
1416                 // Do not signal API result in this case.
1417                 // Need to wait for snapshot done in metadta.
1418                 m_state = QCAMERA_SM_STATE_PREPARE_SNAPSHOT;
1419                 applyDelayedMsgs();
1420             } else {
1421                 // Do not change state in this case.
1422                 LOGE("prepareHardwareForSnapshot failed %d",
1423                      rc);
1424 
1425                 result.status = rc;
1426                 result.request_api = evt;
1427                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1428                 m_parent->signalAPIResult(&result);
1429             }
1430         }
1431         break;
1432     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
1433         {
1434             rc = m_parent->preTakePicture();
1435             result.status = rc;
1436             result.request_api = evt;
1437             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1438             m_parent->signalAPIResult(&result);
1439         }
1440         break;
1441     case QCAMERA_SM_EVT_TAKE_PICTURE:
1442        {
1443            LOGL("QCAMERA_SM_EVT_TAKE_PICTURE ");
1444            if ( m_parent->mParameters.getRecordingHintValue() == true) {
1445                 m_parent->stopPreview();
1446                 m_parent->mParameters.updateRecordingHintValue(FALSE);
1447                 // start preview again
1448                 rc = m_parent->preparePreview();
1449                 if (rc == NO_ERROR) {
1450                     rc = m_parent->startPreview();
1451                     if (rc != NO_ERROR) {
1452                         m_parent->unpreparePreview();
1453                     }
1454                 }
1455            }
1456            if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
1457                bool restartPreview = m_parent->isPreviewRestartEnabled();
1458                if ((restartPreview) && (m_parent->mParameters.getManualCaptureMode()
1459                        >= CAM_MANUAL_CAPTURE_TYPE_3)) {
1460                    /* stop preview and disable ZSL now */
1461                    m_parent->stopPreview();
1462                    m_parent->mParameters.updateZSLModeValue(FALSE);
1463                    m_RestoreZSL = TRUE;
1464                    m_bDelayPreviewMsgs = true;
1465                    m_state = QCAMERA_SM_STATE_PIC_TAKING;
1466                } else {
1467                    m_state = QCAMERA_SM_STATE_PREVIEW_PIC_TAKING;
1468                    m_bDelayPreviewMsgs = true;
1469                }
1470 
1471                rc = m_parent->takePicture();
1472                if (rc != NO_ERROR) {
1473                    // move state to previewing state
1474                    m_parent->unconfigureAdvancedCapture();
1475                    m_state = QCAMERA_SM_STATE_PREVIEWING;
1476                }
1477                if (!(m_parent->isRetroPicture()) || (rc != NO_ERROR)) {
1478                    LOGD("signal API result, m_state = %d",
1479                           m_state);
1480                    result.status = rc;
1481                    result.request_api = evt;
1482                    result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1483                    m_parent->signalAPIResult(&result);
1484                }
1485            } else {
1486                m_state = QCAMERA_SM_STATE_PIC_TAKING;
1487                rc = m_parent->takePicture();
1488                if (rc != NO_ERROR) {
1489                    int32_t temp_rc = NO_ERROR;
1490                    // move state to preview stopped state
1491                    m_parent->unconfigureAdvancedCapture();
1492                    m_parent->stopPreview();
1493                    // start preview again
1494                    temp_rc = m_parent->preparePreview();
1495                    if (temp_rc == NO_ERROR) {
1496                        temp_rc = m_parent->startPreview();
1497                        if (temp_rc != NO_ERROR) {
1498                            m_parent->unpreparePreview();
1499                            m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1500                        } else {
1501                            m_state = QCAMERA_SM_STATE_PREVIEWING;
1502                        }
1503                    } else {
1504                        m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1505                    }
1506                }
1507                result.status = rc;
1508                result.request_api = evt;
1509                result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1510                m_parent->signalAPIResult(&result);
1511            }
1512         }
1513         break;
1514     case QCAMERA_SM_EVT_SEND_COMMAND:
1515         {
1516             qcamera_sm_evt_command_payload_t *cmd_payload =
1517                     (qcamera_sm_evt_command_payload_t *)payload;
1518             rc = m_parent->sendCommand(cmd_payload->cmd,
1519                     cmd_payload->arg1,
1520                     cmd_payload->arg2);
1521             m_bPreviewNeedsRestart =
1522                     (QCAMERA_SM_EVT_RESTART_PERVIEW == cmd_payload->arg1);
1523             m_bPreviewDelayedRestart =
1524                     (QCAMERA_SM_EVT_DELAYED_RESTART == cmd_payload->arg2);
1525 
1526 #ifndef VANILLA_HAL
1527             if ((CAMERA_CMD_LONGSHOT_ON == cmd_payload->cmd) &&
1528                     (m_bPreviewNeedsRestart)) {
1529                 m_parent->stopPreview();
1530                 // Clear memory pools
1531                 m_parent->m_memoryPool.clear();
1532 
1533                 if (!m_bPreviewDelayedRestart) {
1534                     // start preview again
1535                     rc = m_parent->preparePreview();
1536                     if (rc == NO_ERROR) {
1537                         applyDelayedMsgs();
1538                         rc = m_parent->startPreview();
1539                         if (rc != NO_ERROR) {
1540                             m_parent->unpreparePreview();
1541                         }
1542                     }
1543                 }
1544             }
1545 #endif
1546             result.status = rc;
1547             result.request_api = evt;
1548             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1549             m_parent->signalAPIResult(&result);
1550         }
1551         break;
1552     case QCAMERA_SM_EVT_SEND_COMMAND_RESTART:
1553         {
1554 #ifndef VANILLA_HAL
1555             qcamera_sm_evt_command_payload_t *cmd_payload =
1556                     (qcamera_sm_evt_command_payload_t *)payload;
1557             if ((CAMERA_CMD_LONGSHOT_ON == cmd_payload->cmd) &&
1558                     (m_bPreviewNeedsRestart) &&
1559                     (m_bPreviewDelayedRestart)) {
1560                 // start preview again
1561                 rc = m_parent->preparePreview();
1562                 if (rc == NO_ERROR) {
1563                     rc = m_parent->startPreview();
1564                     if (rc != NO_ERROR) {
1565                         m_parent->unpreparePreview();
1566                     }
1567                 }
1568             }
1569 #endif
1570             result.status = rc;
1571             result.request_api = evt;
1572             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1573             m_parent->signalAPIResult(&result);
1574         }
1575         break;
1576     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
1577         {
1578             int32_t faceID = 0;
1579             qcamera_sm_evt_reg_face_payload_t *reg_payload =
1580                 (qcamera_sm_evt_reg_face_payload_t *)payload;
1581             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
1582                                              reg_payload->config,
1583                                              faceID);
1584             result.status = rc;
1585             result.request_api = evt;
1586             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
1587             result.handle = faceID;
1588             m_parent->signalAPIResult(&result);
1589         }
1590         break;
1591     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1592         {
1593             LOGW("Free video handle %d %d", evt, m_state);
1594             QCameraVideoMemory::closeNativeHandle((const void *)payload);
1595         }
1596     case QCAMERA_SM_EVT_CANCEL_PICTURE:
1597     case QCAMERA_SM_EVT_STOP_RECORDING:
1598     case QCAMERA_SM_EVT_RELEASE:
1599         {
1600             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
1601             rc = INVALID_OPERATION;
1602             result.status = rc;
1603             result.request_api = evt;
1604             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1605             m_parent->signalAPIResult(&result);
1606         }
1607         break;
1608     case QCAMERA_SM_EVT_EVT_INTERNAL:
1609         {
1610             qcamera_sm_internal_evt_payload_t *internal_evt =
1611                 (qcamera_sm_internal_evt_payload_t *)payload;
1612             switch (internal_evt->evt_type) {
1613             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1614                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1615                 break;
1616             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
1617                 break;
1618             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
1619                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
1620                 break;
1621             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
1622                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
1623                 break;
1624             case QCAMERA_INTERNAL_EVT_CROP_INFO:
1625                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
1626                 break;
1627             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
1628                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
1629                 break;
1630             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
1631                 rc = m_parent->mParameters.updateFlashMode(internal_evt->led_data);
1632                 break;
1633             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
1634                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
1635                 break;
1636             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
1637                 rc = m_parent->processAEInfo(internal_evt->ae_data);
1638                 break;
1639             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
1640                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
1641                 break;
1642             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
1643                 rc = m_parent->processHDRData(internal_evt->hdr_data);
1644                 break;
1645             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
1646                 rc = m_parent->processRetroAECUnlock();
1647                 break;
1648             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
1649                 rc = m_parent->processZSLCaptureDone();
1650                 break;
1651             case QCAMERA_INTERNAL_EVT_DUAL_CAMERA_FOV_CONTROL:
1652                 m_parent->processDualCamFovControl();
1653                 break;
1654             case QCAMERA_INTERNAL_EVT_LED_CALIB_UPDATE:
1655                 rc = m_parent->processLEDCalibration(internal_evt->led_calib_result);
1656                 break;
1657             default:
1658                 LOGE("Invalid internal event %d in state(%d)",
1659                              internal_evt->evt_type, m_state);
1660                 break;
1661             }
1662         }
1663         break;
1664     case QCAMERA_SM_EVT_EVT_NOTIFY:
1665         {
1666             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1667             switch (cam_evt->server_event_type) {
1668             case CAM_EVENT_TYPE_DAEMON_DIED:
1669                 {
1670                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
1671                                             CAMERA_ERROR_SERVER_DIED,
1672                                             0);
1673                 }
1674                 break;
1675             default:
1676                 LOGW("no handling for server evt (%d) at this state",
1677                        cam_evt->server_event_type);
1678                 break;
1679             }
1680         }
1681         break;
1682     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1683         {
1684             rc = m_parent->updateThermalLevel(payload);
1685         }
1686         break;
1687     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1688         {
1689             // No ops, but need to notify
1690             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1691             result.status = rc;
1692             result.request_api = evt;
1693             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1694             m_parent->signalEvtResult(&result);
1695         }
1696        break;
1697     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
1698         {
1699             m_parent->stopPreview();
1700             result.status = rc;
1701             result.request_api = evt;
1702             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1703             m_parent->signalAPIResult(&result);
1704         }
1705        break;
1706     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
1707         {
1708             rc = m_parent->preparePreview();
1709             if (rc == NO_ERROR) {
1710                 rc = m_parent->startPreview();
1711             }
1712             result.status = rc;
1713             result.request_api = evt;
1714             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1715             m_parent->signalAPIResult(&result);
1716         }
1717        break;
1718     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1719     default:
1720         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1721         break;
1722     }
1723 
1724     return rc;
1725 }
1726 
1727 /*===========================================================================
1728  * FUNCTION   : procEvtPrepareSnapshotState
1729  *
1730  * DESCRIPTION: finite state machine function to handle event in state of
1731  *              QCAMERA_SM_STATE_PREPARE_SNAPSHOT.
1732  *
1733  * PARAMETERS :
1734  *   @evt      : event to be processed
1735  *   @payload  : event payload. Can be NULL if not needed.
1736  *
1737  * RETURN     : int32_t type of status
1738  *              NO_ERROR  -- success
1739  *              none-zero failure code
1740  *==========================================================================*/
procEvtPrepareSnapshotState(qcamera_sm_evt_enum_t evt,void * payload)1741 int32_t QCameraStateMachine::procEvtPrepareSnapshotState(qcamera_sm_evt_enum_t evt,
1742                                                     void *payload)
1743 {
1744     int32_t rc = NO_ERROR;
1745     qcamera_api_result_t result;
1746     memset(&result, 0, sizeof(qcamera_api_result_t));
1747 
1748     LOGL("event (%d)", evt);
1749     switch (evt) {
1750     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1751     case QCAMERA_SM_EVT_SET_CALLBACKS:
1752     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1753     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1754     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1755     case QCAMERA_SM_EVT_SET_PARAMS:
1756     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
1757     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
1758     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
1759     case QCAMERA_SM_EVT_GET_PARAMS:
1760     case QCAMERA_SM_EVT_PUT_PARAMS:
1761     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
1762     case QCAMERA_SM_EVT_START_PREVIEW:
1763     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1764     case QCAMERA_SM_EVT_STOP_PREVIEW:
1765     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1766     case QCAMERA_SM_EVT_RECORDING_ENABLED:
1767     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1768     case QCAMERA_SM_EVT_DUMP:
1769     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1770     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1771     case QCAMERA_SM_EVT_PRE_START_RECORDING:
1772     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
1773     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
1774     case QCAMERA_SM_EVT_START_RECORDING:
1775     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
1776     case QCAMERA_SM_EVT_TAKE_PICTURE:
1777     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
1778     case QCAMERA_SM_EVT_SEND_COMMAND:
1779     case QCAMERA_SM_EVT_CANCEL_PICTURE:
1780     case QCAMERA_SM_EVT_STOP_RECORDING:
1781     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1782     case QCAMERA_SM_EVT_RELEASE:
1783         {
1784             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
1785             rc = INVALID_OPERATION;
1786             result.status = rc;
1787             result.request_api = evt;
1788             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1789             m_parent->signalAPIResult(&result);
1790         }
1791         break;
1792     case QCAMERA_SM_EVT_EVT_INTERNAL:
1793         {
1794             qcamera_sm_internal_evt_payload_t *internal_evt =
1795                 (qcamera_sm_internal_evt_payload_t *)payload;
1796             switch (internal_evt->evt_type) {
1797             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1798                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1799                 break;
1800             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
1801                 m_parent->processPrepSnapshotDoneEvent(internal_evt->prep_snapshot_state);
1802                 m_state = QCAMERA_SM_STATE_PREVIEWING;
1803 
1804                 result.status = NO_ERROR;
1805                 result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
1806                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1807                 m_parent->signalAPIResult(&result);
1808                 break;
1809             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
1810                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
1811                 break;
1812             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
1813                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
1814                 break;
1815             case QCAMERA_INTERNAL_EVT_CROP_INFO:
1816                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
1817                 break;
1818             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
1819                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
1820                 break;
1821             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
1822                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1823                 break;
1824             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
1825                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
1826                 break;
1827             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
1828                 rc = m_parent->processAEInfo(internal_evt->ae_data);
1829                 break;
1830             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
1831                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
1832                 break;
1833             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
1834                 rc = m_parent->processHDRData(internal_evt->hdr_data);
1835                 break;
1836             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
1837                 rc = m_parent->processRetroAECUnlock();
1838                 break;
1839             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
1840                 rc = m_parent->processZSLCaptureDone();
1841                 break;
1842             case QCAMERA_INTERNAL_EVT_DUAL_CAMERA_FOV_CONTROL:
1843                 m_parent->processDualCamFovControl();
1844                 break;
1845             case QCAMERA_INTERNAL_EVT_LED_CALIB_UPDATE:
1846                 rc = m_parent->processLEDCalibration(internal_evt->led_calib_result);
1847                 break;
1848             default:
1849                 LOGE("Invalid internal event %d in state(%d)",
1850                              internal_evt->evt_type, m_state);
1851                 break;
1852             }
1853         }
1854         break;
1855     case QCAMERA_SM_EVT_EVT_NOTIFY:
1856         {
1857             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1858             switch (cam_evt->server_event_type) {
1859             case CAM_EVENT_TYPE_DAEMON_DIED:
1860                 {
1861                     // Send internal events to stop indefinite wait on prepare
1862                     // snapshot done event.
1863                     m_state = QCAMERA_SM_STATE_PREVIEWING;
1864 
1865                     result.status = rc;
1866                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
1867                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1868                     m_parent->signalAPIResult(&result);
1869 
1870                     result.status = rc;
1871                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
1872                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1873                     m_parent->signalAPIResult(&result);
1874 
1875                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
1876                                             CAMERA_ERROR_SERVER_DIED,
1877                                             0);
1878                 }
1879                 break;
1880             default:
1881                 LOGE("Invalid internal event %d in state(%d)",
1882                              cam_evt->server_event_type, m_state);
1883                 break;
1884             }
1885         }
1886         break;
1887     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1888         {
1889             // No ops, but need to notify
1890             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1891             result.status = rc;
1892             result.request_api = evt;
1893             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1894             m_parent->signalEvtResult(&result);
1895         }
1896        break;
1897     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1898         {
1899             rc = m_parent->updateThermalLevel(payload);
1900         }
1901         break;
1902     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1903     default:
1904         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
1905         break;
1906     }
1907 
1908     return rc;
1909 }
1910 
1911 /*===========================================================================
1912  * FUNCTION   : procEvtPicTakingState
1913  *
1914  * DESCRIPTION: finite state machine function to handle event in state of
1915  *              QCAMERA_SM_STATE_PIC_TAKING.
1916  *
1917  * PARAMETERS :
1918  *   @evt      : event to be processed
1919  *   @payload  : event payload. Can be NULL if not needed.
1920  *
1921  * RETURN     : int32_t type of status
1922  *              NO_ERROR  -- success
1923  *              none-zero failure code
1924  *==========================================================================*/
procEvtPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)1925 int32_t QCameraStateMachine::procEvtPicTakingState(qcamera_sm_evt_enum_t evt,
1926                                                    void *payload)
1927 {
1928     int32_t rc = NO_ERROR;
1929     qcamera_api_result_t result;
1930     memset(&result, 0, sizeof(qcamera_api_result_t));
1931 
1932     LOGL("event (%d)", evt);
1933     switch (evt) {
1934     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1935         {
1936             // Error setting preview window during previewing
1937             LOGE("Error!! cannot set preview window when preview is running");
1938             rc = INVALID_OPERATION;
1939             result.status = rc;
1940             result.request_api = evt;
1941             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1942             m_parent->signalAPIResult(&result);
1943         }
1944         break;
1945     case QCAMERA_SM_EVT_SET_CALLBACKS:
1946         {
1947             qcamera_sm_evt_setcb_payload_t *setcbs =
1948                 (qcamera_sm_evt_setcb_payload_t *)payload;
1949             rc = m_parent->setCallBacks(setcbs->notify_cb,
1950                                         setcbs->data_cb,
1951                                         setcbs->data_cb_timestamp,
1952                                         setcbs->get_memory,
1953                                         setcbs->user);
1954             result.status = rc;
1955             result.request_api = evt;
1956             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1957             m_parent->signalAPIResult(&result);
1958         }
1959         break;
1960     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1961         {
1962             rc = m_parent->enableMsgType(*((int32_t *)payload));
1963             result.status = rc;
1964             result.request_api = evt;
1965             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1966             m_parent->signalAPIResult(&result);
1967         }
1968         break;
1969     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1970         {
1971             rc = m_parent->disableMsgType(*((int32_t *)payload));
1972             result.status = rc;
1973             result.request_api = evt;
1974             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1975             m_parent->signalAPIResult(&result);
1976         }
1977         break;
1978     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1979         {
1980             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
1981             result.status = rc;
1982             result.request_api = evt;
1983             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1984             result.enabled = enabled;
1985             m_parent->signalAPIResult(&result);
1986         }
1987         break;
1988     case QCAMERA_SM_EVT_SET_PARAMS:
1989         {
1990             bool needRestart = false;
1991             rc = m_parent->updateParameters((char*)payload, needRestart);
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_SET_PARAMS_STOP:
1999         {
2000             result.status = rc;
2001             result.request_api = evt;
2002             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2003             m_parent->signalAPIResult(&result);
2004         }
2005         break;
2006     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
2007         {
2008             rc = m_parent->commitParameterChanges();
2009             result.status = rc;
2010             result.request_api = evt;
2011             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2012             m_parent->signalAPIResult(&result);
2013         }
2014         break;
2015     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
2016         {
2017             m_parent->setNeedRestart(false);
2018             result.status           =  rc;
2019             result.request_api      =  evt;
2020             result.result_type      =  QCAMERA_API_RESULT_TYPE_DEF;
2021             m_parent->signalAPIResult(&result);
2022         }
2023         break;
2024     case QCAMERA_SM_EVT_GET_PARAMS:
2025         {
2026             result.params = m_parent->getParameters();
2027             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
2028             result.status = rc;
2029             result.request_api = evt;
2030             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
2031             m_parent->signalAPIResult(&result);
2032         }
2033         break;
2034     case QCAMERA_SM_EVT_PUT_PARAMS:
2035         {
2036             rc = m_parent->putParameters((char*)payload);
2037             result.status = rc;
2038             result.request_api = evt;
2039             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2040             m_parent->signalAPIResult(&result);
2041         }
2042         break;
2043     case QCAMERA_SM_EVT_STOP_PREVIEW:
2044         {
2045             // cancel picture first
2046             rc = m_parent->cancelPicture();
2047             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2048 
2049             result.status = rc;
2050             result.request_api = evt;
2051             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2052             m_parent->signalAPIResult(&result);
2053         }
2054         break;
2055     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2056         {
2057             rc = NO_ERROR;
2058             result.status = rc;
2059             result.request_api = evt;
2060             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2061             result.enabled = 0;
2062             m_parent->signalAPIResult(&result);
2063         }
2064         break;
2065     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2066         {
2067             rc = NO_ERROR;
2068             result.status = rc;
2069             result.request_api = evt;
2070             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2071             result.enabled = 0;
2072             m_parent->signalAPIResult(&result);
2073         }
2074         break;
2075     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2076         {
2077             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
2078             result.status = rc;
2079             result.request_api = evt;
2080             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2081             m_parent->signalAPIResult(&result);
2082         }
2083         break;
2084     case QCAMERA_SM_EVT_DUMP:
2085         {
2086             rc = m_parent->dump(*((int *)payload));
2087             result.status = rc;
2088             result.request_api = evt;
2089             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2090             m_parent->signalAPIResult(&result);
2091         }
2092         break;
2093     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2094         {
2095             rc = m_parent->autoFocus();
2096             result.status = rc;
2097             result.request_api = evt;
2098             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2099             m_parent->signalAPIResult(&result);
2100         }
2101         break;
2102     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2103         {
2104             rc = m_parent->cancelAutoFocus();
2105             result.status = rc;
2106             result.request_api = evt;
2107             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2108             m_parent->signalAPIResult(&result);
2109         }
2110         break;
2111     case QCAMERA_SM_EVT_SEND_COMMAND:
2112         {
2113             qcamera_sm_evt_command_payload_t *cmd_payload =
2114                 (qcamera_sm_evt_command_payload_t *)payload;
2115             rc = m_parent->sendCommand(cmd_payload->cmd,
2116                                        cmd_payload->arg1,
2117                                        cmd_payload->arg2);
2118 #ifndef VANILLA_HAL
2119             if ( CAMERA_CMD_LONGSHOT_OFF == cmd_payload->cmd ) {
2120                 // move state to previewing state
2121                 m_state = QCAMERA_SM_STATE_PREVIEWING;
2122             }
2123 #endif
2124             result.status = rc;
2125             result.request_api = evt;
2126             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2127             m_parent->signalAPIResult(&result);
2128         }
2129         break;
2130     case QCAMERA_SM_EVT_CANCEL_PICTURE:
2131         {
2132             rc = m_parent->cancelPicture();
2133             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2134             result.status = rc;
2135             result.request_api = evt;
2136             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2137             m_parent->signalAPIResult(&result);
2138         }
2139         break;
2140     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2141         {
2142             int32_t faceID = 0;
2143             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2144                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2145             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2146                                              reg_payload->config,
2147                                              faceID);
2148             result.status = rc;
2149             result.request_api = evt;
2150             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2151             result.handle = faceID;
2152             m_parent->signalAPIResult(&result);
2153         }
2154         break;
2155     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
2156         {
2157            if ( m_parent->isLongshotEnabled() ) {
2158                // no ops here, need to singal NO_ERROR
2159                rc = NO_ERROR;
2160             } else {
2161                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2162                 rc = INVALID_OPERATION;
2163             }
2164 
2165             result.status = rc;
2166             result.request_api = evt;
2167             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2168             m_parent->signalAPIResult(&result);
2169         }
2170         break;
2171     case QCAMERA_SM_EVT_TAKE_PICTURE:
2172         {
2173            if ( m_parent->isLongshotEnabled() ) {
2174                rc = m_parent->longShot();
2175             } else {
2176                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2177                 rc = INVALID_OPERATION;
2178             }
2179 
2180             result.status = rc;
2181             result.request_api = evt;
2182             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2183             m_parent->signalAPIResult(&result);
2184         }
2185         break;
2186     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
2187     case QCAMERA_SM_EVT_PRE_START_RECORDING:
2188     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
2189     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
2190     case QCAMERA_SM_EVT_START_RECORDING:
2191     case QCAMERA_SM_EVT_STOP_RECORDING:
2192     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2193     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
2194     case QCAMERA_SM_EVT_START_PREVIEW:
2195     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2196     case QCAMERA_SM_EVT_RELEASE:
2197         {
2198             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2199             rc = INVALID_OPERATION;
2200             result.status = rc;
2201             result.request_api = evt;
2202             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2203             m_parent->signalAPIResult(&result);
2204         }
2205         break;
2206     case QCAMERA_SM_EVT_EVT_INTERNAL:
2207         {
2208             qcamera_sm_internal_evt_payload_t *internal_evt =
2209                 (qcamera_sm_internal_evt_payload_t *)payload;
2210             switch (internal_evt->evt_type) {
2211             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2212                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2213                 break;
2214             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
2215                 break;
2216             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
2217                 break;
2218             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
2219                 break;
2220             case QCAMERA_INTERNAL_EVT_CROP_INFO:
2221                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
2222                 break;
2223             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
2224                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
2225                 break;
2226             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
2227                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2228                 break;
2229             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
2230                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
2231                 break;
2232             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
2233                 rc = m_parent->processAEInfo(internal_evt->ae_data);
2234                 break;
2235             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
2236                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
2237                 break;
2238             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
2239                 rc = m_parent->processHDRData(internal_evt->hdr_data);
2240                 break;
2241             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
2242                 rc = m_parent->processRetroAECUnlock();
2243                 break;
2244             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
2245                 rc = m_parent->processZSLCaptureDone();
2246                 break;
2247             case QCAMERA_INTERNAL_EVT_DUAL_CAMERA_FOV_CONTROL:
2248                 m_parent->processDualCamFovControl();
2249                 break;
2250             case QCAMERA_INTERNAL_EVT_LED_CALIB_UPDATE:
2251                 rc = m_parent->processLEDCalibration(internal_evt->led_calib_result);
2252                 break;
2253             default:
2254                 break;
2255             }
2256         }
2257         break;
2258     case QCAMERA_SM_EVT_EVT_NOTIFY:
2259         {
2260             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2261             switch (cam_evt->server_event_type) {
2262             case CAM_EVENT_TYPE_DAEMON_DIED:
2263                 {
2264                     // Send internal events to stop indefinite wait on prepare
2265                     // snapshot done event.
2266                     result.status = rc;
2267                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
2268                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2269                     m_parent->signalAPIResult(&result);
2270 
2271                     result.status = rc;
2272                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
2273                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2274                     m_parent->signalAPIResult(&result);
2275 
2276                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
2277                                             CAMERA_ERROR_SERVER_DIED,
2278                                             0);
2279                 }
2280                 break;
2281             case CAM_EVENT_TYPE_CAC_DONE:
2282                 if (m_parent->isCACEnabled() || m_parent->mParameters.isOEMFeatEnabled()) {
2283                     LOGD("[LONG_SHOT_DBG] : Received CAC Done");
2284                     if (m_parent->isLongshotEnabled()
2285                             && !m_parent->isCaptureShutterEnabled()) {
2286                         // play shutter sound for longshot
2287                         // after CAC stage is done
2288                         m_parent->playShutter();
2289                     }
2290                     m_parent->mCACDoneReceived = TRUE;
2291                 }
2292                 break;
2293             default:
2294                 LOGH("no handling for server evt (%d) at this state",
2295                        cam_evt->server_event_type);
2296                 break;
2297             }
2298         }
2299         break;
2300     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2301         {
2302             qcamera_jpeg_evt_payload_t *jpeg_job =
2303                 (qcamera_jpeg_evt_payload_t *)payload;
2304             rc = m_parent->processJpegNotify(jpeg_job);
2305         }
2306         break;
2307     case QCAMERA_SM_EVT_STOP_CAPTURE_CHANNEL:
2308         {
2309             bool restartPreview = m_parent->isPreviewRestartEnabled();
2310             rc = m_parent->stopCaptureChannel(restartPreview);
2311 
2312             if (restartPreview && (NO_ERROR == rc)) {
2313                 m_parent->unconfigureAdvancedCapture();
2314                 rc = m_parent->preparePreview();
2315                 if (NO_ERROR == rc) {
2316                     m_parent->m_bPreviewStarted = true;
2317                     applyDelayedMsgs();
2318                     rc = m_parent->startPreview();
2319                 }
2320             }
2321 
2322             result.status = rc;
2323             result.request_api = evt;
2324             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2325             m_parent->signalAPIResult(&result);
2326         }
2327         break;
2328     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2329         {
2330             rc = m_parent->cancelPicture();
2331 
2332             bool restartPreview = m_parent->isPreviewRestartEnabled();
2333             if (restartPreview) {
2334                 if (m_parent->mParameters.getManualCaptureMode()
2335                         >= CAM_MANUAL_CAPTURE_TYPE_3) {
2336                     m_parent->mParameters.updateZSLModeValue(m_RestoreZSL);
2337                     m_RestoreZSL = FALSE;
2338                     rc = m_parent->preparePreview();
2339                     if (NO_ERROR == rc) {
2340                         m_parent->m_bPreviewStarted = true;
2341                         applyDelayedMsgs();
2342                         rc = m_parent->startPreview();
2343                     }
2344                 }
2345                 m_state = QCAMERA_SM_STATE_PREVIEWING;
2346             } else {
2347                 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2348             }
2349 
2350             result.status = rc;
2351             result.request_api = evt;
2352             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2353             m_parent->signalEvtResult(&result);
2354         }
2355         break;
2356     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2357         {
2358             rc = m_parent->updateThermalLevel(payload);
2359         }
2360         break;
2361     default:
2362         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2363         break;
2364     }
2365 
2366     return rc;
2367 }
2368 
2369 /*===========================================================================
2370  * FUNCTION   : procEvtRecordingState
2371  *
2372  * DESCRIPTION: finite state machine function to handle event in state of
2373  *              QCAMERA_SM_STATE_RECORDING.
2374  *
2375  * PARAMETERS :
2376  *   @evt      : event to be processed
2377  *   @payload  : event payload. Can be NULL if not needed.
2378  *
2379  * RETURN     : int32_t type of status
2380  *              NO_ERROR  -- success
2381  *              none-zero failure code
2382  *==========================================================================*/
procEvtRecordingState(qcamera_sm_evt_enum_t evt,void * payload)2383 int32_t QCameraStateMachine::procEvtRecordingState(qcamera_sm_evt_enum_t evt,
2384                                                    void *payload)
2385 {
2386     int32_t rc = NO_ERROR;
2387     qcamera_api_result_t result;
2388     memset(&result, 0, sizeof(qcamera_api_result_t));
2389 
2390     LOGL("event (%d)", evt);
2391     switch (evt) {
2392     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
2393     case QCAMERA_SM_EVT_START_PREVIEW:
2394     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2395         {
2396             // WA: CTS test VideoSnapshot will try to
2397             //     start preview during video recording.
2398             LOGH("CTS video restart op");
2399             rc = NO_ERROR;
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_SET_CALLBACKS:
2407         {
2408             qcamera_sm_evt_setcb_payload_t *setcbs =
2409                 (qcamera_sm_evt_setcb_payload_t *)payload;
2410             rc = m_parent->setCallBacks(setcbs->notify_cb,
2411                                         setcbs->data_cb,
2412                                         setcbs->data_cb_timestamp,
2413                                         setcbs->get_memory,
2414                                         setcbs->user);
2415             result.status = rc;
2416             result.request_api = evt;
2417             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2418             m_parent->signalAPIResult(&result);
2419         }
2420         break;
2421     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
2422         {
2423             rc = m_parent->enableMsgType(*((int32_t *)payload));
2424             result.status = rc;
2425             result.request_api = evt;
2426             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2427             m_parent->signalAPIResult(&result);
2428         }
2429         break;
2430     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
2431         {
2432             rc = m_parent->disableMsgType(*((int32_t *)payload));
2433             result.status = rc;
2434             result.request_api = evt;
2435             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2436             m_parent->signalAPIResult(&result);
2437         }
2438         break;
2439     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
2440         {
2441             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
2442             result.status = rc;
2443             result.request_api = evt;
2444             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2445             result.enabled = enabled;
2446             m_parent->signalAPIResult(&result);
2447         }
2448         break;
2449     case QCAMERA_SM_EVT_SET_PARAMS:
2450         {
2451             bool needRestart = false;
2452             rc = m_parent->updateParameters((char*)payload, needRestart);
2453             if (rc == NO_ERROR) {
2454                 if (needRestart) {
2455                     // cannot set parameters that requires restart during recording
2456                     LOGE("Error!! cannot set parameters that requires restart during recording");
2457                     rc = BAD_VALUE;
2458                 }
2459             }
2460             if (rc != NO_ERROR) {
2461                 m_parent->setNeedRestart(false);
2462             }
2463             result.status = rc;
2464             result.request_api = evt;
2465             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2466             m_parent->signalAPIResult(&result);
2467         }
2468         break;
2469     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
2470         {
2471             rc = m_parent->commitParameterChanges();
2472             result.status = rc;
2473             result.request_api = evt;
2474             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2475             m_parent->signalAPIResult(&result);
2476         }
2477         break;
2478     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
2479     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
2480         {
2481             ALOGE("%s: Error!! cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2482             rc = INVALID_OPERATION;
2483             result.status = rc;
2484             result.request_api = evt;
2485             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2486             m_parent->signalAPIResult(&result);
2487         }
2488         break;
2489     case QCAMERA_SM_EVT_GET_PARAMS:
2490         {
2491             result.params = m_parent->getParameters();
2492             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
2493             result.status = rc;
2494             result.request_api = evt;
2495             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
2496             m_parent->signalAPIResult(&result);
2497         }
2498         break;
2499     case QCAMERA_SM_EVT_PUT_PARAMS:
2500         {
2501             rc = m_parent->putParameters((char*)payload);
2502             result.status = rc;
2503             result.request_api = evt;
2504             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2505             m_parent->signalAPIResult(&result);
2506         }
2507         break;
2508     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2509         {
2510             rc = NO_ERROR;
2511             result.status = rc;
2512             result.request_api = evt;
2513             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2514             result.enabled = 0;
2515             m_parent->signalAPIResult(&result);
2516         }
2517         break;
2518     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2519         {
2520             rc = NO_ERROR;
2521             result.status = rc;
2522             result.request_api = evt;
2523             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2524             result.enabled = 1;
2525             m_parent->signalAPIResult(&result);
2526         }
2527         break;
2528     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2529         {
2530             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
2531             result.status = rc;
2532             result.request_api = evt;
2533             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2534             m_parent->signalAPIResult(&result);
2535         }
2536         break;
2537     case QCAMERA_SM_EVT_DUMP:
2538         {
2539             rc = m_parent->dump(*((int *)payload));
2540             result.status = rc;
2541             result.request_api = evt;
2542             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2543             m_parent->signalAPIResult(&result);
2544         }
2545         break;
2546     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2547         {
2548             rc = m_parent->autoFocus();
2549             result.status = rc;
2550             result.request_api = evt;
2551             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2552             m_parent->signalAPIResult(&result);
2553         }
2554         break;
2555     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2556         {
2557             rc = m_parent->cancelAutoFocus();
2558             result.status = rc;
2559             result.request_api = evt;
2560             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2561             m_parent->signalAPIResult(&result);
2562         }
2563         break;
2564     case QCAMERA_SM_EVT_SEND_COMMAND:
2565         {
2566             qcamera_sm_evt_command_payload_t *cmd_payload =
2567                 (qcamera_sm_evt_command_payload_t *)payload;
2568             rc = m_parent->sendCommand(cmd_payload->cmd,
2569                                        cmd_payload->arg1,
2570                                        cmd_payload->arg2);
2571             result.status = rc;
2572             result.request_api = evt;
2573             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2574             m_parent->signalAPIResult(&result);
2575         }
2576         break;
2577     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
2578         {
2579             // No ops here, send NO_ERROR.
2580             rc = NO_ERROR;
2581             result.status = rc;
2582             result.request_api = evt;
2583             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2584             m_parent->signalAPIResult(&result);
2585         }
2586         break;
2587     case QCAMERA_SM_EVT_TAKE_PICTURE:
2588         {
2589             m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
2590             rc = m_parent->takeLiveSnapshot();
2591             if (rc != NO_ERROR) {
2592                 m_parent->unconfigureAdvancedCapture();
2593                 m_state = QCAMERA_SM_STATE_RECORDING;
2594             }
2595             result.status = rc;
2596             result.request_api = evt;
2597             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2598             m_parent->signalAPIResult(&result);
2599         }
2600         break;
2601     case QCAMERA_SM_EVT_PRE_START_RECORDING:
2602     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
2603     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
2604     case QCAMERA_SM_EVT_START_RECORDING:
2605         {
2606             // no ops here
2607             LOGW("already in recording state, no ops for start_recording");
2608             rc = 0;
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_STOP_RECORDING:
2616         {
2617             rc = m_parent->stopRecording();
2618             m_state = QCAMERA_SM_STATE_PREVIEWING;
2619             result.status = rc;
2620             result.request_api = evt;
2621             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2622             m_parent->signalAPIResult(&result);
2623         }
2624         break;
2625     case QCAMERA_SM_EVT_STOP_PREVIEW:
2626         {
2627             rc = m_parent->stopRecording();
2628             m_state = QCAMERA_SM_STATE_PREVIEWING;
2629 
2630             rc = m_parent->stopPreview();
2631             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
2632 
2633             result.status = rc;
2634             result.request_api = evt;
2635             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2636             m_parent->signalAPIResult(&result);
2637         }
2638         break;
2639     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2640         {
2641             rc = m_parent->releaseRecordingFrame((const void *)payload);
2642             result.status = rc;
2643             result.request_api = evt;
2644             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2645             m_parent->signalAPIResult(&result);
2646         }
2647         break;
2648     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
2649         {
2650             int32_t faceID = 0;
2651             qcamera_sm_evt_reg_face_payload_t *reg_payload =
2652                 (qcamera_sm_evt_reg_face_payload_t *)payload;
2653             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
2654                                              reg_payload->config,
2655                                              faceID);
2656             result.status = rc;
2657             result.request_api = evt;
2658             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
2659             result.handle = faceID;
2660             m_parent->signalAPIResult(&result);
2661         }
2662         break;
2663     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
2664         {
2665             //In Video snapshot, prepare hardware is a no-op.
2666             result.status = NO_ERROR;
2667             result.request_api = evt;
2668             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2669             m_parent->signalAPIResult(&result);
2670         }
2671         break;
2672     case QCAMERA_SM_EVT_CANCEL_PICTURE:
2673     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2674     case QCAMERA_SM_EVT_RELEASE:
2675         {
2676             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
2677             rc = INVALID_OPERATION;
2678             result.status = rc;
2679             result.request_api = evt;
2680             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2681             m_parent->signalAPIResult(&result);
2682         }
2683         break;
2684     case QCAMERA_SM_EVT_EVT_INTERNAL:
2685         {
2686             qcamera_sm_internal_evt_payload_t *internal_evt =
2687                 (qcamera_sm_internal_evt_payload_t *)payload;
2688             switch (internal_evt->evt_type) {
2689             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2690                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2691                 break;
2692             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
2693                 break;
2694             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
2695                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
2696                 break;
2697             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
2698                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
2699                 break;
2700             case QCAMERA_INTERNAL_EVT_CROP_INFO:
2701                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
2702                 break;
2703             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
2704                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
2705                 break;
2706             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
2707                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2708                 break;
2709             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
2710                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
2711                 break;
2712             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
2713                 rc = m_parent->processAEInfo(internal_evt->ae_data);
2714                 break;
2715             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
2716                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
2717                 break;
2718             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
2719                 rc = m_parent->processHDRData(internal_evt->hdr_data);
2720                 break;
2721             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
2722                 rc = m_parent->processRetroAECUnlock();
2723                 break;
2724             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
2725                 rc = m_parent->processZSLCaptureDone();
2726                 break;
2727             case QCAMERA_INTERNAL_EVT_DUAL_CAMERA_FOV_CONTROL:
2728                 m_parent->processDualCamFovControl();
2729                 break;
2730             case QCAMERA_INTERNAL_EVT_LED_CALIB_UPDATE:
2731                 rc = m_parent->processLEDCalibration(internal_evt->led_calib_result);
2732                 break;
2733             default:
2734                 break;
2735             }
2736         }
2737         break;
2738     case QCAMERA_SM_EVT_EVT_NOTIFY:
2739         {
2740             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2741             switch (cam_evt->server_event_type) {
2742             case CAM_EVENT_TYPE_DAEMON_DIED:
2743                 {
2744                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
2745                                             CAMERA_ERROR_SERVER_DIED,
2746                                             0);
2747                 }
2748                 break;
2749             default:
2750                 LOGE("Invalid internal event %d in state(%d)",
2751                              cam_evt->server_event_type, m_state);
2752                 break;
2753             }
2754         }
2755         break;
2756     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2757         {
2758             rc = m_parent->updateThermalLevel(payload);
2759         }
2760         break;
2761     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2762         {
2763             // No ops, but need to notify
2764             LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2765             result.status = rc;
2766             result.request_api = evt;
2767             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2768             m_parent->signalEvtResult(&result);
2769         }
2770        break;
2771     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2772     default:
2773         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
2774         break;
2775     }
2776 
2777     return rc;
2778 }
2779 
2780 /*===========================================================================
2781  * FUNCTION   : procEvtVideoPicTakingState
2782  *
2783  * DESCRIPTION: finite state machine function to handle event in state of
2784  *              QCAMERA_SM_STATE_VIDEO_PIC_TAKING.
2785  *
2786  * PARAMETERS :
2787  *   @evt      : event to be processed
2788  *   @payload  : event payload. Can be NULL if not needed.
2789  *
2790  * RETURN     : int32_t type of status
2791  *              NO_ERROR  -- success
2792  *              none-zero failure code
2793  *==========================================================================*/
procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)2794 int32_t QCameraStateMachine::procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,
2795                                                         void *payload)
2796 {
2797     int32_t rc = NO_ERROR;
2798     qcamera_api_result_t result;
2799     memset(&result, 0, sizeof(qcamera_api_result_t));
2800 
2801     LOGL("event (%d)", evt);
2802     switch (evt) {
2803     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2804         {
2805             // Error setting preview window during previewing
2806             LOGE("Error!! cannot set preview window when preview is running");
2807             rc = INVALID_OPERATION;
2808             result.status = rc;
2809             result.request_api = evt;
2810             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2811             m_parent->signalAPIResult(&result);
2812         }
2813         break;
2814     case QCAMERA_SM_EVT_SET_CALLBACKS:
2815         {
2816             qcamera_sm_evt_setcb_payload_t *setcbs =
2817                 (qcamera_sm_evt_setcb_payload_t *)payload;
2818             rc = m_parent->setCallBacks(setcbs->notify_cb,
2819                                         setcbs->data_cb,
2820                                         setcbs->data_cb_timestamp,
2821                                         setcbs->get_memory,
2822                                         setcbs->user);
2823             result.status = rc;
2824             result.request_api = evt;
2825             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2826             m_parent->signalAPIResult(&result);
2827         }
2828         break;
2829     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
2830         {
2831             rc = m_parent->enableMsgType(*((int32_t *)payload));
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_DISABLE_MSG_TYPE:
2839         {
2840             rc = m_parent->disableMsgType(*((int32_t *)payload));
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_MSG_TYPE_ENABLED:
2848         {
2849             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
2850             result.status = rc;
2851             result.request_api = evt;
2852             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2853             result.enabled = enabled;
2854             m_parent->signalAPIResult(&result);
2855         }
2856         break;
2857     case QCAMERA_SM_EVT_SET_PARAMS:
2858         {
2859             bool needRestart = false;
2860             rc = m_parent->updateParameters((char*)payload, needRestart);
2861             if (rc == NO_ERROR) {
2862                 if (needRestart) {
2863                     // cannot set parameters that requires restart during recording
2864                     LOGE("Error!! cannot set parameters that requires restart during recording");
2865                     rc = BAD_VALUE;
2866                 }
2867             }
2868             if (rc != NO_ERROR) {
2869                 m_parent->setNeedRestart(false);
2870             }
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_SET_PARAMS_COMMIT:
2878         {
2879             rc = m_parent->commitParameterChanges();
2880             result.status = rc;
2881             result.request_api = evt;
2882             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2883             m_parent->signalAPIResult(&result);
2884         }
2885         break;
2886     case QCAMERA_SM_EVT_SET_PARAMS_STOP:
2887     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
2888         {
2889             ALOGE("%s: Error!! cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2890             rc = INVALID_OPERATION;
2891             result.status = rc;
2892             result.request_api = evt;
2893             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2894             m_parent->signalAPIResult(&result);
2895         }
2896         break;
2897     case QCAMERA_SM_EVT_GET_PARAMS:
2898         {
2899             result.params = m_parent->getParameters();
2900             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
2901             result.status = rc;
2902             result.request_api = evt;
2903             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
2904             m_parent->signalAPIResult(&result);
2905         }
2906         break;
2907     case QCAMERA_SM_EVT_PUT_PARAMS:
2908         {
2909             rc = m_parent->putParameters((char*)payload);
2910             result.status = rc;
2911             result.request_api = evt;
2912             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2913             m_parent->signalAPIResult(&result);
2914         }
2915         break;
2916     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
2917         {
2918             rc = NO_ERROR;
2919             result.status = rc;
2920             result.request_api = evt;
2921             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2922             result.enabled = 1;
2923             m_parent->signalAPIResult(&result);
2924         }
2925         break;
2926     case QCAMERA_SM_EVT_RECORDING_ENABLED:
2927         {
2928             rc = NO_ERROR;
2929             result.status = rc;
2930             result.request_api = evt;
2931             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2932             result.enabled = 1;
2933             m_parent->signalAPIResult(&result);
2934         }
2935         break;
2936     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2937         {
2938             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
2939             result.status = rc;
2940             result.request_api = evt;
2941             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2942             m_parent->signalAPIResult(&result);
2943         }
2944         break;
2945     case QCAMERA_SM_EVT_DUMP:
2946         {
2947             rc = m_parent->dump(*((int *)payload));
2948             result.status = rc;
2949             result.request_api = evt;
2950             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2951             m_parent->signalAPIResult(&result);
2952         }
2953         break;
2954     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2955         {
2956             rc = m_parent->autoFocus();
2957             result.status = rc;
2958             result.request_api = evt;
2959             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2960             m_parent->signalAPIResult(&result);
2961         }
2962         break;
2963     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2964         {
2965             rc = m_parent->cancelAutoFocus();
2966             result.status = rc;
2967             result.request_api = evt;
2968             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2969             m_parent->signalAPIResult(&result);
2970         }
2971         break;
2972     case QCAMERA_SM_EVT_SEND_COMMAND:
2973         {
2974             qcamera_sm_evt_command_payload_t *cmd_payload =
2975                 (qcamera_sm_evt_command_payload_t *)payload;
2976             rc = m_parent->sendCommand(cmd_payload->cmd,
2977                                        cmd_payload->arg1,
2978                                        cmd_payload->arg2);
2979             result.status = rc;
2980             result.request_api = evt;
2981             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2982             m_parent->signalAPIResult(&result);
2983         }
2984         break;
2985     case QCAMERA_SM_EVT_STOP_RECORDING:
2986         {
2987             rc = m_parent->cancelLiveSnapshot();
2988             m_state = QCAMERA_SM_STATE_RECORDING;
2989 
2990             rc = m_parent->stopRecording();
2991             m_state = QCAMERA_SM_STATE_PREVIEWING;
2992 
2993             result.status = rc;
2994             result.request_api = evt;
2995             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2996             m_parent->signalAPIResult(&result);
2997         }
2998         break;
2999     case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
3000         {
3001             rc = m_parent->releaseRecordingFrame((const void *)payload);
3002             result.status = rc;
3003             result.request_api = evt;
3004             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3005             m_parent->signalAPIResult(&result);
3006         }
3007         break;
3008     case QCAMERA_SM_EVT_CANCEL_PICTURE:
3009         {
3010             rc = m_parent->cancelLiveSnapshot();
3011             m_state = QCAMERA_SM_STATE_RECORDING;
3012             result.status = rc;
3013             result.request_api = evt;
3014             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3015             m_parent->signalAPIResult(&result);
3016         }
3017         break;
3018     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
3019         {
3020             int32_t faceID = 0;
3021             qcamera_sm_evt_reg_face_payload_t *reg_payload =
3022                 (qcamera_sm_evt_reg_face_payload_t *)payload;
3023             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
3024                                              reg_payload->config,
3025                                              faceID);
3026             result.status = rc;
3027             result.request_api = evt;
3028             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
3029             result.handle = faceID;
3030             m_parent->signalAPIResult(&result);
3031         }
3032         break;
3033     case QCAMERA_SM_EVT_STOP_PREVIEW:
3034         {
3035             rc = m_parent->cancelLiveSnapshot();
3036             m_state = QCAMERA_SM_STATE_RECORDING;
3037 
3038             rc = m_parent->stopRecording();
3039             m_state = QCAMERA_SM_STATE_PREVIEWING;
3040 
3041             rc = m_parent->stopPreview();
3042             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
3043 
3044             result.status = rc;
3045             result.request_api = evt;
3046             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3047             m_parent->signalAPIResult(&result);
3048         }
3049         break;
3050     case QCAMERA_SM_EVT_PRE_START_RECORDING:
3051     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
3052     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
3053     case QCAMERA_SM_EVT_START_RECORDING:
3054     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
3055     case QCAMERA_SM_EVT_START_PREVIEW:
3056     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
3057     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
3058     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
3059     case QCAMERA_SM_EVT_TAKE_PICTURE:
3060     case QCAMERA_SM_EVT_RELEASE:
3061         {
3062             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3063             rc = INVALID_OPERATION;
3064             result.status = rc;
3065             result.request_api = evt;
3066             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3067             m_parent->signalAPIResult(&result);
3068         }
3069         break;
3070     case QCAMERA_SM_EVT_EVT_INTERNAL:
3071         {
3072             qcamera_sm_internal_evt_payload_t *internal_evt =
3073                 (qcamera_sm_internal_evt_payload_t *)payload;
3074             switch (internal_evt->evt_type) {
3075             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
3076                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
3077                 break;
3078             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
3079                 break;
3080             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
3081                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
3082                 break;
3083             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
3084                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
3085                 break;
3086             case QCAMERA_INTERNAL_EVT_CROP_INFO:
3087                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
3088                 break;
3089             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
3090                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
3091                 break;
3092             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
3093                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3094                 break;
3095             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
3096                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
3097                 break;
3098             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
3099                 rc = m_parent->processAEInfo(internal_evt->ae_data);
3100                 break;
3101             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
3102                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
3103                 break;
3104             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
3105                 rc = m_parent->processHDRData(internal_evt->hdr_data);
3106                 break;
3107             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
3108                 rc = m_parent->processRetroAECUnlock();
3109                 break;
3110             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
3111                 rc = m_parent->processZSLCaptureDone();
3112                 break;
3113             case QCAMERA_INTERNAL_EVT_DUAL_CAMERA_FOV_CONTROL:
3114                 m_parent->processDualCamFovControl();
3115                 break;
3116             case QCAMERA_INTERNAL_EVT_LED_CALIB_UPDATE:
3117                 rc = m_parent->processLEDCalibration(internal_evt->led_calib_result);
3118                 break;
3119             default:
3120                 break;
3121             }
3122         }
3123         break;
3124     case QCAMERA_SM_EVT_EVT_NOTIFY:
3125         {
3126             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
3127             switch (cam_evt->server_event_type) {
3128             case CAM_EVENT_TYPE_DAEMON_DIED:
3129                 {
3130                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
3131                                             CAMERA_ERROR_SERVER_DIED,
3132                                             0);
3133                 }
3134                 break;
3135             default:
3136                 LOGE("Invalid internal event %d in state(%d)",
3137                              cam_evt->server_event_type, m_state);
3138                 break;
3139             }
3140         }
3141         break;
3142     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
3143         {
3144             qcamera_jpeg_evt_payload_t *jpeg_job =
3145                 (qcamera_jpeg_evt_payload_t *)payload;
3146             rc = m_parent->processJpegNotify(jpeg_job);
3147         }
3148         break;
3149     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
3150         {
3151             rc = m_parent->cancelLiveSnapshot();
3152             m_state = QCAMERA_SM_STATE_RECORDING;
3153             result.status = rc;
3154             result.request_api = evt;
3155             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3156             m_parent->signalEvtResult(&result);
3157         }
3158         break;
3159     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
3160         {
3161             rc = m_parent->updateThermalLevel(payload);
3162         }
3163         break;
3164     default:
3165         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3166         break;
3167     }
3168 
3169     return rc;
3170 }
3171 
3172 /*===========================================================================
3173  * FUNCTION   : procEvtPreviewPicTakingState
3174  *
3175  * DESCRIPTION: finite state machine function to handle event in state of
3176  *              QCAMERA_SM_STATE_PREVIEW_PIC_TAKING.
3177  *
3178  * PARAMETERS :
3179  *   @evt      : event to be processed
3180  *   @payload  : event payload. Can be NULL if not needed.
3181  *
3182  * RETURN     : int32_t type of status
3183  *              NO_ERROR  -- success
3184  *              none-zero failure code
3185  *==========================================================================*/
procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,void * payload)3186 int32_t QCameraStateMachine::procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,
3187                                                           void *payload)
3188 {
3189     int32_t rc = NO_ERROR;
3190     qcamera_api_result_t result;
3191     memset(&result, 0, sizeof(qcamera_api_result_t));
3192 
3193     LOGL("event (%d)", evt);
3194     switch (evt) {
3195     case QCAMERA_SM_EVT_SET_CALLBACKS:
3196         {
3197             qcamera_sm_evt_setcb_payload_t *setcbs =
3198                 (qcamera_sm_evt_setcb_payload_t *)payload;
3199             rc = m_parent->setCallBacks(setcbs->notify_cb,
3200                                         setcbs->data_cb,
3201                                         setcbs->data_cb_timestamp,
3202                                         setcbs->get_memory,
3203                                         setcbs->user);
3204             result.status = rc;
3205             result.request_api = evt;
3206             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3207             m_parent->signalAPIResult(&result);
3208         }
3209         break;
3210     case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
3211         {
3212             rc = m_parent->enableMsgType(*((int32_t *)payload));
3213             result.status = rc;
3214             result.request_api = evt;
3215             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3216             m_parent->signalAPIResult(&result);
3217         }
3218         break;
3219     case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
3220         {
3221             rc = m_parent->disableMsgType(*((int32_t *)payload));
3222             result.status = rc;
3223             result.request_api = evt;
3224             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3225             m_parent->signalAPIResult(&result);
3226         }
3227         break;
3228     case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
3229         {
3230             int enabled = m_parent->msgTypeEnabled(*((int32_t *)payload));
3231             result.status = rc;
3232             result.request_api = evt;
3233             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
3234             result.enabled = enabled;
3235             m_parent->signalAPIResult(&result);
3236         }
3237         break;
3238     case QCAMERA_SM_EVT_SET_PARAMS:
3239         {
3240             bool needRestart = false;
3241             rc = m_parent->updateParameters((char*)payload, needRestart);
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_SET_PARAMS_STOP:
3249         {
3250             // need restart preview for parameters to take effect
3251             LOGD("Stopping preview...");
3252             // stop preview
3253             rc = m_parent->stopPreview();
3254             // Clear memory pools
3255             m_parent->m_memoryPool.clear();
3256             result.status = rc;
3257             result.request_api = evt;
3258             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3259             m_parent->signalAPIResult(&result);
3260         }
3261         break;
3262     case QCAMERA_SM_EVT_SET_PARAMS_COMMIT:
3263         {
3264             // commit parameter changes to server
3265             rc = m_parent->commitParameterChanges();
3266             result.status = rc;
3267             result.request_api = evt;
3268             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3269             m_parent->signalAPIResult(&result);
3270         }
3271         break;
3272     case QCAMERA_SM_EVT_SET_PARAMS_RESTART:
3273         {
3274             // start preview again
3275             rc = m_parent->preparePreview();
3276             if (rc == NO_ERROR) {
3277                 applyDelayedMsgs();
3278                 rc = m_parent->startPreview();
3279                 if (rc != NO_ERROR) {
3280                     m_parent->unpreparePreview();
3281                 }
3282             }
3283             if (rc != NO_ERROR) {
3284                 m_state = QCAMERA_SM_STATE_PIC_TAKING;
3285             }
3286             m_parent->setNeedRestart(false);
3287             result.status           =  rc;
3288             result.request_api      =  evt;
3289             result.result_type      =  QCAMERA_API_RESULT_TYPE_DEF;
3290             m_parent->signalAPIResult(&result);
3291         }
3292         break;
3293     case QCAMERA_SM_EVT_GET_PARAMS:
3294         {
3295             result.params = m_parent->getParameters();
3296             rc = result.params ? NO_ERROR : UNKNOWN_ERROR;
3297             result.status = rc;
3298             result.request_api = evt;
3299             result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
3300             m_parent->signalAPIResult(&result);
3301         }
3302         break;
3303     case QCAMERA_SM_EVT_PUT_PARAMS:
3304         {
3305             rc = m_parent->putParameters((char*)payload);
3306             result.status = rc;
3307             result.request_api = evt;
3308             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3309             m_parent->signalAPIResult(&result);
3310         }
3311         break;
3312     case QCAMERA_SM_EVT_PREVIEW_ENABLED:
3313         {
3314             rc = NO_ERROR;
3315             result.status = rc;
3316             result.request_api = evt;
3317             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
3318             result.enabled = 1;
3319             m_parent->signalAPIResult(&result);
3320         }
3321         break;
3322     case QCAMERA_SM_EVT_RECORDING_ENABLED:
3323         {
3324             rc = NO_ERROR;
3325             result.status = rc;
3326             result.request_api = evt;
3327             result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
3328             result.enabled = 0;
3329             m_parent->signalAPIResult(&result);
3330         }
3331         break;
3332     case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
3333         {
3334             rc = m_parent->storeMetaDataInBuffers(*((int *)payload));
3335             result.status = rc;
3336             result.request_api = evt;
3337             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3338             m_parent->signalAPIResult(&result);
3339         }
3340         break;
3341     case QCAMERA_SM_EVT_DUMP:
3342         {
3343             rc = m_parent->dump(*((int *)payload));
3344             result.status = rc;
3345             result.request_api = evt;
3346             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3347             m_parent->signalAPIResult(&result);
3348         }
3349         break;
3350     case QCAMERA_SM_EVT_START_AUTO_FOCUS:
3351         {
3352             rc = m_parent->autoFocus();
3353             result.status = rc;
3354             result.request_api = evt;
3355             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3356             m_parent->signalAPIResult(&result);
3357         }
3358         break;
3359     case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
3360         {
3361             rc = m_parent->cancelAutoFocus();
3362             result.status = rc;
3363             result.request_api = evt;
3364             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3365             m_parent->signalAPIResult(&result);
3366         }
3367         break;
3368     case QCAMERA_SM_EVT_SEND_COMMAND:
3369         {
3370             qcamera_sm_evt_command_payload_t *cmd_payload =
3371                 (qcamera_sm_evt_command_payload_t *)payload;
3372             rc = m_parent->sendCommand(cmd_payload->cmd,
3373                                        cmd_payload->arg1,
3374                                        cmd_payload->arg2);
3375 #ifndef VANILLA_HAL
3376             if ( CAMERA_CMD_LONGSHOT_OFF == cmd_payload->cmd ) {
3377                 // move state to previewing state
3378                 m_state = QCAMERA_SM_STATE_PREVIEWING;
3379             }
3380 #endif
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_RELEASE_RECORIDNG_FRAME:
3388         {
3389             rc = m_parent->releaseRecordingFrame((const void *)payload);
3390             result.status = rc;
3391             result.request_api = evt;
3392             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3393             m_parent->signalAPIResult(&result);
3394         }
3395         break;
3396     case QCAMERA_SM_EVT_CANCEL_PICTURE:
3397         {
3398             if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
3399                 rc = m_parent->cancelPicture();
3400             } else {
3401                 rc = m_parent->cancelLiveSnapshot();
3402             }
3403             m_state = QCAMERA_SM_STATE_PREVIEWING;
3404             result.status = rc;
3405             result.request_api = evt;
3406             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3407             m_parent->signalAPIResult(&result);
3408         }
3409         break;
3410     case QCAMERA_SM_EVT_STOP_PREVIEW:
3411         {
3412             if (m_parent->isZSLMode()) {
3413                 // cancel picture first
3414                 rc = m_parent->cancelPicture();
3415                 m_parent->stopChannel(QCAMERA_CH_TYPE_ZSL);
3416             } else if (m_parent->isLongshotEnabled()) {
3417                 // just cancel picture
3418                 rc = m_parent->cancelPicture();
3419             } else {
3420                 rc = m_parent->cancelLiveSnapshot();
3421                 m_parent->stopChannel(QCAMERA_CH_TYPE_PREVIEW);
3422             }
3423             // unprepare preview
3424             m_parent->unpreparePreview();
3425             m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
3426             result.status = rc;
3427             result.request_api = evt;
3428             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3429             m_parent->signalAPIResult(&result);
3430         }
3431         break;
3432     case QCAMERA_SM_EVT_PRE_START_RECORDING:
3433         {
3434             if (m_parent->isZSLMode()) {
3435                 LOGE("Error!! cannot handle evt(%d) in state(%d) in ZSL mode", evt, m_state);
3436                 rc = INVALID_OPERATION;
3437             } else if (m_parent->isLongshotEnabled()) {
3438                 LOGE("Error!! cannot handle evt(%d) in state(%d) in Longshot mode", evt, m_state);
3439                 rc = INVALID_OPERATION;
3440             } else {
3441                 rc = m_parent->preStartRecording();
3442             }
3443             result.status = rc;
3444             result.request_api = evt;
3445             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3446             m_parent->signalAPIResult(&result);
3447         }
3448         break;
3449     case QCAMERA_SM_EVT_START_RECORDING:
3450         {
3451             if (m_parent->isZSLMode()) {
3452                 LOGE("Error!! cannot handle evt(%d) in state(%d) in ZSL mode",
3453                        evt, m_state);
3454                 rc = INVALID_OPERATION;
3455             } else if (m_parent->isLongshotEnabled()) {
3456                 LOGE("Error!! cannot handle evt(%d) in state(%d) in Longshot mode",
3457                        evt, m_state);
3458                 rc = INVALID_OPERATION;
3459             } else {
3460                 rc = m_parent->startRecording();
3461                 if (rc == NO_ERROR) {
3462                     m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
3463                 }
3464             }
3465             result.status = rc;
3466             result.request_api = evt;
3467             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3468             m_parent->signalAPIResult(&result);
3469         }
3470         break;
3471     case QCAMERA_SM_EVT_REG_FACE_IMAGE:
3472         {
3473             int32_t faceID = 0;
3474             qcamera_sm_evt_reg_face_payload_t *reg_payload =
3475                 (qcamera_sm_evt_reg_face_payload_t *)payload;
3476             rc = m_parent->registerFaceImage(reg_payload->img_ptr,
3477                                              reg_payload->config,
3478                                              faceID);
3479             result.status = rc;
3480             result.request_api = evt;
3481             result.result_type = QCAMERA_API_RESULT_TYPE_HANDLE;
3482             result.handle = faceID;
3483             m_parent->signalAPIResult(&result);
3484         }
3485         break;
3486     case QCAMERA_SM_EVT_PRE_TAKE_PICTURE:
3487         {
3488            if ( m_parent->isLongshotEnabled() ) {
3489                // no ops here, need to singal NO_ERROR
3490                rc = NO_ERROR;
3491             } else {
3492                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3493                 rc = INVALID_OPERATION;
3494             }
3495 
3496             result.status = rc;
3497             result.request_api = evt;
3498             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3499             m_parent->signalAPIResult(&result);
3500         }
3501         break;
3502     case QCAMERA_SM_EVT_TAKE_PICTURE:
3503         {
3504             if ( m_parent->isLongshotEnabled() ) {
3505                rc = m_parent->longShot();
3506             } else {
3507                 LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3508                 rc = INVALID_OPERATION;
3509             }
3510 
3511             result.status = rc;
3512             result.request_api = evt;
3513             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3514             m_parent->signalAPIResult(&result);
3515         }
3516         break;
3517 
3518     case QCAMERA_SM_EVT_PREPARE_SNAPSHOT:
3519         {
3520           LOGD("Prepare Snapshot");
3521           if (m_parent->isRetroPicture()) {
3522               LOGD("Prepare Snapshot in Retro Mode");
3523               rc = m_parent->prepareHardwareForSnapshot(FALSE);
3524               if (rc != NO_ERROR) {
3525                   LOGE("prepareHardwareForSnapshot failed %d",
3526                        rc);
3527                   result.status = rc;
3528                   result.request_api = evt;
3529                   result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3530                   m_parent->signalAPIResult(&result);
3531               }
3532           }
3533           else {
3534               LOGE("Error!! cannot handle evt(%d) in state(%d)",
3535                  evt, m_state);
3536               rc = INVALID_OPERATION;
3537               result.status = rc;
3538               result.request_api = evt;
3539               result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3540               m_parent->signalAPIResult(&result);
3541           }
3542         }
3543         break;
3544     case QCAMERA_SM_EVT_STOP_RECORDING:
3545     case QCAMERA_SM_EVT_PREPARE_PREVIEW:
3546     case QCAMERA_SM_EVT_START_PREVIEW:
3547     case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
3548     case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
3549     case QCAMERA_SM_EVT_RELEASE:
3550         {
3551             LOGE("Error!! cannot handle evt(%d) in state(%d)", evt, m_state);
3552             rc = INVALID_OPERATION;
3553             result.status = rc;
3554             result.request_api = evt;
3555             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3556             m_parent->signalAPIResult(&result);
3557         }
3558         break;
3559     case QCAMERA_SM_EVT_EVT_INTERNAL:
3560         {
3561             qcamera_sm_internal_evt_payload_t *internal_evt =
3562                 (qcamera_sm_internal_evt_payload_t *)payload;
3563             switch (internal_evt->evt_type) {
3564             case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
3565                 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
3566                 break;
3567             case QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE:
3568                 LOGD("Received QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE event");
3569                 if (m_parent->isRetroPicture()) {
3570                     m_parent->processPrepSnapshotDoneEvent(internal_evt->prep_snapshot_state);
3571                     LOGD("Retro picture");
3572                     result.status = NO_ERROR;
3573                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
3574                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3575                     m_parent->signalAPIResult(&result);
3576                 }
3577                 else {
3578                     LOGE("Invalid Case for  "
3579                             "QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event");
3580                 }
3581                 break;
3582             case QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT:
3583                 rc = m_parent->processFaceDetectionResult(&internal_evt->faces_data);
3584                 break;
3585             case QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT:
3586                 // This is valid only in Retro picture Mode
3587                 if (m_parent->isRetroPicture()) {
3588                     LOGD("Received QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event");
3589                     result.status = NO_ERROR;
3590                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
3591                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3592                     m_parent->signalAPIResult(&result);
3593                 }
3594                 else {
3595                     LOGD("Wrong Case for QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT event");
3596                 }
3597                 break;
3598             case QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS:
3599                 rc = m_parent->processHistogramStats(internal_evt->stats_data);
3600                 break;
3601             case QCAMERA_INTERNAL_EVT_CROP_INFO:
3602                 rc = m_parent->processZoomEvent(internal_evt->crop_data);
3603                 break;
3604             case QCAMERA_INTERNAL_EVT_ASD_UPDATE:
3605                 rc = m_parent->processASDUpdate(internal_evt->asd_data);
3606                 break;
3607             case QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE:
3608                 LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3609                 break;
3610             case QCAMERA_INTERNAL_EVT_AWB_UPDATE:
3611                 rc = m_parent->transAwbMetaToParams(internal_evt->awb_data);
3612                 break;
3613             case QCAMERA_INTERNAL_EVT_AE_UPDATE:
3614                 rc = m_parent->processAEInfo(internal_evt->ae_data);
3615                 break;
3616             case QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE:
3617                 rc = m_parent->processFocusPositionInfo(internal_evt->focus_pos);
3618                 break;
3619             case QCAMERA_INTERNAL_EVT_HDR_UPDATE:
3620                 rc = m_parent->processHDRData(internal_evt->hdr_data);
3621                 break;
3622             case QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK:
3623                 rc = m_parent->processRetroAECUnlock();
3624                 break;
3625             case QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE:
3626                 rc = m_parent->processZSLCaptureDone();
3627                 break;
3628             case QCAMERA_INTERNAL_EVT_DUAL_CAMERA_FOV_CONTROL:
3629                 m_parent->processDualCamFovControl();
3630                 break;
3631             case QCAMERA_INTERNAL_EVT_LED_CALIB_UPDATE:
3632                 rc = m_parent->processLEDCalibration(internal_evt->led_calib_result);
3633                 break;
3634             default:
3635                 break;
3636             }
3637         }
3638         break;
3639     case QCAMERA_SM_EVT_EVT_NOTIFY:
3640         {
3641             mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
3642             switch (cam_evt->server_event_type) {
3643             case CAM_EVENT_TYPE_DAEMON_DIED:
3644                 {
3645                     // Send internal events to stop indefinite wait on prepare
3646                     // snapshot done event.
3647                     result.status = rc;
3648                     result.request_api = QCAMERA_SM_EVT_PREPARE_SNAPSHOT;
3649                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3650                     m_parent->signalAPIResult(&result);
3651 
3652                     result.status = rc;
3653                     result.request_api = QCAMERA_SM_EVT_TAKE_PICTURE;
3654                     result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3655                     m_parent->signalAPIResult(&result);
3656 
3657                     m_parent->sendEvtNotify(CAMERA_MSG_ERROR,
3658                                             CAMERA_ERROR_SERVER_DIED,
3659                                             0);
3660                 }
3661                 break;
3662             case CAM_EVENT_TYPE_CAC_DONE:
3663                 if (m_parent->isCACEnabled() || m_parent->mParameters.isOEMFeatEnabled()) {
3664                     LOGD("[LONG_SHOT_DBG] : Received CAC Done");
3665                     if ((m_parent->isLongshotEnabled())
3666                             && (!m_parent->isCaptureShutterEnabled())) {
3667                         // play shutter sound for longshot
3668                         // after CAC stage is done
3669                         m_parent->playShutter();
3670                     }
3671                     m_parent->mCACDoneReceived = TRUE;
3672                 }
3673                 break;
3674             default:
3675                 LOGE("Invalid internal event %d in state(%d)",
3676                              cam_evt->server_event_type, m_state);
3677                 break;
3678             }
3679         }
3680         break;
3681     case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
3682         {
3683             LOGL("Calling Process Jpeg Notify");
3684             qcamera_jpeg_evt_payload_t *jpeg_job =
3685                 (qcamera_jpeg_evt_payload_t *)payload;
3686             rc = m_parent->processJpegNotify(jpeg_job);
3687         }
3688         break;
3689     case QCAMERA_SM_EVT_SNAPSHOT_DONE:
3690         {
3691             LOGL("Snapshot Done");
3692             if (m_parent->isZSLMode() || m_parent->isLongshotEnabled()) {
3693                 rc = m_parent->cancelPicture();
3694             } else {
3695                 rc = m_parent->cancelLiveSnapshot();
3696             }
3697             m_state = QCAMERA_SM_STATE_PREVIEWING;
3698             if (m_parent->isRetroPicture()){
3699                 result.status = rc;
3700                 result.request_api = evt;
3701                 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3702                 LOGL("\n Signalling for JPEG snapshot done!!");
3703                 m_parent->signalAPIResult(&result);
3704 
3705             }
3706             result.status = rc;
3707             result.request_api = evt;
3708             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3709             m_parent->signalEvtResult(&result);
3710         }
3711         break;
3712     case QCAMERA_SM_EVT_THERMAL_NOTIFY:
3713         {
3714             rc = m_parent->updateThermalLevel(payload);
3715         }
3716         break;
3717     case QCAMERA_SM_EVT_RESTART_STOP_PREVIEW:
3718         {
3719             m_parent->stopPreview();
3720             result.status = rc;
3721             result.request_api = evt;
3722             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3723             m_parent->signalAPIResult(&result);
3724         }
3725        break;
3726     case QCAMERA_SM_EVT_RESTART_START_PREVIEW:
3727         {
3728             rc = m_parent->preparePreview();
3729             if (rc == NO_ERROR) {
3730                 rc = m_parent->startPreview();
3731             }
3732             result.status = rc;
3733             result.request_api = evt;
3734             result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
3735             m_parent->signalAPIResult(&result);
3736         }
3737        break;
3738     default:
3739         LOGW("Cannot handle evt(%d) in state(%d)", evt, m_state);
3740         break;
3741     }
3742 
3743     return rc;
3744 }
3745 
3746 /*===========================================================================
3747  * FUNCTION   : isRecording
3748  *
3749  * DESCRIPTION: check if recording is in process.
3750  *
3751  * PARAMETERS : None
3752  *
3753  * RETURN     : true -- recording
3754  *              false -- not in recording mode
3755  *==========================================================================*/
isRecording()3756 bool QCameraStateMachine::isRecording()
3757 {
3758     switch (m_state) {
3759     case QCAMERA_SM_STATE_RECORDING:
3760     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3761         return true;
3762     default:
3763         return false;
3764     }
3765 }
3766 
3767 /*===========================================================================
3768  * FUNCTION   : isPreviewRunning
3769  *
3770  * DESCRIPTION: check if preview is in process.
3771  *
3772  * PARAMETERS : None
3773  *
3774  * RETURN     : true -- preview running
3775  *              false -- preview stopped
3776  *==========================================================================*/
isPreviewRunning()3777 bool QCameraStateMachine::isPreviewRunning()
3778 {
3779     switch (m_state) {
3780     case QCAMERA_SM_STATE_PREVIEWING:
3781     case QCAMERA_SM_STATE_RECORDING:
3782     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3783     case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3784     case QCAMERA_SM_STATE_PREPARE_SNAPSHOT:
3785     case QCAMERA_SM_STATE_PREVIEW_READY:
3786         return true;
3787     default:
3788         return false;
3789     }
3790 }
3791 
3792 /*===========================================================================
3793  * FUNCTION   : isPreviewReady
3794  *
3795  * DESCRIPTION: check if preview is in ready state.
3796  *
3797  * PARAMETERS : None
3798  *
3799  * RETURN     : true -- preview is in ready state
3800  *              false -- preview is stopped
3801  *==========================================================================*/
isPreviewReady()3802 bool QCameraStateMachine::isPreviewReady()
3803 {
3804     switch (m_state) {
3805     case QCAMERA_SM_STATE_PREVIEW_READY:
3806         return true;
3807     default:
3808         return false;
3809     }
3810 }
3811 
3812 /*===========================================================================
3813  * FUNCTION   : isCaptureRunning
3814  *
3815  * DESCRIPTION: check if image capture is in process.
3816  *
3817  * PARAMETERS : None
3818  *
3819  * RETURN     : true -- capture running
3820  *              false -- capture stopped
3821  *==========================================================================*/
isCaptureRunning()3822 bool QCameraStateMachine::isCaptureRunning()
3823 {
3824     switch (m_state) {
3825     case QCAMERA_SM_STATE_PIC_TAKING:
3826     case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3827     case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3828         return true;
3829     default:
3830         return false;
3831     }
3832 }
3833 /*===========================================================================
3834  * FUNCTION   : isNonZSLCaptureRunning
3835  *
3836  * DESCRIPTION: check if image capture is in process in non ZSL mode.
3837  *
3838  * PARAMETERS : None
3839  *
3840  * RETURN     : true -- capture running in non ZSL mode
3841  *              false -- Either in not capture mode or captur is not in non ZSL mode
3842  *==========================================================================*/
isNonZSLCaptureRunning()3843 bool QCameraStateMachine::isNonZSLCaptureRunning()
3844 {
3845     switch (m_state) {
3846     case QCAMERA_SM_STATE_PIC_TAKING:
3847         return true;
3848     default:
3849         return false;
3850     }
3851 }
3852 
3853 /*===========================================================================
3854  * FUNCTION   : dump
3855  *
3856  * DESCRIPTION: Composes a string based on current configuration
3857  *
3858  * PARAMETERS : none
3859  *
3860  * RETURN     : Formatted string
3861  *==========================================================================*/
dump()3862 String8 QCameraStateMachine::dump()
3863 {
3864     String8 str("\n");
3865     char s[128];
3866 
3867     snprintf(s, 128, "Is Preview Running: %d\n", isPreviewRunning());
3868     str += s;
3869 
3870     snprintf(s, 128, "Is Capture Running: %d\n", isCaptureRunning());
3871     str += s;
3872 
3873     snprintf(s, 128, "Is Non ZSL Capture Running: %d\n",
3874         isNonZSLCaptureRunning());
3875     str += s;
3876 
3877     snprintf(s, 128, "Current State: %d \n", m_state);
3878     str += s;
3879 
3880     switch(m_state){
3881         case QCAMERA_SM_STATE_PREVIEW_STOPPED:
3882         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_STOPPED \n");
3883         break;
3884 
3885         case QCAMERA_SM_STATE_PREVIEW_READY:
3886         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_READY \n");
3887         break;
3888 
3889         case QCAMERA_SM_STATE_PREVIEWING:
3890         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEWING \n");
3891         break;
3892 
3893         case QCAMERA_SM_STATE_PREPARE_SNAPSHOT:
3894         snprintf(s, 128, " QCAMERA_SM_STATE_PREPARE_SNAPSHOT \n");
3895         break;
3896 
3897         case QCAMERA_SM_STATE_PIC_TAKING:
3898         snprintf(s, 128, " QCAMERA_SM_STATE_PIC_TAKING \n");
3899         break;
3900 
3901         case QCAMERA_SM_STATE_RECORDING:
3902         snprintf(s, 128, " QCAMERA_SM_STATE_RECORDING \n");
3903         break;
3904 
3905         case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
3906         snprintf(s, 128, " QCAMERA_SM_STATE_VIDEO_PIC_TAKING \n");
3907         break;
3908 
3909         case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
3910         snprintf(s, 128, " QCAMERA_SM_STATE_PREVIEW_PIC_TAKING \n");
3911         break;
3912     }
3913     str += s;
3914 
3915     return str;
3916 }
3917 
3918 }; // namespace qcamera
3919