This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Not able to use "if-else" condition inside "switch-case" event handler

I'm using the Custom service example as by base. Now this was really itching me to not know the reason which causes it. I'm giving an "if-else" condition inside a "switch-case" condition as shown below inside the custom ble service handler as follows. In the BLE_CUS_EVT_NOTIFICATION_ENABLED case to be specific and inside it the if condition checking for the received_data variable : 

static void on_cus_evt(ble_cus_t     * p_cus_service,
                       ble_cus_evt_t * p_evt)
{
    ret_code_t err_code;
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    switch(p_evt->evt_type)
    {
        case BLE_CUS_EVT_NOTIFICATION_ENABLED:
              if (pdPASS != xTimerStartFromISR(m_cus_notification_timer, &xHigherPriorityTaskWoken))
            {
              APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
            }
            
            if(received_data==1){
            // Enter here, set a breakpoint here
            xTaskResumeFromISR(m_mpu_thread_0);
            }
            break;

        case BLE_CUS_EVT_NOTIFICATION_DISABLED:
             if (pdPASS != xTimerStopFromISR(m_cus_notification_timer, &xHigherPriorityTaskWoken))
            {
              APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
            }

             break;

        case BLE_CUS_EVT_CONNECTED:
            break;

        case BLE_CUS_EVT_DISCONNECTED:
              break;

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

received_data is the variable which stores the data written to the service. It is a global variable so I think scope isn't the issue here. When I see my "received_data" in the debug terminal it shows that the value is 1 but it never enters the "if(received_data==1)" condition.

But if the same thing is done in ble event handler callback function then it's working fine . In the BLE_GATTS_EVT_WRITE event to specific and if condition works here fine : 

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    uint32_t err_code;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO("Disconnected");
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            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_GATTS_EVT_WRITE:
            received_data = p_ble_evt->evt.gatts_evt.params.write.data[0];
            
            if(received_data==1){
            
            xTaskResumeFromISR(m_mpu_thread_0);

            }
            else if(received_data==2){
            xTaskResumeFromISR(m_thread_1);
            }
            break;

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

what's the reason that if works in on ble event handler and not in the custom service event handler. Aren't if-else cases applicable anywhere in the code?

Parents Reply
  • Hi,

    To answer you last question. If you take a look at the example code you can see that the application event handler on_cus_init is called inside the on_write function after BLE_GATTS_EVT_WRITE has occured in ble_cus_on_ble_evt and the priorities of ble_cus_on_ble_evt and ble_evt_handler are set as

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

    in main.c and

    NRF_SDH_BLE_OBSERVER(_name ## _obs,                                                                 \
                         BLE_HRS_BLE_OBSERVER_PRIO,                                                     \
                         ble_cus_on_ble_evt, &_name)

    in ble_cus.h.  BLE_HRS_BLE_OBSERVER_PRIO = 2 in sdk_config.h, and APP_BLE_OBSERVER_PRIO is defined as 3 in main.c.

    So the callback function for ble_cus_on_ble_evt will have higher priority than ble_evt_handler.

    Best Regards,

    Marjeris

Children
No Data
Related