Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

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

Committer:
AzureIoTClient
Date:
Tue Jan 24 15:23:38 2017 -0800
Revision:
57:4524910c6445
Parent:
55:59b527ab3452
Child:
58:15b0d29b2667
1.1.5

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