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

📄 app_dongle.c

📁 最新HID DONGLE软件,给大家参考
💻 C
📖 第 1 页 / 共 2 页
字号:
    {
    	int dev;
        
		MAIN_PRINT(("Found device, class = %lx\n", res->dev_class));
    
    	/* Look for empty device */
		for (dev = 0; dev < APP_DONGLE_NUM_DEVICES; dev++)
        {   
            appDeviceTaskData *theDevice = &theApp->dev_table[dev];
            
        	/* Check if we have a empty slot for the class of device */
        	if (appDeviceIsEmpty(theDevice, res->dev_class))
            {
            	/* Try to connect to device */
            	appDeviceConnectRequest(theDevice, &res->bd_addr, TRUE);
                break;
            }
        }
    }
    else if (res->status == inquiry_status_ready)
    {
    	/* Inquiry finished */
        MAIN_PRINT(("Inquiry finished\n"));
        
        /* Move to active state */
        appSetState(theApp, appDongleActive);
    }
}

/*************************************************************************
NAME  
    appHandleClSmPinCodeInd  
DESCRIPTION
    This function is called to handle pin code indication for a device.
RETURNS
    void
*/
static void appHandleClSmPinCodeInd(appDongleTaskData *theApp, CL_SM_PIN_CODE_IND_T *ind)
{
    int dev;
    
 	MAIN_PRINT(("appHandleHidConnectInd\n"));
     
   	/* Look for matching device */
	for (dev = 0; dev < APP_DONGLE_NUM_DEVICES; dev++)
    {
        appDeviceTaskData *theDevice = &theApp->dev_table[dev];        
        if (appDeviceIsBdaddr(theDevice, &ind->bd_addr))
        {
            /* TODO: Check if device is in initial connecting state */

            /* Send pin code response */
            ConnectionSmPinCodeResponse(&ind->bd_addr, strlen(theDevice->pin), (uint8 *)theDevice->pin); 
            return;
        } 
    }
   
    /* Send empty pin code response */
    ConnectionSmPinCodeResponse(&ind->bd_addr, 0, (uint8 *)""); 
}

/*************************************************************************
NAME    
    appHandleInternalInquiry
DESCRIPTION
    This function is called to place the dongle into inquiry mode.
RETURNS
    void
*/
static void appHandleInternalInquiry(appDongleTaskData *theApp)
{   
    /* Move to inquiry state if we have an empty device */
    if (appIsInquiryRequired(theApp, FALSE))
        appSetState(theApp, appDongleInquiry);
}

/*************************************************************************
NAME   
    appHandleInternalClear 
DESCRIPTION
    This function is called to clear all paired devices.
RETURNS
    void
*/
static void appHandleInternalClear(appDongleTaskData *theApp)
{
    int dev;
    bool need_to_delete = FALSE;
    
	MAIN_PRINT(("appHandleInternalClear\n"));
    
    /* Delete all devices */
	for (dev = 0; dev < APP_DONGLE_NUM_DEVICES; dev++)
    {   
        appDeviceTaskData *theDevice = &theApp->dev_table[dev];
        
        /* Attempt to delete device */
        if (!appDeviceIsEmpty(theDevice, 0xFFFFFFFF))
        {
            appDeviceUnplugRequest(theDevice);
            need_to_delete = TRUE;
        }
    }
    
    /* Move to clearing */
    appSetState(theApp, need_to_delete ? appDongleClearing : appDongleInquiry);
}

/*************************************************************************
NAME    
    appHandleDevUnplugInd
DESCRIPTION
    This function is called when a device has been unplugged.
RETURNS
    void
*/
static void appHandleDevUnplugInd(appDongleTaskData *theApp)
{
    int dev;
    
	MAIN_PRINT(("appHandleDevUnplugInd\n"));
    
    /* Check if all devices are now deleted */
	for (dev = 0; dev < APP_DONGLE_NUM_DEVICES; dev++)
    {   
        appDeviceTaskData *theDevice = &theApp->dev_table[dev];
        
        /* If device is not empty then exit function now */
        if (!appDeviceIsEmpty(theDevice, 0xFFFFFFFF))
            return;
    }
    
    /* Check if we need to enable/disable page scan */
    appUpdatePageScanEnable(theApp);
    
    /* Move to inquiry */
    appSetState(theApp, appDongleInquiry);
}

