#include <stdio.h>
#include <stdlib.h>
#include "../ports_clocktimer.h"

static PORTS_ClockTimerInfo_t PORTS_Tinfo0;

int PORTS_ClockTimerInit(int *argc, char **argv) {
  PORTS_Tinfo0.resolution = 1.0e6;
  PORTS_Tinfo0.rollover   = -1.0;
  PORTS_Tinfo0.attributes = PORTS_CLOCKTIMER_SYNCHRONIZED |
                            PORTS_CLOCKTIMER_WALL;
  return PORTS_CLOCKTIMER_SUCCESS;
}

int PORTS_TimerInitInternal0(PORTS_Timer0_t *t)  {
  if ( (*t = (PORTS_Timer0_t) malloc(sizeof(PORTS_Timer0_t))) != NULL ) {
    PORTS_TimerReset0(*t);
    return PORTS_CLOCKTIMER_SUCCESS;
  }
  else
    return PORTS_CLOCKTIMER_ERROR;
}

int PORTS_TimerDestroy0(PORTS_Timer0_t t)  {
  free(t);
  return PORTS_CLOCKTIMER_SUCCESS;
}

int PORTS_TimerReset0(PORTS_Timer0_t t) {
  if (t == NULL) return PORTS_CLOCKTIMER_ERROR;
  t->count       = 0;
  t->sum.tv_sec  = 0;
  t->sum.tv_usec = 0;
  t->clk.tv_sec  = 0;
  t->clk.tv_usec = 0;
  return PORTS_CLOCKTIMER_SUCCESS;
}

double PORTS_TimerElapsed0(PORTS_Timer0_t t, int *c) {
  if (t == NULL) return -1.0;
  if (c) *c = t->count;
  return (t->sum.tv_sec + t->sum.tv_usec * 1.0e-6);
}
