This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2540 BLE 1.4 RSSI Update

Hi,

Would like to understand that if the BLE1.4 stack is still with the issue of not having the RSSI update while in IDLE mode (no data packet transmission). Currently based on the test, there is no constant RSSI update if there is no data packet tranmission. Hence, need to confirm if this is still the case.

Thanks.

Regards,

RVG

  • Hi RVG,

    Yes, that's correct. 

    Best Regards

    Joakim

  • Hi Joakim,

    Attach here with the test result’s print screen and the source code.

     

    1. To enable monitoring RSSI,

    Add the below code simpleperipheral when peripheral device is connected

                                    uint16 rssi_read_rate = 1000;

                                    GAPRole_SetParameter(GAPROLE_RSSI_READ_RATE, sizeof(uint16), &rssi_read_rate); /* In ms */

     

    1. In peripheral.c of stack 1.2 and 1.4,  Add the highlighted code in green for debug purpose. (.jpg)

     

    Highlight in green is the changes made in peripheral.c for debug purpose.

    Highlight in red indicate the result

    You will see that 1.2 stack manage to calculate the RSSI but 1.4 cannot. Any advise on this?

    Thanks.

     

    Regards,
    RVG

     

    /**************************************************************************************************
      Filename:       peripheral.c
      Revised:        $Date: 2013-09-23 08:15:44 -0700 (Mon, 23 Sep 2013) $
      Revision:       $Revision: 35416 $
    
      Description:    GAP Peripheral Role
    
    
      Copyright 2009 - 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "bcomdef.h"
    #include "OSAL.h"
    #include "hci_tl.h"
    #include "l2cap.h"
    #include "gap.h"
    #include "linkdb.h"
    #include "att.h"
    #include "gatt.h"
    #include "osal_snv.h"
    
    #include "peripheral.h"
    #include "gapbondmgr.h"
    
    /*********************************************************************
     * MACROS
     */
    
    /*********************************************************************
     * CONSTANTS
     */
    // Profile Events
    #define START_ADVERTISING_EVT         0x0001  // Start Advertising
    #define RSSI_READ_EVT                 0x0002  // Read RSSI
    #define START_CONN_UPDATE_EVT         0x0004  // Start Connection Update Procedure
    #define CONN_PARAM_TIMEOUT_EVT        0x0008  // Connection Parameters Update Timeout
    
    #define DEFAULT_ADVERT_OFF_TIME       30000   // 30 seconds
    
    #define RSSI_NOT_AVAILABLE            127
    
    #define DEFAULT_MIN_CONN_INTERVAL     0x0006  // 100 milliseconds
    #define DEFAULT_MAX_CONN_INTERVAL     0x0C80  // 4 seconds
    
    #define MIN_CONN_INTERVAL             0x0006
    #define MAX_CONN_INTERVAL             0x0C80
    
    #define DEFAULT_TIMEOUT_MULTIPLIER    1000
    
    #define CONN_INTERVAL_MULTIPLIER      6
    
    #define MIN_SLAVE_LATENCY             0
    #define MAX_SLAVE_LATENCY             500
    
    #define MIN_TIMEOUT_MULTIPLIER        0x000a
    #define MAX_TIMEOUT_MULTIPLIER        0x0c80
    
    #define MAX_TIMEOUT_VALUE             0xFFFF
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    /*********************************************************************
     * EXTERNAL VARIABLES
     */
    
    /*********************************************************************
     * EXTERNAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    static uint8 gapRole_TaskID;   // Task ID for internal task/event processing
    
    static gaprole_States_t gapRole_state;
    
    /*********************************************************************
     * Profile Parameters - reference GAPROLE_PROFILE_PARAMETERS for
     * descriptions
     */
    
    static uint8  gapRole_profileRole;
    static uint8  gapRole_IRK[KEYLEN];
    static uint8  gapRole_SRK[KEYLEN];
    static uint32 gapRole_signCounter;
    static uint8  gapRole_bdAddr[B_ADDR_LEN];
    static uint8  gapRole_AdvEnabled = TRUE;
    static uint16 gapRole_AdvertOffTime = DEFAULT_ADVERT_OFF_TIME;
    static uint8  gapRole_AdvertDataLen = 3;
    static uint8  gapRole_AdvertData[B_MAX_ADV_LEN] =
    {
      0x02,   // length of this data
      GAP_ADTYPE_FLAGS,   // AD Type = Flags
      // Limited Discoverable & BR/EDR not supported
      (GAP_ADTYPE_FLAGS_GENERAL | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED),
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    };
    static uint8  gapRole_ScanRspDataLen = 0;
    static uint8  gapRole_ScanRspData[B_MAX_ADV_LEN] = {0};
    static uint8  gapRole_AdvEventType;
    static uint8  gapRole_AdvDirectType;
    static uint8  gapRole_AdvDirectAddr[B_ADDR_LEN] = {0};
    static uint8  gapRole_AdvChanMap;
    static uint8  gapRole_AdvFilterPolicy;
    
    static uint16 gapRole_ConnectionHandle = INVALID_CONNHANDLE;
    static uint16 gapRole_RSSIReadRate = 0;
    
    static uint8  gapRole_ConnectedDevAddr[B_ADDR_LEN] = {0};
    
    static uint8  gapRole_ParamUpdateEnable = FALSE;
    static uint16 gapRole_MinConnInterval = DEFAULT_MIN_CONN_INTERVAL;
    static uint16 gapRole_MaxConnInterval = DEFAULT_MAX_CONN_INTERVAL;
    static uint16 gapRole_SlaveLatency = MIN_SLAVE_LATENCY;
    static uint16 gapRole_TimeoutMultiplier = DEFAULT_TIMEOUT_MULTIPLIER;
    
    static uint16 gapRole_ConnInterval = 0;
    static uint16 gapRole_ConnSlaveLatency = 0;
    static uint16 gapRole_ConnTimeout = 0;
    
    static uint8 paramUpdateNoSuccessOption = GAPROLE_NO_ACTION;
    
    // Application callbacks
    static gapRolesCBs_t *pGapRoles_AppCGs = NULL;
    static gapRolesParamUpdateCB_t *pGapRoles_ParamUpdateCB = NULL;
    
    /*********************************************************************
     * Profile Attributes - variables
     */
    
    /*********************************************************************
     * Profile Attributes - Table
     */
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void gapRole_ProcessOSALMsg( osal_event_hdr_t *pMsg );
    static void gapRole_ProcessGAPMsg( gapEventHdr_t *pMsg );
    static void gapRole_SetupGAP( void );
    static void gapRole_HandleParamUpdateNoSuccess( void );
    static void gapRole_startConnUpdate( uint8 handleFailure );
    
    /*********************************************************************
     * NETWORK LAYER CALLBACKS
     */
    
    /*********************************************************************
     * PUBLIC FUNCTIONS
     */
    
    /*********************************************************************
     * @brief   Set a GAP Role parameter.
     *
     * Public function defined in peripheral.h.
     */
    bStatus_t GAPRole_SetParameter( uint16 param, uint8 len, void *pValue )
    {
      bStatus_t ret = SUCCESS;
      switch ( param )
      {
        case GAPROLE_IRK:
          if ( len == KEYLEN )
          {
            VOID osal_memcpy( gapRole_IRK, pValue, KEYLEN ) ;
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_SRK:
          if ( len == KEYLEN )
          {
            VOID osal_memcpy( gapRole_SRK, pValue, KEYLEN ) ;
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_SIGNCOUNTER:
          if ( len == sizeof ( uint32 ) )
          {
            gapRole_signCounter = *((uint32*)pValue);
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_ADVERT_ENABLED:
          if ( len == sizeof( uint8 ) )
          {
            uint8 oldAdvEnabled = gapRole_AdvEnabled;
            gapRole_AdvEnabled = *((uint8*)pValue);
    
            if ( (oldAdvEnabled) && (gapRole_AdvEnabled == FALSE) )
            {
              // Turn off Advertising
              if ( ( gapRole_state == GAPROLE_ADVERTISING ) 
                  || ( gapRole_state == GAPROLE_CONNECTED_ADV )
                  || ( gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT ) )
              {
                VOID GAP_EndDiscoverable( gapRole_TaskID );
              }
            }
            else if ( (oldAdvEnabled == FALSE) && (gapRole_AdvEnabled) )
            {
              // Turn on Advertising
              if ( (gapRole_state == GAPROLE_STARTED)
                  || (gapRole_state == GAPROLE_WAITING)
                  || (gapRole_state == GAPROLE_CONNECTED)
                  || (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT) )
              {
                VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
              }
            }
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_ADVERT_OFF_TIME:
          if ( len == sizeof ( uint16 ) )
          {
            gapRole_AdvertOffTime = *((uint16*)pValue);
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_ADVERT_DATA:
          if ( len <= B_MAX_ADV_LEN )
          {
            VOID osal_memset( gapRole_AdvertData, 0, B_MAX_ADV_LEN );
            VOID osal_memcpy( gapRole_AdvertData, pValue, len );
            gapRole_AdvertDataLen = len;
            
            // Update the advertising data
            ret = GAP_UpdateAdvertisingData( gapRole_TaskID,
                                  TRUE, gapRole_AdvertDataLen, gapRole_AdvertData );
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_SCAN_RSP_DATA:
          if ( len <= B_MAX_ADV_LEN )
          {
            VOID osal_memset( gapRole_ScanRspData, 0, B_MAX_ADV_LEN );
            VOID osal_memcpy( gapRole_ScanRspData, pValue, len );
            gapRole_ScanRspDataLen = len;
            
            // Update the Response Data
            ret = GAP_UpdateAdvertisingData( gapRole_TaskID,
                                  FALSE, gapRole_ScanRspDataLen, gapRole_ScanRspData );
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_ADV_EVENT_TYPE:
          if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAP_ADTYPE_ADV_LDC_DIRECT_IND) )
          {
            gapRole_AdvEventType = *((uint8*)pValue);
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_ADV_DIRECT_TYPE:
          if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= ADDRTYPE_PRIVATE_RESOLVE) )
          {
            gapRole_AdvDirectType = *((uint8*)pValue);
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_ADV_DIRECT_ADDR:
          if ( len == B_ADDR_LEN )
          {
            VOID osal_memcpy( gapRole_AdvDirectAddr, pValue, B_ADDR_LEN ) ;
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_ADV_CHANNEL_MAP:
          if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= 0x07) )
          {
            gapRole_AdvChanMap = *((uint8*)pValue);
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_ADV_FILTER_POLICY:
          if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAP_FILTER_POLICY_WHITE) )
          {
            gapRole_AdvFilterPolicy = *((uint8*)pValue);
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_RSSI_READ_RATE:
          if ( len == sizeof ( uint16 ) )
          {
            gapRole_RSSIReadRate = *((uint16*)pValue);
    
            if ( (gapRole_RSSIReadRate) && (gapRole_state == GAPROLE_CONNECTED) )
            {
              // Start the RSSI Reads
              VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
            }
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_PARAM_UPDATE_ENABLE:
          if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= TRUE) )
          {
            gapRole_ParamUpdateEnable = *((uint8*)pValue);
          }
          else
          {
            ret = bleInvalidRange;
          }
          break;
    
        case GAPROLE_MIN_CONN_INTERVAL:
          {
            uint16 newInterval = *((uint16*)pValue);
            if (   len == sizeof ( uint16 )           &&
                 ( newInterval >= MIN_CONN_INTERVAL ) &&
                 ( newInterval <= MAX_CONN_INTERVAL ) )
            {
              gapRole_MinConnInterval = newInterval;
            }
            else
            {
              ret = bleInvalidRange;
            }
          }
          break;
    
        case GAPROLE_MAX_CONN_INTERVAL:
          {
            uint16 newInterval = *((uint16*)pValue);
            if (   len == sizeof ( uint16 )          &&
                 ( newInterval >= MIN_CONN_INTERVAL) &&
                 ( newInterval <= MAX_CONN_INTERVAL) )
            {
              gapRole_MaxConnInterval = newInterval;
            }
            else
            {
              ret = bleInvalidRange;
            }
          }
          break;
    
        case GAPROLE_SLAVE_LATENCY:
          {
            uint16 latency = *((uint16*)pValue);
            if ( len == sizeof ( uint16 ) && (latency < MAX_SLAVE_LATENCY) )
            {
              gapRole_SlaveLatency = latency;
            }
            else
            {
              ret = bleInvalidRange;
            }
          }
          break;
    
        case GAPROLE_TIMEOUT_MULTIPLIER:
          {
            uint16 newTimeout = *((uint16*)pValue);
            if ( len == sizeof ( uint16 )
                && (newTimeout >= MIN_TIMEOUT_MULTIPLIER) && (newTimeout <= MAX_TIMEOUT_MULTIPLIER) )
            {
              gapRole_TimeoutMultiplier = newTimeout;
            }
            else
            {
              ret = bleInvalidRange;
            }
          }
          break;
    
          case GAPROLE_PARAM_UPDATE_REQ:
            {
              uint8 req = *((uint8*)pValue);
              if ( len == sizeof ( uint8 ) && (req == TRUE) )
              {
                // Make sure we don't send an L2CAP Connection Parameter Update Request
                // command within TGAP(conn_param_timeout) of an L2CAP Connection Parameter
                // Update Response being received.
                if ( osal_get_timeoutEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT ) == 0 )
                {             
                  // Start connection update procedure
                  gapRole_startConnUpdate( GAPROLE_NO_ACTION );
                  
                  // Connection update requested by app, cancel such pending procedure (if active)
                  VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
                }
                else
                {
                  ret = blePending;
                }
              }
              else
              {
                ret = bleInvalidRange;
              }
            }
            break;
      
        default:
          // The param value isn't part of this profile, try the GAP.
          if ( (param < TGAP_PARAMID_MAX) && (len == sizeof ( uint16 )) )
          {
            ret = GAP_SetParamValue( param, *((uint16*)pValue) );
          }
          else
          {
            ret = INVALIDPARAMETER;
          }
          break;
      }
    
      return ( ret );
    }
    
    /*********************************************************************
     * @brief   Get a GAP Role parameter.
     *
     * Public function defined in peripheral.h.
     */
    bStatus_t GAPRole_GetParameter( uint16 param, void *pValue )
    {
      bStatus_t ret = SUCCESS;
      switch ( param )
      {
        case GAPROLE_PROFILEROLE:
          *((uint8*)pValue) = gapRole_profileRole;
          break;
    
        case GAPROLE_IRK:
          VOID osal_memcpy( pValue, gapRole_IRK, KEYLEN ) ;
          break;
    
        case GAPROLE_SRK:
          VOID osal_memcpy( pValue, gapRole_SRK, KEYLEN ) ;
          break;
    
        case GAPROLE_SIGNCOUNTER:
          *((uint32*)pValue) = gapRole_signCounter;
          break;
    
        case GAPROLE_BD_ADDR:
          VOID osal_memcpy( pValue, gapRole_bdAddr, B_ADDR_LEN ) ;
          break;
    
        case GAPROLE_ADVERT_ENABLED:
          *((uint8*)pValue) = gapRole_AdvEnabled;
          break;
    
        case GAPROLE_ADVERT_OFF_TIME:
          *((uint16*)pValue) = gapRole_AdvertOffTime;
          break;
    
        case GAPROLE_ADVERT_DATA:
          VOID osal_memcpy( pValue , gapRole_AdvertData, gapRole_AdvertDataLen );
          break;
    
        case GAPROLE_SCAN_RSP_DATA:
          VOID osal_memcpy( pValue, gapRole_ScanRspData, gapRole_ScanRspDataLen ) ;
          break;
    
        case GAPROLE_ADV_EVENT_TYPE:
          *((uint8*)pValue) = gapRole_AdvEventType;
          break;
    
        case GAPROLE_ADV_DIRECT_TYPE:
          *((uint8*)pValue) = gapRole_AdvDirectType;
          break;
    
        case GAPROLE_ADV_DIRECT_ADDR:
          VOID osal_memcpy( pValue, gapRole_AdvDirectAddr, B_ADDR_LEN ) ;
          break;
    
        case GAPROLE_ADV_CHANNEL_MAP:
          *((uint8*)pValue) = gapRole_AdvChanMap;
          break;
    
        case GAPROLE_ADV_FILTER_POLICY:
          *((uint8*)pValue) = gapRole_AdvFilterPolicy;
          break;
    
        case GAPROLE_CONNHANDLE:
          *((uint16*)pValue) = gapRole_ConnectionHandle;
          break;
    
        case GAPROLE_RSSI_READ_RATE:
          *((uint16*)pValue) = gapRole_RSSIReadRate;
          break;
    
        case GAPROLE_PARAM_UPDATE_ENABLE:
          *((uint16*)pValue) = gapRole_ParamUpdateEnable;
          break;
    
        case GAPROLE_MIN_CONN_INTERVAL:
          *((uint16*)pValue) = gapRole_MinConnInterval;
          break;
    
        case GAPROLE_MAX_CONN_INTERVAL:
          *((uint16*)pValue) = gapRole_MaxConnInterval;
          break;
    
        case GAPROLE_SLAVE_LATENCY:
          *((uint16*)pValue) = gapRole_SlaveLatency;
          break;
    
        case GAPROLE_TIMEOUT_MULTIPLIER:
          *((uint16*)pValue) = gapRole_TimeoutMultiplier;
          break;
    
        case GAPROLE_CONN_BD_ADDR:
          VOID osal_memcpy( pValue, gapRole_ConnectedDevAddr, B_ADDR_LEN ) ;
          break;
    
        case GAPROLE_CONN_INTERVAL:
          *((uint16*)pValue) = gapRole_ConnInterval;
          break;
    
        case GAPROLE_CONN_LATENCY:
          *((uint16*)pValue) = gapRole_ConnSlaveLatency;
          break;
    
        case GAPROLE_CONN_TIMEOUT:
          *((uint16*)pValue) = gapRole_ConnTimeout;
          break;
    
        case GAPROLE_STATE:
          *((uint8*)pValue) = gapRole_state;
          break;
        
        default:
          // The param value isn't part of this profile, try the GAP.
          if ( param < TGAP_PARAMID_MAX )
          {
            *((uint16*)pValue) = GAP_GetParamValue( param );
          }
          else
          {
            ret = INVALIDPARAMETER;
          }
          break;
      }
    
      return ( ret );
    }
    
    /*********************************************************************
     * @brief   Does the device initialization.
     *
     * Public function defined in peripheral.h.
     */
    bStatus_t GAPRole_StartDevice( gapRolesCBs_t *pAppCallbacks )
    {
      if ( gapRole_state == GAPROLE_INIT )
      {
        // Clear all of the Application callbacks
        if ( pAppCallbacks )
        {
          pGapRoles_AppCGs = pAppCallbacks;
        }
    
        // Start the GAP
        gapRole_SetupGAP();
    
        return ( SUCCESS );
      }
      else
      {
        return ( bleAlreadyInRequestedMode );
      }
    }
    
    /*********************************************************************
     * @brief   Register application's callbacks.
     *
     * Public function defined in peripheral.h.
     */
    void GAPRole_RegisterAppCBs( gapRolesParamUpdateCB_t *pParamUpdateCB )
    {
      if ( pParamUpdateCB != NULL )
      {
        pGapRoles_ParamUpdateCB = pParamUpdateCB;
      }
    }
    
    /*********************************************************************
     * @brief   Terminates the existing connection.
     *
     * Public function defined in peripheral.h.
     */
    bStatus_t GAPRole_TerminateConnection( void )
    {
      if ( gapRole_state == GAPROLE_CONNECTED )
      {
        return ( GAP_TerminateLinkReq( gapRole_TaskID, gapRole_ConnectionHandle, 
                                       HCI_DISCONNECT_REMOTE_USER_TERM ) );
      }
      else
      {
        return ( bleIncorrectMode );
      }
    }
    
    /*********************************************************************
     * LOCAL FUNCTION PROTOTYPES
     */
    
    /*********************************************************************
     * @brief   Task Initialization function.
     *
     * Internal function defined in peripheral.h.
     */
    void GAPRole_Init( uint8 task_id )
    {
      gapRole_TaskID = task_id;
    
      gapRole_state = GAPROLE_INIT;
      gapRole_ConnectionHandle = INVALID_CONNHANDLE;
    
      GAP_RegisterForHCIMsgs( gapRole_TaskID );
    
      // Initialize the Profile Advertising and Connection Parameters
      gapRole_profileRole = GAP_PROFILE_PERIPHERAL;
      VOID osal_memset( gapRole_IRK, 0, KEYLEN );
      VOID osal_memset( gapRole_SRK, 0, KEYLEN );
      gapRole_signCounter = 0;
      gapRole_AdvEventType = GAP_ADTYPE_ADV_IND;
      gapRole_AdvDirectType = ADDRTYPE_PUBLIC;
      gapRole_AdvChanMap = GAP_ADVCHAN_ALL;
      gapRole_AdvFilterPolicy = GAP_FILTER_POLICY_ALL;
    
      // Restore Items from NV
      VOID osal_snv_read( BLE_NVID_IRK, KEYLEN, gapRole_IRK );
      VOID osal_snv_read( BLE_NVID_CSRK, KEYLEN, gapRole_SRK );
      VOID osal_snv_read( BLE_NVID_SIGNCOUNTER, sizeof( uint32 ), &gapRole_signCounter );
    }
    
    /*********************************************************************
     * @brief   Task Event Processor function.
     *
     * Internal function defined in peripheral.h.
     */
    uint16 GAPRole_ProcessEvent( uint8 task_id, uint16 events )
    {
      VOID task_id; // OSAL required parameter that isn't used in this function
    
      if ( events & SYS_EVENT_MSG )
      {
        uint8 *pMsg;
    
        if ( (pMsg = osal_msg_receive( gapRole_TaskID )) != NULL )
        {
          gapRole_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
    
          // Release the OSAL message
          VOID osal_msg_deallocate( pMsg );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & GAP_EVENT_SIGN_COUNTER_CHANGED )
      {
        // Sign counter changed, save it to NV
        VOID osal_snv_write( BLE_NVID_SIGNCOUNTER, sizeof( uint32 ), &gapRole_signCounter );
    
        return ( events ^ GAP_EVENT_SIGN_COUNTER_CHANGED );
      }
    
      if ( events & START_ADVERTISING_EVT )
      {
        if ( gapRole_AdvEnabled )
        {
          gapAdvertisingParams_t params;
    
          // Setup advertisement parameters
          if ( gapRole_state == GAPROLE_CONNECTED )
          {
            // While in a connection, we can only advertise non-connectable undirected.
            params.eventType = GAP_ADTYPE_ADV_NONCONN_IND; //liaw
          }
          else
          {
            params.eventType = gapRole_AdvEventType;
            params.initiatorAddrType = gapRole_AdvDirectType;
            VOID osal_memcpy( params.initiatorAddr, gapRole_AdvDirectAddr, B_ADDR_LEN );
          }
          params.channelMap = gapRole_AdvChanMap;
          params.filterPolicy = gapRole_AdvFilterPolicy;
    
          if ( GAP_MakeDiscoverable( gapRole_TaskID, &params ) != SUCCESS )
          {
            gapRole_state = GAPROLE_ERROR;
            
            // Notify the application with the new state change
            if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
            {
              pGapRoles_AppCGs->pfnStateChange( gapRole_state );
            }
          }
        }
        return ( events ^ START_ADVERTISING_EVT );
      }
    
      if ( events & RSSI_READ_EVT )
      {
        // Only get RSSI when in a connection
        if ( gapRole_state == GAPROLE_CONNECTED )
        {
          // Ask for RSSI
          VOID HCI_ReadRssiCmd( gapRole_ConnectionHandle );
    
          // Setup next event
          if ( gapRole_RSSIReadRate )
          {
            VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
          }
        }
        return ( events ^ RSSI_READ_EVT );
      }
    
      if ( events & START_CONN_UPDATE_EVT )
      {
        // Start connection update procedure
        gapRole_startConnUpdate( GAPROLE_NO_ACTION );
    
        return ( events ^ START_CONN_UPDATE_EVT );
      }
      
      if ( events & CONN_PARAM_TIMEOUT_EVT )
      {
        // Unsuccessful in updating connection parameters
        gapRole_HandleParamUpdateNoSuccess();
    
        return ( events ^ CONN_PARAM_TIMEOUT_EVT );
      }
      
      // Discard unknown events
      return 0;
    }
    
    /*********************************************************************
     * @fn      gapRole_ProcessOSALMsg
     *
     * @brief   Process an incoming task message.
     *
     * @param   pMsg - message to process
     *
     * @return  none
     */
    
    uint8 test_rssi[100];
    uint8 testptr = 0;
    static void gapRole_ProcessOSALMsg( osal_event_hdr_t *pMsg )
    {
      switch ( pMsg->event )
      {
        case HCI_GAP_EVENT_EVENT:
          if ( pMsg->status == HCI_COMMAND_COMPLETE_EVENT_CODE )
          {
            hciEvt_CmdComplete_t *pPkt = (hciEvt_CmdComplete_t *)pMsg;
    
            if ( pPkt->cmdOpcode == HCI_READ_RSSI )
            {
              int8 rssi = (int8)pPkt->pReturnParam[3];
              test_rssi[testptr] = rssi;
              testptr++;
              if(testptr >= 100)
              {
                testptr = 0;
              }
              if ( (gapRole_state == GAPROLE_CONNECTED) && (rssi != RSSI_NOT_AVAILABLE) )
              {
                // Report RSSI to app
                if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnRssiRead )
                {
                  pGapRoles_AppCGs->pfnRssiRead( rssi );
                }
              }
            }
          }
          break;
    
        case GAP_MSG_EVENT:
          gapRole_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
          break;
    
        case L2CAP_SIGNAL_EVENT:
          {
            l2capSignalEvent_t *pPkt = (l2capSignalEvent_t *)pMsg;
    
            // Process the Parameter Update Response
            if ( pPkt->opcode == L2CAP_PARAM_UPDATE_RSP )
            {
              l2capParamUpdateRsp_t *pRsp = (l2capParamUpdateRsp_t *)&(pPkt->cmd.updateRsp);
                      
              if ( ( pRsp->result == L2CAP_CONN_PARAMS_REJECTED ) &&
                   ( paramUpdateNoSuccessOption == GAPROLE_TERMINATE_LINK ) )
              {
                // Cancel connection param update timeout timer
                VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
                          
                // Terminate connection immediately
                GAPRole_TerminateConnection();
              }
              else
              {
                uint16 timeout = GAP_GetParamValue( TGAP_CONN_PARAM_TIMEOUT );
                
                // Let's wait for Controller to update connection parameters if they're
                // accepted. Otherwise, decide what to do based on no success option.
                VOID osal_start_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT, timeout );
              }
            }
          }
          break;
    
        default:
          break;
      }
    }
    
    /*********************************************************************
     * @fn      gapRole_ProcessGAPMsg
     *
     * @brief   Process an incoming task message.
     *
     * @param   pMsg - message to process
     *
     * @return  none
     */
    static void gapRole_ProcessGAPMsg( gapEventHdr_t *pMsg )
    {
      uint8 notify = FALSE;   // State changed notify the app? (default no)
    
      switch ( pMsg->opcode )
      {
        case GAP_DEVICE_INIT_DONE_EVENT:
          {
            gapDeviceInitDoneEvent_t *pPkt = (gapDeviceInitDoneEvent_t *)pMsg;
            bStatus_t stat = pPkt->hdr.status;
    
            if ( stat == SUCCESS )
            {
              // Save off the generated keys
              VOID osal_snv_write( BLE_NVID_IRK, KEYLEN, gapRole_IRK );
              VOID osal_snv_write( BLE_NVID_CSRK, KEYLEN, gapRole_SRK );
    
              // Save off the information
              VOID osal_memcpy( gapRole_bdAddr, pPkt->devAddr, B_ADDR_LEN );
    
              gapRole_state = GAPROLE_STARTED;
    
              // Update the advertising data
              stat = GAP_UpdateAdvertisingData( gapRole_TaskID,
                                  TRUE, gapRole_AdvertDataLen, gapRole_AdvertData );
            }
    
            if ( stat != SUCCESS )
            {
              gapRole_state = GAPROLE_ERROR;
            }
    
            notify = TRUE;
          }
          break;
    
        case GAP_ADV_DATA_UPDATE_DONE_EVENT:
          {
            gapAdvDataUpdateEvent_t *pPkt = (gapAdvDataUpdateEvent_t *)pMsg;
    
            if ( pPkt->hdr.status == SUCCESS )
            {
              if ( pPkt->adType )
              {
                // Setup the Response Data
                pPkt->hdr.status = GAP_UpdateAdvertisingData( gapRole_TaskID,
                                  FALSE, gapRole_ScanRspDataLen, gapRole_ScanRspData );
              }
              else if ( ( gapRole_state != GAPROLE_ADVERTISING ) &&
                        ( osal_get_timeoutEx( gapRole_TaskID, START_ADVERTISING_EVT ) == 0 ) )
              {
                // Start advertising
                VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
              }
            }
    
            if ( pPkt->hdr.status != SUCCESS )
            {
              // Set into Error state
              gapRole_state = GAPROLE_ERROR;
              notify = TRUE;
            }
          }
          break;
    
        case GAP_MAKE_DISCOVERABLE_DONE_EVENT:
        case GAP_END_DISCOVERABLE_DONE_EVENT:
          {
            gapMakeDiscoverableRspEvent_t *pPkt = (gapMakeDiscoverableRspEvent_t *)pMsg;
    
            if ( pPkt->hdr.status == SUCCESS )
            {
              if ( pMsg->opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT )
              {
                if (gapRole_state == GAPROLE_CONNECTED)
                {
                  gapRole_state = GAPROLE_CONNECTED_ADV;
                }
                else
                {
                  gapRole_state = GAPROLE_ADVERTISING;
                }
              }
              else // GAP_END_DISCOVERABLE_DONE_EVENT
              {
    
                if ( gapRole_AdvertOffTime != 0 )
                {
                  if ( ( gapRole_AdvEnabled ) )
                  {
                    VOID osal_start_timerEx( gapRole_TaskID, START_ADVERTISING_EVT, gapRole_AdvertOffTime );
                  }
                }
                else
                {
                  // Since gapRole_AdvertOffTime is set to 0, the device should not
                  // automatically become discoverable again after a period of time.
                  // Set enabler to FALSE; device will become discoverable again when
                  // this value gets set to TRUE
                  gapRole_AdvEnabled = FALSE;
                }
                
                if (gapRole_state == GAPROLE_CONNECTED_ADV)
                {
                  // In the Advertising Off period
                  gapRole_state = GAPROLE_CONNECTED;
                }
                else if (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT)
                {
                  // Advertising was just turned off after the link disconnected so begin
                  // advertising again.
                  gapRole_AdvEnabled = TRUE;
                  
                  // Turn advertising back on.
                  VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
                }
                else
                {
                  // In the Advertising Off period
                  gapRole_state = GAPROLE_WAITING;
                }
    
              }
            }
            else
            {
              gapRole_state = GAPROLE_ERROR;
            }
            notify = TRUE;
          }
          break;
    
        case GAP_LINK_ESTABLISHED_EVENT:
          {
            gapEstLinkReqEvent_t *pPkt = (gapEstLinkReqEvent_t *)pMsg;
    
            if ( pPkt->hdr.status == SUCCESS )
            {
              VOID osal_memcpy( gapRole_ConnectedDevAddr, pPkt->devAddr, B_ADDR_LEN );
              gapRole_ConnectionHandle = pPkt->connectionHandle;
              gapRole_state = GAPROLE_CONNECTED;
    
              if ( gapRole_RSSIReadRate )
              {
                // Start the RSSI Reads
                VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
              }
    
              // Store connection information
              gapRole_ConnInterval = pPkt->connInterval;
              gapRole_ConnSlaveLatency = pPkt->connLatency;
              gapRole_ConnTimeout = pPkt->connTimeout;
    
              // Check whether update parameter request is enabled
              if ( gapRole_ParamUpdateEnable == TRUE )
              {
                // Get the minimum time upon connection establishment before the 
                // peripheral can start a connection update procedure.
                uint16 timeout = GAP_GetParamValue( TGAP_CONN_PAUSE_PERIPHERAL );
                
                osal_start_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT, timeout*1000 );
              }
    
              // Notify the Bond Manager to the connection
              VOID GAPBondMgr_LinkEst( pPkt->devAddrType, pPkt->devAddr, pPkt->connectionHandle, GAP_PROFILE_PERIPHERAL );
              
              // Set enabler to FALSE; device will become discoverable again when
              // this value gets set to TRUE
              gapRole_AdvEnabled = FALSE;          
            }
            else if ( pPkt->hdr.status == bleGAPConnNotAcceptable )
            {
              // Set enabler to FALSE; device will become discoverable again when
              // this value gets set to TRUE
              gapRole_AdvEnabled = FALSE;
    
              // Go to WAITING state, and then start advertising
              gapRole_state = GAPROLE_WAITING;
            }
            else
            {
              gapRole_state = GAPROLE_ERROR;
            }
            notify = TRUE;
          }
          break;
    
        case GAP_LINK_TERMINATED_EVENT:
          {
            gapTerminateLinkEvent_t *pPkt = (gapTerminateLinkEvent_t *)pMsg;
    
            VOID GAPBondMgr_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
            osal_memset( gapRole_ConnectedDevAddr, 0, B_ADDR_LEN );
    
            // Erase connection information
            gapRole_ConnInterval = 0;
            gapRole_ConnSlaveLatency = 0;
            gapRole_ConnTimeout = 0;
    
            // Cancel all connection parameter update timers (if any active)
            VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
            VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
              
            // Go to WAITING state, and then start advertising
            if( pPkt->reason == LL_SUPERVISION_TIMEOUT_TERM )
            {
              gapRole_state = GAPROLE_WAITING_AFTER_TIMEOUT;
            }
            else
            {
              gapRole_state = GAPROLE_WAITING;
            }
    
            notify = TRUE;
            
            // Check if still advertising from within last connection.
            if ( gapRole_AdvEnabled)
            {
              // End advertising so we can restart advertising in order 
              // to change to connectable advertising from nonconnectable.
              VOID GAP_EndDiscoverable( gapRole_TaskID );
            }
            else  // Turn advertising back on.
            {
              gapRole_AdvEnabled = TRUE;
              VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT);
            }
    
            gapRole_ConnectionHandle = INVALID_CONNHANDLE;
          }
          break;
    
        case GAP_LINK_PARAM_UPDATE_EVENT:
          {
            gapLinkUpdateEvent_t *pPkt = (gapLinkUpdateEvent_t *)pMsg;
    
            // Cancel connection param update timeout timer (if active)
            VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
            
            if ( pPkt->hdr.status == SUCCESS )
            {
              // Store new connection parameters
              gapRole_ConnInterval = pPkt->connInterval;
              gapRole_ConnSlaveLatency = pPkt->connLatency;
              gapRole_ConnTimeout = pPkt->connTimeout;
              
              // Make sure there's no pending connection update procedure
              if ( osal_get_timeoutEx( gapRole_TaskID, START_CONN_UPDATE_EVT ) == 0 )
              {
                // Notify the application with the new connection parameters
                if ( pGapRoles_ParamUpdateCB != NULL )
                {
                  (*pGapRoles_ParamUpdateCB)( gapRole_ConnInterval, 
                                              gapRole_ConnSlaveLatency, 
                                              gapRole_ConnTimeout );
                }
              }
            }
          }
          break;
    
        default:
          break;
      }
    
      if ( notify == TRUE )
      {
        // Notify the application with the new state change
        if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
        {
          pGapRoles_AppCGs->pfnStateChange( gapRole_state );
        }
      }
    }
    
    /*********************************************************************
     * @fn      gapRole_SetupGAP
     *
     * @brief   Call the GAP Device Initialization function using the
     *          Profile Parameters.
     *
     * @param   none
     *
     * @return  none
     */
    static void gapRole_SetupGAP( void )
    {
      VOID GAP_DeviceInit( gapRole_TaskID,
              gapRole_profileRole, 0,
              gapRole_IRK, gapRole_SRK,
              &gapRole_signCounter );
    }
    
    /*********************************************************************
     * @fn      gapRole_HandleParamUpdateNoSuccess
     *
     * @brief   Handle unsuccessful connection parameters update.
     *
     * @param   none
     *
     * @return  none
     */
    static void gapRole_HandleParamUpdateNoSuccess( void )
    {
      // See which option was choosen for unsuccessful updates
      switch ( paramUpdateNoSuccessOption )
      {
        case GAPROLE_RESEND_PARAM_UPDATE:
          GAPRole_SendUpdateParam( gapRole_MinConnInterval, gapRole_MaxConnInterval,
                                   gapRole_SlaveLatency, gapRole_TimeoutMultiplier,
                                   GAPROLE_RESEND_PARAM_UPDATE );
          break;
    
        case GAPROLE_TERMINATE_LINK:
          GAPRole_TerminateConnection();
          break;
    
        case GAPROLE_NO_ACTION:
          // fall through
        default:
          //do nothing
          break;
      }
    }
    
    /********************************************************************
     * @fn          gapRole_startConnUpdate
     *
     * @brief       Start the connection update procedure
     *
     * @param       handleFailure - what to do if the update does not occur.
     *              Method may choose to terminate connection, try again, or take no action
     *
     * @return      none
     */
    static void gapRole_startConnUpdate( uint8 handleFailure )
    {
        /*
      // First check the current connection parameters versus the configured parameters
      if ( (gapRole_ConnInterval < gapRole_MinConnInterval)   ||
           (gapRole_ConnInterval > gapRole_MaxConnInterval)   ||
           (gapRole_ConnSlaveLatency != gapRole_SlaveLatency) ||
           (gapRole_ConnTimeout  != gapRole_TimeoutMultiplier) )
      {
        l2capParamUpdateReq_t updateReq;
        uint16 timeout = GAP_GetParamValue( TGAP_CONN_PARAM_TIMEOUT );
            
        updateReq.intervalMin = gapRole_MinConnInterval;
        updateReq.intervalMax = gapRole_MaxConnInterval;
        updateReq.slaveLatency = gapRole_SlaveLatency;
        updateReq.timeoutMultiplier = gapRole_TimeoutMultiplier;
        
        VOID L2CAP_ConnParamUpdateReq( gapRole_ConnectionHandle, &updateReq, gapRole_TaskID );
            
        paramUpdateNoSuccessOption = handleFailure;
            
        // Let's wait for L2CAP Connection Parameters Update Response
        VOID osal_start_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT, timeout );
      }
        */
    }
    
    /********************************************************************
     * @fn          GAPRole_SendUpdateParam
     *
     * @brief       Update the parameters of an existing connection
     *
     * @param       minConnInterval - the new min connection interval
     * @param       maxConnInterval - the new max connection interval
     * @param       latency - the new slave latency
     * @param       connTimeout - the new timeout value
     * @param       handleFailure - what to do if the update does not occur.
     *              Method may choose to terminate connection, try again, or take no action
     *
     * @return      SUCCESS, bleNotConnected, or bleInvalidRange
     */
    bStatus_t GAPRole_SendUpdateParam( uint16 minConnInterval, uint16 maxConnInterval,
                                       uint16 latency, uint16 connTimeout, uint8 handleFailure )
    {    
      // If there is no existing connection no update need be sent
      if ( gapRole_state != GAPROLE_CONNECTED )
      {
        return ( bleNotConnected );
      }
      
      // Check that all parameters are in range before sending request
      if ( ( minConnInterval >= DEFAULT_MIN_CONN_INTERVAL ) &&
           ( minConnInterval < DEFAULT_MAX_CONN_INTERVAL  ) &&
           ( maxConnInterval >= DEFAULT_MIN_CONN_INTERVAL ) &&
           ( maxConnInterval < DEFAULT_MAX_CONN_INTERVAL  ) &&
           ( latency         < MAX_SLAVE_LATENCY          ) &&
           ( connTimeout     >= MIN_TIMEOUT_MULTIPLIER    ) &&
           ( connTimeout     < MAX_TIMEOUT_MULTIPLIER     ) )
      {
        gapRole_MinConnInterval = minConnInterval;
        gapRole_MaxConnInterval = maxConnInterval;
        gapRole_SlaveLatency = latency;
        gapRole_TimeoutMultiplier = connTimeout;
    
        // Start connection update procedure
        gapRole_startConnUpdate( handleFailure );
    
        // Connection update requested by app, cancel such pending procedure (if active)
        VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
                  
        return ( SUCCESS );
      }
    
      return ( bleInvalidRange );
    }
    
    /*********************************************************************
    *********************************************************************/
    

    /**************************************************************************************************
      Filename:       simpleBLEPeripheral.c
      Revised:        $Date: 2010-08-06 08:56:11 -0700 (Fri, 06 Aug 2010) $
      Revision:       $Revision: 23333 $
    
      Description:    This file contains the Simple BLE Peripheral sample application
                      for use with the CC2540 Bluetooth Low Energy Protocol Stack.
    
      Copyright 2010 - 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    
    #include "bcomdef.h"
    #include "OSAL.h"
    #include "OSAL_PwrMgr.h"
    
    #include "OnBoard.h"
    #include "hal_adc.h"
    #include "hal_led.h"
    #include "hal_key.h"
    #include "hal_lcd.h"
    
    #include "gatt.h"
    
    #include "hci.h"
    
    #include "gapgattserver.h"
    #include "gattservapp.h"
    #include "devinfoservice.h"
    #include "simpleGATTprofile.h"
    
    #if defined( CC2540_MINIDK )
      #include "simplekeys.h"
    #endif
    
    #include "peripheral.h"
    
    #include "gapbondmgr.h"
    
    #include "simpleBLEPeripheral.h"
    
    #if defined FEATURE_OAD
      #include "oad.h"
      #include "oad_target.h"
    #endif
    
    /*********************************************************************
     * MACROS
     */
    
    /*********************************************************************
     * CONSTANTS
     */
    
    // How often to perform periodic event
    #define SBP_PERIODIC_EVT_PERIOD                   5000
    
    // What is the advertising interval when device is discoverable (units of 625us, 160=100ms)
    #define DEFAULT_ADVERTISING_INTERVAL          160
    
    // Limited discoverable mode advertises for 30.72s, and then stops
    // General discoverable mode advertises indefinitely
    
    #if defined ( CC2540_MINIDK )
    #define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_LIMITED
    #else
    #define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_GENERAL
    #endif  // defined ( CC2540_MINIDK )
    
    // Minimum connection interval (units of 1.25ms, 80=100ms) if automatic parameter update request is enabled
    #define DEFAULT_DESIRED_MIN_CONN_INTERVAL     80
    
    // Maximum connection interval (units of 1.25ms, 800=1000ms) if automatic parameter update request is enabled
    #define DEFAULT_DESIRED_MAX_CONN_INTERVAL     800
    
    // Slave latency to use if automatic parameter update request is enabled
    #define DEFAULT_DESIRED_SLAVE_LATENCY         0
    
    // Supervision timeout value (units of 10ms, 1000=10s) if automatic parameter update request is enabled
    #define DEFAULT_DESIRED_CONN_TIMEOUT          1000
    
    // Whether to enable automatic parameter update request when a connection is formed
    #define DEFAULT_ENABLE_UPDATE_REQUEST         TRUE
    
    // Connection Pause Peripheral time value (in seconds)
    #define DEFAULT_CONN_PAUSE_PERIPHERAL         6
    
    // Company Identifier: Texas Instruments Inc. (13)
    #define TI_COMPANY_ID                         0x000D
    
    #define INVALID_CONNHANDLE                    0xFFFF
    
    // Length of bd addr as a string
    #define B_ADDR_STR_LEN                        15
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    /*********************************************************************
     * EXTERNAL VARIABLES
     */
    
    /*********************************************************************
     * EXTERNAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    static uint8 simpleBLEPeripheral_TaskID;   // Task ID for internal task/event processing
    
    static gaprole_States_t gapProfileState = GAPROLE_INIT;
    
    // GAP - SCAN RSP data (max size = 31 bytes)
    static uint8 scanRspData[] =
    {
      // complete name
      0x14,   // length of this data
      GAP_ADTYPE_LOCAL_NAME_COMPLETE,
      0x53,   // 'S'
      0x69,   // 'i'
      0x6d,   // 'm'
      0x70,   // 'p'
      0x6c,   // 'l'
      0x65,   // 'e'
      0x42,   // 'B'
      0x4c,   // 'L'
      0x45,   // 'E'
      0x50,   // 'P'
      0x65,   // 'e'
      0x72,   // 'r'
      0x69,   // 'i'
      0x70,   // 'p'
      0x68,   // 'h'
      0x65,   // 'e'
      0x72,   // 'r'
      0x61,   // 'a'
      0x6c,   // 'l'
    
      // connection interval range
      0x05,   // length of this data
      GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
      LO_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ),   // 100ms
      HI_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ),
      LO_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ),   // 1s
      HI_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ),
    
      // Tx power level
      0x02,   // length of this data
      GAP_ADTYPE_POWER_LEVEL,
      0       // 0dBm
    };
    
    // GAP - Advertisement data (max size = 31 bytes, though this is
    // best kept short to conserve power while advertisting)
    static uint8 advertData[] =
    {
      // Flags; this sets the device to use limited discoverable
      // mode (advertises for 30 seconds at a time) instead of general
      // discoverable mode (advertises indefinitely)
      0x02,   // length of this data
      GAP_ADTYPE_FLAGS,
      DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
    
      // service UUID, to notify central devices what services are included
      // in this peripheral
      0x03,   // length of this data
      GAP_ADTYPE_16BIT_MORE,      // some of the UUID's, but not all
      LO_UINT16( SIMPLEPROFILE_SERV_UUID ),
      HI_UINT16( SIMPLEPROFILE_SERV_UUID ),
    
    };
    
    // GAP GATT Attributes
    static uint8 attDeviceName[GAP_DEVICE_NAME_LEN] = "Simple BLE Peripheral";
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void simpleBLEPeripheral_ProcessOSALMsg( osal_event_hdr_t *pMsg );
    static void peripheralStateNotificationCB( gaprole_States_t newState );
    static void performPeriodicTask( void );
    static void simpleProfileChangeCB( uint8 paramID );
    
    #if defined( CC2540_MINIDK )
    static void simpleBLEPeripheral_HandleKeys( uint8 shift, uint8 keys );
    #endif
    
    #if (defined HAL_LCD) && (HAL_LCD == TRUE)
    static char *bdAddr2Str ( uint8 *pAddr );
    #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
    
    
    
    /*********************************************************************
     * PROFILE CALLBACKS
     */
    
    // GAP Role Callbacks
    static gapRolesCBs_t simpleBLEPeripheral_PeripheralCBs =
    {
      peripheralStateNotificationCB,  // Profile State Change Callbacks
      NULL                            // When a valid RSSI is read from controller (not used by application)
    };
    
    // GAP Bond Manager Callbacks
    static gapBondCBs_t simpleBLEPeripheral_BondMgrCBs =
    {
      NULL,                     // Passcode callback (not used by application)
      NULL                      // Pairing / Bonding state Callback (not used by application)
    };
    
    // Simple GATT Profile Callbacks
    static simpleProfileCBs_t simpleBLEPeripheral_SimpleProfileCBs =
    {
      simpleProfileChangeCB    // Charactersitic value change callback
    };
    /*********************************************************************
     * PUBLIC FUNCTIONS
     */
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_Init
     *
     * @brief   Initialization function for the Simple BLE Peripheral App Task.
     *          This is called during initialization and should contain
     *          any application specific initialization (ie. hardware
     *          initialization/setup, table initialization, power up
     *          notificaiton ... ).
     *
     * @param   task_id - the ID assigned by OSAL.  This ID should be
     *                    used to send messages and set timers.
     *
     * @return  none
     */
    void SimpleBLEPeripheral_Init( uint8 task_id )
    {
      simpleBLEPeripheral_TaskID = task_id;
    
      // Setup the GAP
      VOID GAP_SetParamValue( TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL );
      
      // Setup the GAP Peripheral Role Profile
      {
        #if defined( CC2540_MINIDK )
          // For the CC2540DK-MINI keyfob, device doesn't start advertising until button is pressed
          uint8 initial_advertising_enable = FALSE;
        #else
          // For other hardware platforms, device starts advertising upon initialization
          uint8 initial_advertising_enable = TRUE;
        #endif
    
        // By setting this to zero, the device will go into the waiting state after
        // being discoverable for 30.72 second, and will not being advertising again
        // until the enabler is set back to TRUE
        uint16 gapRole_AdvertOffTime = 0;
    
        uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST;
        uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
        uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
        uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY;
        uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT;
    
        // Set the GAP Role Parameters
        GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );
        GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );
    
        GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData );
        GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData );
    
        GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request );
        GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval );
        GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval );
        GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency );
        GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout );
      }
    
      // Set the GAP Characteristics
      GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName );
    
      // Set advertising interval
      {
        uint16 advInt = DEFAULT_ADVERTISING_INTERVAL;
    
        GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt );
        GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt );
        GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt );
        GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt );
      }
    
      // Setup the GAP Bond Manager
      {
        uint32 passkey = 0; // passkey "000000"
        uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
        uint8 mitm = TRUE;
        uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
        uint8 bonding = TRUE;
        GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof ( uint32 ), &passkey );
        GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof ( uint8 ), &pairMode );
        GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof ( uint8 ), &mitm );
        GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof ( uint8 ), &ioCap );
        GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof ( uint8 ), &bonding );
      }
    
      // Initialize GATT attributes
      GGS_AddService( GATT_ALL_SERVICES );            // GAP
      GATTServApp_AddService( GATT_ALL_SERVICES );    // GATT attributes
      DevInfo_AddService();                           // Device Information Service
      SimpleProfile_AddService( GATT_ALL_SERVICES );  // Simple GATT Profile
    #if defined FEATURE_OAD
      VOID OADTarget_AddService();                    // OAD Profile
    #endif
    
      // Setup the SimpleProfile Characteristic Values
      {
        uint8 charValue1 = 1;
        uint8 charValue2 = 2;
        uint8 charValue3 = 3;
        uint8 charValue4 = 4;
        uint8 charValue5[SIMPLEPROFILE_CHAR5_LEN] = { 1, 2, 3, 4, 5 };
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR1, sizeof ( uint8 ), &charValue1 );
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR2, sizeof ( uint8 ), &charValue2 );
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR3, sizeof ( uint8 ), &charValue3 );
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof ( uint8 ), &charValue4 );
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5 );
      }
    
    
    #if defined( CC2540_MINIDK )
    
      SK_AddService( GATT_ALL_SERVICES ); // Simple Keys Profile
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( simpleBLEPeripheral_TaskID );
    
      // makes sure LEDs are off
      HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF );
    
      // For keyfob board set GPIO pins into a power-optimized state
      // Note that there is still some leakage current from the buzzer,
      // accelerometer, LEDs, and buttons on the PCB.
    
      P0SEL = 0; // Configure Port 0 as GPIO
      P1SEL = 0; // Configure Port 1 as GPIO
      P2SEL = 0; // Configure Port 2 as GPIO
    
      P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons),
                    // all others (P0.2-P0.7) as output
      P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output
      P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output
    
      P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons)
      P1 = 0;   // All pins on port 1 to low
      P2 = 0;   // All pins on port 2 to low
    
    #endif // #if defined( CC2540_MINIDK )
    
    #if (defined HAL_LCD) && (HAL_LCD == TRUE)
    
    #if defined FEATURE_OAD
      #if defined (HAL_IMAGE_A)
        HalLcdWriteStringValue( "BLE Peri-A", OAD_VER_NUM( _imgHdr.ver ), 16, HAL_LCD_LINE_1 );
      #else
        HalLcdWriteStringValue( "BLE Peri-B", OAD_VER_NUM( _imgHdr.ver ), 16, HAL_LCD_LINE_1 );
      #endif // HAL_IMAGE_A
    #else
      HalLcdWriteString( "BLE Peripheral", HAL_LCD_LINE_1 );
    #endif // FEATURE_OAD
    
    #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
    
      // Register callback with SimpleGATTprofile
      VOID SimpleProfile_RegisterAppCBs( &simpleBLEPeripheral_SimpleProfileCBs );
    
      // Enable clock divide on halt
      // This reduces active current while radio is active and CC254x MCU
      // is halted
      HCI_EXT_ClkDivOnHaltCmd( HCI_EXT_ENABLE_CLK_DIVIDE_ON_HALT );
    
    #if defined ( DC_DC_P0_7 )
    
      // Enable stack to toggle bypass control on TPS62730 (DC/DC converter)
      HCI_EXT_MapPmIoPortCmd( HCI_EXT_PM_IO_PORT_P0, HCI_EXT_PM_IO_PORT_PIN7 );
    
    #endif // defined ( DC_DC_P0_7 )
    
      // Setup a delayed profile startup
      osal_set_event( simpleBLEPeripheral_TaskID, SBP_START_DEVICE_EVT );
    
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_ProcessEvent
     *
     * @brief   Simple BLE Peripheral Application Task event processor.  This function
     *          is called to process all events for the task.  Events
     *          include timers, messages and any other user defined events.
     *
     * @param   task_id  - The OSAL assigned task ID.
     * @param   events - events to process.  This is a bit map and can
     *                   contain more than one event.
     *
     * @return  events not processed
     */
    uint16 SimpleBLEPeripheral_ProcessEvent( uint8 task_id, uint16 events )
    {
    
      VOID task_id; // OSAL required parameter that isn't used in this function
    
      if ( events & SYS_EVENT_MSG )
      {
        uint8 *pMsg;
    
        if ( (pMsg = osal_msg_receive( simpleBLEPeripheral_TaskID )) != NULL )
        {
          simpleBLEPeripheral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
    
          // Release the OSAL message
          VOID osal_msg_deallocate( pMsg );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SBP_START_DEVICE_EVT )
      {
        // Start the Device
        VOID GAPRole_StartDevice( &simpleBLEPeripheral_PeripheralCBs );
    
        // Start Bond Manager
        VOID GAPBondMgr_Register( &simpleBLEPeripheral_BondMgrCBs );
    
        // Set timer for first periodic event
        osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );
    
        return ( events ^ SBP_START_DEVICE_EVT );
      }
    
      if ( events & SBP_PERIODIC_EVT )
      {
        // Restart timer
        if ( SBP_PERIODIC_EVT_PERIOD )
        {
          osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );
        }
    
        // Perform periodic application task
        performPeriodicTask();
    
        return (events ^ SBP_PERIODIC_EVT);
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*********************************************************************
     * @fn      simpleBLEPeripheral_ProcessOSALMsg
     *
     * @brief   Process an incoming task message.
     *
     * @param   pMsg - message to process
     *
     * @return  none
     */
    static void simpleBLEPeripheral_ProcessOSALMsg( osal_event_hdr_t *pMsg )
    {
      switch ( pMsg->event )
      {
      #if defined( CC2540_MINIDK )
        case KEY_CHANGE:
          simpleBLEPeripheral_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys );
          break;
      #endif // #if defined( CC2540_MINIDK )
    
      default:
        // do nothing
        break;
      }
    }
    
    #if defined( CC2540_MINIDK )
    /*********************************************************************
     * @fn      simpleBLEPeripheral_HandleKeys
     *
     * @brief   Handles all key events for this device.
     *
     * @param   shift - true if in shift/alt.
     * @param   keys - bit field for key events. Valid entries:
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void simpleBLEPeripheral_HandleKeys( uint8 shift, uint8 keys )
    {
      uint8 SK_Keys = 0;
    
      VOID shift;  // Intentionally unreferenced parameter
    
      if ( keys & HAL_KEY_SW_1 )
      {
        SK_Keys |= SK_KEY_LEFT;
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
    
        SK_Keys |= SK_KEY_RIGHT;
    
        // if device is not in a connection, pressing the right key should toggle
        // advertising on and off
        // Note:  If PLUS_BROADCASTER is define this condition is ignored and
        //        Device may advertise during connections as well. 
    #ifndef PLUS_BROADCASTER  
        if( gapProfileState != GAPROLE_CONNECTED )
        {
    #endif // PLUS_BROADCASTER
          uint8 current_adv_enabled_status;
          uint8 new_adv_enabled_status;
    
          //Find the current GAP advertisement status
          GAPRole_GetParameter( GAPROLE_ADVERT_ENABLED, &current_adv_enabled_status );
    
          if( current_adv_enabled_status == FALSE )
          {
            new_adv_enabled_status = TRUE;
          }
          else
          {
            new_adv_enabled_status = FALSE;
          }
    
          //change the GAP advertisement status to opposite of current status
          GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &new_adv_enabled_status );
    #ifndef PLUS_BROADCASTER
        }
    #endif // PLUS_BROADCASTER
      }
    
      // Set the value of the keys state to the Simple Keys Profile;
      // This will send out a notification of the keys state if enabled
      SK_SetParameter( SK_KEY_ATTR, sizeof ( uint8 ), &SK_Keys );
    }
    #endif // #if defined( CC2540_MINIDK )
    
    /*********************************************************************
     * @fn      peripheralStateNotificationCB
     *
     * @brief   Notification from the profile of a state change.
     *
     * @param   newState - new state
     *
     * @return  none
     */
    static void peripheralStateNotificationCB( gaprole_States_t newState )
    {
    #ifdef PLUS_BROADCASTER
      static uint8 first_conn_flag = 0;
    #endif // PLUS_BROADCASTER
      
      
      switch ( newState )
      {
        case GAPROLE_STARTED:
          {
            uint8 ownAddress[B_ADDR_LEN];
            uint8 systemId[DEVINFO_SYSTEM_ID_LEN];
    
            GAPRole_GetParameter(GAPROLE_BD_ADDR, ownAddress);
    
            // use 6 bytes of device address for 8 bytes of system ID value
            systemId[0] = ownAddress[0];
            systemId[1] = ownAddress[1];
            systemId[2] = ownAddress[2];
    
            // set middle bytes to zero
            systemId[4] = 0x00;
            systemId[3] = 0x00;
    
            // shift three bytes up
            systemId[7] = ownAddress[5];
            systemId[6] = ownAddress[4];
            systemId[5] = ownAddress[3];
    
            DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId);
    
            #if (defined HAL_LCD) && (HAL_LCD == TRUE)
              // Display device address
              HalLcdWriteString( bdAddr2Str( ownAddress ),  HAL_LCD_LINE_2 );
              HalLcdWriteString( "Initialized",  HAL_LCD_LINE_3 );
            #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
          }
          break;
    
        case GAPROLE_ADVERTISING:
          {
            #if (defined HAL_LCD) && (HAL_LCD == TRUE)
              HalLcdWriteString( "Advertising",  HAL_LCD_LINE_3 );
            #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
          }
          break;
    
        case GAPROLE_CONNECTED:
          {        
              uint16 rssi_read_rate = 1000;
              GAPRole_SetParameter(GAPROLE_RSSI_READ_RATE, sizeof(uint16), &rssi_read_rate); /* In ms */
            #if (defined HAL_LCD) && (HAL_LCD == TRUE)
              HalLcdWriteString( "Connected",  HAL_LCD_LINE_3 );
            #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
              
    #ifdef PLUS_BROADCASTER
            // Only turn advertising on for this state when we first connect
            // otherwise, when we go from connected_advertising back to this state
            // we will be turning advertising back on.
            if ( first_conn_flag == 0 ) 
            {
              uint8 adv_enabled_status = 1;
              GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8), &adv_enabled_status); // Turn on Advertising
              first_conn_flag = 1;
            }
    #endif // PLUS_BROADCASTER
          }
          break;
    
        case GAPROLE_CONNECTED_ADV:
          {
            #if (defined HAL_LCD) && (HAL_LCD == TRUE)
              HalLcdWriteString( "Connected Advertising",  HAL_LCD_LINE_3 );
            #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
          }
          break;      
        case GAPROLE_WAITING:
          {
            #if (defined HAL_LCD) && (HAL_LCD == TRUE)
              HalLcdWriteString( "Disconnected",  HAL_LCD_LINE_3 );
            #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
          }
          break;
    
        case GAPROLE_WAITING_AFTER_TIMEOUT:
          {
            #if (defined HAL_LCD) && (HAL_LCD == TRUE)
              HalLcdWriteString( "Timed Out",  HAL_LCD_LINE_3 );
            #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
              
    #ifdef PLUS_BROADCASTER
            // Reset flag for next connection.
            first_conn_flag = 0;
    #endif //#ifdef (PLUS_BROADCASTER)
          }
          break;
    
        case GAPROLE_ERROR:
          {
            #if (defined HAL_LCD) && (HAL_LCD == TRUE)
              HalLcdWriteString( "Error",  HAL_LCD_LINE_3 );
            #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
          }
          break;
    
        default:
          {
            #if (defined HAL_LCD) && (HAL_LCD == TRUE)
              HalLcdWriteString( "",  HAL_LCD_LINE_3 );
            #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
          }
          break;
    
      }
    
      gapProfileState = newState;
    
    #if !defined( CC2540_MINIDK )
      VOID gapProfileState;     // added to prevent compiler warning with
                                // "CC2540 Slave" configurations
    #endif
    
    
    }
    
    /*********************************************************************
     * @fn      performPeriodicTask
     *
     * @brief   Perform a periodic application task. This function gets
     *          called every five seconds as a result of the SBP_PERIODIC_EVT
     *          OSAL event. In this example, the value of the third
     *          characteristic in the SimpleGATTProfile service is retrieved
     *          from the profile, and then copied into the value of the
     *          the fourth characteristic.
     *
     * @param   none
     *
     * @return  none
     */
    static void performPeriodicTask( void )
    {
      uint8 valueToCopy;
      uint8 stat;
    
      // Call to retrieve the value of the third characteristic in the profile
      stat = SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &valueToCopy);
    
      if( stat == SUCCESS )
      {
        /*
         * Call to set that value of the fourth characteristic in the profile. Note
         * that if notifications of the fourth characteristic have been enabled by
         * a GATT client device, then a notification will be sent every time this
         * function is called.
         */
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &valueToCopy);
      }
    }
    
    /*********************************************************************
     * @fn      simpleProfileChangeCB
     *
     * @brief   Callback from SimpleBLEProfile indicating a value change
     *
     * @param   paramID - parameter ID of the value that was changed.
     *
     * @return  none
     */
    static void simpleProfileChangeCB( uint8 paramID )
    {
      uint8 newValue;
    
      switch( paramID )
      {
        case SIMPLEPROFILE_CHAR1:
          SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue );
    
          #if (defined HAL_LCD) && (HAL_LCD == TRUE)
            HalLcdWriteStringValue( "Char 1:", (uint16)(newValue), 10,  HAL_LCD_LINE_3 );
          #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
    
          break;
    
        case SIMPLEPROFILE_CHAR3:
          SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &newValue );
    
          #if (defined HAL_LCD) && (HAL_LCD == TRUE)
            HalLcdWriteStringValue( "Char 3:", (uint16)(newValue), 10,  HAL_LCD_LINE_3 );
          #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
    
          break;
    
        default:
          // should not reach here!
          break;
      }
    }
    
    #if (defined HAL_LCD) && (HAL_LCD == TRUE)
    /*********************************************************************
     * @fn      bdAddr2Str
     *
     * @brief   Convert Bluetooth address to string. Only needed when
     *          LCD display is used.
     *
     * @return  none
     */
    char *bdAddr2Str( uint8 *pAddr )
    {
      uint8       i;
      char        hex[] = "0123456789ABCDEF";
      static char str[B_ADDR_STR_LEN];
      char        *pStr = str;
    
      *pStr++ = '0';
      *pStr++ = 'x';
    
      // Start from end of addr
      pAddr += B_ADDR_LEN;
    
      for ( i = B_ADDR_LEN; i > 0; i-- )
      {
        *pStr++ = hex[*--pAddr >> 4];
        *pStr++ = hex[*pAddr & 0x0F];
      }
    
      *pStr = 0;
    
      return str;
    }
    #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
    
    /*********************************************************************
    *********************************************************************/
    

     

  • Hi,

    Any advise?

    Thanks.

    Regards,

    RVG