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