ch573 cc2540 central 事件

转自:https://blog.csdn.net/xiaoleiacmer/article/details/42554605

 

 

BLE协议栈Central 工程中与广播连接建立的事件

        最近在改写BLE协议栈,需要实现实时发现周边从设备,并建立连接,获取RSSI的功能,没什么难度,但着实将各个事件好好看了一遍。曾经看到一位大神说相同的任务使用事件触发方式,不同的任务使用消息传递方式。冥冥之中突然来了感觉~

       BLE进行广播和建立连接的事件主要实现是在simpleBLECentralEventCB的回调函数中:

       所有使用到的变量都在联合体gapCentralRoleEvent_t中:

       

  1.  
    typedef union
  2.  
    {
  3.  
    gapEventHdr_t gap; //!< GAP_MSG_EVENT and status.
  4.  
    gapDeviceInitDoneEvent_t initDone; //!< GAP initialization done. 本机信息
  5.  
    gapDeviceInfoEvent_t deviceInfo;//!< Discovery device information event structure. 从机信息
  6.  
    gapDevDiscEvent_t discCmpl;//!< Discovery complete event structure.从机信息(全部)
  7.  
    gapEstLinkReqEvent_t linkCmpl; //!< Link complete event structure.
  8.  
    gapLinkUpdateEvent_t linkUpdate; //!< Link update event structure.
  9.  
    gapTerminateLinkEvent_t linkTerminate; //!< Link terminated event structure.
  10.  
    } gapCentralRoleEvent_t; // central.h


 

1 板级初始化事件:GAP_DEVICE_INIT_DONE_EVENT

  1.  
    case GAP_DEVICE_INIT_DONE_EVENT: // 初始化 // 是本机GAP层的初始化信息 在运行到GAPCentralRole_StartDevice函数被回调
  2.  
    {
  3.  
    LCD_WRITE_STRING( bdAddr2Str( pEvent->initDone.devAddr ), HAL_LCD_LINE_2 );
  4.  
    }
  5.  
    break;

详解:

  1.  
    作用:进行GAP层的初始化
  2.  
    详解:
  3.  
     
  4.  
    /**
  5.  
    * GAP_DEVICE_INIT_DONE_EVENT message format. This message is sent to the
  6.  
    * app when the Device Initialization is done [initiated by calling
  7.  
    * GAP_DeviceInit()]. gap.h
  8.  
    */
  9.  
    typedef struct
  10.  
    {
  11.  
    osal_event_hdr_t hdr; //!< GAP_MSG_EVENT and status
  12.  
    uint8 opcode; //!< GAP_DEVICE_INIT_DONE_EVENT
  13.  
    uint8 devAddr[B_ADDR_LEN]; //!< Device's BD_ADDR 本机的MAC地址
  14.  
    uint16 dataPktLen; //!< HC_LE_Data_Packet_Length 初始化长度为27
  15.  
    uint8 numDataPkts; //!< HC_Total_Num_LE_Data_Packets 初始化为4
  16.  
    } gapDeviceInitDoneEvent_t;


 

 

 

2 搜索从机事件(每搜到一个从机进入一次):GAP_DEVICE_INFO_EVENT

  1.  
    case GAP_DEVICE_INFO_EVENT: //过程量 从机的广播数据 搜索到之后添加到从机列表中
  2.  
    {
  3.  
    char temprssiarg[4]={0};
  4.  
    // if filtering device discovery results based on service UUID
  5.  
    if ( DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE )
  6.  
    {
  7.  
    if ( simpleBLEFindSvcUuid( SIMPLEPROFILE_SERV_UUID,
  8.  
    pEvent->deviceInfo.pEvtData,
  9.  
    pEvent->deviceInfo.dataLen ) )
  10.  
    {
  11.  
    simpleBLEAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType );
  12.  
     
  13.  
    }
  14.  
    }
  15.  
    }

