1. 程式人生 > >CC2640R2F 連線引數更新

CC2640R2F 連線引數更新

-----------------------------------------------------------------simple_peripheral.c  (初始化)

// After the connection is formed, the peripheral waits until the central
// device asks for its preferred connection parameters
#define DEFAULT_ENABLE_UPDATE_REQUEST         GAPROLE_LINK_PARAM_UPDATE_WAIT_REMOTE_PARAMS

注意:需要特別注意DEFAULT_ENABLE_UPDATE_REQUEST。需要在其 定義處修改成GAPROLE_LINK_PARAM_UPDATE_INITIATE_BOTH_PARAMS。這樣雙方都可以啟動連線引數更新過程


    uint8_t enableUpdateRequest = DEFAULT_ENABLE_UPDATE_REQUEST;
    uint16_t desiredMinInterval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
    uint16_t desiredMaxInterval = DEFAULT_DESIRED_MAX_CONN_INTERVAL

;
    uint16_t desiredSlaveLatency = DEFAULT_DESIRED_SLAVE_LATENCY;

    uint16_t desiredConnTimeout = DEFAULT_DESIRED_CONN_TIMEOUT;

    GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE, sizeof(uint8_t), &enableUpdateRequest);
    GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desiredMinInterval);
    GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desiredMaxInterval);
    GAPRole_SetParameter(GAPROLE_SLAVE_LATENCY, sizeof(uint16_t), &desiredSlaveLatency);
    GAPRole_SetParameter(GAPROLE_TIMEOUT_MULTIPLIER, sizeof(uint16_t), &desiredConnTimeout);

-----------------------------------------------------------------main.c

  GAPRole_createTask();

-----------------------------------------------------------------peripheral.c

Task_construct(&gapRoleTask, gapRole_taskFxn, &taskParams, NULL);

-----------------------------------------------------------------peripheral.c

 gapRole_init();

-----------------------------------------------------------------peripheral.c  

  Util_constructClock(&startUpdateClock, gapRole_clockHandler,   0, 0, false, START_CONN_UPDATE_EVT);


-----------------------------------------------------------------peripheral.c   (觸發)

static void gapRole_taskFxn(UArg a0, UArg a1)
{
  // Initialize profile
  gapRole_init();
  // Profile main loop
  for (;;)

  {

    if (events)
    {
      ICall_EntityID dest;
      ICall_ServiceEnum src;
      ICall_HciExtEvt *pMsg = NULL;


      if (ICall_fetchServiceMsg(&src, &dest,
                                (void **)&pMsg) == ICALL_ERRNO_SUCCESS)
      {
        if ((src == ICALL_SERVICE_CLASS_BLE) && (dest == selfEntity))
        {
          ICall_Stack_Event *pEvt = (ICall_Stack_Event *)pMsg;


          // Check for BLE stack events first
          if (pEvt->signature == 0xffff)
          {
            if (pEvt->event_flag & GAP_EVENT_SIGN_COUNTER_CHANGED)
            {
              // Sign counter changed, save it to NV
              VOID osal_snv_write(BLE_NVID_SIGNCOUNTER, sizeof(uint32_t),
                                  &gapRole_signCounter);
            }
          }
          else
          {
            // Process inter-task message
            gapRole_processStackMsg((ICall_Hdr *)pMsg);
          }
        }
        if (pMsg)
        {
          ICall_freeMsg(pMsg);
        }

      }

    }

    ....

    }

    ....

}

static void gapRole_processStackMsg(ICall_Hdr *pMsg)

{

  switch (pMsg->event)
  {
    case GAP_MSG_EVENT:
      gapRole_processGAPMsg((gapEventHdr_t *)pMsg);

      break;

    ......

    }

    ......

}

static void gapRole_processGAPMsg(gapEventHdr_t *pMsg)

