From 693cdb53dda5770677fd23ae4db0410ef51cf438 Mon Sep 17 00:00:00 2001 From: "hyuna0213.jo" Date: Fri, 11 Mar 2016 11:54:02 +0900 Subject: [PATCH] changed block-wise transfer logic to process non-confirmable message according to the block-wise transfer specification, block messages should be transmitted with request-response pairs of information from a resource representation. current block-wise transfer logic did not handle non-confirmable message like a specification. Change-Id: I6305bf14166720638c86eae8dc21aa5903f5b7f4 Signed-off-by: hyuna0213.jo Reviewed-on: https://gerrit.iotivity.org/gerrit/5699 Tested-by: jenkins-iotivity Reviewed-by: Ashok Babu Channa --- .../csdk/connectivity/inc/cablockwisetransfer.h | 17 +- resource/csdk/connectivity/inc/caprotocolmessage.h | 9 + .../csdk/connectivity/src/cablockwisetransfer.c | 777 ++++++++++----------- resource/csdk/connectivity/src/camessagehandler.c | 30 +- resource/csdk/connectivity/src/caprotocolmessage.c | 12 + 5 files changed, 400 insertions(+), 445 deletions(-) diff --git a/resource/csdk/connectivity/inc/cablockwisetransfer.h b/resource/csdk/connectivity/inc/cablockwisetransfer.h index ece06ad..b9ee8a7 100644 --- a/resource/csdk/connectivity/inc/cablockwisetransfer.h +++ b/resource/csdk/connectivity/inc/cablockwisetransfer.h @@ -98,15 +98,13 @@ typedef struct typedef enum { CA_BLOCK_UNKNOWN = 0, - CA_OPTION1_ACK, - CA_OPTION1_NO_ACK_LAST_BLOCK, - CA_OPTION1_NO_ACK_BLOCK, + CA_OPTION1_RESPONSE, + CA_OPTION1_REQUEST_LAST_BLOCK, + CA_OPTION1_REQUEST_BLOCK, CA_OPTION2_FIRST_BLOCK, CA_OPTION2_LAST_BLOCK, - CA_OPTION2_ACK, - CA_OPTION2_NON, - CA_OPTION2_CON, - CA_SENT_PREVIOUS_NON_MSG, + CA_OPTION2_RESPONSE, + CA_OPTION2_REQUEST, CA_BLOCK_INCOMPLETE, CA_BLOCK_TOO_LARGE, CA_BLOCK_RECEIVED_ALREADY @@ -193,12 +191,11 @@ CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData * send block message to remote device. * @param[in] pdu received pdu binary data. * @param[in] msgType the message type of the block. - * @param[in] status block-wise state to move next step. * @param[in] blockID ID set of CABlockData. * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). */ CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType, - uint8_t status, const CABlockDataID_t *blockID); + const CABlockDataID_t *blockID); /** * send error message to remote device. @@ -256,7 +253,7 @@ CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). */ CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block, - CAMessageType_t msgType, uint16_t blockType); + coap_pdu_t *pdu, uint16_t blockType); /** * Update the block option in block-wise transfer list. diff --git a/resource/csdk/connectivity/inc/caprotocolmessage.h b/resource/csdk/connectivity/inc/caprotocolmessage.h index 343d600..b752a4d 100644 --- a/resource/csdk/connectivity/inc/caprotocolmessage.h +++ b/resource/csdk/connectivity/inc/caprotocolmessage.h @@ -256,6 +256,15 @@ CAPayloadFormat_t CAConvertFormat(uint8_t format); bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter); #endif +#ifdef WITH_BWT +/** + * check whether blockwise transfer is supported or not. + * @param[in] adapter transport adapter type. + * @return true or false. + */ +bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter); +#endif + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/resource/csdk/connectivity/src/cablockwisetransfer.c b/resource/csdk/connectivity/src/cablockwisetransfer.c index f3447fd..a2a450c 100644 --- a/resource/csdk/connectivity/src/cablockwisetransfer.c +++ b/resource/csdk/connectivity/src/cablockwisetransfer.c @@ -223,7 +223,7 @@ CAResult_t CASendBlockWiseData(const CAData_t *sendData) // #4. send block message OIC_LOG(DEBUG, TAG, "send first block msg"); res = CAAddSendThreadQueue(currData->sentData, - (const CABlockDataID_t *)&currData->blockDataId); + (const CABlockDataID_t *) &currData->blockDataId); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); @@ -333,7 +333,7 @@ CAResult_t CAReceiveBlockWiseData(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, } // check if block option is set and get block data - coap_block_t block = {0, 0, 0}; + coap_block_t block = { 0, 0, 0 }; // get block1 option int isBlock1 = coap_get_block(pdu, COAP_OPTION_BLOCK1, &block); @@ -400,7 +400,7 @@ CAResult_t CAReceiveBlockWiseData(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, { OIC_LOG(ERROR, TAG, "setting has failed"); CADestroyBlockID(blockDataID); - return res; + return CA_STATUS_FAILED; } } else if (COAP_OPTION_BLOCK1 == data->type) @@ -480,7 +480,7 @@ CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData } break; - case CA_OPTION2_CON: + case CA_OPTION2_REQUEST: // add data to send thread data = CAGetDataSetFromBlockDataList(blockID); if (!data) @@ -508,11 +508,10 @@ CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData break; - case CA_OPTION1_ACK: - case CA_OPTION2_ACK: - case CA_SENT_PREVIOUS_NON_MSG: - res = CASendBlockMessage(pdu, CA_MSG_CONFIRM, blockWiseStatus, - blockID); + case CA_OPTION1_RESPONSE: + case CA_OPTION2_RESPONSE: + case CA_OPTION1_REQUEST_BLOCK: + res = CASendBlockMessage(pdu, pdu->hdr->coap_hdr_udp_t.type, blockID); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "send has failed"); @@ -538,7 +537,7 @@ CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData } break; - case CA_OPTION1_NO_ACK_LAST_BLOCK: + case CA_OPTION1_REQUEST_LAST_BLOCK: // process last block and send upper layer res = CAReceiveLastBlock(blockID, receivedData); if (CA_STATUS_OK != res) @@ -546,53 +545,22 @@ CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData OIC_LOG(ERROR, TAG, "receive has failed"); return res; } - - if (CA_MSG_NONCONFIRM == pdu->hdr->coap_hdr_udp_t.type) - { - // remove data from list - res = CARemoveBlockDataFromList(blockID); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "remove has failed"); - return res; - } - } - break; - - case CA_OPTION1_NO_ACK_BLOCK: - if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type) - { - // add data to send thread - res = CASendBlockMessage(pdu, CA_MSG_ACKNOWLEDGE, blockWiseStatus, - blockID); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "send has failed"); - return res; - } - } break; case CA_BLOCK_INCOMPLETE: - if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type || - CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type) + // add data to send thread + res = CASendErrorMessage(pdu, blockWiseStatus, CA_REQUEST_ENTITY_INCOMPLETE, blockID); + if (CA_STATUS_OK != res) { - // add data to send thread - res = CASendErrorMessage(pdu, blockWiseStatus, - CA_REQUEST_ENTITY_INCOMPLETE, - blockID); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "send has failed"); - return res; - } + OIC_LOG(ERROR, TAG, "send has failed"); + return res; } break; case CA_BLOCK_TOO_LARGE: - if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type) + if (receivedData->requestInfo) { - res = CASendBlockMessage(pdu, CA_MSG_CONFIRM, blockWiseStatus, + res = CASendErrorMessage(pdu, blockWiseStatus, CA_REQUEST_ENTITY_TOO_LARGE, blockID); if (CA_STATUS_OK != res) { @@ -600,11 +568,9 @@ CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData return res; } } - else if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type) + else if (receivedData->responseInfo) { - res = CASendErrorMessage(pdu, blockWiseStatus, - CA_REQUEST_ENTITY_TOO_LARGE, - blockID); + res = CASendBlockMessage(pdu, pdu->hdr->coap_hdr_udp_t.type, blockID); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "send has failed"); @@ -619,7 +585,7 @@ CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData } CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType, - uint8_t status, const CABlockDataID_t *blockID) + const CABlockDataID_t *blockID) { VERIFY_NON_NULL(pdu, TAG, "pdu"); VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr"); @@ -632,34 +598,38 @@ CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType, return CA_STATUS_FAILED; } - if (CA_MSG_CONFIRM == msgType) + CAMessageType_t sentMsgType = CA_MSG_NONCONFIRM; + switch (msgType) { - OIC_LOG(DEBUG, TAG, "need new msgID"); - if (data->requestInfo) - { - data->requestInfo->info.messageId = 0; - } + case CA_MSG_CONFIRM: + sentMsgType = CA_MSG_ACKNOWLEDGE; + break; + case CA_MSG_ACKNOWLEDGE: + sentMsgType = CA_MSG_CONFIRM; + break; + default: + sentMsgType = CA_MSG_NONCONFIRM; + break; + } + uint32_t code = pdu->hdr->coap_hdr_udp_t.code; + if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code) + { if (data->responseInfo) { - data->responseInfo->info.messageId = 0; + OIC_LOG(DEBUG, TAG, "set response info"); + data->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id; + data->responseInfo->info.type = sentMsgType; + data->responseInfo->result = CA_CONTINUE; } } - else if (CA_MSG_ACKNOWLEDGE == msgType) + else { - if (data->responseInfo) + OIC_LOG(DEBUG, TAG, "need new msgID"); + if (data->requestInfo) { - OIC_LOG(DEBUG, TAG, "set ACK message"); - data->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id; - data->responseInfo->info.type = CA_MSG_ACKNOWLEDGE; - if (CA_OPTION1_NO_ACK_LAST_BLOCK == status) - { - data->responseInfo->result = CA_CHANGED; - } - else if (CA_OPTION1_NO_ACK_BLOCK == status) - { - data->responseInfo->result = CA_CONTINUE; - } + data->requestInfo->info.messageId = 0; + data->requestInfo->info.type = sentMsgType; } } @@ -674,8 +644,7 @@ CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType, } CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status, - CAResponseResult_t responseResult, - const CABlockDataID_t *blockID) + CAResponseResult_t responseResult, const CABlockDataID_t *blockID) { VERIFY_NON_NULL(pdu, TAG, "pdu"); VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr"); @@ -689,32 +658,57 @@ CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status, return CA_STATUS_FAILED; } + CAMessageType_t sentMsgType = CA_MSG_NONCONFIRM; + switch (pdu->hdr->coap_hdr_udp_t.type) + { + case CA_MSG_CONFIRM: + sentMsgType = CA_MSG_ACKNOWLEDGE; + break; + case CA_MSG_ACKNOWLEDGE: + sentMsgType = CA_MSG_CONFIRM; + break; + } + CAData_t *cloneData = NULL; - if (data->sentData && data->sentData->responseInfo) + if (data->sentData) { - data->sentData->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id; - data->sentData->responseInfo->info.type = CA_MSG_ACKNOWLEDGE; - data->sentData->responseInfo->result = responseResult; cloneData = CACloneCAData(data->sentData); if (!cloneData) { OIC_LOG(ERROR, TAG, "clone has failed"); return CA_MEMORY_ALLOC_FAILED; } - OIC_LOG(DEBUG, TAG, "set ACK message"); - } - else if (data->sentData) - { - cloneData = CACreateNewDataSet(pdu, data->sentData->remoteEndpoint); - if(!cloneData) + + if (cloneData->responseInfo) { - OIC_LOG(ERROR, TAG, PCF("CACreateNewDataSet failed")); - return CA_MEMORY_ALLOC_FAILED; + cloneData->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id; + cloneData->responseInfo->info.type = sentMsgType; + cloneData->responseInfo->result = responseResult; } + else + { + CAInfo_t responseData = { .tokenLength = pdu->hdr->coap_hdr_udp_t.token_length }; + responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength); + if (!responseData.token) + { + OIC_LOG(ERROR, TAG, "out of memory"); + return CA_MEMORY_ALLOC_FAILED; + } + memcpy(responseData.token, pdu->hdr->coap_hdr_udp_t.token, responseData.tokenLength); - cloneData->responseInfo->info.type = CA_MSG_CONFIRM; - cloneData->responseInfo->result = responseResult; - OIC_LOG(DEBUG, TAG, "set CON message"); + cloneData->responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t)); + if (!cloneData->responseInfo) + { + OIC_LOG(ERROR, TAG, "out of memory"); + OICFree(responseData.token); + return CA_MEMORY_ALLOC_FAILED; + } + + cloneData->responseInfo->info = responseData; + cloneData->responseInfo->info.type = sentMsgType; + cloneData->responseInfo->result = responseResult; + } + OIC_LOG(DEBUG, TAG, "set response message to send error code"); } else { @@ -722,6 +716,13 @@ CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status, return CA_MEMORY_ALLOC_FAILED; } + // if there is a requestInfo, remove it to send response message + if (cloneData->requestInfo) + { + CADestroyRequestInfoInternal(cloneData->requestInfo); + cloneData->requestInfo = NULL; + } + // add data to send thread if (g_context.sendThreadFunc) { @@ -748,8 +749,7 @@ CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status, return CA_STATUS_OK; } -CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID, - const CAData_t *receivedData) +CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID, const CAData_t *receivedData) { VERIFY_NON_NULL(blockID, TAG, "blockID"); VERIFY_NON_NULL(receivedData, TAG, "receivedData"); @@ -764,8 +764,7 @@ CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID, // update payload size_t fullPayloadLen = 0; - CAPayload_t fullPayload = CAGetPayloadFromBlockDataList(blockID, - &fullPayloadLen); + CAPayload_t fullPayload = CAGetPayloadFromBlockDataList(blockID, &fullPayloadLen); if (fullPayload) { CAResult_t res = CAUpdatePayloadToCAData(cloneData, fullPayload, fullPayloadLen); @@ -839,75 +838,26 @@ CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, } // update BLOCK OPTION1 type - CAResult_t res = CAUpdateBlockOptionType(blockDataID, - COAP_OPTION_BLOCK1); + CAResult_t res = CAUpdateBlockOptionType(blockDataID, COAP_OPTION_BLOCK1); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN; - // received type from remote device - if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type) - { - uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code); - if (0 == block.m && - (CA_REQUEST_ENTITY_INCOMPLETE != code && CA_REQUEST_ENTITY_TOO_LARGE != code)) - { - int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block); - if (isBlock2) - { - OIC_LOG(INFO, TAG, "received data is combining block1 and block2"); - // initialize block number for response message - data->block1.num = 0; - } - else - { - OIC_LOG(INFO, TAG, "received data is not bulk data"); - CAReceiveLastBlock(blockDataID, receivedData); - CARemoveBlockDataFromList(blockDataID); - } - - CADestroyBlockID(blockDataID); - return CA_STATUS_OK; - } - - blockWiseStatus = CA_OPTION1_ACK; - res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "update has failed"); - CADestroyBlockID(blockDataID); - return res; - } - - res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; - } - } - else // CON or NON message + uint32_t code = pdu->hdr->coap_hdr_udp_t.code; + if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code) { + // received message type is request OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m); // check the size option - bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu, - COAP_OPTION_SIZE1, + bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu, COAP_OPTION_SIZE1, &(data->payloadLength)); - // check if received payload is exact - if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type) - { - blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData, - COAP_OPTION_BLOCK1, dataLen); - } + blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData, + COAP_OPTION_BLOCK1, dataLen); if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus) { @@ -917,9 +867,7 @@ CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, @@ -927,9 +875,7 @@ CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } // update block data @@ -937,9 +883,7 @@ CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } } @@ -949,25 +893,71 @@ CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, if (0 == block.m) // Last block is received { OIC_LOG(DEBUG, TAG, "M bit is 0"); - blockWiseStatus = CA_OPTION1_NO_ACK_LAST_BLOCK; + blockWiseStatus = CA_OPTION1_REQUEST_LAST_BLOCK; } else { OIC_LOG(DEBUG, TAG, "M bit is 1"); - blockWiseStatus = CA_OPTION1_NO_ACK_BLOCK; + blockWiseStatus = CA_OPTION1_REQUEST_BLOCK; } } } + else + { + // received message type is response + uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code); + if (0 == block.m && (CA_REQUEST_ENTITY_INCOMPLETE != code + && CA_REQUEST_ENTITY_TOO_LARGE != code)) + { + int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block); + if (isBlock2) + { + OIC_LOG(INFO, TAG, "received data is combining block1 and block2"); + // initialize block number for response message + data->block1.num = 0; + CADestroyBlockID(blockDataID); + return CA_STATUS_OK; + } + else + { + OIC_LOG(INFO, TAG, "received data is not bulk data"); + CAReceiveLastBlock(blockDataID, receivedData); + res = CA_STATUS_OK; + goto exit; + } + } + + blockWiseStatus = CA_OPTION1_RESPONSE; + res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "update has failed"); + CADestroyBlockID(blockDataID); + return res; + } + + res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "update has failed"); + goto exit; + } + } res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "setting has failed"); - CARemoveBlockDataFromList(blockDataID); + goto exit; } CADestroyBlockID(blockDataID); return res; + +exit: + CARemoveBlockDataFromList(blockDataID); + CADestroyBlockID(blockDataID); + return res; } // TODO make pdu const after libcoap is updated to support that. @@ -1024,9 +1014,7 @@ CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN; @@ -1038,9 +1026,7 @@ CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } // first block data have to notify to Application @@ -1048,29 +1034,48 @@ CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } blockWiseStatus = CA_OPTION2_FIRST_BLOCK; } else { - // received type from remote device - if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type || - (CA_MSG_NONCONFIRM == pdu->hdr->coap_hdr_udp_t.type && - NULL != receivedData->responseInfo)) + uint32_t code = pdu->hdr->coap_hdr_udp_t.code; + if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code) { - OIC_LOG(DEBUG, TAG, "received ACK or NON"); + // received message type is request + OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m); + + blockWiseStatus = CA_OPTION2_REQUEST; + + res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "update has failed"); + goto exit; + } + + res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "update has failed"); + goto exit; + } + } + else + { + // received message type is response + OIC_LOG(DEBUG, TAG, "received response message with block option2"); // check the size option bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu, COAP_OPTION_SIZE2, &(data->payloadLength)); - // check if received payload is exact - if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type) + uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code); + if (CA_REQUEST_ENTITY_INCOMPLETE != code && CA_REQUEST_ENTITY_TOO_LARGE != code) { + // check if received payload is exact blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData, COAP_OPTION_BLOCK2, dataLen); } @@ -1083,9 +1088,7 @@ CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } } @@ -1100,15 +1103,7 @@ CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus) { OIC_LOG(DEBUG, TAG, "M bit is 1"); - - if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type) - { - blockWiseStatus = CA_OPTION2_ACK; - } - else - { - blockWiseStatus = CA_OPTION2_NON; - } + blockWiseStatus = CA_OPTION2_RESPONSE; } res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, @@ -1116,58 +1111,33 @@ CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint, if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } } } - else // CON message and so on. - { - OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m); - - blockWiseStatus = CA_OPTION2_CON; - - res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; - } - - res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "update has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; - } - } } res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "setting has failed"); - CARemoveBlockDataFromList(blockDataID); - CADestroyBlockID(blockDataID); - return res; + goto exit; } CADestroyBlockID(blockDataID); return CA_STATUS_OK; + +exit: + CARemoveBlockDataFromList(blockDataID); + CADestroyBlockID(blockDataID); + return res; } CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu, @@ -1197,7 +1167,7 @@ CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *p // update block option items switch (status) { - case CA_OPTION1_ACK: + case CA_OPTION1_RESPONSE: if (currData->block1.num > block->num) { OIC_LOG(ERROR, TAG, "received incorrect block num"); @@ -1205,14 +1175,10 @@ CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *p } block->num++; break; - case CA_OPTION2_NON: - block->num++; + case CA_OPTION2_REQUEST: block->m = 0; break; - case CA_OPTION2_CON: - block->m = 0; - break; - case CA_OPTION2_ACK: + case CA_OPTION2_RESPONSE: if (currData->block2.num > block->num) { OIC_LOG(ERROR, TAG, "received incorrect block num"); @@ -1242,7 +1208,7 @@ CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *p if (CA_BLOCK_INCOMPLETE != status && CA_BLOCK_TOO_LARGE != status) { // negotiate block size - res = CANegotiateBlockSize(currData, block, pdu->hdr->coap_hdr_udp_t.type, blockType); + res = CANegotiateBlockSize(currData, block, pdu, blockType); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "negotiation has failed"); @@ -1257,8 +1223,7 @@ CAResult_t CASetMoreBitFromBlock(size_t payloadLen, coap_block_t *block) { VERIFY_NON_NULL(block, TAG, "block"); - if ((size_t)((block->num + 1) << (block->szx + BLOCK_NUMBER_IDX)) - < payloadLen) + if ((size_t) ((block->num + 1) << (block->szx + BLOCK_NUMBER_IDX)) < payloadLen) { OIC_LOG(DEBUG, TAG, "Set the M-bit(1)"); block->m = 1; @@ -1273,18 +1238,27 @@ CAResult_t CASetMoreBitFromBlock(size_t payloadLen, coap_block_t *block) } CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block, - CAMessageType_t msgType, uint16_t blockType) + coap_pdu_t *pdu, uint16_t blockType) { OIC_LOG(DEBUG, TAG, "IN-NegotiateBlockSize"); VERIFY_NON_NULL(currData, TAG, "currData"); VERIFY_NON_NULL(block, TAG, "block"); + VERIFY_NON_NULL(pdu, TAG, "pdu"); + VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr"); + + bool isReqMsg = false; + uint32_t code = pdu->hdr->coap_hdr_udp_t.code; + if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code) + { + isReqMsg = true; + } // #1. check the block option type if (COAP_OPTION_BLOCK2 == blockType) { // #2. check the message type - if (CA_MSG_ACKNOWLEDGE == msgType) + if (!isReqMsg) { if (block->szx > currData->block2.szx) { @@ -1310,7 +1284,7 @@ CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block, } else if (COAP_OPTION_BLOCK1 == blockType) { - if (CA_MSG_ACKNOWLEDGE == msgType) + if (!isReqMsg) { if (block->szx < currData->block1.szx) { @@ -1371,22 +1345,20 @@ CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block, CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID) { VERIFY_NON_NULL(pdu, TAG, "pdu"); + VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr"); VERIFY_NON_NULL(blockID, TAG, "blockID"); - // if CON message is sent, update messageId in block-wise transfer list - if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type) + // if message is sent, update messageId in block-wise transfer list + CAData_t * cadata = CAGetDataSetFromBlockDataList(blockID); + if (!cadata) { - CAData_t * cadata = CAGetDataSetFromBlockDataList(blockID); - if (!cadata) - { - OIC_LOG(ERROR, TAG, "CAData is unavailable"); - return CA_STATUS_FAILED; - } + OIC_LOG(ERROR, TAG, "CAData is unavailable"); + return CA_STATUS_FAILED; + } - if (cadata->requestInfo) - { - cadata->requestInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id; - } + if (cadata->requestInfo) + { + cadata->requestInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id; } return CA_STATUS_OK; @@ -1403,6 +1375,7 @@ CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info, VERIFY_NON_NULL(endpoint, TAG, "endpoint"); VERIFY_NON_NULL(options, TAG, "options"); + CAResult_t res = CA_STATUS_OK; size_t dataLength = 0; if (info->payload) { @@ -1410,16 +1383,6 @@ CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info, OIC_LOG_V(DEBUG, TAG, "dataLength - %zu", dataLength); } - OIC_LOG_V(DEBUG, TAG, "previous payload - %s", (*pdu)->data); - - CAResult_t res = CA_STATUS_OK; - uint32_t code = CA_RESPONSE_CODE((*pdu)->hdr->coap_hdr_udp_t.code); - if (CA_REQUEST_ENTITY_INCOMPLETE == code) - { - OIC_LOG(INFO, TAG, "don't use option"); - return res; - } - CABlockDataID_t* blockDataID = CACreateBlockDatablockId( (CAToken_t)(*pdu)->hdr->coap_hdr_udp_t.token, (*pdu)->hdr->coap_hdr_udp_t.token_length, @@ -1431,6 +1394,29 @@ CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info, goto exit; } + uint32_t code = (*pdu)->hdr->coap_hdr_udp_t.code; + if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code) + { + // if received message type is RESET from remote device, + // we have to use the updated message id of request message to find token. + res = CAUpdateMessageId(*pdu, blockDataID); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "fail to update message id"); + goto exit; + } + } + else + { + uint32_t repCode = CA_RESPONSE_CODE((*pdu)->hdr->coap_hdr_udp_t.code); + if (CA_REQUEST_ENTITY_INCOMPLETE == repCode) + { + OIC_LOG(INFO, TAG, "don't use option"); + res = CA_STATUS_OK; + goto exit; + } + } + uint8_t blockType = CAGetBlockOptionType(blockDataID); if (COAP_OPTION_BLOCK2 == blockType) { @@ -1483,15 +1469,6 @@ CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info, } } - // if received message type is RESET from remote device, - // we have to use the updated message id to find token. - res = CAUpdateMessageId(*pdu, blockDataID); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "fail to update CON message id "); - goto exit; - } - exit: if (CA_ADAPTER_IP == endpoint->adapter && 0 == endpoint->port) { @@ -1522,33 +1499,28 @@ CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t data return CA_STATUS_FAILED; } - CALogBlockInfo(block2); - - if (CA_MSG_ACKNOWLEDGE == (*pdu)->hdr->coap_hdr_udp_t.type || - (CA_MSG_NONCONFIRM == (*pdu)->hdr->coap_hdr_udp_t.type && - CA_GET != (*pdu)->hdr->coap_hdr_udp_t.code)) + CAResult_t res = CA_STATUS_OK; + uint32_t code = (*pdu)->hdr->coap_hdr_udp_t.code; + if (CA_GET != code && CA_POST != code && CA_PUT != code && CA_DELETE != code) { CASetMoreBitFromBlock(dataLength, block2); - CALogBlockInfo(block2); // if block number is 0, add size2 option if (0 == block2->num) { - CAResult_t res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE2, dataLength, options); + res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE2, dataLength, options); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; + goto exit; } } - CAResult_t res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options); + res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; + goto exit; } if (block1->num) @@ -1558,8 +1530,7 @@ CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t data if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; + goto exit; } // initialize block number block1->num = 0; @@ -1569,8 +1540,7 @@ CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t data if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; + goto exit; } if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload, @@ -1580,51 +1550,38 @@ CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t data return CA_STATUS_FAILED; } + CALogBlockInfo(block2); + if (!block2->m) { // if sent message is last response block message, remove data CARemoveBlockDataFromList(blockID); } - else - { - if (CA_MSG_NONCONFIRM == (*pdu)->hdr->coap_hdr_udp_t.type) - { - OIC_LOG(DEBUG, TAG, "NON, send next block.."); - // update block data - block2->num++; - CAResult_t res = CAProcessNextStep(*pdu, NULL, - CA_SENT_PREVIOUS_NON_MSG, - blockID); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "failed to process next step"); - CARemoveBlockDataFromList(blockID); - return res; - } - } - } } else { - OIC_LOG(DEBUG, TAG, "option2, not ACK msg"); - CAResult_t res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options); + OIC_LOG(DEBUG, TAG, "option2, not response msg"); + res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; + goto exit; } res = CAAddOptionToPDU(*pdu, options); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; + goto exit; } + CALogBlockInfo(block2); } return CA_STATUS_OK; + +exit: + CARemoveBlockDataFromList(blockID); + return res; } CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength, @@ -1646,115 +1603,89 @@ CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t *info, size_t data return CA_STATUS_FAILED; } - CALogBlockInfo(block1); - - if (CA_MSG_ACKNOWLEDGE == (*pdu)->hdr->coap_hdr_udp_t.type) + CAResult_t res = CA_STATUS_OK; + uint32_t code = (*pdu)->hdr->coap_hdr_udp_t.code; + if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code) { - OIC_LOG(DEBUG, TAG, "option1 and ACK msg.."); - CAResult_t res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options); + CASetMoreBitFromBlock(dataLength, block1); + + // if block number is 0, add size1 option + if (0 == block1->num) + { + res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE1, dataLength, options); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "add has failed"); + goto exit; + } + } + + res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; + goto exit; } res = CAAddOptionToPDU(*pdu, options); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; + goto exit; } - if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload)) + if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload, block1->num, + block1->szx)) { - OIC_LOG(ERROR, TAG, "failed to add payload"); + OIC_LOG(ERROR, TAG, "Data length is smaller than the start index"); return CA_STATUS_FAILED; } - - // reset block-list after write block - if (0 == block1->m) - { - // remove data from list - CAResult_t res = CARemoveBlockDataFromList(blockID); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "remove has failed"); - return res; - } - } } else { - CASetMoreBitFromBlock(dataLength, block1); - - CAResult_t res = CA_STATUS_OK; - // if block number is 0, add size1 option - if (0 == block1->num) - { - res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE1, dataLength, options); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; - } - } - + OIC_LOG(DEBUG, TAG, "received response message with block option1"); res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; + goto exit; } res = CAAddOptionToPDU(*pdu, options); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "add has failed"); - CARemoveBlockDataFromList(blockID); - return res; + goto exit; } - CALogBlockInfo(block1); - - if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload, - block1->num, block1->szx)) + if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload)) { - OIC_LOG(ERROR, TAG, "Data length is smaller than the start index"); + OIC_LOG(ERROR, TAG, "failed to add payload"); return CA_STATUS_FAILED; } - // check the message type and if message type is NON, next block message will be sent - if (CA_MSG_NONCONFIRM == (*pdu)->hdr->coap_hdr_udp_t.type) + // reset block-list after write block + if (0 == block1->m) { - if (block1->m) - { - OIC_LOG(DEBUG, TAG, "NON, send next block.."); - // update block data - block1->num++; - CAResult_t res = CAProcessNextStep(*pdu, NULL, - CA_SENT_PREVIOUS_NON_MSG, - blockID); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "failed to process next step"); - CARemoveBlockDataFromList(blockID); - return res; - } - } - else + // remove data from list + res = CARemoveBlockDataFromList(blockID); + if (CA_STATUS_OK != res) { - CARemoveBlockDataFromList(blockID); + OIC_LOG(ERROR, TAG, "remove has failed"); + return res; } } } + CALogBlockInfo(block1); + OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption1"); return CA_STATUS_OK; + +exit: + CARemoveBlockDataFromList(blockID); + return res; } CAResult_t CAAddBlockOptionImpl(coap_block_t *block, uint8_t blockType, @@ -1889,8 +1820,7 @@ uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlo if (COAP_OPTION_BLOCK1 == blockType) { size_t prePayloadLen = currData->receivedPayloadLen; - if (prePayloadLen != (size_t)BLOCK_SIZE(receivedBlock->szx) - * receivedBlock->num) + if (prePayloadLen != (size_t) BLOCK_SIZE(receivedBlock->szx) * receivedBlock->num) { if (receivedBlock->num > currData->block1.num + 1) { @@ -1919,8 +1849,7 @@ uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlo // #3. check if error check logic is required size_t optionLen = dataLen - blockPayloadLen; - if (receivedBlock->m && blockPayloadLen != - (size_t)BLOCK_SIZE(receivedBlock->szx)) + if (receivedBlock->m && blockPayloadLen != (size_t) BLOCK_SIZE(receivedBlock->szx)) { // 413 Error handling of too large entity if (COAP_MAX_PDU_SIZE < BLOCK_SIZE(receivedBlock->szx) + optionLen) @@ -1935,14 +1864,8 @@ uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlo if (COAP_MAX_PDU_SIZE >= BLOCK_SIZE(size) + optionLen) { OIC_LOG_V(ERROR, TAG, "replace sze with %d", size); - if (COAP_OPTION_BLOCK2 == blockType) - { - currData->block2.szx = size; - } - else - { - currData->block1.szx = size; - } + currData->block2.szx = size; + currData->block1.szx = size; break; } } @@ -2062,33 +1985,69 @@ CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint VERIFY_NON_NULL_RET(pdu->hdr, TAG, "pdu->hdr", NULL); VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL); - CAInfo_t responseData = { .tokenLength = pdu->hdr->coap_hdr_udp_t.token_length }; - responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength); - if (!responseData.token) + CARequestInfo_t* requestInfo = NULL; + CAResponseInfo_t* responseInfo = NULL; + + uint32_t code = pdu->hdr->coap_hdr_udp_t.code; + if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code) { - OIC_LOG(ERROR, TAG, "out of memory"); - return NULL; - } - memcpy(responseData.token, pdu->hdr->coap_hdr_udp_t.token, responseData.tokenLength); + CAInfo_t responseData = { .tokenLength = pdu->hdr->coap_hdr_udp_t.token_length }; + responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength); + if (!responseData.token) + { + OIC_LOG(ERROR, TAG, "out of memory"); + return NULL; + } + memcpy(responseData.token, pdu->hdr->coap_hdr_udp_t.token, responseData.tokenLength); - CAResponseInfo_t* responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t)); - if (!responseInfo) + responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t)); + if (!responseInfo) + { + OIC_LOG(ERROR, TAG, "out of memory"); + OICFree(responseData.token); + return NULL; + } + responseInfo->info = responseData; + } + else { - OIC_LOG(ERROR, TAG, "out of memory"); - OICFree(responseData.token); - return NULL; + CAInfo_t requestData = { .tokenLength = pdu->hdr->coap_hdr_udp_t.token_length }; + requestData.token = (CAToken_t) OICMalloc(requestData.tokenLength); + if (!requestData.token) + { + OIC_LOG(ERROR, TAG, "out of memory"); + return NULL; + } + memcpy(requestData.token, pdu->hdr->coap_hdr_udp_t.token, requestData.tokenLength); + + requestInfo = (CARequestInfo_t*) OICCalloc(1, sizeof(CARequestInfo_t)); + if (!requestInfo) + { + OIC_LOG(ERROR, TAG, "out of memory"); + OICFree(requestData.token); + return NULL; + } + requestInfo->info = requestData; + + // get resource uri information from received response message + // to send next request message to remote device + CAResponseInfo_t resInfo = { 0 }; + CAGetResponseInfoFromPDU(pdu, &resInfo, endpoint); + + requestInfo->method = CA_GET; + requestInfo->info.resourceUri = resInfo.info.resourceUri; } - responseInfo->info = responseData; CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t)); if (!data) { OIC_LOG(ERROR, TAG, "out of memory"); + OICFree(requestInfo); OICFree(responseInfo); return NULL; } - data->requestInfo = NULL; + data->requestInfo = requestInfo; data->responseInfo = responseInfo; data->remoteEndpoint = CACloneEndpoint(endpoint); data->type = SEND_TYPE_UNICAST; @@ -2477,8 +2436,7 @@ CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID) return NULL; } -coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID, - uint16_t blockType) +coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID, uint16_t blockType) { OIC_LOG(DEBUG, TAG, "IN-GetBlockOption"); VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL); @@ -2552,7 +2510,7 @@ CABlockData_t *CACreateNewBlockData(const CAData_t *sendData) data->block1.szx = CA_DEFAULT_BLOCK_SIZE; data->block2.szx = CA_DEFAULT_BLOCK_SIZE; data->sentData = CACloneCAData(sendData); - if(!data->sentData) + if (!data->sentData) { OIC_LOG(ERROR, TAG, PCF("memory alloc has failed")); OICFree(data); @@ -2567,7 +2525,7 @@ CABlockData_t *CACreateNewBlockData(const CAData_t *sendData) tokenLength = data->sentData->requestInfo->info.tokenLength; token = data->sentData->requestInfo->info.token; } - else if(data->sentData->responseInfo) + else if (data->sentData->responseInfo) { tokenLength = data->sentData->responseInfo->info.tokenLength; token = data->sentData->responseInfo->info.token; @@ -2581,9 +2539,8 @@ CABlockData_t *CACreateNewBlockData(const CAData_t *sendData) return NULL; } - CABlockDataID_t* blockDataID = CACreateBlockDatablockId( - token, tokenLength, - data->sentData->remoteEndpoint->port); + CABlockDataID_t* blockDataID = CACreateBlockDatablockId(token, tokenLength, + data->sentData->remoteEndpoint->port); if (NULL == blockDataID || blockDataID->idLength < 1) { OIC_LOG(ERROR, TAG, "blockId is null"); @@ -2663,9 +2620,9 @@ void CADestroyDataSet(CAData_t* data) CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength, uint16_t portNumber) { - char port[PORT_LENGTH] = {0,}; - port[0] = (char)((portNumber>>8) & 0xFF); - port[1] = (char)(portNumber & 0xFF); + char port[PORT_LENGTH] = { 0, }; + port[0] = (char) ((portNumber >> 8) & 0xFF); + port[1] = (char) (portNumber & 0xFF); CABlockDataID_t* blockDataID = (CABlockDataID_t *) OICMalloc(sizeof(CABlockDataID_t)); if (!blockDataID) diff --git a/resource/csdk/connectivity/src/camessagehandler.c b/resource/csdk/connectivity/src/camessagehandler.c index d91ef7d..09b5b4b 100644 --- a/resource/csdk/connectivity/src/camessagehandler.c +++ b/resource/csdk/connectivity/src/camessagehandler.c @@ -414,11 +414,7 @@ static CAResult_t CAProcessMulticastData(const CAData_t *data) if (NULL != pdu) { #ifdef WITH_BWT - if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter -#ifdef WITH_TCP - && !CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter) -#endif - ) + if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter)) { // Blockwise transfer res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options); @@ -448,11 +444,7 @@ static CAResult_t CAProcessMulticastData(const CAData_t *data) if (NULL != pdu) { #ifdef WITH_BWT - if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter -#ifdef WITH_TCP - && !CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter) -#endif - ) + if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter)) { // Blockwise transfer if (NULL != info) @@ -560,11 +552,7 @@ static CAResult_t CAProcessSendData(const CAData_t *data) if (NULL != pdu) { #ifdef WITH_BWT - if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter -#ifdef WITH_TCP - && !CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter) -#endif - ) + if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter)) { // Blockwise transfer if (NULL != info) @@ -821,11 +809,7 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, CAProcessReceivedData(cadata); #else #ifdef WITH_BWT - if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter -#ifdef WITH_TCP - && !CAIsSupportedCoAPOverTCP(sep->endpoint.adapter) -#endif - ) + if (CAIsSupportedBlockwiseTransfer(sep->endpoint.adapter)) { CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen); if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res) @@ -1009,11 +993,7 @@ CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg CADestroyData(data, sizeof(CAData_t)); #else #ifdef WITH_BWT - if (CA_ADAPTER_GATT_BTLE != endpoint->adapter -#ifdef WITH_TCP - && !CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter) -#endif - ) + if (CAIsSupportedBlockwiseTransfer(endpoint->adapter)) { // send block data CAResult_t res = CASendBlockWiseData(data); diff --git a/resource/csdk/connectivity/src/caprotocolmessage.c b/resource/csdk/connectivity/src/caprotocolmessage.c index d1975ed..5ef5a87 100644 --- a/resource/csdk/connectivity/src/caprotocolmessage.c +++ b/resource/csdk/connectivity/src/caprotocolmessage.c @@ -1120,6 +1120,18 @@ CAPayloadFormat_t CAConvertFormat(uint8_t format) } } +#ifdef WITH_BWT +bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter) +{ + if (CA_ADAPTER_IP & adapter || CA_ADAPTER_NFC & adapter + || CA_DEFAULT_ADAPTER == adapter) + { + return true; + } + return false; +} +#endif + #ifdef WITH_TCP bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter) { -- 2.7.4