作用:  每搜索到一个从设备时,都会进入该事件,并使用simpleBLEAddDeviceInfo加入到扫描结果列表中,deviceInfo.addr中储存着当前正被扫描到从设备地址,扫描从机的数量(simpleBLEScanRes)在simpleBLEAddDeviceInfo中增加!

详解:

  1.  
    **
  2.  
    * GAP_DEVICE_INFO_EVENT message format. This message is sent to the
  3.  
    * app during a Device Discovery Request, when a new advertisement or scan
  4.  
    * response is received. //gap.h
  5.  
    */
  6.  
    typedef struct
  7.  
    {
  8.  
    osal_event_hdr_t hdr; //!< GAP_MSG_EVENT and status
  9.  
    uint8 opcode; //!< GAP_DEVICE_INFO_EVENT
  10.  
    uint8 eventType; //!< Advertisement Type: @ref GAP_ADVERTISEMENT_TYPE_DEFINES
  11.  
    uint8 addrType; //!< address type: @ref GAP_ADDR_TYPE_DEFINES
  12.  
    uint8 addr[B_ADDR_LEN]; //!< Address of the advertisement or SCAN_RSP 从机MAC地址
  13.  
    int8 rssi; //!< Advertisement or SCAN_RSP RSSI 从机RSSI值
  14.  
    uint8 dataLen; //!< Length (in bytes) of the data field (evtData)
  15.  
    uint8 *pEvtData; //!< Data field of advertisement or SCAN_RSP
  16.  
    } gapDeviceInfoEvent_t;


 

 

 

 

3 搜索从机事件(搜索完毕时进入一次):GAP_DEVICE_DISCOVERY_EVENT

  1.  
    case GAP_DEVICE_DISCOVERY_EVENT: //结果量 // 搜索周边广播从机事件 显示搜索结果
  2.  
    {
  3.  
    // discovery complete
  4.  
    simpleBLEScanning = FALSE;
  5.  
    // if not filtering device discovery results based on service UUID
  6.  
    if ( DEFAULT_DEV_DISC_BY_SVC_UUID == FALSE )
  7.  
    {
  8.  
    // Copy results
  9.  
    simpleBLEScanRes = pEvent->discCmpl.numDevs;
  10.  
    osal_memcpy( simpleBLEDevList, pEvent->discCmpl.pDevList,
  11.  
    (sizeof( gapDevRec_t ) * pEvent->discCmpl.numDevs) );
  12.  
    }
  13.  
    char temp_simpleBLEScanRes[10]="Find : \n";
  14.  
     
  15.  
    FineDeviceNum=FindDeviceNum=simpleBLEScanRes;//***************************************************************** 扫描的数量
  16.  
     
  17.  
    temp_simpleBLEScanRes[8] = simpleBLEScanRes+'0';
  18.  
     
  19.  
    Uart0Send_String(temp_simpleBLEScanRes,10);//***********************************
  20.  
    /******************************************************************************************************/
  21.  
     
  22.  
    // initialize scan index to last device
  23.  
    simpleBLEScanIdx = simpleBLEScanRes;
  24.  
    IsPeriodicStart=1;
  25.  
     
  26.  
     
  27.  
    }
  28.  
    break;

作用:程序运行到这一步可以知道当前有多少个已经被扫描到的从设备,且它们的地址被保存到simpleBLEDevList里。

搜索结果列表 // Scan result list    

static gapDevRec_t simpleBLEDevList[DEFAULT_MAX_SCAN_RES]; // 扫描结果列表

