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
  • Hey msromero,

    I figured it out !! The "on_cus_evt" callback is executed first and then "ble_evt_handler", since the value of received_data is changed in the "ble_evt_hanlder" it wasn't entering the case.

    Is there any rule by which it decides which callback function to call first, the notification enabled or disabled callback function or the callback function when something is written to the characteristic.

Children
Related