Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
Azure.IoT.Build
Date:
Wed Dec 14 16:00:59 2016 -0800
Revision:
16:18e7ebd42bb2
Parent:
0:fa2de1b79154
Child:
19:2e0811512ceb
1.1.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:fa2de1b79154 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:fa2de1b79154 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:fa2de1b79154 3
Azure.IoT Build 0:fa2de1b79154 4 #include <cstdlib>
Azure.IoT Build 0:fa2de1b79154 5
Azure.IoT Build 0:fa2de1b79154 6 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:fa2de1b79154 7 #include <crtdbg.h>
Azure.IoT Build 0:fa2de1b79154 8 #endif
Azure.IoT Build 0:fa2de1b79154 9
Azure.IoT Build 0:fa2de1b79154 10 #include <cstdio>
Azure.IoT Build 0:fa2de1b79154 11 #include <cctype>
Azure.IoT Build 0:fa2de1b79154 12 #include "mbed.h"
Azure.IoT Build 0:fa2de1b79154 13 #include "azure_c_shared_utility/tickcounter.h"
Azure.IoT Build 0:fa2de1b79154 14
Azure.IoT Build 0:fa2de1b79154 15 class TICK_COUNTER_INSTANCE_TAG
Azure.IoT Build 0:fa2de1b79154 16 {
Azure.IoT Build 0:fa2de1b79154 17 public:
Azure.IoT Build 0:fa2de1b79154 18 clock_t last_clock_value;
Azure.IoT.Build 16:18e7ebd42bb2 19 tickcounter_ms_t current_ms;
Azure.IoT Build 0:fa2de1b79154 20 };
Azure.IoT Build 0:fa2de1b79154 21
Azure.IoT Build 0:fa2de1b79154 22 TICK_COUNTER_HANDLE tickcounter_create(void)
Azure.IoT Build 0:fa2de1b79154 23 {
Azure.IoT Build 0:fa2de1b79154 24 TICK_COUNTER_INSTANCE_TAG* result;
Azure.IoT Build 0:fa2de1b79154 25 result = new TICK_COUNTER_INSTANCE_TAG();
Azure.IoT Build 0:fa2de1b79154 26 result->last_clock_value = clock();
Azure.IoT Build 0:fa2de1b79154 27 result->current_ms = result->last_clock_value * 1000 / CLOCKS_PER_SEC;
Azure.IoT Build 0:fa2de1b79154 28 return result;
Azure.IoT Build 0:fa2de1b79154 29 }
Azure.IoT Build 0:fa2de1b79154 30
Azure.IoT Build 0:fa2de1b79154 31 void tickcounter_destroy(TICK_COUNTER_HANDLE tick_counter)
Azure.IoT Build 0:fa2de1b79154 32 {
Azure.IoT Build 0:fa2de1b79154 33 if (tick_counter != NULL)
Azure.IoT Build 0:fa2de1b79154 34 {
Azure.IoT Build 0:fa2de1b79154 35 delete tick_counter;
Azure.IoT Build 0:fa2de1b79154 36 }
Azure.IoT Build 0:fa2de1b79154 37 }
Azure.IoT Build 0:fa2de1b79154 38
Azure.IoT.Build 16:18e7ebd42bb2 39 int tickcounter_get_current_ms(TICK_COUNTER_HANDLE tick_counter, tickcounter_ms_t * current_ms)
Azure.IoT Build 0:fa2de1b79154 40 {
Azure.IoT Build 0:fa2de1b79154 41 int result;
Azure.IoT Build 0:fa2de1b79154 42 if (tick_counter == NULL || current_ms == NULL)
Azure.IoT Build 0:fa2de1b79154 43 {
Azure.IoT Build 0:fa2de1b79154 44 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 45 }
Azure.IoT Build 0:fa2de1b79154 46 else
Azure.IoT Build 0:fa2de1b79154 47 {
Azure.IoT Build 0:fa2de1b79154 48 TICK_COUNTER_INSTANCE_TAG* tick_counter_instance = (TICK_COUNTER_INSTANCE_TAG*)tick_counter;
Azure.IoT Build 0:fa2de1b79154 49
Azure.IoT Build 0:fa2de1b79154 50 clock_t clock_value = clock();
Azure.IoT Build 0:fa2de1b79154 51
Azure.IoT Build 0:fa2de1b79154 52 tick_counter_instance->current_ms += (clock_value - tick_counter_instance->last_clock_value) * 1000 / CLOCKS_PER_SEC;
Azure.IoT Build 0:fa2de1b79154 53 tick_counter_instance->last_clock_value = clock_value;
Azure.IoT Build 0:fa2de1b79154 54 *current_ms = tick_counter_instance->current_ms;
Azure.IoT Build 0:fa2de1b79154 55
Azure.IoT Build 0:fa2de1b79154 56 result = 0;
Azure.IoT Build 0:fa2de1b79154 57 }
Azure.IoT Build 0:fa2de1b79154 58 return result;
Azure.IoT Build 0:fa2de1b79154 59 }