Home | History | Annotate | Download | only in HAL
      1 /* Copyright (c) 2012-2013, 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 #ifndef __QCAMERA_STATEMACHINE_H__
     31 #define __QCAMERA_STATEMACHINE_H__
     32 
     33 #include <pthread.h>
     34 
     35 #include <cam_semaphore.h>
     36 extern "C" {
     37 #include <mm_camera_interface.h>
     38 }
     39 
     40 #include "QCameraQueue.h"
     41 #include "QCameraChannel.h"
     42 
     43 namespace qcamera {
     44 
     45 class QCamera2HardwareInterface;
     46 
     47 typedef enum {
     48     /*******BEGIN OF: API EVT*********/
     49     QCAMERA_SM_EVT_SET_PREVIEW_WINDOW = 1,   // set preview window
     50     QCAMERA_SM_EVT_SET_CALLBACKS,            // set callbacks
     51     QCAMERA_SM_EVT_ENABLE_MSG_TYPE,          // enable msg type
     52     QCAMERA_SM_EVT_DISABLE_MSG_TYPE,         // disable msg type
     53     QCAMERA_SM_EVT_MSG_TYPE_ENABLED,         // query certain msg type is enabled
     54 
     55     QCAMERA_SM_EVT_SET_PARAMS,               // set parameters
     56     QCAMERA_SM_EVT_GET_PARAMS,               // get parameters
     57     QCAMERA_SM_EVT_PUT_PARAMS,               // put parameters, release param buf
     58 
     59     QCAMERA_SM_EVT_START_PREVIEW,            // start preview (zsl, camera mode, camcorder mode)
     60     QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW,  // start no display preview (zsl, camera mode, camcorder mode)
     61     QCAMERA_SM_EVT_STOP_PREVIEW,             // stop preview (zsl, camera mode, camcorder mode)
     62     QCAMERA_SM_EVT_PREVIEW_ENABLED,          // query if preview is running
     63 
     64     QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS,   // request to store meta data in video buffers
     65     QCAMERA_SM_EVT_START_RECORDING,          // start recording
     66     QCAMERA_SM_EVT_STOP_RECORDING,           // stop recording
     67     QCAMERA_SM_EVT_RECORDING_ENABLED,        // query if recording is running
     68     QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME,  // release recording frame
     69 
     70     QCAMERA_SM_EVT_PREPARE_SNAPSHOT,         // prepare snapshot in case LED needs to be flashed
     71     QCAMERA_SM_EVT_TAKE_PICTURE,             // take picutre (zsl, regualr capture, live snapshot
     72     QCAMERA_SM_EVT_CANCEL_PICTURE,           // cancel picture
     73 
     74     QCAMERA_SM_EVT_START_AUTO_FOCUS,         // start auto focus
     75     QCAMERA_SM_EVT_STOP_AUTO_FOCUS,          // stop auto focus
     76     QCAMERA_SM_EVT_SEND_COMMAND,             // send command
     77 
     78     QCAMERA_SM_EVT_RELEASE,                  // release camera resource
     79     QCAMERA_SM_EVT_DUMP,                     // dump
     80     QCAMERA_SM_EVT_REG_FACE_IMAGE,           // register a face image in imaging lib
     81     /*******END OF: API EVT*********/
     82 
     83     QCAMERA_SM_EVT_EVT_INTERNAL,             // internal evt notify
     84     QCAMERA_SM_EVT_EVT_NOTIFY,               // evt notify from server
     85     QCAMERA_SM_EVT_JPEG_EVT_NOTIFY,          // evt notify from jpeg
     86     QCAMERA_SM_EVT_SNAPSHOT_DONE,            // internal evt that snapshot is done
     87     QCAMERA_SM_EVT_THERMAL_NOTIFY,           // evt notify from thermal daemon
     88     QCAMERA_SM_EVT_MAX
     89 } qcamera_sm_evt_enum_t;
     90 
     91 typedef enum {
     92     QCAMERA_API_RESULT_TYPE_DEF,             // default type, no additional info
     93     QCAMERA_API_RESULT_TYPE_ENABLE_FLAG,     // msg_enabled, preview_enabled, recording_enabled
     94     QCAMERA_API_RESULT_TYPE_PARAMS,          // returned parameters in string
     95     QCAMERA_API_RESULT_TYPE_HANDLE,          // returned handle in int
     96     QCAMERA_API_RESULT_TYPE_MAX
     97 } qcamera_api_result_type_t;
     98 
     99 typedef struct {
    100     int32_t status;                          // api call status
    101     qcamera_sm_evt_enum_t request_api;       // api evt requested
    102     qcamera_api_result_type_t result_type;   // result type
    103     union {
    104         int enabled;                          // result_type == QCAMERA_API_RESULT_TYPE_ENABLE_FLAG
    105         char *params;                         // result_type == QCAMERA_API_RESULT_TYPE_PARAMS
    106         int handle;                           // result_type ==QCAMERA_API_RESULT_TYPE_HANDLE
    107     };
    108 } qcamera_api_result_t;
    109 
    110 // definition for payload type of setting callback
    111 typedef struct {
    112     camera_notify_callback notify_cb;
    113     camera_data_callback data_cb;
    114     camera_data_timestamp_callback data_cb_timestamp;
    115     camera_request_memory get_memory;
    116     void *user;
    117 } qcamera_sm_evt_setcb_payload_t;
    118 
    119 // definition for payload type of sending command
    120 typedef struct {
    121     int32_t cmd;
    122     int32_t arg1;
    123     int32_t arg2;
    124 } qcamera_sm_evt_command_payload_t;
    125 
    126 // definition for payload type of sending command
    127 typedef struct {
    128     void *img_ptr;
    129     cam_pp_offline_src_config_t *config;
    130 } qcamera_sm_evt_reg_face_payload_t;
    131 
    132 typedef enum {
    133     QCAMERA_INTERNAL_EVT_FOCUS_UPDATE,       // focus updating result
    134     QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE, // prepare snapshot done
    135     QCAMERA_INTERNAL_EVT_MAX
    136 } qcamera_internal_evt_type_t;
    137 
    138 typedef struct {
    139     qcamera_internal_evt_type_t evt_type;
    140     union {
    141         cam_auto_focus_data_t focus_data;
    142         cam_prep_snapshot_state_t prep_snapshot_state;
    143     };
    144 } qcamera_sm_internal_evt_payload_t;
    145 
    146 class QCameraStateMachine
    147 {
    148 public:
    149     QCameraStateMachine(QCamera2HardwareInterface *ctrl);
    150     virtual ~QCameraStateMachine();
    151     int32_t procAPI(qcamera_sm_evt_enum_t evt, void *api_payload);
    152     int32_t procEvt(qcamera_sm_evt_enum_t evt, void *evt_payload);
    153 
    154     bool isPreviewRunning(); // check if preview is running
    155 
    156 private:
    157     typedef enum {
    158         QCAMERA_SM_STATE_PREVIEW_STOPPED,          // preview is stopped
    159         QCAMERA_SM_STATE_PREVIEW_READY,            // preview started but preview window is not set yet
    160         QCAMERA_SM_STATE_PREVIEWING,               // previewing
    161         QCAMERA_SM_STATE_PREPARE_SNAPSHOT,         // prepare snapshot in case aec estimation is
    162                                                    // needed for LED flash
    163         QCAMERA_SM_STATE_PIC_TAKING,               // taking picture (preview stopped)
    164         QCAMERA_SM_STATE_RECORDING,                // recording (preview running)
    165         QCAMERA_SM_STATE_VIDEO_PIC_TAKING,         // taking live snapshot during recording (preview running)
    166         QCAMERA_SM_STATE_PREVIEW_PIC_TAKING        // taking ZSL/live snapshot (recording stopped but preview running)
    167     } qcamera_state_enum_t;
    168 
    169     typedef enum
    170     {
    171         QCAMERA_SM_CMD_TYPE_API,                   // cmd from API
    172         QCAMERA_SM_CMD_TYPE_EVT,                   // cmd from mm-camera-interface/mm-jpeg-interface event
    173         QCAMERA_SM_CMD_TYPE_EXIT,                  // cmd for exiting statemachine cmdThread
    174         QCAMERA_SM_CMD_TYPE_MAX
    175     } qcamera_sm_cmd_type_t;
    176 
    177     typedef struct {
    178         qcamera_sm_cmd_type_t cmd;                  // cmd type (where it comes from)
    179         qcamera_sm_evt_enum_t evt;                  // event type
    180         void *evt_payload;                          // ptr to payload
    181     } qcamera_sm_cmd_t;
    182 
    183     int32_t stateMachine(qcamera_sm_evt_enum_t evt, void *payload);
    184     int32_t procEvtPreviewStoppedState(qcamera_sm_evt_enum_t evt, void *payload);
    185     int32_t procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt, void *payload);
    186     int32_t procEvtPreviewingState(qcamera_sm_evt_enum_t evt, void *payload);
    187     int32_t procEvtPrepareSnapshotState(qcamera_sm_evt_enum_t evt, void *payload);
    188     int32_t procEvtPicTakingState(qcamera_sm_evt_enum_t evt, void *payload);
    189     int32_t procEvtRecordingState(qcamera_sm_evt_enum_t evt, void *payload);
    190     int32_t procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt, void *payload);
    191     int32_t procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt, void *payload);
    192 
    193     // main statemachine process routine
    194     static void *smEvtProcRoutine(void *data);
    195 
    196     QCamera2HardwareInterface *m_parent;  // ptr to HWI
    197     qcamera_state_enum_t m_state;         // statemachine state
    198     QCameraQueue api_queue;               // cmd queue for APIs
    199     QCameraQueue evt_queue;               // cmd queue for evt from mm-camera-intf/mm-jpeg-intf
    200     pthread_t cmd_pid;                    // cmd thread ID
    201     cam_semaphore_t cmd_sem;              // semaphore for cmd thread
    202 
    203 };
    204 
    205 }; // namespace qcamera
    206 
    207 #endif /* __QCAMERA_STATEMACHINE_H__ */
    208