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