blob: 82e9514028e23ed17076dc80bbeaba4cd77da914 [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:
Muhua Libc9a8082012-11-07 15:51:28 -0800511 default:
512 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
513 break;
514 }
515
516 return rc;
517}
518
Muhua Lida2c4be2012-11-26 09:14:16 -0800519/*===========================================================================
520 * FUNCTION : procEvtPreviewReadyState
521 *
522 * DESCRIPTION: finite state machine function to handle event in state of
523 * QCAMERA_SM_STATE_PREVIEW_READY.
524 *
525 * PARAMETERS :
526 * @evt : event to be processed
527 * @payload : event payload. Can be NULL if not needed.
528 *
529 * RETURN : int32_t type of status
530 * NO_ERROR -- success
531 * none-zero failure code
532 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800533int32_t QCameraStateMachine::procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt,
534 void *payload)
535{
536 int32_t rc = NO_ERROR;
537 qcamera_api_result_t result;
538 memset(&result, 0, sizeof(qcamera_api_result_t));
539
540 switch (evt) {
541 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
542 {
543 m_parent->setPreviewWindow((struct preview_stream_ops *)payload);
544 if (m_parent->mPreviewWindow != NULL) {
545 rc = m_parent->startPreview();
546 if (rc != NO_ERROR) {
547 m_parent->unpreparePreview();
548 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
549 } else {
550 m_state = QCAMERA_SM_STATE_PREVIEWING;
551 }
552 }
553
554 result.status = rc;
555 result.request_api = evt;
556 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
557 m_parent->signalAPIResult(&result);
558 }
559 break;
560 case QCAMERA_SM_EVT_SET_CALLBACKS:
561 {
562 qcamera_sm_evt_setcb_payload_t *setcbs =
563 (qcamera_sm_evt_setcb_payload_t *)payload;
564 rc = m_parent->setCallBacks(setcbs->notify_cb,
565 setcbs->data_cb,
566 setcbs->data_cb_timestamp,
567 setcbs->get_memory,
568 setcbs->user);
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_ENABLE_MSG_TYPE:
576 {
577 rc = m_parent->enableMsgType(int32_t(payload));
578 result.status = rc;
579 result.request_api = evt;
580 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
581 m_parent->signalAPIResult(&result);
582 }
583 break;
584 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
585 {
586 rc = m_parent->disableMsgType(int32_t(payload));
587 result.status = rc;
588 result.request_api = evt;
589 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
590 m_parent->signalAPIResult(&result);
591 }
592 break;
593 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
594 {
595 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
596 result.status = rc;
597 result.request_api = evt;
598 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
599 result.enabled = enabled;
600 m_parent->signalAPIResult(&result);
601 }
602 break;
603 case QCAMERA_SM_EVT_SET_PARAMS:
604 {
Muhua Lida2c4be2012-11-26 09:14:16 -0800605 bool needRestart = false;
606 rc = m_parent->updateParameters((char*)payload, needRestart);
607 if (rc == NO_ERROR) {
608 rc = m_parent->commitParameterChanges();
609 }
Muhua Libc9a8082012-11-07 15:51:28 -0800610 result.status = rc;
611 result.request_api = evt;
612 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
613 m_parent->signalAPIResult(&result);
614 }
615 break;
616 case QCAMERA_SM_EVT_GET_PARAMS:
617 {
618 result.params = m_parent->getParameters();
619 rc = NO_ERROR;
620 result.status = rc;
621 result.request_api = evt;
622 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
623 m_parent->signalAPIResult(&result);
624 }
625 break;
626 case QCAMERA_SM_EVT_PUT_PARAMS:
627 {
628 rc = m_parent->putParameters((char*)payload);
629 result.status = rc;
630 result.request_api = evt;
631 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
632 m_parent->signalAPIResult(&result);
633 }
634 break;
635 case QCAMERA_SM_EVT_START_PREVIEW:
636 {
637 // no ops here
638 rc = NO_ERROR;
639 result.status = rc;
640 result.request_api = evt;
641 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
642 m_parent->signalAPIResult(&result);
643 }
644 break;
645 case QCAMERA_SM_EVT_STOP_PREVIEW:
646 {
647 m_parent->unpreparePreview();
648 rc = 0;
649 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
650 result.status = rc;
651 result.request_api = evt;
652 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
653 m_parent->signalAPIResult(&result);
654 }
655 break;
656 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
657 {
658 rc = NO_ERROR;
659 result.status = rc;
660 result.request_api = evt;
661 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
662 result.enabled = 1;
663 m_parent->signalAPIResult(&result);
664 }
665 break;
666 case QCAMERA_SM_EVT_RECORDING_ENABLED:
667 {
668 rc = 0;
669 result.status = rc;
670 result.request_api = evt;
671 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
672 result.enabled = 0;
673 m_parent->signalAPIResult(&result);
674 }
675 break;
676 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
677 {
678 rc = m_parent->storeMetaDataInBuffers(int(payload));
679 result.status = rc;
680 result.request_api = evt;
681 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
682 m_parent->signalAPIResult(&result);
683 }
684 break;
685 case QCAMERA_SM_EVT_DUMP:
686 {
687 rc = m_parent->dump((int)payload);
688 result.status = rc;
689 result.request_api = evt;
690 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
691 m_parent->signalAPIResult(&result);
692 }
693 break;
694 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
695 {
696 rc = m_parent->autoFocus();
697 result.status = rc;
698 result.request_api = evt;
699 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
700 m_parent->signalAPIResult(&result);
701 }
702 break;
703 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
704 {
705 rc = m_parent->cancelAutoFocus();
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_SEND_COMMAND:
713 {
714 qcamera_sm_evt_command_payload_t *cmd_payload =
715 (qcamera_sm_evt_command_payload_t *)payload;
716 rc = m_parent->sendCommand(cmd_payload->cmd,
717 cmd_payload->arg1,
718 cmd_payload->arg2);
719 result.status = rc;
720 result.request_api = evt;
721 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
722 m_parent->signalAPIResult(&result);
723 }
724 break;
725 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
726 case QCAMERA_SM_EVT_START_RECORDING:
727 case QCAMERA_SM_EVT_STOP_RECORDING:
728 case QCAMERA_SM_EVT_TAKE_PICTURE:
729 case QCAMERA_SM_EVT_CANCEL_PICTURE:
730 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
731 case QCAMERA_SM_EVT_RELEASE:
732 {
733 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
734 rc = INVALID_OPERATION;
735 result.status = rc;
736 result.request_api = evt;
737 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
738 m_parent->signalAPIResult(&result);
739 }
740 break;
Muhua Li31eaee02012-12-11 08:56:45 -0800741 case QCAMERA_SM_EVT_EVT_INTERNAL:
Muhua Libc9a8082012-11-07 15:51:28 -0800742 case QCAMERA_SM_EVT_EVT_NOTIFY:
743 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -0800744 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Muhua Libc9a8082012-11-07 15:51:28 -0800745 default:
746 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
747 break;
748 }
749
750 return rc;
751}
752
Muhua Lida2c4be2012-11-26 09:14:16 -0800753/*===========================================================================
754 * FUNCTION : procEvtPreviewingState
755 *
756 * DESCRIPTION: finite state machine function to handle event in state of
757 * QCAMERA_SM_STATE_PREVIEWING.
758 *
759 * PARAMETERS :
760 * @evt : event to be processed
761 * @payload : event payload. Can be NULL if not needed.
762 *
763 * RETURN : int32_t type of status
764 * NO_ERROR -- success
765 * none-zero failure code
766 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800767int32_t QCameraStateMachine::procEvtPreviewingState(qcamera_sm_evt_enum_t evt,
768 void *payload)
769{
770 int32_t rc = NO_ERROR;
771 qcamera_api_result_t result;
772 memset(&result, 0, sizeof(qcamera_api_result_t));
773
774 switch (evt) {
775 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
776 {
777 // Error setting preview window during previewing
778 ALOGE("Cannot set preview window when preview is running");
779 rc = INVALID_OPERATION;
780 result.status = rc;
781 result.request_api = evt;
782 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
783 m_parent->signalAPIResult(&result);
784 }
785 break;
786 case QCAMERA_SM_EVT_SET_CALLBACKS:
787 {
788 qcamera_sm_evt_setcb_payload_t *setcbs =
789 (qcamera_sm_evt_setcb_payload_t *)payload;
790 rc = m_parent->setCallBacks(setcbs->notify_cb,
791 setcbs->data_cb,
792 setcbs->data_cb_timestamp,
793 setcbs->get_memory,
794 setcbs->user);
795 result.status = rc;
796 result.request_api = evt;
797 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
798 m_parent->signalAPIResult(&result);
799 }
800 break;
801 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
802 {
803 rc = m_parent->enableMsgType(int32_t(payload));
804 result.status = rc;
805 result.request_api = evt;
806 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
807 m_parent->signalAPIResult(&result);
808 }
809 break;
810 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
811 {
812 rc = m_parent->disableMsgType(int32_t(payload));
813 result.status = rc;
814 result.request_api = evt;
815 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
816 m_parent->signalAPIResult(&result);
817 }
818 break;
819 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
820 {
821 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
822 result.status = rc;
823 result.request_api = evt;
824 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
825 result.enabled = enabled;
826 m_parent->signalAPIResult(&result);
827 }
828 break;
829 case QCAMERA_SM_EVT_SET_PARAMS:
830 {
Muhua Lida2c4be2012-11-26 09:14:16 -0800831 bool needRestart = false;
832 rc = m_parent->updateParameters((char*)payload, needRestart);
833 if (rc == NO_ERROR) {
834 if (needRestart) {
835 // need restart preview for parameters to take effect
836 // stop preview
837 m_parent->stopPreview();
838 // commit parameter changes to server
839 rc = m_parent->commitParameterChanges();
840 // start preview again
841 m_parent->startPreview();
842 } else {
843 rc = m_parent->commitParameterChanges();
844 }
845 }
Muhua Libc9a8082012-11-07 15:51:28 -0800846 result.status = rc;
847 result.request_api = evt;
848 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
849 m_parent->signalAPIResult(&result);
850 }
851 break;
852 case QCAMERA_SM_EVT_GET_PARAMS:
853 {
854 result.params = m_parent->getParameters();
855 rc = NO_ERROR;
856 result.status = rc;
857 result.request_api = evt;
858 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
859 m_parent->signalAPIResult(&result);
860 }
861 break;
862 case QCAMERA_SM_EVT_PUT_PARAMS:
863 {
864 rc = m_parent->putParameters((char*)payload);
865 result.status = rc;
866 result.request_api = evt;
867 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
868 m_parent->signalAPIResult(&result);
869 }
870 break;
871 case QCAMERA_SM_EVT_START_PREVIEW:
872 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
873 {
874 // no ops here
875 ALOGD("%s: Already in previewing, no ops here to start preview", __func__);
876 rc = NO_ERROR;
877 result.status = rc;
878 result.request_api = evt;
879 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
880 m_parent->signalAPIResult(&result);
881 }
882 break;
883 case QCAMERA_SM_EVT_STOP_PREVIEW:
884 {
885 rc = m_parent->stopPreview();
886 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
887 result.status = rc;
888 result.request_api = evt;
889 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
890 m_parent->signalAPIResult(&result);
891 }
892 break;
893 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
894 {
895 rc = NO_ERROR;
896 result.status = rc;
897 result.request_api = evt;
898 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
899 result.enabled = 1;
900 m_parent->signalAPIResult(&result);
901 }
902 break;
903 case QCAMERA_SM_EVT_RECORDING_ENABLED:
904 {
905 rc = NO_ERROR;
906 result.status = rc;
907 result.request_api = evt;
908 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
909 result.enabled = 0;
910 m_parent->signalAPIResult(&result);
911 }
912 break;
913 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
914 {
915 rc = m_parent->storeMetaDataInBuffers(int(payload));
916 result.status = rc;
917 result.request_api = evt;
918 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
919 m_parent->signalAPIResult(&result);
920 }
921 break;
922 case QCAMERA_SM_EVT_DUMP:
923 {
924 rc = m_parent->dump((int)payload);
925 result.status = rc;
926 result.request_api = evt;
927 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
928 m_parent->signalAPIResult(&result);
929 }
930 break;
931 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
932 {
933 rc = m_parent->autoFocus();
934 result.status = rc;
935 result.request_api = evt;
936 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
937 m_parent->signalAPIResult(&result);
938 }
939 break;
940 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
941 {
942 rc = m_parent->cancelAutoFocus();
943 result.status = rc;
944 result.request_api = evt;
945 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
946 m_parent->signalAPIResult(&result);
947 }
948 break;
949 case QCAMERA_SM_EVT_START_RECORDING:
950 {
951 rc = m_parent->startRecording();
952 if (rc == NO_ERROR) {
953 // move state to recording state
954 m_state = QCAMERA_SM_STATE_RECORDING;
955 }
956 result.status = rc;
957 result.request_api = evt;
958 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
959 m_parent->signalAPIResult(&result);
960 }
961 break;
962 case QCAMERA_SM_EVT_TAKE_PICTURE:
963 {
964 rc = m_parent->takePicture();
965 if (rc == NO_ERROR) {
966 // move state to picture taking state
967 m_state = QCAMERA_SM_STATE_PIC_TAKING;
968 } else {
969 // move state to preview stopped state
970 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
971 }
972 result.status = rc;
973 result.request_api = evt;
974 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
975 m_parent->signalAPIResult(&result);
976 }
977 break;
978 case QCAMERA_SM_EVT_SEND_COMMAND:
979 {
980 qcamera_sm_evt_command_payload_t *cmd_payload =
981 (qcamera_sm_evt_command_payload_t *)payload;
982 rc = m_parent->sendCommand(cmd_payload->cmd,
983 cmd_payload->arg1,
984 cmd_payload->arg2);
985 result.status = rc;
986 result.request_api = evt;
987 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
988 m_parent->signalAPIResult(&result);
989 }
990 break;
991 case QCAMERA_SM_EVT_CANCEL_PICTURE:
992 case QCAMERA_SM_EVT_STOP_RECORDING:
993 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
994 case QCAMERA_SM_EVT_RELEASE:
995 {
996 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
997 rc = INVALID_OPERATION;
998 result.status = rc;
999 result.request_api = evt;
1000 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1001 m_parent->signalAPIResult(&result);
1002 }
1003 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001004 case QCAMERA_SM_EVT_EVT_INTERNAL:
1005 {
1006 qcamera_sm_internal_evt_payload_t *internal_evt =
1007 (qcamera_sm_internal_evt_payload_t *)payload;
1008 switch (internal_evt->evt_type) {
1009 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1010 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1011 break;
1012 default:
1013 break;
1014 }
1015 }
1016 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001017 case QCAMERA_SM_EVT_EVT_NOTIFY:
1018 {
1019 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1020 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001021 case CAM_EVENT_TYPE_ZOOM_DONE:
1022 rc = m_parent->processZoomEvent(cam_evt->status);
1023 break;
1024 default:
1025 ALOGD("%s: no handling for server evt (%d) at this state",
1026 __func__, cam_evt->server_event_type);
1027 break;
1028 }
1029 }
1030 break;
1031 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -08001032 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Muhua Libc9a8082012-11-07 15:51:28 -08001033 default:
1034 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1035 break;
1036 }
1037
1038 return rc;
1039}
1040
Muhua Lida2c4be2012-11-26 09:14:16 -08001041/*===========================================================================
1042 * FUNCTION : procEvtPicTakingState
1043 *
1044 * DESCRIPTION: finite state machine function to handle event in state of
1045 * QCAMERA_SM_STATE_PIC_TAKING.
1046 *
1047 * PARAMETERS :
1048 * @evt : event to be processed
1049 * @payload : event payload. Can be NULL if not needed.
1050 *
1051 * RETURN : int32_t type of status
1052 * NO_ERROR -- success
1053 * none-zero failure code
1054 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001055int32_t QCameraStateMachine::procEvtPicTakingState(qcamera_sm_evt_enum_t evt,
1056 void *payload)
1057{
1058 int32_t rc = NO_ERROR;
1059 qcamera_api_result_t result;
1060 memset(&result, 0, sizeof(qcamera_api_result_t));
1061
1062 switch (evt) {
1063 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1064 {
1065 // Error setting preview window during previewing
1066 ALOGE("Cannot set preview window when preview is running");
1067 rc = INVALID_OPERATION;
1068 result.status = rc;
1069 result.request_api = evt;
1070 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1071 m_parent->signalAPIResult(&result);
1072 }
1073 break;
1074 case QCAMERA_SM_EVT_SET_CALLBACKS:
1075 {
1076 qcamera_sm_evt_setcb_payload_t *setcbs =
1077 (qcamera_sm_evt_setcb_payload_t *)payload;
1078 rc = m_parent->setCallBacks(setcbs->notify_cb,
1079 setcbs->data_cb,
1080 setcbs->data_cb_timestamp,
1081 setcbs->get_memory,
1082 setcbs->user);
1083 result.status = rc;
1084 result.request_api = evt;
1085 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1086 m_parent->signalAPIResult(&result);
1087 }
1088 break;
1089 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1090 {
1091 rc = m_parent->enableMsgType(int32_t(payload));
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_DISABLE_MSG_TYPE:
1099 {
1100 rc = m_parent->disableMsgType(int32_t(payload));
1101 result.status = rc;
1102 result.request_api = evt;
1103 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1104 m_parent->signalAPIResult(&result);
1105 }
1106 break;
1107 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1108 {
1109 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1110 result.status = rc;
1111 result.request_api = evt;
1112 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1113 result.enabled = enabled;
1114 m_parent->signalAPIResult(&result);
1115 }
1116 break;
1117 case QCAMERA_SM_EVT_SET_PARAMS:
1118 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001119 bool needRestart = false;
1120 rc = m_parent->updateParameters((char*)payload, needRestart);
1121 if (rc == NO_ERROR) {
1122 rc = m_parent->commitParameterChanges();
1123 }
Muhua Libc9a8082012-11-07 15:51:28 -08001124 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_GET_PARAMS:
1131 {
1132 result.params = m_parent->getParameters();
1133 rc = NO_ERROR;
1134 result.status = rc;
1135 result.request_api = evt;
1136 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1137 m_parent->signalAPIResult(&result);
1138 }
1139 break;
1140 case QCAMERA_SM_EVT_PUT_PARAMS:
1141 {
1142 rc = m_parent->putParameters((char*)payload);
1143 result.status = rc;
1144 result.request_api = evt;
1145 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1146 m_parent->signalAPIResult(&result);
1147 }
1148 break;
1149 case QCAMERA_SM_EVT_STOP_PREVIEW:
1150 {
1151 // no ops, since preview is stopped (normal),
1152 // or preview msg type is disabled (ZSL)
1153 rc = NO_ERROR;
1154 result.status = rc;
1155 result.request_api = evt;
1156 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1157 m_parent->signalAPIResult(&result);
1158 }
1159 break;
1160 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1161 {
1162 rc = NO_ERROR;
1163 result.status = rc;
1164 result.request_api = evt;
1165 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1166 result.enabled = 0;
1167 m_parent->signalAPIResult(&result);
1168 }
1169 break;
1170 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1171 {
1172 rc = NO_ERROR;
1173 result.status = rc;
1174 result.request_api = evt;
1175 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1176 result.enabled = 0;
1177 m_parent->signalAPIResult(&result);
1178 }
1179 break;
1180 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1181 {
1182 rc = m_parent->storeMetaDataInBuffers(int(payload));
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_DUMP:
1190 {
1191 rc = m_parent->dump((int)payload);
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_AUTO_FOCUS:
1199 {
1200 rc = m_parent->autoFocus();
1201 result.status = rc;
1202 result.request_api = evt;
1203 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1204 m_parent->signalAPIResult(&result);
1205 }
1206 break;
1207 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1208 {
1209 rc = m_parent->cancelAutoFocus();
1210 result.status = rc;
1211 result.request_api = evt;
1212 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1213 m_parent->signalAPIResult(&result);
1214 }
1215 break;
1216 case QCAMERA_SM_EVT_SEND_COMMAND:
1217 {
1218 qcamera_sm_evt_command_payload_t *cmd_payload =
1219 (qcamera_sm_evt_command_payload_t *)payload;
1220 rc = m_parent->sendCommand(cmd_payload->cmd,
1221 cmd_payload->arg1,
1222 cmd_payload->arg2);
1223 result.status = rc;
1224 result.request_api = evt;
1225 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1226 m_parent->signalAPIResult(&result);
1227 }
1228 break;
1229 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1230 {
1231 rc = m_parent->cancelPicture();
1232 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1233 result.status = rc;
1234 result.request_api = evt;
1235 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1236 m_parent->signalAPIResult(&result);
1237 }
1238 break;
1239 case QCAMERA_SM_EVT_TAKE_PICTURE:
1240 case QCAMERA_SM_EVT_START_RECORDING:
1241 case QCAMERA_SM_EVT_STOP_RECORDING:
1242 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1243 case QCAMERA_SM_EVT_START_PREVIEW:
1244 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1245 case QCAMERA_SM_EVT_RELEASE:
1246 {
1247 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1248 rc = INVALID_OPERATION;
1249 result.status = rc;
1250 result.request_api = evt;
1251 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1252 m_parent->signalAPIResult(&result);
1253 }
1254 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001255 case QCAMERA_SM_EVT_EVT_INTERNAL:
1256 {
1257 qcamera_sm_internal_evt_payload_t *internal_evt =
1258 (qcamera_sm_internal_evt_payload_t *)payload;
1259 switch (internal_evt->evt_type) {
1260 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1261 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1262 break;
1263 default:
1264 break;
1265 }
1266 }
1267 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001268 case QCAMERA_SM_EVT_EVT_NOTIFY:
1269 {
1270 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1271 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001272 case CAM_EVENT_TYPE_ZOOM_DONE:
1273 rc = m_parent->processZoomEvent(cam_evt->status);
1274 break;
1275 default:
1276 ALOGD("%s: no handling for server evt (%d) at this state",
1277 __func__, cam_evt->server_event_type);
1278 break;
1279 }
1280 }
1281 break;
1282 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1283 {
1284 qcamera_jpeg_evt_payload_t *jpeg_job =
1285 (qcamera_jpeg_evt_payload_t *)payload;
1286 rc = m_parent->processJpegNotify(jpeg_job);
1287 }
1288 break;
1289 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1290 {
1291 rc = m_parent->cancelPicture();
1292 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1293 }
1294 break;
1295 default:
1296 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1297 break;
1298 }
1299
1300 return rc;
1301}
1302
Muhua Lida2c4be2012-11-26 09:14:16 -08001303/*===========================================================================
1304 * FUNCTION : procEvtRecordingState
1305 *
1306 * DESCRIPTION: finite state machine function to handle event in state of
1307 * QCAMERA_SM_STATE_RECORDING.
1308 *
1309 * PARAMETERS :
1310 * @evt : event to be processed
1311 * @payload : event payload. Can be NULL if not needed.
1312 *
1313 * RETURN : int32_t type of status
1314 * NO_ERROR -- success
1315 * none-zero failure code
1316 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001317int32_t QCameraStateMachine::procEvtRecordingState(qcamera_sm_evt_enum_t evt,
1318 void *payload)
1319{
1320 int32_t rc = NO_ERROR;
1321 qcamera_api_result_t result;
1322 memset(&result, 0, sizeof(qcamera_api_result_t));
1323
1324 switch (evt) {
1325 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1326 {
1327 // Error setting preview window during previewing
1328 ALOGE("Cannot set preview window when preview is running");
1329 rc = INVALID_OPERATION;
1330 result.status = rc;
1331 result.request_api = evt;
1332 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1333 m_parent->signalAPIResult(&result);
1334 }
1335 break;
1336 case QCAMERA_SM_EVT_SET_CALLBACKS:
1337 {
1338 qcamera_sm_evt_setcb_payload_t *setcbs =
1339 (qcamera_sm_evt_setcb_payload_t *)payload;
1340 rc = m_parent->setCallBacks(setcbs->notify_cb,
1341 setcbs->data_cb,
1342 setcbs->data_cb_timestamp,
1343 setcbs->get_memory,
1344 setcbs->user);
1345 result.status = rc;
1346 result.request_api = evt;
1347 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1348 m_parent->signalAPIResult(&result);
1349 }
1350 break;
1351 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1352 {
1353 rc = m_parent->enableMsgType(int32_t(payload));
1354 result.status = rc;
1355 result.request_api = evt;
1356 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1357 m_parent->signalAPIResult(&result);
1358 }
1359 break;
1360 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1361 {
1362 rc = m_parent->disableMsgType(int32_t(payload));
1363 result.status = rc;
1364 result.request_api = evt;
1365 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1366 m_parent->signalAPIResult(&result);
1367 }
1368 break;
1369 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1370 {
1371 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1372 result.status = rc;
1373 result.request_api = evt;
1374 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1375 result.enabled = enabled;
1376 m_parent->signalAPIResult(&result);
1377 }
1378 break;
1379 case QCAMERA_SM_EVT_SET_PARAMS:
1380 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001381 bool needRestart = false;
1382 rc = m_parent->updateParameters((char*)payload, needRestart);
1383 if (rc == NO_ERROR) {
1384 if (needRestart) {
1385 // cannot set parameters that requires restart during recording
1386 ALOGE("%s: Cannot set parameters that requires restart during recording",
1387 __func__);
1388 rc = BAD_VALUE;
1389 } else {
1390 rc = m_parent->commitParameterChanges();
1391 }
1392 }
Muhua Libc9a8082012-11-07 15:51:28 -08001393 result.status = rc;
1394 result.request_api = evt;
1395 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1396 m_parent->signalAPIResult(&result);
1397 }
1398 break;
1399 case QCAMERA_SM_EVT_GET_PARAMS:
1400 {
1401 result.params = m_parent->getParameters();
1402 rc = NO_ERROR;
1403 result.status = rc;
1404 result.request_api = evt;
1405 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1406 m_parent->signalAPIResult(&result);
1407 }
1408 break;
1409 case QCAMERA_SM_EVT_PUT_PARAMS:
1410 {
1411 rc = m_parent->putParameters((char*)payload);
1412 result.status = rc;
1413 result.request_api = evt;
1414 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1415 m_parent->signalAPIResult(&result);
1416 }
1417 break;
1418 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1419 {
1420 rc = NO_ERROR;
1421 result.status = rc;
1422 result.request_api = evt;
1423 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1424 result.enabled = 0;
1425 m_parent->signalAPIResult(&result);
1426 }
1427 break;
1428 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1429 {
1430 rc = NO_ERROR;
1431 result.status = rc;
1432 result.request_api = evt;
1433 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1434 result.enabled = 1;
1435 m_parent->signalAPIResult(&result);
1436 }
1437 break;
1438 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1439 {
1440 rc = m_parent->storeMetaDataInBuffers(int(payload));
1441 result.status = rc;
1442 result.request_api = evt;
1443 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1444 m_parent->signalAPIResult(&result);
1445 }
1446 break;
1447 case QCAMERA_SM_EVT_DUMP:
1448 {
1449 rc = m_parent->dump((int)payload);
1450 result.status = rc;
1451 result.request_api = evt;
1452 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1453 m_parent->signalAPIResult(&result);
1454 }
1455 break;
1456 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1457 {
1458 rc = m_parent->autoFocus();
1459 result.status = rc;
1460 result.request_api = evt;
1461 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1462 m_parent->signalAPIResult(&result);
1463 }
1464 break;
1465 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1466 {
1467 rc = m_parent->cancelAutoFocus();
1468 result.status = rc;
1469 result.request_api = evt;
1470 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1471 m_parent->signalAPIResult(&result);
1472 }
1473 break;
1474 case QCAMERA_SM_EVT_SEND_COMMAND:
1475 {
1476 qcamera_sm_evt_command_payload_t *cmd_payload =
1477 (qcamera_sm_evt_command_payload_t *)payload;
1478 rc = m_parent->sendCommand(cmd_payload->cmd,
1479 cmd_payload->arg1,
1480 cmd_payload->arg2);
1481 result.status = rc;
1482 result.request_api = evt;
1483 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1484 m_parent->signalAPIResult(&result);
1485 }
1486 break;
1487 case QCAMERA_SM_EVT_TAKE_PICTURE:
1488 {
1489 rc = m_parent->takeLiveSnapshot();
1490 if (rc == 0) {
1491 m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
1492 }
1493 result.status = rc;
1494 result.request_api = evt;
1495 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1496 m_parent->signalAPIResult(&result);
1497 }
1498 break;
1499 case QCAMERA_SM_EVT_START_RECORDING:
1500 {
1501 // no ops here
1502 ALOGD("%s: already in recording state, no ops for start_recording", __func__);
1503 rc = 0;
1504 result.status = rc;
1505 result.request_api = evt;
1506 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1507 m_parent->signalAPIResult(&result);
1508 }
1509 break;
1510 case QCAMERA_SM_EVT_STOP_RECORDING:
1511 {
1512 rc = m_parent->stopRecording();
1513 m_state = QCAMERA_SM_STATE_PREVIEWING;
1514 result.status = rc;
1515 result.request_api = evt;
1516 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1517 m_parent->signalAPIResult(&result);
1518 }
1519 break;
1520 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1521 {
1522 rc = m_parent->releaseRecordingFrame((const void *)payload);
1523 result.status = rc;
1524 result.request_api = evt;
1525 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1526 m_parent->signalAPIResult(&result);
1527 }
1528 break;
1529 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1530 case QCAMERA_SM_EVT_START_PREVIEW:
1531 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1532 case QCAMERA_SM_EVT_STOP_PREVIEW:
1533 case QCAMERA_SM_EVT_RELEASE:
1534 {
1535 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1536 rc = INVALID_OPERATION;
1537 result.status = rc;
1538 result.request_api = evt;
1539 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1540 m_parent->signalAPIResult(&result);
1541 }
1542 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001543 case QCAMERA_SM_EVT_EVT_INTERNAL:
1544 {
1545 qcamera_sm_internal_evt_payload_t *internal_evt =
1546 (qcamera_sm_internal_evt_payload_t *)payload;
1547 switch (internal_evt->evt_type) {
1548 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1549 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1550 break;
1551 default:
1552 break;
1553 }
1554 }
1555 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001556 case QCAMERA_SM_EVT_EVT_NOTIFY:
1557 {
1558 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1559 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001560 case CAM_EVENT_TYPE_ZOOM_DONE:
1561 rc = m_parent->processZoomEvent(cam_evt->status);
1562 break;
1563 default:
1564 ALOGD("%s: no handling for server evt (%d) at this state",
1565 __func__, cam_evt->server_event_type);
1566 break;
1567 }
1568 }
1569 break;
1570 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -08001571 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Muhua Libc9a8082012-11-07 15:51:28 -08001572 default:
1573 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1574 break;
1575 }
1576
1577 return rc;
1578}
1579
Muhua Lida2c4be2012-11-26 09:14:16 -08001580/*===========================================================================
1581 * FUNCTION : procEvtVideoPicTakingState
1582 *
1583 * DESCRIPTION: finite state machine function to handle event in state of
1584 * QCAMERA_SM_STATE_VIDEO_PIC_TAKING.
1585 *
1586 * PARAMETERS :
1587 * @evt : event to be processed
1588 * @payload : event payload. Can be NULL if not needed.
1589 *
1590 * RETURN : int32_t type of status
1591 * NO_ERROR -- success
1592 * none-zero failure code
1593 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001594int32_t QCameraStateMachine::procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,
1595 void *payload)
1596{
1597 int32_t rc = NO_ERROR;
1598 qcamera_api_result_t result;
1599 memset(&result, 0, sizeof(qcamera_api_result_t));
1600
1601 switch (evt) {
1602 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1603 {
1604 // Error setting preview window during previewing
1605 ALOGE("Cannot set preview window when preview is running");
1606 rc = INVALID_OPERATION;
1607 result.status = rc;
1608 result.request_api = evt;
1609 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1610 m_parent->signalAPIResult(&result);
1611 }
1612 break;
1613 case QCAMERA_SM_EVT_SET_CALLBACKS:
1614 {
1615 qcamera_sm_evt_setcb_payload_t *setcbs =
1616 (qcamera_sm_evt_setcb_payload_t *)payload;
1617 rc = m_parent->setCallBacks(setcbs->notify_cb,
1618 setcbs->data_cb,
1619 setcbs->data_cb_timestamp,
1620 setcbs->get_memory,
1621 setcbs->user);
1622 result.status = rc;
1623 result.request_api = evt;
1624 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1625 m_parent->signalAPIResult(&result);
1626 }
1627 break;
1628 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1629 {
1630 rc = m_parent->enableMsgType(int32_t(payload));
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_DISABLE_MSG_TYPE:
1638 {
1639 rc = m_parent->disableMsgType(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_MSG_TYPE_ENABLED:
1647 {
1648 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1649 result.status = rc;
1650 result.request_api = evt;
1651 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1652 result.enabled = enabled;
1653 m_parent->signalAPIResult(&result);
1654 }
1655 break;
1656 case QCAMERA_SM_EVT_SET_PARAMS:
1657 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001658 bool needRestart = false;
1659 rc = m_parent->updateParameters((char*)payload, needRestart);
1660 if (rc == NO_ERROR) {
1661 if (needRestart) {
1662 // cannot set parameters that requires restart during recording
1663 ALOGE("%s: Cannot set parameters that requires restart during recording",
1664 __func__);
1665 rc = BAD_VALUE;
1666 } else {
1667 rc = m_parent->commitParameterChanges();
1668 }
1669 }
Muhua Libc9a8082012-11-07 15:51:28 -08001670 result.status = rc;
1671 result.request_api = evt;
1672 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1673 m_parent->signalAPIResult(&result);
1674 }
1675 break;
1676 case QCAMERA_SM_EVT_GET_PARAMS:
1677 {
1678 result.params = m_parent->getParameters();
1679 rc = NO_ERROR;
1680 result.status = rc;
1681 result.request_api = evt;
1682 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1683 m_parent->signalAPIResult(&result);
1684 }
1685 break;
1686 case QCAMERA_SM_EVT_PUT_PARAMS:
1687 {
1688 rc = m_parent->putParameters((char*)payload);
1689 result.status = rc;
1690 result.request_api = evt;
1691 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1692 m_parent->signalAPIResult(&result);
1693 }
1694 break;
1695 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1696 {
1697 rc = NO_ERROR;
1698 result.status = rc;
1699 result.request_api = evt;
1700 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1701 result.enabled = 1;
1702 m_parent->signalAPIResult(&result);
1703 }
1704 break;
1705 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1706 {
1707 rc = NO_ERROR;
1708 result.status = rc;
1709 result.request_api = evt;
1710 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1711 result.enabled = 1;
1712 m_parent->signalAPIResult(&result);
1713 }
1714 break;
1715 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1716 {
1717 rc = m_parent->storeMetaDataInBuffers(int(payload));
1718 result.status = rc;
1719 result.request_api = evt;
1720 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1721 m_parent->signalAPIResult(&result);
1722 }
1723 break;
1724 case QCAMERA_SM_EVT_DUMP:
1725 {
1726 rc = m_parent->dump((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_START_AUTO_FOCUS:
1734 {
1735 rc = m_parent->autoFocus();
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_STOP_AUTO_FOCUS:
1743 {
1744 rc = m_parent->cancelAutoFocus();
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_SEND_COMMAND:
1752 {
1753 qcamera_sm_evt_command_payload_t *cmd_payload =
1754 (qcamera_sm_evt_command_payload_t *)payload;
1755 rc = m_parent->sendCommand(cmd_payload->cmd,
1756 cmd_payload->arg1,
1757 cmd_payload->arg2);
1758 result.status = rc;
1759 result.request_api = evt;
1760 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1761 m_parent->signalAPIResult(&result);
1762 }
1763 break;
1764 case QCAMERA_SM_EVT_STOP_RECORDING:
1765 {
1766 rc = m_parent->stopRecording();
1767 m_state = QCAMERA_SM_STATE_PREVIEW_PIC_TAKING;
1768 result.status = rc;
1769 result.request_api = evt;
1770 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1771 m_parent->signalAPIResult(&result);
1772 }
1773 break;
1774 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1775 {
1776 rc = m_parent->releaseRecordingFrame((const void *)payload);
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_CANCEL_PICTURE:
1784 {
1785 rc = m_parent->cancelLiveSnapshot();
1786 m_state = QCAMERA_SM_STATE_RECORDING;
1787 result.status = rc;
1788 result.request_api = evt;
1789 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1790 m_parent->signalAPIResult(&result);
1791 }
1792 break;
1793 case QCAMERA_SM_EVT_START_RECORDING:
1794 case QCAMERA_SM_EVT_START_PREVIEW:
1795 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1796 case QCAMERA_SM_EVT_STOP_PREVIEW:
1797 case QCAMERA_SM_EVT_TAKE_PICTURE:
1798 case QCAMERA_SM_EVT_RELEASE:
1799 {
1800 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1801 rc = INVALID_OPERATION;
1802 result.status = rc;
1803 result.request_api = evt;
1804 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1805 m_parent->signalAPIResult(&result);
1806 }
1807 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001808 case QCAMERA_SM_EVT_EVT_INTERNAL:
1809 {
1810 qcamera_sm_internal_evt_payload_t *internal_evt =
1811 (qcamera_sm_internal_evt_payload_t *)payload;
1812 switch (internal_evt->evt_type) {
1813 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1814 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1815 break;
1816 default:
1817 break;
1818 }
1819 }
1820 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001821 case QCAMERA_SM_EVT_EVT_NOTIFY:
1822 {
1823 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1824 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001825 case CAM_EVENT_TYPE_ZOOM_DONE:
1826 rc = m_parent->processZoomEvent(cam_evt->status);
1827 break;
1828 default:
1829 ALOGD("%s: no handling for server evt (%d) at this state",
1830 __func__, cam_evt->server_event_type);
1831 break;
1832 }
1833 }
1834 break;
1835 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1836 {
1837 qcamera_jpeg_evt_payload_t *jpeg_job =
1838 (qcamera_jpeg_evt_payload_t *)payload;
1839 rc = m_parent->processJpegNotify(jpeg_job);
1840 }
1841 break;
1842 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1843 {
1844 rc = m_parent->cancelLiveSnapshot();
1845 m_state = QCAMERA_SM_STATE_RECORDING;
1846 }
1847 break;
1848 default:
1849 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1850 break;
1851 }
1852
1853 return rc;
1854}
1855
Muhua Lida2c4be2012-11-26 09:14:16 -08001856/*===========================================================================
1857 * FUNCTION : procEvtPreviewPicTakingState
1858 *
1859 * DESCRIPTION: finite state machine function to handle event in state of
1860 * QCAMERA_SM_STATE_PREVIEW_PIC_TAKING.
1861 *
1862 * PARAMETERS :
1863 * @evt : event to be processed
1864 * @payload : event payload. Can be NULL if not needed.
1865 *
1866 * RETURN : int32_t type of status
1867 * NO_ERROR -- success
1868 * none-zero failure code
1869 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001870int32_t QCameraStateMachine::procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,
1871 void *payload)
1872{
1873 int32_t rc = NO_ERROR;
1874 qcamera_api_result_t result;
1875 memset(&result, 0, sizeof(qcamera_api_result_t));
1876
1877 switch (evt) {
1878 case QCAMERA_SM_EVT_SET_CALLBACKS:
1879 {
1880 qcamera_sm_evt_setcb_payload_t *setcbs =
1881 (qcamera_sm_evt_setcb_payload_t *)payload;
1882 rc = m_parent->setCallBacks(setcbs->notify_cb,
1883 setcbs->data_cb,
1884 setcbs->data_cb_timestamp,
1885 setcbs->get_memory,
1886 setcbs->user);
1887 result.status = rc;
1888 result.request_api = evt;
1889 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1890 m_parent->signalAPIResult(&result);
1891 }
1892 break;
1893 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1894 {
1895 rc = m_parent->enableMsgType(int32_t(payload));
1896 result.status = rc;
1897 result.request_api = evt;
1898 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1899 m_parent->signalAPIResult(&result);
1900 }
1901 break;
1902 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1903 {
1904 rc = m_parent->disableMsgType(int32_t(payload));
1905 result.status = rc;
1906 result.request_api = evt;
1907 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1908 m_parent->signalAPIResult(&result);
1909 }
1910 break;
1911 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1912 {
1913 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1914 result.status = rc;
1915 result.request_api = evt;
1916 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1917 result.enabled = enabled;
1918 m_parent->signalAPIResult(&result);
1919 }
1920 break;
1921 case QCAMERA_SM_EVT_SET_PARAMS:
1922 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001923 bool needRestart = false;
1924 rc = m_parent->updateParameters((char*)payload, needRestart);
1925 if (rc == NO_ERROR) {
1926 if (needRestart) {
1927 // need restart preview for parameters to take effect
1928 // stop preview
1929 m_parent->stopPreview();
1930 // commit parameter changes to server
1931 rc = m_parent->commitParameterChanges();
1932 // start preview again
1933 m_parent->startPreview();
1934 } else {
1935 rc = m_parent->commitParameterChanges();
1936 }
1937 }
Muhua Libc9a8082012-11-07 15:51:28 -08001938 result.status = rc;
1939 result.request_api = evt;
1940 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1941 m_parent->signalAPIResult(&result);
1942 }
1943 break;
1944 case QCAMERA_SM_EVT_GET_PARAMS:
1945 {
1946 result.params = m_parent->getParameters();
1947 rc = NO_ERROR;
1948 result.status = rc;
1949 result.request_api = evt;
1950 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1951 m_parent->signalAPIResult(&result);
1952 }
1953 break;
1954 case QCAMERA_SM_EVT_PUT_PARAMS:
1955 {
1956 rc = m_parent->putParameters((char*)payload);
1957 result.status = rc;
1958 result.request_api = evt;
1959 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1960 m_parent->signalAPIResult(&result);
1961 }
1962 break;
1963 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1964 {
1965 rc = NO_ERROR;
1966 result.status = rc;
1967 result.request_api = evt;
1968 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1969 result.enabled = 1;
1970 m_parent->signalAPIResult(&result);
1971 }
1972 break;
1973 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1974 {
1975 rc = NO_ERROR;
1976 result.status = rc;
1977 result.request_api = evt;
1978 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1979 result.enabled = 0;
1980 m_parent->signalAPIResult(&result);
1981 }
1982 break;
1983 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1984 {
1985 rc = m_parent->storeMetaDataInBuffers(int(payload));
1986 result.status = rc;
1987 result.request_api = evt;
1988 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1989 m_parent->signalAPIResult(&result);
1990 }
1991 break;
1992 case QCAMERA_SM_EVT_DUMP:
1993 {
1994 rc = m_parent->dump((int)payload);
1995 result.status = rc;
1996 result.request_api = evt;
1997 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1998 m_parent->signalAPIResult(&result);
1999 }
2000 break;
2001 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2002 {
2003 rc = m_parent->autoFocus();
2004 result.status = rc;
2005 result.request_api = evt;
2006 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2007 m_parent->signalAPIResult(&result);
2008 }
2009 break;
2010 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2011 {
2012 rc = m_parent->cancelAutoFocus();
2013 result.status = rc;
2014 result.request_api = evt;
2015 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2016 m_parent->signalAPIResult(&result);
2017 }
2018 break;
2019 case QCAMERA_SM_EVT_SEND_COMMAND:
2020 {
2021 qcamera_sm_evt_command_payload_t *cmd_payload =
2022 (qcamera_sm_evt_command_payload_t *)payload;
2023 rc = m_parent->sendCommand(cmd_payload->cmd,
2024 cmd_payload->arg1,
2025 cmd_payload->arg2);
2026 result.status = rc;
2027 result.request_api = evt;
2028 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2029 m_parent->signalAPIResult(&result);
2030 }
2031 break;
2032 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2033 {
2034 rc = m_parent->releaseRecordingFrame((const void *)payload);
2035 result.status = rc;
2036 result.request_api = evt;
2037 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2038 m_parent->signalAPIResult(&result);
2039 }
2040 break;
2041 case QCAMERA_SM_EVT_CANCEL_PICTURE:
2042 {
2043 rc = m_parent->cancelLiveSnapshot();
2044 m_state = QCAMERA_SM_STATE_PREVIEWING;
2045 result.status = rc;
2046 result.request_api = evt;
2047 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2048 m_parent->signalAPIResult(&result);
2049 }
2050 break;
2051 case QCAMERA_SM_EVT_STOP_PREVIEW:
2052 {
2053 m_parent->stopChannel(QCAMERA_CH_TYPE_PREVIEW);
2054 m_parent->delChannel(QCAMERA_CH_TYPE_PREVIEW);
2055 m_parent->delChannel(QCAMERA_CH_TYPE_VIDEO);
2056 m_state = QCAMERA_SM_STATE_PIC_TAKING;
2057 rc = NO_ERROR;
2058 result.status = rc;
2059 result.request_api = evt;
2060 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2061 m_parent->signalAPIResult(&result);
2062 }
2063 break;
2064 case QCAMERA_SM_EVT_START_RECORDING:
2065 {
2066 rc = m_parent->stopRecording();
2067 if (rc == NO_ERROR) {
2068 m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
2069 }
2070 result.status = rc;
2071 result.request_api = evt;
2072 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2073 m_parent->signalAPIResult(&result);
2074 }
2075 break;
2076 case QCAMERA_SM_EVT_STOP_RECORDING:
2077 case QCAMERA_SM_EVT_START_PREVIEW:
2078 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2079 case QCAMERA_SM_EVT_TAKE_PICTURE:
2080 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2081 case QCAMERA_SM_EVT_RELEASE:
2082 {
2083 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2084 rc = INVALID_OPERATION;
2085 result.status = rc;
2086 result.request_api = evt;
2087 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2088 m_parent->signalAPIResult(&result);
2089 }
2090 break;
Muhua Li31eaee02012-12-11 08:56:45 -08002091 case QCAMERA_SM_EVT_EVT_INTERNAL:
2092 {
2093 qcamera_sm_internal_evt_payload_t *internal_evt =
2094 (qcamera_sm_internal_evt_payload_t *)payload;
2095 switch (internal_evt->evt_type) {
2096 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2097 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2098 break;
2099 default:
2100 break;
2101 }
2102 }
2103 break;
Muhua Libc9a8082012-11-07 15:51:28 -08002104 case QCAMERA_SM_EVT_EVT_NOTIFY:
2105 {
2106 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2107 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08002108 case CAM_EVENT_TYPE_ZOOM_DONE:
2109 rc = m_parent->processZoomEvent(cam_evt->status);
2110 break;
2111 default:
2112 ALOGD("%s: no handling for server evt (%d) at this state",
2113 __func__, cam_evt->server_event_type);
2114 break;
2115 }
2116 }
2117 break;
2118 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2119 {
2120 qcamera_jpeg_evt_payload_t *jpeg_job =
2121 (qcamera_jpeg_evt_payload_t *)payload;
2122 rc = m_parent->processJpegNotify(jpeg_job);
2123 }
2124 break;
2125 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2126 {
2127 rc = m_parent->cancelLiveSnapshot();
2128 m_state = QCAMERA_SM_STATE_PREVIEWING;
2129 }
2130 break;
2131 default:
2132 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2133 break;
2134 }
2135
2136 return rc;
2137}
2138
Muhua Lida2c4be2012-11-26 09:14:16 -08002139/*===========================================================================
2140 * FUNCTION : isPreviewRunning
2141 *
2142 * DESCRIPTION: check if preview is in process.
2143 *
2144 * PARAMETERS : None
2145 *
2146 * RETURN : true -- preview running
2147 * false -- preview stopped
2148 *==========================================================================*/
2149bool QCameraStateMachine::isPreviewRunning()
2150{
2151 switch (m_state) {
2152 case QCAMERA_SM_STATE_PREVIEWING:
2153 case QCAMERA_SM_STATE_RECORDING:
2154 case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
2155 case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
2156 return true;
2157 default:
2158 return false;
2159 }
2160}
2161
Shuzhen Wang89635cf2012-12-20 13:47:22 -08002162}; // namespace qcamera