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