详解:

  1.  
    /**
  2.  
    * Type of device discovery (Scan) to perform.
  3.  
    */
  4.  
    typedef struct
  5.  
    {
  6.  
    uint8 eventType;
  7.  
    //!< Indicates advertising event type used by the advertiser: @ref GAP_ADVERTISEMENT_TYPE_DEFINES
  8.  
    uint8 addrType; //!< Address Type: @ref GAP_ADDR_TYPE_DEFINES
  9.  
    uint8 addr[B_ADDR_LEN]; //!< Device's Address // 储存着当前搜索到的设备地址 最大可见扫描数量8
  10.  
    } gapDevRec_t; 搜索结果结构体
  11.  
    /**
  12.  
    * GAP_DEVICE_DISCOVERY_EVENT message format. This message is sent to the
  13.  
    * Application after a scan is performed.
  14.  
    */
  15.  
    typedef struct
  16.  
    {
  17.  
    osal_event_hdr_t hdr; //!< GAP_MSG_EVENT and status
  18.  
    uint8 opcode; //!< GAP_DEVICE_DISCOVERY_EVENT
  19.  
    uint8 numDevs; //!< Number of devices found during scan
  20.  
    gapDevRec_t *pDevList; //!< array of device records
  21.  
    } gapDevDiscEvent_t; 搜索结果事件结构体


Note: 需要知道一个从设备的RSSI值,只需知道从设备的MAC地址信息在simpleBLEDevList中即可!

 

4 连接建立事件:GAP_LINK_ESTABLISHED_EVENT

