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 = oc_mutex_new();
130 if (!g_context.blockDataListMutex)
132 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
133 return CA_STATUS_FAILED;
137 if (!g_context.blockDataSenderMutex)
139 g_context.blockDataSenderMutex = oc_mutex_new();
140 if (!g_context.blockDataSenderMutex)
142 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
143 CATerminateBlockWiseMutexVariables();
144 return CA_STATUS_FAILED;
151 void CATerminateBlockWiseMutexVariables()
153 if (g_context.blockDataListMutex)
155 oc_mutex_free(g_context.blockDataListMutex);
156 g_context.blockDataListMutex = NULL;
159 if (g_context.blockDataSenderMutex)
161 oc_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 oc_mutex_lock(g_context.blockDataSenderMutex);
270 g_context.sendThreadFunc(cloneData);
271 oc_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->transport_hdr, TAG, "pdu->transport_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->transport_hdr->udp.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,
334 endpoint->addr, endpoint->port);
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 uint32_t code = CA_RESPONSE_CODE(pdu->transport_hdr->udp.code);
395 if (CA_REQUEST_ENTITY_INCOMPLETE == code)
397 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
398 (CAToken_t)pdu->transport_hdr->udp.token,
399 pdu->transport_hdr->udp.token_length,
400 endpoint->addr, endpoint->port);
401 if (NULL == blockDataID || blockDataID->idLength < 1)
403 OIC_LOG(ERROR, TAG, "blockId is null");
404 CADestroyBlockID(blockDataID);
405 return CA_STATUS_FAILED;
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 CARemoveBlockDataFromListWithSeed((CAToken_t)pdu->transport_hdr->udp.token,
457 pdu->transport_hdr->udp.token_length,
458 endpoint->addr, endpoint->port);
460 return CA_NOT_SUPPORTED;
466 CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData,
467 uint8_t blockWiseStatus, const CABlockDataID_t *blockID)
469 VERIFY_NON_NULL(pdu, TAG, "pdu");
470 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
471 VERIFY_NON_NULL(blockID, TAG, "blockID");
473 CAResult_t res = CA_STATUS_OK;
474 CAData_t *data = NULL;
476 // process blockWiseStatus
477 switch (blockWiseStatus)
479 case CA_OPTION2_FIRST_BLOCK:
480 res = CAAddSendThreadQueue(receivedData, blockID);
481 if (CA_STATUS_OK != res)
483 OIC_LOG(ERROR, TAG, "add has failed");
488 case CA_OPTION2_REQUEST:
489 // add data to send thread
490 data = CAGetDataSetFromBlockDataList(blockID);
493 OIC_LOG(ERROR, TAG, "it's unavailable");
494 return CA_STATUS_FAILED;
497 if (data->responseInfo)
499 data->responseInfo->info.type =
500 (pdu->transport_hdr->udp.type == CA_MSG_CONFIRM) ?
501 CA_MSG_ACKNOWLEDGE : CA_MSG_NONCONFIRM;
502 data->responseInfo->info.messageId = pdu->transport_hdr->udp.id;
504 res = CAAddSendThreadQueue(data, blockID);
505 if (CA_STATUS_OK != res)
507 OIC_LOG(ERROR, TAG, "add has failed");
513 case CA_OPTION1_RESPONSE:
514 case CA_OPTION2_RESPONSE:
515 case CA_OPTION1_REQUEST_BLOCK:
516 res = CASendBlockMessage(pdu, pdu->transport_hdr->udp.type, blockID);
517 if (CA_STATUS_OK != res)
519 OIC_LOG(ERROR, TAG, "send has failed");
524 case CA_OPTION2_LAST_BLOCK:
525 // process last block and send upper layer
526 res = CAReceiveLastBlock(blockID, receivedData);
527 if (CA_STATUS_OK != res)
529 OIC_LOG(ERROR, TAG, "receive has failed");
533 // remove data from list
534 res = CARemoveBlockDataFromList(blockID);
535 if (CA_STATUS_OK != res)
537 OIC_LOG(ERROR, TAG, "remove has failed");
542 case CA_OPTION1_REQUEST_LAST_BLOCK:
543 // process last block and send upper layer
544 res = CAReceiveLastBlock(blockID, receivedData);
545 if (CA_STATUS_OK != res)
547 OIC_LOG(ERROR, TAG, "receive has failed");
552 case CA_BLOCK_INCOMPLETE:
553 // add data to send thread
554 res = CASendErrorMessage(pdu, blockWiseStatus, CA_REQUEST_ENTITY_INCOMPLETE, blockID);
555 if (CA_STATUS_OK != res)
557 OIC_LOG(ERROR, TAG, "send has failed");
562 case CA_BLOCK_TOO_LARGE:
563 if (receivedData->requestInfo)
565 res = CASendErrorMessage(pdu, blockWiseStatus, CA_REQUEST_ENTITY_TOO_LARGE,
567 if (CA_STATUS_OK != res)
569 OIC_LOG(ERROR, TAG, "send has failed");
573 else if (receivedData->responseInfo)
575 res = CASendBlockMessage(pdu, pdu->transport_hdr->udp.type, blockID);
576 if (CA_STATUS_OK != res)
578 OIC_LOG(ERROR, TAG, "send has failed");
584 OIC_LOG_V(ERROR, TAG, "no logic [%d]", blockWiseStatus);
589 static CAResult_t CASendDirectEmptyResponse(const CAEndpoint_t *endpoint, uint16_t messageId)
591 OIC_LOG(DEBUG, TAG, "Entering CASendDirectEmptyResponse");
592 CAResponseInfo_t respInfo = {
595 respInfo.info.type = CA_MSG_ACKNOWLEDGE;
596 respInfo.info.messageId = messageId;
597 respInfo.info.dataType = CA_RESPONSE_DATA;
599 CAResult_t caResult = CASendResponse(endpoint, &respInfo);
601 OIC_LOG(DEBUG, TAG, "Exit CASendDirectEmptyResponse");
605 CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType,
606 const CABlockDataID_t *blockID)
608 VERIFY_NON_NULL(pdu, TAG, "pdu");
609 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
610 VERIFY_NON_NULL(blockID, TAG, "blockID");
612 CAData_t *data = CAGetDataSetFromBlockDataList(blockID);
615 OIC_LOG(ERROR, TAG, "CAData is unavailable");
616 return CA_STATUS_FAILED;
619 CAMessageType_t sentMsgType = CA_MSG_NONCONFIRM;
623 sentMsgType = CA_MSG_ACKNOWLEDGE;
625 case CA_MSG_ACKNOWLEDGE:
626 sentMsgType = CA_MSG_CONFIRM;
629 sentMsgType = CA_MSG_NONCONFIRM;
633 uint32_t code = pdu->transport_hdr->udp.code;
634 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
636 if (data->responseInfo)
638 OIC_LOG(DEBUG, TAG, "set response info");
639 data->responseInfo->info.messageId = pdu->transport_hdr->udp.id;
640 data->responseInfo->info.type = sentMsgType;
641 data->responseInfo->result = CA_CONTINUE;
646 if (data->requestInfo)
648 // if the received response message type is CON, send empty message.
649 // and then, send next block request message with new messagId.
650 if (msgType == CA_MSG_CONFIRM)
652 CASendDirectEmptyResponse(data->remoteEndpoint,
653 data->requestInfo->info.messageId);
654 sentMsgType = CA_MSG_CONFIRM;
657 OIC_LOG(DEBUG, TAG, "need new msgID");
658 data->requestInfo->info.messageId = 0;
659 data->requestInfo->info.type = sentMsgType;
661 else if (data->responseInfo)
663 data->responseInfo->info.messageId = pdu->transport_hdr->udp.id;
664 data->responseInfo->info.type = sentMsgType;
668 // add data to send thread
669 CAResult_t res = CAAddSendThreadQueue(data, blockID);
670 if (CA_STATUS_OK != res)
672 OIC_LOG(ERROR, TAG, "add has failed");
678 CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status,
679 CAResponseResult_t responseResult, const CABlockDataID_t *blockID)
681 VERIFY_NON_NULL(pdu, TAG, "pdu");
682 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
683 VERIFY_NON_NULL(blockID, TAG, "blockID");
685 // create error responseInfo
686 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockID);
689 OIC_LOG(ERROR, TAG, "data is unavailable");
690 return CA_STATUS_FAILED;
693 CAMessageType_t sentMsgType = CA_MSG_NONCONFIRM;
694 switch (pdu->transport_hdr->udp.type)
697 sentMsgType = CA_MSG_ACKNOWLEDGE;
699 case CA_MSG_ACKNOWLEDGE:
700 sentMsgType = CA_MSG_CONFIRM;
703 sentMsgType = CA_MSG_NONCONFIRM;
706 CAData_t *cloneData = NULL;
709 cloneData = CACloneCAData(data->sentData);
712 OIC_LOG(ERROR, TAG, "clone has failed");
713 return CA_MEMORY_ALLOC_FAILED;
716 if (cloneData->responseInfo)
718 cloneData->responseInfo->info.messageId = pdu->transport_hdr->udp.id;
719 cloneData->responseInfo->info.type = sentMsgType;
720 cloneData->responseInfo->result = responseResult;
724 CAInfo_t responseData = { .tokenLength = pdu->transport_hdr->udp.token_length };
725 responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength);
726 if (!responseData.token)
728 OIC_LOG(ERROR, TAG, "out of memory");
729 return CA_MEMORY_ALLOC_FAILED;
731 memcpy(responseData.token, pdu->transport_hdr->udp.token, responseData.tokenLength);
733 cloneData->responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
734 if (!cloneData->responseInfo)
736 OIC_LOG(ERROR, TAG, "out of memory");
737 OICFree(responseData.token);
738 return CA_MEMORY_ALLOC_FAILED;
741 cloneData->responseInfo->info = responseData;
742 cloneData->responseInfo->info.type = sentMsgType;
743 cloneData->responseInfo->result = responseResult;
745 OIC_LOG(DEBUG, TAG, "set response message to send error code");
749 OIC_LOG(ERROR, TAG, "data has no sent-data");
750 return CA_MEMORY_ALLOC_FAILED;
753 // if there is a requestInfo, remove it to send response message
754 if (cloneData->requestInfo)
756 CADestroyRequestInfoInternal(cloneData->requestInfo);
757 cloneData->requestInfo = NULL;
760 // add data to send thread
761 if (g_context.sendThreadFunc)
763 oc_mutex_lock(g_context.blockDataSenderMutex);
764 g_context.sendThreadFunc(cloneData);
765 oc_mutex_unlock(g_context.blockDataSenderMutex);
769 CADestroyDataSet(cloneData);
772 // if error code is 4.08, remove the stored payload and initialize block number
773 if (CA_BLOCK_INCOMPLETE == status)
775 OICFree(data->payload);
776 data->payload = NULL;
777 data->payloadLength = 0;
778 data->receivedPayloadLen = 0;
779 data->block1.num = 0;
780 data->block2.num = 0;
786 CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID, const CAData_t *receivedData)
788 VERIFY_NON_NULL(blockID, TAG, "blockID");
789 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
791 // total block data have to notify to Application
792 CAData_t *cloneData = CACloneCAData(receivedData);
795 OIC_LOG(ERROR, TAG, "clone has failed");
796 return CA_MEMORY_ALLOC_FAILED;
800 size_t fullPayloadLen = 0;
801 CAPayload_t fullPayload = CAGetPayloadFromBlockDataList(blockID, &fullPayloadLen);
804 CAResult_t res = CAUpdatePayloadToCAData(cloneData, fullPayload, fullPayloadLen);
805 if (CA_STATUS_OK != res)
807 OIC_LOG(ERROR, TAG, "update has failed");
808 CADestroyDataSet(cloneData);
813 if (g_context.receivedThreadFunc)
815 g_context.receivedThreadFunc(cloneData);
819 CADestroyDataSet(cloneData);
825 static CABlockData_t* CACheckTheExistOfBlockData(const CABlockDataID_t* blockDataID,
826 coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
829 // Get BlockData data. If does not exist, create a new data
830 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
833 OIC_LOG(DEBUG, TAG, "block data doesn't exist in list. create new one");
835 CAData_t *cadata = CACreateNewDataSet(pdu, endpoint);
838 OIC_LOG(ERROR, TAG, "data is null");
842 data = CACreateNewBlockData(cadata);
845 OIC_LOG(ERROR, TAG, "failed to create a new block data");
846 CADestroyDataSet(cadata);
849 CADestroyDataSet(cadata);
852 // update BLOCK OPTION type
853 CAResult_t res = CAUpdateBlockOptionType(blockDataID, blockType);
854 if (CA_STATUS_OK != res)
856 OIC_LOG(ERROR, TAG, "update has failed");
863 // TODO make pdu const after libcoap is updated to support that.
864 CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
865 const CAData_t *receivedData, coap_block_t block,
868 OIC_LOG(INFO, TAG, "CASetNextBlockOption1");
869 VERIFY_NON_NULL(pdu, TAG, "pdu");
870 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
871 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
872 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
874 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
876 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
877 (CAToken_t)pdu->transport_hdr->udp.token,
878 pdu->transport_hdr->udp.token_length,
879 endpoint->addr, endpoint->port);
880 if (NULL == blockDataID || blockDataID->idLength < 1)
882 OIC_LOG(ERROR, TAG, "blockId is null");
883 CADestroyBlockID(blockDataID);
884 return CA_STATUS_FAILED;
887 CAResult_t res = CA_STATUS_OK;
888 CABlockData_t *data = CACheckTheExistOfBlockData(blockDataID, pdu, endpoint,
892 OIC_LOG(ERROR, TAG, "Failed to create or get block data");
893 res = CA_STATUS_FAILED;
897 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
898 uint32_t code = pdu->transport_hdr->udp.code;
899 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
901 // received message type is request
902 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
904 // check the size option
905 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu, COAP_OPTION_SIZE1,
906 &(data->payloadLength));
908 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
909 COAP_OPTION_BLOCK1, dataLen);
911 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
913 // store the received payload and merge
914 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
915 isSizeOption, COAP_OPTION_BLOCK1);
916 if (CA_STATUS_OK != res)
918 OIC_LOG(ERROR, TAG, "update has failed");
922 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1,
924 if (CA_STATUS_OK != res)
926 OIC_LOG(ERROR, TAG, "update has failed");
931 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
932 if (CA_STATUS_OK != res)
934 OIC_LOG(ERROR, TAG, "update has failed");
939 // check the blcok-wise transfer status for next step
940 if (CA_BLOCK_UNKNOWN == blockWiseStatus || CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
942 OIC_LOG_V(DEBUG, TAG, "M bit is %d", block.m);
944 blockWiseStatus = (0 == block.m) ?
945 CA_OPTION1_REQUEST_LAST_BLOCK : CA_OPTION1_REQUEST_BLOCK;
950 // received message type is response
951 uint32_t code = CA_RESPONSE_CODE(pdu->transport_hdr->udp.code);
952 if (0 == block.m && (CA_REQUEST_ENTITY_INCOMPLETE != code
953 && CA_REQUEST_ENTITY_TOO_LARGE != code))
955 int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
958 OIC_LOG(INFO, TAG, "received data is combining block1 and block2");
959 // initialize block number for response message
960 data->block1.num = 0;
961 CADestroyBlockID(blockDataID);
966 OIC_LOG(INFO, TAG, "received data is not bulk data");
967 CAReceiveLastBlock(blockDataID, receivedData);
973 blockWiseStatus = CA_OPTION1_RESPONSE;
974 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus);
975 if (CA_STATUS_OK != res)
977 OIC_LOG(ERROR, TAG, "update has failed");
978 CADestroyBlockID(blockDataID);
982 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
983 if (CA_STATUS_OK != res)
985 OIC_LOG(ERROR, TAG, "update has failed");
990 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
991 if (CA_STATUS_OK != res)
993 OIC_LOG(ERROR, TAG, "setting has failed");
997 CADestroyBlockID(blockDataID);
1001 CARemoveBlockDataFromList(blockDataID);
1002 CADestroyBlockID(blockDataID);
1006 // TODO make pdu const after libcoap is updated to support that.
1007 CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
1008 const CAData_t *receivedData, coap_block_t block,
1011 OIC_LOG(DEBUG, TAG, "CASetNextBlockOption2");
1012 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
1014 VERIFY_NON_NULL(pdu, TAG, "pdu");
1015 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
1016 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1017 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
1019 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
1020 (CAToken_t)pdu->transport_hdr->udp.token,
1021 pdu->transport_hdr->udp.token_length,
1022 endpoint->addr, endpoint->port);
1023 if (NULL == blockDataID || blockDataID->idLength < 1)
1025 OIC_LOG(ERROR, TAG, "blockId is null");
1026 CADestroyBlockID(blockDataID);
1027 return CA_STATUS_FAILED;
1030 CAResult_t res = CA_STATUS_OK;
1031 CABlockData_t *data = CACheckTheExistOfBlockData(blockDataID, pdu, endpoint,
1032 COAP_OPTION_BLOCK2);
1035 OIC_LOG(ERROR, TAG, "Failed to create or get block data");
1036 res = CA_STATUS_FAILED;
1040 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
1041 if (0 == block.num && CA_GET == pdu->transport_hdr->udp.code && 0 == block.m)
1043 OIC_LOG(INFO, TAG, "first block number");
1045 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1046 if (CA_STATUS_OK != res)
1048 OIC_LOG(ERROR, TAG, "update has failed");
1052 // first block data have to notify to Application
1053 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1054 if (CA_STATUS_OK != res)
1056 OIC_LOG(ERROR, TAG, "update has failed");
1059 blockWiseStatus = CA_OPTION2_FIRST_BLOCK;
1063 uint32_t code = pdu->transport_hdr->udp.code;
1064 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1066 // received message type is request
1067 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
1069 blockWiseStatus = CA_OPTION2_REQUEST;
1071 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1072 if (CA_STATUS_OK != res)
1074 OIC_LOG(ERROR, TAG, "update has failed");
1078 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1079 if (CA_STATUS_OK != res)
1081 OIC_LOG(ERROR, TAG, "update has failed");
1087 // received message type is response
1088 OIC_LOG(DEBUG, TAG, "received response message with block option2");
1090 // check the size option
1091 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
1093 &(data->payloadLength));
1095 uint32_t code = CA_RESPONSE_CODE(pdu->transport_hdr->udp.code);
1096 if (CA_REQUEST_ENTITY_INCOMPLETE != code && CA_REQUEST_ENTITY_TOO_LARGE != code)
1098 // check if received payload is exact
1099 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
1100 COAP_OPTION_BLOCK2, dataLen);
1103 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
1105 // store the received payload and merge
1106 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
1107 isSizeOption, COAP_OPTION_BLOCK2);
1108 if (CA_STATUS_OK != res)
1110 OIC_LOG(ERROR, TAG, "update has failed");
1115 if (0 == block.m && CA_BLOCK_UNKNOWN == blockWiseStatus) // Last block is received
1117 OIC_LOG(DEBUG, TAG, "M bit is 0");
1118 blockWiseStatus = CA_OPTION2_LAST_BLOCK;
1122 if (CA_BLOCK_UNKNOWN == blockWiseStatus ||
1123 CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
1125 OIC_LOG(DEBUG, TAG, "M bit is 1");
1126 blockWiseStatus = CA_OPTION2_RESPONSE;
1129 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2,
1131 if (CA_STATUS_OK != res)
1133 OIC_LOG(ERROR, TAG, "update has failed");
1137 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1138 if (CA_STATUS_OK != res)
1140 OIC_LOG(ERROR, TAG, "update has failed");
1147 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
1148 if (CA_STATUS_OK != res)
1150 OIC_LOG(ERROR, TAG, "setting has failed");
1154 CADestroyBlockID(blockDataID);
1155 return CA_STATUS_OK;
1158 CARemoveBlockDataFromList(blockDataID);
1159 CADestroyBlockID(blockDataID);
1163 CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu,
1164 coap_block_t *block, uint16_t blockType,
1167 VERIFY_NON_NULL(currData, TAG, "currData");
1168 VERIFY_NON_NULL(pdu, TAG, "pdu");
1169 VERIFY_NON_NULL(block, TAG, "block");
1171 // update block data
1172 CAResult_t res = CA_STATUS_OK;
1173 uint32_t code = CA_RESPONSE_CODE(pdu->transport_hdr->udp.code);
1175 if (CA_REQUEST_ENTITY_INCOMPLETE == code || CA_REQUEST_ENTITY_TOO_LARGE == code)
1177 // response error code of the received block message
1178 res = CAHandleBlockErrorResponse(block, blockType, code);
1179 if (CA_STATUS_OK != res)
1181 OIC_LOG(ERROR, TAG, "error handle has failed");
1187 // update block option items
1190 case CA_OPTION1_RESPONSE:
1191 if (currData->block1.num > block->num)
1193 OIC_LOG(ERROR, TAG, "received incorrect block num");
1194 return CA_STATUS_FAILED;
1198 case CA_OPTION2_REQUEST:
1201 case CA_OPTION2_RESPONSE:
1202 if (currData->block2.num > block->num)
1204 OIC_LOG(ERROR, TAG, "received incorrect block num");
1205 return CA_STATUS_FAILED;
1210 case CA_BLOCK_TOO_LARGE:
1211 // if state of received block message is CA_BLOCK_TOO_LARGE or CA_BLOCK_INCOMPLETE
1212 // we set the response error code appropriately and send
1213 if (COAP_OPTION_BLOCK2 == blockType)
1217 block->szx = currData->block2.szx;
1221 block->szx = currData->block1.szx;
1225 OIC_LOG_V(ERROR, TAG, "no logic [%d]", status);
1228 if (CA_BLOCK_INCOMPLETE != status && CA_BLOCK_TOO_LARGE != status)
1230 // negotiate block size
1231 res = CANegotiateBlockSize(currData, block, pdu, blockType);
1232 if (CA_STATUS_OK != res)
1234 OIC_LOG(ERROR, TAG, "negotiation has failed");
1242 CAResult_t CASetMoreBitFromBlock(size_t payloadLen, coap_block_t *block)
1244 VERIFY_NON_NULL(block, TAG, "block");
1246 if ((size_t) ((block->num + 1) << (block->szx + BLOCK_NUMBER_IDX)) < payloadLen)
1248 OIC_LOG(DEBUG, TAG, "Set the M-bit(1)");
1253 OIC_LOG(DEBUG, TAG, "Set the M-bit(0)");
1257 return CA_STATUS_OK;
1260 CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block,
1261 const coap_pdu_t *pdu, uint16_t blockType)
1263 OIC_LOG(DEBUG, TAG, "IN-NegotiateBlockSize");
1265 VERIFY_NON_NULL(currData, TAG, "currData");
1266 VERIFY_NON_NULL(block, TAG, "block");
1267 VERIFY_NON_NULL(pdu, TAG, "pdu");
1268 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
1270 bool isReqMsg = false;
1271 uint32_t code = pdu->transport_hdr->udp.code;
1272 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1277 // #1. check the block option type
1278 if (COAP_OPTION_BLOCK2 == blockType)
1280 // #2. check the message type
1283 if (block->szx > currData->block2.szx)
1285 OIC_LOG(DEBUG, TAG, "sze is big");
1287 // #3. calculate new block number from block size
1288 unsigned int blockNum = BLOCK_SIZE(block->szx) /
1289 BLOCK_SIZE(currData->block2.szx) - 1;
1290 OIC_LOG(DEBUG, TAG, "num is set as Negotiation");
1291 block->num += blockNum;
1292 block->szx = currData->block2.szx;
1293 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1298 if (block->szx > currData->block2.szx)
1300 OIC_LOG(DEBUG, TAG, "sze is big");
1301 block->szx = currData->block2.szx;
1305 else if (COAP_OPTION_BLOCK1 == blockType)
1309 if (block->szx < currData->block1.szx)
1311 OIC_LOG(DEBUG, TAG, "sze is small");
1313 unsigned int blockNum = BLOCK_SIZE(currData->block1.szx) /
1314 BLOCK_SIZE(block->szx) - 1;
1315 block->num += blockNum;
1316 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1321 if (block->szx > currData->block1.szx)
1323 OIC_LOG(DEBUG, TAG, "sze is big");
1324 block->szx = currData->block1.szx;
1330 OIC_LOG(DEBUG, TAG, "Invalid block option");
1331 return CA_STATUS_FAILED;
1334 OIC_LOG(DEBUG, TAG, "OUT-NegotiateBlockSize");
1336 return CA_STATUS_OK;
1339 CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block,
1342 VERIFY_NON_NULL(currData, TAG, "currData");
1344 // check if block size is bigger than CABlockSize_t
1345 if (block.szx > CA_BLOCK_SIZE_1024_BYTE)
1347 OIC_LOG(DEBUG, TAG, "invalid block szx");
1348 return CA_STATUS_FAILED;
1351 // update block option
1352 if (COAP_OPTION_BLOCK2 == blockType)
1354 currData->block2 = block;
1358 currData->block1 = block;
1361 OIC_LOG(DEBUG, TAG, "data has updated");
1362 return CA_STATUS_OK;
1365 CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID)
1367 VERIFY_NON_NULL(pdu, TAG, "pdu");
1368 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
1369 VERIFY_NON_NULL(blockID, TAG, "blockID");
1371 // if message is sent, update messageId in block-wise transfer list
1372 CAData_t * cadata = CAGetDataSetFromBlockDataList(blockID);
1375 OIC_LOG(ERROR, TAG, "CAData is unavailable");
1376 return CA_STATUS_FAILED;
1379 if (cadata->requestInfo)
1381 cadata->requestInfo->info.messageId = pdu->transport_hdr->udp.id;
1384 return CA_STATUS_OK;
1387 CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info,
1388 const CAEndpoint_t *endpoint, coap_list_t **options)
1390 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption");
1391 VERIFY_NON_NULL(pdu, TAG, "pdu");
1392 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1393 VERIFY_NON_NULL((*pdu)->transport_hdr, TAG, "(*pdu)->transport_hdr");
1394 VERIFY_NON_NULL(info, TAG, "info");
1395 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1396 VERIFY_NON_NULL(options, TAG, "options");
1398 CAResult_t res = CA_STATUS_OK;
1399 size_t dataLength = 0;
1402 dataLength = info->payloadSize;
1403 OIC_LOG_V(DEBUG, TAG, "dataLength - %zu", dataLength);
1406 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
1407 (CAToken_t)(*pdu)->transport_hdr->udp.token,
1408 (*pdu)->transport_hdr->udp.token_length,
1409 endpoint->addr, endpoint->port);
1410 if (NULL == blockDataID || blockDataID->idLength < 1)
1412 OIC_LOG(ERROR, TAG, "blockId is null");
1413 res = CA_STATUS_FAILED;
1417 uint32_t repCode = CA_RESPONSE_CODE((*pdu)->transport_hdr->udp.code);
1418 if (CA_REQUEST_ENTITY_INCOMPLETE == repCode)
1420 OIC_LOG(INFO, TAG, "don't use option");
1425 uint8_t blockType = CAGetBlockOptionType(blockDataID);
1426 if (COAP_OPTION_BLOCK2 == blockType)
1428 res = CAAddBlockOption2(pdu, info, dataLength, blockDataID, options);
1429 if (CA_STATUS_OK != res)
1431 OIC_LOG(ERROR, TAG, "add has failed");
1435 else if (COAP_OPTION_BLOCK1 == blockType)
1437 res = CAAddBlockOption1(pdu, info, dataLength, blockDataID, options);
1438 if (CA_STATUS_OK != res)
1440 OIC_LOG(ERROR, TAG, "add has failed");
1446 OIC_LOG(DEBUG, TAG, "no BLOCK option");
1448 // in case it is not large data, add option list to pdu.
1451 for (coap_list_t *opt = *options; opt; opt = opt->next)
1453 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
1454 COAP_OPTION_DATA(*(coap_option *) opt->data));
1456 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", (*pdu)->length);
1457 coap_add_option(*pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
1458 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
1459 COAP_OPTION_DATA(*(coap_option *) opt->data));
1463 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", (*pdu)->length);
1465 // if response data is so large. it have to send as block transfer
1466 if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload))
1468 OIC_LOG(INFO, TAG, "it have to use block");
1469 res = CA_STATUS_FAILED;
1474 OIC_LOG(INFO, TAG, "not Blockwise Transfer");
1478 uint32_t code = (*pdu)->transport_hdr->udp.code;
1479 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1481 // if received message type is RESET from remote device,
1482 // we have to use the updated message id of request message to find token.
1483 CABlockData_t *blockData = CAGetBlockDataFromBlockDataList(blockDataID);
1486 res = CAUpdateMessageId(*pdu, blockDataID);
1487 if (CA_STATUS_OK != res)
1489 OIC_LOG(ERROR, TAG, "fail to update message id");
1496 CADestroyBlockID(blockDataID);
1497 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption");
1501 CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
1502 const CABlockDataID_t *blockID, coap_list_t **options)
1504 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption2");
1505 VERIFY_NON_NULL(pdu, TAG, "pdu");
1506 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1507 VERIFY_NON_NULL((*pdu)->transport_hdr, TAG, "(*pdu)->transport_hdr");
1508 VERIFY_NON_NULL(info, TAG, "info");
1509 VERIFY_NON_NULL(blockID, TAG, "blockID");
1510 VERIFY_NON_NULL(options, TAG, "options");
1512 // get set block data from CABlock list-set.
1513 coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1514 coap_block_t *block2 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK2);
1515 if (!block1 || !block2)
1517 OIC_LOG(ERROR, TAG, "getting has failed");
1518 return CA_STATUS_FAILED;
1521 CAResult_t res = CA_STATUS_OK;
1522 uint32_t code = (*pdu)->transport_hdr->udp.code;
1523 if (CA_GET != code && CA_POST != code && CA_PUT != code && CA_DELETE != code)
1525 CASetMoreBitFromBlock(dataLength, block2);
1527 // if block number is 0, add size2 option
1528 if (0 == block2->num)
1530 res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE2, dataLength, options);
1531 if (CA_STATUS_OK != res)
1533 OIC_LOG(ERROR, TAG, "add has failed");
1538 res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options);
1539 if (CA_STATUS_OK != res)
1541 OIC_LOG(ERROR, TAG, "add has failed");
1547 OIC_LOG(DEBUG, TAG, "combining block1 and block2");
1548 res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1549 if (CA_STATUS_OK != res)
1551 OIC_LOG(ERROR, TAG, "add has failed");
1554 // initialize block number
1558 res = CAAddOptionToPDU(*pdu, options);
1559 if (CA_STATUS_OK != res)
1561 OIC_LOG(ERROR, TAG, "add has failed");
1565 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload,
1566 block2->num, block2->szx))
1568 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1569 return CA_STATUS_FAILED;
1572 CALogBlockInfo(block2);
1576 // if sent message is last response block message, remove data
1577 CARemoveBlockDataFromList(blockID);
1582 OIC_LOG(DEBUG, TAG, "option2, not response msg");
1583 res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options);
1584 if (CA_STATUS_OK != res)
1586 OIC_LOG(ERROR, TAG, "add has failed");
1590 res = CAAddOptionToPDU(*pdu, options);
1591 if (CA_STATUS_OK != res)
1593 OIC_LOG(ERROR, TAG, "add has failed");
1596 CALogBlockInfo(block2);
1599 return CA_STATUS_OK;
1602 CARemoveBlockDataFromList(blockID);
1606 CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
1607 const CABlockDataID_t *blockID, coap_list_t **options)
1609 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption1");
1610 VERIFY_NON_NULL(pdu, TAG, "pdu");
1611 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1612 VERIFY_NON_NULL((*pdu)->transport_hdr, TAG, "(*pdu)->transport_hdr");
1613 VERIFY_NON_NULL(info, TAG, "info");
1614 VERIFY_NON_NULL(blockID, TAG, "blockID");
1615 VERIFY_NON_NULL(options, TAG, "options");
1617 // get set block data from CABlock list-set.
1618 coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1621 OIC_LOG(ERROR, TAG, "getting has failed");
1622 return CA_STATUS_FAILED;
1625 CAResult_t res = CA_STATUS_OK;
1626 uint32_t code = (*pdu)->transport_hdr->udp.code;
1627 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1629 CASetMoreBitFromBlock(dataLength, block1);
1631 // if block number is 0, add size1 option
1632 if (0 == block1->num)
1634 res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE1, dataLength, options);
1635 if (CA_STATUS_OK != res)
1637 OIC_LOG(ERROR, TAG, "add has failed");
1642 // add block option to option list.
1643 res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1644 if (CA_STATUS_OK != res)
1646 OIC_LOG(ERROR, TAG, "add has failed");
1650 // add option list to pdu.
1651 res = CAAddOptionToPDU(*pdu, options);
1652 if (CA_STATUS_OK != res)
1654 OIC_LOG(ERROR, TAG, "add has failed");
1658 // add the payload data as the block size.
1659 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload, block1->num,
1662 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1663 return CA_STATUS_FAILED;
1668 OIC_LOG(DEBUG, TAG, "received response message with block option1");
1670 // add block option to option list.
1671 res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1672 if (CA_STATUS_OK != res)
1674 OIC_LOG(ERROR, TAG, "add has failed");
1678 // add option list to pdu.
1679 res = CAAddOptionToPDU(*pdu, options);
1680 if (CA_STATUS_OK != res)
1682 OIC_LOG(ERROR, TAG, "add has failed");
1686 // add the payload data as the block size.
1687 if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload))
1689 OIC_LOG(ERROR, TAG, "failed to add payload");
1690 return CA_STATUS_FAILED;
1693 // if it is last block message, remove block data from list.
1696 // remove data from list
1697 res = CARemoveBlockDataFromList(blockID);
1698 if (CA_STATUS_OK != res)
1700 OIC_LOG(ERROR, TAG, "remove has failed");
1706 CALogBlockInfo(block1);
1708 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption1");
1710 return CA_STATUS_OK;
1713 CARemoveBlockDataFromList(blockID);
1717 CAResult_t CAAddBlockOptionImpl(coap_block_t *block, uint8_t blockType,
1718 coap_list_t **options)
1720 OIC_LOG(DEBUG, TAG, "IN-AddBlockOptionImpl");
1721 VERIFY_NON_NULL(block, TAG, "block");
1722 VERIFY_NON_NULL(options, TAG, "options");
1724 unsigned char buf[BLOCKWISE_OPTION_BUFFER] = { 0 };
1725 unsigned int optionLength = coap_encode_var_bytes(buf,
1726 ((block->num << BLOCK_NUMBER_IDX)
1727 | (block->m << BLOCK_M_BIT_IDX)
1730 int ret = coap_insert(options,
1731 CACreateNewOptionNode(blockType, optionLength, (char *) buf),
1735 return CA_STATUS_INVALID_PARAM;
1738 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOptionImpl");
1739 return CA_STATUS_OK;
1742 CAResult_t CAAddOptionToPDU(coap_pdu_t *pdu, coap_list_t **options)
1744 // after adding the block option to option list, add option list to pdu.
1747 for (coap_list_t *opt = *options; opt; opt = opt->next)
1749 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
1750 COAP_OPTION_DATA(*(coap_option *) opt->data));
1752 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
1753 int ret = coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
1754 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
1755 COAP_OPTION_DATA(*(coap_option *) opt->data));
1758 return CA_STATUS_FAILED;
1763 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
1765 return CA_STATUS_OK;
1768 CAResult_t CAAddBlockSizeOption(coap_pdu_t *pdu, uint16_t sizeType, size_t dataLength,
1769 coap_list_t **options)
1771 OIC_LOG(DEBUG, TAG, "IN-CAAddBlockSizeOption");
1772 VERIFY_NON_NULL(pdu, TAG, "pdu");
1773 VERIFY_NON_NULL(options, TAG, "options");
1775 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1777 OIC_LOG(ERROR, TAG, "unknown option type");
1778 return CA_STATUS_FAILED;
1781 unsigned char value[BLOCKWISE_OPTION_BUFFER] = { 0 };
1782 unsigned int optionLength = coap_encode_var_bytes(value, dataLength);
1784 int ret = coap_insert(options,
1785 CACreateNewOptionNode(sizeType, optionLength, (char *) value),
1789 return CA_STATUS_INVALID_PARAM;
1792 OIC_LOG(DEBUG, TAG, "OUT-CAAddBlockSizeOption");
1794 return CA_STATUS_OK;
1797 // TODO make pdu const after libcoap is updated to support that.
1798 bool CAIsPayloadLengthInPduWithBlockSizeOption(coap_pdu_t *pdu,
1800 size_t *totalPayloadLen)
1802 OIC_LOG(DEBUG, TAG, "IN-CAIsPayloadLengthInPduWithBlockSizeOption");
1803 VERIFY_NON_NULL(pdu, TAG, "pdu");
1804 VERIFY_NON_NULL(totalPayloadLen, TAG, "totalPayloadLen");
1806 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1808 OIC_LOG(ERROR, TAG, "unknown option type");
1809 return CA_STATUS_FAILED;
1812 coap_opt_iterator_t opt_iter;
1813 coap_opt_t *option = coap_check_option(pdu, sizeType, &opt_iter);
1816 OIC_LOG(DEBUG, TAG, "get size option from pdu");
1817 *totalPayloadLen = coap_decode_var_bytes(COAP_OPT_VALUE(option),
1818 COAP_OPT_LENGTH(option));
1820 OIC_LOG_V(DEBUG, TAG, "the total payload length to be received is [%zu]bytes",
1826 OIC_LOG(DEBUG, TAG, "OUT-CAIsPayloadLengthInPduWithBlockSizeOption");
1831 uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlock,
1832 const CAData_t *receivedData, uint16_t blockType,
1835 OIC_LOG(DEBUG, TAG, "IN-CheckBlockError");
1837 VERIFY_NON_NULL(currData, TAG, "currData is NULL");
1838 VERIFY_NON_NULL(receivedBlock, TAG, "receivedBlock is NULL");
1839 VERIFY_NON_NULL(receivedData, TAG, "receivedData is NULL");
1841 // #1. check the received payload length
1842 size_t blockPayloadLen = 0;
1843 CAGetPayloadInfo(receivedData, &blockPayloadLen);
1845 // #2. check if the block sequence is right
1846 if (COAP_OPTION_BLOCK1 == blockType)
1848 size_t prePayloadLen = currData->receivedPayloadLen;
1849 if (prePayloadLen != (size_t) BLOCK_SIZE(receivedBlock->szx) * receivedBlock->num)
1851 if (receivedBlock->num > currData->block1.num + 1)
1853 // 408 Error handling of block loss
1854 OIC_LOG(ERROR, TAG, "option1: error 4.08");
1855 OIC_LOG(ERROR, TAG, "it didn't order");
1856 return CA_BLOCK_INCOMPLETE;
1858 return CA_BLOCK_RECEIVED_ALREADY;
1861 else if (COAP_OPTION_BLOCK2 == blockType)
1863 if (receivedBlock->num != currData->block2.num)
1865 if (receivedBlock->num > currData->block2.num)
1867 // 408 Error handling of block loss
1868 OIC_LOG(ERROR, TAG, "option2: error 4.08");
1869 OIC_LOG(ERROR, TAG, "it didn't order");
1870 return CA_BLOCK_INCOMPLETE;
1874 OIC_LOG(ERROR, TAG, "already received this block");
1875 return CA_BLOCK_RECEIVED_ALREADY;
1880 // #3. check if error check logic is required
1881 size_t optionLen = dataLen - blockPayloadLen;
1882 if (receivedBlock->m && blockPayloadLen != (size_t) BLOCK_SIZE(receivedBlock->szx))
1884 // 413 Error handling of too large entity
1885 if (COAP_MAX_PDU_SIZE < BLOCK_SIZE(receivedBlock->szx) + optionLen)
1887 // buffer size is smaller than received block size
1888 OIC_LOG(ERROR, TAG, "error type 4.13");
1889 OIC_LOG(ERROR, TAG, "too large size");
1891 // set the block size to be smaller than COAP_MAX_PDU_SIZE
1892 for (int32_t size = CA_DEFAULT_BLOCK_SIZE; size >= 0; size--)
1894 if (COAP_MAX_PDU_SIZE >= BLOCK_SIZE(size) + optionLen)
1896 OIC_LOG_V(ERROR, TAG, "replace sze with %d", size);
1897 currData->block2.szx = size;
1898 currData->block1.szx = size;
1902 return CA_BLOCK_TOO_LARGE;
1906 // 408 Error handling of payload loss
1907 OIC_LOG(ERROR, TAG, "error type 4.08");
1908 OIC_LOG(ERROR, TAG, "payload len != block sze");
1909 return CA_BLOCK_INCOMPLETE;
1912 else if (0 == receivedBlock->m && 0 != currData->payloadLength)
1914 // if the received block is last block, check the total payload length
1915 size_t receivedPayloadLen = currData->receivedPayloadLen;
1916 receivedPayloadLen += blockPayloadLen;
1918 if (receivedPayloadLen != currData->payloadLength)
1920 OIC_LOG(ERROR, TAG, "error type 4.08");
1921 OIC_LOG(ERROR, TAG, "total payload length is wrong");
1922 return CA_BLOCK_INCOMPLETE;
1926 OIC_LOG(DEBUG, TAG, "received all data normally");
1928 OIC_LOG(DEBUG, TAG, "OUT-CheckBlockError");
1930 return CA_BLOCK_UNKNOWN;
1933 CAResult_t CAUpdatePayloadData(CABlockData_t *currData, const CAData_t *receivedData,
1934 uint8_t status, bool isSizeOption, uint16_t blockType)
1936 OIC_LOG(DEBUG, TAG, "IN-UpdatePayloadData");
1938 VERIFY_NON_NULL(currData, TAG, "currData");
1939 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
1941 // if error code is 4.08, do not update payload
1942 if (CA_BLOCK_INCOMPLETE == status)
1944 OIC_LOG(ERROR, TAG, "no require to update");
1945 return CA_STATUS_OK;
1948 size_t blockPayloadLen = 0;
1949 CAPayload_t blockPayload = CAGetPayloadInfo(receivedData, &blockPayloadLen);
1951 if (CA_BLOCK_TOO_LARGE == status)
1953 blockPayloadLen = (COAP_OPTION_BLOCK2 == blockType) ?
1954 BLOCK_SIZE(currData->block2.szx) : BLOCK_SIZE(currData->block1.szx);
1957 // memory allocation for the received block payload
1958 size_t prePayloadLen = currData->receivedPayloadLen;
1961 if (currData->payloadLength)
1963 // in case the block message has the size option
1964 // allocate the memory for the total payload
1967 CAPayload_t prePayload = currData->payload;
1969 OIC_LOG(DEBUG, TAG, "allocate memory for the total payload");
1970 currData->payload = (CAPayload_t) OICCalloc(1, currData->payloadLength);
1971 if (NULL == currData->payload)
1973 OIC_LOG(ERROR, TAG, "out of memory");
1974 return CA_MEMORY_ALLOC_FAILED;
1976 memcpy(currData->payload, prePayload, prePayloadLen);
1977 OICFree(prePayload);
1980 // update the total payload
1981 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
1985 OIC_LOG(DEBUG, TAG, "allocate memory for the received block payload");
1987 size_t totalPayloadLen = prePayloadLen + blockPayloadLen;
1988 CAPayload_t newPayload = OICRealloc(currData->payload, totalPayloadLen);
1989 if (NULL == newPayload)
1991 OIC_LOG(ERROR, TAG, "out of memory");
1992 return CA_MEMORY_ALLOC_FAILED;
1995 // update the total payload
1996 memset(newPayload + prePayloadLen, 0, blockPayloadLen);
1997 currData->payload = newPayload;
1998 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
2001 // update received payload length
2002 currData->receivedPayloadLen += blockPayloadLen;
2004 OIC_LOG_V(DEBUG, TAG, "updated payload: %s, len: %zu", currData->payload,
2005 currData->receivedPayloadLen);
2008 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayloadData");
2009 return CA_STATUS_OK;
2012 CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
2014 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", NULL);
2015 VERIFY_NON_NULL_RET(pdu->transport_hdr, TAG, "pdu->transport_hdr", NULL);
2016 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
2018 CARequestInfo_t* requestInfo = NULL;
2019 CAResponseInfo_t* responseInfo = NULL;
2021 uint32_t code = pdu->transport_hdr->udp.code;
2022 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
2024 CAInfo_t responseData = { .tokenLength = pdu->transport_hdr->udp.token_length };
2025 responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength);
2026 if (!responseData.token)
2028 OIC_LOG(ERROR, TAG, "out of memory");
2031 memcpy(responseData.token, pdu->transport_hdr->udp.token, responseData.tokenLength);
2033 responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
2036 OIC_LOG(ERROR, TAG, "out of memory");
2037 OICFree(responseData.token);
2040 responseInfo->info = responseData;
2044 CAInfo_t requestData = { .tokenLength = pdu->transport_hdr->udp.token_length };
2045 requestData.token = (CAToken_t) OICMalloc(requestData.tokenLength);
2046 if (!requestData.token)
2048 OIC_LOG(ERROR, TAG, "out of memory");
2051 memcpy(requestData.token, pdu->transport_hdr->udp.token, requestData.tokenLength);
2053 requestInfo = (CARequestInfo_t*) OICCalloc(1, sizeof(CARequestInfo_t));
2056 OIC_LOG(ERROR, TAG, "out of memory");
2057 OICFree(requestData.token);
2060 requestInfo->info = requestData;
2062 // get resource uri information from received response message
2063 // to send next request message to remote device
2064 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(*resInfo));
2067 OIC_LOG(ERROR, TAG, "memory allocation failed");
2068 OICFree(requestData.token);
2072 CAGetResponseInfoFromPDU(pdu, resInfo, endpoint);
2073 requestInfo->method = CA_GET;
2074 requestInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu->transport_hdr, pdu->length);
2075 requestInfo->info.resourceUri = OICStrdup(resInfo->info.resourceUri);
2077 // after copying the resource uri, destroy response info.
2078 CADestroyResponseInfoInternal(resInfo);
2081 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2084 OIC_LOG(ERROR, TAG, "out of memory");
2085 OICFree(requestInfo);
2086 OICFree(responseInfo);
2090 data->requestInfo = requestInfo;
2091 data->responseInfo = responseInfo;
2092 data->remoteEndpoint = CACloneEndpoint(endpoint);
2093 data->type = SEND_TYPE_UNICAST;
2098 CAData_t *CACloneCAData(const CAData_t *data)
2100 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2102 CAData_t *clone = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2105 OIC_LOG(ERROR, TAG, "out of memory");
2110 if (data->requestInfo)
2112 clone->requestInfo = CACloneRequestInfo(data->requestInfo);
2114 else if (data->responseInfo)
2116 clone->responseInfo = CACloneResponseInfo(data->responseInfo);
2119 if (data->remoteEndpoint)
2121 clone->remoteEndpoint = CACloneEndpoint(data->remoteEndpoint);
2127 CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
2130 OIC_LOG(DEBUG, TAG, "IN-UpdatePayload");
2132 VERIFY_NON_NULL(data, TAG, "data is NULL");
2133 VERIFY_NON_NULL(payload, TAG, "payload is NULL");
2135 CAPayload_t newPayload = NULL;
2136 switch (data->dataType)
2138 case CA_REQUEST_DATA:
2139 if (!data->requestInfo)
2141 OIC_LOG(ERROR, TAG, "request info is null");
2142 return CA_STATUS_FAILED;
2144 // allocate payload field
2145 newPayload = OICRealloc(data->requestInfo->info.payload, payloadLen);
2148 OIC_LOG(ERROR, TAG, "out of memory");
2149 return CA_STATUS_FAILED;
2151 data->requestInfo->info.payload = newPayload;
2152 memcpy(data->requestInfo->info.payload, payload, payloadLen);
2153 data->requestInfo->info.payloadSize = payloadLen;
2156 case CA_RESPONSE_DATA:
2157 if (!data->responseInfo)
2159 OIC_LOG(ERROR, TAG, "response info is null");
2160 return CA_STATUS_FAILED;
2162 // allocate payload field
2163 newPayload = OICRealloc(data->responseInfo->info.payload, payloadLen);
2166 OIC_LOG(ERROR, TAG, "out of memory");
2167 return CA_STATUS_FAILED;
2169 data->responseInfo->info.payload = newPayload;
2170 memcpy(data->responseInfo->info.payload, payload, payloadLen);
2171 data->responseInfo->info.payloadSize = payloadLen;
2175 // does not occur case
2176 OIC_LOG(ERROR, TAG, "not supported data type");
2177 return CA_NOT_SUPPORTED;
2180 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayload");
2182 return CA_STATUS_OK;
2185 CAPayload_t CAGetPayloadInfo(const CAData_t *data, size_t *payloadLen)
2187 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2188 VERIFY_NON_NULL_RET(payloadLen, TAG, "payloadLen", NULL);
2190 if (data->requestInfo)
2192 if (data->requestInfo->info.payload)
2194 *payloadLen = data->requestInfo->info.payloadSize;
2195 return data->requestInfo->info.payload;
2200 if (data->responseInfo->info.payload)
2202 *payloadLen = data->responseInfo->info.payloadSize;
2203 return data->responseInfo->info.payload;
2210 CAResult_t CAHandleBlockErrorResponse(coap_block_t *block, uint16_t blockType,
2211 uint32_t responseResult)
2213 OIC_LOG(DEBUG, TAG, "IN-HandleBlockErrorRes");
2214 VERIFY_NON_NULL(block, TAG, "block is NULL");
2216 // update block data
2217 switch (responseResult)
2219 case CA_REQUEST_ENTITY_INCOMPLETE:
2222 case CA_REQUEST_ENTITY_TOO_LARGE:
2223 if (COAP_OPTION_BLOCK1 == blockType)
2230 OIC_LOG_V(ERROR, TAG, "there is no Error Code of BWT[%d]", responseResult);
2231 return CA_STATUS_FAILED;
2234 OIC_LOG(DEBUG, TAG, "OUT-HandleBlockErrorRes");
2235 return CA_STATUS_OK;
2238 CAResult_t CAUpdateBlockOptionType(const CABlockDataID_t *blockID, uint8_t blockType)
2240 OIC_LOG(DEBUG, TAG, "IN-UpdateBlockOptionType");
2241 VERIFY_NON_NULL(blockID, TAG, "blockID");
2243 oc_mutex_lock(g_context.blockDataListMutex);
2245 size_t len = u_arraylist_length(g_context.dataList);
2246 for (size_t i = 0; i < len; i++)
2248 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2249 if (CABlockidMatches(currData, blockID))
2251 currData->type = blockType;
2252 oc_mutex_unlock(g_context.blockDataListMutex);
2253 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2254 return CA_STATUS_OK;
2257 oc_mutex_unlock(g_context.blockDataListMutex);
2259 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2260 return CA_STATUS_FAILED;
2263 uint8_t CAGetBlockOptionType(const CABlockDataID_t *blockID)
2265 OIC_LOG(DEBUG, TAG, "IN-GetBlockOptionType");
2266 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", 0);
2268 oc_mutex_lock(g_context.blockDataListMutex);
2270 size_t len = u_arraylist_length(g_context.dataList);
2271 for (size_t i = 0; i < len; i++)
2273 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2274 if (CABlockidMatches(currData, blockID))
2276 oc_mutex_unlock(g_context.blockDataListMutex);
2277 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2278 return currData->type;
2281 oc_mutex_unlock(g_context.blockDataListMutex);
2283 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2287 CAData_t *CAGetDataSetFromBlockDataList(const CABlockDataID_t *blockID)
2289 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2291 oc_mutex_lock(g_context.blockDataListMutex);
2293 size_t len = u_arraylist_length(g_context.dataList);
2294 for (size_t i = 0; i < len; i++)
2296 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2297 if (CABlockidMatches(currData, blockID))
2299 oc_mutex_unlock(g_context.blockDataListMutex);
2300 return currData->sentData;
2303 oc_mutex_unlock(g_context.blockDataListMutex);
2308 CAResult_t CAGetTokenFromBlockDataList(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
2309 CAResponseInfo_t *responseInfo)
2311 OIC_LOG(DEBUG, TAG, "IN-CAGetTokenFromBlockDataList");
2312 VERIFY_NON_NULL(pdu, TAG, "pdu");
2313 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
2314 VERIFY_NON_NULL(responseInfo, TAG, "responseInfo");
2316 oc_mutex_lock(g_context.blockDataListMutex);
2318 size_t len = u_arraylist_length(g_context.dataList);
2319 for (size_t i = 0; i < len; i++)
2321 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2322 if (NULL == currData)
2327 if (NULL != currData->sentData && NULL != currData->sentData->requestInfo)
2329 if (pdu->transport_hdr->udp.id == currData->sentData->requestInfo->info.messageId &&
2330 endpoint->adapter == currData->sentData->remoteEndpoint->adapter)
2332 if (NULL != currData->sentData->requestInfo->info.token)
2334 uint8_t length = currData->sentData->requestInfo->info.tokenLength;
2335 responseInfo->info.tokenLength = length;
2336 responseInfo->info.token = (char *) OICMalloc(length);
2337 if (NULL == responseInfo->info.token)
2339 OIC_LOG(ERROR, TAG, "out of memory");
2340 oc_mutex_unlock(g_context.blockDataListMutex);
2341 return CA_MEMORY_ALLOC_FAILED;
2343 memcpy(responseInfo->info.token, currData->sentData->requestInfo->info.token,
2344 responseInfo->info.tokenLength);
2346 oc_mutex_unlock(g_context.blockDataListMutex);
2347 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2348 return CA_STATUS_OK;
2354 oc_mutex_unlock(g_context.blockDataListMutex);
2356 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2357 return CA_STATUS_FAILED;
2360 CAResult_t CACheckBlockDataValidation(const CAData_t *sendData, CABlockData_t **blockData)
2362 VERIFY_NON_NULL(sendData, TAG, "sendData");
2363 VERIFY_NON_NULL(blockData, TAG, "blockData");
2365 if (sendData->responseInfo)
2367 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2368 (CAToken_t)sendData->responseInfo->info.token,
2369 sendData->responseInfo->info.tokenLength,
2370 sendData->remoteEndpoint->addr, sendData->remoteEndpoint->port);
2371 if (NULL == blockDataID || blockDataID->idLength < 1)
2373 OIC_LOG(ERROR, TAG, "blockId is null");
2374 CADestroyBlockID(blockDataID);
2375 return CA_STATUS_FAILED;
2378 CABlockData_t *storedData = CAGetBlockDataFromBlockDataList(blockDataID);
2381 OIC_LOG(DEBUG, TAG, "Send response about the received block request.");
2382 if (storedData->sentData)
2384 OIC_LOG(DEBUG, TAG, "init block number");
2385 CADestroyDataSet(storedData->sentData);
2387 storedData->sentData = CACloneCAData(sendData);
2388 *blockData = storedData;
2389 CADestroyBlockID(blockDataID);
2390 return CA_STATUS_OK;
2392 CADestroyBlockID(blockDataID);
2395 return CA_STATUS_FAILED;
2398 CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID)
2400 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2402 oc_mutex_lock(g_context.blockDataListMutex);
2404 size_t len = u_arraylist_length(g_context.dataList);
2405 for (size_t i = 0; i < len; i++)
2407 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2408 if (CABlockidMatches(currData, blockID))
2410 oc_mutex_unlock(g_context.blockDataListMutex);
2414 oc_mutex_unlock(g_context.blockDataListMutex);
2419 coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID, uint16_t blockType)
2421 OIC_LOG(DEBUG, TAG, "IN-GetBlockOption");
2422 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2424 oc_mutex_lock(g_context.blockDataListMutex);
2426 size_t len = u_arraylist_length(g_context.dataList);
2427 for (size_t i = 0; i < len; i++)
2429 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2430 if (CABlockidMatches(currData, blockID))
2432 oc_mutex_unlock(g_context.blockDataListMutex);
2433 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2434 if (COAP_OPTION_BLOCK2 == blockType)
2436 return &currData->block2;
2438 else if (COAP_OPTION_BLOCK1 == blockType)
2440 return &currData->block1;
2444 oc_mutex_unlock(g_context.blockDataListMutex);
2446 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2450 CAPayload_t CAGetPayloadFromBlockDataList(const CABlockDataID_t *blockID,
2451 size_t *fullPayloadLen)
2453 OIC_LOG(DEBUG, TAG, "IN-GetFullPayload");
2454 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2455 VERIFY_NON_NULL_RET(fullPayloadLen, TAG, "fullPayloadLen", NULL);
2457 oc_mutex_lock(g_context.blockDataListMutex);
2459 size_t len = u_arraylist_length(g_context.dataList);
2460 for (size_t i = 0; i < len; i++)
2462 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2463 if (CABlockidMatches(currData, blockID))
2465 oc_mutex_unlock(g_context.blockDataListMutex);
2466 *fullPayloadLen = currData->receivedPayloadLen;
2467 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2468 return currData->payload;
2471 oc_mutex_unlock(g_context.blockDataListMutex);
2473 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2477 CABlockData_t *CACreateNewBlockData(const CAData_t *sendData)
2479 OIC_LOG(DEBUG, TAG, "IN-CACreateNewBlockData");
2480 VERIFY_NON_NULL_RET(sendData, TAG, "sendData", NULL);
2482 // create block data
2483 CABlockData_t *data = (CABlockData_t *) OICCalloc(1, sizeof(CABlockData_t));
2486 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2490 data->block1.szx = CA_DEFAULT_BLOCK_SIZE;
2491 data->block2.szx = CA_DEFAULT_BLOCK_SIZE;
2492 data->sentData = CACloneCAData(sendData);
2493 if (!data->sentData)
2495 OIC_LOG(ERROR, TAG, PCF("memory alloc has failed"));
2500 CAToken_t token = NULL;
2501 uint8_t tokenLength = 0;
2502 if (data->sentData->requestInfo)
2504 // update token info
2505 tokenLength = data->sentData->requestInfo->info.tokenLength;
2506 token = data->sentData->requestInfo->info.token;
2508 else if (data->sentData->responseInfo)
2510 tokenLength = data->sentData->responseInfo->info.tokenLength;
2511 token = data->sentData->responseInfo->info.token;
2514 if (!data->sentData->remoteEndpoint)
2516 OIC_LOG(ERROR, TAG, "remoteEndpoint is null");
2517 CADestroyDataSet(data->sentData);
2522 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(token, tokenLength,
2523 data->sentData->remoteEndpoint->addr,
2524 data->sentData->remoteEndpoint->port);
2525 if (NULL == blockDataID || blockDataID->idLength < 1)
2527 OIC_LOG(ERROR, TAG, "blockId is null");
2528 CADestroyBlockID(blockDataID);
2529 CADestroyDataSet(data->sentData);
2533 data->blockDataId = blockDataID;
2535 oc_mutex_lock(g_context.blockDataListMutex);
2537 bool res = u_arraylist_add(g_context.dataList, (void *) data);
2540 OIC_LOG(ERROR, TAG, "add has failed");
2541 CADestroyBlockID(data->blockDataId);
2542 CADestroyDataSet(data->sentData);
2544 oc_mutex_unlock(g_context.blockDataListMutex);
2547 oc_mutex_unlock(g_context.blockDataListMutex);
2549 OIC_LOG(DEBUG, TAG, "OUT-CreateBlockData");
2553 CAResult_t CARemoveBlockDataFromList(const CABlockDataID_t *blockID)
2555 OIC_LOG(DEBUG, TAG, "CARemoveBlockData");
2556 VERIFY_NON_NULL(blockID, TAG, "blockID");
2558 oc_mutex_lock(g_context.blockDataListMutex);
2560 size_t len = u_arraylist_length(g_context.dataList);
2561 for (size_t i = 0; i < len; i++)
2563 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2564 if (CABlockidMatches(currData, blockID))
2566 CABlockData_t *removedData = u_arraylist_remove(g_context.dataList, i);
2569 OIC_LOG(ERROR, TAG, "data is NULL");
2570 oc_mutex_unlock(g_context.blockDataListMutex);
2571 return CA_STATUS_FAILED;
2575 CADestroyDataSet(currData->sentData);
2576 CADestroyBlockID(currData->blockDataId);
2577 OICFree(currData->payload);
2579 oc_mutex_unlock(g_context.blockDataListMutex);
2580 return CA_STATUS_OK;
2583 oc_mutex_unlock(g_context.blockDataListMutex);
2585 return CA_STATUS_OK;
2588 CAResult_t CARemoveAllBlockDataFromList()
2590 OIC_LOG(DEBUG, TAG, "CARemoveAllBlockDataFromList");
2592 oc_mutex_lock(g_context.blockDataListMutex);
2594 size_t len = u_arraylist_length(g_context.dataList);
2595 for (size_t i = len; i > 0; i--)
2597 CABlockData_t *removedData = u_arraylist_remove(g_context.dataList, i - 1);
2601 if (removedData->sentData)
2603 CADestroyDataSet(removedData->sentData);
2605 CADestroyBlockID(removedData->blockDataId);
2606 OICFree(removedData->payload);
2607 OICFree(removedData);
2610 oc_mutex_unlock(g_context.blockDataListMutex);
2612 return CA_STATUS_OK;
2615 void CADestroyDataSet(CAData_t* data)
2617 VERIFY_NON_NULL_VOID(data, TAG, "data");
2619 CAFreeEndpoint(data->remoteEndpoint);
2620 if (data->requestInfo)
2622 CADestroyRequestInfoInternal(data->requestInfo);
2624 if (data->responseInfo)
2626 CADestroyResponseInfoInternal(data->responseInfo);
2631 CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength,
2632 const char* addr, uint16_t portNumber)
2634 size_t addrLength = strlen(addr);
2635 char port[PORT_LENGTH] = { 0, };
2636 port[0] = (char) ((portNumber >> 8) & 0xFF);
2637 port[1] = (char) (portNumber & 0xFF);
2639 CABlockDataID_t* blockDataID = (CABlockDataID_t *) OICMalloc(sizeof(CABlockDataID_t));
2642 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2645 blockDataID->idLength = tokenLength + sizeof(port) + addrLength;
2646 blockDataID->id = (uint8_t *) OICMalloc(blockDataID->idLength);
2647 if (!blockDataID->id)
2649 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2650 OICFree(blockDataID);
2656 memcpy(blockDataID->id, token, tokenLength);
2659 memcpy(blockDataID->id + tokenLength, port, sizeof(port));
2660 memcpy(blockDataID->id + tokenLength + sizeof(port), addr, addrLength);
2662 OIC_LOG(DEBUG, TAG, "BlockID is ");
2663 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)blockDataID->id, blockDataID->idLength);
2668 void CADestroyBlockID(CABlockDataID_t *blockID)
2670 VERIFY_NON_NULL_VOID(blockID, TAG, "blockID");
2671 OICFree(blockID->id);
2676 bool CABlockidMatches(const CABlockData_t *currData, const CABlockDataID_t *blockID)
2678 VERIFY_NON_NULL_RET(currData, TAG, "currData", false);
2679 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", false);
2680 VERIFY_NON_NULL_RET(blockID->id, TAG, "blockID->id", false);
2682 if ((currData->blockDataId)
2683 && (currData->blockDataId->id)
2684 && (currData->blockDataId->idLength == blockID->idLength)
2685 && !memcmp(currData->blockDataId->id, blockID->id, currData->blockDataId->idLength))
2692 void CALogBlockInfo(coap_block_t *block)
2694 VERIFY_NON_NULL_VOID(block, TAG, "block");
2696 OIC_LOG(DEBUG, TAG, "block option info");
2698 OIC_LOG_V(DEBUG, TAG, "block option-num : %d", block->num);
2700 OIC_LOG_V(DEBUG, TAG, "block option-m : %d", block->m);
2702 OIC_LOG_V(DEBUG, TAG, "block option-szx : %d", block->szx);
2705 CAResult_t CARemoveBlockDataFromListWithSeed(const CAToken_t token, uint8_t tokenLength,
2706 const char* addr, uint16_t portNumber)
2708 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(token, tokenLength, addr, portNumber);
2709 if (NULL == blockDataID || blockDataID->idLength < 1)
2711 OIC_LOG(ERROR, TAG, "blockId is null");
2712 CADestroyBlockID(blockDataID);
2713 return CA_STATUS_FAILED;
2716 CAResult_t res = CA_STATUS_OK;
2718 if (NULL != CAGetBlockDataFromBlockDataList(blockDataID))
2720 res = CARemoveBlockDataFromList(blockDataID);
2721 if (CA_STATUS_OK != res)
2723 OIC_LOG(ERROR, TAG, "CARemoveBlockDataFromList failed");
2727 CADestroyBlockID(blockDataID);