⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 蓝牙鼠标的例程
💻 C
📖 第 1 页 / 共 3 页
字号:
{
    /* Check if connection is from authorised host */
    if (BdaddrIsSame(&theApp->auth_bd_addr, &ind->bd_addr) && (ind->protocol_id == protocol_l2cap))
    {
    	MAIN_PRINT(("appHandleClSmAuthoriseInd, accept\n"));   

        /* Accept connection */
        ConnectionSmAuthoriseResponse(&ind->bd_addr, ind->protocol_id, ind->channel, ind->incoming, TRUE);
    }
    else
    {
    	MAIN_PRINT(("appHandleClSmAuthoriseInd, reject\n"));   

        /* Reject connection */
        ConnectionSmAuthoriseResponse(&ind->bd_addr, ind->protocol_id, ind->channel, ind->incoming, FALSE);
    }
}

/*************************************************************************
NAME    
    appRejectClSmAuthoriseInd   
DESCRIPTION
    Rejects incoming connection.
RETURNS
    void     
*/
static void appRejectClSmAuthoriseInd(appTaskData *theApp, CL_SM_AUTHORISE_IND_T *ind)
{
	MAIN_PRINT(("appRejectClSmAuthoriseInd\n"));    
    
    /* Reject connection */
    ConnectionSmAuthoriseResponse(&ind->bd_addr, ind->protocol_id, ind->channel, ind->incoming, FALSE);
}

/*************************************************************************
NAME    
    appHandleClDmModeChangeEvent   
DESCRIPTION
    This function handles the CM_DM_MODE_CHANGE_EVENT message, this
    message is sent when the power mode changes.  This function checks if
    we have entered the last sniff interval mode and starts an idle timer.
    When the timer expires the HID connection will be released.
RETURNS
    void     
*/
static void appHandleClDmModeChangeEvent(appTaskData *theApp, CL_DM_MODE_CHANGE_EVENT_T *ev)
{
	MAIN_PRINT(("appHandleClDmModeChangeEvent\n"));    

    /* Cancel any idle timeout message */
    MessageCancelAll(&theApp->task, APP_INTERNAL_IDLE_TIMEOUT_IND);
    
    /* Check if we have entered the last sniff interval */
    if ((ev->mode == lp_sniff) && (ev->interval >= app_mouse_power_table[APP_POWER_TABLE_ENTRIES - 1].min_interval))
    {
        /* Get clock offset */        
        ConnectionReadClockOffset(&theApp->task, theApp->interrupt_sink);

        /* Send idle timeout message in 30 seconds */
        MessageSendLater(&theApp->task, APP_INTERNAL_IDLE_TIMEOUT_IND, 0, D_SEC(APP_IDLE_TIMEOUT));
    }
}

/*************************************************************************
NAME    
    appHandleClDmClockOffsetCfm   
DESCRIPTION
    This function handles the CM_DM_CLOCK_OFFSET_CFM message, this
    message in response to a COnnectionReadClockOffset function. This
    function calls ConnectionWriteCachedClockOffset to make the connection
    library cache the clock offset to improve the reconnection speed.
RETURNS
    void     
*/
static void appHandleClDmClockOffsetCfm(appTaskData *theApp, CL_DM_CLOCK_OFFSET_CFM_T *cfm)
{
	MAIN_PRINT(("appHandleClDmClockOffsetCfm, status %d, offset %d\n", cfm->status, cfm->clock_offset));    

    /* Check read was successful */
    if (cfm->status == hci_success)
    {
        /* Cache clock offset */
        ConnectionWriteCachedClockOffset(&theApp->host_bd_addr, cfm->clock_offset); 
    }
}

