Exemplo n.º 1
0
int main()
{
  
    /* Initializing all the Flags and Indexes to 0 */
    ALL_LED_OFF ();
    Count = 0;
    Index = 0;
    AddRequest = 0;
    DelRequest = 0;

    CyGlobalIntEnable;  /* Comment this line to disable global interrupts. */
    
    /* Start BLE component and register Event handler function */	
    CyBle_Start(StackEventHandler);
	

    /* Start UART Component which is used for receiving inputs and Debugging */
    UART_Start();

	printf("BLE WhiteList Example \r\n");
    printf("Press A to add a Device to WhiteList. R to remove the Device from Whitelist \r\n");

    /* Continuous loop scans for inputs from UART Terminal and accordingly 
    handles Addition to and Removal from Whitelist. Also processes
    BLE events */
    
    for(;;)
    {
        //Checks the internal task queue in the BLE Stack
        CyBle_ProcessEvents();
        
        if(UART_SpiUartGetRxBufferSize())
		{
		   	UartRxDataSim = UART_UartGetChar();
            if (UartRxDataSim == 'A' || UartRxDataSim == 'a')  // The user has to Enter D for disconnection 
            {
                printf ("Enter the Address of the Device. Press Z to Go Back \r\n");
                for (;;)
                {
                    if (Count ==12)
                    {
                        //If the user had entered the full address, stop advertisement
                        //for addition process
                        CyBle_GappStopAdvertisement ();
                        /*Once We stop advertisement, the 
                        CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP event is invoked.
                         After this, the API for adding the device to whitelist is invoked
                        in the StackEventHandler*/
                        RED_LED_ON ();
                        AddRequest = 1;
                        printf ("\r\n");
                        printf ("Address is 0x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x \r\n",
                                whitelistdeviceaddress.bdAddr[5],
                                whitelistdeviceaddress.bdAddr[4],
                                whitelistdeviceaddress.bdAddr[3],
                                whitelistdeviceaddress.bdAddr[2],
                                whitelistdeviceaddress.bdAddr[1],
                                whitelistdeviceaddress.bdAddr[0]);
                        printf ("Attempting to Add to whitelist \r \n");
                        Count = 0; 
                        break;
                    }
                    
                    if(UART_SpiUartGetRxBufferSize())
                    {
                        UartRxDataSim = UART_UartGetChar();
                        if (UartRxDataSim == 'Z' || UartRxDataSim == 'z')  
                        {
                            Count = 0;
                            printf("Press A to add a Device to WhiteList \r\n");
                            break;
                        }
                       
                        else
                        {
                            if ((UartRxDataSim >= '0') && (UartRxDataSim <= '9' ))
                            {
                                AddrNibble = UartRxDataSim - '0';
                                UART_UartPutChar (UartRxDataSim);
                            }
                            else if ((UartRxDataSim >= 'A') && (UartRxDataSim <= 'F' ))
                            {
                                AddrNibble = UartRxDataSim - 'A' + 0xA;
                                UART_UartPutChar (UartRxDataSim);
                            }
                             else if ((UartRxDataSim >= 'a') && (UartRxDataSim <= 'f' ))
                            {
                                AddrNibble = UartRxDataSim - 'a' + 0xA;
                                UART_UartPutChar (UartRxDataSim);
                            }
                            else 
                            {
                                printf ("\nplease Enter a Valid Address. Press A to Enter a New Address. R ro remove the Device\r\n");
                                Count = 0;
                                break;
                            }
                            
                            //Receiving the addresss Nibble by Nibble
                            whitelistdeviceaddress.bdAddr[5 - (Count/2)] =
                            (whitelistdeviceaddress.bdAddr[5 - (Count/2)]<<4)|AddrNibble;
                            Count ++;
                        }
                    }
                }
            }
            
            else if (UartRxDataSim == 'R' || UartRxDataSim == 'r')
            {
                if (Index == 0)
                {
                    printf ("No Devices in WhiteList. press A to Add \r\n");
                   
                }
                else
                {
                    printf (" The List of Devices are given below \4\n");
                    uint8 i = 0;
                    // Retrieving the list of added devices for user to choose
                    for (i = 0; i< Index; i++)
                    {
                        printf ("Device %d 0x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x \r\n",i + 1,
                            whitelistdeviceaddressBackup[i].bdAddr[5],
                            whitelistdeviceaddressBackup[i].bdAddr[4],
                            whitelistdeviceaddressBackup[i].bdAddr[3],
                            whitelistdeviceaddressBackup[i].bdAddr[2],
                            whitelistdeviceaddressBackup[i].bdAddr[1],
                            whitelistdeviceaddressBackup[i].bdAddr[0]);
                    }
                    printf ("Enter the Index of the device to be removed. Press Z to go back \r\n");
                    
                    for (;;)
                    {
                        if(UART_SpiUartGetRxBufferSize())
                        {
                            UartRxDataSim = UART_UartGetChar();
                            if (UartRxDataSim == 'Z' || UartRxDataSim == 'z')  
                            {
                                printf("Press A to add a Device to WhiteList. R to remove \r\n");
                                break;
                            }
                            else if (UartRxDataSim >= '1' || UartRxDataSim <= '0' + Index)
                            {
                                RemoveIndex = UartRxDataSim - '1';
                                if(RemoveIndex < Index)
                                {
                                    CyBle_GappStopAdvertisement ();
                                /*Once We stop advertisement, the 
                                CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP event is invoked.
                                After this, the API for removing the device from whitelist 
                                is invoked in the StackEventHandler*/
                                    DelRequest = 1;
                                    break;
                                }
                                else
                                {
                                    printf("There is no device with that number.\r\n");
                                }
                            }
                            else 
                            {
                                printf ("Invaid Index. Press A to Add and R to remove a Device");
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
Exemplo n.º 2
0
/*******************************************************************************
* Function Name: SwitchRole
********************************************************************************
* Summary:
*        This function switches the role between Central and Peripheral. If device 
* is connected while switching role, then it is first disconnected.
*
* Parameters:
*  void
*
* Return:
*  void
*
*******************************************************************************/
void SwitchRole(void)
{
	CYBLE_API_RESULT_T  apiResult;
	
	/* if the switch role flag is set... */
	if(switch_Role == TRUE)
	{	
		/* Process pending BLE events */
		CyBle_ProcessEvents();
		
		/* If there is an existing connection, then disconnect before switching
		* role. */
		if((cyBle_connHandle.bdHandle != 0))
		{
			/* Disconnect the device and process the event */
			CyBle_GapDisconnect(cyBle_connHandle.bdHandle);
			CyBle_ProcessEvents();
			#ifdef DEBUG_ENABLED
			UART_UartPutString("Peripheral closed connection ");
			SendBLEStatetoUART(CyBle_GetState());
			UART_UartPutCRLF(' ');
			#endif
		}		
		
		switch(ble_gap_state)
		{
			case BLE_PERIPHERAL:
				/* If the current role is Peripheral and system is advertising,
				* then stop advertisement before switching role */
				if(CyBle_GetState() == CYBLE_STATE_ADVERTISING)
				{
					CyBle_GappStopAdvertisement();
					CyBle_ProcessEvents();

					#ifdef DEBUG_ENABLED
						UART_UartPutString("Peripheral Advertisment Stopped ");
						SendBLEStatetoUART(CyBle_GetState());
						UART_UartPutCRLF(' ');
					#endif
				}
				
				if(CyBle_GetState() == CYBLE_STATE_DISCONNECTED)
				{
					/* Switch BLE role by starting scan. This way, the system is set
					* to Central role */
					apiResult = CyBle_GapcStartScan(CYBLE_SCANNING_FAST);
					
					if(CYBLE_ERROR_OK == apiResult)
					{
						#ifdef DEBUG_ENABLED
						UART_UartPutString("Start Scan API called ");
						SendBLEStatetoUART(CyBle_GetState());
						UART_UartPutCRLF(' ');
						#endif
						
						/* Record the time at which Central role was started. This will be 
						* used for timeout and switching to Peripheral operation*/
						centralStartedTime = WatchDog_CurrentCount();
						
						/* Update the current BLE role to Central */
						ble_gap_state = BLE_CENTRAL;
						
						/* Reset the switch role flag*/
						switch_Role = FALSE;
					}
					else
					{
						/* If scanning did not start, maintain the current role and retry later */
						ble_gap_state = BLE_PERIPHERAL;
						
						#ifdef DEBUG_ENABLED
						UART_UartPutString("Start Scan API failed ");
						SendBLEStatetoUART(CyBle_GetState());
						UART_UartPutCRLF(' ');
						#endif
					}
				}
				
				/* Process Pending BLE Events */
				CyBle_ProcessEvents();
				
			break;
			
			case BLE_CENTRAL:
				/* If the current role is Central and system is scanning,
				* then stop scanning before switching role */
				if(CyBle_GetState() == CYBLE_STATE_SCANNING)
				{
					CyBle_GapcStopScan();
					CyBle_ProcessEvents();
					
					#ifdef DEBUG_ENABLED
						UART_UartPutString("Central Scan stopped ");
						UART_UartPutCRLF(' ');
					#endif
				}
				
				if(CyBle_GetState() == CYBLE_STATE_DISCONNECTED)
				{
					#ifdef ENABLE_ADV_DATA_COUNTER
					/* Increment data counter */
					new_advData.advData[new_advData.advDataLen - 1] = dataADVCounter;
					
					cyBle_discoveryModeInfo.advData = &new_advData;
					
					#ifdef DEBUG_ENABLED
						UART_UartPutString("Updated ADV data = ");
						PrintNum(dataADVCounter);
						UART_UartPutCRLF(' ');
					#endif
					#endif
					
					/* Switch BLE role by starting advertisement. This way, the system is 
					* set to Peripheral role */
					apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
					
					if(apiResult == CYBLE_ERROR_OK)
					{
						/* If advertisement started successfully, set the BLE state and
						* reset the switch role flag*/
						ble_gap_state = BLE_PERIPHERAL;
						clientConnectToDevice = FALSE;	
						switch_Role = FALSE;
						
						#ifdef DEBUG_ENABLED
						UART_UartPutString("Peripheral Advertisment called ");
						SendBLEStatetoUART(CyBle_GetState());
						UART_UartPutCRLF(' ');
						#endif
					}
					else
					{
						/* If advertisement did not start, maintain the current role and retry later */
						ble_gap_state = BLE_CENTRAL;
						
						#ifdef DEBUG_ENABLED
						UART_UartPutString("Start Peripheral Advertisment Failed ");
						SendBLEStatetoUART(CyBle_GetState());
						UART_UartPutCRLF(' ');
						#endif					
					}
				}
				
				/* Process Pending BLE Events */
				CyBle_ProcessEvents();
			break;
			
			default:
			
			break;
			
		}
	}
}
Exemplo n.º 3
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;
    }
}