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:
Thu Apr 06 14:11:13 2017 -0700
Revision:
63:1bf1c2d60aab
Parent:
62:5a4cdacf5090
Child:
66:a419827cb051
1.1.11

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