/*************************************************************************
NAME    
    appHandleHidInitConfirm   
DESCRIPTION
    Called when HID library has initialised, performs application
    initialisation, HID transform configuration and connection
    library configuration.
RETURNS
    void     
*/
static void appHandleHidInitConfirm(appTaskData *theApp, HID_INIT_CFM_T *cfm)
{
	MAIN_PRINT(("appHandleHidInitConfirm\n"));
	
	/* Check for HID initialised successfully */
	if (cfm->status == hid_init_success)
	{
    	/* Record HID library handle */
		theApp->hid_lib = cfm->hid_lib;
        
        /* Initialise application variables */
        theApp->trusted_device_index = 0;
        theApp->unplug_pending = FALSE;
        theApp->shutdown_pending = FALSE;

#ifdef HW_DEV_1409
		/* Configure HID source as 2030/2051 with 3 buttons and wheel */
		PanicFalse(SourceConfigureHidSensorAgilentAN2051(StreamHidSource(), &app_hidio_config));
		PanicFalse(SourceConfigure(StreamHidSource(), VM_SOURCE_HID_BUTTONS, 3));
		PanicFalse(SourceConfigure(StreamHidSource(), VM_SOURCE_HID_WHEEL, 2));
#endif

#ifdef HW_CASIRA_ADNS3030 
		/* Configure HID source as 3030/3040, no buttons or wheel */
		PanicFalse(SourceConfigureHidSensorAgilentADNS3030(StreamHidSource(), &app_hidio_config));
#endif

		/* Disable idle mode and sampling */
       	PanicFalse(SourceConfigure(StreamHidSource(), VM_SOURCE_HID_IDLE_MODE, 0));
        PanicFalse(SourceConfigure(StreamHidSource(), VM_SOURCE_HID_SAMPLE_RATE, 0));

        /* Send MESSAGE_MORE_DATA to our task */
        MessageSinkTask(StreamHidSink(), &theApp->task);
        
		/* Write class of device */
		ConnectionWriteClassOfDevice(HID_MAJOR_DEVICE_CLASS | HID_MINOR_MOUSE);
        
        /* Turn on security */
		ConnectionSmSetSecurityMode(&theApp->task, sec_mode2_service, hci_enc_mode_off);
		ConnectionSmRegisterIncomingService(protocol_l2cap, 0, secl_in_authentication);
        
        /* Response to HID DIAC */
        ConnectionWriteInquiryAccessCode(&theApp->task, app_hid_diac, 2);

        /* Allow SDP record browsing */
		ConnectionSmSetSdpSecurityIn(TRUE);
        
        /* Move to connecting cabled state */
        appSetState(theApp, appCabledConnecting);
    }
    else
    {
        /* HID failed, stop application */
        Panic();
    }
}

/*************************************************************************
NAME    
    appHandleHidConnectIndDiscoverable   
DESCRIPTION
    Handles incoming connection when device is discoverable.  Accepts
    connection and moves to connecting state.
RETURNS
    void     
*/
static void appHandleHidConnectIndDiscoverable(appTaskData *theApp, HID_CONNECT_IND_T *ind)
{
	MAIN_PRINT(("appHandleHidConnectIndDiscoverable\n"));
	
    /* Store HID connection instance */
    theApp->hid = ind->hid;

    /* Store host bluetooth address */
    theApp->host_bd_addr = ind->bd_addr;
    
	/* Accept connection */
	HidConnectResponse(theApp->hid, &theApp->task, TRUE, &app_keyboard_connection_config);
    
    /* Debug output */
    MAIN_PRINT(("Connecting to host, nap=%d, uap=%d, lap=%ld\n", ind->bd_addr.nap, ind->bd_addr.uap, ind->bd_addr.lap));

    /* Move to connecting discoverable state */
    appSetState(theApp, appDiscoverableConnecting);
}


/*************************************************************************
NAME    
    appHandleHidConnectIndReject   
DESCRIPTION
    Rejects any incoming HID connection, used when there is already an
    existing HID connection.
RETURNS
    void     
*/
static void appHandleHidConnectIndReject(appTaskData *app, HID_CONNECT_IND_T *ind)
{
	MAIN_PRINT(("appHandleHidConnectIndReject\n"));
	
	/* Reject connection */
    HidConnectResponse(ind->hid, &app->task, FALSE, NULL);
}

/*************************************************************************
NAME    
    appHandleHidConnectCfm   
DESCRIPTION
    Handles HID connection confirmation for a new device, if successful
    enters connected state and marks device as trusted otherwise just goes
    back to discoverable state.
RETURNS
    void     
*/
static void appHandleHidConnectCfmDiscoverable(appTaskData *theApp, HID_CONNECT_CFM_T *cfm)
{
	MAIN_PRINT(("appHandleHidConnectCfmDiscoverable, status=%d\n", cfm->status));
    
    /* Check if connect was successful */
    if (cfm->status == hid_connect_success)
    {
        /* Store HID connection instance */
        theApp->hid = cfm->hid;
        
        /* Save HID interrupt sink */
        theApp->interrupt_sink = cfm->interrupt_sink;
        
        /* Connecting successful, now set trust level */
        ConnectionSmSetTrustLevel(&theApp->auth_bd_addr, TRUE);

        /* Move to connected state */
        appSetState(theApp, appCabledConnected);
    }
    else
    {
        /* Move to discoverable state */
        appSetState(theApp, appDiscoverable);
    }
}

