NORDIC softDevice 蓝牙协议栈初始化程序分析(蓝牙主机,ble_central)

基于 NORDIC softDevice 蓝牙协议栈 S132

蓝牙角色: 机,ble_central


应用主函数 main() 中必须的函数:

    ble_stack_init();
    gatt_init();
    db_discovery_init();
    scan_start();

int main(void)
{
    // Initialize.
    log_init();
    timer_init();
    leds_init();
    buttons_init();
    power_management_init();
    ble_stack_init();
    gatt_init();
    db_discovery_init();
    lbs_c_init();

    // Start execution.
    NRF_LOG_INFO("Blinky CENTRAL example started.");
    scan_start();

    // Turn on the LED to signal scanning.
    bsp_board_led_on(CENTRAL_SCANNING_LED);

    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}

ble_stack_init()

NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);  语句中, ble_evt_handler 为 BLE 事件回调函数 (handler for BLE events).

/**@brief Function for initializing the BLE stack.
 * @details Initializes the SoftDevice and the BLE event interrupts.
 */
static void ble_stack_init(void)
{
    ret_code_t err_code;

    err_code = nrf_sdh_enable_request();
    APP_ERROR_CHECK(err_code);

    // Configure the BLE stack using the default settings.
    // Fetch the start address of the application RAM.
    uint32_t ram_start = 0;
    err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
    APP_ERROR_CHECK(err_code);

    // Enable BLE stack.
    err_code = nrf_sdh_ble_enable(&ram_start);
    APP_ERROR_CHECK(err_code);

    // Register a handler for BLE events.
    NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
}

BLE 事件回调函数,ble_evt_handler

/**@brief Function for handling BLE events.
 * @param[in]   p_ble_evt   Bluetooth stack event.
 * @param[in]   p_context   Unused.
 */
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    ret_code_t err_code;

    // For readability.
    ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;

    switch (p_ble_evt->header.evt_id)
    {
        // Upon connection, check which peripheral has connected (HR or RSC), initiate DB
        // discovery, update LEDs status and resume scanning if necessary. */
        case BLE_GAP_EVT_CONNECTED:
        {
            NRF_LOG_INFO("Connected.");
            err_code = ble_lbs_c_handles_assign(&m_ble_lbs_c, p_gap_evt->conn_handle, NULL);
            APP_ERROR_CHECK(err_code);

            err_code = ble_db_discovery_start(&m_db_disc, p_gap_evt->conn_handle);
            APP_ERROR_CHECK(err_code);

            // Update LEDs status, and check if we should be looking for more
            // peripherals to connect to.
            bsp_board_led_on(CENTRAL_CONNECTED_LED);
            bsp_board_led_off(CENTRAL_SCANNING_LED);
        } break;

        // Upon disconnection, reset the connection handle of the peer which disconnected, update
        // the LEDs status and start scanning again.
        case BLE_GAP_EVT_DISCONNECTED:
        {
            NRF_LOG_INFO("Disconnected.");
            scan_start();
        } break;

        case BLE_GAP_EVT_ADV_REPORT:
        {
            on_adv_report(&p_gap_evt->params.adv_report);
        } break;

        case BLE_GAP_EVT_TIMEOUT:
        {
            // We have not specified a timeout for scanning, so only connection attemps can timeout.
            if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
            {
                NRF_LOG_DEBUG("Connection request timed out.");
            }
        } break;

        case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
        {
            // Accept parameters requested by peer.
            err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle,
                                        &p_gap_evt->params.conn_param_update_request.conn_params);
            APP_ERROR_CHECK(err_code);
        } break;

        case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
        {
            NRF_LOG_DEBUG("PHY update request.");
            ble_gap_phys_t const phys =
            {
                .rx_phys = BLE_GAP_PHY_AUTO,
                .tx_phys = BLE_GAP_PHY_AUTO,
            };
            err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
            APP_ERROR_CHECK(err_code);
        } break;

        case BLE_GATTC_EVT_TIMEOUT:
        {
            // Disconnect on GATT Client timeout event.
            NRF_LOG_DEBUG("GATT Client Timeout.");
            err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
        } break;

        case BLE_GATTS_EVT_TIMEOUT:
        {
            // Disconnect on GATT Server timeout event.
            NRF_LOG_DEBUG("GATT Server Timeout.");
            err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
        } break;

        default:
            // No implementation needed.
            break;
    }
}

ble_evt_handler 回调函数中,事件 BLE_GAP_EVT_ADV_REPORT 处理函数 on_adv_report(), 用于匹配的蓝牙联结; 

/**@brief Function for handling the advertising report BLE event.
 *
 * @param[in] p_adv_report  Advertising report from the SoftDevice.
 */
