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"
48 #define TAG "OIC_CA_BWT"
50 #define BLOCKWISE_OPTION_BUFFER (sizeof(unsigned int))
51 #define BLOCK_NUMBER_IDX 4
52 #define BLOCK_M_BIT_IDX 3
55 #define BLOCK_SIZE(arg) (1 << ((arg) + 4))
57 #define BLOCK_DATA_TIMEOUT_SECONDS (60 * 1) // 1 minutes.
58 static const uint64_t USECS_PER_SEC = 1000000;
60 // context for block-wise transfer
61 static CABlockWiseContext_t g_context = { .sendThreadFunc = NULL,
62 .receivedThreadFunc = NULL,
65 static bool CACheckPayloadLength(const CAData_t *sendData)
67 size_t payloadLen = 0;
68 CAGetPayloadInfo(sendData, &payloadLen);
70 // check if message has to be transfered to a block
71 size_t maxBlockSize = BLOCK_SIZE(CA_DEFAULT_BLOCK_SIZE);
72 OIC_LOG_V(DEBUG, TAG, "payloadLen=%zu, maxBlockSize=%zu", payloadLen, maxBlockSize);
74 if (payloadLen <= maxBlockSize)
82 CAResult_t CAInitializeBlockWiseTransfer(CASendThreadFunc sendThreadFunc,
83 CAReceiveThreadFunc receivedThreadFunc)
85 OIC_LOG(DEBUG, TAG, "CAInitializeBlockWiseTransfer");
87 // set block-wise transfer context
88 if (!g_context.sendThreadFunc)
90 g_context.sendThreadFunc = sendThreadFunc;
93 if (!g_context.receivedThreadFunc)
95 g_context.receivedThreadFunc = receivedThreadFunc;
98 if (!g_context.dataList)
100 g_context.dataList = u_arraylist_create();
103 CAResult_t res = CAInitBlockWiseMutexVariables();
104 if (CA_STATUS_OK != res)
106 u_arraylist_free(&g_context.dataList);
107 g_context.dataList = NULL;
108 OIC_LOG(ERROR, TAG, "init has failed");
114 CAResult_t CATerminateBlockWiseTransfer()
116 OIC_LOG(DEBUG, TAG, "CATerminateBlockWiseTransfer");
118 if (g_context.dataList)
120 CARemoveAllBlockDataFromList();
121 u_arraylist_free(&g_context.dataList);
124 CATerminateBlockWiseMutexVariables();
129 CAResult_t CAInitBlockWiseMutexVariables()
131 if (!g_context.blockDataListMutex)
133 g_context.blockDataListMutex = oc_mutex_new();
134 if (!g_context.blockDataListMutex)
136 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
137 return CA_STATUS_FAILED;
141 if (!g_context.blockDataSenderMutex)
143 g_context.blockDataSenderMutex = oc_mutex_new();
144 if (!g_context.blockDataSenderMutex)
146 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
147 CATerminateBlockWiseMutexVariables();
148 return CA_STATUS_FAILED;
155 void CATerminateBlockWiseMutexVariables()
157 if (g_context.blockDataListMutex)
159 oc_mutex_free(g_context.blockDataListMutex);
160 g_context.blockDataListMutex = NULL;
163 if (g_context.blockDataSenderMutex)
165 oc_mutex_free(g_context.blockDataSenderMutex);
166 g_context.blockDataSenderMutex = NULL;
170 CAResult_t CASendBlockWiseData(const CAData_t *sendData)
172 VERIFY_NON_NULL(sendData, TAG, "sendData");
174 // check if message type is CA_MSG_RESET
175 if (sendData->requestInfo)
177 if (CA_MSG_RESET == sendData->requestInfo->info.type)
179 OIC_LOG(DEBUG, TAG, "reset message can't be sent to the block");
180 return CA_NOT_SUPPORTED;
184 * Other uses of the Block options in conjunction with multicast
185 * messages are for further study.
187 if (sendData->requestInfo->isMulticast)
189 OIC_LOG(DEBUG, TAG, "multicast message can't be sent to the block");
190 return CA_NOT_SUPPORTED;
193 else if (sendData->responseInfo)
195 if (CA_MSG_RESET == sendData->responseInfo->info.type)
197 OIC_LOG(DEBUG, TAG, "reset message can't be sent to the block");
198 return CA_NOT_SUPPORTED;
200 if (sendData->responseInfo->isMulticast)
202 OIC_LOG(DEBUG, TAG, "multicast message can't be sent to the block");
203 return CA_NOT_SUPPORTED;
207 // #1. check if it is already included in block data list
208 CABlockData_t *currData = NULL;
209 CAResult_t res = CACheckBlockDataValidation(sendData, &currData);
210 if (CA_STATUS_OK != res)
212 // #2. if it is not included, add the data into list
215 OIC_LOG(DEBUG, TAG, "There is no block data");
217 bool isBlock = CACheckPayloadLength(sendData);
220 if (sendData->requestInfo)
222 currData = CACreateNewBlockData(sendData);
225 OIC_LOG(ERROR, TAG, "failed to create block data");
226 return CA_MEMORY_ALLOC_FAILED;
229 return CA_NOT_SUPPORTED;
231 currData = CACreateNewBlockData(sendData);
234 OIC_LOG(ERROR, TAG, "failed to create block data");
235 return CA_MEMORY_ALLOC_FAILED;
240 // #3. check request/response block option type and payload length
241 res = CACheckBlockOptionType(currData);
242 if (CA_STATUS_OK == res)
244 // #4. send block message
245 OIC_LOG(DEBUG, TAG, "send first block msg");
246 res = CAAddSendThreadQueue(currData->sentData,
247 (const CABlockDataID_t *) &currData->blockDataId);
248 if (CA_STATUS_OK != res)
250 OIC_LOG(ERROR, TAG, "add has failed");
258 CAResult_t CAAddSendThreadQueue(const CAData_t *sendData, const CABlockDataID_t *blockID)
260 VERIFY_NON_NULL(sendData, TAG, "sendData");
261 VERIFY_NON_NULL(blockID, TAG, "blockID");
263 CAData_t *cloneData = CACloneCAData(sendData);
266 OIC_LOG(ERROR, TAG, "clone has failed");
267 CARemoveBlockDataFromList(blockID);
268 return CA_STATUS_FAILED;
271 if (g_context.sendThreadFunc)
273 oc_mutex_lock(g_context.blockDataSenderMutex);
274 g_context.sendThreadFunc(cloneData);
275 oc_mutex_unlock(g_context.blockDataSenderMutex);
279 CADestroyDataSet(cloneData);
284 CAResult_t CACheckBlockOptionType(CABlockData_t *currData)
286 VERIFY_NON_NULL(currData, TAG, "currData");
287 VERIFY_NON_NULL(currData->sentData, TAG, "currData->sentData");
289 bool isBlock = CACheckPayloadLength(currData->sentData);
292 return CA_NOT_SUPPORTED;
295 // set block option (COAP_OPTION_BLOCK2 or COAP_OPTION_BLOCK1)
296 if (currData->sentData->requestInfo) // request message
298 currData->type = COAP_OPTION_BLOCK1;
300 else // response message
302 currData->type = COAP_OPTION_BLOCK2;
308 // TODO make pdu const after libcoap is updated to support that.
309 CAResult_t CAReceiveBlockWiseData(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
310 const CAData_t *receivedData, size_t dataLen)
312 OIC_LOG(DEBUG, TAG, "CAReceiveBlockWiseData");
313 VERIFY_NON_NULL(pdu, TAG, "pdu");
314 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
315 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
316 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
318 // check if received message type is CA_MSG_RESET
319 if (CA_EMPTY == pdu->transport_hdr->udp.code)
321 OIC_LOG(DEBUG, TAG, "code is CA_EMPTY..");
323 if (!receivedData->responseInfo->info.token)
325 // get token from block-wise transfer list when CA_EMPTY(RST/ACK) is received
326 CAResult_t res = CAGetTokenFromBlockDataList(pdu, endpoint,
327 receivedData->responseInfo);
328 if (CA_STATUS_OK != res)
330 OIC_LOG(ERROR, TAG, "fail to get token");
335 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
336 receivedData->responseInfo->info.token,
337 receivedData->responseInfo->info.tokenLength,
339 if (NULL == blockDataID || blockDataID->idLength < 1)
341 // if retransmission is timeout, callback msg will be send without token.
342 if (NULL == blockDataID && !receivedData->responseInfo->info.token)
344 OIC_LOG(INFO, TAG, "retransmission was stopped");
345 return CA_REQUEST_TIMEOUT;
348 OIC_LOG(ERROR, TAG, "blockId is null");
349 CADestroyBlockID(blockDataID);
350 return CA_STATUS_FAILED;
353 // If we didn't send the last block message and received EMPTY message,
354 // we have to remain the block data from list.
355 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
356 if (data && (data->block1.m || data->block2.m))
358 OIC_LOG(DEBUG, TAG, "this is normal EMPTY message for blockwise-transfer.");
359 CADestroyBlockID(blockDataID);
363 CARemoveBlockDataFromList(blockDataID);
364 CADestroyBlockID(blockDataID);
365 return CA_NOT_SUPPORTED;
368 // check if block option is set and get block data
369 coap_block_t block = { 0, 0, 0 };
372 int isBlock1 = coap_get_block(pdu, COAP_OPTION_BLOCK1, &block);
375 CAResult_t res = CASetNextBlockOption1(pdu, endpoint, receivedData, block, dataLen);
376 if (CA_STATUS_OK != res)
378 OIC_LOG(ERROR, TAG, "setting has failed");
384 int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
387 CAResult_t res = CASetNextBlockOption2(pdu, endpoint, receivedData, block, dataLen);
388 if (CA_STATUS_OK != res)
390 OIC_LOG(ERROR, TAG, "setting has failed");
395 // if there is no block option in pdu, check if there is error code.
396 if (!isBlock1 && !isBlock2)
398 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
399 (CAToken_t)pdu->transport_hdr->udp.token,
400 pdu->transport_hdr->udp.token_length,
402 if (NULL == blockDataID || blockDataID->idLength < 1)
404 OIC_LOG(ERROR, TAG, "blockId is null");
405 CADestroyBlockID(blockDataID);
406 return CA_STATUS_FAILED;
409 uint32_t code = CA_RESPONSE_CODE(pdu->transport_hdr->udp.code);
410 if (CA_REQUEST_ENTITY_INCOMPLETE == code)
412 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
415 OIC_LOG(ERROR, TAG, "getting has failed");
416 CADestroyBlockID(blockDataID);
417 return CA_STATUS_FAILED;
420 coap_block_t *block = CAGetBlockOption(blockDataID, data->type);
423 OIC_LOG(ERROR, TAG, "block is null");
424 CADestroyBlockID(blockDataID);
425 return CA_STATUS_FAILED;
428 CAResult_t res = CA_STATUS_OK;
429 if (COAP_OPTION_BLOCK2 == data->type)
431 res = CASetNextBlockOption2(pdu, endpoint, receivedData, *block, dataLen);
432 if (CA_STATUS_OK != res)
434 OIC_LOG(ERROR, TAG, "setting has failed");
435 CADestroyBlockID(blockDataID);
436 return CA_STATUS_FAILED;
439 else if (COAP_OPTION_BLOCK1 == data->type)
441 res = CASetNextBlockOption1(pdu, endpoint, receivedData, *block, dataLen);
442 if (CA_STATUS_OK != res)
444 OIC_LOG(ERROR, TAG, "setting has failed");
445 CADestroyBlockID(blockDataID);
449 CADestroyBlockID(blockDataID);
454 OIC_LOG(DEBUG, TAG, "it's normal pdu");
456 // if received data is response message
457 // and sent data remain in block data list, remove block data
458 if (receivedData->responseInfo)
460 CARemoveBlockDataFromList(blockDataID);
462 CADestroyBlockID(blockDataID);
463 return CA_NOT_SUPPORTED;
469 CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData,
470 uint8_t blockWiseStatus, const CABlockDataID_t *blockID)
472 VERIFY_NON_NULL(pdu, TAG, "pdu");
473 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
474 VERIFY_NON_NULL(blockID, TAG, "blockID");
476 CAResult_t res = CA_STATUS_OK;
477 CAData_t *data = NULL;
479 // process blockWiseStatus
480 switch (blockWiseStatus)
482 case CA_OPTION2_FIRST_BLOCK:
483 res = CAAddSendThreadQueue(receivedData, blockID);
484 if (CA_STATUS_OK != res)
486 OIC_LOG(ERROR, TAG, "add has failed");
491 case CA_OPTION2_REQUEST:
492 // add data to send thread
493 data = CAGetDataSetFromBlockDataList(blockID);
496 OIC_LOG(ERROR, TAG, "it's unavailable");
497 return CA_STATUS_FAILED;
500 if (data->responseInfo)
502 data->responseInfo->info.type =
503 (pdu->transport_hdr->udp.type == CA_MSG_CONFIRM) ?
504 CA_MSG_ACKNOWLEDGE : CA_MSG_NONCONFIRM;
505 data->responseInfo->info.messageId = pdu->transport_hdr->udp.id;
507 res = CAAddSendThreadQueue(data, blockID);
508 if (CA_STATUS_OK != res)
510 OIC_LOG(ERROR, TAG, "add has failed");
516 case CA_OPTION1_RESPONSE:
517 case CA_OPTION2_RESPONSE:
518 case CA_OPTION1_REQUEST_BLOCK:
519 res = CASendBlockMessage(pdu, pdu->transport_hdr->udp.type, blockID);
520 if (CA_STATUS_OK != res)
522 OIC_LOG(ERROR, TAG, "send has failed");
527 case CA_OPTION2_LAST_BLOCK:
528 // process last block and send upper layer
529 res = CAReceiveLastBlock(blockID, receivedData);
530 if (CA_STATUS_OK != res)
532 OIC_LOG(ERROR, TAG, "receive has failed");
536 // remove data from list
537 res = CARemoveBlockDataFromList(blockID);
538 if (CA_STATUS_OK != res)
540 OIC_LOG(ERROR, TAG, "remove has failed");
545 case CA_OPTION1_REQUEST_LAST_BLOCK:
546 // process last block and send upper layer
547 res = CAReceiveLastBlock(blockID, receivedData);
548 if (CA_STATUS_OK != res)
550 OIC_LOG(ERROR, TAG, "receive has failed");
555 case CA_BLOCK_INCOMPLETE:
556 // add data to send thread
557 res = CASendErrorMessage(pdu, blockWiseStatus, CA_REQUEST_ENTITY_INCOMPLETE, blockID);
558 if (CA_STATUS_OK != res)
560 OIC_LOG(ERROR, TAG, "send has failed");
565 case CA_BLOCK_TOO_LARGE:
566 if (receivedData->requestInfo)
568 res = CASendErrorMessage(pdu, blockWiseStatus, CA_REQUEST_ENTITY_TOO_LARGE,
570 if (CA_STATUS_OK != res)
572 OIC_LOG(ERROR, TAG, "send has failed");
576 else if (receivedData->responseInfo)
578 res = CASendBlockMessage(pdu, pdu->transport_hdr->udp.type, blockID);
579 if (CA_STATUS_OK != res)
581 OIC_LOG(ERROR, TAG, "send has failed");
587 OIC_LOG_V(ERROR, TAG, "no logic [%d]", blockWiseStatus);
592 static CAResult_t CASendDirectEmptyResponse(const CAEndpoint_t *endpoint, uint16_t messageId)
594 OIC_LOG(DEBUG, TAG, "Entering CASendDirectEmptyResponse");
595 CAResponseInfo_t respInfo = {
598 respInfo.info.type = CA_MSG_ACKNOWLEDGE;
599 respInfo.info.messageId = messageId;
600 respInfo.info.dataType = CA_RESPONSE_DATA;
602 CAResult_t caResult = CASendResponse(endpoint, &respInfo);
604 OIC_LOG(DEBUG, TAG, "Exit CASendDirectEmptyResponse");
608 CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType,
609 const CABlockDataID_t *blockID)
611 VERIFY_NON_NULL(pdu, TAG, "pdu");
612 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
613 VERIFY_NON_NULL(blockID, TAG, "blockID");
615 CAData_t *data = CAGetDataSetFromBlockDataList(blockID);
618 OIC_LOG(ERROR, TAG, "CAData is unavailable");
619 return CA_STATUS_FAILED;
622 CAMessageType_t sentMsgType = CA_MSG_NONCONFIRM;
626 sentMsgType = CA_MSG_ACKNOWLEDGE;
628 case CA_MSG_ACKNOWLEDGE:
629 sentMsgType = CA_MSG_CONFIRM;
632 sentMsgType = CA_MSG_NONCONFIRM;
636 uint32_t code = pdu->transport_hdr->udp.code;
637 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
639 if (data->responseInfo)
641 OIC_LOG(DEBUG, TAG, "set response info");
642 data->responseInfo->info.messageId = pdu->transport_hdr->udp.id;
643 data->responseInfo->info.type = sentMsgType;
644 data->responseInfo->result = CA_CONTINUE;
649 if (data->requestInfo)
651 // if the received response message type is CON, send empty message.
652 // and then, send next block request message with new messagId.
653 if (msgType == CA_MSG_CONFIRM)
655 CASendDirectEmptyResponse(data->remoteEndpoint,
656 data->requestInfo->info.messageId);
657 sentMsgType = CA_MSG_CONFIRM;
660 OIC_LOG(DEBUG, TAG, "need new msgID");
661 data->requestInfo->info.messageId = 0;
662 data->requestInfo->info.type = sentMsgType;
664 else if (data->responseInfo)
666 data->responseInfo->info.messageId = pdu->transport_hdr->udp.id;
667 data->responseInfo->info.type = sentMsgType;
671 // add data to send thread
672 CAResult_t res = CAAddSendThreadQueue(data, blockID);
673 if (CA_STATUS_OK != res)
675 OIC_LOG(ERROR, TAG, "add has failed");
681 CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status,
682 CAResponseResult_t responseResult, const CABlockDataID_t *blockID)
684 VERIFY_NON_NULL(pdu, TAG, "pdu");
685 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
686 VERIFY_NON_NULL(blockID, TAG, "blockID");
688 // create error responseInfo
689 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockID);
692 OIC_LOG(ERROR, TAG, "data is unavailable");
693 return CA_STATUS_FAILED;
698 OIC_LOG(ERROR, TAG, "data has no sent-data");
699 return CA_STATUS_FAILED;
702 CAData_t *cloneData = CACloneCAData(data->sentData);
705 OIC_LOG(ERROR, TAG, "clone has failed");
706 return CA_MEMORY_ALLOC_FAILED;
709 CAMessageType_t sentMsgType = CA_MSG_NONCONFIRM;
710 switch (pdu->transport_hdr->udp.type)
713 sentMsgType = CA_MSG_ACKNOWLEDGE;
715 case CA_MSG_ACKNOWLEDGE:
716 sentMsgType = CA_MSG_CONFIRM;
719 sentMsgType = CA_MSG_NONCONFIRM;
722 if (cloneData->responseInfo)
724 cloneData->responseInfo->info.messageId = pdu->transport_hdr->udp.id;
725 cloneData->responseInfo->info.type = sentMsgType;
726 cloneData->responseInfo->result = responseResult;
730 CAInfo_t responseData = { .tokenLength = pdu->transport_hdr->udp.token_length };
731 responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength);
732 if (!responseData.token)
734 OIC_LOG(ERROR, TAG, "out of memory");
735 CADestroyDataSet(cloneData);
736 return CA_MEMORY_ALLOC_FAILED;
738 memcpy(responseData.token, pdu->transport_hdr->udp.token, responseData.tokenLength);
740 cloneData->responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
741 if (!cloneData->responseInfo)
743 OIC_LOG(ERROR, TAG, "out of memory");
744 CADestroyDataSet(cloneData);
745 OICFree(responseData.token);
746 return CA_MEMORY_ALLOC_FAILED;
749 cloneData->responseInfo->info = responseData;
750 cloneData->responseInfo->info.type = sentMsgType;
751 cloneData->responseInfo->result = responseResult;
754 OIC_LOG(DEBUG, TAG, "set response message to send error code");
756 // if there is a requestInfo, remove it to send response message
757 if (cloneData->requestInfo)
759 CADestroyRequestInfoInternal(cloneData->requestInfo);
760 cloneData->requestInfo = NULL;
763 // add data to send thread
764 if (g_context.sendThreadFunc)
766 oc_mutex_lock(g_context.blockDataSenderMutex);
767 g_context.sendThreadFunc(cloneData);
768 oc_mutex_unlock(g_context.blockDataSenderMutex);
772 CADestroyDataSet(cloneData);
775 // if error code is 4.08, remove the stored payload and initialize block number
776 if (CA_BLOCK_INCOMPLETE == status)
778 OICFree(data->payload);
779 data->payload = NULL;
780 data->payloadLength = 0;
781 data->receivedPayloadLen = 0;
782 data->block1.num = 0;
783 data->block2.num = 0;
789 CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID, const CAData_t *receivedData)
791 VERIFY_NON_NULL(blockID, TAG, "blockID");
792 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
794 // total block data have to notify to Application
795 CAData_t *cloneData = CACloneCAData(receivedData);
798 OIC_LOG(ERROR, TAG, "clone has failed");
799 return CA_MEMORY_ALLOC_FAILED;
803 size_t fullPayloadLen = 0;
804 CAPayload_t fullPayload = CAGetPayloadFromBlockDataList(blockID, &fullPayloadLen);
807 CAResult_t res = CAUpdatePayloadToCAData(cloneData, fullPayload, fullPayloadLen);
808 if (CA_STATUS_OK != res)
810 OIC_LOG(ERROR, TAG, "update has failed");
811 CADestroyDataSet(cloneData);
816 if (g_context.receivedThreadFunc)
818 g_context.receivedThreadFunc(cloneData);
822 CADestroyDataSet(cloneData);
828 static CABlockData_t* CACheckTheExistOfBlockData(const CABlockDataID_t* blockDataID,
829 coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
832 // Get BlockData data. If does not exist, create a new data
833 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
836 OIC_LOG(DEBUG, TAG, "block data doesn't exist in list. create new one");
838 CAData_t *cadata = CACreateNewDataSet(pdu, endpoint);
841 OIC_LOG(ERROR, TAG, "data is null");
845 data = CACreateNewBlockData(cadata);
848 OIC_LOG(ERROR, TAG, "failed to create a new block data");
849 CADestroyDataSet(cadata);
852 CADestroyDataSet(cadata);
855 // update BLOCK OPTION type
856 CAResult_t res = CAUpdateBlockOptionType(blockDataID, blockType);
857 if (CA_STATUS_OK != res)
859 OIC_LOG(ERROR, TAG, "update has failed");
866 // TODO make pdu const after libcoap is updated to support that.
867 CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
868 const CAData_t *receivedData, coap_block_t block,
871 OIC_LOG(INFO, TAG, "CASetNextBlockOption1");
872 VERIFY_NON_NULL(pdu, TAG, "pdu");
873 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
874 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
875 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
877 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
879 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
880 (CAToken_t)pdu->transport_hdr->udp.token,
881 pdu->transport_hdr->udp.token_length,
883 if (NULL == blockDataID || blockDataID->idLength < 1)
885 OIC_LOG(ERROR, TAG, "blockId is null");
886 CADestroyBlockID(blockDataID);
887 return CA_STATUS_FAILED;
890 CAResult_t res = CA_STATUS_OK;
891 CABlockData_t *data = CACheckTheExistOfBlockData(blockDataID, pdu, endpoint,
895 OIC_LOG(ERROR, TAG, "Failed to create or get block data");
896 res = CA_STATUS_FAILED;
900 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
901 uint32_t code = pdu->transport_hdr->udp.code;
902 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
904 // received message type is request
905 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
907 // check the size option
908 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu, COAP_OPTION_SIZE1,
909 &(data->payloadLength));
911 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
912 COAP_OPTION_BLOCK1, dataLen);
914 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
916 // store the received payload and merge
917 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
918 isSizeOption, COAP_OPTION_BLOCK1);
919 if (CA_STATUS_OK != res)
921 OIC_LOG(ERROR, TAG, "update has failed");
925 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1,
927 if (CA_STATUS_OK != res)
929 OIC_LOG(ERROR, TAG, "update has failed");
934 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
935 if (CA_STATUS_OK != res)
937 OIC_LOG(ERROR, TAG, "update has failed");
942 // check the blcok-wise transfer status for next step
943 if (CA_BLOCK_UNKNOWN == blockWiseStatus || CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
945 OIC_LOG_V(DEBUG, TAG, "M bit is %d", block.m);
947 blockWiseStatus = (0 == block.m) ?
948 CA_OPTION1_REQUEST_LAST_BLOCK : CA_OPTION1_REQUEST_BLOCK;
953 // received message type is response
954 uint32_t code = CA_RESPONSE_CODE(pdu->transport_hdr->udp.code);
955 if (0 == block.m && (CA_REQUEST_ENTITY_INCOMPLETE != code
956 && CA_REQUEST_ENTITY_TOO_LARGE != code))
958 int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
961 OIC_LOG(INFO, TAG, "received data is combining block1 and block2");
962 // initialize block number for response message
963 data->block1.num = 0;
964 CADestroyBlockID(blockDataID);
969 OIC_LOG(INFO, TAG, "received data is not bulk data");
970 CAReceiveLastBlock(blockDataID, receivedData);
976 blockWiseStatus = CA_OPTION1_RESPONSE;
977 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus);
978 if (CA_STATUS_OK != res)
980 OIC_LOG(ERROR, TAG, "update has failed");
981 CADestroyBlockID(blockDataID);
985 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
986 if (CA_STATUS_OK != res)
988 OIC_LOG(ERROR, TAG, "update has failed");
993 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
994 if (CA_STATUS_OK != res)
996 OIC_LOG(ERROR, TAG, "setting has failed");
1000 CADestroyBlockID(blockDataID);
1004 CARemoveBlockDataFromList(blockDataID);
1005 CADestroyBlockID(blockDataID);
1009 // TODO make pdu const after libcoap is updated to support that.
1010 CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
1011 const CAData_t *receivedData, coap_block_t block,
1014 OIC_LOG(DEBUG, TAG, "CASetNextBlockOption2");
1015 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
1017 VERIFY_NON_NULL(pdu, TAG, "pdu");
1018 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
1019 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1020 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
1022 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
1023 (CAToken_t)pdu->transport_hdr->udp.token,
1024 pdu->transport_hdr->udp.token_length,
1026 if (NULL == blockDataID || blockDataID->idLength < 1)
1028 OIC_LOG(ERROR, TAG, "blockId is null");
1029 CADestroyBlockID(blockDataID);
1030 return CA_STATUS_FAILED;
1033 CAResult_t res = CA_STATUS_OK;
1034 CABlockData_t *data = CACheckTheExistOfBlockData(blockDataID, pdu, endpoint,
1035 COAP_OPTION_BLOCK2);
1038 OIC_LOG(ERROR, TAG, "Failed to create or get block data");
1039 res = CA_STATUS_FAILED;
1043 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
1044 if (0 == block.num && CA_GET == pdu->transport_hdr->udp.code && 0 == block.m)
1046 OIC_LOG(INFO, TAG, "first block number");
1048 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1049 if (CA_STATUS_OK != res)
1051 OIC_LOG(ERROR, TAG, "update has failed");
1055 // first block data have to notify to Application
1056 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1057 if (CA_STATUS_OK != res)
1059 OIC_LOG(ERROR, TAG, "update has failed");
1062 blockWiseStatus = CA_OPTION2_FIRST_BLOCK;
1066 uint32_t code = pdu->transport_hdr->udp.code;
1067 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1069 // received message type is request
1070 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
1072 blockWiseStatus = CA_OPTION2_REQUEST;
1074 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1075 if (CA_STATUS_OK != res)
1077 OIC_LOG(ERROR, TAG, "update has failed");
1081 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1082 if (CA_STATUS_OK != res)
1084 OIC_LOG(ERROR, TAG, "update has failed");
1090 // received message type is response
1091 OIC_LOG(DEBUG, TAG, "received response message with block option2");
1093 // check the size option
1094 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
1096 &(data->payloadLength));
1098 uint32_t code = CA_RESPONSE_CODE(pdu->transport_hdr->udp.code);
1099 if (CA_REQUEST_ENTITY_INCOMPLETE != code && CA_REQUEST_ENTITY_TOO_LARGE != code)
1101 // check if received payload is exact
1102 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
1103 COAP_OPTION_BLOCK2, dataLen);
1106 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
1108 // store the received payload and merge
1109 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
1110 isSizeOption, COAP_OPTION_BLOCK2);
1111 if (CA_STATUS_OK != res)
1113 OIC_LOG(ERROR, TAG, "update has failed");
1118 if (0 == block.m && CA_BLOCK_UNKNOWN == blockWiseStatus) // Last block is received
1120 OIC_LOG(DEBUG, TAG, "M bit is 0");
1121 blockWiseStatus = CA_OPTION2_LAST_BLOCK;
1125 if (CA_BLOCK_UNKNOWN == blockWiseStatus ||
1126 CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
1128 OIC_LOG(DEBUG, TAG, "M bit is 1");
1129 blockWiseStatus = CA_OPTION2_RESPONSE;
1132 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2,
1134 if (CA_STATUS_OK != res)
1136 OIC_LOG(ERROR, TAG, "update has failed");
1140 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1141 if (CA_STATUS_OK != res)
1143 OIC_LOG(ERROR, TAG, "update has failed");
1150 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
1151 if (CA_STATUS_OK != res)
1153 OIC_LOG(ERROR, TAG, "setting has failed");
1157 CADestroyBlockID(blockDataID);
1158 return CA_STATUS_OK;
1161 CARemoveBlockDataFromList(blockDataID);
1162 CADestroyBlockID(blockDataID);
1166 CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu,
1167 coap_block_t *block, uint16_t blockType,
1170 VERIFY_NON_NULL(currData, TAG, "currData");
1171 VERIFY_NON_NULL(pdu, TAG, "pdu");
1172 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
1173 VERIFY_NON_NULL(block, TAG, "block");
1175 // update block data
1176 CAResult_t res = CA_STATUS_OK;
1177 uint32_t code = CA_RESPONSE_CODE(pdu->transport_hdr->udp.code);
1179 if (CA_REQUEST_ENTITY_INCOMPLETE == code || CA_REQUEST_ENTITY_TOO_LARGE == code)
1181 // response error code of the received block message
1182 res = CAHandleBlockErrorResponse(block, blockType, code);
1183 if (CA_STATUS_OK != res)
1185 OIC_LOG(ERROR, TAG, "error handle has failed");
1191 // update block option items
1194 case CA_OPTION1_RESPONSE:
1195 if (currData->block1.num > block->num)
1197 OIC_LOG(ERROR, TAG, "received incorrect block num");
1198 return CA_STATUS_FAILED;
1202 case CA_OPTION2_REQUEST:
1205 case CA_OPTION2_RESPONSE:
1206 if (currData->block2.num > block->num)
1208 OIC_LOG(ERROR, TAG, "received incorrect block num");
1209 return CA_STATUS_FAILED;
1214 case CA_BLOCK_TOO_LARGE:
1215 // if state of received block message is CA_BLOCK_TOO_LARGE or CA_BLOCK_INCOMPLETE
1216 // we set the response error code appropriately and send
1217 if (COAP_OPTION_BLOCK2 == blockType)
1221 block->szx = currData->block2.szx;
1225 block->szx = currData->block1.szx;
1229 OIC_LOG_V(ERROR, TAG, "no logic [%d]", status);
1232 if (CA_BLOCK_INCOMPLETE != status && CA_BLOCK_TOO_LARGE != status)
1234 // negotiate block size
1235 res = CANegotiateBlockSize(currData, block, pdu, blockType);
1236 if (CA_STATUS_OK != res)
1238 OIC_LOG(ERROR, TAG, "negotiation has failed");
1246 CAResult_t CASetMoreBitFromBlock(size_t payloadLen, coap_block_t *block)
1248 VERIFY_NON_NULL(block, TAG, "block");
1250 if ((size_t) ((block->num + 1) << (block->szx + BLOCK_NUMBER_IDX)) < payloadLen)
1252 OIC_LOG(DEBUG, TAG, "Set the M-bit(1)");
1257 OIC_LOG(DEBUG, TAG, "Set the M-bit(0)");
1261 return CA_STATUS_OK;
1264 CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block,
1265 const coap_pdu_t *pdu, uint16_t blockType)
1267 OIC_LOG(DEBUG, TAG, "IN-NegotiateBlockSize");
1269 VERIFY_NON_NULL(currData, TAG, "currData");
1270 VERIFY_NON_NULL(block, TAG, "block");
1271 VERIFY_NON_NULL(pdu, TAG, "pdu");
1272 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
1274 bool isReqMsg = false;
1275 uint32_t code = pdu->transport_hdr->udp.code;
1276 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1281 // #1. check the block option type
1282 if (COAP_OPTION_BLOCK2 == blockType)
1284 // #2. check the message type
1287 if (block->szx > currData->block2.szx)
1289 OIC_LOG(DEBUG, TAG, "sze is big");
1291 // #3. calculate new block number from block size
1292 unsigned int blockNum = BLOCK_SIZE(block->szx) /
1293 BLOCK_SIZE(currData->block2.szx) - 1;
1294 OIC_LOG(DEBUG, TAG, "num is set as Negotiation");
1295 block->num += blockNum;
1296 block->szx = currData->block2.szx;
1297 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1302 if (block->szx > currData->block2.szx)
1304 OIC_LOG(DEBUG, TAG, "sze is big");
1305 block->szx = currData->block2.szx;
1309 else if (COAP_OPTION_BLOCK1 == blockType)
1313 if (block->szx < currData->block1.szx)
1315 OIC_LOG(DEBUG, TAG, "sze is small");
1317 unsigned int blockNum = BLOCK_SIZE(currData->block1.szx) /
1318 BLOCK_SIZE(block->szx) - 1;
1319 block->num += blockNum;
1320 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1325 if (block->szx > currData->block1.szx)
1327 OIC_LOG(DEBUG, TAG, "sze is big");
1328 block->szx = currData->block1.szx;
1334 OIC_LOG(DEBUG, TAG, "Invalid block option");
1335 return CA_STATUS_FAILED;
1338 OIC_LOG(DEBUG, TAG, "OUT-NegotiateBlockSize");
1340 return CA_STATUS_OK;
1343 CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block,
1346 VERIFY_NON_NULL(currData, TAG, "currData");
1348 // check if block size is bigger than CABlockSize_t
1349 if (block.szx > CA_BLOCK_SIZE_1024_BYTE)
1351 OIC_LOG(DEBUG, TAG, "invalid block szx");
1352 return CA_STATUS_FAILED;
1355 // update block option
1356 if (COAP_OPTION_BLOCK2 == blockType)
1358 currData->block2 = block;
1362 currData->block1 = block;
1365 OIC_LOG(DEBUG, TAG, "data has updated");
1366 return CA_STATUS_OK;
1369 CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID)
1371 VERIFY_NON_NULL(pdu, TAG, "pdu");
1372 VERIFY_NON_NULL(pdu->transport_hdr, TAG, "pdu->transport_hdr");
1373 VERIFY_NON_NULL(blockID, TAG, "blockID");
1375 // if message is sent, update messageId in block-wise transfer list
1376 CAData_t * cadata = CAGetDataSetFromBlockDataList(blockID);
1379 OIC_LOG(ERROR, TAG, "CAData is unavailable");
1380 return CA_STATUS_FAILED;
1383 if (cadata->requestInfo)
1385 cadata->requestInfo->info.messageId = pdu->transport_hdr->udp.id;
1388 return CA_STATUS_OK;
1391 CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info,
1392 const CAEndpoint_t *endpoint, coap_list_t **options)
1394 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption");
1395 VERIFY_NON_NULL(pdu, TAG, "pdu");
1396 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1397 VERIFY_NON_NULL((*pdu)->transport_hdr, TAG, "(*pdu)->transport_hdr");
1398 VERIFY_NON_NULL(info, TAG, "info");
1399 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1400 VERIFY_NON_NULL(options, TAG, "options");
1402 CAResult_t res = CA_STATUS_OK;
1403 size_t dataLength = 0;
1406 dataLength = info->payloadSize;
1407 OIC_LOG_V(DEBUG, TAG, "dataLength - %zu", dataLength);
1410 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
1411 (CAToken_t)(*pdu)->transport_hdr->udp.token,
1412 (*pdu)->transport_hdr->udp.token_length,
1414 if (NULL == blockDataID || blockDataID->idLength < 1)
1416 OIC_LOG(ERROR, TAG, "blockId is null");
1417 res = CA_STATUS_FAILED;
1421 uint32_t repCode = CA_RESPONSE_CODE((*pdu)->transport_hdr->udp.code);
1422 if (CA_REQUEST_ENTITY_INCOMPLETE == repCode)
1424 OIC_LOG(INFO, TAG, "don't use option");
1429 uint8_t blockType = CAGetBlockOptionType(blockDataID);
1430 if (COAP_OPTION_BLOCK2 == blockType)
1432 res = CAAddBlockOption2(pdu, info, dataLength, blockDataID, options);
1433 if (CA_STATUS_OK != res)
1435 OIC_LOG(ERROR, TAG, "add has failed");
1439 else if (COAP_OPTION_BLOCK1 == blockType)
1441 res = CAAddBlockOption1(pdu, info, dataLength, blockDataID, options);
1442 if (CA_STATUS_OK != res)
1444 OIC_LOG(ERROR, TAG, "add has failed");
1450 OIC_LOG(DEBUG, TAG, "no BLOCK option");
1452 // in case it is not large data, add option list to pdu.
1455 for (coap_list_t *opt = *options; opt; opt = opt->next)
1457 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
1458 COAP_OPTION_DATA(*(coap_option *) opt->data));
1459 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", (*pdu)->length);
1461 if (0 == coap_add_option(*pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
1462 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
1463 COAP_OPTION_DATA(*(coap_option *) opt->data)))
1465 OIC_LOG(ERROR, TAG, "coap_add_option has failed");
1466 res = CA_STATUS_FAILED;
1471 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", (*pdu)->length);
1473 // if response data is so large. it have to send as block transfer
1474 if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload))
1476 OIC_LOG(INFO, TAG, "it has to use block");
1477 res = CA_STATUS_FAILED;
1482 OIC_LOG(INFO, TAG, "not Blockwise Transfer");
1486 uint32_t code = (*pdu)->transport_hdr->udp.code;
1487 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1489 // if received message type is RESET from remote device,
1490 // we have to use the updated message id of request message to find token.
1491 CABlockData_t *blockData = CAGetBlockDataFromBlockDataList(blockDataID);
1494 res = CAUpdateMessageId(*pdu, blockDataID);
1495 if (CA_STATUS_OK != res)
1497 OIC_LOG(ERROR, TAG, "fail to update message id");
1503 CAResetBlockDataTTL(blockDataID);
1505 CADestroyBlockID(blockDataID);
1506 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption");
1510 CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
1511 const CABlockDataID_t *blockID, coap_list_t **options)
1513 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption2");
1514 VERIFY_NON_NULL(pdu, TAG, "pdu");
1515 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1516 VERIFY_NON_NULL((*pdu)->transport_hdr, TAG, "(*pdu)->transport_hdr");
1517 VERIFY_NON_NULL(info, TAG, "info");
1518 VERIFY_NON_NULL(blockID, TAG, "blockID");
1519 VERIFY_NON_NULL(options, TAG, "options");
1521 // get set block data from CABlock list-set.
1522 coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1523 coap_block_t *block2 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK2);
1524 if (!block1 || !block2)
1526 OIC_LOG(ERROR, TAG, "getting has failed");
1527 return CA_STATUS_FAILED;
1530 CAResult_t res = CA_STATUS_OK;
1531 uint32_t code = (*pdu)->transport_hdr->udp.code;
1532 if (CA_GET != code && CA_POST != code && CA_PUT != code && CA_DELETE != code)
1534 CASetMoreBitFromBlock(dataLength, block2);
1536 // if block number is 0, add size2 option
1537 if (0 == block2->num)
1539 res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE2, dataLength, options);
1540 if (CA_STATUS_OK != res)
1542 OIC_LOG(ERROR, TAG, "add has failed");
1547 res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options);
1548 if (CA_STATUS_OK != res)
1550 OIC_LOG(ERROR, TAG, "add has failed");
1556 OIC_LOG(DEBUG, TAG, "combining block1 and block2");
1557 res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1558 if (CA_STATUS_OK != res)
1560 OIC_LOG(ERROR, TAG, "add has failed");
1563 // initialize block number
1567 res = CAAddOptionToPDU(*pdu, options);
1568 if (CA_STATUS_OK != res)
1570 OIC_LOG(ERROR, TAG, "add has failed");
1574 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload,
1575 block2->num, block2->szx))
1577 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1578 return CA_STATUS_FAILED;
1581 CALogBlockInfo(block2);
1585 // if sent message is last response block message, remove data
1586 CARemoveBlockDataFromList(blockID);
1591 OIC_LOG(DEBUG, TAG, "option2, not response msg");
1592 res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options);
1593 if (CA_STATUS_OK != res)
1595 OIC_LOG(ERROR, TAG, "add has failed");
1599 res = CAAddOptionToPDU(*pdu, options);
1600 if (CA_STATUS_OK != res)
1602 OIC_LOG(ERROR, TAG, "add has failed");
1605 CALogBlockInfo(block2);
1608 return CA_STATUS_OK;
1611 CARemoveBlockDataFromList(blockID);
1615 CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
1616 const CABlockDataID_t *blockID, coap_list_t **options)
1618 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption1");
1619 VERIFY_NON_NULL(pdu, TAG, "pdu");
1620 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1621 VERIFY_NON_NULL((*pdu)->transport_hdr, TAG, "(*pdu)->transport_hdr");
1622 VERIFY_NON_NULL(info, TAG, "info");
1623 VERIFY_NON_NULL(blockID, TAG, "blockID");
1624 VERIFY_NON_NULL(options, TAG, "options");
1626 // get set block data from CABlock list-set.
1627 coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1630 OIC_LOG(ERROR, TAG, "getting has failed");
1631 return CA_STATUS_FAILED;
1634 CAResult_t res = CA_STATUS_OK;
1635 uint32_t code = (*pdu)->transport_hdr->udp.code;
1636 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
1638 CASetMoreBitFromBlock(dataLength, block1);
1640 // if block number is 0, add size1 option
1641 if (0 == block1->num)
1643 res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE1, dataLength, options);
1644 if (CA_STATUS_OK != res)
1646 OIC_LOG(ERROR, TAG, "add has failed");
1651 // add block option to option list.
1652 res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1653 if (CA_STATUS_OK != res)
1655 OIC_LOG(ERROR, TAG, "add has failed");
1659 // add option list to pdu.
1660 res = CAAddOptionToPDU(*pdu, options);
1661 if (CA_STATUS_OK != res)
1663 OIC_LOG(ERROR, TAG, "add has failed");
1667 // add the payload data as the block size.
1668 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload, block1->num,
1671 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1672 return CA_STATUS_FAILED;
1677 OIC_LOG(DEBUG, TAG, "received response message with block option1");
1679 // add block option to option list.
1680 res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1681 if (CA_STATUS_OK != res)
1683 OIC_LOG(ERROR, TAG, "add has failed");
1687 // add option list to pdu.
1688 res = CAAddOptionToPDU(*pdu, options);
1689 if (CA_STATUS_OK != res)
1691 OIC_LOG(ERROR, TAG, "add has failed");
1695 // add the payload data as the block size.
1696 if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload))
1698 OIC_LOG(ERROR, TAG, "failed to add payload");
1699 return CA_STATUS_FAILED;
1702 // if it is last block message, remove block data from list.
1705 // remove data from list
1706 res = CARemoveBlockDataFromList(blockID);
1707 if (CA_STATUS_OK != res)
1709 OIC_LOG(ERROR, TAG, "remove has failed");
1715 CALogBlockInfo(block1);
1717 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption1");
1719 return CA_STATUS_OK;
1722 CARemoveBlockDataFromList(blockID);
1726 CAResult_t CAAddBlockOptionImpl(coap_block_t *block, uint8_t blockType,
1727 coap_list_t **options)
1729 OIC_LOG(DEBUG, TAG, "IN-AddBlockOptionImpl");
1730 VERIFY_NON_NULL(block, TAG, "block");
1731 VERIFY_NON_NULL(options, TAG, "options");
1733 unsigned char buf[BLOCKWISE_OPTION_BUFFER] = { 0 };
1734 unsigned int optionLength = coap_encode_var_bytes(buf,
1735 ((block->num << BLOCK_NUMBER_IDX)
1736 | (block->m << BLOCK_M_BIT_IDX)
1739 int ret = coap_insert(options,
1740 CACreateNewOptionNode(blockType, optionLength, (char *) buf),
1744 return CA_STATUS_INVALID_PARAM;
1747 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOptionImpl");
1748 return CA_STATUS_OK;
1751 CAResult_t CAAddOptionToPDU(coap_pdu_t *pdu, coap_list_t **options)
1753 // after adding the block option to option list, add option list to pdu.
1756 for (coap_list_t *opt = *options; opt; opt = opt->next)
1758 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
1759 COAP_OPTION_DATA(*(coap_option *) opt->data));
1761 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
1762 int ret = coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
1763 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
1764 COAP_OPTION_DATA(*(coap_option *) opt->data));
1767 return CA_STATUS_FAILED;
1772 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
1774 return CA_STATUS_OK;
1777 CAResult_t CAAddBlockSizeOption(coap_pdu_t *pdu, uint16_t sizeType, size_t dataLength,
1778 coap_list_t **options)
1780 OIC_LOG(DEBUG, TAG, "IN-CAAddBlockSizeOption");
1781 VERIFY_NON_NULL(pdu, TAG, "pdu");
1782 VERIFY_NON_NULL(options, TAG, "options");
1784 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1786 OIC_LOG(ERROR, TAG, "unknown option type");
1787 return CA_STATUS_FAILED;
1790 unsigned char value[BLOCKWISE_OPTION_BUFFER] = { 0 };
1791 unsigned int optionLength = coap_encode_var_bytes(value, dataLength);
1793 int ret = coap_insert(options,
1794 CACreateNewOptionNode(sizeType, optionLength, (char *) value),
1798 return CA_STATUS_INVALID_PARAM;
1801 OIC_LOG(DEBUG, TAG, "OUT-CAAddBlockSizeOption");
1803 return CA_STATUS_OK;
1806 // TODO make pdu const after libcoap is updated to support that.
1807 bool CAIsPayloadLengthInPduWithBlockSizeOption(coap_pdu_t *pdu,
1809 size_t *totalPayloadLen)
1811 OIC_LOG(DEBUG, TAG, "IN-CAIsPayloadLengthInPduWithBlockSizeOption");
1812 VERIFY_NON_NULL(pdu, TAG, "pdu");
1813 VERIFY_NON_NULL(totalPayloadLen, TAG, "totalPayloadLen");
1815 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1817 OIC_LOG(ERROR, TAG, "unknown option type");
1818 return CA_STATUS_FAILED;
1821 coap_opt_iterator_t opt_iter;
1822 coap_opt_t *option = coap_check_option(pdu, sizeType, &opt_iter);
1825 OIC_LOG(DEBUG, TAG, "get size option from pdu");
1826 *totalPayloadLen = coap_decode_var_bytes(COAP_OPT_VALUE(option),
1827 COAP_OPT_LENGTH(option));
1829 OIC_LOG_V(DEBUG, TAG, "the total payload length to be received is [%zu]bytes",
1835 OIC_LOG(DEBUG, TAG, "OUT-CAIsPayloadLengthInPduWithBlockSizeOption");
1840 uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlock,
1841 const CAData_t *receivedData, uint16_t blockType,
1844 OIC_LOG(DEBUG, TAG, "IN-CheckBlockError");
1846 VERIFY_NON_NULL(currData, TAG, "currData is NULL");
1847 VERIFY_NON_NULL(receivedBlock, TAG, "receivedBlock is NULL");
1848 VERIFY_NON_NULL(receivedData, TAG, "receivedData is NULL");
1850 // #1. check the received payload length
1851 size_t blockPayloadLen = 0;
1852 CAGetPayloadInfo(receivedData, &blockPayloadLen);
1854 // #2. check if the block sequence is right
1855 if (COAP_OPTION_BLOCK1 == blockType)
1857 size_t prePayloadLen = currData->receivedPayloadLen;
1858 if (prePayloadLen != (size_t) BLOCK_SIZE(receivedBlock->szx) * receivedBlock->num)
1860 if (receivedBlock->num > currData->block1.num + 1)
1862 // 408 Error handling of block loss
1863 OIC_LOG(ERROR, TAG, "option1: error 4.08");
1864 OIC_LOG(ERROR, TAG, "it didn't order");
1865 return CA_BLOCK_INCOMPLETE;
1867 return CA_BLOCK_RECEIVED_ALREADY;
1870 else if (COAP_OPTION_BLOCK2 == blockType)
1872 if (receivedBlock->num != currData->block2.num)
1874 if (receivedBlock->num > currData->block2.num)
1876 // 408 Error handling of block loss
1877 OIC_LOG(ERROR, TAG, "option2: error 4.08");
1878 OIC_LOG(ERROR, TAG, "it didn't order");
1879 return CA_BLOCK_INCOMPLETE;
1883 OIC_LOG(ERROR, TAG, "already received this block");
1884 return CA_BLOCK_RECEIVED_ALREADY;
1889 // #3. check if error check logic is required
1890 size_t optionLen = dataLen - blockPayloadLen;
1891 if (receivedBlock->m && blockPayloadLen != (size_t) BLOCK_SIZE(receivedBlock->szx))
1893 // 413 Error handling of too large entity
1894 if (COAP_MAX_PDU_SIZE < BLOCK_SIZE(receivedBlock->szx) + optionLen)
1896 // buffer size is smaller than received block size
1897 OIC_LOG(ERROR, TAG, "error type 4.13");
1898 OIC_LOG(ERROR, TAG, "too large size");
1900 // set the block size to be smaller than COAP_MAX_PDU_SIZE
1901 for (int32_t size = CA_DEFAULT_BLOCK_SIZE; size >= 0; size--)
1903 if (COAP_MAX_PDU_SIZE >= BLOCK_SIZE(size) + optionLen)
1905 OIC_LOG_V(ERROR, TAG, "replace sze with %d", size);
1906 currData->block2.szx = size;
1907 currData->block1.szx = size;
1911 return CA_BLOCK_TOO_LARGE;
1915 // 408 Error handling of payload loss
1916 OIC_LOG(ERROR, TAG, "error type 4.08");
1917 OIC_LOG(ERROR, TAG, "payload len != block sze");
1918 return CA_BLOCK_INCOMPLETE;
1921 else if (0 == receivedBlock->m && 0 != currData->payloadLength)
1923 // if the received block is last block, check the total payload length
1924 size_t receivedPayloadLen = currData->receivedPayloadLen;
1925 receivedPayloadLen += blockPayloadLen;
1927 if (receivedPayloadLen != currData->payloadLength)
1929 OIC_LOG(ERROR, TAG, "error type 4.08");
1930 OIC_LOG(ERROR, TAG, "total payload length is wrong");
1931 return CA_BLOCK_INCOMPLETE;
1935 OIC_LOG(DEBUG, TAG, "received all data normally");
1937 OIC_LOG(DEBUG, TAG, "OUT-CheckBlockError");
1939 return CA_BLOCK_UNKNOWN;
1942 CAResult_t CAUpdatePayloadData(CABlockData_t *currData, const CAData_t *receivedData,
1943 uint8_t status, bool isSizeOption, uint16_t blockType)
1945 OIC_LOG(DEBUG, TAG, "IN-UpdatePayloadData");
1947 VERIFY_NON_NULL(currData, TAG, "currData");
1948 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
1950 // if error code is 4.08, do not update payload
1951 if (CA_BLOCK_INCOMPLETE == status)
1953 OIC_LOG(ERROR, TAG, "no require to update");
1954 return CA_STATUS_OK;
1957 size_t blockPayloadLen = 0;
1958 CAPayload_t blockPayload = CAGetPayloadInfo(receivedData, &blockPayloadLen);
1960 if (CA_BLOCK_TOO_LARGE == status)
1962 blockPayloadLen = (COAP_OPTION_BLOCK2 == blockType) ?
1963 BLOCK_SIZE(currData->block2.szx) : BLOCK_SIZE(currData->block1.szx);
1966 // memory allocation for the received block payload
1967 size_t prePayloadLen = currData->receivedPayloadLen;
1970 if (currData->payloadLength)
1972 // in case the block message has the size option
1973 // allocate the memory for the total payload
1976 CAPayload_t prePayload = currData->payload;
1978 OIC_LOG(DEBUG, TAG, "allocate memory for the total payload");
1979 currData->payload = (CAPayload_t) OICCalloc(1, currData->payloadLength);
1980 if (NULL == currData->payload)
1982 OIC_LOG(ERROR, TAG, "out of memory");
1983 return CA_MEMORY_ALLOC_FAILED;
1985 memcpy(currData->payload, prePayload, prePayloadLen);
1986 OICFree(prePayload);
1989 // update the total payload
1990 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
1994 OIC_LOG(DEBUG, TAG, "allocate memory for the received block payload");
1996 size_t totalPayloadLen = prePayloadLen + blockPayloadLen;
1997 CAPayload_t newPayload = OICRealloc(currData->payload, totalPayloadLen);
1998 if (NULL == newPayload)
2000 OIC_LOG(ERROR, TAG, "out of memory");
2001 return CA_MEMORY_ALLOC_FAILED;
2004 // update the total payload
2005 memset(newPayload + prePayloadLen, 0, blockPayloadLen);
2006 currData->payload = newPayload;
2007 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
2010 // update received payload length
2011 currData->receivedPayloadLen += blockPayloadLen;
2013 OIC_LOG_V(DEBUG, TAG, "updated payload: %s, len: %zu", currData->payload,
2014 currData->receivedPayloadLen);
2017 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayloadData");
2018 return CA_STATUS_OK;
2021 CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
2023 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", NULL);
2024 VERIFY_NON_NULL_RET(pdu->transport_hdr, TAG, "pdu->transport_hdr", NULL);
2025 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
2027 CARequestInfo_t* requestInfo = NULL;
2028 CAResponseInfo_t* responseInfo = NULL;
2030 uint32_t code = pdu->transport_hdr->udp.code;
2031 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
2033 CAInfo_t responseData = { .tokenLength = pdu->transport_hdr->udp.token_length };
2034 responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength);
2035 if (!responseData.token)
2037 OIC_LOG(ERROR, TAG, "out of memory");
2040 memcpy(responseData.token, pdu->transport_hdr->udp.token, responseData.tokenLength);
2042 responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
2045 OIC_LOG(ERROR, TAG, "out of memory");
2046 OICFree(responseData.token);
2049 responseInfo->info = responseData;
2053 CAInfo_t requestData = { .tokenLength = pdu->transport_hdr->udp.token_length };
2054 requestData.token = (CAToken_t) OICMalloc(requestData.tokenLength);
2055 if (!requestData.token)
2057 OIC_LOG(ERROR, TAG, "out of memory");
2060 memcpy(requestData.token, pdu->transport_hdr->udp.token, requestData.tokenLength);
2062 requestInfo = (CARequestInfo_t*) OICCalloc(1, sizeof(CARequestInfo_t));
2065 OIC_LOG(ERROR, TAG, "out of memory");
2066 OICFree(requestData.token);
2069 requestInfo->info = requestData;
2071 // get resource uri information from received response message
2072 // to send next request message to remote device
2073 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(*resInfo));
2076 OIC_LOG(ERROR, TAG, "memory allocation failed");
2077 OICFree(requestData.token);
2078 OICFree(requestInfo);
2082 CAGetResponseInfoFromPDU(pdu, resInfo, endpoint);
2083 requestInfo->method = CA_GET;
2084 requestInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu->transport_hdr,
2086 requestInfo->info.resourceUri = OICStrdup(resInfo->info.resourceUri);
2088 // after copying the resource uri, destroy response info.
2089 CADestroyResponseInfoInternal(resInfo);
2092 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2095 OIC_LOG(ERROR, TAG, "out of memory");
2096 if (NULL != requestInfo)
2098 OICFree(requestInfo->info.resourceUri);
2099 OICFree(requestInfo->info.token);
2100 OICFree(requestInfo);
2102 if (NULL != responseInfo)
2104 OICFree(responseInfo->info.token);
2105 OICFree(responseInfo);
2110 data->requestInfo = requestInfo;
2111 data->responseInfo = responseInfo;
2112 data->remoteEndpoint = CACloneEndpoint(endpoint);
2113 data->type = SEND_TYPE_UNICAST;
2118 CAData_t *CACloneCAData(const CAData_t *data)
2120 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2122 CAData_t *clone = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2125 OIC_LOG(ERROR, TAG, "out of memory");
2130 if (data->requestInfo)
2132 clone->requestInfo = CACloneRequestInfo(data->requestInfo);
2134 else if (data->responseInfo)
2136 clone->responseInfo = CACloneResponseInfo(data->responseInfo);
2139 if (data->remoteEndpoint)
2141 clone->remoteEndpoint = CACloneEndpoint(data->remoteEndpoint);
2147 CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
2150 OIC_LOG(DEBUG, TAG, "IN-UpdatePayload");
2152 VERIFY_NON_NULL(data, TAG, "data is NULL");
2153 VERIFY_NON_NULL(payload, TAG, "payload is NULL");
2155 CAPayload_t newPayload = NULL;
2156 switch (data->dataType)
2158 case CA_REQUEST_DATA:
2159 if (!data->requestInfo)
2161 OIC_LOG(ERROR, TAG, "request info is null");
2162 return CA_STATUS_FAILED;
2164 // allocate payload field
2165 newPayload = OICRealloc(data->requestInfo->info.payload, payloadLen);
2168 OIC_LOG(ERROR, TAG, "out of memory");
2169 return CA_STATUS_FAILED;
2171 data->requestInfo->info.payload = newPayload;
2172 memcpy(data->requestInfo->info.payload, payload, payloadLen);
2173 data->requestInfo->info.payloadSize = payloadLen;
2176 case CA_RESPONSE_DATA:
2177 if (!data->responseInfo)
2179 OIC_LOG(ERROR, TAG, "response info is null");
2180 return CA_STATUS_FAILED;
2182 // allocate payload field
2183 newPayload = OICRealloc(data->responseInfo->info.payload, payloadLen);
2186 OIC_LOG(ERROR, TAG, "out of memory");
2187 return CA_STATUS_FAILED;
2189 data->responseInfo->info.payload = newPayload;
2190 memcpy(data->responseInfo->info.payload, payload, payloadLen);
2191 data->responseInfo->info.payloadSize = payloadLen;
2195 // does not occur case
2196 OIC_LOG(ERROR, TAG, "not supported data type");
2197 return CA_NOT_SUPPORTED;
2200 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayload");
2202 return CA_STATUS_OK;
2205 CAPayload_t CAGetPayloadInfo(const CAData_t *data, size_t *payloadLen)
2207 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2208 VERIFY_NON_NULL_RET(payloadLen, TAG, "payloadLen", NULL);
2210 if (data->requestInfo)
2212 if (data->requestInfo->info.payload)
2214 *payloadLen = data->requestInfo->info.payloadSize;
2215 return data->requestInfo->info.payload;
2218 else if (data->responseInfo)
2220 if (data->responseInfo->info.payload)
2222 *payloadLen = data->responseInfo->info.payloadSize;
2223 return data->responseInfo->info.payload;
2230 CAResult_t CAHandleBlockErrorResponse(coap_block_t *block, uint16_t blockType,
2231 uint32_t responseResult)
2233 OIC_LOG(DEBUG, TAG, "IN-HandleBlockErrorRes");
2234 VERIFY_NON_NULL(block, TAG, "block is NULL");
2236 // update block data
2237 switch (responseResult)
2239 case CA_REQUEST_ENTITY_INCOMPLETE:
2242 case CA_REQUEST_ENTITY_TOO_LARGE:
2243 if (COAP_OPTION_BLOCK1 == blockType)
2250 OIC_LOG_V(ERROR, TAG, "there is no Error Code of BWT[%d]", responseResult);
2251 return CA_STATUS_FAILED;
2254 OIC_LOG(DEBUG, TAG, "OUT-HandleBlockErrorRes");
2255 return CA_STATUS_OK;
2258 CAResult_t CAUpdateBlockOptionType(const CABlockDataID_t *blockID, uint8_t blockType)
2260 OIC_LOG(DEBUG, TAG, "IN-UpdateBlockOptionType");
2261 VERIFY_NON_NULL(blockID, TAG, "blockID");
2263 oc_mutex_lock(g_context.blockDataListMutex);
2265 size_t len = u_arraylist_length(g_context.dataList);
2266 for (size_t i = 0; i < len; i++)
2268 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2269 if (CABlockidMatches(currData, blockID))
2271 currData->type = blockType;
2272 oc_mutex_unlock(g_context.blockDataListMutex);
2273 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2274 return CA_STATUS_OK;
2277 oc_mutex_unlock(g_context.blockDataListMutex);
2279 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2280 return CA_STATUS_FAILED;
2283 uint8_t CAGetBlockOptionType(const CABlockDataID_t *blockID)
2285 OIC_LOG(DEBUG, TAG, "IN-GetBlockOptionType");
2286 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", 0);
2288 oc_mutex_lock(g_context.blockDataListMutex);
2290 size_t len = u_arraylist_length(g_context.dataList);
2291 for (size_t i = 0; i < len; i++)
2293 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2294 if (CABlockidMatches(currData, blockID))
2296 oc_mutex_unlock(g_context.blockDataListMutex);
2297 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2298 return currData->type;
2301 oc_mutex_unlock(g_context.blockDataListMutex);
2303 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2307 CAData_t *CAGetDataSetFromBlockDataList(const CABlockDataID_t *blockID)
2309 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2311 oc_mutex_lock(g_context.blockDataListMutex);
2313 size_t len = u_arraylist_length(g_context.dataList);
2314 for (size_t i = 0; i < len; i++)
2316 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2317 if (CABlockidMatches(currData, blockID))
2319 oc_mutex_unlock(g_context.blockDataListMutex);
2320 return currData->sentData;
2323 oc_mutex_unlock(g_context.blockDataListMutex);
2328 CABlockData_t *CAUpdateDataSetFromBlockDataList(const CABlockDataID_t *blockID,
2329 const CAData_t *sendData)
2331 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2332 VERIFY_NON_NULL_RET(sendData, TAG, "sendData", NULL);
2334 oc_mutex_lock(g_context.blockDataListMutex);
2336 size_t len = u_arraylist_length(g_context.dataList);
2337 for (size_t i = 0; i < len; i++)
2339 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2340 if (CABlockidMatches(currData, blockID))
2342 CADestroyDataSet(currData->sentData);
2343 currData->sentData = CACloneCAData(sendData);
2344 oc_mutex_unlock(g_context.blockDataListMutex);
2348 oc_mutex_unlock(g_context.blockDataListMutex);
2353 CAResult_t CAGetTokenFromBlockDataList(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
2354 CAResponseInfo_t *responseInfo)
2356 OIC_LOG(DEBUG, TAG, "IN-CAGetTokenFromBlockDataList");
2357 VERIFY_NON_NULL(pdu, TAG, "pdu");
2358 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
2359 VERIFY_NON_NULL(responseInfo, TAG, "responseInfo");
2361 oc_mutex_lock(g_context.blockDataListMutex);
2363 size_t len = u_arraylist_length(g_context.dataList);
2364 for (size_t i = 0; i < len; i++)
2366 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2367 if (NULL == currData)
2372 if (NULL != currData->sentData && NULL != currData->sentData->requestInfo)
2374 if (pdu->transport_hdr->udp.id == currData->sentData->requestInfo->info.messageId &&
2375 endpoint->adapter == currData->sentData->remoteEndpoint->adapter)
2377 if (NULL != currData->sentData->requestInfo->info.token)
2379 uint8_t length = currData->sentData->requestInfo->info.tokenLength;
2380 responseInfo->info.tokenLength = length;
2381 responseInfo->info.token = (char *) OICMalloc(length);
2382 if (NULL == responseInfo->info.token)
2384 OIC_LOG(ERROR, TAG, "out of memory");
2385 oc_mutex_unlock(g_context.blockDataListMutex);
2386 return CA_MEMORY_ALLOC_FAILED;
2388 memcpy(responseInfo->info.token, currData->sentData->requestInfo->info.token,
2389 responseInfo->info.tokenLength);
2391 oc_mutex_unlock(g_context.blockDataListMutex);
2392 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2393 return CA_STATUS_OK;
2399 oc_mutex_unlock(g_context.blockDataListMutex);
2401 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2402 return CA_STATUS_FAILED;
2405 CAResult_t CACheckBlockDataValidation(const CAData_t *sendData, CABlockData_t **blockData)
2407 VERIFY_NON_NULL(sendData, TAG, "sendData");
2408 VERIFY_NON_NULL(blockData, TAG, "blockData");
2410 if (sendData->responseInfo)
2412 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2413 (CAToken_t)sendData->responseInfo->info.token,
2414 sendData->responseInfo->info.tokenLength,
2415 sendData->remoteEndpoint->port);
2416 if (NULL == blockDataID || blockDataID->idLength < 1)
2418 OIC_LOG(ERROR, TAG, "blockId is null");
2419 CADestroyBlockID(blockDataID);
2420 return CA_STATUS_FAILED;
2423 CABlockData_t *updatedData = CAUpdateDataSetFromBlockDataList(blockDataID, sendData);
2426 OIC_LOG(DEBUG, TAG, "Send response about the received block request.");
2427 *blockData = updatedData;
2428 CADestroyBlockID(blockDataID);
2429 return CA_STATUS_OK;
2431 CADestroyBlockID(blockDataID);
2434 return CA_STATUS_FAILED;
2437 CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID)
2439 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2441 oc_mutex_lock(g_context.blockDataListMutex);
2443 size_t len = u_arraylist_length(g_context.dataList);
2444 for (size_t i = 0; i < len; i++)
2446 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2447 if (CABlockidMatches(currData, blockID))
2449 oc_mutex_unlock(g_context.blockDataListMutex);
2453 oc_mutex_unlock(g_context.blockDataListMutex);
2458 coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID, uint16_t blockType)
2460 OIC_LOG(DEBUG, TAG, "IN-GetBlockOption");
2461 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2463 oc_mutex_lock(g_context.blockDataListMutex);
2465 size_t len = u_arraylist_length(g_context.dataList);
2466 for (size_t i = 0; i < len; i++)
2468 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2469 if (CABlockidMatches(currData, blockID))
2471 oc_mutex_unlock(g_context.blockDataListMutex);
2472 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2473 if (COAP_OPTION_BLOCK2 == blockType)
2475 return &currData->block2;
2477 else if (COAP_OPTION_BLOCK1 == blockType)
2479 return &currData->block1;
2483 oc_mutex_unlock(g_context.blockDataListMutex);
2485 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2489 CAPayload_t CAGetPayloadFromBlockDataList(const CABlockDataID_t *blockID,
2490 size_t *fullPayloadLen)
2492 OIC_LOG(DEBUG, TAG, "IN-GetFullPayload");
2493 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2494 VERIFY_NON_NULL_RET(fullPayloadLen, TAG, "fullPayloadLen", NULL);
2496 oc_mutex_lock(g_context.blockDataListMutex);
2498 size_t len = u_arraylist_length(g_context.dataList);
2499 for (size_t i = 0; i < len; i++)
2501 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2502 if (CABlockidMatches(currData, blockID))
2504 oc_mutex_unlock(g_context.blockDataListMutex);
2505 *fullPayloadLen = currData->receivedPayloadLen;
2506 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2507 return currData->payload;
2510 oc_mutex_unlock(g_context.blockDataListMutex);
2512 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2516 CABlockData_t *CACreateNewBlockData(const CAData_t *sendData)
2518 OIC_LOG(DEBUG, TAG, "IN-CACreateNewBlockData");
2519 VERIFY_NON_NULL_RET(sendData, TAG, "sendData", NULL);
2521 // create block data
2522 CABlockData_t *data = (CABlockData_t *) OICCalloc(1, sizeof(CABlockData_t));
2525 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2529 data->block1.szx = CA_DEFAULT_BLOCK_SIZE;
2530 data->block2.szx = CA_DEFAULT_BLOCK_SIZE;
2531 data->sentData = CACloneCAData(sendData);
2532 if (!data->sentData)
2534 OIC_LOG(ERROR, TAG, PCF("memory alloc has failed"));
2539 CAToken_t token = NULL;
2540 uint8_t tokenLength = 0;
2541 if (data->sentData->requestInfo)
2543 // update token info
2544 tokenLength = data->sentData->requestInfo->info.tokenLength;
2545 token = data->sentData->requestInfo->info.token;
2547 else if (data->sentData->responseInfo)
2549 tokenLength = data->sentData->responseInfo->info.tokenLength;
2550 token = data->sentData->responseInfo->info.token;
2553 if (!data->sentData->remoteEndpoint)
2555 OIC_LOG(ERROR, TAG, "remoteEndpoint is null");
2556 CADestroyDataSet(data->sentData);
2561 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(token, tokenLength,
2562 data->sentData->remoteEndpoint->port);
2563 if (NULL == blockDataID || blockDataID->idLength < 1)
2565 OIC_LOG(ERROR, TAG, "blockId is null");
2566 CADestroyBlockID(blockDataID);
2567 CADestroyDataSet(data->sentData);
2571 data->blockDataId = blockDataID;
2572 uint64_t now = OICGetCurrentTime(TIME_IN_US);
2573 data->ttl = now + (BLOCK_DATA_TIMEOUT_SECONDS * USECS_PER_SEC);
2575 oc_mutex_lock(g_context.blockDataListMutex);
2577 bool res = u_arraylist_add(g_context.dataList, (void *) data);
2580 OIC_LOG(ERROR, TAG, "add has failed");
2581 CADestroyBlockID(data->blockDataId);
2582 CADestroyDataSet(data->sentData);
2584 oc_mutex_unlock(g_context.blockDataListMutex);
2587 oc_mutex_unlock(g_context.blockDataListMutex);
2589 OIC_LOG(DEBUG, TAG, "OUT-CreateBlockData");
2593 CAResult_t CARemoveBlockDataFromList(const CABlockDataID_t *blockID)
2595 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
2596 VERIFY_NON_NULL(blockID, TAG, "blockID");
2598 oc_mutex_lock(g_context.blockDataListMutex);
2600 size_t len = u_arraylist_length(g_context.dataList);
2601 for (size_t i = 0; i < len; i++)
2603 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2604 if (CABlockidMatches(currData, blockID))
2606 CABlockData_t *removedData = u_arraylist_remove(g_context.dataList, i);
2609 OIC_LOG(ERROR, TAG, "data is NULL");
2610 oc_mutex_unlock(g_context.blockDataListMutex);
2611 return CA_STATUS_FAILED;
2614 OIC_LOG(DEBUG, TAG, "Removed BlockID is ");
2615 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) removedData->blockDataId->id,
2616 removedData->blockDataId->idLength);
2619 CADestroyDataSet(removedData->sentData);
2620 CADestroyBlockID(removedData->blockDataId);
2621 OICFree(removedData->payload);
2622 OICFree(removedData);
2623 oc_mutex_unlock(g_context.blockDataListMutex);
2624 return CA_STATUS_OK;
2627 oc_mutex_unlock(g_context.blockDataListMutex);
2628 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
2629 return CA_STATUS_OK;
2632 CAResult_t CARemoveAllBlockDataFromList()
2634 OIC_LOG(DEBUG, TAG, "CARemoveAllBlockDataFromList");
2636 oc_mutex_lock(g_context.blockDataListMutex);
2638 size_t len = u_arraylist_length(g_context.dataList);
2639 for (size_t i = len; i > 0; i--)
2641 CABlockData_t *removedData = u_arraylist_remove(g_context.dataList, i - 1);
2645 if (removedData->sentData)
2647 CADestroyDataSet(removedData->sentData);
2649 CADestroyBlockID(removedData->blockDataId);
2650 OICFree(removedData->payload);
2651 OICFree(removedData);
2654 oc_mutex_unlock(g_context.blockDataListMutex);
2656 return CA_STATUS_OK;
2659 void CADestroyDataSet(CAData_t* data)
2661 VERIFY_NON_NULL_VOID(data, TAG, "data");
2663 if (data->remoteEndpoint)
2665 CAFreeEndpoint(data->remoteEndpoint);
2666 data->remoteEndpoint = NULL;
2668 if (data->requestInfo)
2670 CADestroyRequestInfoInternal(data->requestInfo);
2671 data->requestInfo = NULL;
2673 if (data->responseInfo)
2675 CADestroyResponseInfoInternal(data->responseInfo);
2676 data->responseInfo = NULL;
2681 CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength,
2682 uint16_t portNumber)
2684 char port[PORT_LENGTH] = { 0, };
2685 port[0] = (char) ((portNumber >> 8) & 0xFF);
2686 port[1] = (char) (portNumber & 0xFF);
2688 CABlockDataID_t* blockDataID = (CABlockDataID_t *) OICMalloc(sizeof(CABlockDataID_t));
2691 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2694 blockDataID->idLength = tokenLength + sizeof(port);
2695 blockDataID->id = (uint8_t *) OICMalloc(blockDataID->idLength);
2696 if (!blockDataID->id)
2698 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2699 OICFree(blockDataID);
2705 memcpy(blockDataID->id, token, tokenLength);
2708 memcpy(blockDataID->id + tokenLength, port, sizeof(port));
2710 OIC_LOG(DEBUG, TAG, "BlockID is ");
2711 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)blockDataID->id, blockDataID->idLength);
2716 void CADestroyBlockID(CABlockDataID_t *blockID)
2718 VERIFY_NON_NULL_VOID(blockID, TAG, "blockID");
2719 OICFree(blockID->id);
2724 bool CABlockidMatches(const CABlockData_t *currData, const CABlockDataID_t *blockID)
2726 VERIFY_NON_NULL_RET(currData, TAG, "currData", false);
2727 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", false);
2728 VERIFY_NON_NULL_RET(blockID->id, TAG, "blockID->id", false);
2730 if ((currData->blockDataId)
2731 && (currData->blockDataId->id)
2732 && (currData->blockDataId->idLength == blockID->idLength)
2733 && !memcmp(currData->blockDataId->id, blockID->id, currData->blockDataId->idLength))
2740 void CALogBlockInfo(coap_block_t *block)
2742 VERIFY_NON_NULL_VOID(block, TAG, "block");
2744 OIC_LOG(DEBUG, TAG, "block option info");
2746 OIC_LOG_V(DEBUG, TAG, "block option-num : %d", block->num);
2748 OIC_LOG_V(DEBUG, TAG, "block option-m : %d", block->m);
2750 OIC_LOG_V(DEBUG, TAG, "block option-szx : %d", block->szx);
2753 CAResult_t CARemoveBlockDataFromListWithSeed(const CAToken_t token, uint8_t tokenLength,
2754 uint16_t portNumber)
2756 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(token, tokenLength, portNumber);
2757 if (NULL == blockDataID || blockDataID->idLength < 1)
2759 OIC_LOG(ERROR, TAG, "blockId is null");
2760 CADestroyBlockID(blockDataID);
2761 return CA_STATUS_FAILED;
2764 CAResult_t res = CARemoveBlockDataFromList(blockDataID);
2765 if (CA_STATUS_OK != res)
2767 OIC_LOG(ERROR, TAG, "CARemoveBlockDataFromList failed");
2770 CADestroyBlockID(blockDataID);
2774 void CAResetBlockDataTTL(const CABlockDataID_t *blockID)
2776 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
2778 oc_mutex_lock(g_context.blockDataListMutex);
2779 size_t len = u_arraylist_length(g_context.dataList);
2780 for (size_t i = 0; i < len; i++)
2782 CABlockData_t *blockData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2783 if (CABlockidMatches(blockData, blockID))
2785 uint64_t now = OICGetCurrentTime(TIME_IN_US);
2786 blockData->ttl = now + (BLOCK_DATA_TIMEOUT_SECONDS * USECS_PER_SEC);
2787 oc_mutex_unlock(g_context.blockDataListMutex);
2788 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
2792 oc_mutex_unlock(g_context.blockDataListMutex);
2793 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
2796 void CACheckAndDeleteTimedOutBlockData()
2798 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
2800 uint64_t now = OICGetCurrentTime(TIME_IN_US);
2802 oc_mutex_lock(g_context.blockDataListMutex);
2803 for (size_t i = 0; i < u_arraylist_length(g_context.dataList); i++)
2805 CABlockData_t *blockData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2806 if (blockData && blockData->ttl < now)
2808 OIC_LOG(INFO, TAG, "Deleting timed-out BlockData");
2809 OIC_LOG(DEBUG, TAG, "BlockID is ");
2810 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) blockData->blockDataId->id,
2811 blockData->blockDataId->idLength);
2813 blockData = (CABlockData_t *) u_arraylist_remove(g_context.dataList, i);
2817 CADestroyDataSet(blockData->sentData);
2818 CADestroyBlockID(blockData->blockDataId);
2819 OICFree(blockData->payload);
2824 oc_mutex_unlock(g_context.blockDataListMutex);
2825 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);