/*************************************************************************
NAME    
    appHandleHidConnectCfmCabled   
DESCRIPTION
    Handles HID connection confirmation.  If confirmation was successful
    enters connected state, otherwise attempts the next trusted device.
RETURNS
    void     
*/
static void appHandleHidConnectCfmCabled(appTaskData *theApp, HID_CONNECT_CFM_T *cfm)
{
	MAIN_PRINT(("appHandleHidConnectCfmCabled, status=%d\n", cfm->status));
    
    /* Check if connect was successful */
    if (cfm->status == hid_connect_success)
    {
        /* Store HID connection instance */
        theApp->hid = cfm->hid;
        
        /* Save HID interrupt sink */
        theApp->interrupt_sink = cfm->interrupt_sink;
        
        /* Move to connected state */
        appSetState(theApp, appCabledConnected);
    }
    else
    {
        /* Try next paired device */
        theApp->trusted_device_index++;
        
        /* Move to connecting state */
        appSetState(theApp, appCabledConnecting);
    }
}

/*************************************************************************
NAME    
    appHandleHidDisconnectInd   
DESCRIPTION
    Handles HID disconnection.  If disconnection due to link loss,
    attempts to re-establish link.
RETURNS
    void     
*/
static void appHandleHidDisconnectInd(appTaskData *theApp, HID_DISCONNECT_IND_T *ind)
{
	MAIN_PRINT(("appHandleHidDisconnectInd, status=%d\n", ind->status));
    
    /* Check disconnection reason */
    switch (ind->status)
    {
        case hid_disconnect_link_loss:
        {
            MAIN_PRINT(("Link loss, reconnecting\n"));
            
            /* Move to connecting local state */
            appSetState(theApp, appCabledConnecting);
        }
        break;
        
        case hid_disconnect_virtual_unplug:
        {
            MAIN_PRINT(("Virtual unplug\n"));
            
            /* Delete all trusted devices */
            ConnectionSmDeleteAllAuthDevices(APP_MOUSE_ATTR_PS_BASE);
            
            /* Move to the idle state */
            appSetState(theApp, appIdle);
        }
        break;
        
        default:
        {
            /* Move to the disconnected state */
            appSetState(theApp, appCabledDisconnected);
        }
        break;
    }        
}

/*************************************************************************
NAME    
    appHandleDataDispose
DESCRIPTION
    Handles data on interrupt channel passed down by firmware.  This
    function just free the data.
RETURNS
    void     
*/
static void appHandleDataDispose(appTaskData *theApp, MessageMoreData *msg)
{
    int size;
    Source source = msg->source;
       
    /* Loop, pulling packets from source */
    while ((size = SourceBoundary(source)) != 0)
    {
		/* Discard the packet */
		SourceDrop(source, size);
    } 
}
   
/*************************************************************************
NAME    
    app_handler   
DESCRIPTION
    Message handler for all messages in this application.
RETURNS
    void     
*/
static void app_handler(Task task, MessageId id, Message message)
{
	appTaskData *theApp = (appTaskData *)task;
	app_state state = appGetState(theApp);
	
	switch(id)
    {
        /* Connection Library Messages */       
		case CL_INIT_CFM:
        {
        	switch (state)
            {
            	case appInitialising:
                	appHandleClInitConfirm(theApp, (CL_INIT_CFM_T *)message);
                    break;
                default:
                	appHandleUnexpected(theApp, id);
                	break;
            }
        }
        break;
        
        case CL_SM_GET_INDEXED_ATTRIBUTE_CFM:
        {
            switch (state)
            {
                case appCabledConnecting:
                    appHandleClSmGetIndexedAttributeConfirm(theApp, (CL_SM_GET_INDEXED_ATTRIBUTE_CFM_T *)message);
                    break;
                default:
                    appHandleUnexpected(theApp, id);
                    break;
            }
        }
        break;
        
        case CL_SM_PIN_CODE_IND:
        {
            appHandleClSmPinCodeIndication(theApp, (CL_SM_PIN_CODE_IND_T *)message);
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -