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

Timer implementation

Hi,

I was referring the timer example code for PCA10040. I wanted to include the timer code in my ble peripheral code which uses custom GATT service. Can you help me with this? What I was trying - Once I receive a message from the host, I start the timer. If the next message is not received within a certain timeout, then I quit.

static void cus_on_write(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt)
{
	int i;
	ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
    // Custom Value Characteristic Written to.
    if (p_evt_write->handle == p_cus->custom_value_handles.value_handle)
    {		
		NRF_LOG_INFO("Data Length:::::::::::::: %d", p_evt_write->len);		
		if((p_evt_write->data[0] == 0x0A) && (p_evt_write->data[1] == 0x0B) && (p_evt_write->data[2] == 0x0C) && (p_evt_write->data[3] == 0x0D) && (p_evt_write->data[4] == 0x0E))
		{
			NRF_LOG_INFO("Received message from host");
			//IF THIS MESSAGE IS NOT RECEIVED WITHIN 2 SECONDS, THEN CALL delete_bonds()
		}
    }
    // Check if the Custom value CCCD is written to and that the value is the appropriate length, i.e 2 bytes.
    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)
        {
            ble_cus_evt_t evt;
            if (ble_srv_is_notification_enabled(p_evt_write->data))
            {
				NRF_LOG_INFO("BLE_CUS_EVT_NOTIFICATION_ENABLED\r\n");
				stop_sending = false;
                evt.evt_type = BLE_CUS_EVT_NOTIFICATION_ENABLED;
            }
            else
            {
				NRF_LOG_INFO("BLE_CUS_EVT_NOTIFICATION_DISABLED\r\n");
				stop_sending = true;
                evt.evt_type = BLE_CUS_EVT_NOTIFICATION_DISABLED;
            }
            // Call the application event handler.
            p_cus->evt_handler(p_cus, &evt);
        }
    }
}

Parents
  • Do the following:

    • Implement a timer into your application
    • Create a boolean variable timer_expired and set it to true
    • When the first message is received, start the timer and set timer_expired to false
    • When the timeout handler function is called set timer_expired  to true
    • When the second message is received, check the value of timer_expired  and take appropriate action. E.g. if it is equal to true then you quit/delete bonds.
      • Maybe you could create a second boolean variable timer_started in case you receive this message before the other message

    Best regards,

    Simon

Reply
  • Do the following:

    • Implement a timer into your application
    • Create a boolean variable timer_expired and set it to true
    • When the first message is received, start the timer and set timer_expired to false
    • When the timeout handler function is called set timer_expired  to true
    • When the second message is received, check the value of timer_expired  and take appropriate action. E.g. if it is equal to true then you quit/delete bonds.
      • Maybe you could create a second boolean variable timer_started in case you receive this message before the other message

    Best regards,

    Simon

Children
No Data
Related