/*************************************************************************
NAME    
    appHandler
DESCRIPTION
    This function is called to handle all messages received by the main
    task.
RETURNS
    void
*/
static void appHandler(Task task, MessageId id, Message message)
{
	appDongleTaskData *theApp = (appDongleTaskData *)task;
    appDongleState state = appGetState(theApp);
    
	/* Check the message id */
	switch (id)
    {
	    case CL_INIT_CFM:
        	switch (state)
            {
            	case appDongleInitialising:
		        	appHandleClInitCfm(theApp, (CL_INIT_CFM_T *)message);
                    break;
                default:
                	appHandleUnexpected(theApp, id);
                    break;
            }
            break;
            
		case CL_DM_INQUIRE_RESULT:
        	switch (state)
            {
            	case appDongleInquiry:
                	appHandleClDmInquireResult(theApp, (CL_DM_INQUIRE_RESULT_T *)message);
                    break;
                default:
                	appHandleUnexpected(theApp, id);
                    break;
             }
             break;
             
        case CL_SM_PIN_CODE_IND:
            appHandleClSmPinCodeInd(theApp, (CL_SM_PIN_CODE_IND_T *)message);
            break;
            
        case CL_DM_ACL_OPENED_IND:
            LedUpdate(&theApp->led, 900, 100, 1);
            LedSetPriority(&theApp->led, 1);   
            break;

        case CL_DM_ACL_CLOSED_IND:
            LedSetPriority(&theApp->led, 0);   
            break;
            
        case HID_INIT_CFM:
        	switch (state)
            {
            	case appDongleInitialising:
					appHandleHidInitCfm(theApp, (HID_INIT_CFM_T *)message);
                    break;
                default:
                	appHandleUnexpected(theApp, id);
                    break;
            }
            break;
            
		case HID_CONNECT_IND:
        	switch (state)
            {
            	case appDongleInquiry:
                case appDongleActive:
                	appHandleHidConnectInd(theApp, (HID_CONNECT_IND_T *)message);
                    break;	
                case appDongleClearing:
                    appRejectHidConnectInd(theApp, (HID_CONNECT_IND_T *)message);
                    break;
				default:
        			appHandleUnexpected(theApp, id);
		            break;
            }
            break;
            
        case APP_DEVICE_CONNECT_IND:
        case APP_DEVICE_DISCONNECT_IND:
            appUpdatePageScanEnable(theApp);
            break;
            
        case APP_DEVICE_UNPLUG_IND:
            switch (state)
            {
                case appDongleClearing:
                    appHandleDevUnplugInd(theApp);
                    break;
                case appDongleInquiry:
                case appDongleActive:
                    break;
                default:
                    appHandleUnexpected(theApp, id);
                    break;
            }
            break;
            
        case APP_DONGLE_INTERNAL_INQUIRY:
            switch (state)
            {
                case appDongleActive:
                    appHandleInternalInquiry(theApp);
                    break;
                case appDongleInquiry:
                case appDongleClearing:
                    break;
                default:
                    appHandleUnexpected(theApp, id);
                    break;
            }
            break;
            
        case APP_DONGLE_INTERNAL_CLEAR:
            switch (state)
            {
                case appDongleInquiry:
                case appDongleActive:
                    appHandleInternalClear(theApp);
                    break;
                case appDongleClearing:
                    break;
                default:
                    appHandleUnexpected(theApp, id);
                    break;
            }
            break;
            
		default:
        	appHandleUnexpected(theApp, id);
            break;
    }
}

/*************************************************************************
NAME    
    _init
DESCRIPTION
    Extra initialisation for time critical functionality.
RETURNS
*/
extern void _init(void);
void _init(void)
{
    /* Initialise USB interface */
    UsbInitInterfaceTimeCritical();
}

/*************************************************************************
NAME    
    main
DESCRIPTION
    Main entry point.
RETURNS
    int - Should never return.
*/
int main(void)
{
    static  appDongleTaskData theApp;
    
    theApp.task.handler = appHandler;
   	theApp.state = appDongleInitialising;
   
    /* Initialise buttons */
    pioInit(&theApp.pio_state, &theApp.task);
    
    /* Initialise Led */
    LedInit(&theApp.led, &theApp);
    LedUpdate(&theApp.led, 0, -1, 1);
    LedSetPriority(&theApp.led, 1);   
    
    /* Initialise devices */
    appDeviceInit(&theApp.dev_table[0], 0, HID_MAJOR_DEVICE_CLASS | HID_MINOR_KEYBOARD, &theApp, "0000");
    appDeviceInit(&theApp.dev_table[1], 1, HID_MAJOR_DEVICE_CLASS | HID_MINOR_MOUSE,    &theApp, "0000");
    
    /* Initialise the USB devices */
    UsbInitKeyboardDevice(&theApp.dev_table[0]);
    UsbInitMouseDevice(&theApp.dev_table[1]);
    
    /* Init the Connection Manager */
    ConnectionInit(&theApp.task);

	/* Start the message scheduler loop */
    MessageLoop();
    
    /* We should never get here */
    return 0;
}

⌨️ 快捷键说明

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