blob: 5773fcd96b69d1e11507b89633d62b66a2f8a27b [file] [log] [blame]
Muhua Li1612f422013-01-03 11:07:39 -08001/* Copyright (c) 2012-2013, The Linux Foundataion. All rights reserved.
Muhua Libc9a8082012-11-07 15:51:28 -08002*
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/Log.h>
33#include <utils/Errors.h>
34#include "QCamera2HWI.h"
35#include "QCameraStateMachine.h"
36
Shuzhen Wang89635cf2012-12-20 13:47:22 -080037namespace qcamera {
Muhua Libc9a8082012-11-07 15:51:28 -080038
Muhua Lida2c4be2012-11-26 09:14:16 -080039/*===========================================================================
40 * FUNCTION : smEvtProcRoutine
41 *
42 * DESCRIPTION: Statemachine process thread routine to handle events
43 * in different state.
44 *
45 * PARAMETERS :
46 * @data : ptr to QCameraStateMachine object
47 *
48 * RETURN : none
49 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -080050void *QCameraStateMachine::smEvtProcRoutine(void *data)
51{
52 int running = 1, ret;
53 QCameraStateMachine *pme = (QCameraStateMachine *)data;
54
55 ALOGD("%s: E", __func__);
56 do {
57 do {
Shuzhen Wang2da44612012-12-20 10:51:57 -080058 ret = cam_sem_wait(&pme->cmd_sem);
Muhua Libc9a8082012-11-07 15:51:28 -080059 if (ret != 0 && errno != EINVAL) {
Shuzhen Wang2da44612012-12-20 10:51:57 -080060 ALOGE("%s: cam_sem_wait error (%s)",
Muhua Libc9a8082012-11-07 15:51:28 -080061 __func__, strerror(errno));
62 return NULL;
63 }
64 } while (ret != 0);
65
66 // we got notified about new cmd avail in cmd queue
67 // first check API cmd queue
68 qcamera_sm_cmd_t *node = (qcamera_sm_cmd_t *)pme->api_queue.dequeue();
69 if (node == NULL) {
70 // no API cmd, then check evt cmd queue
71 node = (qcamera_sm_cmd_t *)pme->evt_queue.dequeue();
72 }
73 if (node != NULL) {
74 switch (node->cmd) {
75 case QCAMERA_SM_CMD_TYPE_API:
Muhua Li31eaee02012-12-11 08:56:45 -080076 pme->stateMachine(node->evt, node->evt_payload);
77 // API is in a way sync call, so evt_payload is managed by HWI
78 // no need to free payload for API
79 break;
Muhua Libc9a8082012-11-07 15:51:28 -080080 case QCAMERA_SM_CMD_TYPE_EVT:
81 pme->stateMachine(node->evt, node->evt_payload);
Muhua Li31eaee02012-12-11 08:56:45 -080082
83 // EVT is async call, so payload need to be free after use
84 free(node->evt_payload);
85 node->evt_payload = NULL;
Muhua Libc9a8082012-11-07 15:51:28 -080086 break;
87 case QCAMERA_SM_CMD_TYPE_EXIT:
88 running = 0;
89 break;
90 default:
91 break;
92 }
Muhua Li31eaee02012-12-11 08:56:45 -080093 free(node);
94 node = NULL;
Muhua Libc9a8082012-11-07 15:51:28 -080095 }
96 } while (running);
97 ALOGD("%s: X", __func__);
98 return NULL;
99}
100
Muhua Lida2c4be2012-11-26 09:14:16 -0800101/*===========================================================================
102 * FUNCTION : QCameraStateMachine
103 *
104 * DESCRIPTION: constructor of QCameraStateMachine. Will start process thread
105 *
106 * PARAMETERS :
107 * @ctrl : ptr to HWI object
108 *
109 * RETURN : none
110 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800111QCameraStateMachine::QCameraStateMachine(QCamera2HardwareInterface *ctrl) :
112 api_queue(),
113 evt_queue()
114{
115 m_parent = ctrl;
116 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
117 cmd_pid = 0;
Shuzhen Wang2da44612012-12-20 10:51:57 -0800118 cam_sem_init(&cmd_sem, 0);
Muhua Libc9a8082012-11-07 15:51:28 -0800119 pthread_create(&cmd_pid,
120 NULL,
121 smEvtProcRoutine,
122 this);
123}
124
Muhua Lida2c4be2012-11-26 09:14:16 -0800125/*===========================================================================
126 * FUNCTION : ~QCameraStateMachine
127 *
128 * DESCRIPTION: desctructor of QCameraStateMachine. Will stop process thread.
129 *
130 * PARAMETERS : none
131 *
132 * RETURN : none
133 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800134QCameraStateMachine::~QCameraStateMachine()
135{
136 if (cmd_pid != 0) {
137 qcamera_sm_cmd_t *node =
138 (qcamera_sm_cmd_t *)malloc(sizeof(qcamera_sm_cmd_t));
139 if (NULL != node) {
140 memset(node, 0, sizeof(qcamera_sm_cmd_t));
141 node->cmd = QCAMERA_SM_CMD_TYPE_EXIT;
142
143 api_queue.enqueue((void *)node);
Shuzhen Wang2da44612012-12-20 10:51:57 -0800144 cam_sem_post(&cmd_sem);
Muhua Libc9a8082012-11-07 15:51:28 -0800145
146 /* wait until cmd thread exits */
147 if (pthread_join(cmd_pid, NULL) != 0) {
148 ALOGD("%s: pthread dead already\n", __func__);
149 }
150 }
151 cmd_pid = 0;
152 }
Shuzhen Wang2da44612012-12-20 10:51:57 -0800153 cam_sem_destroy(&cmd_sem);
Muhua Libc9a8082012-11-07 15:51:28 -0800154}
155
Muhua Lida2c4be2012-11-26 09:14:16 -0800156/*===========================================================================
157 * FUNCTION : procAPI
158 *
159 * DESCRIPTION: process incoming API request from framework layer.
160 *
161 * PARAMETERS :
162 * @evt : event to be processed
163 * @api_payload : API payload. Can be NULL if not needed.
164 *
165 * RETURN : int32_t type of status
166 * NO_ERROR -- success
167 * none-zero failure code
168 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800169int32_t QCameraStateMachine::procAPI(qcamera_sm_evt_enum_t evt,
170 void *api_payload)
171{
172 qcamera_sm_cmd_t *node =
173 (qcamera_sm_cmd_t *)malloc(sizeof(qcamera_sm_cmd_t));
Muhua Lida2c4be2012-11-26 09:14:16 -0800174 if (NULL == node) {
175 ALOGE("%s: No memory for qcamera_sm_cmd_t", __func__);
176 return NO_MEMORY;
Muhua Libc9a8082012-11-07 15:51:28 -0800177 }
Muhua Lida2c4be2012-11-26 09:14:16 -0800178
179 memset(node, 0, sizeof(qcamera_sm_cmd_t));
180 node->cmd = QCAMERA_SM_CMD_TYPE_API;
181 node->evt = evt;
182 node->evt_payload = api_payload;
183 if (api_queue.enqueue((void *)node)) {
Shuzhen Wang2da44612012-12-20 10:51:57 -0800184 cam_sem_post(&cmd_sem);
Muhua Lida2c4be2012-11-26 09:14:16 -0800185 return NO_ERROR;
186 } else {
187 free(node);
188 return UNKNOWN_ERROR;
189 }
Muhua Libc9a8082012-11-07 15:51:28 -0800190}
191
Muhua Lida2c4be2012-11-26 09:14:16 -0800192/*===========================================================================
193 * FUNCTION : procEvt
194 *
195 * DESCRIPTION: process incoming envent from mm-camera-interface and
196 * mm-jpeg-interface.
197 *
198 * PARAMETERS :
199 * @evt : event to be processed
200 * @evt_payload : event payload. Can be NULL if not needed.
201 *
202 * RETURN : int32_t type of status
203 * NO_ERROR -- success
204 * none-zero failure code
205 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800206int32_t QCameraStateMachine::procEvt(qcamera_sm_evt_enum_t evt,
207 void *evt_payload)
208{
209 qcamera_sm_cmd_t *node =
210 (qcamera_sm_cmd_t *)malloc(sizeof(qcamera_sm_cmd_t));
Muhua Lida2c4be2012-11-26 09:14:16 -0800211 if (NULL == node) {
212 ALOGE("%s: No memory for qcamera_sm_cmd_t", __func__);
213 return NO_MEMORY;
Muhua Libc9a8082012-11-07 15:51:28 -0800214 }
Muhua Lida2c4be2012-11-26 09:14:16 -0800215
216 memset(node, 0, sizeof(qcamera_sm_cmd_t));
217 node->cmd = QCAMERA_SM_CMD_TYPE_EVT;
218 node->evt = evt;
219 node->evt_payload = evt_payload;
220 if (evt_queue.enqueue((void *)node)) {
Shuzhen Wang2da44612012-12-20 10:51:57 -0800221 cam_sem_post(&cmd_sem);
Muhua Lida2c4be2012-11-26 09:14:16 -0800222 return NO_ERROR;
223 } else {
224 free(node);
225 return UNKNOWN_ERROR;
226 }
Muhua Libc9a8082012-11-07 15:51:28 -0800227}
228
Muhua Lida2c4be2012-11-26 09:14:16 -0800229/*===========================================================================
230 * FUNCTION : stateMachine
231 *
232 * DESCRIPTION: finite state machine entry function. Depends on state,
233 * incoming event will be handled differently.
234 *
235 * PARAMETERS :
236 * @evt : event to be processed
237 * @payload : event payload. Can be NULL if not needed.
238 *
239 * RETURN : int32_t type of status
240 * NO_ERROR -- success
241 * none-zero failure code
242 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800243int32_t QCameraStateMachine::stateMachine(qcamera_sm_evt_enum_t evt, void *payload)
244{
245 int32_t rc = NO_ERROR;
246 switch (m_state) {
247 case QCAMERA_SM_STATE_PREVIEW_STOPPED:
248 rc = procEvtPreviewStoppedState(evt, payload);
249 break;
250 case QCAMERA_SM_STATE_PREVIEW_READY:
251 rc = procEvtPreviewReadyState(evt, payload);
252 break;
253 case QCAMERA_SM_STATE_PREVIEWING:
254 rc = procEvtPreviewingState(evt, payload);
255 break;
256 case QCAMERA_SM_STATE_PIC_TAKING:
257 rc = procEvtPicTakingState(evt, payload);
258 break;
259 case QCAMERA_SM_STATE_RECORDING:
260 rc = procEvtRecordingState(evt, payload);
261 break;
262 case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
263 rc = procEvtVideoPicTakingState(evt, payload);
264 break;
265 case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
266 rc = procEvtPreviewPicTakingState(evt, payload);
267 break;
268 default:
269 break;
270 }
271
272 return rc;
273}
274
Muhua Lida2c4be2012-11-26 09:14:16 -0800275/*===========================================================================
276 * FUNCTION : procEvtPreviewStoppedState
277 *
278 * DESCRIPTION: finite state machine function to handle event in state of
279 * QCAMERA_SM_STATE_PREVIEW_STOPPED.
280 *
281 * PARAMETERS :
282 * @evt : event to be processed
283 * @payload : event payload. Can be NULL if not needed.
284 *
285 * RETURN : int32_t type of status
286 * NO_ERROR -- success
287 * none-zero failure code
288 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800289int32_t QCameraStateMachine::procEvtPreviewStoppedState(qcamera_sm_evt_enum_t evt,
290 void *payload)
291{
292 int32_t rc = NO_ERROR;
293 qcamera_api_result_t result;
294 memset(&result, 0, sizeof(qcamera_api_result_t));
295
296 switch (evt) {
297 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
298 {
299 rc = m_parent->setPreviewWindow((struct preview_stream_ops *)payload);
300 result.status = rc;
301 result.request_api = evt;
302 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
303 m_parent->signalAPIResult(&result);
304 }
305 break;
306 case QCAMERA_SM_EVT_SET_CALLBACKS:
307 {
308 qcamera_sm_evt_setcb_payload_t *setcbs =
309 (qcamera_sm_evt_setcb_payload_t *)payload;
310 rc = m_parent->setCallBacks(setcbs->notify_cb,
311 setcbs->data_cb,
312 setcbs->data_cb_timestamp,
313 setcbs->get_memory,
314 setcbs->user);
315 result.status = rc;
316 result.request_api = evt;
317 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
318 m_parent->signalAPIResult(&result);
319 }
320 break;
321 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
322 {
323 rc = m_parent->enableMsgType(int32_t(payload));
324 result.status = rc;
325 result.request_api = evt;
326 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
327 m_parent->signalAPIResult(&result);
328 }
329 break;
330 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
331 {
332 rc = m_parent->disableMsgType(int32_t(payload));
333 result.status = rc;
334 result.request_api = evt;
335 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
336 m_parent->signalAPIResult(&result);
337 }
338 break;
339 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
340 {
341 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
342 result.status = rc;
343 result.request_api = evt;
344 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
345 result.enabled = enabled;
346 m_parent->signalAPIResult(&result);
347 }
348 break;
349 case QCAMERA_SM_EVT_SET_PARAMS:
350 {
Muhua Lida2c4be2012-11-26 09:14:16 -0800351 bool needRestart = false;
352 rc = m_parent->updateParameters((char*)payload, needRestart);
353 if (rc == NO_ERROR) {
354 rc = m_parent->commitParameterChanges();
355 }
Muhua Libc9a8082012-11-07 15:51:28 -0800356 result.status = rc;
357 result.request_api = evt;
358 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
359 m_parent->signalAPIResult(&result);
360 }
361 break;
362 case QCAMERA_SM_EVT_GET_PARAMS:
363 {
364 result.params = m_parent->getParameters();
365 rc = NO_ERROR;
366 result.status = rc;
367 result.request_api = evt;
368 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
369 m_parent->signalAPIResult(&result);
370 }
371 break;
372 case QCAMERA_SM_EVT_PUT_PARAMS:
373 {
374 rc = m_parent->putParameters((char*)payload);
375 result.status = rc;
376 result.request_api = evt;
377 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
378 m_parent->signalAPIResult(&result);
379 }
380 break;
381 case QCAMERA_SM_EVT_START_PREVIEW:
382 {
383 if (m_parent->mPreviewWindow == NULL) {
Muhua Lida2c4be2012-11-26 09:14:16 -0800384 // preview window is not set yet, move to previewReady state
Muhua Libc9a8082012-11-07 15:51:28 -0800385 m_state = QCAMERA_SM_STATE_PREVIEW_READY;
386 rc = NO_ERROR;
387 } else {
388 rc = m_parent->preparePreview();
Muhua Lida2c4be2012-11-26 09:14:16 -0800389 if (rc == NO_ERROR) {
Muhua Libc9a8082012-11-07 15:51:28 -0800390 rc = m_parent->startPreview();
Muhua Lida2c4be2012-11-26 09:14:16 -0800391 if (rc != NO_ERROR) {
Muhua Libc9a8082012-11-07 15:51:28 -0800392 m_parent->unpreparePreview();
393 } else {
Muhua Lida2c4be2012-11-26 09:14:16 -0800394 // start preview success, move to previewing state
Muhua Libc9a8082012-11-07 15:51:28 -0800395 m_state = QCAMERA_SM_STATE_PREVIEWING;
396 }
397 }
398 }
399 result.status = rc;
400 result.request_api = evt;
401 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
402 m_parent->signalAPIResult(&result);
403 }
404 break;
405 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
406 {
407 rc = m_parent->preparePreview();
408 if (rc == NO_ERROR) {
409 rc = m_parent->startPreview();
410 if (rc != NO_ERROR) {
411 m_parent->unpreparePreview();
412 } else {
413 m_state = QCAMERA_SM_STATE_PREVIEWING;
414 }
415 }
416 result.status = rc;
417 result.request_api = evt;
418 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
419 m_parent->signalAPIResult(&result);
420 }
421 break;
422 case QCAMERA_SM_EVT_STOP_PREVIEW:
423 {
424 // no op needed here
425 ALOGD("%s: already in preview stopped state, do nothing", __func__);
426 result.status = NO_ERROR;
427 result.request_api = evt;
428 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
429 m_parent->signalAPIResult(&result);
430 }
431 break;
432 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
433 case QCAMERA_SM_EVT_RECORDING_ENABLED:
434 {
435 result.status = NO_ERROR;
436 result.request_api = evt;
437 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
438 result.enabled = 0;
439 m_parent->signalAPIResult(&result);
440 }
441 break;
442 case QCAMERA_SM_EVT_RELEASE:
443 {
444 rc = m_parent->release();
445 result.status = rc;
446 result.request_api = evt;
447 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
448 m_parent->signalAPIResult(&result);
449 }
450 break;
451 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
452 {
453 rc = m_parent->storeMetaDataInBuffers(int(payload));
454 result.status = rc;
455 result.request_api = evt;
456 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
457 m_parent->signalAPIResult(&result);
458 }
459 break;
460 case QCAMERA_SM_EVT_DUMP:
461 {
462 rc = m_parent->dump((int)payload);
463 result.status = rc;
464 result.request_api = evt;
465 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
466 m_parent->signalAPIResult(&result);
467 }
468 break;
Ankit Premrajka3b00dc62012-12-14 18:37:52 -0800469 case QCAMERA_SM_EVT_SEND_COMMAND:
470 {
471 qcamera_sm_evt_command_payload_t *cmd_payload =
472 (qcamera_sm_evt_command_payload_t *)payload;
473 rc = m_parent->sendCommand(cmd_payload->cmd,
474 cmd_payload->arg1,
475 cmd_payload->arg2);
476 result.status = rc;
477 result.request_api = evt;
478 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
479 m_parent->signalAPIResult(&result);
480 }
481 break;
Muhua Libc9a8082012-11-07 15:51:28 -0800482 case QCAMERA_SM_EVT_START_RECORDING:
483 case QCAMERA_SM_EVT_STOP_RECORDING:
484 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
485 case QCAMERA_SM_EVT_TAKE_PICTURE:
Muhua Libc9a8082012-11-07 15:51:28 -0800486 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
Muhua Libc9a8082012-11-07 15:51:28 -0800487 {
488 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
489 rc = INVALID_OPERATION;
490 result.status = rc;
491 result.request_api = evt;
492 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
493 m_parent->signalAPIResult(&result);
494 }
495 break;
Muhua Li1612f422013-01-03 11:07:39 -0800496 case QCAMERA_SM_EVT_CANCEL_PICTURE:
497 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
498 {
499 // no op needed here
500 ALOGD("%s: No ops for evt(%d) in state(%d)", __func__, evt, m_state);
501 result.status = NO_ERROR;
502 result.request_api = evt;
503 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
504 m_parent->signalAPIResult(&result);
505 }
506 break;
Muhua Li31eaee02012-12-11 08:56:45 -0800507 case QCAMERA_SM_EVT_EVT_INTERNAL:
Muhua Libc9a8082012-11-07 15:51:28 -0800508 case QCAMERA_SM_EVT_EVT_NOTIFY:
509 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -0800510 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Shuzhen Wangaa829ce2013-01-09 14:41:49 -0800511 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
Muhua Libc9a8082012-11-07 15:51:28 -0800512 default:
513 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
514 break;
515 }
516
517 return rc;
518}
519
Muhua Lida2c4be2012-11-26 09:14:16 -0800520/*===========================================================================
521 * FUNCTION : procEvtPreviewReadyState
522 *
523 * DESCRIPTION: finite state machine function to handle event in state of
524 * QCAMERA_SM_STATE_PREVIEW_READY.
525 *
526 * PARAMETERS :
527 * @evt : event to be processed
528 * @payload : event payload. Can be NULL if not needed.
529 *
530 * RETURN : int32_t type of status
531 * NO_ERROR -- success
532 * none-zero failure code
533 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800534int32_t QCameraStateMachine::procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt,
535 void *payload)
536{
537 int32_t rc = NO_ERROR;
538 qcamera_api_result_t result;
539 memset(&result, 0, sizeof(qcamera_api_result_t));
540
541 switch (evt) {
542 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
543 {
544 m_parent->setPreviewWindow((struct preview_stream_ops *)payload);
545 if (m_parent->mPreviewWindow != NULL) {
546 rc = m_parent->startPreview();
547 if (rc != NO_ERROR) {
548 m_parent->unpreparePreview();
549 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
550 } else {
551 m_state = QCAMERA_SM_STATE_PREVIEWING;
552 }
553 }
554
555 result.status = rc;
556 result.request_api = evt;
557 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
558 m_parent->signalAPIResult(&result);
559 }
560 break;
561 case QCAMERA_SM_EVT_SET_CALLBACKS:
562 {
563 qcamera_sm_evt_setcb_payload_t *setcbs =
564 (qcamera_sm_evt_setcb_payload_t *)payload;
565 rc = m_parent->setCallBacks(setcbs->notify_cb,
566 setcbs->data_cb,
567 setcbs->data_cb_timestamp,
568 setcbs->get_memory,
569 setcbs->user);
570 result.status = rc;
571 result.request_api = evt;
572 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
573 m_parent->signalAPIResult(&result);
574 }
575 break;
576 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
577 {
578 rc = m_parent->enableMsgType(int32_t(payload));
579 result.status = rc;
580 result.request_api = evt;
581 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
582 m_parent->signalAPIResult(&result);
583 }
584 break;
585 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
586 {
587 rc = m_parent->disableMsgType(int32_t(payload));
588 result.status = rc;
589 result.request_api = evt;
590 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
591 m_parent->signalAPIResult(&result);
592 }
593 break;
594 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
595 {
596 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
597 result.status = rc;
598 result.request_api = evt;
599 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
600 result.enabled = enabled;
601 m_parent->signalAPIResult(&result);
602 }
603 break;
604 case QCAMERA_SM_EVT_SET_PARAMS:
605 {
Muhua Lida2c4be2012-11-26 09:14:16 -0800606 bool needRestart = false;
607 rc = m_parent->updateParameters((char*)payload, needRestart);
608 if (rc == NO_ERROR) {
609 rc = m_parent->commitParameterChanges();
610 }
Muhua Libc9a8082012-11-07 15:51:28 -0800611 result.status = rc;
612 result.request_api = evt;
613 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
614 m_parent->signalAPIResult(&result);
615 }
616 break;
617 case QCAMERA_SM_EVT_GET_PARAMS:
618 {
619 result.params = m_parent->getParameters();
620 rc = NO_ERROR;
621 result.status = rc;
622 result.request_api = evt;
623 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
624 m_parent->signalAPIResult(&result);
625 }
626 break;
627 case QCAMERA_SM_EVT_PUT_PARAMS:
628 {
629 rc = m_parent->putParameters((char*)payload);
630 result.status = rc;
631 result.request_api = evt;
632 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
633 m_parent->signalAPIResult(&result);
634 }
635 break;
636 case QCAMERA_SM_EVT_START_PREVIEW:
637 {
638 // no ops here
639 rc = NO_ERROR;
640 result.status = rc;
641 result.request_api = evt;
642 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
643 m_parent->signalAPIResult(&result);
644 }
645 break;
646 case QCAMERA_SM_EVT_STOP_PREVIEW:
647 {
648 m_parent->unpreparePreview();
649 rc = 0;
650 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
651 result.status = rc;
652 result.request_api = evt;
653 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
654 m_parent->signalAPIResult(&result);
655 }
656 break;
657 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
658 {
659 rc = NO_ERROR;
660 result.status = rc;
661 result.request_api = evt;
662 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
663 result.enabled = 1;
664 m_parent->signalAPIResult(&result);
665 }
666 break;
667 case QCAMERA_SM_EVT_RECORDING_ENABLED:
668 {
669 rc = 0;
670 result.status = rc;
671 result.request_api = evt;
672 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
673 result.enabled = 0;
674 m_parent->signalAPIResult(&result);
675 }
676 break;
677 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
678 {
679 rc = m_parent->storeMetaDataInBuffers(int(payload));
680 result.status = rc;
681 result.request_api = evt;
682 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
683 m_parent->signalAPIResult(&result);
684 }
685 break;
686 case QCAMERA_SM_EVT_DUMP:
687 {
688 rc = m_parent->dump((int)payload);
689 result.status = rc;
690 result.request_api = evt;
691 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
692 m_parent->signalAPIResult(&result);
693 }
694 break;
695 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
696 {
697 rc = m_parent->autoFocus();
698 result.status = rc;
699 result.request_api = evt;
700 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
701 m_parent->signalAPIResult(&result);
702 }
703 break;
704 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
705 {
706 rc = m_parent->cancelAutoFocus();
707 result.status = rc;
708 result.request_api = evt;
709 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
710 m_parent->signalAPIResult(&result);
711 }
712 break;
713 case QCAMERA_SM_EVT_SEND_COMMAND:
714 {
715 qcamera_sm_evt_command_payload_t *cmd_payload =
716 (qcamera_sm_evt_command_payload_t *)payload;
717 rc = m_parent->sendCommand(cmd_payload->cmd,
718 cmd_payload->arg1,
719 cmd_payload->arg2);
720 result.status = rc;
721 result.request_api = evt;
722 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
723 m_parent->signalAPIResult(&result);
724 }
725 break;
726 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
727 case QCAMERA_SM_EVT_START_RECORDING:
728 case QCAMERA_SM_EVT_STOP_RECORDING:
729 case QCAMERA_SM_EVT_TAKE_PICTURE:
730 case QCAMERA_SM_EVT_CANCEL_PICTURE:
731 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
732 case QCAMERA_SM_EVT_RELEASE:
733 {
734 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
735 rc = INVALID_OPERATION;
736 result.status = rc;
737 result.request_api = evt;
738 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
739 m_parent->signalAPIResult(&result);
740 }
741 break;
Muhua Li31eaee02012-12-11 08:56:45 -0800742 case QCAMERA_SM_EVT_EVT_INTERNAL:
Muhua Libc9a8082012-11-07 15:51:28 -0800743 case QCAMERA_SM_EVT_EVT_NOTIFY:
744 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -0800745 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Shuzhen Wangaa829ce2013-01-09 14:41:49 -0800746 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
Muhua Libc9a8082012-11-07 15:51:28 -0800747 default:
748 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
749 break;
750 }
751
752 return rc;
753}
754
Muhua Lida2c4be2012-11-26 09:14:16 -0800755/*===========================================================================
756 * FUNCTION : procEvtPreviewingState
757 *
758 * DESCRIPTION: finite state machine function to handle event in state of
759 * QCAMERA_SM_STATE_PREVIEWING.
760 *
761 * PARAMETERS :
762 * @evt : event to be processed
763 * @payload : event payload. Can be NULL if not needed.
764 *
765 * RETURN : int32_t type of status
766 * NO_ERROR -- success
767 * none-zero failure code
768 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800769int32_t QCameraStateMachine::procEvtPreviewingState(qcamera_sm_evt_enum_t evt,
770 void *payload)
771{
772 int32_t rc = NO_ERROR;
773 qcamera_api_result_t result;
774 memset(&result, 0, sizeof(qcamera_api_result_t));
775
776 switch (evt) {
777 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
778 {
779 // Error setting preview window during previewing
780 ALOGE("Cannot set preview window when preview is running");
781 rc = INVALID_OPERATION;
782 result.status = rc;
783 result.request_api = evt;
784 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
785 m_parent->signalAPIResult(&result);
786 }
787 break;
788 case QCAMERA_SM_EVT_SET_CALLBACKS:
789 {
790 qcamera_sm_evt_setcb_payload_t *setcbs =
791 (qcamera_sm_evt_setcb_payload_t *)payload;
792 rc = m_parent->setCallBacks(setcbs->notify_cb,
793 setcbs->data_cb,
794 setcbs->data_cb_timestamp,
795 setcbs->get_memory,
796 setcbs->user);
797 result.status = rc;
798 result.request_api = evt;
799 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
800 m_parent->signalAPIResult(&result);
801 }
802 break;
803 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
804 {
805 rc = m_parent->enableMsgType(int32_t(payload));
806 result.status = rc;
807 result.request_api = evt;
808 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
809 m_parent->signalAPIResult(&result);
810 }
811 break;
812 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
813 {
814 rc = m_parent->disableMsgType(int32_t(payload));
815 result.status = rc;
816 result.request_api = evt;
817 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
818 m_parent->signalAPIResult(&result);
819 }
820 break;
821 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
822 {
823 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
824 result.status = rc;
825 result.request_api = evt;
826 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
827 result.enabled = enabled;
828 m_parent->signalAPIResult(&result);
829 }
830 break;
831 case QCAMERA_SM_EVT_SET_PARAMS:
832 {
Muhua Lida2c4be2012-11-26 09:14:16 -0800833 bool needRestart = false;
834 rc = m_parent->updateParameters((char*)payload, needRestart);
835 if (rc == NO_ERROR) {
836 if (needRestart) {
837 // need restart preview for parameters to take effect
838 // stop preview
839 m_parent->stopPreview();
840 // commit parameter changes to server
841 rc = m_parent->commitParameterChanges();
842 // start preview again
843 m_parent->startPreview();
844 } else {
845 rc = m_parent->commitParameterChanges();
846 }
847 }
Muhua Libc9a8082012-11-07 15:51:28 -0800848 result.status = rc;
849 result.request_api = evt;
850 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
851 m_parent->signalAPIResult(&result);
852 }
853 break;
854 case QCAMERA_SM_EVT_GET_PARAMS:
855 {
856 result.params = m_parent->getParameters();
857 rc = NO_ERROR;
858 result.status = rc;
859 result.request_api = evt;
860 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
861 m_parent->signalAPIResult(&result);
862 }
863 break;
864 case QCAMERA_SM_EVT_PUT_PARAMS:
865 {
866 rc = m_parent->putParameters((char*)payload);
867 result.status = rc;
868 result.request_api = evt;
869 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
870 m_parent->signalAPIResult(&result);
871 }
872 break;
873 case QCAMERA_SM_EVT_START_PREVIEW:
874 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
875 {
876 // no ops here
877 ALOGD("%s: Already in previewing, no ops here to start preview", __func__);
878 rc = NO_ERROR;
879 result.status = rc;
880 result.request_api = evt;
881 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
882 m_parent->signalAPIResult(&result);
883 }
884 break;
885 case QCAMERA_SM_EVT_STOP_PREVIEW:
886 {
887 rc = m_parent->stopPreview();
888 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
889 result.status = rc;
890 result.request_api = evt;
891 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
892 m_parent->signalAPIResult(&result);
893 }
894 break;
895 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
896 {
897 rc = NO_ERROR;
898 result.status = rc;
899 result.request_api = evt;
900 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
901 result.enabled = 1;
902 m_parent->signalAPIResult(&result);
903 }
904 break;
905 case QCAMERA_SM_EVT_RECORDING_ENABLED:
906 {
907 rc = NO_ERROR;
908 result.status = rc;
909 result.request_api = evt;
910 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
911 result.enabled = 0;
912 m_parent->signalAPIResult(&result);
913 }
914 break;
915 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
916 {
917 rc = m_parent->storeMetaDataInBuffers(int(payload));
918 result.status = rc;
919 result.request_api = evt;
920 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
921 m_parent->signalAPIResult(&result);
922 }
923 break;
924 case QCAMERA_SM_EVT_DUMP:
925 {
926 rc = m_parent->dump((int)payload);
927 result.status = rc;
928 result.request_api = evt;
929 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
930 m_parent->signalAPIResult(&result);
931 }
932 break;
933 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
934 {
935 rc = m_parent->autoFocus();
936 result.status = rc;
937 result.request_api = evt;
938 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
939 m_parent->signalAPIResult(&result);
940 }
941 break;
942 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
943 {
944 rc = m_parent->cancelAutoFocus();
945 result.status = rc;
946 result.request_api = evt;
947 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
948 m_parent->signalAPIResult(&result);
949 }
950 break;
951 case QCAMERA_SM_EVT_START_RECORDING:
952 {
953 rc = m_parent->startRecording();
954 if (rc == NO_ERROR) {
955 // move state to recording state
956 m_state = QCAMERA_SM_STATE_RECORDING;
957 }
958 result.status = rc;
959 result.request_api = evt;
960 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
961 m_parent->signalAPIResult(&result);
962 }
963 break;
964 case QCAMERA_SM_EVT_TAKE_PICTURE:
965 {
966 rc = m_parent->takePicture();
967 if (rc == NO_ERROR) {
968 // move state to picture taking state
969 m_state = QCAMERA_SM_STATE_PIC_TAKING;
970 } else {
971 // move state to preview stopped state
972 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
973 }
974 result.status = rc;
975 result.request_api = evt;
976 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
977 m_parent->signalAPIResult(&result);
978 }
979 break;
980 case QCAMERA_SM_EVT_SEND_COMMAND:
981 {
982 qcamera_sm_evt_command_payload_t *cmd_payload =
983 (qcamera_sm_evt_command_payload_t *)payload;
984 rc = m_parent->sendCommand(cmd_payload->cmd,
985 cmd_payload->arg1,
986 cmd_payload->arg2);
987 result.status = rc;
988 result.request_api = evt;
989 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
990 m_parent->signalAPIResult(&result);
991 }
992 break;
993 case QCAMERA_SM_EVT_CANCEL_PICTURE:
994 case QCAMERA_SM_EVT_STOP_RECORDING:
995 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
996 case QCAMERA_SM_EVT_RELEASE:
997 {
998 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
999 rc = INVALID_OPERATION;
1000 result.status = rc;
1001 result.request_api = evt;
1002 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1003 m_parent->signalAPIResult(&result);
1004 }
1005 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001006 case QCAMERA_SM_EVT_EVT_INTERNAL:
1007 {
1008 qcamera_sm_internal_evt_payload_t *internal_evt =
1009 (qcamera_sm_internal_evt_payload_t *)payload;
1010 switch (internal_evt->evt_type) {
1011 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1012 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1013 break;
1014 default:
1015 break;
1016 }
1017 }
1018 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001019 case QCAMERA_SM_EVT_EVT_NOTIFY:
1020 {
1021 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1022 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001023 case CAM_EVENT_TYPE_ZOOM_DONE:
1024 rc = m_parent->processZoomEvent(cam_evt->status);
1025 break;
1026 default:
1027 ALOGD("%s: no handling for server evt (%d) at this state",
1028 __func__, cam_evt->server_event_type);
1029 break;
1030 }
1031 }
1032 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001033 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1034 //TODO: Adjust FPS.
1035 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001036 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -08001037 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Muhua Libc9a8082012-11-07 15:51:28 -08001038 default:
1039 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1040 break;
1041 }
1042
1043 return rc;
1044}
1045
Muhua Lida2c4be2012-11-26 09:14:16 -08001046/*===========================================================================
1047 * FUNCTION : procEvtPicTakingState
1048 *
1049 * DESCRIPTION: finite state machine function to handle event in state of
1050 * QCAMERA_SM_STATE_PIC_TAKING.
1051 *
1052 * PARAMETERS :
1053 * @evt : event to be processed
1054 * @payload : event payload. Can be NULL if not needed.
1055 *
1056 * RETURN : int32_t type of status
1057 * NO_ERROR -- success
1058 * none-zero failure code
1059 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001060int32_t QCameraStateMachine::procEvtPicTakingState(qcamera_sm_evt_enum_t evt,
1061 void *payload)
1062{
1063 int32_t rc = NO_ERROR;
1064 qcamera_api_result_t result;
1065 memset(&result, 0, sizeof(qcamera_api_result_t));
1066
1067 switch (evt) {
1068 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1069 {
1070 // Error setting preview window during previewing
1071 ALOGE("Cannot set preview window when preview is running");
1072 rc = INVALID_OPERATION;
1073 result.status = rc;
1074 result.request_api = evt;
1075 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1076 m_parent->signalAPIResult(&result);
1077 }
1078 break;
1079 case QCAMERA_SM_EVT_SET_CALLBACKS:
1080 {
1081 qcamera_sm_evt_setcb_payload_t *setcbs =
1082 (qcamera_sm_evt_setcb_payload_t *)payload;
1083 rc = m_parent->setCallBacks(setcbs->notify_cb,
1084 setcbs->data_cb,
1085 setcbs->data_cb_timestamp,
1086 setcbs->get_memory,
1087 setcbs->user);
1088 result.status = rc;
1089 result.request_api = evt;
1090 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1091 m_parent->signalAPIResult(&result);
1092 }
1093 break;
1094 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1095 {
1096 rc = m_parent->enableMsgType(int32_t(payload));
1097 result.status = rc;
1098 result.request_api = evt;
1099 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1100 m_parent->signalAPIResult(&result);
1101 }
1102 break;
1103 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1104 {
1105 rc = m_parent->disableMsgType(int32_t(payload));
1106 result.status = rc;
1107 result.request_api = evt;
1108 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1109 m_parent->signalAPIResult(&result);
1110 }
1111 break;
1112 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1113 {
1114 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1115 result.status = rc;
1116 result.request_api = evt;
1117 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1118 result.enabled = enabled;
1119 m_parent->signalAPIResult(&result);
1120 }
1121 break;
1122 case QCAMERA_SM_EVT_SET_PARAMS:
1123 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001124 bool needRestart = false;
1125 rc = m_parent->updateParameters((char*)payload, needRestart);
1126 if (rc == NO_ERROR) {
1127 rc = m_parent->commitParameterChanges();
1128 }
Muhua Libc9a8082012-11-07 15:51:28 -08001129 result.status = rc;
1130 result.request_api = evt;
1131 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1132 m_parent->signalAPIResult(&result);
1133 }
1134 break;
1135 case QCAMERA_SM_EVT_GET_PARAMS:
1136 {
1137 result.params = m_parent->getParameters();
1138 rc = NO_ERROR;
1139 result.status = rc;
1140 result.request_api = evt;
1141 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1142 m_parent->signalAPIResult(&result);
1143 }
1144 break;
1145 case QCAMERA_SM_EVT_PUT_PARAMS:
1146 {
1147 rc = m_parent->putParameters((char*)payload);
1148 result.status = rc;
1149 result.request_api = evt;
1150 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1151 m_parent->signalAPIResult(&result);
1152 }
1153 break;
1154 case QCAMERA_SM_EVT_STOP_PREVIEW:
1155 {
1156 // no ops, since preview is stopped (normal),
1157 // or preview msg type is disabled (ZSL)
1158 rc = NO_ERROR;
1159 result.status = rc;
1160 result.request_api = evt;
1161 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1162 m_parent->signalAPIResult(&result);
1163 }
1164 break;
1165 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1166 {
1167 rc = NO_ERROR;
1168 result.status = rc;
1169 result.request_api = evt;
1170 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1171 result.enabled = 0;
1172 m_parent->signalAPIResult(&result);
1173 }
1174 break;
1175 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1176 {
1177 rc = NO_ERROR;
1178 result.status = rc;
1179 result.request_api = evt;
1180 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1181 result.enabled = 0;
1182 m_parent->signalAPIResult(&result);
1183 }
1184 break;
1185 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1186 {
1187 rc = m_parent->storeMetaDataInBuffers(int(payload));
1188 result.status = rc;
1189 result.request_api = evt;
1190 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1191 m_parent->signalAPIResult(&result);
1192 }
1193 break;
1194 case QCAMERA_SM_EVT_DUMP:
1195 {
1196 rc = m_parent->dump((int)payload);
1197 result.status = rc;
1198 result.request_api = evt;
1199 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1200 m_parent->signalAPIResult(&result);
1201 }
1202 break;
1203 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1204 {
1205 rc = m_parent->autoFocus();
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_STOP_AUTO_FOCUS:
1213 {
1214 rc = m_parent->cancelAutoFocus();
1215 result.status = rc;
1216 result.request_api = evt;
1217 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1218 m_parent->signalAPIResult(&result);
1219 }
1220 break;
1221 case QCAMERA_SM_EVT_SEND_COMMAND:
1222 {
1223 qcamera_sm_evt_command_payload_t *cmd_payload =
1224 (qcamera_sm_evt_command_payload_t *)payload;
1225 rc = m_parent->sendCommand(cmd_payload->cmd,
1226 cmd_payload->arg1,
1227 cmd_payload->arg2);
1228 result.status = rc;
1229 result.request_api = evt;
1230 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1231 m_parent->signalAPIResult(&result);
1232 }
1233 break;
1234 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1235 {
1236 rc = m_parent->cancelPicture();
1237 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1238 result.status = rc;
1239 result.request_api = evt;
1240 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1241 m_parent->signalAPIResult(&result);
1242 }
1243 break;
1244 case QCAMERA_SM_EVT_TAKE_PICTURE:
1245 case QCAMERA_SM_EVT_START_RECORDING:
1246 case QCAMERA_SM_EVT_STOP_RECORDING:
1247 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1248 case QCAMERA_SM_EVT_START_PREVIEW:
1249 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1250 case QCAMERA_SM_EVT_RELEASE:
1251 {
1252 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1253 rc = INVALID_OPERATION;
1254 result.status = rc;
1255 result.request_api = evt;
1256 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1257 m_parent->signalAPIResult(&result);
1258 }
1259 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001260 case QCAMERA_SM_EVT_EVT_INTERNAL:
1261 {
1262 qcamera_sm_internal_evt_payload_t *internal_evt =
1263 (qcamera_sm_internal_evt_payload_t *)payload;
1264 switch (internal_evt->evt_type) {
1265 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1266 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1267 break;
1268 default:
1269 break;
1270 }
1271 }
1272 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001273 case QCAMERA_SM_EVT_EVT_NOTIFY:
1274 {
1275 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1276 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001277 case CAM_EVENT_TYPE_ZOOM_DONE:
1278 rc = m_parent->processZoomEvent(cam_evt->status);
1279 break;
1280 default:
1281 ALOGD("%s: no handling for server evt (%d) at this state",
1282 __func__, cam_evt->server_event_type);
1283 break;
1284 }
1285 }
1286 break;
1287 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1288 {
1289 qcamera_jpeg_evt_payload_t *jpeg_job =
1290 (qcamera_jpeg_evt_payload_t *)payload;
1291 rc = m_parent->processJpegNotify(jpeg_job);
1292 }
1293 break;
1294 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1295 {
1296 rc = m_parent->cancelPicture();
1297 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1298 }
1299 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001300 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
Muhua Libc9a8082012-11-07 15:51:28 -08001301 default:
1302 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1303 break;
1304 }
1305
1306 return rc;
1307}
1308
Muhua Lida2c4be2012-11-26 09:14:16 -08001309/*===========================================================================
1310 * FUNCTION : procEvtRecordingState
1311 *
1312 * DESCRIPTION: finite state machine function to handle event in state of
1313 * QCAMERA_SM_STATE_RECORDING.
1314 *
1315 * PARAMETERS :
1316 * @evt : event to be processed
1317 * @payload : event payload. Can be NULL if not needed.
1318 *
1319 * RETURN : int32_t type of status
1320 * NO_ERROR -- success
1321 * none-zero failure code
1322 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001323int32_t QCameraStateMachine::procEvtRecordingState(qcamera_sm_evt_enum_t evt,
1324 void *payload)
1325{
1326 int32_t rc = NO_ERROR;
1327 qcamera_api_result_t result;
1328 memset(&result, 0, sizeof(qcamera_api_result_t));
1329
1330 switch (evt) {
1331 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1332 {
1333 // Error setting preview window during previewing
1334 ALOGE("Cannot set preview window when preview is running");
1335 rc = INVALID_OPERATION;
1336 result.status = rc;
1337 result.request_api = evt;
1338 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1339 m_parent->signalAPIResult(&result);
1340 }
1341 break;
1342 case QCAMERA_SM_EVT_SET_CALLBACKS:
1343 {
1344 qcamera_sm_evt_setcb_payload_t *setcbs =
1345 (qcamera_sm_evt_setcb_payload_t *)payload;
1346 rc = m_parent->setCallBacks(setcbs->notify_cb,
1347 setcbs->data_cb,
1348 setcbs->data_cb_timestamp,
1349 setcbs->get_memory,
1350 setcbs->user);
1351 result.status = rc;
1352 result.request_api = evt;
1353 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1354 m_parent->signalAPIResult(&result);
1355 }
1356 break;
1357 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1358 {
1359 rc = m_parent->enableMsgType(int32_t(payload));
1360 result.status = rc;
1361 result.request_api = evt;
1362 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1363 m_parent->signalAPIResult(&result);
1364 }
1365 break;
1366 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1367 {
1368 rc = m_parent->disableMsgType(int32_t(payload));
1369 result.status = rc;
1370 result.request_api = evt;
1371 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1372 m_parent->signalAPIResult(&result);
1373 }
1374 break;
1375 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1376 {
1377 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1378 result.status = rc;
1379 result.request_api = evt;
1380 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1381 result.enabled = enabled;
1382 m_parent->signalAPIResult(&result);
1383 }
1384 break;
1385 case QCAMERA_SM_EVT_SET_PARAMS:
1386 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001387 bool needRestart = false;
1388 rc = m_parent->updateParameters((char*)payload, needRestart);
1389 if (rc == NO_ERROR) {
1390 if (needRestart) {
1391 // cannot set parameters that requires restart during recording
1392 ALOGE("%s: Cannot set parameters that requires restart during recording",
1393 __func__);
1394 rc = BAD_VALUE;
1395 } else {
1396 rc = m_parent->commitParameterChanges();
1397 }
1398 }
Muhua Libc9a8082012-11-07 15:51:28 -08001399 result.status = rc;
1400 result.request_api = evt;
1401 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1402 m_parent->signalAPIResult(&result);
1403 }
1404 break;
1405 case QCAMERA_SM_EVT_GET_PARAMS:
1406 {
1407 result.params = m_parent->getParameters();
1408 rc = NO_ERROR;
1409 result.status = rc;
1410 result.request_api = evt;
1411 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1412 m_parent->signalAPIResult(&result);
1413 }
1414 break;
1415 case QCAMERA_SM_EVT_PUT_PARAMS:
1416 {
1417 rc = m_parent->putParameters((char*)payload);
1418 result.status = rc;
1419 result.request_api = evt;
1420 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1421 m_parent->signalAPIResult(&result);
1422 }
1423 break;
1424 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1425 {
1426 rc = NO_ERROR;
1427 result.status = rc;
1428 result.request_api = evt;
1429 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1430 result.enabled = 0;
1431 m_parent->signalAPIResult(&result);
1432 }
1433 break;
1434 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1435 {
1436 rc = NO_ERROR;
1437 result.status = rc;
1438 result.request_api = evt;
1439 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1440 result.enabled = 1;
1441 m_parent->signalAPIResult(&result);
1442 }
1443 break;
1444 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1445 {
1446 rc = m_parent->storeMetaDataInBuffers(int(payload));
1447 result.status = rc;
1448 result.request_api = evt;
1449 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1450 m_parent->signalAPIResult(&result);
1451 }
1452 break;
1453 case QCAMERA_SM_EVT_DUMP:
1454 {
1455 rc = m_parent->dump((int)payload);
1456 result.status = rc;
1457 result.request_api = evt;
1458 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1459 m_parent->signalAPIResult(&result);
1460 }
1461 break;
1462 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1463 {
1464 rc = m_parent->autoFocus();
1465 result.status = rc;
1466 result.request_api = evt;
1467 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1468 m_parent->signalAPIResult(&result);
1469 }
1470 break;
1471 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1472 {
1473 rc = m_parent->cancelAutoFocus();
1474 result.status = rc;
1475 result.request_api = evt;
1476 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1477 m_parent->signalAPIResult(&result);
1478 }
1479 break;
1480 case QCAMERA_SM_EVT_SEND_COMMAND:
1481 {
1482 qcamera_sm_evt_command_payload_t *cmd_payload =
1483 (qcamera_sm_evt_command_payload_t *)payload;
1484 rc = m_parent->sendCommand(cmd_payload->cmd,
1485 cmd_payload->arg1,
1486 cmd_payload->arg2);
1487 result.status = rc;
1488 result.request_api = evt;
1489 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1490 m_parent->signalAPIResult(&result);
1491 }
1492 break;
1493 case QCAMERA_SM_EVT_TAKE_PICTURE:
1494 {
1495 rc = m_parent->takeLiveSnapshot();
1496 if (rc == 0) {
1497 m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
1498 }
1499 result.status = rc;
1500 result.request_api = evt;
1501 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1502 m_parent->signalAPIResult(&result);
1503 }
1504 break;
1505 case QCAMERA_SM_EVT_START_RECORDING:
1506 {
1507 // no ops here
1508 ALOGD("%s: already in recording state, no ops for start_recording", __func__);
1509 rc = 0;
1510 result.status = rc;
1511 result.request_api = evt;
1512 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1513 m_parent->signalAPIResult(&result);
1514 }
1515 break;
1516 case QCAMERA_SM_EVT_STOP_RECORDING:
1517 {
1518 rc = m_parent->stopRecording();
1519 m_state = QCAMERA_SM_STATE_PREVIEWING;
1520 result.status = rc;
1521 result.request_api = evt;
1522 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1523 m_parent->signalAPIResult(&result);
1524 }
1525 break;
1526 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1527 {
1528 rc = m_parent->releaseRecordingFrame((const void *)payload);
1529 result.status = rc;
1530 result.request_api = evt;
1531 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1532 m_parent->signalAPIResult(&result);
1533 }
1534 break;
1535 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1536 case QCAMERA_SM_EVT_START_PREVIEW:
1537 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1538 case QCAMERA_SM_EVT_STOP_PREVIEW:
1539 case QCAMERA_SM_EVT_RELEASE:
1540 {
1541 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1542 rc = INVALID_OPERATION;
1543 result.status = rc;
1544 result.request_api = evt;
1545 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1546 m_parent->signalAPIResult(&result);
1547 }
1548 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001549 case QCAMERA_SM_EVT_EVT_INTERNAL:
1550 {
1551 qcamera_sm_internal_evt_payload_t *internal_evt =
1552 (qcamera_sm_internal_evt_payload_t *)payload;
1553 switch (internal_evt->evt_type) {
1554 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1555 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1556 break;
1557 default:
1558 break;
1559 }
1560 }
1561 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001562 case QCAMERA_SM_EVT_EVT_NOTIFY:
1563 {
1564 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1565 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001566 case CAM_EVENT_TYPE_ZOOM_DONE:
1567 rc = m_parent->processZoomEvent(cam_evt->status);
1568 break;
1569 default:
1570 ALOGD("%s: no handling for server evt (%d) at this state",
1571 __func__, cam_evt->server_event_type);
1572 break;
1573 }
1574 }
1575 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001576 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1577 //TODO: Adjust FPS
1578 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001579 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -08001580 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Muhua Libc9a8082012-11-07 15:51:28 -08001581 default:
1582 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1583 break;
1584 }
1585
1586 return rc;
1587}
1588
Muhua Lida2c4be2012-11-26 09:14:16 -08001589/*===========================================================================
1590 * FUNCTION : procEvtVideoPicTakingState
1591 *
1592 * DESCRIPTION: finite state machine function to handle event in state of
1593 * QCAMERA_SM_STATE_VIDEO_PIC_TAKING.
1594 *
1595 * PARAMETERS :
1596 * @evt : event to be processed
1597 * @payload : event payload. Can be NULL if not needed.
1598 *
1599 * RETURN : int32_t type of status
1600 * NO_ERROR -- success
1601 * none-zero failure code
1602 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001603int32_t QCameraStateMachine::procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,
1604 void *payload)
1605{
1606 int32_t rc = NO_ERROR;
1607 qcamera_api_result_t result;
1608 memset(&result, 0, sizeof(qcamera_api_result_t));
1609
1610 switch (evt) {
1611 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1612 {
1613 // Error setting preview window during previewing
1614 ALOGE("Cannot set preview window when preview is running");
1615 rc = INVALID_OPERATION;
1616 result.status = rc;
1617 result.request_api = evt;
1618 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1619 m_parent->signalAPIResult(&result);
1620 }
1621 break;
1622 case QCAMERA_SM_EVT_SET_CALLBACKS:
1623 {
1624 qcamera_sm_evt_setcb_payload_t *setcbs =
1625 (qcamera_sm_evt_setcb_payload_t *)payload;
1626 rc = m_parent->setCallBacks(setcbs->notify_cb,
1627 setcbs->data_cb,
1628 setcbs->data_cb_timestamp,
1629 setcbs->get_memory,
1630 setcbs->user);
1631 result.status = rc;
1632 result.request_api = evt;
1633 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1634 m_parent->signalAPIResult(&result);
1635 }
1636 break;
1637 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1638 {
1639 rc = m_parent->enableMsgType(int32_t(payload));
1640 result.status = rc;
1641 result.request_api = evt;
1642 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1643 m_parent->signalAPIResult(&result);
1644 }
1645 break;
1646 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1647 {
1648 rc = m_parent->disableMsgType(int32_t(payload));
1649 result.status = rc;
1650 result.request_api = evt;
1651 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1652 m_parent->signalAPIResult(&result);
1653 }
1654 break;
1655 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1656 {
1657 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1658 result.status = rc;
1659 result.request_api = evt;
1660 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1661 result.enabled = enabled;
1662 m_parent->signalAPIResult(&result);
1663 }
1664 break;
1665 case QCAMERA_SM_EVT_SET_PARAMS:
1666 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001667 bool needRestart = false;
1668 rc = m_parent->updateParameters((char*)payload, needRestart);
1669 if (rc == NO_ERROR) {
1670 if (needRestart) {
1671 // cannot set parameters that requires restart during recording
1672 ALOGE("%s: Cannot set parameters that requires restart during recording",
1673 __func__);
1674 rc = BAD_VALUE;
1675 } else {
1676 rc = m_parent->commitParameterChanges();
1677 }
1678 }
Muhua Libc9a8082012-11-07 15:51:28 -08001679 result.status = rc;
1680 result.request_api = evt;
1681 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1682 m_parent->signalAPIResult(&result);
1683 }
1684 break;
1685 case QCAMERA_SM_EVT_GET_PARAMS:
1686 {
1687 result.params = m_parent->getParameters();
1688 rc = NO_ERROR;
1689 result.status = rc;
1690 result.request_api = evt;
1691 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1692 m_parent->signalAPIResult(&result);
1693 }
1694 break;
1695 case QCAMERA_SM_EVT_PUT_PARAMS:
1696 {
1697 rc = m_parent->putParameters((char*)payload);
1698 result.status = rc;
1699 result.request_api = evt;
1700 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1701 m_parent->signalAPIResult(&result);
1702 }
1703 break;
1704 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1705 {
1706 rc = NO_ERROR;
1707 result.status = rc;
1708 result.request_api = evt;
1709 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1710 result.enabled = 1;
1711 m_parent->signalAPIResult(&result);
1712 }
1713 break;
1714 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1715 {
1716 rc = NO_ERROR;
1717 result.status = rc;
1718 result.request_api = evt;
1719 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1720 result.enabled = 1;
1721 m_parent->signalAPIResult(&result);
1722 }
1723 break;
1724 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1725 {
1726 rc = m_parent->storeMetaDataInBuffers(int(payload));
1727 result.status = rc;
1728 result.request_api = evt;
1729 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1730 m_parent->signalAPIResult(&result);
1731 }
1732 break;
1733 case QCAMERA_SM_EVT_DUMP:
1734 {
1735 rc = m_parent->dump((int)payload);
1736 result.status = rc;
1737 result.request_api = evt;
1738 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1739 m_parent->signalAPIResult(&result);
1740 }
1741 break;
1742 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1743 {
1744 rc = m_parent->autoFocus();
1745 result.status = rc;
1746 result.request_api = evt;
1747 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1748 m_parent->signalAPIResult(&result);
1749 }
1750 break;
1751 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1752 {
1753 rc = m_parent->cancelAutoFocus();
1754 result.status = rc;
1755 result.request_api = evt;
1756 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1757 m_parent->signalAPIResult(&result);
1758 }
1759 break;
1760 case QCAMERA_SM_EVT_SEND_COMMAND:
1761 {
1762 qcamera_sm_evt_command_payload_t *cmd_payload =
1763 (qcamera_sm_evt_command_payload_t *)payload;
1764 rc = m_parent->sendCommand(cmd_payload->cmd,
1765 cmd_payload->arg1,
1766 cmd_payload->arg2);
1767 result.status = rc;
1768 result.request_api = evt;
1769 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1770 m_parent->signalAPIResult(&result);
1771 }
1772 break;
1773 case QCAMERA_SM_EVT_STOP_RECORDING:
1774 {
1775 rc = m_parent->stopRecording();
1776 m_state = QCAMERA_SM_STATE_PREVIEW_PIC_TAKING;
1777 result.status = rc;
1778 result.request_api = evt;
1779 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1780 m_parent->signalAPIResult(&result);
1781 }
1782 break;
1783 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1784 {
1785 rc = m_parent->releaseRecordingFrame((const void *)payload);
1786 result.status = rc;
1787 result.request_api = evt;
1788 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1789 m_parent->signalAPIResult(&result);
1790 }
1791 break;
1792 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1793 {
1794 rc = m_parent->cancelLiveSnapshot();
1795 m_state = QCAMERA_SM_STATE_RECORDING;
1796 result.status = rc;
1797 result.request_api = evt;
1798 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1799 m_parent->signalAPIResult(&result);
1800 }
1801 break;
1802 case QCAMERA_SM_EVT_START_RECORDING:
1803 case QCAMERA_SM_EVT_START_PREVIEW:
1804 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1805 case QCAMERA_SM_EVT_STOP_PREVIEW:
1806 case QCAMERA_SM_EVT_TAKE_PICTURE:
1807 case QCAMERA_SM_EVT_RELEASE:
1808 {
1809 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1810 rc = INVALID_OPERATION;
1811 result.status = rc;
1812 result.request_api = evt;
1813 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1814 m_parent->signalAPIResult(&result);
1815 }
1816 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001817 case QCAMERA_SM_EVT_EVT_INTERNAL:
1818 {
1819 qcamera_sm_internal_evt_payload_t *internal_evt =
1820 (qcamera_sm_internal_evt_payload_t *)payload;
1821 switch (internal_evt->evt_type) {
1822 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1823 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1824 break;
1825 default:
1826 break;
1827 }
1828 }
1829 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001830 case QCAMERA_SM_EVT_EVT_NOTIFY:
1831 {
1832 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1833 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001834 case CAM_EVENT_TYPE_ZOOM_DONE:
1835 rc = m_parent->processZoomEvent(cam_evt->status);
1836 break;
1837 default:
1838 ALOGD("%s: no handling for server evt (%d) at this state",
1839 __func__, cam_evt->server_event_type);
1840 break;
1841 }
1842 }
1843 break;
1844 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1845 {
1846 qcamera_jpeg_evt_payload_t *jpeg_job =
1847 (qcamera_jpeg_evt_payload_t *)payload;
1848 rc = m_parent->processJpegNotify(jpeg_job);
1849 }
1850 break;
1851 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1852 {
1853 rc = m_parent->cancelLiveSnapshot();
1854 m_state = QCAMERA_SM_STATE_RECORDING;
1855 }
1856 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001857 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1858 //TODO: Adjust FPS.
1859 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001860 default:
1861 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1862 break;
1863 }
1864
1865 return rc;
1866}
1867
Muhua Lida2c4be2012-11-26 09:14:16 -08001868/*===========================================================================
1869 * FUNCTION : procEvtPreviewPicTakingState
1870 *
1871 * DESCRIPTION: finite state machine function to handle event in state of
1872 * QCAMERA_SM_STATE_PREVIEW_PIC_TAKING.
1873 *
1874 * PARAMETERS :
1875 * @evt : event to be processed
1876 * @payload : event payload. Can be NULL if not needed.
1877 *
1878 * RETURN : int32_t type of status
1879 * NO_ERROR -- success
1880 * none-zero failure code
1881 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001882int32_t QCameraStateMachine::procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,
1883 void *payload)
1884{
1885 int32_t rc = NO_ERROR;
1886 qcamera_api_result_t result;
1887 memset(&result, 0, sizeof(qcamera_api_result_t));
1888
1889 switch (evt) {
1890 case QCAMERA_SM_EVT_SET_CALLBACKS:
1891 {
1892 qcamera_sm_evt_setcb_payload_t *setcbs =
1893 (qcamera_sm_evt_setcb_payload_t *)payload;
1894 rc = m_parent->setCallBacks(setcbs->notify_cb,
1895 setcbs->data_cb,
1896 setcbs->data_cb_timestamp,
1897 setcbs->get_memory,
1898 setcbs->user);
1899 result.status = rc;
1900 result.request_api = evt;
1901 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1902 m_parent->signalAPIResult(&result);
1903 }
1904 break;
1905 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1906 {
1907 rc = m_parent->enableMsgType(int32_t(payload));
1908 result.status = rc;
1909 result.request_api = evt;
1910 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1911 m_parent->signalAPIResult(&result);
1912 }
1913 break;
1914 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1915 {
1916 rc = m_parent->disableMsgType(int32_t(payload));
1917 result.status = rc;
1918 result.request_api = evt;
1919 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1920 m_parent->signalAPIResult(&result);
1921 }
1922 break;
1923 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1924 {
1925 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1926 result.status = rc;
1927 result.request_api = evt;
1928 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1929 result.enabled = enabled;
1930 m_parent->signalAPIResult(&result);
1931 }
1932 break;
1933 case QCAMERA_SM_EVT_SET_PARAMS:
1934 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001935 bool needRestart = false;
1936 rc = m_parent->updateParameters((char*)payload, needRestart);
1937 if (rc == NO_ERROR) {
1938 if (needRestart) {
1939 // need restart preview for parameters to take effect
1940 // stop preview
1941 m_parent->stopPreview();
1942 // commit parameter changes to server
1943 rc = m_parent->commitParameterChanges();
1944 // start preview again
1945 m_parent->startPreview();
1946 } else {
1947 rc = m_parent->commitParameterChanges();
1948 }
1949 }
Muhua Libc9a8082012-11-07 15:51:28 -08001950 result.status = rc;
1951 result.request_api = evt;
1952 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1953 m_parent->signalAPIResult(&result);
1954 }
1955 break;
1956 case QCAMERA_SM_EVT_GET_PARAMS:
1957 {
1958 result.params = m_parent->getParameters();
1959 rc = NO_ERROR;
1960 result.status = rc;
1961 result.request_api = evt;
1962 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1963 m_parent->signalAPIResult(&result);
1964 }
1965 break;
1966 case QCAMERA_SM_EVT_PUT_PARAMS:
1967 {
1968 rc = m_parent->putParameters((char*)payload);
1969 result.status = rc;
1970 result.request_api = evt;
1971 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1972 m_parent->signalAPIResult(&result);
1973 }
1974 break;
1975 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1976 {
1977 rc = NO_ERROR;
1978 result.status = rc;
1979 result.request_api = evt;
1980 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1981 result.enabled = 1;
1982 m_parent->signalAPIResult(&result);
1983 }
1984 break;
1985 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1986 {
1987 rc = NO_ERROR;
1988 result.status = rc;
1989 result.request_api = evt;
1990 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1991 result.enabled = 0;
1992 m_parent->signalAPIResult(&result);
1993 }
1994 break;
1995 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1996 {
1997 rc = m_parent->storeMetaDataInBuffers(int(payload));
1998 result.status = rc;
1999 result.request_api = evt;
2000 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2001 m_parent->signalAPIResult(&result);
2002 }
2003 break;
2004 case QCAMERA_SM_EVT_DUMP:
2005 {
2006 rc = m_parent->dump((int)payload);
2007 result.status = rc;
2008 result.request_api = evt;
2009 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2010 m_parent->signalAPIResult(&result);
2011 }
2012 break;
2013 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2014 {
2015 rc = m_parent->autoFocus();
2016 result.status = rc;
2017 result.request_api = evt;
2018 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2019 m_parent->signalAPIResult(&result);
2020 }
2021 break;
2022 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2023 {
2024 rc = m_parent->cancelAutoFocus();
2025 result.status = rc;
2026 result.request_api = evt;
2027 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2028 m_parent->signalAPIResult(&result);
2029 }
2030 break;
2031 case QCAMERA_SM_EVT_SEND_COMMAND:
2032 {
2033 qcamera_sm_evt_command_payload_t *cmd_payload =
2034 (qcamera_sm_evt_command_payload_t *)payload;
2035 rc = m_parent->sendCommand(cmd_payload->cmd,
2036 cmd_payload->arg1,
2037 cmd_payload->arg2);
2038 result.status = rc;
2039 result.request_api = evt;
2040 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2041 m_parent->signalAPIResult(&result);
2042 }
2043 break;
2044 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2045 {
2046 rc = m_parent->releaseRecordingFrame((const void *)payload);
2047 result.status = rc;
2048 result.request_api = evt;
2049 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2050 m_parent->signalAPIResult(&result);
2051 }
2052 break;
2053 case QCAMERA_SM_EVT_CANCEL_PICTURE:
2054 {
2055 rc = m_parent->cancelLiveSnapshot();
2056 m_state = QCAMERA_SM_STATE_PREVIEWING;
2057 result.status = rc;
2058 result.request_api = evt;
2059 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2060 m_parent->signalAPIResult(&result);
2061 }
2062 break;
2063 case QCAMERA_SM_EVT_STOP_PREVIEW:
2064 {
2065 m_parent->stopChannel(QCAMERA_CH_TYPE_PREVIEW);
2066 m_parent->delChannel(QCAMERA_CH_TYPE_PREVIEW);
2067 m_parent->delChannel(QCAMERA_CH_TYPE_VIDEO);
2068 m_state = QCAMERA_SM_STATE_PIC_TAKING;
2069 rc = NO_ERROR;
2070 result.status = rc;
2071 result.request_api = evt;
2072 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2073 m_parent->signalAPIResult(&result);
2074 }
2075 break;
2076 case QCAMERA_SM_EVT_START_RECORDING:
2077 {
2078 rc = m_parent->stopRecording();
2079 if (rc == NO_ERROR) {
2080 m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
2081 }
2082 result.status = rc;
2083 result.request_api = evt;
2084 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2085 m_parent->signalAPIResult(&result);
2086 }
2087 break;
2088 case QCAMERA_SM_EVT_STOP_RECORDING:
2089 case QCAMERA_SM_EVT_START_PREVIEW:
2090 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2091 case QCAMERA_SM_EVT_TAKE_PICTURE:
2092 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2093 case QCAMERA_SM_EVT_RELEASE:
2094 {
2095 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2096 rc = INVALID_OPERATION;
2097 result.status = rc;
2098 result.request_api = evt;
2099 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2100 m_parent->signalAPIResult(&result);
2101 }
2102 break;
Muhua Li31eaee02012-12-11 08:56:45 -08002103 case QCAMERA_SM_EVT_EVT_INTERNAL:
2104 {
2105 qcamera_sm_internal_evt_payload_t *internal_evt =
2106 (qcamera_sm_internal_evt_payload_t *)payload;
2107 switch (internal_evt->evt_type) {
2108 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2109 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2110 break;
2111 default:
2112 break;
2113 }
2114 }
2115 break;
Muhua Libc9a8082012-11-07 15:51:28 -08002116 case QCAMERA_SM_EVT_EVT_NOTIFY:
2117 {
2118 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2119 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08002120 case CAM_EVENT_TYPE_ZOOM_DONE:
2121 rc = m_parent->processZoomEvent(cam_evt->status);
2122 break;
2123 default:
2124 ALOGD("%s: no handling for server evt (%d) at this state",
2125 __func__, cam_evt->server_event_type);
2126 break;
2127 }
2128 }
2129 break;
2130 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2131 {
2132 qcamera_jpeg_evt_payload_t *jpeg_job =
2133 (qcamera_jpeg_evt_payload_t *)payload;
2134 rc = m_parent->processJpegNotify(jpeg_job);
2135 }
2136 break;
2137 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2138 {
2139 rc = m_parent->cancelLiveSnapshot();
2140 m_state = QCAMERA_SM_STATE_PREVIEWING;
2141 }
2142 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08002143 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2144 //TODO: Adjust FPS
2145 break;
Muhua Libc9a8082012-11-07 15:51:28 -08002146 default:
2147 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2148 break;
2149 }
2150
2151 return rc;
2152}
2153
Muhua Lida2c4be2012-11-26 09:14:16 -08002154/*===========================================================================
2155 * FUNCTION : isPreviewRunning
2156 *
2157 * DESCRIPTION: check if preview is in process.
2158 *
2159 * PARAMETERS : None
2160 *
2161 * RETURN : true -- preview running
2162 * false -- preview stopped
2163 *==========================================================================*/
2164bool QCameraStateMachine::isPreviewRunning()
2165{
2166 switch (m_state) {
2167 case QCAMERA_SM_STATE_PREVIEWING:
2168 case QCAMERA_SM_STATE_RECORDING:
2169 case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
2170 case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
2171 return true;
2172 default:
2173 return false;
2174 }
2175}
2176
Shuzhen Wang89635cf2012-12-20 13:47:22 -08002177}; // namespace qcamera