forked from rfuentess/TinyDTLS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtls_time.c
71 lines (59 loc) · 1.67 KB
/
dtls_time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*******************************************************************************
*
* Copyright (c) 2011, 2012, 2013, 2014, 2015 Olaf Bergmann (TZI) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Olaf Bergmann - initial API and implementation
*
*******************************************************************************/
/**
* @file dtls_time.c
* @brief Clock Handling
*/
#include "tinydtls.h"
#include "dtls_config.h"
#include "dtls_time.h"
#ifdef WITH_CONTIKI
clock_time_t dtls_clock_offset;
void
dtls_clock_init(void) {
clock_init();
dtls_clock_offset = clock_time();
}
void
dtls_ticks(dtls_tick_t *t) {
*t = clock_time();
}
#else /* WITH_CONTIKI */
time_t dtls_clock_offset;
void
dtls_clock_init(void) {
#ifdef HAVE_TIME_H
dtls_clock_offset = time(NULL);
#else
# ifdef __GNUC__
/* Issue a warning when using gcc. Other prepropressors do
* not seem to have a similar feature. */
# warning "cannot initialize clock"
# endif
dtls_clock_offset = 0;
#endif
}
void dtls_ticks(dtls_tick_t *t) {
#ifdef HAVE_SYS_TIME_H
struct timeval tv;
gettimeofday(&tv, NULL);
*t = (tv.tv_sec - dtls_clock_offset) * DTLS_TICKS_PER_SECOND
+ (tv.tv_usec * DTLS_TICKS_PER_SECOND / 1000000);
#else
#error "clock not implemented"
#endif
}
#endif /* WITH_CONTIKI */