..
timing_alt.h@0:5f745af3ec9b, 2019-08-23 (annotated)
- Committer:
- ImranBilalButt
- Date:
- Fri Aug 23 13:29:35 2019 +0000
- Revision:
- 0:5f745af3ec9b
..
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ImranBilalButt | 0:5f745af3ec9b | 1 | /** |
ImranBilalButt | 0:5f745af3ec9b | 2 | * \file timing.h |
ImranBilalButt | 0:5f745af3ec9b | 3 | * |
ImranBilalButt | 0:5f745af3ec9b | 4 | * \brief Portable interface to timeouts and to the CPU cycle counter |
ImranBilalButt | 0:5f745af3ec9b | 5 | */ |
ImranBilalButt | 0:5f745af3ec9b | 6 | /* |
ImranBilalButt | 0:5f745af3ec9b | 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
ImranBilalButt | 0:5f745af3ec9b | 8 | * SPDX-License-Identifier: Apache-2.0 |
ImranBilalButt | 0:5f745af3ec9b | 9 | * |
ImranBilalButt | 0:5f745af3ec9b | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
ImranBilalButt | 0:5f745af3ec9b | 11 | * not use this file except in compliance with the License. |
ImranBilalButt | 0:5f745af3ec9b | 12 | * You may obtain a copy of the License at |
ImranBilalButt | 0:5f745af3ec9b | 13 | * |
ImranBilalButt | 0:5f745af3ec9b | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ImranBilalButt | 0:5f745af3ec9b | 15 | * |
ImranBilalButt | 0:5f745af3ec9b | 16 | * Unless required by applicable law or agreed to in writing, software |
ImranBilalButt | 0:5f745af3ec9b | 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
ImranBilalButt | 0:5f745af3ec9b | 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ImranBilalButt | 0:5f745af3ec9b | 19 | * See the License for the specific language governing permissions and |
ImranBilalButt | 0:5f745af3ec9b | 20 | * limitations under the License. |
ImranBilalButt | 0:5f745af3ec9b | 21 | * |
ImranBilalButt | 0:5f745af3ec9b | 22 | * This file is part of mbed TLS (https://tls.mbed.org) |
ImranBilalButt | 0:5f745af3ec9b | 23 | */ |
ImranBilalButt | 0:5f745af3ec9b | 24 | //#ifndef MBEDTLS_TIMING_H |
ImranBilalButt | 0:5f745af3ec9b | 25 | //#define MBEDTLS_TIMING_H |
ImranBilalButt | 0:5f745af3ec9b | 26 | |
ImranBilalButt | 0:5f745af3ec9b | 27 | //#if !defined(MBEDTLS_CONFIG_FILE) |
ImranBilalButt | 0:5f745af3ec9b | 28 | //#include "config.h" |
ImranBilalButt | 0:5f745af3ec9b | 29 | //#else |
ImranBilalButt | 0:5f745af3ec9b | 30 | //#include MBEDTLS_CONFIG_FILE |
ImranBilalButt | 0:5f745af3ec9b | 31 | //#endif |
ImranBilalButt | 0:5f745af3ec9b | 32 | |
ImranBilalButt | 0:5f745af3ec9b | 33 | #include <stdint.h> |
ImranBilalButt | 0:5f745af3ec9b | 34 | |
ImranBilalButt | 0:5f745af3ec9b | 35 | //#ifdef __cplusplus |
ImranBilalButt | 0:5f745af3ec9b | 36 | //extern "C" { |
ImranBilalButt | 0:5f745af3ec9b | 37 | //#endif |
ImranBilalButt | 0:5f745af3ec9b | 38 | |
ImranBilalButt | 0:5f745af3ec9b | 39 | // Regular implementation |
ImranBilalButt | 0:5f745af3ec9b | 40 | // |
ImranBilalButt | 0:5f745af3ec9b | 41 | |
ImranBilalButt | 0:5f745af3ec9b | 42 | /** |
ImranBilalButt | 0:5f745af3ec9b | 43 | * \brief timer structure |
ImranBilalButt | 0:5f745af3ec9b | 44 | */ |
ImranBilalButt | 0:5f745af3ec9b | 45 | /*struct mbedtls_timing_hr_time |
ImranBilalButt | 0:5f745af3ec9b | 46 | { |
ImranBilalButt | 0:5f745af3ec9b | 47 | unsigned char opaque[32]; |
ImranBilalButt | 0:5f745af3ec9b | 48 | }; */ |
ImranBilalButt | 0:5f745af3ec9b | 49 | |
ImranBilalButt | 0:5f745af3ec9b | 50 | #include "mbed.h" |
ImranBilalButt | 0:5f745af3ec9b | 51 | //#include <Timer.h> |
ImranBilalButt | 0:5f745af3ec9b | 52 | |
ImranBilalButt | 0:5f745af3ec9b | 53 | //Timer *timer; |
ImranBilalButt | 0:5f745af3ec9b | 54 | //uint32_t start, now; |
ImranBilalButt | 0:5f745af3ec9b | 55 | /** |
ImranBilalButt | 0:5f745af3ec9b | 56 | * \brief Context for mbedtls_timing_set/get_delay() |
ImranBilalButt | 0:5f745af3ec9b | 57 | */ |
ImranBilalButt | 0:5f745af3ec9b | 58 | //timer->start(); |
ImranBilalButt | 0:5f745af3ec9b | 59 | /* |
ImranBilalButt | 0:5f745af3ec9b | 60 | struct dtls_timer_context { |
ImranBilalButt | 0:5f745af3ec9b | 61 | Timer *timer; |
ImranBilalButt | 0:5f745af3ec9b | 62 | uint32_t int_ms; |
ImranBilalButt | 0:5f745af3ec9b | 63 | uint32_t fin_ms; |
ImranBilalButt | 0:5f745af3ec9b | 64 | uint32_t now, start; |
ImranBilalButt | 0:5f745af3ec9b | 65 | }; */ |
ImranBilalButt | 0:5f745af3ec9b | 66 | |
ImranBilalButt | 0:5f745af3ec9b | 67 | //Timer timer; |
ImranBilalButt | 0:5f745af3ec9b | 68 | |
ImranBilalButt | 0:5f745af3ec9b | 69 | typedef struct mbedtls_timing_delay_context |
ImranBilalButt | 0:5f745af3ec9b | 70 | { |
ImranBilalButt | 0:5f745af3ec9b | 71 | uint32_t now, ref; |
ImranBilalButt | 0:5f745af3ec9b | 72 | //mbedtls_timing_hr_time _hr_time; |
ImranBilalButt | 0:5f745af3ec9b | 73 | uint32_t int_ms; |
ImranBilalButt | 0:5f745af3ec9b | 74 | uint32_t fin_ms; |
ImranBilalButt | 0:5f745af3ec9b | 75 | }; |
ImranBilalButt | 0:5f745af3ec9b | 76 | |
ImranBilalButt | 0:5f745af3ec9b | 77 | |
ImranBilalButt | 0:5f745af3ec9b | 78 | //extern volatile int mbedtls_timing_alarmed; |
ImranBilalButt | 0:5f745af3ec9b | 79 | |
ImranBilalButt | 0:5f745af3ec9b | 80 | /** |
ImranBilalButt | 0:5f745af3ec9b | 81 | * \brief Return the CPU cycle counter value |
ImranBilalButt | 0:5f745af3ec9b | 82 | * |
ImranBilalButt | 0:5f745af3ec9b | 83 | * \warning This is only a best effort! Do not rely on this! |
ImranBilalButt | 0:5f745af3ec9b | 84 | * In particular, it is known to be unreliable on virtual |
ImranBilalButt | 0:5f745af3ec9b | 85 | * machines. |
ImranBilalButt | 0:5f745af3ec9b | 86 | * |
ImranBilalButt | 0:5f745af3ec9b | 87 | * \note This value starts at an unspecified origin and |
ImranBilalButt | 0:5f745af3ec9b | 88 | * may wrap around. |
ImranBilalButt | 0:5f745af3ec9b | 89 | */ |
ImranBilalButt | 0:5f745af3ec9b | 90 | //unsigned long mbedtls_timing_hardclock( void ){ |
ImranBilalButt | 0:5f745af3ec9b | 91 | |
ImranBilalButt | 0:5f745af3ec9b | 92 | //}; |
ImranBilalButt | 0:5f745af3ec9b | 93 | |
ImranBilalButt | 0:5f745af3ec9b | 94 | /** |
ImranBilalButt | 0:5f745af3ec9b | 95 | * \brief Return the elapsed time in milliseconds |
ImranBilalButt | 0:5f745af3ec9b | 96 | * |
ImranBilalButt | 0:5f745af3ec9b | 97 | * \param val points to a timer structure |
ImranBilalButt | 0:5f745af3ec9b | 98 | * \param reset If 0, query the elapsed time. Otherwise (re)start the timer. |
ImranBilalButt | 0:5f745af3ec9b | 99 | * |
ImranBilalButt | 0:5f745af3ec9b | 100 | * \return Elapsed time since the previous reset in ms. When |
ImranBilalButt | 0:5f745af3ec9b | 101 | * restarting, this is always 0. |
ImranBilalButt | 0:5f745af3ec9b | 102 | * |
ImranBilalButt | 0:5f745af3ec9b | 103 | * \note To initialize a timer, call this function with reset=1. |
ImranBilalButt | 0:5f745af3ec9b | 104 | * |
ImranBilalButt | 0:5f745af3ec9b | 105 | * Determining the elapsed time and resetting the timer is not |
ImranBilalButt | 0:5f745af3ec9b | 106 | * atomic on all platforms, so after the sequence |
ImranBilalButt | 0:5f745af3ec9b | 107 | * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 = |
ImranBilalButt | 0:5f745af3ec9b | 108 | * get_timer(0) }` the value time1+time2 is only approximately |
ImranBilalButt | 0:5f745af3ec9b | 109 | * the delay since the first reset. |
ImranBilalButt | 0:5f745af3ec9b | 110 | */ |
ImranBilalButt | 0:5f745af3ec9b | 111 | /*unsigned long mbedtls_timing_get_timer( mbedtls_timing_hr_time *val, int reset ){ |
ImranBilalButt | 0:5f745af3ec9b | 112 | if (reset) { |
ImranBilalButt | 0:5f745af3ec9b | 113 | |
ImranBilalButt | 0:5f745af3ec9b | 114 | } |
ImranBilalButt | 0:5f745af3ec9b | 115 | else { |
ImranBilalButt | 0:5f745af3ec9b | 116 | |
ImranBilalButt | 0:5f745af3ec9b | 117 | } |
ImranBilalButt | 0:5f745af3ec9b | 118 | }; */ |
ImranBilalButt | 0:5f745af3ec9b | 119 | |
ImranBilalButt | 0:5f745af3ec9b | 120 | /** |
ImranBilalButt | 0:5f745af3ec9b | 121 | * \brief Setup an alarm clock |
ImranBilalButt | 0:5f745af3ec9b | 122 | * |
ImranBilalButt | 0:5f745af3ec9b | 123 | * \param seconds delay before the "mbedtls_timing_alarmed" flag is set |
ImranBilalButt | 0:5f745af3ec9b | 124 | * (must be >=0) |
ImranBilalButt | 0:5f745af3ec9b | 125 | * |
ImranBilalButt | 0:5f745af3ec9b | 126 | * \warning Only one alarm at a time is supported. In a threaded |
ImranBilalButt | 0:5f745af3ec9b | 127 | * context, this means one for the whole process, not one per |
ImranBilalButt | 0:5f745af3ec9b | 128 | * thread. |
ImranBilalButt | 0:5f745af3ec9b | 129 | */ |
ImranBilalButt | 0:5f745af3ec9b | 130 | //void mbedtls_set_alarm( int seconds ); |
ImranBilalButt | 0:5f745af3ec9b | 131 | |
ImranBilalButt | 0:5f745af3ec9b | 132 | /** |
ImranBilalButt | 0:5f745af3ec9b | 133 | * \brief Set a pair of delays to watch |
ImranBilalButt | 0:5f745af3ec9b | 134 | * (See \c mbedtls_timing_get_delay().) |
ImranBilalButt | 0:5f745af3ec9b | 135 | * |
ImranBilalButt | 0:5f745af3ec9b | 136 | * \param data Pointer to timing data. |
ImranBilalButt | 0:5f745af3ec9b | 137 | * Must point to a valid \c mbedtls_timing_delay_context struct. |
ImranBilalButt | 0:5f745af3ec9b | 138 | * \param int_ms First (intermediate) delay in milliseconds. |
ImranBilalButt | 0:5f745af3ec9b | 139 | * The effect if int_ms > fin_ms is unspecified. |
ImranBilalButt | 0:5f745af3ec9b | 140 | * \param fin_ms Second (final) delay in milliseconds. |
ImranBilalButt | 0:5f745af3ec9b | 141 | * Pass 0 to cancel the current delay. |
ImranBilalButt | 0:5f745af3ec9b | 142 | * |
ImranBilalButt | 0:5f745af3ec9b | 143 | * \note To set a single delay, either use \c mbedtls_timing_set_timer |
ImranBilalButt | 0:5f745af3ec9b | 144 | * directly or use this function with int_ms == fin_ms. |
ImranBilalButt | 0:5f745af3ec9b | 145 | */ |
ImranBilalButt | 0:5f745af3ec9b | 146 | // set delays to watch |
ImranBilalButt | 0:5f745af3ec9b | 147 | void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ){ |
ImranBilalButt | 0:5f745af3ec9b | 148 | |
ImranBilalButt | 0:5f745af3ec9b | 149 | mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; |
ImranBilalButt | 0:5f745af3ec9b | 150 | //dtls_timer_context *ctx = (dtls_timer_context *) data; |
ImranBilalButt | 0:5f745af3ec9b | 151 | |
ImranBilalButt | 0:5f745af3ec9b | 152 | ctx->int_ms = int_ms; |
ImranBilalButt | 0:5f745af3ec9b | 153 | ctx->fin_ms = fin_ms; |
ImranBilalButt | 0:5f745af3ec9b | 154 | |
ImranBilalButt | 0:5f745af3ec9b | 155 | printf("\nInitial delay time (ms) set: ctx->int_ms = %d\n", ctx->int_ms); |
ImranBilalButt | 0:5f745af3ec9b | 156 | printf("\nFinish delay time (ms) set: ctx->fin_ms = %d\n", ctx->fin_ms); |
ImranBilalButt | 0:5f745af3ec9b | 157 | |
ImranBilalButt | 0:5f745af3ec9b | 158 | //timer.start(); |
ImranBilalButt | 0:5f745af3ec9b | 159 | //printf("\ntimer.start() executed and timer.read_ms() = %d\n", timer.read_ms()); |
ImranBilalButt | 0:5f745af3ec9b | 160 | |
ImranBilalButt | 0:5f745af3ec9b | 161 | if( fin_ms != 0 ) { |
ImranBilalButt | 0:5f745af3ec9b | 162 | // reset = 1; get_timer(data, 1) |
ImranBilalButt | 0:5f745af3ec9b | 163 | // ref/start = put clocks |
ImranBilalButt | 0:5f745af3ec9b | 164 | ctx->ref = osKernelGetTickCount(); |
ImranBilalButt | 0:5f745af3ec9b | 165 | printf("\nReference time set: ctx->ref = %d\n", ctx->ref); |
ImranBilalButt | 0:5f745af3ec9b | 166 | } |
ImranBilalButt | 0:5f745af3ec9b | 167 | }; |
ImranBilalButt | 0:5f745af3ec9b | 168 | |
ImranBilalButt | 0:5f745af3ec9b | 169 | /** |
ImranBilalButt | 0:5f745af3ec9b | 170 | * \brief Get the status of delays |
ImranBilalButt | 0:5f745af3ec9b | 171 | * (Memory helper: number of delays passed.) |
ImranBilalButt | 0:5f745af3ec9b | 172 | * |
ImranBilalButt | 0:5f745af3ec9b | 173 | * \param data Pointer to timing data |
ImranBilalButt | 0:5f745af3ec9b | 174 | * Must point to a valid \c mbedtls_timing_delay_context struct. |
ImranBilalButt | 0:5f745af3ec9b | 175 | * |
ImranBilalButt | 0:5f745af3ec9b | 176 | * \return -1 if cancelled (fin_ms = 0), |
ImranBilalButt | 0:5f745af3ec9b | 177 | * 0 if none of the delays are passed, |
ImranBilalButt | 0:5f745af3ec9b | 178 | * 1 if only the intermediate delay is passed, |
ImranBilalButt | 0:5f745af3ec9b | 179 | * 2 if the final delay is passed. |
ImranBilalButt | 0:5f745af3ec9b | 180 | */ |
ImranBilalButt | 0:5f745af3ec9b | 181 | int mbedtls_timing_get_delay( void *data ){ |
ImranBilalButt | 0:5f745af3ec9b | 182 | |
ImranBilalButt | 0:5f745af3ec9b | 183 | mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; |
ImranBilalButt | 0:5f745af3ec9b | 184 | //dtls_timer_context *ctx = (dtls_timer_context *) data; |
ImranBilalButt | 0:5f745af3ec9b | 185 | |
ImranBilalButt | 0:5f745af3ec9b | 186 | unsigned long elapsed_ms; |
ImranBilalButt | 0:5f745af3ec9b | 187 | |
ImranBilalButt | 0:5f745af3ec9b | 188 | if( ctx->fin_ms == 0 ) { |
ImranBilalButt | 0:5f745af3ec9b | 189 | printf("\n Timer has been reset, returned -1\n"); |
ImranBilalButt | 0:5f745af3ec9b | 190 | return( -1 ); |
ImranBilalButt | 0:5f745af3ec9b | 191 | } |
ImranBilalButt | 0:5f745af3ec9b | 192 | |
ImranBilalButt | 0:5f745af3ec9b | 193 | ctx->now = osKernelGetTickCount(); |
ImranBilalButt | 0:5f745af3ec9b | 194 | elapsed_ms = ((ctx->now - ctx->ref) / osKernelGetTickFreq()) * 1000; |
ImranBilalButt | 0:5f745af3ec9b | 195 | printf("\nThe elapsed delay time (ms) is: elapsed_ms = %d %d\n", elapsed_ms, osKernelGetTickFreq()); |
ImranBilalButt | 0:5f745af3ec9b | 196 | |
ImranBilalButt | 0:5f745af3ec9b | 197 | if( elapsed_ms >= ctx->fin_ms ) { |
ImranBilalButt | 0:5f745af3ec9b | 198 | printf("\nFlight delay-Time has been over-delayed, returned 2\n"); |
ImranBilalButt | 0:5f745af3ec9b | 199 | return( 2 ); |
ImranBilalButt | 0:5f745af3ec9b | 200 | } |
ImranBilalButt | 0:5f745af3ec9b | 201 | |
ImranBilalButt | 0:5f745af3ec9b | 202 | if( elapsed_ms >= ctx->int_ms ) { |
ImranBilalButt | 0:5f745af3ec9b | 203 | printf("\nOnly the intermediate delay is passed, returned 1\n"); |
ImranBilalButt | 0:5f745af3ec9b | 204 | return( 1 ); |
ImranBilalButt | 0:5f745af3ec9b | 205 | } |
ImranBilalButt | 0:5f745af3ec9b | 206 | |
ImranBilalButt | 0:5f745af3ec9b | 207 | printf("\nNo delay is been passed, returned 0\n"); |
ImranBilalButt | 0:5f745af3ec9b | 208 | return( 0 ); |
ImranBilalButt | 0:5f745af3ec9b | 209 | }; |
ImranBilalButt | 0:5f745af3ec9b | 210 | |
ImranBilalButt | 0:5f745af3ec9b | 211 | //#if defined(MBEDTLS_SELF_TEST) |
ImranBilalButt | 0:5f745af3ec9b | 212 | /** |
ImranBilalButt | 0:5f745af3ec9b | 213 | * \brief Checkup routine |
ImranBilalButt | 0:5f745af3ec9b | 214 | * |
ImranBilalButt | 0:5f745af3ec9b | 215 | * \return 0 if successful, or 1 if a test failed |
ImranBilalButt | 0:5f745af3ec9b | 216 | */ |
ImranBilalButt | 0:5f745af3ec9b | 217 | //int mbedtls_timing_self_test( int verbose ); |
ImranBilalButt | 0:5f745af3ec9b | 218 | //#endif |
ImranBilalButt | 0:5f745af3ec9b | 219 | |
ImranBilalButt | 0:5f745af3ec9b | 220 | //#ifdef __cplusplus |
ImranBilalButt | 0:5f745af3ec9b | 221 | //} |
ImranBilalButt | 0:5f745af3ec9b | 222 | //#endif |
ImranBilalButt | 0:5f745af3ec9b | 223 | |
ImranBilalButt | 0:5f745af3ec9b | 224 | //#endif /* timing.h */ |