本事件里开起一个定时器,事件连接超时断开!

  1.  
    case GAP_LINK_ESTABLISHED_EVENT:
  2.  
    {
  3.  
    if ( pEvent->gap.hdr.status == SUCCESS )
  4.  
    {
  5.  
    simpleBLEState = BLE_STATE_CONNECTED;
  6.  
    simpleBLEConnHandle = pEvent->linkCmpl.connectionHandle;
  7.  
    simpleBLEProcedureInProgress = TRUE;
  8.  
     
  9.  
    // If service discovery not performed initiate service discovery
  10.  
    if ( simpleBLECharHdl == 0 )
  11.  
    {
  12.  
    osal_start_timerEx( simpleBLETaskId, START_DISCOVERY_EVT, DEFAULT_SVC_DISCOVERY_DELAY );
  13.  
    }
  14.  
    // SerialPrintString("Connected: ");
  15.  
    // Uart0Send_String( bdAddr2Str( pEvent->linkCmpl.devAddr ), sizeof(bdAddr2Str( pEvent->linkCmpl.devAddr )));
  16.  
    // for(int i=0;i<10000;i++);
  17.  
    for(int i=0;i<10000;i++);
  18.  
    LCD_WRITE_STRING( bdAddr2Str( pEvent->linkCmpl.devAddr ), HAL_LCD_LINE_2 );
  19.  
    for(int i=0;i<10000;i++);
  20.  
    // Uart0Send_String("12222",5);
  21.  
    //LCD_WRITE_STRING( "Connected", HAL_LCD_LINE_1 );
  22.  
    /*********** 2015年1月8日 不间断的搜寻周围的从设备 ************** 建立连接后读取当前的RSSI 值***********************************************************************************************/
  23.  
     
  24.  
    if ( simpleBLEState == BLE_STATE_CONNECTED )
  25.  
    {
  26.  
    if ( !simpleBLERssi )
  27.  
    {
  28.  
    simpleBLERssi = TRUE;
  29.  
    GAPCentralRole_StartRssi( simpleBLEConnHandle, DEFAULT_RSSI_PERIOD );
  30.  
    }
  31.  
    else
  32.  
    {
  33.  
    simpleBLERssi = FALSE;
  34.  
    GAPCentralRole_CancelRssi( simpleBLEConnHandle );
  35.  
    LCD_WRITE_STRING( "RSSI Cancelled", HAL_LCD_LINE_1 );
  36.  
    // SerialPrintString("RSSI Cancelled\r\n");
  37.  
    }
  38.  
    }
  39.  
    /****************************************************************************************************************/
  40.  
    }
  41.  
    else
  42.  
    {
  43.  
    simpleBLEState = BLE_STATE_IDLE;
  44.  
    simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
  45.  
    simpleBLERssi = FALSE;
  46.  
    simpleBLEDiscState = BLE_DISC_STATE_IDLE;
  47.  
     
  48.  
    LCD_WRITE_STRING( "Connect Failed", HAL_LCD_LINE_1 );
  49.  
    // SerialPrintString("Connect Failed: ");
  50.  
    LCD_WRITE_STRING_VALUE( "Reason:", pEvent->gap.hdr.status, 10, HAL_LCD_LINE_2 );
  51.  
     
  52.  
     
  53.  
    GAPCentralRole_CancelRssi( simpleBLEConnHandle );
  54.  
    /*********** 2015年1月8日 不间断的搜寻周围的从设备 ************** 连接错误 主机从新进行搜索***********************************************************************************************/
  55.  
     
  56.  
    simpleBLEScanning = TRUE;
  57.  
    simpleBLEScanRes = 0;
  58.  
     
  59.  
    LCD_WRITE_STRING( "Discovering...", HAL_LCD_LINE_1 );
  60.  
    LCD_WRITE_STRING( "", HAL_LCD_LINE_2 );
  61.  
     
  62.  
    GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE,
  63.  
    DEFAULT_DISCOVERY_ACTIVE_SCAN,
  64.  
    DEFAULT_DISCOVERY_WHITE_LIST );
  65.  
    /*****************************************************************************************************/
  66.  
    // SerialPrintValue("Reason:", pEvent->gap.hdr.status,10);
  67.  
    }
  68.  
    }
  69.  
    break;
  1.  
    详解:
  2.  
    /**
  3.  
    * GAP_LINK_ESTABLISHED_EVENT message format. This message is sent to the app
  4.  
    * when the link request is complete.<BR>
  5.  
    * <BR>
  6.  
    * For an Observer, this message is sent to complete the Establish Link Request.<BR>
  7.  
    * For a Peripheral, this message is sent to indicate that a link has been created.
  8.  
    */
  9.  
    typedef struct
  10.  
    {
  11.  
    osal_event_hdr_t hdr; //!< GAP_MSG_EVENT and status
  12.  
    uint8 opcode; //!< GAP_LINK_ESTABLISHED_EVENT
  13.  
    uint8 devAddrType; //!< Device address type: @ref GAP_ADDR_TYPE_DEFINES
  14.  
    uint8 devAddr[B_ADDR_LEN]; //!< Device address of link
  15.  
    uint16 connectionHandle; //!< Connection Handle from controller used to ref the device
  16.  
    uint16 connInterval; //!< Connection Interval
  17.  
    uint16 connLatency; //!< Conenction Latency
  18.  
    uint16 connTimeout; //!< Connection Timeout
  19.  
    uint8 clockAccuracy; //!< Clock Accuracy
  20.  
    } gapEstLinkReqEvent_t;



5 连接超时事件 : GAP_LINK_TERMINATED_EVENT

  1.  
    case GAP_LINK_TERMINATED_EVENT:
  2.  
    {
  3.  
    simpleBLEState = BLE_STATE_IDLE;
  4.  
    simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
  5.  
    simpleBLERssi = FALSE;
  6.  
    simpleBLEDiscState = BLE_DISC_STATE_IDLE;
  7.  
    simpleBLECharHdl = 0;
  8.  
    simpleBLEProcedureInProgress = FALSE;
  9.  
     
  10.  
    LCD_WRITE_STRING( "Disconnected", HAL_LCD_LINE_1 );
  11.  
    // SerialPrintString("Disconnected: ");
  12.  
    LCD_WRITE_STRING_VALUE( "Reason:", pEvent->linkTerminate.reason,
  13.  
    10, HAL_LCD_LINE_2 );
  14.  
     
  15.  
     
  16.  
    // SerialPrintValue("Reason:", pEvent->linkTerminate.reason,10);
  17.  
    }


 

 

 

 

 

 


 


 

 

  

 

    

posted @ 2021-01-28 13:42  XZHDJH  阅读(426)  评论(0)    收藏  举报