Ejemplo n.º 1
0
/*******************************************************************************
**
** Function         btif_task
**
** Description      BTIF task handler managing all messages from stack&btif.
**
** Returns          void
**
*******************************************************************************/
void *btif_task(void *arg)
{
	bt_gap_context_t *pContext = (bt_gap_context_t *)arg;
	int bExit = 0;
	int i = 0;
	
	ALOGI("+++[btif_task]+++!\n");
    btif_associate_evt();
#if 1
	while (1) {
		ALOGI("[btif_task]:Start retrieve data pollMemberCount=%d!\n", pContext->pollMemberCount);
		for (i = 0; i < pContext->pollMemberCount; i++) {
			if (!pContext->pollData[i].revents) {
				continue;
			}
			
			ALOGI("[btif_task]:fd %d data ready!\n", i);
			{
				ilm_struct ilm;
				int ret;

				ALOGI("[btif_task]:pollData[i].fd data ready : revents = 0x%X!\n", pContext->pollData[i].revents);
				while ((ret = recv(pContext->pollData[i].fd, (void*)&ilm, sizeof(ilm_struct), MSG_DONTWAIT)) != -1) 
				{
					btmtk_ext_msg_handler_struct *ptr = pContext->pHandler;
					pthread_mutex_lock(&(pContext->thread_mutex));
					while(ptr)
					{
						ALOGI("in profile[%d] handler!", ptr->profile_id);
						if(pContext->pollData[i].fd == ptr->socket)
						{
							if((ptr->profile_id == PROFILE_GAP) && (ilm.msg_id == EV_BT_CB_TASK_EXIT))
							{
								pthread_mutex_unlock(&(pContext->thread_mutex));
								goto BTIF_TASK_EXIT;
							}
							
							if(ptr->handler != NULL)
							{
								ptr->handler(ptr->pContext, &ilm);
								ALOGI("out profile[%d] handler!", ptr->profile_id);
							}
							break;
						}
						ALOGI("out profile[%d] handler!", ptr->profile_id);
						ptr = ptr->next;
					}
					pthread_mutex_unlock(&(pContext->thread_mutex));
				}
			}
		}
		ALOGI("[btif_task]:Start polling!\n");
		poll(pContext->pollData, pContext->pollMemberCount, -1);
		ALOGI("[btif_task]:Polling returned!\n");
	}
#else
	
	while(1)
	{
		ilm_struct ilm;
		int ret = 0;
		fd_set fd;
		
		FD_ZERO(&fd);
		for (i = 0; i < pContext->pollMemberCount; i++)
		{
			FD_SET(pContext->pollData[i].fd, &fd);
			ret = select(pContext->pollData[i].fd + 1, &fd, NULL, NULL, NULL);
			if(ret < 0)
			{
				ALOGI("[btif_task]:select failed : %s, errno=%d", strerror(errno), errno);
            	continue;
			}
			else if(ret == 0)
			{
				ALOGI("[btif_task]:wait timeout");
			}
			else
			{
				if(pContext->pollData[i].fd >= 0 && FD_ISSET(pContext->pollData[i].fd, &fs))
				{
					ALOGI("+++[btif_task]:select success!\n+++");
					ret = recvfrom(pContext->pollData[i].fd, ilm, sizeof(ilm_struct), 0, NULL, NULL);
					if(ret > 0)
					{
						btmtk_ext_msg_handler_struct *ptr = pContext->pHandler;
						while(ptr)
						{
							if(pContext->pollData[i].fd == ptr->socket)
							{
								if(ptr->profile_id == PROFILE_GAP && ilm.msg_id == EV_BT_CB_TASK_EXIT)
								{
									goto BTIF_TASK_EXIT;
								}
								ptr->handler(ptr->pContext, &ilm);
							}
							ptr = ptr->next;
						}
					}
					ALOGI("---[btif_task]:select success!\n---");
				}
			}
		}
	}
#endif
BTIF_TASK_EXIT:
	btmtk_clear_handlers();
	btif_disassociate_evt();
    ALOGI("---[btif_task]---!\n");
	return 0;
}
Ejemplo n.º 2
0
static void btif_task(UINT32 params)
{
    UINT16   event;
    BT_HDR   *p_msg;

    BTIF_TRACE_DEBUG0("btif task starting");

    btif_associate_evt();

    for(;;)
    {
        /* wait for specified events */
        event = GKI_wait(0xFFFF, 0);

        /*
         * Wait for the trigger to init chip and stack. This trigger will
         * be received by btu_task once the UART is opened and ready
         */
        if (event == BT_EVT_TRIGGER_STACK_INIT)
        {
            BTIF_TRACE_DEBUG0("btif_task: received trigger stack init event");
            btif_enable_bluetooth_evt(BT_STATE_ON);
        }

        /*
         * Failed to initialize controller hardware, reset state and bring
         * down all threads
         */
        if (event == BT_EVT_HARDWARE_INIT_FAIL)
        {
            BTIF_TRACE_DEBUG0("btif_task: hardware init failed");
            bte_main_disable();
            GKI_task_self_cleanup(BTIF_TASK);
            bte_main_shutdown();
            btif_dut_mode = 0;
            btif_core_state = BTIF_CORE_STATE_DISABLED;
            HAL_CBACK(bt_hal_cbacks,adapter_state_changed_cb,BT_STATE_OFF);
            break;
        }

        if (event & EVENT_MASK(GKI_SHUTDOWN_EVT))
            break;

        if(event & TASK_MBOX_1_EVT_MASK)
        {
            while((p_msg = GKI_read_mbox(BTU_BTIF_MBOX)) != NULL)
            {
                BTIF_TRACE_VERBOSE1("btif task fetched event %x", p_msg->event);

                switch (p_msg->event)
                {
                    case BT_EVT_CONTEXT_SWITCH_EVT:
                        btif_context_switched(p_msg);
                        break;

                    case BT_EVT_RX:
                    btif_mp_test_evt(p_msg);
                    break;
                    
                    case BT_EVT_MP_NOTIFY_BTIF:
                    btif_mp_notify_evt(p_msg);

                    break;
                    

                    default:
                        BTIF_TRACE_ERROR1("unhandled btif event (%d)", p_msg->event & BT_EVT_MASK);
                        break;
                }

                GKI_freebuf(p_msg);
            }
        }
    }

    btif_disassociate_evt();

    BTIF_TRACE_DEBUG0("btif task exiting");
}