void IoTHubClient_LL_Destroy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) { /*Codes_SRS_IOTHUBCLIENT_LL_02_009: [IoTHubClient_LL_Destroy shall do nothing if parameter iotHubClientHandle is NULL.]*/ if (iotHubClientHandle != NULL) { PDLIST_ENTRY unsend; /*Codes_SRS_IOTHUBCLIENT_LL_17_010: [IoTHubClient_LL_Destroy shall call the underlaying layer's _Unregister function] */ IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; handleData->IoTHubTransport_Unregister(handleData->deviceHandle); if (handleData->isSharedTransport == false) { /*Codes_SRS_IOTHUBCLIENT_LL_02_010: [If iotHubClientHandle was not created by IoTHubClient_LL_CreateWithTransport, IoTHubClient_LL_Destroy shall call the underlaying layer's _Destroy function.] */ handleData->IoTHubTransport_Destroy(handleData->transportHandle); } /*if any, remove the items currently not send*/ while ((unsend = DList_RemoveHeadList(&(handleData->waitingToSend))) != &(handleData->waitingToSend)) { IOTHUB_MESSAGE_LIST* temp = containingRecord(unsend, IOTHUB_MESSAGE_LIST, entry); /*Codes_SRS_IOTHUBCLIENT_LL_02_033: [Otherwise, IoTHubClient_LL_Destroy shall complete all the event message callbacks that are in the waitingToSend list with the result IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY.] */ if (temp->callback != NULL) { temp->callback(IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY, temp->context); } IoTHubMessage_Destroy(temp->messageHandle); free(temp); } /*Codes_SRS_IOTHUBCLIENT_LL_17_011: [IoTHubClient_LL_Destroy shall free the resources allocated by IoTHubClient (if any).] */ tickcounter_destroy(handleData->tickCounter); IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle); free(handleData); } }
void IoTHubClient_LL_SendComplete(IOTHUB_CLIENT_LL_HANDLE handle, PDLIST_ENTRY completed, IOTHUB_BATCHSTATE_RESULT result) { /*Codes_SRS_IOTHUBCLIENT_LL_02_022: [If parameter completed is NULL, or parameter handle is NULL then IoTHubClient_LL_SendBatch shall return.]*/ if ( (handle == NULL) || (completed == NULL) ) { /*"shall return"*/ LogError("invalid arg"); } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_027: [If parameter result is IOTHUB_BACTHSTATE_FAILED then IoTHubClient_LL_SendComplete shall call all the non-NULL callbacks with the result parameter set to IOTHUB_CLIENT_CONFIRMATION_ERROR and the context set to the context passed originally in the SendEventAsync call.] */ /*Codes_SRS_IOTHUBCLIENT_LL_02_025: [If parameter result is IOTHUB_BATCHSTATE_SUCCESS then IoTHubClient_LL_SendComplete shall call all the non-NULL callbacks with the result parameter set to IOTHUB_CLIENT_CONFIRMATION_OK and the context set to the context passed originally in the SendEventAsync call.]*/ IOTHUB_CLIENT_CONFIRMATION_RESULT resultToBeCalled = (result == IOTHUB_BATCHSTATE_SUCCESS) ? IOTHUB_CLIENT_CONFIRMATION_OK : IOTHUB_CLIENT_CONFIRMATION_ERROR; PDLIST_ENTRY oldest; while ((oldest = DList_RemoveHeadList(completed)) != completed) { IOTHUB_MESSAGE_LIST* messageList = (IOTHUB_MESSAGE_LIST*)containingRecord(oldest, IOTHUB_MESSAGE_LIST, entry); /*Codes_SRS_IOTHUBCLIENT_LL_02_026: [If any callback is NULL then there shall not be a callback call.]*/ if (messageList->callback != NULL) { messageList->callback(resultToBeCalled, messageList->context); } IoTHubMessage_Destroy(messageList->messageHandle); free(messageList); } } }
MESSAGE_HANDLE MESSAGE_QUEUE_front(MESSAGE_QUEUE_HANDLE handle) { MESSAGE_HANDLE result; if (handle == NULL) { /*Codes_SRS_MESSAGE_QUEUE_17_019: [ MESSAGE_QUEUE_front shall return NULL if handle is NULL. ]*/ LogError("invalid argument handle (NULL)."); result = NULL; } else { /*Codes_SRS_MESSAGE_QUEUE_17_020: [ MESSAGE_QUEUE_front shall return NULL if the message queue is empty. ]*/ if (DList_IsListEmpty((PDLIST_ENTRY)&(handle->queue_head))) { result = NULL; } else { /*Codes_SRS_MESSAGE_QUEUE_17_021: [ On a non-empty queue, MESSAGE_QUEUE_front shall return the first remaining element that was pushed onto the message queue. ]*/ MESSAGE_QUEUE_STORAGE* entry = (MESSAGE_QUEUE_STORAGE*)DList_RemoveHeadList((PDLIST_ENTRY)&(handle->queue_head)); result = entry->message; /*Codes_SRS_MESSAGE_QUEUE_17_022: [ The content of the message queue shall not be changed after calling MESSAGE_QUEUE_front. ]*/ DList_InsertHeadList((PDLIST_ENTRY)&(handle->queue_head), (PDLIST_ENTRY)entry); } } return result; }
void IoTHubTransportMqtt_Destroy(TRANSPORT_HANDLE handle) { /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_012: [IoTHubTransportMqtt_Destroy shall do nothing if parameter handle is NULL.] */ PMQTTTRANSPORT_HANDLE_DATA transportState = (PMQTTTRANSPORT_HANDLE_DATA)handle; if (transportState != NULL) { transportState->destroyCalled = true; DisconnectFromClient(transportState); //Empty the Waiting for Ack Messages. while (!DList_IsListEmpty(&transportState->waitingForAck)) { PDLIST_ENTRY currentEntry = DList_RemoveHeadList(&transportState->waitingForAck); MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentEntry, MQTT_MESSAGE_DETAILS_LIST, entry); sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transportState, IOTHUB_BATCHSTATE_FAILED); free(mqttMsgEntry); } /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_014: [IoTHubTransportMqtt_Destroy shall free all the resources currently in use.] */ mqtt_client_deinit(transportState->mqttClient); STRING_delete(transportState->mqttEventTopic); STRING_delete(transportState->mqttMessageTopic); STRING_delete(transportState->device_id); STRING_delete(transportState->device_key); STRING_delete(transportState->sasTokenSr); STRING_delete(transportState->hostAddress); STRING_delete(transportState->configPassedThroughUsername); tickcounter_destroy(g_msgTickCounter); free(transportState); } }
static MESSAGE_HANDLE message_pop(MESSAGE_QUEUE_HANDLE_DATA* handle) { MESSAGE_HANDLE result; if (DList_IsListEmpty((PDLIST_ENTRY)&(handle->queue_head))) { /*Codes_SRS_MESSAGE_QUEUE_17_013: [ MESSAGE_QUEUE_pop shall return NULL on an empty message queue. ]*/ result = NULL; } else { /*Codes_SRS_MESSAGE_QUEUE_17_014: [ MESSAGE_QUEUE_pop shall remove messages from the queue in a first-in-first-out order. ]*/ /*Codes_SRS_MESSAGE_QUEUE_17_015: [ A successful call to MESSAGE_QUEUE_pop on a queue with one message will cause the message queue to be empty. ]*/ MESSAGE_QUEUE_STORAGE* entry = (MESSAGE_QUEUE_STORAGE*)DList_RemoveHeadList( (PDLIST_ENTRY)&(handle->queue_head)); result = ((MESSAGE_QUEUE_STORAGE*)entry)->message; /*Codes_SRS_MESSAGE_QUEUE_17_006: [ MESSAGE_QUEUE_destroy shall free all allocated resources. ]*/ free(entry); } return result; }
void MQTTAPI_DoWork(MQTTAPI_HANDLE instance) { PMQTTTAPI_HANDLE_DATA handle = instance; /* Codes_SRS_MQTTAPI_04_050: [if parameter instance is NULL then MQTTAPI_DoWork shall not perform any action.] */ if (handle != NULL) { if (!checkAndTryToConnect(handle)) { LogError("Client Not Connected and could not connect.\r\n"); } else { if (Lock(handle->LockHandle) == LOCK_OK) { //message { PDLIST_ENTRY received; while ((received = DList_RemoveHeadList(&(handle->messagesReceived))) != &(handle->messagesReceived)) { MQTTAPI_MESSAGE_RECEIVED_LIST* temp = containingRecord(received, MQTTAPI_MESSAGE_RECEIVED_LIST, entry); if (handle->maCallBack != NULL) { if (!handle->maCallBack(handle->maCallbackContext, temp->messageReceived)) { LogError("Client could not handle message, dropping it."); } } free(temp->messageReceived->payload); free(temp->messageReceived); free(temp); } } Unlock(handle->LockHandle); } else { LogError("Could not aquire lock for message.\r\n"); } //Event { PDLIST_ENTRY messageToSend; while ((messageToSend = DList_RemoveHeadList(&(handle->messagesToSend))) != &(handle->messagesToSend)) { MQTTAPI_MESSAGE_SEND_LIST* temp = containingRecord(messageToSend, MQTTAPI_MESSAGE_SEND_LIST, entry); if (MQTTClient_publishMessage(handle->client, STRING_c_str(temp->topicName), temp->messageToSend, &(temp->dt)) != MQTTCLIENT_SUCCESS) { handle->dcCallback(temp->context, MQTTAPI_CONFIRMATION_ERROR); MQTTClient_freeMessage(&(temp->messageToSend)); free(temp->messageToSend); STRING_delete(temp->topicName); free(temp); } else { DList_InsertTailList(&(handle->messagesSent), &(temp->entry)); } } } } } }
void MQTTAPI_Destroy(MQTTAPI_HANDLE instance) { PMQTTTAPI_HANDLE_DATA mqttHandleData = instance; /* Codes_SRS_MQTTAPI_04_021: [If parameter instance is NULL then MQTTAPI_Destroy shall take no action.] */ if (mqttHandleData != NULL) { /* Codes_SRS_MQTTAPI_04_022: [MQTTAPI_Destroy shall free all resources used by MQTTAPI_HANDLE.] */ STRING_delete(mqttHandleData->device_id); STRING_delete(mqttHandleData->device_key); STRING_delete(mqttHandleData->sasTokenSr); /* Codes_SRS_MQTTAPI_04_049: [If the instance is connected, MQTTAPI_Destroy shall disconnect the instance] */ if (mqttHandleData->subscribed) { MQTTAPI_Unsubscribe(mqttHandleData->subscribedTopicHandleData); } if (mqttHandleData->connected) { MQTTClient_disconnect(mqttHandleData->client, 0); } /* Codes_SRS_MQTTAPI_04_053: [If there are messages to be sent MQTTAPI_Destroy shall signalize the user that the message couldn’t be sent by reason of MQTTAPI_CONFIRMATION_BECAUSE_DESTROY] */ { PDLIST_ENTRY received; while ((received = DList_RemoveHeadList(&(mqttHandleData->messagesReceived))) != &(mqttHandleData->messagesReceived)) { MQTTAPI_MESSAGE_RECEIVED_LIST* temp = containingRecord(received, MQTTAPI_MESSAGE_RECEIVED_LIST, entry); if (mqttHandleData->maCallBack != NULL) { if (!mqttHandleData->maCallBack(mqttHandleData->maCallbackContext, temp->messageReceived)) { LogError("Client could not handle message, dropping it."); } } free(temp->messageReceived->payload); free(temp->messageReceived); free(temp); } } { PDLIST_ENTRY messageToSend; while ((messageToSend = DList_RemoveHeadList(&(mqttHandleData->messagesToSend))) != &(mqttHandleData->messagesToSend)) { MQTTAPI_MESSAGE_SEND_LIST* temp = containingRecord(messageToSend, MQTTAPI_MESSAGE_SEND_LIST, entry); if (mqttHandleData->dcCallback != NULL) { mqttHandleData->dcCallback(temp->context, MQTTAPI_CONFIRMATION_BECAUSE_DESTROY); MQTTClient_freeMessage(&(temp->messageToSend)); free(temp->messageToSend); STRING_delete(temp->topicName); free(temp); } } } { PDLIST_ENTRY currentListEntry; while ((currentListEntry = DList_RemoveHeadList(&(mqttHandleData->messagesSent))) != &(mqttHandleData->messagesSent)) { MQTTAPI_MESSAGE_SEND_LIST* temp = containingRecord(currentListEntry, MQTTAPI_MESSAGE_SEND_LIST, entry); if (mqttHandleData->dcCallback != NULL) { mqttHandleData->dcCallback(temp->context, MQTTAPI_CONFIRMATION_BECAUSE_DESTROY); MQTTClient_freeMessage(&(temp->messageToSend)); free(temp->messageToSend); STRING_delete(temp->topicName); free(temp); } } } MQTTClient_destroy(mqttHandleData->client); Lock_Deinit(mqttHandleData->LockHandle); free(mqttHandleData); } }