mbed port of tinydtls

Committer:
ashleymills
Date:
Fri Oct 11 08:46:21 2013 +0000
Revision:
1:bc8a649bad13
Parent:
0:04990d454f45
Cleaned up all the debug stuff I added finding the hash table bug.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:04990d454f45 1 /* dtls -- a very basic DTLS implementation
ashleymills 0:04990d454f45 2 *
ashleymills 0:04990d454f45 3 * Copyright (C) 2011--2013 Olaf Bergmann <bergmann@tzi.org>
ashleymills 0:04990d454f45 4 *
ashleymills 0:04990d454f45 5 * Permission is hereby granted, free of charge, to any person
ashleymills 0:04990d454f45 6 * obtaining a copy of this software and associated documentation
ashleymills 0:04990d454f45 7 * files (the "Software"), to deal in the Software without
ashleymills 0:04990d454f45 8 * restriction, including without limitation the rights to use, copy,
ashleymills 0:04990d454f45 9 * modify, merge, publish, distribute, sublicense, and/or sell copies
ashleymills 0:04990d454f45 10 * of the Software, and to permit persons to whom the Software is
ashleymills 0:04990d454f45 11 * furnished to do so, subject to the following conditions:
ashleymills 0:04990d454f45 12 *
ashleymills 0:04990d454f45 13 * The above copyright notice and this permission notice shall be
ashleymills 0:04990d454f45 14 * included in all copies or substantial portions of the Software.
ashleymills 0:04990d454f45 15 *
ashleymills 0:04990d454f45 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
ashleymills 0:04990d454f45 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
ashleymills 0:04990d454f45 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ashleymills 0:04990d454f45 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
ashleymills 0:04990d454f45 20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ashleymills 0:04990d454f45 21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
ashleymills 0:04990d454f45 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
ashleymills 0:04990d454f45 23 * SOFTWARE.
ashleymills 0:04990d454f45 24 */
ashleymills 0:04990d454f45 25
ashleymills 0:04990d454f45 26 /**
ashleymills 0:04990d454f45 27 * @file dtls_time.c
ashleymills 0:04990d454f45 28 * @brief Clock Handling
ashleymills 0:04990d454f45 29 */
ashleymills 0:04990d454f45 30
ashleymills 0:04990d454f45 31 #include "dtls_time.h"
ashleymills 0:04990d454f45 32 #include "bsd_socket.h"
ashleymills 0:04990d454f45 33
ashleymills 0:04990d454f45 34 #ifdef MBED
ashleymills 0:04990d454f45 35 /**
ashleymills 0:04990d454f45 36 * gettimeofday() not in mbed
ashleymills 0:04990d454f45 37 */
ashleymills 0:04990d454f45 38 void gettimeofday(struct timeval* t, void* timezone)
ashleymills 0:04990d454f45 39 {
ashleymills 0:04990d454f45 40 t->tv_sec = time(NULL);
ashleymills 0:04990d454f45 41 t->tv_usec = 0; /* 1sec precision only */
ashleymills 0:04990d454f45 42
ashleymills 0:04990d454f45 43 }
ashleymills 0:04990d454f45 44
ashleymills 0:04990d454f45 45 #endif
ashleymills 0:04990d454f45 46
ashleymills 0:04990d454f45 47 #ifdef WITH_CONTIKI
ashleymills 0:04990d454f45 48 clock_time_t dtls_clock_offset;
ashleymills 0:04990d454f45 49
ashleymills 0:04990d454f45 50 void
ashleymills 0:04990d454f45 51 dtls_clock_init(void) {
ashleymills 0:04990d454f45 52 clock_init();
ashleymills 0:04990d454f45 53 dtls_clock_offset = clock_time();
ashleymills 0:04990d454f45 54 }
ashleymills 0:04990d454f45 55
ashleymills 0:04990d454f45 56 void
ashleymills 0:04990d454f45 57 dtls_ticks(dtls_tick_t *t) {
ashleymills 0:04990d454f45 58 *t = clock_time();
ashleymills 0:04990d454f45 59 }
ashleymills 0:04990d454f45 60
ashleymills 0:04990d454f45 61 #else /* WITH_CONTIKI */
ashleymills 0:04990d454f45 62
ashleymills 0:04990d454f45 63 time_t dtls_clock_offset;
ashleymills 0:04990d454f45 64
ashleymills 0:04990d454f45 65 void
ashleymills 0:04990d454f45 66 dtls_clock_init(void) {
ashleymills 0:04990d454f45 67 #ifdef HAVE_TIME_H
ashleymills 0:04990d454f45 68 dtls_clock_offset = time(NULL);
ashleymills 0:04990d454f45 69 #else
ashleymills 0:04990d454f45 70 # ifdef __GNUC__
ashleymills 0:04990d454f45 71 /* Issue a warning when using gcc. Other prepropressors do
ashleymills 0:04990d454f45 72 * not seem to have a similar feature. */
ashleymills 0:04990d454f45 73 # warning "cannot initialize clock"
ashleymills 0:04990d454f45 74 # endif
ashleymills 0:04990d454f45 75 dtls_clock_offset = 0;
ashleymills 0:04990d454f45 76 #endif
ashleymills 0:04990d454f45 77 }
ashleymills 0:04990d454f45 78
ashleymills 0:04990d454f45 79 void dtls_ticks(dtls_tick_t *t) {
ashleymills 0:04990d454f45 80 #ifdef HAVE_SYS_TIME_H
ashleymills 0:04990d454f45 81 struct timeval tv;
ashleymills 0:04990d454f45 82 gettimeofday(&tv, NULL);
ashleymills 0:04990d454f45 83 *t = (tv.tv_sec - dtls_clock_offset) * DTLS_TICKS_PER_SECOND
ashleymills 0:04990d454f45 84 + (tv.tv_usec * DTLS_TICKS_PER_SECOND / 1000000);
ashleymills 0:04990d454f45 85 #else
ashleymills 0:04990d454f45 86 #error "clock not implemented"
ashleymills 0:04990d454f45 87 #endif
ashleymills 0:04990d454f45 88 }
ashleymills 0:04990d454f45 89
ashleymills 0:04990d454f45 90 #endif /* WITH_CONTIKI */
ashleymills 0:04990d454f45 91
ashleymills 0:04990d454f45 92