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"
48 #define BLOCKWISE_OPTION_BUFFER (sizeof(unsigned int))
49 #define BLOCK_NUMBER_IDX 4
50 #define BLOCK_M_BIT_IDX 3
53 #define BLOCK_SIZE(arg) (1 << ((arg) + 4))
55 // context for block-wise transfer
56 static CABlockWiseContext_t g_context = { 0 };
58 static bool CACheckPayloadLength(const CAData_t *sendData)
60 size_t payloadLen = 0;
61 CAGetPayloadInfo(sendData, &payloadLen);
63 // check if message has to be transfered to a block
64 size_t maxBlockSize = BLOCK_SIZE(CA_DEFAULT_BLOCK_SIZE);
65 OIC_LOG_V(DEBUG, TAG, "payloadLen=%d, maxBlockSize=%d", payloadLen, maxBlockSize);
67 if (payloadLen <= maxBlockSize)
75 CAResult_t CAInitializeBlockWiseTransfer(CASendThreadFunc sendThreadFunc,
76 CAReceiveThreadFunc receivedThreadFunc)
78 OIC_LOG(DEBUG, TAG, "initialize");
80 // set block-wise transfer context
81 if (!g_context.sendThreadFunc)
83 g_context.sendThreadFunc = sendThreadFunc;
86 if (!g_context.receivedThreadFunc)
88 g_context.receivedThreadFunc = receivedThreadFunc;
91 if (!g_context.dataList)
93 g_context.dataList = u_arraylist_create();
96 CAResult_t res = CAInitBlockWiseMutexVariables();
97 if (CA_STATUS_OK != res)
99 OIC_LOG(ERROR, TAG, "init has failed");
105 CAResult_t CATerminateBlockWiseTransfer()
107 OIC_LOG(DEBUG, TAG, "terminate");
109 if (g_context.dataList)
111 u_arraylist_free(&g_context.dataList);
114 CATerminateBlockWiseMutexVariables();
119 CAResult_t CAInitBlockWiseMutexVariables()
121 if (!g_context.blockDataListMutex)
123 g_context.blockDataListMutex = ca_mutex_new();
124 if (!g_context.blockDataListMutex)
126 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
127 return CA_STATUS_FAILED;
131 if (!g_context.blockDataSenderMutex)
133 g_context.blockDataSenderMutex = ca_mutex_new();
134 if (!g_context.blockDataSenderMutex)
136 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
137 CATerminateBlockWiseMutexVariables();
138 return CA_STATUS_FAILED;
145 void CATerminateBlockWiseMutexVariables()
147 if (g_context.blockDataListMutex)
149 ca_mutex_free(g_context.blockDataListMutex);
150 g_context.blockDataListMutex = NULL;
153 if (g_context.blockDataSenderMutex)
155 ca_mutex_free(g_context.blockDataSenderMutex);
156 g_context.blockDataSenderMutex = NULL;
160 CAResult_t CASendBlockWiseData(const CAData_t *sendData)
162 VERIFY_NON_NULL(sendData, TAG, "sendData");
164 // check if message type is CA_MSG_RESET
165 if (sendData->requestInfo)
167 if (CA_MSG_RESET == sendData->requestInfo->info.type)
169 OIC_LOG(DEBUG, TAG, "reset message can't be sent to the block");
170 return CA_NOT_SUPPORTED;
173 else if (sendData->responseInfo)
175 if (CA_MSG_RESET == sendData->responseInfo->info.type)
177 OIC_LOG(DEBUG, TAG, "reset message can't be sent to the block");
178 return CA_NOT_SUPPORTED;
182 // #1. check if it is already included in block data list
183 CABlockData_t *currData = NULL;
184 CAResult_t res = CACheckBlockDataValidation(sendData, &currData);
185 if (CA_STATUS_OK != res)
187 // #2. if it is not included, add the data into list
190 OIC_LOG(DEBUG, TAG, "There is no block data");
192 bool isBlock = CACheckPayloadLength(sendData);
195 if (sendData->requestInfo)
197 currData = CACreateNewBlockData(sendData);
200 OIC_LOG(ERROR, TAG, "failed to create block data");
201 return CA_MEMORY_ALLOC_FAILED;
204 return CA_NOT_SUPPORTED;
206 currData = CACreateNewBlockData(sendData);
209 OIC_LOG(ERROR, TAG, "failed to create block data");
210 return CA_MEMORY_ALLOC_FAILED;
215 // #3. check request/response block option type and payload length
216 res = CACheckBlockOptionType(currData);
217 if (CA_STATUS_OK == res)
219 // #4. send block message
220 OIC_LOG(DEBUG, TAG, "send first block msg");
221 res = CAAddSendThreadQueue(currData->sentData,
222 (const CABlockDataID_t *)&currData->blockDataId);
223 if (CA_STATUS_OK != res)
225 OIC_LOG(ERROR, TAG, "add has failed");
233 CAResult_t CAAddSendThreadQueue(const CAData_t *sendData, const CABlockDataID_t *blockID)
235 VERIFY_NON_NULL(sendData, TAG, "sendData");
236 VERIFY_NON_NULL(blockID, TAG, "blockID");
238 CAData_t *cloneData = CACloneCAData(sendData);
241 OIC_LOG(ERROR, TAG, "clone has failed");
242 CARemoveBlockDataFromList(blockID);
243 return CA_STATUS_FAILED;
246 if (g_context.sendThreadFunc)
248 ca_mutex_lock(g_context.blockDataSenderMutex);
249 g_context.sendThreadFunc(cloneData);
250 ca_mutex_unlock(g_context.blockDataSenderMutex);
254 CADestroyDataSet(cloneData);
259 CAResult_t CACheckBlockOptionType(CABlockData_t *currData)
261 VERIFY_NON_NULL(currData, TAG, "currData");
262 VERIFY_NON_NULL(currData->sentData, TAG, "currData->sentData");
264 bool isBlock = CACheckPayloadLength(currData->sentData);
267 return CA_NOT_SUPPORTED;
270 // set block option (COAP_OPTION_BLOCK2 or COAP_OPTION_BLOCK1)
271 if (currData->sentData->requestInfo) // request message
273 currData->type = COAP_OPTION_BLOCK1;
275 else // response message
277 currData->type = COAP_OPTION_BLOCK2;
283 // TODO make pdu const after libcoap is updated to support that.
284 CAResult_t CAReceiveBlockWiseData(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
285 const CAData_t *receivedData, size_t dataLen)
287 OIC_LOG(DEBUG, TAG, "CAReceiveBlockWiseData");
288 VERIFY_NON_NULL(pdu, TAG, "pdu");
289 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
290 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
291 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
293 // check if received message type is CA_MSG_RESET
294 if (CA_EMPTY == pdu->hdr->coap_hdr_udp_t.code)
296 OIC_LOG(DEBUG, TAG, "code is CA_EMPTY..");
298 // get token from block-wise transfer list when CA_EMPTY(RST/ACK) is received
299 CAResult_t res = CAGetTokenFromBlockDataList(pdu, endpoint, receivedData->responseInfo);
300 if (CA_STATUS_OK != res)
302 OIC_LOG(ERROR, TAG, "fail to get token");
306 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
307 receivedData->responseInfo->info.token,
308 receivedData->responseInfo->info.tokenLength,
310 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
312 OIC_LOG(ERROR, TAG, "blockId is null");
313 CADestroyBlockID(blockDataID);
314 return CA_STATUS_FAILED;
317 CARemoveBlockDataFromList(blockDataID);
318 CADestroyBlockID(blockDataID);
319 return CA_NOT_SUPPORTED;
322 // check if block option is set and get block data
323 coap_block_t block = {0, 0, 0};
326 int isBlock1 = coap_get_block(pdu, COAP_OPTION_BLOCK1, &block);
329 CAResult_t res = CASetNextBlockOption1(pdu, endpoint, receivedData, block, dataLen);
330 if (CA_STATUS_OK != res)
332 OIC_LOG(ERROR, TAG, "setting has failed");
338 int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
341 CAResult_t res = CASetNextBlockOption2(pdu, endpoint, receivedData, block, dataLen);
342 if (CA_STATUS_OK != res)
344 OIC_LOG(ERROR, TAG, "setting has failed");
349 // check if there is error code
350 if (!isBlock1 && !isBlock2)
352 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code);
353 if (CA_REQUEST_ENTITY_INCOMPLETE == code)
355 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
356 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
357 pdu->hdr->coap_hdr_udp_t.token_length,
360 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
362 OIC_LOG(ERROR, TAG, "blockId is null");
363 CADestroyBlockID(blockDataID);
364 return CA_STATUS_FAILED;
367 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
370 OIC_LOG(ERROR, TAG, "getting has failed");
371 CADestroyBlockID(blockDataID);
372 return CA_STATUS_FAILED;
375 if (COAP_OPTION_BLOCK2 == data->type)
377 coap_block_t *block2 = CAGetBlockOption(blockDataID, COAP_OPTION_BLOCK2);
380 OIC_LOG(ERROR, TAG, "block is null");
381 CADestroyBlockID(blockDataID);
382 return CA_STATUS_FAILED;
385 CAResult_t res = CASetNextBlockOption2(pdu, endpoint, receivedData, *block2,
387 if (CA_STATUS_OK != res)
389 OIC_LOG(ERROR, TAG, "setting has failed");
390 CADestroyBlockID(blockDataID);
394 else if (COAP_OPTION_BLOCK1 == data->type)
396 coap_block_t *block1 = CAGetBlockOption(blockDataID, COAP_OPTION_BLOCK1);
399 OIC_LOG(ERROR, TAG, "block is null");
400 CADestroyBlockID(blockDataID);
401 return CA_STATUS_FAILED;
404 CAResult_t res = CASetNextBlockOption1(pdu, endpoint, receivedData, *block1,
406 if (CA_STATUS_OK != res)
408 OIC_LOG(ERROR, TAG, "setting has failed");
409 CADestroyBlockID(blockDataID);
417 OIC_LOG(DEBUG, TAG, "it's normal pdu");
419 // if received data is response message
420 // and sent data remain in block data list, remove block data
421 if (receivedData->responseInfo)
423 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
424 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
425 pdu->hdr->coap_hdr_udp_t.token_length,
427 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
429 OIC_LOG(ERROR, TAG, "blockId is null");
430 CADestroyBlockID(blockDataID);
431 return CA_STATUS_FAILED;
434 CARemoveBlockDataFromList(blockDataID);
435 CADestroyBlockID(blockDataID);
437 return CA_NOT_SUPPORTED;
444 CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData,
445 uint8_t blockWiseStatus, const CABlockDataID_t *blockID)
447 VERIFY_NON_NULL(pdu, TAG, "pdu");
448 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
449 VERIFY_NON_NULL(blockID, TAG, "blockID");
451 CAResult_t res = CA_STATUS_OK;
452 CAData_t *data = NULL;
454 // process blockWiseStatus
455 switch (blockWiseStatus)
457 case CA_OPTION2_FIRST_BLOCK:
458 res = CAAddSendThreadQueue(receivedData, blockID);
459 if (CA_STATUS_OK != res)
461 OIC_LOG(ERROR, TAG, "add has failed");
467 // add data to send thread
468 data = CAGetDataSetFromBlockDataList(blockID);
471 OIC_LOG(ERROR, TAG, "it's unavailable");
472 return CA_STATUS_FAILED;
475 if (data->requestInfo)
477 data->requestInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
480 if (data->responseInfo)
482 data->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
485 res = CAAddSendThreadQueue(data, blockID);
486 if (CA_STATUS_OK != res)
488 OIC_LOG(ERROR, TAG, "add has failed");
496 case CA_SENT_PREVIOUS_NON_MSG:
497 res = CASendBlockMessage(pdu, CA_MSG_CONFIRM, blockWiseStatus,
499 if (CA_STATUS_OK != res)
501 OIC_LOG(ERROR, TAG, "send has failed");
506 case CA_OPTION2_LAST_BLOCK:
507 // process last block and send upper layer
508 res = CAReceiveLastBlock(blockID, receivedData);
509 if (CA_STATUS_OK != res)
511 OIC_LOG(ERROR, TAG, "receive has failed");
515 // remove data from list
516 res = CARemoveBlockDataFromList(blockID);
517 if (CA_STATUS_OK != res)
519 OIC_LOG(ERROR, TAG, "remove has failed");
524 case CA_OPTION1_NO_ACK_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 if (CA_MSG_NONCONFIRM == pdu->hdr->coap_hdr_udp_t.type)
535 // remove data from list
536 res = CARemoveBlockDataFromList(blockID);
537 if (CA_STATUS_OK != res)
539 OIC_LOG(ERROR, TAG, "remove has failed");
545 case CA_OPTION1_NO_ACK_BLOCK:
546 if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type)
548 // add data to send thread
549 res = CASendBlockMessage(pdu, CA_MSG_ACKNOWLEDGE, blockWiseStatus,
551 if (CA_STATUS_OK != res)
553 OIC_LOG(ERROR, TAG, "send has failed");
559 case CA_BLOCK_INCOMPLETE:
560 if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type ||
561 CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type)
563 // add data to send thread
564 res = CASendErrorMessage(pdu, blockWiseStatus,
565 CA_REQUEST_ENTITY_INCOMPLETE,
567 if (CA_STATUS_OK != res)
569 OIC_LOG(ERROR, TAG, "send has failed");
575 case CA_BLOCK_TOO_LARGE:
576 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type)
578 res = CASendBlockMessage(pdu, CA_MSG_CONFIRM, blockWiseStatus,
580 if (CA_STATUS_OK != res)
582 OIC_LOG(ERROR, TAG, "send has failed");
586 else if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type)
588 res = CASendErrorMessage(pdu, blockWiseStatus,
589 CA_REQUEST_ENTITY_TOO_LARGE,
591 if (CA_STATUS_OK != res)
593 OIC_LOG(ERROR, TAG, "send has failed");
599 OIC_LOG_V(ERROR, TAG, "no logic [%d]", blockWiseStatus);
604 CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType,
605 uint8_t status, const CABlockDataID_t *blockID)
607 VERIFY_NON_NULL(pdu, TAG, "pdu");
608 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
609 VERIFY_NON_NULL(blockID, TAG, "blockID");
611 CAData_t *data = CAGetDataSetFromBlockDataList(blockID);
614 OIC_LOG(ERROR, TAG, "CAData is unavailable");
615 return CA_STATUS_FAILED;
618 if (CA_MSG_CONFIRM == msgType)
620 OIC_LOG(DEBUG, TAG, "need new msgID");
621 if (data->requestInfo)
623 data->requestInfo->info.messageId = 0;
626 if (data->responseInfo)
628 data->responseInfo->info.messageId = 0;
631 else if (CA_MSG_ACKNOWLEDGE == msgType)
633 if (data->responseInfo)
635 OIC_LOG(DEBUG, TAG, "set ACK message");
636 data->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
637 data->responseInfo->info.type = CA_MSG_ACKNOWLEDGE;
638 if (CA_OPTION1_NO_ACK_LAST_BLOCK == status)
640 data->responseInfo->result = CA_CHANGED;
642 else if (CA_OPTION1_NO_ACK_BLOCK == status)
644 data->responseInfo->result = CA_CONTINUE;
649 // add data to send thread
650 CAResult_t res = CAAddSendThreadQueue(data, blockID);
651 if (CA_STATUS_OK != res)
653 OIC_LOG(ERROR, TAG, "add has failed");
659 CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status,
660 CAResponseResult_t responseResult,
661 const CABlockDataID_t *blockID)
663 VERIFY_NON_NULL(pdu, TAG, "pdu");
664 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
665 VERIFY_NON_NULL(blockID, TAG, "blockID");
667 // create error responseInfo
668 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockID);
671 OIC_LOG(ERROR, TAG, "data is unavailable");
672 return CA_STATUS_FAILED;
675 CAData_t *cloneData = NULL;
676 if (data->sentData && data->sentData->responseInfo)
678 data->sentData->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
679 data->sentData->responseInfo->info.type = CA_MSG_ACKNOWLEDGE;
680 data->sentData->responseInfo->result = responseResult;
681 cloneData = CACloneCAData(data->sentData);
684 OIC_LOG(ERROR, TAG, "clone has failed");
685 return CA_MEMORY_ALLOC_FAILED;
687 OIC_LOG(DEBUG, TAG, "set ACK message");
689 else if (data->sentData)
691 cloneData = CACreateNewDataSet(pdu, data->sentData->remoteEndpoint);
694 OIC_LOG(ERROR, TAG, PCF("CACreateNewDataSet failed"));
695 return CA_MEMORY_ALLOC_FAILED;
698 cloneData->responseInfo->info.type = CA_MSG_CONFIRM;
699 cloneData->responseInfo->result = responseResult;
700 OIC_LOG(DEBUG, TAG, "set CON message");
704 OIC_LOG(ERROR, TAG, "data has no sent-data");
705 return CA_MEMORY_ALLOC_FAILED;
708 // add data to send thread
709 if (g_context.sendThreadFunc)
711 ca_mutex_lock(g_context.blockDataSenderMutex);
712 g_context.sendThreadFunc(cloneData);
713 ca_mutex_unlock(g_context.blockDataSenderMutex);
717 CADestroyDataSet(cloneData);
720 // if error code is 4.08, remove the stored payload and initialize block number
721 if (CA_BLOCK_INCOMPLETE == status)
723 OICFree(data->payload);
724 data->payload = NULL;
725 data->payloadLength = 0;
726 data->receivedPayloadLen = 0;
727 data->block1.num = 0;
728 data->block2.num = 0;
734 CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID,
735 const CAData_t *receivedData)
737 VERIFY_NON_NULL(blockID, TAG, "blockID");
738 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
740 // total block data have to notify to Application
741 CAData_t *cloneData = CACloneCAData(receivedData);
744 OIC_LOG(ERROR, TAG, "clone has failed");
745 return CA_MEMORY_ALLOC_FAILED;
749 size_t fullPayloadLen = 0;
750 CAPayload_t fullPayload = CAGetPayloadFromBlockDataList(blockID,
754 CAResult_t res = CAUpdatePayloadToCAData(cloneData, fullPayload, fullPayloadLen);
755 if (CA_STATUS_OK != res)
757 OIC_LOG(ERROR, TAG, "update has failed");
758 CADestroyDataSet(cloneData);
763 if (g_context.receivedThreadFunc)
765 g_context.receivedThreadFunc(cloneData);
769 CADestroyDataSet(cloneData);
775 // TODO make pdu const after libcoap is updated to support that.
776 CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
777 const CAData_t *receivedData, coap_block_t block,
780 OIC_LOG(INFO, TAG, "CASetNextBlockOption1");
781 VERIFY_NON_NULL(pdu, TAG, "pdu");
782 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
783 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
784 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
786 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
788 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
789 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
790 pdu->hdr->coap_hdr_udp_t.token_length,
793 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
795 OIC_LOG(ERROR, TAG, "blockId is null");
796 CADestroyBlockID(blockDataID);
797 return CA_STATUS_FAILED;
800 // BlockData data is created if it not existed
801 if (!CAIsBlockDataInList(blockDataID))
803 OIC_LOG(DEBUG, TAG, "no message in list");
805 CAData_t *data = CACreateNewDataSet(pdu, endpoint);
808 OIC_LOG(ERROR, TAG, "data is null");
809 CADestroyBlockID(blockDataID);
810 return CA_STATUS_FAILED;
813 CABlockData_t *currData = CACreateNewBlockData(data);
816 OIC_LOG(ERROR, TAG, "currData is null");
817 CADestroyDataSet(data);
818 CADestroyBlockID(blockDataID);
819 return CA_STATUS_FAILED;
821 CADestroyDataSet(data);
824 // update BLOCK OPTION1 type
825 CAResult_t res = CAUpdateBlockOptionType(blockDataID,
827 if (CA_STATUS_OK != res)
829 OIC_LOG(ERROR, TAG, "update has failed");
830 CARemoveBlockDataFromList(blockDataID);
831 CADestroyBlockID(blockDataID);
835 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
838 OIC_LOG(ERROR, TAG, "getting has failed");
839 CADestroyBlockID(blockDataID);
840 return CA_STATUS_FAILED;
843 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
844 // received type from remote device
845 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type)
847 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code);
849 (CA_REQUEST_ENTITY_INCOMPLETE != code && CA_REQUEST_ENTITY_TOO_LARGE != code))
851 int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
854 OIC_LOG(INFO, TAG, "received data is combining block1 and block2");
855 // initialize block number for response message
856 data->block1.num = 0;
857 CADestroyBlockID(blockDataID);
862 OIC_LOG(INFO, TAG, "received data is not bulk data");
863 CAReceiveLastBlock(blockDataID, receivedData);
864 CARemoveBlockDataFromList(blockDataID);
865 CADestroyBlockID(blockDataID);
870 blockWiseStatus = CA_OPTION1_ACK;
871 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus);
872 if (CA_STATUS_OK != res)
874 OIC_LOG(ERROR, TAG, "update has failed");
875 CADestroyBlockID(blockDataID);
879 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
880 if (CA_STATUS_OK != res)
882 OIC_LOG(ERROR, TAG, "update has failed");
883 CARemoveBlockDataFromList(blockDataID);
884 CADestroyBlockID(blockDataID);
888 else // CON or NON message
890 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
892 // check the size option
893 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
895 &(data->payloadLength));
897 // check if received payload is exact
898 if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type)
900 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
901 COAP_OPTION_BLOCK1, dataLen);
904 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
906 // store the received payload and merge
907 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
908 isSizeOption, COAP_OPTION_BLOCK1);
909 if (CA_STATUS_OK != res)
911 OIC_LOG(ERROR, TAG, "update has failed");
912 CARemoveBlockDataFromList(blockDataID);
913 CADestroyBlockID(blockDataID);
917 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus);
918 if (CA_STATUS_OK != res)
920 OIC_LOG(ERROR, TAG, "update has failed");
921 CARemoveBlockDataFromList(blockDataID);
922 CADestroyBlockID(blockDataID);
927 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
928 if (CA_STATUS_OK != res)
930 OIC_LOG(ERROR, TAG, "update has failed");
931 CARemoveBlockDataFromList(blockDataID);
932 CADestroyBlockID(blockDataID);
937 // check the blcok-wise transfer status for next step
938 if (CA_BLOCK_UNKNOWN == blockWiseStatus || CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
940 if (0 == block.m) // Last block is received
942 OIC_LOG(DEBUG, TAG, "M bit is 0");
943 blockWiseStatus = CA_OPTION1_NO_ACK_LAST_BLOCK;
947 OIC_LOG(DEBUG, TAG, "M bit is 1");
948 blockWiseStatus = CA_OPTION1_NO_ACK_BLOCK;
953 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
954 if (CA_STATUS_OK != res)
956 OIC_LOG(ERROR, TAG, "setting has failed");
957 CARemoveBlockDataFromList(blockDataID);
960 CADestroyBlockID(blockDataID);
964 // TODO make pdu const after libcoap is updated to support that.
965 CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
966 const CAData_t *receivedData, coap_block_t block,
969 OIC_LOG(DEBUG, TAG, "CASetNextBlockOption2");
970 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
972 VERIFY_NON_NULL(pdu, TAG, "pdu");
973 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
974 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
975 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
977 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
978 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
979 pdu->hdr->coap_hdr_udp_t.token_length,
982 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
984 OIC_LOG(ERROR, TAG, "blockId is null");
985 CADestroyBlockID(blockDataID);
986 return CA_STATUS_FAILED;
989 // BlockData data is created if it not existed
990 if (!CAIsBlockDataInList(blockDataID))
992 OIC_LOG(DEBUG, TAG, "no msg in list.");
994 CAData_t *data = CACreateNewDataSet(pdu, endpoint);
997 OIC_LOG(ERROR, TAG, "data is null");
998 CADestroyBlockID(blockDataID);
999 return CA_STATUS_FAILED;
1002 CABlockData_t *currData = CACreateNewBlockData(data);
1005 OIC_LOG(ERROR, TAG, "data is null");
1006 CADestroyDataSet(data);
1007 CADestroyBlockID(blockDataID);
1008 return CA_STATUS_FAILED;
1010 CADestroyDataSet(data);
1013 // set Block Option Type
1014 CAResult_t res = CAUpdateBlockOptionType(blockDataID,
1015 COAP_OPTION_BLOCK2);
1016 if (CA_STATUS_OK != res)
1018 OIC_LOG(ERROR, TAG, "update has failed");
1019 CARemoveBlockDataFromList(blockDataID);
1020 CADestroyBlockID(blockDataID);
1024 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
1027 OIC_LOG(ERROR, TAG, "getting has failed");
1028 CARemoveBlockDataFromList(blockDataID);
1029 CADestroyBlockID(blockDataID);
1030 return CA_STATUS_FAILED;
1033 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
1034 if (0 == block.num && CA_GET == pdu->hdr->coap_hdr_udp_t.code && 0 == block.m)
1036 OIC_LOG(INFO, TAG, "first block number");
1038 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1039 if (CA_STATUS_OK != res)
1041 OIC_LOG(ERROR, TAG, "update has failed");
1042 CARemoveBlockDataFromList(blockDataID);
1043 CADestroyBlockID(blockDataID);
1047 // first block data have to notify to Application
1048 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1049 if (CA_STATUS_OK != res)
1051 OIC_LOG(ERROR, TAG, "update has failed");
1052 CARemoveBlockDataFromList(blockDataID);
1053 CADestroyBlockID(blockDataID);
1056 blockWiseStatus = CA_OPTION2_FIRST_BLOCK;
1060 // received type from remote device
1061 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type ||
1062 (CA_MSG_NONCONFIRM == pdu->hdr->coap_hdr_udp_t.type &&
1063 NULL != receivedData->responseInfo))
1065 OIC_LOG(DEBUG, TAG, "received ACK or NON");
1067 // check the size option
1068 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
1070 &(data->payloadLength));
1072 // check if received payload is exact
1073 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type)
1075 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
1076 COAP_OPTION_BLOCK2, dataLen);
1079 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
1081 // store the received payload and merge
1082 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
1083 isSizeOption, COAP_OPTION_BLOCK2);
1084 if (CA_STATUS_OK != res)
1086 OIC_LOG(ERROR, TAG, "update has failed");
1087 CARemoveBlockDataFromList(blockDataID);
1088 CADestroyBlockID(blockDataID);
1093 if (0 == block.m && CA_BLOCK_UNKNOWN == blockWiseStatus) // Last block is received
1095 OIC_LOG(DEBUG, TAG, "M bit is 0");
1096 blockWiseStatus = CA_OPTION2_LAST_BLOCK;
1100 if (CA_BLOCK_UNKNOWN == blockWiseStatus ||
1101 CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
1103 OIC_LOG(DEBUG, TAG, "M bit is 1");
1105 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type)
1107 blockWiseStatus = CA_OPTION2_ACK;
1111 blockWiseStatus = CA_OPTION2_NON;
1115 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2,
1117 if (CA_STATUS_OK != res)
1119 OIC_LOG(ERROR, TAG, "update has failed");
1120 CARemoveBlockDataFromList(blockDataID);
1121 CADestroyBlockID(blockDataID);
1125 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1126 if (CA_STATUS_OK != res)
1128 OIC_LOG(ERROR, TAG, "update has failed");
1129 CARemoveBlockDataFromList(blockDataID);
1130 CADestroyBlockID(blockDataID);
1135 else // CON message and so on.
1137 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
1139 blockWiseStatus = CA_OPTION2_CON;
1141 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1142 if (CA_STATUS_OK != res)
1144 OIC_LOG(ERROR, TAG, "update has failed");
1145 CARemoveBlockDataFromList(blockDataID);
1146 CADestroyBlockID(blockDataID);
1150 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1151 if (CA_STATUS_OK != res)
1153 OIC_LOG(ERROR, TAG, "update has failed");
1154 CARemoveBlockDataFromList(blockDataID);
1155 CADestroyBlockID(blockDataID);
1161 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
1162 if (CA_STATUS_OK != res)
1164 OIC_LOG(ERROR, TAG, "setting has failed");
1165 CARemoveBlockDataFromList(blockDataID);
1166 CADestroyBlockID(blockDataID);
1170 CADestroyBlockID(blockDataID);
1171 return CA_STATUS_OK;
1174 CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu,
1175 coap_block_t *block, uint16_t blockType,
1178 VERIFY_NON_NULL(currData, TAG, "currData");
1179 VERIFY_NON_NULL(pdu, TAG, "pdu");
1180 VERIFY_NON_NULL(block, TAG, "block");
1182 // update block data
1183 CAResult_t res = CA_STATUS_OK;
1184 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code);
1186 if (CA_REQUEST_ENTITY_INCOMPLETE == code || CA_REQUEST_ENTITY_TOO_LARGE == code)
1188 // response error code of the received block message
1189 res = CAHandleBlockErrorResponse(block, blockType, code);
1190 if (CA_STATUS_OK != res)
1192 OIC_LOG(ERROR, TAG, "error handle has failed");
1198 // update block option items
1201 case CA_OPTION1_ACK:
1202 if (currData->block1.num > block->num)
1204 OIC_LOG(ERROR, TAG, "received incorrect block num");
1205 return CA_STATUS_FAILED;
1209 case CA_OPTION2_NON:
1213 case CA_OPTION2_CON:
1216 case CA_OPTION2_ACK:
1217 if (currData->block2.num > block->num)
1219 OIC_LOG(ERROR, TAG, "received incorrect block num");
1220 return CA_STATUS_FAILED;
1225 case CA_BLOCK_TOO_LARGE:
1226 // if state of received block message is CA_BLOCK_TOO_LARGE or CA_BLOCK_INCOMPLETE
1227 // we set the response error code appropriately and send
1228 if (COAP_OPTION_BLOCK2 == blockType)
1232 block->szx = currData->block2.szx;
1236 block->szx = currData->block1.szx;
1240 OIC_LOG_V(ERROR, TAG, "no logic [%d]", status);
1243 if (CA_BLOCK_INCOMPLETE != status && CA_BLOCK_TOO_LARGE != status)
1245 // negotiate block size
1246 res = CANegotiateBlockSize(currData, block, pdu->hdr->coap_hdr_udp_t.type, blockType);
1247 if (CA_STATUS_OK != res)
1249 OIC_LOG(ERROR, TAG, "negotiation has failed");
1257 CAResult_t CAGetMoreBitFromBlock(size_t payloadLen, coap_block_t *block)
1259 VERIFY_NON_NULL(block, TAG, "block");
1261 if ((size_t)((block->num + 1) << (block->szx + BLOCK_NUMBER_IDX))
1264 OIC_LOG(DEBUG, TAG, "Set the M-bit(1)");
1269 OIC_LOG(DEBUG, TAG, "Set the M-bit(0)");
1273 return CA_STATUS_OK;
1276 CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block,
1277 CAMessageType_t msgType, uint16_t blockType)
1279 OIC_LOG(DEBUG, TAG, "IN-NegotiateBlockSize");
1281 VERIFY_NON_NULL(currData, TAG, "currData");
1282 VERIFY_NON_NULL(block, TAG, "block");
1284 // #1. check the block option type
1285 if (COAP_OPTION_BLOCK2 == blockType)
1287 // #2. check the message type
1288 if (CA_MSG_ACKNOWLEDGE == msgType)
1290 if (block->szx > currData->block2.szx)
1292 OIC_LOG(DEBUG, TAG, "sze is big");
1294 // #3. calculate new block number from block size
1295 unsigned int blockNum = BLOCK_SIZE(block->szx) /
1296 BLOCK_SIZE(currData->block2.szx) - 1;
1297 OIC_LOG(DEBUG, TAG, "num is set as Negotiation");
1298 block->num += blockNum;
1299 block->szx = currData->block2.szx;
1300 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1305 if (block->szx > currData->block2.szx)
1307 OIC_LOG(DEBUG, TAG, "sze is big");
1308 block->szx = currData->block2.szx;
1312 else if (COAP_OPTION_BLOCK1 == blockType)
1314 if (CA_MSG_ACKNOWLEDGE == msgType)
1316 if (block->szx < currData->block1.szx)
1318 OIC_LOG(DEBUG, TAG, "sze is small");
1320 unsigned int blockNum = BLOCK_SIZE(currData->block1.szx) /
1321 BLOCK_SIZE(block->szx) - 1;
1322 block->num += blockNum;
1323 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1328 if (block->szx > currData->block1.szx)
1330 OIC_LOG(DEBUG, TAG, "sze is big");
1331 block->szx = currData->block1.szx;
1337 OIC_LOG(DEBUG, TAG, "Invalid block option");
1338 return CA_STATUS_FAILED;
1341 OIC_LOG(DEBUG, TAG, "OUT-NegotiateBlockSize");
1343 return CA_STATUS_OK;
1346 CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block,
1349 VERIFY_NON_NULL(currData, TAG, "currData");
1351 // check if block size is bigger than CABlockSize_t
1352 if (block.szx > CA_BLOCK_SIZE_1024_BYTE)
1354 OIC_LOG(DEBUG, TAG, "invalid block szx");
1355 return CA_STATUS_FAILED;
1358 // update block option
1359 if (COAP_OPTION_BLOCK2 == blockType)
1361 currData->block2 = block;
1365 currData->block1 = block;
1368 OIC_LOG(DEBUG, TAG, "data has updated");
1369 return CA_STATUS_OK;
1372 CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID)
1374 VERIFY_NON_NULL(pdu, TAG, "pdu");
1375 VERIFY_NON_NULL(blockID, TAG, "blockID");
1377 // if CON message is sent, update messageId in block-wise transfer list
1378 if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type)
1380 CAData_t * cadata = CAGetDataSetFromBlockDataList(blockID);
1383 OIC_LOG(ERROR, TAG, "CAData is unavailable");
1384 return CA_STATUS_FAILED;
1387 if (cadata->requestInfo)
1389 cadata->requestInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
1393 return CA_STATUS_OK;
1396 CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info,
1397 const CAEndpoint_t *endpoint, coap_list_t **options)
1399 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption");
1400 VERIFY_NON_NULL(pdu, TAG, "pdu");
1401 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1402 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1403 VERIFY_NON_NULL(info, TAG, "info");
1404 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1405 VERIFY_NON_NULL(options, TAG, "options");
1407 size_t dataLength = 0;
1410 dataLength = info->payloadSize;
1411 OIC_LOG_V(DEBUG, TAG, "dataLength - %d", dataLength);
1414 OIC_LOG_V(DEBUG, TAG, "previous payload - %s", (*pdu)->data);
1416 CAResult_t res = CA_STATUS_OK;
1417 uint32_t code = CA_RESPONSE_CODE((*pdu)->hdr->coap_hdr_udp_t.code);
1418 if (CA_REQUEST_ENTITY_INCOMPLETE == code)
1420 OIC_LOG(INFO, TAG, "don't use option");
1424 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
1425 (CAToken_t)(*pdu)->hdr->coap_hdr_udp_t.token,
1426 (*pdu)->hdr->coap_hdr_udp_t.token_length,
1429 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
1431 OIC_LOG(ERROR, TAG, "blockId is null");
1432 res = CA_STATUS_FAILED;
1436 uint8_t blockType = CAGetBlockOptionType(blockDataID);
1437 if (COAP_OPTION_BLOCK2 == blockType)
1439 res = CAAddBlockOption2(pdu, info, dataLength, blockDataID, options);
1440 if (CA_STATUS_OK != res)
1442 OIC_LOG(ERROR, TAG, "add has failed");
1446 else if (COAP_OPTION_BLOCK1 == blockType)
1448 res = CAAddBlockOption1(pdu, info, dataLength, blockDataID, options);
1449 if (CA_STATUS_OK != res)
1451 OIC_LOG(ERROR, TAG, "add has failed");
1457 OIC_LOG(DEBUG, TAG, "no BLOCK option");
1459 // in case it is not large data, add option list to pdu.
1462 for (coap_list_t *opt = *options; opt; opt = opt->next)
1464 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
1465 COAP_OPTION_DATA(*(coap_option *) opt->data));
1467 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", (*pdu)->length);
1468 coap_add_option(*pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
1469 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
1470 COAP_OPTION_DATA(*(coap_option *) opt->data), coap_udp);
1474 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", (*pdu)->length);
1476 // if response data is so large. it have to send as block transfer
1477 if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload))
1479 OIC_LOG(INFO, TAG, "it have to use block");
1483 OIC_LOG(INFO, TAG, "not Blockwise Transfer");
1488 // if received message type is RESET from remote device,
1489 // we have to use the updated message id to find token.
1490 res = CAUpdateMessageId(*pdu, blockDataID);
1491 if (CA_STATUS_OK != res)
1493 OIC_LOG(ERROR, TAG, "fail to update CON message id ");
1498 if (CA_ADAPTER_IP == endpoint->adapter && 0 == endpoint->port)
1500 CARemoveBlockDataFromList(blockDataID);
1502 CADestroyBlockID(blockDataID);
1503 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption");
1507 CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
1508 const CABlockDataID_t *blockID, coap_list_t **options)
1510 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption2");
1511 VERIFY_NON_NULL(pdu, TAG, "pdu");
1512 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1513 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1514 VERIFY_NON_NULL(info, TAG, "info");
1515 VERIFY_NON_NULL(blockID, TAG, "blockID");
1516 VERIFY_NON_NULL(options, TAG, "options");
1518 // get set block data from CABlock list-set.
1519 coap_block_t *block1 = CAGetBlockOption(blockID,
1520 COAP_OPTION_BLOCK1);
1521 coap_block_t *block2 = CAGetBlockOption(blockID,
1522 COAP_OPTION_BLOCK2);
1523 if (!block1 || !block2)
1525 OIC_LOG(ERROR, TAG, "getting has failed");
1526 return CA_STATUS_FAILED;
1529 CALogBlockInfo(block2);
1532 if (CA_MSG_ACKNOWLEDGE == (*pdu)->hdr->coap_hdr_udp_t.type ||
1533 (CA_MSG_NONCONFIRM == (*pdu)->hdr->coap_hdr_udp_t.type &&
1534 CA_GET != (*pdu)->hdr->coap_hdr_udp_t.code))
1536 int32_t res = coap_write_block_opt(block2, COAP_OPTION_BLOCK2, *pdu, dataLength);
1539 case -2: /* illegal block */
1540 code = COAP_RESPONSE_CODE(CA_BAD_REQ);
1541 OIC_LOG(ERROR, TAG, "write block option : -2");
1543 case -1: /* should really not happen */
1544 OIC_LOG(ERROR, TAG, "write block option : -1");
1546 case -3: /* cannot handle request */
1547 code = COAP_RESPONSE_CODE(CA_INTERNAL_SERVER_ERROR);
1548 OIC_LOG(ERROR, TAG, "write block option : -3");
1551 OIC_LOG(INFO, TAG, "success write block option");
1553 CALogBlockInfo(block2);
1555 // if block number is 0, add size2 option
1556 if (0 == block2->num)
1558 res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE2, dataLength, options);
1559 if (CA_STATUS_OK != res)
1561 OIC_LOG(ERROR, TAG, "add has failed");
1562 CARemoveBlockDataFromList(blockID);
1569 OIC_LOG(DEBUG, TAG, "combining block1 and block2");
1570 CAResult_t res = CAAddBlockOptionImpl(*pdu, block1, COAP_OPTION_BLOCK1, options);
1571 if (CA_STATUS_OK != res)
1573 OIC_LOG(ERROR, TAG, "add has failed");
1574 CARemoveBlockDataFromList(blockID);
1577 // initialize block number
1581 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload,
1582 block2->num, block2->szx))
1584 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1585 return CA_STATUS_FAILED;
1590 // if sent message is last response block message, remove data
1591 CARemoveBlockDataFromList(blockID);
1595 if (CA_MSG_NONCONFIRM == (*pdu)->hdr->coap_hdr_udp_t.type)
1597 OIC_LOG(DEBUG, TAG, "NON, send next block..");
1598 // update block data
1600 CAResult_t res = CAProcessNextStep(*pdu, NULL,
1601 CA_SENT_PREVIOUS_NON_MSG,
1603 if (CA_STATUS_OK != res)
1605 OIC_LOG(ERROR, TAG, "failed to process next step");
1606 CARemoveBlockDataFromList(blockID);
1614 OIC_LOG(DEBUG, TAG, "option2, not ACK msg");
1615 CAResult_t res = CAAddBlockOptionImpl(*pdu, block2, COAP_OPTION_BLOCK2, options);
1616 if (CA_STATUS_OK != res)
1618 OIC_LOG(ERROR, TAG, "add has failed");
1619 CARemoveBlockDataFromList(blockID);
1624 return CA_STATUS_OK;
1627 OIC_LOG_V(ERROR, TAG, "error : %d", code);
1629 char* phrase = coap_response_phrase(code);
1632 coap_add_data(*pdu, strlen(phrase),
1633 (unsigned char *) phrase);
1635 return CA_STATUS_FAILED;
1638 CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
1639 const CABlockDataID_t *blockID, coap_list_t **options)
1641 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption1");
1642 VERIFY_NON_NULL(pdu, TAG, "pdu");
1643 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1644 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1645 VERIFY_NON_NULL(info, TAG, "info");
1646 VERIFY_NON_NULL(blockID, TAG, "blockID");
1647 VERIFY_NON_NULL(options, TAG, "options");
1649 // get set block data from CABlock list-set.
1650 coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1653 OIC_LOG(ERROR, TAG, "getting has failed");
1654 return CA_STATUS_FAILED;
1657 CALogBlockInfo(block1);
1659 if (CA_MSG_ACKNOWLEDGE == (*pdu)->hdr->coap_hdr_udp_t.type)
1661 OIC_LOG(DEBUG, TAG, "option1 and ACK msg..");
1662 CAResult_t res = CAAddBlockOptionImpl(*pdu, block1, COAP_OPTION_BLOCK1, options);
1663 if (CA_STATUS_OK != res)
1665 OIC_LOG(ERROR, TAG, "add has failed");
1666 CARemoveBlockDataFromList(blockID);
1670 if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload))
1672 OIC_LOG(ERROR, TAG, "failed to add payload");
1673 return CA_STATUS_FAILED;
1676 // reset block-list after write block
1679 // remove data from list
1680 CAResult_t res = CARemoveBlockDataFromList(blockID);
1681 if (CA_STATUS_OK != res)
1683 OIC_LOG(ERROR, TAG, "remove has failed");
1690 CAGetMoreBitFromBlock(dataLength, block1);
1692 CAResult_t res = CA_STATUS_OK;
1693 // if block number is 0, add size1 option
1694 if (0 == block1->num)
1696 res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE1, dataLength, options);
1697 if (CA_STATUS_OK != res)
1699 OIC_LOG(ERROR, TAG, "add has failed");
1700 CARemoveBlockDataFromList(blockID);
1705 res = CAAddBlockOptionImpl(*pdu, block1, COAP_OPTION_BLOCK1, options);
1706 if (CA_STATUS_OK != res)
1708 OIC_LOG(ERROR, TAG, "add has failed");
1709 CARemoveBlockDataFromList(blockID);
1712 CALogBlockInfo(block1);
1714 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload,
1715 block1->num, block1->szx))
1717 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1718 return CA_STATUS_FAILED;
1721 // check the message type and if message type is NON, next block message will be sent
1722 if (CA_MSG_NONCONFIRM == (*pdu)->hdr->coap_hdr_udp_t.type)
1726 OIC_LOG(DEBUG, TAG, "NON, send next block..");
1727 // update block data
1729 CAResult_t res = CAProcessNextStep(*pdu, NULL,
1730 CA_SENT_PREVIOUS_NON_MSG,
1732 if (CA_STATUS_OK != res)
1734 OIC_LOG(ERROR, TAG, "failed to process next step");
1735 CARemoveBlockDataFromList(blockID);
1741 CARemoveBlockDataFromList(blockID);
1746 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption1");
1748 return CA_STATUS_OK;
1751 CAResult_t CAAddBlockOptionImpl(coap_pdu_t *pdu, coap_block_t *block, uint8_t blockType,
1752 coap_list_t **options)
1754 OIC_LOG(DEBUG, TAG, "IN-AddBlockOptionImpl");
1755 VERIFY_NON_NULL(pdu, TAG, "pdu");
1756 VERIFY_NON_NULL(block, TAG, "block");
1757 VERIFY_NON_NULL(options, TAG, "options");
1759 unsigned char buf[BLOCKWISE_OPTION_BUFFER] = { 0 };
1760 unsigned int optionLength = coap_encode_var_bytes(buf,
1761 ((block->num << BLOCK_NUMBER_IDX)
1762 | (block->m << BLOCK_M_BIT_IDX)
1765 int ret = coap_insert(options,
1766 CACreateNewOptionNode(blockType, optionLength, (char *) buf),
1770 return CA_STATUS_INVALID_PARAM;
1773 // after adding the block option to option list, add option list to pdu.
1776 for (coap_list_t *opt = *options; opt; opt = opt->next)
1778 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
1779 COAP_OPTION_DATA(*(coap_option *) opt->data));
1781 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
1782 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
1783 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
1784 COAP_OPTION_DATA(*(coap_option *) opt->data), coap_udp);
1788 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
1790 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOptionImpl");
1791 return CA_STATUS_OK;
1794 CAResult_t CAAddBlockSizeOption(coap_pdu_t *pdu, uint16_t sizeType, size_t dataLength,
1795 coap_list_t **options)
1797 OIC_LOG(DEBUG, TAG, "IN-CAAddBlockSizeOption");
1798 VERIFY_NON_NULL(pdu, TAG, "pdu");
1799 VERIFY_NON_NULL(options, TAG, "options");
1801 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1803 OIC_LOG(ERROR, TAG, "unknown option type");
1804 return CA_STATUS_FAILED;
1807 unsigned char value[BLOCKWISE_OPTION_BUFFER] = { 0 };
1808 unsigned int optionLength = coap_encode_var_bytes(value, dataLength);
1810 int ret = coap_insert(options,
1811 CACreateNewOptionNode(sizeType, optionLength, (char *) value),
1815 return CA_STATUS_INVALID_PARAM;
1818 OIC_LOG(DEBUG, TAG, "OUT-CAAddBlockSizeOption");
1820 return CA_STATUS_OK;
1823 // TODO make pdu const after libcoap is updated to support that.
1824 bool CAIsPayloadLengthInPduWithBlockSizeOption(coap_pdu_t *pdu,
1826 size_t *totalPayloadLen)
1828 OIC_LOG(DEBUG, TAG, "IN-CAIsPayloadLengthInPduWithBlockSizeOption");
1829 VERIFY_NON_NULL(pdu, TAG, "pdu");
1830 VERIFY_NON_NULL(totalPayloadLen, TAG, "totalPayloadLen");
1832 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1834 OIC_LOG(ERROR, TAG, "unknown option type");
1835 return CA_STATUS_FAILED;
1838 coap_opt_iterator_t opt_iter;
1839 coap_opt_t *option = coap_check_option(pdu, sizeType, &opt_iter);
1842 OIC_LOG(DEBUG, TAG, "get size option from pdu");
1843 *totalPayloadLen = coap_decode_var_bytes(COAP_OPT_VALUE(option),
1844 COAP_OPT_LENGTH(option));
1846 OIC_LOG_V(DEBUG, TAG, "the total payload length to be received is [%d]bytes",
1852 OIC_LOG(DEBUG, TAG, "OUT-CAIsPayloadLengthInPduWithBlockSizeOption");
1857 uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlock,
1858 const CAData_t *receivedData, uint16_t blockType,
1861 OIC_LOG(DEBUG, TAG, "IN-CheckBlockError");
1863 VERIFY_NON_NULL(currData, TAG, "currData is NULL");
1864 VERIFY_NON_NULL(receivedBlock, TAG, "receivedBlock is NULL");
1865 VERIFY_NON_NULL(receivedData, TAG, "receivedData is NULL");
1867 // #1. check the received payload length
1868 size_t blockPayloadLen = 0;
1869 CAGetPayloadInfo(receivedData, &blockPayloadLen);
1871 // #2. check if the block sequence is right
1872 if (COAP_OPTION_BLOCK1 == blockType)
1874 size_t prePayloadLen = currData->receivedPayloadLen;
1875 if (prePayloadLen != (size_t)BLOCK_SIZE(receivedBlock->szx)
1876 * receivedBlock->num)
1878 if (receivedBlock->num > currData->block1.num + 1)
1880 // 408 Error handling of block loss
1881 OIC_LOG(ERROR, TAG, "option1: error 4.08");
1882 OIC_LOG(ERROR, TAG, "it didn't order");
1883 return CA_BLOCK_INCOMPLETE;
1885 return CA_BLOCK_RECEIVED_ALREADY;
1888 else if (COAP_OPTION_BLOCK2 == blockType)
1890 if (receivedBlock->num != currData->block2.num)
1892 if (receivedBlock->num > currData->block2.num)
1894 // 408 Error handling of block loss
1895 OIC_LOG(ERROR, TAG, "option2: error 4.08");
1896 OIC_LOG(ERROR, TAG, "it didn't order");
1897 return CA_BLOCK_INCOMPLETE;
1899 return CA_BLOCK_RECEIVED_ALREADY;
1903 // #3. check if error check logic is required
1904 size_t optionLen = dataLen - blockPayloadLen;
1905 if (receivedBlock->m && blockPayloadLen !=
1906 (size_t)BLOCK_SIZE(receivedBlock->szx))
1908 // 413 Error handling of too large entity
1909 if (COAP_MAX_PDU_SIZE < BLOCK_SIZE(receivedBlock->szx) + optionLen)
1911 // buffer size is smaller than received block size
1912 OIC_LOG(ERROR, TAG, "error type 4.13");
1913 OIC_LOG(ERROR, TAG, "too large size");
1915 // set the block size to be smaller than COAP_MAX_PDU_SIZE
1916 for (int32_t size = CA_DEFAULT_BLOCK_SIZE; size >= 0; size--)
1918 if (COAP_MAX_PDU_SIZE >= BLOCK_SIZE(size) + optionLen)
1920 OIC_LOG_V(ERROR, TAG, "replace sze with %d", size);
1921 if (COAP_OPTION_BLOCK2 == blockType)
1923 currData->block2.szx = size;
1927 currData->block1.szx = size;
1932 return CA_BLOCK_TOO_LARGE;
1936 // 408 Error handling of payload loss
1937 OIC_LOG(ERROR, TAG, "error type 4.08");
1938 OIC_LOG(ERROR, TAG, "payload len != block sze");
1939 return CA_BLOCK_INCOMPLETE;
1942 else if (0 == receivedBlock->m && 0 != currData->payloadLength)
1944 // if the received block is last block, check the total payload length
1945 size_t receivedPayloadLen = currData->receivedPayloadLen;
1946 receivedPayloadLen += blockPayloadLen;
1948 if (receivedPayloadLen != currData->payloadLength)
1950 OIC_LOG(ERROR, TAG, "error type 4.08");
1951 OIC_LOG(ERROR, TAG, "total payload length is wrong");
1952 return CA_BLOCK_INCOMPLETE;
1956 OIC_LOG(DEBUG, TAG, "received all data normally");
1958 OIC_LOG(DEBUG, TAG, "OUT-CheckBlockError");
1960 return CA_BLOCK_UNKNOWN;
1963 CAResult_t CAUpdatePayloadData(CABlockData_t *currData, const CAData_t *receivedData,
1964 uint8_t status, bool isSizeOption, uint16_t blockType)
1966 OIC_LOG(DEBUG, TAG, "IN-UpdatePayloadData");
1968 VERIFY_NON_NULL(currData, TAG, "currData");
1969 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
1971 // if error code is 4.08, do not update payload
1972 if (CA_BLOCK_INCOMPLETE == status)
1974 OIC_LOG(ERROR, TAG, "no require to update");
1975 return CA_STATUS_OK;
1978 size_t blockPayloadLen = 0;
1979 CAPayload_t blockPayload = CAGetPayloadInfo(receivedData, &blockPayloadLen);
1981 if (CA_BLOCK_TOO_LARGE == status)
1983 blockPayloadLen = (COAP_OPTION_BLOCK2 == blockType) ?
1984 BLOCK_SIZE(currData->block2.szx) : BLOCK_SIZE(currData->block1.szx);
1987 // memory allocation for the received block payload
1988 size_t prePayloadLen = currData->receivedPayloadLen;
1991 if (currData->payloadLength)
1993 // in case the block message has the size option
1994 // allocate the memory for the total payload
1997 CAPayload_t prePayload = currData->payload;
1999 OIC_LOG(DEBUG, TAG, "allocate memory for the total payload");
2000 currData->payload = (CAPayload_t) OICCalloc(1, currData->payloadLength);
2001 if (NULL == currData->payload)
2003 OIC_LOG(ERROR, TAG, "out of memory");
2004 return CA_MEMORY_ALLOC_FAILED;
2006 memcpy(currData->payload, prePayload, prePayloadLen);
2007 OICFree(prePayload);
2010 // update the total payload
2011 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
2015 OIC_LOG(DEBUG, TAG, "allocate memory for the received block payload");
2017 size_t totalPayloadLen = prePayloadLen + blockPayloadLen;
2018 CAPayload_t newPayload = OICRealloc(currData->payload, totalPayloadLen);
2019 if (NULL == newPayload)
2021 OIC_LOG(ERROR, TAG, "out of memory");
2022 return CA_MEMORY_ALLOC_FAILED;
2025 // update the total payload
2026 memset(newPayload + prePayloadLen, 0, blockPayloadLen);
2027 currData->payload = newPayload;
2028 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
2031 // update received payload length
2032 currData->receivedPayloadLen += blockPayloadLen;
2034 OIC_LOG_V(DEBUG, TAG, "updated payload: %s, len: %d", currData->payload,
2035 currData->receivedPayloadLen);
2038 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayloadData");
2039 return CA_STATUS_OK;
2042 CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
2044 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", NULL);
2045 VERIFY_NON_NULL_RET(pdu->hdr, TAG, "pdu->hdr", NULL);
2046 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
2048 CAInfo_t responseData = { .tokenLength = pdu->hdr->coap_hdr_udp_t.token_length };
2049 responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength);
2050 if (!responseData.token)
2052 OIC_LOG(ERROR, TAG, "out of memory");
2055 memcpy(responseData.token, pdu->hdr->coap_hdr_udp_t.token, responseData.tokenLength);
2057 CAResponseInfo_t* responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
2060 OIC_LOG(ERROR, TAG, "out of memory");
2061 OICFree(responseData.token);
2064 responseInfo->info = responseData;
2066 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2069 OIC_LOG(ERROR, TAG, "out of memory");
2070 OICFree(responseInfo);
2074 data->requestInfo = NULL;
2075 data->responseInfo = responseInfo;
2076 data->remoteEndpoint = CACloneEndpoint(endpoint);
2077 data->type = SEND_TYPE_UNICAST;
2082 CAData_t *CACloneCAData(const CAData_t *data)
2084 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2086 CAData_t *clone = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2089 OIC_LOG(ERROR, TAG, "out of memory");
2094 if (data->requestInfo)
2096 clone->requestInfo = CACloneRequestInfo(data->requestInfo);
2098 else if (data->responseInfo)
2100 clone->responseInfo = CACloneResponseInfo(data->responseInfo);
2103 if (data->remoteEndpoint)
2105 clone->remoteEndpoint = CACloneEndpoint(data->remoteEndpoint);
2111 CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
2114 OIC_LOG(DEBUG, TAG, "IN-UpdatePayload");
2116 VERIFY_NON_NULL(data, TAG, "data is NULL");
2117 VERIFY_NON_NULL(payload, TAG, "payload is NULL");
2119 CAPayload_t newPayload = NULL;
2120 switch (data->dataType)
2122 case CA_REQUEST_DATA:
2123 if (!data->requestInfo)
2125 OIC_LOG(ERROR, TAG, "request info is null");
2126 return CA_STATUS_FAILED;
2128 // allocate payload field
2129 newPayload = OICRealloc(data->requestInfo->info.payload, payloadLen);
2132 OIC_LOG(ERROR, TAG, "out of memory");
2133 return CA_STATUS_FAILED;
2135 data->requestInfo->info.payload = newPayload;
2136 memcpy(data->requestInfo->info.payload, payload, payloadLen);
2137 data->requestInfo->info.payloadSize = payloadLen;
2140 case CA_RESPONSE_DATA:
2141 if (!data->responseInfo)
2143 OIC_LOG(ERROR, TAG, "response info is null");
2144 return CA_STATUS_FAILED;
2146 // allocate payload field
2147 newPayload = OICRealloc(data->responseInfo->info.payload, payloadLen);
2150 OIC_LOG(ERROR, TAG, "out of memory");
2151 return CA_STATUS_FAILED;
2153 data->responseInfo->info.payload = newPayload;
2154 memcpy(data->responseInfo->info.payload, payload, payloadLen);
2155 data->responseInfo->info.payloadSize = payloadLen;
2159 // does not occur case
2160 OIC_LOG(ERROR, TAG, "not supported data type");
2161 return CA_NOT_SUPPORTED;
2164 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayload");
2166 return CA_STATUS_OK;
2169 CAPayload_t CAGetPayloadInfo(const CAData_t *data, size_t *payloadLen)
2171 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2172 VERIFY_NON_NULL_RET(payloadLen, TAG, "payloadLen", NULL);
2174 if (data->requestInfo)
2176 if (data->requestInfo->info.payload)
2178 *payloadLen = data->requestInfo->info.payloadSize;
2179 return data->requestInfo->info.payload;
2184 if (data->responseInfo->info.payload)
2186 *payloadLen = data->responseInfo->info.payloadSize;
2187 return data->responseInfo->info.payload;
2194 CAResult_t CAHandleBlockErrorResponse(coap_block_t *block, uint16_t blockType,
2195 uint32_t responseResult)
2197 OIC_LOG(DEBUG, TAG, "IN-HandleBlockErrorRes");
2198 VERIFY_NON_NULL(block, TAG, "block is NULL");
2200 // update block data
2201 switch (responseResult)
2203 case CA_REQUEST_ENTITY_INCOMPLETE:
2206 case CA_REQUEST_ENTITY_TOO_LARGE:
2207 if (COAP_OPTION_BLOCK1 == blockType)
2214 OIC_LOG_V(ERROR, TAG, "there is no Error Code of BWT[%d]", responseResult);
2217 OIC_LOG(DEBUG, TAG, "OUT-HandleBlockErrorRes");
2218 return CA_STATUS_OK;
2221 CAResult_t CAUpdateBlockOptionType(const CABlockDataID_t *blockID,
2224 OIC_LOG(DEBUG, TAG, "IN-UpdateBlockOptionType");
2225 VERIFY_NON_NULL(blockID, TAG, "blockID");
2227 ca_mutex_lock(g_context.blockDataListMutex);
2229 size_t len = u_arraylist_length(g_context.dataList);
2230 for (size_t i = 0; i < len; i++)
2232 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2233 if (CABlockidMatches(currData, blockID))
2235 currData->type = blockType;
2236 ca_mutex_unlock(g_context.blockDataListMutex);
2237 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2238 return CA_STATUS_OK;
2241 ca_mutex_unlock(g_context.blockDataListMutex);
2243 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2244 return CA_STATUS_FAILED;
2247 uint8_t CAGetBlockOptionType(const CABlockDataID_t *blockID)
2249 OIC_LOG(DEBUG, TAG, "IN-GetBlockOptionType");
2250 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", 0);
2252 ca_mutex_lock(g_context.blockDataListMutex);
2254 size_t len = u_arraylist_length(g_context.dataList);
2255 for (size_t i = 0; i < len; i++)
2257 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2258 if (CABlockidMatches(currData, blockID))
2260 ca_mutex_unlock(g_context.blockDataListMutex);
2261 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2262 return currData->type;
2265 ca_mutex_unlock(g_context.blockDataListMutex);
2267 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2271 CAData_t *CAGetDataSetFromBlockDataList(const CABlockDataID_t *blockID)
2273 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2275 ca_mutex_lock(g_context.blockDataListMutex);
2277 size_t len = u_arraylist_length(g_context.dataList);
2278 for (size_t i = 0; i < len; i++)
2280 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2281 if (CABlockidMatches(currData, blockID))
2283 ca_mutex_unlock(g_context.blockDataListMutex);
2284 return currData->sentData;
2287 ca_mutex_unlock(g_context.blockDataListMutex);
2292 CAResult_t CAGetTokenFromBlockDataList(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
2293 CAResponseInfo_t *responseInfo)
2295 OIC_LOG(DEBUG, TAG, "IN-CAGetTokenFromBlockDataList");
2296 VERIFY_NON_NULL(pdu, TAG, "pdu");
2297 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
2298 VERIFY_NON_NULL(responseInfo, TAG, "responseInfo");
2300 ca_mutex_lock(g_context.blockDataListMutex);
2302 size_t len = u_arraylist_length(g_context.dataList);
2303 for (size_t i = 0; i < len; i++)
2305 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2306 if (NULL == currData)
2311 if (NULL != currData->sentData && NULL != currData->sentData->requestInfo)
2313 if (pdu->hdr->coap_hdr_udp_t.id == currData->sentData->requestInfo->info.messageId &&
2314 endpoint->adapter == currData->sentData->remoteEndpoint->adapter)
2316 if (NULL != currData->sentData->requestInfo->info.token)
2318 uint8_t length = currData->sentData->requestInfo->info.tokenLength;
2319 responseInfo->info.tokenLength = length;
2320 responseInfo->info.token = (char *) OICMalloc(length);
2321 if (NULL == responseInfo->info.token)
2323 OIC_LOG(ERROR, TAG, "out of memory");
2324 ca_mutex_unlock(g_context.blockDataListMutex);
2325 return CA_MEMORY_ALLOC_FAILED;
2327 memcpy(responseInfo->info.token, currData->sentData->requestInfo->info.token,
2328 responseInfo->info.tokenLength);
2330 ca_mutex_unlock(g_context.blockDataListMutex);
2331 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2332 return CA_STATUS_OK;
2338 ca_mutex_unlock(g_context.blockDataListMutex);
2340 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2341 return CA_STATUS_OK;
2344 CAResult_t CACheckBlockDataValidation(const CAData_t *sendData, CABlockData_t **blockData)
2346 VERIFY_NON_NULL(sendData, TAG, "sendData");
2347 VERIFY_NON_NULL(blockData, TAG, "blockData");
2349 ca_mutex_lock(g_context.blockDataListMutex);
2351 size_t len = u_arraylist_length(g_context.dataList);
2352 for (size_t i = 0; i < len; i++)
2354 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2361 if (sendData->requestInfo) // sendData is requestMessage
2363 OIC_LOG(DEBUG, TAG, "Send request");
2364 if (NULL != currData->blockDataId
2365 && NULL != currData->blockDataId->id
2366 && currData->blockDataId->idLength > 0
2367 && NULL != sendData->requestInfo->info.token)
2369 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2370 (CAToken_t)sendData->requestInfo->info.token,
2371 sendData->requestInfo->info.tokenLength,
2372 sendData->remoteEndpoint->port);
2374 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
2376 OIC_LOG(ERROR, TAG, "blockId is null");
2377 CADestroyBlockID(blockDataID);
2378 return CA_STATUS_FAILED;
2381 if (CABlockidMatches(currData, blockDataID))
2383 OIC_LOG(ERROR, TAG, "already sent");
2384 CADestroyBlockID(blockDataID);
2387 CADestroyBlockID(blockDataID);
2390 else if (sendData->responseInfo) // sendData is responseMessage
2392 OIC_LOG(DEBUG, TAG, "Send response");
2393 if (NULL != currData->blockDataId
2394 && NULL != currData->blockDataId->id
2395 && currData->blockDataId->idLength > 0
2396 && NULL != sendData->responseInfo->info.token)
2398 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2399 (CAToken_t)sendData->responseInfo->info.token,
2400 sendData->responseInfo->info.tokenLength,
2401 sendData->remoteEndpoint->port);
2403 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
2405 OIC_LOG(ERROR, TAG, "blockId is null");
2406 CADestroyBlockID(blockDataID);
2407 return CA_STATUS_FAILED;
2410 if (CABlockidMatches(currData, blockDataID))
2413 if (NULL != currData->sentData)
2415 OIC_LOG(DEBUG, TAG, "init block number");
2416 CADestroyDataSet(currData->sentData);
2418 currData->sentData = CACloneCAData(sendData);
2419 *blockData = currData;
2420 CADestroyBlockID(blockDataID);
2421 ca_mutex_unlock(g_context.blockDataListMutex);
2422 return CA_STATUS_OK;
2424 CADestroyBlockID(blockDataID);
2429 OIC_LOG(ERROR, TAG, "no CAInfo data");
2433 ca_mutex_unlock(g_context.blockDataListMutex);
2435 return CA_STATUS_FAILED;
2438 CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID)
2440 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2442 ca_mutex_lock(g_context.blockDataListMutex);
2444 size_t len = u_arraylist_length(g_context.dataList);
2445 for (size_t i = 0; i < len; i++)
2447 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2448 if (CABlockidMatches(currData, blockID))
2450 ca_mutex_unlock(g_context.blockDataListMutex);
2454 ca_mutex_unlock(g_context.blockDataListMutex);
2459 coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID,
2462 OIC_LOG(DEBUG, TAG, "IN-GetBlockOption");
2463 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2465 ca_mutex_lock(g_context.blockDataListMutex);
2467 size_t len = u_arraylist_length(g_context.dataList);
2468 for (size_t i = 0; i < len; i++)
2470 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2471 if (CABlockidMatches(currData, blockID))
2473 ca_mutex_unlock(g_context.blockDataListMutex);
2474 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2475 if (COAP_OPTION_BLOCK2 == blockType)
2477 return &currData->block2;
2481 return &currData->block1;
2485 ca_mutex_unlock(g_context.blockDataListMutex);
2487 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2491 CAPayload_t CAGetPayloadFromBlockDataList(const CABlockDataID_t *blockID,
2492 size_t *fullPayloadLen)
2494 OIC_LOG(DEBUG, TAG, "IN-GetFullPayload");
2495 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2496 VERIFY_NON_NULL_RET(fullPayloadLen, TAG, "fullPayloadLen", NULL);
2498 ca_mutex_lock(g_context.blockDataListMutex);
2500 size_t len = u_arraylist_length(g_context.dataList);
2501 for (size_t i = 0; i < len; i++)
2503 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2504 if (CABlockidMatches(currData, blockID))
2506 ca_mutex_unlock(g_context.blockDataListMutex);
2507 *fullPayloadLen = currData->receivedPayloadLen;
2508 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2509 return currData->payload;
2512 ca_mutex_unlock(g_context.blockDataListMutex);
2514 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2518 CABlockData_t *CACreateNewBlockData(const CAData_t *sendData)
2520 OIC_LOG(DEBUG, TAG, "IN-CACreateNewBlockData");
2521 VERIFY_NON_NULL_RET(sendData, TAG, "sendData", NULL);
2523 // create block data
2524 CABlockData_t *data = (CABlockData_t *) OICCalloc(1, sizeof(CABlockData_t));
2527 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2531 data->block1.szx = CA_DEFAULT_BLOCK_SIZE;
2532 data->block2.szx = CA_DEFAULT_BLOCK_SIZE;
2533 data->sentData = CACloneCAData(sendData);
2536 OIC_LOG(ERROR, TAG, PCF("memory alloc has failed"));
2541 CAToken_t token = NULL;
2542 uint8_t tokenLength = 0;
2543 if (data->sentData->requestInfo)
2545 // update token info
2546 tokenLength = data->sentData->requestInfo->info.tokenLength;
2547 token = data->sentData->requestInfo->info.token;
2549 else if(data->sentData->responseInfo)
2551 tokenLength = data->sentData->responseInfo->info.tokenLength;
2552 token = data->sentData->responseInfo->info.token;
2555 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2557 data->sentData->remoteEndpoint->port);
2558 if (NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
2560 OIC_LOG(ERROR, TAG, "blockId is null");
2561 CADestroyBlockID(blockDataID);
2562 CADestroyDataSet(data->sentData);
2566 data->blockDataId = blockDataID;
2568 ca_mutex_lock(g_context.blockDataListMutex);
2570 bool res = u_arraylist_add(g_context.dataList, (void *) data);
2573 OIC_LOG(ERROR, TAG, "add has failed");
2574 CADestroyBlockID(data->blockDataId);
2575 CADestroyDataSet(data->sentData);
2577 ca_mutex_unlock(g_context.blockDataListMutex);
2580 ca_mutex_unlock(g_context.blockDataListMutex);
2582 OIC_LOG(DEBUG, TAG, "OUT-CreateBlockData");
2586 CAResult_t CARemoveBlockDataFromList(const CABlockDataID_t *blockID)
2588 OIC_LOG(DEBUG, TAG, "CARemoveBlockData");
2589 VERIFY_NON_NULL(blockID, TAG, "blockID");
2591 ca_mutex_lock(g_context.blockDataListMutex);
2593 size_t len = u_arraylist_length(g_context.dataList);
2594 for (size_t i = 0; i < len; i++)
2596 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2597 if (CABlockidMatches(currData, blockID))
2599 CABlockData_t *removedData = u_arraylist_remove(g_context.dataList, i);
2602 OIC_LOG(ERROR, TAG, "data is NULL");
2603 ca_mutex_unlock(g_context.blockDataListMutex);
2604 return CA_STATUS_FAILED;
2608 if (currData->sentData)
2610 CADestroyDataSet(currData->sentData);
2612 CADestroyBlockID(currData->blockDataId);
2613 OICFree(currData->payload);
2615 ca_mutex_unlock(g_context.blockDataListMutex);
2616 return CA_STATUS_OK;
2619 ca_mutex_unlock(g_context.blockDataListMutex);
2621 return CA_STATUS_OK;
2624 bool CAIsBlockDataInList(const CABlockDataID_t *blockID)
2626 OIC_LOG(DEBUG, TAG, "IN-IsBlockDataInList");
2627 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", false);
2629 ca_mutex_lock(g_context.blockDataListMutex);
2631 size_t len = u_arraylist_length(g_context.dataList);
2632 for (size_t i = 0; i < len; i++)
2634 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2635 if (CABlockidMatches(currData, blockID))
2637 OIC_LOG(DEBUG, TAG, "found block data");
2638 ca_mutex_unlock(g_context.blockDataListMutex);
2642 ca_mutex_unlock(g_context.blockDataListMutex);
2644 OIC_LOG(DEBUG, TAG, "OUT-IsBlockDataInList");
2648 void CADestroyDataSet(CAData_t* data)
2650 VERIFY_NON_NULL_VOID(data, TAG, "data");
2652 CAFreeEndpoint(data->remoteEndpoint);
2653 CADestroyRequestInfoInternal(data->requestInfo);
2654 CADestroyResponseInfoInternal(data->responseInfo);
2658 CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength,
2659 uint16_t portNumber)
2661 VERIFY_NON_NULL_RET(token, TAG, "token", NULL);
2663 char port[PORT_LENGTH] = {0,};
2664 port[0] = (char)((portNumber>>8) & 0xFF);
2665 port[1] = (char)(portNumber & 0xFF);
2667 CABlockDataID_t* blockDataID = (CABlockDataID_t *) OICMalloc(sizeof(CABlockDataID_t));
2670 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2673 blockDataID->idLength = tokenLength + sizeof(port);
2674 blockDataID->id = (uint8_t *) OICMalloc(blockDataID->idLength);
2675 if (!blockDataID->id)
2677 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2678 OICFree(blockDataID);
2682 memcpy(blockDataID->id, token, tokenLength);
2683 memcpy(blockDataID->id + tokenLength, port, sizeof(port));
2685 OIC_LOG(DEBUG, TAG, "BlockID is ");
2686 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)blockDataID->id, blockDataID->idLength);
2691 void CADestroyBlockID(CABlockDataID_t *blockID)
2693 VERIFY_NON_NULL_VOID(blockID, TAG, "blockID");
2694 OICFree(blockID->id);
2699 bool CABlockidMatches(const CABlockData_t *currData, const CABlockDataID_t *blockID)
2701 VERIFY_NON_NULL_RET(currData, TAG, "currData", false);
2702 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", false);
2703 VERIFY_NON_NULL_RET(blockID->id, TAG, "blockID->id", false);
2705 if ((currData->blockDataId)
2706 && (currData->blockDataId->id)
2707 && (currData->blockDataId->idLength == blockID->idLength)
2708 && !memcmp(currData->blockDataId->id, blockID->id, currData->blockDataId->idLength))
2715 void CALogBlockInfo(coap_block_t *block)
2717 VERIFY_NON_NULL_VOID(block, TAG, "block");
2719 OIC_LOG(DEBUG, TAG, "block option info");
2721 OIC_LOG_V(DEBUG, TAG, "block option-num : %d", block->num);
2723 OIC_LOG_V(DEBUG, TAG, "block option-m : %d", block->m);
2725 OIC_LOG_V(DEBUG, TAG, "block option-szx : %d", block->szx);