Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

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

Committer:
AzureIoTClient
Date:
Fri Feb 24 14:00:43 2017 -0800
Revision:
60:41648c4e7036
Parent:
58:15b0d29b2667
Child:
61:8b85a4e797cf
1.1.8

Who changed what in which revision?

UserRevisionLine numberNew 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>
Azure.IoT Build 38:a05929a75111 5 #include <string.h>
AzureIoTClient 60:41648c4e7036 6 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 38:a05929a75111 7 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 38:a05929a75111 8 #include "azure_c_shared_utility/string_tokenizer.h"
Azure.IoT Build 38:a05929a75111 9 #include "azure_c_shared_utility/doublylinkedlist.h"
Azure.IoT Build 45:54c11b1b1407 10 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 38:a05929a75111 11 #include "azure_c_shared_utility/tickcounter.h"
AzureIoTClient 53:1e5a1ca1f274 12 #include "azure_c_shared_utility/constbuffer.h"
AzureIoTClient 16:deba40344375 13
AzureIoTClient 16:deba40344375 14 #include "iothub_client_ll.h"
AzureIoTClient 16:deba40344375 15 #include "iothub_client_private.h"
AzureIoTClient 33:b372b0efcd20 16 #include "iothub_client_version.h"
Azure.IoT Build 38:a05929a75111 17 #include "iothub_transport_ll.h"
AzureIoTClient 53:1e5a1ca1f274 18 #include <stdint.h>
AzureIoTClient 43:038d8511e817 19
AzureIoTClient 44:33dd78697616 20 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 21 #include "iothub_client_ll_uploadtoblob.h"
AzureIoTClient 43:038d8511e817 22 #endif
Azure.IoT Build 36:67300d5a4c1f 23
Azure.IoT Build 45:54c11b1b1407 24 #define LOG_ERROR_RESULT LogError("result = %s", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result));
AzureIoTClient 16:deba40344375 25 #define INDEFINITE_TIME ((time_t)(-1))
AzureIoTClient 16:deba40344375 26
AzureIoTClient 16:deba40344375 27 DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_RESULT_VALUES);
Azure.IoT Build 45:54c11b1b1407 28 DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES);
AzureIoTClient 16:deba40344375 29
AzureIoTClient 16:deba40344375 30 typedef struct IOTHUB_CLIENT_LL_HANDLE_DATA_TAG
AzureIoTClient 16:deba40344375 31 {
AzureIoTClient 42:448eecc3676e 32 DLIST_ENTRY waitingToSend;
AzureIoTClient 53:1e5a1ca1f274 33 DLIST_ENTRY iot_msg_queue;
AzureIoTClient 53:1e5a1ca1f274 34 DLIST_ENTRY iot_ack_queue;
AzureIoTClient 42:448eecc3676e 35 TRANSPORT_LL_HANDLE transportHandle;
AzureIoTClient 42:448eecc3676e 36 bool isSharedTransport;
AzureIoTClient 42:448eecc3676e 37 IOTHUB_DEVICE_HANDLE deviceHandle;
AzureIoTClient 42:448eecc3676e 38 TRANSPORT_PROVIDER_FIELDS;
AzureIoTClient 42:448eecc3676e 39 IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback;
AzureIoTClient 42:448eecc3676e 40 void* messageUserContextCallback;
AzureIoTClient 53:1e5a1ca1f274 41 IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK conStatusCallback;
AzureIoTClient 53:1e5a1ca1f274 42 void* conStatusUserContextCallback;
AzureIoTClient 42:448eecc3676e 43 time_t lastMessageReceiveTime;
AzureIoTClient 42:448eecc3676e 44 TICK_COUNTER_HANDLE tickCounter; /*shared tickcounter used to track message timeouts in waitingToSend list*/
Azure.IoT.Build 54:6dcad9019a64 45 tickcounter_ms_t currentMessageTimeout;
AzureIoTClient 53:1e5a1ca1f274 46 uint64_t current_device_twin_timeout;
AzureIoTClient 53:1e5a1ca1f274 47 IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback;
AzureIoTClient 53:1e5a1ca1f274 48 void* deviceTwinContextCallback;
AzureIoTClient 53:1e5a1ca1f274 49 IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC deviceMethodCallback;
AzureIoTClient 55:59b527ab3452 50 IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK deviceInboundMethodCallback;
AzureIoTClient 53:1e5a1ca1f274 51 void* deviceMethodUserContextCallback;
AzureIoTClient 52:1cc3c6d07cad 52 IOTHUB_CLIENT_RETRY_POLICY retryPolicy;
AzureIoTClient 53:1e5a1ca1f274 53 size_t retryTimeoutLimitInSeconds;
AzureIoTClient 44:33dd78697616 54 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 55 IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE uploadToBlobHandle;
AzureIoTClient 43:038d8511e817 56 #endif
AzureIoTClient 53:1e5a1ca1f274 57 uint32_t data_msg_id;
AzureIoTClient 53:1e5a1ca1f274 58 bool complete_twin_update_encountered;
AzureIoTClient 16:deba40344375 59 }IOTHUB_CLIENT_LL_HANDLE_DATA;
AzureIoTClient 16:deba40344375 60
AzureIoTClient 16:deba40344375 61 static const char HOSTNAME_TOKEN[] = "HostName";
AzureIoTClient 16:deba40344375 62 static const char DEVICEID_TOKEN[] = "DeviceId";
Azure.IoT Build 45:54c11b1b1407 63 static const char X509_TOKEN[] = "x509";
Azure.IoT Build 45:54c11b1b1407 64 static const char X509_TOKEN_ONLY_ACCEPTABLE_VALUE[] = "true";
AzureIoTClient 16:deba40344375 65 static const char DEVICEKEY_TOKEN[] = "SharedAccessKey";
AzureIoTClient 40:1a94db9139ea 66 static const char DEVICESAS_TOKEN[] = "SharedAccessSignature";
AzureIoTClient 16:deba40344375 67 static const char PROTOCOL_GATEWAY_HOST[] = "GatewayHostName";
AzureIoTClient 16:deba40344375 68
AzureIoTClient 53:1e5a1ca1f274 69 static void device_twin_data_destroy(IOTHUB_DEVICE_TWIN* client_item)
AzureIoTClient 53:1e5a1ca1f274 70 {
AzureIoTClient 53:1e5a1ca1f274 71 CONSTBUFFER_Destroy(client_item->report_data_handle);
AzureIoTClient 53:1e5a1ca1f274 72 free(client_item);
AzureIoTClient 53:1e5a1ca1f274 73 }
AzureIoTClient 53:1e5a1ca1f274 74
AzureIoTClient 53:1e5a1ca1f274 75 static uint32_t get_next_item_id(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData)
AzureIoTClient 53:1e5a1ca1f274 76 {
AzureIoTClient 53:1e5a1ca1f274 77 if (handleData->data_msg_id+1 >= UINT32_MAX)
AzureIoTClient 53:1e5a1ca1f274 78 {
AzureIoTClient 53:1e5a1ca1f274 79 handleData->data_msg_id = 1;
AzureIoTClient 53:1e5a1ca1f274 80 }
AzureIoTClient 53:1e5a1ca1f274 81 else
AzureIoTClient 53:1e5a1ca1f274 82 {
AzureIoTClient 53:1e5a1ca1f274 83 handleData->data_msg_id++;
AzureIoTClient 53:1e5a1ca1f274 84 }
AzureIoTClient 53:1e5a1ca1f274 85 return handleData->data_msg_id;
AzureIoTClient 53:1e5a1ca1f274 86 }
AzureIoTClient 53:1e5a1ca1f274 87
AzureIoTClient 53:1e5a1ca1f274 88 static IOTHUB_DEVICE_TWIN* dev_twin_data_create(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData, uint32_t id, const unsigned char* reportedState, size_t size, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reportedStateCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 89 {
AzureIoTClient 53:1e5a1ca1f274 90 IOTHUB_DEVICE_TWIN* result = (IOTHUB_DEVICE_TWIN*)malloc(sizeof(IOTHUB_DEVICE_TWIN) );
AzureIoTClient 53:1e5a1ca1f274 91 if (result != NULL)
AzureIoTClient 53:1e5a1ca1f274 92 {
AzureIoTClient 53:1e5a1ca1f274 93 result->report_data_handle = CONSTBUFFER_Create(reportedState, size);
AzureIoTClient 53:1e5a1ca1f274 94 if (result->report_data_handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 95 {
AzureIoTClient 53:1e5a1ca1f274 96 LogError("Failure allocating reported state data");
AzureIoTClient 53:1e5a1ca1f274 97 free(result);
AzureIoTClient 53:1e5a1ca1f274 98 result = NULL;
AzureIoTClient 53:1e5a1ca1f274 99 }
AzureIoTClient 53:1e5a1ca1f274 100 else if (tickcounter_get_current_ms(handleData->tickCounter, &result->ms_timesOutAfter) != 0)
AzureIoTClient 53:1e5a1ca1f274 101 {
AzureIoTClient 53:1e5a1ca1f274 102 LogError("Failure getting tickcount info");
AzureIoTClient 53:1e5a1ca1f274 103 CONSTBUFFER_Destroy(result->report_data_handle);
AzureIoTClient 53:1e5a1ca1f274 104 free(result);
AzureIoTClient 53:1e5a1ca1f274 105 result = NULL;
AzureIoTClient 53:1e5a1ca1f274 106 }
AzureIoTClient 53:1e5a1ca1f274 107 else
AzureIoTClient 53:1e5a1ca1f274 108 {
AzureIoTClient 53:1e5a1ca1f274 109 result->item_id = id;
AzureIoTClient 53:1e5a1ca1f274 110 result->ms_timesOutAfter = 0;
AzureIoTClient 53:1e5a1ca1f274 111 result->context = userContextCallback;
AzureIoTClient 53:1e5a1ca1f274 112 result->reported_state_callback = reportedStateCallback;
AzureIoTClient 53:1e5a1ca1f274 113 }
AzureIoTClient 53:1e5a1ca1f274 114 }
AzureIoTClient 53:1e5a1ca1f274 115 else
AzureIoTClient 53:1e5a1ca1f274 116 {
AzureIoTClient 53:1e5a1ca1f274 117 LogError("Failure allocating device twin information");
AzureIoTClient 53:1e5a1ca1f274 118 }
AzureIoTClient 53:1e5a1ca1f274 119 return result;
AzureIoTClient 53:1e5a1ca1f274 120 }
AzureIoTClient 53:1e5a1ca1f274 121
AzureIoTClient 16:deba40344375 122 IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_CreateFromConnectionString(const char* connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol)
AzureIoTClient 16:deba40344375 123 {
AzureIoTClient 53:1e5a1ca1f274 124 IOTHUB_CLIENT_LL_HANDLE result;
AzureIoTClient 16:deba40344375 125
AzureIoTClient 42:448eecc3676e 126 /*Codes_SRS_IOTHUBCLIENT_LL_05_001: [IoTHubClient_LL_CreateFromConnectionString shall obtain the version string by a call to IoTHubClient_GetVersionString.]*/
AzureIoTClient 42:448eecc3676e 127 /*Codes_SRS_IOTHUBCLIENT_LL_05_002: [IoTHubClient_LL_CreateFromConnectionString shall print the version string to standard output.]*/
AzureIoTClient 42:448eecc3676e 128 LogInfo("IoT Hub SDK for C, version %s", IoTHubClient_GetVersionString());
AzureIoTClient 16:deba40344375 129
AzureIoTClient 42:448eecc3676e 130 /* Codes_SRS_IOTHUBCLIENT_LL_12_003: [IoTHubClient_LL_CreateFromConnectionString shall verify the input parameter and if it is NULL then return NULL] */
AzureIoTClient 42:448eecc3676e 131 if (connectionString == NULL)
AzureIoTClient 42:448eecc3676e 132 {
AzureIoTClient 42:448eecc3676e 133 LogError("Input parameter is NULL: connectionString");
AzureIoTClient 53:1e5a1ca1f274 134 result = NULL;
AzureIoTClient 42:448eecc3676e 135 }
AzureIoTClient 42:448eecc3676e 136 else if (protocol == NULL)
AzureIoTClient 42:448eecc3676e 137 {
AzureIoTClient 42:448eecc3676e 138 LogError("Input parameter is NULL: protocol");
AzureIoTClient 53:1e5a1ca1f274 139 result = NULL;
AzureIoTClient 42:448eecc3676e 140 }
AzureIoTClient 42:448eecc3676e 141 else
AzureIoTClient 42:448eecc3676e 142 {
AzureIoTClient 42:448eecc3676e 143 /* Codes_SRS_IOTHUBCLIENT_LL_12_004: [IoTHubClient_LL_CreateFromConnectionString shall allocate IOTHUB_CLIENT_CONFIG structure] */
AzureIoTClient 53:1e5a1ca1f274 144 IOTHUB_CLIENT_CONFIG* config = (IOTHUB_CLIENT_CONFIG*) malloc(sizeof(IOTHUB_CLIENT_CONFIG));
AzureIoTClient 42:448eecc3676e 145 if (config == NULL)
AzureIoTClient 42:448eecc3676e 146 {
AzureIoTClient 42:448eecc3676e 147 /* Codes_SRS_IOTHUBCLIENT_LL_12_012: [If the allocation failed IoTHubClient_LL_CreateFromConnectionString returns NULL] */
AzureIoTClient 42:448eecc3676e 148 LogError("Malloc failed");
AzureIoTClient 53:1e5a1ca1f274 149 result = NULL;
AzureIoTClient 42:448eecc3676e 150 }
AzureIoTClient 42:448eecc3676e 151 else
AzureIoTClient 42:448eecc3676e 152 {
AzureIoTClient 42:448eecc3676e 153 STRING_TOKENIZER_HANDLE tokenizer1 = NULL;
AzureIoTClient 42:448eecc3676e 154 STRING_HANDLE connString = NULL;
AzureIoTClient 42:448eecc3676e 155 STRING_HANDLE tokenString = NULL;
AzureIoTClient 42:448eecc3676e 156 STRING_HANDLE valueString = NULL;
AzureIoTClient 42:448eecc3676e 157 STRING_HANDLE hostNameString = NULL;
AzureIoTClient 42:448eecc3676e 158 STRING_HANDLE hostSuffixString = NULL;
AzureIoTClient 42:448eecc3676e 159 STRING_HANDLE deviceIdString = NULL;
AzureIoTClient 42:448eecc3676e 160 STRING_HANDLE deviceKeyString = NULL;
AzureIoTClient 42:448eecc3676e 161 STRING_HANDLE deviceSasTokenString = NULL;
AzureIoTClient 42:448eecc3676e 162 STRING_HANDLE protocolGateway = NULL;
AzureIoTClient 16:deba40344375 163
AzureIoTClient 42:448eecc3676e 164 config->protocol = protocol;
AzureIoTClient 16:deba40344375 165
AzureIoTClient 42:448eecc3676e 166 config->iotHubName = NULL;
AzureIoTClient 42:448eecc3676e 167 config->iotHubSuffix = NULL;
AzureIoTClient 42:448eecc3676e 168 config->deviceId = NULL;
AzureIoTClient 42:448eecc3676e 169 config->deviceKey = NULL;
AzureIoTClient 42:448eecc3676e 170 config->deviceSasToken = NULL;
AzureIoTClient 40:1a94db9139ea 171
AzureIoTClient 42:448eecc3676e 172 /* Codes_SRS_IOTHUBCLIENT_LL_04_002: [If it does not, it shall pass the protocolGatewayHostName NULL.] */
AzureIoTClient 42:448eecc3676e 173 config->protocolGatewayHostName = NULL;
AzureIoTClient 16:deba40344375 174
AzureIoTClient 42:448eecc3676e 175 if ((connString = STRING_construct(connectionString)) == NULL)
AzureIoTClient 42:448eecc3676e 176 {
AzureIoTClient 42:448eecc3676e 177 LogError("Error constructing connectiong String");
AzureIoTClient 53:1e5a1ca1f274 178 result = NULL;
AzureIoTClient 42:448eecc3676e 179 }
AzureIoTClient 42:448eecc3676e 180 else if ((tokenizer1 = STRING_TOKENIZER_create(connString)) == NULL)
AzureIoTClient 42:448eecc3676e 181 {
AzureIoTClient 42:448eecc3676e 182 LogError("Error creating Tokenizer");
AzureIoTClient 53:1e5a1ca1f274 183 result = NULL;
AzureIoTClient 42:448eecc3676e 184 }
AzureIoTClient 42:448eecc3676e 185 else if ((tokenString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 186 {
AzureIoTClient 42:448eecc3676e 187 LogError("Error creating Token String");
AzureIoTClient 53:1e5a1ca1f274 188 result = NULL;
AzureIoTClient 42:448eecc3676e 189 }
AzureIoTClient 42:448eecc3676e 190 else if ((valueString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 191 {
AzureIoTClient 42:448eecc3676e 192 LogError("Error creating Value String");
AzureIoTClient 53:1e5a1ca1f274 193 result = NULL;
AzureIoTClient 42:448eecc3676e 194 }
AzureIoTClient 42:448eecc3676e 195 else if ((hostNameString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 196 {
AzureIoTClient 42:448eecc3676e 197 LogError("Error creating HostName String");
AzureIoTClient 53:1e5a1ca1f274 198 result = NULL;
AzureIoTClient 42:448eecc3676e 199 }
AzureIoTClient 42:448eecc3676e 200 else if ((hostSuffixString = STRING_new()) == NULL)
AzureIoTClient 42:448eecc3676e 201 {
AzureIoTClient 42:448eecc3676e 202 LogError("Error creating HostSuffix String");
AzureIoTClient 53:1e5a1ca1f274 203 result = NULL;
AzureIoTClient 42:448eecc3676e 204 }
AzureIoTClient 42:448eecc3676e 205 /* Codes_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..."] */
Azure.IoT Build 45:54c11b1b1407 206 /* Codes_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, SharedAccessSignature or x509] */
AzureIoTClient 42:448eecc3676e 207 else
AzureIoTClient 42:448eecc3676e 208 {
Azure.IoT Build 45:54c11b1b1407 209 int isx509found = 0;
AzureIoTClient 42:448eecc3676e 210 while ((STRING_TOKENIZER_get_next_token(tokenizer1, tokenString, "=") == 0))
AzureIoTClient 42:448eecc3676e 211 {
AzureIoTClient 42:448eecc3676e 212 if (STRING_TOKENIZER_get_next_token(tokenizer1, valueString, ";") != 0)
AzureIoTClient 42:448eecc3676e 213 {
AzureIoTClient 42:448eecc3676e 214 LogError("Tokenizer error");
AzureIoTClient 42:448eecc3676e 215 break;
AzureIoTClient 42:448eecc3676e 216 }
AzureIoTClient 42:448eecc3676e 217 else
AzureIoTClient 42:448eecc3676e 218 {
AzureIoTClient 42:448eecc3676e 219 if (tokenString != NULL)
AzureIoTClient 42:448eecc3676e 220 {
AzureIoTClient 42:448eecc3676e 221 /* Codes_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 or deviceSasToken = SharedAccessSignature] */
AzureIoTClient 42:448eecc3676e 222 const char* s_token = STRING_c_str(tokenString);
AzureIoTClient 42:448eecc3676e 223 if (strcmp(s_token, HOSTNAME_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 224 {
AzureIoTClient 42:448eecc3676e 225 /* Codes_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 42:448eecc3676e 226 STRING_TOKENIZER_HANDLE tokenizer2 = NULL;
AzureIoTClient 42:448eecc3676e 227 if ((tokenizer2 = STRING_TOKENIZER_create(valueString)) == NULL)
AzureIoTClient 42:448eecc3676e 228 {
AzureIoTClient 42:448eecc3676e 229 LogError("Error creating Tokenizer");
AzureIoTClient 42:448eecc3676e 230 break;
AzureIoTClient 42:448eecc3676e 231 }
AzureIoTClient 42:448eecc3676e 232 else
AzureIoTClient 42:448eecc3676e 233 {
AzureIoTClient 42:448eecc3676e 234 /* Codes_SRS_IOTHUBCLIENT_LL_12_015: [If the string split failed, IoTHubClient_LL_CreateFromConnectionString returns NULL ] */
AzureIoTClient 42:448eecc3676e 235 if (STRING_TOKENIZER_get_next_token(tokenizer2, hostNameString, ".") != 0)
AzureIoTClient 42:448eecc3676e 236 {
AzureIoTClient 42:448eecc3676e 237 LogError("Tokenizer error");
AzureIoTClient 42:448eecc3676e 238 STRING_TOKENIZER_destroy(tokenizer2);
AzureIoTClient 42:448eecc3676e 239 break;
AzureIoTClient 42:448eecc3676e 240 }
AzureIoTClient 42:448eecc3676e 241 else
AzureIoTClient 42:448eecc3676e 242 {
AzureIoTClient 42:448eecc3676e 243 config->iotHubName = STRING_c_str(hostNameString);
AzureIoTClient 42:448eecc3676e 244 if (STRING_TOKENIZER_get_next_token(tokenizer2, hostSuffixString, ";") != 0)
AzureIoTClient 42:448eecc3676e 245 {
AzureIoTClient 42:448eecc3676e 246 LogError("Tokenizer error");
AzureIoTClient 42:448eecc3676e 247 STRING_TOKENIZER_destroy(tokenizer2);
AzureIoTClient 42:448eecc3676e 248 break;
AzureIoTClient 42:448eecc3676e 249 }
AzureIoTClient 42:448eecc3676e 250 else
AzureIoTClient 42:448eecc3676e 251 {
AzureIoTClient 42:448eecc3676e 252 config->iotHubSuffix = STRING_c_str(hostSuffixString);
AzureIoTClient 42:448eecc3676e 253 }
AzureIoTClient 42:448eecc3676e 254 }
AzureIoTClient 42:448eecc3676e 255 STRING_TOKENIZER_destroy(tokenizer2);
AzureIoTClient 42:448eecc3676e 256 }
AzureIoTClient 42:448eecc3676e 257 }
AzureIoTClient 42:448eecc3676e 258 else if (strcmp(s_token, DEVICEID_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 259 {
AzureIoTClient 42:448eecc3676e 260 deviceIdString = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 261 if (deviceIdString != NULL)
AzureIoTClient 42:448eecc3676e 262 {
AzureIoTClient 42:448eecc3676e 263 config->deviceId = STRING_c_str(deviceIdString);
AzureIoTClient 42:448eecc3676e 264 }
AzureIoTClient 53:1e5a1ca1f274 265 else
AzureIoTClient 53:1e5a1ca1f274 266 {
AzureIoTClient 53:1e5a1ca1f274 267 LogError("Failure cloning device id string");
AzureIoTClient 53:1e5a1ca1f274 268 break;
AzureIoTClient 53:1e5a1ca1f274 269 }
AzureIoTClient 42:448eecc3676e 270 }
AzureIoTClient 42:448eecc3676e 271 else if (strcmp(s_token, DEVICEKEY_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 272 {
AzureIoTClient 42:448eecc3676e 273 deviceKeyString = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 274 if (deviceKeyString != NULL)
AzureIoTClient 42:448eecc3676e 275 {
AzureIoTClient 42:448eecc3676e 276 config->deviceKey = STRING_c_str(deviceKeyString);
AzureIoTClient 42:448eecc3676e 277 }
AzureIoTClient 53:1e5a1ca1f274 278 else
AzureIoTClient 53:1e5a1ca1f274 279 {
AzureIoTClient 53:1e5a1ca1f274 280 LogError("Failure cloning device key string");
AzureIoTClient 53:1e5a1ca1f274 281 break;
AzureIoTClient 53:1e5a1ca1f274 282 }
AzureIoTClient 42:448eecc3676e 283 }
AzureIoTClient 42:448eecc3676e 284 else if (strcmp(s_token, DEVICESAS_TOKEN) == 0)
AzureIoTClient 42:448eecc3676e 285 {
AzureIoTClient 42:448eecc3676e 286 deviceSasTokenString = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 287 if (deviceSasTokenString != NULL)
AzureIoTClient 42:448eecc3676e 288 {
AzureIoTClient 42:448eecc3676e 289 config->deviceSasToken = STRING_c_str(deviceSasTokenString);
AzureIoTClient 42:448eecc3676e 290 }
AzureIoTClient 53:1e5a1ca1f274 291 else
AzureIoTClient 53:1e5a1ca1f274 292 {
AzureIoTClient 53:1e5a1ca1f274 293 LogError("Failure cloning device sasToken string");
AzureIoTClient 53:1e5a1ca1f274 294 break;
AzureIoTClient 53:1e5a1ca1f274 295 }
AzureIoTClient 42:448eecc3676e 296 }
Azure.IoT Build 45:54c11b1b1407 297 else if (strcmp(s_token, X509_TOKEN) == 0)
Azure.IoT Build 45:54c11b1b1407 298 {
Azure.IoT Build 45:54c11b1b1407 299 if (strcmp(STRING_c_str(valueString), X509_TOKEN_ONLY_ACCEPTABLE_VALUE) != 0)
Azure.IoT Build 45:54c11b1b1407 300 {
Azure.IoT Build 45:54c11b1b1407 301 LogError("x509 option has wrong value, the only acceptable one is \"true\"");
AzureIoTClient 53:1e5a1ca1f274 302 break;
Azure.IoT Build 45:54c11b1b1407 303 }
Azure.IoT Build 45:54c11b1b1407 304 else
Azure.IoT Build 45:54c11b1b1407 305 {
Azure.IoT Build 45:54c11b1b1407 306 isx509found = 1;
Azure.IoT Build 45:54c11b1b1407 307 }
Azure.IoT Build 45:54c11b1b1407 308 }
AzureIoTClient 42:448eecc3676e 309 /* 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 42:448eecc3676e 310 else if (strcmp(s_token, PROTOCOL_GATEWAY_HOST) == 0)
AzureIoTClient 42:448eecc3676e 311 {
AzureIoTClient 42:448eecc3676e 312 protocolGateway = STRING_clone(valueString);
AzureIoTClient 42:448eecc3676e 313 if (protocolGateway != NULL)
AzureIoTClient 42:448eecc3676e 314 {
AzureIoTClient 42:448eecc3676e 315 config->protocolGatewayHostName = STRING_c_str(protocolGateway);
AzureIoTClient 42:448eecc3676e 316 }
AzureIoTClient 53:1e5a1ca1f274 317 else
AzureIoTClient 53:1e5a1ca1f274 318 {
AzureIoTClient 53:1e5a1ca1f274 319 LogError("Failure cloning protocol Gateway Name");
AzureIoTClient 53:1e5a1ca1f274 320 break;
AzureIoTClient 53:1e5a1ca1f274 321 }
AzureIoTClient 42:448eecc3676e 322 }
AzureIoTClient 42:448eecc3676e 323 }
AzureIoTClient 42:448eecc3676e 324 }
AzureIoTClient 42:448eecc3676e 325 }
AzureIoTClient 42:448eecc3676e 326 /* parsing is done - check the result */
AzureIoTClient 42:448eecc3676e 327 if (config->iotHubName == NULL)
AzureIoTClient 42:448eecc3676e 328 {
AzureIoTClient 42:448eecc3676e 329 LogError("iotHubName is not found");
AzureIoTClient 53:1e5a1ca1f274 330 result = NULL;
AzureIoTClient 42:448eecc3676e 331 }
AzureIoTClient 42:448eecc3676e 332 else if (config->iotHubSuffix == NULL)
AzureIoTClient 42:448eecc3676e 333 {
AzureIoTClient 42:448eecc3676e 334 LogError("iotHubSuffix is not found");
AzureIoTClient 53:1e5a1ca1f274 335 result = NULL;
AzureIoTClient 42:448eecc3676e 336 }
AzureIoTClient 42:448eecc3676e 337 else if (config->deviceId == NULL)
AzureIoTClient 42:448eecc3676e 338 {
AzureIoTClient 42:448eecc3676e 339 LogError("deviceId is not found");
AzureIoTClient 53:1e5a1ca1f274 340 result = NULL;
AzureIoTClient 42:448eecc3676e 341 }
Azure.IoT Build 45:54c11b1b1407 342 else if (!(
Azure.IoT Build 45:54c11b1b1407 343 ((!isx509found) && (config->deviceSasToken == NULL) ^ (config->deviceKey == NULL)) ||
Azure.IoT Build 45:54c11b1b1407 344 ((isx509found) && (config->deviceSasToken == NULL) && (config->deviceKey == NULL))
Azure.IoT Build 45:54c11b1b1407 345 ))
AzureIoTClient 42:448eecc3676e 346 {
Azure.IoT Build 45:54c11b1b1407 347 LogError("invalid combination of x509, deviceSasToken and deviceKey");
AzureIoTClient 53:1e5a1ca1f274 348 result = NULL;
AzureIoTClient 42:448eecc3676e 349 }
AzureIoTClient 42:448eecc3676e 350 else
AzureIoTClient 42:448eecc3676e 351 {
AzureIoTClient 42:448eecc3676e 352 /* Codes_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 42:448eecc3676e 353
AzureIoTClient 42:448eecc3676e 354 result = IoTHubClient_LL_Create(config);
AzureIoTClient 42:448eecc3676e 355 if (result == NULL)
AzureIoTClient 42:448eecc3676e 356 {
AzureIoTClient 42:448eecc3676e 357 LogError("IoTHubClient_LL_Create failed");
AzureIoTClient 42:448eecc3676e 358 }
AzureIoTClient 42:448eecc3676e 359 else
AzureIoTClient 42:448eecc3676e 360 {
AzureIoTClient 42:448eecc3676e 361 /*return as is*/
AzureIoTClient 42:448eecc3676e 362 }
AzureIoTClient 42:448eecc3676e 363 }
AzureIoTClient 42:448eecc3676e 364 }
AzureIoTClient 42:448eecc3676e 365 if (deviceSasTokenString != NULL)
AzureIoTClient 42:448eecc3676e 366 STRING_delete(deviceSasTokenString);
AzureIoTClient 42:448eecc3676e 367 if (deviceKeyString != NULL)
AzureIoTClient 42:448eecc3676e 368 STRING_delete(deviceKeyString);
AzureIoTClient 42:448eecc3676e 369 if (deviceIdString != NULL)
AzureIoTClient 42:448eecc3676e 370 STRING_delete(deviceIdString);
AzureIoTClient 42:448eecc3676e 371 if (hostSuffixString != NULL)
AzureIoTClient 42:448eecc3676e 372 STRING_delete(hostSuffixString);
AzureIoTClient 42:448eecc3676e 373 if (hostNameString != NULL)
AzureIoTClient 42:448eecc3676e 374 STRING_delete(hostNameString);
AzureIoTClient 42:448eecc3676e 375 if (valueString != NULL)
AzureIoTClient 42:448eecc3676e 376 STRING_delete(valueString);
AzureIoTClient 42:448eecc3676e 377 if (tokenString != NULL)
AzureIoTClient 42:448eecc3676e 378 STRING_delete(tokenString);
AzureIoTClient 42:448eecc3676e 379 if (connString != NULL)
AzureIoTClient 42:448eecc3676e 380 STRING_delete(connString);
AzureIoTClient 42:448eecc3676e 381 if (protocolGateway != NULL)
AzureIoTClient 42:448eecc3676e 382 STRING_delete(protocolGateway);
AzureIoTClient 16:deba40344375 383
AzureIoTClient 42:448eecc3676e 384 if (tokenizer1 != NULL)
AzureIoTClient 42:448eecc3676e 385 STRING_TOKENIZER_destroy(tokenizer1);
AzureIoTClient 16:deba40344375 386
AzureIoTClient 42:448eecc3676e 387 free(config);
AzureIoTClient 42:448eecc3676e 388 }
AzureIoTClient 42:448eecc3676e 389 }
AzureIoTClient 42:448eecc3676e 390 return result;
AzureIoTClient 16:deba40344375 391 }
AzureIoTClient 16:deba40344375 392
Azure.IoT Build 36:67300d5a4c1f 393 static void setTransportProtocol(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData, TRANSPORT_PROVIDER* protocol)
Azure.IoT Build 36:67300d5a4c1f 394 {
AzureIoTClient 43:038d8511e817 395 handleData->IoTHubTransport_GetHostname = protocol->IoTHubTransport_GetHostname;
AzureIoTClient 42:448eecc3676e 396 handleData->IoTHubTransport_SetOption = protocol->IoTHubTransport_SetOption;
AzureIoTClient 42:448eecc3676e 397 handleData->IoTHubTransport_Create = protocol->IoTHubTransport_Create;
AzureIoTClient 42:448eecc3676e 398 handleData->IoTHubTransport_Destroy = protocol->IoTHubTransport_Destroy;
AzureIoTClient 42:448eecc3676e 399 handleData->IoTHubTransport_Register = protocol->IoTHubTransport_Register;
AzureIoTClient 42:448eecc3676e 400 handleData->IoTHubTransport_Unregister = protocol->IoTHubTransport_Unregister;
AzureIoTClient 42:448eecc3676e 401 handleData->IoTHubTransport_Subscribe = protocol->IoTHubTransport_Subscribe;
AzureIoTClient 42:448eecc3676e 402 handleData->IoTHubTransport_Unsubscribe = protocol->IoTHubTransport_Unsubscribe;
AzureIoTClient 42:448eecc3676e 403 handleData->IoTHubTransport_DoWork = protocol->IoTHubTransport_DoWork;
AzureIoTClient 53:1e5a1ca1f274 404 handleData->IoTHubTransport_SetRetryPolicy = protocol->IoTHubTransport_SetRetryPolicy;
AzureIoTClient 42:448eecc3676e 405 handleData->IoTHubTransport_GetSendStatus = protocol->IoTHubTransport_GetSendStatus;
AzureIoTClient 53:1e5a1ca1f274 406 handleData->IoTHubTransport_ProcessItem = protocol->IoTHubTransport_ProcessItem;
AzureIoTClient 53:1e5a1ca1f274 407 handleData->IoTHubTransport_Subscribe_DeviceTwin = protocol->IoTHubTransport_Subscribe_DeviceTwin;
AzureIoTClient 53:1e5a1ca1f274 408 handleData->IoTHubTransport_Unsubscribe_DeviceTwin = protocol->IoTHubTransport_Unsubscribe_DeviceTwin;
AzureIoTClient 53:1e5a1ca1f274 409 handleData->IoTHubTransport_Subscribe_DeviceMethod = protocol->IoTHubTransport_Subscribe_DeviceMethod;
AzureIoTClient 53:1e5a1ca1f274 410 handleData->IoTHubTransport_Unsubscribe_DeviceMethod = protocol->IoTHubTransport_Unsubscribe_DeviceMethod;
AzureIoTClient 55:59b527ab3452 411 handleData->IoTHubTransport_DeviceMethod_Response = protocol->IoTHubTransport_DeviceMethod_Response;
Azure.IoT Build 36:67300d5a4c1f 412 }
AzureIoTClient 16:deba40344375 413
AzureIoTClient 16:deba40344375 414 IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_Create(const IOTHUB_CLIENT_CONFIG* config)
AzureIoTClient 16:deba40344375 415 {
AzureIoTClient 42:448eecc3676e 416 IOTHUB_CLIENT_LL_HANDLE result;
AzureIoTClient 42:448eecc3676e 417 /*Codes_SRS_IOTHUBCLIENT_LL_02_001: [IoTHubClient_LL_Create shall return NULL if config parameter is NULL or protocol field is NULL.]*/
AzureIoTClient 42:448eecc3676e 418 if(
AzureIoTClient 42:448eecc3676e 419 (config == NULL) ||
AzureIoTClient 42:448eecc3676e 420 (config->protocol == NULL)
AzureIoTClient 42:448eecc3676e 421 )
AzureIoTClient 42:448eecc3676e 422 {
AzureIoTClient 42:448eecc3676e 423 result = NULL;
AzureIoTClient 42:448eecc3676e 424 LogError("invalid configuration (NULL detected)");
AzureIoTClient 42:448eecc3676e 425 }
AzureIoTClient 42:448eecc3676e 426 else
AzureIoTClient 42:448eecc3676e 427 {
AzureIoTClient 42:448eecc3676e 428 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)malloc(sizeof(IOTHUB_CLIENT_LL_HANDLE_DATA));
AzureIoTClient 42:448eecc3676e 429 if (handleData == NULL)
AzureIoTClient 42:448eecc3676e 430 {
AzureIoTClient 42:448eecc3676e 431 LogError("malloc failed");
AzureIoTClient 42:448eecc3676e 432 result = NULL;
AzureIoTClient 42:448eecc3676e 433 }
AzureIoTClient 42:448eecc3676e 434 else
AzureIoTClient 42:448eecc3676e 435 {
AzureIoTClient 44:33dd78697616 436 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 437 /*Codes_SRS_IOTHUBCLIENT_LL_02_094: [ IoTHubClient_LL_Create shall create a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE from IOTHUB_CLIENT_CONFIG. ]*/
AzureIoTClient 42:448eecc3676e 438 /*Codes_SRS_IOTHUBCLIENT_LL_02_092: [ IoTHubClient_LL_CreateFromConnectionString shall create a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE from IOTHUB_CLIENT_CONFIG. ]*/
AzureIoTClient 42:448eecc3676e 439 handleData->uploadToBlobHandle = IoTHubClient_LL_UploadToBlob_Create(config);
AzureIoTClient 42:448eecc3676e 440 if (handleData->uploadToBlobHandle == NULL)
AzureIoTClient 42:448eecc3676e 441 {
AzureIoTClient 42:448eecc3676e 442 /*Codes_SRS_IOTHUBCLIENT_LL_02_093: [ If creating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateFromConnectionString shall fail and return NULL. ]*/
AzureIoTClient 42:448eecc3676e 443 /*Codes_SRS_IOTHUBCLIENT_LL_02_095: [ If creating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_Create shall fail and return NULL. ]*/
AzureIoTClient 42:448eecc3676e 444 LogError("unable to IoTHubClient_LL_UploadToBlob_Create");
AzureIoTClient 42:448eecc3676e 445 free(handleData);
AzureIoTClient 42:448eecc3676e 446 result = NULL;
AzureIoTClient 42:448eecc3676e 447 }
AzureIoTClient 42:448eecc3676e 448 else
AzureIoTClient 43:038d8511e817 449 #endif
AzureIoTClient 42:448eecc3676e 450 {
AzureIoTClient 42:448eecc3676e 451 /*Codes_SRS_IOTHUBCLIENT_LL_02_045: [ Otherwise IoTHubClient_LL_Create shall create a new TICK_COUNTER_HANDLE ]*/
AzureIoTClient 42:448eecc3676e 452 if ((handleData->tickCounter = tickcounter_create()) == NULL)
AzureIoTClient 42:448eecc3676e 453 {
AzureIoTClient 44:33dd78697616 454 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 455 /*Codes_SRS_IOTHUBCLIENT_LL_02_046: [ If creating the TICK_COUNTER_HANDLE fails then IoTHubClient_LL_Create shall fail and return NULL. ]*/
AzureIoTClient 42:448eecc3676e 456 IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle);
AzureIoTClient 43:038d8511e817 457 #endif
AzureIoTClient 42:448eecc3676e 458 LogError("unable to get a tickcounter");
AzureIoTClient 42:448eecc3676e 459 free(handleData);
AzureIoTClient 42:448eecc3676e 460 result = NULL;
AzureIoTClient 42:448eecc3676e 461 }
AzureIoTClient 42:448eecc3676e 462 else
AzureIoTClient 42:448eecc3676e 463 {
AzureIoTClient 42:448eecc3676e 464 /*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 42:448eecc3676e 465 IOTHUBTRANSPORT_CONFIG lowerLayerConfig;
AzureIoTClient 42:448eecc3676e 466 DList_InitializeListHead(&(handleData->waitingToSend));
AzureIoTClient 53:1e5a1ca1f274 467 DList_InitializeListHead(&(handleData->iot_msg_queue));
AzureIoTClient 53:1e5a1ca1f274 468 DList_InitializeListHead(&(handleData->iot_ack_queue));
AzureIoTClient 42:448eecc3676e 469 setTransportProtocol(handleData, (TRANSPORT_PROVIDER*)config->protocol());
AzureIoTClient 42:448eecc3676e 470 handleData->messageCallback = NULL;
AzureIoTClient 42:448eecc3676e 471 handleData->messageUserContextCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 472 handleData->deviceTwinCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 473 handleData->deviceTwinContextCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 474 handleData->deviceMethodCallback = NULL;
AzureIoTClient 55:59b527ab3452 475 handleData->deviceInboundMethodCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 476 handleData->deviceMethodUserContextCallback = NULL;
AzureIoTClient 42:448eecc3676e 477 handleData->lastMessageReceiveTime = INDEFINITE_TIME;
AzureIoTClient 53:1e5a1ca1f274 478 handleData->data_msg_id = 1;
AzureIoTClient 53:1e5a1ca1f274 479 handleData->complete_twin_update_encountered = false;
AzureIoTClient 53:1e5a1ca1f274 480 handleData->conStatusCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 481 handleData->conStatusUserContextCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 482 handleData->lastMessageReceiveTime = INDEFINITE_TIME;
AzureIoTClient 53:1e5a1ca1f274 483
AzureIoTClient 42:448eecc3676e 484 /*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 42:448eecc3676e 485 lowerLayerConfig.upperConfig = config;
AzureIoTClient 42:448eecc3676e 486 lowerLayerConfig.waitingToSend = &(handleData->waitingToSend);
AzureIoTClient 42:448eecc3676e 487 /*Codes_SRS_IOTHUBCLIENT_LL_02_007: [If the underlaying layer _Create function fails them IoTHubClient_LL_Create shall fail and return NULL.] */
AzureIoTClient 42:448eecc3676e 488 if ((handleData->transportHandle = handleData->IoTHubTransport_Create(&lowerLayerConfig)) == NULL)
AzureIoTClient 42:448eecc3676e 489 {
AzureIoTClient 42:448eecc3676e 490 LogError("underlying transport failed");
AzureIoTClient 44:33dd78697616 491 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 492 IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle);
AzureIoTClient 43:038d8511e817 493 #endif
AzureIoTClient 42:448eecc3676e 494 tickcounter_destroy(handleData->tickCounter);
AzureIoTClient 42:448eecc3676e 495 free(handleData);
AzureIoTClient 42:448eecc3676e 496 result = NULL;
AzureIoTClient 42:448eecc3676e 497 }
AzureIoTClient 42:448eecc3676e 498 else
AzureIoTClient 42:448eecc3676e 499 {
AzureIoTClient 42:448eecc3676e 500 IOTHUB_DEVICE_CONFIG deviceConfig;
AzureIoTClient 40:1a94db9139ea 501
AzureIoTClient 42:448eecc3676e 502 deviceConfig.deviceId = config->deviceId;
AzureIoTClient 42:448eecc3676e 503 deviceConfig.deviceKey = config->deviceKey;
AzureIoTClient 42:448eecc3676e 504 deviceConfig.deviceSasToken = config->deviceSasToken;
AzureIoTClient 40:1a94db9139ea 505
AzureIoTClient 42:448eecc3676e 506 /*Codes_SRS_IOTHUBCLIENT_LL_17_008: [IoTHubClient_LL_Create shall call the transport _Register function with a populated structure of type IOTHUB_DEVICE_CONFIG and waitingToSend list.] */
AzureIoTClient 42:448eecc3676e 507 if ((handleData->deviceHandle = handleData->IoTHubTransport_Register(handleData->transportHandle, &deviceConfig, handleData, &(handleData->waitingToSend))) == NULL)
AzureIoTClient 42:448eecc3676e 508 {
AzureIoTClient 42:448eecc3676e 509 /*Codes_SRS_IOTHUBCLIENT_LL_17_009: [If the _Register function fails, this function shall fail and return NULL.]*/
AzureIoTClient 42:448eecc3676e 510 LogError("Registering device in transport failed");
AzureIoTClient 42:448eecc3676e 511 handleData->IoTHubTransport_Destroy(handleData->transportHandle);
AzureIoTClient 44:33dd78697616 512 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 513 IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle);
AzureIoTClient 43:038d8511e817 514 #endif
AzureIoTClient 42:448eecc3676e 515 tickcounter_destroy(handleData->tickCounter);
AzureIoTClient 42:448eecc3676e 516 free(handleData);
AzureIoTClient 42:448eecc3676e 517 result = NULL;
AzureIoTClient 42:448eecc3676e 518 }
AzureIoTClient 42:448eecc3676e 519 else
AzureIoTClient 42:448eecc3676e 520 {
AzureIoTClient 42:448eecc3676e 521 /*Codes_SRS_IOTHUBCLIENT_LL_02_008: [Otherwise, IoTHubClient_LL_Create shall succeed and return a non-NULL handle.] */
AzureIoTClient 42:448eecc3676e 522 handleData->isSharedTransport = false;
AzureIoTClient 42:448eecc3676e 523 /*Codes_SRS_IOTHUBCLIENT_LL_02_042: [ By default, messages shall not timeout. ]*/
AzureIoTClient 42:448eecc3676e 524 handleData->currentMessageTimeout = 0;
AzureIoTClient 53:1e5a1ca1f274 525 handleData->current_device_twin_timeout = 0;
AzureIoTClient 42:448eecc3676e 526 result = handleData;
AzureIoTClient 53:1e5a1ca1f274 527 /*Codes_SRS_IOTHUBCLIENT_LL_25_124: [ `IoTHubClient_LL_Create` shall set the default retry policy as Exponential backoff with jitter and if succeed and return a `non-NULL` handle. ]*/
AzureIoTClient 53:1e5a1ca1f274 528 if (IoTHubClient_LL_SetRetryPolicy(handleData, IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF_WITH_JITTER, 0) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 529 {
AzureIoTClient 53:1e5a1ca1f274 530 LogError("Setting default retry policy in transport failed");
AzureIoTClient 53:1e5a1ca1f274 531 IoTHubClient_LL_Destroy(handleData);
AzureIoTClient 53:1e5a1ca1f274 532 result = NULL;
AzureIoTClient 53:1e5a1ca1f274 533 }
AzureIoTClient 42:448eecc3676e 534 }
AzureIoTClient 42:448eecc3676e 535 }
AzureIoTClient 42:448eecc3676e 536 }
AzureIoTClient 42:448eecc3676e 537 }
AzureIoTClient 42:448eecc3676e 538 }
AzureIoTClient 42:448eecc3676e 539 }
AzureIoTClient 16:deba40344375 540
AzureIoTClient 42:448eecc3676e 541 return result;
AzureIoTClient 16:deba40344375 542 }
AzureIoTClient 16:deba40344375 543
Azure.IoT Build 36:67300d5a4c1f 544 IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_CreateWithTransport(const IOTHUB_CLIENT_DEVICE_CONFIG * config)
Azure.IoT Build 36:67300d5a4c1f 545 {
AzureIoTClient 42:448eecc3676e 546 IOTHUB_CLIENT_LL_HANDLE result;
AzureIoTClient 42:448eecc3676e 547 /*Codes_SRS_IOTHUBCLIENT_LL_17_001: [IoTHubClient_LL_CreateWithTransport shall return NULL if config parameter is NULL, or protocol field is NULL or transportHandle is NULL.]*/
AzureIoTClient 42:448eecc3676e 548 if (
AzureIoTClient 42:448eecc3676e 549 (config == NULL) ||
AzureIoTClient 42:448eecc3676e 550 (config->protocol == NULL) ||
Azure.IoT Build 45:54c11b1b1407 551 (config->transportHandle == NULL) ||
Azure.IoT Build 45:54c11b1b1407 552 /*Codes_SRS_IOTHUBCLIENT_LL_02_098: [ IoTHubClient_LL_CreateWithTransport shall fail and return NULL if both config->deviceKey AND config->deviceSasToken are NULL. ]*/
Azure.IoT Build 45:54c11b1b1407 553 ((config->deviceKey == NULL) && (config->deviceSasToken == NULL))
AzureIoTClient 42:448eecc3676e 554 )
AzureIoTClient 42:448eecc3676e 555 {
AzureIoTClient 42:448eecc3676e 556 result = NULL;
AzureIoTClient 42:448eecc3676e 557 LogError("invalid configuration (NULL detected)");
AzureIoTClient 42:448eecc3676e 558 }
AzureIoTClient 42:448eecc3676e 559 else
AzureIoTClient 42:448eecc3676e 560 {
AzureIoTClient 42:448eecc3676e 561 /*Codes_SRS_IOTHUBCLIENT_LL_17_002: [IoTHubClient_LL_CreateWithTransport shall allocate data for the IOTHUB_CLIENT_LL_HANDLE.]*/
AzureIoTClient 42:448eecc3676e 562 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)malloc(sizeof(IOTHUB_CLIENT_LL_HANDLE_DATA));
AzureIoTClient 42:448eecc3676e 563 if (handleData == NULL)
AzureIoTClient 42:448eecc3676e 564 {
AzureIoTClient 42:448eecc3676e 565 /*Codes_SRS_IOTHUBCLIENT_LL_17_003: [If allocation fails, the function shall fail and return NULL.] */
AzureIoTClient 42:448eecc3676e 566 LogError("malloc failed");
AzureIoTClient 42:448eecc3676e 567 result = NULL;
AzureIoTClient 42:448eecc3676e 568 }
AzureIoTClient 42:448eecc3676e 569 else
AzureIoTClient 42:448eecc3676e 570 {
AzureIoTClient 43:038d8511e817 571 handleData->transportHandle = config->transportHandle;
AzureIoTClient 43:038d8511e817 572 setTransportProtocol(handleData, (TRANSPORT_PROVIDER*)config->protocol());
AzureIoTClient 43:038d8511e817 573
AzureIoTClient 44:33dd78697616 574 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 43:038d8511e817 575 const char* hostname = STRING_c_str(handleData->IoTHubTransport_GetHostname(handleData->transportHandle));
AzureIoTClient 43:038d8511e817 576 /*Codes_SRS_IOTHUBCLIENT_LL_02_096: [ IoTHubClient_LL_CreateWithTransport shall create the data structures needed to instantiate a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE. ]*/
AzureIoTClient 43:038d8511e817 577 /*the first '.' says where the iothubname finishes*/
AzureIoTClient 43:038d8511e817 578 const char* whereIsDot = strchr(hostname, '.');
AzureIoTClient 43:038d8511e817 579 if (whereIsDot == NULL)
AzureIoTClient 42:448eecc3676e 580 {
AzureIoTClient 43:038d8511e817 581 /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateWithTransport shall fail and return NULL. ]*/
AzureIoTClient 43:038d8511e817 582 LogError("unable to determine the IoTHub name");
AzureIoTClient 42:448eecc3676e 583 free(handleData);
AzureIoTClient 42:448eecc3676e 584 result = NULL;
AzureIoTClient 42:448eecc3676e 585 }
AzureIoTClient 42:448eecc3676e 586 else
AzureIoTClient 42:448eecc3676e 587 {
AzureIoTClient 43:038d8511e817 588 /*Codes_SRS_IOTHUBCLIENT_LL_02_096: [ IoTHubClient_LL_CreateWithTransport shall create the data structures needed to instantiate a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE. ]*/
AzureIoTClient 53:1e5a1ca1f274 589 char* IoTHubName = (char*) malloc(whereIsDot - hostname + 1);
AzureIoTClient 43:038d8511e817 590 if (IoTHubName == NULL)
AzureIoTClient 42:448eecc3676e 591 {
AzureIoTClient 43:038d8511e817 592 /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateWithTransport shall fail and return NULL. ]*/
AzureIoTClient 43:038d8511e817 593 LogError("unable to malloc");
AzureIoTClient 42:448eecc3676e 594 free(handleData);
AzureIoTClient 42:448eecc3676e 595 result = NULL;
AzureIoTClient 42:448eecc3676e 596 }
AzureIoTClient 42:448eecc3676e 597 else
AzureIoTClient 42:448eecc3676e 598 {
AzureIoTClient 43:038d8511e817 599 const char* IotHubSuffix = whereIsDot + 1;
AzureIoTClient 58:15b0d29b2667 600 (void)memcpy(IoTHubName, hostname, whereIsDot - hostname);
AzureIoTClient 43:038d8511e817 601 IoTHubName[whereIsDot - hostname ] = '\0';
AzureIoTClient 43:038d8511e817 602
AzureIoTClient 43:038d8511e817 603 IOTHUB_CLIENT_CONFIG temp;
AzureIoTClient 43:038d8511e817 604 temp.deviceId = config->deviceId;
AzureIoTClient 43:038d8511e817 605 temp.deviceKey = config->deviceKey;
AzureIoTClient 43:038d8511e817 606 temp.deviceSasToken = config->deviceSasToken;
AzureIoTClient 43:038d8511e817 607 temp.iotHubName = IoTHubName;
AzureIoTClient 43:038d8511e817 608 temp.iotHubSuffix = IotHubSuffix;
AzureIoTClient 43:038d8511e817 609 temp.protocol = NULL; /*irrelevant to IoTHubClient_LL_UploadToBlob*/
AzureIoTClient 43:038d8511e817 610 temp.protocolGatewayHostName = NULL; /*irrelevant to IoTHubClient_LL_UploadToBlob*/
AzureIoTClient 43:038d8511e817 611
AzureIoTClient 43:038d8511e817 612 /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateWithTransport shall fail and return NULL. ]*/
AzureIoTClient 43:038d8511e817 613 handleData->uploadToBlobHandle = IoTHubClient_LL_UploadToBlob_Create(&temp);
AzureIoTClient 43:038d8511e817 614 if (handleData->uploadToBlobHandle == NULL)
AzureIoTClient 43:038d8511e817 615 {
AzureIoTClient 43:038d8511e817 616 /*Codes_SRS_IOTHUBCLIENT_LL_02_096: [ IoTHubClient_LL_CreateWithTransport shall create the data structures needed to instantiate a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE. ]*/
AzureIoTClient 43:038d8511e817 617 LogError("unable to IoTHubClient_LL_UploadToBlob_Create");
AzureIoTClient 43:038d8511e817 618 free(handleData);
AzureIoTClient 43:038d8511e817 619 result = NULL;
AzureIoTClient 43:038d8511e817 620 }
AzureIoTClient 43:038d8511e817 621 else
AzureIoTClient 43:038d8511e817 622 #endif
AzureIoTClient 43:038d8511e817 623 {
AzureIoTClient 43:038d8511e817 624 /*Codes_SRS_IOTHUBCLIENT_LL_02_047: [ IoTHubClient_LL_CreateWithTransport shall create a TICK_COUNTER_HANDLE. ]*/
AzureIoTClient 43:038d8511e817 625 if ((handleData->tickCounter = tickcounter_create()) == NULL)
AzureIoTClient 43:038d8511e817 626 {
AzureIoTClient 43:038d8511e817 627 /*Codes_SRS_IOTHUBCLIENT_LL_02_048: [ If creating the handle fails, then IoTHubClient_LL_CreateWithTransport shall fail and return NULL ]*/
AzureIoTClient 43:038d8511e817 628 LogError("unable to get a tickcounter");
AzureIoTClient 44:33dd78697616 629 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 43:038d8511e817 630 IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle);
AzureIoTClient 43:038d8511e817 631 #endif
AzureIoTClient 43:038d8511e817 632 free(handleData);
AzureIoTClient 43:038d8511e817 633 result = NULL;
AzureIoTClient 43:038d8511e817 634 }
AzureIoTClient 43:038d8511e817 635 else
AzureIoTClient 43:038d8511e817 636 {
AzureIoTClient 43:038d8511e817 637 /*Codes_SRS_IOTHUBCLIENT_LL_17_004: [IoTHubClient_LL_CreateWithTransport 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 43:038d8511e817 638 DList_InitializeListHead(&(handleData->waitingToSend));
AzureIoTClient 53:1e5a1ca1f274 639 DList_InitializeListHead(&(handleData->iot_msg_queue));
AzureIoTClient 53:1e5a1ca1f274 640 DList_InitializeListHead(&(handleData->iot_ack_queue));
AzureIoTClient 43:038d8511e817 641 handleData->messageCallback = NULL;
AzureIoTClient 43:038d8511e817 642 handleData->messageUserContextCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 643 handleData->deviceTwinCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 644 handleData->deviceTwinContextCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 645 handleData->deviceMethodCallback = NULL;
AzureIoTClient 55:59b527ab3452 646 handleData->deviceInboundMethodCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 647 handleData->deviceMethodUserContextCallback = NULL;
AzureIoTClient 43:038d8511e817 648 handleData->lastMessageReceiveTime = INDEFINITE_TIME;
AzureIoTClient 53:1e5a1ca1f274 649 handleData->data_msg_id = 1;
AzureIoTClient 53:1e5a1ca1f274 650 handleData->complete_twin_update_encountered = false;
AzureIoTClient 43:038d8511e817 651
AzureIoTClient 43:038d8511e817 652 IOTHUB_DEVICE_CONFIG deviceConfig;
AzureIoTClient 43:038d8511e817 653
AzureIoTClient 43:038d8511e817 654 deviceConfig.deviceId = config->deviceId;
AzureIoTClient 43:038d8511e817 655 deviceConfig.deviceKey = config->deviceKey;
AzureIoTClient 43:038d8511e817 656 deviceConfig.deviceSasToken = config->deviceSasToken;
AzureIoTClient 43:038d8511e817 657
AzureIoTClient 43:038d8511e817 658 /*Codes_SRS_IOTHUBCLIENT_LL_17_006: [IoTHubClient_LL_CreateWithTransport shall call the transport _Register function with the IOTHUB_DEVICE_CONFIG populated structure and waitingToSend list.]*/
AzureIoTClient 43:038d8511e817 659 if ((handleData->deviceHandle = handleData->IoTHubTransport_Register(config->transportHandle, &deviceConfig, handleData, &(handleData->waitingToSend))) == NULL)
AzureIoTClient 43:038d8511e817 660 {
AzureIoTClient 43:038d8511e817 661 /*Codes_SRS_IOTHUBCLIENT_LL_17_007: [If the _Register function fails, this function shall fail and return NULL.]*/
AzureIoTClient 43:038d8511e817 662 LogError("Registering device in transport failed");
AzureIoTClient 44:33dd78697616 663 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 43:038d8511e817 664 IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle);
AzureIoTClient 43:038d8511e817 665 #endif
AzureIoTClient 43:038d8511e817 666 tickcounter_destroy(handleData->tickCounter);
AzureIoTClient 43:038d8511e817 667 free(handleData);
AzureIoTClient 43:038d8511e817 668 result = NULL;
AzureIoTClient 43:038d8511e817 669 }
AzureIoTClient 43:038d8511e817 670 else
AzureIoTClient 43:038d8511e817 671 {
AzureIoTClient 43:038d8511e817 672 /*Codes_SRS_IOTHUBCLIENT_LL_17_005: [IoTHubClient_LL_CreateWithTransport shall save the transport handle and mark this transport as shared.]*/
AzureIoTClient 43:038d8511e817 673 handleData->isSharedTransport = true;
AzureIoTClient 43:038d8511e817 674 /*Codes_SRS_IOTHUBCLIENT_LL_02_042: [ By default, messages shall not timeout. ]*/
AzureIoTClient 43:038d8511e817 675 handleData->currentMessageTimeout = 0;
AzureIoTClient 53:1e5a1ca1f274 676 handleData->current_device_twin_timeout = 0;
AzureIoTClient 43:038d8511e817 677 result = handleData;
AzureIoTClient 53:1e5a1ca1f274 678 /*Codes_SRS_IOTHUBCLIENT_LL_25_125: [ `IoTHubClient_LL_CreateWithTransport` shall set the default retry policy as Exponential backoff with jitter and if succeed and return a `non-NULL` handle. ]*/
AzureIoTClient 53:1e5a1ca1f274 679 if (IoTHubClient_LL_SetRetryPolicy(handleData, IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF_WITH_JITTER, 0) != IOTHUB_CLIENT_OK)
AzureIoTClient 53:1e5a1ca1f274 680 {
AzureIoTClient 53:1e5a1ca1f274 681 LogError("Setting default retry policy in transport failed");
AzureIoTClient 53:1e5a1ca1f274 682 IoTHubClient_LL_Destroy(handleData);
AzureIoTClient 53:1e5a1ca1f274 683 result = NULL;
AzureIoTClient 53:1e5a1ca1f274 684 }
AzureIoTClient 43:038d8511e817 685 }
AzureIoTClient 43:038d8511e817 686 }
AzureIoTClient 43:038d8511e817 687 }
AzureIoTClient 44:33dd78697616 688 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 43:038d8511e817 689 free(IoTHubName);
AzureIoTClient 42:448eecc3676e 690 }
AzureIoTClient 42:448eecc3676e 691 }
AzureIoTClient 43:038d8511e817 692 #endif
AzureIoTClient 42:448eecc3676e 693 }
AzureIoTClient 42:448eecc3676e 694 }
Azure.IoT Build 36:67300d5a4c1f 695
AzureIoTClient 42:448eecc3676e 696 return result;
Azure.IoT Build 36:67300d5a4c1f 697 }
Azure.IoT Build 36:67300d5a4c1f 698
AzureIoTClient 16:deba40344375 699 void IoTHubClient_LL_Destroy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 16:deba40344375 700 {
AzureIoTClient 42:448eecc3676e 701 /*Codes_SRS_IOTHUBCLIENT_LL_02_009: [IoTHubClient_LL_Destroy shall do nothing if parameter iotHubClientHandle is NULL.]*/
AzureIoTClient 42:448eecc3676e 702 if (iotHubClientHandle != NULL)
AzureIoTClient 42:448eecc3676e 703 {
AzureIoTClient 42:448eecc3676e 704 PDLIST_ENTRY unsend;
AzureIoTClient 42:448eecc3676e 705 /*Codes_SRS_IOTHUBCLIENT_LL_17_010: [IoTHubClient_LL_Destroy shall call the underlaying layer's _Unregister function] */
AzureIoTClient 42:448eecc3676e 706 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 707 handleData->IoTHubTransport_Unregister(handleData->deviceHandle);
AzureIoTClient 42:448eecc3676e 708 if (handleData->isSharedTransport == false)
AzureIoTClient 42:448eecc3676e 709 {
AzureIoTClient 42:448eecc3676e 710 /*Codes_SRS_IOTHUBCLIENT_LL_02_010: [If iotHubClientHandle was not created by IoTHubClient_LL_CreateWithTransport, IoTHubClient_LL_Destroy shall call the underlaying layer's _Destroy function.] */
AzureIoTClient 42:448eecc3676e 711 handleData->IoTHubTransport_Destroy(handleData->transportHandle);
AzureIoTClient 42:448eecc3676e 712 }
AzureIoTClient 42:448eecc3676e 713 /*if any, remove the items currently not send*/
AzureIoTClient 42:448eecc3676e 714 while ((unsend = DList_RemoveHeadList(&(handleData->waitingToSend))) != &(handleData->waitingToSend))
AzureIoTClient 42:448eecc3676e 715 {
AzureIoTClient 42:448eecc3676e 716 IOTHUB_MESSAGE_LIST* temp = containingRecord(unsend, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 42:448eecc3676e 717 /*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 42:448eecc3676e 718 if (temp->callback != NULL)
AzureIoTClient 42:448eecc3676e 719 {
AzureIoTClient 42:448eecc3676e 720 temp->callback(IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY, temp->context);
AzureIoTClient 42:448eecc3676e 721 }
AzureIoTClient 42:448eecc3676e 722 IoTHubMessage_Destroy(temp->messageHandle);
AzureIoTClient 42:448eecc3676e 723 free(temp);
AzureIoTClient 42:448eecc3676e 724 }
AzureIoTClient 53:1e5a1ca1f274 725
AzureIoTClient 53:1e5a1ca1f274 726 /* Codes_SRS_IOTHUBCLIENT_LL_07_007: [ IoTHubClient_LL_Destroy shall iterate the device twin queues and destroy any remaining items. ] */
AzureIoTClient 53:1e5a1ca1f274 727 while ((unsend = DList_RemoveHeadList(&(handleData->iot_msg_queue))) != &(handleData->iot_msg_queue))
AzureIoTClient 53:1e5a1ca1f274 728 {
AzureIoTClient 53:1e5a1ca1f274 729 IOTHUB_DEVICE_TWIN* temp = containingRecord(unsend, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 730 device_twin_data_destroy(temp);
AzureIoTClient 53:1e5a1ca1f274 731 }
AzureIoTClient 53:1e5a1ca1f274 732 while ((unsend = DList_RemoveHeadList(&(handleData->iot_ack_queue))) != &(handleData->iot_ack_queue))
AzureIoTClient 53:1e5a1ca1f274 733 {
AzureIoTClient 53:1e5a1ca1f274 734 IOTHUB_DEVICE_TWIN* temp = containingRecord(unsend, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 735 device_twin_data_destroy(temp);
AzureIoTClient 53:1e5a1ca1f274 736 }
AzureIoTClient 53:1e5a1ca1f274 737
AzureIoTClient 42:448eecc3676e 738 /*Codes_SRS_IOTHUBCLIENT_LL_17_011: [IoTHubClient_LL_Destroy shall free the resources allocated by IoTHubClient (if any).] */
AzureIoTClient 42:448eecc3676e 739 tickcounter_destroy(handleData->tickCounter);
AzureIoTClient 44:33dd78697616 740 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 741 IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle);
AzureIoTClient 43:038d8511e817 742 #endif
AzureIoTClient 42:448eecc3676e 743 free(handleData);
AzureIoTClient 42:448eecc3676e 744 }
AzureIoTClient 16:deba40344375 745 }
AzureIoTClient 16:deba40344375 746
Azure.IoT Build 36:67300d5a4c1f 747 /*Codes_SRS_IOTHUBCLIENT_LL_02_044: [ Messages already delivered to IoTHubClient_LL shall not have their timeouts modified by a new call to IoTHubClient_LL_SetOption. ]*/
Azure.IoT Build 36:67300d5a4c1f 748 /*returns 0 on success, any other value is error*/
Azure.IoT Build 36:67300d5a4c1f 749 static int attach_ms_timesOutAfter(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData, IOTHUB_MESSAGE_LIST *newEntry)
Azure.IoT Build 36:67300d5a4c1f 750 {
AzureIoTClient 42:448eecc3676e 751 int result;
AzureIoTClient 42:448eecc3676e 752 /*Codes_SRS_IOTHUBCLIENT_LL_02_043: [ Calling IoTHubClient_LL_SetOption with value set to "0" shall disable the timeout mechanism for all new messages. ]*/
AzureIoTClient 42:448eecc3676e 753 if (handleData->currentMessageTimeout == 0)
AzureIoTClient 42:448eecc3676e 754 {
AzureIoTClient 42:448eecc3676e 755 newEntry->ms_timesOutAfter = 0; /*do not timeout*/
AzureIoTClient 42:448eecc3676e 756 result = 0;
AzureIoTClient 42:448eecc3676e 757 }
AzureIoTClient 42:448eecc3676e 758 else
AzureIoTClient 42:448eecc3676e 759 {
AzureIoTClient 42:448eecc3676e 760 /*Codes_SRS_IOTHUBCLIENT_LL_02_039: [ "messageTimeout" - once IoTHubClient_LL_SendEventAsync is called the message shall timeout after value miliseconds. Value is a pointer to a uint64. ]*/
AzureIoTClient 42:448eecc3676e 761 if (tickcounter_get_current_ms(handleData->tickCounter, &newEntry->ms_timesOutAfter) != 0)
AzureIoTClient 42:448eecc3676e 762 {
AzureIoTClient 60:41648c4e7036 763 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 764 LogError("unable to get the current relative tickcount");
AzureIoTClient 42:448eecc3676e 765 }
AzureIoTClient 42:448eecc3676e 766 else
AzureIoTClient 42:448eecc3676e 767 {
AzureIoTClient 42:448eecc3676e 768 newEntry->ms_timesOutAfter += handleData->currentMessageTimeout;
AzureIoTClient 42:448eecc3676e 769 result = 0;
AzureIoTClient 42:448eecc3676e 770 }
AzureIoTClient 42:448eecc3676e 771 }
AzureIoTClient 42:448eecc3676e 772 return result;
Azure.IoT Build 36:67300d5a4c1f 773 }
Azure.IoT Build 36:67300d5a4c1f 774
AzureIoTClient 16:deba40344375 775 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 776 {
AzureIoTClient 42:448eecc3676e 777 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 778 /*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 42:448eecc3676e 779 if (
AzureIoTClient 42:448eecc3676e 780 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 781 (eventMessageHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 782 /*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 42:448eecc3676e 783 ((eventConfirmationCallback == NULL) && (userContextCallback != NULL))
AzureIoTClient 42:448eecc3676e 784 )
AzureIoTClient 42:448eecc3676e 785 {
AzureIoTClient 42:448eecc3676e 786 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 45:54c11b1b1407 787 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 788 }
AzureIoTClient 42:448eecc3676e 789 else
AzureIoTClient 42:448eecc3676e 790 {
AzureIoTClient 42:448eecc3676e 791 IOTHUB_MESSAGE_LIST *newEntry = (IOTHUB_MESSAGE_LIST*)malloc(sizeof(IOTHUB_MESSAGE_LIST));
AzureIoTClient 42:448eecc3676e 792 if (newEntry == NULL)
AzureIoTClient 42:448eecc3676e 793 {
AzureIoTClient 42:448eecc3676e 794 result = IOTHUB_CLIENT_ERROR;
Azure.IoT Build 45:54c11b1b1407 795 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 796 }
AzureIoTClient 42:448eecc3676e 797 else
AzureIoTClient 42:448eecc3676e 798 {
AzureIoTClient 42:448eecc3676e 799 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 40:1a94db9139ea 800
AzureIoTClient 42:448eecc3676e 801 if (attach_ms_timesOutAfter(handleData, newEntry) != 0)
AzureIoTClient 42:448eecc3676e 802 {
AzureIoTClient 42:448eecc3676e 803 result = IOTHUB_CLIENT_ERROR;
Azure.IoT Build 45:54c11b1b1407 804 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 805 free(newEntry);
AzureIoTClient 42:448eecc3676e 806 }
AzureIoTClient 42:448eecc3676e 807 else
AzureIoTClient 42:448eecc3676e 808 {
AzureIoTClient 42:448eecc3676e 809 /*Codes_SRS_IOTHUBCLIENT_LL_02_013: [IoTHubClient_SendEventAsync shall add the DLIST waitingToSend a new record cloning the information from eventMessageHandle, eventConfirmationCallback, userContextCallback.]*/
AzureIoTClient 42:448eecc3676e 810 if ((newEntry->messageHandle = IoTHubMessage_Clone(eventMessageHandle)) == NULL)
AzureIoTClient 42:448eecc3676e 811 {
AzureIoTClient 42:448eecc3676e 812 /*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 42:448eecc3676e 813 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 814 free(newEntry);
Azure.IoT Build 45:54c11b1b1407 815 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 816 }
AzureIoTClient 42:448eecc3676e 817 else
AzureIoTClient 42:448eecc3676e 818 {
AzureIoTClient 42:448eecc3676e 819 /*Codes_SRS_IOTHUBCLIENT_LL_02_013: [IoTHubClient_SendEventAsync shall add the DLIST waitingToSend a new record cloning the information from eventMessageHandle, eventConfirmationCallback, userContextCallback.]*/
AzureIoTClient 42:448eecc3676e 820 newEntry->callback = eventConfirmationCallback;
AzureIoTClient 42:448eecc3676e 821 newEntry->context = userContextCallback;
AzureIoTClient 46:6a69294b6119 822 DList_InsertTailList(&(iotHubClientHandle->waitingToSend), &(newEntry->entry));
AzureIoTClient 42:448eecc3676e 823 /*Codes_SRS_IOTHUBCLIENT_LL_02_015: [Otherwise IoTHubClient_LL_SendEventAsync shall succeed and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 42:448eecc3676e 824 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 825 }
AzureIoTClient 42:448eecc3676e 826 }
AzureIoTClient 42:448eecc3676e 827 }
AzureIoTClient 42:448eecc3676e 828 }
AzureIoTClient 42:448eecc3676e 829 return result;
AzureIoTClient 16:deba40344375 830 }
AzureIoTClient 16:deba40344375 831
AzureIoTClient 16:deba40344375 832 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetMessageCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback)
AzureIoTClient 16:deba40344375 833 {
AzureIoTClient 42:448eecc3676e 834 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 835 /*Codes_SRS_IOTHUBCLIENT_LL_02_016: [IoTHubClient_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.] */
AzureIoTClient 42:448eecc3676e 836 if (iotHubClientHandle == NULL)
AzureIoTClient 42:448eecc3676e 837 {
AzureIoTClient 42:448eecc3676e 838 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 45:54c11b1b1407 839 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 840 }
AzureIoTClient 42:448eecc3676e 841 else
AzureIoTClient 42:448eecc3676e 842 {
AzureIoTClient 42:448eecc3676e 843 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 844 if (messageCallback == NULL)
AzureIoTClient 42:448eecc3676e 845 {
AzureIoTClient 42:448eecc3676e 846 /*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 42:448eecc3676e 847 handleData->IoTHubTransport_Unsubscribe(handleData->deviceHandle);
AzureIoTClient 42:448eecc3676e 848 handleData->messageCallback = NULL;
AzureIoTClient 42:448eecc3676e 849 handleData->messageUserContextCallback = NULL;
AzureIoTClient 42:448eecc3676e 850 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 851 }
AzureIoTClient 42:448eecc3676e 852 else
AzureIoTClient 42:448eecc3676e 853 {
AzureIoTClient 42:448eecc3676e 854 /*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 42:448eecc3676e 855 if (handleData->IoTHubTransport_Subscribe(handleData->deviceHandle) == 0)
AzureIoTClient 42:448eecc3676e 856 {
AzureIoTClient 42:448eecc3676e 857 handleData->messageCallback = messageCallback;
AzureIoTClient 42:448eecc3676e 858 handleData->messageUserContextCallback = userContextCallback;
AzureIoTClient 42:448eecc3676e 859 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 860 }
AzureIoTClient 42:448eecc3676e 861 else
AzureIoTClient 42:448eecc3676e 862 {
AzureIoTClient 42:448eecc3676e 863 handleData->messageCallback = NULL;
AzureIoTClient 42:448eecc3676e 864 handleData->messageUserContextCallback = NULL;
AzureIoTClient 42:448eecc3676e 865 /*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 42:448eecc3676e 866 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 867 }
AzureIoTClient 42:448eecc3676e 868 }
AzureIoTClient 42:448eecc3676e 869 }
AzureIoTClient 40:1a94db9139ea 870
AzureIoTClient 42:448eecc3676e 871 return result;
AzureIoTClient 16:deba40344375 872 }
AzureIoTClient 16:deba40344375 873
Azure.IoT Build 36:67300d5a4c1f 874 static void DoTimeouts(IOTHUB_CLIENT_LL_HANDLE_DATA* handleData)
Azure.IoT Build 36:67300d5a4c1f 875 {
Azure.IoT.Build 54:6dcad9019a64 876 tickcounter_ms_t nowTick;
AzureIoTClient 42:448eecc3676e 877 if (tickcounter_get_current_ms(handleData->tickCounter, &nowTick) != 0)
AzureIoTClient 42:448eecc3676e 878 {
AzureIoTClient 42:448eecc3676e 879 LogError("unable to get the current ms, timeouts will not be processed");
AzureIoTClient 42:448eecc3676e 880 }
AzureIoTClient 42:448eecc3676e 881 else
AzureIoTClient 42:448eecc3676e 882 {
AzureIoTClient 42:448eecc3676e 883 DLIST_ENTRY* currentItemInWaitingToSend = handleData->waitingToSend.Flink;
AzureIoTClient 42:448eecc3676e 884 while (currentItemInWaitingToSend != &(handleData->waitingToSend)) /*while we are not at the end of the list*/
AzureIoTClient 42:448eecc3676e 885 {
AzureIoTClient 42:448eecc3676e 886 IOTHUB_MESSAGE_LIST* fullEntry = containingRecord(currentItemInWaitingToSend, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 42:448eecc3676e 887 /*Codes_SRS_IOTHUBCLIENT_LL_02_041: [ If more than value miliseconds have passed since the call to IoTHubClient_LL_SendEventAsync then the message callback shall be called with a status code of IOTHUB_CLIENT_CONFIRMATION_TIMEOUT. ]*/
AzureIoTClient 42:448eecc3676e 888 if ((fullEntry->ms_timesOutAfter != 0) && (fullEntry->ms_timesOutAfter < nowTick))
AzureIoTClient 42:448eecc3676e 889 {
AzureIoTClient 42:448eecc3676e 890 PDLIST_ENTRY theNext = currentItemInWaitingToSend->Flink; /*need to save the next item, because the below operations are destructive*/
AzureIoTClient 42:448eecc3676e 891 DList_RemoveEntryList(currentItemInWaitingToSend);
AzureIoTClient 42:448eecc3676e 892 if (fullEntry->callback != NULL)
AzureIoTClient 42:448eecc3676e 893 {
AzureIoTClient 42:448eecc3676e 894 fullEntry->callback(IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT, fullEntry->context);
AzureIoTClient 42:448eecc3676e 895 }
AzureIoTClient 42:448eecc3676e 896 IoTHubMessage_Destroy(fullEntry->messageHandle); /*because it has been cloned*/
AzureIoTClient 42:448eecc3676e 897 free(fullEntry);
AzureIoTClient 42:448eecc3676e 898 currentItemInWaitingToSend = theNext;
AzureIoTClient 42:448eecc3676e 899 }
AzureIoTClient 42:448eecc3676e 900 else
AzureIoTClient 42:448eecc3676e 901 {
AzureIoTClient 42:448eecc3676e 902 currentItemInWaitingToSend = currentItemInWaitingToSend->Flink;
AzureIoTClient 42:448eecc3676e 903 }
AzureIoTClient 42:448eecc3676e 904 }
AzureIoTClient 42:448eecc3676e 905 }
Azure.IoT Build 36:67300d5a4c1f 906 }
Azure.IoT Build 36:67300d5a4c1f 907
AzureIoTClient 16:deba40344375 908 void IoTHubClient_LL_DoWork(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 16:deba40344375 909 {
AzureIoTClient 42:448eecc3676e 910 /*Codes_SRS_IOTHUBCLIENT_LL_02_020: [If parameter iotHubClientHandle is NULL then IoTHubClient_LL_DoWork shall not perform any action.] */
AzureIoTClient 42:448eecc3676e 911 if (iotHubClientHandle != NULL)
AzureIoTClient 42:448eecc3676e 912 {
AzureIoTClient 42:448eecc3676e 913 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 914 DoTimeouts(handleData);
AzureIoTClient 42:448eecc3676e 915
AzureIoTClient 53:1e5a1ca1f274 916 /*Codes_SRS_IOTHUBCLIENT_LL_07_008: [ IoTHubClient_LL_DoWork shall iterate the message queue and execute the underlying transports IoTHubTransport_ProcessItem function for each item. ] */
AzureIoTClient 53:1e5a1ca1f274 917 DLIST_ENTRY* client_item = handleData->iot_msg_queue.Flink;
AzureIoTClient 53:1e5a1ca1f274 918 while (client_item != &(handleData->iot_msg_queue)) /*while we are not at the end of the list*/
AzureIoTClient 53:1e5a1ca1f274 919 {
AzureIoTClient 53:1e5a1ca1f274 920 PDLIST_ENTRY next_item = client_item->Flink;
AzureIoTClient 53:1e5a1ca1f274 921
AzureIoTClient 53:1e5a1ca1f274 922 IOTHUB_DEVICE_TWIN* queue_data = containingRecord(client_item, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 923 IOTHUB_IDENTITY_INFO identity_info;
AzureIoTClient 53:1e5a1ca1f274 924 identity_info.device_twin = queue_data;
AzureIoTClient 53:1e5a1ca1f274 925 IOTHUB_PROCESS_ITEM_RESULT process_results = handleData->IoTHubTransport_ProcessItem(handleData->transportHandle, IOTHUB_TYPE_DEVICE_TWIN, &identity_info);
AzureIoTClient 53:1e5a1ca1f274 926 if (process_results == IOTHUB_PROCESS_CONTINUE || process_results == IOTHUB_PROCESS_NOT_CONNECTED)
AzureIoTClient 53:1e5a1ca1f274 927 {
AzureIoTClient 53:1e5a1ca1f274 928 /*Codes_SRS_IOTHUBCLIENT_LL_07_010: [ If 'IoTHubTransport_ProcessItem' returns IOTHUB_PROCESS_CONTINUE or IOTHUB_PROCESS_NOT_CONNECTED IoTHubClient_LL_DoWork shall continue on to call the underlaying layer's _DoWork function. ]*/
AzureIoTClient 53:1e5a1ca1f274 929 break;
AzureIoTClient 53:1e5a1ca1f274 930 }
AzureIoTClient 53:1e5a1ca1f274 931 else
AzureIoTClient 53:1e5a1ca1f274 932 {
AzureIoTClient 53:1e5a1ca1f274 933 DList_RemoveEntryList(client_item);
AzureIoTClient 53:1e5a1ca1f274 934 if (process_results == IOTHUB_PROCESS_OK)
AzureIoTClient 53:1e5a1ca1f274 935 {
AzureIoTClient 53:1e5a1ca1f274 936 /*Codes_SRS_IOTHUBCLIENT_LL_07_011: [ If 'IoTHubTransport_ProcessItem' returns IOTHUB_PROCESS_OK IoTHubClient_LL_DoWork shall add the IOTHUB_DEVICE_TWIN to the ack queue. ]*/
AzureIoTClient 53:1e5a1ca1f274 937 DList_InsertTailList(&(iotHubClientHandle->iot_ack_queue), &(queue_data->entry));
AzureIoTClient 53:1e5a1ca1f274 938 }
AzureIoTClient 53:1e5a1ca1f274 939 else
AzureIoTClient 53:1e5a1ca1f274 940 {
AzureIoTClient 53:1e5a1ca1f274 941 /*Codes_SRS_IOTHUBCLIENT_LL_07_012: [ If 'IoTHubTransport_ProcessItem' returns any other value IoTHubClient_LL_DoWork shall destroy the IOTHUB_DEVICE_TWIN item. ]*/
AzureIoTClient 53:1e5a1ca1f274 942 LogError("Failure queue processing item");
AzureIoTClient 53:1e5a1ca1f274 943 device_twin_data_destroy(queue_data);
AzureIoTClient 53:1e5a1ca1f274 944 }
AzureIoTClient 53:1e5a1ca1f274 945 }
AzureIoTClient 53:1e5a1ca1f274 946 // Move along to the next item
AzureIoTClient 53:1e5a1ca1f274 947 client_item = next_item;
AzureIoTClient 53:1e5a1ca1f274 948 }
AzureIoTClient 53:1e5a1ca1f274 949
AzureIoTClient 42:448eecc3676e 950 /*Codes_SRS_IOTHUBCLIENT_LL_02_021: [Otherwise, IoTHubClient_LL_DoWork shall invoke the underlaying layer's _DoWork function.]*/
AzureIoTClient 42:448eecc3676e 951 handleData->IoTHubTransport_DoWork(handleData->transportHandle, iotHubClientHandle);
AzureIoTClient 42:448eecc3676e 952 }
AzureIoTClient 16:deba40344375 953 }
AzureIoTClient 16:deba40344375 954
AzureIoTClient 16:deba40344375 955 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetSendStatus(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 16:deba40344375 956 {
AzureIoTClient 42:448eecc3676e 957 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 16:deba40344375 958
AzureIoTClient 42:448eecc3676e 959 /* Codes_SRS_IOTHUBCLIENT_09_007: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter] */
AzureIoTClient 42:448eecc3676e 960 if (iotHubClientHandle == NULL || iotHubClientStatus == NULL)
AzureIoTClient 42:448eecc3676e 961 {
AzureIoTClient 42:448eecc3676e 962 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 45:54c11b1b1407 963 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 964 }
AzureIoTClient 42:448eecc3676e 965 else
AzureIoTClient 42:448eecc3676e 966 {
AzureIoTClient 42:448eecc3676e 967 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 968
AzureIoTClient 42:448eecc3676e 969 /* 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 42:448eecc3676e 970 /* 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 42:448eecc3676e 971 result = handleData->IoTHubTransport_GetSendStatus(handleData->deviceHandle, iotHubClientStatus);
AzureIoTClient 42:448eecc3676e 972 }
AzureIoTClient 16:deba40344375 973
AzureIoTClient 42:448eecc3676e 974 return result;
AzureIoTClient 16:deba40344375 975 }
AzureIoTClient 16:deba40344375 976
Azure.IoT Build 45:54c11b1b1407 977 void IoTHubClient_LL_SendComplete(IOTHUB_CLIENT_LL_HANDLE handle, PDLIST_ENTRY completed, IOTHUB_CLIENT_CONFIRMATION_RESULT result)
AzureIoTClient 16:deba40344375 978 {
AzureIoTClient 42:448eecc3676e 979 /*Codes_SRS_IOTHUBCLIENT_LL_02_022: [If parameter completed is NULL, or parameter handle is NULL then IoTHubClient_LL_SendBatch shall return.]*/
AzureIoTClient 42:448eecc3676e 980 if (
AzureIoTClient 42:448eecc3676e 981 (handle == NULL) ||
AzureIoTClient 42:448eecc3676e 982 (completed == NULL)
AzureIoTClient 42:448eecc3676e 983 )
AzureIoTClient 42:448eecc3676e 984 {
AzureIoTClient 42:448eecc3676e 985 /*"shall return"*/
AzureIoTClient 42:448eecc3676e 986 LogError("invalid arg");
AzureIoTClient 42:448eecc3676e 987 }
AzureIoTClient 42:448eecc3676e 988 else
AzureIoTClient 42:448eecc3676e 989 {
Azure.IoT Build 45:54c11b1b1407 990 /*Codes_SRS_IOTHUBCLIENT_LL_02_027: [If parameter result is IOTHUB_CLIENT_CONFIRMATION_ERROR 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.] */
Azure.IoT Build 45:54c11b1b1407 991 /*Codes_SRS_IOTHUBCLIENT_LL_02_025: [If parameter result is IOTHUB_CLIENT_CONFIRMATION_OK 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 42:448eecc3676e 992 PDLIST_ENTRY oldest;
AzureIoTClient 42:448eecc3676e 993 while ((oldest = DList_RemoveHeadList(completed)) != completed)
AzureIoTClient 42:448eecc3676e 994 {
AzureIoTClient 42:448eecc3676e 995 IOTHUB_MESSAGE_LIST* messageList = (IOTHUB_MESSAGE_LIST*)containingRecord(oldest, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 42:448eecc3676e 996 /*Codes_SRS_IOTHUBCLIENT_LL_02_026: [If any callback is NULL then there shall not be a callback call.]*/
AzureIoTClient 42:448eecc3676e 997 if (messageList->callback != NULL)
AzureIoTClient 42:448eecc3676e 998 {
Azure.IoT Build 45:54c11b1b1407 999 messageList->callback(result, messageList->context);
AzureIoTClient 42:448eecc3676e 1000 }
AzureIoTClient 42:448eecc3676e 1001 IoTHubMessage_Destroy(messageList->messageHandle);
AzureIoTClient 42:448eecc3676e 1002 free(messageList);
AzureIoTClient 42:448eecc3676e 1003 }
AzureIoTClient 42:448eecc3676e 1004 }
AzureIoTClient 16:deba40344375 1005 }
AzureIoTClient 16:deba40344375 1006
AzureIoTClient 55:59b527ab3452 1007 int IoTHubClient_LL_DeviceMethodComplete(IOTHUB_CLIENT_LL_HANDLE handle, const char* method_name, const unsigned char* payLoad, size_t size, METHOD_HANDLE response_id)
AzureIoTClient 53:1e5a1ca1f274 1008 {
AzureIoTClient 53:1e5a1ca1f274 1009 int result;
AzureIoTClient 55:59b527ab3452 1010 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1011 {
AzureIoTClient 53:1e5a1ca1f274 1012 /* Codes_SRS_IOTHUBCLIENT_LL_07_017: [ If handle or response is NULL then IoTHubClient_LL_DeviceMethodComplete shall return 500. ] */
AzureIoTClient 53:1e5a1ca1f274 1013 LogError("Invalid argument handle=%p", handle);
AzureIoTClient 60:41648c4e7036 1014 result = __FAILURE__;
AzureIoTClient 53:1e5a1ca1f274 1015 }
AzureIoTClient 53:1e5a1ca1f274 1016 else
AzureIoTClient 53:1e5a1ca1f274 1017 {
AzureIoTClient 53:1e5a1ca1f274 1018 /* Codes_SRS_IOTHUBCLIENT_LL_07_018: [ If deviceMethodCallback is not NULL IoTHubClient_LL_DeviceMethodComplete shall execute deviceMethodCallback and return the status. ] */
AzureIoTClient 53:1e5a1ca1f274 1019 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 53:1e5a1ca1f274 1020 if (handleData->deviceMethodCallback)
AzureIoTClient 53:1e5a1ca1f274 1021 {
AzureIoTClient 53:1e5a1ca1f274 1022 unsigned char* payload_resp = NULL;
AzureIoTClient 55:59b527ab3452 1023 size_t response_size = 0;
AzureIoTClient 55:59b527ab3452 1024 result = handleData->deviceMethodCallback(method_name, payLoad, size, &payload_resp, &response_size, handleData->deviceMethodUserContextCallback);
AzureIoTClient 53:1e5a1ca1f274 1025 /* Codes_SRS_IOTHUBCLIENT_LL_07_020: [ deviceMethodCallback shall buil the BUFFER_HANDLE with the response payload from the IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC callback. ] */
AzureIoTClient 55:59b527ab3452 1026 if (payload_resp != NULL && response_size > 0)
AzureIoTClient 53:1e5a1ca1f274 1027 {
AzureIoTClient 55:59b527ab3452 1028 result = handleData->IoTHubTransport_DeviceMethod_Response(handleData->deviceHandle, response_id, payload_resp, response_size, result);
AzureIoTClient 55:59b527ab3452 1029 }
AzureIoTClient 55:59b527ab3452 1030 else
AzureIoTClient 55:59b527ab3452 1031 {
AzureIoTClient 60:41648c4e7036 1032 result = __FAILURE__;
AzureIoTClient 53:1e5a1ca1f274 1033 }
AzureIoTClient 53:1e5a1ca1f274 1034 if (payload_resp != NULL)
AzureIoTClient 53:1e5a1ca1f274 1035 {
AzureIoTClient 53:1e5a1ca1f274 1036 free(payload_resp);
AzureIoTClient 53:1e5a1ca1f274 1037 }
AzureIoTClient 53:1e5a1ca1f274 1038 }
AzureIoTClient 55:59b527ab3452 1039 else if (handleData->deviceInboundMethodCallback)
AzureIoTClient 55:59b527ab3452 1040 {
AzureIoTClient 55:59b527ab3452 1041 result = handleData->deviceInboundMethodCallback(method_name, payLoad, size, response_id, handleData->deviceMethodUserContextCallback);
AzureIoTClient 55:59b527ab3452 1042 }
AzureIoTClient 53:1e5a1ca1f274 1043 else
AzureIoTClient 53:1e5a1ca1f274 1044 {
AzureIoTClient 53:1e5a1ca1f274 1045 /* Codes_SRS_IOTHUBCLIENT_LL_07_019: [ If deviceMethodCallback is NULL IoTHubClient_LL_DeviceMethodComplete shall return 404. ] */
AzureIoTClient 55:59b527ab3452 1046 result = 0;
AzureIoTClient 53:1e5a1ca1f274 1047 }
AzureIoTClient 53:1e5a1ca1f274 1048 }
AzureIoTClient 53:1e5a1ca1f274 1049 return result;
AzureIoTClient 53:1e5a1ca1f274 1050 }
AzureIoTClient 53:1e5a1ca1f274 1051
AzureIoTClient 53:1e5a1ca1f274 1052 void IoTHubClient_LL_RetrievePropertyComplete(IOTHUB_CLIENT_LL_HANDLE handle, DEVICE_TWIN_UPDATE_STATE update_state, const unsigned char* payLoad, size_t size)
AzureIoTClient 53:1e5a1ca1f274 1053 {
AzureIoTClient 53:1e5a1ca1f274 1054 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1055 {
AzureIoTClient 53:1e5a1ca1f274 1056 /* Codes_SRS_IOTHUBCLIENT_LL_07_013: [ If handle is NULL then IoTHubClient_LL_RetrievePropertyComplete shall do nothing.] */
AzureIoTClient 53:1e5a1ca1f274 1057 LogError("Invalid argument handle=%p", handle);
AzureIoTClient 53:1e5a1ca1f274 1058 }
AzureIoTClient 53:1e5a1ca1f274 1059 else
AzureIoTClient 53:1e5a1ca1f274 1060 {
AzureIoTClient 53:1e5a1ca1f274 1061 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 53:1e5a1ca1f274 1062 /* Codes_SRS_IOTHUBCLIENT_LL_07_014: [ If deviceTwinCallback is NULL then IoTHubClient_LL_RetrievePropertyComplete shall do nothing.] */
AzureIoTClient 53:1e5a1ca1f274 1063 if (handleData->deviceTwinCallback)
AzureIoTClient 53:1e5a1ca1f274 1064 {
AzureIoTClient 53:1e5a1ca1f274 1065 /* Codes_SRS_IOTHUBCLIENT_LL_07_015: [ If the the update_state parameter is DEVICE_TWIN_UPDATE_PARTIAL and a DEVICE_TWIN_UPDATE_COMPLETE has not been previously recieved then IoTHubClient_LL_RetrievePropertyComplete shall do nothing.] */
AzureIoTClient 53:1e5a1ca1f274 1066 if (update_state == DEVICE_TWIN_UPDATE_COMPLETE)
AzureIoTClient 53:1e5a1ca1f274 1067 {
AzureIoTClient 53:1e5a1ca1f274 1068 handleData->complete_twin_update_encountered = true;
AzureIoTClient 53:1e5a1ca1f274 1069 }
AzureIoTClient 53:1e5a1ca1f274 1070 if (handleData->complete_twin_update_encountered)
AzureIoTClient 53:1e5a1ca1f274 1071 {
AzureIoTClient 53:1e5a1ca1f274 1072 /* Codes_SRS_IOTHUBCLIENT_LL_07_016: [ If deviceTwinCallback is set and DEVICE_TWIN_UPDATE_COMPLETE has been encountered then IoTHubClient_LL_RetrievePropertyComplete shall call deviceTwinCallback.] */
AzureIoTClient 53:1e5a1ca1f274 1073 handleData->deviceTwinCallback(update_state, payLoad, size, handleData->deviceTwinContextCallback);
AzureIoTClient 53:1e5a1ca1f274 1074 }
AzureIoTClient 53:1e5a1ca1f274 1075 }
AzureIoTClient 53:1e5a1ca1f274 1076 }
AzureIoTClient 53:1e5a1ca1f274 1077 }
AzureIoTClient 53:1e5a1ca1f274 1078
AzureIoTClient 53:1e5a1ca1f274 1079 void IoTHubClient_LL_ReportedStateComplete(IOTHUB_CLIENT_LL_HANDLE handle, uint32_t item_id, int status_code)
AzureIoTClient 53:1e5a1ca1f274 1080 {
AzureIoTClient 53:1e5a1ca1f274 1081 /* Codes_SRS_IOTHUBCLIENT_LL_07_002: [ if handle or queue_handle are NULL then IoTHubClient_LL_ReportedStateComplete shall do nothing. ] */
AzureIoTClient 53:1e5a1ca1f274 1082 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1083 {
AzureIoTClient 53:1e5a1ca1f274 1084 /*"shall return"*/
AzureIoTClient 53:1e5a1ca1f274 1085 LogError("Invalid argument handle=%p", handle);
AzureIoTClient 53:1e5a1ca1f274 1086 }
AzureIoTClient 53:1e5a1ca1f274 1087 else
AzureIoTClient 53:1e5a1ca1f274 1088 {
AzureIoTClient 53:1e5a1ca1f274 1089 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 53:1e5a1ca1f274 1090
AzureIoTClient 53:1e5a1ca1f274 1091 /* Codes_SRS_IOTHUBCLIENT_LL_07_003: [ IoTHubClient_LL_ReportedStateComplete shall enumerate through the IOTHUB_DEVICE_TWIN structures in queue_handle. ]*/
AzureIoTClient 53:1e5a1ca1f274 1092 DLIST_ENTRY* client_item = handleData->iot_ack_queue.Flink;
AzureIoTClient 53:1e5a1ca1f274 1093 while (client_item != &(handleData->iot_ack_queue)) /*while we are not at the end of the list*/
AzureIoTClient 53:1e5a1ca1f274 1094 {
AzureIoTClient 53:1e5a1ca1f274 1095 PDLIST_ENTRY next_item = client_item->Flink;
AzureIoTClient 53:1e5a1ca1f274 1096 IOTHUB_DEVICE_TWIN* queue_data = containingRecord(client_item, IOTHUB_DEVICE_TWIN, entry);
AzureIoTClient 53:1e5a1ca1f274 1097 if (queue_data->item_id == item_id)
AzureIoTClient 53:1e5a1ca1f274 1098 {
AzureIoTClient 53:1e5a1ca1f274 1099 if (queue_data->reported_state_callback != NULL)
AzureIoTClient 53:1e5a1ca1f274 1100 {
AzureIoTClient 53:1e5a1ca1f274 1101 queue_data->reported_state_callback(status_code, queue_data->context);
AzureIoTClient 53:1e5a1ca1f274 1102 }
AzureIoTClient 53:1e5a1ca1f274 1103 /*Codes_SRS_IOTHUBCLIENT_LL_07_009: [ IoTHubClient_LL_ReportedStateComplete shall remove the IOTHUB_DEVICE_TWIN item from the ack queue.]*/
AzureIoTClient 53:1e5a1ca1f274 1104 DList_RemoveEntryList(client_item);
AzureIoTClient 53:1e5a1ca1f274 1105 device_twin_data_destroy(queue_data);
AzureIoTClient 53:1e5a1ca1f274 1106 break;
AzureIoTClient 53:1e5a1ca1f274 1107 }
AzureIoTClient 53:1e5a1ca1f274 1108 client_item = next_item;
AzureIoTClient 53:1e5a1ca1f274 1109 }
AzureIoTClient 53:1e5a1ca1f274 1110 }
AzureIoTClient 53:1e5a1ca1f274 1111 }
AzureIoTClient 53:1e5a1ca1f274 1112
AzureIoTClient 16:deba40344375 1113 IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubClient_LL_MessageCallback(IOTHUB_CLIENT_LL_HANDLE handle, IOTHUB_MESSAGE_HANDLE message)
AzureIoTClient 16:deba40344375 1114 {
AzureIoTClient 42:448eecc3676e 1115 int result;
AzureIoTClient 42:448eecc3676e 1116 /*Codes_SRS_IOTHUBCLIENT_LL_02_029: [If parameter handle is NULL then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */
AzureIoTClient 42:448eecc3676e 1117 if (handle == NULL)
AzureIoTClient 42:448eecc3676e 1118 {
AzureIoTClient 42:448eecc3676e 1119 LogError("invalid argument");
AzureIoTClient 42:448eecc3676e 1120 result = IOTHUBMESSAGE_ABANDONED;
AzureIoTClient 42:448eecc3676e 1121 }
AzureIoTClient 42:448eecc3676e 1122 else
AzureIoTClient 42:448eecc3676e 1123 {
AzureIoTClient 42:448eecc3676e 1124 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 16:deba40344375 1125
AzureIoTClient 42:448eecc3676e 1126 /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */
AzureIoTClient 42:448eecc3676e 1127 handleData->lastMessageReceiveTime = get_time(NULL);
AzureIoTClient 16:deba40344375 1128
AzureIoTClient 42:448eecc3676e 1129 /*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 42:448eecc3676e 1130 if (handleData->messageCallback != NULL)
AzureIoTClient 42:448eecc3676e 1131 {
AzureIoTClient 42:448eecc3676e 1132 result = handleData->messageCallback(message, handleData->messageUserContextCallback);
AzureIoTClient 42:448eecc3676e 1133 }
AzureIoTClient 42:448eecc3676e 1134 else
AzureIoTClient 42:448eecc3676e 1135 {
AzureIoTClient 42:448eecc3676e 1136 /*Codes_SRS_IOTHUBCLIENT_LL_02_032: [If the last callback function was NULL, then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */
AzureIoTClient 42:448eecc3676e 1137 LogError("user callback was NULL");
AzureIoTClient 42:448eecc3676e 1138 result = IOTHUBMESSAGE_ABANDONED;
AzureIoTClient 42:448eecc3676e 1139 }
AzureIoTClient 42:448eecc3676e 1140 }
AzureIoTClient 42:448eecc3676e 1141 /*Codes_SRS_IOTHUBCLIENT_LL_02_031: [Then IoTHubClient_LL_MessageCallback shall return what the user function returns.]*/
AzureIoTClient 53:1e5a1ca1f274 1142 return (IOTHUBMESSAGE_DISPOSITION_RESULT) result;
AzureIoTClient 16:deba40344375 1143 }
AzureIoTClient 16:deba40344375 1144
AzureIoTClient 53:1e5a1ca1f274 1145 void IotHubClient_LL_ConnectionStatusCallBack(IOTHUB_CLIENT_LL_HANDLE handle, IOTHUB_CLIENT_CONNECTION_STATUS status, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason)
AzureIoTClient 52:1cc3c6d07cad 1146 {
AzureIoTClient 53:1e5a1ca1f274 1147 /*Codes_SRS_IOTHUBCLIENT_LL_25_113: [If parameter connectionStatus is NULL or parameter handle is NULL then IotHubClient_LL_ConnectionStatusCallBack shall return.]*/
AzureIoTClient 53:1e5a1ca1f274 1148 if (handle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1149 {
AzureIoTClient 53:1e5a1ca1f274 1150 /*"shall return"*/
AzureIoTClient 53:1e5a1ca1f274 1151 LogError("invalid arg");
AzureIoTClient 53:1e5a1ca1f274 1152 }
AzureIoTClient 53:1e5a1ca1f274 1153 else
AzureIoTClient 53:1e5a1ca1f274 1154 {
AzureIoTClient 53:1e5a1ca1f274 1155 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle;
AzureIoTClient 53:1e5a1ca1f274 1156
AzureIoTClient 53:1e5a1ca1f274 1157 /*Codes_SRS_IOTHUBCLIENT_LL_25_114: [IotHubClient_LL_ConnectionStatusCallBack shall call non-callback set by the user from IoTHubClient_LL_SetConnectionStatusCallback passing the status, reason and the passed userContextCallback.]*/
AzureIoTClient 53:1e5a1ca1f274 1158 if (handleData->conStatusCallback != NULL)
AzureIoTClient 53:1e5a1ca1f274 1159 {
AzureIoTClient 53:1e5a1ca1f274 1160 handleData->conStatusCallback(status, reason, handleData->conStatusUserContextCallback);
AzureIoTClient 53:1e5a1ca1f274 1161 }
AzureIoTClient 53:1e5a1ca1f274 1162 }
AzureIoTClient 53:1e5a1ca1f274 1163
AzureIoTClient 52:1cc3c6d07cad 1164 }
AzureIoTClient 52:1cc3c6d07cad 1165
AzureIoTClient 52:1cc3c6d07cad 1166 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetConnectionStatusCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK connectionStatusCallback, void * userContextCallback)
AzureIoTClient 52:1cc3c6d07cad 1167 {
AzureIoTClient 53:1e5a1ca1f274 1168 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1169 /*Codes_SRS_IOTHUBCLIENT_LL_25_111: [IoTHubClient_LL_SetConnectionStatusCallback shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter iotHubClientHandle**]** */
AzureIoTClient 53:1e5a1ca1f274 1170 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1171 {
AzureIoTClient 53:1e5a1ca1f274 1172 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1173 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1174 }
AzureIoTClient 53:1e5a1ca1f274 1175 else
AzureIoTClient 53:1e5a1ca1f274 1176 {
AzureIoTClient 53:1e5a1ca1f274 1177 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1178 /*Codes_SRS_IOTHUBCLIENT_LL_25_112: [IoTHubClient_LL_SetConnectionStatusCallback shall return IOTHUB_CLIENT_OK and save the callback and userContext as a member of the handle.] */
AzureIoTClient 53:1e5a1ca1f274 1179 handleData->conStatusCallback = connectionStatusCallback;
AzureIoTClient 53:1e5a1ca1f274 1180 handleData->conStatusUserContextCallback = userContextCallback;
AzureIoTClient 53:1e5a1ca1f274 1181 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1182 }
AzureIoTClient 52:1cc3c6d07cad 1183
AzureIoTClient 52:1cc3c6d07cad 1184 return result;
AzureIoTClient 52:1cc3c6d07cad 1185 }
AzureIoTClient 52:1cc3c6d07cad 1186
AzureIoTClient 53:1e5a1ca1f274 1187 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetRetryPolicy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1188 {
AzureIoTClient 53:1e5a1ca1f274 1189 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1190 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 52:1cc3c6d07cad 1191
AzureIoTClient 53:1e5a1ca1f274 1192 /* Codes_SRS_IOTHUBCLIENT_LL_25_116: [**IoTHubClient_LL_SetRetryPolicy shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL iotHubClientHandle]*/
AzureIoTClient 53:1e5a1ca1f274 1193 if (handleData == NULL)
AzureIoTClient 53:1e5a1ca1f274 1194 {
AzureIoTClient 53:1e5a1ca1f274 1195 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1196 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1197 }
AzureIoTClient 53:1e5a1ca1f274 1198 else
AzureIoTClient 53:1e5a1ca1f274 1199 {
AzureIoTClient 53:1e5a1ca1f274 1200 if (handleData->transportHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1201 {
AzureIoTClient 53:1e5a1ca1f274 1202 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1203 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1204 }
AzureIoTClient 53:1e5a1ca1f274 1205 else
AzureIoTClient 53:1e5a1ca1f274 1206 {
AzureIoTClient 53:1e5a1ca1f274 1207 if (handleData->IoTHubTransport_SetRetryPolicy(handleData->transportHandle, retryPolicy, retryTimeoutLimitInSeconds) != 0)
AzureIoTClient 53:1e5a1ca1f274 1208 {
AzureIoTClient 53:1e5a1ca1f274 1209 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1210 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1211 }
AzureIoTClient 53:1e5a1ca1f274 1212 else
AzureIoTClient 53:1e5a1ca1f274 1213 {
AzureIoTClient 53:1e5a1ca1f274 1214 /*Codes_SRS_IOTHUBCLIENT_LL_25_118: [**IoTHubClient_LL_SetRetryPolicy shall save connection retry policies specified by the user to retryPolicy in struct IOTHUB_CLIENT_LL_HANDLE_DATA]*/
AzureIoTClient 53:1e5a1ca1f274 1215 /*Codes_SRS_IOTHUBCLIENT_LL_25_119: [**IoTHubClient_LL_SetRetryPolicy shall save retryTimeoutLimitInSeconds in seconds to retryTimeout in struct IOTHUB_CLIENT_LL_HANDLE_DATA]*/
AzureIoTClient 53:1e5a1ca1f274 1216 handleData->retryPolicy = retryPolicy;
AzureIoTClient 53:1e5a1ca1f274 1217 handleData->retryTimeoutLimitInSeconds = retryTimeoutLimitInSeconds;
AzureIoTClient 53:1e5a1ca1f274 1218 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1219 }
AzureIoTClient 53:1e5a1ca1f274 1220 }
AzureIoTClient 53:1e5a1ca1f274 1221 }
AzureIoTClient 52:1cc3c6d07cad 1222 return result;
AzureIoTClient 52:1cc3c6d07cad 1223 }
AzureIoTClient 52:1cc3c6d07cad 1224
AzureIoTClient 53:1e5a1ca1f274 1225 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetRetryPolicy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY* retryPolicy, size_t* retryTimeoutLimitInSeconds)
AzureIoTClient 52:1cc3c6d07cad 1226 {
AzureIoTClient 52:1cc3c6d07cad 1227 IOTHUB_CLIENT_RESULT result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1228 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1229
AzureIoTClient 53:1e5a1ca1f274 1230 /* Codes_SRS_IOTHUBCLIENT_LL_09_001: [IoTHubClient_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG if any of the arguments is NULL] */
AzureIoTClient 53:1e5a1ca1f274 1231 if (handleData == NULL || retryPolicy == NULL || retryTimeoutLimitInSeconds == NULL)
AzureIoTClient 53:1e5a1ca1f274 1232 {
AzureIoTClient 53:1e5a1ca1f274 1233 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1234 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1235 }
AzureIoTClient 53:1e5a1ca1f274 1236 else
AzureIoTClient 53:1e5a1ca1f274 1237 {
AzureIoTClient 53:1e5a1ca1f274 1238 *retryPolicy = handleData->retryPolicy;
AzureIoTClient 53:1e5a1ca1f274 1239 *retryTimeoutLimitInSeconds = handleData->retryTimeoutLimitInSeconds;
AzureIoTClient 53:1e5a1ca1f274 1240 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1241 }
AzureIoTClient 52:1cc3c6d07cad 1242
AzureIoTClient 52:1cc3c6d07cad 1243 return result;
AzureIoTClient 52:1cc3c6d07cad 1244 }
AzureIoTClient 52:1cc3c6d07cad 1245
AzureIoTClient 16:deba40344375 1246 IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetLastMessageReceiveTime(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime)
AzureIoTClient 16:deba40344375 1247 {
AzureIoTClient 42:448eecc3676e 1248 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1249 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 16:deba40344375 1250
AzureIoTClient 42:448eecc3676e 1251 /* Codes_SRS_IOTHUBCLIENT_LL_09_001: [IoTHubClient_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG if any of the arguments is NULL] */
AzureIoTClient 42:448eecc3676e 1252 if (handleData == NULL || lastMessageReceiveTime == NULL)
AzureIoTClient 42:448eecc3676e 1253 {
AzureIoTClient 42:448eecc3676e 1254 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 45:54c11b1b1407 1255 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 1256 }
AzureIoTClient 42:448eecc3676e 1257 else
AzureIoTClient 42:448eecc3676e 1258 {
AzureIoTClient 42:448eecc3676e 1259 /* 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 42:448eecc3676e 1260 if (handleData->lastMessageReceiveTime == INDEFINITE_TIME)
AzureIoTClient 42:448eecc3676e 1261 {
AzureIoTClient 42:448eecc3676e 1262 result = IOTHUB_CLIENT_INDEFINITE_TIME;
Azure.IoT Build 45:54c11b1b1407 1263 LOG_ERROR_RESULT;
AzureIoTClient 42:448eecc3676e 1264 }
AzureIoTClient 42:448eecc3676e 1265 else
AzureIoTClient 42:448eecc3676e 1266 {
AzureIoTClient 42:448eecc3676e 1267 /* 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 42:448eecc3676e 1268 /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */
AzureIoTClient 42:448eecc3676e 1269 *lastMessageReceiveTime = handleData->lastMessageReceiveTime;
AzureIoTClient 42:448eecc3676e 1270 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 1271 }
AzureIoTClient 42:448eecc3676e 1272 }
AzureIoTClient 16:deba40344375 1273
AzureIoTClient 42:448eecc3676e 1274 return result;
AzureIoTClient 16:deba40344375 1275 }
AzureIoTClient 16:deba40344375 1276
AzureIoTClient 16:deba40344375 1277 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetOption(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* optionName, const void* value)
AzureIoTClient 16:deba40344375 1278 {
AzureIoTClient 40:1a94db9139ea 1279
AzureIoTClient 42:448eecc3676e 1280 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1281 /*Codes_SRS_IOTHUBCLIENT_LL_02_034: [If iotHubClientHandle is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]*/
AzureIoTClient 42:448eecc3676e 1282 /*Codes_SRS_IOTHUBCLIENT_LL_02_035: [If optionName is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 42:448eecc3676e 1283 /*Codes_SRS_IOTHUBCLIENT_LL_02_036: [If value is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 42:448eecc3676e 1284 if (
AzureIoTClient 42:448eecc3676e 1285 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 1286 (optionName == NULL) ||
AzureIoTClient 42:448eecc3676e 1287 (value == NULL)
AzureIoTClient 42:448eecc3676e 1288 )
AzureIoTClient 42:448eecc3676e 1289 {
AzureIoTClient 42:448eecc3676e 1290 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 1291 LogError("invalid argument (NULL)");
AzureIoTClient 42:448eecc3676e 1292 }
AzureIoTClient 42:448eecc3676e 1293 else
AzureIoTClient 42:448eecc3676e 1294 {
AzureIoTClient 42:448eecc3676e 1295 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 42:448eecc3676e 1296
AzureIoTClient 42:448eecc3676e 1297 /*Codes_SRS_IOTHUBCLIENT_LL_02_039: [ "messageTimeout" - once IoTHubClient_LL_SendEventAsync is called the message shall timeout after value miliseconds. Value is a pointer to a uint64. ]*/
AzureIoTClient 42:448eecc3676e 1298 if (strcmp(optionName, "messageTimeout") == 0)
AzureIoTClient 42:448eecc3676e 1299 {
AzureIoTClient 42:448eecc3676e 1300 /*this is an option handled by IoTHubClient_LL*/
AzureIoTClient 42:448eecc3676e 1301 /*Codes_SRS_IOTHUBCLIENT_LL_02_043: [ Calling IoTHubClient_LL_SetOption with value set to "0" shall disable the timeout mechanism for all new messages. ]*/
Azure.IoT.Build 54:6dcad9019a64 1302 handleData->currentMessageTimeout = *(const tickcounter_ms_t*)value;
AzureIoTClient 42:448eecc3676e 1303 result = IOTHUB_CLIENT_OK;
AzureIoTClient 42:448eecc3676e 1304 }
AzureIoTClient 42:448eecc3676e 1305 else
AzureIoTClient 42:448eecc3676e 1306 {
AzureIoTClient 46:6a69294b6119 1307
AzureIoTClient 46:6a69294b6119 1308 /*Codes_SRS_IOTHUBCLIENT_LL_02_099: [ IoTHubClient_LL_SetOption shall return according to the table below ]*/
AzureIoTClient 46:6a69294b6119 1309 IOTHUB_CLIENT_RESULT uploadToBlob_result;
AzureIoTClient 46:6a69294b6119 1310 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 46:6a69294b6119 1311 uploadToBlob_result = IoTHubClient_LL_UploadToBlob_SetOption(handleData->uploadToBlobHandle, optionName, value);
AzureIoTClient 46:6a69294b6119 1312 if(uploadToBlob_result == IOTHUB_CLIENT_ERROR)
AzureIoTClient 46:6a69294b6119 1313 {
AzureIoTClient 46:6a69294b6119 1314 LogError("unable to IoTHubClient_LL_UploadToBlob_SetOption");
AzureIoTClient 46:6a69294b6119 1315 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 46:6a69294b6119 1316 }
AzureIoTClient 46:6a69294b6119 1317 #else
AzureIoTClient 46:6a69294b6119 1318 uploadToBlob_result = IOTHUB_CLIENT_INVALID_ARG; /*harmless value (IOTHUB_CLIENT_INVALID_ARG)in the case when uploadtoblob is not compiled in, otherwise whatever IoTHubClient_LL_UploadToBlob_SetOption returned*/
AzureIoTClient 46:6a69294b6119 1319 #endif /*DONT_USE_UPLOADTOBLOB*/
AzureIoTClient 16:deba40344375 1320
AzureIoTClient 46:6a69294b6119 1321
AzureIoTClient 46:6a69294b6119 1322 result =
AzureIoTClient 46:6a69294b6119 1323 /*based on uploadToBlob_result value this is what happens:*/
AzureIoTClient 46:6a69294b6119 1324 /*IOTHUB_CLIENT_INVALID_ARG always returns what IoTHubTransport_SetOption returns*/
AzureIoTClient 46:6a69294b6119 1325 /*IOTHUB_CLIENT_ERROR always returns IOTHUB_CLIENT_ERROR */
AzureIoTClient 46:6a69294b6119 1326 /*IOTHUB_CLIENT_OK returns OK
AzureIoTClient 46:6a69294b6119 1327 IOTHUB_CLIENT_OK if IoTHubTransport_SetOption returns OK or INVALID_ARG
AzureIoTClient 46:6a69294b6119 1328 IOTHUB_CLIENT_ERROR if IoTHubTransport_SetOption returns ERROR*/
AzureIoTClient 46:6a69294b6119 1329
AzureIoTClient 46:6a69294b6119 1330 (uploadToBlob_result == IOTHUB_CLIENT_INVALID_ARG) ? handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value) :
AzureIoTClient 46:6a69294b6119 1331 (uploadToBlob_result == IOTHUB_CLIENT_ERROR) ? IOTHUB_CLIENT_ERROR :
AzureIoTClient 46:6a69294b6119 1332 (handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value) == IOTHUB_CLIENT_ERROR) ? IOTHUB_CLIENT_ERROR : IOTHUB_CLIENT_OK;
AzureIoTClient 46:6a69294b6119 1333
AzureIoTClient 47:aaa262b5f898 1334 if (result != IOTHUB_CLIENT_OK)
AzureIoTClient 47:aaa262b5f898 1335 {
AzureIoTClient 47:aaa262b5f898 1336 LogError("underlying transport failed, returned = %s", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result));
AzureIoTClient 42:448eecc3676e 1337 }
AzureIoTClient 47:aaa262b5f898 1338 }
AzureIoTClient 42:448eecc3676e 1339 }
AzureIoTClient 42:448eecc3676e 1340 return result;
AzureIoTClient 42:448eecc3676e 1341 }
AzureIoTClient 16:deba40344375 1342
AzureIoTClient 53:1e5a1ca1f274 1343 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetDeviceTwinCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1344 {
AzureIoTClient 53:1e5a1ca1f274 1345 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1346 /* Codes_SRS_IOTHUBCLIENT_LL_10_001: [ IoTHubClient_LL_SetDeviceTwinCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.] */
AzureIoTClient 53:1e5a1ca1f274 1347 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1348 {
AzureIoTClient 53:1e5a1ca1f274 1349 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1350 LogError("Invalid argument specified iothubClientHandle=%p", iotHubClientHandle);
AzureIoTClient 53:1e5a1ca1f274 1351 }
AzureIoTClient 53:1e5a1ca1f274 1352 else
AzureIoTClient 53:1e5a1ca1f274 1353 {
AzureIoTClient 53:1e5a1ca1f274 1354 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1355 if (deviceTwinCallback == NULL)
AzureIoTClient 53:1e5a1ca1f274 1356 {
AzureIoTClient 53:1e5a1ca1f274 1357 /* Codes_SRS_IOTHUBCLIENT_LL_10_006: [ If deviceTwinCallback is NULL, then IoTHubClient_LL_SetDeviceTwinCallback shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 53:1e5a1ca1f274 1358 handleData->IoTHubTransport_Unsubscribe_DeviceTwin(handleData->transportHandle);
AzureIoTClient 53:1e5a1ca1f274 1359 handleData->deviceTwinCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 1360 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1361 }
AzureIoTClient 53:1e5a1ca1f274 1362 else
AzureIoTClient 53:1e5a1ca1f274 1363 {
AzureIoTClient 53:1e5a1ca1f274 1364 /* Codes_SRS_IOTHUBCLIENT_LL_10_002: [ If deviceTwinCallback is not NULL, then IoTHubClient_LL_SetDeviceTwinCallback shall call the underlying layer's _Subscribe function.] */
AzureIoTClient 53:1e5a1ca1f274 1365 if (handleData->IoTHubTransport_Subscribe_DeviceTwin(handleData->transportHandle) == 0)
AzureIoTClient 53:1e5a1ca1f274 1366 {
AzureIoTClient 53:1e5a1ca1f274 1367 handleData->deviceTwinCallback = deviceTwinCallback;
AzureIoTClient 53:1e5a1ca1f274 1368 handleData->deviceTwinContextCallback = userContextCallback;
AzureIoTClient 53:1e5a1ca1f274 1369 /* Codes_SRS_IOTHUBCLIENT_LL_10_005: [ Otherwise IoTHubClient_LL_SetDeviceTwinCallback shall succeed and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 53:1e5a1ca1f274 1370 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1371 }
AzureIoTClient 53:1e5a1ca1f274 1372 else
AzureIoTClient 53:1e5a1ca1f274 1373 {
AzureIoTClient 53:1e5a1ca1f274 1374 /* Codes_SRS_IOTHUBCLIENT_LL_10_003: [ If the underlying layer's _Subscribe function fails, then IoTHubClient_LL_SetDeviceTwinCallback shall fail and return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 53:1e5a1ca1f274 1375 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1376 }
AzureIoTClient 53:1e5a1ca1f274 1377 }
AzureIoTClient 53:1e5a1ca1f274 1378 }
AzureIoTClient 53:1e5a1ca1f274 1379 return result;
AzureIoTClient 53:1e5a1ca1f274 1380 }
AzureIoTClient 53:1e5a1ca1f274 1381
AzureIoTClient 53:1e5a1ca1f274 1382 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SendReportedState(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* reportedState, size_t size, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reportedStateCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1383 {
AzureIoTClient 53:1e5a1ca1f274 1384 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1385 /* Codes_SRS_IOTHUBCLIENT_LL_10_012: [ IoTHubClient_LL_SendReportedState shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL. ] */
AzureIoTClient 53:1e5a1ca1f274 1386 /* Codes_SRS_IOTHUBCLIENT_LL_10_013: [ IoTHubClient_LL_SendReportedState shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter reportedState is NULL] */
AzureIoTClient 53:1e5a1ca1f274 1387 /* Codes_SRS_IOTHUBCLIENT_LL_07_005: [ IoTHubClient_LL_SendReportedState shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter size is equal to 0. ] */
AzureIoTClient 53:1e5a1ca1f274 1388 if (iotHubClientHandle == NULL || (reportedState == NULL || size == 0) )
AzureIoTClient 53:1e5a1ca1f274 1389 {
AzureIoTClient 53:1e5a1ca1f274 1390 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1391 LogError("Invalid argument specified iothubClientHandle=%p, reportedState=%p, size=%zu", iotHubClientHandle, reportedState, size);
AzureIoTClient 53:1e5a1ca1f274 1392 }
AzureIoTClient 53:1e5a1ca1f274 1393 else
AzureIoTClient 53:1e5a1ca1f274 1394 {
AzureIoTClient 53:1e5a1ca1f274 1395 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1396 /* Codes_SRS_IOTHUBCLIENT_LL_10_014: [IoTHubClient_LL_SendReportedState shall construct and queue the reported a Device_Twin structure for transmition by the underlying transport.] */
AzureIoTClient 53:1e5a1ca1f274 1397 IOTHUB_DEVICE_TWIN* client_data = dev_twin_data_create(handleData, get_next_item_id(handleData), reportedState, size, reportedStateCallback, userContextCallback);
AzureIoTClient 53:1e5a1ca1f274 1398 if (client_data == NULL)
AzureIoTClient 53:1e5a1ca1f274 1399 {
AzureIoTClient 53:1e5a1ca1f274 1400 /* Codes_SRS_IOTHUBCLIENT_LL_10_015: [If any error is encountered IoTHubClient_LL_SendReportedState shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 53:1e5a1ca1f274 1401 LogError("Failure constructing device twin data");
AzureIoTClient 53:1e5a1ca1f274 1402 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1403 }
AzureIoTClient 53:1e5a1ca1f274 1404 else
AzureIoTClient 53:1e5a1ca1f274 1405 {
AzureIoTClient 53:1e5a1ca1f274 1406 if (handleData->IoTHubTransport_Subscribe_DeviceTwin(handleData->transportHandle) != 0)
AzureIoTClient 53:1e5a1ca1f274 1407 {
AzureIoTClient 53:1e5a1ca1f274 1408 LogError("Failure adding device twin data to queue");
AzureIoTClient 53:1e5a1ca1f274 1409 device_twin_data_destroy(client_data);
AzureIoTClient 53:1e5a1ca1f274 1410 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1411 }
AzureIoTClient 53:1e5a1ca1f274 1412 else
AzureIoTClient 53:1e5a1ca1f274 1413 {
AzureIoTClient 53:1e5a1ca1f274 1414 /* Codes_SRS_IOTHUBCLIENT_LL_07_001: [ IoTHubClient_LL_SendReportedState shall queue the constructed reportedState data to be consumed by the targeted transport. ] */
AzureIoTClient 53:1e5a1ca1f274 1415 DList_InsertTailList(&(iotHubClientHandle->iot_msg_queue), &(client_data->entry));
AzureIoTClient 53:1e5a1ca1f274 1416
AzureIoTClient 53:1e5a1ca1f274 1417 /* Codes_SRS_IOTHUBCLIENT_LL_10_016: [ Otherwise IoTHubClient_LL_SendReportedState shall succeed and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 53:1e5a1ca1f274 1418 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1419 }
AzureIoTClient 53:1e5a1ca1f274 1420 }
AzureIoTClient 53:1e5a1ca1f274 1421 }
AzureIoTClient 53:1e5a1ca1f274 1422 return result;
AzureIoTClient 53:1e5a1ca1f274 1423 }
AzureIoTClient 53:1e5a1ca1f274 1424
AzureIoTClient 53:1e5a1ca1f274 1425 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetDeviceMethodCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC deviceMethodCallback, void* userContextCallback)
AzureIoTClient 53:1e5a1ca1f274 1426 {
AzureIoTClient 53:1e5a1ca1f274 1427 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 53:1e5a1ca1f274 1428
AzureIoTClient 53:1e5a1ca1f274 1429 /*Codes_SRS_IOTHUBCLIENT_LL_12_017: [ IoTHubClient_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL. ] */
AzureIoTClient 53:1e5a1ca1f274 1430 if (iotHubClientHandle == NULL)
AzureIoTClient 53:1e5a1ca1f274 1431 {
AzureIoTClient 53:1e5a1ca1f274 1432 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 53:1e5a1ca1f274 1433 LOG_ERROR_RESULT;
AzureIoTClient 53:1e5a1ca1f274 1434 }
AzureIoTClient 53:1e5a1ca1f274 1435 else
AzureIoTClient 53:1e5a1ca1f274 1436 {
AzureIoTClient 53:1e5a1ca1f274 1437 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 53:1e5a1ca1f274 1438 if (deviceMethodCallback == NULL)
AzureIoTClient 53:1e5a1ca1f274 1439 {
AzureIoTClient 53:1e5a1ca1f274 1440 /*Codes_SRS_IOTHUBCLIENT_LL_12_018: [If deviceMethodCallback is NULL, then IoTHubClient_LL_SetDeviceMethodCallback shall call the underlying layer's IoTHubTransport_Unsubscribe_DeviceMethod function and return IOTHUB_CLIENT_OK. ] */
AzureIoTClient 53:1e5a1ca1f274 1441 /*Codes_SRS_IOTHUBCLIENT_LL_12_022: [ Otherwise IoTHubClient_LL_SetDeviceMethodCallback shall succeed and return IOTHUB_CLIENT_OK. ]*/
AzureIoTClient 53:1e5a1ca1f274 1442 handleData->IoTHubTransport_Unsubscribe_DeviceMethod(handleData->transportHandle);
AzureIoTClient 53:1e5a1ca1f274 1443 handleData->deviceMethodCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 1444 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1445 }
AzureIoTClient 53:1e5a1ca1f274 1446 else
AzureIoTClient 53:1e5a1ca1f274 1447 {
AzureIoTClient 53:1e5a1ca1f274 1448 /*Codes_SRS_IOTHUBCLIENT_LL_12_019: [ If deviceMethodCallback is not NULL, then IoTHubClient_LL_SetDeviceMethodCallback shall call the underlying layer's IoTHubTransport_Subscribe_DeviceMethod function. ]*/
AzureIoTClient 53:1e5a1ca1f274 1449 if (handleData->IoTHubTransport_Subscribe_DeviceMethod(handleData->deviceHandle) == 0)
AzureIoTClient 53:1e5a1ca1f274 1450 {
AzureIoTClient 53:1e5a1ca1f274 1451 /*Codes_SRS_IOTHUBCLIENT_LL_12_022: [ Otherwise IoTHubClient_LL_SetDeviceMethodCallback shall succeed and return IOTHUB_CLIENT_OK. ]*/
AzureIoTClient 53:1e5a1ca1f274 1452 handleData->deviceMethodCallback = deviceMethodCallback;
AzureIoTClient 55:59b527ab3452 1453 handleData->deviceInboundMethodCallback = NULL;
AzureIoTClient 53:1e5a1ca1f274 1454 handleData->deviceMethodUserContextCallback = userContextCallback;
AzureIoTClient 53:1e5a1ca1f274 1455 result = IOTHUB_CLIENT_OK;
AzureIoTClient 53:1e5a1ca1f274 1456 }
AzureIoTClient 53:1e5a1ca1f274 1457 else
AzureIoTClient 53:1e5a1ca1f274 1458 {
AzureIoTClient 53:1e5a1ca1f274 1459 /*Codes_SRS_IOTHUBCLIENT_LL_12_020: [ If the underlying layer's IoTHubTransport_Subscribe_DeviceMethod function fails, then IoTHubClient_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 53:1e5a1ca1f274 1460 /*Codes_SRS_IOTHUBCLIENT_LL_12_021: [ If adding the information fails for any reason, IoTHubClient_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 53:1e5a1ca1f274 1461 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 53:1e5a1ca1f274 1462 }
AzureIoTClient 53:1e5a1ca1f274 1463 }
AzureIoTClient 53:1e5a1ca1f274 1464 }
AzureIoTClient 53:1e5a1ca1f274 1465 return result;
AzureIoTClient 53:1e5a1ca1f274 1466 }
AzureIoTClient 53:1e5a1ca1f274 1467
AzureIoTClient 55:59b527ab3452 1468 IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetDeviceMethodCallback_Ex(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK inboundDeviceMethodCallback, void* userContextCallback)
AzureIoTClient 55:59b527ab3452 1469 {
AzureIoTClient 55:59b527ab3452 1470 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1471 /* Codes_SRS_IOTHUBCLIENT_LL_07_021: [ If handle is NULL then IoTHubClient_LL_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 55:59b527ab3452 1472 if (iotHubClientHandle == NULL)
AzureIoTClient 55:59b527ab3452 1473 {
AzureIoTClient 55:59b527ab3452 1474 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1475 LOG_ERROR_RESULT;
AzureIoTClient 55:59b527ab3452 1476 }
AzureIoTClient 55:59b527ab3452 1477 else
AzureIoTClient 55:59b527ab3452 1478 {
AzureIoTClient 55:59b527ab3452 1479 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1480 if (inboundDeviceMethodCallback == NULL)
AzureIoTClient 55:59b527ab3452 1481 {
AzureIoTClient 55:59b527ab3452 1482 /* Codes_SRS_IOTHUBCLIENT_LL_07_022: [ If inboundDeviceMethodCallback is NULL then IoTHubClient_LL_SetDeviceMethodCallback_Ex shall call the underlying layer's IoTHubTransport_Unsubscribe_DeviceMethod function and return IOTHUB_CLIENT_OK.] */
AzureIoTClient 55:59b527ab3452 1483 handleData->IoTHubTransport_Unsubscribe_DeviceMethod(handleData->transportHandle);
AzureIoTClient 55:59b527ab3452 1484 handleData->deviceInboundMethodCallback = NULL;
AzureIoTClient 55:59b527ab3452 1485 result = IOTHUB_CLIENT_OK;
AzureIoTClient 55:59b527ab3452 1486 }
AzureIoTClient 55:59b527ab3452 1487 else
AzureIoTClient 55:59b527ab3452 1488 {
AzureIoTClient 55:59b527ab3452 1489 /* Codes_SRS_IOTHUBCLIENT_LL_07_023: [ If inboundDeviceMethodCallback is non-NULL then IoTHubClient_LL_SetDeviceMethodCallback_Ex shall call the underlying layer's IoTHubTransport_Subscribe_DeviceMethod function.]*/
AzureIoTClient 55:59b527ab3452 1490 if (handleData->IoTHubTransport_Subscribe_DeviceMethod(handleData->deviceHandle) == 0)
AzureIoTClient 55:59b527ab3452 1491 {
AzureIoTClient 55:59b527ab3452 1492 handleData->deviceInboundMethodCallback = inboundDeviceMethodCallback;
AzureIoTClient 55:59b527ab3452 1493 handleData->deviceMethodCallback = NULL;
AzureIoTClient 55:59b527ab3452 1494 handleData->deviceMethodUserContextCallback = userContextCallback;
AzureIoTClient 55:59b527ab3452 1495 result = IOTHUB_CLIENT_OK;
AzureIoTClient 55:59b527ab3452 1496 }
AzureIoTClient 55:59b527ab3452 1497 else
AzureIoTClient 55:59b527ab3452 1498 {
AzureIoTClient 55:59b527ab3452 1499 /* Codes_SRS_IOTHUBCLIENT_LL_07_025: [ If any error is encountered then IoTHubClient_LL_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_ERROR.] */
AzureIoTClient 55:59b527ab3452 1500 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1501 LOG_ERROR_RESULT;
AzureIoTClient 55:59b527ab3452 1502 }
AzureIoTClient 55:59b527ab3452 1503 }
AzureIoTClient 55:59b527ab3452 1504 }
AzureIoTClient 55:59b527ab3452 1505 return result;
AzureIoTClient 55:59b527ab3452 1506 }
AzureIoTClient 55:59b527ab3452 1507
AzureIoTClient 55:59b527ab3452 1508 IOTHUB_CLIENT_RESULT IoTHubClient_LL_DeviceMethodResponse(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, METHOD_HANDLE methodId, const unsigned char* response, size_t response_size, int status_response)
AzureIoTClient 55:59b527ab3452 1509 {
AzureIoTClient 55:59b527ab3452 1510 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 55:59b527ab3452 1511 /* Codes_SRS_IOTHUBCLIENT_LL_07_026: [ If handle or methodId is NULL then IoTHubClient_LL_DeviceMethodResponse shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 55:59b527ab3452 1512 if (iotHubClientHandle == NULL || methodId == NULL)
AzureIoTClient 55:59b527ab3452 1513 {
AzureIoTClient 55:59b527ab3452 1514 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 55:59b527ab3452 1515 LOG_ERROR_RESULT;
AzureIoTClient 55:59b527ab3452 1516 }
AzureIoTClient 55:59b527ab3452 1517 else
AzureIoTClient 55:59b527ab3452 1518 {
AzureIoTClient 55:59b527ab3452 1519 IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle;
AzureIoTClient 55:59b527ab3452 1520 /* Codes_SRS_IOTHUBCLIENT_LL_07_027: [ IoTHubClient_LL_DeviceMethodResponse shall call the IoTHubTransport_DeviceMethod_Response transport function.] */
AzureIoTClient 55:59b527ab3452 1521 if (handleData->IoTHubTransport_DeviceMethod_Response(handleData->deviceHandle, methodId, response, response_size, status_response) != 0)
AzureIoTClient 55:59b527ab3452 1522 {
AzureIoTClient 55:59b527ab3452 1523 LogError("IoTHubTransport_DeviceMethod_Response failed");
AzureIoTClient 55:59b527ab3452 1524 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 55:59b527ab3452 1525 }
AzureIoTClient 55:59b527ab3452 1526 else
AzureIoTClient 55:59b527ab3452 1527 {
AzureIoTClient 55:59b527ab3452 1528 result = IOTHUB_CLIENT_OK;
AzureIoTClient 55:59b527ab3452 1529 }
AzureIoTClient 55:59b527ab3452 1530 }
AzureIoTClient 55:59b527ab3452 1531 return result;
AzureIoTClient 55:59b527ab3452 1532 }
AzureIoTClient 55:59b527ab3452 1533
AzureIoTClient 44:33dd78697616 1534 #ifndef DONT_USE_UPLOADTOBLOB
AzureIoTClient 42:448eecc3676e 1535 IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadToBlob(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* destinationFileName, const unsigned char* source, size_t size)
AzureIoTClient 42:448eecc3676e 1536 {
AzureIoTClient 42:448eecc3676e 1537 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 42:448eecc3676e 1538 /*Codes_SRS_IOTHUBCLIENT_LL_02_061: [ If iotHubClientHandle is NULL then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1539 /*Codes_SRS_IOTHUBCLIENT_LL_02_062: [ If destinationFileName is NULL then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 1540 if (
AzureIoTClient 42:448eecc3676e 1541 (iotHubClientHandle == NULL) ||
AzureIoTClient 42:448eecc3676e 1542 (destinationFileName == NULL) ||
AzureIoTClient 42:448eecc3676e 1543 ((source == NULL) && (size >0))
AzureIoTClient 42:448eecc3676e 1544 )
AzureIoTClient 42:448eecc3676e 1545 {
AzureIoTClient 42:448eecc3676e 1546 LogError("invalid parameters IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle=%p, const char* destinationFileName=%s, const unsigned char* source=%p, size_t size=%zu", iotHubClientHandle, destinationFileName, source, size);
AzureIoTClient 42:448eecc3676e 1547 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 1548 }
AzureIoTClient 42:448eecc3676e 1549 else
AzureIoTClient 42:448eecc3676e 1550 {
AzureIoTClient 42:448eecc3676e 1551 result = IoTHubClient_LL_UploadToBlob_Impl(iotHubClientHandle->uploadToBlobHandle, destinationFileName, source, size);
AzureIoTClient 42:448eecc3676e 1552 }
AzureIoTClient 42:448eecc3676e 1553 return result;
AzureIoTClient 16:deba40344375 1554 }
AzureIoTClient 43:038d8511e817 1555 #endif