static void on_adv_report(ble_gap_evt_adv_report_t const * p_adv_report)
{
    ret_code_t err_code;

    if (ble_advdata_name_find(p_adv_report->data.p_data,
                              p_adv_report->data.len,
                              m_target_periph_name))
    {
        // Name is a match, initiate connection.
        err_code = sd_ble_gap_connect(&p_adv_report->peer_addr,
                                      &m_scan_params,
                                      &m_connection_param,
                                      APP_BLE_CONN_CFG_TAG);
        APP_ERROR_CHECK(err_code);
    }
    else
    {
        err_code = sd_ble_gap_scan_start(NULL, &m_scan_buffer);
        APP_ERROR_CHECK(err_code);
    }
}

BLE GAP 事件列表 (GAP Event IDs.);  (BLE 事件回调函数 ble_evt_handler 中常处理的事件)

enum BLE_GAP_EVTS

GAP Event IDs. IDs that uniquely identify an event coming from the stack to the application.

Enumerator
BLE_GAP_EVT_CONNECTED 

GAP BLE Event base.

BLE_GAP_EVT_DISCONNECTED 

GAP BLE Event base. Disconnected from peer. 
See ble_gap_evt_disconnected_t.

BLE_GAP_EVT_CONN_PARAM_UPDATE 

GAP BLE Event base. Connection Parameters updated. 
See ble_gap_evt_conn_param_update_t.

BLE_GAP_EVT_SEC_PARAMS_REQUEST 

GAP BLE Event base. Request to provide security parameters. 
Reply with sd_ble_gap_sec_params_reply
See ble_gap_evt_sec_params_request_t.

BLE_GAP_EVT_SEC_INFO_REQUEST 

GAP BLE Event base. Request to provide security information. 
Reply with sd_ble_gap_sec_info_reply
See ble_gap_evt_sec_info_request_t.

BLE_GAP_EVT_PASSKEY_DISPLAY 

GAP BLE Event base. Request to display a passkey to the user. 
In LESC Numeric Comparison, reply with sd_ble_gap_auth_key_reply
See ble_gap_evt_passkey_display_t.

BLE_GAP_EVT_KEY_PRESSED 

GAP BLE Event base. Notification of a keypress on the remote device.
See ble_gap_evt_key_pressed_t

BLE_GAP_EVT_AUTH_KEY_REQUEST 

GAP BLE Event base. Request to provide an authentication key. 
Reply with sd_ble_gap_auth_key_reply
See ble_gap_evt_auth_key_request_t.

BLE_GAP_EVT_LESC_DHKEY_REQUEST 

GAP BLE Event base. Request to calculate an LE Secure Connections DHKey. 
Reply with sd_ble_gap_lesc_dhkey_reply
See ble_gap_evt_lesc_dhkey_request_t

BLE_GAP_EVT_AUTH_STATUS 

GAP BLE Event base. Authentication procedure completed with status. 
See ble_gap_evt_auth_status_t.

BLE_GAP_EVT_CONN_SEC_UPDATE 

GAP BLE Event base. Connection security updated. 
See ble_gap_evt_conn_sec_update_t.

BLE_GAP_EVT_TIMEOUT 

GAP BLE Event base. Timeout expired. 
See ble_gap_evt_timeout_t.

BLE_GAP_EVT_RSSI_CHANGED 

GAP BLE Event base. RSSI report. 
See ble_gap_evt_rssi_changed_t.

BLE_GAP_EVT_ADV_REPORT 

GAP BLE Event base. Advertising report. 
See ble_gap_evt_adv_report_t.

BLE_GAP_EVT_SEC_REQUEST 

GAP BLE Event base. Security Request. 
See ble_gap_evt_sec_request_t.

BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST 

GAP BLE Event base. Connection Parameter Update Request. 
Reply with sd_ble_gap_conn_param_update
See ble_gap_evt_conn_param_update_request_t.

BLE_GAP_EVT_SCAN_REQ_REPORT 

GAP BLE Event base. Scan request report. 
See ble_gap_evt_scan_req_report_t.

BLE_GAP_EVT_PHY_UPDATE_REQUEST 

GAP BLE Event base. PHY Update Request. 
Reply with sd_ble_gap_phy_update
See ble_gap_evt_phy_update_request_t.

BLE_GAP_EVT_PHY_UPDATE 

GAP BLE Event base. PHY Update Procedure is complete. 
See ble_gap_evt_phy_update_t.

BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST 

GAP BLE Event base. Data Length Update Request. 
Reply with sd_ble_gap_data_length_update.
See ble_gap_evt_data_length_update_request_t.

BLE_GAP_EVT_DATA_LENGTH_UPDATE 

GAP BLE Event base. LL Data Channel PDU payload length updated. 
See ble_gap_evt_data_length_update_t.

BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT 

GAP BLE Event base. Channel survey report. 
See ble_gap_evt_qos_channel_survey_report_t.

BLE_GAP_EVT_ADV_SET_TERMINATED 

