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 : 

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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))
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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 : 

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 =
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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?