timing_alt.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2020-01-08 19:44:56
  5. * @LastEditTime : 2020-01-13 01:01:39
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #if !defined(MBEDTLS_CONFIG_FILE)
  9. #include "mbedtls/config.h"
  10. #else
  11. #include MBEDTLS_CONFIG_FILE
  12. #endif
  13. #include "platform_timer.h"
  14. #include "timing_alt.h"
  15. unsigned long mbedtls_timing_hardclock( void )
  16. {
  17. return 1600*1000*1000;
  18. }
  19. unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
  20. {
  21. struct mbedtls_timing_hr_time now;
  22. now.timer_ms = platform_timer_now();
  23. if (reset) {
  24. val->timer_ms = now.timer_ms;
  25. }
  26. return (unsigned long)(now.timer_ms - val->timer_ms);
  27. }
  28. /*
  29. * Set delays to watch
  30. */
  31. void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
  32. {
  33. mbedtls_timing_delay_context *ctx;
  34. if (!data) {
  35. return;
  36. }
  37. ctx = (mbedtls_timing_delay_context*)data;
  38. ctx->int_ms = int_ms;
  39. ctx->fin_ms = fin_ms;
  40. if (fin_ms != 0) {
  41. (void)mbedtls_timing_get_timer(&ctx->timer, 1);
  42. }
  43. }
  44. /*
  45. * Get number of delays expired
  46. */
  47. int mbedtls_timing_get_delay(void *data)
  48. {
  49. unsigned long elapsed_ms;
  50. mbedtls_timing_delay_context *ctx;
  51. if (!data) {
  52. return -1;
  53. }
  54. ctx = (mbedtls_timing_delay_context*)data;
  55. if (ctx->fin_ms == 0) {
  56. return -1;
  57. }
  58. elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
  59. if (elapsed_ms >= ctx->fin_ms) {
  60. return 2;
  61. }
  62. if (elapsed_ms >= ctx->int_ms) {
  63. return 1;
  64. }
  65. return 0;
  66. }