GAP BLE Event base. Advertising set terminated. 
See ble_gap_evt_adv_set_terminated_t.

BLE GATT Server 事件列表 (GATT Server Event IDs.)(BLE 事件回调函数 ble_evt_handler 中常处理的事件)

enum BLE_GATTS_EVTS

GATT Server Event IDs.

Enumerator
BLE_GATTS_EVT_WRITE 

GATTS BLE Event base. Write operation performed. 
See ble_gatts_evt_write_t.

BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST 

Read/Write Authorization request. 
Reply with sd_ble_gatts_rw_authorize_reply
See ble_gatts_evt_rw_authorize_request_t.

BLE_GATTS_EVT_SYS_ATTR_MISSING 

A persistent system attribute access is pending. 
Respond with sd_ble_gatts_sys_attr_set
See ble_gatts_evt_sys_attr_missing_t.

BLE_GATTS_EVT_HVC 

Handle Value Confirmation. 
See ble_gatts_evt_hvc_t.

BLE_GATTS_EVT_SC_CONFIRM 

Service Changed Confirmation. 
No additional event structure applies.

BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST 

Exchange MTU Request. 
Reply with sd_ble_gatts_exchange_mtu_reply
See ble_gatts_evt_exchange_mtu_request_t.

BLE_GATTS_EVT_TIMEOUT 

Peer failed to respond to an ATT request in time. 
See ble_gatts_evt_timeout_t.

BLE_GATTS_EVT_HVN_TX_COMPLETE 

Handle Value Notification transmission complete. 
See ble_gatts_evt_hvn_tx_complete_t.

BLE GATT Client 事件列表 (GATT Client Event IDs.)(BLE 事件回调函数 ble_evt_handler 中常处理的事件)

enum BLE_GATTC_EVTS

GATT Client Event IDs.

Enumerator
BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP 

GATTC BLE Event base. Primary Service Discovery Response event. 
See ble_gattc_evt_prim_srvc_disc_rsp_t.

BLE_GATTC_EVT_REL_DISC_RSP 

Relationship Discovery Response event. 
See ble_gattc_evt_rel_disc_rsp_t.

BLE_GATTC_EVT_CHAR_DISC_RSP 

Characteristic Discovery Response event. 
See ble_gattc_evt_char_disc_rsp_t.

BLE_GATTC_EVT_DESC_DISC_RSP 

Descriptor Discovery Response event. 
See ble_gattc_evt_desc_disc_rsp_t.

BLE_GATTC_EVT_ATTR_INFO_DISC_RSP 

Attribute Information Response event. 
See ble_gattc_evt_attr_info_disc_rsp_t.

BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP 

Read By UUID Response event. 
See ble_gattc_evt_char_val_by_uuid_read_rsp_t.

BLE_GATTC_EVT_READ_RSP 

Read Response event. 
See ble_gattc_evt_read_rsp_t.

BLE_GATTC_EVT_CHAR_VALS_READ_RSP 

Read multiple Response event. 
See ble_gattc_evt_char_vals_read_rsp_t.

BLE_GATTC_EVT_WRITE_RSP 

Write Response event. 
See ble_gattc_evt_write_rsp_t.

BLE_GATTC_EVT_HVX 

Handle Value Notification or Indication event. 
Confirm indication with sd_ble_gattc_hv_confirm
See ble_gattc_evt_hvx_t.

BLE_GATTC_EVT_EXCHANGE_MTU_RSP 

Exchange MTU Response event. 
See ble_gattc_evt_exchange_mtu_rsp_t.

BLE_GATTC_EVT_TIMEOUT 

Timeout event. 
See ble_gattc_evt_timeout_t.

BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE 

Write without Response transmission complete. 
See ble_gattc_evt_write_cmd_tx_complete_t.


gatt_init()

/**@brief Function for initializing the GATT module.
 */
static void gatt_init(void)
{
    ret_code_t err_code = nrf_ble_gatt_init(&m_gatt, NULL);
    APP_ERROR_CHECK(err_code);
}

db_discovery_init()

/**@brief Database discovery initialization.
 */
static void db_discovery_init(void)
{
    ret_code_t err_code = ble_db_discovery_init(db_disc_handler);
    APP_ERROR_CHECK(err_code);
}

scan_start()

/**@brief Function to start scanning.
 */
static void scan_start(void)
{
    ret_code_t err_code;

    (void) sd_ble_gap_scan_stop();

    err_code = sd_ble_gap_scan_start(&m_scan_params, &m_scan_buffer);
    APP_ERROR_CHECK(err_code);

    bsp_board_led_off(CENTRAL_CONNECTED_LED);
    bsp_board_led_on(CENTRAL_SCANNING_LED);
}

猜你喜欢

转载自blog.csdn.net/weixin_42396877/article/details/81258409