{

.....

    case GAP_LINK_ESTABLISHED_EVENT:// 連線成功建立之後底層返回的事件
      {
        gapEstLinkReqEvent_t *pPkt = (gapEstLinkReqEvent_t *)pMsg;


        if (pPkt->hdr.status == SUCCESS)
        {
          VOID memcpy(gapRole_ConnectedDevAddr, pPkt->devAddr, B_ADDR_LEN);
          gapRole_ConnectionHandle = pPkt->connectionHandle;
          gapRole_state = GAPROLE_CONNECTED;


          // Store connection information  儲存連線剛建立時的連線引數
          gapRole_ConnInterval = pPkt->connInterval;
          gapRole_ConnSlaveLatency = pPkt->connLatency;
          gapRole_ConnTimeout = pPkt->connTimeout;
          gapRole_ConnectedDevAddrType = pPkt->devAddrType;


          // 檢測更新連線引數請求是否被使能
          // Check whether update parameter request is enabled
          
          if ((gapRole_updateConnParams.paramUpdateEnable ==
               GAPROLE_LINK_PARAM_UPDATE_INITIATE_BOTH_PARAMS) ||
              (gapRole_updateConnParams.paramUpdateEnable ==
               GAPROLE_LINK_PARAM_UPDATE_INITIATE_APP_PARAMS))
          {
            // Get the minimum time upon connection establishment before the
            // peripheral can start a connection update procedure.
            // 獲取設定的時間間隔,
            uint16_t timeout = GAP_GetParamValue(TGAP_CONN_PAUSE_PERIPHERAL);
            //從機將在連線建立之後延時至少該時間間隔之後觸發連線引數更新事件  // ----------->
            Util_restartClock(&startUpdateClock, timeout*1000);
          }
          // Notify the Bond Manager to the connection
          VOID GAPBondMgr_LinkEst(pPkt->devAddrType, pPkt->devAddr,
                                  pPkt->connectionHandle, GAP_PROFILE_PERIPHERAL);
        }
        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;

.....

}


-----------------------------------------------------------------peripheral.c   (執行)

static uint16_t gapRole_ConnInterval = 0;
static uint16_t gapRole_ConnSlaveLatency = 0;
static uint16_t gapRole_ConnTimeout = 0;
static uint8_t  gapRole_ConnectedDevAddrType = 0;

static uint8_t  gapRole_ConnTermReason = 0;

**********

#define DEFAULT_MIN_CONN_INTERVAL     0x0006  // 100 milliseconds

#define DEFAULT_MAX_CONN_INTERVAL     0x0C80  // 4 seconds

#define MIN_SLAVE_LATENCY             0
#define MAX_SLAVE_LATENCY             500

**********

// Connection parameter update parameters.
static gapRole_updateConnParams_t gapRole_updateConnParams =
{
  // Default behavior is to accept the remote device's request until application changes local parameters.
  .paramUpdateEnable = GAPROLE_LINK_PARAM_UPDATE_WAIT_REMOTE_PARAMS,
  .minConnInterval = DEFAULT_MIN_CONN_INTERVAL,
  .maxConnInterval = DEFAULT_MAX_CONN_INTERVAL,
  .slaveLatency = MIN_SLAVE_LATENCY,
  .timeoutMultiplier = DEFAULT_TIMEOUT_MULTIPLIER

};

**********

      if (events & START_CONN_UPDATE_EVT)
      {
        // Start connection update procedure
        gapRole_startConnUpdate(GAPROLE_NO_ACTION, &gapRole_updateConnParams);
      }

-----------------------------------------------------------------peripheral.c  

    linkParams.connectionHandle = gapRole_ConnectionHandle;
    linkParams.intervalMin = pConnParams->minConnInterval;
    linkParams.intervalMax = pConnParams->maxConnInterval;
    linkParams.connLatency = pConnParams->slaveLatency;
    linkParams.connTimeout = pConnParams->timeoutMultiplier;

    status = GAP_UpdateLinkParamReq( &linkParams );

    if(status == SUCCESS)
    {
      paramUpdateNoSuccessOption = handleFailure;
      // Let's wait either for L2CAP Connection Parameters Update Response or
      // for Controller to update connection parameters

      Util_restartClock(&updateTimeoutClock, timeout);

    }



-----------------------------------------------------------------

-----------------------------------------------------------------