Hi everyone,
I am writting a custom firmware and I am stacked on how to manipulate properly BLE events. First of all I have a handler function that listens for BLE events, If the BLE_GATTS_EVT_WRITE (see ble_gatts.h) is received then I call the on_write() function:
static void on_write(ble_cus_t *p_cus, ble_evt_t const *p_ble_evt) { ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; // When the central do a write command a BLE_GATTS_EVT_WRITE event is received ble_cus_evt_t evt; // Here we check if the handle that has been written matches the handle of the CCCD and that the value has the correct length (2bytes) if ((p_evt_write->handle == p_cus->custom_value_handles.cccd_handle) && (p_evt_write->len == 2)) { // CCCD written, call application event handler if (p_cus->evt_handler != NULL) { // na - check that the event handler function is pointing somewhere (in our case on on_cus_evt function, see main.c) NRF_LOG_INFO("Write: %d", p_evt_write->data[0]); // Here we need to check if the notification enabled or disabled if (p_evt_write->data[0] == 0x01) { evt.evt_type = BLE_CUS_EVT_NOTIFICATION_ENABLED; } else if (p_evt_write->data[0] == 0x00) { evt.evt_type = BLE_CUS_EVT_NOTIFICATION_DISABLED; } else { evt.evt_type = INVALID_STATE; NRF_LOG_INFO("Invalid State"); } p_cus->evt_handler(p_cus, &evt); // na - Call the application event handler. } } // Here we check if the handle that has been written matches the handle of the characteristic we want to write if ((p_evt_write->handle == p_cus->custom_value_handles.value_handle) && (p_evt_write->len == 1)) { if (p_cus->evt_handler != NULL) { // na - check that the event handler function is pointing somewhere (in our case on on_cus_evt function, see main.c) NRF_LOG_INFO("Write: %d", p_evt_write->data[0]); NRF_LOG_INFO("evt_UN_type: %d", evt.evt_UN_type); // Here we need to check the type of UN command if (p_evt_write->data[0] == 0x05) { evt.evt_UN_type = BLE_CUS_EVT_STANDBY; } else if (p_evt_write->data[0] == 0x06) { evt.evt_UN_type = BLE_CUS_EVT_ACTIVE; } else if (p_evt_write->data[0] == 0x07) { evt.evt_UN_type = BLE_CUS_EVT_LOW_ACTIVE; } NRF_LOG_INFO("evt_UN_type1: %d", evt.evt_UN_type); p_cus->evt_handler(p_cus, &evt); // na - Call the application event handler. } } }
Inside the on_write() function I have two if statements, where the first one looks if the CCCD handle has been written while the second one if the vlaue handle has been written.
When the value handle is written I check the written value and assign it on the evt_UN_type variable
These are the enumerators:
My problem is that the evt_UN_type is not remain constant.. For example, when I write 0x06 on value handle, you see that the first NRF_LOG_INFO("evt_UN_type: %d", evt.evt_UN_type); prints 59 (I do not know what this values is and from where it is come form) and after the assignment it normally prints 6. When I write 0x06 again, I am expecting that the evt_UN_type variable would store the last value (which is 6), but as you can see it prints again 59.. When I write 7, it again prints 59 before the asignment (I was expecting to see 6, that was the last value)..
Thanks in advance
Nick