blob: adaf214b71d8cb7a6f6cb2e5b31c3fbc3f621148 [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) {
Mansoor Aftab06c809c2013-01-23 19:44:47 -0800384 rc = m_parent->preparePreview();
385 if(rc == NO_ERROR) {
386 // preview window is not set yet, move to previewReady state
387 m_state = QCAMERA_SM_STATE_PREVIEW_READY;
388 } else {
389 ALOGE("%s: preparePreview failed",__func__);
390 }
Muhua Libc9a8082012-11-07 15:51:28 -0800391 } else {
392 rc = m_parent->preparePreview();
Muhua Lida2c4be2012-11-26 09:14:16 -0800393 if (rc == NO_ERROR) {
Muhua Libc9a8082012-11-07 15:51:28 -0800394 rc = m_parent->startPreview();
Muhua Lida2c4be2012-11-26 09:14:16 -0800395 if (rc != NO_ERROR) {
Muhua Libc9a8082012-11-07 15:51:28 -0800396 m_parent->unpreparePreview();
397 } else {
Muhua Lida2c4be2012-11-26 09:14:16 -0800398 // start preview success, move to previewing state
Muhua Libc9a8082012-11-07 15:51:28 -0800399 m_state = QCAMERA_SM_STATE_PREVIEWING;
400 }
401 }
402 }
403 result.status = rc;
404 result.request_api = evt;
405 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
406 m_parent->signalAPIResult(&result);
407 }
408 break;
409 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
410 {
411 rc = m_parent->preparePreview();
412 if (rc == NO_ERROR) {
413 rc = m_parent->startPreview();
414 if (rc != NO_ERROR) {
415 m_parent->unpreparePreview();
416 } else {
417 m_state = QCAMERA_SM_STATE_PREVIEWING;
418 }
419 }
420 result.status = rc;
421 result.request_api = evt;
422 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
423 m_parent->signalAPIResult(&result);
424 }
425 break;
426 case QCAMERA_SM_EVT_STOP_PREVIEW:
427 {
428 // no op needed here
429 ALOGD("%s: already in preview stopped state, do nothing", __func__);
430 result.status = NO_ERROR;
431 result.request_api = evt;
432 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
433 m_parent->signalAPIResult(&result);
434 }
435 break;
436 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
437 case QCAMERA_SM_EVT_RECORDING_ENABLED:
438 {
439 result.status = NO_ERROR;
440 result.request_api = evt;
441 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
442 result.enabled = 0;
443 m_parent->signalAPIResult(&result);
444 }
445 break;
446 case QCAMERA_SM_EVT_RELEASE:
447 {
448 rc = m_parent->release();
449 result.status = rc;
450 result.request_api = evt;
451 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
452 m_parent->signalAPIResult(&result);
453 }
454 break;
455 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
456 {
457 rc = m_parent->storeMetaDataInBuffers(int(payload));
458 result.status = rc;
459 result.request_api = evt;
460 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
461 m_parent->signalAPIResult(&result);
462 }
463 break;
464 case QCAMERA_SM_EVT_DUMP:
465 {
466 rc = m_parent->dump((int)payload);
467 result.status = rc;
468 result.request_api = evt;
469 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
470 m_parent->signalAPIResult(&result);
471 }
472 break;
Ankit Premrajka3b00dc62012-12-14 18:37:52 -0800473 case QCAMERA_SM_EVT_SEND_COMMAND:
474 {
475 qcamera_sm_evt_command_payload_t *cmd_payload =
476 (qcamera_sm_evt_command_payload_t *)payload;
477 rc = m_parent->sendCommand(cmd_payload->cmd,
478 cmd_payload->arg1,
479 cmd_payload->arg2);
480 result.status = rc;
481 result.request_api = evt;
482 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
483 m_parent->signalAPIResult(&result);
484 }
485 break;
Muhua Libc9a8082012-11-07 15:51:28 -0800486 case QCAMERA_SM_EVT_START_RECORDING:
487 case QCAMERA_SM_EVT_STOP_RECORDING:
488 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
489 case QCAMERA_SM_EVT_TAKE_PICTURE:
Muhua Libc9a8082012-11-07 15:51:28 -0800490 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
Muhua Libc9a8082012-11-07 15:51:28 -0800491 {
492 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
493 rc = INVALID_OPERATION;
494 result.status = rc;
495 result.request_api = evt;
496 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
497 m_parent->signalAPIResult(&result);
498 }
499 break;
Muhua Li1612f422013-01-03 11:07:39 -0800500 case QCAMERA_SM_EVT_CANCEL_PICTURE:
501 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
502 {
503 // no op needed here
504 ALOGD("%s: No ops for evt(%d) in state(%d)", __func__, evt, m_state);
505 result.status = NO_ERROR;
506 result.request_api = evt;
507 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
508 m_parent->signalAPIResult(&result);
509 }
510 break;
Muhua Li31eaee02012-12-11 08:56:45 -0800511 case QCAMERA_SM_EVT_EVT_INTERNAL:
Muhua Libc9a8082012-11-07 15:51:28 -0800512 case QCAMERA_SM_EVT_EVT_NOTIFY:
513 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -0800514 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Shuzhen Wangaa829ce2013-01-09 14:41:49 -0800515 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
Muhua Libc9a8082012-11-07 15:51:28 -0800516 default:
517 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
518 break;
519 }
520
521 return rc;
522}
523
Muhua Lida2c4be2012-11-26 09:14:16 -0800524/*===========================================================================
525 * FUNCTION : procEvtPreviewReadyState
526 *
527 * DESCRIPTION: finite state machine function to handle event in state of
528 * QCAMERA_SM_STATE_PREVIEW_READY.
529 *
530 * PARAMETERS :
531 * @evt : event to be processed
532 * @payload : event payload. Can be NULL if not needed.
533 *
534 * RETURN : int32_t type of status
535 * NO_ERROR -- success
536 * none-zero failure code
537 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800538int32_t QCameraStateMachine::procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt,
539 void *payload)
540{
541 int32_t rc = NO_ERROR;
542 qcamera_api_result_t result;
543 memset(&result, 0, sizeof(qcamera_api_result_t));
544
545 switch (evt) {
546 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
547 {
548 m_parent->setPreviewWindow((struct preview_stream_ops *)payload);
549 if (m_parent->mPreviewWindow != NULL) {
550 rc = m_parent->startPreview();
551 if (rc != NO_ERROR) {
552 m_parent->unpreparePreview();
553 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
554 } else {
555 m_state = QCAMERA_SM_STATE_PREVIEWING;
556 }
557 }
558
559 result.status = rc;
560 result.request_api = evt;
561 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
562 m_parent->signalAPIResult(&result);
563 }
564 break;
565 case QCAMERA_SM_EVT_SET_CALLBACKS:
566 {
567 qcamera_sm_evt_setcb_payload_t *setcbs =
568 (qcamera_sm_evt_setcb_payload_t *)payload;
569 rc = m_parent->setCallBacks(setcbs->notify_cb,
570 setcbs->data_cb,
571 setcbs->data_cb_timestamp,
572 setcbs->get_memory,
573 setcbs->user);
574 result.status = rc;
575 result.request_api = evt;
576 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
577 m_parent->signalAPIResult(&result);
578 }
579 break;
580 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
581 {
582 rc = m_parent->enableMsgType(int32_t(payload));
583 result.status = rc;
584 result.request_api = evt;
585 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
586 m_parent->signalAPIResult(&result);
587 }
588 break;
589 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
590 {
591 rc = m_parent->disableMsgType(int32_t(payload));
592 result.status = rc;
593 result.request_api = evt;
594 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
595 m_parent->signalAPIResult(&result);
596 }
597 break;
598 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
599 {
600 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
601 result.status = rc;
602 result.request_api = evt;
603 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
604 result.enabled = enabled;
605 m_parent->signalAPIResult(&result);
606 }
607 break;
608 case QCAMERA_SM_EVT_SET_PARAMS:
609 {
Muhua Lida2c4be2012-11-26 09:14:16 -0800610 bool needRestart = false;
611 rc = m_parent->updateParameters((char*)payload, needRestart);
612 if (rc == NO_ERROR) {
613 rc = m_parent->commitParameterChanges();
614 }
Muhua Libc9a8082012-11-07 15:51:28 -0800615 result.status = rc;
616 result.request_api = evt;
617 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
618 m_parent->signalAPIResult(&result);
619 }
620 break;
621 case QCAMERA_SM_EVT_GET_PARAMS:
622 {
623 result.params = m_parent->getParameters();
624 rc = NO_ERROR;
625 result.status = rc;
626 result.request_api = evt;
627 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
628 m_parent->signalAPIResult(&result);
629 }
630 break;
631 case QCAMERA_SM_EVT_PUT_PARAMS:
632 {
633 rc = m_parent->putParameters((char*)payload);
634 result.status = rc;
635 result.request_api = evt;
636 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
637 m_parent->signalAPIResult(&result);
638 }
639 break;
640 case QCAMERA_SM_EVT_START_PREVIEW:
641 {
642 // no ops here
643 rc = NO_ERROR;
644 result.status = rc;
645 result.request_api = evt;
646 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
647 m_parent->signalAPIResult(&result);
648 }
649 break;
650 case QCAMERA_SM_EVT_STOP_PREVIEW:
651 {
652 m_parent->unpreparePreview();
653 rc = 0;
654 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
655 result.status = rc;
656 result.request_api = evt;
657 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
658 m_parent->signalAPIResult(&result);
659 }
660 break;
661 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
662 {
663 rc = NO_ERROR;
664 result.status = rc;
665 result.request_api = evt;
666 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
667 result.enabled = 1;
668 m_parent->signalAPIResult(&result);
669 }
670 break;
671 case QCAMERA_SM_EVT_RECORDING_ENABLED:
672 {
673 rc = 0;
674 result.status = rc;
675 result.request_api = evt;
676 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
677 result.enabled = 0;
678 m_parent->signalAPIResult(&result);
679 }
680 break;
681 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
682 {
683 rc = m_parent->storeMetaDataInBuffers(int(payload));
684 result.status = rc;
685 result.request_api = evt;
686 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
687 m_parent->signalAPIResult(&result);
688 }
689 break;
690 case QCAMERA_SM_EVT_DUMP:
691 {
692 rc = m_parent->dump((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_START_AUTO_FOCUS:
700 {
701 rc = m_parent->autoFocus();
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_STOP_AUTO_FOCUS:
709 {
710 rc = m_parent->cancelAutoFocus();
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_SEND_COMMAND:
718 {
719 qcamera_sm_evt_command_payload_t *cmd_payload =
720 (qcamera_sm_evt_command_payload_t *)payload;
721 rc = m_parent->sendCommand(cmd_payload->cmd,
722 cmd_payload->arg1,
723 cmd_payload->arg2);
724 result.status = rc;
725 result.request_api = evt;
726 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
727 m_parent->signalAPIResult(&result);
728 }
729 break;
730 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
731 case QCAMERA_SM_EVT_START_RECORDING:
732 case QCAMERA_SM_EVT_STOP_RECORDING:
733 case QCAMERA_SM_EVT_TAKE_PICTURE:
734 case QCAMERA_SM_EVT_CANCEL_PICTURE:
735 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
736 case QCAMERA_SM_EVT_RELEASE:
737 {
738 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
739 rc = INVALID_OPERATION;
740 result.status = rc;
741 result.request_api = evt;
742 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
743 m_parent->signalAPIResult(&result);
744 }
745 break;
Muhua Li31eaee02012-12-11 08:56:45 -0800746 case QCAMERA_SM_EVT_EVT_INTERNAL:
Muhua Libc9a8082012-11-07 15:51:28 -0800747 case QCAMERA_SM_EVT_EVT_NOTIFY:
748 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -0800749 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Shuzhen Wangaa829ce2013-01-09 14:41:49 -0800750 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
Muhua Libc9a8082012-11-07 15:51:28 -0800751 default:
752 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
753 break;
754 }
755
756 return rc;
757}
758
Muhua Lida2c4be2012-11-26 09:14:16 -0800759/*===========================================================================
760 * FUNCTION : procEvtPreviewingState
761 *
762 * DESCRIPTION: finite state machine function to handle event in state of
763 * QCAMERA_SM_STATE_PREVIEWING.
764 *
765 * PARAMETERS :
766 * @evt : event to be processed
767 * @payload : event payload. Can be NULL if not needed.
768 *
769 * RETURN : int32_t type of status
770 * NO_ERROR -- success
771 * none-zero failure code
772 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -0800773int32_t QCameraStateMachine::procEvtPreviewingState(qcamera_sm_evt_enum_t evt,
774 void *payload)
775{
776 int32_t rc = NO_ERROR;
777 qcamera_api_result_t result;
778 memset(&result, 0, sizeof(qcamera_api_result_t));
779
780 switch (evt) {
781 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
782 {
783 // Error setting preview window during previewing
784 ALOGE("Cannot set preview window when preview is running");
785 rc = INVALID_OPERATION;
786 result.status = rc;
787 result.request_api = evt;
788 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
789 m_parent->signalAPIResult(&result);
790 }
791 break;
792 case QCAMERA_SM_EVT_SET_CALLBACKS:
793 {
794 qcamera_sm_evt_setcb_payload_t *setcbs =
795 (qcamera_sm_evt_setcb_payload_t *)payload;
796 rc = m_parent->setCallBacks(setcbs->notify_cb,
797 setcbs->data_cb,
798 setcbs->data_cb_timestamp,
799 setcbs->get_memory,
800 setcbs->user);
801 result.status = rc;
802 result.request_api = evt;
803 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
804 m_parent->signalAPIResult(&result);
805 }
806 break;
807 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
808 {
809 rc = m_parent->enableMsgType(int32_t(payload));
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_DISABLE_MSG_TYPE:
817 {
818 rc = m_parent->disableMsgType(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_MSG_TYPE_ENABLED:
826 {
827 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
828 result.status = rc;
829 result.request_api = evt;
830 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
831 result.enabled = enabled;
832 m_parent->signalAPIResult(&result);
833 }
834 break;
835 case QCAMERA_SM_EVT_SET_PARAMS:
836 {
Muhua Lida2c4be2012-11-26 09:14:16 -0800837 bool needRestart = false;
838 rc = m_parent->updateParameters((char*)payload, needRestart);
839 if (rc == NO_ERROR) {
840 if (needRestart) {
841 // need restart preview for parameters to take effect
842 // stop preview
843 m_parent->stopPreview();
844 // commit parameter changes to server
Muhua Li66f3b532013-01-23 17:19:05 -0800845 m_parent->commitParameterChanges();
Muhua Lida2c4be2012-11-26 09:14:16 -0800846 // start preview again
Muhua Li66f3b532013-01-23 17:19:05 -0800847 rc = m_parent->preparePreview();
848 if (rc == NO_ERROR) {
849 rc = m_parent->startPreview();
850 if (rc != NO_ERROR) {
851 m_parent->unpreparePreview();
852 }
853 }
854 if (rc != NO_ERROR) {
855 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
856 }
Muhua Lida2c4be2012-11-26 09:14:16 -0800857 } else {
858 rc = m_parent->commitParameterChanges();
859 }
860 }
Muhua Libc9a8082012-11-07 15:51:28 -0800861 result.status = rc;
862 result.request_api = evt;
863 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
864 m_parent->signalAPIResult(&result);
865 }
866 break;
867 case QCAMERA_SM_EVT_GET_PARAMS:
868 {
869 result.params = m_parent->getParameters();
870 rc = NO_ERROR;
871 result.status = rc;
872 result.request_api = evt;
873 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
874 m_parent->signalAPIResult(&result);
875 }
876 break;
877 case QCAMERA_SM_EVT_PUT_PARAMS:
878 {
879 rc = m_parent->putParameters((char*)payload);
880 result.status = rc;
881 result.request_api = evt;
882 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
883 m_parent->signalAPIResult(&result);
884 }
885 break;
886 case QCAMERA_SM_EVT_START_PREVIEW:
887 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
888 {
889 // no ops here
890 ALOGD("%s: Already in previewing, no ops here to start preview", __func__);
891 rc = NO_ERROR;
892 result.status = rc;
893 result.request_api = evt;
894 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
895 m_parent->signalAPIResult(&result);
896 }
897 break;
898 case QCAMERA_SM_EVT_STOP_PREVIEW:
899 {
900 rc = m_parent->stopPreview();
901 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
902 result.status = rc;
903 result.request_api = evt;
904 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
905 m_parent->signalAPIResult(&result);
906 }
907 break;
908 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
909 {
910 rc = NO_ERROR;
911 result.status = rc;
912 result.request_api = evt;
913 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
914 result.enabled = 1;
915 m_parent->signalAPIResult(&result);
916 }
917 break;
918 case QCAMERA_SM_EVT_RECORDING_ENABLED:
919 {
920 rc = NO_ERROR;
921 result.status = rc;
922 result.request_api = evt;
923 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
924 result.enabled = 0;
925 m_parent->signalAPIResult(&result);
926 }
927 break;
928 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
929 {
930 rc = m_parent->storeMetaDataInBuffers(int(payload));
931 result.status = rc;
932 result.request_api = evt;
933 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
934 m_parent->signalAPIResult(&result);
935 }
936 break;
937 case QCAMERA_SM_EVT_DUMP:
938 {
939 rc = m_parent->dump((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_START_AUTO_FOCUS:
947 {
948 rc = m_parent->autoFocus();
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_STOP_AUTO_FOCUS:
956 {
957 rc = m_parent->cancelAutoFocus();
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_START_RECORDING:
965 {
966 rc = m_parent->startRecording();
967 if (rc == NO_ERROR) {
968 // move state to recording state
969 m_state = QCAMERA_SM_STATE_RECORDING;
970 }
971 result.status = rc;
972 result.request_api = evt;
973 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
974 m_parent->signalAPIResult(&result);
975 }
976 break;
977 case QCAMERA_SM_EVT_TAKE_PICTURE:
978 {
979 rc = m_parent->takePicture();
980 if (rc == NO_ERROR) {
981 // move state to picture taking state
982 m_state = QCAMERA_SM_STATE_PIC_TAKING;
983 } else {
984 // move state to preview stopped state
985 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
986 }
987 result.status = rc;
988 result.request_api = evt;
989 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
990 m_parent->signalAPIResult(&result);
991 }
992 break;
993 case QCAMERA_SM_EVT_SEND_COMMAND:
994 {
995 qcamera_sm_evt_command_payload_t *cmd_payload =
996 (qcamera_sm_evt_command_payload_t *)payload;
997 rc = m_parent->sendCommand(cmd_payload->cmd,
998 cmd_payload->arg1,
999 cmd_payload->arg2);
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_CANCEL_PICTURE:
1007 case QCAMERA_SM_EVT_STOP_RECORDING:
1008 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1009 case QCAMERA_SM_EVT_RELEASE:
1010 {
1011 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1012 rc = INVALID_OPERATION;
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;
Muhua Li31eaee02012-12-11 08:56:45 -08001019 case QCAMERA_SM_EVT_EVT_INTERNAL:
1020 {
1021 qcamera_sm_internal_evt_payload_t *internal_evt =
1022 (qcamera_sm_internal_evt_payload_t *)payload;
1023 switch (internal_evt->evt_type) {
1024 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1025 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1026 break;
1027 default:
1028 break;
1029 }
1030 }
1031 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001032 case QCAMERA_SM_EVT_EVT_NOTIFY:
1033 {
1034 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1035 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001036 case CAM_EVENT_TYPE_ZOOM_DONE:
1037 rc = m_parent->processZoomEvent(cam_evt->status);
1038 break;
1039 default:
1040 ALOGD("%s: no handling for server evt (%d) at this state",
1041 __func__, cam_evt->server_event_type);
1042 break;
1043 }
1044 }
1045 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001046 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1047 //TODO: Adjust FPS.
1048 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001049 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -08001050 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Muhua Libc9a8082012-11-07 15:51:28 -08001051 default:
1052 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1053 break;
1054 }
1055
1056 return rc;
1057}
1058
Muhua Lida2c4be2012-11-26 09:14:16 -08001059/*===========================================================================
1060 * FUNCTION : procEvtPicTakingState
1061 *
1062 * DESCRIPTION: finite state machine function to handle event in state of
1063 * QCAMERA_SM_STATE_PIC_TAKING.
1064 *
1065 * PARAMETERS :
1066 * @evt : event to be processed
1067 * @payload : event payload. Can be NULL if not needed.
1068 *
1069 * RETURN : int32_t type of status
1070 * NO_ERROR -- success
1071 * none-zero failure code
1072 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001073int32_t QCameraStateMachine::procEvtPicTakingState(qcamera_sm_evt_enum_t evt,
1074 void *payload)
1075{
1076 int32_t rc = NO_ERROR;
1077 qcamera_api_result_t result;
1078 memset(&result, 0, sizeof(qcamera_api_result_t));
1079
1080 switch (evt) {
1081 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1082 {
1083 // Error setting preview window during previewing
1084 ALOGE("Cannot set preview window when preview is running");
1085 rc = INVALID_OPERATION;
1086 result.status = rc;
1087 result.request_api = evt;
1088 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1089 m_parent->signalAPIResult(&result);
1090 }
1091 break;
1092 case QCAMERA_SM_EVT_SET_CALLBACKS:
1093 {
1094 qcamera_sm_evt_setcb_payload_t *setcbs =
1095 (qcamera_sm_evt_setcb_payload_t *)payload;
1096 rc = m_parent->setCallBacks(setcbs->notify_cb,
1097 setcbs->data_cb,
1098 setcbs->data_cb_timestamp,
1099 setcbs->get_memory,
1100 setcbs->user);
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_ENABLE_MSG_TYPE:
1108 {
1109 rc = m_parent->enableMsgType(int32_t(payload));
1110 result.status = rc;
1111 result.request_api = evt;
1112 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1113 m_parent->signalAPIResult(&result);
1114 }
1115 break;
1116 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1117 {
1118 rc = m_parent->disableMsgType(int32_t(payload));
1119 result.status = rc;
1120 result.request_api = evt;
1121 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1122 m_parent->signalAPIResult(&result);
1123 }
1124 break;
1125 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1126 {
1127 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1128 result.status = rc;
1129 result.request_api = evt;
1130 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1131 result.enabled = enabled;
1132 m_parent->signalAPIResult(&result);
1133 }
1134 break;
1135 case QCAMERA_SM_EVT_SET_PARAMS:
1136 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001137 bool needRestart = false;
1138 rc = m_parent->updateParameters((char*)payload, needRestart);
1139 if (rc == NO_ERROR) {
1140 rc = m_parent->commitParameterChanges();
1141 }
Muhua Libc9a8082012-11-07 15:51:28 -08001142 result.status = rc;
1143 result.request_api = evt;
1144 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1145 m_parent->signalAPIResult(&result);
1146 }
1147 break;
1148 case QCAMERA_SM_EVT_GET_PARAMS:
1149 {
1150 result.params = m_parent->getParameters();
1151 rc = NO_ERROR;
1152 result.status = rc;
1153 result.request_api = evt;
1154 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1155 m_parent->signalAPIResult(&result);
1156 }
1157 break;
1158 case QCAMERA_SM_EVT_PUT_PARAMS:
1159 {
1160 rc = m_parent->putParameters((char*)payload);
1161 result.status = rc;
1162 result.request_api = evt;
1163 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1164 m_parent->signalAPIResult(&result);
1165 }
1166 break;
1167 case QCAMERA_SM_EVT_STOP_PREVIEW:
1168 {
1169 // no ops, since preview is stopped (normal),
1170 // or preview msg type is disabled (ZSL)
1171 rc = NO_ERROR;
1172 result.status = rc;
1173 result.request_api = evt;
1174 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1175 m_parent->signalAPIResult(&result);
1176 }
1177 break;
1178 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1179 {
1180 rc = NO_ERROR;
1181 result.status = rc;
1182 result.request_api = evt;
1183 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1184 result.enabled = 0;
1185 m_parent->signalAPIResult(&result);
1186 }
1187 break;
1188 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1189 {
1190 rc = NO_ERROR;
1191 result.status = rc;
1192 result.request_api = evt;
1193 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1194 result.enabled = 0;
1195 m_parent->signalAPIResult(&result);
1196 }
1197 break;
1198 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1199 {
1200 rc = m_parent->storeMetaDataInBuffers(int(payload));
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_DUMP:
1208 {
1209 rc = m_parent->dump((int)payload);
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_START_AUTO_FOCUS:
1217 {
1218 rc = m_parent->autoFocus();
1219 result.status = rc;
1220 result.request_api = evt;
1221 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1222 m_parent->signalAPIResult(&result);
1223 }
1224 break;
1225 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1226 {
1227 rc = m_parent->cancelAutoFocus();
1228 result.status = rc;
1229 result.request_api = evt;
1230 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1231 m_parent->signalAPIResult(&result);
1232 }
1233 break;
1234 case QCAMERA_SM_EVT_SEND_COMMAND:
1235 {
1236 qcamera_sm_evt_command_payload_t *cmd_payload =
1237 (qcamera_sm_evt_command_payload_t *)payload;
1238 rc = m_parent->sendCommand(cmd_payload->cmd,
1239 cmd_payload->arg1,
1240 cmd_payload->arg2);
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_CANCEL_PICTURE:
1248 {
1249 rc = m_parent->cancelPicture();
1250 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1251 result.status = rc;
1252 result.request_api = evt;
1253 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1254 m_parent->signalAPIResult(&result);
1255 }
1256 break;
1257 case QCAMERA_SM_EVT_TAKE_PICTURE:
1258 case QCAMERA_SM_EVT_START_RECORDING:
1259 case QCAMERA_SM_EVT_STOP_RECORDING:
1260 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1261 case QCAMERA_SM_EVT_START_PREVIEW:
1262 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1263 case QCAMERA_SM_EVT_RELEASE:
1264 {
1265 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1266 rc = INVALID_OPERATION;
1267 result.status = rc;
1268 result.request_api = evt;
1269 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1270 m_parent->signalAPIResult(&result);
1271 }
1272 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001273 case QCAMERA_SM_EVT_EVT_INTERNAL:
1274 {
1275 qcamera_sm_internal_evt_payload_t *internal_evt =
1276 (qcamera_sm_internal_evt_payload_t *)payload;
1277 switch (internal_evt->evt_type) {
1278 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1279 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1280 break;
1281 default:
1282 break;
1283 }
1284 }
1285 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001286 case QCAMERA_SM_EVT_EVT_NOTIFY:
1287 {
1288 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1289 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001290 case CAM_EVENT_TYPE_ZOOM_DONE:
1291 rc = m_parent->processZoomEvent(cam_evt->status);
1292 break;
1293 default:
1294 ALOGD("%s: no handling for server evt (%d) at this state",
1295 __func__, cam_evt->server_event_type);
1296 break;
1297 }
1298 }
1299 break;
1300 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1301 {
1302 qcamera_jpeg_evt_payload_t *jpeg_job =
1303 (qcamera_jpeg_evt_payload_t *)payload;
1304 rc = m_parent->processJpegNotify(jpeg_job);
1305 }
1306 break;
1307 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1308 {
1309 rc = m_parent->cancelPicture();
1310 m_state = QCAMERA_SM_STATE_PREVIEW_STOPPED;
1311 }
1312 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001313 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
Muhua Libc9a8082012-11-07 15:51:28 -08001314 default:
1315 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1316 break;
1317 }
1318
1319 return rc;
1320}
1321
Muhua Lida2c4be2012-11-26 09:14:16 -08001322/*===========================================================================
1323 * FUNCTION : procEvtRecordingState
1324 *
1325 * DESCRIPTION: finite state machine function to handle event in state of
1326 * QCAMERA_SM_STATE_RECORDING.
1327 *
1328 * PARAMETERS :
1329 * @evt : event to be processed
1330 * @payload : event payload. Can be NULL if not needed.
1331 *
1332 * RETURN : int32_t type of status
1333 * NO_ERROR -- success
1334 * none-zero failure code
1335 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001336int32_t QCameraStateMachine::procEvtRecordingState(qcamera_sm_evt_enum_t evt,
1337 void *payload)
1338{
1339 int32_t rc = NO_ERROR;
1340 qcamera_api_result_t result;
1341 memset(&result, 0, sizeof(qcamera_api_result_t));
1342
1343 switch (evt) {
1344 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1345 {
1346 // Error setting preview window during previewing
1347 ALOGE("Cannot set preview window when preview is running");
1348 rc = INVALID_OPERATION;
1349 result.status = rc;
1350 result.request_api = evt;
1351 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1352 m_parent->signalAPIResult(&result);
1353 }
1354 break;
1355 case QCAMERA_SM_EVT_SET_CALLBACKS:
1356 {
1357 qcamera_sm_evt_setcb_payload_t *setcbs =
1358 (qcamera_sm_evt_setcb_payload_t *)payload;
1359 rc = m_parent->setCallBacks(setcbs->notify_cb,
1360 setcbs->data_cb,
1361 setcbs->data_cb_timestamp,
1362 setcbs->get_memory,
1363 setcbs->user);
1364 result.status = rc;
1365 result.request_api = evt;
1366 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1367 m_parent->signalAPIResult(&result);
1368 }
1369 break;
1370 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1371 {
1372 rc = m_parent->enableMsgType(int32_t(payload));
1373 result.status = rc;
1374 result.request_api = evt;
1375 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1376 m_parent->signalAPIResult(&result);
1377 }
1378 break;
1379 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1380 {
1381 rc = m_parent->disableMsgType(int32_t(payload));
1382 result.status = rc;
1383 result.request_api = evt;
1384 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1385 m_parent->signalAPIResult(&result);
1386 }
1387 break;
1388 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1389 {
1390 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1391 result.status = rc;
1392 result.request_api = evt;
1393 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1394 result.enabled = enabled;
1395 m_parent->signalAPIResult(&result);
1396 }
1397 break;
1398 case QCAMERA_SM_EVT_SET_PARAMS:
1399 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001400 bool needRestart = false;
1401 rc = m_parent->updateParameters((char*)payload, needRestart);
1402 if (rc == NO_ERROR) {
1403 if (needRestart) {
1404 // cannot set parameters that requires restart during recording
1405 ALOGE("%s: Cannot set parameters that requires restart during recording",
1406 __func__);
1407 rc = BAD_VALUE;
1408 } else {
1409 rc = m_parent->commitParameterChanges();
1410 }
1411 }
Muhua Libc9a8082012-11-07 15:51:28 -08001412 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_GET_PARAMS:
1419 {
1420 result.params = m_parent->getParameters();
1421 rc = NO_ERROR;
1422 result.status = rc;
1423 result.request_api = evt;
1424 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1425 m_parent->signalAPIResult(&result);
1426 }
1427 break;
1428 case QCAMERA_SM_EVT_PUT_PARAMS:
1429 {
1430 rc = m_parent->putParameters((char*)payload);
1431 result.status = rc;
1432 result.request_api = evt;
1433 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1434 m_parent->signalAPIResult(&result);
1435 }
1436 break;
1437 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1438 {
1439 rc = NO_ERROR;
1440 result.status = rc;
1441 result.request_api = evt;
1442 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1443 result.enabled = 0;
1444 m_parent->signalAPIResult(&result);
1445 }
1446 break;
1447 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1448 {
1449 rc = NO_ERROR;
1450 result.status = rc;
1451 result.request_api = evt;
1452 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1453 result.enabled = 1;
1454 m_parent->signalAPIResult(&result);
1455 }
1456 break;
1457 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1458 {
1459 rc = m_parent->storeMetaDataInBuffers(int(payload));
1460 result.status = rc;
1461 result.request_api = evt;
1462 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1463 m_parent->signalAPIResult(&result);
1464 }
1465 break;
1466 case QCAMERA_SM_EVT_DUMP:
1467 {
1468 rc = m_parent->dump((int)payload);
1469 result.status = rc;
1470 result.request_api = evt;
1471 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1472 m_parent->signalAPIResult(&result);
1473 }
1474 break;
1475 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1476 {
1477 rc = m_parent->autoFocus();
1478 result.status = rc;
1479 result.request_api = evt;
1480 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1481 m_parent->signalAPIResult(&result);
1482 }
1483 break;
1484 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
1485 {
1486 rc = m_parent->cancelAutoFocus();
1487 result.status = rc;
1488 result.request_api = evt;
1489 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1490 m_parent->signalAPIResult(&result);
1491 }
1492 break;
1493 case QCAMERA_SM_EVT_SEND_COMMAND:
1494 {
1495 qcamera_sm_evt_command_payload_t *cmd_payload =
1496 (qcamera_sm_evt_command_payload_t *)payload;
1497 rc = m_parent->sendCommand(cmd_payload->cmd,
1498 cmd_payload->arg1,
1499 cmd_payload->arg2);
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_TAKE_PICTURE:
1507 {
1508 rc = m_parent->takeLiveSnapshot();
1509 if (rc == 0) {
1510 m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
1511 }
1512 result.status = rc;
1513 result.request_api = evt;
1514 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1515 m_parent->signalAPIResult(&result);
1516 }
1517 break;
1518 case QCAMERA_SM_EVT_START_RECORDING:
1519 {
1520 // no ops here
1521 ALOGD("%s: already in recording state, no ops for start_recording", __func__);
1522 rc = 0;
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_STOP_RECORDING:
1530 {
1531 rc = m_parent->stopRecording();
1532 m_state = QCAMERA_SM_STATE_PREVIEWING;
1533 result.status = rc;
1534 result.request_api = evt;
1535 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1536 m_parent->signalAPIResult(&result);
1537 }
1538 break;
1539 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1540 {
1541 rc = m_parent->releaseRecordingFrame((const void *)payload);
1542 result.status = rc;
1543 result.request_api = evt;
1544 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1545 m_parent->signalAPIResult(&result);
1546 }
1547 break;
1548 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1549 case QCAMERA_SM_EVT_START_PREVIEW:
1550 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1551 case QCAMERA_SM_EVT_STOP_PREVIEW:
1552 case QCAMERA_SM_EVT_RELEASE:
1553 {
1554 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1555 rc = INVALID_OPERATION;
1556 result.status = rc;
1557 result.request_api = evt;
1558 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1559 m_parent->signalAPIResult(&result);
1560 }
1561 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001562 case QCAMERA_SM_EVT_EVT_INTERNAL:
1563 {
1564 qcamera_sm_internal_evt_payload_t *internal_evt =
1565 (qcamera_sm_internal_evt_payload_t *)payload;
1566 switch (internal_evt->evt_type) {
1567 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1568 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1569 break;
1570 default:
1571 break;
1572 }
1573 }
1574 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001575 case QCAMERA_SM_EVT_EVT_NOTIFY:
1576 {
1577 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1578 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001579 case CAM_EVENT_TYPE_ZOOM_DONE:
1580 rc = m_parent->processZoomEvent(cam_evt->status);
1581 break;
1582 default:
1583 ALOGD("%s: no handling for server evt (%d) at this state",
1584 __func__, cam_evt->server_event_type);
1585 break;
1586 }
1587 }
1588 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001589 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1590 //TODO: Adjust FPS
1591 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001592 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
Muhua Lida2c4be2012-11-26 09:14:16 -08001593 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
Muhua Libc9a8082012-11-07 15:51:28 -08001594 default:
1595 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1596 break;
1597 }
1598
1599 return rc;
1600}
1601
Muhua Lida2c4be2012-11-26 09:14:16 -08001602/*===========================================================================
1603 * FUNCTION : procEvtVideoPicTakingState
1604 *
1605 * DESCRIPTION: finite state machine function to handle event in state of
1606 * QCAMERA_SM_STATE_VIDEO_PIC_TAKING.
1607 *
1608 * PARAMETERS :
1609 * @evt : event to be processed
1610 * @payload : event payload. Can be NULL if not needed.
1611 *
1612 * RETURN : int32_t type of status
1613 * NO_ERROR -- success
1614 * none-zero failure code
1615 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001616int32_t QCameraStateMachine::procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt,
1617 void *payload)
1618{
1619 int32_t rc = NO_ERROR;
1620 qcamera_api_result_t result;
1621 memset(&result, 0, sizeof(qcamera_api_result_t));
1622
1623 switch (evt) {
1624 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
1625 {
1626 // Error setting preview window during previewing
1627 ALOGE("Cannot set preview window when preview is running");
1628 rc = INVALID_OPERATION;
1629 result.status = rc;
1630 result.request_api = evt;
1631 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1632 m_parent->signalAPIResult(&result);
1633 }
1634 break;
1635 case QCAMERA_SM_EVT_SET_CALLBACKS:
1636 {
1637 qcamera_sm_evt_setcb_payload_t *setcbs =
1638 (qcamera_sm_evt_setcb_payload_t *)payload;
1639 rc = m_parent->setCallBacks(setcbs->notify_cb,
1640 setcbs->data_cb,
1641 setcbs->data_cb_timestamp,
1642 setcbs->get_memory,
1643 setcbs->user);
1644 result.status = rc;
1645 result.request_api = evt;
1646 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1647 m_parent->signalAPIResult(&result);
1648 }
1649 break;
1650 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1651 {
1652 rc = m_parent->enableMsgType(int32_t(payload));
1653 result.status = rc;
1654 result.request_api = evt;
1655 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1656 m_parent->signalAPIResult(&result);
1657 }
1658 break;
1659 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1660 {
1661 rc = m_parent->disableMsgType(int32_t(payload));
1662 result.status = rc;
1663 result.request_api = evt;
1664 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1665 m_parent->signalAPIResult(&result);
1666 }
1667 break;
1668 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1669 {
1670 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1671 result.status = rc;
1672 result.request_api = evt;
1673 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1674 result.enabled = enabled;
1675 m_parent->signalAPIResult(&result);
1676 }
1677 break;
1678 case QCAMERA_SM_EVT_SET_PARAMS:
1679 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001680 bool needRestart = false;
1681 rc = m_parent->updateParameters((char*)payload, needRestart);
1682 if (rc == NO_ERROR) {
1683 if (needRestart) {
1684 // cannot set parameters that requires restart during recording
1685 ALOGE("%s: Cannot set parameters that requires restart during recording",
1686 __func__);
1687 rc = BAD_VALUE;
1688 } else {
1689 rc = m_parent->commitParameterChanges();
1690 }
1691 }
Muhua Libc9a8082012-11-07 15:51:28 -08001692 result.status = rc;
1693 result.request_api = evt;
1694 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1695 m_parent->signalAPIResult(&result);
1696 }
1697 break;
1698 case QCAMERA_SM_EVT_GET_PARAMS:
1699 {
1700 result.params = m_parent->getParameters();
1701 rc = NO_ERROR;
1702 result.status = rc;
1703 result.request_api = evt;
1704 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1705 m_parent->signalAPIResult(&result);
1706 }
1707 break;
1708 case QCAMERA_SM_EVT_PUT_PARAMS:
1709 {
1710 rc = m_parent->putParameters((char*)payload);
1711 result.status = rc;
1712 result.request_api = evt;
1713 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1714 m_parent->signalAPIResult(&result);
1715 }
1716 break;
1717 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1718 {
1719 rc = NO_ERROR;
1720 result.status = rc;
1721 result.request_api = evt;
1722 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1723 result.enabled = 1;
1724 m_parent->signalAPIResult(&result);
1725 }
1726 break;
1727 case QCAMERA_SM_EVT_RECORDING_ENABLED:
1728 {
1729 rc = NO_ERROR;
1730 result.status = rc;
1731 result.request_api = evt;
1732 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1733 result.enabled = 1;
1734 m_parent->signalAPIResult(&result);
1735 }
1736 break;
1737 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
1738 {
1739 rc = m_parent->storeMetaDataInBuffers(int(payload));
1740 result.status = rc;
1741 result.request_api = evt;
1742 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1743 m_parent->signalAPIResult(&result);
1744 }
1745 break;
1746 case QCAMERA_SM_EVT_DUMP:
1747 {
1748 rc = m_parent->dump((int)payload);
1749 result.status = rc;
1750 result.request_api = evt;
1751 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1752 m_parent->signalAPIResult(&result);
1753 }
1754 break;
1755 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
1756 {
1757 rc = m_parent->autoFocus();
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_AUTO_FOCUS:
1765 {
1766 rc = m_parent->cancelAutoFocus();
1767 result.status = rc;
1768 result.request_api = evt;
1769 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1770 m_parent->signalAPIResult(&result);
1771 }
1772 break;
1773 case QCAMERA_SM_EVT_SEND_COMMAND:
1774 {
1775 qcamera_sm_evt_command_payload_t *cmd_payload =
1776 (qcamera_sm_evt_command_payload_t *)payload;
1777 rc = m_parent->sendCommand(cmd_payload->cmd,
1778 cmd_payload->arg1,
1779 cmd_payload->arg2);
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_STOP_RECORDING:
1787 {
1788 rc = m_parent->stopRecording();
1789 m_state = QCAMERA_SM_STATE_PREVIEW_PIC_TAKING;
1790 result.status = rc;
1791 result.request_api = evt;
1792 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1793 m_parent->signalAPIResult(&result);
1794 }
1795 break;
1796 case QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME:
1797 {
1798 rc = m_parent->releaseRecordingFrame((const void *)payload);
1799 result.status = rc;
1800 result.request_api = evt;
1801 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1802 m_parent->signalAPIResult(&result);
1803 }
1804 break;
1805 case QCAMERA_SM_EVT_CANCEL_PICTURE:
1806 {
1807 rc = m_parent->cancelLiveSnapshot();
1808 m_state = QCAMERA_SM_STATE_RECORDING;
1809 result.status = rc;
1810 result.request_api = evt;
1811 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1812 m_parent->signalAPIResult(&result);
1813 }
1814 break;
1815 case QCAMERA_SM_EVT_START_RECORDING:
1816 case QCAMERA_SM_EVT_START_PREVIEW:
1817 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
1818 case QCAMERA_SM_EVT_STOP_PREVIEW:
1819 case QCAMERA_SM_EVT_TAKE_PICTURE:
1820 case QCAMERA_SM_EVT_RELEASE:
1821 {
1822 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1823 rc = INVALID_OPERATION;
1824 result.status = rc;
1825 result.request_api = evt;
1826 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1827 m_parent->signalAPIResult(&result);
1828 }
1829 break;
Muhua Li31eaee02012-12-11 08:56:45 -08001830 case QCAMERA_SM_EVT_EVT_INTERNAL:
1831 {
1832 qcamera_sm_internal_evt_payload_t *internal_evt =
1833 (qcamera_sm_internal_evt_payload_t *)payload;
1834 switch (internal_evt->evt_type) {
1835 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
1836 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
1837 break;
1838 default:
1839 break;
1840 }
1841 }
1842 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001843 case QCAMERA_SM_EVT_EVT_NOTIFY:
1844 {
1845 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
1846 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08001847 case CAM_EVENT_TYPE_ZOOM_DONE:
1848 rc = m_parent->processZoomEvent(cam_evt->status);
1849 break;
1850 default:
1851 ALOGD("%s: no handling for server evt (%d) at this state",
1852 __func__, cam_evt->server_event_type);
1853 break;
1854 }
1855 }
1856 break;
1857 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
1858 {
1859 qcamera_jpeg_evt_payload_t *jpeg_job =
1860 (qcamera_jpeg_evt_payload_t *)payload;
1861 rc = m_parent->processJpegNotify(jpeg_job);
1862 }
1863 break;
1864 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
1865 {
1866 rc = m_parent->cancelLiveSnapshot();
1867 m_state = QCAMERA_SM_STATE_RECORDING;
1868 }
1869 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08001870 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
1871 //TODO: Adjust FPS.
1872 break;
Muhua Libc9a8082012-11-07 15:51:28 -08001873 default:
1874 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
1875 break;
1876 }
1877
1878 return rc;
1879}
1880
Muhua Lida2c4be2012-11-26 09:14:16 -08001881/*===========================================================================
1882 * FUNCTION : procEvtPreviewPicTakingState
1883 *
1884 * DESCRIPTION: finite state machine function to handle event in state of
1885 * QCAMERA_SM_STATE_PREVIEW_PIC_TAKING.
1886 *
1887 * PARAMETERS :
1888 * @evt : event to be processed
1889 * @payload : event payload. Can be NULL if not needed.
1890 *
1891 * RETURN : int32_t type of status
1892 * NO_ERROR -- success
1893 * none-zero failure code
1894 *==========================================================================*/
Muhua Libc9a8082012-11-07 15:51:28 -08001895int32_t QCameraStateMachine::procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt,
1896 void *payload)
1897{
1898 int32_t rc = NO_ERROR;
1899 qcamera_api_result_t result;
1900 memset(&result, 0, sizeof(qcamera_api_result_t));
1901
1902 switch (evt) {
1903 case QCAMERA_SM_EVT_SET_CALLBACKS:
1904 {
1905 qcamera_sm_evt_setcb_payload_t *setcbs =
1906 (qcamera_sm_evt_setcb_payload_t *)payload;
1907 rc = m_parent->setCallBacks(setcbs->notify_cb,
1908 setcbs->data_cb,
1909 setcbs->data_cb_timestamp,
1910 setcbs->get_memory,
1911 setcbs->user);
1912 result.status = rc;
1913 result.request_api = evt;
1914 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1915 m_parent->signalAPIResult(&result);
1916 }
1917 break;
1918 case QCAMERA_SM_EVT_ENABLE_MSG_TYPE:
1919 {
1920 rc = m_parent->enableMsgType(int32_t(payload));
1921 result.status = rc;
1922 result.request_api = evt;
1923 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1924 m_parent->signalAPIResult(&result);
1925 }
1926 break;
1927 case QCAMERA_SM_EVT_DISABLE_MSG_TYPE:
1928 {
1929 rc = m_parent->disableMsgType(int32_t(payload));
1930 result.status = rc;
1931 result.request_api = evt;
1932 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1933 m_parent->signalAPIResult(&result);
1934 }
1935 break;
1936 case QCAMERA_SM_EVT_MSG_TYPE_ENABLED:
1937 {
1938 int enabled = m_parent->msgTypeEnabled(int32_t(payload));
1939 result.status = rc;
1940 result.request_api = evt;
1941 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
1942 result.enabled = enabled;
1943 m_parent->signalAPIResult(&result);
1944 }
1945 break;
1946 case QCAMERA_SM_EVT_SET_PARAMS:
1947 {
Muhua Lida2c4be2012-11-26 09:14:16 -08001948 bool needRestart = false;
1949 rc = m_parent->updateParameters((char*)payload, needRestart);
1950 if (rc == NO_ERROR) {
1951 if (needRestart) {
1952 // need restart preview for parameters to take effect
1953 // stop preview
1954 m_parent->stopPreview();
1955 // commit parameter changes to server
Muhua Li66f3b532013-01-23 17:19:05 -08001956 m_parent->commitParameterChanges();
Muhua Lida2c4be2012-11-26 09:14:16 -08001957 // start preview again
Muhua Li66f3b532013-01-23 17:19:05 -08001958 rc = m_parent->preparePreview();
1959 if (rc == NO_ERROR) {
1960 rc = m_parent->startPreview();
1961 if (rc != NO_ERROR) {
1962 m_parent->unpreparePreview();
1963 }
1964 }
1965 if (rc != NO_ERROR) {
1966 m_state = QCAMERA_SM_STATE_PIC_TAKING;
1967 }
Muhua Lida2c4be2012-11-26 09:14:16 -08001968 } else {
1969 rc = m_parent->commitParameterChanges();
1970 }
1971 }
Muhua Libc9a8082012-11-07 15:51:28 -08001972 result.status = rc;
1973 result.request_api = evt;
1974 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1975 m_parent->signalAPIResult(&result);
1976 }
1977 break;
1978 case QCAMERA_SM_EVT_GET_PARAMS:
1979 {
1980 result.params = m_parent->getParameters();
1981 rc = NO_ERROR;
1982 result.status = rc;
1983 result.request_api = evt;
1984 result.result_type = QCAMERA_API_RESULT_TYPE_PARAMS;
1985 m_parent->signalAPIResult(&result);
1986 }
1987 break;
1988 case QCAMERA_SM_EVT_PUT_PARAMS:
1989 {
1990 rc = m_parent->putParameters((char*)payload);
1991 result.status = rc;
1992 result.request_api = evt;
1993 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
1994 m_parent->signalAPIResult(&result);
1995 }
1996 break;
1997 case QCAMERA_SM_EVT_PREVIEW_ENABLED:
1998 {
1999 rc = NO_ERROR;
2000 result.status = rc;
2001 result.request_api = evt;
2002 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2003 result.enabled = 1;
2004 m_parent->signalAPIResult(&result);
2005 }
2006 break;
2007 case QCAMERA_SM_EVT_RECORDING_ENABLED:
2008 {
2009 rc = NO_ERROR;
2010 result.status = rc;
2011 result.request_api = evt;
2012 result.result_type = QCAMERA_API_RESULT_TYPE_ENABLE_FLAG;
2013 result.enabled = 0;
2014 m_parent->signalAPIResult(&result);
2015 }
2016 break;
2017 case QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS:
2018 {
2019 rc = m_parent->storeMetaDataInBuffers(int(payload));
2020 result.status = rc;
2021 result.request_api = evt;
2022 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2023 m_parent->signalAPIResult(&result);
2024 }
2025 break;
2026 case QCAMERA_SM_EVT_DUMP:
2027 {
2028 rc = m_parent->dump((int)payload);
2029 result.status = rc;
2030 result.request_api = evt;
2031 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2032 m_parent->signalAPIResult(&result);
2033 }
2034 break;
2035 case QCAMERA_SM_EVT_START_AUTO_FOCUS:
2036 {
2037 rc = m_parent->autoFocus();
2038 result.status = rc;
2039 result.request_api = evt;
2040 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2041 m_parent->signalAPIResult(&result);
2042 }
2043 break;
2044 case QCAMERA_SM_EVT_STOP_AUTO_FOCUS:
2045 {
2046 rc = m_parent->cancelAutoFocus();
2047 result.status = rc;
2048 result.request_api = evt;
2049 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2050 m_parent->signalAPIResult(&result);
2051 }
2052 break;
2053 case QCAMERA_SM_EVT_SEND_COMMAND:
2054 {
2055 qcamera_sm_evt_command_payload_t *cmd_payload =
2056 (qcamera_sm_evt_command_payload_t *)payload;
2057 rc = m_parent->sendCommand(cmd_payload->cmd,
2058 cmd_payload->arg1,
2059 cmd_payload->arg2);
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_RELEASE_RECORIDNG_FRAME:
2067 {
2068 rc = m_parent->releaseRecordingFrame((const void *)payload);
2069 result.status = rc;
2070 result.request_api = evt;
2071 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2072 m_parent->signalAPIResult(&result);
2073 }
2074 break;
2075 case QCAMERA_SM_EVT_CANCEL_PICTURE:
2076 {
2077 rc = m_parent->cancelLiveSnapshot();
2078 m_state = QCAMERA_SM_STATE_PREVIEWING;
2079 result.status = rc;
2080 result.request_api = evt;
2081 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2082 m_parent->signalAPIResult(&result);
2083 }
2084 break;
2085 case QCAMERA_SM_EVT_STOP_PREVIEW:
2086 {
2087 m_parent->stopChannel(QCAMERA_CH_TYPE_PREVIEW);
2088 m_parent->delChannel(QCAMERA_CH_TYPE_PREVIEW);
2089 m_parent->delChannel(QCAMERA_CH_TYPE_VIDEO);
2090 m_state = QCAMERA_SM_STATE_PIC_TAKING;
2091 rc = NO_ERROR;
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_START_RECORDING:
2099 {
2100 rc = m_parent->stopRecording();
2101 if (rc == NO_ERROR) {
2102 m_state = QCAMERA_SM_STATE_VIDEO_PIC_TAKING;
2103 }
2104 result.status = rc;
2105 result.request_api = evt;
2106 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2107 m_parent->signalAPIResult(&result);
2108 }
2109 break;
2110 case QCAMERA_SM_EVT_STOP_RECORDING:
2111 case QCAMERA_SM_EVT_START_PREVIEW:
2112 case QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW:
2113 case QCAMERA_SM_EVT_TAKE_PICTURE:
2114 case QCAMERA_SM_EVT_SET_PREVIEW_WINDOW:
2115 case QCAMERA_SM_EVT_RELEASE:
2116 {
2117 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2118 rc = INVALID_OPERATION;
2119 result.status = rc;
2120 result.request_api = evt;
2121 result.result_type = QCAMERA_API_RESULT_TYPE_DEF;
2122 m_parent->signalAPIResult(&result);
2123 }
2124 break;
Muhua Li31eaee02012-12-11 08:56:45 -08002125 case QCAMERA_SM_EVT_EVT_INTERNAL:
2126 {
2127 qcamera_sm_internal_evt_payload_t *internal_evt =
2128 (qcamera_sm_internal_evt_payload_t *)payload;
2129 switch (internal_evt->evt_type) {
2130 case QCAMERA_INTERNAL_EVT_FOCUS_UPDATE:
2131 rc = m_parent->processAutoFocusEvent(internal_evt->focus_data);
2132 break;
2133 default:
2134 break;
2135 }
2136 }
2137 break;
Muhua Libc9a8082012-11-07 15:51:28 -08002138 case QCAMERA_SM_EVT_EVT_NOTIFY:
2139 {
2140 mm_camera_event_t *cam_evt = (mm_camera_event_t *)payload;
2141 switch (cam_evt->server_event_type) {
Muhua Libc9a8082012-11-07 15:51:28 -08002142 case CAM_EVENT_TYPE_ZOOM_DONE:
2143 rc = m_parent->processZoomEvent(cam_evt->status);
2144 break;
2145 default:
2146 ALOGD("%s: no handling for server evt (%d) at this state",
2147 __func__, cam_evt->server_event_type);
2148 break;
2149 }
2150 }
2151 break;
2152 case QCAMERA_SM_EVT_JPEG_EVT_NOTIFY:
2153 {
2154 qcamera_jpeg_evt_payload_t *jpeg_job =
2155 (qcamera_jpeg_evt_payload_t *)payload;
2156 rc = m_parent->processJpegNotify(jpeg_job);
2157 }
2158 break;
2159 case QCAMERA_SM_EVT_SNAPSHOT_DONE:
2160 {
2161 rc = m_parent->cancelLiveSnapshot();
2162 m_state = QCAMERA_SM_STATE_PREVIEWING;
2163 }
2164 break;
Shuzhen Wangaa829ce2013-01-09 14:41:49 -08002165 case QCAMERA_SM_EVT_THERMAL_NOTIFY:
2166 //TODO: Adjust FPS
2167 break;
Muhua Libc9a8082012-11-07 15:51:28 -08002168 default:
2169 ALOGE("%s: cannot handle evt(%d) in state(%d)", __func__, evt, m_state);
2170 break;
2171 }
2172
2173 return rc;
2174}
2175
Muhua Lida2c4be2012-11-26 09:14:16 -08002176/*===========================================================================
2177 * FUNCTION : isPreviewRunning
2178 *
2179 * DESCRIPTION: check if preview is in process.
2180 *
2181 * PARAMETERS : None
2182 *
2183 * RETURN : true -- preview running
2184 * false -- preview stopped
2185 *==========================================================================*/
2186bool QCameraStateMachine::isPreviewRunning()
2187{
2188 switch (m_state) {
2189 case QCAMERA_SM_STATE_PREVIEWING:
2190 case QCAMERA_SM_STATE_RECORDING:
2191 case QCAMERA_SM_STATE_VIDEO_PIC_TAKING:
2192 case QCAMERA_SM_STATE_PREVIEW_PIC_TAKING:
2193 return true;
2194 default:
2195 return false;
2196 }
2197}
2198
Shuzhen Wang89635cf2012-12-20 13:47:22 -08002199}; // namespace qcamera