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:
Sun Jan 08 11:11:58 2017 -0800
Revision:
55:59b527ab3452
Parent:
54:6dcad9019a64
Child:
57:4524910c6445
1.1.3

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