int prov_transport_common_mqtt_open(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* registration_id, BUFFER_HANDLE ek, BUFFER_HANDLE srk, PROV_DEVICE_TRANSPORT_REGISTER_CALLBACK data_callback, void* user_ctx, PROV_DEVICE_TRANSPORT_STATUS_CALLBACK status_cb, void* status_ctx) { int result; PROV_TRANSPORT_MQTT_INFO* mqtt_info = (PROV_TRANSPORT_MQTT_INFO*)handle; if (mqtt_info == NULL || data_callback == NULL || status_cb == NULL || registration_id == NULL) { /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_007: [ If handle, data_callback, or status_cb is NULL, prov_transport_common_mqtt_open shall return a non-zero value. ] */ LogError("Invalid parameter specified handle: %p, data_callback: %p, status_cb: %p, registration_id: %p", handle, data_callback, status_cb, registration_id); result = __FAILURE__; } else if ( (mqtt_info->hsm_type == TRANSPORT_HSM_TYPE_TPM) && (ek == NULL || srk == NULL) ) { /* Codes_PROV_TRANSPORT_MQTT_COMMON_07_008: [ If hsm_type is TRANSPORT_HSM_TYPE_TPM and ek or srk is NULL, prov_transport_common_mqtt_open shall return a non-zero value. ] */ LogError("Invalid parameter specified ek: %p, srk: %p", ek, srk); result = __FAILURE__; } /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_009: [ prov_transport_common_mqtt_open shall clone the ek and srk values.] */ else if (ek != NULL && (mqtt_info->ek = BUFFER_clone(ek)) == NULL) { /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_041: [ If a failure is encountered, prov_transport_common_mqtt_open shall return a non-zero value. ] */ LogError("Unable to allocate endorsement key"); result = __FAILURE__; } else if (srk != NULL && (mqtt_info->srk = BUFFER_clone(srk)) == NULL) { /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_041: [ If a failure is encountered, prov_transport_common_mqtt_open shall return a non-zero value. ] */ LogError("Unable to allocate storage root key"); BUFFER_delete(mqtt_info->ek); mqtt_info->ek = NULL; result = __FAILURE__; } else if (mallocAndStrcpy_s(&mqtt_info->registration_id, registration_id) != 0) { /* Codes_PROV_TRANSPORT_HTTP_CLIENT_07_003: [ If any error is encountered prov_transport_http_create shall return NULL. ] */ LogError("failure constructing registration Id"); BUFFER_delete(mqtt_info->ek); mqtt_info->ek = NULL; BUFFER_delete(mqtt_info->srk); mqtt_info->srk = NULL; result = __FAILURE__; } else { mqtt_info->register_data_cb = data_callback; mqtt_info->user_ctx = user_ctx; mqtt_info->status_cb = status_cb; mqtt_info->status_ctx = status_ctx; mqtt_info->mqtt_state = MQTT_STATE_DISCONNECTED; result = 0; } return result; }
/*Codes_SRS_IOTHUBMESSAGE_03_001: [IoTHubMessage_Clone shall create a new IoT hub message with data content identical to that of the iotHubMessageHandle parameter.]*/ IOTHUB_MESSAGE_HANDLE IoTHubMessage_Clone(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle) { IOTHUB_MESSAGE_HANDLE_DATA* result; const IOTHUB_MESSAGE_HANDLE_DATA* source = (const IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle; /* Codes_SRS_IOTHUBMESSAGE_03_005: [IoTHubMessage_Clone shall return NULL if iotHubMessageHandle is NULL.] */ if (source == NULL) { result = NULL; LogError("iotHubMessageHandle parameter cannot be NULL for IoTHubMessage_Clone\r\n"); } else { result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA)); /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ if (result == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ /*do nothing and return as is*/ LogError("unable to malloc\r\n"); } else { result->messageId = NULL; result->correlationId = NULL; if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0) { LogError("unable to Copy messageId\r\n"); free(result); result = NULL; } else if (source->correlationId != NULL && mallocAndStrcpy_s(&result->correlationId, source->correlationId) != 0) { LogError("unable to Copy correlationId\r\n"); if (result->messageId != NULL) { free(result->messageId); result->messageId = NULL; } free(result); result = NULL; } else if (source->contentType == IOTHUBMESSAGE_BYTEARRAY) { /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone to content by a call to BUFFER_clone] */ if ((result->value.byteArray = BUFFER_clone(source->value.byteArray)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to BUFFER_clone\r\n"); if (result->messageId) { free(result->messageId); result->messageId = NULL; } if (result->correlationId != NULL) { free(result->correlationId); result->correlationId = NULL; } free(result); result = NULL; } /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */ else if ((result->properties = Map_Clone(source->properties)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to Map_Clone\r\n"); BUFFER_delete(result->value.byteArray); if (result->messageId) { free(result->messageId); result->messageId = NULL; } if (result->correlationId != NULL) { free(result->correlationId); result->correlationId = NULL; } free(result); result = NULL; } else { result->contentType = IOTHUBMESSAGE_BYTEARRAY; /*Codes_SRS_IOTHUBMESSAGE_03_002: [IoTHubMessage_Clone shall return upon success a non-NULL handle to the newly created IoT hub message.]*/ /*return as is, this is a good result*/ } } else /*can only be STRING*/ { /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone the content by a call to BUFFER_clone or STRING_clone] */ if ((result->value.string = STRING_clone(source->value.string)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ if (result->messageId) { free(result->messageId); result->messageId = NULL; } if (result->correlationId != NULL) { free(result->correlationId); result->correlationId = NULL; } free(result); result = NULL; LogError("failed to STRING_clone\r\n"); } /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */ else if ((result->properties = Map_Clone(source->properties)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to Map_Clone\r\n"); STRING_delete(result->value.string); if (result->messageId) { free(result->messageId); result->messageId = NULL; } if (result->correlationId != NULL) { free(result->correlationId); result->correlationId = NULL; } free(result); result = NULL; } else { result->contentType = IOTHUBMESSAGE_STRING; /*all is fine*/ } } } } return result; }
/*Codes_SRS_IOTHUBMESSAGE_03_001: [IoTHubMessage_Clone shall create a new IoT hub message with data content identical to that of the iotHubMessageHandle parameter.]*/ IOTHUB_MESSAGE_HANDLE IoTHubMessage_Clone(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle) { IOTHUB_MESSAGE_HANDLE_DATA* result; const IOTHUB_MESSAGE_HANDLE_DATA* source = (const IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle; /* Codes_SRS_IOTHUBMESSAGE_03_005: [IoTHubMessage_Clone shall return NULL if iotHubMessageHandle is NULL.] */ if (source == NULL) { result = NULL; LogError("iotHubMessageHandle parameter cannot be NULL for IoTHubMessage_Clone"); } else { result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA)); /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ if (result == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ /*do nothing and return as is*/ LogError("unable to malloc"); } else { memset(result, 0, sizeof(*result)); result->contentType = source->contentType; if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0) { LogError("unable to Copy messageId"); DestroyMessageData(result); result = NULL; } else if (source->correlationId != NULL && mallocAndStrcpy_s(&result->correlationId, source->correlationId) != 0) { LogError("unable to Copy correlationId"); DestroyMessageData(result); result = NULL; } else if (source->userDefinedContentType != NULL && mallocAndStrcpy_s(&result->userDefinedContentType, source->userDefinedContentType) != 0) { LogError("unable to copy contentType"); DestroyMessageData(result); result = NULL; } else if (source->contentEncoding != NULL && mallocAndStrcpy_s(&result->contentEncoding, source->contentEncoding) != 0) { LogError("unable to copy contentEncoding"); DestroyMessageData(result); result = NULL; } else if (source->diagnosticData != NULL && (result->diagnosticData = CloneDiagnosticPropertyData(source->diagnosticData)) == NULL) { LogError("unable to CloneDiagnosticPropertyData"); DestroyMessageData(result); result = NULL; } else if (source->contentType == IOTHUBMESSAGE_BYTEARRAY) { /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone to content by a call to BUFFER_clone] */ if ((result->value.byteArray = BUFFER_clone(source->value.byteArray)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to BUFFER_clone"); DestroyMessageData(result); result = NULL; } /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */ else if ((result->properties = Map_Clone(source->properties)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to Map_Clone"); DestroyMessageData(result); result = NULL; } /*Codes_SRS_IOTHUBMESSAGE_03_002: [IoTHubMessage_Clone shall return upon success a non-NULL handle to the newly created IoT hub message.]*/ } else /*can only be STRING*/ { /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone the content by a call to BUFFER_clone or STRING_clone] */ if ((result->value.string = STRING_clone(source->value.string)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("failed to STRING_clone"); DestroyMessageData(result); result = NULL; } /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */ else if ((result->properties = Map_Clone(source->properties)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to Map_Clone"); DestroyMessageData(result); result = NULL; } } } } return result; }