Skip to content

Commit

Permalink
Merge pull request tock#355 from alistair23/alistair/gettimeofday
Browse files Browse the repository at this point in the history
libtock: sys: Implement _gettimeofday
  • Loading branch information
bradjc authored Dec 15, 2023
2 parents 8b96bb8 + de1865b commit 76a821b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions libtock/alarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ extern "C" {

#include "tock.h"

#include <sys/time.h>

/** \brief Opaque handle to a single-shot alarm.
*
* An opaque handle to an alarm created by `alarm_at` or `alarm_in`. Memory
Expand Down Expand Up @@ -69,6 +71,18 @@ int alarm_at(uint32_t reference, uint32_t dt, subscribe_upcall, void*, alarm_t *
*/
void alarm_cancel(alarm_t*);

// Use this to implement _gettimeofday yourself as libtock-c doesn't provide
// an implementation.
//
// See https://github.com/tock/libtock-c/pull/355#issuecomment-1841351091 for
// more details
//
// ```c
// int _gettimeofday(struct timeval *tv, void *tzvp) {
// return gettimeasticks(tv, tzvp);
// }
// ```
int gettimeasticks(struct timeval *tv, void *tzvp);

#ifdef __cplusplus
}
Expand Down
16 changes: 16 additions & 0 deletions libtock/alarm_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,19 @@ int yield_for_with_timeout(bool* cond, uint32_t ms) {
timer_cancel(&timer);
return RETURNCODE_SUCCESS;
}

int gettimeasticks(struct timeval *tv, __attribute__ ((unused)) void *tzvp)
{
uint32_t frequency, now, seconds, remainder;

alarm_internal_frequency(&frequency);
alarm_internal_read(&now);

seconds = now / frequency;
remainder = now % frequency;

tv->tv_sec = seconds;
tv->tv_usec = (remainder * 1000 * 1000) / frequency;

return 0;
}

0 comments on commit 76a821b

Please sign in to comment.