How to get the current time from the CSA Matter SDK on espressif ESP32

194 Views Asked by At

My understanding of the CSA Matter (aka project CHIP) spec (11.16) seems to indicate that nodes on the Matter fabric will sync the current time using the TimeSync cluster service.

If true, it seems like there should be a way to use Matter to get the current real time from within my app code. The Matter SDK includes various headers and functions for dealing with time. SystemClock().GetClock_RealTime() seems promising, but when I call it I only seem to get the number of microseconds since device boot rather than the real time. ToTimeval() in the same header also seems promising to convert to a timeval, but that declaration is not defined on ESP32, at least on my version.

I'm using the jakubdybczak/esp32-arduino-matter Arduino port of the Espressif Matter SDK.

I also tried accessing the time service on Node 0, but the returned attribute is always null:

  endpoint_t *root_endpoint = endpoint::get(node,0);
  esp_matter::cluster_t *time_cluster = cluster::create(root_endpoint,TimeSynchronization::Id, CLUSTER_FLAG_CLIENT);
  // in this example I'm looking at timezone, but the same thing happens for LocalTime::Id
  attribute_t *timezone_attribute = attribute::get(time_cluster,TimeSynchronization::Attributes::TimeZone::Id); // always NULL
  esp_matter_attr_val_t timezone_value = esp_matter_invalid(NULL);
  attribute::get_val(timezone_attribute, &timezone_value);

What is the proper way to use Matter to get the current real time?

1

There are 1 best solutions below

0
On

You can try making use of the NTP protocol to keep track of the current date and time. Also, you can refer to the time synchronization cluster in the matter specification documentation for understanding the mechanism how the nodes try to achieve and maintain time synchronization.

There are also a bunch of implementation guidance that can be found here.