Exemple #1
0
void StackEventHandler(uint32 event,void *eventParam)
{
    
    switch(event)
	{
        
		case CYBLE_EVT_STACK_ON:
            //Starting Advertisement as soon as Stack is ON
             apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
            if (apiResult == CYBLE_ERROR_OK)
            {
                printf ("Starting Advertisement\r\n");
            }
            else
            {
                printf ("Error Start Adv %d\r\n",apiResult);
            }
            break;
            
        case CYBLE_EVT_TIMEOUT:
         if( CYBLE_GAP_ADV_MODE_TO ==*(uint16*) eventParam)
            {
                printf ("Advertisement TimedOut\r\n");
                apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
                if (apiResult != CYBLE_ERROR_OK)
                {
                    printf ("\nRestarting  Advertisement\r\n");
                }
            }
		
		case CYBLE_EVT_GATT_DISCONNECT_IND:
			/* Red LED Glows when device is disconnected */
            RED_LED_ON ();
            break;
            
        case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:
            printf ("Disconnected \r\n");
            // Starting Advertisent again when there is disconnection
            apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
            if (apiResult != CYBLE_ERROR_OK)
            {
                printf ("\nRestarting  Advertisement\r\n");
            }
            break;
            
			
		case CYBLE_EVT_GAP_DEVICE_CONNECTED:
            printf ("\n\r Connection Established \r\n");
            //Blue LED glows when device is connected
            BLUE_LED_ON ();
            break;

		
		case CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP:
         // This Event is received when advertisement is started or stopped.
            if (CyBle_GetState() == CYBLE_STATE_ADVERTISING)
            {
                printf("Advertising...\r\n");
                //Green LED Indicates that Advertisement is going on.
                GREEN_LED_ON();
            }
            else
            {
                RED_LED_ON();
                printf ("Advertisement Stopped \r\n");
                if (AddRequest == 1)
                {
                    AddRequest = 0;
                    // Adding the Device to whitelist
                    apiResult = CyBle_GapAddDeviceToWhiteList(&whitelistdeviceaddress); 
                                
                    if(apiResult == CYBLE_ERROR_INVALID_PARAMETER)
					{
						printf ("Adding to Whitelist Failed. Invalid parameter \r\n");
                    }
                    else if   (apiResult ==CYBLE_ERROR_INVALID_OPERATION)
                    {
                        printf ("Invalid Operation \r\n");
                    }
                    else if (apiResult ==CYBLE_ERROR_INSUFFICIENT_RESOURCES)
                    {
                      printf ("Adding to Whitelist Failed. List already full \r\n");
                    }
                    else if (apiResult ==CYBLE_ERROR_DEVICE_ALREADY_EXISTS)
                    {
                        printf ("Device Already exists \r\n");
                    }
                    else if (apiResult == CYBLE_ERROR_OK)
                    {
                        //Copying the address to the BackUp Array
                        whitelistdeviceaddressBackup[Index] = whitelistdeviceaddress; 
                        Index++;
                        printf ("Device Added to WhiteList\r\n");
                        printf ("Press A to Add or R to remove a from Whitelist \r\n");
                    }
                    
                    // Restarting the advertisement
                    apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
                    if (apiResult == CYBLE_ERROR_OK)
                    {
                        printf ("Restarting Advertisement \r\n");
                        GREEN_LED_ON ();
                    }
                    else
                    {
                        printf ("Error Start Adv %d \r\n",apiResult);
                    }
                }
                if (DelRequest == 1)
                {
                    DelRequest = 0;
                    //Removing the Device from Whitelist
                    apiResult = CyBle_GapRemoveDeviceFromWhiteList 
                    (&whitelistdeviceaddressBackup[RemoveIndex]);
                    if (apiResult == CYBLE_ERROR_OK)
                    {
                        
                        uint8 j;
                        for (j = RemoveIndex; j<Index - 1; j++)
                        {
                            whitelistdeviceaddressBackup[j] = 
                            whitelistdeviceaddressBackup[j+1];
                        }
                        Index--;
                                                                                                                      
                        printf ("Device %d Removed from Whitelist\r\n",RemoveIndex + 1);
                        printf ("Press A to Add a Device or R to remove a device\r\n");
                    }
                    else if (apiResult == CYBLE_ERROR_NO_DEVICE_ENTITY)
                    {
                        printf ("No Such Device Exists. Press A to Add a Device or R to remove\r\n");
                    }
                    else 
                    {
                        printf ("Error: Operation cannot be performed");
                        printf ("Press A to Add a Device or R to remove\r\n");
                    }
                    //re-starting Advertisement
                    apiResult = CyBle_GappStartAdvertisement (CYBLE_ADVERTISING_FAST);
                    if (apiResult == CYBLE_ERROR_OK)
                    {
                        printf ("Restarting Advertisement\r\n");
                    }
                    else
                    {
                        printf ("Restarting Advertisement Failed\r\n");
                    }
                }
            }
            
		default:
			break;
	}
}
Exemple #2
0
/*******************************************************************************
* Function Name: BondingImplementation()
********************************************************************************
* Summary:
* Implements bonding of peer BLE device information.
*
* Parameters:
* None
*
* Return:
* None
*
* Theory:
* The function stores the peer BLE device information to flash (called bonding)
* when the device is ready. When the user wants to clear the bond information, 
* it is cleared in this function.
*
*******************************************************************************/
static void BondingImplementation(void)
{
    uint8 command;
    CYBLE_GAP_BD_ADDR_T clearAllDevices = {{0,0,0,0,0,0},0};
    
    switch(authState)
    {
        case AUTHENTICATION_COMPLETE_BONDING_REQD:
            /* Store bonding data of the current connection */
            while(CYBLE_ERROR_OK != CyBle_StoreBondingData(1));
            
            authState = AUTHENTICATION_BONDING_COMPLETE;
            UART_UartPutString("Bonding complete. ");
            break;
            
            
        case AUTHENTICATION_BONDING_COMPLETE:
            /* See if the user pressed 'R' button to clear the bond list. */
            command = UART_UartGetChar();
            
            if(command != 0u)
            {
                if((command == 'r') || (command == 'R'))
                {
                    /* User wants the bond to be removed */
                    UART_UartPutString("\n\rClear the bond list. ");
                    if(CyBle_GetState() == CYBLE_STATE_CONNECTED)
                    {
                        /* Disconnect */
                        authState = AUTHENTICATION_BONDING_REMOVE_WAITING_EVENT;
                        CyBle_GapDisconnect(cyBle_connHandle.bdHandle);
                    }
                    else if(CyBle_GetState() == CYBLE_STATE_ADVERTISING)
                    {
                        /* Stop advertisement */
                        authState = AUTHENTICATION_BONDING_REMOVE_WAITING_EVENT;
                        CyBle_GappStopAdvertisement();
                    }
                    else
                    {
                        authState = AUTHENTICATION_BONDING_REMOVE_GO_AHEAD;
                    }
                }
            }
            break;
            
            
        case AUTHENTICATION_BONDING_REMOVE_GO_AHEAD:
            /* Remove all bonded devices in the list */
            CyBle_GapRemoveDeviceFromWhiteList(&clearAllDevices);
            
            while(CYBLE_ERROR_OK != CyBle_StoreBondingData(1));
            UART_UartPutString("Cleared the list of bonded devices. \n\n\r");
                
            authState = AUTHENTICATION_NOT_CONNECTED;
            
            /* Start advertisement again */
            UART_UartPutString("Advertising. ");
            CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
            break;
            
            
        default:
            break;
    }
}