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_ll.c@9:3ec7e2695f98, 2015-09-22 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Tue Sep 22 20:36:48 2015 -0700
- Revision:
- 9:3ec7e2695f98
- Parent:
- 1:bec6dd049b54
- Child:
- 10:38383e246675
New release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 0:e393db310d89 | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 0:e393db310d89 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 0:e393db310d89 | 3 | |
AzureIoTClient | 0:e393db310d89 | 4 | #include <stdlib.h> |
AzureIoTClient | 0:e393db310d89 | 5 | #ifdef _CRTDBG_MAP_ALLOC |
AzureIoTClient | 0:e393db310d89 | 6 | #include <crtdbg.h> |
AzureIoTClient | 0:e393db310d89 | 7 | #endif |
AzureIoTClient | 0:e393db310d89 | 8 | #include "gballoc.h" |
AzureIoTClient | 0:e393db310d89 | 9 | #include "string.h" |
AzureIoTClient | 0:e393db310d89 | 10 | #include "string_tokenizer.h" |
AzureIoTClient | 0:e393db310d89 | 11 | |
AzureIoTClient | 0:e393db310d89 | 12 | #include "iothub_client_ll.h" |
AzureIoTClient | 0:e393db310d89 | 13 | #include "iothub_client_private.h" |
AzureIoTClient | 0:e393db310d89 | 14 | #include "doublylinkedlist.h" |
AzureIoTClient | 1:bec6dd049b54 | 15 | #include "version.h" |
AzureIoTClient | 0:e393db310d89 | 16 | |
AzureIoTClient | 0:e393db310d89 | 17 | #include "iot_logging.h" |
AzureIoTClient | 0:e393db310d89 | 18 | |
AzureIoTClient | 0:e393db310d89 | 19 | #define LOG_ERROR LogError("result = %s\r\n", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result)); |
AzureIoTClient | 0:e393db310d89 | 20 | #define INDEFINITE_TIME ((time_t)(-1)) |
AzureIoTClient | 0:e393db310d89 | 21 | |
AzureIoTClient | 0:e393db310d89 | 22 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_RESULT_VALUES); |
AzureIoTClient | 0:e393db310d89 | 23 | |
AzureIoTClient | 0:e393db310d89 | 24 | typedef struct IOTHUB_CLIENT_LL_HANDLE_DATA_TAG |
AzureIoTClient | 0:e393db310d89 | 25 | { |
AzureIoTClient | 0:e393db310d89 | 26 | DLIST_ENTRY waitingToSend; |
AzureIoTClient | 0:e393db310d89 | 27 | TRANSPORT_HANDLE transportHandle; |
AzureIoTClient | 0:e393db310d89 | 28 | TRANSPORT_PROVIDER_FIELDS; |
AzureIoTClient | 9:3ec7e2695f98 | 29 | IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback; |
AzureIoTClient | 9:3ec7e2695f98 | 30 | void* messageUserContextCallback; |
AzureIoTClient | 9:3ec7e2695f98 | 31 | time_t lastMessageReceiveTime; |
AzureIoTClient | 0:e393db310d89 | 32 | }IOTHUB_CLIENT_LL_HANDLE_DATA; |
AzureIoTClient | 0:e393db310d89 | 33 | |
AzureIoTClient | 0:e393db310d89 | 34 | static const char HOSTNAME_TOKEN[] = "HostName"; |
AzureIoTClient | 0:e393db310d89 | 35 | static const char DEVICEID_TOKEN[] = "DeviceId"; |
AzureIoTClient | 0:e393db310d89 | 36 | static const char DEVICEKEY_TOKEN[] = "SharedAccessKey"; |
AzureIoTClient | 9:3ec7e2695f98 | 37 | static const char PROTOCOL_GATEWAY_HOST[] = "GatewayHostName"; |
AzureIoTClient | 0:e393db310d89 | 38 | |
AzureIoTClient | 0:e393db310d89 | 39 | IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_CreateFromConnectionString(const char* connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol) |
AzureIoTClient | 0:e393db310d89 | 40 | { |
AzureIoTClient | 0:e393db310d89 | 41 | IOTHUB_CLIENT_LL_HANDLE* result = NULL; |
AzureIoTClient | 0:e393db310d89 | 42 | |
AzureIoTClient | 1:bec6dd049b54 | 43 | /*Codes_SRS_IOTHUBCLIENT_LL_05_001: [IoTHubClient_LL_CreateFromConnectionString shall obtain the version string by a call to IoTHubClient_GetVersionString.]*/ |
AzureIoTClient | 1:bec6dd049b54 | 44 | /*Codes_SRS_IOTHUBCLIENT_LL_05_002: [IoTHubClient_LL_CreateFromConnectionString shall print the version string to standard output.]*/ |
AzureIoTClient | 1:bec6dd049b54 | 45 | LogInfo("IoT Hub SDK for C, version %s\r\n", IoTHubClient_GetVersionString()); |
AzureIoTClient | 1:bec6dd049b54 | 46 | |
AzureIoTClient | 0:e393db310d89 | 47 | /* SRS_IOTHUBCLIENT_LL_12_003: [IoTHubClient_LL_CreateFromConnectionString shall verify the input parameter and if it is NULL then return NULL] */ |
AzureIoTClient | 0:e393db310d89 | 48 | if (connectionString == NULL) |
AzureIoTClient | 0:e393db310d89 | 49 | { |
AzureIoTClient | 0:e393db310d89 | 50 | LogError("Input parameter is NULL: connectionString\r\n"); |
AzureIoTClient | 0:e393db310d89 | 51 | } |
AzureIoTClient | 0:e393db310d89 | 52 | else if (protocol == NULL) |
AzureIoTClient | 0:e393db310d89 | 53 | { |
AzureIoTClient | 0:e393db310d89 | 54 | LogError("Input parameter is NULL: protocol\r\n"); |
AzureIoTClient | 0:e393db310d89 | 55 | } |
AzureIoTClient | 0:e393db310d89 | 56 | else |
AzureIoTClient | 0:e393db310d89 | 57 | { |
AzureIoTClient | 0:e393db310d89 | 58 | /* SRS_IOTHUBCLIENT_LL_12_004: [IoTHubClient_LL_CreateFromConnectionString shall allocate IOTHUB_CLIENT_CONFIG structure] */ |
AzureIoTClient | 0:e393db310d89 | 59 | IOTHUB_CLIENT_CONFIG* config = malloc(sizeof(IOTHUB_CLIENT_CONFIG)); |
AzureIoTClient | 0:e393db310d89 | 60 | if (config == NULL) |
AzureIoTClient | 0:e393db310d89 | 61 | { |
AzureIoTClient | 0:e393db310d89 | 62 | /* SRS_IOTHUBCLIENT_LL_12_012: [If the allocation failed IoTHubClient_LL_CreateFromConnectionString returns NULL] */ |
AzureIoTClient | 0:e393db310d89 | 63 | LogError("Malloc failed\r\n"); |
AzureIoTClient | 0:e393db310d89 | 64 | return NULL; |
AzureIoTClient | 0:e393db310d89 | 65 | } |
AzureIoTClient | 0:e393db310d89 | 66 | else |
AzureIoTClient | 0:e393db310d89 | 67 | { |
AzureIoTClient | 0:e393db310d89 | 68 | STRING_TOKENIZER_HANDLE tokenizer1 = NULL; |
AzureIoTClient | 0:e393db310d89 | 69 | STRING_HANDLE connString = NULL; |
AzureIoTClient | 0:e393db310d89 | 70 | STRING_HANDLE tokenString = NULL; |
AzureIoTClient | 0:e393db310d89 | 71 | STRING_HANDLE valueString = NULL; |
AzureIoTClient | 0:e393db310d89 | 72 | STRING_HANDLE hostNameString = NULL; |
AzureIoTClient | 0:e393db310d89 | 73 | STRING_HANDLE hostSuffixString = NULL; |
AzureIoTClient | 0:e393db310d89 | 74 | STRING_HANDLE deviceIdString = NULL; |
AzureIoTClient | 0:e393db310d89 | 75 | STRING_HANDLE deviceKeyString = NULL; |
AzureIoTClient | 9:3ec7e2695f98 | 76 | STRING_HANDLE protocolGateway = NULL; |
AzureIoTClient | 0:e393db310d89 | 77 | |
AzureIoTClient | 0:e393db310d89 | 78 | config->protocol = protocol; |
AzureIoTClient | 0:e393db310d89 | 79 | |
AzureIoTClient | 0:e393db310d89 | 80 | config->iotHubName = NULL; |
AzureIoTClient | 0:e393db310d89 | 81 | config->iotHubSuffix = NULL; |
AzureIoTClient | 0:e393db310d89 | 82 | config->deviceId = NULL; |
AzureIoTClient | 0:e393db310d89 | 83 | config->deviceKey = NULL; |
AzureIoTClient | 0:e393db310d89 | 84 | |
AzureIoTClient | 0:e393db310d89 | 85 | if ((connString = STRING_construct(connectionString)) == NULL) |
AzureIoTClient | 0:e393db310d89 | 86 | { |
AzureIoTClient | 0:e393db310d89 | 87 | LogError("Error constructing connectiong String\r\n"); |
AzureIoTClient | 0:e393db310d89 | 88 | } |
AzureIoTClient | 0:e393db310d89 | 89 | else if ((tokenizer1 = STRING_TOKENIZER_create(connString)) == NULL) |
AzureIoTClient | 0:e393db310d89 | 90 | { |
AzureIoTClient | 0:e393db310d89 | 91 | LogError("Error creating Tokenizer\r\n"); |
AzureIoTClient | 0:e393db310d89 | 92 | } |
AzureIoTClient | 0:e393db310d89 | 93 | else if ((tokenString = STRING_new()) == NULL) |
AzureIoTClient | 0:e393db310d89 | 94 | { |
AzureIoTClient | 0:e393db310d89 | 95 | LogError("Error creating Token String\r\n"); |
AzureIoTClient | 0:e393db310d89 | 96 | } |
AzureIoTClient | 0:e393db310d89 | 97 | else if ((valueString = STRING_new()) == NULL) |
AzureIoTClient | 0:e393db310d89 | 98 | { |
AzureIoTClient | 0:e393db310d89 | 99 | LogError("Error creating Value String\r\n"); |
AzureIoTClient | 0:e393db310d89 | 100 | } |
AzureIoTClient | 0:e393db310d89 | 101 | else if ((hostNameString = STRING_new()) == NULL) |
AzureIoTClient | 0:e393db310d89 | 102 | { |
AzureIoTClient | 0:e393db310d89 | 103 | LogError("Error creating HostName String\r\n"); |
AzureIoTClient | 0:e393db310d89 | 104 | } |
AzureIoTClient | 0:e393db310d89 | 105 | else if ((hostSuffixString = STRING_new()) == NULL) |
AzureIoTClient | 0:e393db310d89 | 106 | { |
AzureIoTClient | 0:e393db310d89 | 107 | LogError("Error creating HostSuffix String\r\n"); |
AzureIoTClient | 0:e393db310d89 | 108 | } |
AzureIoTClient | 0:e393db310d89 | 109 | /* SRS_IOTHUBCLIENT_LL_12_005: [IoTHubClient_LL_CreateFromConnectionString shall try to parse the connectionString input parameter for the following structure: "Key1=value1;key2=value2;key3=value3..."] */ |
AzureIoTClient | 9:3ec7e2695f98 | 110 | /* SRS_IOTHUBCLIENT_LL_12_006: [IoTHubClient_LL_CreateFromConnectionString shall verify the existence of the following Key/Value pairs in the connection string: HostName, DeviceId, SharedAccessKey.] */ |
AzureIoTClient | 0:e393db310d89 | 111 | else |
AzureIoTClient | 0:e393db310d89 | 112 | { |
AzureIoTClient | 0:e393db310d89 | 113 | while ((STRING_TOKENIZER_get_next_token(tokenizer1, tokenString, "=") == 0)) |
AzureIoTClient | 0:e393db310d89 | 114 | { |
AzureIoTClient | 0:e393db310d89 | 115 | if (STRING_TOKENIZER_get_next_token(tokenizer1, valueString, ";") != 0) |
AzureIoTClient | 0:e393db310d89 | 116 | { |
AzureIoTClient | 0:e393db310d89 | 117 | LogError("Tokenizer error\r\n"); |
AzureIoTClient | 0:e393db310d89 | 118 | break; |
AzureIoTClient | 0:e393db310d89 | 119 | } |
AzureIoTClient | 0:e393db310d89 | 120 | else |
AzureIoTClient | 0:e393db310d89 | 121 | { |
AzureIoTClient | 0:e393db310d89 | 122 | if (tokenString != NULL) |
AzureIoTClient | 0:e393db310d89 | 123 | { |
AzureIoTClient | 0:e393db310d89 | 124 | /* SRS_IOTHUBCLIENT_LL_12_010: [IoTHubClient_LL_CreateFromConnectionString shall fill up the IOTHUB_CLIENT_CONFIG structure using the following mapping: iotHubName = Name, iotHubSuffix = Suffix, deviceId = DeviceId, deviceKey = SharedAccessKey] */ |
AzureIoTClient | 0:e393db310d89 | 125 | const char* s_token = STRING_c_str(tokenString); |
AzureIoTClient | 0:e393db310d89 | 126 | const char* s_value = STRING_c_str(valueString); |
AzureIoTClient | 9:3ec7e2695f98 | 127 | if (strcmp(s_token, HOSTNAME_TOKEN) == 0) |
AzureIoTClient | 0:e393db310d89 | 128 | { |
AzureIoTClient | 0:e393db310d89 | 129 | /* SRS_IOTHUBCLIENT_LL_12_009: [IoTHubClient_LL_CreateFromConnectionString shall split the value of HostName to Name and Suffix using the first "." as a separator] */ |
AzureIoTClient | 0:e393db310d89 | 130 | STRING_TOKENIZER_HANDLE tokenizer2 = NULL; |
AzureIoTClient | 0:e393db310d89 | 131 | if ((tokenizer2 = STRING_TOKENIZER_create(valueString)) == NULL) |
AzureIoTClient | 0:e393db310d89 | 132 | { |
AzureIoTClient | 0:e393db310d89 | 133 | LogError("Error creating Tokenizer\r\n"); |
AzureIoTClient | 0:e393db310d89 | 134 | break; |
AzureIoTClient | 0:e393db310d89 | 135 | } |
AzureIoTClient | 0:e393db310d89 | 136 | else |
AzureIoTClient | 0:e393db310d89 | 137 | { |
AzureIoTClient | 0:e393db310d89 | 138 | /* SRS_IOTHUBCLIENT_LL_12_015: [If the string split failed, IoTHubClient_LL_CreateFromConnectionString returns NULL ] */ |
AzureIoTClient | 0:e393db310d89 | 139 | if (STRING_TOKENIZER_get_next_token(tokenizer2, hostNameString, ".") != 0) |
AzureIoTClient | 0:e393db310d89 | 140 | { |
AzureIoTClient | 0:e393db310d89 | 141 | LogError("Tokenizer error\r\n"); |
AzureIoTClient | 0:e393db310d89 | 142 | STRING_TOKENIZER_destroy(tokenizer2); |
AzureIoTClient | 0:e393db310d89 | 143 | break; |
AzureIoTClient | 0:e393db310d89 | 144 | } |
AzureIoTClient | 0:e393db310d89 | 145 | else |
AzureIoTClient | 0:e393db310d89 | 146 | { |
AzureIoTClient | 0:e393db310d89 | 147 | config->iotHubName = STRING_c_str(hostNameString); |
AzureIoTClient | 0:e393db310d89 | 148 | if (STRING_TOKENIZER_get_next_token(tokenizer2, hostSuffixString, ";") != 0) |
AzureIoTClient | 0:e393db310d89 | 149 | { |
AzureIoTClient | 0:e393db310d89 | 150 | LogError("Tokenizer error\r\n"); |
AzureIoTClient | 0:e393db310d89 | 151 | STRING_TOKENIZER_destroy(tokenizer2); |
AzureIoTClient | 0:e393db310d89 | 152 | break; |
AzureIoTClient | 0:e393db310d89 | 153 | } |
AzureIoTClient | 0:e393db310d89 | 154 | else |
AzureIoTClient | 0:e393db310d89 | 155 | { |
AzureIoTClient | 0:e393db310d89 | 156 | config->iotHubSuffix = STRING_c_str(hostSuffixString); |
AzureIoTClient | 0:e393db310d89 | 157 | } |
AzureIoTClient | 0:e393db310d89 | 158 | } |
AzureIoTClient | 0:e393db310d89 | 159 | STRING_TOKENIZER_destroy(tokenizer2); |
AzureIoTClient | 0:e393db310d89 | 160 | } |
AzureIoTClient | 0:e393db310d89 | 161 | } |
AzureIoTClient | 0:e393db310d89 | 162 | else if (strcmp(s_token, DEVICEID_TOKEN) == 0) |
AzureIoTClient | 0:e393db310d89 | 163 | { |
AzureIoTClient | 0:e393db310d89 | 164 | deviceIdString = STRING_clone(valueString); |
AzureIoTClient | 0:e393db310d89 | 165 | if (deviceIdString != NULL) |
AzureIoTClient | 0:e393db310d89 | 166 | { |
AzureIoTClient | 0:e393db310d89 | 167 | config->deviceId = STRING_c_str(deviceIdString); |
AzureIoTClient | 0:e393db310d89 | 168 | } |
AzureIoTClient | 0:e393db310d89 | 169 | } |
AzureIoTClient | 0:e393db310d89 | 170 | else if (strcmp(s_token, DEVICEKEY_TOKEN) == 0) |
AzureIoTClient | 0:e393db310d89 | 171 | { |
AzureIoTClient | 0:e393db310d89 | 172 | deviceKeyString = STRING_clone(valueString); |
AzureIoTClient | 0:e393db310d89 | 173 | if (deviceKeyString != NULL) |
AzureIoTClient | 0:e393db310d89 | 174 | { |
AzureIoTClient | 0:e393db310d89 | 175 | config->deviceKey = STRING_c_str(deviceKeyString); |
AzureIoTClient | 0:e393db310d89 | 176 | } |
AzureIoTClient | 0:e393db310d89 | 177 | } |
AzureIoTClient | 9:3ec7e2695f98 | 178 | else if (strcmp(s_token, PROTOCOL_GATEWAY_HOST) == 0) |
AzureIoTClient | 9:3ec7e2695f98 | 179 | { |
AzureIoTClient | 9:3ec7e2695f98 | 180 | protocolGateway = STRING_clone(valueString); |
AzureIoTClient | 9:3ec7e2695f98 | 181 | if (protocolGateway != NULL) |
AzureIoTClient | 9:3ec7e2695f98 | 182 | { |
AzureIoTClient | 9:3ec7e2695f98 | 183 | config->protocolGatewayHostName = STRING_c_str(protocolGateway); |
AzureIoTClient | 9:3ec7e2695f98 | 184 | } |
AzureIoTClient | 9:3ec7e2695f98 | 185 | } |
AzureIoTClient | 0:e393db310d89 | 186 | } |
AzureIoTClient | 0:e393db310d89 | 187 | } |
AzureIoTClient | 0:e393db310d89 | 188 | } |
AzureIoTClient | 0:e393db310d89 | 189 | /* parsing is done - check the result */ |
AzureIoTClient | 9:3ec7e2695f98 | 190 | if (config->iotHubName == NULL) |
AzureIoTClient | 0:e393db310d89 | 191 | { |
AzureIoTClient | 0:e393db310d89 | 192 | LogError("iotHubName is not found\r\n"); |
AzureIoTClient | 0:e393db310d89 | 193 | } |
AzureIoTClient | 0:e393db310d89 | 194 | else if (config->iotHubSuffix == NULL) |
AzureIoTClient | 0:e393db310d89 | 195 | { |
AzureIoTClient | 0:e393db310d89 | 196 | LogError("iotHubSuffix is not found\r\n"); |
AzureIoTClient | 0:e393db310d89 | 197 | } |
AzureIoTClient | 0:e393db310d89 | 198 | else if (config->deviceId == NULL) |
AzureIoTClient | 0:e393db310d89 | 199 | { |
AzureIoTClient | 0:e393db310d89 | 200 | LogError("deviceId is not found\r\n"); |
AzureIoTClient | 0:e393db310d89 | 201 | } |
AzureIoTClient | 0:e393db310d89 | 202 | else if (config->deviceKey == NULL) |
AzureIoTClient | 0:e393db310d89 | 203 | { |
AzureIoTClient | 0:e393db310d89 | 204 | LogError("deviceId is not found\r\n"); |
AzureIoTClient | 0:e393db310d89 | 205 | } |
AzureIoTClient | 0:e393db310d89 | 206 | else |
AzureIoTClient | 0:e393db310d89 | 207 | { |
AzureIoTClient | 0:e393db310d89 | 208 | /* SRS_IOTHUBCLIENT_LL_12_011: [IoTHubClient_LL_CreateFromConnectionString shall call into the IoTHubClient_LL_Create API with the current structure and returns with the return value of it] */ |
AzureIoTClient | 0:e393db310d89 | 209 | result = IoTHubClient_LL_Create(config); |
AzureIoTClient | 0:e393db310d89 | 210 | if (result == NULL) |
AzureIoTClient | 0:e393db310d89 | 211 | { |
AzureIoTClient | 0:e393db310d89 | 212 | LogError("IoTHubClient_LL_Create failed\r\n"); |
AzureIoTClient | 0:e393db310d89 | 213 | } |
AzureIoTClient | 0:e393db310d89 | 214 | } |
AzureIoTClient | 0:e393db310d89 | 215 | } |
AzureIoTClient | 0:e393db310d89 | 216 | if (deviceKeyString != NULL) |
AzureIoTClient | 0:e393db310d89 | 217 | STRING_delete(deviceKeyString); |
AzureIoTClient | 0:e393db310d89 | 218 | if (deviceIdString != NULL) |
AzureIoTClient | 0:e393db310d89 | 219 | STRING_delete(deviceIdString); |
AzureIoTClient | 0:e393db310d89 | 220 | if (hostSuffixString != NULL) |
AzureIoTClient | 0:e393db310d89 | 221 | STRING_delete(hostSuffixString); |
AzureIoTClient | 0:e393db310d89 | 222 | if (hostNameString != NULL) |
AzureIoTClient | 0:e393db310d89 | 223 | STRING_delete(hostNameString); |
AzureIoTClient | 0:e393db310d89 | 224 | if (valueString != NULL) |
AzureIoTClient | 0:e393db310d89 | 225 | STRING_delete(valueString); |
AzureIoTClient | 0:e393db310d89 | 226 | if (tokenString != NULL) |
AzureIoTClient | 0:e393db310d89 | 227 | STRING_delete(tokenString); |
AzureIoTClient | 0:e393db310d89 | 228 | if (connString != NULL) |
AzureIoTClient | 0:e393db310d89 | 229 | STRING_delete(connString); |
AzureIoTClient | 9:3ec7e2695f98 | 230 | if (protocolGateway != NULL) |
AzureIoTClient | 9:3ec7e2695f98 | 231 | STRING_delete(protocolGateway); |
AzureIoTClient | 0:e393db310d89 | 232 | |
AzureIoTClient | 0:e393db310d89 | 233 | if (tokenizer1 != NULL) |
AzureIoTClient | 0:e393db310d89 | 234 | STRING_TOKENIZER_destroy(tokenizer1); |
AzureIoTClient | 0:e393db310d89 | 235 | |
AzureIoTClient | 0:e393db310d89 | 236 | free(config); |
AzureIoTClient | 0:e393db310d89 | 237 | } |
AzureIoTClient | 0:e393db310d89 | 238 | } |
AzureIoTClient | 0:e393db310d89 | 239 | return result; |
AzureIoTClient | 0:e393db310d89 | 240 | } |
AzureIoTClient | 0:e393db310d89 | 241 | |
AzureIoTClient | 0:e393db310d89 | 242 | |
AzureIoTClient | 0:e393db310d89 | 243 | IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_Create(const IOTHUB_CLIENT_CONFIG* config) |
AzureIoTClient | 0:e393db310d89 | 244 | { |
AzureIoTClient | 0:e393db310d89 | 245 | IOTHUB_CLIENT_LL_HANDLE result; |
AzureIoTClient | 0:e393db310d89 | 246 | /*Codes_SRS_IOTHUBCLIENT_LL_02_001: [IoTHubClient_LL_Create shall return NULL if config parameter is NULL or protocol field is NULL.]*/ |
AzureIoTClient | 0:e393db310d89 | 247 | if ( |
AzureIoTClient | 0:e393db310d89 | 248 | (config == NULL) || |
AzureIoTClient | 0:e393db310d89 | 249 | (config->protocol == NULL) |
AzureIoTClient | 0:e393db310d89 | 250 | ) |
AzureIoTClient | 0:e393db310d89 | 251 | { |
AzureIoTClient | 0:e393db310d89 | 252 | result = NULL; |
AzureIoTClient | 0:e393db310d89 | 253 | LogError("invalid configuration (NULL detected)\r\n"); |
AzureIoTClient | 0:e393db310d89 | 254 | } |
AzureIoTClient | 0:e393db310d89 | 255 | else |
AzureIoTClient | 0:e393db310d89 | 256 | { |
AzureIoTClient | 0:e393db310d89 | 257 | IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)malloc(sizeof(IOTHUB_CLIENT_LL_HANDLE_DATA)); |
AzureIoTClient | 0:e393db310d89 | 258 | if (handleData == NULL) |
AzureIoTClient | 0:e393db310d89 | 259 | { |
AzureIoTClient | 0:e393db310d89 | 260 | LogError("malloc failed\r\n"); |
AzureIoTClient | 0:e393db310d89 | 261 | result = NULL; |
AzureIoTClient | 0:e393db310d89 | 262 | } |
AzureIoTClient | 0:e393db310d89 | 263 | else |
AzureIoTClient | 0:e393db310d89 | 264 | { |
AzureIoTClient | 0:e393db310d89 | 265 | /*Codes_SRS_IOTHUBCLIENT_LL_02_004: [Otherwise IoTHubClient_LL_Create shall initialize a new DLIST (further called "waitingToSend") containing records with fields of the following types: IOTHUB_MESSAGE_HANDLE, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK, void*.]*/ |
AzureIoTClient | 0:e393db310d89 | 266 | IOTHUBTRANSPORT_CONFIG lowerLayerConfig; |
AzureIoTClient | 0:e393db310d89 | 267 | DList_InitializeListHead(&(handleData->waitingToSend)); |
AzureIoTClient | 0:e393db310d89 | 268 | handleData->IoTHubTransport_SetOption = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_SetOption; |
AzureIoTClient | 0:e393db310d89 | 269 | handleData->IoTHubTransport_Create = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_Create; |
AzureIoTClient | 0:e393db310d89 | 270 | handleData->IoTHubTransport_Destroy = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_Destroy; |
AzureIoTClient | 0:e393db310d89 | 271 | handleData->IoTHubTransport_Subscribe = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_Subscribe; |
AzureIoTClient | 0:e393db310d89 | 272 | handleData->IoTHubTransport_Unsubscribe = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_Unsubscribe; |
AzureIoTClient | 0:e393db310d89 | 273 | handleData->IoTHubTransport_DoWork = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_DoWork; |
AzureIoTClient | 0:e393db310d89 | 274 | handleData->IoTHubTransport_GetSendStatus = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_GetSendStatus; |
AzureIoTClient | 9:3ec7e2695f98 | 275 | handleData->messageCallback = NULL; |
AzureIoTClient | 9:3ec7e2695f98 | 276 | handleData->messageUserContextCallback = NULL; |
AzureIoTClient | 9:3ec7e2695f98 | 277 | handleData->lastMessageReceiveTime = INDEFINITE_TIME; |
AzureIoTClient | 0:e393db310d89 | 278 | /*Codes_SRS_IOTHUBCLIENT_LL_02_006: [IoTHubClient_LL_Create shall populate a structure of type IOTHUBTRANSPORT_CONFIG with the information from config parameter and the previous DLIST and shall pass that to the underlying layer _Create function.]*/ |
AzureIoTClient | 0:e393db310d89 | 279 | lowerLayerConfig.upperConfig = config; |
AzureIoTClient | 0:e393db310d89 | 280 | lowerLayerConfig.waitingToSend = &(handleData->waitingToSend); |
AzureIoTClient | 0:e393db310d89 | 281 | /*Codes_SRS_IOTHUBCLIENT_LL_02_007: [If the underlaying layer _Create function fails them IoTHubClient_LL_Create shall fail and return NULL.] */ |
AzureIoTClient | 0:e393db310d89 | 282 | if ((handleData->transportHandle = handleData->IoTHubTransport_Create(&lowerLayerConfig)) == NULL) |
AzureIoTClient | 0:e393db310d89 | 283 | { |
AzureIoTClient | 0:e393db310d89 | 284 | LogError("underlying transport failed\r\n"); |
AzureIoTClient | 0:e393db310d89 | 285 | free(handleData); |
AzureIoTClient | 0:e393db310d89 | 286 | result = NULL; |
AzureIoTClient | 0:e393db310d89 | 287 | } |
AzureIoTClient | 0:e393db310d89 | 288 | else |
AzureIoTClient | 0:e393db310d89 | 289 | { |
AzureIoTClient | 0:e393db310d89 | 290 | /*Codes_SRS_IOTHUBCLIENT_LL_02_008: [Otherwise, IoTHubClient_LL_Create shall succeed and return a non-NULL handle.] */ |
AzureIoTClient | 0:e393db310d89 | 291 | result = handleData; |
AzureIoTClient | 0:e393db310d89 | 292 | } |
AzureIoTClient | 0:e393db310d89 | 293 | } |
AzureIoTClient | 0:e393db310d89 | 294 | } |
AzureIoTClient | 0:e393db310d89 | 295 | |
AzureIoTClient | 0:e393db310d89 | 296 | return result; |
AzureIoTClient | 0:e393db310d89 | 297 | } |
AzureIoTClient | 0:e393db310d89 | 298 | |
AzureIoTClient | 0:e393db310d89 | 299 | void IoTHubClient_LL_Destroy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 0:e393db310d89 | 300 | { |
AzureIoTClient | 0:e393db310d89 | 301 | /*Codes_SRS_IOTHUBCLIENT_LL_02_009: [IoTHubClient_LL_Destroy shall do nothing if parameter iotHubClientHandle is NULL.]*/ |
AzureIoTClient | 0:e393db310d89 | 302 | if (iotHubClientHandle != NULL) |
AzureIoTClient | 0:e393db310d89 | 303 | { |
AzureIoTClient | 0:e393db310d89 | 304 | PDLIST_ENTRY unsend; |
AzureIoTClient | 0:e393db310d89 | 305 | /*Codes_SRS_IOTHUBCLIENT_LL_02_010: [IoTHubClient_LL_Destroy it shall call the underlaying layer's _Destroy function and shall free the resources allocated by IoTHubClient (if any).] */ |
AzureIoTClient | 0:e393db310d89 | 306 | IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 0:e393db310d89 | 307 | handleData->IoTHubTransport_Destroy(handleData->transportHandle); |
AzureIoTClient | 0:e393db310d89 | 308 | /*if any, remove the items currently not send*/ |
AzureIoTClient | 0:e393db310d89 | 309 | while ((unsend = DList_RemoveHeadList(&(handleData->waitingToSend))) != &(handleData->waitingToSend)) |
AzureIoTClient | 0:e393db310d89 | 310 | { |
AzureIoTClient | 0:e393db310d89 | 311 | IOTHUB_MESSAGE_LIST* temp = containingRecord(unsend, IOTHUB_MESSAGE_LIST, entry); |
AzureIoTClient | 9:3ec7e2695f98 | 312 | /*Codes_SRS_IOTHUBCLIENT_LL_02_033: [Otherwise, IoTHubClient_LL_Destroy shall complete all the event message callbacks that are in the waitingToSend list with the result IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY.] */ |
AzureIoTClient | 0:e393db310d89 | 313 | if (temp->callback != NULL) |
AzureIoTClient | 0:e393db310d89 | 314 | { |
AzureIoTClient | 0:e393db310d89 | 315 | temp->callback(IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY, temp->context); |
AzureIoTClient | 0:e393db310d89 | 316 | } |
AzureIoTClient | 0:e393db310d89 | 317 | IoTHubMessage_Destroy(temp->messageHandle); |
AzureIoTClient | 0:e393db310d89 | 318 | free(temp); |
AzureIoTClient | 0:e393db310d89 | 319 | } |
AzureIoTClient | 0:e393db310d89 | 320 | free(handleData); |
AzureIoTClient | 0:e393db310d89 | 321 | } |
AzureIoTClient | 0:e393db310d89 | 322 | } |
AzureIoTClient | 0:e393db310d89 | 323 | |
AzureIoTClient | 0:e393db310d89 | 324 | IOTHUB_CLIENT_RESULT IoTHubClient_LL_SendEventAsync(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_MESSAGE_HANDLE eventMessageHandle, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK eventConfirmationCallback, void* userContextCallback) |
AzureIoTClient | 0:e393db310d89 | 325 | { |
AzureIoTClient | 0:e393db310d89 | 326 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 0:e393db310d89 | 327 | /*Codes_SRS_IOTHUBCLIENT_LL_02_011: [IoTHubClient_LL_SendEventAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle or eventMessageHandle is NULL.]*/ |
AzureIoTClient | 0:e393db310d89 | 328 | if ( |
AzureIoTClient | 0:e393db310d89 | 329 | (iotHubClientHandle == NULL) || |
AzureIoTClient | 0:e393db310d89 | 330 | (eventMessageHandle == NULL) || |
AzureIoTClient | 0:e393db310d89 | 331 | /*Codes_SRS_IOTHUBCLIENT_LL_02_012: [IoTHubClient_LL_SendEventAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter eventConfirmationCallback is NULL and userContextCallback is not NULL.] */ |
AzureIoTClient | 0:e393db310d89 | 332 | ((eventConfirmationCallback == NULL) && (userContextCallback != NULL)) |
AzureIoTClient | 0:e393db310d89 | 333 | ) |
AzureIoTClient | 0:e393db310d89 | 334 | { |
AzureIoTClient | 0:e393db310d89 | 335 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 0:e393db310d89 | 336 | LOG_ERROR; |
AzureIoTClient | 0:e393db310d89 | 337 | } |
AzureIoTClient | 0:e393db310d89 | 338 | else |
AzureIoTClient | 0:e393db310d89 | 339 | { |
AzureIoTClient | 0:e393db310d89 | 340 | IOTHUB_MESSAGE_LIST *newEntry = (IOTHUB_MESSAGE_LIST*)malloc(sizeof(IOTHUB_MESSAGE_LIST)); |
AzureIoTClient | 0:e393db310d89 | 341 | if (newEntry == NULL) |
AzureIoTClient | 0:e393db310d89 | 342 | { |
AzureIoTClient | 0:e393db310d89 | 343 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 0:e393db310d89 | 344 | LOG_ERROR; |
AzureIoTClient | 0:e393db310d89 | 345 | } |
AzureIoTClient | 0:e393db310d89 | 346 | else |
AzureIoTClient | 0:e393db310d89 | 347 | { |
AzureIoTClient | 0:e393db310d89 | 348 | /*Codes_SRS_IOTHUBCLIENT_LL_02_013: [IoTHubClient_SendEventAsync shall add the DLIST waitingToSend a new record cloning the information from eventMessageHandle, eventConfirmationCallback, userContextCallback.]*/ |
AzureIoTClient | 0:e393db310d89 | 349 | if ((newEntry->messageHandle = IoTHubMessage_Clone(eventMessageHandle)) == NULL) |
AzureIoTClient | 0:e393db310d89 | 350 | { |
AzureIoTClient | 0:e393db310d89 | 351 | /*Codes_SRS_IOTHUBCLIENT_LL_02_014: [If cloning and/or adding the information fails for any reason, IoTHubClient_LL_SendEventAsync shall fail and return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 0:e393db310d89 | 352 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 0:e393db310d89 | 353 | free(newEntry); |
AzureIoTClient | 0:e393db310d89 | 354 | LOG_ERROR; |
AzureIoTClient | 0:e393db310d89 | 355 | } |
AzureIoTClient | 0:e393db310d89 | 356 | else |
AzureIoTClient | 0:e393db310d89 | 357 | { |
AzureIoTClient | 0:e393db310d89 | 358 | IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 0:e393db310d89 | 359 | /*Codes_SRS_IOTHUBCLIENT_LL_02_013: [IoTHubClient_SendEventAsync shall add the DLIST waitingToSend a new record cloning the information from eventMessageHandle, eventConfirmationCallback, userContextCallback.]*/ |
AzureIoTClient | 0:e393db310d89 | 360 | newEntry->callback = eventConfirmationCallback; |
AzureIoTClient | 0:e393db310d89 | 361 | newEntry->context = userContextCallback; |
AzureIoTClient | 0:e393db310d89 | 362 | DList_InsertTailList(&(handleData->waitingToSend), &(newEntry->entry)); |
AzureIoTClient | 0:e393db310d89 | 363 | /*Codes_SRS_IOTHUBCLIENT_LL_02_015: [Otherwise IoTHubClient_LL_SendEventAsync shall succeed and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 0:e393db310d89 | 364 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 0:e393db310d89 | 365 | } |
AzureIoTClient | 0:e393db310d89 | 366 | } |
AzureIoTClient | 0:e393db310d89 | 367 | } |
AzureIoTClient | 0:e393db310d89 | 368 | return result; |
AzureIoTClient | 0:e393db310d89 | 369 | } |
AzureIoTClient | 0:e393db310d89 | 370 | |
AzureIoTClient | 9:3ec7e2695f98 | 371 | IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetMessageCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback) |
AzureIoTClient | 0:e393db310d89 | 372 | { |
AzureIoTClient | 0:e393db310d89 | 373 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 9:3ec7e2695f98 | 374 | /*Codes_SRS_IOTHUBCLIENT_LL_02_016: [IoTHubClient_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.] */ |
AzureIoTClient | 0:e393db310d89 | 375 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 0:e393db310d89 | 376 | { |
AzureIoTClient | 0:e393db310d89 | 377 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 0:e393db310d89 | 378 | LOG_ERROR; |
AzureIoTClient | 0:e393db310d89 | 379 | } |
AzureIoTClient | 0:e393db310d89 | 380 | else |
AzureIoTClient | 0:e393db310d89 | 381 | { |
AzureIoTClient | 0:e393db310d89 | 382 | IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 9:3ec7e2695f98 | 383 | if (messageCallback == NULL) |
AzureIoTClient | 0:e393db310d89 | 384 | { |
AzureIoTClient | 9:3ec7e2695f98 | 385 | /*Codes_SRS_IOTHUBCLIENT_LL_02_019: [If parameter messageCallback is NULL then IoTHubClient_LL_SetMessageCallback shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 0:e393db310d89 | 386 | handleData->IoTHubTransport_Unsubscribe(handleData->transportHandle); |
AzureIoTClient | 9:3ec7e2695f98 | 387 | handleData->messageCallback = NULL; |
AzureIoTClient | 9:3ec7e2695f98 | 388 | handleData->messageUserContextCallback = NULL; |
AzureIoTClient | 0:e393db310d89 | 389 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 0:e393db310d89 | 390 | } |
AzureIoTClient | 0:e393db310d89 | 391 | else |
AzureIoTClient | 0:e393db310d89 | 392 | { |
AzureIoTClient | 9:3ec7e2695f98 | 393 | /*Codes_SRS_IOTHUBCLIENT_LL_02_017: [If parameter messageCallback is non-NULL then IoTHubClient_LL_SetMessageCallback shall call the underlying layer's _Subscribe function.]*/ |
AzureIoTClient | 0:e393db310d89 | 394 | if (handleData->IoTHubTransport_Subscribe(handleData->transportHandle) == 0) |
AzureIoTClient | 0:e393db310d89 | 395 | { |
AzureIoTClient | 9:3ec7e2695f98 | 396 | handleData->messageCallback = messageCallback; |
AzureIoTClient | 9:3ec7e2695f98 | 397 | handleData->messageUserContextCallback = userContextCallback; |
AzureIoTClient | 0:e393db310d89 | 398 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 0:e393db310d89 | 399 | } |
AzureIoTClient | 0:e393db310d89 | 400 | else |
AzureIoTClient | 0:e393db310d89 | 401 | { |
AzureIoTClient | 9:3ec7e2695f98 | 402 | handleData->messageCallback = NULL; |
AzureIoTClient | 9:3ec7e2695f98 | 403 | handleData->messageUserContextCallback = NULL; |
AzureIoTClient | 9:3ec7e2695f98 | 404 | /*Codes_SRS_IOTHUBCLIENT_LL_02_018: [If the underlying layer's _Subscribe function fails, then IoTHubClient_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_ERROR. Otherwise IoTHubClient_LL_SetMessageCallback shall succeed and return IOTHUB_CLIENT_OK.]*/ |
AzureIoTClient | 0:e393db310d89 | 405 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 0:e393db310d89 | 406 | } |
AzureIoTClient | 0:e393db310d89 | 407 | } |
AzureIoTClient | 0:e393db310d89 | 408 | } |
AzureIoTClient | 0:e393db310d89 | 409 | |
AzureIoTClient | 0:e393db310d89 | 410 | return result; |
AzureIoTClient | 0:e393db310d89 | 411 | } |
AzureIoTClient | 0:e393db310d89 | 412 | |
AzureIoTClient | 0:e393db310d89 | 413 | void IoTHubClient_LL_DoWork(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 0:e393db310d89 | 414 | { |
AzureIoTClient | 0:e393db310d89 | 415 | /*Codes_SRS_IOTHUBCLIENT_LL_02_020: [If parameter iotHubClientHandle is NULL then IoTHubClient_LL_DoWork shall not perform any action.] */ |
AzureIoTClient | 0:e393db310d89 | 416 | if (iotHubClientHandle != NULL) |
AzureIoTClient | 0:e393db310d89 | 417 | { |
AzureIoTClient | 0:e393db310d89 | 418 | IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 0:e393db310d89 | 419 | handleData->IoTHubTransport_DoWork(handleData->transportHandle, iotHubClientHandle); |
AzureIoTClient | 0:e393db310d89 | 420 | } |
AzureIoTClient | 0:e393db310d89 | 421 | } |
AzureIoTClient | 0:e393db310d89 | 422 | |
AzureIoTClient | 0:e393db310d89 | 423 | IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetSendStatus(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus) |
AzureIoTClient | 0:e393db310d89 | 424 | { |
AzureIoTClient | 0:e393db310d89 | 425 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 0:e393db310d89 | 426 | |
AzureIoTClient | 0:e393db310d89 | 427 | /* Codes_SRS_IOTHUBCLIENT_09_007: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter] */ |
AzureIoTClient | 0:e393db310d89 | 428 | if (iotHubClientHandle == NULL || iotHubClientStatus == NULL) |
AzureIoTClient | 0:e393db310d89 | 429 | { |
AzureIoTClient | 0:e393db310d89 | 430 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 0:e393db310d89 | 431 | LOG_ERROR; |
AzureIoTClient | 0:e393db310d89 | 432 | } |
AzureIoTClient | 0:e393db310d89 | 433 | else |
AzureIoTClient | 0:e393db310d89 | 434 | { |
AzureIoTClient | 0:e393db310d89 | 435 | IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 0:e393db310d89 | 436 | |
AzureIoTClient | 0:e393db310d89 | 437 | /* Codes_SRS_IOTHUBCLIENT_09_008: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_IDLE if there is currently no items to be sent] */ |
AzureIoTClient | 0:e393db310d89 | 438 | /* Codes_SRS_IOTHUBCLIENT_09_009: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_BUSY if there are currently items to be sent] */ |
AzureIoTClient | 0:e393db310d89 | 439 | result = handleData->IoTHubTransport_GetSendStatus(handleData->transportHandle, iotHubClientStatus); |
AzureIoTClient | 0:e393db310d89 | 440 | } |
AzureIoTClient | 0:e393db310d89 | 441 | |
AzureIoTClient | 0:e393db310d89 | 442 | return result; |
AzureIoTClient | 0:e393db310d89 | 443 | } |
AzureIoTClient | 0:e393db310d89 | 444 | |
AzureIoTClient | 0:e393db310d89 | 445 | void IoTHubClient_LL_SendComplete(IOTHUB_CLIENT_LL_HANDLE handle, PDLIST_ENTRY completed, IOTHUB_BATCHSTATE_RESULT result) |
AzureIoTClient | 0:e393db310d89 | 446 | { |
AzureIoTClient | 0:e393db310d89 | 447 | /*Codes_SRS_IOTHUBCLIENT_LL_02_022: [If parameter completed is NULL, or parameter handle is NULL then IoTHubClient_LL_SendBatch shall return.]*/ |
AzureIoTClient | 0:e393db310d89 | 448 | if ( |
AzureIoTClient | 0:e393db310d89 | 449 | (handle == NULL) || |
AzureIoTClient | 0:e393db310d89 | 450 | (completed == NULL) |
AzureIoTClient | 0:e393db310d89 | 451 | ) |
AzureIoTClient | 0:e393db310d89 | 452 | { |
AzureIoTClient | 0:e393db310d89 | 453 | /*"shall return"*/ |
AzureIoTClient | 0:e393db310d89 | 454 | LogError("invalid arg\r\n"); |
AzureIoTClient | 0:e393db310d89 | 455 | } |
AzureIoTClient | 0:e393db310d89 | 456 | else |
AzureIoTClient | 0:e393db310d89 | 457 | { |
AzureIoTClient | 0:e393db310d89 | 458 | /*Codes_SRS_IOTHUBCLIENT_LL_02_027: [If parameter result is IOTHUB_BACTHSTATE_FAILED then IoTHubClient_LL_SendComplete shall call all the non-NULL callbacks with the result parameter set to IOTHUB_CLIENT_CONFIRMATION_ERROR and the context set to the context passed originally in the SendEventAsync call.] */ |
AzureIoTClient | 0:e393db310d89 | 459 | /*Codes_SRS_IOTHUBCLIENT_LL_02_025: [If parameter result is IOTHUB_BATCHSTATE_SUCCESS then IoTHubClient_LL_SendComplete shall call all the non-NULL callbacks with the result parameter set to IOTHUB_CLIENT_CONFIRMATION_OK and the context set to the context passed originally in the SendEventAsync call.]*/ |
AzureIoTClient | 0:e393db310d89 | 460 | IOTHUB_CLIENT_CONFIRMATION_RESULT resultToBeCalled = (result == IOTHUB_BATCHSTATE_SUCCESS) ? IOTHUB_CLIENT_CONFIRMATION_OK : IOTHUB_CLIENT_CONFIRMATION_ERROR; |
AzureIoTClient | 0:e393db310d89 | 461 | PDLIST_ENTRY oldest; |
AzureIoTClient | 0:e393db310d89 | 462 | while((oldest= DList_RemoveHeadList(completed))!=completed) |
AzureIoTClient | 0:e393db310d89 | 463 | { |
AzureIoTClient | 0:e393db310d89 | 464 | IOTHUB_MESSAGE_LIST* messageList = (IOTHUB_MESSAGE_LIST*)containingRecord(oldest, IOTHUB_MESSAGE_LIST, entry); |
AzureIoTClient | 0:e393db310d89 | 465 | if (messageList->callback != NULL) |
AzureIoTClient | 0:e393db310d89 | 466 | { |
AzureIoTClient | 0:e393db310d89 | 467 | messageList->callback(resultToBeCalled, messageList->context); |
AzureIoTClient | 0:e393db310d89 | 468 | } |
AzureIoTClient | 0:e393db310d89 | 469 | IoTHubMessage_Destroy(messageList->messageHandle); |
AzureIoTClient | 0:e393db310d89 | 470 | free(messageList); |
AzureIoTClient | 0:e393db310d89 | 471 | } |
AzureIoTClient | 0:e393db310d89 | 472 | } |
AzureIoTClient | 0:e393db310d89 | 473 | } |
AzureIoTClient | 0:e393db310d89 | 474 | |
AzureIoTClient | 9:3ec7e2695f98 | 475 | IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubClient_LL_MessageCallback(IOTHUB_CLIENT_LL_HANDLE handle, IOTHUB_MESSAGE_HANDLE message) |
AzureIoTClient | 0:e393db310d89 | 476 | { |
AzureIoTClient | 0:e393db310d89 | 477 | int result; |
AzureIoTClient | 9:3ec7e2695f98 | 478 | /*Codes_SRS_IOTHUBCLIENT_LL_02_029: [If parameter handle is NULL then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */ |
AzureIoTClient | 0:e393db310d89 | 479 | if (handle == NULL) |
AzureIoTClient | 0:e393db310d89 | 480 | { |
AzureIoTClient | 0:e393db310d89 | 481 | LogError("invalid argument\r\n"); |
AzureIoTClient | 0:e393db310d89 | 482 | result = IOTHUBMESSAGE_ABANDONED; |
AzureIoTClient | 0:e393db310d89 | 483 | } |
AzureIoTClient | 0:e393db310d89 | 484 | else |
AzureIoTClient | 0:e393db310d89 | 485 | { |
AzureIoTClient | 0:e393db310d89 | 486 | IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle; |
AzureIoTClient | 0:e393db310d89 | 487 | |
AzureIoTClient | 9:3ec7e2695f98 | 488 | /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */ |
AzureIoTClient | 9:3ec7e2695f98 | 489 | handleData->lastMessageReceiveTime = get_time(NULL); |
AzureIoTClient | 0:e393db310d89 | 490 | |
AzureIoTClient | 9:3ec7e2695f98 | 491 | /*Codes_SRS_IOTHUBCLIENT_LL_02_030: [IoTHubClient_LL_MessageCallback shall invoke the last callback function (the parameter messageCallback to IoTHubClient_LL_SetMessageCallback) passing the message and the passed userContextCallback.]*/ |
AzureIoTClient | 9:3ec7e2695f98 | 492 | if (handleData->messageCallback != NULL) |
AzureIoTClient | 0:e393db310d89 | 493 | { |
AzureIoTClient | 9:3ec7e2695f98 | 494 | result = handleData->messageCallback(message, handleData->messageUserContextCallback); |
AzureIoTClient | 0:e393db310d89 | 495 | } |
AzureIoTClient | 0:e393db310d89 | 496 | else |
AzureIoTClient | 0:e393db310d89 | 497 | { |
AzureIoTClient | 9:3ec7e2695f98 | 498 | /*Codes_SRS_IOTHUBCLIENT_LL_02_032: [If the last callback function was NULL, then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */ |
AzureIoTClient | 0:e393db310d89 | 499 | LogError("user callback was NULL\r\n"); |
AzureIoTClient | 0:e393db310d89 | 500 | result = IOTHUBMESSAGE_ABANDONED; |
AzureIoTClient | 0:e393db310d89 | 501 | } |
AzureIoTClient | 0:e393db310d89 | 502 | } |
AzureIoTClient | 9:3ec7e2695f98 | 503 | /*Codes_SRS_IOTHUBCLIENT_LL_02_031: [Then IoTHubClient_LL_MessageCallback shall return what the user function returns.]*/ |
AzureIoTClient | 0:e393db310d89 | 504 | return result; |
AzureIoTClient | 0:e393db310d89 | 505 | } |
AzureIoTClient | 0:e393db310d89 | 506 | |
AzureIoTClient | 9:3ec7e2695f98 | 507 | IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetLastMessageReceiveTime(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime) |
AzureIoTClient | 0:e393db310d89 | 508 | { |
AzureIoTClient | 0:e393db310d89 | 509 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 0:e393db310d89 | 510 | IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 0:e393db310d89 | 511 | |
AzureIoTClient | 9:3ec7e2695f98 | 512 | /* Codes_SRS_IOTHUBCLIENT_LL_09_001: [IoTHubClient_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG if any of the arguments is NULL] */ |
AzureIoTClient | 9:3ec7e2695f98 | 513 | if (handleData == NULL || lastMessageReceiveTime == NULL) |
AzureIoTClient | 0:e393db310d89 | 514 | { |
AzureIoTClient | 0:e393db310d89 | 515 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 0:e393db310d89 | 516 | LOG_ERROR; |
AzureIoTClient | 0:e393db310d89 | 517 | } |
AzureIoTClient | 0:e393db310d89 | 518 | else |
AzureIoTClient | 0:e393db310d89 | 519 | { |
AzureIoTClient | 9:3ec7e2695f98 | 520 | /* Codes_SRS_IOTHUBCLIENT_LL_09_002: [IoTHubClient_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INDEFINITE_TIME - and not set 'lastMessageReceiveTime' - if it is unable to provide the time for the last commands] */ |
AzureIoTClient | 9:3ec7e2695f98 | 521 | if (handleData->lastMessageReceiveTime == INDEFINITE_TIME) |
AzureIoTClient | 0:e393db310d89 | 522 | { |
AzureIoTClient | 0:e393db310d89 | 523 | result = IOTHUB_CLIENT_INDEFINITE_TIME; |
AzureIoTClient | 0:e393db310d89 | 524 | LOG_ERROR; |
AzureIoTClient | 0:e393db310d89 | 525 | } |
AzureIoTClient | 0:e393db310d89 | 526 | else |
AzureIoTClient | 0:e393db310d89 | 527 | { |
AzureIoTClient | 9:3ec7e2695f98 | 528 | /* Codes_SRS_IOTHUBCLIENT_LL_09_003: [IoTHubClient_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_OK if it wrote in the lastMessageReceiveTime the time when the last command was received] */ |
AzureIoTClient | 9:3ec7e2695f98 | 529 | /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */ |
AzureIoTClient | 9:3ec7e2695f98 | 530 | *lastMessageReceiveTime = handleData->lastMessageReceiveTime; |
AzureIoTClient | 0:e393db310d89 | 531 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 0:e393db310d89 | 532 | } |
AzureIoTClient | 0:e393db310d89 | 533 | } |
AzureIoTClient | 0:e393db310d89 | 534 | |
AzureIoTClient | 0:e393db310d89 | 535 | return result; |
AzureIoTClient | 0:e393db310d89 | 536 | } |
AzureIoTClient | 0:e393db310d89 | 537 | |
AzureIoTClient | 0:e393db310d89 | 538 | IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetOption(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* optionName, const void* value) |
AzureIoTClient | 0:e393db310d89 | 539 | { |
AzureIoTClient | 0:e393db310d89 | 540 | |
AzureIoTClient | 0:e393db310d89 | 541 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 0:e393db310d89 | 542 | /*Codes_SRS_IOTHUBCLIENT_LL_02_034: [If iotHubClientHandle is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]*/ |
AzureIoTClient | 0:e393db310d89 | 543 | /*Codes_SRS_IOTHUBCLIENT_LL_02_035: [If optionName is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 0:e393db310d89 | 544 | /*Codes_SRS_IOTHUBCLIENT_LL_02_036: [If value is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 0:e393db310d89 | 545 | if ( |
AzureIoTClient | 0:e393db310d89 | 546 | (iotHubClientHandle == NULL) || |
AzureIoTClient | 0:e393db310d89 | 547 | (optionName == NULL) || |
AzureIoTClient | 0:e393db310d89 | 548 | (value == NULL) |
AzureIoTClient | 0:e393db310d89 | 549 | ) |
AzureIoTClient | 0:e393db310d89 | 550 | { |
AzureIoTClient | 0:e393db310d89 | 551 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 0:e393db310d89 | 552 | LogError("invalid argument (NULL)\r\n"); |
AzureIoTClient | 0:e393db310d89 | 553 | } |
AzureIoTClient | 0:e393db310d89 | 554 | else |
AzureIoTClient | 0:e393db310d89 | 555 | { |
AzureIoTClient | 0:e393db310d89 | 556 | IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 0:e393db310d89 | 557 | |
AzureIoTClient | 0:e393db310d89 | 558 | /*Codes_SRS_IOTHUBCLIENT_LL_02_038: [Otherwise, IoTHubClient_LL shall call the function _SetOption of the underlying transport and return what that function is returning.] */ |
AzureIoTClient | 0:e393db310d89 | 559 | result = handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value); |
AzureIoTClient | 0:e393db310d89 | 560 | |
AzureIoTClient | 0:e393db310d89 | 561 | if (result != IOTHUB_CLIENT_OK) |
AzureIoTClient | 0:e393db310d89 | 562 | { |
AzureIoTClient | 0:e393db310d89 | 563 | LogError("underlying transport failed, returned = %s\r\n", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result)); |
AzureIoTClient | 0:e393db310d89 | 564 | } |
AzureIoTClient | 0:e393db310d89 | 565 | |
AzureIoTClient | 0:e393db310d89 | 566 | } |
AzureIoTClient | 0:e393db310d89 | 567 | return result; |
AzureIoTClient | 0:e393db310d89 | 568 | } |