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
iothub_client_diagnostic.c@92:97148cf9aa2a, 2018-09-11 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Tue Sep 11 11:13:11 2018 -0700
- Revision:
- 92:97148cf9aa2a
- Parent:
- 89:a2ed767a532e
1.2.9
Who changed what in which revision?
User | Revision | Line number | New 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 | 89:a2ed767a532e | 5 | #include <inttypes.h> |
AzureIoTClient | 77:e4e36df9caee | 6 | #include <math.h> |
AzureIoTClient | 77:e4e36df9caee | 7 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 77:e4e36df9caee | 8 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 77:e4e36df9caee | 9 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 77:e4e36df9caee | 10 | #include "azure_c_shared_utility/agenttime.h" |
AzureIoTClient | 77:e4e36df9caee | 11 | #include "azure_c_shared_utility/buffer_.h" |
AzureIoTClient | 77:e4e36df9caee | 12 | |
AzureIoTClient | 88:248736be106e | 13 | #include "internal/iothub_client_diagnostic.h" |
AzureIoTClient | 77:e4e36df9caee | 14 | |
AzureIoTClient | 77:e4e36df9caee | 15 | #define TIME_STRING_BUFFER_LEN 30 |
AzureIoTClient | 77:e4e36df9caee | 16 | |
AzureIoTClient | 77:e4e36df9caee | 17 | static const int BASE_36 = 36; |
AzureIoTClient | 77:e4e36df9caee | 18 | |
AzureIoTClient | 77:e4e36df9caee | 19 | #define INDEFINITE_TIME ((time_t)-1) |
AzureIoTClient | 77:e4e36df9caee | 20 | |
AzureIoTClient | 77:e4e36df9caee | 21 | static char* get_epoch_time(char* timeBuffer) |
AzureIoTClient | 77:e4e36df9caee | 22 | { |
AzureIoTClient | 77:e4e36df9caee | 23 | char* result; |
AzureIoTClient | 77:e4e36df9caee | 24 | time_t epochTime; |
AzureIoTClient | 77:e4e36df9caee | 25 | int timeLen = sizeof(time_t); |
AzureIoTClient | 89:a2ed767a532e | 26 | |
AzureIoTClient | 77:e4e36df9caee | 27 | if ((epochTime = get_time(NULL)) == INDEFINITE_TIME) |
AzureIoTClient | 77:e4e36df9caee | 28 | { |
AzureIoTClient | 77:e4e36df9caee | 29 | LogError("Failed getting current time"); |
AzureIoTClient | 77:e4e36df9caee | 30 | result = NULL; |
AzureIoTClient | 77:e4e36df9caee | 31 | } |
AzureIoTClient | 88:248736be106e | 32 | else if (timeLen == sizeof(int64_t)) |
AzureIoTClient | 88:248736be106e | 33 | { |
AzureIoTClient | 89:a2ed767a532e | 34 | if (sprintf(timeBuffer, "%"PRIu64, (int64_t)epochTime) < 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 | 88:248736be106e | 43 | } |
AzureIoTClient | 88:248736be106e | 44 | else if (timeLen == sizeof(int32_t)) |
AzureIoTClient | 88:248736be106e | 45 | { |
AzureIoTClient | 89:a2ed767a532e | 46 | if (sprintf(timeBuffer, "%"PRIu32, (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 | 88:248736be106e | 55 | } |
AzureIoTClient | 77:e4e36df9caee | 56 | else |
AzureIoTClient | 77:e4e36df9caee | 57 | { |
AzureIoTClient | 89:a2ed767a532e | 58 | LogError("Unknown size of time_t"); |
AzureIoTClient | 77:e4e36df9caee | 59 | result = NULL; |
AzureIoTClient | 77:e4e36df9caee | 60 | } |
AzureIoTClient | 89:a2ed767a532e | 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 | 88:248736be106e | 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 | 88:248736be106e | 92 | double number; |
AzureIoTClient | 88:248736be106e | 93 | double percentage; |
AzureIoTClient | 89:a2ed767a532e | 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 | 88:248736be106e | 126 | char* timeBuffer; |
AzureIoTClient | 89:a2ed767a532e | 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 | 92:97148cf9aa2a | 144 | free(timeBuffer); |
AzureIoTClient | 77:e4e36df9caee | 145 | result = NULL; |
AzureIoTClient | 77:e4e36df9caee | 146 | } |
AzureIoTClient | 77:e4e36df9caee | 147 | else |
AzureIoTClient | 77:e4e36df9caee | 148 | { |
AzureIoTClient | 77:e4e36df9caee | 149 | result->diagnosticCreationTimeUtc = timeBuffer; |
AzureIoTClient | 77:e4e36df9caee | 150 | } |
AzureIoTClient | 77:e4e36df9caee | 151 | } |
AzureIoTClient | 77:e4e36df9caee | 152 | } |
AzureIoTClient | 77:e4e36df9caee | 153 | return result; |
AzureIoTClient | 77:e4e36df9caee | 154 | } |
AzureIoTClient | 77:e4e36df9caee | 155 | |
AzureIoTClient | 77:e4e36df9caee | 156 | int IoTHubClient_Diagnostic_AddIfNecessary(IOTHUB_DIAGNOSTIC_SETTING_DATA* diagSetting, IOTHUB_MESSAGE_HANDLE messageHandle) |
AzureIoTClient | 77:e4e36df9caee | 157 | { |
AzureIoTClient | 77:e4e36df9caee | 158 | int result; |
AzureIoTClient | 77:e4e36df9caee | 159 | /* Codes_SRS_IOTHUB_DIAGNOSTIC_13_001: [ IoTHubClient_Diagnostic_AddIfNecessary should return nonezero if diagSetting or messageHandle is NULL. ]*/ |
AzureIoTClient | 77:e4e36df9caee | 160 | if (diagSetting == NULL || messageHandle == NULL) |
AzureIoTClient | 77:e4e36df9caee | 161 | { |
AzureIoTClient | 77:e4e36df9caee | 162 | result = __FAILURE__; |
AzureIoTClient | 77:e4e36df9caee | 163 | } |
AzureIoTClient | 77:e4e36df9caee | 164 | /* 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 | 165 | else if (should_add_diagnostic_info(diagSetting)) |
AzureIoTClient | 77:e4e36df9caee | 166 | { |
AzureIoTClient | 77:e4e36df9caee | 167 | /* Codes_SRS_IOTHUB_DIAGNOSTIC_13_004: [ If diagSamplingPercentage is equal to 100, diagnostic properties should be added to all messages]*/ |
AzureIoTClient | 77:e4e36df9caee | 168 | /* Codes_SRS_IOTHUB_DIAGNOSTIC_13_005: [ If diagSamplingPercentage is between(0, 100), diagnostic properties should be added based on percentage]*/ |
AzureIoTClient | 77:e4e36df9caee | 169 | |
AzureIoTClient | 77:e4e36df9caee | 170 | IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* diagnosticData; |
AzureIoTClient | 77:e4e36df9caee | 171 | if ((diagnosticData = prepare_message_diagnostic_data()) == NULL) |
AzureIoTClient | 77:e4e36df9caee | 172 | { |
AzureIoTClient | 77:e4e36df9caee | 173 | result = __FAILURE__; |
AzureIoTClient | 77:e4e36df9caee | 174 | } |
AzureIoTClient | 77:e4e36df9caee | 175 | else |
AzureIoTClient | 77:e4e36df9caee | 176 | { |
AzureIoTClient | 77:e4e36df9caee | 177 | if (IoTHubMessage_SetDiagnosticPropertyData(messageHandle, diagnosticData) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 77:e4e36df9caee | 178 | { |
AzureIoTClient | 77:e4e36df9caee | 179 | /* Codes_SRS_IOTHUB_DIAGNOSTIC_13_002: [ IoTHubClient_Diagnostic_AddIfNecessary should return nonezero if failing to add diagnostic property. ]*/ |
AzureIoTClient | 77:e4e36df9caee | 180 | result = __FAILURE__; |
AzureIoTClient | 77:e4e36df9caee | 181 | } |
AzureIoTClient | 77:e4e36df9caee | 182 | else |
AzureIoTClient | 77:e4e36df9caee | 183 | { |
AzureIoTClient | 77:e4e36df9caee | 184 | result = 0; |
AzureIoTClient | 77:e4e36df9caee | 185 | } |
AzureIoTClient | 77:e4e36df9caee | 186 | |
AzureIoTClient | 77:e4e36df9caee | 187 | free(diagnosticData->diagnosticCreationTimeUtc); |
AzureIoTClient | 77:e4e36df9caee | 188 | free(diagnosticData->diagnosticId); |
AzureIoTClient | 77:e4e36df9caee | 189 | free(diagnosticData); |
AzureIoTClient | 77:e4e36df9caee | 190 | diagnosticData = NULL; |
AzureIoTClient | 77:e4e36df9caee | 191 | } |
AzureIoTClient | 77:e4e36df9caee | 192 | } |
AzureIoTClient | 77:e4e36df9caee | 193 | else |
AzureIoTClient | 77:e4e36df9caee | 194 | { |
AzureIoTClient | 77:e4e36df9caee | 195 | result = 0; |
AzureIoTClient | 77:e4e36df9caee | 196 | } |
AzureIoTClient | 77:e4e36df9caee | 197 | |
AzureIoTClient | 77:e4e36df9caee | 198 | return result; |
AzureIoTClient | 77:e4e36df9caee | 199 | } |