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

📄 hid_keyboard_auth.c

📁 根據bluelab3.5.2實例hid-keyboard做出應用
💻 C
字号:
/****************************************************************************
Copyright (C) Cambridge Silicon Radio Ltd. 2005-2006
Part of BlueLab 3.5.2-release

FILE NAME
    hid_keyboard_auth.c        
DESCRIPTION
    This file contains the authentication functions.
NOTES

*/

/****************************************************************************
    Header files
*/
#include "hid_keyboard.h"
#include "hid_keyboard_sm.h"
#include "hid_keyboard_auth.h"
#include "hid_keyboard_scan.h"

/*************************************************************************
NAME    
    appIsPinCodeEntry
DESCRIPTION
    Returns boolean indicating if pin code entry is currently in progress.
RETURNS
    bool - TRUE if pin code is being entered, FALSE otherwise.
*/
bool appIsPinCodeEntry(appTaskData *theApp)
{
    return theApp->auth_state == appAuthPinCode;
}

/*************************************************************************
NAME    
    appIsAuthenticated
DESCRIPTION
    Returns boolean indicating if device has completed authentication.
RETURNS
    bool - TRUE if authentication successful, FALSE otherwise.
*/
bool appIsAuthenticated(appTaskData *theApp)
{
    return theApp->auth_state == appAuthTrusted;
}

/*************************************************************************
NAME    
    appStartPinCodeEntry
DESCRIPTION
    This is called when authenticating has started, normally when a pin
    code indication is received.
RETURNS
    void     
*/
void appStartPinCodeEntry(appTaskData *theApp)
{
    MAIN_PRINT(("appStartPinCodeEntry\n"));
    
    /* Set pin code state */
    theApp->auth_state = appAuthPinCode;

    /* Initialise pin code entry */
    HidPinInit(&theApp->hid_pin);
    
    /* Check if we need to disconnect HID source */
    if (appGetState(theApp) == appCabledConnected)
    {
        /* Disconnect HID stream, so we get HID reports sent to task */
       	StreamDisconnect(StreamHidSource(), theApp->interrupt_sink);
    }

    /* Flash status LED */
    appLedUpdate(&theApp->led, 250, 250, 1);
    appLedSetPriority(&theApp->led, 1);

    /* Cancel any timeout message */
    MessageCancelAll(&theApp->task, APP_INTERNAL_PIN_CODE_TIMEOUT_IND);
    MessageCancelAll(&theApp->task, APP_INTERNAL_DISCOVERABLE_TIMEOUT_IND);
    
    /* Start pin code timeout */
    MessageSendLater(&theApp->task, APP_INTERNAL_PIN_CODE_TIMEOUT_IND, 0, D_SEC(APP_PIN_CODE_TIMEOUT));    

	/* Update inquiry scan and page scan */
    appUpdateScanEnable(theApp);
}

/*************************************************************************
NAME    
    appStopPinCodeEntry
DESCRIPTION
    This function stops pin code entry.
RETURNS
    void     
*/
void appStopPinCodeEntry(appTaskData *theApp, bool success)
{
    MAIN_PRINT(("appStopPinCodeEntry\n"));
    
    /* Set authenticating state */
    theApp->auth_state = success ? appAuthTrusted : appAuthIdle;

    /* Check if we need to reconnect HID source */
    if (appGetState(theApp) == appCabledConnected)
    {
        /* Connect HID transform in interrupt sink */
        StreamConnect(StreamHidSource(), theApp->interrupt_sink);
    }

    /* Stop flashing LED */
    appLedSetPriority(&theApp->led, 0);

    /* Cancel timeout message */
    MessageCancelAll(&theApp->task, APP_INTERNAL_PIN_CODE_TIMEOUT_IND);    

    /* Start discovery timeout if in discoverable state */
   	if ((appGetState(theApp) == appDiscoverable) || (appGetState(theApp) == appDiscoverableConnecting))		
        MessageSendLater(&theApp->task, APP_PAIRMODE_LED_UPDATE, 0, D_SEC(APP_DISCOVERABLE_TIMEOUT));    

	/* Update inquiry scan and page scan */
    appUpdateScanEnable(theApp);
}

/*************************************************************************
NAME    
    appHandleDataPinCode
DESCRIPTION
    Handles data on interrupt channel passed down by firmware.  This
    function attempts to build up a pin code.
RETURNS
    void     
*/
void appHandleDataPinCode(appTaskData *theApp, Source source)
{
    int size;
   
    /* Loop, pulling packets from source */
    while ((size = SourceBoundary(source)) != 0)
    {
		const uint8 *ptr = SourceMap(source);
        hid_pin_status status;
              
        /* Attempt to handle input report */
        status = HidPinCodeHandleReport(&theApp->hid_pin, ptr, size);
        switch (status)
		{
			case hid_pin_complete:
	        {
    	        MAIN_PRINT(("Pin code entered\n"));
                
        	    /* We have complete pin code, send response */
            	ConnectionSmPinCodeResponse(&theApp->auth_bd_addr, HidPinCodeLength(&theApp->hid_pin), HidPinCodeData(&theApp->hid_pin)); 
        	}
			break;

			case hid_pin_add:
			{
				if (appGetState(theApp) == appDiscoverableConnected && theApp->keyboard_protocol == hid_protocol_report)
				{
					const uint8 data[2] = {0xFF, 0x00};

					/* Send input report */
					HidInterruptReport(theApp->hid, hid_report_input, 2, data);
				}
			}
			break;
			
			case hid_pin_delete:
			{
				if (appGetState(theApp) == appDiscoverableConnected && theApp->keyboard_protocol == hid_protocol_report)
				{
					const uint8 data[2] = {0xFF, 0x01};

					/* Send input report */
					HidInterruptReport(theApp->hid, hid_report_input, 2, data);

					/* Check if pin code cleared */
					if (HidPinCodeLength(&theApp->hid_pin) == 0)
					{
						const uint8 data[2] = {0xFF, 0x02};

						/* Send input report */
						HidInterruptReport(theApp->hid, hid_report_input, 2, data);
					}
				}
			}
			break;
		case hid_pin_error:
			MAIN_PRINT(("HidPinCodeHandleReport Return pin read error\n"));
			if(0x20&&0x23)
				MAIN_PRINT(("0x20 && 0x23 ==TRUE  \n"));
			else
				MAIN_PRINT(("0x20 && 0x23 ==FALSE  \n"));
			default:
				break;
		}
        
		/* Discard the packet */
		SourceDrop(source, size);
    } 
}

⌨️ 快捷键说明

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