cc2540 连接时操作的函数

很多人会对cc2540的一些参数不知道怎么操作,例如哪里判断连接成功/失败?哪里操作断开连接等等。.下面以SimpleBLEPeripheral工程为例子吧。

BLE广播的使能

uint8 initial_advertising_enable = TRUE;

GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );

在SimpleBLEPeripheral.c文件下面的这段代码是管理初始化的时候是否进行广播的函数;你可以找到peripheral.c中变量gapRole_AdvEnabled。每次调用这个函数其实都是写入到这个变量,而连接结束后是根据这个变量来决定要不要重新启动广播的。

在peripheral.c的函数中static void gapRole_ProcessGAPMsg(gapEventHdr_t * pMsg)中可以找到:

case GAP_LINK_ESTABLISHED_EVENT:

{

...

gapRole_AdvEnabled = FALSE;  //建立连接后,将其强制为FALSE。

}

case GAP_LINK_TERMINATED_EVENT://断开连接

{

...

if ( gapRole_AdvEnabled)      //如果最后的状态为TRUE,则不重新启动广播
{
// 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.//如果为FALSE
{
gapRole_AdvEnabled = TRUE;
VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT);//启动广播
}

}

以上便是操作BLE广播的操作

BLE连接断开函数

在peripheral.c文件中,当ble连接成功的时候会调用gapRole_ProcessGAPMsg()下面的GAP_LINK_ESTABLISHED_EVENT事件:

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;

同样道理,当ble断开的时候就会调用GAP_LINK_TERMINATED_EVENT事件:

 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;
        UserRight=0;//复位用户权限
        isLogin=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;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值