Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

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

Committer:
AzureIoTClient
Date:
Fri Mar 24 16:35:32 2017 -0700
Revision:
62:5a4cdacf5090
Parent:
61:8b85a4e797cf
Child:
63:1bf1c2d60aab
1.1.10

Who changed what in which revision?

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