/*************************************************************************** * Copyright (c) 2010, Broadcom Corporation * All Rights Reserved * Confidential Property of Broadcom Corporation * * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. * * $brcm_Workfile: $ * $brcm_Revision: $ * $brcm_Date: $ * * Module Description: * * Revision History: * * $brcm_Log: $ * ***************************************************************************/ #include "bapp_remote.h" #include "bos.h" #include "bir_codes.h" #include "bsettop_user_io.h" BDBG_MODULE(app_remote); /* Register software module with debug interface */ #define BUSER_IO_NUM_EVENTS 16 #define BUSER_IO_MSG BDBG_ERR /* Convert timeout from milliseconds to ticks */ #define MAX_TIMEOUT ((300 * g_ticks_per_second)/ 1000) #define DEFAULT_PEND_TIMEOUT 10 /* in milliseconds */ static bapp_remote_config_t s_config = { 8, false/*rf4ce*/, true/*ir*/ }; struct bapp_remote { /* User IO */ buser_input_t userio; unsigned int timeout; unsigned int pend_timeout; b_queue_t queue; b_event_t events[BUSER_IO_NUM_EVENTS]; }; static struct bapp_remote *s_p_remote = NULL; /* User Input API */ /* Summary: Open a user input object for receiving IR remote and keypad input. Description: For now, the following id's are used: 0 - remote a 1 - remote b 2 - 56 MHz Sejin IR Keyboard 3 - keypad (TODO implement) 8 - Moto remote, device_type = 0x14 */ bapp_result_t bapp_remote_open(bapp_remote_t *p_remote) { *p_remote = (struct bapp_remote*)BKNI_Malloc(sizeof(struct bapp_remote)); if (!*p_remote) return eBAPP_RESULT_ALLOC_FAILURE; BKNI_Memset(*p_remote,0,sizeof(struct bapp_remote)); bos_create_queue(&(*p_remote)->queue,(*p_remote)->events,BUSER_IO_NUM_EVENTS); if (!(*p_remote)->queue) { bapp_remote_close(*p_remote); return eBAPP_RESULT_ALLOC_FAILURE; } /* USER IO */ (*p_remote)->userio = buser_input_open(B_ID(s_config.user_io_id)); s_p_remote = *p_remote; s_p_remote->pend_timeout = DEFAULT_PEND_TIMEOUT; return eBAPP_RESULT_OK; } /* Summary: Close a user input handle. Description: Releases all resources associated with the user input object */ void bapp_remote_close( bapp_remote_t handle /* user input object */ ) { struct bapp_remote *p_remote = (struct bapp_remote*)handle; if (p_remote->queue) { bos_delete_queue(&p_remote->queue); } /* USER IO */ if (handle->userio) { buser_input_close(handle->userio); } BKNI_Free(p_remote); } /* Summary: Map key codes to match type 0 */ static unsigned int bapp_remote_map( unsigned int code ) { unsigned int mapped_code = 0xFF; BDBG_ERR(("############### %s, code = 0x%08x\n",__FUNCTION__,code)); code &= 0x000000FF; switch (code) { default: mapped_code = code; break; case 0x09: mapped_code = eIR_MENU; break; case 0x37: mapped_code = eIR_PGUP; break; case 0x38: mapped_code = eIR_PGDOWN; break; case 0x0d: mapped_code = eIR_EXIT; break; case 0x4c: mapped_code = eIR_PRECH; break; /* TODO: */ #if 0 case 0x74: mapped_code = eIR_A; break; case 0x71: mapped_code = eIR_B; break; case 0x72: mapped_code = eIR_C; break; case 0x73: mapped_code = eIR_D; break; #endif case 0x00: mapped_code = eIR_SELECT; break; case 0x01: mapped_code = eIR_UP; break; case 0x02: mapped_code = eIR_DOWN; break; case 0x03: mapped_code = eIR_LEFT; break; case 0x04: mapped_code = eIR_RIGHT; break; case 0x0b: mapped_code = eIR_GUIDE; break; case 0x6b: mapped_code = eIR_POWER; break; case 0x21: mapped_code = eIR_1; break; case 0x22: mapped_code = eIR_2; break; case 0x23: mapped_code = eIR_3; break; case 0x24: mapped_code = eIR_4; break; case 0x25: mapped_code = eIR_5; break; case 0x26: mapped_code = eIR_6; break; case 0x27: mapped_code = eIR_7; break; case 0x28: mapped_code = eIR_8; break; case 0x29: mapped_code = eIR_9; break; case 0x20: mapped_code = eIR_0; break; case 0x30: mapped_code = eIR_CH_UP; break; case 0x31: mapped_code = eIR_CH_DOWN; break; /* mute */ case 0x43: mapped_code = eIR_MUTE; break; /* + vol */ case 0x41: mapped_code = eIR_VOL_UP; break; /* - vol */ case 0x42: mapped_code = eIR_VOL_DOWN; break; /* exit */ case 0x2a: mapped_code = eIR_EXIT; break; case 0x2b: mapped_code = eIR_SELECT; break; case 0x35: mapped_code = eIR_INFO; break; case 0x32: mapped_code = eIR_PRECH; break; /* power off/on */ case 0x6c: case 0x6d: mapped_code = eIR_POWER; break; /* help */ case 0x56: mapped_code = eIR_HELP; break; /* FAV */ case 0x52: mapped_code = eIR_FAV; break; case 0x53: mapped_code = eIR_GUIDE; break; case 0xfd: mapped_code = eIR_LANG; break; } return mapped_code; } /* Summary: map gpio based buttons to key code based on current IR remote protocol used */ static unsigned int bapp_remote_map_code( bapp_remote_t handle, /* user input object */ unsigned int button_id) { return bapp_remote_map(button_id); } /* Summary: Read events from a user input device. Description: Because this function does not return a void* to raw data, but an array of structures, it is not called buser_input_read. */ bapp_result_t bapp_remote_get_event( bapp_remote_t handle, /* user input object */ bapp_event_t *p_event /* [out] event from the user input device */ ) { uint32_t pend_event; bapp_result_t result = eBAPP_RESULT_BUSY; unsigned int event_cnt; struct bapp_remote *p_remote = (struct bapp_remote*)handle; /* check first IR remote (10ms timeout is consumed) */ if (buser_input_get_event(p_remote->userio,(buser_input_event*)p_event,1,&event_cnt) == b_ok) { if (event_cnt > 0) { if (s_config.use_ir) return eBAPP_RESULT_OK; else return eBAPP_RESULT_TIMEOUT; } else { return eBAPP_RESULT_TIMEOUT; } } /* for RF4CE remote */ pend_event = (unsigned int)bos_pend_event(p_remote->queue,p_remote->pend_timeout); if (pend_event) { if (!s_config.use_rf4ce) return eBAPP_RESULT_TIMEOUT; BDBG_ERR(("%s, pend_event = 0x%08x\n",__FUNCTION__,pend_event)); p_event->code = pend_event & 0xFF; if (!(pend_event & eKEY_CMD)) { p_event->code = bapp_remote_map_code(handle,p_event->code); } if (pend_event & eKEY_UP) p_event->code |= eKEY_UP; result = eBAPP_RESULT_OK; BUSER_IO_MSG(("%s, event = 0x%08x\n",__FUNCTION__,p_event->code)); } return result; } void bapp_remote_set_config( bapp_remote_config_t *p_config ) { BKNI_Memcpy(&s_config,p_config,sizeof(s_config)); } void bapp_remote_get_config( bapp_remote_config_t *p_config ) { BKNI_Memcpy(p_config,&s_config,sizeof(s_config)); } void bapp_remote_post_event(uint32_t code,uint8_t modifier) { if (s_p_remote && s_p_remote->queue) { if (modifier == 0x01) bos_post_event(s_p_remote->queue,(b_event_t*)(eKEY_DOWN | code)); else bos_post_event(s_p_remote->queue,(b_event_t*)(eKEY_UP | code)); } }