]> rtime.felk.cvut.cz Git - frescor/fosa.git/blobdiff - src_marte/fosa_clocks_and_timers.c
Time abstraction added to FOSA to replace timespec_operations
[frescor/fosa.git] / src_marte / fosa_clocks_and_timers.c
index 53fac890cad642fae1007593065023f5251e753a..f518990a37e0f3a4581e50f288cbecba9636cb90 100644 (file)
@@ -68,6 +68,7 @@
 #include <time.h>
 #include <stdio.h>
 
+#include "fosa_time.h"
 #include "fosa_clocks_and_timers.h"
 
 static const struct timespec zero_time={0,0};
@@ -94,9 +95,15 @@ static const struct timespec zero_time={0,0};
  * notify it to the system console and then terminate the FRSH
  * implementation and dependant applications
  **/
-int fosa_clock_get_time(fosa_clock_id_t clockid, struct timespec *current_time)
+int fosa_clock_get_time(fosa_clock_id_t clockid, fosa_abs_time_t *current_time)
 {
-  return clock_gettime(clockid,current_time);
+    struct timespec current_time_tspec;
+    int ret_value;
+
+    ret_value = clock_gettime(clockid, &current_time_tspec);
+    *current_time = fosa_timespec_to_abs_time(current_time_tspec);
+
+    return ret_value;
 }
 
 
@@ -218,22 +225,20 @@ int fosa_timer_delete(fosa_timer_id_t timerid)
   return timer_delete(timerid);
 }
 
+
 /**
- * fosa_timer_arm()
+ * fosa_rel_timer_arm()
  *
- * Arm a timer
+ * Arm a timer with a relative time interval
  *
  * The timer specified by timer is armed and starts counting time.
  *
- * If abstime is true, the value pointed to by value is the absolute
- * time at which the timer will expire. If value specifies a time instant
- * in the past, the timer expires immediately.
+ * The value pointed to by value is the relative interval that must
+ * elapse for the timer to expire.  Negative values cause the timer to
+ * expire immediately.
  *
- * If abstime is false, the value pointed to by value is the relative interval
- * that must elapse for the timer to expire.
- *
- * In both cases, absolute or relative, the time is measured with the clock
- * associated with the timer when it was created.
+ * The time is measured with the clock associated with the timer when
+ * it was created. 
  *
  * If the timer was already armed, the previous time or interval is discarded
  * and the timer is rearmed with the new value.
@@ -241,32 +246,68 @@ int fosa_timer_delete(fosa_timer_id_t timerid)
  * When the timer expires, it is disarmed.
  *
  * Returns 0 if successful; otherwise it returns an error code:
- *    EINVAL: the value of timerid or value is invalid
+ *    FOSA_EINVAL: the value of timerid or value is invalid
  *
  * Alternatively, in case of error the implementation is allowed to
  * notify it to the system console and then terminate the FRSH
  * implementation and dependant applications
  **/
-int fosa_timer_arm
-      (fosa_timer_id_t timerid, bool abstime,
-       const struct timespec *value)
+int fosa_rel_timer_arm
+      (fosa_timer_id_t timerid, const fosa_rel_time_t *value)
 {
-  int timer_abstime=0;
   struct itimerspec timer_value;
 
-  // set the abstime flag if necessary
-  if (abstime) {
-    timer_abstime=TIMER_ABSTIME;
-  }
+  // set the timer to the specified value, one shot only
+  timer_value.it_value= fosa_rel_time_to_timespec(*value);
+  timer_value.it_interval=zero_time;
+
+  // arm the timer
+  return timer_settime(timerid, 0, &timer_value,NULL);
+}
+
+
+/**
+ * fosa_abs_timer_arm()
+ *
+ * Arm a timer that will expire in an absolute time instant.
+ *
+ * The timer specified by timer is armed and starts counting time.
+ *
+ * The value pointed to by value is the absolute time at which the
+ * timer will expire. If value specifies a time instant in the past,
+ * the timer expires immediately. 
+ *
+ * The time is measured with the clock associated with the timer when
+ * it was created. 
+ *
+ * If the timer was already armed, the previous time or interval is discarded
+ * and the timer is rearmed with the new value.
+ *
+ * When the timer expires, it is disarmed.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *    FOSA_EINVAL: the value of timerid or value is invalid
+ *
+ * Alternatively, in case of error the implementation is allowed to
+ * notify it to the system console and then terminate the FRSH
+ * implementation and dependant applications
+ **/
+int fosa_abs_timer_arm
+      (fosa_timer_id_t timerid, const fosa_abs_time_t *value)
+{
+  struct itimerspec timer_value;
 
   // set the timer to the specified value, one shot only
-  timer_value.it_value=*value;
+  timer_value.it_value= fosa_abs_time_to_timespec(*value);
   timer_value.it_interval=zero_time;
 
   // arm the timer
-  return timer_settime(timerid,timer_abstime,&timer_value,NULL);
+  return timer_settime(timerid, TIMER_ABSTIME,&timer_value,NULL);
 }
 
+
+
+
 /**
  * fosa_timer_get_remaining_time()
  *
@@ -286,18 +327,19 @@ int fosa_timer_arm
  * implementation and dependant applications
  **/
 int fosa_timer_get_remaining_time
-        (fosa_timer_id_t timerid, struct timespec *remaining_time)
+        (fosa_timer_id_t timerid, fosa_rel_time_t *remaining_time)
 {
     struct itimerspec timer_value;
     int error_code;
 
-    if (remaining_time!=NULL) {
-        error_code=timer_gettime(timerid,&timer_value);
-        if (error_code==-1) return errno;
-        *remaining_time=timer_value.it_value;
-    } else {
+    if (remaining_time == NULL) {
         return EINVAL;
     }
+    error_code=timer_gettime(timerid,&timer_value);
+    if (error_code==-1) return errno;
+    
+    *remaining_time = fosa_timespec_to_rel_time(timer_value.it_value);
+
     return 0;
 }
 
@@ -323,7 +365,7 @@ int fosa_timer_get_remaining_time
  **/
 int fosa_timer_disarm
    (fosa_timer_id_t timerid,
-    struct timespec *remaining_time)
+    fosa_rel_time_t *remaining_time)
 {
   struct itimerspec timer_value;
   int error_code;
@@ -331,7 +373,7 @@ int fosa_timer_disarm
   if (remaining_time!=NULL) {
     error_code=timer_gettime(timerid,&timer_value);
     if (error_code==-1) return errno;
-    *remaining_time=timer_value.it_value;
+    *remaining_time = fosa_timespec_to_rel_time(timer_value.it_value);
   }
   timer_value.it_value=zero_time;
   timer_value.it_interval=zero_time;