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

questions on how to implement the current time service

I'm working on an implementation of the Current Time Service specified by the Bluetooth SIG (I'm happy to share it).

where I am so far:

  • The service and the caracteristics are set up and working correctly.
  • I can initialise the value attribute with a static date_time
  • I can increment the time using a timer created with the timer lib and update the value in the softdevice

My questions:

  • where do I get the time from? The timer lib uses the RTC but can I still set and read the absolute value of the RTC and read the time-value from there? Or will it interfere? I later want to be able to go into a power saving mode where the device doesn't do anything until the user wakes it up by the press of a button. Of course the RTC should continue to run during that time. Or will I anyway not save a significant amount of energy compared to waking up every secound to run the timer lib?

  • the time gets read from the device very seldomly. Do I really need to update the time value in the softdevice every secound even though it is almost never read? Is there a possibility to get an event from the softdevice when the time is read, so the application can than read the RTC value, write it to the softdevice and tell the softdevice that the value is now ready to be delivered to the peer device?

P.S. Im using your ble_date_time.h file for the date_time format definition. Could it be that there is the include #include "app_util.h" missing? the definitions for __INLINE and the datatypes are missing. I was unable to compile without adding it.

  • Are you implementing the Time Server or the Time Client? I suppose the most common application would be to have the nRF51822 be a Time Client to a smart phone's Time Server, that can get the current time from GPS and/or the phone network. In this case, the nRF51822 would be a GATT Client, and reading the current time from the phone's GATT Server, to be used locally.

    However, if you for some reason are implementing a Time Server on the nRF51, I'd suppose you have some external time keeping device, that you can read to get a proper time for your server. If you want to know when a characteristic is read, you can enable authorization for it, which will give you the events as shown here. Authorization can be enabled by setting the rd_auth flag in the ble_gatts_attr_md_t struct when initializing the characteristic.

  • Thanks for your explanations Ole,

    I'm implementing a Time Server. My device needs accurate time and as it is head-less I must give an option to the user the verify, that the time is set correctly.

    The advice with the "GATTS Read Request with Authorization" might suit my application. Just to confirm: Will the communication be any different from the peer devices perspective when using authorization? Just to be sure to comply with the SIG specs.

    I don't like the idea of an external time keeping device as it costs money, consumes power and needs space. Do you see any problems in a soft RTC implementation based on nrf51822's RTC0/RTC1 value and/or interrupts?

  • The Time Service isn't meant to be used for setting time, it is for a Time Client to get the current time from a Time Server, without user interaction. Hence, in your case, I'd still recommend you to implement a Time Client on your device, that will request a time update from any connected Time Server.

    However, to answer your questions: authorization will not affect the peer device in any noticeable way. Except for precision, there should be no problem with using a soft implementation based on the internal RTC. It will however drift quite significantly, even with a 20 ppm crytsal about 10 minutes a year, which is why I think it would make sense to have a Time Client on your device to get accurate time from a connected smart phone.

  • Thank you Ole! Yes, trough the CTS you can't set the time. But I want to enable the user to find out what time is set on the device now. And I will also implement a time client as proposed by you compensate the drift automatically once in a while. Let's see how good that will work.

  • Hello Mike , Could you please share your implementation of the CTS ? Thanks , /k

Related