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
iothub_client_core_ll.c@89:a2ed767a532e, 2018-06-26 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Tue Jun 26 19:14:28 2018 -0700
- Revision:
- 89:a2ed767a532e
- Parent:
- 88:248736be106e
- Child:
- 91:bbf806070c5f
1.2.6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 88:248736be106e | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 88:248736be106e | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 88:248736be106e | 3 | |
AzureIoTClient | 88:248736be106e | 4 | #include <stdlib.h> |
AzureIoTClient | 88:248736be106e | 5 | #include <stdint.h> |
AzureIoTClient | 88:248736be106e | 6 | #include <string.h> |
AzureIoTClient | 88:248736be106e | 7 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 88:248736be106e | 8 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 88:248736be106e | 9 | #include "azure_c_shared_utility/string_tokenizer.h" |
AzureIoTClient | 88:248736be106e | 10 | #include "azure_c_shared_utility/doublylinkedlist.h" |
AzureIoTClient | 88:248736be106e | 11 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 88:248736be106e | 12 | #include "azure_c_shared_utility/tickcounter.h" |
AzureIoTClient | 88:248736be106e | 13 | #include "azure_c_shared_utility/constbuffer.h" |
AzureIoTClient | 88:248736be106e | 14 | #include "azure_c_shared_utility/platform.h" |
AzureIoTClient | 89:a2ed767a532e | 15 | #include "azure_c_shared_utility/singlylinkedlist.h" |
AzureIoTClient | 89:a2ed767a532e | 16 | #include "azure_c_shared_utility/shared_util_options.h" |
AzureIoTClient | 89:a2ed767a532e | 17 | #include "azure_c_shared_utility/agenttime.h" |
AzureIoTClient | 88:248736be106e | 18 | |
AzureIoTClient | 88:248736be106e | 19 | #include "iothub_client_core_ll.h" |
AzureIoTClient | 88:248736be106e | 20 | #include "internal/iothub_client_authorization.h" |
AzureIoTClient | 88:248736be106e | 21 | #include "iothub_transport_ll.h" |
AzureIoTClient | 88:248736be106e | 22 | #include "internal/iothub_client_private.h" |
AzureIoTClient | 88:248736be106e | 23 | #include "iothub_client_options.h" |
AzureIoTClient | 88:248736be106e | 24 | #include "iothub_client_version.h" |
AzureIoTClient | 88:248736be106e | 25 | #include "internal/iothub_client_diagnostic.h" |
AzureIoTClient | 89:a2ed767a532e | 26 | #include <stdint.h> |
AzureIoTClient | 88:248736be106e | 27 | #include "internal/iothubtransport.h" |
AzureIoTClient | 88:248736be106e | 28 | |
AzureIoTClient | 88:248736be106e | 29 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 88:248736be106e | 30 | #include "internal/iothub_client_ll_uploadtoblob.h" |
AzureIoTClient | 88:248736be106e | 31 | #endif |
AzureIoTClient | 88:248736be106e | 32 | |
AzureIoTClient | 88:248736be106e | 33 | #define LOG_ERROR_RESULT LogError("result = %s", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result)); |
AzureIoTClient | 88:248736be106e | 34 | #define INDEFINITE_TIME ((time_t)(-1)) |
AzureIoTClient | 88:248736be106e | 35 | |
AzureIoTClient | 88:248736be106e | 36 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_FILE_UPLOAD_RESULT, IOTHUB_CLIENT_FILE_UPLOAD_RESULT_VALUES); |
AzureIoTClient | 88:248736be106e | 37 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_RESULT_VALUES); |
AzureIoTClient | 88:248736be106e | 38 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_RETRY_POLICY, IOTHUB_CLIENT_RETRY_POLICY_VALUES); |
AzureIoTClient | 88:248736be106e | 39 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_STATUS, IOTHUB_CLIENT_STATUS_VALUES); |
AzureIoTClient | 88:248736be106e | 40 | DEFINE_ENUM_STRINGS(IOTHUB_IDENTITY_TYPE, IOTHUB_IDENTITY_TYPE_VALUE); |
AzureIoTClient | 88:248736be106e | 41 | DEFINE_ENUM_STRINGS(IOTHUB_PROCESS_ITEM_RESULT, IOTHUB_PROCESS_ITEM_RESULT_VALUE); |
AzureIoTClient | 88:248736be106e | 42 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_IOTHUB_METHOD_STATUS, IOTHUB_CLIENT_IOTHUB_METHOD_STATUS_VALUES); |
AzureIoTClient | 88:248736be106e | 43 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES); |
AzureIoTClient | 88:248736be106e | 44 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONNECTION_STATUS, IOTHUB_CLIENT_CONNECTION_STATUS_VALUES); |
AzureIoTClient | 88:248736be106e | 45 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONNECTION_STATUS_REASON, IOTHUB_CLIENT_CONNECTION_STATUS_REASON_VALUES); |
AzureIoTClient | 88:248736be106e | 46 | DEFINE_ENUM_STRINGS(TRANSPORT_TYPE, TRANSPORT_TYPE_VALUES); |
AzureIoTClient | 88:248736be106e | 47 | DEFINE_ENUM_STRINGS(DEVICE_TWIN_UPDATE_STATE, DEVICE_TWIN_UPDATE_STATE_VALUES); |
AzureIoTClient | 88:248736be106e | 48 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 88:248736be106e | 49 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_RESULT, IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_RESULT_VALUES); |
AzureIoTClient | 88:248736be106e | 50 | #endif // DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 88:248736be106e | 51 | |
AzureIoTClient | 88:248736be106e | 52 | #define CALLBACK_TYPE_VALUES \ |
AzureIoTClient | 88:248736be106e | 53 | CALLBACK_TYPE_NONE, \ |
AzureIoTClient | 88:248736be106e | 54 | CALLBACK_TYPE_SYNC, \ |
AzureIoTClient | 88:248736be106e | 55 | CALLBACK_TYPE_ASYNC |
AzureIoTClient | 88:248736be106e | 56 | |
AzureIoTClient | 88:248736be106e | 57 | DEFINE_ENUM(CALLBACK_TYPE, CALLBACK_TYPE_VALUES) |
AzureIoTClient | 88:248736be106e | 58 | DEFINE_ENUM_STRINGS(CALLBACK_TYPE, CALLBACK_TYPE_VALUES) |
AzureIoTClient | 88:248736be106e | 59 | |
AzureIoTClient | 88:248736be106e | 60 | typedef struct IOTHUB_METHOD_CALLBACK_DATA_TAG |
AzureIoTClient | 88:248736be106e | 61 | { |
AzureIoTClient | 88:248736be106e | 62 | CALLBACK_TYPE type; |
AzureIoTClient | 88:248736be106e | 63 | IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC callbackSync; |
AzureIoTClient | 88:248736be106e | 64 | IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK callbackAsync; |
AzureIoTClient | 88:248736be106e | 65 | void* userContextCallback; |
AzureIoTClient | 88:248736be106e | 66 | }IOTHUB_METHOD_CALLBACK_DATA; |
AzureIoTClient | 88:248736be106e | 67 | |
AzureIoTClient | 89:a2ed767a532e | 68 | typedef struct IOTHUB_EVENT_CALLBACK_TAG |
AzureIoTClient | 89:a2ed767a532e | 69 | { |
AzureIoTClient | 89:a2ed767a532e | 70 | STRING_HANDLE inputName; |
AzureIoTClient | 89:a2ed767a532e | 71 | IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC callbackAsync; |
AzureIoTClient | 89:a2ed767a532e | 72 | IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC_EX callbackAsyncEx; |
AzureIoTClient | 89:a2ed767a532e | 73 | void* userContextCallback; |
AzureIoTClient | 89:a2ed767a532e | 74 | void* userContextCallbackEx; |
AzureIoTClient | 89:a2ed767a532e | 75 | }IOTHUB_EVENT_CALLBACK; |
AzureIoTClient | 89:a2ed767a532e | 76 | |
AzureIoTClient | 88:248736be106e | 77 | typedef struct IOTHUB_MESSAGE_CALLBACK_DATA_TAG |
AzureIoTClient | 88:248736be106e | 78 | { |
AzureIoTClient | 88:248736be106e | 79 | CALLBACK_TYPE type; |
AzureIoTClient | 88:248736be106e | 80 | IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC callbackSync; |
AzureIoTClient | 88:248736be106e | 81 | IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC_EX callbackAsync; |
AzureIoTClient | 88:248736be106e | 82 | void* userContextCallback; |
AzureIoTClient | 88:248736be106e | 83 | }IOTHUB_MESSAGE_CALLBACK_DATA; |
AzureIoTClient | 88:248736be106e | 84 | |
AzureIoTClient | 88:248736be106e | 85 | typedef struct IOTHUB_CLIENT_CORE_LL_HANDLE_DATA_TAG |
AzureIoTClient | 88:248736be106e | 86 | { |
AzureIoTClient | 88:248736be106e | 87 | DLIST_ENTRY waitingToSend; |
AzureIoTClient | 88:248736be106e | 88 | DLIST_ENTRY iot_msg_queue; |
AzureIoTClient | 88:248736be106e | 89 | DLIST_ENTRY iot_ack_queue; |
AzureIoTClient | 88:248736be106e | 90 | TRANSPORT_LL_HANDLE transportHandle; |
AzureIoTClient | 88:248736be106e | 91 | bool isSharedTransport; |
AzureIoTClient | 88:248736be106e | 92 | IOTHUB_DEVICE_HANDLE deviceHandle; |
AzureIoTClient | 88:248736be106e | 93 | TRANSPORT_PROVIDER_FIELDS; |
AzureIoTClient | 88:248736be106e | 94 | IOTHUB_MESSAGE_CALLBACK_DATA messageCallback; |
AzureIoTClient | 88:248736be106e | 95 | IOTHUB_METHOD_CALLBACK_DATA methodCallback; |
AzureIoTClient | 88:248736be106e | 96 | IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK conStatusCallback; |
AzureIoTClient | 88:248736be106e | 97 | void* conStatusUserContextCallback; |
AzureIoTClient | 88:248736be106e | 98 | time_t lastMessageReceiveTime; |
AzureIoTClient | 88:248736be106e | 99 | TICK_COUNTER_HANDLE tickCounter; /*shared tickcounter used to track message timeouts in waitingToSend list*/ |
AzureIoTClient | 88:248736be106e | 100 | tickcounter_ms_t currentMessageTimeout; |
AzureIoTClient | 88:248736be106e | 101 | uint64_t current_device_twin_timeout; |
AzureIoTClient | 88:248736be106e | 102 | IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback; |
AzureIoTClient | 88:248736be106e | 103 | void* deviceTwinContextCallback; |
AzureIoTClient | 88:248736be106e | 104 | IOTHUB_CLIENT_RETRY_POLICY retryPolicy; |
AzureIoTClient | 88:248736be106e | 105 | size_t retryTimeoutLimitInSeconds; |
AzureIoTClient | 88:248736be106e | 106 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 88:248736be106e | 107 | IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE uploadToBlobHandle; |
AzureIoTClient | 88:248736be106e | 108 | #endif |
AzureIoTClient | 88:248736be106e | 109 | uint32_t data_msg_id; |
AzureIoTClient | 88:248736be106e | 110 | bool complete_twin_update_encountered; |
AzureIoTClient | 88:248736be106e | 111 | IOTHUB_AUTHORIZATION_HANDLE authorization_module; |
AzureIoTClient | 88:248736be106e | 112 | STRING_HANDLE product_info; |
AzureIoTClient | 88:248736be106e | 113 | IOTHUB_DIAGNOSTIC_SETTING_DATA diagnostic_setting; |
AzureIoTClient | 89:a2ed767a532e | 114 | SINGLYLINKEDLIST_HANDLE event_callbacks; // List of IOTHUB_EVENT_CALLBACK's |
AzureIoTClient | 88:248736be106e | 115 | }IOTHUB_CLIENT_CORE_LL_HANDLE_DATA; |
AzureIoTClient | 88:248736be106e | 116 | |
AzureIoTClient | 88:248736be106e | 117 | static const char HOSTNAME_TOKEN[] = "HostName"; |
AzureIoTClient | 88:248736be106e | 118 | static const char DEVICEID_TOKEN[] = "DeviceId"; |
AzureIoTClient | 88:248736be106e | 119 | static const char X509_TOKEN[] = "x509"; |
AzureIoTClient | 88:248736be106e | 120 | static const char X509_TOKEN_ONLY_ACCEPTABLE_VALUE[] = "true"; |
AzureIoTClient | 88:248736be106e | 121 | static const char DEVICEKEY_TOKEN[] = "SharedAccessKey"; |
AzureIoTClient | 88:248736be106e | 122 | static const char DEVICESAS_TOKEN[] = "SharedAccessSignature"; |
AzureIoTClient | 89:a2ed767a532e | 123 | static const char PROTOCOL_GATEWAY_HOST_TOKEN[] = "GatewayHostName"; |
AzureIoTClient | 89:a2ed767a532e | 124 | static const char MODULE_ID_TOKEN[] = "ModuleId"; |
AzureIoTClient | 88:248736be106e | 125 | static const char PROVISIONING_TOKEN[] = "UseProvisioning"; |
AzureIoTClient | 88:248736be106e | 126 | static const char PROVISIONING_ACCEPTABLE_VALUE[] = "true"; |
AzureIoTClient | 88:248736be106e | 127 | |
AzureIoTClient | 88:248736be106e | 128 | static void setTransportProtocol(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData, TRANSPORT_PROVIDER* protocol) |
AzureIoTClient | 88:248736be106e | 129 | { |
AzureIoTClient | 88:248736be106e | 130 | handleData->IoTHubTransport_SendMessageDisposition = protocol->IoTHubTransport_SendMessageDisposition; |
AzureIoTClient | 88:248736be106e | 131 | handleData->IoTHubTransport_GetHostname = protocol->IoTHubTransport_GetHostname; |
AzureIoTClient | 88:248736be106e | 132 | handleData->IoTHubTransport_SetOption = protocol->IoTHubTransport_SetOption; |
AzureIoTClient | 88:248736be106e | 133 | handleData->IoTHubTransport_Create = protocol->IoTHubTransport_Create; |
AzureIoTClient | 88:248736be106e | 134 | handleData->IoTHubTransport_Destroy = protocol->IoTHubTransport_Destroy; |
AzureIoTClient | 88:248736be106e | 135 | handleData->IoTHubTransport_Register = protocol->IoTHubTransport_Register; |
AzureIoTClient | 88:248736be106e | 136 | handleData->IoTHubTransport_Unregister = protocol->IoTHubTransport_Unregister; |
AzureIoTClient | 88:248736be106e | 137 | handleData->IoTHubTransport_Subscribe = protocol->IoTHubTransport_Subscribe; |
AzureIoTClient | 88:248736be106e | 138 | handleData->IoTHubTransport_Unsubscribe = protocol->IoTHubTransport_Unsubscribe; |
AzureIoTClient | 88:248736be106e | 139 | handleData->IoTHubTransport_DoWork = protocol->IoTHubTransport_DoWork; |
AzureIoTClient | 88:248736be106e | 140 | handleData->IoTHubTransport_SetRetryPolicy = protocol->IoTHubTransport_SetRetryPolicy; |
AzureIoTClient | 88:248736be106e | 141 | handleData->IoTHubTransport_GetSendStatus = protocol->IoTHubTransport_GetSendStatus; |
AzureIoTClient | 88:248736be106e | 142 | handleData->IoTHubTransport_ProcessItem = protocol->IoTHubTransport_ProcessItem; |
AzureIoTClient | 88:248736be106e | 143 | handleData->IoTHubTransport_Subscribe_DeviceTwin = protocol->IoTHubTransport_Subscribe_DeviceTwin; |
AzureIoTClient | 88:248736be106e | 144 | handleData->IoTHubTransport_Unsubscribe_DeviceTwin = protocol->IoTHubTransport_Unsubscribe_DeviceTwin; |
AzureIoTClient | 88:248736be106e | 145 | handleData->IoTHubTransport_Subscribe_DeviceMethod = protocol->IoTHubTransport_Subscribe_DeviceMethod; |
AzureIoTClient | 88:248736be106e | 146 | handleData->IoTHubTransport_Unsubscribe_DeviceMethod = protocol->IoTHubTransport_Unsubscribe_DeviceMethod; |
AzureIoTClient | 88:248736be106e | 147 | handleData->IoTHubTransport_DeviceMethod_Response = protocol->IoTHubTransport_DeviceMethod_Response; |
AzureIoTClient | 89:a2ed767a532e | 148 | handleData->IoTHubTransport_Subscribe_InputQueue = protocol->IoTHubTransport_Subscribe_InputQueue; |
AzureIoTClient | 89:a2ed767a532e | 149 | handleData->IoTHubTransport_Unsubscribe_InputQueue = protocol->IoTHubTransport_Unsubscribe_InputQueue; |
AzureIoTClient | 88:248736be106e | 150 | } |
AzureIoTClient | 88:248736be106e | 151 | |
AzureIoTClient | 88:248736be106e | 152 | static void device_twin_data_destroy(IOTHUB_DEVICE_TWIN* client_item) |
AzureIoTClient | 88:248736be106e | 153 | { |
AzureIoTClient | 88:248736be106e | 154 | CONSTBUFFER_Destroy(client_item->report_data_handle); |
AzureIoTClient | 88:248736be106e | 155 | free(client_item); |
AzureIoTClient | 88:248736be106e | 156 | } |
AzureIoTClient | 88:248736be106e | 157 | |
AzureIoTClient | 88:248736be106e | 158 | static int create_blob_upload_module(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handle_data, const IOTHUB_CLIENT_CONFIG* config) |
AzureIoTClient | 88:248736be106e | 159 | { |
AzureIoTClient | 88:248736be106e | 160 | int result; |
AzureIoTClient | 88:248736be106e | 161 | (void)handle_data; |
AzureIoTClient | 88:248736be106e | 162 | (void)config; |
AzureIoTClient | 88:248736be106e | 163 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 88:248736be106e | 164 | handle_data->uploadToBlobHandle = IoTHubClient_LL_UploadToBlob_Create(config); |
AzureIoTClient | 88:248736be106e | 165 | if (handle_data->uploadToBlobHandle == NULL) |
AzureIoTClient | 88:248736be106e | 166 | { |
AzureIoTClient | 88:248736be106e | 167 | LogError("unable to IoTHubClientCore_LL_UploadToBlob_Create"); |
AzureIoTClient | 88:248736be106e | 168 | result = __FAILURE__; |
AzureIoTClient | 88:248736be106e | 169 | } |
AzureIoTClient | 88:248736be106e | 170 | else |
AzureIoTClient | 88:248736be106e | 171 | { |
AzureIoTClient | 88:248736be106e | 172 | result = 0; |
AzureIoTClient | 88:248736be106e | 173 | } |
AzureIoTClient | 88:248736be106e | 174 | #else |
AzureIoTClient | 88:248736be106e | 175 | result = 0; |
AzureIoTClient | 88:248736be106e | 176 | #endif |
AzureIoTClient | 88:248736be106e | 177 | return result; |
AzureIoTClient | 88:248736be106e | 178 | } |
AzureIoTClient | 88:248736be106e | 179 | |
AzureIoTClient | 88:248736be106e | 180 | static void destroy_blob_upload_module(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handle_data) |
AzureIoTClient | 88:248736be106e | 181 | { |
AzureIoTClient | 88:248736be106e | 182 | (void)handle_data; |
AzureIoTClient | 88:248736be106e | 183 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 88:248736be106e | 184 | /*Codes_SRS_IOTHUBCLIENT_LL_02_046: [ If creating the TICK_COUNTER_HANDLE fails then IoTHubClientCore_LL_Create shall fail and return NULL. ]*/ |
AzureIoTClient | 88:248736be106e | 185 | IoTHubClient_LL_UploadToBlob_Destroy(handle_data->uploadToBlobHandle); |
AzureIoTClient | 88:248736be106e | 186 | #endif |
AzureIoTClient | 88:248736be106e | 187 | } |
AzureIoTClient | 88:248736be106e | 188 | |
AzureIoTClient | 88:248736be106e | 189 | /*Codes_SRS_IOTHUBCLIENT_LL_10_032: ["product_info" - takes a char string as an argument to specify the product information(e.g. `"ProductName/ProductVersion"`). ]*/ |
AzureIoTClient | 88:248736be106e | 190 | /*Codes_SRS_IOTHUBCLIENT_LL_10_034: ["product_info" - shall store the given string concatenated with the sdk information and the platform information in the form(ProductInfo DeviceSDKName / DeviceSDKVersion(OSName OSVersion; Architecture). ]*/ |
AzureIoTClient | 88:248736be106e | 191 | static STRING_HANDLE make_product_info(const char* product) |
AzureIoTClient | 88:248736be106e | 192 | { |
AzureIoTClient | 88:248736be106e | 193 | STRING_HANDLE result; |
AzureIoTClient | 88:248736be106e | 194 | STRING_HANDLE pfi = platform_get_platform_info(); |
AzureIoTClient | 88:248736be106e | 195 | if (pfi == NULL) |
AzureIoTClient | 88:248736be106e | 196 | { |
AzureIoTClient | 88:248736be106e | 197 | result = NULL; |
AzureIoTClient | 88:248736be106e | 198 | } |
AzureIoTClient | 88:248736be106e | 199 | else |
AzureIoTClient | 88:248736be106e | 200 | { |
AzureIoTClient | 88:248736be106e | 201 | if (product == NULL) |
AzureIoTClient | 88:248736be106e | 202 | { |
AzureIoTClient | 88:248736be106e | 203 | result = STRING_construct_sprintf("%s %s", CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION, STRING_c_str(pfi)); |
AzureIoTClient | 88:248736be106e | 204 | } |
AzureIoTClient | 88:248736be106e | 205 | else |
AzureIoTClient | 88:248736be106e | 206 | { |
AzureIoTClient | 88:248736be106e | 207 | result = STRING_construct_sprintf("%s %s %s", product, CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION, STRING_c_str(pfi)); |
AzureIoTClient | 88:248736be106e | 208 | } |
AzureIoTClient | 88:248736be106e | 209 | STRING_delete(pfi); |
AzureIoTClient | 88:248736be106e | 210 | } |
AzureIoTClient | 88:248736be106e | 211 | return result; |
AzureIoTClient | 88:248736be106e | 212 | } |
AzureIoTClient | 88:248736be106e | 213 | |
AzureIoTClient | 89:a2ed767a532e | 214 | static IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* initialize_iothub_client(const IOTHUB_CLIENT_CONFIG* client_config, const IOTHUB_CLIENT_DEVICE_CONFIG* device_config, bool use_dev_auth, const char* module_id) |
AzureIoTClient | 88:248736be106e | 215 | { |
AzureIoTClient | 88:248736be106e | 216 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* result; |
AzureIoTClient | 88:248736be106e | 217 | srand((unsigned int)time(NULL)); |
AzureIoTClient | 88:248736be106e | 218 | STRING_HANDLE product_info = make_product_info(NULL); |
AzureIoTClient | 88:248736be106e | 219 | if (product_info == NULL) |
AzureIoTClient | 88:248736be106e | 220 | { |
AzureIoTClient | 88:248736be106e | 221 | LogError("failed to initialize product info"); |
AzureIoTClient | 88:248736be106e | 222 | result = NULL; |
AzureIoTClient | 88:248736be106e | 223 | } |
AzureIoTClient | 88:248736be106e | 224 | else |
AzureIoTClient | 88:248736be106e | 225 | { |
AzureIoTClient | 88:248736be106e | 226 | result = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)malloc(sizeof(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA)); |
AzureIoTClient | 88:248736be106e | 227 | if (result == NULL) |
AzureIoTClient | 88:248736be106e | 228 | { |
AzureIoTClient | 88:248736be106e | 229 | LogError("failure allocating IOTHUB_CLIENT_CORE_LL_HANDLE_DATA"); |
AzureIoTClient | 88:248736be106e | 230 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 231 | } |
AzureIoTClient | 88:248736be106e | 232 | else |
AzureIoTClient | 88:248736be106e | 233 | { |
AzureIoTClient | 88:248736be106e | 234 | IOTHUB_CLIENT_CONFIG actual_config; |
AzureIoTClient | 88:248736be106e | 235 | const IOTHUB_CLIENT_CONFIG* config = NULL; |
AzureIoTClient | 88:248736be106e | 236 | char* IoTHubName = NULL; |
AzureIoTClient | 88:248736be106e | 237 | char* IoTHubSuffix = NULL; |
AzureIoTClient | 88:248736be106e | 238 | |
AzureIoTClient | 88:248736be106e | 239 | memset(result, 0, sizeof(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA)); |
AzureIoTClient | 88:248736be106e | 240 | if (use_dev_auth) |
AzureIoTClient | 88:248736be106e | 241 | { |
AzureIoTClient | 89:a2ed767a532e | 242 | if ((result->authorization_module = IoTHubClient_Auth_CreateFromDeviceAuth(client_config->deviceId, module_id)) == NULL) |
AzureIoTClient | 88:248736be106e | 243 | { |
AzureIoTClient | 88:248736be106e | 244 | LogError("Failed create authorization module"); |
AzureIoTClient | 88:248736be106e | 245 | free(result); |
AzureIoTClient | 88:248736be106e | 246 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 247 | result = NULL; |
AzureIoTClient | 88:248736be106e | 248 | } |
AzureIoTClient | 88:248736be106e | 249 | } |
AzureIoTClient | 88:248736be106e | 250 | else |
AzureIoTClient | 88:248736be106e | 251 | { |
AzureIoTClient | 88:248736be106e | 252 | const char* device_key; |
AzureIoTClient | 88:248736be106e | 253 | const char* device_id; |
AzureIoTClient | 88:248736be106e | 254 | const char* sas_token; |
AzureIoTClient | 88:248736be106e | 255 | if (device_config == NULL) |
AzureIoTClient | 88:248736be106e | 256 | { |
AzureIoTClient | 88:248736be106e | 257 | device_key = client_config->deviceKey; |
AzureIoTClient | 88:248736be106e | 258 | device_id = client_config->deviceId; |
AzureIoTClient | 88:248736be106e | 259 | sas_token = client_config->deviceSasToken; |
AzureIoTClient | 88:248736be106e | 260 | } |
AzureIoTClient | 88:248736be106e | 261 | else |
AzureIoTClient | 88:248736be106e | 262 | { |
AzureIoTClient | 88:248736be106e | 263 | device_key = device_config->deviceKey; |
AzureIoTClient | 88:248736be106e | 264 | device_id = device_config->deviceId; |
AzureIoTClient | 88:248736be106e | 265 | sas_token = device_config->deviceSasToken; |
AzureIoTClient | 88:248736be106e | 266 | } |
AzureIoTClient | 88:248736be106e | 267 | |
AzureIoTClient | 88:248736be106e | 268 | /* Codes_SRS_IOTHUBCLIENT_LL_07_029: [ IoTHubClientCore_LL_Create shall create the Auth module with the device_key, device_id, and/or deviceSasToken values ] */ |
AzureIoTClient | 89:a2ed767a532e | 269 | if ((result->authorization_module = IoTHubClient_Auth_Create(device_key, device_id, sas_token, module_id)) == NULL) |
AzureIoTClient | 88:248736be106e | 270 | { |
AzureIoTClient | 88:248736be106e | 271 | LogError("Failed create authorization module"); |
AzureIoTClient | 88:248736be106e | 272 | free(result); |
AzureIoTClient | 88:248736be106e | 273 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 274 | result = NULL; |
AzureIoTClient | 88:248736be106e | 275 | } |
AzureIoTClient | 88:248736be106e | 276 | } |
AzureIoTClient | 88:248736be106e | 277 | |
AzureIoTClient | 88:248736be106e | 278 | if (result != NULL) |
AzureIoTClient | 88:248736be106e | 279 | { |
AzureIoTClient | 88:248736be106e | 280 | if (client_config != NULL) |
AzureIoTClient | 88:248736be106e | 281 | { |
AzureIoTClient | 88:248736be106e | 282 | IOTHUBTRANSPORT_CONFIG lowerLayerConfig; |
AzureIoTClient | 88:248736be106e | 283 | memset(&lowerLayerConfig, 0, sizeof(IOTHUBTRANSPORT_CONFIG)); |
AzureIoTClient | 88:248736be106e | 284 | /*Codes_SRS_IOTHUBCLIENT_LL_02_006: [IoTHubClientCore_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 | 88:248736be106e | 285 | lowerLayerConfig.upperConfig = client_config; |
AzureIoTClient | 88:248736be106e | 286 | lowerLayerConfig.waitingToSend = &(result->waitingToSend); |
AzureIoTClient | 88:248736be106e | 287 | lowerLayerConfig.auth_module_handle = result->authorization_module; |
AzureIoTClient | 89:a2ed767a532e | 288 | lowerLayerConfig.moduleId = module_id; |
AzureIoTClient | 88:248736be106e | 289 | |
AzureIoTClient | 88:248736be106e | 290 | setTransportProtocol(result, (TRANSPORT_PROVIDER*)client_config->protocol()); |
AzureIoTClient | 88:248736be106e | 291 | if ((result->transportHandle = result->IoTHubTransport_Create(&lowerLayerConfig)) == NULL) |
AzureIoTClient | 88:248736be106e | 292 | { |
AzureIoTClient | 88:248736be106e | 293 | /*Codes_SRS_IOTHUBCLIENT_LL_02_007: [If the underlaying layer _Create function fails them IoTHubClientCore_LL_Create shall fail and return NULL.] */ |
AzureIoTClient | 88:248736be106e | 294 | LogError("underlying transport failed"); |
AzureIoTClient | 88:248736be106e | 295 | destroy_blob_upload_module(result); |
AzureIoTClient | 88:248736be106e | 296 | tickcounter_destroy(result->tickCounter); |
AzureIoTClient | 88:248736be106e | 297 | IoTHubClient_Auth_Destroy(result->authorization_module); |
AzureIoTClient | 88:248736be106e | 298 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 299 | free(result); |
AzureIoTClient | 88:248736be106e | 300 | result = NULL; |
AzureIoTClient | 88:248736be106e | 301 | } |
AzureIoTClient | 88:248736be106e | 302 | else |
AzureIoTClient | 88:248736be106e | 303 | { |
AzureIoTClient | 88:248736be106e | 304 | /*Codes_SRS_IOTHUBCLIENT_LL_02_008: [Otherwise, IoTHubClientCore_LL_Create shall succeed and return a non-NULL handle.] */ |
AzureIoTClient | 88:248736be106e | 305 | result->isSharedTransport = false; |
AzureIoTClient | 88:248736be106e | 306 | config = client_config; |
AzureIoTClient | 88:248736be106e | 307 | } |
AzureIoTClient | 88:248736be106e | 308 | } |
AzureIoTClient | 88:248736be106e | 309 | else |
AzureIoTClient | 88:248736be106e | 310 | { |
AzureIoTClient | 88:248736be106e | 311 | STRING_HANDLE transport_hostname = NULL; |
AzureIoTClient | 88:248736be106e | 312 | |
AzureIoTClient | 88:248736be106e | 313 | result->transportHandle = device_config->transportHandle; |
AzureIoTClient | 88:248736be106e | 314 | setTransportProtocol(result, (TRANSPORT_PROVIDER*)device_config->protocol()); |
AzureIoTClient | 88:248736be106e | 315 | |
AzureIoTClient | 88:248736be106e | 316 | transport_hostname = result->IoTHubTransport_GetHostname(result->transportHandle); |
AzureIoTClient | 88:248736be106e | 317 | if (transport_hostname == NULL) |
AzureIoTClient | 88:248736be106e | 318 | { |
AzureIoTClient | 88:248736be106e | 319 | /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClientCore_LL_CreateWithTransport shall fail and return NULL. ]*/ |
AzureIoTClient | 88:248736be106e | 320 | LogError("unable to determine the transport IoTHub name"); |
AzureIoTClient | 88:248736be106e | 321 | IoTHubClient_Auth_Destroy(result->authorization_module); |
AzureIoTClient | 88:248736be106e | 322 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 323 | free(result); |
AzureIoTClient | 88:248736be106e | 324 | result = NULL; |
AzureIoTClient | 88:248736be106e | 325 | } |
AzureIoTClient | 88:248736be106e | 326 | else |
AzureIoTClient | 88:248736be106e | 327 | { |
AzureIoTClient | 88:248736be106e | 328 | const char* hostname = STRING_c_str(transport_hostname); |
AzureIoTClient | 88:248736be106e | 329 | /*Codes_SRS_IOTHUBCLIENT_LL_02_096: [ IoTHubClientCore_LL_CreateWithTransport shall create the data structures needed to instantiate a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE. ]*/ |
AzureIoTClient | 88:248736be106e | 330 | /*the first '.' says where the iothubname finishes*/ |
AzureIoTClient | 88:248736be106e | 331 | const char* whereIsDot = strchr(hostname, '.'); |
AzureIoTClient | 88:248736be106e | 332 | if (whereIsDot == NULL) |
AzureIoTClient | 88:248736be106e | 333 | { |
AzureIoTClient | 88:248736be106e | 334 | /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClientCore_LL_CreateWithTransport shall fail and return NULL. ]*/ |
AzureIoTClient | 88:248736be106e | 335 | LogError("unable to determine the IoTHub name"); |
AzureIoTClient | 88:248736be106e | 336 | IoTHubClient_Auth_Destroy(result->authorization_module); |
AzureIoTClient | 88:248736be106e | 337 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 338 | free(result); |
AzureIoTClient | 88:248736be106e | 339 | result = NULL; |
AzureIoTClient | 88:248736be106e | 340 | } |
AzureIoTClient | 88:248736be106e | 341 | else |
AzureIoTClient | 88:248736be106e | 342 | { |
AzureIoTClient | 88:248736be106e | 343 | size_t suffix_len = strlen(whereIsDot); |
AzureIoTClient | 88:248736be106e | 344 | /*Codes_SRS_IOTHUBCLIENT_LL_02_096: [ IoTHubClientCore_LL_CreateWithTransport shall create the data structures needed to instantiate a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE. ]*/ |
AzureIoTClient | 88:248736be106e | 345 | IoTHubName = (char*)malloc(whereIsDot - hostname + 1); |
AzureIoTClient | 88:248736be106e | 346 | if (IoTHubName == NULL) |
AzureIoTClient | 88:248736be106e | 347 | { |
AzureIoTClient | 88:248736be106e | 348 | /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClientCore_LL_CreateWithTransport shall fail and return NULL. ]*/ |
AzureIoTClient | 88:248736be106e | 349 | LogError("unable to malloc"); |
AzureIoTClient | 88:248736be106e | 350 | IoTHubClient_Auth_Destroy(result->authorization_module); |
AzureIoTClient | 88:248736be106e | 351 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 352 | free(result); |
AzureIoTClient | 88:248736be106e | 353 | result = NULL; |
AzureIoTClient | 88:248736be106e | 354 | } |
AzureIoTClient | 88:248736be106e | 355 | else if ((IoTHubSuffix = (char*)malloc(suffix_len + 1)) == NULL) |
AzureIoTClient | 88:248736be106e | 356 | { |
AzureIoTClient | 88:248736be106e | 357 | /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClientCore_LL_CreateWithTransport shall fail and return NULL. ]*/ |
AzureIoTClient | 88:248736be106e | 358 | LogError("unable to malloc"); |
AzureIoTClient | 88:248736be106e | 359 | IoTHubClient_Auth_Destroy(result->authorization_module); |
AzureIoTClient | 88:248736be106e | 360 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 361 | free(result); |
AzureIoTClient | 88:248736be106e | 362 | result = NULL; |
AzureIoTClient | 88:248736be106e | 363 | } |
AzureIoTClient | 88:248736be106e | 364 | else |
AzureIoTClient | 88:248736be106e | 365 | { |
AzureIoTClient | 88:248736be106e | 366 | memset(IoTHubName, 0, whereIsDot - hostname + 1); |
AzureIoTClient | 88:248736be106e | 367 | (void)strncpy(IoTHubName, hostname, whereIsDot - hostname); |
AzureIoTClient | 88:248736be106e | 368 | (void)strcpy(IoTHubSuffix, whereIsDot+1); |
AzureIoTClient | 88:248736be106e | 369 | |
AzureIoTClient | 88:248736be106e | 370 | actual_config.deviceId = device_config->deviceId; |
AzureIoTClient | 88:248736be106e | 371 | actual_config.deviceKey = device_config->deviceKey; |
AzureIoTClient | 88:248736be106e | 372 | actual_config.deviceSasToken = device_config->deviceSasToken; |
AzureIoTClient | 88:248736be106e | 373 | actual_config.iotHubName = IoTHubName; |
AzureIoTClient | 88:248736be106e | 374 | actual_config.iotHubSuffix = IoTHubSuffix; |
AzureIoTClient | 88:248736be106e | 375 | actual_config.protocol = NULL; /*irrelevant to IoTHubClientCore_LL_UploadToBlob*/ |
AzureIoTClient | 88:248736be106e | 376 | actual_config.protocolGatewayHostName = NULL; /*irrelevant to IoTHubClientCore_LL_UploadToBlob*/ |
AzureIoTClient | 88:248736be106e | 377 | |
AzureIoTClient | 88:248736be106e | 378 | config = &actual_config; |
AzureIoTClient | 88:248736be106e | 379 | |
AzureIoTClient | 88:248736be106e | 380 | /*Codes_SRS_IOTHUBCLIENT_LL_02_008: [Otherwise, IoTHubClientCore_LL_Create shall succeed and return a non-NULL handle.] */ |
AzureIoTClient | 88:248736be106e | 381 | result->isSharedTransport = true; |
AzureIoTClient | 88:248736be106e | 382 | } |
AzureIoTClient | 88:248736be106e | 383 | } |
AzureIoTClient | 88:248736be106e | 384 | } |
AzureIoTClient | 88:248736be106e | 385 | STRING_delete(transport_hostname); |
AzureIoTClient | 88:248736be106e | 386 | } |
AzureIoTClient | 88:248736be106e | 387 | } |
AzureIoTClient | 88:248736be106e | 388 | if (result != NULL) |
AzureIoTClient | 88:248736be106e | 389 | { |
AzureIoTClient | 88:248736be106e | 390 | if (create_blob_upload_module(result, config) != 0) |
AzureIoTClient | 88:248736be106e | 391 | { |
AzureIoTClient | 88:248736be106e | 392 | LogError("unable to create blob upload"); |
AzureIoTClient | 88:248736be106e | 393 | // Codes_SRS_IOTHUBCLIENT_LL_09_010: [ If any failure occurs `IoTHubClientCore_LL_Create` shall destroy the `transportHandle` only if it has created it ] |
AzureIoTClient | 88:248736be106e | 394 | if (!result->isSharedTransport) |
AzureIoTClient | 88:248736be106e | 395 | { |
AzureIoTClient | 88:248736be106e | 396 | result->IoTHubTransport_Destroy(result->transportHandle); |
AzureIoTClient | 88:248736be106e | 397 | } |
AzureIoTClient | 88:248736be106e | 398 | IoTHubClient_Auth_Destroy(result->authorization_module); |
AzureIoTClient | 88:248736be106e | 399 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 400 | free(result); |
AzureIoTClient | 88:248736be106e | 401 | result = NULL; |
AzureIoTClient | 88:248736be106e | 402 | } |
AzureIoTClient | 88:248736be106e | 403 | else |
AzureIoTClient | 88:248736be106e | 404 | { |
AzureIoTClient | 88:248736be106e | 405 | if ((result->tickCounter = tickcounter_create()) == NULL) |
AzureIoTClient | 88:248736be106e | 406 | { |
AzureIoTClient | 88:248736be106e | 407 | LogError("unable to get a tickcounter"); |
AzureIoTClient | 88:248736be106e | 408 | // Codes_SRS_IOTHUBCLIENT_LL_09_010: [ If any failure occurs `IoTHubClientCore_LL_Create` shall destroy the `transportHandle` only if it has created it ] |
AzureIoTClient | 88:248736be106e | 409 | if (!result->isSharedTransport) |
AzureIoTClient | 88:248736be106e | 410 | { |
AzureIoTClient | 88:248736be106e | 411 | result->IoTHubTransport_Destroy(result->transportHandle); |
AzureIoTClient | 88:248736be106e | 412 | } |
AzureIoTClient | 88:248736be106e | 413 | destroy_blob_upload_module(result); |
AzureIoTClient | 88:248736be106e | 414 | IoTHubClient_Auth_Destroy(result->authorization_module); |
AzureIoTClient | 88:248736be106e | 415 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 416 | free(result); |
AzureIoTClient | 88:248736be106e | 417 | result = NULL; |
AzureIoTClient | 88:248736be106e | 418 | } |
AzureIoTClient | 88:248736be106e | 419 | else |
AzureIoTClient | 88:248736be106e | 420 | { |
AzureIoTClient | 88:248736be106e | 421 | /*Codes_SRS_IOTHUBCLIENT_LL_02_004: [Otherwise IoTHubClientCore_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 | 88:248736be106e | 422 | DList_InitializeListHead(&(result->waitingToSend)); |
AzureIoTClient | 88:248736be106e | 423 | DList_InitializeListHead(&(result->iot_msg_queue)); |
AzureIoTClient | 88:248736be106e | 424 | DList_InitializeListHead(&(result->iot_ack_queue)); |
AzureIoTClient | 88:248736be106e | 425 | result->messageCallback.type = CALLBACK_TYPE_NONE; |
AzureIoTClient | 88:248736be106e | 426 | result->lastMessageReceiveTime = INDEFINITE_TIME; |
AzureIoTClient | 88:248736be106e | 427 | result->data_msg_id = 1; |
AzureIoTClient | 88:248736be106e | 428 | result->product_info = product_info; |
AzureIoTClient | 88:248736be106e | 429 | |
AzureIoTClient | 88:248736be106e | 430 | IOTHUB_DEVICE_CONFIG deviceConfig; |
AzureIoTClient | 88:248736be106e | 431 | deviceConfig.deviceId = config->deviceId; |
AzureIoTClient | 88:248736be106e | 432 | deviceConfig.deviceKey = config->deviceKey; |
AzureIoTClient | 88:248736be106e | 433 | deviceConfig.deviceSasToken = config->deviceSasToken; |
AzureIoTClient | 88:248736be106e | 434 | deviceConfig.authorization_module = result->authorization_module; |
AzureIoTClient | 89:a2ed767a532e | 435 | deviceConfig.moduleId = module_id; |
AzureIoTClient | 88:248736be106e | 436 | |
AzureIoTClient | 88:248736be106e | 437 | /*Codes_SRS_IOTHUBCLIENT_LL_17_008: [IoTHubClientCore_LL_Create shall call the transport _Register function with a populated structure of type IOTHUB_DEVICE_CONFIG and waitingToSend list.] */ |
AzureIoTClient | 88:248736be106e | 438 | if ((result->deviceHandle = result->IoTHubTransport_Register(result->transportHandle, &deviceConfig, result, &(result->waitingToSend))) == NULL) |
AzureIoTClient | 88:248736be106e | 439 | { |
AzureIoTClient | 88:248736be106e | 440 | LogError("Registering device in transport failed"); |
AzureIoTClient | 88:248736be106e | 441 | IoTHubClient_Auth_Destroy(result->authorization_module); |
AzureIoTClient | 88:248736be106e | 442 | // Codes_SRS_IOTHUBCLIENT_LL_09_010: [ If any failure occurs `IoTHubClientCore_LL_Create` shall destroy the `transportHandle` only if it has created it ] |
AzureIoTClient | 88:248736be106e | 443 | if (!result->isSharedTransport) |
AzureIoTClient | 88:248736be106e | 444 | { |
AzureIoTClient | 88:248736be106e | 445 | result->IoTHubTransport_Destroy(result->transportHandle); |
AzureIoTClient | 88:248736be106e | 446 | } |
AzureIoTClient | 88:248736be106e | 447 | destroy_blob_upload_module(result); |
AzureIoTClient | 88:248736be106e | 448 | tickcounter_destroy(result->tickCounter); |
AzureIoTClient | 88:248736be106e | 449 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 450 | free(result); |
AzureIoTClient | 88:248736be106e | 451 | result = NULL; |
AzureIoTClient | 88:248736be106e | 452 | } |
AzureIoTClient | 88:248736be106e | 453 | else |
AzureIoTClient | 88:248736be106e | 454 | { |
AzureIoTClient | 88:248736be106e | 455 | /*Codes_SRS_IOTHUBCLIENT_LL_02_042: [ By default, messages shall not timeout. ]*/ |
AzureIoTClient | 88:248736be106e | 456 | result->currentMessageTimeout = 0; |
AzureIoTClient | 88:248736be106e | 457 | result->current_device_twin_timeout = 0; |
AzureIoTClient | 88:248736be106e | 458 | |
AzureIoTClient | 88:248736be106e | 459 | result->diagnostic_setting.currentMessageNumber = 0; |
AzureIoTClient | 88:248736be106e | 460 | result->diagnostic_setting.diagSamplingPercentage = 0; |
AzureIoTClient | 88:248736be106e | 461 | /*Codes_SRS_IOTHUBCLIENT_LL_25_124: [ `IoTHubClientCore_LL_Create` shall set the default retry policy as Exponential backoff with jitter and if succeed and return a `non-NULL` handle. ]*/ |
AzureIoTClient | 88:248736be106e | 462 | if (IoTHubClientCore_LL_SetRetryPolicy(result, IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF_WITH_JITTER, 0) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 88:248736be106e | 463 | { |
AzureIoTClient | 88:248736be106e | 464 | LogError("Setting default retry policy in transport failed"); |
AzureIoTClient | 88:248736be106e | 465 | result->IoTHubTransport_Unregister(result->deviceHandle); |
AzureIoTClient | 88:248736be106e | 466 | IoTHubClient_Auth_Destroy(result->authorization_module); |
AzureIoTClient | 88:248736be106e | 467 | // Codes_SRS_IOTHUBCLIENT_LL_09_010: [ If any failure occurs `IoTHubClientCore_LL_Create` shall destroy the `transportHandle` only if it has created it ] |
AzureIoTClient | 88:248736be106e | 468 | if (!result->isSharedTransport) |
AzureIoTClient | 88:248736be106e | 469 | { |
AzureIoTClient | 88:248736be106e | 470 | result->IoTHubTransport_Destroy(result->transportHandle); |
AzureIoTClient | 88:248736be106e | 471 | } |
AzureIoTClient | 88:248736be106e | 472 | destroy_blob_upload_module(result); |
AzureIoTClient | 88:248736be106e | 473 | tickcounter_destroy(result->tickCounter); |
AzureIoTClient | 88:248736be106e | 474 | STRING_delete(product_info); |
AzureIoTClient | 88:248736be106e | 475 | free(result); |
AzureIoTClient | 88:248736be106e | 476 | result = NULL; |
AzureIoTClient | 88:248736be106e | 477 | } |
AzureIoTClient | 88:248736be106e | 478 | } |
AzureIoTClient | 88:248736be106e | 479 | } |
AzureIoTClient | 88:248736be106e | 480 | } |
AzureIoTClient | 88:248736be106e | 481 | } |
AzureIoTClient | 88:248736be106e | 482 | if (IoTHubName) |
AzureIoTClient | 88:248736be106e | 483 | { |
AzureIoTClient | 88:248736be106e | 484 | free(IoTHubName); |
AzureIoTClient | 88:248736be106e | 485 | } |
AzureIoTClient | 88:248736be106e | 486 | if (IoTHubSuffix) |
AzureIoTClient | 88:248736be106e | 487 | { |
AzureIoTClient | 88:248736be106e | 488 | free(IoTHubSuffix); |
AzureIoTClient | 88:248736be106e | 489 | } |
AzureIoTClient | 88:248736be106e | 490 | } |
AzureIoTClient | 88:248736be106e | 491 | } |
AzureIoTClient | 88:248736be106e | 492 | return result; |
AzureIoTClient | 88:248736be106e | 493 | } |
AzureIoTClient | 88:248736be106e | 494 | |
AzureIoTClient | 88:248736be106e | 495 | static uint32_t get_next_item_id(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData) |
AzureIoTClient | 88:248736be106e | 496 | { |
AzureIoTClient | 88:248736be106e | 497 | if (handleData->data_msg_id+1 >= UINT32_MAX) |
AzureIoTClient | 88:248736be106e | 498 | { |
AzureIoTClient | 88:248736be106e | 499 | handleData->data_msg_id = 1; |
AzureIoTClient | 88:248736be106e | 500 | } |
AzureIoTClient | 88:248736be106e | 501 | else |
AzureIoTClient | 88:248736be106e | 502 | { |
AzureIoTClient | 88:248736be106e | 503 | handleData->data_msg_id++; |
AzureIoTClient | 88:248736be106e | 504 | } |
AzureIoTClient | 88:248736be106e | 505 | return handleData->data_msg_id; |
AzureIoTClient | 88:248736be106e | 506 | } |
AzureIoTClient | 88:248736be106e | 507 | |
AzureIoTClient | 88:248736be106e | 508 | static IOTHUB_DEVICE_TWIN* dev_twin_data_create(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData, uint32_t id, const unsigned char* reportedState, size_t size, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reportedStateCallback, void* userContextCallback) |
AzureIoTClient | 88:248736be106e | 509 | { |
AzureIoTClient | 88:248736be106e | 510 | IOTHUB_DEVICE_TWIN* result = (IOTHUB_DEVICE_TWIN*)malloc(sizeof(IOTHUB_DEVICE_TWIN) ); |
AzureIoTClient | 88:248736be106e | 511 | if (result != NULL) |
AzureIoTClient | 88:248736be106e | 512 | { |
AzureIoTClient | 88:248736be106e | 513 | result->report_data_handle = CONSTBUFFER_Create(reportedState, size); |
AzureIoTClient | 88:248736be106e | 514 | if (result->report_data_handle == NULL) |
AzureIoTClient | 88:248736be106e | 515 | { |
AzureIoTClient | 88:248736be106e | 516 | LogError("Failure allocating reported state data"); |
AzureIoTClient | 88:248736be106e | 517 | free(result); |
AzureIoTClient | 88:248736be106e | 518 | result = NULL; |
AzureIoTClient | 88:248736be106e | 519 | } |
AzureIoTClient | 88:248736be106e | 520 | else if (tickcounter_get_current_ms(handleData->tickCounter, &result->ms_timesOutAfter) != 0) |
AzureIoTClient | 88:248736be106e | 521 | { |
AzureIoTClient | 88:248736be106e | 522 | LogError("Failure getting tickcount info"); |
AzureIoTClient | 88:248736be106e | 523 | CONSTBUFFER_Destroy(result->report_data_handle); |
AzureIoTClient | 88:248736be106e | 524 | free(result); |
AzureIoTClient | 88:248736be106e | 525 | result = NULL; |
AzureIoTClient | 88:248736be106e | 526 | } |
AzureIoTClient | 88:248736be106e | 527 | else |
AzureIoTClient | 88:248736be106e | 528 | { |
AzureIoTClient | 88:248736be106e | 529 | result->item_id = id; |
AzureIoTClient | 88:248736be106e | 530 | result->ms_timesOutAfter = 0; |
AzureIoTClient | 88:248736be106e | 531 | result->context = userContextCallback; |
AzureIoTClient | 88:248736be106e | 532 | result->reported_state_callback = reportedStateCallback; |
AzureIoTClient | 88:248736be106e | 533 | result->client_handle = handleData; |
AzureIoTClient | 88:248736be106e | 534 | result->device_handle = handleData->deviceHandle; |
AzureIoTClient | 88:248736be106e | 535 | } |
AzureIoTClient | 88:248736be106e | 536 | } |
AzureIoTClient | 88:248736be106e | 537 | else |
AzureIoTClient | 88:248736be106e | 538 | { |
AzureIoTClient | 88:248736be106e | 539 | LogError("Failure allocating device twin information"); |
AzureIoTClient | 88:248736be106e | 540 | } |
AzureIoTClient | 88:248736be106e | 541 | return result; |
AzureIoTClient | 88:248736be106e | 542 | } |
AzureIoTClient | 88:248736be106e | 543 | |
AzureIoTClient | 89:a2ed767a532e | 544 | static void delete_event(IOTHUB_EVENT_CALLBACK* event_callback) |
AzureIoTClient | 89:a2ed767a532e | 545 | { |
AzureIoTClient | 89:a2ed767a532e | 546 | STRING_delete(event_callback->inputName); |
AzureIoTClient | 89:a2ed767a532e | 547 | free(event_callback->userContextCallbackEx); |
AzureIoTClient | 89:a2ed767a532e | 548 | free(event_callback); |
AzureIoTClient | 89:a2ed767a532e | 549 | } |
AzureIoTClient | 89:a2ed767a532e | 550 | |
AzureIoTClient | 89:a2ed767a532e | 551 | static void delete_event_callback(const void* item, const void* action_context, bool* continue_processing) |
AzureIoTClient | 89:a2ed767a532e | 552 | { |
AzureIoTClient | 89:a2ed767a532e | 553 | (void)action_context; |
AzureIoTClient | 89:a2ed767a532e | 554 | delete_event((IOTHUB_EVENT_CALLBACK*)item); |
AzureIoTClient | 89:a2ed767a532e | 555 | *continue_processing = true; |
AzureIoTClient | 89:a2ed767a532e | 556 | } |
AzureIoTClient | 89:a2ed767a532e | 557 | |
AzureIoTClient | 89:a2ed767a532e | 558 | static void delete_event_callback_list(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData) |
AzureIoTClient | 89:a2ed767a532e | 559 | { |
AzureIoTClient | 89:a2ed767a532e | 560 | if (handleData->event_callbacks != NULL) |
AzureIoTClient | 89:a2ed767a532e | 561 | { |
AzureIoTClient | 89:a2ed767a532e | 562 | singlylinkedlist_foreach(handleData->event_callbacks, delete_event_callback, NULL); |
AzureIoTClient | 89:a2ed767a532e | 563 | singlylinkedlist_destroy(handleData->event_callbacks); |
AzureIoTClient | 89:a2ed767a532e | 564 | handleData->event_callbacks = NULL; |
AzureIoTClient | 89:a2ed767a532e | 565 | } |
AzureIoTClient | 89:a2ed767a532e | 566 | } |
AzureIoTClient | 89:a2ed767a532e | 567 | |
AzureIoTClient | 89:a2ed767a532e | 568 | |
AzureIoTClient | 88:248736be106e | 569 | IOTHUB_CLIENT_CORE_LL_HANDLE IoTHubClientCore_LL_CreateFromDeviceAuth(const char* iothub_uri, const char* device_id, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol) |
AzureIoTClient | 88:248736be106e | 570 | { |
AzureIoTClient | 88:248736be106e | 571 | IOTHUB_CLIENT_CORE_LL_HANDLE result; |
AzureIoTClient | 88:248736be106e | 572 | if (iothub_uri == NULL || protocol == NULL || device_id == NULL) |
AzureIoTClient | 88:248736be106e | 573 | { |
AzureIoTClient | 88:248736be106e | 574 | LogError("Input parameter is NULL: iothub_uri: %p protocol: %p device_id: %p", iothub_uri, protocol, device_id); |
AzureIoTClient | 88:248736be106e | 575 | result = NULL; |
AzureIoTClient | 88:248736be106e | 576 | } |
AzureIoTClient | 88:248736be106e | 577 | else |
AzureIoTClient | 88:248736be106e | 578 | { |
AzureIoTClient | 88:248736be106e | 579 | #ifdef USE_PROV_MODULE |
AzureIoTClient | 88:248736be106e | 580 | IOTHUB_CLIENT_CONFIG* config = (IOTHUB_CLIENT_CONFIG*)malloc(sizeof(IOTHUB_CLIENT_CONFIG)); |
AzureIoTClient | 88:248736be106e | 581 | if (config == NULL) |
AzureIoTClient | 88:248736be106e | 582 | { |
AzureIoTClient | 88:248736be106e | 583 | /* Codes_SRS_IOTHUBCLIENT_LL_12_012: [If the allocation failed IoTHubClientCore_LL_CreateFromConnectionString returns NULL] */ |
AzureIoTClient | 88:248736be106e | 584 | LogError("Malloc failed"); |
AzureIoTClient | 88:248736be106e | 585 | result = NULL; |
AzureIoTClient | 88:248736be106e | 586 | } |
AzureIoTClient | 88:248736be106e | 587 | else |
AzureIoTClient | 88:248736be106e | 588 | { |
AzureIoTClient | 88:248736be106e | 589 | const char* iterator; |
AzureIoTClient | 88:248736be106e | 590 | const char* initial; |
AzureIoTClient | 88:248736be106e | 591 | char* iothub_name = NULL; |
AzureIoTClient | 88:248736be106e | 592 | char* iothub_suffix = NULL; |
AzureIoTClient | 88:248736be106e | 593 | |
AzureIoTClient | 88:248736be106e | 594 | memset(config, 0, sizeof(IOTHUB_CLIENT_CONFIG)); |
AzureIoTClient | 88:248736be106e | 595 | config->protocol = protocol; |
AzureIoTClient | 88:248736be106e | 596 | config->deviceId = device_id; |
AzureIoTClient | 88:248736be106e | 597 | |
AzureIoTClient | 88:248736be106e | 598 | // Find the iothub suffix |
AzureIoTClient | 88:248736be106e | 599 | initial = iterator = iothub_uri; |
AzureIoTClient | 88:248736be106e | 600 | while (iterator != NULL && *iterator != '\0') |
AzureIoTClient | 88:248736be106e | 601 | { |
AzureIoTClient | 88:248736be106e | 602 | if (*iterator == '.') |
AzureIoTClient | 88:248736be106e | 603 | { |
AzureIoTClient | 88:248736be106e | 604 | size_t length = iterator - initial; |
AzureIoTClient | 88:248736be106e | 605 | iothub_name = (char*)malloc(length + 1); |
AzureIoTClient | 88:248736be106e | 606 | if (iothub_name != NULL) |
AzureIoTClient | 88:248736be106e | 607 | { |
AzureIoTClient | 88:248736be106e | 608 | memset(iothub_name, 0, length + 1); |
AzureIoTClient | 88:248736be106e | 609 | memcpy(iothub_name, initial, length); |
AzureIoTClient | 88:248736be106e | 610 | config->iotHubName = iothub_name; |
AzureIoTClient | 88:248736be106e | 611 | |
AzureIoTClient | 88:248736be106e | 612 | length = strlen(initial) - length - 1; |
AzureIoTClient | 88:248736be106e | 613 | iothub_suffix = (char*)malloc(length + 1); |
AzureIoTClient | 88:248736be106e | 614 | if (iothub_suffix != NULL) |
AzureIoTClient | 88:248736be106e | 615 | { |
AzureIoTClient | 88:248736be106e | 616 | memset(iothub_suffix, 0, length + 1); |
AzureIoTClient | 88:248736be106e | 617 | memcpy(iothub_suffix, iterator + 1, length); |
AzureIoTClient | 88:248736be106e | 618 | config->iotHubSuffix = iothub_suffix; |
AzureIoTClient | 88:248736be106e | 619 | break; |
AzureIoTClient | 88:248736be106e | 620 | } |
AzureIoTClient | 88:248736be106e | 621 | else |
AzureIoTClient | 88:248736be106e | 622 | { |
AzureIoTClient | 88:248736be106e | 623 | LogError("Failed to allocate iothub suffix"); |
AzureIoTClient | 88:248736be106e | 624 | free(iothub_name); |
AzureIoTClient | 88:248736be106e | 625 | iothub_name = NULL; |
AzureIoTClient | 88:248736be106e | 626 | result = NULL; |
AzureIoTClient | 88:248736be106e | 627 | } |
AzureIoTClient | 88:248736be106e | 628 | } |
AzureIoTClient | 88:248736be106e | 629 | else |
AzureIoTClient | 88:248736be106e | 630 | { |
AzureIoTClient | 88:248736be106e | 631 | LogError("Failed to allocate iothub name"); |
AzureIoTClient | 88:248736be106e | 632 | result = NULL; |
AzureIoTClient | 88:248736be106e | 633 | } |
AzureIoTClient | 88:248736be106e | 634 | } |
AzureIoTClient | 88:248736be106e | 635 | iterator++; |
AzureIoTClient | 88:248736be106e | 636 | } |
AzureIoTClient | 88:248736be106e | 637 | |
AzureIoTClient | 88:248736be106e | 638 | if (config->iotHubName == NULL || config->iotHubSuffix == NULL) |
AzureIoTClient | 88:248736be106e | 639 | { |
AzureIoTClient | 88:248736be106e | 640 | LogError("initialize iothub client"); |
AzureIoTClient | 88:248736be106e | 641 | result = NULL; |
AzureIoTClient | 88:248736be106e | 642 | } |
AzureIoTClient | 88:248736be106e | 643 | else |
AzureIoTClient | 88:248736be106e | 644 | { |
AzureIoTClient | 89:a2ed767a532e | 645 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = initialize_iothub_client(config, NULL, true, NULL); |
AzureIoTClient | 88:248736be106e | 646 | if (handleData == NULL) |
AzureIoTClient | 88:248736be106e | 647 | { |
AzureIoTClient | 88:248736be106e | 648 | LogError("initialize iothub client"); |
AzureIoTClient | 88:248736be106e | 649 | result = NULL; |
AzureIoTClient | 88:248736be106e | 650 | } |
AzureIoTClient | 88:248736be106e | 651 | else |
AzureIoTClient | 88:248736be106e | 652 | { |
AzureIoTClient | 88:248736be106e | 653 | result = handleData; |
AzureIoTClient | 88:248736be106e | 654 | } |
AzureIoTClient | 88:248736be106e | 655 | } |
AzureIoTClient | 88:248736be106e | 656 | |
AzureIoTClient | 88:248736be106e | 657 | free(iothub_name); |
AzureIoTClient | 88:248736be106e | 658 | free(iothub_suffix); |
AzureIoTClient | 88:248736be106e | 659 | free(config); |
AzureIoTClient | 88:248736be106e | 660 | } |
AzureIoTClient | 88:248736be106e | 661 | #else |
AzureIoTClient | 88:248736be106e | 662 | LogError("HSM module is not included"); |
AzureIoTClient | 88:248736be106e | 663 | result = NULL; |
AzureIoTClient | 88:248736be106e | 664 | #endif |
AzureIoTClient | 88:248736be106e | 665 | } |
AzureIoTClient | 88:248736be106e | 666 | return result; |
AzureIoTClient | 88:248736be106e | 667 | } |
AzureIoTClient | 88:248736be106e | 668 | |
AzureIoTClient | 88:248736be106e | 669 | IOTHUB_CLIENT_CORE_LL_HANDLE IoTHubClientCore_LL_CreateFromConnectionString(const char* connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol) |
AzureIoTClient | 88:248736be106e | 670 | { |
AzureIoTClient | 88:248736be106e | 671 | IOTHUB_CLIENT_CORE_LL_HANDLE result; |
AzureIoTClient | 88:248736be106e | 672 | |
AzureIoTClient | 88:248736be106e | 673 | /* Codes_SRS_IOTHUBCLIENT_LL_12_003: [IoTHubClientCore_LL_CreateFromConnectionString shall verify the input parameter and if it is NULL then return NULL] */ |
AzureIoTClient | 88:248736be106e | 674 | if (connectionString == NULL) |
AzureIoTClient | 88:248736be106e | 675 | { |
AzureIoTClient | 88:248736be106e | 676 | LogError("Input parameter is NULL: connectionString"); |
AzureIoTClient | 88:248736be106e | 677 | result = NULL; |
AzureIoTClient | 88:248736be106e | 678 | } |
AzureIoTClient | 88:248736be106e | 679 | else if (protocol == NULL) |
AzureIoTClient | 88:248736be106e | 680 | { |
AzureIoTClient | 88:248736be106e | 681 | LogError("Input parameter is NULL: protocol"); |
AzureIoTClient | 88:248736be106e | 682 | result = NULL; |
AzureIoTClient | 88:248736be106e | 683 | } |
AzureIoTClient | 88:248736be106e | 684 | else |
AzureIoTClient | 88:248736be106e | 685 | { |
AzureIoTClient | 88:248736be106e | 686 | /* Codes_SRS_IOTHUBCLIENT_LL_12_004: [IoTHubClientCore_LL_CreateFromConnectionString shall allocate IOTHUB_CLIENT_CONFIG structure] */ |
AzureIoTClient | 88:248736be106e | 687 | IOTHUB_CLIENT_CONFIG* config = (IOTHUB_CLIENT_CONFIG*) malloc(sizeof(IOTHUB_CLIENT_CONFIG)); |
AzureIoTClient | 88:248736be106e | 688 | if (config == NULL) |
AzureIoTClient | 88:248736be106e | 689 | { |
AzureIoTClient | 88:248736be106e | 690 | /* Codes_SRS_IOTHUBCLIENT_LL_12_012: [If the allocation failed IoTHubClientCore_LL_CreateFromConnectionString returns NULL] */ |
AzureIoTClient | 88:248736be106e | 691 | LogError("Malloc failed"); |
AzureIoTClient | 88:248736be106e | 692 | result = NULL; |
AzureIoTClient | 88:248736be106e | 693 | } |
AzureIoTClient | 88:248736be106e | 694 | else |
AzureIoTClient | 88:248736be106e | 695 | { |
AzureIoTClient | 88:248736be106e | 696 | STRING_TOKENIZER_HANDLE tokenizer1 = NULL; |
AzureIoTClient | 88:248736be106e | 697 | STRING_HANDLE connString = NULL; |
AzureIoTClient | 88:248736be106e | 698 | STRING_HANDLE tokenString = NULL; |
AzureIoTClient | 88:248736be106e | 699 | STRING_HANDLE valueString = NULL; |
AzureIoTClient | 88:248736be106e | 700 | STRING_HANDLE hostNameString = NULL; |
AzureIoTClient | 88:248736be106e | 701 | STRING_HANDLE hostSuffixString = NULL; |
AzureIoTClient | 88:248736be106e | 702 | STRING_HANDLE deviceIdString = NULL; |
AzureIoTClient | 88:248736be106e | 703 | STRING_HANDLE deviceKeyString = NULL; |
AzureIoTClient | 88:248736be106e | 704 | STRING_HANDLE deviceSasTokenString = NULL; |
AzureIoTClient | 88:248736be106e | 705 | STRING_HANDLE protocolGateway = NULL; |
AzureIoTClient | 89:a2ed767a532e | 706 | STRING_HANDLE moduleId = NULL; |
AzureIoTClient | 88:248736be106e | 707 | |
AzureIoTClient | 89:a2ed767a532e | 708 | memset(config, 0, sizeof(*config)); |
AzureIoTClient | 89:a2ed767a532e | 709 | config->protocol = protocol; |
AzureIoTClient | 88:248736be106e | 710 | |
AzureIoTClient | 88:248736be106e | 711 | /* Codes_SRS_IOTHUBCLIENT_LL_04_002: [If it does not, it shall pass the protocolGatewayHostName NULL.] */ |
AzureIoTClient | 88:248736be106e | 712 | config->protocolGatewayHostName = NULL; |
AzureIoTClient | 88:248736be106e | 713 | |
AzureIoTClient | 88:248736be106e | 714 | if ((connString = STRING_construct(connectionString)) == NULL) |
AzureIoTClient | 88:248736be106e | 715 | { |
AzureIoTClient | 88:248736be106e | 716 | LogError("Error constructing connectiong String"); |
AzureIoTClient | 88:248736be106e | 717 | result = NULL; |
AzureIoTClient | 88:248736be106e | 718 | } |
AzureIoTClient | 88:248736be106e | 719 | else if ((tokenizer1 = STRING_TOKENIZER_create(connString)) == NULL) |
AzureIoTClient | 88:248736be106e | 720 | { |
AzureIoTClient | 88:248736be106e | 721 | LogError("Error creating Tokenizer"); |
AzureIoTClient | 88:248736be106e | 722 | result = NULL; |
AzureIoTClient | 88:248736be106e | 723 | } |
AzureIoTClient | 88:248736be106e | 724 | else if ((tokenString = STRING_new()) == NULL) |
AzureIoTClient | 88:248736be106e | 725 | { |
AzureIoTClient | 88:248736be106e | 726 | LogError("Error creating Token String"); |
AzureIoTClient | 88:248736be106e | 727 | result = NULL; |
AzureIoTClient | 88:248736be106e | 728 | } |
AzureIoTClient | 88:248736be106e | 729 | else if ((valueString = STRING_new()) == NULL) |
AzureIoTClient | 88:248736be106e | 730 | { |
AzureIoTClient | 88:248736be106e | 731 | LogError("Error creating Value String"); |
AzureIoTClient | 88:248736be106e | 732 | result = NULL; |
AzureIoTClient | 88:248736be106e | 733 | } |
AzureIoTClient | 88:248736be106e | 734 | else if ((hostNameString = STRING_new()) == NULL) |
AzureIoTClient | 88:248736be106e | 735 | { |
AzureIoTClient | 88:248736be106e | 736 | LogError("Error creating HostName String"); |
AzureIoTClient | 88:248736be106e | 737 | result = NULL; |
AzureIoTClient | 88:248736be106e | 738 | } |
AzureIoTClient | 88:248736be106e | 739 | else if ((hostSuffixString = STRING_new()) == NULL) |
AzureIoTClient | 88:248736be106e | 740 | { |
AzureIoTClient | 88:248736be106e | 741 | LogError("Error creating HostSuffix String"); |
AzureIoTClient | 88:248736be106e | 742 | result = NULL; |
AzureIoTClient | 88:248736be106e | 743 | } |
AzureIoTClient | 88:248736be106e | 744 | /* Codes_SRS_IOTHUBCLIENT_LL_12_005: [IoTHubClientCore_LL_CreateFromConnectionString shall try to parse the connectionString input parameter for the following structure: "Key1=value1;key2=value2;key3=value3..."] */ |
AzureIoTClient | 88:248736be106e | 745 | /* Codes_SRS_IOTHUBCLIENT_LL_12_006: [IoTHubClientCore_LL_CreateFromConnectionString shall verify the existence of the following Key/Value pairs in the connection string: HostName, DeviceId, SharedAccessKey, SharedAccessSignature or x509] */ |
AzureIoTClient | 88:248736be106e | 746 | else |
AzureIoTClient | 88:248736be106e | 747 | { |
AzureIoTClient | 88:248736be106e | 748 | int isx509found = 0; |
AzureIoTClient | 88:248736be106e | 749 | bool use_provisioning = false; |
AzureIoTClient | 88:248736be106e | 750 | while ((STRING_TOKENIZER_get_next_token(tokenizer1, tokenString, "=") == 0)) |
AzureIoTClient | 88:248736be106e | 751 | { |
AzureIoTClient | 88:248736be106e | 752 | if (STRING_TOKENIZER_get_next_token(tokenizer1, valueString, ";") != 0) |
AzureIoTClient | 88:248736be106e | 753 | { |
AzureIoTClient | 88:248736be106e | 754 | LogError("Tokenizer error"); |
AzureIoTClient | 88:248736be106e | 755 | break; |
AzureIoTClient | 88:248736be106e | 756 | } |
AzureIoTClient | 88:248736be106e | 757 | else |
AzureIoTClient | 88:248736be106e | 758 | { |
AzureIoTClient | 88:248736be106e | 759 | if (tokenString != NULL) |
AzureIoTClient | 88:248736be106e | 760 | { |
AzureIoTClient | 88:248736be106e | 761 | /* Codes_SRS_IOTHUBCLIENT_LL_12_010: [IoTHubClientCore_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 | 88:248736be106e | 762 | const char* s_token = STRING_c_str(tokenString); |
AzureIoTClient | 88:248736be106e | 763 | if (strcmp(s_token, HOSTNAME_TOKEN) == 0) |
AzureIoTClient | 88:248736be106e | 764 | { |
AzureIoTClient | 88:248736be106e | 765 | /* Codes_SRS_IOTHUBCLIENT_LL_12_009: [IoTHubClientCore_LL_CreateFromConnectionString shall split the value of HostName to Name and Suffix using the first "." as a separator] */ |
AzureIoTClient | 88:248736be106e | 766 | STRING_TOKENIZER_HANDLE tokenizer2 = NULL; |
AzureIoTClient | 88:248736be106e | 767 | if ((tokenizer2 = STRING_TOKENIZER_create(valueString)) == NULL) |
AzureIoTClient | 88:248736be106e | 768 | { |
AzureIoTClient | 88:248736be106e | 769 | LogError("Error creating Tokenizer"); |
AzureIoTClient | 88:248736be106e | 770 | break; |
AzureIoTClient | 88:248736be106e | 771 | } |
AzureIoTClient | 88:248736be106e | 772 | else |
AzureIoTClient | 88:248736be106e | 773 | { |
AzureIoTClient | 88:248736be106e | 774 | /* Codes_SRS_IOTHUBCLIENT_LL_12_015: [If the string split failed, IoTHubClientCore_LL_CreateFromConnectionString returns NULL ] */ |
AzureIoTClient | 88:248736be106e | 775 | if (STRING_TOKENIZER_get_next_token(tokenizer2, hostNameString, ".") != 0) |
AzureIoTClient | 88:248736be106e | 776 | { |
AzureIoTClient | 88:248736be106e | 777 | LogError("Tokenizer error"); |
AzureIoTClient | 88:248736be106e | 778 | STRING_TOKENIZER_destroy(tokenizer2); |
AzureIoTClient | 88:248736be106e | 779 | break; |
AzureIoTClient | 88:248736be106e | 780 | } |
AzureIoTClient | 88:248736be106e | 781 | else |
AzureIoTClient | 88:248736be106e | 782 | { |
AzureIoTClient | 88:248736be106e | 783 | config->iotHubName = STRING_c_str(hostNameString); |
AzureIoTClient | 88:248736be106e | 784 | if (STRING_TOKENIZER_get_next_token(tokenizer2, hostSuffixString, ";") != 0) |
AzureIoTClient | 88:248736be106e | 785 | { |
AzureIoTClient | 88:248736be106e | 786 | LogError("Tokenizer error"); |
AzureIoTClient | 88:248736be106e | 787 | STRING_TOKENIZER_destroy(tokenizer2); |
AzureIoTClient | 88:248736be106e | 788 | break; |
AzureIoTClient | 88:248736be106e | 789 | } |
AzureIoTClient | 88:248736be106e | 790 | else |
AzureIoTClient | 88:248736be106e | 791 | { |
AzureIoTClient | 88:248736be106e | 792 | config->iotHubSuffix = STRING_c_str(hostSuffixString); |
AzureIoTClient | 88:248736be106e | 793 | } |
AzureIoTClient | 88:248736be106e | 794 | } |
AzureIoTClient | 88:248736be106e | 795 | STRING_TOKENIZER_destroy(tokenizer2); |
AzureIoTClient | 88:248736be106e | 796 | } |
AzureIoTClient | 88:248736be106e | 797 | } |
AzureIoTClient | 88:248736be106e | 798 | else if (strcmp(s_token, DEVICEID_TOKEN) == 0) |
AzureIoTClient | 88:248736be106e | 799 | { |
AzureIoTClient | 88:248736be106e | 800 | deviceIdString = STRING_clone(valueString); |
AzureIoTClient | 88:248736be106e | 801 | if (deviceIdString != NULL) |
AzureIoTClient | 88:248736be106e | 802 | { |
AzureIoTClient | 88:248736be106e | 803 | config->deviceId = STRING_c_str(deviceIdString); |
AzureIoTClient | 88:248736be106e | 804 | } |
AzureIoTClient | 88:248736be106e | 805 | else |
AzureIoTClient | 88:248736be106e | 806 | { |
AzureIoTClient | 88:248736be106e | 807 | LogError("Failure cloning device id string"); |
AzureIoTClient | 88:248736be106e | 808 | break; |
AzureIoTClient | 88:248736be106e | 809 | } |
AzureIoTClient | 88:248736be106e | 810 | } |
AzureIoTClient | 88:248736be106e | 811 | else if (strcmp(s_token, DEVICEKEY_TOKEN) == 0) |
AzureIoTClient | 88:248736be106e | 812 | { |
AzureIoTClient | 88:248736be106e | 813 | deviceKeyString = STRING_clone(valueString); |
AzureIoTClient | 88:248736be106e | 814 | if (deviceKeyString != NULL) |
AzureIoTClient | 88:248736be106e | 815 | { |
AzureIoTClient | 88:248736be106e | 816 | config->deviceKey = STRING_c_str(deviceKeyString); |
AzureIoTClient | 88:248736be106e | 817 | } |
AzureIoTClient | 88:248736be106e | 818 | else |
AzureIoTClient | 88:248736be106e | 819 | { |
AzureIoTClient | 88:248736be106e | 820 | LogError("Failure cloning device key string"); |
AzureIoTClient | 88:248736be106e | 821 | break; |
AzureIoTClient | 88:248736be106e | 822 | } |
AzureIoTClient | 88:248736be106e | 823 | } |
AzureIoTClient | 88:248736be106e | 824 | else if (strcmp(s_token, DEVICESAS_TOKEN) == 0) |
AzureIoTClient | 88:248736be106e | 825 | { |
AzureIoTClient | 88:248736be106e | 826 | deviceSasTokenString = STRING_clone(valueString); |
AzureIoTClient | 88:248736be106e | 827 | if (deviceSasTokenString != NULL) |
AzureIoTClient | 88:248736be106e | 828 | { |
AzureIoTClient | 88:248736be106e | 829 | config->deviceSasToken = STRING_c_str(deviceSasTokenString); |
AzureIoTClient | 88:248736be106e | 830 | } |
AzureIoTClient | 88:248736be106e | 831 | else |
AzureIoTClient | 88:248736be106e | 832 | { |
AzureIoTClient | 88:248736be106e | 833 | LogError("Failure cloning device sasToken string"); |
AzureIoTClient | 88:248736be106e | 834 | break; |
AzureIoTClient | 88:248736be106e | 835 | } |
AzureIoTClient | 88:248736be106e | 836 | } |
AzureIoTClient | 88:248736be106e | 837 | else if (strcmp(s_token, X509_TOKEN) == 0) |
AzureIoTClient | 88:248736be106e | 838 | { |
AzureIoTClient | 88:248736be106e | 839 | if (strcmp(STRING_c_str(valueString), X509_TOKEN_ONLY_ACCEPTABLE_VALUE) != 0) |
AzureIoTClient | 88:248736be106e | 840 | { |
AzureIoTClient | 88:248736be106e | 841 | LogError("x509 option has wrong value, the only acceptable one is \"true\""); |
AzureIoTClient | 88:248736be106e | 842 | break; |
AzureIoTClient | 88:248736be106e | 843 | } |
AzureIoTClient | 88:248736be106e | 844 | else |
AzureIoTClient | 88:248736be106e | 845 | { |
AzureIoTClient | 88:248736be106e | 846 | isx509found = 1; |
AzureIoTClient | 88:248736be106e | 847 | } |
AzureIoTClient | 88:248736be106e | 848 | } |
AzureIoTClient | 88:248736be106e | 849 | else if (strcmp(s_token, PROVISIONING_TOKEN) == 0) |
AzureIoTClient | 88:248736be106e | 850 | { |
AzureIoTClient | 88:248736be106e | 851 | if (strcmp(STRING_c_str(valueString), PROVISIONING_ACCEPTABLE_VALUE) != 0) |
AzureIoTClient | 88:248736be106e | 852 | { |
AzureIoTClient | 88:248736be106e | 853 | LogError("provisioning option has wrong value, the only acceptable one is \"true\""); |
AzureIoTClient | 88:248736be106e | 854 | break; |
AzureIoTClient | 88:248736be106e | 855 | } |
AzureIoTClient | 88:248736be106e | 856 | else |
AzureIoTClient | 88:248736be106e | 857 | { |
AzureIoTClient | 88:248736be106e | 858 | use_provisioning = 1; |
AzureIoTClient | 88:248736be106e | 859 | } |
AzureIoTClient | 88:248736be106e | 860 | } |
AzureIoTClient | 88:248736be106e | 861 | |
AzureIoTClient | 88:248736be106e | 862 | /* Codes_SRS_IOTHUBCLIENT_LL_04_001: [IoTHubClientCore_LL_CreateFromConnectionString shall verify the existence of key/value pair GatewayHostName. If it does exist it shall pass the value to IoTHubClientCore_LL_Create API.] */ |
AzureIoTClient | 89:a2ed767a532e | 863 | else if (strcmp(s_token, PROTOCOL_GATEWAY_HOST_TOKEN) == 0) |
AzureIoTClient | 88:248736be106e | 864 | { |
AzureIoTClient | 88:248736be106e | 865 | protocolGateway = STRING_clone(valueString); |
AzureIoTClient | 88:248736be106e | 866 | if (protocolGateway != NULL) |
AzureIoTClient | 88:248736be106e | 867 | { |
AzureIoTClient | 88:248736be106e | 868 | config->protocolGatewayHostName = STRING_c_str(protocolGateway); |
AzureIoTClient | 88:248736be106e | 869 | } |
AzureIoTClient | 88:248736be106e | 870 | else |
AzureIoTClient | 88:248736be106e | 871 | { |
AzureIoTClient | 88:248736be106e | 872 | LogError("Failure cloning protocol Gateway Name"); |
AzureIoTClient | 88:248736be106e | 873 | break; |
AzureIoTClient | 88:248736be106e | 874 | } |
AzureIoTClient | 88:248736be106e | 875 | } |
AzureIoTClient | 89:a2ed767a532e | 876 | /*Codes_SRS_IOTHUBCLIENT_LL_31_126: [IoTHubClient_LL_CreateFromConnectionString shall optionally parse ModuleId, if present.] */ |
AzureIoTClient | 89:a2ed767a532e | 877 | else if (strcmp(s_token, MODULE_ID_TOKEN) == 0) |
AzureIoTClient | 89:a2ed767a532e | 878 | { |
AzureIoTClient | 89:a2ed767a532e | 879 | moduleId = STRING_clone(valueString); |
AzureIoTClient | 89:a2ed767a532e | 880 | if (moduleId == NULL) |
AzureIoTClient | 89:a2ed767a532e | 881 | { |
AzureIoTClient | 89:a2ed767a532e | 882 | LogError("Failure cloning moduleId string"); |
AzureIoTClient | 89:a2ed767a532e | 883 | break; |
AzureIoTClient | 89:a2ed767a532e | 884 | } |
AzureIoTClient | 89:a2ed767a532e | 885 | } |
AzureIoTClient | 88:248736be106e | 886 | } |
AzureIoTClient | 88:248736be106e | 887 | } |
AzureIoTClient | 88:248736be106e | 888 | } |
AzureIoTClient | 88:248736be106e | 889 | /* parsing is done - check the result */ |
AzureIoTClient | 88:248736be106e | 890 | if (config->iotHubName == NULL) |
AzureIoTClient | 88:248736be106e | 891 | { |
AzureIoTClient | 88:248736be106e | 892 | LogError("iotHubName is not found"); |
AzureIoTClient | 88:248736be106e | 893 | result = NULL; |
AzureIoTClient | 88:248736be106e | 894 | } |
AzureIoTClient | 88:248736be106e | 895 | else if (config->iotHubSuffix == NULL) |
AzureIoTClient | 88:248736be106e | 896 | { |
AzureIoTClient | 88:248736be106e | 897 | LogError("iotHubSuffix is not found"); |
AzureIoTClient | 88:248736be106e | 898 | result = NULL; |
AzureIoTClient | 88:248736be106e | 899 | } |
AzureIoTClient | 88:248736be106e | 900 | else if (config->deviceId == NULL) |
AzureIoTClient | 88:248736be106e | 901 | { |
AzureIoTClient | 88:248736be106e | 902 | LogError("deviceId is not found"); |
AzureIoTClient | 88:248736be106e | 903 | result = NULL; |
AzureIoTClient | 88:248736be106e | 904 | } |
AzureIoTClient | 88:248736be106e | 905 | else if (!( |
AzureIoTClient | 88:248736be106e | 906 | ((!use_provisioning && !isx509found) && (config->deviceSasToken == NULL) ^ (config->deviceKey == NULL)) || |
AzureIoTClient | 88:248736be106e | 907 | ((use_provisioning || isx509found) && (config->deviceSasToken == NULL) && (config->deviceKey == NULL)) |
AzureIoTClient | 88:248736be106e | 908 | )) |
AzureIoTClient | 88:248736be106e | 909 | { |
AzureIoTClient | 88:248736be106e | 910 | LogError("invalid combination of x509, provisioning, deviceSasToken and deviceKey"); |
AzureIoTClient | 88:248736be106e | 911 | result = NULL; |
AzureIoTClient | 88:248736be106e | 912 | } |
AzureIoTClient | 88:248736be106e | 913 | else |
AzureIoTClient | 88:248736be106e | 914 | { |
AzureIoTClient | 88:248736be106e | 915 | /* Codes_SRS_IOTHUBCLIENT_LL_12_011: [IoTHubClientCore_LL_CreateFromConnectionString shall call into the IoTHubClientCore_LL_Create API with the current structure and returns with the return value of it] */ |
AzureIoTClient | 89:a2ed767a532e | 916 | result = initialize_iothub_client(config, NULL, use_provisioning, STRING_c_str(moduleId)); |
AzureIoTClient | 88:248736be106e | 917 | if (result == NULL) |
AzureIoTClient | 88:248736be106e | 918 | { |
AzureIoTClient | 88:248736be106e | 919 | LogError("IoTHubClientCore_LL_Create failed"); |
AzureIoTClient | 88:248736be106e | 920 | } |
AzureIoTClient | 88:248736be106e | 921 | else |
AzureIoTClient | 88:248736be106e | 922 | { |
AzureIoTClient | 88:248736be106e | 923 | /*return as is*/ |
AzureIoTClient | 88:248736be106e | 924 | } |
AzureIoTClient | 88:248736be106e | 925 | } |
AzureIoTClient | 88:248736be106e | 926 | } |
AzureIoTClient | 88:248736be106e | 927 | if (deviceSasTokenString != NULL) |
AzureIoTClient | 88:248736be106e | 928 | STRING_delete(deviceSasTokenString); |
AzureIoTClient | 88:248736be106e | 929 | if (deviceKeyString != NULL) |
AzureIoTClient | 88:248736be106e | 930 | STRING_delete(deviceKeyString); |
AzureIoTClient | 88:248736be106e | 931 | if (deviceIdString != NULL) |
AzureIoTClient | 88:248736be106e | 932 | STRING_delete(deviceIdString); |
AzureIoTClient | 88:248736be106e | 933 | if (hostSuffixString != NULL) |
AzureIoTClient | 88:248736be106e | 934 | STRING_delete(hostSuffixString); |
AzureIoTClient | 88:248736be106e | 935 | if (hostNameString != NULL) |
AzureIoTClient | 88:248736be106e | 936 | STRING_delete(hostNameString); |
AzureIoTClient | 88:248736be106e | 937 | if (valueString != NULL) |
AzureIoTClient | 88:248736be106e | 938 | STRING_delete(valueString); |
AzureIoTClient | 88:248736be106e | 939 | if (tokenString != NULL) |
AzureIoTClient | 88:248736be106e | 940 | STRING_delete(tokenString); |
AzureIoTClient | 88:248736be106e | 941 | if (connString != NULL) |
AzureIoTClient | 88:248736be106e | 942 | STRING_delete(connString); |
AzureIoTClient | 88:248736be106e | 943 | if (protocolGateway != NULL) |
AzureIoTClient | 88:248736be106e | 944 | STRING_delete(protocolGateway); |
AzureIoTClient | 89:a2ed767a532e | 945 | if (moduleId != NULL) |
AzureIoTClient | 89:a2ed767a532e | 946 | STRING_delete(moduleId); |
AzureIoTClient | 88:248736be106e | 947 | |
AzureIoTClient | 88:248736be106e | 948 | if (tokenizer1 != NULL) |
AzureIoTClient | 88:248736be106e | 949 | STRING_TOKENIZER_destroy(tokenizer1); |
AzureIoTClient | 88:248736be106e | 950 | |
AzureIoTClient | 88:248736be106e | 951 | free(config); |
AzureIoTClient | 88:248736be106e | 952 | } |
AzureIoTClient | 88:248736be106e | 953 | } |
AzureIoTClient | 88:248736be106e | 954 | return result; |
AzureIoTClient | 88:248736be106e | 955 | } |
AzureIoTClient | 88:248736be106e | 956 | |
AzureIoTClient | 89:a2ed767a532e | 957 | IOTHUB_CLIENT_CORE_LL_HANDLE IoTHubClientCore_LL_CreateImpl(const IOTHUB_CLIENT_CONFIG* config, const char* module_id, bool use_dev_auth) |
AzureIoTClient | 88:248736be106e | 958 | { |
AzureIoTClient | 88:248736be106e | 959 | IOTHUB_CLIENT_CORE_LL_HANDLE result; |
AzureIoTClient | 88:248736be106e | 960 | /*Codes_SRS_IOTHUBCLIENT_LL_02_001: [IoTHubClientCore_LL_Create shall return NULL if config parameter is NULL or protocol field is NULL.]*/ |
AzureIoTClient | 88:248736be106e | 961 | if( |
AzureIoTClient | 88:248736be106e | 962 | (config == NULL) || |
AzureIoTClient | 88:248736be106e | 963 | (config->protocol == NULL) |
AzureIoTClient | 88:248736be106e | 964 | ) |
AzureIoTClient | 88:248736be106e | 965 | { |
AzureIoTClient | 88:248736be106e | 966 | result = NULL; |
AzureIoTClient | 88:248736be106e | 967 | LogError("invalid configuration (NULL detected)"); |
AzureIoTClient | 88:248736be106e | 968 | } |
AzureIoTClient | 88:248736be106e | 969 | else |
AzureIoTClient | 88:248736be106e | 970 | { |
AzureIoTClient | 89:a2ed767a532e | 971 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = initialize_iothub_client(config, NULL, use_dev_auth, module_id); |
AzureIoTClient | 88:248736be106e | 972 | if (handleData == NULL) |
AzureIoTClient | 88:248736be106e | 973 | { |
AzureIoTClient | 88:248736be106e | 974 | LogError("initialize iothub client"); |
AzureIoTClient | 88:248736be106e | 975 | result = NULL; |
AzureIoTClient | 88:248736be106e | 976 | } |
AzureIoTClient | 88:248736be106e | 977 | else |
AzureIoTClient | 88:248736be106e | 978 | { |
AzureIoTClient | 88:248736be106e | 979 | result = handleData; |
AzureIoTClient | 88:248736be106e | 980 | } |
AzureIoTClient | 88:248736be106e | 981 | } |
AzureIoTClient | 88:248736be106e | 982 | |
AzureIoTClient | 88:248736be106e | 983 | return result; |
AzureIoTClient | 88:248736be106e | 984 | } |
AzureIoTClient | 88:248736be106e | 985 | |
AzureIoTClient | 89:a2ed767a532e | 986 | IOTHUB_CLIENT_CORE_LL_HANDLE IoTHubClientCore_LL_Create(const IOTHUB_CLIENT_CONFIG* config) |
AzureIoTClient | 89:a2ed767a532e | 987 | { |
AzureIoTClient | 89:a2ed767a532e | 988 | return IoTHubClientCore_LL_CreateImpl(config, NULL, false); |
AzureIoTClient | 89:a2ed767a532e | 989 | } |
AzureIoTClient | 89:a2ed767a532e | 990 | |
AzureIoTClient | 89:a2ed767a532e | 991 | #ifdef USE_EDGE_MODULES |
AzureIoTClient | 89:a2ed767a532e | 992 | IOTHUB_CLIENT_CORE_LL_HANDLE IoTHubClientCore_LL_CreateFromEnvironment(const IOTHUB_CLIENT_CONFIG* config, const char* module_id) |
AzureIoTClient | 89:a2ed767a532e | 993 | { |
AzureIoTClient | 89:a2ed767a532e | 994 | IOTHUB_CLIENT_CORE_LL_HANDLE result; |
AzureIoTClient | 89:a2ed767a532e | 995 | |
AzureIoTClient | 89:a2ed767a532e | 996 | if (module_id == NULL) |
AzureIoTClient | 89:a2ed767a532e | 997 | { |
AzureIoTClient | 89:a2ed767a532e | 998 | LogError("module_id cannot be NULL"); |
AzureIoTClient | 89:a2ed767a532e | 999 | result = NULL; |
AzureIoTClient | 89:a2ed767a532e | 1000 | } |
AzureIoTClient | 89:a2ed767a532e | 1001 | else if ((result = IoTHubClientCore_LL_CreateImpl(config, module_id, true)) != NULL) |
AzureIoTClient | 89:a2ed767a532e | 1002 | { |
AzureIoTClient | 89:a2ed767a532e | 1003 | // Because the Edge Hub almost always use self-signed certificates, we need |
AzureIoTClient | 89:a2ed767a532e | 1004 | // to query it for the the certificate its using so we can trust it. |
AzureIoTClient | 89:a2ed767a532e | 1005 | char* trustedCertificate = IoTHubClient_Auth_Get_TrustBundle(result->authorization_module); |
AzureIoTClient | 89:a2ed767a532e | 1006 | IOTHUB_CLIENT_RESULT setTrustResult; |
AzureIoTClient | 89:a2ed767a532e | 1007 | |
AzureIoTClient | 89:a2ed767a532e | 1008 | if (trustedCertificate == NULL) |
AzureIoTClient | 89:a2ed767a532e | 1009 | { |
AzureIoTClient | 89:a2ed767a532e | 1010 | LogError("IoTHubClient_Auth_Get_TrustBundle failed"); |
AzureIoTClient | 89:a2ed767a532e | 1011 | IoTHubClientCore_LL_Destroy(result); |
AzureIoTClient | 89:a2ed767a532e | 1012 | result = NULL; |
AzureIoTClient | 89:a2ed767a532e | 1013 | } |
AzureIoTClient | 89:a2ed767a532e | 1014 | else if ((setTrustResult = IoTHubClientCore_LL_SetOption(result, OPTION_TRUSTED_CERT, trustedCertificate)) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 89:a2ed767a532e | 1015 | { |
AzureIoTClient | 89:a2ed767a532e | 1016 | LogError("IoTHubClientCore_LL_SetOption failed, err = %d", setTrustResult); |
AzureIoTClient | 89:a2ed767a532e | 1017 | IoTHubClientCore_LL_Destroy(result); |
AzureIoTClient | 89:a2ed767a532e | 1018 | result = NULL; |
AzureIoTClient | 89:a2ed767a532e | 1019 | } |
AzureIoTClient | 89:a2ed767a532e | 1020 | |
AzureIoTClient | 89:a2ed767a532e | 1021 | free(trustedCertificate); |
AzureIoTClient | 89:a2ed767a532e | 1022 | } |
AzureIoTClient | 89:a2ed767a532e | 1023 | return result; |
AzureIoTClient | 89:a2ed767a532e | 1024 | } |
AzureIoTClient | 89:a2ed767a532e | 1025 | #endif |
AzureIoTClient | 89:a2ed767a532e | 1026 | |
AzureIoTClient | 89:a2ed767a532e | 1027 | |
AzureIoTClient | 88:248736be106e | 1028 | IOTHUB_CLIENT_CORE_LL_HANDLE IoTHubClientCore_LL_CreateWithTransport(const IOTHUB_CLIENT_DEVICE_CONFIG * config) |
AzureIoTClient | 88:248736be106e | 1029 | { |
AzureIoTClient | 88:248736be106e | 1030 | IOTHUB_CLIENT_CORE_LL_HANDLE result; |
AzureIoTClient | 88:248736be106e | 1031 | /*Codes_SRS_IOTHUBCLIENT_LL_17_001: [IoTHubClientCore_LL_CreateWithTransport shall return NULL if config parameter is NULL, or protocol field is NULL or transportHandle is NULL.]*/ |
AzureIoTClient | 88:248736be106e | 1032 | if ( |
AzureIoTClient | 88:248736be106e | 1033 | (config == NULL) || |
AzureIoTClient | 88:248736be106e | 1034 | (config->protocol == NULL) || |
AzureIoTClient | 88:248736be106e | 1035 | (config->transportHandle == NULL) || |
AzureIoTClient | 88:248736be106e | 1036 | /*Codes_SRS_IOTHUBCLIENT_LL_02_098: [ IoTHubClientCore_LL_CreateWithTransport shall fail and return NULL if both config->deviceKey AND config->deviceSasToken are NULL. ]*/ |
AzureIoTClient | 88:248736be106e | 1037 | ((config->deviceKey == NULL) && (config->deviceSasToken == NULL)) |
AzureIoTClient | 88:248736be106e | 1038 | ) |
AzureIoTClient | 88:248736be106e | 1039 | { |
AzureIoTClient | 88:248736be106e | 1040 | result = NULL; |
AzureIoTClient | 88:248736be106e | 1041 | LogError("invalid configuration (NULL detected)"); |
AzureIoTClient | 88:248736be106e | 1042 | } |
AzureIoTClient | 88:248736be106e | 1043 | else |
AzureIoTClient | 88:248736be106e | 1044 | { |
AzureIoTClient | 89:a2ed767a532e | 1045 | result = initialize_iothub_client(NULL, config, false, NULL); |
AzureIoTClient | 88:248736be106e | 1046 | } |
AzureIoTClient | 88:248736be106e | 1047 | return result; |
AzureIoTClient | 88:248736be106e | 1048 | } |
AzureIoTClient | 88:248736be106e | 1049 | |
AzureIoTClient | 88:248736be106e | 1050 | void IoTHubClientCore_LL_Destroy(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 88:248736be106e | 1051 | { |
AzureIoTClient | 88:248736be106e | 1052 | /*Codes_SRS_IOTHUBCLIENT_LL_02_009: [IoTHubClientCore_LL_Destroy shall do nothing if parameter iotHubClientHandle is NULL.]*/ |
AzureIoTClient | 88:248736be106e | 1053 | if (iotHubClientHandle != NULL) |
AzureIoTClient | 88:248736be106e | 1054 | { |
AzureIoTClient | 88:248736be106e | 1055 | PDLIST_ENTRY unsend; |
AzureIoTClient | 88:248736be106e | 1056 | /*Codes_SRS_IOTHUBCLIENT_LL_17_010: [IoTHubClientCore_LL_Destroy shall call the underlaying layer's _Unregister function] */ |
AzureIoTClient | 88:248736be106e | 1057 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1058 | handleData->IoTHubTransport_Unregister(handleData->deviceHandle); |
AzureIoTClient | 88:248736be106e | 1059 | if (handleData->isSharedTransport == false) |
AzureIoTClient | 88:248736be106e | 1060 | { |
AzureIoTClient | 88:248736be106e | 1061 | /*Codes_SRS_IOTHUBCLIENT_LL_02_010: [If iotHubClientHandle was not created by IoTHubClientCore_LL_CreateWithTransport, IoTHubClientCore_LL_Destroy shall call the underlaying layer's _Destroy function.] */ |
AzureIoTClient | 88:248736be106e | 1062 | handleData->IoTHubTransport_Destroy(handleData->transportHandle); |
AzureIoTClient | 88:248736be106e | 1063 | } |
AzureIoTClient | 88:248736be106e | 1064 | /*if any, remove the items currently not send*/ |
AzureIoTClient | 88:248736be106e | 1065 | while ((unsend = DList_RemoveHeadList(&(handleData->waitingToSend))) != &(handleData->waitingToSend)) |
AzureIoTClient | 88:248736be106e | 1066 | { |
AzureIoTClient | 88:248736be106e | 1067 | IOTHUB_MESSAGE_LIST* temp = containingRecord(unsend, IOTHUB_MESSAGE_LIST, entry); |
AzureIoTClient | 88:248736be106e | 1068 | /*Codes_SRS_IOTHUBCLIENT_LL_02_033: [Otherwise, IoTHubClientCore_LL_Destroy shall complete all the event message callbacks that are in the waitingToSend list with the result IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY.] */ |
AzureIoTClient | 88:248736be106e | 1069 | if (temp->callback != NULL) |
AzureIoTClient | 88:248736be106e | 1070 | { |
AzureIoTClient | 88:248736be106e | 1071 | temp->callback(IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY, temp->context); |
AzureIoTClient | 88:248736be106e | 1072 | } |
AzureIoTClient | 88:248736be106e | 1073 | IoTHubMessage_Destroy(temp->messageHandle); |
AzureIoTClient | 88:248736be106e | 1074 | free(temp); |
AzureIoTClient | 88:248736be106e | 1075 | } |
AzureIoTClient | 88:248736be106e | 1076 | |
AzureIoTClient | 88:248736be106e | 1077 | /* Codes_SRS_IOTHUBCLIENT_LL_07_007: [ IoTHubClientCore_LL_Destroy shall iterate the device twin queues and destroy any remaining items. ] */ |
AzureIoTClient | 88:248736be106e | 1078 | while ((unsend = DList_RemoveHeadList(&(handleData->iot_msg_queue))) != &(handleData->iot_msg_queue)) |
AzureIoTClient | 88:248736be106e | 1079 | { |
AzureIoTClient | 88:248736be106e | 1080 | IOTHUB_DEVICE_TWIN* temp = containingRecord(unsend, IOTHUB_DEVICE_TWIN, entry); |
AzureIoTClient | 88:248736be106e | 1081 | device_twin_data_destroy(temp); |
AzureIoTClient | 88:248736be106e | 1082 | } |
AzureIoTClient | 88:248736be106e | 1083 | while ((unsend = DList_RemoveHeadList(&(handleData->iot_ack_queue))) != &(handleData->iot_ack_queue)) |
AzureIoTClient | 88:248736be106e | 1084 | { |
AzureIoTClient | 88:248736be106e | 1085 | IOTHUB_DEVICE_TWIN* temp = containingRecord(unsend, IOTHUB_DEVICE_TWIN, entry); |
AzureIoTClient | 88:248736be106e | 1086 | device_twin_data_destroy(temp); |
AzureIoTClient | 88:248736be106e | 1087 | } |
AzureIoTClient | 88:248736be106e | 1088 | |
AzureIoTClient | 89:a2ed767a532e | 1089 | /* Codes_SRS_IOTHUBCLIENT_LL_31_141: [ IoTHubClient_LL_Destroy shall iterate registered callbacks for input queues and destroy any remaining items. ] */ |
AzureIoTClient | 89:a2ed767a532e | 1090 | delete_event_callback_list(handleData); |
AzureIoTClient | 89:a2ed767a532e | 1091 | |
AzureIoTClient | 88:248736be106e | 1092 | /*Codes_SRS_IOTHUBCLIENT_LL_17_011: [IoTHubClientCore_LL_Destroy shall free the resources allocated by IoTHubClient (if any).] */ |
AzureIoTClient | 88:248736be106e | 1093 | IoTHubClient_Auth_Destroy(handleData->authorization_module); |
AzureIoTClient | 88:248736be106e | 1094 | tickcounter_destroy(handleData->tickCounter); |
AzureIoTClient | 88:248736be106e | 1095 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 88:248736be106e | 1096 | IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle); |
AzureIoTClient | 88:248736be106e | 1097 | #endif |
AzureIoTClient | 88:248736be106e | 1098 | STRING_delete(handleData->product_info); |
AzureIoTClient | 88:248736be106e | 1099 | free(handleData); |
AzureIoTClient | 88:248736be106e | 1100 | } |
AzureIoTClient | 88:248736be106e | 1101 | } |
AzureIoTClient | 88:248736be106e | 1102 | |
AzureIoTClient | 88:248736be106e | 1103 | /*Codes_SRS_IOTHUBCLIENT_LL_02_044: [ Messages already delivered to IoTHubClientCore_LL shall not have their timeouts modified by a new call to IoTHubClientCore_LL_SetOption. ]*/ |
AzureIoTClient | 88:248736be106e | 1104 | /*returns 0 on success, any other value is error*/ |
AzureIoTClient | 88:248736be106e | 1105 | static int attach_ms_timesOutAfter(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData, IOTHUB_MESSAGE_LIST *newEntry) |
AzureIoTClient | 88:248736be106e | 1106 | { |
AzureIoTClient | 88:248736be106e | 1107 | int result; |
AzureIoTClient | 88:248736be106e | 1108 | /*Codes_SRS_IOTHUBCLIENT_LL_02_043: [ Calling IoTHubClientCore_LL_SetOption with value set to "0" shall disable the timeout mechanism for all new messages. ]*/ |
AzureIoTClient | 88:248736be106e | 1109 | if (handleData->currentMessageTimeout == 0) |
AzureIoTClient | 88:248736be106e | 1110 | { |
AzureIoTClient | 88:248736be106e | 1111 | newEntry->ms_timesOutAfter = 0; /*do not timeout*/ |
AzureIoTClient | 88:248736be106e | 1112 | result = 0; |
AzureIoTClient | 88:248736be106e | 1113 | } |
AzureIoTClient | 88:248736be106e | 1114 | else |
AzureIoTClient | 88:248736be106e | 1115 | { |
AzureIoTClient | 88:248736be106e | 1116 | /*Codes_SRS_IOTHUBCLIENT_LL_02_039: [ "messageTimeout" - once IoTHubClientCore_LL_SendEventAsync is called the message shall timeout after value miliseconds. Value is a pointer to a tickcounter_ms_t. ]*/ |
AzureIoTClient | 88:248736be106e | 1117 | if (tickcounter_get_current_ms(handleData->tickCounter, &newEntry->ms_timesOutAfter) != 0) |
AzureIoTClient | 88:248736be106e | 1118 | { |
AzureIoTClient | 88:248736be106e | 1119 | result = __FAILURE__; |
AzureIoTClient | 88:248736be106e | 1120 | LogError("unable to get the current relative tickcount"); |
AzureIoTClient | 88:248736be106e | 1121 | } |
AzureIoTClient | 88:248736be106e | 1122 | else |
AzureIoTClient | 88:248736be106e | 1123 | { |
AzureIoTClient | 88:248736be106e | 1124 | newEntry->ms_timesOutAfter += handleData->currentMessageTimeout; |
AzureIoTClient | 88:248736be106e | 1125 | result = 0; |
AzureIoTClient | 88:248736be106e | 1126 | } |
AzureIoTClient | 88:248736be106e | 1127 | } |
AzureIoTClient | 88:248736be106e | 1128 | return result; |
AzureIoTClient | 88:248736be106e | 1129 | } |
AzureIoTClient | 88:248736be106e | 1130 | |
AzureIoTClient | 88:248736be106e | 1131 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SendEventAsync(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_MESSAGE_HANDLE eventMessageHandle, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK eventConfirmationCallback, void* userContextCallback) |
AzureIoTClient | 88:248736be106e | 1132 | { |
AzureIoTClient | 88:248736be106e | 1133 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 1134 | /*Codes_SRS_IOTHUBCLIENT_LL_02_011: [IoTHubClientCore_LL_SendEventAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle or eventMessageHandle is NULL.]*/ |
AzureIoTClient | 88:248736be106e | 1135 | if ( |
AzureIoTClient | 88:248736be106e | 1136 | (iotHubClientHandle == NULL) || |
AzureIoTClient | 88:248736be106e | 1137 | (eventMessageHandle == NULL) || |
AzureIoTClient | 88:248736be106e | 1138 | /*Codes_SRS_IOTHUBCLIENT_LL_02_012: [IoTHubClientCore_LL_SendEventAsync shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter eventConfirmationCallback is NULL and userContextCallback is not NULL.] */ |
AzureIoTClient | 88:248736be106e | 1139 | ((eventConfirmationCallback == NULL) && (userContextCallback != NULL)) |
AzureIoTClient | 88:248736be106e | 1140 | ) |
AzureIoTClient | 88:248736be106e | 1141 | { |
AzureIoTClient | 88:248736be106e | 1142 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 1143 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1144 | } |
AzureIoTClient | 88:248736be106e | 1145 | else |
AzureIoTClient | 88:248736be106e | 1146 | { |
AzureIoTClient | 88:248736be106e | 1147 | IOTHUB_MESSAGE_LIST *newEntry = (IOTHUB_MESSAGE_LIST*)malloc(sizeof(IOTHUB_MESSAGE_LIST)); |
AzureIoTClient | 88:248736be106e | 1148 | if (newEntry == NULL) |
AzureIoTClient | 88:248736be106e | 1149 | { |
AzureIoTClient | 88:248736be106e | 1150 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1151 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1152 | } |
AzureIoTClient | 88:248736be106e | 1153 | else |
AzureIoTClient | 88:248736be106e | 1154 | { |
AzureIoTClient | 88:248736be106e | 1155 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1156 | |
AzureIoTClient | 88:248736be106e | 1157 | if (attach_ms_timesOutAfter(handleData, newEntry) != 0) |
AzureIoTClient | 88:248736be106e | 1158 | { |
AzureIoTClient | 88:248736be106e | 1159 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1160 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1161 | free(newEntry); |
AzureIoTClient | 88:248736be106e | 1162 | } |
AzureIoTClient | 88:248736be106e | 1163 | else |
AzureIoTClient | 88:248736be106e | 1164 | { |
AzureIoTClient | 88:248736be106e | 1165 | /*Codes_SRS_IOTHUBCLIENT_LL_02_013: [IoTHubClientCore_LL_SendEventAsync shall add the DLIST waitingToSend a new record cloning the information from eventMessageHandle, eventConfirmationCallback, userContextCallback.]*/ |
AzureIoTClient | 88:248736be106e | 1166 | if ((newEntry->messageHandle = IoTHubMessage_Clone(eventMessageHandle)) == NULL) |
AzureIoTClient | 88:248736be106e | 1167 | { |
AzureIoTClient | 88:248736be106e | 1168 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1169 | free(newEntry); |
AzureIoTClient | 88:248736be106e | 1170 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1171 | } |
AzureIoTClient | 88:248736be106e | 1172 | else if (IoTHubClient_Diagnostic_AddIfNecessary(&handleData->diagnostic_setting, newEntry->messageHandle) != 0) |
AzureIoTClient | 88:248736be106e | 1173 | { |
AzureIoTClient | 88:248736be106e | 1174 | /*Codes_SRS_IOTHUBCLIENT_LL_02_014: [If cloning and/or adding the information/diagnostic fails for any reason, IoTHubClientCore_LL_SendEventAsync shall fail and return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 88:248736be106e | 1175 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1176 | IoTHubMessage_Destroy(newEntry->messageHandle); |
AzureIoTClient | 88:248736be106e | 1177 | free(newEntry); |
AzureIoTClient | 88:248736be106e | 1178 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1179 | } |
AzureIoTClient | 88:248736be106e | 1180 | else |
AzureIoTClient | 88:248736be106e | 1181 | { |
AzureIoTClient | 88:248736be106e | 1182 | /*Codes_SRS_IOTHUBCLIENT_LL_02_013: [IoTHubClientCore_LL_SendEventAsync shall add the DLIST waitingToSend a new record cloning the information from eventMessageHandle, eventConfirmationCallback, userContextCallback.]*/ |
AzureIoTClient | 88:248736be106e | 1183 | newEntry->callback = eventConfirmationCallback; |
AzureIoTClient | 88:248736be106e | 1184 | newEntry->context = userContextCallback; |
AzureIoTClient | 88:248736be106e | 1185 | DList_InsertTailList(&(iotHubClientHandle->waitingToSend), &(newEntry->entry)); |
AzureIoTClient | 88:248736be106e | 1186 | /*Codes_SRS_IOTHUBCLIENT_LL_02_015: [Otherwise IoTHubClientCore_LL_SendEventAsync shall succeed and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 88:248736be106e | 1187 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1188 | } |
AzureIoTClient | 88:248736be106e | 1189 | } |
AzureIoTClient | 88:248736be106e | 1190 | } |
AzureIoTClient | 88:248736be106e | 1191 | } |
AzureIoTClient | 88:248736be106e | 1192 | return result; |
AzureIoTClient | 88:248736be106e | 1193 | } |
AzureIoTClient | 88:248736be106e | 1194 | |
AzureIoTClient | 88:248736be106e | 1195 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetMessageCallback(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback) |
AzureIoTClient | 88:248736be106e | 1196 | { |
AzureIoTClient | 88:248736be106e | 1197 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 1198 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 88:248736be106e | 1199 | { |
AzureIoTClient | 88:248736be106e | 1200 | /*Codes_SRS_IOTHUBCLIENT_LL_02_016: [IoTHubClientCore_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.] */ |
AzureIoTClient | 88:248736be106e | 1201 | LogError("Invalid argument - iotHubClientHandle is NULL"); |
AzureIoTClient | 88:248736be106e | 1202 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 1203 | } |
AzureIoTClient | 88:248736be106e | 1204 | else |
AzureIoTClient | 88:248736be106e | 1205 | { |
AzureIoTClient | 88:248736be106e | 1206 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1207 | if (messageCallback == NULL) |
AzureIoTClient | 88:248736be106e | 1208 | { |
AzureIoTClient | 88:248736be106e | 1209 | if (handleData->messageCallback.type == CALLBACK_TYPE_NONE) |
AzureIoTClient | 88:248736be106e | 1210 | { |
AzureIoTClient | 88:248736be106e | 1211 | /*Codes_SRS_IOTHUBCLIENT_LL_10_010: [If parameter messageCallback is NULL and the _SetMessageCallback had not been called to subscribe for messages, then IoTHubClientCore_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 88:248736be106e | 1212 | LogError("not currently set to accept or process incoming messages."); |
AzureIoTClient | 88:248736be106e | 1213 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1214 | } |
AzureIoTClient | 88:248736be106e | 1215 | else if (handleData->messageCallback.type == CALLBACK_TYPE_ASYNC) |
AzureIoTClient | 88:248736be106e | 1216 | { |
AzureIoTClient | 88:248736be106e | 1217 | /*Codes_SRS_IOTHUBCLIENT_LL_10_010: [If parameter messageCallback is NULL and the _SetMessageCallback had not been called to subscribe for messages, then IoTHubClientCore_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 88:248736be106e | 1218 | LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClientCore_LL_SetMessageCallback_Ex function."); |
AzureIoTClient | 88:248736be106e | 1219 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1220 | } |
AzureIoTClient | 88:248736be106e | 1221 | else |
AzureIoTClient | 88:248736be106e | 1222 | { |
AzureIoTClient | 88:248736be106e | 1223 | /*Codes_SRS_IOTHUBCLIENT_LL_02_019: [If parameter messageCallback is NULL then IoTHubClientCore_LL_SetMessageCallback shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 88:248736be106e | 1224 | handleData->IoTHubTransport_Unsubscribe(handleData->deviceHandle); |
AzureIoTClient | 88:248736be106e | 1225 | handleData->messageCallback.type = CALLBACK_TYPE_NONE; |
AzureIoTClient | 88:248736be106e | 1226 | handleData->messageCallback.callbackSync = NULL; |
AzureIoTClient | 88:248736be106e | 1227 | handleData->messageCallback.callbackAsync = NULL; |
AzureIoTClient | 88:248736be106e | 1228 | handleData->messageCallback.userContextCallback = NULL; |
AzureIoTClient | 88:248736be106e | 1229 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1230 | } |
AzureIoTClient | 88:248736be106e | 1231 | } |
AzureIoTClient | 88:248736be106e | 1232 | else |
AzureIoTClient | 88:248736be106e | 1233 | { |
AzureIoTClient | 88:248736be106e | 1234 | if (handleData->messageCallback.type == CALLBACK_TYPE_ASYNC) |
AzureIoTClient | 88:248736be106e | 1235 | { |
AzureIoTClient | 88:248736be106e | 1236 | /* Codes_SRS_IOTHUBCLIENT_LL_10_011: [If parameter messageCallback is non-NULL and the _SetMessageCallback_Ex had been used to susbscribe for messages, then IoTHubClientCore_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 88:248736be106e | 1237 | LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClientCore_LL_SetMessageCallback_Ex function before subscribing with MessageCallback."); |
AzureIoTClient | 88:248736be106e | 1238 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1239 | } |
AzureIoTClient | 88:248736be106e | 1240 | else |
AzureIoTClient | 88:248736be106e | 1241 | { |
AzureIoTClient | 88:248736be106e | 1242 | if (handleData->IoTHubTransport_Subscribe(handleData->deviceHandle) == 0) |
AzureIoTClient | 88:248736be106e | 1243 | { |
AzureIoTClient | 88:248736be106e | 1244 | /*Codes_SRS_IOTHUBCLIENT_LL_02_017: [If parameter messageCallback is non-NULL then IoTHubClientCore_LL_SetMessageCallback shall call the underlying layer's _Subscribe function.]*/ |
AzureIoTClient | 88:248736be106e | 1245 | handleData->messageCallback.type = CALLBACK_TYPE_SYNC; |
AzureIoTClient | 88:248736be106e | 1246 | handleData->messageCallback.callbackSync = messageCallback; |
AzureIoTClient | 88:248736be106e | 1247 | handleData->messageCallback.userContextCallback = userContextCallback; |
AzureIoTClient | 88:248736be106e | 1248 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1249 | } |
AzureIoTClient | 88:248736be106e | 1250 | else |
AzureIoTClient | 88:248736be106e | 1251 | { |
AzureIoTClient | 88:248736be106e | 1252 | /*Codes_SRS_IOTHUBCLIENT_LL_02_018: [If the underlying layer's _Subscribe function fails, then IoTHubClientCore_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_ERROR. Otherwise IoTHubClientCore_LL_SetMessageCallback shall succeed and return IOTHUB_CLIENT_OK.]*/ |
AzureIoTClient | 88:248736be106e | 1253 | LogError("IoTHubTransport_Subscribe failed"); |
AzureIoTClient | 88:248736be106e | 1254 | handleData->messageCallback.type = CALLBACK_TYPE_NONE; |
AzureIoTClient | 88:248736be106e | 1255 | handleData->messageCallback.callbackSync = NULL; |
AzureIoTClient | 88:248736be106e | 1256 | handleData->messageCallback.callbackAsync = NULL; |
AzureIoTClient | 88:248736be106e | 1257 | handleData->messageCallback.userContextCallback = NULL; |
AzureIoTClient | 88:248736be106e | 1258 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1259 | } |
AzureIoTClient | 88:248736be106e | 1260 | } |
AzureIoTClient | 88:248736be106e | 1261 | } |
AzureIoTClient | 88:248736be106e | 1262 | } |
AzureIoTClient | 88:248736be106e | 1263 | return result; |
AzureIoTClient | 88:248736be106e | 1264 | } |
AzureIoTClient | 88:248736be106e | 1265 | |
AzureIoTClient | 88:248736be106e | 1266 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetMessageCallback_Ex(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC_EX messageCallback, void* userContextCallback) |
AzureIoTClient | 88:248736be106e | 1267 | { |
AzureIoTClient | 88:248736be106e | 1268 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 1269 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 88:248736be106e | 1270 | { |
AzureIoTClient | 88:248736be106e | 1271 | /*Codes_SRS_IOTHUBCLIENT_LL_10_021: [IoTHubClientCore_LL_SetMessageCallback_Ex shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.]*/ |
AzureIoTClient | 88:248736be106e | 1272 | LogError("Invalid argument - iotHubClientHandle is NULL"); |
AzureIoTClient | 88:248736be106e | 1273 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 1274 | } |
AzureIoTClient | 88:248736be106e | 1275 | else |
AzureIoTClient | 88:248736be106e | 1276 | { |
AzureIoTClient | 88:248736be106e | 1277 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1278 | if (messageCallback == NULL) |
AzureIoTClient | 88:248736be106e | 1279 | { |
AzureIoTClient | 88:248736be106e | 1280 | if (handleData->messageCallback.type == CALLBACK_TYPE_NONE) |
AzureIoTClient | 88:248736be106e | 1281 | { |
AzureIoTClient | 88:248736be106e | 1282 | /*Codes_SRS_IOTHUBCLIENT_LL_10_018: [If parameter messageCallback is NULL and IoTHubClientCore_LL_SetMessageCallback_Ex had not been used to subscribe for messages, then IoTHubClientCore_LL_SetMessageCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 88:248736be106e | 1283 | LogError("not currently set to accept or process incoming messages."); |
AzureIoTClient | 88:248736be106e | 1284 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1285 | } |
AzureIoTClient | 88:248736be106e | 1286 | else if (handleData->messageCallback.type == CALLBACK_TYPE_SYNC) |
AzureIoTClient | 88:248736be106e | 1287 | { |
AzureIoTClient | 88:248736be106e | 1288 | /*Codes_SRS_IOTHUBCLIENT_LL_10_019: [If parameter messageCallback is NULL and IoTHubClientCore_LL_SetMessageCallback had been used to subscribe for messages, then IoTHubClientCore_LL_SetMessageCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 88:248736be106e | 1289 | LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClientCore_LL_SetMessageCallback function."); |
AzureIoTClient | 88:248736be106e | 1290 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1291 | } |
AzureIoTClient | 88:248736be106e | 1292 | else |
AzureIoTClient | 88:248736be106e | 1293 | { |
AzureIoTClient | 88:248736be106e | 1294 | /*Codes_SRS_IOTHUBCLIENT_LL_10_023: [If parameter messageCallback is NULL then IoTHubClientCore_LL_SetMessageCallback_Ex shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 88:248736be106e | 1295 | handleData->IoTHubTransport_Unsubscribe(handleData->deviceHandle); |
AzureIoTClient | 88:248736be106e | 1296 | handleData->messageCallback.type = CALLBACK_TYPE_NONE; |
AzureIoTClient | 88:248736be106e | 1297 | handleData->messageCallback.callbackSync = NULL; |
AzureIoTClient | 88:248736be106e | 1298 | handleData->messageCallback.callbackAsync = NULL; |
AzureIoTClient | 88:248736be106e | 1299 | handleData->messageCallback.userContextCallback = NULL; |
AzureIoTClient | 88:248736be106e | 1300 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1301 | } |
AzureIoTClient | 88:248736be106e | 1302 | } |
AzureIoTClient | 88:248736be106e | 1303 | else |
AzureIoTClient | 88:248736be106e | 1304 | { |
AzureIoTClient | 88:248736be106e | 1305 | if (handleData->messageCallback.type == CALLBACK_TYPE_SYNC) |
AzureIoTClient | 88:248736be106e | 1306 | { |
AzureIoTClient | 88:248736be106e | 1307 | /*Codes_SRS_IOTHUBCLIENT_LL_10_020: [If parameter messageCallback is non-NULL, and IoTHubClientCore_LL_SetMessageCallback had been used to subscribe for messages, then IoTHubClientCore_LL_SetMessageCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 88:248736be106e | 1308 | LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClientCore_LL_MessageCallbackEx function before subscribing with MessageCallback."); |
AzureIoTClient | 88:248736be106e | 1309 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1310 | } |
AzureIoTClient | 88:248736be106e | 1311 | else |
AzureIoTClient | 88:248736be106e | 1312 | { |
AzureIoTClient | 88:248736be106e | 1313 | if (handleData->IoTHubTransport_Subscribe(handleData->deviceHandle) == 0) |
AzureIoTClient | 88:248736be106e | 1314 | { |
AzureIoTClient | 88:248736be106e | 1315 | /*Codes_SRS_IOTHUBCLIENT_LL_10_024: [If parameter messageCallback is non-NULL then IoTHubClientCore_LL_SetMessageCallback_Ex shall call the underlying layer's _Subscribe function.]*/ |
AzureIoTClient | 88:248736be106e | 1316 | handleData->messageCallback.type = CALLBACK_TYPE_ASYNC; |
AzureIoTClient | 88:248736be106e | 1317 | handleData->messageCallback.callbackAsync = messageCallback; |
AzureIoTClient | 88:248736be106e | 1318 | handleData->messageCallback.userContextCallback = userContextCallback; |
AzureIoTClient | 88:248736be106e | 1319 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1320 | } |
AzureIoTClient | 88:248736be106e | 1321 | else |
AzureIoTClient | 88:248736be106e | 1322 | { |
AzureIoTClient | 88:248736be106e | 1323 | /*Codes_SRS_IOTHUBCLIENT_LL_10_025: [If the underlying layer's _Subscribe function fails, then IoTHubClientCore_LL_SetMessageCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR. Otherwise IoTHubClientCore_LL_SetMessageCallback_Ex shall succeed and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 88:248736be106e | 1324 | LogError("IoTHubTransport_Subscribe failed"); |
AzureIoTClient | 88:248736be106e | 1325 | handleData->messageCallback.type = CALLBACK_TYPE_NONE; |
AzureIoTClient | 88:248736be106e | 1326 | handleData->messageCallback.callbackSync = NULL; |
AzureIoTClient | 88:248736be106e | 1327 | handleData->messageCallback.callbackAsync = NULL; |
AzureIoTClient | 88:248736be106e | 1328 | handleData->messageCallback.userContextCallback = NULL; |
AzureIoTClient | 88:248736be106e | 1329 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1330 | } |
AzureIoTClient | 88:248736be106e | 1331 | } |
AzureIoTClient | 88:248736be106e | 1332 | } |
AzureIoTClient | 88:248736be106e | 1333 | } |
AzureIoTClient | 88:248736be106e | 1334 | return result; |
AzureIoTClient | 88:248736be106e | 1335 | } |
AzureIoTClient | 88:248736be106e | 1336 | |
AzureIoTClient | 88:248736be106e | 1337 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SendMessageDisposition(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, MESSAGE_CALLBACK_INFO* message_data, IOTHUBMESSAGE_DISPOSITION_RESULT disposition) |
AzureIoTClient | 88:248736be106e | 1338 | { |
AzureIoTClient | 88:248736be106e | 1339 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 1340 | if ((iotHubClientHandle == NULL) || (message_data == NULL)) |
AzureIoTClient | 88:248736be106e | 1341 | { |
AzureIoTClient | 88:248736be106e | 1342 | /*Codes_SRS_IOTHUBCLIENT_LL_10_026: [IoTHubClientCore_LL_SendMessageDisposition shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.]*/ |
AzureIoTClient | 88:248736be106e | 1343 | LogError("Invalid argument handle=%p, message=%p", iotHubClientHandle, message_data); |
AzureIoTClient | 88:248736be106e | 1344 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 1345 | } |
AzureIoTClient | 88:248736be106e | 1346 | else |
AzureIoTClient | 88:248736be106e | 1347 | { |
AzureIoTClient | 88:248736be106e | 1348 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1349 | /*Codes_SRS_IOTHUBCLIENT_LL_10_027: [IoTHubClientCore_LL_SendMessageDisposition shall return the result from calling the underlying layer's _Send_Message_Disposition.]*/ |
AzureIoTClient | 88:248736be106e | 1350 | result = handleData->IoTHubTransport_SendMessageDisposition(message_data, disposition); |
AzureIoTClient | 88:248736be106e | 1351 | } |
AzureIoTClient | 88:248736be106e | 1352 | return result; |
AzureIoTClient | 88:248736be106e | 1353 | } |
AzureIoTClient | 88:248736be106e | 1354 | |
AzureIoTClient | 88:248736be106e | 1355 | static void DoTimeouts(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData) |
AzureIoTClient | 88:248736be106e | 1356 | { |
AzureIoTClient | 88:248736be106e | 1357 | tickcounter_ms_t nowTick; |
AzureIoTClient | 88:248736be106e | 1358 | if (tickcounter_get_current_ms(handleData->tickCounter, &nowTick) != 0) |
AzureIoTClient | 88:248736be106e | 1359 | { |
AzureIoTClient | 88:248736be106e | 1360 | LogError("unable to get the current ms, timeouts will not be processed"); |
AzureIoTClient | 88:248736be106e | 1361 | } |
AzureIoTClient | 88:248736be106e | 1362 | else |
AzureIoTClient | 88:248736be106e | 1363 | { |
AzureIoTClient | 88:248736be106e | 1364 | DLIST_ENTRY* currentItemInWaitingToSend = handleData->waitingToSend.Flink; |
AzureIoTClient | 88:248736be106e | 1365 | while (currentItemInWaitingToSend != &(handleData->waitingToSend)) /*while we are not at the end of the list*/ |
AzureIoTClient | 88:248736be106e | 1366 | { |
AzureIoTClient | 88:248736be106e | 1367 | IOTHUB_MESSAGE_LIST* fullEntry = containingRecord(currentItemInWaitingToSend, IOTHUB_MESSAGE_LIST, entry); |
AzureIoTClient | 88:248736be106e | 1368 | /*Codes_SRS_IOTHUBCLIENT_LL_02_041: [ If more than value miliseconds have passed since the call to IoTHubClientCore_LL_SendEventAsync then the message callback shall be called with a status code of IOTHUB_CLIENT_CONFIRMATION_TIMEOUT. ]*/ |
AzureIoTClient | 88:248736be106e | 1369 | if ((fullEntry->ms_timesOutAfter != 0) && (fullEntry->ms_timesOutAfter < nowTick)) |
AzureIoTClient | 88:248736be106e | 1370 | { |
AzureIoTClient | 88:248736be106e | 1371 | PDLIST_ENTRY theNext = currentItemInWaitingToSend->Flink; /*need to save the next item, because the below operations are destructive*/ |
AzureIoTClient | 88:248736be106e | 1372 | DList_RemoveEntryList(currentItemInWaitingToSend); |
AzureIoTClient | 88:248736be106e | 1373 | if (fullEntry->callback != NULL) |
AzureIoTClient | 88:248736be106e | 1374 | { |
AzureIoTClient | 88:248736be106e | 1375 | fullEntry->callback(IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT, fullEntry->context); |
AzureIoTClient | 88:248736be106e | 1376 | } |
AzureIoTClient | 88:248736be106e | 1377 | IoTHubMessage_Destroy(fullEntry->messageHandle); /*because it has been cloned*/ |
AzureIoTClient | 88:248736be106e | 1378 | free(fullEntry); |
AzureIoTClient | 88:248736be106e | 1379 | currentItemInWaitingToSend = theNext; |
AzureIoTClient | 88:248736be106e | 1380 | } |
AzureIoTClient | 88:248736be106e | 1381 | else |
AzureIoTClient | 88:248736be106e | 1382 | { |
AzureIoTClient | 88:248736be106e | 1383 | currentItemInWaitingToSend = currentItemInWaitingToSend->Flink; |
AzureIoTClient | 88:248736be106e | 1384 | } |
AzureIoTClient | 88:248736be106e | 1385 | } |
AzureIoTClient | 88:248736be106e | 1386 | } |
AzureIoTClient | 88:248736be106e | 1387 | } |
AzureIoTClient | 88:248736be106e | 1388 | |
AzureIoTClient | 88:248736be106e | 1389 | void IoTHubClientCore_LL_DoWork(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 88:248736be106e | 1390 | { |
AzureIoTClient | 88:248736be106e | 1391 | /*Codes_SRS_IOTHUBCLIENT_LL_02_020: [If parameter iotHubClientHandle is NULL then IoTHubClientCore_LL_DoWork shall not perform any action.] */ |
AzureIoTClient | 88:248736be106e | 1392 | if (iotHubClientHandle != NULL) |
AzureIoTClient | 88:248736be106e | 1393 | { |
AzureIoTClient | 88:248736be106e | 1394 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1395 | DoTimeouts(handleData); |
AzureIoTClient | 88:248736be106e | 1396 | |
AzureIoTClient | 88:248736be106e | 1397 | /*Codes_SRS_IOTHUBCLIENT_LL_07_008: [ IoTHubClientCore_LL_DoWork shall iterate the message queue and execute the underlying transports IoTHubTransport_ProcessItem function for each item. ] */ |
AzureIoTClient | 88:248736be106e | 1398 | DLIST_ENTRY* client_item = handleData->iot_msg_queue.Flink; |
AzureIoTClient | 88:248736be106e | 1399 | while (client_item != &(handleData->iot_msg_queue)) /*while we are not at the end of the list*/ |
AzureIoTClient | 88:248736be106e | 1400 | { |
AzureIoTClient | 88:248736be106e | 1401 | PDLIST_ENTRY next_item = client_item->Flink; |
AzureIoTClient | 88:248736be106e | 1402 | |
AzureIoTClient | 88:248736be106e | 1403 | IOTHUB_DEVICE_TWIN* queue_data = containingRecord(client_item, IOTHUB_DEVICE_TWIN, entry); |
AzureIoTClient | 88:248736be106e | 1404 | IOTHUB_IDENTITY_INFO identity_info; |
AzureIoTClient | 88:248736be106e | 1405 | identity_info.device_twin = queue_data; |
AzureIoTClient | 88:248736be106e | 1406 | IOTHUB_PROCESS_ITEM_RESULT process_results = handleData->IoTHubTransport_ProcessItem(handleData->transportHandle, IOTHUB_TYPE_DEVICE_TWIN, &identity_info); |
AzureIoTClient | 88:248736be106e | 1407 | if (process_results == IOTHUB_PROCESS_CONTINUE || process_results == IOTHUB_PROCESS_NOT_CONNECTED) |
AzureIoTClient | 88:248736be106e | 1408 | { |
AzureIoTClient | 88:248736be106e | 1409 | /*Codes_SRS_IOTHUBCLIENT_LL_07_010: [ If 'IoTHubTransport_ProcessItem' returns IOTHUB_PROCESS_CONTINUE or IOTHUB_PROCESS_NOT_CONNECTED IoTHubClientCore_LL_DoWork shall continue on to call the underlaying layer's _DoWork function. ]*/ |
AzureIoTClient | 88:248736be106e | 1410 | break; |
AzureIoTClient | 88:248736be106e | 1411 | } |
AzureIoTClient | 88:248736be106e | 1412 | else |
AzureIoTClient | 88:248736be106e | 1413 | { |
AzureIoTClient | 88:248736be106e | 1414 | DList_RemoveEntryList(client_item); |
AzureIoTClient | 88:248736be106e | 1415 | if (process_results == IOTHUB_PROCESS_OK) |
AzureIoTClient | 88:248736be106e | 1416 | { |
AzureIoTClient | 88:248736be106e | 1417 | /*Codes_SRS_IOTHUBCLIENT_LL_07_011: [ If 'IoTHubTransport_ProcessItem' returns IOTHUB_PROCESS_OK IoTHubClientCore_LL_DoWork shall add the IOTHUB_DEVICE_TWIN to the ack queue. ]*/ |
AzureIoTClient | 88:248736be106e | 1418 | DList_InsertTailList(&(iotHubClientHandle->iot_ack_queue), &(queue_data->entry)); |
AzureIoTClient | 88:248736be106e | 1419 | } |
AzureIoTClient | 88:248736be106e | 1420 | else |
AzureIoTClient | 88:248736be106e | 1421 | { |
AzureIoTClient | 88:248736be106e | 1422 | /*Codes_SRS_IOTHUBCLIENT_LL_07_012: [ If 'IoTHubTransport_ProcessItem' returns any other value IoTHubClientCore_LL_DoWork shall destroy the IOTHUB_DEVICE_TWIN item. ]*/ |
AzureIoTClient | 88:248736be106e | 1423 | LogError("Failure queue processing item"); |
AzureIoTClient | 88:248736be106e | 1424 | device_twin_data_destroy(queue_data); |
AzureIoTClient | 88:248736be106e | 1425 | } |
AzureIoTClient | 88:248736be106e | 1426 | } |
AzureIoTClient | 88:248736be106e | 1427 | // Move along to the next item |
AzureIoTClient | 88:248736be106e | 1428 | client_item = next_item; |
AzureIoTClient | 88:248736be106e | 1429 | } |
AzureIoTClient | 88:248736be106e | 1430 | |
AzureIoTClient | 88:248736be106e | 1431 | /*Codes_SRS_IOTHUBCLIENT_LL_02_021: [Otherwise, IoTHubClientCore_LL_DoWork shall invoke the underlaying layer's _DoWork function.]*/ |
AzureIoTClient | 88:248736be106e | 1432 | handleData->IoTHubTransport_DoWork(handleData->transportHandle, iotHubClientHandle); |
AzureIoTClient | 88:248736be106e | 1433 | } |
AzureIoTClient | 88:248736be106e | 1434 | } |
AzureIoTClient | 88:248736be106e | 1435 | |
AzureIoTClient | 88:248736be106e | 1436 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_GetSendStatus(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus) |
AzureIoTClient | 88:248736be106e | 1437 | { |
AzureIoTClient | 88:248736be106e | 1438 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 1439 | |
AzureIoTClient | 88:248736be106e | 1440 | /* Codes_SRS_IOTHUBCLIENT_09_007: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter] */ |
AzureIoTClient | 88:248736be106e | 1441 | if (iotHubClientHandle == NULL || iotHubClientStatus == NULL) |
AzureIoTClient | 88:248736be106e | 1442 | { |
AzureIoTClient | 88:248736be106e | 1443 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 1444 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1445 | } |
AzureIoTClient | 88:248736be106e | 1446 | else |
AzureIoTClient | 88:248736be106e | 1447 | { |
AzureIoTClient | 88:248736be106e | 1448 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1449 | |
AzureIoTClient | 88:248736be106e | 1450 | /* 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 | 88:248736be106e | 1451 | /* 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 | 88:248736be106e | 1452 | result = handleData->IoTHubTransport_GetSendStatus(handleData->deviceHandle, iotHubClientStatus); |
AzureIoTClient | 88:248736be106e | 1453 | } |
AzureIoTClient | 88:248736be106e | 1454 | |
AzureIoTClient | 88:248736be106e | 1455 | return result; |
AzureIoTClient | 88:248736be106e | 1456 | } |
AzureIoTClient | 88:248736be106e | 1457 | |
AzureIoTClient | 88:248736be106e | 1458 | void IoTHubClientCore_LL_SendComplete(IOTHUB_CLIENT_CORE_LL_HANDLE handle, PDLIST_ENTRY completed, IOTHUB_CLIENT_CONFIRMATION_RESULT result) |
AzureIoTClient | 88:248736be106e | 1459 | { |
AzureIoTClient | 88:248736be106e | 1460 | /*Codes_SRS_IOTHUBCLIENT_LL_02_022: [If parameter completed is NULL, or parameter handle is NULL then IoTHubClientCore_LL_SendBatch shall return.]*/ |
AzureIoTClient | 88:248736be106e | 1461 | if ( |
AzureIoTClient | 88:248736be106e | 1462 | (handle == NULL) || |
AzureIoTClient | 88:248736be106e | 1463 | (completed == NULL) |
AzureIoTClient | 88:248736be106e | 1464 | ) |
AzureIoTClient | 88:248736be106e | 1465 | { |
AzureIoTClient | 88:248736be106e | 1466 | /*"shall return"*/ |
AzureIoTClient | 88:248736be106e | 1467 | LogError("invalid arg"); |
AzureIoTClient | 88:248736be106e | 1468 | } |
AzureIoTClient | 88:248736be106e | 1469 | else |
AzureIoTClient | 88:248736be106e | 1470 | { |
AzureIoTClient | 88:248736be106e | 1471 | /*Codes_SRS_IOTHUBCLIENT_LL_02_027: [If parameter result is IOTHUB_CLIENT_CONFIRMATION_ERROR then IoTHubClientCore_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.] */ |
AzureIoTClient | 88:248736be106e | 1472 | /*Codes_SRS_IOTHUBCLIENT_LL_02_025: [If parameter result is IOTHUB_CLIENT_CONFIRMATION_OK then IoTHubClientCore_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 | 88:248736be106e | 1473 | PDLIST_ENTRY oldest; |
AzureIoTClient | 88:248736be106e | 1474 | while ((oldest = DList_RemoveHeadList(completed)) != completed) |
AzureIoTClient | 88:248736be106e | 1475 | { |
AzureIoTClient | 88:248736be106e | 1476 | IOTHUB_MESSAGE_LIST* messageList = (IOTHUB_MESSAGE_LIST*)containingRecord(oldest, IOTHUB_MESSAGE_LIST, entry); |
AzureIoTClient | 88:248736be106e | 1477 | /*Codes_SRS_IOTHUBCLIENT_LL_02_026: [If any callback is NULL then there shall not be a callback call.]*/ |
AzureIoTClient | 88:248736be106e | 1478 | if (messageList->callback != NULL) |
AzureIoTClient | 88:248736be106e | 1479 | { |
AzureIoTClient | 88:248736be106e | 1480 | messageList->callback(result, messageList->context); |
AzureIoTClient | 88:248736be106e | 1481 | } |
AzureIoTClient | 88:248736be106e | 1482 | IoTHubMessage_Destroy(messageList->messageHandle); |
AzureIoTClient | 88:248736be106e | 1483 | free(messageList); |
AzureIoTClient | 88:248736be106e | 1484 | } |
AzureIoTClient | 88:248736be106e | 1485 | } |
AzureIoTClient | 88:248736be106e | 1486 | } |
AzureIoTClient | 88:248736be106e | 1487 | |
AzureIoTClient | 88:248736be106e | 1488 | int IoTHubClientCore_LL_DeviceMethodComplete(IOTHUB_CLIENT_CORE_LL_HANDLE handle, const char* method_name, const unsigned char* payLoad, size_t size, METHOD_HANDLE response_id) |
AzureIoTClient | 88:248736be106e | 1489 | { |
AzureIoTClient | 88:248736be106e | 1490 | int result; |
AzureIoTClient | 88:248736be106e | 1491 | if (handle == NULL) |
AzureIoTClient | 88:248736be106e | 1492 | { |
AzureIoTClient | 88:248736be106e | 1493 | /* Codes_SRS_IOTHUBCLIENT_LL_07_017: [ If handle or response is NULL then IoTHubClientCore_LL_DeviceMethodComplete shall return 500. ] */ |
AzureIoTClient | 88:248736be106e | 1494 | LogError("Invalid argument handle=%p", handle); |
AzureIoTClient | 88:248736be106e | 1495 | result = __FAILURE__; |
AzureIoTClient | 88:248736be106e | 1496 | } |
AzureIoTClient | 88:248736be106e | 1497 | else |
AzureIoTClient | 88:248736be106e | 1498 | { |
AzureIoTClient | 88:248736be106e | 1499 | /* Codes_SRS_IOTHUBCLIENT_LL_07_018: [ If deviceMethodCallback is not NULL IoTHubClientCore_LL_DeviceMethodComplete shall execute deviceMethodCallback and return the status. ] */ |
AzureIoTClient | 88:248736be106e | 1500 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)handle; |
AzureIoTClient | 88:248736be106e | 1501 | switch (handleData->methodCallback.type) |
AzureIoTClient | 88:248736be106e | 1502 | { |
AzureIoTClient | 88:248736be106e | 1503 | case CALLBACK_TYPE_SYNC: |
AzureIoTClient | 88:248736be106e | 1504 | { |
AzureIoTClient | 88:248736be106e | 1505 | unsigned char* payload_resp = NULL; |
AzureIoTClient | 88:248736be106e | 1506 | size_t response_size = 0; |
AzureIoTClient | 88:248736be106e | 1507 | result = handleData->methodCallback.callbackSync(method_name, payLoad, size, &payload_resp, &response_size, handleData->methodCallback.userContextCallback); |
AzureIoTClient | 88:248736be106e | 1508 | /* 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 | 88:248736be106e | 1509 | if (payload_resp != NULL && response_size > 0) |
AzureIoTClient | 88:248736be106e | 1510 | { |
AzureIoTClient | 88:248736be106e | 1511 | result = handleData->IoTHubTransport_DeviceMethod_Response(handleData->deviceHandle, response_id, payload_resp, response_size, result); |
AzureIoTClient | 88:248736be106e | 1512 | } |
AzureIoTClient | 88:248736be106e | 1513 | else |
AzureIoTClient | 88:248736be106e | 1514 | { |
AzureIoTClient | 88:248736be106e | 1515 | result = __FAILURE__; |
AzureIoTClient | 88:248736be106e | 1516 | } |
AzureIoTClient | 88:248736be106e | 1517 | if (payload_resp != NULL) |
AzureIoTClient | 88:248736be106e | 1518 | { |
AzureIoTClient | 88:248736be106e | 1519 | free(payload_resp); |
AzureIoTClient | 88:248736be106e | 1520 | } |
AzureIoTClient | 88:248736be106e | 1521 | break; |
AzureIoTClient | 88:248736be106e | 1522 | } |
AzureIoTClient | 88:248736be106e | 1523 | case CALLBACK_TYPE_ASYNC: |
AzureIoTClient | 88:248736be106e | 1524 | result = handleData->methodCallback.callbackAsync(method_name, payLoad, size, response_id, handleData->methodCallback.userContextCallback); |
AzureIoTClient | 88:248736be106e | 1525 | break; |
AzureIoTClient | 88:248736be106e | 1526 | default: |
AzureIoTClient | 88:248736be106e | 1527 | /* Codes_SRS_IOTHUBCLIENT_LL_07_019: [ If deviceMethodCallback is NULL IoTHubClientCore_LL_DeviceMethodComplete shall return 404. ] */ |
AzureIoTClient | 88:248736be106e | 1528 | result = 0; |
AzureIoTClient | 88:248736be106e | 1529 | break; |
AzureIoTClient | 88:248736be106e | 1530 | } |
AzureIoTClient | 88:248736be106e | 1531 | } |
AzureIoTClient | 88:248736be106e | 1532 | return result; |
AzureIoTClient | 88:248736be106e | 1533 | } |
AzureIoTClient | 88:248736be106e | 1534 | |
AzureIoTClient | 88:248736be106e | 1535 | void IoTHubClientCore_LL_RetrievePropertyComplete(IOTHUB_CLIENT_CORE_LL_HANDLE handle, DEVICE_TWIN_UPDATE_STATE update_state, const unsigned char* payLoad, size_t size) |
AzureIoTClient | 88:248736be106e | 1536 | { |
AzureIoTClient | 88:248736be106e | 1537 | if (handle == NULL) |
AzureIoTClient | 88:248736be106e | 1538 | { |
AzureIoTClient | 88:248736be106e | 1539 | /* Codes_SRS_IOTHUBCLIENT_LL_07_013: [ If handle is NULL then IoTHubClientCore_LL_RetrievePropertyComplete shall do nothing.] */ |
AzureIoTClient | 88:248736be106e | 1540 | LogError("Invalid argument handle=%p", handle); |
AzureIoTClient | 88:248736be106e | 1541 | } |
AzureIoTClient | 88:248736be106e | 1542 | else |
AzureIoTClient | 88:248736be106e | 1543 | { |
AzureIoTClient | 88:248736be106e | 1544 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)handle; |
AzureIoTClient | 88:248736be106e | 1545 | /* Codes_SRS_IOTHUBCLIENT_LL_07_014: [ If deviceTwinCallback is NULL then IoTHubClientCore_LL_RetrievePropertyComplete shall do nothing.] */ |
AzureIoTClient | 88:248736be106e | 1546 | if (handleData->deviceTwinCallback) |
AzureIoTClient | 88:248736be106e | 1547 | { |
AzureIoTClient | 88:248736be106e | 1548 | /* 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 IoTHubClientCore_LL_RetrievePropertyComplete shall do nothing.] */ |
AzureIoTClient | 88:248736be106e | 1549 | if (update_state == DEVICE_TWIN_UPDATE_COMPLETE) |
AzureIoTClient | 88:248736be106e | 1550 | { |
AzureIoTClient | 88:248736be106e | 1551 | handleData->complete_twin_update_encountered = true; |
AzureIoTClient | 88:248736be106e | 1552 | } |
AzureIoTClient | 88:248736be106e | 1553 | if (handleData->complete_twin_update_encountered) |
AzureIoTClient | 88:248736be106e | 1554 | { |
AzureIoTClient | 88:248736be106e | 1555 | /* Codes_SRS_IOTHUBCLIENT_LL_07_016: [ If deviceTwinCallback is set and DEVICE_TWIN_UPDATE_COMPLETE has been encountered then IoTHubClientCore_LL_RetrievePropertyComplete shall call deviceTwinCallback.] */ |
AzureIoTClient | 88:248736be106e | 1556 | handleData->deviceTwinCallback(update_state, payLoad, size, handleData->deviceTwinContextCallback); |
AzureIoTClient | 88:248736be106e | 1557 | } |
AzureIoTClient | 88:248736be106e | 1558 | } |
AzureIoTClient | 88:248736be106e | 1559 | } |
AzureIoTClient | 88:248736be106e | 1560 | } |
AzureIoTClient | 88:248736be106e | 1561 | |
AzureIoTClient | 88:248736be106e | 1562 | void IoTHubClientCore_LL_ReportedStateComplete(IOTHUB_CLIENT_CORE_LL_HANDLE handle, uint32_t item_id, int status_code) |
AzureIoTClient | 88:248736be106e | 1563 | { |
AzureIoTClient | 88:248736be106e | 1564 | /* Codes_SRS_IOTHUBCLIENT_LL_07_002: [ if handle or queue_handle are NULL then IoTHubClientCore_LL_ReportedStateComplete shall do nothing. ] */ |
AzureIoTClient | 88:248736be106e | 1565 | if (handle == NULL) |
AzureIoTClient | 88:248736be106e | 1566 | { |
AzureIoTClient | 88:248736be106e | 1567 | /*"shall return"*/ |
AzureIoTClient | 88:248736be106e | 1568 | LogError("Invalid argument handle=%p", handle); |
AzureIoTClient | 88:248736be106e | 1569 | } |
AzureIoTClient | 88:248736be106e | 1570 | else |
AzureIoTClient | 88:248736be106e | 1571 | { |
AzureIoTClient | 88:248736be106e | 1572 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)handle; |
AzureIoTClient | 88:248736be106e | 1573 | |
AzureIoTClient | 88:248736be106e | 1574 | /* Codes_SRS_IOTHUBCLIENT_LL_07_003: [ IoTHubClientCore_LL_ReportedStateComplete shall enumerate through the IOTHUB_DEVICE_TWIN structures in queue_handle. ]*/ |
AzureIoTClient | 88:248736be106e | 1575 | DLIST_ENTRY* client_item = handleData->iot_ack_queue.Flink; |
AzureIoTClient | 88:248736be106e | 1576 | while (client_item != &(handleData->iot_ack_queue)) /*while we are not at the end of the list*/ |
AzureIoTClient | 88:248736be106e | 1577 | { |
AzureIoTClient | 88:248736be106e | 1578 | PDLIST_ENTRY next_item = client_item->Flink; |
AzureIoTClient | 88:248736be106e | 1579 | IOTHUB_DEVICE_TWIN* queue_data = containingRecord(client_item, IOTHUB_DEVICE_TWIN, entry); |
AzureIoTClient | 88:248736be106e | 1580 | if (queue_data->item_id == item_id) |
AzureIoTClient | 88:248736be106e | 1581 | { |
AzureIoTClient | 88:248736be106e | 1582 | if (queue_data->reported_state_callback != NULL) |
AzureIoTClient | 88:248736be106e | 1583 | { |
AzureIoTClient | 88:248736be106e | 1584 | queue_data->reported_state_callback(status_code, queue_data->context); |
AzureIoTClient | 88:248736be106e | 1585 | } |
AzureIoTClient | 88:248736be106e | 1586 | /*Codes_SRS_IOTHUBCLIENT_LL_07_009: [ IoTHubClientCore_LL_ReportedStateComplete shall remove the IOTHUB_DEVICE_TWIN item from the ack queue.]*/ |
AzureIoTClient | 88:248736be106e | 1587 | DList_RemoveEntryList(client_item); |
AzureIoTClient | 88:248736be106e | 1588 | device_twin_data_destroy(queue_data); |
AzureIoTClient | 88:248736be106e | 1589 | break; |
AzureIoTClient | 88:248736be106e | 1590 | } |
AzureIoTClient | 88:248736be106e | 1591 | client_item = next_item; |
AzureIoTClient | 88:248736be106e | 1592 | } |
AzureIoTClient | 88:248736be106e | 1593 | } |
AzureIoTClient | 88:248736be106e | 1594 | } |
AzureIoTClient | 88:248736be106e | 1595 | |
AzureIoTClient | 89:a2ed767a532e | 1596 | static bool invoke_message_callback(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData, MESSAGE_CALLBACK_INFO* messageData) |
AzureIoTClient | 89:a2ed767a532e | 1597 | { |
AzureIoTClient | 89:a2ed767a532e | 1598 | bool result; |
AzureIoTClient | 89:a2ed767a532e | 1599 | /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */ |
AzureIoTClient | 89:a2ed767a532e | 1600 | handleData->lastMessageReceiveTime = get_time(NULL); |
AzureIoTClient | 89:a2ed767a532e | 1601 | |
AzureIoTClient | 89:a2ed767a532e | 1602 | switch (handleData->messageCallback.type) |
AzureIoTClient | 89:a2ed767a532e | 1603 | { |
AzureIoTClient | 89:a2ed767a532e | 1604 | case CALLBACK_TYPE_NONE: |
AzureIoTClient | 89:a2ed767a532e | 1605 | { |
AzureIoTClient | 89:a2ed767a532e | 1606 | /*Codes_SRS_IOTHUBCLIENT_LL_02_032: [If the client is not subscribed to receive messages then IoTHubClient_LL_MessageCallback shall return false.] */ |
AzureIoTClient | 89:a2ed767a532e | 1607 | LogError("Invalid workflow - not currently set up to accept messages"); |
AzureIoTClient | 89:a2ed767a532e | 1608 | result = false; |
AzureIoTClient | 89:a2ed767a532e | 1609 | break; |
AzureIoTClient | 89:a2ed767a532e | 1610 | } |
AzureIoTClient | 89:a2ed767a532e | 1611 | case CALLBACK_TYPE_SYNC: |
AzureIoTClient | 89:a2ed767a532e | 1612 | { |
AzureIoTClient | 89:a2ed767a532e | 1613 | /*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 | 89:a2ed767a532e | 1614 | IOTHUBMESSAGE_DISPOSITION_RESULT cb_result = handleData->messageCallback.callbackSync(messageData->messageHandle, handleData->messageCallback.userContextCallback); |
AzureIoTClient | 89:a2ed767a532e | 1615 | |
AzureIoTClient | 89:a2ed767a532e | 1616 | /*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 | 89:a2ed767a532e | 1617 | if (handleData->IoTHubTransport_SendMessageDisposition(messageData, cb_result) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 89:a2ed767a532e | 1618 | { |
AzureIoTClient | 89:a2ed767a532e | 1619 | LogError("IoTHubTransport_SendMessageDisposition failed"); |
AzureIoTClient | 89:a2ed767a532e | 1620 | } |
AzureIoTClient | 89:a2ed767a532e | 1621 | result = true; |
AzureIoTClient | 89:a2ed767a532e | 1622 | break; |
AzureIoTClient | 89:a2ed767a532e | 1623 | } |
AzureIoTClient | 89:a2ed767a532e | 1624 | case CALLBACK_TYPE_ASYNC: |
AzureIoTClient | 89:a2ed767a532e | 1625 | { |
AzureIoTClient | 89:a2ed767a532e | 1626 | /* Codes_SRS_IOTHUBCLIENT_LL_10_009: [If messageCallbackType is ASYNC then IoTHubClient_LL_MessageCallback shall return what messageCallbacEx returns.] */ |
AzureIoTClient | 89:a2ed767a532e | 1627 | result = handleData->messageCallback.callbackAsync(messageData, handleData->messageCallback.userContextCallback); |
AzureIoTClient | 89:a2ed767a532e | 1628 | if (!result) |
AzureIoTClient | 89:a2ed767a532e | 1629 | { |
AzureIoTClient | 89:a2ed767a532e | 1630 | LogError("messageCallbackEx failed"); |
AzureIoTClient | 89:a2ed767a532e | 1631 | } |
AzureIoTClient | 89:a2ed767a532e | 1632 | break; |
AzureIoTClient | 89:a2ed767a532e | 1633 | } |
AzureIoTClient | 89:a2ed767a532e | 1634 | default: |
AzureIoTClient | 89:a2ed767a532e | 1635 | { |
AzureIoTClient | 89:a2ed767a532e | 1636 | LogError("Invalid state"); |
AzureIoTClient | 89:a2ed767a532e | 1637 | result = false; |
AzureIoTClient | 89:a2ed767a532e | 1638 | break; |
AzureIoTClient | 89:a2ed767a532e | 1639 | } |
AzureIoTClient | 89:a2ed767a532e | 1640 | } |
AzureIoTClient | 89:a2ed767a532e | 1641 | |
AzureIoTClient | 89:a2ed767a532e | 1642 | return result; |
AzureIoTClient | 89:a2ed767a532e | 1643 | } |
AzureIoTClient | 89:a2ed767a532e | 1644 | |
AzureIoTClient | 88:248736be106e | 1645 | bool IoTHubClientCore_LL_MessageCallback(IOTHUB_CLIENT_CORE_LL_HANDLE handle, MESSAGE_CALLBACK_INFO* messageData) |
AzureIoTClient | 88:248736be106e | 1646 | { |
AzureIoTClient | 88:248736be106e | 1647 | bool result; |
AzureIoTClient | 88:248736be106e | 1648 | if ((handle == NULL) || messageData == NULL) |
AzureIoTClient | 88:248736be106e | 1649 | { |
AzureIoTClient | 89:a2ed767a532e | 1650 | /*Codes_SRS_IOTHUBCLIENT_LL_02_029: [If parameter handle is NULL then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */ |
AzureIoTClient | 88:248736be106e | 1651 | LogError("invalid argument: handle(%p), messageData(%p)", handle, messageData); |
AzureIoTClient | 88:248736be106e | 1652 | result = false; |
AzureIoTClient | 88:248736be106e | 1653 | } |
AzureIoTClient | 88:248736be106e | 1654 | else if (messageData->messageHandle == NULL) |
AzureIoTClient | 88:248736be106e | 1655 | { |
AzureIoTClient | 89:a2ed767a532e | 1656 | /*Codes_SRS_IOTHUBCLIENT_LL_10_004: [If messageHandle field of paramger messageData is NULL then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */ |
AzureIoTClient | 88:248736be106e | 1657 | LogError("invalid argument messageData->messageHandle(NULL)"); |
AzureIoTClient | 88:248736be106e | 1658 | result = false; |
AzureIoTClient | 88:248736be106e | 1659 | } |
AzureIoTClient | 88:248736be106e | 1660 | else |
AzureIoTClient | 88:248736be106e | 1661 | { |
AzureIoTClient | 88:248736be106e | 1662 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)handle; |
AzureIoTClient | 89:a2ed767a532e | 1663 | return invoke_message_callback(handleData, messageData); |
AzureIoTClient | 89:a2ed767a532e | 1664 | } |
AzureIoTClient | 89:a2ed767a532e | 1665 | return result; |
AzureIoTClient | 89:a2ed767a532e | 1666 | } |
AzureIoTClient | 88:248736be106e | 1667 | |
AzureIoTClient | 89:a2ed767a532e | 1668 | static bool is_event_equal(IOTHUB_EVENT_CALLBACK *event_callback, const char *input_name) |
AzureIoTClient | 89:a2ed767a532e | 1669 | { |
AzureIoTClient | 89:a2ed767a532e | 1670 | bool result; |
AzureIoTClient | 89:a2ed767a532e | 1671 | |
AzureIoTClient | 89:a2ed767a532e | 1672 | if (event_callback != NULL) |
AzureIoTClient | 89:a2ed767a532e | 1673 | { |
AzureIoTClient | 89:a2ed767a532e | 1674 | const char* event_input_name = STRING_c_str(event_callback->inputName); |
AzureIoTClient | 89:a2ed767a532e | 1675 | if ((event_input_name != NULL) && (input_name != NULL)) |
AzureIoTClient | 89:a2ed767a532e | 1676 | { |
AzureIoTClient | 89:a2ed767a532e | 1677 | // Matched the input queue name of a named handler |
AzureIoTClient | 89:a2ed767a532e | 1678 | result = (strcmp(event_input_name, input_name) == 0); |
AzureIoTClient | 89:a2ed767a532e | 1679 | } |
AzureIoTClient | 89:a2ed767a532e | 1680 | else if ((input_name == NULL) && (event_input_name == NULL)) |
AzureIoTClient | 89:a2ed767a532e | 1681 | { |
AzureIoTClient | 89:a2ed767a532e | 1682 | // Matched the default handler |
AzureIoTClient | 89:a2ed767a532e | 1683 | result = true; |
AzureIoTClient | 89:a2ed767a532e | 1684 | } |
AzureIoTClient | 89:a2ed767a532e | 1685 | else |
AzureIoTClient | 88:248736be106e | 1686 | { |
AzureIoTClient | 89:a2ed767a532e | 1687 | result = false; |
AzureIoTClient | 89:a2ed767a532e | 1688 | } |
AzureIoTClient | 89:a2ed767a532e | 1689 | } |
AzureIoTClient | 89:a2ed767a532e | 1690 | else |
AzureIoTClient | 89:a2ed767a532e | 1691 | { |
AzureIoTClient | 89:a2ed767a532e | 1692 | result = false; |
AzureIoTClient | 89:a2ed767a532e | 1693 | } |
AzureIoTClient | 89:a2ed767a532e | 1694 | return result; |
AzureIoTClient | 89:a2ed767a532e | 1695 | } |
AzureIoTClient | 89:a2ed767a532e | 1696 | |
AzureIoTClient | 89:a2ed767a532e | 1697 | static bool is_event_equal_for_match(LIST_ITEM_HANDLE list_item, const void* match_context) |
AzureIoTClient | 89:a2ed767a532e | 1698 | { |
AzureIoTClient | 89:a2ed767a532e | 1699 | return is_event_equal((IOTHUB_EVENT_CALLBACK*)singlylinkedlist_item_get_value(list_item), (const char*)match_context); |
AzureIoTClient | 89:a2ed767a532e | 1700 | } |
AzureIoTClient | 89:a2ed767a532e | 1701 | |
AzureIoTClient | 89:a2ed767a532e | 1702 | bool IoTHubClientCore_LL_MessageCallbackFromInput(IOTHUB_CLIENT_CORE_LL_HANDLE handle, MESSAGE_CALLBACK_INFO* messageData) |
AzureIoTClient | 89:a2ed767a532e | 1703 | { |
AzureIoTClient | 89:a2ed767a532e | 1704 | bool result; |
AzureIoTClient | 89:a2ed767a532e | 1705 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)handle; |
AzureIoTClient | 89:a2ed767a532e | 1706 | |
AzureIoTClient | 89:a2ed767a532e | 1707 | if ((handle == NULL) || messageData == NULL) |
AzureIoTClient | 89:a2ed767a532e | 1708 | { |
AzureIoTClient | 89:a2ed767a532e | 1709 | // Codes_SRS_IOTHUBCLIENT_LL_31_137: [ If either parameter `handle` or `messageData` is `NULL` then `IoTHubClient_LL_MessageCallbackFromInput` shall return `false`.** ] |
AzureIoTClient | 89:a2ed767a532e | 1710 | LogError("invalid argument: handle(%p), messageData(%p)", handle, messageData); |
AzureIoTClient | 89:a2ed767a532e | 1711 | result = false; |
AzureIoTClient | 89:a2ed767a532e | 1712 | } |
AzureIoTClient | 89:a2ed767a532e | 1713 | else if (messageData->messageHandle == NULL) |
AzureIoTClient | 89:a2ed767a532e | 1714 | { |
AzureIoTClient | 89:a2ed767a532e | 1715 | // Codes_SRS_IOTHUBCLIENT_LL_31_137: [ If either parameter `handle` or `messageData` is `NULL` then `IoTHubClient_LL_MessageCallbackFromInput` shall return `false`.** ] |
AzureIoTClient | 89:a2ed767a532e | 1716 | LogError("invalid argument messageData->messageHandle(NULL)"); |
AzureIoTClient | 89:a2ed767a532e | 1717 | result = false; |
AzureIoTClient | 89:a2ed767a532e | 1718 | } |
AzureIoTClient | 89:a2ed767a532e | 1719 | else if (handleData->event_callbacks == NULL) |
AzureIoTClient | 89:a2ed767a532e | 1720 | { |
AzureIoTClient | 89:a2ed767a532e | 1721 | LogError("Callback from input called but no input specific callbacks registered"); |
AzureIoTClient | 89:a2ed767a532e | 1722 | result = false; |
AzureIoTClient | 89:a2ed767a532e | 1723 | } |
AzureIoTClient | 89:a2ed767a532e | 1724 | else |
AzureIoTClient | 89:a2ed767a532e | 1725 | { |
AzureIoTClient | 89:a2ed767a532e | 1726 | const char* inputName = IoTHubMessage_GetInputName(messageData->messageHandle); |
AzureIoTClient | 89:a2ed767a532e | 1727 | |
AzureIoTClient | 89:a2ed767a532e | 1728 | LIST_ITEM_HANDLE item_handle = NULL; |
AzureIoTClient | 89:a2ed767a532e | 1729 | |
AzureIoTClient | 89:a2ed767a532e | 1730 | item_handle = singlylinkedlist_find(handleData->event_callbacks, is_event_equal_for_match, (const void*)inputName); |
AzureIoTClient | 89:a2ed767a532e | 1731 | |
AzureIoTClient | 89:a2ed767a532e | 1732 | if (item_handle == NULL) |
AzureIoTClient | 89:a2ed767a532e | 1733 | { |
AzureIoTClient | 89:a2ed767a532e | 1734 | // Codes_SRS_IOTHUBCLIENT_LL_31_138: [ If there is no registered handler for the inputName from `IoTHubMessage_GetInputName`, then `IoTHubClient_LL_MessageCallbackFromInput` shall attempt invoke the default handler handler.** ] |
AzureIoTClient | 89:a2ed767a532e | 1735 | item_handle = singlylinkedlist_find(handleData->event_callbacks, is_event_equal_for_match, NULL); |
AzureIoTClient | 89:a2ed767a532e | 1736 | } |
AzureIoTClient | 89:a2ed767a532e | 1737 | |
AzureIoTClient | 89:a2ed767a532e | 1738 | if (item_handle == NULL) |
AzureIoTClient | 89:a2ed767a532e | 1739 | { |
AzureIoTClient | 89:a2ed767a532e | 1740 | LogError("Could not find callback (explicit or default) for input queue %s", inputName); |
AzureIoTClient | 89:a2ed767a532e | 1741 | result = false; |
AzureIoTClient | 89:a2ed767a532e | 1742 | } |
AzureIoTClient | 89:a2ed767a532e | 1743 | else |
AzureIoTClient | 89:a2ed767a532e | 1744 | { |
AzureIoTClient | 89:a2ed767a532e | 1745 | IOTHUB_EVENT_CALLBACK* event_callback = (IOTHUB_EVENT_CALLBACK*)singlylinkedlist_item_get_value(item_handle); |
AzureIoTClient | 89:a2ed767a532e | 1746 | if (NULL == event_callback) |
AzureIoTClient | 88:248736be106e | 1747 | { |
AzureIoTClient | 89:a2ed767a532e | 1748 | LogError("singlylinkedlist_item_get_value for event_callback failed"); |
AzureIoTClient | 88:248736be106e | 1749 | result = false; |
AzureIoTClient | 88:248736be106e | 1750 | } |
AzureIoTClient | 89:a2ed767a532e | 1751 | else |
AzureIoTClient | 88:248736be106e | 1752 | { |
AzureIoTClient | 89:a2ed767a532e | 1753 | // Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] |
AzureIoTClient | 89:a2ed767a532e | 1754 | handleData->lastMessageReceiveTime = get_time(NULL); |
AzureIoTClient | 88:248736be106e | 1755 | |
AzureIoTClient | 89:a2ed767a532e | 1756 | if (event_callback->callbackAsyncEx != NULL) |
AzureIoTClient | 88:248736be106e | 1757 | { |
AzureIoTClient | 89:a2ed767a532e | 1758 | // Codes_SRS_IOTHUBCLIENT_LL_31_139: [ `IoTHubClient_LL_MessageCallbackFromInput` shall the callback from the given inputName queue if it has been registered.** ] |
AzureIoTClient | 89:a2ed767a532e | 1759 | result = event_callback->callbackAsyncEx(messageData, event_callback->userContextCallbackEx); |
AzureIoTClient | 88:248736be106e | 1760 | } |
AzureIoTClient | 89:a2ed767a532e | 1761 | else |
AzureIoTClient | 88:248736be106e | 1762 | { |
AzureIoTClient | 89:a2ed767a532e | 1763 | // Codes_SRS_IOTHUBCLIENT_LL_31_139: [ `IoTHubClient_LL_MessageCallbackFromInput` shall the callback from the given inputName queue if it has been registered.** ] |
AzureIoTClient | 89:a2ed767a532e | 1764 | IOTHUBMESSAGE_DISPOSITION_RESULT cb_result = event_callback->callbackAsync(messageData->messageHandle, event_callback->userContextCallback); |
AzureIoTClient | 89:a2ed767a532e | 1765 | |
AzureIoTClient | 89:a2ed767a532e | 1766 | // Codes_SRS_IOTHUBCLIENT_LL_31_140: [ `IoTHubClient_LL_MessageCallbackFromInput` shall send the message disposition as returned by the client to the underlying layer and return `true` if an input queue match is found.** ] |
AzureIoTClient | 89:a2ed767a532e | 1767 | if (handleData->IoTHubTransport_SendMessageDisposition(messageData, cb_result) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 89:a2ed767a532e | 1768 | { |
AzureIoTClient | 89:a2ed767a532e | 1769 | LogError("IoTHubTransport_SendMessageDisposition failed"); |
AzureIoTClient | 89:a2ed767a532e | 1770 | } |
AzureIoTClient | 89:a2ed767a532e | 1771 | result = true; |
AzureIoTClient | 88:248736be106e | 1772 | } |
AzureIoTClient | 88:248736be106e | 1773 | } |
AzureIoTClient | 88:248736be106e | 1774 | } |
AzureIoTClient | 88:248736be106e | 1775 | } |
AzureIoTClient | 89:a2ed767a532e | 1776 | |
AzureIoTClient | 88:248736be106e | 1777 | return result; |
AzureIoTClient | 88:248736be106e | 1778 | } |
AzureIoTClient | 88:248736be106e | 1779 | |
AzureIoTClient | 89:a2ed767a532e | 1780 | |
AzureIoTClient | 88:248736be106e | 1781 | void IoTHubClientCore_LL_ConnectionStatusCallBack(IOTHUB_CLIENT_CORE_LL_HANDLE handle, IOTHUB_CLIENT_CONNECTION_STATUS status, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason) |
AzureIoTClient | 88:248736be106e | 1782 | { |
AzureIoTClient | 88:248736be106e | 1783 | /*Codes_SRS_IOTHUBCLIENT_LL_25_113: [If parameter connectionStatus is NULL or parameter handle is NULL then IoTHubClientCore_LL_ConnectionStatusCallBack shall return.]*/ |
AzureIoTClient | 88:248736be106e | 1784 | if (handle == NULL) |
AzureIoTClient | 88:248736be106e | 1785 | { |
AzureIoTClient | 88:248736be106e | 1786 | /*"shall return"*/ |
AzureIoTClient | 88:248736be106e | 1787 | LogError("invalid arg"); |
AzureIoTClient | 88:248736be106e | 1788 | } |
AzureIoTClient | 88:248736be106e | 1789 | else |
AzureIoTClient | 88:248736be106e | 1790 | { |
AzureIoTClient | 88:248736be106e | 1791 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)handle; |
AzureIoTClient | 88:248736be106e | 1792 | |
AzureIoTClient | 88:248736be106e | 1793 | /*Codes_SRS_IOTHUBCLIENT_LL_25_114: [IoTHubClientCore_LL_ConnectionStatusCallBack shall call non-callback set by the user from IoTHubClientCore_LL_SetConnectionStatusCallback passing the status, reason and the passed userContextCallback.]*/ |
AzureIoTClient | 88:248736be106e | 1794 | if (handleData->conStatusCallback != NULL) |
AzureIoTClient | 88:248736be106e | 1795 | { |
AzureIoTClient | 88:248736be106e | 1796 | handleData->conStatusCallback(status, reason, handleData->conStatusUserContextCallback); |
AzureIoTClient | 88:248736be106e | 1797 | } |
AzureIoTClient | 88:248736be106e | 1798 | } |
AzureIoTClient | 88:248736be106e | 1799 | |
AzureIoTClient | 88:248736be106e | 1800 | } |
AzureIoTClient | 88:248736be106e | 1801 | |
AzureIoTClient | 88:248736be106e | 1802 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetConnectionStatusCallback(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_CONNECTION_STATUS_CALLBACK connectionStatusCallback, void * userContextCallback) |
AzureIoTClient | 88:248736be106e | 1803 | { |
AzureIoTClient | 88:248736be106e | 1804 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 1805 | /*Codes_SRS_IOTHUBCLIENT_LL_25_111: [IoTHubClientCore_LL_SetConnectionStatusCallback shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter iotHubClientHandle**]** */ |
AzureIoTClient | 88:248736be106e | 1806 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 88:248736be106e | 1807 | { |
AzureIoTClient | 88:248736be106e | 1808 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 1809 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1810 | } |
AzureIoTClient | 88:248736be106e | 1811 | else |
AzureIoTClient | 88:248736be106e | 1812 | { |
AzureIoTClient | 88:248736be106e | 1813 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1814 | /*Codes_SRS_IOTHUBCLIENT_LL_25_112: [IoTHubClientCore_LL_SetConnectionStatusCallback shall return IOTHUB_CLIENT_OK and save the callback and userContext as a member of the handle.] */ |
AzureIoTClient | 88:248736be106e | 1815 | handleData->conStatusCallback = connectionStatusCallback; |
AzureIoTClient | 88:248736be106e | 1816 | handleData->conStatusUserContextCallback = userContextCallback; |
AzureIoTClient | 88:248736be106e | 1817 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1818 | } |
AzureIoTClient | 88:248736be106e | 1819 | |
AzureIoTClient | 88:248736be106e | 1820 | return result; |
AzureIoTClient | 88:248736be106e | 1821 | } |
AzureIoTClient | 88:248736be106e | 1822 | |
AzureIoTClient | 88:248736be106e | 1823 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetRetryPolicy(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds) |
AzureIoTClient | 88:248736be106e | 1824 | { |
AzureIoTClient | 88:248736be106e | 1825 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 1826 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1827 | |
AzureIoTClient | 88:248736be106e | 1828 | /* Codes_SRS_IOTHUBCLIENT_LL_25_116: [If iotHubClientHandle, retryPolicy or retryTimeoutLimitinSeconds is NULL, IoTHubClientCore_LL_GetRetryPolicy shall return IOTHUB_CLIENT_INVALID_ARG]*/ |
AzureIoTClient | 88:248736be106e | 1829 | if (handleData == NULL) |
AzureIoTClient | 88:248736be106e | 1830 | { |
AzureIoTClient | 88:248736be106e | 1831 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 1832 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1833 | } |
AzureIoTClient | 88:248736be106e | 1834 | else |
AzureIoTClient | 88:248736be106e | 1835 | { |
AzureIoTClient | 88:248736be106e | 1836 | if (handleData->transportHandle == NULL) |
AzureIoTClient | 88:248736be106e | 1837 | { |
AzureIoTClient | 88:248736be106e | 1838 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1839 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1840 | } |
AzureIoTClient | 88:248736be106e | 1841 | else |
AzureIoTClient | 88:248736be106e | 1842 | { |
AzureIoTClient | 88:248736be106e | 1843 | if (handleData->IoTHubTransport_SetRetryPolicy(handleData->transportHandle, retryPolicy, retryTimeoutLimitInSeconds) != 0) |
AzureIoTClient | 88:248736be106e | 1844 | { |
AzureIoTClient | 88:248736be106e | 1845 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1846 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1847 | } |
AzureIoTClient | 88:248736be106e | 1848 | else |
AzureIoTClient | 88:248736be106e | 1849 | { |
AzureIoTClient | 88:248736be106e | 1850 | /* Codes_SRS_IOTHUBCLIENT_LL_25_118: [IoTHubClientCore_LL_SetRetryPolicy shall save connection retry policies specified by the user to retryPolicy in struct IOTHUB_CLIENT_CORE_LL_HANDLE_DATA] */ |
AzureIoTClient | 88:248736be106e | 1851 | /* Codes_SRS_IOTHUBCLIENT_LL_25_119: [IoTHubClientCore_LL_SetRetryPolicy shall save retryTimeoutLimitInSeconds in seconds to retryTimeout in struct IOTHUB_CLIENT_CORE_LL_HANDLE_DATA] */ |
AzureIoTClient | 88:248736be106e | 1852 | handleData->retryPolicy = retryPolicy; |
AzureIoTClient | 88:248736be106e | 1853 | handleData->retryTimeoutLimitInSeconds = retryTimeoutLimitInSeconds; |
AzureIoTClient | 88:248736be106e | 1854 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1855 | } |
AzureIoTClient | 88:248736be106e | 1856 | } |
AzureIoTClient | 88:248736be106e | 1857 | } |
AzureIoTClient | 88:248736be106e | 1858 | return result; |
AzureIoTClient | 88:248736be106e | 1859 | } |
AzureIoTClient | 88:248736be106e | 1860 | |
AzureIoTClient | 88:248736be106e | 1861 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_GetRetryPolicy(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_RETRY_POLICY* retryPolicy, size_t* retryTimeoutLimitInSeconds) |
AzureIoTClient | 88:248736be106e | 1862 | { |
AzureIoTClient | 88:248736be106e | 1863 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 1864 | |
AzureIoTClient | 88:248736be106e | 1865 | /* Codes_SRS_IOTHUBCLIENT_LL_09_001: [IoTHubClientCore_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG if any of the arguments is NULL] */ |
AzureIoTClient | 88:248736be106e | 1866 | if (iotHubClientHandle == NULL || retryPolicy == NULL || retryTimeoutLimitInSeconds == NULL) |
AzureIoTClient | 88:248736be106e | 1867 | { |
AzureIoTClient | 88:248736be106e | 1868 | LogError("Invalid parameter IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle = %p, IOTHUB_CLIENT_RETRY_POLICY* retryPolicy = %p, size_t* retryTimeoutLimitInSeconds = %p", iotHubClientHandle, retryPolicy, retryTimeoutLimitInSeconds); |
AzureIoTClient | 88:248736be106e | 1869 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 1870 | } |
AzureIoTClient | 88:248736be106e | 1871 | else |
AzureIoTClient | 88:248736be106e | 1872 | { |
AzureIoTClient | 88:248736be106e | 1873 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1874 | |
AzureIoTClient | 88:248736be106e | 1875 | *retryPolicy = handleData->retryPolicy; |
AzureIoTClient | 88:248736be106e | 1876 | *retryTimeoutLimitInSeconds = handleData->retryTimeoutLimitInSeconds; |
AzureIoTClient | 88:248736be106e | 1877 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1878 | } |
AzureIoTClient | 88:248736be106e | 1879 | |
AzureIoTClient | 88:248736be106e | 1880 | return result; |
AzureIoTClient | 88:248736be106e | 1881 | } |
AzureIoTClient | 88:248736be106e | 1882 | |
AzureIoTClient | 88:248736be106e | 1883 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_GetLastMessageReceiveTime(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, time_t* lastMessageReceiveTime) |
AzureIoTClient | 88:248736be106e | 1884 | { |
AzureIoTClient | 88:248736be106e | 1885 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 1886 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1887 | |
AzureIoTClient | 88:248736be106e | 1888 | /* Codes_SRS_IOTHUBCLIENT_LL_09_001: [IoTHubClientCore_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_INVALID_ARG if any of the arguments is NULL] */ |
AzureIoTClient | 88:248736be106e | 1889 | if (handleData == NULL || lastMessageReceiveTime == NULL) |
AzureIoTClient | 88:248736be106e | 1890 | { |
AzureIoTClient | 88:248736be106e | 1891 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 1892 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1893 | } |
AzureIoTClient | 88:248736be106e | 1894 | else |
AzureIoTClient | 88:248736be106e | 1895 | { |
AzureIoTClient | 88:248736be106e | 1896 | /* Codes_SRS_IOTHUBCLIENT_LL_09_002: [IoTHubClientCore_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 | 88:248736be106e | 1897 | if (handleData->lastMessageReceiveTime == INDEFINITE_TIME) |
AzureIoTClient | 88:248736be106e | 1898 | { |
AzureIoTClient | 88:248736be106e | 1899 | result = IOTHUB_CLIENT_INDEFINITE_TIME; |
AzureIoTClient | 88:248736be106e | 1900 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 1901 | } |
AzureIoTClient | 88:248736be106e | 1902 | else |
AzureIoTClient | 88:248736be106e | 1903 | { |
AzureIoTClient | 88:248736be106e | 1904 | /* Codes_SRS_IOTHUBCLIENT_LL_09_003: [IoTHubClientCore_LL_GetLastMessageReceiveTime shall return IOTHUB_CLIENT_OK if it wrote in the lastMessageReceiveTime the time when the last command was received] */ |
AzureIoTClient | 88:248736be106e | 1905 | /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClientCore_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */ |
AzureIoTClient | 88:248736be106e | 1906 | *lastMessageReceiveTime = handleData->lastMessageReceiveTime; |
AzureIoTClient | 88:248736be106e | 1907 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1908 | } |
AzureIoTClient | 88:248736be106e | 1909 | } |
AzureIoTClient | 88:248736be106e | 1910 | |
AzureIoTClient | 88:248736be106e | 1911 | return result; |
AzureIoTClient | 88:248736be106e | 1912 | } |
AzureIoTClient | 88:248736be106e | 1913 | |
AzureIoTClient | 88:248736be106e | 1914 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetOption(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, const char* optionName, const void* value) |
AzureIoTClient | 88:248736be106e | 1915 | { |
AzureIoTClient | 88:248736be106e | 1916 | |
AzureIoTClient | 88:248736be106e | 1917 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 1918 | /*Codes_SRS_IOTHUBCLIENT_LL_02_034: [If iotHubClientHandle is NULL then IoTHubClientCore_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]*/ |
AzureIoTClient | 88:248736be106e | 1919 | /*Codes_SRS_IOTHUBCLIENT_LL_02_035: [If optionName is NULL then IoTHubClientCore_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 88:248736be106e | 1920 | /*Codes_SRS_IOTHUBCLIENT_LL_02_036: [If value is NULL then IoTHubClientCore_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 88:248736be106e | 1921 | if ( |
AzureIoTClient | 88:248736be106e | 1922 | (iotHubClientHandle == NULL) || |
AzureIoTClient | 88:248736be106e | 1923 | (optionName == NULL) || |
AzureIoTClient | 88:248736be106e | 1924 | (value == NULL) |
AzureIoTClient | 88:248736be106e | 1925 | ) |
AzureIoTClient | 88:248736be106e | 1926 | { |
AzureIoTClient | 88:248736be106e | 1927 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 1928 | LogError("invalid argument (NULL)"); |
AzureIoTClient | 88:248736be106e | 1929 | } |
AzureIoTClient | 88:248736be106e | 1930 | else |
AzureIoTClient | 88:248736be106e | 1931 | { |
AzureIoTClient | 88:248736be106e | 1932 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 1933 | |
AzureIoTClient | 88:248736be106e | 1934 | /*Codes_SRS_IOTHUBCLIENT_LL_02_039: [ "messageTimeout" - once IoTHubClientCore_LL_SendEventAsync is called the message shall timeout after value miliseconds. Value is a pointer to a tickcounter_ms_t. ]*/ |
AzureIoTClient | 88:248736be106e | 1935 | if (strcmp(optionName, OPTION_MESSAGE_TIMEOUT) == 0) |
AzureIoTClient | 88:248736be106e | 1936 | { |
AzureIoTClient | 88:248736be106e | 1937 | /*this is an option handled by IoTHubClientCore_LL*/ |
AzureIoTClient | 88:248736be106e | 1938 | /*Codes_SRS_IOTHUBCLIENT_LL_02_043: [ Calling IoTHubClientCore_LL_SetOption with value set to "0" shall disable the timeout mechanism for all new messages. ]*/ |
AzureIoTClient | 88:248736be106e | 1939 | handleData->currentMessageTimeout = *(const tickcounter_ms_t*)value; |
AzureIoTClient | 88:248736be106e | 1940 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1941 | } |
AzureIoTClient | 88:248736be106e | 1942 | else if (strcmp(optionName, OPTION_PRODUCT_INFO) == 0) |
AzureIoTClient | 88:248736be106e | 1943 | { |
AzureIoTClient | 88:248736be106e | 1944 | /*Codes_SRS_IOTHUBCLIENT_LL_10_033: [repeat calls with "product_info" will erase the previously set product information if applicatble. ]*/ |
AzureIoTClient | 88:248736be106e | 1945 | if (handleData->product_info != NULL) |
AzureIoTClient | 88:248736be106e | 1946 | { |
AzureIoTClient | 88:248736be106e | 1947 | STRING_delete(handleData->product_info); |
AzureIoTClient | 88:248736be106e | 1948 | handleData->product_info = NULL; |
AzureIoTClient | 88:248736be106e | 1949 | } |
AzureIoTClient | 88:248736be106e | 1950 | |
AzureIoTClient | 88:248736be106e | 1951 | /*Codes_SRS_IOTHUBCLIENT_LL_10_035: [If string concatenation fails, `IoTHubClientCore_LL_SetOption` shall return `IOTHUB_CLIENT_ERRROR`. Otherwise, `IOTHUB_CLIENT_OK` shall be returned. ]*/ |
AzureIoTClient | 88:248736be106e | 1952 | handleData->product_info = make_product_info((const char*)value); |
AzureIoTClient | 88:248736be106e | 1953 | if (handleData->product_info == NULL) |
AzureIoTClient | 88:248736be106e | 1954 | { |
AzureIoTClient | 88:248736be106e | 1955 | LogError("STRING_construct_sprintf failed"); |
AzureIoTClient | 88:248736be106e | 1956 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1957 | } |
AzureIoTClient | 88:248736be106e | 1958 | else |
AzureIoTClient | 88:248736be106e | 1959 | { |
AzureIoTClient | 88:248736be106e | 1960 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1961 | } |
AzureIoTClient | 88:248736be106e | 1962 | } |
AzureIoTClient | 88:248736be106e | 1963 | else if (strcmp(optionName, OPTION_DIAGNOSTIC_SAMPLING_PERCENTAGE) == 0) |
AzureIoTClient | 88:248736be106e | 1964 | { |
AzureIoTClient | 88:248736be106e | 1965 | uint32_t percentage = *(uint32_t*)value; |
AzureIoTClient | 88:248736be106e | 1966 | if (percentage > 100) |
AzureIoTClient | 88:248736be106e | 1967 | { |
AzureIoTClient | 88:248736be106e | 1968 | /*Codes_SRS_IOTHUBCLIENT_LL_10_036: [Calling IoTHubClientCore_LL_SetOption with value > 100 shall return `IOTHUB_CLIENT_ERRROR`. ]*/ |
AzureIoTClient | 88:248736be106e | 1969 | LogError("The value of diag_sampling_percentage is out of range [0, 100]: %u", percentage); |
AzureIoTClient | 88:248736be106e | 1970 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1971 | } |
AzureIoTClient | 88:248736be106e | 1972 | else |
AzureIoTClient | 88:248736be106e | 1973 | { |
AzureIoTClient | 88:248736be106e | 1974 | /*Codes_SRS_IOTHUBCLIENT_LL_10_037: [Calling IoTHubClientCore_LL_SetOption with value between [0, 100] shall return `IOTHUB_CLIENT_OK`. ]*/ |
AzureIoTClient | 88:248736be106e | 1975 | handleData->diagnostic_setting.diagSamplingPercentage = percentage; |
AzureIoTClient | 88:248736be106e | 1976 | handleData->diagnostic_setting.currentMessageNumber = 0; |
AzureIoTClient | 88:248736be106e | 1977 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 1978 | } |
AzureIoTClient | 88:248736be106e | 1979 | } |
AzureIoTClient | 88:248736be106e | 1980 | else if (strcmp(optionName, OPTION_BLOB_UPLOAD_TIMEOUT_SECS) == 0) |
AzureIoTClient | 88:248736be106e | 1981 | { |
AzureIoTClient | 88:248736be106e | 1982 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 88:248736be106e | 1983 | // This option just gets passed down into IoTHubClientCore_LL_UploadToBlob |
AzureIoTClient | 88:248736be106e | 1984 | /*Codes_SRS_IOTHUBCLIENT_LL_30_010: [ blob_xfr_timeout - IoTHubClientCore_LL_SetOption shall pass this option to IoTHubClient_UploadToBlob_SetOption and return its result. ]*/ |
AzureIoTClient | 88:248736be106e | 1985 | result = IoTHubClient_LL_UploadToBlob_SetOption(handleData->uploadToBlobHandle, optionName, value); |
AzureIoTClient | 88:248736be106e | 1986 | if(result != IOTHUB_CLIENT_OK) |
AzureIoTClient | 88:248736be106e | 1987 | { |
AzureIoTClient | 88:248736be106e | 1988 | LogError("unable to IoTHubClientCore_LL_UploadToBlob_SetOption"); |
AzureIoTClient | 88:248736be106e | 1989 | } |
AzureIoTClient | 88:248736be106e | 1990 | #else |
AzureIoTClient | 88:248736be106e | 1991 | LogError("OPTION_BLOB_TRANSFER_TIMEOUT option being set with DONT_USE_UPLOADTOBLOB compiler switch"); |
AzureIoTClient | 88:248736be106e | 1992 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 1993 | #endif /*DONT_USE_UPLOADTOBLOB*/ |
AzureIoTClient | 88:248736be106e | 1994 | } |
AzureIoTClient | 88:248736be106e | 1995 | else |
AzureIoTClient | 88:248736be106e | 1996 | { |
AzureIoTClient | 88:248736be106e | 1997 | // This section is unusual for SetOption calls because it attempts to pass unhandled options |
AzureIoTClient | 88:248736be106e | 1998 | // to two downstream targets (IoTHubTransport_SetOption and IoTHubClientCore_LL_UploadToBlob_SetOption) instead of one. |
AzureIoTClient | 88:248736be106e | 1999 | |
AzureIoTClient | 88:248736be106e | 2000 | /*Codes_SRS_IOTHUBCLIENT_LL_30_011: [ IoTHubClientCore_LL_SetOption shall always pass unhandled options to Transport_SetOption. ]*/ |
AzureIoTClient | 88:248736be106e | 2001 | /*Codes_SRS_IOTHUBCLIENT_LL_30_012: [ If Transport_SetOption fails, IoTHubClientCore_LL_SetOption shall return that failure code. ]*/ |
AzureIoTClient | 88:248736be106e | 2002 | result = handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value); |
AzureIoTClient | 88:248736be106e | 2003 | if(result != IOTHUB_CLIENT_OK) |
AzureIoTClient | 88:248736be106e | 2004 | { |
AzureIoTClient | 88:248736be106e | 2005 | LogError("unable to IoTHubTransport_SetOption"); |
AzureIoTClient | 88:248736be106e | 2006 | } |
AzureIoTClient | 88:248736be106e | 2007 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 88:248736be106e | 2008 | else |
AzureIoTClient | 88:248736be106e | 2009 | { |
AzureIoTClient | 88:248736be106e | 2010 | /*Codes_SRS_IOTHUBCLIENT_LL_30_013: [ If the DONT_USE_UPLOADTOBLOB compiler switch is undefined, IoTHubClientCore_LL_SetOption shall pass unhandled options to IoTHubClient_UploadToBlob_SetOption and ignore the result. ]*/ |
AzureIoTClient | 88:248736be106e | 2011 | (void)IoTHubClient_LL_UploadToBlob_SetOption(handleData->uploadToBlobHandle, optionName, value); |
AzureIoTClient | 88:248736be106e | 2012 | } |
AzureIoTClient | 88:248736be106e | 2013 | #endif /*DONT_USE_UPLOADTOBLOB*/ |
AzureIoTClient | 88:248736be106e | 2014 | } |
AzureIoTClient | 88:248736be106e | 2015 | } |
AzureIoTClient | 88:248736be106e | 2016 | return result; |
AzureIoTClient | 88:248736be106e | 2017 | } |
AzureIoTClient | 88:248736be106e | 2018 | |
AzureIoTClient | 88:248736be106e | 2019 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_GetOption(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, const char* optionName, void** value) |
AzureIoTClient | 88:248736be106e | 2020 | { |
AzureIoTClient | 88:248736be106e | 2021 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 2022 | |
AzureIoTClient | 88:248736be106e | 2023 | if ((iotHubClientHandle == NULL) || (optionName == NULL) || (value == NULL)) |
AzureIoTClient | 88:248736be106e | 2024 | { |
AzureIoTClient | 88:248736be106e | 2025 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 2026 | LogError("invalid argument iotHubClientHandle(%p); optionName(%p); value(%p)", iotHubClientHandle, optionName, value); |
AzureIoTClient | 88:248736be106e | 2027 | } |
AzureIoTClient | 88:248736be106e | 2028 | else if (strcmp(optionName, OPTION_PRODUCT_INFO) == 0) |
AzureIoTClient | 88:248736be106e | 2029 | { |
AzureIoTClient | 88:248736be106e | 2030 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 2031 | *value = iotHubClientHandle->product_info; |
AzureIoTClient | 88:248736be106e | 2032 | } |
AzureIoTClient | 88:248736be106e | 2033 | else |
AzureIoTClient | 88:248736be106e | 2034 | { |
AzureIoTClient | 88:248736be106e | 2035 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 2036 | LogError("invalid argument (%s)", optionName); |
AzureIoTClient | 88:248736be106e | 2037 | } |
AzureIoTClient | 88:248736be106e | 2038 | return result; |
AzureIoTClient | 88:248736be106e | 2039 | } |
AzureIoTClient | 88:248736be106e | 2040 | |
AzureIoTClient | 88:248736be106e | 2041 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetDeviceTwinCallback(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback, void* userContextCallback) |
AzureIoTClient | 88:248736be106e | 2042 | { |
AzureIoTClient | 88:248736be106e | 2043 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 2044 | /* Codes_SRS_IOTHUBCLIENT_LL_10_001: [ IoTHubClientCore_LL_SetDeviceTwinCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.] */ |
AzureIoTClient | 88:248736be106e | 2045 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 88:248736be106e | 2046 | { |
AzureIoTClient | 88:248736be106e | 2047 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 2048 | LogError("Invalid argument specified iothubClientHandle=%p", iotHubClientHandle); |
AzureIoTClient | 88:248736be106e | 2049 | } |
AzureIoTClient | 88:248736be106e | 2050 | else |
AzureIoTClient | 88:248736be106e | 2051 | { |
AzureIoTClient | 88:248736be106e | 2052 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 2053 | if (deviceTwinCallback == NULL) |
AzureIoTClient | 88:248736be106e | 2054 | { |
AzureIoTClient | 88:248736be106e | 2055 | /* Codes_SRS_IOTHUBCLIENT_LL_10_006: [ If deviceTwinCallback is NULL, then IoTHubClientCore_LL_SetDeviceTwinCallback shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 88:248736be106e | 2056 | handleData->IoTHubTransport_Unsubscribe_DeviceTwin(handleData->transportHandle); |
AzureIoTClient | 88:248736be106e | 2057 | handleData->deviceTwinCallback = NULL; |
AzureIoTClient | 88:248736be106e | 2058 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 2059 | } |
AzureIoTClient | 88:248736be106e | 2060 | else |
AzureIoTClient | 88:248736be106e | 2061 | { |
AzureIoTClient | 88:248736be106e | 2062 | /* Codes_SRS_IOTHUBCLIENT_LL_10_002: [ If deviceTwinCallback is not NULL, then IoTHubClientCore_LL_SetDeviceTwinCallback shall call the underlying layer's _Subscribe function.] */ |
AzureIoTClient | 88:248736be106e | 2063 | if (handleData->IoTHubTransport_Subscribe_DeviceTwin(handleData->transportHandle) == 0) |
AzureIoTClient | 88:248736be106e | 2064 | { |
AzureIoTClient | 88:248736be106e | 2065 | handleData->deviceTwinCallback = deviceTwinCallback; |
AzureIoTClient | 88:248736be106e | 2066 | handleData->deviceTwinContextCallback = userContextCallback; |
AzureIoTClient | 88:248736be106e | 2067 | /* Codes_SRS_IOTHUBCLIENT_LL_10_005: [ Otherwise IoTHubClientCore_LL_SetDeviceTwinCallback shall succeed and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 88:248736be106e | 2068 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 2069 | } |
AzureIoTClient | 88:248736be106e | 2070 | else |
AzureIoTClient | 88:248736be106e | 2071 | { |
AzureIoTClient | 88:248736be106e | 2072 | /* Codes_SRS_IOTHUBCLIENT_LL_10_003: [ If the underlying layer's _Subscribe function fails, then IoTHubClientCore_LL_SetDeviceTwinCallback shall fail and return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 88:248736be106e | 2073 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2074 | } |
AzureIoTClient | 88:248736be106e | 2075 | } |
AzureIoTClient | 88:248736be106e | 2076 | } |
AzureIoTClient | 88:248736be106e | 2077 | return result; |
AzureIoTClient | 88:248736be106e | 2078 | } |
AzureIoTClient | 88:248736be106e | 2079 | |
AzureIoTClient | 88:248736be106e | 2080 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SendReportedState(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, const unsigned char* reportedState, size_t size, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reportedStateCallback, void* userContextCallback) |
AzureIoTClient | 88:248736be106e | 2081 | { |
AzureIoTClient | 88:248736be106e | 2082 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 2083 | /* Codes_SRS_IOTHUBCLIENT_LL_10_012: [ IoTHubClientCore_LL_SendReportedState shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL. ] */ |
AzureIoTClient | 88:248736be106e | 2084 | /* Codes_SRS_IOTHUBCLIENT_LL_10_013: [ IoTHubClientCore_LL_SendReportedState shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter reportedState is NULL] */ |
AzureIoTClient | 88:248736be106e | 2085 | /* Codes_SRS_IOTHUBCLIENT_LL_07_005: [ IoTHubClientCore_LL_SendReportedState shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter size is equal to 0. ] */ |
AzureIoTClient | 88:248736be106e | 2086 | if (iotHubClientHandle == NULL || (reportedState == NULL || size == 0) ) |
AzureIoTClient | 88:248736be106e | 2087 | { |
AzureIoTClient | 88:248736be106e | 2088 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 2089 | LogError("Invalid argument specified iothubClientHandle=%p, reportedState=%p, size=%zu", iotHubClientHandle, reportedState, size); |
AzureIoTClient | 88:248736be106e | 2090 | } |
AzureIoTClient | 88:248736be106e | 2091 | else |
AzureIoTClient | 88:248736be106e | 2092 | { |
AzureIoTClient | 88:248736be106e | 2093 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 2094 | /* Codes_SRS_IOTHUBCLIENT_LL_10_014: [IoTHubClientCore_LL_SendReportedState shall construct and queue the reported a Device_Twin structure for transmition by the underlying transport.] */ |
AzureIoTClient | 88:248736be106e | 2095 | IOTHUB_DEVICE_TWIN* client_data = dev_twin_data_create(handleData, get_next_item_id(handleData), reportedState, size, reportedStateCallback, userContextCallback); |
AzureIoTClient | 88:248736be106e | 2096 | if (client_data == NULL) |
AzureIoTClient | 88:248736be106e | 2097 | { |
AzureIoTClient | 88:248736be106e | 2098 | /* Codes_SRS_IOTHUBCLIENT_LL_10_015: [If any error is encountered IoTHubClientCore_LL_SendReportedState shall return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 88:248736be106e | 2099 | LogError("Failure constructing device twin data"); |
AzureIoTClient | 88:248736be106e | 2100 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2101 | } |
AzureIoTClient | 88:248736be106e | 2102 | else |
AzureIoTClient | 88:248736be106e | 2103 | { |
AzureIoTClient | 88:248736be106e | 2104 | if (handleData->IoTHubTransport_Subscribe_DeviceTwin(handleData->transportHandle) != 0) |
AzureIoTClient | 88:248736be106e | 2105 | { |
AzureIoTClient | 88:248736be106e | 2106 | LogError("Failure adding device twin data to queue"); |
AzureIoTClient | 88:248736be106e | 2107 | device_twin_data_destroy(client_data); |
AzureIoTClient | 88:248736be106e | 2108 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2109 | } |
AzureIoTClient | 88:248736be106e | 2110 | else |
AzureIoTClient | 88:248736be106e | 2111 | { |
AzureIoTClient | 88:248736be106e | 2112 | /* Codes_SRS_IOTHUBCLIENT_LL_07_001: [ IoTHubClientCore_LL_SendReportedState shall queue the constructed reportedState data to be consumed by the targeted transport. ] */ |
AzureIoTClient | 88:248736be106e | 2113 | DList_InsertTailList(&(iotHubClientHandle->iot_msg_queue), &(client_data->entry)); |
AzureIoTClient | 88:248736be106e | 2114 | |
AzureIoTClient | 88:248736be106e | 2115 | /* Codes_SRS_IOTHUBCLIENT_LL_10_016: [ Otherwise IoTHubClientCore_LL_SendReportedState shall succeed and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 88:248736be106e | 2116 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 2117 | } |
AzureIoTClient | 88:248736be106e | 2118 | } |
AzureIoTClient | 88:248736be106e | 2119 | } |
AzureIoTClient | 88:248736be106e | 2120 | return result; |
AzureIoTClient | 88:248736be106e | 2121 | } |
AzureIoTClient | 88:248736be106e | 2122 | |
AzureIoTClient | 88:248736be106e | 2123 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetDeviceMethodCallback(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_DEVICE_METHOD_CALLBACK_ASYNC deviceMethodCallback, void* userContextCallback) |
AzureIoTClient | 88:248736be106e | 2124 | { |
AzureIoTClient | 88:248736be106e | 2125 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 2126 | |
AzureIoTClient | 88:248736be106e | 2127 | /*Codes_SRS_IOTHUBCLIENT_LL_12_017: [ IoTHubClientCore_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL. ] */ |
AzureIoTClient | 88:248736be106e | 2128 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 88:248736be106e | 2129 | { |
AzureIoTClient | 88:248736be106e | 2130 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 2131 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 2132 | } |
AzureIoTClient | 88:248736be106e | 2133 | else |
AzureIoTClient | 88:248736be106e | 2134 | { |
AzureIoTClient | 88:248736be106e | 2135 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 2136 | if (deviceMethodCallback == NULL) |
AzureIoTClient | 88:248736be106e | 2137 | { |
AzureIoTClient | 88:248736be106e | 2138 | if (handleData->methodCallback.type == CALLBACK_TYPE_NONE) |
AzureIoTClient | 88:248736be106e | 2139 | { |
AzureIoTClient | 88:248736be106e | 2140 | /* Codes_SRS_IOTHUBCLIENT_LL_10_029: [ If deviceMethodCallback is NULL and the client is not subscribed to receive method calls, IoTHubClientCore_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ] */ |
AzureIoTClient | 88:248736be106e | 2141 | LogError("not currently set to accept or process incoming messages."); |
AzureIoTClient | 88:248736be106e | 2142 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2143 | } |
AzureIoTClient | 88:248736be106e | 2144 | else if (handleData->methodCallback.type == CALLBACK_TYPE_ASYNC) |
AzureIoTClient | 88:248736be106e | 2145 | { |
AzureIoTClient | 88:248736be106e | 2146 | /* Codes_SRS_IOTHUBCLIENT_LL_10_028: [If the user has subscribed using IoTHubClientCore_LL_SetDeviceMethodCallback_Ex, IoTHubClientCore_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ] */ |
AzureIoTClient | 88:248736be106e | 2147 | LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClientCore_LL_SetDeviceMethodCallback_Ex function."); |
AzureIoTClient | 88:248736be106e | 2148 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2149 | } |
AzureIoTClient | 88:248736be106e | 2150 | else |
AzureIoTClient | 88:248736be106e | 2151 | { |
AzureIoTClient | 88:248736be106e | 2152 | /*Codes_SRS_IOTHUBCLIENT_LL_02_019: [If parameter messageCallback is NULL then IoTHubClientCore_LL_SetMessageCallback shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 88:248736be106e | 2153 | /*Codes_SRS_IOTHUBCLIENT_LL_12_018: [If deviceMethodCallback is NULL, then IoTHubClientCore_LL_SetDeviceMethodCallback shall call the underlying layer's IoTHubTransport_Unsubscribe_DeviceMethod function and return IOTHUB_CLIENT_OK. ] */ |
AzureIoTClient | 88:248736be106e | 2154 | /*Codes_SRS_IOTHUBCLIENT_LL_12_022: [ Otherwise IoTHubClientCore_LL_SetDeviceMethodCallback shall succeed and return IOTHUB_CLIENT_OK. ]*/ |
AzureIoTClient | 88:248736be106e | 2155 | handleData->IoTHubTransport_Unsubscribe_DeviceMethod(handleData->transportHandle); |
AzureIoTClient | 88:248736be106e | 2156 | handleData->methodCallback.type = CALLBACK_TYPE_NONE; |
AzureIoTClient | 88:248736be106e | 2157 | handleData->methodCallback.callbackSync = NULL; |
AzureIoTClient | 88:248736be106e | 2158 | handleData->methodCallback.userContextCallback = NULL; |
AzureIoTClient | 88:248736be106e | 2159 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 2160 | } |
AzureIoTClient | 88:248736be106e | 2161 | } |
AzureIoTClient | 88:248736be106e | 2162 | else |
AzureIoTClient | 88:248736be106e | 2163 | { |
AzureIoTClient | 88:248736be106e | 2164 | if (handleData->methodCallback.type == CALLBACK_TYPE_ASYNC) |
AzureIoTClient | 88:248736be106e | 2165 | { |
AzureIoTClient | 88:248736be106e | 2166 | /* Codes_SRS_IOTHUBCLIENT_LL_10_028: [If the user has subscribed using IoTHubClientCore_LL_SetDeviceMethodCallback_Ex, IoTHubClientCore_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ] */ |
AzureIoTClient | 88:248736be106e | 2167 | LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClientCore_LL_SetDeviceMethodCallback_Ex function before subscribing with IoTHubClientCore_LL_SetDeviceMethodCallback."); |
AzureIoTClient | 88:248736be106e | 2168 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2169 | } |
AzureIoTClient | 88:248736be106e | 2170 | else |
AzureIoTClient | 88:248736be106e | 2171 | { |
AzureIoTClient | 88:248736be106e | 2172 | /*Codes_SRS_IOTHUBCLIENT_LL_12_019: [ If deviceMethodCallback is not NULL, then IoTHubClientCore_LL_SetDeviceMethodCallback shall call the underlying layer's IoTHubTransport_Subscribe_DeviceMethod function. ]*/ |
AzureIoTClient | 88:248736be106e | 2173 | if (handleData->IoTHubTransport_Subscribe_DeviceMethod(handleData->deviceHandle) == 0) |
AzureIoTClient | 88:248736be106e | 2174 | { |
AzureIoTClient | 88:248736be106e | 2175 | /*Codes_SRS_IOTHUBCLIENT_LL_12_022: [ Otherwise IoTHubClientCore_LL_SetDeviceMethodCallback shall succeed and return IOTHUB_CLIENT_OK. ]*/ |
AzureIoTClient | 88:248736be106e | 2176 | handleData->methodCallback.type = CALLBACK_TYPE_SYNC; |
AzureIoTClient | 88:248736be106e | 2177 | handleData->methodCallback.callbackSync = deviceMethodCallback; |
AzureIoTClient | 88:248736be106e | 2178 | handleData->methodCallback.callbackAsync = NULL; |
AzureIoTClient | 88:248736be106e | 2179 | handleData->methodCallback.userContextCallback = userContextCallback; |
AzureIoTClient | 88:248736be106e | 2180 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 2181 | } |
AzureIoTClient | 88:248736be106e | 2182 | else |
AzureIoTClient | 88:248736be106e | 2183 | { |
AzureIoTClient | 88:248736be106e | 2184 | /*Codes_SRS_IOTHUBCLIENT_LL_12_020: [ If the underlying layer's IoTHubTransport_Subscribe_DeviceMethod function fails, then IoTHubClientCore_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ]*/ |
AzureIoTClient | 88:248736be106e | 2185 | /*Codes_SRS_IOTHUBCLIENT_LL_12_021: [ If adding the information fails for any reason, IoTHubClientCore_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ]*/ |
AzureIoTClient | 88:248736be106e | 2186 | LogError("IoTHubTransport_Subscribe_DeviceMethod failed"); |
AzureIoTClient | 88:248736be106e | 2187 | handleData->methodCallback.type = CALLBACK_TYPE_NONE; |
AzureIoTClient | 88:248736be106e | 2188 | handleData->methodCallback.callbackAsync = NULL; |
AzureIoTClient | 88:248736be106e | 2189 | handleData->methodCallback.callbackSync = NULL; |
AzureIoTClient | 88:248736be106e | 2190 | handleData->methodCallback.userContextCallback = NULL; |
AzureIoTClient | 88:248736be106e | 2191 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2192 | } |
AzureIoTClient | 88:248736be106e | 2193 | } |
AzureIoTClient | 88:248736be106e | 2194 | } |
AzureIoTClient | 88:248736be106e | 2195 | } |
AzureIoTClient | 88:248736be106e | 2196 | return result; |
AzureIoTClient | 88:248736be106e | 2197 | } |
AzureIoTClient | 88:248736be106e | 2198 | |
AzureIoTClient | 88:248736be106e | 2199 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetDeviceMethodCallback_Ex(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_INBOUND_DEVICE_METHOD_CALLBACK inboundDeviceMethodCallback, void* userContextCallback) |
AzureIoTClient | 88:248736be106e | 2200 | { |
AzureIoTClient | 88:248736be106e | 2201 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 2202 | /* Codes_SRS_IOTHUBCLIENT_LL_07_021: [ If handle is NULL then IoTHubClientCore_LL_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 88:248736be106e | 2203 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 88:248736be106e | 2204 | { |
AzureIoTClient | 88:248736be106e | 2205 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 2206 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 2207 | } |
AzureIoTClient | 88:248736be106e | 2208 | else |
AzureIoTClient | 88:248736be106e | 2209 | { |
AzureIoTClient | 88:248736be106e | 2210 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 2211 | if (inboundDeviceMethodCallback == NULL) |
AzureIoTClient | 88:248736be106e | 2212 | { |
AzureIoTClient | 88:248736be106e | 2213 | if (handleData->methodCallback.type == CALLBACK_TYPE_NONE) |
AzureIoTClient | 88:248736be106e | 2214 | { |
AzureIoTClient | 88:248736be106e | 2215 | /* Codes_SRS_IOTHUBCLIENT_LL_10_030: [ If deviceMethodCallback is NULL and the client is not subscribed to receive method calls, IoTHubClientCore_LL_SetDeviceMethodCallback shall fail and return IOTHUB_CLIENT_ERROR. ] */ |
AzureIoTClient | 88:248736be106e | 2216 | LogError("not currently set to accept or process incoming messages."); |
AzureIoTClient | 88:248736be106e | 2217 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2218 | } |
AzureIoTClient | 88:248736be106e | 2219 | else if (handleData->methodCallback.type == CALLBACK_TYPE_SYNC) |
AzureIoTClient | 88:248736be106e | 2220 | { |
AzureIoTClient | 88:248736be106e | 2221 | /* Codes_SRS_IOTHUBCLIENT_LL_10_031: [If the user has subscribed using IoTHubClientCore_LL_SetDeviceMethodCallback, IoTHubClientCore_LL_SetDeviceMethodCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR. ] */ |
AzureIoTClient | 88:248736be106e | 2222 | LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClientCore_LL_SetDeviceMethodCallback function."); |
AzureIoTClient | 88:248736be106e | 2223 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2224 | } |
AzureIoTClient | 88:248736be106e | 2225 | else |
AzureIoTClient | 88:248736be106e | 2226 | { |
AzureIoTClient | 88:248736be106e | 2227 | /* Codes_SRS_IOTHUBCLIENT_LL_07_022: [ If inboundDeviceMethodCallback is NULL then IoTHubClientCore_LL_SetDeviceMethodCallback_Ex shall call the underlying layer's IoTHubTransport_Unsubscribe_DeviceMethod function and return IOTHUB_CLIENT_OK.] */ |
AzureIoTClient | 88:248736be106e | 2228 | handleData->IoTHubTransport_Unsubscribe_DeviceMethod(handleData->transportHandle); |
AzureIoTClient | 88:248736be106e | 2229 | handleData->methodCallback.type = CALLBACK_TYPE_NONE; |
AzureIoTClient | 88:248736be106e | 2230 | handleData->methodCallback.callbackAsync = NULL; |
AzureIoTClient | 88:248736be106e | 2231 | handleData->methodCallback.userContextCallback = NULL; |
AzureIoTClient | 88:248736be106e | 2232 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 2233 | } |
AzureIoTClient | 88:248736be106e | 2234 | } |
AzureIoTClient | 88:248736be106e | 2235 | else |
AzureIoTClient | 88:248736be106e | 2236 | { |
AzureIoTClient | 88:248736be106e | 2237 | if (handleData->methodCallback.type == CALLBACK_TYPE_SYNC) |
AzureIoTClient | 88:248736be106e | 2238 | { |
AzureIoTClient | 88:248736be106e | 2239 | /* Codes_SRS_IOTHUBCLIENT_LL_10_031: [If the user has subscribed using IoTHubClientCore_LL_SetDeviceMethodCallback, IoTHubClientCore_LL_SetDeviceMethodCallback_Ex shall fail and return IOTHUB_CLIENT_ERROR. ] */ |
AzureIoTClient | 88:248736be106e | 2240 | LogError("Invalid workflow sequence. Please unsubscribe using the IoTHubClientCore_LL_SetDeviceMethodCallback function before subscribing with IoTHubClientCore_LL_SetDeviceMethodCallback_Ex."); |
AzureIoTClient | 88:248736be106e | 2241 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2242 | } |
AzureIoTClient | 88:248736be106e | 2243 | else |
AzureIoTClient | 88:248736be106e | 2244 | { |
AzureIoTClient | 88:248736be106e | 2245 | /* Codes_SRS_IOTHUBCLIENT_LL_07_023: [ If inboundDeviceMethodCallback is non-NULL then IoTHubClientCore_LL_SetDeviceMethodCallback_Ex shall call the underlying layer's IoTHubTransport_Subscribe_DeviceMethod function.]*/ |
AzureIoTClient | 88:248736be106e | 2246 | if (handleData->IoTHubTransport_Subscribe_DeviceMethod(handleData->deviceHandle) == 0) |
AzureIoTClient | 88:248736be106e | 2247 | { |
AzureIoTClient | 88:248736be106e | 2248 | handleData->methodCallback.type = CALLBACK_TYPE_ASYNC; |
AzureIoTClient | 88:248736be106e | 2249 | handleData->methodCallback.callbackAsync = inboundDeviceMethodCallback; |
AzureIoTClient | 88:248736be106e | 2250 | handleData->methodCallback.callbackSync = NULL; |
AzureIoTClient | 88:248736be106e | 2251 | handleData->methodCallback.userContextCallback = userContextCallback; |
AzureIoTClient | 88:248736be106e | 2252 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 2253 | } |
AzureIoTClient | 88:248736be106e | 2254 | else |
AzureIoTClient | 88:248736be106e | 2255 | { |
AzureIoTClient | 88:248736be106e | 2256 | /* Codes_SRS_IOTHUBCLIENT_LL_07_025: [ If any error is encountered then IoTHubClientCore_LL_SetDeviceMethodCallback_Ex shall return IOTHUB_CLIENT_ERROR.] */ |
AzureIoTClient | 88:248736be106e | 2257 | LogError("IoTHubTransport_Subscribe_DeviceMethod failed"); |
AzureIoTClient | 88:248736be106e | 2258 | handleData->methodCallback.type = CALLBACK_TYPE_NONE; |
AzureIoTClient | 88:248736be106e | 2259 | handleData->methodCallback.callbackAsync = NULL; |
AzureIoTClient | 88:248736be106e | 2260 | handleData->methodCallback.callbackSync = NULL; |
AzureIoTClient | 88:248736be106e | 2261 | handleData->methodCallback.userContextCallback = NULL; |
AzureIoTClient | 88:248736be106e | 2262 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2263 | } |
AzureIoTClient | 88:248736be106e | 2264 | } |
AzureIoTClient | 88:248736be106e | 2265 | } |
AzureIoTClient | 88:248736be106e | 2266 | } |
AzureIoTClient | 88:248736be106e | 2267 | return result; |
AzureIoTClient | 88:248736be106e | 2268 | } |
AzureIoTClient | 88:248736be106e | 2269 | |
AzureIoTClient | 88:248736be106e | 2270 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_DeviceMethodResponse(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, METHOD_HANDLE methodId, const unsigned char* response, size_t response_size, int status_response) |
AzureIoTClient | 88:248736be106e | 2271 | { |
AzureIoTClient | 88:248736be106e | 2272 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 2273 | /* Codes_SRS_IOTHUBCLIENT_LL_07_026: [ If handle or methodId is NULL then IoTHubClientCore_LL_DeviceMethodResponse shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 88:248736be106e | 2274 | if (iotHubClientHandle == NULL || methodId == NULL) |
AzureIoTClient | 88:248736be106e | 2275 | { |
AzureIoTClient | 88:248736be106e | 2276 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 2277 | LOG_ERROR_RESULT; |
AzureIoTClient | 88:248736be106e | 2278 | } |
AzureIoTClient | 88:248736be106e | 2279 | else |
AzureIoTClient | 88:248736be106e | 2280 | { |
AzureIoTClient | 88:248736be106e | 2281 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 88:248736be106e | 2282 | /* Codes_SRS_IOTHUBCLIENT_LL_07_027: [ IoTHubClientCore_LL_DeviceMethodResponse shall call the IoTHubTransport_DeviceMethod_Response transport function.] */ |
AzureIoTClient | 88:248736be106e | 2283 | if (handleData->IoTHubTransport_DeviceMethod_Response(handleData->deviceHandle, methodId, response, response_size, status_response) != 0) |
AzureIoTClient | 88:248736be106e | 2284 | { |
AzureIoTClient | 88:248736be106e | 2285 | LogError("IoTHubTransport_DeviceMethod_Response failed"); |
AzureIoTClient | 88:248736be106e | 2286 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 88:248736be106e | 2287 | } |
AzureIoTClient | 88:248736be106e | 2288 | else |
AzureIoTClient | 88:248736be106e | 2289 | { |
AzureIoTClient | 88:248736be106e | 2290 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 88:248736be106e | 2291 | } |
AzureIoTClient | 88:248736be106e | 2292 | } |
AzureIoTClient | 88:248736be106e | 2293 | return result; |
AzureIoTClient | 88:248736be106e | 2294 | } |
AzureIoTClient | 88:248736be106e | 2295 | |
AzureIoTClient | 88:248736be106e | 2296 | #ifndef DONT_USE_UPLOADTOBLOB |
AzureIoTClient | 88:248736be106e | 2297 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_UploadToBlob(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, const char* destinationFileName, const unsigned char* source, size_t size) |
AzureIoTClient | 88:248736be106e | 2298 | { |
AzureIoTClient | 88:248736be106e | 2299 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 2300 | /*Codes_SRS_IOTHUBCLIENT_LL_02_061: [ If iotHubClientHandle is NULL then IoTHubClientCore_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/ |
AzureIoTClient | 88:248736be106e | 2301 | /*Codes_SRS_IOTHUBCLIENT_LL_02_062: [ If destinationFileName is NULL then IoTHubClientCore_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/ |
AzureIoTClient | 88:248736be106e | 2302 | /*Codes_SRS_IOTHUBCLIENT_LL_02_063: [ If `source` is `NULL` and size is greater than 0 then `IoTHubClientCore_LL_UploadToBlob` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ |
AzureIoTClient | 88:248736be106e | 2303 | if ( |
AzureIoTClient | 88:248736be106e | 2304 | (iotHubClientHandle == NULL) || |
AzureIoTClient | 88:248736be106e | 2305 | (destinationFileName == NULL) || |
AzureIoTClient | 88:248736be106e | 2306 | ((source == NULL) && (size >0)) |
AzureIoTClient | 88:248736be106e | 2307 | ) |
AzureIoTClient | 88:248736be106e | 2308 | { |
AzureIoTClient | 88:248736be106e | 2309 | LogError("invalid parameters IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle=%p, const char* destinationFileName=%s, const unsigned char* source=%p, size_t size=%zu", iotHubClientHandle, destinationFileName, source, size); |
AzureIoTClient | 88:248736be106e | 2310 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 2311 | } |
AzureIoTClient | 88:248736be106e | 2312 | else |
AzureIoTClient | 88:248736be106e | 2313 | { |
AzureIoTClient | 88:248736be106e | 2314 | result = IoTHubClient_LL_UploadToBlob_Impl(iotHubClientHandle->uploadToBlobHandle, destinationFileName, source, size); |
AzureIoTClient | 88:248736be106e | 2315 | } |
AzureIoTClient | 88:248736be106e | 2316 | return result; |
AzureIoTClient | 88:248736be106e | 2317 | } |
AzureIoTClient | 88:248736be106e | 2318 | |
AzureIoTClient | 88:248736be106e | 2319 | typedef struct UPLOAD_MULTIPLE_BLOCKS_WRAPPER_CONTEXT_TAG |
AzureIoTClient | 88:248736be106e | 2320 | { |
AzureIoTClient | 88:248736be106e | 2321 | IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_CALLBACK getDataCallback; |
AzureIoTClient | 88:248736be106e | 2322 | void* context; |
AzureIoTClient | 88:248736be106e | 2323 | } UPLOAD_MULTIPLE_BLOCKS_WRAPPER_CONTEXT; |
AzureIoTClient | 88:248736be106e | 2324 | |
AzureIoTClient | 88:248736be106e | 2325 | |
AzureIoTClient | 88:248736be106e | 2326 | static IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_RESULT uploadMultipleBlocksCallbackWrapper(IOTHUB_CLIENT_FILE_UPLOAD_RESULT result, unsigned char const ** data, size_t* size, void* context) |
AzureIoTClient | 88:248736be106e | 2327 | { |
AzureIoTClient | 88:248736be106e | 2328 | UPLOAD_MULTIPLE_BLOCKS_WRAPPER_CONTEXT* wrapperContext = (UPLOAD_MULTIPLE_BLOCKS_WRAPPER_CONTEXT*)context; |
AzureIoTClient | 88:248736be106e | 2329 | wrapperContext->getDataCallback(result, data, size, wrapperContext->context); |
AzureIoTClient | 88:248736be106e | 2330 | return IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_OK; |
AzureIoTClient | 88:248736be106e | 2331 | } |
AzureIoTClient | 88:248736be106e | 2332 | |
AzureIoTClient | 88:248736be106e | 2333 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_UploadMultipleBlocksToBlob(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, const char* destinationFileName, IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_CALLBACK getDataCallback, void* context) |
AzureIoTClient | 88:248736be106e | 2334 | { |
AzureIoTClient | 88:248736be106e | 2335 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 2336 | /*Codes_SRS_IOTHUBCLIENT_LL_99_005: [ If `iotHubClientHandle` is `NULL` then `IoTHubClientCore_LL_UploadMultipleBlocksToBlob(Ex)` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ |
AzureIoTClient | 88:248736be106e | 2337 | /*Codes_SRS_IOTHUBCLIENT_LL_99_006: [ If `destinationFileName` is `NULL` then `IoTHubClientCore_LL_UploadMultipleBlocksToBlob(Ex)` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ |
AzureIoTClient | 88:248736be106e | 2338 | /*Codes_SRS_IOTHUBCLIENT_LL_99_007: [ If `getDataCallback` is `NULL` then `IoTHubClientCore_LL_UploadMultipleBlocksToBlob(Ex)` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ |
AzureIoTClient | 88:248736be106e | 2339 | if ( |
AzureIoTClient | 88:248736be106e | 2340 | (iotHubClientHandle == NULL) || |
AzureIoTClient | 88:248736be106e | 2341 | (destinationFileName == NULL) || |
AzureIoTClient | 88:248736be106e | 2342 | (getDataCallback == NULL) |
AzureIoTClient | 88:248736be106e | 2343 | ) |
AzureIoTClient | 88:248736be106e | 2344 | { |
AzureIoTClient | 88:248736be106e | 2345 | LogError("invalid parameters IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle=%p, const char* destinationFileName=%p, getDataCallback=%p", iotHubClientHandle, destinationFileName, getDataCallback); |
AzureIoTClient | 88:248736be106e | 2346 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 2347 | } |
AzureIoTClient | 88:248736be106e | 2348 | else |
AzureIoTClient | 88:248736be106e | 2349 | { |
AzureIoTClient | 88:248736be106e | 2350 | UPLOAD_MULTIPLE_BLOCKS_WRAPPER_CONTEXT uploadMultipleBlocksWrapperContext; |
AzureIoTClient | 88:248736be106e | 2351 | uploadMultipleBlocksWrapperContext.getDataCallback = getDataCallback; |
AzureIoTClient | 88:248736be106e | 2352 | uploadMultipleBlocksWrapperContext.context = context; |
AzureIoTClient | 88:248736be106e | 2353 | |
AzureIoTClient | 88:248736be106e | 2354 | result = IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(iotHubClientHandle->uploadToBlobHandle, destinationFileName, uploadMultipleBlocksCallbackWrapper, &uploadMultipleBlocksWrapperContext); |
AzureIoTClient | 88:248736be106e | 2355 | } |
AzureIoTClient | 88:248736be106e | 2356 | return result; |
AzureIoTClient | 88:248736be106e | 2357 | } |
AzureIoTClient | 88:248736be106e | 2358 | |
AzureIoTClient | 88:248736be106e | 2359 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_UploadMultipleBlocksToBlobEx(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, const char* destinationFileName, IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_CALLBACK_EX getDataCallbackEx, void* context) |
AzureIoTClient | 88:248736be106e | 2360 | { |
AzureIoTClient | 88:248736be106e | 2361 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 88:248736be106e | 2362 | /*Codes_SRS_IOTHUBCLIENT_LL_99_005: [ If `iotHubClientHandle` is `NULL` then `IoTHubClientCore_LL_UploadMultipleBlocksToBlob(Ex)` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ |
AzureIoTClient | 88:248736be106e | 2363 | /*Codes_SRS_IOTHUBCLIENT_LL_99_006: [ If `destinationFileName` is `NULL` then `IoTHubClientCore_LL_UploadMultipleBlocksToBlob(Ex)` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ |
AzureIoTClient | 88:248736be106e | 2364 | /*Codes_SRS_IOTHUBCLIENT_LL_99_007: [ If `getDataCallback` is `NULL` then `IoTHubClientCore_LL_UploadMultipleBlocksToBlob(Ex)` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ |
AzureIoTClient | 88:248736be106e | 2365 | if ( |
AzureIoTClient | 88:248736be106e | 2366 | (iotHubClientHandle == NULL) || |
AzureIoTClient | 88:248736be106e | 2367 | (destinationFileName == NULL) || |
AzureIoTClient | 88:248736be106e | 2368 | (getDataCallbackEx == NULL) |
AzureIoTClient | 88:248736be106e | 2369 | ) |
AzureIoTClient | 88:248736be106e | 2370 | { |
AzureIoTClient | 88:248736be106e | 2371 | LogError("invalid parameters IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle=%p, destinationFileName=%p, getDataCallbackEx=%p", iotHubClientHandle, destinationFileName, getDataCallbackEx); |
AzureIoTClient | 88:248736be106e | 2372 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 88:248736be106e | 2373 | } |
AzureIoTClient | 88:248736be106e | 2374 | else |
AzureIoTClient | 88:248736be106e | 2375 | { |
AzureIoTClient | 88:248736be106e | 2376 | result = IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(iotHubClientHandle->uploadToBlobHandle, destinationFileName, getDataCallbackEx, context); |
AzureIoTClient | 88:248736be106e | 2377 | } |
AzureIoTClient | 88:248736be106e | 2378 | return result; |
AzureIoTClient | 88:248736be106e | 2379 | } |
AzureIoTClient | 88:248736be106e | 2380 | |
AzureIoTClient | 89:a2ed767a532e | 2381 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SendEventToOutputAsync(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, IOTHUB_MESSAGE_HANDLE eventMessageHandle, const char* outputName, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK eventConfirmationCallback, void* userContextCallback) |
AzureIoTClient | 89:a2ed767a532e | 2382 | { |
AzureIoTClient | 89:a2ed767a532e | 2383 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 89:a2ed767a532e | 2384 | |
AzureIoTClient | 89:a2ed767a532e | 2385 | if ((iotHubClientHandle == NULL) || (outputName == NULL) || (eventMessageHandle == NULL) || ((eventConfirmationCallback == NULL) && (userContextCallback != NULL))) |
AzureIoTClient | 89:a2ed767a532e | 2386 | { |
AzureIoTClient | 89:a2ed767a532e | 2387 | // Codes_SRS_IOTHUBCLIENT_LL_31_127: [ If `iotHubClientHandle`, `outputName`, or `eventConfirmationCallback` is `NULL`, `IoTHubClient_LL_SendEventToOutputAsync` shall return `IOTHUB_CLIENT_INVALID_ARG`. ] |
AzureIoTClient | 89:a2ed767a532e | 2388 | LogError("Invalid argument (iotHubClientHandle=%p, outputName=%p, eventMessageHandle=%p)", iotHubClientHandle, outputName, eventMessageHandle); |
AzureIoTClient | 89:a2ed767a532e | 2389 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 89:a2ed767a532e | 2390 | } |
AzureIoTClient | 89:a2ed767a532e | 2391 | else |
AzureIoTClient | 89:a2ed767a532e | 2392 | { |
AzureIoTClient | 89:a2ed767a532e | 2393 | // Codes_SRS_IOTHUBCLIENT_LL_31_128: [ `IoTHubClient_LL_SendEventToOutputAsync` shall set the outputName of the message to send. ] |
AzureIoTClient | 89:a2ed767a532e | 2394 | if (IoTHubMessage_SetOutputName(eventMessageHandle, outputName) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 89:a2ed767a532e | 2395 | { |
AzureIoTClient | 89:a2ed767a532e | 2396 | LogError("IoTHubMessage_SetOutputName failed"); |
AzureIoTClient | 89:a2ed767a532e | 2397 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2398 | } |
AzureIoTClient | 89:a2ed767a532e | 2399 | // Codes_SRS_IOTHUBCLIENT_LL_31_129: [ `IoTHubClient_LL_SendEventToOutputAsync` shall invoke `IoTHubClient_LL_SendEventAsync` to send the message. ] |
AzureIoTClient | 89:a2ed767a532e | 2400 | else if ((result = IoTHubClientCore_LL_SendEventAsync(iotHubClientHandle, eventMessageHandle, eventConfirmationCallback, userContextCallback)) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 89:a2ed767a532e | 2401 | { |
AzureIoTClient | 89:a2ed767a532e | 2402 | LogError("Call into IoTHubClient_LL_SendEventAsync failed, result=%d", result); |
AzureIoTClient | 89:a2ed767a532e | 2403 | } |
AzureIoTClient | 89:a2ed767a532e | 2404 | } |
AzureIoTClient | 89:a2ed767a532e | 2405 | |
AzureIoTClient | 89:a2ed767a532e | 2406 | return result; |
AzureIoTClient | 89:a2ed767a532e | 2407 | } |
AzureIoTClient | 89:a2ed767a532e | 2408 | |
AzureIoTClient | 89:a2ed767a532e | 2409 | |
AzureIoTClient | 89:a2ed767a532e | 2410 | static IOTHUB_CLIENT_RESULT create_event_handler_callback(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData, const char* inputName, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC callbackSync, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC_EX callbackSyncEx, void* userContextCallback, void* userContextCallbackEx, size_t userContextCallbackExLength) |
AzureIoTClient | 89:a2ed767a532e | 2411 | { |
AzureIoTClient | 89:a2ed767a532e | 2412 | IOTHUB_CLIENT_RESULT result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2413 | bool add_to_list = false; |
AzureIoTClient | 89:a2ed767a532e | 2414 | |
AzureIoTClient | 89:a2ed767a532e | 2415 | if ((handleData->event_callbacks == NULL) && ((handleData->event_callbacks = singlylinkedlist_create()) == NULL)) |
AzureIoTClient | 89:a2ed767a532e | 2416 | { |
AzureIoTClient | 89:a2ed767a532e | 2417 | LogError("Could not allocate linked list for callbacks"); |
AzureIoTClient | 89:a2ed767a532e | 2418 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2419 | } |
AzureIoTClient | 89:a2ed767a532e | 2420 | else |
AzureIoTClient | 89:a2ed767a532e | 2421 | { |
AzureIoTClient | 89:a2ed767a532e | 2422 | IOTHUB_EVENT_CALLBACK* event_callback = NULL; |
AzureIoTClient | 89:a2ed767a532e | 2423 | LIST_ITEM_HANDLE item_handle = singlylinkedlist_find(handleData->event_callbacks, is_event_equal_for_match, (const void*)inputName); |
AzureIoTClient | 89:a2ed767a532e | 2424 | if (item_handle == NULL) |
AzureIoTClient | 89:a2ed767a532e | 2425 | { |
AzureIoTClient | 89:a2ed767a532e | 2426 | // Codes_SRS_IOTHUBCLIENT_LL_31_134: [ `IoTHubClient_LL_SetInputMessageCallback` shall allocate a callback handle to associate callbacks from the transport => client if `inputName` isn't already present in the callback list. ] |
AzureIoTClient | 89:a2ed767a532e | 2427 | event_callback = (IOTHUB_EVENT_CALLBACK*)malloc(sizeof(IOTHUB_EVENT_CALLBACK)); |
AzureIoTClient | 89:a2ed767a532e | 2428 | if (event_callback == NULL) |
AzureIoTClient | 89:a2ed767a532e | 2429 | { |
AzureIoTClient | 89:a2ed767a532e | 2430 | LogError("Could not allocate IOTHUB_EVENT_CALLBACK"); |
AzureIoTClient | 89:a2ed767a532e | 2431 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2432 | } |
AzureIoTClient | 89:a2ed767a532e | 2433 | else |
AzureIoTClient | 89:a2ed767a532e | 2434 | { |
AzureIoTClient | 89:a2ed767a532e | 2435 | memset(event_callback, 0, sizeof(*event_callback)); |
AzureIoTClient | 89:a2ed767a532e | 2436 | add_to_list = true; |
AzureIoTClient | 89:a2ed767a532e | 2437 | } |
AzureIoTClient | 89:a2ed767a532e | 2438 | } |
AzureIoTClient | 89:a2ed767a532e | 2439 | else |
AzureIoTClient | 89:a2ed767a532e | 2440 | { |
AzureIoTClient | 89:a2ed767a532e | 2441 | // Codes_SRS_IOTHUBCLIENT_LL_31_135: [ `IoTHubClient_LL_SetInputMessageCallback` shall reuse the existing callback handle if `inputName` is already present in the callback list. ] |
AzureIoTClient | 89:a2ed767a532e | 2442 | event_callback = (IOTHUB_EVENT_CALLBACK*)singlylinkedlist_item_get_value(item_handle); |
AzureIoTClient | 89:a2ed767a532e | 2443 | if (event_callback == NULL) |
AzureIoTClient | 89:a2ed767a532e | 2444 | { |
AzureIoTClient | 89:a2ed767a532e | 2445 | LogError("singlylinkedlist_item_get_value failed looking up event callback"); |
AzureIoTClient | 89:a2ed767a532e | 2446 | } |
AzureIoTClient | 89:a2ed767a532e | 2447 | } |
AzureIoTClient | 89:a2ed767a532e | 2448 | |
AzureIoTClient | 89:a2ed767a532e | 2449 | if (event_callback != NULL) |
AzureIoTClient | 89:a2ed767a532e | 2450 | { |
AzureIoTClient | 89:a2ed767a532e | 2451 | if ((inputName != NULL) && (event_callback->inputName == NULL)) |
AzureIoTClient | 89:a2ed767a532e | 2452 | { |
AzureIoTClient | 89:a2ed767a532e | 2453 | event_callback->inputName = STRING_construct(inputName); |
AzureIoTClient | 89:a2ed767a532e | 2454 | } |
AzureIoTClient | 89:a2ed767a532e | 2455 | |
AzureIoTClient | 89:a2ed767a532e | 2456 | if ((inputName == NULL) || (event_callback->inputName != NULL)) |
AzureIoTClient | 89:a2ed767a532e | 2457 | { |
AzureIoTClient | 89:a2ed767a532e | 2458 | event_callback->callbackAsync = callbackSync; |
AzureIoTClient | 89:a2ed767a532e | 2459 | event_callback->callbackAsyncEx = callbackSyncEx; |
AzureIoTClient | 89:a2ed767a532e | 2460 | |
AzureIoTClient | 89:a2ed767a532e | 2461 | free(event_callback->userContextCallbackEx); |
AzureIoTClient | 89:a2ed767a532e | 2462 | event_callback->userContextCallbackEx = NULL; |
AzureIoTClient | 89:a2ed767a532e | 2463 | |
AzureIoTClient | 89:a2ed767a532e | 2464 | if (userContextCallbackEx == NULL) |
AzureIoTClient | 89:a2ed767a532e | 2465 | { |
AzureIoTClient | 89:a2ed767a532e | 2466 | event_callback->userContextCallback = userContextCallback; |
AzureIoTClient | 89:a2ed767a532e | 2467 | } |
AzureIoTClient | 89:a2ed767a532e | 2468 | |
AzureIoTClient | 89:a2ed767a532e | 2469 | if ((userContextCallbackEx != NULL) && |
AzureIoTClient | 89:a2ed767a532e | 2470 | (NULL == (event_callback->userContextCallbackEx = malloc(userContextCallbackExLength)))) |
AzureIoTClient | 89:a2ed767a532e | 2471 | { |
AzureIoTClient | 89:a2ed767a532e | 2472 | LogError("Unable to allocate userContextCallback"); |
AzureIoTClient | 89:a2ed767a532e | 2473 | delete_event(event_callback); |
AzureIoTClient | 89:a2ed767a532e | 2474 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2475 | } |
AzureIoTClient | 89:a2ed767a532e | 2476 | else if ((add_to_list == true) && (NULL == singlylinkedlist_add(handleData->event_callbacks, event_callback))) |
AzureIoTClient | 89:a2ed767a532e | 2477 | { |
AzureIoTClient | 89:a2ed767a532e | 2478 | LogError("Unable to add eventCallback to list"); |
AzureIoTClient | 89:a2ed767a532e | 2479 | delete_event(event_callback); |
AzureIoTClient | 89:a2ed767a532e | 2480 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2481 | } |
AzureIoTClient | 89:a2ed767a532e | 2482 | else |
AzureIoTClient | 89:a2ed767a532e | 2483 | { |
AzureIoTClient | 89:a2ed767a532e | 2484 | if (userContextCallbackEx != NULL) |
AzureIoTClient | 89:a2ed767a532e | 2485 | { |
AzureIoTClient | 89:a2ed767a532e | 2486 | // Codes_SRS_IOTHUBCLIENT_LL_31_141: [`IoTHubClient_LL_SetInputMessageCallbackEx` shall copy the data passed in extended context. ] |
AzureIoTClient | 89:a2ed767a532e | 2487 | memcpy(event_callback->userContextCallbackEx, userContextCallbackEx, userContextCallbackExLength); |
AzureIoTClient | 89:a2ed767a532e | 2488 | } |
AzureIoTClient | 89:a2ed767a532e | 2489 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 89:a2ed767a532e | 2490 | } |
AzureIoTClient | 89:a2ed767a532e | 2491 | } |
AzureIoTClient | 89:a2ed767a532e | 2492 | else |
AzureIoTClient | 89:a2ed767a532e | 2493 | { |
AzureIoTClient | 89:a2ed767a532e | 2494 | delete_event(event_callback); |
AzureIoTClient | 89:a2ed767a532e | 2495 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2496 | } |
AzureIoTClient | 89:a2ed767a532e | 2497 | } |
AzureIoTClient | 89:a2ed767a532e | 2498 | } |
AzureIoTClient | 89:a2ed767a532e | 2499 | |
AzureIoTClient | 89:a2ed767a532e | 2500 | return result; |
AzureIoTClient | 89:a2ed767a532e | 2501 | } |
AzureIoTClient | 89:a2ed767a532e | 2502 | |
AzureIoTClient | 89:a2ed767a532e | 2503 | static IOTHUB_CLIENT_RESULT remove_event_unsubscribe_if_needed(IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData, const char* inputName) |
AzureIoTClient | 89:a2ed767a532e | 2504 | { |
AzureIoTClient | 89:a2ed767a532e | 2505 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 89:a2ed767a532e | 2506 | |
AzureIoTClient | 89:a2ed767a532e | 2507 | LIST_ITEM_HANDLE item_handle = singlylinkedlist_find(handleData->event_callbacks, is_event_equal_for_match, (const void*)inputName); |
AzureIoTClient | 89:a2ed767a532e | 2508 | if (item_handle == NULL) |
AzureIoTClient | 89:a2ed767a532e | 2509 | { |
AzureIoTClient | 89:a2ed767a532e | 2510 | // Codes_SRS_IOTHUBCLIENT_LL_31_132: [ If `eventHandlerCallback` is NULL, `IoTHubClient_LL_SetInputMessageCallback` shall return `IOTHUB_CLIENT_ERROR` if the `inputName` is not present. ] |
AzureIoTClient | 89:a2ed767a532e | 2511 | LogError("Input name %s was not present", inputName); |
AzureIoTClient | 89:a2ed767a532e | 2512 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2513 | } |
AzureIoTClient | 89:a2ed767a532e | 2514 | else |
AzureIoTClient | 89:a2ed767a532e | 2515 | { |
AzureIoTClient | 89:a2ed767a532e | 2516 | IOTHUB_EVENT_CALLBACK* event_callback = (IOTHUB_EVENT_CALLBACK*)singlylinkedlist_item_get_value(item_handle); |
AzureIoTClient | 89:a2ed767a532e | 2517 | if (event_callback == NULL) |
AzureIoTClient | 89:a2ed767a532e | 2518 | { |
AzureIoTClient | 89:a2ed767a532e | 2519 | LogError("singlylinkedlist_item_get_value failed"); |
AzureIoTClient | 89:a2ed767a532e | 2520 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2521 | } |
AzureIoTClient | 89:a2ed767a532e | 2522 | else |
AzureIoTClient | 89:a2ed767a532e | 2523 | { |
AzureIoTClient | 89:a2ed767a532e | 2524 | delete_event(event_callback); |
AzureIoTClient | 89:a2ed767a532e | 2525 | // Codes_SRS_IOTHUBCLIENT_LL_31_131: [ If `eventHandlerCallback` is NULL, `IoTHubClient_LL_SetInputMessageCallback` shall remove the `inputName` from its callback list if present. ] |
AzureIoTClient | 89:a2ed767a532e | 2526 | if (singlylinkedlist_remove(handleData->event_callbacks, item_handle) != 0) |
AzureIoTClient | 89:a2ed767a532e | 2527 | { |
AzureIoTClient | 89:a2ed767a532e | 2528 | LogError("singlylinkedlist_remove failed"); |
AzureIoTClient | 89:a2ed767a532e | 2529 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2530 | } |
AzureIoTClient | 89:a2ed767a532e | 2531 | else |
AzureIoTClient | 89:a2ed767a532e | 2532 | { |
AzureIoTClient | 89:a2ed767a532e | 2533 | if (singlylinkedlist_get_head_item(handleData->event_callbacks) == NULL) |
AzureIoTClient | 89:a2ed767a532e | 2534 | { |
AzureIoTClient | 89:a2ed767a532e | 2535 | // Codes_SRS_IOTHUBCLIENT_LL_31_133: [ If `eventHandlerCallback` is NULL, `IoTHubClient_LL_SetInputMessageCallback` shall invoke `IoTHubTransport_Unsubscribe_InputQueue` if this was the last input callback. ] |
AzureIoTClient | 89:a2ed767a532e | 2536 | handleData->IoTHubTransport_Unsubscribe_InputQueue(handleData); |
AzureIoTClient | 89:a2ed767a532e | 2537 | } |
AzureIoTClient | 89:a2ed767a532e | 2538 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 89:a2ed767a532e | 2539 | } |
AzureIoTClient | 89:a2ed767a532e | 2540 | } |
AzureIoTClient | 89:a2ed767a532e | 2541 | } |
AzureIoTClient | 89:a2ed767a532e | 2542 | |
AzureIoTClient | 89:a2ed767a532e | 2543 | return result; |
AzureIoTClient | 89:a2ed767a532e | 2544 | } |
AzureIoTClient | 89:a2ed767a532e | 2545 | |
AzureIoTClient | 89:a2ed767a532e | 2546 | |
AzureIoTClient | 89:a2ed767a532e | 2547 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetInputMessageCallbackImpl(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, const char* inputName, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC eventHandlerCallback, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC_EX eventHandlerCallbackEx, void *userContextCallback, void *userContextCallbackEx, size_t userContextCallbackExLength) |
AzureIoTClient | 89:a2ed767a532e | 2548 | { |
AzureIoTClient | 89:a2ed767a532e | 2549 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 89:a2ed767a532e | 2550 | |
AzureIoTClient | 89:a2ed767a532e | 2551 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 89:a2ed767a532e | 2552 | { |
AzureIoTClient | 89:a2ed767a532e | 2553 | // Codes_SRS_IOTHUBCLIENT_LL_31_130: [ If `iotHubClientHandle` or `inputName` is NULL, `IoTHubClient_LL_SetInputMessageCallback` shall return IOTHUB_CLIENT_INVALID_ARG. ] |
AzureIoTClient | 89:a2ed767a532e | 2554 | LogError("Invalid argument - iotHubClientHandle=%p, inputName=%p", iotHubClientHandle, inputName); |
AzureIoTClient | 89:a2ed767a532e | 2555 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 89:a2ed767a532e | 2556 | } |
AzureIoTClient | 89:a2ed767a532e | 2557 | else |
AzureIoTClient | 89:a2ed767a532e | 2558 | { |
AzureIoTClient | 89:a2ed767a532e | 2559 | IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_CORE_LL_HANDLE_DATA*)iotHubClientHandle; |
AzureIoTClient | 89:a2ed767a532e | 2560 | if ((eventHandlerCallback == NULL) && (eventHandlerCallbackEx == NULL)) |
AzureIoTClient | 89:a2ed767a532e | 2561 | { |
AzureIoTClient | 89:a2ed767a532e | 2562 | result = (IOTHUB_CLIENT_RESULT)remove_event_unsubscribe_if_needed(handleData, inputName); |
AzureIoTClient | 89:a2ed767a532e | 2563 | } |
AzureIoTClient | 89:a2ed767a532e | 2564 | else |
AzureIoTClient | 89:a2ed767a532e | 2565 | { |
AzureIoTClient | 89:a2ed767a532e | 2566 | bool registered_with_transport_handler = (handleData->event_callbacks != NULL) && (singlylinkedlist_get_head_item(handleData->event_callbacks) != NULL); |
AzureIoTClient | 89:a2ed767a532e | 2567 | if ((result = (IOTHUB_CLIENT_RESULT)create_event_handler_callback(handleData, inputName, eventHandlerCallback, eventHandlerCallbackEx, userContextCallback, userContextCallbackEx, userContextCallbackExLength)) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 89:a2ed767a532e | 2568 | { |
AzureIoTClient | 89:a2ed767a532e | 2569 | LogError("create_event_handler_callback call failed, error = %d", result); |
AzureIoTClient | 89:a2ed767a532e | 2570 | } |
AzureIoTClient | 89:a2ed767a532e | 2571 | // Codes_SRS_IOTHUBCLIENT_LL_31_136: [ `IoTHubClient_LL_SetInputMessageCallback` shall invoke `IoTHubTransport_Subscribe_InputQueue` if this is the first callback being registered. ] |
AzureIoTClient | 89:a2ed767a532e | 2572 | else if (!registered_with_transport_handler && (handleData->IoTHubTransport_Subscribe_InputQueue(handleData->deviceHandle) != 0)) |
AzureIoTClient | 89:a2ed767a532e | 2573 | { |
AzureIoTClient | 89:a2ed767a532e | 2574 | LogError("IoTHubTransport_Subscribe_InputQueue failed"); |
AzureIoTClient | 89:a2ed767a532e | 2575 | delete_event_callback_list(handleData); |
AzureIoTClient | 89:a2ed767a532e | 2576 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 89:a2ed767a532e | 2577 | } |
AzureIoTClient | 89:a2ed767a532e | 2578 | else |
AzureIoTClient | 89:a2ed767a532e | 2579 | { |
AzureIoTClient | 89:a2ed767a532e | 2580 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 89:a2ed767a532e | 2581 | } |
AzureIoTClient | 89:a2ed767a532e | 2582 | } |
AzureIoTClient | 89:a2ed767a532e | 2583 | } |
AzureIoTClient | 89:a2ed767a532e | 2584 | return result; |
AzureIoTClient | 89:a2ed767a532e | 2585 | |
AzureIoTClient | 89:a2ed767a532e | 2586 | } |
AzureIoTClient | 89:a2ed767a532e | 2587 | |
AzureIoTClient | 89:a2ed767a532e | 2588 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetInputMessageCallbackEx(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, const char* inputName, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC_EX eventHandlerCallbackEx, void *userContextCallbackEx, size_t userContextCallbackExLength) |
AzureIoTClient | 89:a2ed767a532e | 2589 | { |
AzureIoTClient | 89:a2ed767a532e | 2590 | return IoTHubClientCore_LL_SetInputMessageCallbackImpl(iotHubClientHandle, inputName, NULL, eventHandlerCallbackEx, NULL, userContextCallbackEx, userContextCallbackExLength); |
AzureIoTClient | 89:a2ed767a532e | 2591 | } |
AzureIoTClient | 89:a2ed767a532e | 2592 | |
AzureIoTClient | 89:a2ed767a532e | 2593 | IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_SetInputMessageCallback(IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, const char* inputName, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC eventHandlerCallback, void* userContextCallback) |
AzureIoTClient | 89:a2ed767a532e | 2594 | { |
AzureIoTClient | 89:a2ed767a532e | 2595 | return IoTHubClientCore_LL_SetInputMessageCallbackImpl(iotHubClientHandle, inputName, eventHandlerCallback, NULL, userContextCallback, NULL, 0); |
AzureIoTClient | 89:a2ed767a532e | 2596 | } |
AzureIoTClient | 89:a2ed767a532e | 2597 | |
AzureIoTClient | 88:248736be106e | 2598 | |
AzureIoTClient | 88:248736be106e | 2599 | |
AzureIoTClient | 88:248736be106e | 2600 | #endif /* DONT_USE_UPLOADTOBLOB */ |