/*****************************************************************************
NAME    
     headsetHandleUserConfirmationInd
    
DESCRIPTION
     This function is called on receipt on an CL_SM_USER_CONFIRMATION_REQ_IND

RETURNS
     void
*/
void headsetHandleUserConfirmationInd(const CL_SM_USER_CONFIRMATION_REQ_IND_T* ind)
{
    if (AuthCanHeadsetPair())
    {
#if 1  
        theHeadset.confirmation = TRUE;
        AUTH_DEBUG(("auth: can confirm %ld\n", ind->numeric_value));
        /* should use text to speech here */
        theHeadset.confirmation_addr = (bdaddr*)PanicUnlessMalloc(sizeof(bdaddr));
        *theHeadset.confirmation_addr = ind->bd_addr;
#else
        ConnectionSmUserConfirmationResponse(&ind->bd_addr, TRUE);
        /* infor ssp numeric_value */
        UartPrintf("\r\n+SSP=%ld\r\n",ind->numeric_value);
#endif        
    }
    else
    {
        /* reject the confirmation request */
        AUTH_DEBUG(("auth: rejecting confirmation req\n"));
        ConnectionSmUserConfirmationResponse(&ind->bd_addr, FALSE);
    }
}
void ConnectionSmUserConfirmationResponseTestExtra(
        uint8           type,
        const bdaddr*   bd_addr,
        uint16          transport,
        bool            confirm
        )
{
    tp_bdaddr       tpaddr;

    tpaddr.transport    = (TRANSPORT_T) transport;
    tpaddr.taddr.type   = type;
    tpaddr.taddr.addr   = *bd_addr;

    ConnectionSmUserConfirmationResponse(&tpaddr, confirm);
}
/****************************************************************************
NAME
    clMsgHandleLibMessage

DESCRIPTION
    Handles the CL library messages and calls the relevant function.

*/
void clMsgHandleLibMessage(MessageId id, Message message)
{
    switch(id)
    {
        case CL_INIT_CFM:
        {
            DEBUG_CL(("CL_INIT_CFM status = %u\n", ((CL_INIT_CFM_T *)message)->status));
            handleClInitCfm((CL_INIT_CFM_T *)message);    
            break;
        }    
        case CL_DM_WRITE_INQUIRY_MODE_CFM:
        {
            DEBUG_CL(("CL_DM_WRITE_INQUIRY_MODE_CFM\n"));
            /* Read the local name to put in our EIR data */
            ConnectionReadInquiryTx(&the_app->task);
            break;
        }    
        case CL_DM_READ_INQUIRY_TX_CFM:
        {
            the_app->inquiry_tx = ((CL_DM_READ_INQUIRY_TX_CFM_T*)message)->tx_power;
            ConnectionReadLocalName(&the_app->task);
            break;
        }    
        case CL_DM_LOCAL_NAME_COMPLETE:
        {
            DEBUG_CL(("CL_DM_LOCAL_NAME_COMPLETE\n"));
            /* Write EIR data and initialise the codec task */
            scanWriteEirData((CL_DM_LOCAL_NAME_COMPLETE_T*)message);
            break;
        }    
        case CL_DM_ACL_OPENED_IND:
        {
            DEBUG_CL(("CL_DM_ACL_OPENED_IND from: 0x%X 0x%X 0x%lX\n", ((CL_DM_ACL_OPENED_IND_T *)message)->bd_addr.nap, ((CL_DM_ACL_OPENED_IND_T *)message)->bd_addr.uap, ((CL_DM_ACL_OPENED_IND_T *)message)->bd_addr.lap));
            /* Ignore this message for now */
            DEBUG_CL((" - ignored\n"));
            break;
        }    
        case CL_DM_ACL_CLOSED_IND:
        {
            DEBUG_CL(("CL_DM_ACL_CLOSED_IND from: 0x%X 0x%X 0x%lX\n", ((CL_DM_ACL_CLOSED_IND_T *)message)->bd_addr.nap, ((CL_DM_ACL_CLOSED_IND_T *)message)->bd_addr.uap, ((CL_DM_ACL_CLOSED_IND_T *)message)->bd_addr.lap));
            /* Ignore this message for now */
            DEBUG_CL((" - ignored\n"));
            break;
        }
        case CL_SM_PIN_CODE_IND:
        {
            DEBUG_CL(("CL_SM_PIN_CODE_IND from: 0x%X 0x%X 0x%lX\n", (uint16)((CL_SM_PIN_CODE_IND_T *)message)->bd_addr.nap, (uint16)((CL_SM_PIN_CODE_IND_T *)message)->bd_addr.uap, (uint32)((CL_SM_PIN_CODE_IND_T *)message)->bd_addr.lap));
            handleClSmPinCodeInd((CL_SM_PIN_CODE_IND_T *)message);
            break;
        }
        case CL_SM_IO_CAPABILITY_REQ_IND:
        {
            DEBUG_CL(("CL_SM_IO_CAPABILITY_REQUEST_IND\n"));
            {
                CL_SM_IO_CAPABILITY_REQ_IND_T *prim = (CL_SM_IO_CAPABILITY_REQ_IND_T *)message;
                ConnectionSmIoCapabilityResponse(&prim->bd_addr, cl_sm_io_cap_no_input_no_output, FALSE, TRUE, FALSE, NULL, NULL);
            }
            break;
        }
        case CL_SM_USER_CONFIRMATION_REQ_IND:
        {
            DEBUG_CL(("CL_SM_USER_CONFIRMATION_REQ_IND\n"));
            /* Shouldn't get this so if we do reject it! */
            ConnectionSmUserConfirmationResponse(&((CL_SM_USER_CONFIRMATION_REQ_IND_T*)message)->bd_addr, FALSE);
            break;
        }
        case CL_SM_AUTHORISE_IND:
        {
            DEBUG_CL(("CL_SM_AUTHORISE_IND\n"));
            {    /* For now, blindly accept this request */
                CL_SM_AUTHORISE_IND_T *prim = (CL_SM_AUTHORISE_IND_T *)message;
                ConnectionSmAuthoriseResponse(&prim->bd_addr, prim->protocol_id, prim->channel, prim->incoming, TRUE);
            }    
            break;
        }
        case CL_SM_AUTHENTICATE_CFM:
        {
            DEBUG_CL(("CL_SM_AUTHENTICATE_CFM status = %u\n", ((CL_SM_AUTHENTICATE_CFM_T *)message)->status));
            if ( ((CL_SM_AUTHENTICATE_CFM_T *)message)->status == auth_status_success )
            {    /* Pin code will be stored on a successful SLC/A2DP connection */
            }
            break;
        }
        case CL_SM_SECURITY_LEVEL_CFM:
        {
            DEBUG_CL(("CL_SM_SECURITY_LEVEL_CFM success = %u\n", ((CL_SM_SECURITY_LEVEL_CFM_T *)message)->success));
            break;
        }
        case CL_DM_INQUIRE_RESULT:
        {
            DEBUG_CL(("CL_DM_INQUIRE_RESULT status = %u\n", ((CL_DM_INQUIRE_RESULT_T *)message)->status));
            handleClDmInquireResult((CL_DM_INQUIRE_RESULT_T *)message);        
            break;
        }
        case CL_SDP_OPEN_SEARCH_CFM:
        {   
            DEBUG_CL(("CL_SDP_OPEN_SEARCH_CFM status = %u\n", ((CL_SDP_OPEN_SEARCH_CFM_T *)message)->status)); 
            handleClSdpOpenSearchCfm((CL_SDP_OPEN_SEARCH_CFM_T *)message);       
            break;
        }    
        case CL_SDP_CLOSE_SEARCH_CFM:
        {
            DEBUG_CL(("CL_SDP_CLOSE_SEARCH_CFM status = %u\n", ((CL_SDP_CLOSE_SEARCH_CFM_T *)message)->status));
            handleClSdpCloseSearchCfm();      
            break;
        }    
        case CL_SDP_SERVICE_SEARCH_CFM:
        {
            DEBUG_CL(("CL_SDP_SERVICE_SEARCH_CFM status = %u\n", ((CL_SDP_SERVICE_SEARCH_CFM_T *)message)->status));
            handleClSdpServiceSearchCfm((CL_SDP_SERVICE_SEARCH_CFM_T *)message);          
            break;
        }    
        case CL_SM_SEC_MODE_CONFIG_CFM:
        {
            DEBUG_CL(("CL_SM_SEC_MODE_CONFIG_CFM\n"));
            DEBUG_CL((" - ignored\n"));
            break;
        }    
        case CL_SM_REMOTE_IO_CAPABILITY_IND:
        {
            DEBUG_CL(("CL_SM_REMOTE_IO_CAPABILITY_IND\n"));
            DEBUG_CL((" - ignored\n"));
            break;
        }
        case CL_DM_LINK_SUPERVISION_TIMEOUT_IND:
        {
            DEBUG_CL(("CL_DM_LINK_SUPERVISION_TIMEOUT_IND:\n"));
            DEBUG_CL(("    timeout:[0x%x] bdaddr:[0x%x%x%lx]\n", 
                        ((CL_DM_LINK_SUPERVISION_TIMEOUT_IND_T *)message)->timeout,
                        ((CL_DM_LINK_SUPERVISION_TIMEOUT_IND_T *)message)->bd_addr.nap,
                        ((CL_DM_LINK_SUPERVISION_TIMEOUT_IND_T *)message)->bd_addr.uap,
                        ((CL_DM_LINK_SUPERVISION_TIMEOUT_IND_T *)message)->bd_addr.lap));
            break;
        }
        case CL_DM_ROLE_IND:
        {
            DEBUG_CL(("CL_DM_ROLE_IND\n"));
            handleClRoleInd((CL_DM_ROLE_IND_T *)message);
            break;
        }
        case CL_DM_ROLE_CFM:
        {
            DEBUG_CL(("CL_DM_ROLE_CFM\n"));
            handleClRoleCfm((CL_DM_ROLE_CFM_T *)message);
            break;
        }
        case CL_DM_SNIFF_SUB_RATING_IND:
        {
            DEBUG_CL(("CL_DM_SNIFF_SUB_RATING_IND\n"));
            break;
        }
        case CL_DM_REMOTE_FEATURES_CFM:
        {
            DEBUG_CL(("CL_DM_REMOTE_FEATURES_CFM\n"));
            handleClDmRemoteFeaturesConfirm((CL_DM_REMOTE_FEATURES_CFM_T *)message);
            return;
        }

		case CL_DM_REMOTE_NAME_COMPLETE:
		{
			CL_DM_REMOTE_NAME_COMPLETE_T *name = (CL_DM_REMOTE_NAME_COMPLETE_T*)message;
			DEBUG_CL(("CL_DM_REMOTE_NAME_COMPLETE\n"));

			if(name->status == hci_success)
			{
				char *name_str = PanicUnlessMalloc(name->size_remote_name+1);
				memcpy(name_str,name->remote_name,name->size_remote_name);
				name_str[name->size_remote_name] = 0;
				UartPrintf("\r\n+RNM=%s\r\n",name_str);
				free(name_str);
			}
			else
				UartPrintf("\r\nERROR\r\n");
			
			break;
		}
		case CL_DM_RSSI_CFM:
		{
            DEBUG_CL(("CL_DM_RSSI_CFM_T %d (%d)\n",((CL_DM_RSSI_CFM_T*)message)->status,((CL_DM_RSSI_CFM_T*)message)->rssi));
			UartPrintf("\r\n+RSSI=%d\r\n",((CL_DM_RSSI_CFM_T*)message)->rssi);
            break;
		}
        default:
        {
            DEBUG_CL(("Unhandled CL message 0x%X\n", (uint16)id));    
            break;
        }
    }
}