Using scanning causes program crashes

static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context)
{
  ret_code_t err_code;
  NRF_LOG_INFO("Received BLE event: %d", p_ble_evt->header.evt_id);
  switch (p_ble_evt->header.evt_id)
  {

  case BLE_GAP_EVT_ADV_REPORT: // 29
  {
    NRF_LOG_INFO("Report_I");
    on_adv_report(&(p_ble_evt->evt.gap_evt));
    break;
  }

/**@brief Function called on BLE_GAP_EVT_ADV_REPORT event.
 *
 * @details Create a connection if received advertising packet corresponds to desired BLE device.
 *
 * @param[in] p_ble_gap_evt Advertising Report Event.
 */
void on_adv_report(const ble_gap_evt_t *const p_ble_gap_evt)
{
  uint32_t err_code = 0;
  uint8_t str[STRING_BUFFER_SIZE] = {0};
  NRF_LOG_INFO("Adv Report.");

  // Log the Bluetooth device address of advertisement packet received.
  // ble_address_to_string_convert(p_ble_gap_evt->params.adv_report.peer_addr, str);
  const uint8_t *address = p_ble_gap_evt->params.adv_report.peer_addr.addr;
  char _temp_str[250];
  memset(_temp_str, 0, 250);
  sprintf(_temp_str, "Advertising Report: Device Address: %02x%02x%02x%02x%02x%02x, ", address[5], address[4], address[3], address[2], address[1], address[0]);
  sprintf(_temp_str + strlen(_temp_str), "Addr Type: 0x%d, ", p_ble_gap_evt->params.adv_report.peer_addr.addr_type);
  sprintf(_temp_str + strlen(_temp_str), "Addr ID Peer: 0x%d, ", p_ble_gap_evt->params.adv_report.peer_addr.addr_id_peer);
  sprintf(_temp_str + strlen(_temp_str), "Device Rssi: %d\n", p_ble_gap_evt->params.adv_report.rssi);
  usb_printf_variadic("%s\r\n", _temp_str);
  // usb_printf_variadic("Advertising Data: %s", p_ble_gap_evt->params.adv_report.data.p_data);
  NRF_LOG_INFO("SCAN_START_1");
  nrf_delay_us(100000);
  NRF_LOG_INFO("DELAY_END");
  err_code = sd_ble_gap_scan_start(NULL, &m_adv_report_buffer);
  NRF_LOG_INFO("SCAN_START_2");

  if (err_code != NRF_SUCCESS)
  {
    usb_printf_variadic("Error starting scan. Error code 0x%02X\r\n", err_code);
  }
  else
  {
    // //printf("Scan started\n");
    // usb_printf_variadic("GAP Scan Start\n");
  }
}

The above is my code.


[19:03:32.288]收←◆<info> app: SCAN_START_2
<info> app: Received BLE event: 29
<info> app: Report_I
<info> app: Adv Report.
<info> app: SCAN_START_1
<info> app: DELAY_END
<info> app: SCAN_START_2
<info> app: Received BLE event: 29
<info> app: Report_I
<info> app: Adv Report.
<info> app: SCAN_START_1
<info> app: DELAY_END
<info> app: SCAN_START_2

[19:03:32.443]收←◆<info> app: Received BLE event: 29

This is running log.

After scanning multiple times, my application may freeze, as shown in the log, and there will be no response after receiving the event.

Parents
  • Hi Genfeng, 

    How often do you see the issue ?
    I suspect that there could be a problem of the functions you call in the on_adv_report(). The context level of the event handler is at Softdevice low (level 4)


    There could be a chance that calling usb_printf_variadic() inside that context causing a problem. Please try to comment out usb_printf_variadic() and test printing out log on UART or RTT (or use GPIO) to see if you still see the problem.
    If usb_printf_variadic() actually a problem, you can consider using a semaphore or use software interrupt to reduce the context to main for example, before calling the funtion.  

  • void on_adv_report(const ble_gap_evt_t *const p_ble_gap_evt)
    {
      uint32_t err_code = 0;
      uint8_t str[STRING_BUFFER_SIZE] = {0};
    
      // Log the Bluetooth device address of advertisement packet received.
      // ble_address_to_string_convert(p_ble_gap_evt->params.adv_report.peer_addr, str);
      // const uint8_t *address = p_ble_gap_evt->params.adv_report.peer_addr.addr;
      // char _temp_str[250];
      // memset(_temp_str, 0, 250);
      // sprintf(_temp_str, "Advertising Report: Device Address: %02x%02x%02x%02x%02x%02x, ", address[5], address[4], address[3], address[2], address[1], address[0]);
      // sprintf(_temp_str + strlen(_temp_str), "Addr Type: 0x%d, ", p_ble_gap_evt->params.adv_report.peer_addr.addr_type);
      // sprintf(_temp_str + strlen(_temp_str), "Addr ID Peer: 0x%d, ", p_ble_gap_evt->params.adv_report.peer_addr.addr_id_peer);
      // sprintf(_temp_str + strlen(_temp_str), "Device Rssi: %d\n", p_ble_gap_evt->params.adv_report.rssi);
      // usb_printf_variadic("%s\r\n", _temp_str);
      // usb_printf_variadic("Advertising Data: %s", p_ble_gap_evt->params.adv_report.data.p_data);
      // memset(mp_data, 0, 255);
      // m_adv_report_buffer.len = BLE_GAP_SCAN_BUFFER_EXTENDED_MIN * sizeof(mp_data);
      // err_code = sd_ble_gap_scan_stop();
      // nrf_delay_us(100000);
    
      err_code = sd_ble_gap_scan_start(NULL, &m_adv_report_buffer);
      if (err_code != NRF_SUCCESS)
      {
        usb_printf_variadic("Error starting scan. Error code 0x%02X\r\n", err_code);
      }
      else
      {
        // //printf("Scan started\n");
        // usb_printf_variadic("GAP Scan Start\n");
      }
    }
    

    Ok, here is my new code, I disabled usb_printf_variadic() as you said, but the problem still exists.

    [22:43:04.034]收←◆<info> app: uart Rx_data:>SD:Scan_Start
    
    [22:43:04.092]收←◆<info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    
    [22:43:04.155]收←◆<info> app: Received BLE event: 29
    
    [22:43:04.186]收←◆<info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    
    [22:43:04.262]收←◆<info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    
    [22:43:04.299]收←◆<info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    
    [22:43:04.388]收←◆<info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    
    [22:43:04.462]收←◆<info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    
    [22:43:04.492]收←◆<info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    
    [22:43:04.555]收←◆<info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    
    [22:43:04.595]收←◆<info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    
    [22:43:04.657]收←◆<info> app: Received BLE event: 29
    <info> app: Received BLE event: 29
    
    [22:43:06.864]收←◆<info> app: uart Rx_data:>SD:Scan_Stop
    

  • Hi Genfeng, 

    Please provide your full code, how do you start scanning ? Do you use the nrf_ble_scan module ? 
    The nrf_ble_scan already take care of restart scanning when you receiving ADV_REPORT.
    Please don't use usb_printf_variadic when you have an error with sd_ble_gap_scan_start()
    How do you print the log ? 
    When do you print SD:Scan_Stop ?

    Have you tried to test with our example in ble_central folder ? Do you see the same issue ? 

Reply
  • Hi Genfeng, 

    Please provide your full code, how do you start scanning ? Do you use the nrf_ble_scan module ? 
    The nrf_ble_scan already take care of restart scanning when you receiving ADV_REPORT.
    Please don't use usb_printf_variadic when you have an error with sd_ble_gap_scan_start()
    How do you print the log ? 
    When do you print SD:Scan_Stop ?

    Have you tried to test with our example in ble_central folder ? Do you see the same issue ? 

Children
Related