Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
AzureIoTClient
Date:
Fri Feb 24 14:01:41 2017 -0800
Revision:
21:b92006c5b9ff
Parent:
19:2e0811512ceb
1.1.8

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 "mbed.h"
AzureIoTClient 21:b92006c5b9ff 5 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 0:fa2de1b79154 6 #include "azure_c_shared_utility/tickcounter.h"
Azure.IoT Build 0:fa2de1b79154 7
Azure.IoT Build 0:fa2de1b79154 8 class TICK_COUNTER_INSTANCE_TAG
Azure.IoT Build 0:fa2de1b79154 9 {
Azure.IoT Build 0:fa2de1b79154 10 public:
Azure.IoT Build 0:fa2de1b79154 11 clock_t last_clock_value;
Azure.IoT.Build 16:18e7ebd42bb2 12 tickcounter_ms_t current_ms;
Azure.IoT Build 0:fa2de1b79154 13 };
Azure.IoT Build 0:fa2de1b79154 14
Azure.IoT Build 0:fa2de1b79154 15 TICK_COUNTER_HANDLE tickcounter_create(void)
Azure.IoT Build 0:fa2de1b79154 16 {
Azure.IoT Build 0:fa2de1b79154 17 TICK_COUNTER_INSTANCE_TAG* result;
Azure.IoT Build 0:fa2de1b79154 18 result = new TICK_COUNTER_INSTANCE_TAG();
Azure.IoT Build 0:fa2de1b79154 19 result->last_clock_value = clock();
Azure.IoT Build 0:fa2de1b79154 20 result->current_ms = result->last_clock_value * 1000 / CLOCKS_PER_SEC;
Azure.IoT Build 0:fa2de1b79154 21 return result;
Azure.IoT Build 0:fa2de1b79154 22 }
Azure.IoT Build 0:fa2de1b79154 23
Azure.IoT Build 0:fa2de1b79154 24 void tickcounter_destroy(TICK_COUNTER_HANDLE tick_counter)
Azure.IoT Build 0:fa2de1b79154 25 {
Azure.IoT Build 0:fa2de1b79154 26 if (tick_counter != NULL)
Azure.IoT Build 0:fa2de1b79154 27 {
Azure.IoT Build 0:fa2de1b79154 28 delete tick_counter;
Azure.IoT Build 0:fa2de1b79154 29 }
Azure.IoT Build 0:fa2de1b79154 30 }
Azure.IoT Build 0:fa2de1b79154 31
Azure.IoT.Build 16:18e7ebd42bb2 32 int tickcounter_get_current_ms(TICK_COUNTER_HANDLE tick_counter, tickcounter_ms_t * current_ms)
Azure.IoT Build 0:fa2de1b79154 33 {
Azure.IoT Build 0:fa2de1b79154 34 int result;
Azure.IoT Build 0:fa2de1b79154 35 if (tick_counter == NULL || current_ms == NULL)
Azure.IoT Build 0:fa2de1b79154 36 {
AzureIoTClient 21:b92006c5b9ff 37 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 38 }
Azure.IoT Build 0:fa2de1b79154 39 else
Azure.IoT Build 0:fa2de1b79154 40 {
Azure.IoT Build 0:fa2de1b79154 41 TICK_COUNTER_INSTANCE_TAG* tick_counter_instance = (TICK_COUNTER_INSTANCE_TAG*)tick_counter;
Azure.IoT Build 0:fa2de1b79154 42
Azure.IoT Build 0:fa2de1b79154 43 clock_t clock_value = clock();
Azure.IoT Build 0:fa2de1b79154 44
Azure.IoT Build 0:fa2de1b79154 45 tick_counter_instance->current_ms += (clock_value - tick_counter_instance->last_clock_value) * 1000 / CLOCKS_PER_SEC;
Azure.IoT Build 0:fa2de1b79154 46 tick_counter_instance->last_clock_value = clock_value;
Azure.IoT Build 0:fa2de1b79154 47 *current_ms = tick_counter_instance->current_ms;
Azure.IoT Build 0:fa2de1b79154 48
Azure.IoT Build 0:fa2de1b79154 49 result = 0;
Azure.IoT Build 0:fa2de1b79154 50 }
Azure.IoT Build 0:fa2de1b79154 51 return result;
Azure.IoT Build 0:fa2de1b79154 52 }