blob: 5c9eee41ca92de54bd1762ab1d1e4f02e755f66e [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) {
Muhua Li6d69e932013-01-24 16:39:27 -0800609 if (needRestart) {
610 // need restart preview for parameters to take effect
611 m_parent->unpreparePreview();
612 // commit parameter changes to server
613 m_parent->commitParameterChanges();
614 // prepare preview again
615 rc = m_parent->preparePreview();
616 if (rc != NO_ERROR) {
617 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
618 }
619 } else {
620 rc = m_parent->commitParameterChanges();
621 }
Muhua Lida2c4be2012-11-26 09:14:16 -0800622 }
Muhua Li6d69e932013-01-24 16:39:27 -0800623
Muhua Libc9a8082012-11-07 15:51:28 -0800624 result.status = rc;
625 result.request_api = evt;
626 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
627 m_parent->signalAPIResult(&result);
628 }
629 break;
630 case QCAMERA_SM_EVT_GET_PARAMS:
631 {
632 result.params = m_parent->getParameters();
633 rc = NO_ERROR;
634 result.status = rc;
635 result.request_api = evt;
636 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
637 m_parent->signalAPIResult(&result);
638 }
639 break;
640 case QCAMERA_SM_EVT_PUT_PARAMS:
641 {
642 rc = m_parent->putParameters((char*)payload);
643 result.status = rc;
644 result.request_api = evt;
645 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
646 m_parent->signalAPIResult(&result);
647 }
648 break;
649 case QCAMERA_SM_EVT_START_PREVIEW:
650 {
651 // no ops here
652 rc = NO_ERROR;
653 result.status = rc;
654 result.request_api = evt;
655 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
656 m_parent->signalAPIResult(&result);
657 }
658 break;
659 case QCAMERA_SM_EVT_STOP_PREVIEW:
660 {
661 m_parent->unpreparePreview();
662 rc = 0;
663 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
664 result.status = rc;
665 result.request_api = evt;
666 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
667 m_parent->signalAPIResult(&result);
668 }
669 break;
670 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
671 {
672 rc = NO_ERROR;
673 result.status = rc;
674 result.request_api = evt;
675 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
676 result.enabled = 1;
677 m_parent->signalAPIResult(&result);
678 }
679 break;
680 case QCAMERA_SM_EVT_RECORDING_ENABLED:
681 {
682 rc = 0;
683 result.status = rc;
684 result.request_api = evt;
685 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
686 result.enabled = 0;
687 m_parent->signalAPIResult(&result);
688 }
689 break;
690 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
691 {
692 rc = m_parent->storeMetaDataInBuffers(int(payload));
693 result.status = rc;
694 result.request_api = evt;
695 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
696 m_parent->signalAPIResult(&result);
697 }
698 break;
699 case QCAMERA_SM_EVT_DUMP:
700 {
701 rc = m_parent->dump((int)payload);
702 result.status = rc;
703 result.request_api = evt;
704 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
705 m_parent->signalAPIResult(&result);
706 }
707 break;
708 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
709 {
710 rc = m_parent->autoFocus();
711 result.status = rc;
712 result.request_api = evt;
713 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
714 m_parent->signalAPIResult(&result);
715 }
716 break;
717 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
718 {
719 rc = m_parent->cancelAutoFocus();
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_SEND_COMMAND:
727 {
728 qcamera_sm_evt_command_payload_t *cmd_payload =
729 (qcamera_sm_evt_command_payload_t *)payload;
730 rc = m_parent->sendCommand(cmd_payload->cmd,
731 cmd_payload->arg1,
732 cmd_payload->arg2);
733 result.status = rc;
734 result.request_api = evt;
735 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
736 m_parent->signalAPIResult(&result);
737 }
738 break;
739 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
740 case QCAMERA_SM_EVT_START_RECORDING:
741 case QCAMERA_SM_EVT_STOP_RECORDING:
742 case QCAMERA_SM_EVT_TAKE_PICTURE:
743 case QCAMERA_SM_EVT_CANCEL_PICTURE:
744 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
745 case QCAMERA_SM_EVT_RELEASE:
746 {
747 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
748 rc = INVALID_OPERATION;
749 result.status = rc;
750 result.request_api = evt;
751 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
752 m_parent->signalAPIResult(&result);
753 }
754 break;
Muhua Li31eaee02012-12-11 08:56:45 -0800755 case QCAMERA_SM_EVT_EVT_INTERNAL:
Muhua Libc9a8082012-11-07 15:51:28 -0800756 case QCAMERA_SM_EVT_EVT_NOTIFY:
757 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -0800758 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Shuzhen Wangaa829ce2013-01-09 14:41:49 -0800759 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
Muhua Libc9a8082012-11-07 15:51:28 -0800760 default:
761 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
762 break;
763 }
764
765 return rc;
766}
767
Muhua Lida2c4be2012-11-26 09:14:16 -0800768/*===========================================================================
769 * FUNCTION : procEvtPreviewingState
770 *
771 * DESCRIPTION: finite state machine function to handle event in state of
772 * QCAMERA_SM_STATE_PREVIEWING.
773 *
774 * PARAMETERS :
775 * @evt : event to be processed
776 * @payload : event payload. Can be NULL if not needed.
777 *
778 * RETURN : int32_t type of status
779 * NO_ERROR -- success
780 * none-zero failure code
781 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800782int32_t QCameraStateMachine::procEvtPreviewingState(qcamera_sm_evt_enum_t evt,
783 void *payload)
784{
785 int32_t rc = NO_ERROR;
786 qcamera_api_result_t result;
787 memset(&result, 0, sizeof(qcamera_api_result_t));
788
789 switch (evt) {
790 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
791 {
792 // Error setting preview window during previewing
793 ALOGE("Cannot set preview window when preview is running");
794 rc = INVALID_OPERATION;
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_SET_CALLBACKS:
802 {
803 qcamera_sm_evt_setcb_payload_t *setcbs =
804 (qcamera_sm_evt_setcb_payload_t *)payload;
805 rc = m_parent->setCallBacks(setcbs->notify_cb,
806 setcbs->data_cb,
807 setcbs->data_cb_timestamp,
808 setcbs->get_memory,
809 setcbs->user);
810 result.status = rc;
811 result.request_api = evt;
812 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
813 m_parent->signalAPIResult(&result);
814 }
815 break;
816 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
817 {
818 rc = m_parent->enableMsgType(int32_t(payload));
819 result.status = rc;
820 result.request_api = evt;
821 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
822 m_parent->signalAPIResult(&result);
823 }
824 break;
825 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
826 {
827 rc = m_parent->disableMsgType(int32_t(payload));
828 result.status = rc;
829 result.request_api = evt;
830 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
831 m_parent->signalAPIResult(&result);
832 }
833 break;
834 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
835 {
836 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
837 result.status = rc;
838 result.request_api = evt;
839 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
840 result.enabled = enabled;
841 m_parent->signalAPIResult(&result);
842 }
843 break;
844 case QCAMERA_SM_EVT_SET_PARAMS:
845 {
Muhua Lida2c4be2012-11-26 09:14:16 -0800846 bool needRestart = false;
847 rc = m_parent->updateParameters((char*)payload, needRestart);
848 if (rc == NO_ERROR) {
849 if (needRestart) {
850 // need restart preview for parameters to take effect
851 // stop preview
852 m_parent->stopPreview();
853 // commit parameter changes to server
Muhua Li66f3b532013-01-23 17:19:05 -0800854 m_parent->commitParameterChanges();
Muhua Lida2c4be2012-11-26 09:14:16 -0800855 // start preview again
Muhua Li66f3b532013-01-23 17:19:05 -0800856 rc = m_parent->preparePreview();
857 if (rc == NO_ERROR) {
858 rc = m_parent->startPreview();
859 if (rc != NO_ERROR) {
860 m_parent->unpreparePreview();
861 }
862 }
863 if (rc != NO_ERROR) {
864 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
865 }
Muhua Lida2c4be2012-11-26 09:14:16 -0800866 } else {
867 rc = m_parent->commitParameterChanges();
868 }
869 }
Muhua Libc9a8082012-11-07 15:51:28 -0800870 result.status = rc;
871 result.request_api = evt;
872 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
873 m_parent->signalAPIResult(&result);
874 }
875 break;
876 case QCAMERA_SM_EVT_GET_PARAMS:
877 {
878 result.params = m_parent->getParameters();
879 rc = NO_ERROR;
880 result.status = rc;
881 result.request_api = evt;
882 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
883 m_parent->signalAPIResult(&result);
884 }
885 break;
886 case QCAMERA_SM_EVT_PUT_PARAMS:
887 {
888 rc = m_parent->putParameters((char*)payload);
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_START_PREVIEW:
896 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
897 {
898 // no ops here
899 ALOGD("%s: Already in previewing, no ops here to start preview", __func__);
900 rc = NO_ERROR;
901 result.status = rc;
902 result.request_api = evt;
903 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
904 m_parent->signalAPIResult(&result);
905 }
906 break;
907 case QCAMERA_SM_EVT_STOP_PREVIEW:
908 {
909 rc = m_parent->stopPreview();
910 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
911 result.status = rc;
912 result.request_api = evt;
913 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
914 m_parent->signalAPIResult(&result);
915 }
916 break;
917 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
918 {
919 rc = NO_ERROR;
920 result.status = rc;
921 result.request_api = evt;
922 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
923 result.enabled = 1;
924 m_parent->signalAPIResult(&result);
925 }
926 break;
927 case QCAMERA_SM_EVT_RECORDING_ENABLED:
928 {
929 rc = NO_ERROR;
930 result.status = rc;
931 result.request_api = evt;
932 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
933 result.enabled = 0;
934 m_parent->signalAPIResult(&result);
935 }
936 break;
937 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
938 {
939 rc = m_parent->storeMetaDataInBuffers(int(payload));
940 result.status = rc;
941 result.request_api = evt;
942 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
943 m_parent->signalAPIResult(&result);
944 }
945 break;
946 case QCAMERA_SM_EVT_DUMP:
947 {
948 rc = m_parent->dump((int)payload);
949 result.status = rc;
950 result.request_api = evt;
951 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
952 m_parent->signalAPIResult(&result);
953 }
954 break;
955 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
956 {
957 rc = m_parent->autoFocus();
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_STOP_AUTO_FOCUS:
965 {
966 rc = m_parent->cancelAutoFocus();
967 result.status = rc;
968 result.request_api = evt;
969 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
970 m_parent->signalAPIResult(&result);
971 }
972 break;
973 case QCAMERA_SM_EVT_START_RECORDING:
974 {
975 rc = m_parent->startRecording();
976 if (rc == NO_ERROR) {
977 // move state to recording state
978 m_state = QCAMERA_SM_STATE_RECORDING;
979 }
980 result.status = rc;
981 result.request_api = evt;
982 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
983 m_parent->signalAPIResult(&result);
984 }
985 break;
986 case QCAMERA_SM_EVT_TAKE_PICTURE:
987 {
988 rc = m_parent->takePicture();
989 if (rc == NO_ERROR) {
990 // move state to picture taking state
Muhua Li6d69e932013-01-24 16:39:27 -0800991 if (m_parent->isZSLMode()) {
992 m_state = QCAMERA_SM_STATE_PREVIEW_PIC_TAKING;
993 } else {
994 m_state = QCAMERA_SM_STATE_PIC_TAKING;
995 }
Muhua Libc9a8082012-11-07 15:51:28 -0800996 } else {
997 // move state to preview stopped state
998 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
999 }
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;
1006 case QCAMERA_SM_EVT_SEND_COMMAND:
1007 {
1008 qcamera_sm_evt_command_payload_t *cmd_payload =
1009 (qcamera_sm_evt_command_payload_t *)payload;
1010 rc = m_parent->sendCommand(cmd_payload->cmd,
1011 cmd_payload->arg1,
1012 cmd_payload->arg2);
1013 result.status = rc;
1014 result.request_api = evt;
1015 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1016 m_parent->signalAPIResult(&result);
1017 }
1018 break;
1019 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1020 case QCAMERA_SM_EVT_STOP_RECORDING:
1021 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1022 case QCAMERA_SM_EVT_RELEASE:
1023 {
1024 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1025 rc = INVALID_OPERATION;
1026 result.status = rc;
1027 result.request_api = evt;
1028 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1029 m_parent->signalAPIResult(&result);
1030 }
1031 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001032 case QCAMERA_SM_EVT_EVT_INTERNAL:
1033 {
1034 qcamera_sm_internal_evt_payload_t *internal_evt =
1035 (qcamera_sm_internal_evt_payload_t *)payload;
1036 switch (internal_evt->evt_type) {
1037 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1038 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1039 break;
1040 default:
1041 break;
1042 }
1043 }
1044 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001045 case QCAMERA_SM_EVT_EVT_NOTIFY:
1046 {
1047 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1048 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001049 case CAM_EVENT_TYPE_ZOOM_DONE:
1050 rc = m_parent->processZoomEvent(cam_evt->status);
1051 break;
1052 default:
1053 ALOGD("%s: no handling for server evt (%d) at this state",
1054 __func__, cam_evt->server_event_type);
1055 break;
1056 }
1057 }
1058 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001059 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1060 //TODO: Adjust FPS.
1061 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001062 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -08001063 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Muhua Libc9a8082012-11-07 15:51:28 -08001064 default:
1065 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1066 break;
1067 }
1068
1069 return rc;
1070}
1071
Muhua Lida2c4be2012-11-26 09:14:16 -08001072/*===========================================================================
1073 * FUNCTION : procEvtPicTakingState
1074 *
1075 * DESCRIPTION: finite state machine function to handle event in state of
1076 * QCAMERA_SM_STATE_PIC_TAKING.
1077 *
1078 * PARAMETERS :
1079 * @evt : event to be processed
1080 * @payload : event payload. Can be NULL if not needed.
1081 *
1082 * RETURN : int32_t type of status
1083 * NO_ERROR -- success
1084 * none-zero failure code
1085 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001086int32_t QCameraStateMachine::procEvtPicTakingState(qcamera_sm_evt_enum_t evt,
1087 void *payload)
1088{
1089 int32_t rc = NO_ERROR;
1090 qcamera_api_result_t result;
1091 memset(&result, 0, sizeof(qcamera_api_result_t));
1092
1093 switch (evt) {
1094 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1095 {
1096 // Error setting preview window during previewing
1097 ALOGE("Cannot set preview window when preview is running");
1098 rc = INVALID_OPERATION;
1099 result.status = rc;
1100 result.request_api = evt;
1101 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1102 m_parent->signalAPIResult(&result);
1103 }
1104 break;
1105 case QCAMERA_SM_EVT_SET_CALLBACKS:
1106 {
1107 qcamera_sm_evt_setcb_payload_t *setcbs =
1108 (qcamera_sm_evt_setcb_payload_t *)payload;
1109 rc = m_parent->setCallBacks(setcbs->notify_cb,
1110 setcbs->data_cb,
1111 setcbs->data_cb_timestamp,
1112 setcbs->get_memory,
1113 setcbs->user);
1114 result.status = rc;
1115 result.request_api = evt;
1116 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1117 m_parent->signalAPIResult(&result);
1118 }
1119 break;
1120 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1121 {
1122 rc = m_parent->enableMsgType(int32_t(payload));
1123 result.status = rc;
1124 result.request_api = evt;
1125 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1126 m_parent->signalAPIResult(&result);
1127 }
1128 break;
1129 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1130 {
1131 rc = m_parent->disableMsgType(int32_t(payload));
1132 result.status = rc;
1133 result.request_api = evt;
1134 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1135 m_parent->signalAPIResult(&result);
1136 }
1137 break;
1138 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1139 {
1140 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1141 result.status = rc;
1142 result.request_api = evt;
1143 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1144 result.enabled = enabled;
1145 m_parent->signalAPIResult(&result);
1146 }
1147 break;
1148 case QCAMERA_SM_EVT_SET_PARAMS:
1149 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001150 bool needRestart = false;
1151 rc = m_parent->updateParameters((char*)payload, needRestart);
1152 if (rc == NO_ERROR) {
1153 rc = m_parent->commitParameterChanges();
1154 }
Muhua Libc9a8082012-11-07 15:51:28 -08001155 result.status = rc;
1156 result.request_api = evt;
1157 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1158 m_parent->signalAPIResult(&result);
1159 }
1160 break;
1161 case QCAMERA_SM_EVT_GET_PARAMS:
1162 {
1163 result.params = m_parent->getParameters();
1164 rc = NO_ERROR;
1165 result.status = rc;
1166 result.request_api = evt;
1167 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1168 m_parent->signalAPIResult(&result);
1169 }
1170 break;
1171 case QCAMERA_SM_EVT_PUT_PARAMS:
1172 {
1173 rc = m_parent->putParameters((char*)payload);
1174 result.status = rc;
1175 result.request_api = evt;
1176 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1177 m_parent->signalAPIResult(&result);
1178 }
1179 break;
1180 case QCAMERA_SM_EVT_STOP_PREVIEW:
1181 {
1182 // no ops, since preview is stopped (normal),
1183 // or preview msg type is disabled (ZSL)
1184 rc = NO_ERROR;
1185 result.status = rc;
1186 result.request_api = evt;
1187 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1188 m_parent->signalAPIResult(&result);
1189 }
1190 break;
1191 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1192 {
1193 rc = NO_ERROR;
1194 result.status = rc;
1195 result.request_api = evt;
1196 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1197 result.enabled = 0;
1198 m_parent->signalAPIResult(&result);
1199 }
1200 break;
1201 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1202 {
1203 rc = NO_ERROR;
1204 result.status = rc;
1205 result.request_api = evt;
1206 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1207 result.enabled = 0;
1208 m_parent->signalAPIResult(&result);
1209 }
1210 break;
1211 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1212 {
1213 rc = m_parent->storeMetaDataInBuffers(int(payload));
1214 result.status = rc;
1215 result.request_api = evt;
1216 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1217 m_parent->signalAPIResult(&result);
1218 }
1219 break;
1220 case QCAMERA_SM_EVT_DUMP:
1221 {
1222 rc = m_parent->dump((int)payload);
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_START_AUTO_FOCUS:
1230 {
1231 rc = m_parent->autoFocus();
1232 result.status = rc;
1233 result.request_api = evt;
1234 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1235 m_parent->signalAPIResult(&result);
1236 }
1237 break;
1238 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1239 {
1240 rc = m_parent->cancelAutoFocus();
1241 result.status = rc;
1242 result.request_api = evt;
1243 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1244 m_parent->signalAPIResult(&result);
1245 }
1246 break;
1247 case QCAMERA_SM_EVT_SEND_COMMAND:
1248 {
1249 qcamera_sm_evt_command_payload_t *cmd_payload =
1250 (qcamera_sm_evt_command_payload_t *)payload;
1251 rc = m_parent->sendCommand(cmd_payload->cmd,
1252 cmd_payload->arg1,
1253 cmd_payload->arg2);
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;
1260 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1261 {
1262 rc = m_parent->cancelPicture();
1263 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1264 result.status = rc;
1265 result.request_api = evt;
1266 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1267 m_parent->signalAPIResult(&result);
1268 }
1269 break;
1270 case QCAMERA_SM_EVT_TAKE_PICTURE:
1271 case QCAMERA_SM_EVT_START_RECORDING:
1272 case QCAMERA_SM_EVT_STOP_RECORDING:
1273 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1274 case QCAMERA_SM_EVT_START_PREVIEW:
1275 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1276 case QCAMERA_SM_EVT_RELEASE:
1277 {
1278 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1279 rc = INVALID_OPERATION;
1280 result.status = rc;
1281 result.request_api = evt;
1282 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1283 m_parent->signalAPIResult(&result);
1284 }
1285 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001286 case QCAMERA_SM_EVT_EVT_INTERNAL:
1287 {
1288 qcamera_sm_internal_evt_payload_t *internal_evt =
1289 (qcamera_sm_internal_evt_payload_t *)payload;
1290 switch (internal_evt->evt_type) {
1291 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1292 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1293 break;
1294 default:
1295 break;
1296 }
1297 }
1298 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001299 case QCAMERA_SM_EVT_EVT_NOTIFY:
1300 {
1301 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1302 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001303 case CAM_EVENT_TYPE_ZOOM_DONE:
1304 rc = m_parent->processZoomEvent(cam_evt->status);
1305 break;
1306 default:
1307 ALOGD("%s: no handling for server evt (%d) at this state",
1308 __func__, cam_evt->server_event_type);
1309 break;
1310 }
1311 }
1312 break;
1313 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1314 {
1315 qcamera_jpeg_evt_payload_t *jpeg_job =
1316 (qcamera_jpeg_evt_payload_t *)payload;
1317 rc = m_parent->processJpegNotify(jpeg_job);
1318 }
1319 break;
1320 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1321 {
1322 rc = m_parent->cancelPicture();
1323 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1324 }
1325 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001326 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
Muhua Libc9a8082012-11-07 15:51:28 -08001327 default:
1328 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1329 break;
1330 }
1331
1332 return rc;
1333}
1334
Muhua Lida2c4be2012-11-26 09:14:16 -08001335/*===========================================================================
1336 * FUNCTION : procEvtRecordingState
1337 *
1338 * DESCRIPTION: finite state machine function to handle event in state of
1339 * QCAMERA_SM_STATE_RECORDING.
1340 *
1341 * PARAMETERS :
1342 * @evt : event to be processed
1343 * @payload : event payload. Can be NULL if not needed.
1344 *
1345 * RETURN : int32_t type of status
1346 * NO_ERROR -- success
1347 * none-zero failure code
1348 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001349int32_t QCameraStateMachine::procEvtRecordingState(qcamera_sm_evt_enum_t evt,
1350 void *payload)
1351{
1352 int32_t rc = NO_ERROR;
1353 qcamera_api_result_t result;
1354 memset(&result, 0, sizeof(qcamera_api_result_t));
1355
1356 switch (evt) {
1357 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1358 {
1359 // Error setting preview window during previewing
1360 ALOGE("Cannot set preview window when preview is running");
1361 rc = INVALID_OPERATION;
1362 result.status = rc;
1363 result.request_api = evt;
1364 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1365 m_parent->signalAPIResult(&result);
1366 }
1367 break;
1368 case QCAMERA_SM_EVT_SET_CALLBACKS:
1369 {
1370 qcamera_sm_evt_setcb_payload_t *setcbs =
1371 (qcamera_sm_evt_setcb_payload_t *)payload;
1372 rc = m_parent->setCallBacks(setcbs->notify_cb,
1373 setcbs->data_cb,
1374 setcbs->data_cb_timestamp,
1375 setcbs->get_memory,
1376 setcbs->user);
1377 result.status = rc;
1378 result.request_api = evt;
1379 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1380 m_parent->signalAPIResult(&result);
1381 }
1382 break;
1383 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1384 {
1385 rc = m_parent->enableMsgType(int32_t(payload));
1386 result.status = rc;
1387 result.request_api = evt;
1388 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1389 m_parent->signalAPIResult(&result);
1390 }
1391 break;
1392 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1393 {
1394 rc = m_parent->disableMsgType(int32_t(payload));
1395 result.status = rc;
1396 result.request_api = evt;
1397 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1398 m_parent->signalAPIResult(&result);
1399 }
1400 break;
1401 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1402 {
1403 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1404 result.status = rc;
1405 result.request_api = evt;
1406 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1407 result.enabled = enabled;
1408 m_parent->signalAPIResult(&result);
1409 }
1410 break;
1411 case QCAMERA_SM_EVT_SET_PARAMS:
1412 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001413 bool needRestart = false;
1414 rc = m_parent->updateParameters((char*)payload, needRestart);
1415 if (rc == NO_ERROR) {
1416 if (needRestart) {
1417 // cannot set parameters that requires restart during recording
1418 ALOGE("%s: Cannot set parameters that requires restart during recording",
1419 __func__);
1420 rc = BAD_VALUE;
1421 } else {
1422 rc = m_parent->commitParameterChanges();
1423 }
1424 }
Muhua Libc9a8082012-11-07 15:51:28 -08001425 result.status = rc;
1426 result.request_api = evt;
1427 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1428 m_parent->signalAPIResult(&result);
1429 }
1430 break;
1431 case QCAMERA_SM_EVT_GET_PARAMS:
1432 {
1433 result.params = m_parent->getParameters();
1434 rc = NO_ERROR;
1435 result.status = rc;
1436 result.request_api = evt;
1437 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1438 m_parent->signalAPIResult(&result);
1439 }
1440 break;
1441 case QCAMERA_SM_EVT_PUT_PARAMS:
1442 {
1443 rc = m_parent->putParameters((char*)payload);
1444 result.status = rc;
1445 result.request_api = evt;
1446 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1447 m_parent->signalAPIResult(&result);
1448 }
1449 break;
1450 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1451 {
1452 rc = NO_ERROR;
1453 result.status = rc;
1454 result.request_api = evt;
1455 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1456 result.enabled = 0;
1457 m_parent->signalAPIResult(&result);
1458 }
1459 break;
1460 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1461 {
1462 rc = NO_ERROR;
1463 result.status = rc;
1464 result.request_api = evt;
1465 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1466 result.enabled = 1;
1467 m_parent->signalAPIResult(&result);
1468 }
1469 break;
1470 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1471 {
1472 rc = m_parent->storeMetaDataInBuffers(int(payload));
1473 result.status = rc;
1474 result.request_api = evt;
1475 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1476 m_parent->signalAPIResult(&result);
1477 }
1478 break;
1479 case QCAMERA_SM_EVT_DUMP:
1480 {
1481 rc = m_parent->dump((int)payload);
1482 result.status = rc;
1483 result.request_api = evt;
1484 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1485 m_parent->signalAPIResult(&result);
1486 }
1487 break;
1488 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1489 {
1490 rc = m_parent->autoFocus();
1491 result.status = rc;
1492 result.request_api = evt;
1493 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1494 m_parent->signalAPIResult(&result);
1495 }
1496 break;
1497 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1498 {
1499 rc = m_parent->cancelAutoFocus();
1500 result.status = rc;
1501 result.request_api = evt;
1502 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1503 m_parent->signalAPIResult(&result);
1504 }
1505 break;
1506 case QCAMERA_SM_EVT_SEND_COMMAND:
1507 {
1508 qcamera_sm_evt_command_payload_t *cmd_payload =
1509 (qcamera_sm_evt_command_payload_t *)payload;
1510 rc = m_parent->sendCommand(cmd_payload->cmd,
1511 cmd_payload->arg1,
1512 cmd_payload->arg2);
1513 result.status = rc;
1514 result.request_api = evt;
1515 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1516 m_parent->signalAPIResult(&result);
1517 }
1518 break;
1519 case QCAMERA_SM_EVT_TAKE_PICTURE:
1520 {
1521 rc = m_parent->takeLiveSnapshot();
1522 if (rc == 0) {
1523 m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
1524 }
1525 result.status = rc;
1526 result.request_api = evt;
1527 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1528 m_parent->signalAPIResult(&result);
1529 }
1530 break;
1531 case QCAMERA_SM_EVT_START_RECORDING:
1532 {
1533 // no ops here
1534 ALOGD("%s: already in recording state, no ops for start_recording", __func__);
1535 rc = 0;
1536 result.status = rc;
1537 result.request_api = evt;
1538 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1539 m_parent->signalAPIResult(&result);
1540 }
1541 break;
1542 case QCAMERA_SM_EVT_STOP_RECORDING:
1543 {
1544 rc = m_parent->stopRecording();
1545 m_state = QCAMERA_SM_STATE_PREVIEWING;
1546 result.status = rc;
1547 result.request_api = evt;
1548 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1549 m_parent->signalAPIResult(&result);
1550 }
1551 break;
1552 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1553 {
1554 rc = m_parent->releaseRecordingFrame((const void *)payload);
1555 result.status = rc;
1556 result.request_api = evt;
1557 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1558 m_parent->signalAPIResult(&result);
1559 }
1560 break;
1561 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1562 case QCAMERA_SM_EVT_START_PREVIEW:
1563 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1564 case QCAMERA_SM_EVT_STOP_PREVIEW:
1565 case QCAMERA_SM_EVT_RELEASE:
1566 {
1567 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1568 rc = INVALID_OPERATION;
1569 result.status = rc;
1570 result.request_api = evt;
1571 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1572 m_parent->signalAPIResult(&result);
1573 }
1574 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001575 case QCAMERA_SM_EVT_EVT_INTERNAL:
1576 {
1577 qcamera_sm_internal_evt_payload_t *internal_evt =
1578 (qcamera_sm_internal_evt_payload_t *)payload;
1579 switch (internal_evt->evt_type) {
1580 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1581 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1582 break;
1583 default:
1584 break;
1585 }
1586 }
1587 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001588 case QCAMERA_SM_EVT_EVT_NOTIFY:
1589 {
1590 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1591 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001592 case CAM_EVENT_TYPE_ZOOM_DONE:
1593 rc = m_parent->processZoomEvent(cam_evt->status);
1594 break;
1595 default:
1596 ALOGD("%s: no handling for server evt (%d) at this state",
1597 __func__, cam_evt->server_event_type);
1598 break;
1599 }
1600 }
1601 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001602 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1603 //TODO: Adjust FPS
1604 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001605 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -08001606 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Muhua Libc9a8082012-11-07 15:51:28 -08001607 default:
1608 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1609 break;
1610 }
1611
1612 return rc;
1613}
1614
Muhua Lida2c4be2012-11-26 09:14:16 -08001615/*===========================================================================
1616 * FUNCTION : procEvtVideoPicTakingState
1617 *
1618 * DESCRIPTION: finite state machine function to handle event in state of
1619 * QCAMERA_SM_STATE_VIDEO_PIC_TAKING.
1620 *
1621 * PARAMETERS :
1622 * @evt : event to be processed
1623 * @payload : event payload. Can be NULL if not needed.
1624 *
1625 * RETURN : int32_t type of status
1626 * NO_ERROR -- success
1627 * none-zero failure code
1628 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001629int32_t QCameraStateMachine::procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,
1630 void *payload)
1631{
1632 int32_t rc = NO_ERROR;
1633 qcamera_api_result_t result;
1634 memset(&result, 0, sizeof(qcamera_api_result_t));
1635
1636 switch (evt) {
1637 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1638 {
1639 // Error setting preview window during previewing
1640 ALOGE("Cannot set preview window when preview is running");
1641 rc = INVALID_OPERATION;
1642 result.status = rc;
1643 result.request_api = evt;
1644 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1645 m_parent->signalAPIResult(&result);
1646 }
1647 break;
1648 case QCAMERA_SM_EVT_SET_CALLBACKS:
1649 {
1650 qcamera_sm_evt_setcb_payload_t *setcbs =
1651 (qcamera_sm_evt_setcb_payload_t *)payload;
1652 rc = m_parent->setCallBacks(setcbs->notify_cb,
1653 setcbs->data_cb,
1654 setcbs->data_cb_timestamp,
1655 setcbs->get_memory,
1656 setcbs->user);
1657 result.status = rc;
1658 result.request_api = evt;
1659 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1660 m_parent->signalAPIResult(&result);
1661 }
1662 break;
1663 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1664 {
1665 rc = m_parent->enableMsgType(int32_t(payload));
1666 result.status = rc;
1667 result.request_api = evt;
1668 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1669 m_parent->signalAPIResult(&result);
1670 }
1671 break;
1672 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1673 {
1674 rc = m_parent->disableMsgType(int32_t(payload));
1675 result.status = rc;
1676 result.request_api = evt;
1677 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1678 m_parent->signalAPIResult(&result);
1679 }
1680 break;
1681 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1682 {
1683 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1684 result.status = rc;
1685 result.request_api = evt;
1686 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1687 result.enabled = enabled;
1688 m_parent->signalAPIResult(&result);
1689 }
1690 break;
1691 case QCAMERA_SM_EVT_SET_PARAMS:
1692 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001693 bool needRestart = false;
1694 rc = m_parent->updateParameters((char*)payload, needRestart);
1695 if (rc == NO_ERROR) {
1696 if (needRestart) {
1697 // cannot set parameters that requires restart during recording
1698 ALOGE("%s: Cannot set parameters that requires restart during recording",
1699 __func__);
1700 rc = BAD_VALUE;
1701 } else {
1702 rc = m_parent->commitParameterChanges();
1703 }
1704 }
Muhua Libc9a8082012-11-07 15:51:28 -08001705 result.status = rc;
1706 result.request_api = evt;
1707 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1708 m_parent->signalAPIResult(&result);
1709 }
1710 break;
1711 case QCAMERA_SM_EVT_GET_PARAMS:
1712 {
1713 result.params = m_parent->getParameters();
1714 rc = NO_ERROR;
1715 result.status = rc;
1716 result.request_api = evt;
1717 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1718 m_parent->signalAPIResult(&result);
1719 }
1720 break;
1721 case QCAMERA_SM_EVT_PUT_PARAMS:
1722 {
1723 rc = m_parent->putParameters((char*)payload);
1724 result.status = rc;
1725 result.request_api = evt;
1726 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1727 m_parent->signalAPIResult(&result);
1728 }
1729 break;
1730 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1731 {
1732 rc = NO_ERROR;
1733 result.status = rc;
1734 result.request_api = evt;
1735 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1736 result.enabled = 1;
1737 m_parent->signalAPIResult(&result);
1738 }
1739 break;
1740 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1741 {
1742 rc = NO_ERROR;
1743 result.status = rc;
1744 result.request_api = evt;
1745 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1746 result.enabled = 1;
1747 m_parent->signalAPIResult(&result);
1748 }
1749 break;
1750 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1751 {
1752 rc = m_parent->storeMetaDataInBuffers(int(payload));
1753 result.status = rc;
1754 result.request_api = evt;
1755 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1756 m_parent->signalAPIResult(&result);
1757 }
1758 break;
1759 case QCAMERA_SM_EVT_DUMP:
1760 {
1761 rc = m_parent->dump((int)payload);
1762 result.status = rc;
1763 result.request_api = evt;
1764 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1765 m_parent->signalAPIResult(&result);
1766 }
1767 break;
1768 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1769 {
1770 rc = m_parent->autoFocus();
1771 result.status = rc;
1772 result.request_api = evt;
1773 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1774 m_parent->signalAPIResult(&result);
1775 }
1776 break;
1777 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1778 {
1779 rc = m_parent->cancelAutoFocus();
1780 result.status = rc;
1781 result.request_api = evt;
1782 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1783 m_parent->signalAPIResult(&result);
1784 }
1785 break;
1786 case QCAMERA_SM_EVT_SEND_COMMAND:
1787 {
1788 qcamera_sm_evt_command_payload_t *cmd_payload =
1789 (qcamera_sm_evt_command_payload_t *)payload;
1790 rc = m_parent->sendCommand(cmd_payload->cmd,
1791 cmd_payload->arg1,
1792 cmd_payload->arg2);
1793 result.status = rc;
1794 result.request_api = evt;
1795 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1796 m_parent->signalAPIResult(&result);
1797 }
1798 break;
1799 case QCAMERA_SM_EVT_STOP_RECORDING:
1800 {
1801 rc = m_parent->stopRecording();
1802 m_state = QCAMERA_SM_STATE_PREVIEW_PIC_TAKING;
1803 result.status = rc;
1804 result.request_api = evt;
1805 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1806 m_parent->signalAPIResult(&result);
1807 }
1808 break;
1809 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1810 {
1811 rc = m_parent->releaseRecordingFrame((const void *)payload);
1812 result.status = rc;
1813 result.request_api = evt;
1814 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1815 m_parent->signalAPIResult(&result);
1816 }
1817 break;
1818 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1819 {
1820 rc = m_parent->cancelLiveSnapshot();
1821 m_state = QCAMERA_SM_STATE_RECORDING;
1822 result.status = rc;
1823 result.request_api = evt;
1824 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1825 m_parent->signalAPIResult(&result);
1826 }
1827 break;
1828 case QCAMERA_SM_EVT_START_RECORDING:
1829 case QCAMERA_SM_EVT_START_PREVIEW:
1830 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1831 case QCAMERA_SM_EVT_STOP_PREVIEW:
1832 case QCAMERA_SM_EVT_TAKE_PICTURE:
1833 case QCAMERA_SM_EVT_RELEASE:
1834 {
1835 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1836 rc = INVALID_OPERATION;
1837 result.status = rc;
1838 result.request_api = evt;
1839 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1840 m_parent->signalAPIResult(&result);
1841 }
1842 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001843 case QCAMERA_SM_EVT_EVT_INTERNAL:
1844 {
1845 qcamera_sm_internal_evt_payload_t *internal_evt =
1846 (qcamera_sm_internal_evt_payload_t *)payload;
1847 switch (internal_evt->evt_type) {
1848 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1849 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1850 break;
1851 default:
1852 break;
1853 }
1854 }
1855 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001856 case QCAMERA_SM_EVT_EVT_NOTIFY:
1857 {
1858 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1859 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001860 case CAM_EVENT_TYPE_ZOOM_DONE:
1861 rc = m_parent->processZoomEvent(cam_evt->status);
1862 break;
1863 default:
1864 ALOGD("%s: no handling for server evt (%d) at this state",
1865 __func__, cam_evt->server_event_type);
1866 break;
1867 }
1868 }
1869 break;
1870 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1871 {
1872 qcamera_jpeg_evt_payload_t *jpeg_job =
1873 (qcamera_jpeg_evt_payload_t *)payload;
1874 rc = m_parent->processJpegNotify(jpeg_job);
1875 }
1876 break;
1877 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1878 {
1879 rc = m_parent->cancelLiveSnapshot();
1880 m_state = QCAMERA_SM_STATE_RECORDING;
1881 }
1882 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001883 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1884 //TODO: Adjust FPS.
1885 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001886 default:
1887 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1888 break;
1889 }
1890
1891 return rc;
1892}
1893
Muhua Lida2c4be2012-11-26 09:14:16 -08001894/*===========================================================================
1895 * FUNCTION : procEvtPreviewPicTakingState
1896 *
1897 * DESCRIPTION: finite state machine function to handle event in state of
1898 * QCAMERA_SM_STATE_PREVIEW_PIC_TAKING.
1899 *
1900 * PARAMETERS :
1901 * @evt : event to be processed
1902 * @payload : event payload. Can be NULL if not needed.
1903 *
1904 * RETURN : int32_t type of status
1905 * NO_ERROR -- success
1906 * none-zero failure code
1907 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001908int32_t QCameraStateMachine::procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,
1909 void *payload)
1910{
1911 int32_t rc = NO_ERROR;
1912 qcamera_api_result_t result;
1913 memset(&result, 0, sizeof(qcamera_api_result_t));
1914
1915 switch (evt) {
1916 case QCAMERA_SM_EVT_SET_CALLBACKS:
1917 {
1918 qcamera_sm_evt_setcb_payload_t *setcbs =
1919 (qcamera_sm_evt_setcb_payload_t *)payload;
1920 rc = m_parent->setCallBacks(setcbs->notify_cb,
1921 setcbs->data_cb,
1922 setcbs->data_cb_timestamp,
1923 setcbs->get_memory,
1924 setcbs->user);
1925 result.status = rc;
1926 result.request_api = evt;
1927 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1928 m_parent->signalAPIResult(&result);
1929 }
1930 break;
1931 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1932 {
1933 rc = m_parent->enableMsgType(int32_t(payload));
1934 result.status = rc;
1935 result.request_api = evt;
1936 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1937 m_parent->signalAPIResult(&result);
1938 }
1939 break;
1940 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1941 {
1942 rc = m_parent->disableMsgType(int32_t(payload));
1943 result.status = rc;
1944 result.request_api = evt;
1945 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1946 m_parent->signalAPIResult(&result);
1947 }
1948 break;
1949 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1950 {
1951 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1952 result.status = rc;
1953 result.request_api = evt;
1954 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1955 result.enabled = enabled;
1956 m_parent->signalAPIResult(&result);
1957 }
1958 break;
1959 case QCAMERA_SM_EVT_SET_PARAMS:
1960 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001961 bool needRestart = false;
1962 rc = m_parent->updateParameters((char*)payload, needRestart);
1963 if (rc == NO_ERROR) {
1964 if (needRestart) {
1965 // need restart preview for parameters to take effect
1966 // stop preview
1967 m_parent->stopPreview();
1968 // commit parameter changes to server
Muhua Li66f3b532013-01-23 17:19:05 -08001969 m_parent->commitParameterChanges();
Muhua Lida2c4be2012-11-26 09:14:16 -08001970 // start preview again
Muhua Li66f3b532013-01-23 17:19:05 -08001971 rc = m_parent->preparePreview();
1972 if (rc == NO_ERROR) {
1973 rc = m_parent->startPreview();
1974 if (rc != NO_ERROR) {
1975 m_parent->unpreparePreview();
1976 }
1977 }
1978 if (rc != NO_ERROR) {
1979 m_state = QCAMERA_SM_STATE_PIC_TAKING;
1980 }
Muhua Lida2c4be2012-11-26 09:14:16 -08001981 } else {
1982 rc = m_parent->commitParameterChanges();
1983 }
1984 }
Muhua Libc9a8082012-11-07 15:51:28 -08001985 result.status = rc;
1986 result.request_api = evt;
1987 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1988 m_parent->signalAPIResult(&result);
1989 }
1990 break;
1991 case QCAMERA_SM_EVT_GET_PARAMS:
1992 {
1993 result.params = m_parent->getParameters();
1994 rc = NO_ERROR;
1995 result.status = rc;
1996 result.request_api = evt;
1997 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1998 m_parent->signalAPIResult(&result);
1999 }
2000 break;
2001 case QCAMERA_SM_EVT_PUT_PARAMS:
2002 {
2003 rc = m_parent->putParameters((char*)payload);
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_PREVIEW_ENABLED:
2011 {
2012 rc = NO_ERROR;
2013 result.status = rc;
2014 result.request_api = evt;
2015 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2016 result.enabled = 1;
2017 m_parent->signalAPIResult(&result);
2018 }
2019 break;
2020 case QCAMERA_SM_EVT_RECORDING_ENABLED:
2021 {
2022 rc = NO_ERROR;
2023 result.status = rc;
2024 result.request_api = evt;
2025 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2026 result.enabled = 0;
2027 m_parent->signalAPIResult(&result);
2028 }
2029 break;
2030 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2031 {
2032 rc = m_parent->storeMetaDataInBuffers(int(payload));
2033 result.status = rc;
2034 result.request_api = evt;
2035 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2036 m_parent->signalAPIResult(&result);
2037 }
2038 break;
2039 case QCAMERA_SM_EVT_DUMP:
2040 {
2041 rc = m_parent->dump((int)payload);
2042 result.status = rc;
2043 result.request_api = evt;
2044 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2045 m_parent->signalAPIResult(&result);
2046 }
2047 break;
2048 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2049 {
2050 rc = m_parent->autoFocus();
2051 result.status = rc;
2052 result.request_api = evt;
2053 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2054 m_parent->signalAPIResult(&result);
2055 }
2056 break;
2057 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2058 {
2059 rc = m_parent->cancelAutoFocus();
2060 result.status = rc;
2061 result.request_api = evt;
2062 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2063 m_parent->signalAPIResult(&result);
2064 }
2065 break;
2066 case QCAMERA_SM_EVT_SEND_COMMAND:
2067 {
2068 qcamera_sm_evt_command_payload_t *cmd_payload =
2069 (qcamera_sm_evt_command_payload_t *)payload;
2070 rc = m_parent->sendCommand(cmd_payload->cmd,
2071 cmd_payload->arg1,
2072 cmd_payload->arg2);
2073 result.status = rc;
2074 result.request_api = evt;
2075 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2076 m_parent->signalAPIResult(&result);
2077 }
2078 break;
2079 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
2080 {
2081 rc = m_parent->releaseRecordingFrame((const void *)payload);
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_CANCEL_PICTURE:
2089 {
2090 rc = m_parent->cancelLiveSnapshot();
2091 m_state = QCAMERA_SM_STATE_PREVIEWING;
2092 result.status = rc;
2093 result.request_api = evt;
2094 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2095 m_parent->signalAPIResult(&result);
2096 }
2097 break;
2098 case QCAMERA_SM_EVT_STOP_PREVIEW:
2099 {
2100 m_parent->stopChannel(QCAMERA_CH_TYPE_PREVIEW);
2101 m_parent->delChannel(QCAMERA_CH_TYPE_PREVIEW);
2102 m_parent->delChannel(QCAMERA_CH_TYPE_VIDEO);
2103 m_state = QCAMERA_SM_STATE_PIC_TAKING;
2104 rc = NO_ERROR;
2105 result.status = rc;
2106 result.request_api = evt;
2107 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2108 m_parent->signalAPIResult(&result);
2109 }
2110 break;
2111 case QCAMERA_SM_EVT_START_RECORDING:
2112 {
2113 rc = m_parent->stopRecording();
2114 if (rc == NO_ERROR) {
2115 m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
2116 }
2117 result.status = rc;
2118 result.request_api = evt;
2119 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2120 m_parent->signalAPIResult(&result);
2121 }
2122 break;
2123 case QCAMERA_SM_EVT_STOP_RECORDING:
2124 case QCAMERA_SM_EVT_START_PREVIEW:
2125 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2126 case QCAMERA_SM_EVT_TAKE_PICTURE:
2127 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2128 case QCAMERA_SM_EVT_RELEASE:
2129 {
2130 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2131 rc = INVALID_OPERATION;
2132 result.status = rc;
2133 result.request_api = evt;
2134 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2135 m_parent->signalAPIResult(&result);
2136 }
2137 break;
Muhua Li31eaee02012-12-11 08:56:45 -08002138 case QCAMERA_SM_EVT_EVT_INTERNAL:
2139 {
2140 qcamera_sm_internal_evt_payload_t *internal_evt =
2141 (qcamera_sm_internal_evt_payload_t *)payload;
2142 switch (internal_evt->evt_type) {
2143 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2144 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2145 break;
2146 default:
2147 break;
2148 }
2149 }
2150 break;
Muhua Libc9a8082012-11-07 15:51:28 -08002151 case QCAMERA_SM_EVT_EVT_NOTIFY:
2152 {
2153 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2154 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08002155 case CAM_EVENT_TYPE_ZOOM_DONE:
2156 rc = m_parent->processZoomEvent(cam_evt->status);
2157 break;
2158 default:
2159 ALOGD("%s: no handling for server evt (%d) at this state",
2160 __func__, cam_evt->server_event_type);
2161 break;
2162 }
2163 }
2164 break;
2165 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2166 {
2167 qcamera_jpeg_evt_payload_t *jpeg_job =
2168 (qcamera_jpeg_evt_payload_t *)payload;
2169 rc = m_parent->processJpegNotify(jpeg_job);
2170 }
2171 break;
2172 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2173 {
2174 rc = m_parent->cancelLiveSnapshot();
2175 m_state = QCAMERA_SM_STATE_PREVIEWING;
2176 }
2177 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08002178 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2179 //TODO: Adjust FPS
2180 break;
Muhua Libc9a8082012-11-07 15:51:28 -08002181 default:
2182 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2183 break;
2184 }
2185
2186 return rc;
2187}
2188
Muhua Lida2c4be2012-11-26 09:14:16 -08002189/*===========================================================================
2190 * FUNCTION : isPreviewRunning
2191 *
2192 * DESCRIPTION: check if preview is in process.
2193 *
2194 * PARAMETERS : None
2195 *
2196 * RETURN : true -- preview running
2197 * false -- preview stopped
2198 *==========================================================================*/
2199bool QCameraStateMachine::isPreviewRunning()
2200{
2201 switch (m_state) {
2202 case QCAMERA_SM_STATE_PREVIEWING:
2203 case QCAMERA_SM_STATE_RECORDING:
2204 case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
2205 case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
2206 return true;
2207 default:
2208 return false;
2209 }
2210}
2211
Shuzhen Wang89635cf2012-12-20 13:47:22 -08002212}; // namespace qcamera