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