Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

This library implements the Microsoft Azure IoTHub client library. The code is replicated from https://github.com/Azure/azure-iot-sdks

Committer:
AzureIoTClient
Date:
Tue Mar 20 10:29:00 2018 -0700
Revision:
85:de16c0a8a196
Parent:
77:e4e36df9caee
Child:
88:248736be106e
1.2.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 77:e4e36df9caee 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 77:e4e36df9caee 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 77:e4e36df9caee 3
AzureIoTClient 77:e4e36df9caee 4 #include <stdlib.h>
AzureIoTClient 77:e4e36df9caee 5 #include <math.h>
AzureIoTClient 77:e4e36df9caee 6 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 77:e4e36df9caee 7 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 77:e4e36df9caee 8 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 77:e4e36df9caee 9 #include "azure_c_shared_utility/agenttime.h"
AzureIoTClient 77:e4e36df9caee 10 #include "azure_c_shared_utility/buffer_.h"
AzureIoTClient 77:e4e36df9caee 11
AzureIoTClient 77:e4e36df9caee 12 #include "iothub_client_diagnostic.h"
AzureIoTClient 77:e4e36df9caee 13
AzureIoTClient 77:e4e36df9caee 14 #define TIME_STRING_BUFFER_LEN 30
AzureIoTClient 77:e4e36df9caee 15
AzureIoTClient 77:e4e36df9caee 16 static const int BASE_36 = 36;
AzureIoTClient 77:e4e36df9caee 17
AzureIoTClient 77:e4e36df9caee 18 #define INDEFINITE_TIME ((time_t)-1)
AzureIoTClient 77:e4e36df9caee 19
AzureIoTClient 77:e4e36df9caee 20 static char* get_epoch_time(char* timeBuffer)
AzureIoTClient 77:e4e36df9caee 21 {
AzureIoTClient 77:e4e36df9caee 22 char* result;
AzureIoTClient 77:e4e36df9caee 23 time_t epochTime;
AzureIoTClient 77:e4e36df9caee 24 int timeLen = sizeof(time_t);
AzureIoTClient 77:e4e36df9caee 25
AzureIoTClient 77:e4e36df9caee 26 if ((epochTime = get_time(NULL)) == INDEFINITE_TIME)
AzureIoTClient 77:e4e36df9caee 27 {
AzureIoTClient 77:e4e36df9caee 28 LogError("Failed getting current time");
AzureIoTClient 77:e4e36df9caee 29 result = NULL;
AzureIoTClient 77:e4e36df9caee 30 }
AzureIoTClient 77:e4e36df9caee 31 else if (timeLen == sizeof(int64_t))
AzureIoTClient 77:e4e36df9caee 32 {
AzureIoTClient 77:e4e36df9caee 33 long long llTime = (long long)epochTime;
AzureIoTClient 77:e4e36df9caee 34 if (sprintf(timeBuffer, "%lld", llTime) < 0)
AzureIoTClient 77:e4e36df9caee 35 {
AzureIoTClient 77:e4e36df9caee 36 LogError("Failed sprintf to timeBuffer with 8 bytes of time_t");
AzureIoTClient 77:e4e36df9caee 37 result = NULL;
AzureIoTClient 77:e4e36df9caee 38 }
AzureIoTClient 77:e4e36df9caee 39 else
AzureIoTClient 77:e4e36df9caee 40 {
AzureIoTClient 77:e4e36df9caee 41 result = timeBuffer;
AzureIoTClient 77:e4e36df9caee 42 }
AzureIoTClient 77:e4e36df9caee 43 }
AzureIoTClient 77:e4e36df9caee 44 else if (timeLen == sizeof(int32_t))
AzureIoTClient 77:e4e36df9caee 45 {
AzureIoTClient 77:e4e36df9caee 46 if (sprintf(timeBuffer, "%d", (int32_t)epochTime) < 0)
AzureIoTClient 77:e4e36df9caee 47 {
AzureIoTClient 77:e4e36df9caee 48 LogError("Failed sprintf to timeBuffer with 4 bytes of time_t");
AzureIoTClient 77:e4e36df9caee 49 result = NULL;
AzureIoTClient 77:e4e36df9caee 50 }
AzureIoTClient 77:e4e36df9caee 51 else
AzureIoTClient 77:e4e36df9caee 52 {
AzureIoTClient 77:e4e36df9caee 53 result = timeBuffer;
AzureIoTClient 77:e4e36df9caee 54 }
AzureIoTClient 77:e4e36df9caee 55 }
AzureIoTClient 77:e4e36df9caee 56 else
AzureIoTClient 77:e4e36df9caee 57 {
AzureIoTClient 77:e4e36df9caee 58 LogError("Unknow size of time_t");
AzureIoTClient 77:e4e36df9caee 59 result = NULL;
AzureIoTClient 77:e4e36df9caee 60 }
AzureIoTClient 77:e4e36df9caee 61
AzureIoTClient 77:e4e36df9caee 62 return result;
AzureIoTClient 77:e4e36df9caee 63 }
AzureIoTClient 77:e4e36df9caee 64
AzureIoTClient 77:e4e36df9caee 65 static char get_base36_char(unsigned char value)
AzureIoTClient 77:e4e36df9caee 66 {
AzureIoTClient 77:e4e36df9caee 67 return value <= 9 ? '0' + value : 'a' + value - 10;
AzureIoTClient 77:e4e36df9caee 68 }
AzureIoTClient 77:e4e36df9caee 69
AzureIoTClient 77:e4e36df9caee 70 static char* generate_eight_random_characters(char *randomString)
AzureIoTClient 77:e4e36df9caee 71 {
AzureIoTClient 77:e4e36df9caee 72 int i;
AzureIoTClient 77:e4e36df9caee 73 char* randomStringPos = randomString;
AzureIoTClient 77:e4e36df9caee 74 for (i = 0; i < 4; ++i)
AzureIoTClient 77:e4e36df9caee 75 {
AzureIoTClient 77:e4e36df9caee 76 int rawRandom = rand();
AzureIoTClient 77:e4e36df9caee 77 int first = rawRandom % BASE_36;
AzureIoTClient 77:e4e36df9caee 78 int second = rawRandom / BASE_36 % BASE_36;
AzureIoTClient 77:e4e36df9caee 79 *randomStringPos++ = get_base36_char((unsigned char)first);
AzureIoTClient 77:e4e36df9caee 80 *randomStringPos++ = get_base36_char((unsigned char)second);
AzureIoTClient 77:e4e36df9caee 81 }
AzureIoTClient 77:e4e36df9caee 82 *randomStringPos = 0;
AzureIoTClient 77:e4e36df9caee 83
AzureIoTClient 77:e4e36df9caee 84 return randomString;
AzureIoTClient 77:e4e36df9caee 85 }
AzureIoTClient 77:e4e36df9caee 86
AzureIoTClient 77:e4e36df9caee 87 static bool should_add_diagnostic_info(IOTHUB_DIAGNOSTIC_SETTING_DATA* diagSetting)
AzureIoTClient 77:e4e36df9caee 88 {
AzureIoTClient 77:e4e36df9caee 89 bool result = false;
AzureIoTClient 77:e4e36df9caee 90 if (diagSetting->diagSamplingPercentage > 0)
AzureIoTClient 77:e4e36df9caee 91 {
AzureIoTClient 77:e4e36df9caee 92 double number;
AzureIoTClient 77:e4e36df9caee 93 double percentage;
AzureIoTClient 77:e4e36df9caee 94
AzureIoTClient 77:e4e36df9caee 95 if (diagSetting->currentMessageNumber == UINT32_MAX)
AzureIoTClient 77:e4e36df9caee 96 {
AzureIoTClient 77:e4e36df9caee 97 diagSetting->currentMessageNumber %= diagSetting->diagSamplingPercentage * 100;
AzureIoTClient 77:e4e36df9caee 98 }
AzureIoTClient 77:e4e36df9caee 99 ++diagSetting->currentMessageNumber;
AzureIoTClient 77:e4e36df9caee 100
AzureIoTClient 77:e4e36df9caee 101 number = diagSetting->currentMessageNumber;
AzureIoTClient 77:e4e36df9caee 102 percentage = diagSetting->diagSamplingPercentage;
AzureIoTClient 77:e4e36df9caee 103 result = (floor((number - 2) * percentage / 100.0) < floor((number - 1) * percentage / 100.0));
AzureIoTClient 77:e4e36df9caee 104 }
AzureIoTClient 77:e4e36df9caee 105 return result;
AzureIoTClient 77:e4e36df9caee 106 }
AzureIoTClient 77:e4e36df9caee 107
AzureIoTClient 77:e4e36df9caee 108 static IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* prepare_message_diagnostic_data()
AzureIoTClient 77:e4e36df9caee 109 {
AzureIoTClient 77:e4e36df9caee 110 IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* result = (IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA*)malloc(sizeof(IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA));
AzureIoTClient 77:e4e36df9caee 111 if (result == NULL)
AzureIoTClient 77:e4e36df9caee 112 {
AzureIoTClient 77:e4e36df9caee 113 LogError("malloc for DiagnosticData failed");
AzureIoTClient 77:e4e36df9caee 114 }
AzureIoTClient 77:e4e36df9caee 115 else
AzureIoTClient 77:e4e36df9caee 116 {
AzureIoTClient 77:e4e36df9caee 117 char* diagId = (char*)malloc(9);
AzureIoTClient 77:e4e36df9caee 118 if (diagId == NULL)
AzureIoTClient 77:e4e36df9caee 119 {
AzureIoTClient 77:e4e36df9caee 120 LogError("malloc for diagId failed");
AzureIoTClient 77:e4e36df9caee 121 free(result);
AzureIoTClient 77:e4e36df9caee 122 result = NULL;
AzureIoTClient 77:e4e36df9caee 123 }
AzureIoTClient 77:e4e36df9caee 124 else
AzureIoTClient 77:e4e36df9caee 125 {
AzureIoTClient 77:e4e36df9caee 126 char* timeBuffer;
AzureIoTClient 77:e4e36df9caee 127
AzureIoTClient 77:e4e36df9caee 128 (void)generate_eight_random_characters(diagId);
AzureIoTClient 77:e4e36df9caee 129 result->diagnosticId = diagId;
AzureIoTClient 77:e4e36df9caee 130
AzureIoTClient 77:e4e36df9caee 131 timeBuffer = (char*)malloc(TIME_STRING_BUFFER_LEN);
AzureIoTClient 77:e4e36df9caee 132 if (timeBuffer == NULL)
AzureIoTClient 77:e4e36df9caee 133 {
AzureIoTClient 77:e4e36df9caee 134 LogError("malloc for timeBuffer failed");
AzureIoTClient 77:e4e36df9caee 135 free(result->diagnosticId);
AzureIoTClient 77:e4e36df9caee 136 free(result);
AzureIoTClient 77:e4e36df9caee 137 result = NULL;
AzureIoTClient 77:e4e36df9caee 138 }
AzureIoTClient 77:e4e36df9caee 139 else if (get_epoch_time(timeBuffer) == NULL)
AzureIoTClient 77:e4e36df9caee 140 {
AzureIoTClient 77:e4e36df9caee 141 LogError("Failed getting current time");
AzureIoTClient 77:e4e36df9caee 142 free(result->diagnosticId);
AzureIoTClient 77:e4e36df9caee 143 free(result);
AzureIoTClient 77:e4e36df9caee 144 result = NULL;
AzureIoTClient 77:e4e36df9caee 145 }
AzureIoTClient 77:e4e36df9caee 146 else
AzureIoTClient 77:e4e36df9caee 147 {
AzureIoTClient 77:e4e36df9caee 148 result->diagnosticCreationTimeUtc = timeBuffer;
AzureIoTClient 77:e4e36df9caee 149 }
AzureIoTClient 77:e4e36df9caee 150 }
AzureIoTClient 77:e4e36df9caee 151 }
AzureIoTClient 77:e4e36df9caee 152 return result;
AzureIoTClient 77:e4e36df9caee 153 }
AzureIoTClient 77:e4e36df9caee 154
AzureIoTClient 77:e4e36df9caee 155 int IoTHubClient_Diagnostic_AddIfNecessary(IOTHUB_DIAGNOSTIC_SETTING_DATA* diagSetting, IOTHUB_MESSAGE_HANDLE messageHandle)
AzureIoTClient 77:e4e36df9caee 156 {
AzureIoTClient 77:e4e36df9caee 157 int result;
AzureIoTClient 77:e4e36df9caee 158 /* Codes_SRS_IOTHUB_DIAGNOSTIC_13_001: [ IoTHubClient_Diagnostic_AddIfNecessary should return nonezero if diagSetting or messageHandle is NULL. ]*/
AzureIoTClient 77:e4e36df9caee 159 if (diagSetting == NULL || messageHandle == NULL)
AzureIoTClient 77:e4e36df9caee 160 {
AzureIoTClient 77:e4e36df9caee 161 result = __FAILURE__;
AzureIoTClient 77:e4e36df9caee 162 }
AzureIoTClient 77:e4e36df9caee 163 /* Codes_SRS_IOTHUB_DIAGNOSTIC_13_003: [ If diagSamplingPercentage is equal to 0, message number should not be increased and no diagnostic properties added ]*/
AzureIoTClient 77:e4e36df9caee 164 else if (should_add_diagnostic_info(diagSetting))
AzureIoTClient 77:e4e36df9caee 165 {
AzureIoTClient 77:e4e36df9caee 166 /* Codes_SRS_IOTHUB_DIAGNOSTIC_13_004: [ If diagSamplingPercentage is equal to 100, diagnostic properties should be added to all messages]*/
AzureIoTClient 77:e4e36df9caee 167 /* Codes_SRS_IOTHUB_DIAGNOSTIC_13_005: [ If diagSamplingPercentage is between(0, 100), diagnostic properties should be added based on percentage]*/
AzureIoTClient 77:e4e36df9caee 168
AzureIoTClient 77:e4e36df9caee 169 IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* diagnosticData;
AzureIoTClient 77:e4e36df9caee 170 if ((diagnosticData = prepare_message_diagnostic_data()) == NULL)
AzureIoTClient 77:e4e36df9caee 171 {
AzureIoTClient 77:e4e36df9caee 172 result = __FAILURE__;
AzureIoTClient 77:e4e36df9caee 173 }
AzureIoTClient 77:e4e36df9caee 174 else
AzureIoTClient 77:e4e36df9caee 175 {
AzureIoTClient 77:e4e36df9caee 176 if (IoTHubMessage_SetDiagnosticPropertyData(messageHandle, diagnosticData) != IOTHUB_MESSAGE_OK)
AzureIoTClient 77:e4e36df9caee 177 {
AzureIoTClient 77:e4e36df9caee 178 /* Codes_SRS_IOTHUB_DIAGNOSTIC_13_002: [ IoTHubClient_Diagnostic_AddIfNecessary should return nonezero if failing to add diagnostic property. ]*/
AzureIoTClient 77:e4e36df9caee 179 result = __FAILURE__;
AzureIoTClient 77:e4e36df9caee 180 }
AzureIoTClient 77:e4e36df9caee 181 else
AzureIoTClient 77:e4e36df9caee 182 {
AzureIoTClient 77:e4e36df9caee 183 result = 0;
AzureIoTClient 77:e4e36df9caee 184 }
AzureIoTClient 77:e4e36df9caee 185
AzureIoTClient 77:e4e36df9caee 186 free(diagnosticData->diagnosticCreationTimeUtc);
AzureIoTClient 77:e4e36df9caee 187 free(diagnosticData->diagnosticId);
AzureIoTClient 77:e4e36df9caee 188 free(diagnosticData);
AzureIoTClient 77:e4e36df9caee 189 diagnosticData = NULL;
AzureIoTClient 77:e4e36df9caee 190 }
AzureIoTClient 77:e4e36df9caee 191 }
AzureIoTClient 77:e4e36df9caee 192 else
AzureIoTClient 77:e4e36df9caee 193 {
AzureIoTClient 77:e4e36df9caee 194 result = 0;
AzureIoTClient 77:e4e36df9caee 195 }
AzureIoTClient 77:e4e36df9caee 196
AzureIoTClient 77:e4e36df9caee 197 return result;
AzureIoTClient 77:e4e36df9caee 198 }