Clock Time
Motivation
For some protocols or simulations it may be necessary to access the current clock time.
Pre-requisites
-
You have a working miniHIL project setup which you either created using the template project or the example project.
-
You have basic eTrice knowldge. E.g. you successfully finished the eTrice PingPong Tutorials.
eTrice/Room API
The current clock time is provided by a top level service which exposes a SPP for the PDateTime protocol.
To use PDateTime simply add a SAP with that protocol to your actor.
The time is only valid after the pc connection is established (gui or commandline). You should check the isValid attribute of the returned object |
SimModelLib/model/minihil/api/datetime.room
DataClass DDateTime {
usercode1 '''
#include "realtimeclock.h"
'''
Attribute dateTime: RTC_DateTime
Attribute isValid: boolean
}
ProtocolClass PDateTime {
incoming {
Message getDateTime()
}
outgoing {
Message dateTime(DDateTime)
}
}
Actor SAP example
ActorClass AMyActor {
Structure {
SAP dateTimeServ: PDateTime
}
Behavior {
StateMachine {
State active
Transition init0: initial -> active {
action '''dateTimeServ.getDateTime();'''
}
Transition tr0: active -> active {
triggers {
<dateTime: dateTimeServ>
}
action '''
if (transitionData->isValid) {
// transitionData->dateTime contains the current UTC time
/*
typedef struct RTC_DateTime_struct {
uint8_t hours;
uint8_t minutes;
uint8_t seconds;
uint8_t weekDay;
uint8_t month;
uint8_t day;
uint8_t year;
} RTC_DateTime;
*/
transitionData->dateTime.hours; // hours
// [...]
}'''
}
}
}
}
C-API
The current clock time is also exposed via c api which is defined in "SimRuntime/minihil/common/realtimeclock.h".
All times returned by the api are UTC. Timestamps are UNIX timestamps (seconds since 01.01.1970) |
SimRuntime/minihil/common/realtimeclock.h
typedef struct RTC_DateTime_struct {
uint8_t hours;
uint8_t minutes;
uint8_t seconds;
uint8_t weekDay;
uint8_t month;
uint8_t day;
uint8_t year;
} RTC_DateTime;
/**
* returns a unix timestamp if the RTC has been set before. Returns -1 otherwise
*/
int64_t rtc_getTimestamp();
/**
* gets the rtc time if the RTC has been set before
* returns true if the time has been written to the passed dateTime ptr
* returns false if the RTC has not been set, yet.
*/
bool rtc_getDateTime(RTC_DateTime * const dateTime);
/**
* returns 0 if the RTC does not have a valid value
* returns != 0 if the RTC does have a valid date and time
*/
uint8_t rtc_isTimeValid();
/**
* converts the passed dateTime struct to a unix timestamp
*/
int64_t rtc_convertDateTimeToTimestamp(RTC_DateTime const * const dateTime);