1 /* ****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 // Defining _BSD_SOURCE or _DEFAULT_SOURCE causes header files to expose
22 // definitions that may otherwise be skipped. Skipping can cause implicit
23 // declaration warnings and/or bugs and subtle problems in code execution.
24 // For glibc information on feature test macros,
25 // Refer http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
27 // This file requires #define use due to random()
28 // For details on compatibility and glibc support,
29 // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html
30 #define _DEFAULT_SOURCE
37 #include "caadapterutils.h"
38 #include "cainterface.h"
39 #include "camessagehandler.h"
40 #include "caremotehandler.h"
41 #include "cablockwisetransfer.h"
42 #include "oic_malloc.h"
43 #include "oic_string.h"
47 #define TAG "OIC_CA_BWT"
49 #define BLOCKWISE_OPTION_BUFFER (sizeof(unsigned int))
50 #define BLOCK_NUMBER_IDX 4
51 #define BLOCK_M_BIT_IDX 3
54 #define BLOCK_SIZE(arg) (1 << ((arg) + 4))
56 // context for block-wise transfer
57 static CABlockWiseContext_t g_context = { .sendThreadFunc = NULL,
58 .receivedThreadFunc = NULL,
61 static bool CACheckPayloadLength(const CAData_t *sendData)
63 size_t payloadLen = 0;
64 CAGetPayloadInfo(sendData, &payloadLen);
66 // check if message has to be transfered to a block
67 size_t maxBlockSize = BLOCK_SIZE(CA_DEFAULT_BLOCK_SIZE);
68 OIC_LOG_V(DEBUG, TAG, "payloadLen=%zu, maxBlockSize=%zu", payloadLen, maxBlockSize);
70 if (payloadLen <= maxBlockSize)
78 CAResult_t CAInitializeBlockWiseTransfer(CASendThreadFunc sendThreadFunc,
79 CAReceiveThreadFunc receivedThreadFunc)
81 OIC_LOG(DEBUG, TAG, "CAInitializeBlockWiseTransfer");
83 // set block-wise transfer context
84 if (!g_context.sendThreadFunc)
86 g_context.sendThreadFunc = sendThreadFunc;
89 if (!g_context.receivedThreadFunc)
91 g_context.receivedThreadFunc = receivedThreadFunc;
94 if (!g_context.dataList)
96 g_context.dataList = u_arraylist_create();
99 CAResult_t res = CAInitBlockWiseMutexVariables();
100 if (CA_STATUS_OK != res)
102 u_arraylist_free(&g_context.dataList);
103 g_context.dataList = NULL;
104 OIC_LOG(ERROR, TAG, "init has failed");
110 CAResult_t CATerminateBlockWiseTransfer()
112 OIC_LOG(DEBUG, TAG, "CATerminateBlockWiseTransfer");
114 if (g_context.dataList)
116 CARemoveAllBlockDataFromList();
117 u_arraylist_free(&g_context.dataList);
120 CATerminateBlockWiseMutexVariables();
125 CAResult_t CAInitBlockWiseMutexVariables()
127 if (!g_context.blockDataListMutex)
129 g_context.blockDataListMutex = ca_mutex_new();
130 if (!g_context.blockDataListMutex)
132 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
133 return CA_STATUS_FAILED;
137 if (!g_context.blockDataSenderMutex)
139 g_context.blockDataSenderMutex = ca_mutex_new();
140 if (!g_context.blockDataSenderMutex)
142 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
143 CATerminateBlockWiseMutexVariables();
144 return CA_STATUS_FAILED;
151 void CATerminateBlockWiseMutexVariables()
153 if (g_context.blockDataListMutex)
155 ca_mutex_free(g_context.blockDataListMutex);
156 g_context.blockDataListMutex = NULL;
159 if (g_context.blockDataSenderMutex)
161 ca_mutex_free(g_context.blockDataSenderMutex);
162 g_context.blockDataSenderMutex = NULL;
166 CAResult_t CASendBlockWiseData(const CAData_t *sendData)
168 VERIFY_NON_NULL(sendData, TAG, "sendData");
170 // check if message type is CA_MSG_RESET
171 if (sendData->requestInfo)
173 if (CA_MSG_RESET == sendData->requestInfo->info.type)
175 OIC_LOG(DEBUG, TAG, "reset message can't be sent to the block");
176 return CA_NOT_SUPPORTED;
180 * Other uses of the Block options in conjunction with multicast
181 * messages are for further study.
183 if (sendData->requestInfo->isMulticast)
185 OIC_LOG(DEBUG, TAG, "multicast message can't be sent to the block");
186 return CA_NOT_SUPPORTED;
189 else if (sendData->responseInfo)
191 if (CA_MSG_RESET == sendData->responseInfo->info.type)
193 OIC_LOG(DEBUG, TAG, "reset message can't be sent to the block");
194 return CA_NOT_SUPPORTED;
196 if (sendData->responseInfo->isMulticast)
198 OIC_LOG(DEBUG, TAG, "multicast message can't be sent to the block");
199 return CA_NOT_SUPPORTED;
203 // #1. check if it is already included in block data list
204 CABlockData_t *currData = NULL;
205 CAResult_t res = CACheckBlockDataValidation(sendData, &currData);
206 if (CA_STATUS_OK != res)
208 // #2. if it is not included, add the data into list
211 OIC_LOG(DEBUG, TAG, "There is no block data");
213 bool isBlock = CACheckPayloadLength(sendData);
216 if (sendData->requestInfo)
218 currData = CACreateNewBlockData(sendData);
221 OIC_LOG(ERROR, TAG, "failed to create block data");
222 return CA_MEMORY_ALLOC_FAILED;
225 return CA_NOT_SUPPORTED;
227 currData = CACreateNewBlockData(sendData);
230 OIC_LOG(ERROR, TAG, "failed to create block data");
231 return CA_MEMORY_ALLOC_FAILED;
236 // #3. check request/response block option type and payload length
237 res = CACheckBlockOptionType(currData);
238 if (CA_STATUS_OK == res)
240 // #4. send block message
241 OIC_LOG(DEBUG, TAG, "send first block msg");
242 res = CAAddSendThreadQueue(currData->sentData,
243 (const CABlockDataID_t *) &currData->blockDataId);
244 if (CA_STATUS_OK != res)
246 OIC_LOG(ERROR, TAG, "add has failed");
254 CAResult_t CAAddSendThreadQueue(const CAData_t *sendData, const CABlockDataID_t *blockID)
256 VERIFY_NON_NULL(sendData, TAG, "sendData");
257 VERIFY_NON_NULL(blockID, TAG, "blockID");
259 CAData_t *cloneData = CACloneCAData(sendData);
262 OIC_LOG(ERROR, TAG, "clone has failed");
263 CARemoveBlockDataFromList(blockID);
264 return CA_STATUS_FAILED;
267 if (g_context.sendThreadFunc)
269 ca_mutex_lock(g_context.blockDataSenderMutex);
270 g_context.sendThreadFunc(cloneData);
271 ca_mutex_unlock(g_context.blockDataSenderMutex);
275 CADestroyDataSet(cloneData);
280 CAResult_t CACheckBlockOptionType(CABlockData_t *currData)
282 VERIFY_NON_NULL(currData, TAG, "currData");
283 VERIFY_NON_NULL(currData->sentData, TAG, "currData->sentData");
285 bool isBlock = CACheckPayloadLength(currData->sentData);
288 return CA_NOT_SUPPORTED;
291 // set block option (COAP_OPTION_BLOCK2 or COAP_OPTION_BLOCK1)
292 if (currData->sentData->requestInfo) // request message
294 currData->type = COAP_OPTION_BLOCK1;
296 else // response message
298 currData->type = COAP_OPTION_BLOCK2;
304 // TODO make pdu const after libcoap is updated to support that.
305 CAResult_t CAReceiveBlockWiseData(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
306 const CAData_t *receivedData, size_t dataLen)
308 OIC_LOG(DEBUG, TAG, "CAReceiveBlockWiseData");
309 VERIFY_NON_NULL(pdu, TAG, "pdu");
310 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
311 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
312 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
314 // check if received message type is CA_MSG_RESET
315 if (CA_EMPTY == pdu->hdr->coap_hdr_udp_t.code)
317 OIC_LOG(DEBUG, TAG, "code is CA_EMPTY..");
319 if (!receivedData->responseInfo->info.token)
321 // get token from block-wise transfer list when CA_EMPTY(RST/ACK) is received
322 CAResult_t res = CAGetTokenFromBlockDataList(pdu, endpoint,
323 receivedData->responseInfo);
324 if (CA_STATUS_OK != res)
326 OIC_LOG(ERROR, TAG, "fail to get token");
331 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
332 receivedData->responseInfo->info.token,
333 receivedData->responseInfo->info.tokenLength,
335 if (NULL == blockDataID || blockDataID->idLength < 1)
337 // if retransmission is timeout, callback msg will be send without token.
338 if (NULL == blockDataID && !receivedData->responseInfo->info.token)
340 OIC_LOG(INFO, TAG, "retransmission was stopped");
341 return CA_REQUEST_TIMEOUT;
344 OIC_LOG(ERROR, TAG, "blockId is null");
345 CADestroyBlockID(blockDataID);
346 return CA_STATUS_FAILED;
349 // If we didn't send the last block message and received EMPTY message,
350 // we have to remain the block data from list.
351 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
352 if (data && (data->block1.m || data->block2.m))
354 OIC_LOG(DEBUG, TAG, "this is normal EMPTY message for blockwise-transfer.");
355 CADestroyBlockID(blockDataID);
359 CARemoveBlockDataFromList(blockDataID);
360 CADestroyBlockID(blockDataID);
361 return CA_NOT_SUPPORTED;
364 // check if block option is set and get block data
365 coap_block_t block = { 0, 0, 0 };
368 int isBlock1 = coap_get_block(pdu, COAP_OPTION_BLOCK1, &block);
371 CAResult_t res = CASetNextBlockOption1(pdu, endpoint, receivedData, block, dataLen);
372 if (CA_STATUS_OK != res)
374 OIC_LOG(ERROR, TAG, "setting has failed");
380 int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
383 CAResult_t res = CASetNextBlockOption2(pdu, endpoint, receivedData, block, dataLen);
384 if (CA_STATUS_OK != res)
386 OIC_LOG(ERROR, TAG, "setting has failed");
391 // if there is no block option in pdu, check if there is error code.
392 if (!isBlock1 && !isBlock2)
394 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
395 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
396 pdu->hdr->coap_hdr_udp_t.token_length,
398 if (NULL == blockDataID || blockDataID->idLength < 1)
400 OIC_LOG(ERROR, TAG, "blockId is null");
401 CADestroyBlockID(blockDataID);
402 return CA_STATUS_FAILED;
405 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code);
406 if (CA_REQUEST_ENTITY_INCOMPLETE == code)
408 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
411 OIC_LOG(ERROR, TAG, "getting has failed");
412 CADestroyBlockID(blockDataID);
413 return CA_STATUS_FAILED;
416 coap_block_t *block = CAGetBlockOption(blockDataID, data->type);
419 OIC_LOG(ERROR, TAG, "block is null");
420 CADestroyBlockID(blockDataID);
421 return CA_STATUS_FAILED;
424 CAResult_t res = CA_STATUS_OK;
425 if (COAP_OPTION_BLOCK2 == data->type)
427 res = CASetNextBlockOption2(pdu, endpoint, receivedData, *block, dataLen);
428 if (CA_STATUS_OK != res)
430 OIC_LOG(ERROR, TAG, "setting has failed");
431 CADestroyBlockID(blockDataID);
432 return CA_STATUS_FAILED;
435 else if (COAP_OPTION_BLOCK1 == data->type)
437 res = CASetNextBlockOption1(pdu, endpoint, receivedData, *block, dataLen);
438 if (CA_STATUS_OK != res)
440 OIC_LOG(ERROR, TAG, "setting has failed");
441 CADestroyBlockID(blockDataID);
445 CADestroyBlockID(blockDataID);
450 OIC_LOG(DEBUG, TAG, "it's normal pdu");
452 // if received data is response message
453 // and sent data remain in block data list, remove block data
454 if (receivedData->responseInfo)
456 CARemoveBlockDataFromList(blockDataID);
458 CADestroyBlockID(blockDataID);
459 return CA_NOT_SUPPORTED;
465 CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData,
466 uint8_t blockWiseStatus, const CABlockDataID_t *blockID)
468 VERIFY_NON_NULL(pdu, TAG, "pdu");
469 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
470 VERIFY_NON_NULL(blockID, TAG, "blockID");
472 CAResult_t res = CA_STATUS_OK;
473 CAData_t *data = NULL;
475 // process blockWiseStatus
476 switch (blockWiseStatus)
478 case CA_OPTION2_FIRST_BLOCK:
479 res = CAAddSendThreadQueue(receivedData, blockID);
480 if (CA_STATUS_OK != res)
482 OIC_LOG(ERROR, TAG, "add has failed");
487 case CA_OPTION2_REQUEST:
488 // add data to send thread
489 data = CAGetDataSetFromBlockDataList(blockID);
492 OIC_LOG(ERROR, TAG, "it's unavailable");
493 return CA_STATUS_FAILED;
496 if (data->responseInfo)
498 data->responseInfo->info.type =
499 (pdu->hdr->coap_hdr_udp_t.type == CA_MSG_CONFIRM) ?
500 CA_MSG_ACKNOWLEDGE : CA_MSG_NONCONFIRM;
501 data->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
503 res = CAAddSendThreadQueue(data, blockID);
504 if (CA_STATUS_OK != res)
506 OIC_LOG(ERROR, TAG, "add has failed");
512 case CA_OPTION1_RESPONSE:
513 case CA_OPTION2_RESPONSE:
514 case CA_OPTION1_REQUEST_BLOCK:
515 res = CASendBlockMessage(pdu, pdu->hdr->coap_hdr_udp_t.type, blockID);
516 if (CA_STATUS_OK != res)
518 OIC_LOG(ERROR, TAG, "send has failed");
523 case CA_OPTION2_LAST_BLOCK:
524 // process last block and send upper layer
525 res = CAReceiveLastBlock(blockID, receivedData);
526 if (CA_STATUS_OK != res)
528 OIC_LOG(ERROR, TAG, "receive has failed");
532 // remove data from list
533 res = CARemoveBlockDataFromList(blockID);
534 if (CA_STATUS_OK != res)
536 OIC_LOG(ERROR, TAG, "remove has failed");
541 case CA_OPTION1_REQUEST_LAST_BLOCK:
542 // process last block and send upper layer
543 res = CAReceiveLastBlock(blockID, receivedData);
544 if (CA_STATUS_OK != res)
546 OIC_LOG(ERROR, TAG, "receive has failed");
551 case CA_BLOCK_INCOMPLETE:
552 // add data to send thread
553 res = CASendErrorMessage(pdu, blockWiseStatus, CA_REQUEST_ENTITY_INCOMPLETE, blockID);
554 if (CA_STATUS_OK != res)
556 OIC_LOG(ERROR, TAG, "send has failed");
561 case CA_BLOCK_TOO_LARGE:
562 if (receivedData->requestInfo)
564 res = CASendErrorMessage(pdu, blockWiseStatus, CA_REQUEST_ENTITY_TOO_LARGE,
566 if (CA_STATUS_OK != res)
568 OIC_LOG(ERROR, TAG, "send has failed");
572 else if (receivedData->responseInfo)
574 res = CASendBlockMessage(pdu, pdu->hdr->coap_hdr_udp_t.type, blockID);
575 if (CA_STATUS_OK != res)
577 OIC_LOG(ERROR, TAG, "send has failed");
583 OIC_LOG_V(ERROR, TAG, "no logic [%d]", blockWiseStatus);
588 static CAResult_t CASendDirectEmptyResponse(const CAEndpoint_t *endpoint, uint16_t messageId)
590 OIC_LOG(DEBUG, TAG, "Entering CASendDirectEmptyResponse");
591 CAResponseInfo_t respInfo = {
594 respInfo.info.type = CA_MSG_ACKNOWLEDGE;
595 respInfo.info.messageId = messageId;
597 CAResult_t caResult = CASendResponse(endpoint, &respInfo);
599 OIC_LOG(DEBUG, TAG, "Exit CASendDirectEmptyResponse");
603 CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType,
604 const CABlockDataID_t *blockID)
606 VERIFY_NON_NULL(pdu, TAG, "pdu");
607 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
608 VERIFY_NON_NULL(blockID, TAG, "blockID");
610 CAData_t *data = CAGetDataSetFromBlockDataList(blockID);
613 OIC_LOG(ERROR, TAG, "CAData is unavailable");
614 return CA_STATUS_FAILED;
617 CAMessageType_t sentMsgType = CA_MSG_NONCONFIRM;
621 sentMsgType = CA_MSG_ACKNOWLEDGE;
623 case CA_MSG_ACKNOWLEDGE:
624 sentMsgType = CA_MSG_CONFIRM;
627 sentMsgType = CA_MSG_NONCONFIRM;
631 uint32_t code = pdu->hdr->coap_hdr_udp_t.code;
632 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
634 if (data->responseInfo)
636 OIC_LOG(DEBUG, TAG, "set response info");
637 data->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
638 data->responseInfo->info.type = sentMsgType;
639 data->responseInfo->result = CA_CONTINUE;
644 // if the received response message type is CON, send empty message.
645 // and then, send next block request message with new messagId.
646 if (msgType == CA_MSG_CONFIRM)
648 CASendDirectEmptyResponse(data->remoteEndpoint,
649 data->requestInfo->info.messageId);
650 sentMsgType = CA_MSG_CONFIRM;
653 if (data->requestInfo)
655 OIC_LOG(DEBUG, TAG, "need new msgID");
656 data->requestInfo->info.messageId = 0;
657 data->requestInfo->info.type = sentMsgType;
661 // add data to send thread
662 CAResult_t res = CAAddSendThreadQueue(data, blockID);
663 if (CA_STATUS_OK != res)
665 OIC_LOG(ERROR, TAG, "add has failed");
671 CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status,
672 CAResponseResult_t responseResult, const CABlockDataID_t *blockID)
674 VERIFY_NON_NULL(pdu, TAG, "pdu");
675 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
676 VERIFY_NON_NULL(blockID, TAG, "blockID");
678 // create error responseInfo
679 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockID);
682 OIC_LOG(ERROR, TAG, "data is unavailable");
683 return CA_STATUS_FAILED;
686 CAMessageType_t sentMsgType = CA_MSG_NONCONFIRM;
687 switch (pdu->hdr->coap_hdr_udp_t.type)
690 sentMsgType = CA_MSG_ACKNOWLEDGE;
692 case CA_MSG_ACKNOWLEDGE:
693 sentMsgType = CA_MSG_CONFIRM;
697 CAData_t *cloneData = NULL;
700 cloneData = CACloneCAData(data->sentData);
703 OIC_LOG(ERROR, TAG, "clone has failed");
704 return CA_MEMORY_ALLOC_FAILED;
707 if (cloneData->responseInfo)
709 cloneData->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
710 cloneData->responseInfo->info.type = sentMsgType;
711 cloneData->responseInfo->result = responseResult;
715 CAInfo_t responseData = { .tokenLength = pdu->hdr->coap_hdr_udp_t.token_length };
716 responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength);
717 if (!responseData.token)
719 OIC_LOG(ERROR, TAG, "out of memory");
720 return CA_MEMORY_ALLOC_FAILED;
722 memcpy(responseData.token, pdu->hdr->coap_hdr_udp_t.token, responseData.tokenLength);
724 cloneData->responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
725 if (!cloneData->responseInfo)
727 OIC_LOG(ERROR, TAG, "out of memory");
728 OICFree(responseData.token);
729 return CA_MEMORY_ALLOC_FAILED;
732 cloneData->responseInfo->info = responseData;
733 cloneData->responseInfo->info.type = sentMsgType;
734 cloneData->responseInfo->result = responseResult;
736 OIC_LOG(DEBUG, TAG, "set response message to send error code");
740 OIC_LOG(ERROR, TAG, "data has no sent-data");
741 return CA_MEMORY_ALLOC_FAILED;
744 // if there is a requestInfo, remove it to send response message
745 if (cloneData->requestInfo)
747 CADestroyRequestInfoInternal(cloneData->requestInfo);
748 cloneData->requestInfo = NULL;
751 // add data to send thread
752 if (g_context.sendThreadFunc)
754 ca_mutex_lock(g_context.blockDataSenderMutex);
755 g_context.sendThreadFunc(cloneData);
756 ca_mutex_unlock(g_context.blockDataSenderMutex);
760 CADestroyDataSet(cloneData);
763 // if error code is 4.08, remove the stored payload and initialize block number
764 if (CA_BLOCK_INCOMPLETE == status)
766 OICFree(data->payload);
767 data->payload = NULL;
768 data->payloadLength = 0;
769 data->receivedPayloadLen = 0;
770 data->block1.num = 0;
771 data->block2.num = 0;
777 CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID, const CAData_t *receivedData)
779 VERIFY_NON_NULL(blockID, TAG, "blockID");
780 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
782 // total block data have to notify to Application
783 CAData_t *cloneData = CACloneCAData(receivedData);
786 OIC_LOG(ERROR, TAG, "clone has failed");
787 return CA_MEMORY_ALLOC_FAILED;
791 size_t fullPayloadLen = 0;
792 CAPayload_t fullPayload = CAGetPayloadFromBlockDataList(blockID, &fullPayloadLen);
795 CAResult_t res = CAUpdatePayloadToCAData(cloneData, fullPayload, fullPayloadLen);
796 if (CA_STATUS_OK != res)
798 OIC_LOG(ERROR, TAG, "update has failed");
799 CADestroyDataSet(cloneData);
804 if (g_context.receivedThreadFunc)
806 g_context.receivedThreadFunc(cloneData);
810 CADestroyDataSet(cloneData);
816 static CABlockData_t* CACheckTheExistOfBlockData(const CABlockDataID_t* blockDataID,
817 coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
820 // Get BlockData data. If does not exist, create a new data
821 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
824 OIC_LOG(DEBUG, TAG, "block data doesn't exist in list. create new one");
826 CAData_t *cadata = CACreateNewDataSet(pdu, endpoint);
829 OIC_LOG(ERROR, TAG, "data is null");
833 data = CACreateNewBlockData(cadata);
836 OIC_LOG(ERROR, TAG, "failed to create a new block data");
837 CADestroyDataSet(cadata);
840 CADestroyDataSet(cadata);
843 // update BLOCK OPTION type
844 CAResult_t res = CAUpdateBlockOptionType(blockDataID, blockType);
845 if (CA_STATUS_OK != res)
847 OIC_LOG(ERROR, TAG, "update has failed");
854 // TODO make pdu const after libcoap is updated to support that.
855 CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
856 const CAData_t *receivedData, coap_block_t block,
859 OIC_LOG(INFO, TAG, "CASetNextBlockOption1");
860 VERIFY_NON_NULL(pdu, TAG, "pdu");
861 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
862 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
863 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
865 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
867 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
868 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
869 pdu->hdr->coap_hdr_udp_t.token_length,
871 if (NULL == blockDataID || blockDataID->idLength < 1)
873 OIC_LOG(ERROR, TAG, "blockId is null");
874 CADestroyBlockID(blockDataID);
875 return CA_STATUS_FAILED;
878 CABlockData_t *data = CACheckTheExistOfBlockData(blockDataID, pdu, endpoint,
882 OIC_LOG(ERROR, TAG, "Failed to create or get block data");
886 CAResult_t res = CA_STATUS_OK;
887 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
888 uint32_t code = pdu->hdr->coap_hdr_udp_t.code;
889 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
891 // received message type is request
892 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
894 // check the size option
895 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu, COAP_OPTION_SIZE1,
896 &(data->payloadLength));
898 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
899 COAP_OPTION_BLOCK1, dataLen);
901 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
903 // store the received payload and merge
904 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
905 isSizeOption, COAP_OPTION_BLOCK1);
906 if (CA_STATUS_OK != res)
908 OIC_LOG(ERROR, TAG, "update has failed");
912 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1,
914 if (CA_STATUS_OK != res)
916 OIC_LOG(ERROR, TAG, "update has failed");
921 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
922 if (CA_STATUS_OK != res)
924 OIC_LOG(ERROR, TAG, "update has failed");
929 // check the blcok-wise transfer status for next step
930 if (CA_BLOCK_UNKNOWN == blockWiseStatus || CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
932 OIC_LOG_V(DEBUG, TAG, "M bit is %d", block.m);
934 blockWiseStatus = (0 == block.m) ?
935 CA_OPTION1_REQUEST_LAST_BLOCK : CA_OPTION1_REQUEST_BLOCK;
940 // received message type is response
941 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code);
942 if (0 == block.m && (CA_REQUEST_ENTITY_INCOMPLETE != code
943 && CA_REQUEST_ENTITY_TOO_LARGE != code))
945 int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
948 OIC_LOG(INFO, TAG, "received data is combining block1 and block2");
949 // initialize block number for response message
950 data->block1.num = 0;
951 CADestroyBlockID(blockDataID);
956 OIC_LOG(INFO, TAG, "received data is not bulk data");
957 CAReceiveLastBlock(blockDataID, receivedData);
963 blockWiseStatus = CA_OPTION1_RESPONSE;
964 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus);
965 if (CA_STATUS_OK != res)
967 OIC_LOG(ERROR, TAG, "update has failed");
968 CADestroyBlockID(blockDataID);
972 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
973 if (CA_STATUS_OK != res)
975 OIC_LOG(ERROR, TAG, "update has failed");
980 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
981 if (CA_STATUS_OK != res)
983 OIC_LOG(ERROR, TAG, "setting has failed");
987 CADestroyBlockID(blockDataID);
991 CARemoveBlockDataFromList(blockDataID);
992 CADestroyBlockID(blockDataID);
996 // TODO make pdu const after libcoap is updated to support that.
997 CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
998 const CAData_t *receivedData, coap_block_t block,
1001 OIC_LOG(DEBUG, TAG, "CASetNextBlockOption2");
1002 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
1004 VERIFY_NON_NULL(pdu, TAG, "pdu");
1005 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
1006 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1007 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
1009 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
1010 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
1011 pdu->hdr->coap_hdr_udp_t.token_length,
1013 if (NULL == blockDataID || blockDataID->idLength < 1)
1015 OIC_LOG(ERROR, TAG, "blockId is null");
1016 CADestroyBlockID(blockDataID);
1017 return CA_STATUS_FAILED;
1020 CABlockData_t *data = CACheckTheExistOfBlockData(blockDataID, pdu, endpoint,
1021 COAP_OPTION_BLOCK2);
1024 OIC_LOG(ERROR, TAG, "Failed to create or get block data");
1028 CAResult_t res = CA_STATUS_OK;
1029 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
1030 if (0 == block.num && CA_GET == pdu->hdr->coap_hdr_udp_t.code && 0 == block.m)
1032 OIC_LOG(INFO, TAG, "first block number");
1034 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1035 if (CA_STATUS_OK != res)
1037 OIC_LOG(ERROR, TAG, "update has failed");
1041 // first block data have to notify to Application
1042 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1043 if (CA_STATUS_OK != res)
1045 OIC_LOG(ERROR, TAG, "update has failed");
1048 blockWiseStatus = CA_OPTION2_FIRST_BLOCK;
1052 uint32_t code = pdu->hdr->coap_hdr_udp_t.code;
1053 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1055 // received message type is request
1056 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
1058 blockWiseStatus = CA_OPTION2_REQUEST;
1060 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1061 if (CA_STATUS_OK != res)
1063 OIC_LOG(ERROR, TAG, "update has failed");
1067 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1068 if (CA_STATUS_OK != res)
1070 OIC_LOG(ERROR, TAG, "update has failed");
1076 // received message type is response
1077 OIC_LOG(DEBUG, TAG, "received response message with block option2");
1079 // check the size option
1080 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
1082 &(data->payloadLength));
1084 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code);
1085 if (CA_REQUEST_ENTITY_INCOMPLETE != code && CA_REQUEST_ENTITY_TOO_LARGE != code)
1087 // check if received payload is exact
1088 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
1089 COAP_OPTION_BLOCK2, dataLen);
1092 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
1094 // store the received payload and merge
1095 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
1096 isSizeOption, COAP_OPTION_BLOCK2);
1097 if (CA_STATUS_OK != res)
1099 OIC_LOG(ERROR, TAG, "update has failed");
1104 if (0 == block.m && CA_BLOCK_UNKNOWN == blockWiseStatus) // Last block is received
1106 OIC_LOG(DEBUG, TAG, "M bit is 0");
1107 blockWiseStatus = CA_OPTION2_LAST_BLOCK;
1111 if (CA_BLOCK_UNKNOWN == blockWiseStatus ||
1112 CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
1114 OIC_LOG(DEBUG, TAG, "M bit is 1");
1115 blockWiseStatus = CA_OPTION2_RESPONSE;
1118 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2,
1120 if (CA_STATUS_OK != res)
1122 OIC_LOG(ERROR, TAG, "update has failed");
1126 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1127 if (CA_STATUS_OK != res)
1129 OIC_LOG(ERROR, TAG, "update has failed");
1136 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
1137 if (CA_STATUS_OK != res)
1139 OIC_LOG(ERROR, TAG, "setting has failed");
1143 CADestroyBlockID(blockDataID);
1144 return CA_STATUS_OK;
1147 CARemoveBlockDataFromList(blockDataID);
1148 CADestroyBlockID(blockDataID);
1152 CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu,
1153 coap_block_t *block, uint16_t blockType,
1156 VERIFY_NON_NULL(currData, TAG, "currData");
1157 VERIFY_NON_NULL(pdu, TAG, "pdu");
1158 VERIFY_NON_NULL(block, TAG, "block");
1160 // update block data
1161 CAResult_t res = CA_STATUS_OK;
1162 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code);
1164 if (CA_REQUEST_ENTITY_INCOMPLETE == code || CA_REQUEST_ENTITY_TOO_LARGE == code)
1166 // response error code of the received block message
1167 res = CAHandleBlockErrorResponse(block, blockType, code);
1168 if (CA_STATUS_OK != res)
1170 OIC_LOG(ERROR, TAG, "error handle has failed");
1176 // update block option items
1179 case CA_OPTION1_RESPONSE:
1180 if (currData->block1.num > block->num)
1182 OIC_LOG(ERROR, TAG, "received incorrect block num");
1183 return CA_STATUS_FAILED;
1187 case CA_OPTION2_REQUEST:
1190 case CA_OPTION2_RESPONSE:
1191 if (currData->block2.num > block->num)
1193 OIC_LOG(ERROR, TAG, "received incorrect block num");
1194 return CA_STATUS_FAILED;
1199 case CA_BLOCK_TOO_LARGE:
1200 // if state of received block message is CA_BLOCK_TOO_LARGE or CA_BLOCK_INCOMPLETE
1201 // we set the response error code appropriately and send
1202 if (COAP_OPTION_BLOCK2 == blockType)
1206 block->szx = currData->block2.szx;
1210 block->szx = currData->block1.szx;
1214 OIC_LOG_V(ERROR, TAG, "no logic [%d]", status);
1217 if (CA_BLOCK_INCOMPLETE != status && CA_BLOCK_TOO_LARGE != status)
1219 // negotiate block size
1220 res = CANegotiateBlockSize(currData, block, pdu, blockType);
1221 if (CA_STATUS_OK != res)
1223 OIC_LOG(ERROR, TAG, "negotiation has failed");
1231 CAResult_t CASetMoreBitFromBlock(size_t payloadLen, coap_block_t *block)
1233 VERIFY_NON_NULL(block, TAG, "block");
1235 if ((size_t) ((block->num + 1) << (block->szx + BLOCK_NUMBER_IDX)) < payloadLen)
1237 OIC_LOG(DEBUG, TAG, "Set the M-bit(1)");
1242 OIC_LOG(DEBUG, TAG, "Set the M-bit(0)");
1246 return CA_STATUS_OK;
1249 CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block,
1250 const coap_pdu_t *pdu, uint16_t blockType)
1252 OIC_LOG(DEBUG, TAG, "IN-NegotiateBlockSize");
1254 VERIFY_NON_NULL(currData, TAG, "currData");
1255 VERIFY_NON_NULL(block, TAG, "block");
1256 VERIFY_NON_NULL(pdu, TAG, "pdu");
1257 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
1259 bool isReqMsg = false;
1260 uint32_t code = pdu->hdr->coap_hdr_udp_t.code;
1261 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1266 // #1. check the block option type
1267 if (COAP_OPTION_BLOCK2 == blockType)
1269 // #2. check the message type
1272 if (block->szx > currData->block2.szx)
1274 OIC_LOG(DEBUG, TAG, "sze is big");
1276 // #3. calculate new block number from block size
1277 unsigned int blockNum = BLOCK_SIZE(block->szx) /
1278 BLOCK_SIZE(currData->block2.szx) - 1;
1279 OIC_LOG(DEBUG, TAG, "num is set as Negotiation");
1280 block->num += blockNum;
1281 block->szx = currData->block2.szx;
1282 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1287 if (block->szx > currData->block2.szx)
1289 OIC_LOG(DEBUG, TAG, "sze is big");
1290 block->szx = currData->block2.szx;
1294 else if (COAP_OPTION_BLOCK1 == blockType)
1298 if (block->szx < currData->block1.szx)
1300 OIC_LOG(DEBUG, TAG, "sze is small");
1302 unsigned int blockNum = BLOCK_SIZE(currData->block1.szx) /
1303 BLOCK_SIZE(block->szx) - 1;
1304 block->num += blockNum;
1305 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1310 if (block->szx > currData->block1.szx)
1312 OIC_LOG(DEBUG, TAG, "sze is big");
1313 block->szx = currData->block1.szx;
1319 OIC_LOG(DEBUG, TAG, "Invalid block option");
1320 return CA_STATUS_FAILED;
1323 OIC_LOG(DEBUG, TAG, "OUT-NegotiateBlockSize");
1325 return CA_STATUS_OK;
1328 CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block,
1331 VERIFY_NON_NULL(currData, TAG, "currData");
1333 // check if block size is bigger than CABlockSize_t
1334 if (block.szx > CA_BLOCK_SIZE_1024_BYTE)
1336 OIC_LOG(DEBUG, TAG, "invalid block szx");
1337 return CA_STATUS_FAILED;
1340 // update block option
1341 if (COAP_OPTION_BLOCK2 == blockType)
1343 currData->block2 = block;
1347 currData->block1 = block;
1350 OIC_LOG(DEBUG, TAG, "data has updated");
1351 return CA_STATUS_OK;
1354 CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID)
1356 VERIFY_NON_NULL(pdu, TAG, "pdu");
1357 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
1358 VERIFY_NON_NULL(blockID, TAG, "blockID");
1360 // if message is sent, update messageId in block-wise transfer list
1361 CAData_t * cadata = CAGetDataSetFromBlockDataList(blockID);
1364 OIC_LOG(ERROR, TAG, "CAData is unavailable");
1365 return CA_STATUS_FAILED;
1368 if (cadata->requestInfo)
1370 cadata->requestInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
1373 return CA_STATUS_OK;
1376 CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info,
1377 const CAEndpoint_t *endpoint, coap_list_t **options)
1379 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption");
1380 VERIFY_NON_NULL(pdu, TAG, "pdu");
1381 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1382 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1383 VERIFY_NON_NULL(info, TAG, "info");
1384 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1385 VERIFY_NON_NULL(options, TAG, "options");
1387 CAResult_t res = CA_STATUS_OK;
1388 size_t dataLength = 0;
1391 dataLength = info->payloadSize;
1392 OIC_LOG_V(DEBUG, TAG, "dataLength - %zu", dataLength);
1395 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
1396 (CAToken_t)(*pdu)->hdr->coap_hdr_udp_t.token,
1397 (*pdu)->hdr->coap_hdr_udp_t.token_length,
1399 if (NULL == blockDataID || blockDataID->idLength < 1)
1401 OIC_LOG(ERROR, TAG, "blockId is null");
1402 res = CA_STATUS_FAILED;
1406 uint32_t repCode = CA_RESPONSE_CODE((*pdu)->hdr->coap_hdr_udp_t.code);
1407 if (CA_REQUEST_ENTITY_INCOMPLETE == repCode)
1409 OIC_LOG(INFO, TAG, "don't use option");
1414 uint8_t blockType = CAGetBlockOptionType(blockDataID);
1415 if (COAP_OPTION_BLOCK2 == blockType)
1417 res = CAAddBlockOption2(pdu, info, dataLength, blockDataID, options);
1418 if (CA_STATUS_OK != res)
1420 OIC_LOG(ERROR, TAG, "add has failed");
1424 else if (COAP_OPTION_BLOCK1 == blockType)
1426 res = CAAddBlockOption1(pdu, info, dataLength, blockDataID, options);
1427 if (CA_STATUS_OK != res)
1429 OIC_LOG(ERROR, TAG, "add has failed");
1435 OIC_LOG(DEBUG, TAG, "no BLOCK option");
1437 // in case it is not large data, add option list to pdu.
1440 for (coap_list_t *opt = *options; opt; opt = opt->next)
1442 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
1443 COAP_OPTION_DATA(*(coap_option *) opt->data));
1445 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", (*pdu)->length);
1446 coap_add_option(*pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
1447 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
1448 COAP_OPTION_DATA(*(coap_option *) opt->data), coap_udp);
1452 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", (*pdu)->length);
1454 // if response data is so large. it have to send as block transfer
1455 if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload))
1457 OIC_LOG(INFO, TAG, "it have to use block");
1458 res = CA_STATUS_FAILED;
1463 OIC_LOG(INFO, TAG, "not Blockwise Transfer");
1467 uint32_t code = (*pdu)->hdr->coap_hdr_udp_t.code;
1468 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1470 // if received message type is RESET from remote device,
1471 // we have to use the updated message id of request message to find token.
1472 CABlockData_t *blockData = CAGetBlockDataFromBlockDataList(blockDataID);
1475 res = CAUpdateMessageId(*pdu, blockDataID);
1476 if (CA_STATUS_OK != res)
1478 OIC_LOG(ERROR, TAG, "fail to update message id");
1485 CADestroyBlockID(blockDataID);
1486 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption");
1490 CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
1491 const CABlockDataID_t *blockID, coap_list_t **options)
1493 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption2");
1494 VERIFY_NON_NULL(pdu, TAG, "pdu");
1495 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1496 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1497 VERIFY_NON_NULL(info, TAG, "info");
1498 VERIFY_NON_NULL(blockID, TAG, "blockID");
1499 VERIFY_NON_NULL(options, TAG, "options");
1501 // get set block data from CABlock list-set.
1502 coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1503 coap_block_t *block2 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK2);
1504 if (!block1 || !block2)
1506 OIC_LOG(ERROR, TAG, "getting has failed");
1507 return CA_STATUS_FAILED;
1510 CAResult_t res = CA_STATUS_OK;
1511 uint32_t code = (*pdu)->hdr->coap_hdr_udp_t.code;
1512 if (CA_GET != code && CA_POST != code && CA_PUT != code && CA_DELETE != code)
1514 CASetMoreBitFromBlock(dataLength, block2);
1516 // if block number is 0, add size2 option
1517 if (0 == block2->num)
1519 res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE2, dataLength, options);
1520 if (CA_STATUS_OK != res)
1522 OIC_LOG(ERROR, TAG, "add has failed");
1527 res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options);
1528 if (CA_STATUS_OK != res)
1530 OIC_LOG(ERROR, TAG, "add has failed");
1536 OIC_LOG(DEBUG, TAG, "combining block1 and block2");
1537 res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1538 if (CA_STATUS_OK != res)
1540 OIC_LOG(ERROR, TAG, "add has failed");
1543 // initialize block number
1547 res = CAAddOptionToPDU(*pdu, options);
1548 if (CA_STATUS_OK != res)
1550 OIC_LOG(ERROR, TAG, "add has failed");
1554 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload,
1555 block2->num, block2->szx))
1557 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1558 return CA_STATUS_FAILED;
1561 CALogBlockInfo(block2);
1565 // if sent message is last response block message, remove data
1566 CARemoveBlockDataFromList(blockID);
1571 OIC_LOG(DEBUG, TAG, "option2, not response msg");
1572 res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options);
1573 if (CA_STATUS_OK != res)
1575 OIC_LOG(ERROR, TAG, "add has failed");
1579 res = CAAddOptionToPDU(*pdu, options);
1580 if (CA_STATUS_OK != res)
1582 OIC_LOG(ERROR, TAG, "add has failed");
1585 CALogBlockInfo(block2);
1588 return CA_STATUS_OK;
1591 CARemoveBlockDataFromList(blockID);
1595 CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
1596 const CABlockDataID_t *blockID, coap_list_t **options)
1598 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption1");
1599 VERIFY_NON_NULL(pdu, TAG, "pdu");
1600 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1601 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1602 VERIFY_NON_NULL(info, TAG, "info");
1603 VERIFY_NON_NULL(blockID, TAG, "blockID");
1604 VERIFY_NON_NULL(options, TAG, "options");
1606 // get set block data from CABlock list-set.
1607 coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1610 OIC_LOG(ERROR, TAG, "getting has failed");
1611 return CA_STATUS_FAILED;
1614 CAResult_t res = CA_STATUS_OK;
1615 uint32_t code = (*pdu)->hdr->coap_hdr_udp_t.code;
1616 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1618 CASetMoreBitFromBlock(dataLength, block1);
1620 // if block number is 0, add size1 option
1621 if (0 == block1->num)
1623 res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE1, dataLength, options);
1624 if (CA_STATUS_OK != res)
1626 OIC_LOG(ERROR, TAG, "add has failed");
1631 // add block option to option list.
1632 res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1633 if (CA_STATUS_OK != res)
1635 OIC_LOG(ERROR, TAG, "add has failed");
1639 // add option list to pdu.
1640 res = CAAddOptionToPDU(*pdu, options);
1641 if (CA_STATUS_OK != res)
1643 OIC_LOG(ERROR, TAG, "add has failed");
1647 // add the payload data as the block size.
1648 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload, block1->num,
1651 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1652 return CA_STATUS_FAILED;
1657 OIC_LOG(DEBUG, TAG, "received response message with block option1");
1659 // add block option to option list.
1660 res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1661 if (CA_STATUS_OK != res)
1663 OIC_LOG(ERROR, TAG, "add has failed");
1667 // add option list to pdu.
1668 res = CAAddOptionToPDU(*pdu, options);
1669 if (CA_STATUS_OK != res)
1671 OIC_LOG(ERROR, TAG, "add has failed");
1675 // add the payload data as the block size.
1676 if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload))
1678 OIC_LOG(ERROR, TAG, "failed to add payload");
1679 return CA_STATUS_FAILED;
1682 // if it is last block message, remove block data from list.
1685 // remove data from list
1686 res = CARemoveBlockDataFromList(blockID);
1687 if (CA_STATUS_OK != res)
1689 OIC_LOG(ERROR, TAG, "remove has failed");
1695 CALogBlockInfo(block1);
1697 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption1");
1699 return CA_STATUS_OK;
1702 CARemoveBlockDataFromList(blockID);
1706 CAResult_t CAAddBlockOptionImpl(coap_block_t *block, uint8_t blockType,
1707 coap_list_t **options)
1709 OIC_LOG(DEBUG, TAG, "IN-AddBlockOptionImpl");
1710 VERIFY_NON_NULL(block, TAG, "block");
1711 VERIFY_NON_NULL(options, TAG, "options");
1713 unsigned char buf[BLOCKWISE_OPTION_BUFFER] = { 0 };
1714 unsigned int optionLength = coap_encode_var_bytes(buf,
1715 ((block->num << BLOCK_NUMBER_IDX)
1716 | (block->m << BLOCK_M_BIT_IDX)
1719 int ret = coap_insert(options,
1720 CACreateNewOptionNode(blockType, optionLength, (char *) buf),
1724 return CA_STATUS_INVALID_PARAM;
1727 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOptionImpl");
1728 return CA_STATUS_OK;
1731 CAResult_t CAAddOptionToPDU(coap_pdu_t *pdu, coap_list_t **options)
1733 // after adding the block option to option list, add option list to pdu.
1736 for (coap_list_t *opt = *options; opt; opt = opt->next)
1738 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
1739 COAP_OPTION_DATA(*(coap_option *) opt->data));
1741 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
1742 int ret = coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
1743 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
1744 COAP_OPTION_DATA(*(coap_option *) opt->data), coap_udp);
1747 return CA_STATUS_FAILED;
1752 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
1754 return CA_STATUS_OK;
1757 CAResult_t CAAddBlockSizeOption(coap_pdu_t *pdu, uint16_t sizeType, size_t dataLength,
1758 coap_list_t **options)
1760 OIC_LOG(DEBUG, TAG, "IN-CAAddBlockSizeOption");
1761 VERIFY_NON_NULL(pdu, TAG, "pdu");
1762 VERIFY_NON_NULL(options, TAG, "options");
1764 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1766 OIC_LOG(ERROR, TAG, "unknown option type");
1767 return CA_STATUS_FAILED;
1770 unsigned char value[BLOCKWISE_OPTION_BUFFER] = { 0 };
1771 unsigned int optionLength = coap_encode_var_bytes(value, dataLength);
1773 int ret = coap_insert(options,
1774 CACreateNewOptionNode(sizeType, optionLength, (char *) value),
1778 return CA_STATUS_INVALID_PARAM;
1781 OIC_LOG(DEBUG, TAG, "OUT-CAAddBlockSizeOption");
1783 return CA_STATUS_OK;
1786 // TODO make pdu const after libcoap is updated to support that.
1787 bool CAIsPayloadLengthInPduWithBlockSizeOption(coap_pdu_t *pdu,
1789 size_t *totalPayloadLen)
1791 OIC_LOG(DEBUG, TAG, "IN-CAIsPayloadLengthInPduWithBlockSizeOption");
1792 VERIFY_NON_NULL(pdu, TAG, "pdu");
1793 VERIFY_NON_NULL(totalPayloadLen, TAG, "totalPayloadLen");
1795 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1797 OIC_LOG(ERROR, TAG, "unknown option type");
1798 return CA_STATUS_FAILED;
1801 coap_opt_iterator_t opt_iter;
1802 coap_opt_t *option = coap_check_option(pdu, sizeType, &opt_iter);
1805 OIC_LOG(DEBUG, TAG, "get size option from pdu");
1806 *totalPayloadLen = coap_decode_var_bytes(COAP_OPT_VALUE(option),
1807 COAP_OPT_LENGTH(option));
1809 OIC_LOG_V(DEBUG, TAG, "the total payload length to be received is [%zu]bytes",
1815 OIC_LOG(DEBUG, TAG, "OUT-CAIsPayloadLengthInPduWithBlockSizeOption");
1820 uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlock,
1821 const CAData_t *receivedData, uint16_t blockType,
1824 OIC_LOG(DEBUG, TAG, "IN-CheckBlockError");
1826 VERIFY_NON_NULL(currData, TAG, "currData is NULL");
1827 VERIFY_NON_NULL(receivedBlock, TAG, "receivedBlock is NULL");
1828 VERIFY_NON_NULL(receivedData, TAG, "receivedData is NULL");
1830 // #1. check the received payload length
1831 size_t blockPayloadLen = 0;
1832 CAGetPayloadInfo(receivedData, &blockPayloadLen);
1834 // #2. check if the block sequence is right
1835 if (COAP_OPTION_BLOCK1 == blockType)
1837 size_t prePayloadLen = currData->receivedPayloadLen;
1838 if (prePayloadLen != (size_t) BLOCK_SIZE(receivedBlock->szx) * receivedBlock->num)
1840 if (receivedBlock->num > currData->block1.num + 1)
1842 // 408 Error handling of block loss
1843 OIC_LOG(ERROR, TAG, "option1: error 4.08");
1844 OIC_LOG(ERROR, TAG, "it didn't order");
1845 return CA_BLOCK_INCOMPLETE;
1847 return CA_BLOCK_RECEIVED_ALREADY;
1850 else if (COAP_OPTION_BLOCK2 == blockType)
1852 if (receivedBlock->num != currData->block2.num)
1854 if (receivedBlock->num > currData->block2.num)
1856 // 408 Error handling of block loss
1857 OIC_LOG(ERROR, TAG, "option2: error 4.08");
1858 OIC_LOG(ERROR, TAG, "it didn't order");
1859 return CA_BLOCK_INCOMPLETE;
1861 return CA_BLOCK_RECEIVED_ALREADY;
1865 // #3. check if error check logic is required
1866 size_t optionLen = dataLen - blockPayloadLen;
1867 if (receivedBlock->m && blockPayloadLen != (size_t) BLOCK_SIZE(receivedBlock->szx))
1869 // 413 Error handling of too large entity
1870 if (COAP_MAX_PDU_SIZE < BLOCK_SIZE(receivedBlock->szx) + optionLen)
1872 // buffer size is smaller than received block size
1873 OIC_LOG(ERROR, TAG, "error type 4.13");
1874 OIC_LOG(ERROR, TAG, "too large size");
1876 // set the block size to be smaller than COAP_MAX_PDU_SIZE
1877 for (int32_t size = CA_DEFAULT_BLOCK_SIZE; size >= 0; size--)
1879 if (COAP_MAX_PDU_SIZE >= BLOCK_SIZE(size) + optionLen)
1881 OIC_LOG_V(ERROR, TAG, "replace sze with %d", size);
1882 currData->block2.szx = size;
1883 currData->block1.szx = size;
1887 return CA_BLOCK_TOO_LARGE;
1891 // 408 Error handling of payload loss
1892 OIC_LOG(ERROR, TAG, "error type 4.08");
1893 OIC_LOG(ERROR, TAG, "payload len != block sze");
1894 return CA_BLOCK_INCOMPLETE;
1897 else if (0 == receivedBlock->m && 0 != currData->payloadLength)
1899 // if the received block is last block, check the total payload length
1900 size_t receivedPayloadLen = currData->receivedPayloadLen;
1901 receivedPayloadLen += blockPayloadLen;
1903 if (receivedPayloadLen != currData->payloadLength)
1905 OIC_LOG(ERROR, TAG, "error type 4.08");
1906 OIC_LOG(ERROR, TAG, "total payload length is wrong");
1907 return CA_BLOCK_INCOMPLETE;
1911 OIC_LOG(DEBUG, TAG, "received all data normally");
1913 OIC_LOG(DEBUG, TAG, "OUT-CheckBlockError");
1915 return CA_BLOCK_UNKNOWN;
1918 CAResult_t CAUpdatePayloadData(CABlockData_t *currData, const CAData_t *receivedData,
1919 uint8_t status, bool isSizeOption, uint16_t blockType)
1921 OIC_LOG(DEBUG, TAG, "IN-UpdatePayloadData");
1923 VERIFY_NON_NULL(currData, TAG, "currData");
1924 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
1926 // if error code is 4.08, do not update payload
1927 if (CA_BLOCK_INCOMPLETE == status)
1929 OIC_LOG(ERROR, TAG, "no require to update");
1930 return CA_STATUS_OK;
1933 size_t blockPayloadLen = 0;
1934 CAPayload_t blockPayload = CAGetPayloadInfo(receivedData, &blockPayloadLen);
1936 if (CA_BLOCK_TOO_LARGE == status)
1938 blockPayloadLen = (COAP_OPTION_BLOCK2 == blockType) ?
1939 BLOCK_SIZE(currData->block2.szx) : BLOCK_SIZE(currData->block1.szx);
1942 // memory allocation for the received block payload
1943 size_t prePayloadLen = currData->receivedPayloadLen;
1946 if (currData->payloadLength)
1948 // in case the block message has the size option
1949 // allocate the memory for the total payload
1952 CAPayload_t prePayload = currData->payload;
1954 OIC_LOG(DEBUG, TAG, "allocate memory for the total payload");
1955 currData->payload = (CAPayload_t) OICCalloc(1, currData->payloadLength);
1956 if (NULL == currData->payload)
1958 OIC_LOG(ERROR, TAG, "out of memory");
1959 return CA_MEMORY_ALLOC_FAILED;
1961 memcpy(currData->payload, prePayload, prePayloadLen);
1962 OICFree(prePayload);
1965 // update the total payload
1966 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
1970 OIC_LOG(DEBUG, TAG, "allocate memory for the received block payload");
1972 size_t totalPayloadLen = prePayloadLen + blockPayloadLen;
1973 CAPayload_t newPayload = OICRealloc(currData->payload, totalPayloadLen);
1974 if (NULL == newPayload)
1976 OIC_LOG(ERROR, TAG, "out of memory");
1977 return CA_MEMORY_ALLOC_FAILED;
1980 // update the total payload
1981 memset(newPayload + prePayloadLen, 0, blockPayloadLen);
1982 currData->payload = newPayload;
1983 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
1986 // update received payload length
1987 currData->receivedPayloadLen += blockPayloadLen;
1989 OIC_LOG_V(DEBUG, TAG, "updated payload: %s, len: %zu", currData->payload,
1990 currData->receivedPayloadLen);
1993 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayloadData");
1994 return CA_STATUS_OK;
1997 CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
1999 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", NULL);
2000 VERIFY_NON_NULL_RET(pdu->hdr, TAG, "pdu->hdr", NULL);
2001 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
2003 CARequestInfo_t* requestInfo = NULL;
2004 CAResponseInfo_t* responseInfo = NULL;
2006 uint32_t code = pdu->hdr->coap_hdr_udp_t.code;
2007 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
2009 CAInfo_t responseData = { .tokenLength = pdu->hdr->coap_hdr_udp_t.token_length };
2010 responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength);
2011 if (!responseData.token)
2013 OIC_LOG(ERROR, TAG, "out of memory");
2016 memcpy(responseData.token, pdu->hdr->coap_hdr_udp_t.token, responseData.tokenLength);
2018 responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
2021 OIC_LOG(ERROR, TAG, "out of memory");
2022 OICFree(responseData.token);
2025 responseInfo->info = responseData;
2029 CAInfo_t requestData = { .tokenLength = pdu->hdr->coap_hdr_udp_t.token_length };
2030 requestData.token = (CAToken_t) OICMalloc(requestData.tokenLength);
2031 if (!requestData.token)
2033 OIC_LOG(ERROR, TAG, "out of memory");
2036 memcpy(requestData.token, pdu->hdr->coap_hdr_udp_t.token, requestData.tokenLength);
2038 requestInfo = (CARequestInfo_t*) OICCalloc(1, sizeof(CARequestInfo_t));
2041 OIC_LOG(ERROR, TAG, "out of memory");
2042 OICFree(requestData.token);
2045 requestInfo->info = requestData;
2047 // get resource uri information from received response message
2048 // to send next request message to remote device
2049 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(*resInfo));
2052 OIC_LOG(ERROR, TAG, "memory allocation failed");
2053 OICFree(requestData.token);
2057 CAGetResponseInfoFromPDU(pdu, resInfo, endpoint);
2058 requestInfo->method = CA_GET;
2059 requestInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu->hdr, pdu->length);
2060 requestInfo->info.resourceUri = OICStrdup(resInfo->info.resourceUri);
2062 // after copying the resource uri, destroy response info.
2063 CADestroyResponseInfoInternal(resInfo);
2066 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2069 OIC_LOG(ERROR, TAG, "out of memory");
2070 OICFree(requestInfo);
2071 OICFree(responseInfo);
2075 data->requestInfo = requestInfo;
2076 data->responseInfo = responseInfo;
2077 data->remoteEndpoint = CACloneEndpoint(endpoint);
2078 data->type = SEND_TYPE_UNICAST;
2083 CAData_t *CACloneCAData(const CAData_t *data)
2085 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2087 CAData_t *clone = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2090 OIC_LOG(ERROR, TAG, "out of memory");
2095 if (data->requestInfo)
2097 clone->requestInfo = CACloneRequestInfo(data->requestInfo);
2099 else if (data->responseInfo)
2101 clone->responseInfo = CACloneResponseInfo(data->responseInfo);
2104 if (data->remoteEndpoint)
2106 clone->remoteEndpoint = CACloneEndpoint(data->remoteEndpoint);
2112 CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
2115 OIC_LOG(DEBUG, TAG, "IN-UpdatePayload");
2117 VERIFY_NON_NULL(data, TAG, "data is NULL");
2118 VERIFY_NON_NULL(payload, TAG, "payload is NULL");
2120 CAPayload_t newPayload = NULL;
2121 switch (data->dataType)
2123 case CA_REQUEST_DATA:
2124 if (!data->requestInfo)
2126 OIC_LOG(ERROR, TAG, "request info is null");
2127 return CA_STATUS_FAILED;
2129 // allocate payload field
2130 newPayload = OICRealloc(data->requestInfo->info.payload, payloadLen);
2133 OIC_LOG(ERROR, TAG, "out of memory");
2134 return CA_STATUS_FAILED;
2136 data->requestInfo->info.payload = newPayload;
2137 memcpy(data->requestInfo->info.payload, payload, payloadLen);
2138 data->requestInfo->info.payloadSize = payloadLen;
2141 case CA_RESPONSE_DATA:
2142 if (!data->responseInfo)
2144 OIC_LOG(ERROR, TAG, "response info is null");
2145 return CA_STATUS_FAILED;
2147 // allocate payload field
2148 newPayload = OICRealloc(data->responseInfo->info.payload, payloadLen);
2151 OIC_LOG(ERROR, TAG, "out of memory");
2152 return CA_STATUS_FAILED;
2154 data->responseInfo->info.payload = newPayload;
2155 memcpy(data->responseInfo->info.payload, payload, payloadLen);
2156 data->responseInfo->info.payloadSize = payloadLen;
2160 // does not occur case
2161 OIC_LOG(ERROR, TAG, "not supported data type");
2162 return CA_NOT_SUPPORTED;
2165 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayload");
2167 return CA_STATUS_OK;
2170 CAPayload_t CAGetPayloadInfo(const CAData_t *data, size_t *payloadLen)
2172 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2173 VERIFY_NON_NULL_RET(payloadLen, TAG, "payloadLen", NULL);
2175 if (data->requestInfo)
2177 if (data->requestInfo->info.payload)
2179 *payloadLen = data->requestInfo->info.payloadSize;
2180 return data->requestInfo->info.payload;
2185 if (data->responseInfo->info.payload)
2187 *payloadLen = data->responseInfo->info.payloadSize;
2188 return data->responseInfo->info.payload;
2195 CAResult_t CAHandleBlockErrorResponse(coap_block_t *block, uint16_t blockType,
2196 uint32_t responseResult)
2198 OIC_LOG(DEBUG, TAG, "IN-HandleBlockErrorRes");
2199 VERIFY_NON_NULL(block, TAG, "block is NULL");
2201 // update block data
2202 switch (responseResult)
2204 case CA_REQUEST_ENTITY_INCOMPLETE:
2207 case CA_REQUEST_ENTITY_TOO_LARGE:
2208 if (COAP_OPTION_BLOCK1 == blockType)
2215 OIC_LOG_V(ERROR, TAG, "there is no Error Code of BWT[%d]", responseResult);
2216 return CA_STATUS_FAILED;
2219 OIC_LOG(DEBUG, TAG, "OUT-HandleBlockErrorRes");
2220 return CA_STATUS_OK;
2223 CAResult_t CAUpdateBlockOptionType(const CABlockDataID_t *blockID, uint8_t blockType)
2225 OIC_LOG(DEBUG, TAG, "IN-UpdateBlockOptionType");
2226 VERIFY_NON_NULL(blockID, TAG, "blockID");
2228 ca_mutex_lock(g_context.blockDataListMutex);
2230 size_t len = u_arraylist_length(g_context.dataList);
2231 for (size_t i = 0; i < len; i++)
2233 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2234 if (CABlockidMatches(currData, blockID))
2236 currData->type = blockType;
2237 ca_mutex_unlock(g_context.blockDataListMutex);
2238 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2239 return CA_STATUS_OK;
2242 ca_mutex_unlock(g_context.blockDataListMutex);
2244 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2245 return CA_STATUS_FAILED;
2248 uint8_t CAGetBlockOptionType(const CABlockDataID_t *blockID)
2250 OIC_LOG(DEBUG, TAG, "IN-GetBlockOptionType");
2251 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", 0);
2253 ca_mutex_lock(g_context.blockDataListMutex);
2255 size_t len = u_arraylist_length(g_context.dataList);
2256 for (size_t i = 0; i < len; i++)
2258 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2259 if (CABlockidMatches(currData, blockID))
2261 ca_mutex_unlock(g_context.blockDataListMutex);
2262 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2263 return currData->type;
2266 ca_mutex_unlock(g_context.blockDataListMutex);
2268 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2272 CAData_t *CAGetDataSetFromBlockDataList(const CABlockDataID_t *blockID)
2274 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2276 ca_mutex_lock(g_context.blockDataListMutex);
2278 size_t len = u_arraylist_length(g_context.dataList);
2279 for (size_t i = 0; i < len; i++)
2281 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2282 if (CABlockidMatches(currData, blockID))
2284 ca_mutex_unlock(g_context.blockDataListMutex);
2285 return currData->sentData;
2288 ca_mutex_unlock(g_context.blockDataListMutex);
2293 CAResult_t CAGetTokenFromBlockDataList(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
2294 CAResponseInfo_t *responseInfo)
2296 OIC_LOG(DEBUG, TAG, "IN-CAGetTokenFromBlockDataList");
2297 VERIFY_NON_NULL(pdu, TAG, "pdu");
2298 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
2299 VERIFY_NON_NULL(responseInfo, TAG, "responseInfo");
2301 ca_mutex_lock(g_context.blockDataListMutex);
2303 size_t len = u_arraylist_length(g_context.dataList);
2304 for (size_t i = 0; i < len; i++)
2306 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2307 if (NULL == currData)
2312 if (NULL != currData->sentData && NULL != currData->sentData->requestInfo)
2314 if (pdu->hdr->coap_hdr_udp_t.id == currData->sentData->requestInfo->info.messageId &&
2315 endpoint->adapter == currData->sentData->remoteEndpoint->adapter)
2317 if (NULL != currData->sentData->requestInfo->info.token)
2319 uint8_t length = currData->sentData->requestInfo->info.tokenLength;
2320 responseInfo->info.tokenLength = length;
2321 responseInfo->info.token = (char *) OICMalloc(length);
2322 if (NULL == responseInfo->info.token)
2324 OIC_LOG(ERROR, TAG, "out of memory");
2325 ca_mutex_unlock(g_context.blockDataListMutex);
2326 return CA_MEMORY_ALLOC_FAILED;
2328 memcpy(responseInfo->info.token, currData->sentData->requestInfo->info.token,
2329 responseInfo->info.tokenLength);
2331 ca_mutex_unlock(g_context.blockDataListMutex);
2332 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2333 return CA_STATUS_OK;
2339 ca_mutex_unlock(g_context.blockDataListMutex);
2341 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2342 return CA_STATUS_FAILED;
2345 CAResult_t CACheckBlockDataValidation(const CAData_t *sendData, CABlockData_t **blockData)
2347 VERIFY_NON_NULL(sendData, TAG, "sendData");
2348 VERIFY_NON_NULL(blockData, TAG, "blockData");
2350 if (sendData->responseInfo)
2352 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2353 (CAToken_t)sendData->responseInfo->info.token,
2354 sendData->responseInfo->info.tokenLength,
2355 sendData->remoteEndpoint->port);
2356 if (NULL == blockDataID || blockDataID->idLength < 1)
2358 OIC_LOG(ERROR, TAG, "blockId is null");
2359 CADestroyBlockID(blockDataID);
2360 return CA_STATUS_FAILED;
2363 CABlockData_t *storedData = CAGetBlockDataFromBlockDataList(blockDataID);
2366 OIC_LOG(DEBUG, TAG, "Send response about the received block request.");
2367 if (storedData->sentData)
2369 OIC_LOG(DEBUG, TAG, "init block number");
2370 CADestroyDataSet(storedData->sentData);
2372 storedData->sentData = CACloneCAData(sendData);
2373 *blockData = storedData;
2374 CADestroyBlockID(blockDataID);
2375 return CA_STATUS_OK;
2377 CADestroyBlockID(blockDataID);
2380 return CA_STATUS_FAILED;
2383 CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID)
2385 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2387 ca_mutex_lock(g_context.blockDataListMutex);
2389 size_t len = u_arraylist_length(g_context.dataList);
2390 for (size_t i = 0; i < len; i++)
2392 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2393 if (CABlockidMatches(currData, blockID))
2395 ca_mutex_unlock(g_context.blockDataListMutex);
2399 ca_mutex_unlock(g_context.blockDataListMutex);
2404 coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID, uint16_t blockType)
2406 OIC_LOG(DEBUG, TAG, "IN-GetBlockOption");
2407 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2409 ca_mutex_lock(g_context.blockDataListMutex);
2411 size_t len = u_arraylist_length(g_context.dataList);
2412 for (size_t i = 0; i < len; i++)
2414 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2415 if (CABlockidMatches(currData, blockID))
2417 ca_mutex_unlock(g_context.blockDataListMutex);
2418 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2419 if (COAP_OPTION_BLOCK2 == blockType)
2421 return &currData->block2;
2423 else if (COAP_OPTION_BLOCK1 == blockType)
2425 return &currData->block1;
2429 ca_mutex_unlock(g_context.blockDataListMutex);
2431 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2435 CAPayload_t CAGetPayloadFromBlockDataList(const CABlockDataID_t *blockID,
2436 size_t *fullPayloadLen)
2438 OIC_LOG(DEBUG, TAG, "IN-GetFullPayload");
2439 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2440 VERIFY_NON_NULL_RET(fullPayloadLen, TAG, "fullPayloadLen", NULL);
2442 ca_mutex_lock(g_context.blockDataListMutex);
2444 size_t len = u_arraylist_length(g_context.dataList);
2445 for (size_t i = 0; i < len; i++)
2447 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2448 if (CABlockidMatches(currData, blockID))
2450 ca_mutex_unlock(g_context.blockDataListMutex);
2451 *fullPayloadLen = currData->receivedPayloadLen;
2452 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2453 return currData->payload;
2456 ca_mutex_unlock(g_context.blockDataListMutex);
2458 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2462 CABlockData_t *CACreateNewBlockData(const CAData_t *sendData)
2464 OIC_LOG(DEBUG, TAG, "IN-CACreateNewBlockData");
2465 VERIFY_NON_NULL_RET(sendData, TAG, "sendData", NULL);
2467 // create block data
2468 CABlockData_t *data = (CABlockData_t *) OICCalloc(1, sizeof(CABlockData_t));
2471 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2475 data->block1.szx = CA_DEFAULT_BLOCK_SIZE;
2476 data->block2.szx = CA_DEFAULT_BLOCK_SIZE;
2477 data->sentData = CACloneCAData(sendData);
2478 if (!data->sentData)
2480 OIC_LOG(ERROR, TAG, PCF("memory alloc has failed"));
2485 CAToken_t token = NULL;
2486 uint8_t tokenLength = 0;
2487 if (data->sentData->requestInfo)
2489 // update token info
2490 tokenLength = data->sentData->requestInfo->info.tokenLength;
2491 token = data->sentData->requestInfo->info.token;
2493 else if (data->sentData->responseInfo)
2495 tokenLength = data->sentData->responseInfo->info.tokenLength;
2496 token = data->sentData->responseInfo->info.token;
2499 if (!data->sentData->remoteEndpoint)
2501 OIC_LOG(ERROR, TAG, "remoteEndpoint is null");
2502 CADestroyDataSet(data->sentData);
2507 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(token, tokenLength,
2508 data->sentData->remoteEndpoint->port);
2509 if (NULL == blockDataID || blockDataID->idLength < 1)
2511 OIC_LOG(ERROR, TAG, "blockId is null");
2512 CADestroyBlockID(blockDataID);
2513 CADestroyDataSet(data->sentData);
2517 data->blockDataId = blockDataID;
2519 ca_mutex_lock(g_context.blockDataListMutex);
2521 bool res = u_arraylist_add(g_context.dataList, (void *) data);
2524 OIC_LOG(ERROR, TAG, "add has failed");
2525 CADestroyBlockID(data->blockDataId);
2526 CADestroyDataSet(data->sentData);
2528 ca_mutex_unlock(g_context.blockDataListMutex);
2531 ca_mutex_unlock(g_context.blockDataListMutex);
2533 OIC_LOG(DEBUG, TAG, "OUT-CreateBlockData");
2537 CAResult_t CARemoveBlockDataFromList(const CABlockDataID_t *blockID)
2539 OIC_LOG(DEBUG, TAG, "CARemoveBlockData");
2540 VERIFY_NON_NULL(blockID, TAG, "blockID");
2542 ca_mutex_lock(g_context.blockDataListMutex);
2544 size_t len = u_arraylist_length(g_context.dataList);
2545 for (size_t i = 0; i < len; i++)
2547 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2548 if (CABlockidMatches(currData, blockID))
2550 CABlockData_t *removedData = u_arraylist_remove(g_context.dataList, i);
2553 OIC_LOG(ERROR, TAG, "data is NULL");
2554 ca_mutex_unlock(g_context.blockDataListMutex);
2555 return CA_STATUS_FAILED;
2559 CADestroyDataSet(currData->sentData);
2560 CADestroyBlockID(currData->blockDataId);
2561 OICFree(currData->payload);
2563 ca_mutex_unlock(g_context.blockDataListMutex);
2564 return CA_STATUS_OK;
2567 ca_mutex_unlock(g_context.blockDataListMutex);
2569 return CA_STATUS_OK;
2572 CAResult_t CARemoveAllBlockDataFromList()
2574 OIC_LOG(DEBUG, TAG, "CARemoveAllBlockDataFromList");
2576 ca_mutex_lock(g_context.blockDataListMutex);
2578 size_t len = u_arraylist_length(g_context.dataList);
2579 for (size_t i = len; i > 0; i--)
2581 CABlockData_t *removedData = u_arraylist_remove(g_context.dataList, i - 1);
2585 if (removedData->sentData)
2587 CADestroyDataSet(removedData->sentData);
2589 CADestroyBlockID(removedData->blockDataId);
2590 OICFree(removedData->payload);
2591 OICFree(removedData);
2594 ca_mutex_unlock(g_context.blockDataListMutex);
2596 return CA_STATUS_OK;
2599 void CADestroyDataSet(CAData_t* data)
2601 VERIFY_NON_NULL_VOID(data, TAG, "data");
2603 CAFreeEndpoint(data->remoteEndpoint);
2604 CADestroyRequestInfoInternal(data->requestInfo);
2605 CADestroyResponseInfoInternal(data->responseInfo);
2609 CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength,
2610 uint16_t portNumber)
2612 char port[PORT_LENGTH] = { 0, };
2613 port[0] = (char) ((portNumber >> 8) & 0xFF);
2614 port[1] = (char) (portNumber & 0xFF);
2616 CABlockDataID_t* blockDataID = (CABlockDataID_t *) OICMalloc(sizeof(CABlockDataID_t));
2619 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2622 blockDataID->idLength = tokenLength + sizeof(port);
2623 blockDataID->id = (uint8_t *) OICMalloc(blockDataID->idLength);
2624 if (!blockDataID->id)
2626 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2627 OICFree(blockDataID);
2633 memcpy(blockDataID->id, token, tokenLength);
2636 memcpy(blockDataID->id + tokenLength, port, sizeof(port));
2638 OIC_LOG(DEBUG, TAG, "BlockID is ");
2639 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)blockDataID->id, blockDataID->idLength);
2644 void CADestroyBlockID(CABlockDataID_t *blockID)
2646 VERIFY_NON_NULL_VOID(blockID, TAG, "blockID");
2647 OICFree(blockID->id);
2652 bool CABlockidMatches(const CABlockData_t *currData, const CABlockDataID_t *blockID)
2654 VERIFY_NON_NULL_RET(currData, TAG, "currData", false);
2655 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", false);
2656 VERIFY_NON_NULL_RET(blockID->id, TAG, "blockID->id", false);
2658 if ((currData->blockDataId)
2659 && (currData->blockDataId->id)
2660 && (currData->blockDataId->idLength == blockID->idLength)
2661 && !memcmp(currData->blockDataId->id, blockID->id, currData->blockDataId->idLength))
2668 void CALogBlockInfo(coap_block_t *block)
2670 VERIFY_NON_NULL_VOID(block, TAG, "block");
2672 OIC_LOG(DEBUG, TAG, "block option info");
2674 OIC_LOG_V(DEBUG, TAG, "block option-num : %d", block->num);
2676 OIC_LOG_V(DEBUG, TAG, "block option-m : %d", block->m);
2678 OIC_LOG_V(DEBUG, TAG, "block option-szx : %d", block->szx);
2681 CAResult_t CARemoveBlockDataFromListWithSeed(const CAToken_t token, uint8_t tokenLength,
2682 uint16_t portNumber)
2684 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(token, tokenLength, portNumber);
2685 if (NULL == blockDataID || blockDataID->idLength < 1)
2687 OIC_LOG(ERROR, TAG, "blockId is null");
2688 CADestroyBlockID(blockDataID);
2689 return CA_STATUS_FAILED;
2692 CAResult_t res = CA_STATUS_OK;
2694 if (NULL != CAGetBlockDataFromBlockDataList(blockDataID))
2696 res = CARemoveBlockDataFromList(blockDataID);
2697 if (CA_STATUS_OK != res)
2699 OIC_LOG(ERROR, TAG, "CARemoveBlockDataFromList failed");
2703 CADestroyBlockID(blockDataID);