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