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 OIC_LOG(DEBUG, TAG, "IN");
123 if (!g_context.blockDataListMutex)
125 g_context.blockDataListMutex = ca_mutex_new();
126 if (!g_context.blockDataListMutex)
128 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
129 return CA_STATUS_FAILED;
133 if (!g_context.blockDataSenderMutex)
135 g_context.blockDataSenderMutex = ca_mutex_new();
136 if (!g_context.blockDataSenderMutex)
138 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
139 CATerminateBlockWiseMutexVariables();
140 return CA_STATUS_FAILED;
147 void CATerminateBlockWiseMutexVariables()
149 OIC_LOG(DEBUG, TAG, "IN");
151 if (g_context.blockDataListMutex)
153 ca_mutex_free(g_context.blockDataListMutex);
154 g_context.blockDataListMutex = NULL;
157 if (g_context.blockDataSenderMutex)
159 ca_mutex_free(g_context.blockDataSenderMutex);
160 g_context.blockDataSenderMutex = NULL;
164 CAResult_t CASendBlockWiseData(const CAData_t *sendData)
166 VERIFY_NON_NULL(sendData, TAG, "sendData");
168 // check if message type is CA_MSG_RESET
169 if (sendData->responseInfo)
171 if (CA_MSG_RESET == sendData->responseInfo->info.type)
173 OIC_LOG(DEBUG, TAG, "reset message can't be sent to the block");
174 return CA_NOT_SUPPORTED;
178 // #1. check if it is already included in block data list
179 CABlockData_t *currData = NULL;
180 CAResult_t res = CACheckBlockDataValidation(sendData, &currData);
181 if (CA_STATUS_OK != res)
183 // #2. if it is not included, add the data into list
186 OIC_LOG(DEBUG, TAG, "There is no block data");
188 bool isBlock = CACheckPayloadLength(sendData);
191 if (sendData->requestInfo)
193 currData = CACreateNewBlockData(sendData);
196 OIC_LOG(ERROR, TAG, "failed to create block data");
197 return CA_MEMORY_ALLOC_FAILED;
200 return CA_NOT_SUPPORTED;
202 currData = CACreateNewBlockData(sendData);
205 OIC_LOG(ERROR, TAG, "failed to create block data");
206 return CA_MEMORY_ALLOC_FAILED;
211 // #3. check request/response block option type and payload length
212 res = CACheckBlockOptionType(currData);
213 if (CA_STATUS_OK == res)
215 // #4. send block message
216 OIC_LOG(DEBUG, TAG, "send first block msg");
217 res = CAAddSendThreadQueue(currData->sentData,
218 (const CABlockDataID_t *)&currData->blockDataId);
219 if (CA_STATUS_OK != res)
221 OIC_LOG(ERROR, TAG, "add has failed");
229 CAResult_t CAAddSendThreadQueue(const CAData_t *sendData, const CABlockDataID_t *blockID)
231 VERIFY_NON_NULL(sendData, TAG, "sendData");
232 VERIFY_NON_NULL(blockID, TAG, "blockID");
234 CAData_t *cloneData = CACloneCAData(sendData);
237 OIC_LOG(ERROR, TAG, "clone has failed");
238 CARemoveBlockDataFromList(blockID);
239 return CA_STATUS_FAILED;
242 if (g_context.sendThreadFunc)
244 ca_mutex_lock(g_context.blockDataSenderMutex);
245 g_context.sendThreadFunc(cloneData);
246 ca_mutex_unlock(g_context.blockDataSenderMutex);
250 CADestroyDataSet(cloneData);
255 CAResult_t CACheckBlockOptionType(CABlockData_t *currData)
257 VERIFY_NON_NULL(currData, TAG, "currData");
258 VERIFY_NON_NULL(currData->sentData, TAG, "currData->sentData");
260 bool isBlock = CACheckPayloadLength(currData->sentData);
263 return CA_NOT_SUPPORTED;
266 // set block option (COAP_OPTION_BLOCK2 or COAP_OPTION_BLOCK1)
267 if (currData->sentData->requestInfo) // request message
269 currData->type = COAP_OPTION_BLOCK1;
271 else // response message
273 currData->type = COAP_OPTION_BLOCK2;
279 // TODO make pdu const after libcoap is updated to support that.
280 CAResult_t CAReceiveBlockWiseData(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
281 const CAData_t *receivedData, size_t dataLen)
283 OIC_LOG(DEBUG, TAG, "CAReceiveBlockWiseData");
284 VERIFY_NON_NULL(pdu, TAG, "pdu");
285 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
286 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
287 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
289 // check if received message type is CA_MSG_RESET
290 if (CA_EMPTY == pdu->hdr->code)
292 OIC_LOG(DEBUG, TAG, "code is CA_EMPTY..");
294 // get token from block-wise transfer list when CA_EMPTY(RST/ACK) is received
295 CAResult_t res = CAGetTokenFromBlockDataList(pdu, endpoint, receivedData->responseInfo);
296 if (CA_STATUS_OK != res)
298 OIC_LOG(ERROR, TAG, "fail to get token");
302 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
303 receivedData->responseInfo->info.token,
304 receivedData->responseInfo->info.tokenLength,
306 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
308 OIC_LOG(ERROR, TAG, "blockId is null");
309 CADestroyBlockID(blockDataID);
310 return CA_STATUS_FAILED;
313 CARemoveBlockDataFromList(blockDataID);
314 CADestroyBlockID(blockDataID);
315 return CA_NOT_SUPPORTED;
318 // check if block option is set and get block data
319 coap_block_t block = {0, 0, 0};
322 int isBlock1 = coap_get_block(pdu, COAP_OPTION_BLOCK1, &block);
325 CAResult_t res = CASetNextBlockOption1(pdu, endpoint, receivedData, block, dataLen);
326 if (CA_STATUS_OK != res)
328 OIC_LOG(ERROR, TAG, "setting has failed");
334 int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
337 CAResult_t res = CASetNextBlockOption2(pdu, endpoint, receivedData, block, dataLen);
338 if (CA_STATUS_OK != res)
340 OIC_LOG(ERROR, TAG, "setting has failed");
345 // check if there is error code
346 if (!isBlock1 && !isBlock2)
348 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->code);
349 if (CA_REQUEST_ENTITY_INCOMPLETE == code)
351 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
352 (CAToken_t)pdu->hdr->token,
353 pdu->hdr->token_length,
356 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
358 OIC_LOG(ERROR, TAG, "blockId is null");
359 CADestroyBlockID(blockDataID);
360 return CA_STATUS_FAILED;
363 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
366 OIC_LOG(ERROR, TAG, "getting has failed");
367 CADestroyBlockID(blockDataID);
368 return CA_STATUS_FAILED;
371 if (COAP_OPTION_BLOCK2 == data->type)
373 coap_block_t *block2 = CAGetBlockOption(blockDataID, COAP_OPTION_BLOCK2);
376 OIC_LOG(ERROR, TAG, "block is null");
377 CADestroyBlockID(blockDataID);
378 return CA_STATUS_FAILED;
381 CAResult_t res = CASetNextBlockOption2(pdu, endpoint, receivedData, *block2,
383 if (CA_STATUS_OK != res)
385 OIC_LOG(ERROR, TAG, "setting has failed");
386 CADestroyBlockID(blockDataID);
390 else if (COAP_OPTION_BLOCK1 == data->type)
392 coap_block_t *block1 = CAGetBlockOption(blockDataID, COAP_OPTION_BLOCK1);
395 OIC_LOG(ERROR, TAG, "block is null");
396 CADestroyBlockID(blockDataID);
397 return CA_STATUS_FAILED;
400 CAResult_t res = CASetNextBlockOption1(pdu, endpoint, receivedData, *block1,
402 if (CA_STATUS_OK != res)
404 OIC_LOG(ERROR, TAG, "setting has failed");
405 CADestroyBlockID(blockDataID);
413 OIC_LOG(DEBUG, TAG, "it's normal pdu");
415 // if received data is response message
416 // and sent data remain in block data list, remove block data
417 if (receivedData->responseInfo)
419 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
420 (CAToken_t)pdu->hdr->token,
421 pdu->hdr->token_length,
423 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
425 OIC_LOG(ERROR, TAG, "blockId is null");
426 CADestroyBlockID(blockDataID);
427 return CA_STATUS_FAILED;
430 CARemoveBlockDataFromList(blockDataID);
431 CADestroyBlockID(blockDataID);
433 return CA_NOT_SUPPORTED;
440 CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData,
441 uint8_t blockWiseStatus, const CABlockDataID_t *blockID)
443 VERIFY_NON_NULL(pdu, TAG, "pdu");
444 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
445 VERIFY_NON_NULL(blockID, TAG, "blockID");
447 CAResult_t res = CA_STATUS_OK;
448 CAData_t *data = NULL;
450 // process blockWiseStatus
451 switch (blockWiseStatus)
453 case CA_OPTION2_FIRST_BLOCK:
454 res = CAAddSendThreadQueue(receivedData, blockID);
455 if (CA_STATUS_OK != res)
457 OIC_LOG(ERROR, TAG, "add has failed");
463 // add data to send thread
464 data = CAGetDataSetFromBlockDataList(blockID);
467 OIC_LOG(ERROR, TAG, "it's unavailable");
468 return CA_STATUS_FAILED;
471 if (data->requestInfo)
473 data->requestInfo->info.messageId = pdu->hdr->id;
476 if (data->responseInfo)
478 data->responseInfo->info.messageId = pdu->hdr->id;
481 res = CAAddSendThreadQueue(data, blockID);
482 if (CA_STATUS_OK != res)
484 OIC_LOG(ERROR, TAG, "add has failed");
492 case CA_SENT_PREVIOUS_NON_MSG:
493 res = CASendBlockMessage(pdu, CA_MSG_CONFIRM, blockWiseStatus,
495 if (CA_STATUS_OK != res)
497 OIC_LOG(ERROR, TAG, "send has failed");
502 case CA_OPTION2_LAST_BLOCK:
503 // process last block and send upper layer
504 res = CAReceiveLastBlock(blockID, receivedData);
505 if (CA_STATUS_OK != res)
507 OIC_LOG(ERROR, TAG, "receive has failed");
511 // remove data from list
512 res = CARemoveBlockDataFromList(blockID);
513 if (CA_STATUS_OK != res)
515 OIC_LOG(ERROR, TAG, "remove has failed");
520 case CA_OPTION1_NO_ACK_LAST_BLOCK:
521 // process last block and send upper layer
522 res = CAReceiveLastBlock(blockID, receivedData);
523 if (CA_STATUS_OK != res)
525 OIC_LOG(ERROR, TAG, "receive has failed");
529 if (CA_MSG_NONCONFIRM == pdu->hdr->type)
531 // remove data from list
532 res = CARemoveBlockDataFromList(blockID);
533 if (CA_STATUS_OK != res)
535 OIC_LOG(ERROR, TAG, "remove has failed");
541 case CA_OPTION1_NO_ACK_BLOCK:
542 if (CA_MSG_CONFIRM == pdu->hdr->type)
544 // add data to send thread
545 res = CASendBlockMessage(pdu, CA_MSG_ACKNOWLEDGE, blockWiseStatus,
547 if (CA_STATUS_OK != res)
549 OIC_LOG(ERROR, TAG, "send has failed");
555 case CA_BLOCK_INCOMPLETE:
556 if (CA_MSG_CONFIRM == pdu->hdr->type || CA_MSG_ACKNOWLEDGE == pdu->hdr->type)
558 // add data to send thread
559 res = CASendErrorMessage(pdu, blockWiseStatus,
560 CA_REQUEST_ENTITY_INCOMPLETE,
562 if (CA_STATUS_OK != res)
564 OIC_LOG(ERROR, TAG, "send has failed");
570 case CA_BLOCK_TOO_LARGE:
571 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->type)
573 res = CASendBlockMessage(pdu, CA_MSG_CONFIRM, blockWiseStatus,
575 if (CA_STATUS_OK != res)
577 OIC_LOG(ERROR, TAG, "send has failed");
581 else if (CA_MSG_CONFIRM == pdu->hdr->type)
583 res = CASendErrorMessage(pdu, blockWiseStatus,
584 CA_REQUEST_ENTITY_TOO_LARGE,
586 if (CA_STATUS_OK != res)
588 OIC_LOG(ERROR, TAG, "send has failed");
594 OIC_LOG_V(ERROR, TAG, "no logic [%d]", blockWiseStatus);
599 CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType,
600 uint8_t status, const CABlockDataID_t *blockID)
602 VERIFY_NON_NULL(pdu, TAG, "pdu");
603 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
604 VERIFY_NON_NULL(blockID, TAG, "blockID");
606 CAData_t *data = CAGetDataSetFromBlockDataList(blockID);
609 OIC_LOG(ERROR, TAG, "CAData is unavailable");
610 return CA_STATUS_FAILED;
613 if (CA_MSG_CONFIRM == msgType)
615 OIC_LOG(DEBUG, TAG, "need new msgID");
616 if (data->requestInfo)
618 data->requestInfo->info.messageId = 0;
621 if (data->responseInfo)
623 data->responseInfo->info.messageId = 0;
626 else if (CA_MSG_ACKNOWLEDGE == msgType)
628 if (data->responseInfo)
630 OIC_LOG(DEBUG, TAG, "set ACK message");
631 data->responseInfo->info.messageId = pdu->hdr->id;
632 data->responseInfo->info.type = CA_MSG_ACKNOWLEDGE;
633 if (CA_OPTION1_NO_ACK_LAST_BLOCK == status)
635 data->responseInfo->result = CA_CHANGED;
637 else if (CA_OPTION1_NO_ACK_BLOCK == status)
639 data->responseInfo->result = CA_CONTINUE;
644 // add data to send thread
645 CAResult_t res = CAAddSendThreadQueue(data, blockID);
646 if (CA_STATUS_OK != res)
648 OIC_LOG(ERROR, TAG, "add has failed");
654 CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status,
655 CAResponseResult_t responseResult,
656 const CABlockDataID_t *blockID)
658 VERIFY_NON_NULL(pdu, TAG, "pdu");
659 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
660 VERIFY_NON_NULL(blockID, TAG, "blockID");
662 // create error responseInfo
663 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockID);
666 OIC_LOG(ERROR, TAG, "data is unavailable");
667 return CA_STATUS_FAILED;
670 CAData_t *cloneData = NULL;
671 if (data->sentData && data->sentData->responseInfo)
673 data->sentData->responseInfo->info.messageId = pdu->hdr->id;
674 data->sentData->responseInfo->info.type = CA_MSG_ACKNOWLEDGE;
675 data->sentData->responseInfo->result = responseResult;
676 cloneData = CACloneCAData(data->sentData);
679 OIC_LOG(ERROR, TAG, "clone has failed");
680 return CA_MEMORY_ALLOC_FAILED;
682 OIC_LOG(DEBUG, TAG, "set ACK message");
684 else if (data->sentData)
686 cloneData = CACreateNewDataSet(pdu, data->sentData->remoteEndpoint);
689 OIC_LOG(ERROR, TAG, PCF("CACreateNewDataSet failed"));
690 return CA_MEMORY_ALLOC_FAILED;
693 cloneData->responseInfo->info.type = CA_MSG_CONFIRM;
694 cloneData->responseInfo->result = responseResult;
695 OIC_LOG(DEBUG, TAG, "set CON message");
699 OIC_LOG(ERROR, TAG, "data has no sent-data");
700 return CA_MEMORY_ALLOC_FAILED;
703 // add data to send thread
704 if (g_context.sendThreadFunc)
706 ca_mutex_lock(g_context.blockDataSenderMutex);
707 g_context.sendThreadFunc(cloneData);
708 ca_mutex_unlock(g_context.blockDataSenderMutex);
712 CADestroyDataSet(cloneData);
715 // if error code is 4.08, remove the stored payload and initialize block number
716 if (CA_BLOCK_INCOMPLETE == status)
718 OICFree(data->payload);
719 data->payload = NULL;
720 data->payloadLength = 0;
721 data->receivedPayloadLen = 0;
722 data->block1.num = 0;
723 data->block2.num = 0;
729 CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID,
730 const CAData_t *receivedData)
732 VERIFY_NON_NULL(blockID, TAG, "blockID");
733 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
735 // total block data have to notify to Application
736 CAData_t *cloneData = CACloneCAData(receivedData);
739 OIC_LOG(ERROR, TAG, "clone has failed");
740 return CA_MEMORY_ALLOC_FAILED;
744 size_t fullPayloadLen = 0;
745 CAPayload_t fullPayload = CAGetPayloadFromBlockDataList(blockID,
749 CAResult_t res = CAUpdatePayloadToCAData(cloneData, fullPayload, fullPayloadLen);
750 if (CA_STATUS_OK != res)
752 OIC_LOG(ERROR, TAG, "update has failed");
753 CADestroyDataSet(cloneData);
758 if (g_context.receivedThreadFunc)
760 g_context.receivedThreadFunc(cloneData);
764 CADestroyDataSet(cloneData);
770 // TODO make pdu const after libcoap is updated to support that.
771 CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
772 const CAData_t *receivedData, coap_block_t block,
775 OIC_LOG(INFO, TAG, "CASetNextBlockOption1");
776 VERIFY_NON_NULL(pdu, TAG, "pdu");
777 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
778 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
779 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
781 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
783 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
784 (CAToken_t)pdu->hdr->token,
785 pdu->hdr->token_length,
788 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
790 OIC_LOG(ERROR, TAG, "blockId is null");
791 CADestroyBlockID(blockDataID);
792 return CA_STATUS_FAILED;
795 // BlockData data is created if it not existed
796 if (!CAIsBlockDataInList(blockDataID))
798 OIC_LOG(DEBUG, TAG, "no message in list");
800 CAData_t *data = CACreateNewDataSet(pdu, endpoint);
803 OIC_LOG(ERROR, TAG, "data is null");
804 CADestroyBlockID(blockDataID);
805 return CA_STATUS_FAILED;
808 CABlockData_t *currData = CACreateNewBlockData(data);
811 OIC_LOG(ERROR, TAG, "currData is null");
812 CADestroyDataSet(data);
813 CADestroyBlockID(blockDataID);
814 return CA_STATUS_FAILED;
816 CADestroyDataSet(data);
819 // update BLOCK OPTION1 type
820 CAResult_t res = CAUpdateBlockOptionType(blockDataID,
822 if (CA_STATUS_OK != res)
824 OIC_LOG(ERROR, TAG, "update has failed");
825 CARemoveBlockDataFromList(blockDataID);
826 CADestroyBlockID(blockDataID);
830 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
833 OIC_LOG(ERROR, TAG, "getting has failed");
834 CADestroyBlockID(blockDataID);
835 return CA_STATUS_FAILED;
838 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
839 // received type from remote device
840 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->type)
842 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->code);
844 (CA_REQUEST_ENTITY_INCOMPLETE != code && CA_REQUEST_ENTITY_TOO_LARGE != code))
846 OIC_LOG(INFO, TAG, "Data has sent");
847 // initialize block number for response message
848 data->block1.num = 0;
849 CADestroyBlockID(blockDataID);
853 blockWiseStatus = CA_OPTION1_ACK;
854 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus);
855 if (CA_STATUS_OK != res)
857 OIC_LOG(ERROR, TAG, "update has failed");
858 CADestroyBlockID(blockDataID);
862 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
863 if (CA_STATUS_OK != res)
865 OIC_LOG(ERROR, TAG, "update has failed");
866 CARemoveBlockDataFromList(blockDataID);
867 CADestroyBlockID(blockDataID);
871 else // CON or NON message
873 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
875 // check the size option
876 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
878 &(data->payloadLength));
880 // check if received payload is exact
881 if (CA_MSG_CONFIRM == pdu->hdr->type)
883 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
884 COAP_OPTION_BLOCK1, dataLen);
887 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
889 // store the received payload and merge
890 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
891 isSizeOption, COAP_OPTION_BLOCK1);
892 if (CA_STATUS_OK != res)
894 OIC_LOG(ERROR, TAG, "update has failed");
895 CARemoveBlockDataFromList(blockDataID);
896 CADestroyBlockID(blockDataID);
900 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus);
901 if (CA_STATUS_OK != res)
903 OIC_LOG(ERROR, TAG, "update has failed");
904 CARemoveBlockDataFromList(blockDataID);
905 CADestroyBlockID(blockDataID);
910 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
911 if (CA_STATUS_OK != res)
913 OIC_LOG(ERROR, TAG, "update has failed");
914 CARemoveBlockDataFromList(blockDataID);
915 CADestroyBlockID(blockDataID);
920 // check the blcok-wise transfer status for next step
921 if (CA_BLOCK_UNKNOWN == blockWiseStatus || CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
923 if (0 == block.m) // Last block is received
925 OIC_LOG(DEBUG, TAG, "M bit is 0");
926 blockWiseStatus = CA_OPTION1_NO_ACK_LAST_BLOCK;
930 OIC_LOG(DEBUG, TAG, "M bit is 1");
931 blockWiseStatus = CA_OPTION1_NO_ACK_BLOCK;
936 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
937 if (CA_STATUS_OK != res)
939 OIC_LOG(ERROR, TAG, "setting has failed");
940 CARemoveBlockDataFromList(blockDataID);
943 CADestroyBlockID(blockDataID);
947 // TODO make pdu const after libcoap is updated to support that.
948 CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
949 const CAData_t *receivedData, coap_block_t block,
952 OIC_LOG(DEBUG, TAG, "CASetNextBlockOption2");
953 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
955 VERIFY_NON_NULL(pdu, TAG, "pdu");
956 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
957 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
958 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
960 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
961 (CAToken_t)pdu->hdr->token,
962 pdu->hdr->token_length,
965 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
967 OIC_LOG(ERROR, TAG, "blockId is null");
968 CADestroyBlockID(blockDataID);
969 return CA_STATUS_FAILED;
972 // BlockData data is created if it not existed
973 if (!CAIsBlockDataInList(blockDataID))
975 OIC_LOG(DEBUG, TAG, "no msg in list.");
977 CAData_t *data = CACreateNewDataSet(pdu, endpoint);
980 OIC_LOG(ERROR, TAG, "data is null");
981 CADestroyBlockID(blockDataID);
982 return CA_STATUS_FAILED;
985 CABlockData_t *currData = CACreateNewBlockData(data);
988 OIC_LOG(ERROR, TAG, "data is null");
989 CADestroyDataSet(data);
990 CADestroyBlockID(blockDataID);
991 return CA_STATUS_FAILED;
993 CADestroyDataSet(data);
996 // set Block Option Type
997 CAResult_t res = CAUpdateBlockOptionType(blockDataID,
999 if (CA_STATUS_OK != res)
1001 OIC_LOG(ERROR, TAG, "update has failed");
1002 CARemoveBlockDataFromList(blockDataID);
1003 CADestroyBlockID(blockDataID);
1007 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
1010 OIC_LOG(ERROR, TAG, "getting has failed");
1011 CARemoveBlockDataFromList(blockDataID);
1012 CADestroyBlockID(blockDataID);
1013 return CA_STATUS_FAILED;
1016 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
1017 if (0 == block.num && CA_GET == pdu->hdr->code && 0 == block.m)
1019 OIC_LOG(INFO, TAG, "first block number");
1021 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1022 if (CA_STATUS_OK != res)
1024 OIC_LOG(ERROR, TAG, "update has failed");
1025 CARemoveBlockDataFromList(blockDataID);
1026 CADestroyBlockID(blockDataID);
1030 // first block data have to notify to Application
1031 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1032 if (CA_STATUS_OK != res)
1034 OIC_LOG(ERROR, TAG, "update has failed");
1035 CARemoveBlockDataFromList(blockDataID);
1036 CADestroyBlockID(blockDataID);
1039 blockWiseStatus = CA_OPTION2_FIRST_BLOCK;
1043 // received type from remote device
1044 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->type ||
1045 (CA_MSG_NONCONFIRM == pdu->hdr->type && NULL != receivedData->responseInfo))
1047 OIC_LOG(DEBUG, TAG, "received ACK or NON");
1049 // check the size option
1050 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
1052 &(data->payloadLength));
1054 // check if received payload is exact
1055 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->type)
1057 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
1058 COAP_OPTION_BLOCK2, dataLen);
1061 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
1063 // store the received payload and merge
1064 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
1065 isSizeOption, COAP_OPTION_BLOCK2);
1066 if (CA_STATUS_OK != res)
1068 OIC_LOG(ERROR, TAG, "update has failed");
1069 CARemoveBlockDataFromList(blockDataID);
1070 CADestroyBlockID(blockDataID);
1075 if (0 == block.m && CA_BLOCK_UNKNOWN == blockWiseStatus) // Last block is received
1077 OIC_LOG(DEBUG, TAG, "M bit is 0");
1078 blockWiseStatus = CA_OPTION2_LAST_BLOCK;
1082 if (CA_BLOCK_UNKNOWN == blockWiseStatus ||
1083 CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
1085 OIC_LOG(DEBUG, TAG, "M bit is 1");
1087 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->type)
1089 blockWiseStatus = CA_OPTION2_ACK;
1093 blockWiseStatus = CA_OPTION2_NON;
1097 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2,
1099 if (CA_STATUS_OK != res)
1101 OIC_LOG(ERROR, TAG, "update has failed");
1102 CARemoveBlockDataFromList(blockDataID);
1103 CADestroyBlockID(blockDataID);
1107 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1108 if (CA_STATUS_OK != res)
1110 OIC_LOG(ERROR, TAG, "update has failed");
1111 CARemoveBlockDataFromList(blockDataID);
1112 CADestroyBlockID(blockDataID);
1117 else // CON message and so on.
1119 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
1121 blockWiseStatus = CA_OPTION2_CON;
1123 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1124 if (CA_STATUS_OK != res)
1126 OIC_LOG(ERROR, TAG, "update has failed");
1127 CARemoveBlockDataFromList(blockDataID);
1128 CADestroyBlockID(blockDataID);
1132 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1133 if (CA_STATUS_OK != res)
1135 OIC_LOG(ERROR, TAG, "update has failed");
1136 CARemoveBlockDataFromList(blockDataID);
1137 CADestroyBlockID(blockDataID);
1143 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
1144 if (CA_STATUS_OK != res)
1146 OIC_LOG(ERROR, TAG, "setting has failed");
1147 CARemoveBlockDataFromList(blockDataID);
1148 CADestroyBlockID(blockDataID);
1152 CADestroyBlockID(blockDataID);
1153 return CA_STATUS_OK;
1156 CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu,
1157 coap_block_t *block, uint16_t blockType,
1160 VERIFY_NON_NULL(currData, TAG, "currData");
1161 VERIFY_NON_NULL(pdu, TAG, "pdu");
1162 VERIFY_NON_NULL(block, TAG, "block");
1164 // update block data
1165 CAResult_t res = CA_STATUS_OK;
1166 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->code);
1168 if (CA_REQUEST_ENTITY_INCOMPLETE == code || CA_REQUEST_ENTITY_TOO_LARGE == code)
1170 // response error code of the received block message
1171 res = CAHandleBlockErrorResponse(block, blockType, code);
1172 if (CA_STATUS_OK != res)
1174 OIC_LOG(ERROR, TAG, "error handle has failed");
1180 // update block option items
1183 case CA_OPTION1_ACK:
1184 if (currData->block1.num > block->num)
1186 OIC_LOG(ERROR, TAG, "received incorrect block num");
1187 return CA_STATUS_FAILED;
1191 case CA_OPTION2_NON:
1195 case CA_OPTION2_CON:
1198 case CA_OPTION2_ACK:
1199 if (currData->block2.num > block->num)
1201 OIC_LOG(ERROR, TAG, "received incorrect block num");
1202 return CA_STATUS_FAILED;
1207 case CA_BLOCK_TOO_LARGE:
1208 // if state of received block message is CA_BLOCK_TOO_LARGE or CA_BLOCK_INCOMPLETE
1209 // we set the response error code appropriately and send
1210 if (COAP_OPTION_BLOCK2 == blockType)
1214 block->szx = currData->block2.szx;
1218 block->szx = currData->block1.szx;
1222 OIC_LOG_V(ERROR, TAG, "no logic [%d]", status);
1225 if (CA_BLOCK_INCOMPLETE != status && CA_BLOCK_TOO_LARGE != status)
1227 // negotiate block size
1228 res = CANegotiateBlockSize(currData, block, pdu->hdr->type, blockType);
1229 if (CA_STATUS_OK != res)
1231 OIC_LOG(ERROR, TAG, "negotiation has failed");
1239 CAResult_t CAGetMoreBitFromBlock(size_t payloadLen, coap_block_t *block)
1241 VERIFY_NON_NULL(block, TAG, "block");
1243 if ((size_t)((block->num + 1) << (block->szx + BLOCK_NUMBER_IDX))
1246 OIC_LOG(DEBUG, TAG, "Set the M-bit(1)");
1251 OIC_LOG(DEBUG, TAG, "Set the M-bit(0)");
1255 return CA_STATUS_OK;
1258 CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block,
1259 CAMessageType_t msgType, uint16_t blockType)
1261 OIC_LOG(DEBUG, TAG, "IN-NegotiateBlockSize");
1263 VERIFY_NON_NULL(currData, TAG, "currData");
1264 VERIFY_NON_NULL(block, TAG, "block");
1266 // #1. check the block option type
1267 if (COAP_OPTION_BLOCK2 == blockType)
1269 // #2. check the message type
1270 if (CA_MSG_ACKNOWLEDGE == msgType)
1272 if (block->szx > currData->block2.szx)
1274 OIC_LOG(DEBUG, TAG, "sze is big");
1276 // #3. calculate new block number from block size
1277 unsigned int blockNum = BLOCK_SIZE(block->szx) /
1278 BLOCK_SIZE(currData->block2.szx) - 1;
1279 OIC_LOG(DEBUG, TAG, "num is set as Negotiation");
1280 block->num += blockNum;
1281 block->szx = currData->block2.szx;
1282 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1287 if (block->szx > currData->block2.szx)
1289 OIC_LOG(DEBUG, TAG, "sze is big");
1290 block->szx = currData->block2.szx;
1294 else if (COAP_OPTION_BLOCK1 == blockType)
1296 if (CA_MSG_ACKNOWLEDGE == msgType)
1298 if (block->szx < currData->block1.szx)
1300 OIC_LOG(DEBUG, TAG, "sze is small");
1302 unsigned int blockNum = BLOCK_SIZE(currData->block1.szx) /
1303 BLOCK_SIZE(block->szx) - 1;
1304 block->num += blockNum;
1305 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1310 if (block->szx > currData->block1.szx)
1312 OIC_LOG(DEBUG, TAG, "sze is big");
1313 block->szx = currData->block1.szx;
1319 OIC_LOG(DEBUG, TAG, "Invalid block option");
1320 return CA_STATUS_FAILED;
1323 OIC_LOG(DEBUG, TAG, "OUT-NegotiateBlockSize");
1325 return CA_STATUS_OK;
1328 CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block,
1331 VERIFY_NON_NULL(currData, TAG, "currData");
1333 // check if block size is bigger than CABlockSize_t
1334 if (block.szx > CA_BLOCK_SIZE_1024_BYTE)
1336 OIC_LOG(DEBUG, TAG, "invalid block szx");
1337 return CA_STATUS_FAILED;
1340 // update block option
1341 if (COAP_OPTION_BLOCK2 == blockType)
1343 currData->block2 = block;
1347 currData->block1 = block;
1350 OIC_LOG(DEBUG, TAG, "data has updated");
1351 return CA_STATUS_OK;
1354 CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID)
1356 VERIFY_NON_NULL(pdu, TAG, "pdu");
1357 VERIFY_NON_NULL(blockID, TAG, "blockID");
1359 // if CON message is sent, update messageId in block-wise transfer list
1360 if (CA_MSG_CONFIRM == pdu->hdr->type)
1362 CAData_t * cadata = CAGetDataSetFromBlockDataList(blockID);
1365 OIC_LOG(ERROR, TAG, "CAData is unavailable");
1366 return CA_STATUS_FAILED;
1369 if (cadata->requestInfo)
1371 cadata->requestInfo->info.messageId = pdu->hdr->id;
1375 return CA_STATUS_OK;
1378 CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t info,
1379 const CAEndpoint_t *endpoint)
1381 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption");
1382 VERIFY_NON_NULL(pdu, TAG, "pdu");
1383 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1384 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1385 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1387 size_t dataLength = 0;
1390 dataLength = info.payloadSize;
1391 OIC_LOG_V(DEBUG, TAG, "dataLength - %d", dataLength);
1394 OIC_LOG_V(DEBUG, TAG, "previous payload - %s", (*pdu)->data);
1396 uint32_t code = CA_RESPONSE_CODE((*pdu)->hdr->code);
1397 if (CA_REQUEST_ENTITY_INCOMPLETE == code)
1399 OIC_LOG(INFO, TAG, "don't use option");
1400 return CA_STATUS_OK;
1403 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
1404 (CAToken_t)(*pdu)->hdr->token,
1405 (*pdu)->hdr->token_length,
1408 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
1410 OIC_LOG(ERROR, TAG, "blockId is null");
1411 CADestroyBlockID(blockDataID);
1412 return CA_STATUS_FAILED;
1415 uint8_t blockType = CAGetBlockOptionType(blockDataID);
1416 if (COAP_OPTION_BLOCK2 == blockType)
1418 CAResult_t res = CAAddBlockOption2(pdu, info, dataLength,
1420 if (CA_STATUS_OK != res)
1422 OIC_LOG(ERROR, TAG, "add has failed");
1423 CADestroyBlockID(blockDataID);
1427 else if (COAP_OPTION_BLOCK1 == blockType)
1429 CAResult_t res = CAAddBlockOption1(pdu, info, dataLength,
1431 if (CA_STATUS_OK != res)
1433 OIC_LOG(ERROR, TAG, "add has failed");
1434 CADestroyBlockID(blockDataID);
1440 OIC_LOG(DEBUG, TAG, "no BLOCK option");
1441 // if response data is so large. it have to send as block transfer
1442 if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info.payload))
1444 OIC_LOG(INFO, TAG, "it have to use block");
1448 OIC_LOG(INFO, TAG, "not Blockwise Transfer");
1449 CADestroyBlockID(blockDataID);
1450 return CA_STATUS_OK;
1454 CAResult_t res = CAUpdateMessageId(*pdu, blockDataID);
1455 if (CA_STATUS_OK != res)
1457 OIC_LOG(ERROR, TAG, "fail to update CON message id ");
1458 CADestroyBlockID(blockDataID);
1462 CADestroyBlockID(blockDataID);
1463 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption");
1464 return CA_STATUS_OK;
1467 CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t info, size_t dataLength,
1468 const CABlockDataID_t *blockID)
1470 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption2");
1471 VERIFY_NON_NULL(pdu, TAG, "pdu");
1472 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1473 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1474 VERIFY_NON_NULL(blockID, TAG, "blockID");
1476 // get set block data from CABlock list-set.
1477 coap_block_t *block1 = CAGetBlockOption(blockID,
1478 COAP_OPTION_BLOCK1);
1479 coap_block_t *block2 = CAGetBlockOption(blockID,
1480 COAP_OPTION_BLOCK2);
1481 if (!block1 || !block2)
1483 OIC_LOG(ERROR, TAG, "getting has failed");
1484 return CA_STATUS_FAILED;
1487 CALogBlockInfo(block2);
1490 if (CA_MSG_ACKNOWLEDGE == (*pdu)->hdr->type ||
1491 (CA_MSG_NONCONFIRM == (*pdu)->hdr->type && CA_GET != (*pdu)->hdr->code))
1493 int32_t res = coap_write_block_opt(block2, COAP_OPTION_BLOCK2, *pdu, dataLength);
1496 case -2: /* illegal block */
1497 code = COAP_RESPONSE_CODE(CA_BAD_REQ);
1498 OIC_LOG(ERROR, TAG, "write block option : -2");
1500 case -1: /* should really not happen */
1501 OIC_LOG(ERROR, TAG, "write block option : -1");
1503 case -3: /* cannot handle request */
1504 code = COAP_RESPONSE_CODE(CA_INTERNAL_SERVER_ERROR);
1505 OIC_LOG(ERROR, TAG, "write block option : -3");
1508 OIC_LOG(INFO, TAG, "success write block option");
1510 CALogBlockInfo(block2);
1514 OIC_LOG(DEBUG, TAG, "combining block1 and block2");
1515 CAResult_t res = CAAddBlockOptionImpl(*pdu, block1, COAP_OPTION_BLOCK1);
1516 if (CA_STATUS_OK != res)
1518 OIC_LOG(ERROR, TAG, "add has failed");
1519 CARemoveBlockDataFromList(blockID);
1522 // initialize block number
1526 // if block number is 0, add size2 option
1527 if (0 == block2->num)
1529 res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE2, dataLength);
1530 if (CA_STATUS_OK != res)
1532 OIC_LOG(ERROR, TAG, "add has failed");
1533 CARemoveBlockDataFromList(blockID);
1538 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info.payload,
1539 block2->num, block2->szx))
1541 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1542 return CA_STATUS_FAILED;
1547 // if sent message is last response block message, remove data
1548 CARemoveBlockDataFromList(blockID);
1552 if (CA_MSG_NONCONFIRM == (*pdu)->hdr->type)
1554 OIC_LOG(DEBUG, TAG, "NON, send next block..");
1555 // update block data
1557 CAResult_t res = CAProcessNextStep(*pdu, NULL,
1558 CA_SENT_PREVIOUS_NON_MSG,
1560 if (CA_STATUS_OK != res)
1562 OIC_LOG(ERROR, TAG, "failed to process next step");
1563 CARemoveBlockDataFromList(blockID);
1571 OIC_LOG(DEBUG, TAG, "option2, not ACK msg");
1572 CAResult_t res = CAAddBlockOptionImpl(*pdu, block2, COAP_OPTION_BLOCK2);
1573 if (CA_STATUS_OK != res)
1575 OIC_LOG(ERROR, TAG, "add has failed");
1576 CARemoveBlockDataFromList(blockID);
1581 return CA_STATUS_OK;
1584 OIC_LOG_V(ERROR, TAG, "error : %d", code);
1586 char* phrase = coap_response_phrase(code);
1589 coap_add_data(*pdu, strlen(phrase),
1590 (unsigned char *) phrase);
1592 return CA_STATUS_FAILED;
1595 CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t info, size_t dataLength,
1596 const CABlockDataID_t *blockID)
1598 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption1");
1599 VERIFY_NON_NULL(pdu, TAG, "pdu");
1600 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1601 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1602 VERIFY_NON_NULL(blockID, TAG, "blockID");
1604 // get set block data from CABlock list-set.
1605 coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1608 OIC_LOG(ERROR, TAG, "getting has failed");
1609 return CA_STATUS_FAILED;
1612 CALogBlockInfo(block1);
1614 if (CA_MSG_ACKNOWLEDGE == (*pdu)->hdr->type)
1616 OIC_LOG(DEBUG, TAG, "option1 and ACK msg..");
1617 CAResult_t res = CAAddBlockOptionImpl(*pdu, block1, COAP_OPTION_BLOCK1);
1618 if (CA_STATUS_OK != res)
1620 OIC_LOG(ERROR, TAG, "add has failed");
1621 CARemoveBlockDataFromList(blockID);
1625 // reset block-list after write block
1628 // remove data from list
1629 CAResult_t res = CARemoveBlockDataFromList(blockID);
1630 if (CA_STATUS_OK != res)
1632 OIC_LOG(ERROR, TAG, "remove has failed");
1639 CAGetMoreBitFromBlock(dataLength, block1);
1640 CAResult_t res = CAAddBlockOptionImpl(*pdu, block1, COAP_OPTION_BLOCK1);
1641 if (CA_STATUS_OK != res)
1643 OIC_LOG(ERROR, TAG, "add has failed");
1644 CARemoveBlockDataFromList(blockID);
1647 CALogBlockInfo(block1);
1649 // if block number is 0, add size1 option
1650 if (0 == block1->num)
1652 res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE1, dataLength);
1653 if (CA_STATUS_OK != res)
1655 OIC_LOG(ERROR, TAG, "add has failed");
1656 CARemoveBlockDataFromList(blockID);
1661 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info.payload,
1662 block1->num, block1->szx))
1664 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1665 return CA_STATUS_FAILED;
1668 // check the message type and if message type is NON, next block message will be sent
1669 if (CA_MSG_NONCONFIRM == (*pdu)->hdr->type)
1673 OIC_LOG(DEBUG, TAG, "NON, send next block..");
1674 // update block data
1676 CAResult_t res = CAProcessNextStep(*pdu, NULL,
1677 CA_SENT_PREVIOUS_NON_MSG,
1679 if (CA_STATUS_OK != res)
1681 OIC_LOG(ERROR, TAG, "failed to process next step");
1682 CARemoveBlockDataFromList(blockID);
1688 CARemoveBlockDataFromList(blockID);
1693 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption1");
1695 return CA_STATUS_OK;
1698 CAResult_t CAAddBlockOptionImpl(coap_pdu_t *pdu, coap_block_t *block, uint8_t blockType)
1700 OIC_LOG(DEBUG, TAG, "IN-AddBlockOptionImpl");
1701 VERIFY_NON_NULL(pdu, TAG, "pdu");
1702 VERIFY_NON_NULL(block, TAG, "block");
1704 coap_option *option = (coap_option *) OICMalloc(sizeof(coap_option));
1707 OIC_LOG(ERROR, TAG, "out of memory");
1708 return CA_MEMORY_ALLOC_FAILED;
1711 unsigned char buf[BLOCKWISE_OPTION_BUFFER] = { 0 };
1712 option->key = blockType;
1713 option->length = coap_encode_var_bytes(buf,
1714 ((block->num << BLOCK_NUMBER_IDX)
1715 | (block->m << BLOCK_M_BIT_IDX) | block->szx));
1716 if (!coap_add_option(pdu, option->key, option->length, buf))
1718 OIC_LOG(ERROR, TAG, "coap_add_option has failed");
1720 return CA_STATUS_FAILED;
1725 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOptionImpl");
1726 return CA_STATUS_OK;
1729 CAResult_t CAAddBlockSizeOption(coap_pdu_t *pdu, uint16_t sizeType, size_t dataLength)
1731 OIC_LOG(DEBUG, TAG, "IN-CAAddBlockSizeOption");
1732 VERIFY_NON_NULL(pdu, TAG, "pdu");
1734 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1736 OIC_LOG(ERROR, TAG, "unknown option type");
1737 return CA_STATUS_FAILED;
1740 unsigned char value[BLOCKWISE_OPTION_BUFFER] = { 0 };
1741 unsigned int optionLength = coap_encode_var_bytes(value, dataLength);
1743 if (!coap_add_option(pdu, sizeType, optionLength, value))
1745 OIC_LOG(ERROR, TAG, "failed to add size option");
1746 return CA_STATUS_FAILED;
1749 OIC_LOG(DEBUG, TAG, "OUT-CAAddBlockSizeOption");
1751 return CA_STATUS_OK;
1754 // TODO make pdu const after libcoap is updated to support that.
1755 bool CAIsPayloadLengthInPduWithBlockSizeOption(coap_pdu_t *pdu,
1757 size_t *totalPayloadLen)
1759 OIC_LOG(DEBUG, TAG, "IN-CAIsPayloadLengthInPduWithBlockSizeOption");
1760 VERIFY_NON_NULL(pdu, TAG, "pdu");
1761 VERIFY_NON_NULL(totalPayloadLen, TAG, "totalPayloadLen");
1763 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1765 OIC_LOG(ERROR, TAG, "unknown option type");
1766 return CA_STATUS_FAILED;
1769 coap_opt_iterator_t opt_iter;
1770 coap_opt_t *option = coap_check_option(pdu, sizeType, &opt_iter);
1773 OIC_LOG(DEBUG, TAG, "get size option from pdu");
1774 *totalPayloadLen = coap_decode_var_bytes(COAP_OPT_VALUE(option),
1775 COAP_OPT_LENGTH(option));
1777 OIC_LOG_V(DEBUG, TAG, "the total payload length to be received is [%d]bytes",
1783 OIC_LOG(DEBUG, TAG, "OUT-CAIsPayloadLengthInPduWithBlockSizeOption");
1788 uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlock,
1789 const CAData_t *receivedData, uint16_t blockType,
1792 OIC_LOG(DEBUG, TAG, "IN-CheckBlockError");
1794 VERIFY_NON_NULL(currData, TAG, "currData is NULL");
1795 VERIFY_NON_NULL(receivedBlock, TAG, "receivedBlock is NULL");
1796 VERIFY_NON_NULL(receivedData, TAG, "receivedData is NULL");
1798 // #1. check the received payload length
1799 size_t blockPayloadLen = 0;
1800 CAGetPayloadInfo(receivedData, &blockPayloadLen);
1802 // #2. check if the block sequence is right
1803 if (COAP_OPTION_BLOCK1 == blockType)
1805 size_t prePayloadLen = currData->receivedPayloadLen;
1806 if (prePayloadLen != (size_t)BLOCK_SIZE(receivedBlock->szx)
1807 * receivedBlock->num)
1809 if (receivedBlock->num > currData->block1.num + 1)
1811 // 408 Error handling of block loss
1812 OIC_LOG(ERROR, TAG, "option1: error 4.08");
1813 OIC_LOG(ERROR, TAG, "it didn't order");
1814 return CA_BLOCK_INCOMPLETE;
1816 return CA_BLOCK_RECEIVED_ALREADY;
1819 else if (COAP_OPTION_BLOCK2 == blockType)
1821 if (receivedBlock->num != currData->block2.num)
1823 if (receivedBlock->num > currData->block2.num)
1825 // 408 Error handling of block loss
1826 OIC_LOG(ERROR, TAG, "option2: error 4.08");
1827 OIC_LOG(ERROR, TAG, "it didn't order");
1828 return CA_BLOCK_INCOMPLETE;
1830 return CA_BLOCK_RECEIVED_ALREADY;
1834 // #3. check if error check logic is required
1835 size_t optionLen = dataLen - blockPayloadLen;
1836 if (receivedBlock->m && blockPayloadLen !=
1837 (size_t)BLOCK_SIZE(receivedBlock->szx))
1839 // 413 Error handling of too large entity
1840 if (COAP_MAX_PDU_SIZE < BLOCK_SIZE(receivedBlock->szx) + optionLen)
1842 // buffer size is smaller than received block size
1843 OIC_LOG(ERROR, TAG, "error type 4.13");
1844 OIC_LOG(ERROR, TAG, "too large size");
1846 // set the block size to be smaller than COAP_MAX_PDU_SIZE
1847 for (int32_t size = CA_DEFAULT_BLOCK_SIZE; size >= 0; size--)
1849 if (COAP_MAX_PDU_SIZE >= BLOCK_SIZE(size) + optionLen)
1851 OIC_LOG_V(ERROR, TAG, "replace sze with %d", size);
1852 if (COAP_OPTION_BLOCK2 == blockType)
1854 currData->block2.szx = size;
1858 currData->block1.szx = size;
1863 return CA_BLOCK_TOO_LARGE;
1867 // 408 Error handling of payload loss
1868 OIC_LOG(ERROR, TAG, "error type 4.08");
1869 OIC_LOG(ERROR, TAG, "payload len != block sze");
1870 return CA_BLOCK_INCOMPLETE;
1873 else if (0 == receivedBlock->m && 0 != currData->payloadLength)
1875 // if the received block is last block, check the total payload length
1876 size_t receivedPayloadLen = currData->receivedPayloadLen;
1877 receivedPayloadLen += blockPayloadLen;
1879 if (receivedPayloadLen != currData->payloadLength)
1881 OIC_LOG(ERROR, TAG, "error type 4.08");
1882 OIC_LOG(ERROR, TAG, "total payload length is wrong");
1883 return CA_BLOCK_INCOMPLETE;
1887 OIC_LOG(DEBUG, TAG, "received all data normally");
1889 OIC_LOG(DEBUG, TAG, "OUT-CheckBlockError");
1891 return CA_BLOCK_UNKNOWN;
1894 CAResult_t CAUpdatePayloadData(CABlockData_t *currData, const CAData_t *receivedData,
1895 uint8_t status, bool isSizeOption, uint16_t blockType)
1897 OIC_LOG(DEBUG, TAG, "IN-UpdatePayloadData");
1899 VERIFY_NON_NULL(currData, TAG, "currData");
1900 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
1902 // if error code is 4.08, do not update payload
1903 if (CA_BLOCK_INCOMPLETE == status)
1905 OIC_LOG(ERROR, TAG, "no require to update");
1906 return CA_STATUS_OK;
1909 size_t blockPayloadLen = 0;
1910 CAPayload_t blockPayload = CAGetPayloadInfo(receivedData, &blockPayloadLen);
1912 if (CA_BLOCK_TOO_LARGE == status)
1914 blockPayloadLen = (COAP_OPTION_BLOCK2 == blockType) ?
1915 BLOCK_SIZE(currData->block2.szx) : BLOCK_SIZE(currData->block1.szx);
1918 // memory allocation for the received block payload
1919 size_t prePayloadLen = currData->receivedPayloadLen;
1922 if (currData->payloadLength)
1924 // in case the block message has the size option
1925 // allocate the memory for the total payload
1928 CAPayload_t prePayload = currData->payload;
1930 OIC_LOG(DEBUG, TAG, "allocate memory for the total payload");
1931 currData->payload = (CAPayload_t) OICCalloc(1, currData->payloadLength);
1932 if (NULL == currData->payload)
1934 OIC_LOG(ERROR, TAG, "out of memory");
1935 return CA_MEMORY_ALLOC_FAILED;
1937 memcpy(currData->payload, prePayload, prePayloadLen);
1938 OICFree(prePayload);
1941 // update the total payload
1942 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
1946 OIC_LOG(DEBUG, TAG, "allocate memory for the received block payload");
1948 size_t totalPayloadLen = prePayloadLen + blockPayloadLen;
1949 CAPayload_t newPayload = OICRealloc(currData->payload, totalPayloadLen);
1950 if (NULL == newPayload)
1952 OIC_LOG(ERROR, TAG, "out of memory");
1953 return CA_MEMORY_ALLOC_FAILED;
1956 // update the total payload
1957 memset(newPayload + prePayloadLen, 0, blockPayloadLen);
1958 currData->payload = newPayload;
1959 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
1962 // update received payload length
1963 currData->receivedPayloadLen += blockPayloadLen;
1965 OIC_LOG_V(DEBUG, TAG, "updated payload: %s, len: %d", currData->payload,
1966 currData->receivedPayloadLen);
1969 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayloadData");
1970 return CA_STATUS_OK;
1973 CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
1975 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", NULL);
1976 VERIFY_NON_NULL_RET(pdu->hdr, TAG, "pdu->hdr", NULL);
1977 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
1979 CAInfo_t responseData = { .tokenLength = pdu->hdr->token_length };
1980 responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength);
1981 if (!responseData.token)
1983 OIC_LOG(ERROR, TAG, "out of memory");
1986 memcpy(responseData.token, pdu->hdr->token, responseData.tokenLength);
1988 CAResponseInfo_t* responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
1991 OIC_LOG(ERROR, TAG, "out of memory");
1992 OICFree(responseData.token);
1995 responseInfo->info = responseData;
1997 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2000 OIC_LOG(ERROR, TAG, "out of memory");
2001 OICFree(responseInfo);
2005 data->requestInfo = NULL;
2006 data->responseInfo = responseInfo;
2007 data->remoteEndpoint = CACloneEndpoint(endpoint);
2008 data->type = SEND_TYPE_UNICAST;
2013 CAData_t *CACloneCAData(const CAData_t *data)
2015 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2017 CAData_t *clone = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2020 OIC_LOG(ERROR, TAG, "out of memory");
2025 if (data->requestInfo)
2027 clone->requestInfo = CACloneRequestInfo(data->requestInfo);
2030 if (data->responseInfo)
2032 clone->responseInfo = CACloneResponseInfo(data->responseInfo);
2035 if (data->remoteEndpoint)
2037 clone->remoteEndpoint = CACloneEndpoint(data->remoteEndpoint);
2043 CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
2046 OIC_LOG(DEBUG, TAG, "IN-UpdatePayload");
2048 VERIFY_NON_NULL(data, TAG, "data is NULL");
2049 VERIFY_NON_NULL(payload, TAG, "payload is NULL");
2051 if (data->requestInfo)
2053 // allocate payload field
2054 if (data->requestInfo->info.payload)
2056 char *temp = (char *) OICCalloc(payloadLen, sizeof(char));
2059 OIC_LOG(ERROR, TAG, "out of memory");
2060 return CA_STATUS_FAILED;
2062 memcpy(temp, payload, payloadLen);
2064 // save the full payload
2065 OICFree(data->requestInfo->info.payload);
2066 data->requestInfo->info.payload = (CAPayload_t) temp;
2068 data->requestInfo->info.payloadSize = payloadLen;
2071 if (data->responseInfo)
2073 // allocate payload field
2074 if (data->responseInfo->info.payload)
2076 char *temp = (char *) OICCalloc(payloadLen, sizeof(char));
2079 OIC_LOG(ERROR, TAG, "out of memory");
2080 return CA_STATUS_FAILED;
2082 memcpy(temp, payload, payloadLen);
2084 // save the full payload
2085 OICFree(data->responseInfo->info.payload);
2086 data->responseInfo->info.payload = (CAPayload_t) temp;
2088 data->responseInfo->info.payloadSize = payloadLen;
2091 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayload");
2093 return CA_STATUS_OK;
2096 CAPayload_t CAGetPayloadInfo(const CAData_t *data, size_t *payloadLen)
2098 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2099 VERIFY_NON_NULL_RET(payloadLen, TAG, "payloadLen", NULL);
2101 if (data->requestInfo)
2103 if (data->requestInfo->info.payload)
2105 *payloadLen = data->requestInfo->info.payloadSize;
2106 return data->requestInfo->info.payload;
2111 if (data->responseInfo->info.payload)
2113 *payloadLen = data->responseInfo->info.payloadSize;
2114 return data->responseInfo->info.payload;
2121 CAResult_t CAHandleBlockErrorResponse(coap_block_t *block, uint16_t blockType,
2122 uint32_t responseResult)
2124 OIC_LOG(DEBUG, TAG, "IN-HandleBlockErrorRes");
2125 VERIFY_NON_NULL(block, TAG, "block is NULL");
2127 // update block data
2128 switch (responseResult)
2130 case CA_REQUEST_ENTITY_INCOMPLETE:
2133 case CA_REQUEST_ENTITY_TOO_LARGE:
2134 if (COAP_OPTION_BLOCK1 == blockType)
2141 OIC_LOG_V(ERROR, TAG, "there is no Error Code of BWT[%d]", responseResult);
2144 OIC_LOG(DEBUG, TAG, "OUT-HandleBlockErrorRes");
2145 return CA_STATUS_OK;
2148 CAResult_t CAUpdateBlockOptionType(const CABlockDataID_t *blockID,
2151 OIC_LOG(DEBUG, TAG, "IN-UpdateBlockOptionType");
2152 VERIFY_NON_NULL(blockID, TAG, "blockID");
2154 ca_mutex_lock(g_context.blockDataListMutex);
2156 size_t len = u_arraylist_length(g_context.dataList);
2157 for (size_t i = 0; i < len; i++)
2159 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2160 if (CABlockidMatches(currData, blockID))
2162 currData->type = blockType;
2163 ca_mutex_unlock(g_context.blockDataListMutex);
2164 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2165 return CA_STATUS_OK;
2168 ca_mutex_unlock(g_context.blockDataListMutex);
2170 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2171 return CA_STATUS_FAILED;
2174 uint8_t CAGetBlockOptionType(const CABlockDataID_t *blockID)
2176 OIC_LOG(DEBUG, TAG, "IN-GetBlockOptionType");
2177 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", 0);
2179 ca_mutex_lock(g_context.blockDataListMutex);
2181 size_t len = u_arraylist_length(g_context.dataList);
2182 for (size_t i = 0; i < len; i++)
2184 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2185 if (CABlockidMatches(currData, blockID))
2187 ca_mutex_unlock(g_context.blockDataListMutex);
2188 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2189 return currData->type;
2192 ca_mutex_unlock(g_context.blockDataListMutex);
2194 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2198 CAData_t *CAGetDataSetFromBlockDataList(const CABlockDataID_t *blockID)
2200 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2202 ca_mutex_lock(g_context.blockDataListMutex);
2204 size_t len = u_arraylist_length(g_context.dataList);
2205 for (size_t i = 0; i < len; i++)
2207 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2208 if (CABlockidMatches(currData, blockID))
2210 ca_mutex_unlock(g_context.blockDataListMutex);
2211 return currData->sentData;
2214 ca_mutex_unlock(g_context.blockDataListMutex);
2219 CAResult_t CAGetTokenFromBlockDataList(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
2220 CAResponseInfo_t *responseInfo)
2222 OIC_LOG(DEBUG, TAG, "IN-CAGetTokenFromBlockDataList");
2223 VERIFY_NON_NULL(pdu, TAG, "pdu");
2224 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
2225 VERIFY_NON_NULL(responseInfo, TAG, "responseInfo");
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 (NULL == currData)
2238 if (NULL != currData->sentData && NULL != currData->sentData->requestInfo)
2240 if (pdu->hdr->id == currData->sentData->requestInfo->info.messageId &&
2241 endpoint->adapter == currData->sentData->remoteEndpoint->adapter)
2243 if (NULL != currData->sentData->requestInfo->info.token)
2245 uint8_t length = currData->sentData->requestInfo->info.tokenLength;
2246 responseInfo->info.tokenLength = length;
2247 responseInfo->info.token = (char *) OICMalloc(length);
2248 if (NULL == responseInfo->info.token)
2250 OIC_LOG(ERROR, TAG, "out of memory");
2251 ca_mutex_unlock(g_context.blockDataListMutex);
2252 return CA_MEMORY_ALLOC_FAILED;
2254 memcpy(responseInfo->info.token, currData->sentData->requestInfo->info.token,
2255 responseInfo->info.tokenLength);
2257 ca_mutex_unlock(g_context.blockDataListMutex);
2258 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2259 return CA_STATUS_OK;
2265 ca_mutex_unlock(g_context.blockDataListMutex);
2267 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2268 return CA_STATUS_OK;
2271 CAResult_t CACheckBlockDataValidation(const CAData_t *sendData, CABlockData_t **blockData)
2273 VERIFY_NON_NULL(sendData, TAG, "sendData");
2274 VERIFY_NON_NULL(blockData, TAG, "blockData");
2276 ca_mutex_lock(g_context.blockDataListMutex);
2278 size_t len = u_arraylist_length(g_context.dataList);
2279 for (size_t i = 0; i < len; i++)
2281 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2288 if (sendData->requestInfo) // sendData is requestMessage
2290 OIC_LOG(DEBUG, TAG, "Send request");
2291 if (NULL != currData->blockDataId
2292 && NULL != currData->blockDataId->id
2293 && currData->blockDataId->idLength > 0
2294 && NULL != sendData->requestInfo->info.token)
2296 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2297 (CAToken_t)sendData->requestInfo->info.token,
2298 sendData->requestInfo->info.tokenLength,
2299 sendData->remoteEndpoint->port);
2301 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
2303 OIC_LOG(ERROR, TAG, "blockId is null");
2304 CADestroyBlockID(blockDataID);
2305 return CA_STATUS_FAILED;
2308 if (CABlockidMatches(currData, blockDataID))
2310 OIC_LOG(ERROR, TAG, "already sent");
2311 CADestroyBlockID(blockDataID);
2314 CADestroyBlockID(blockDataID);
2317 else if (sendData->responseInfo) // sendData is responseMessage
2319 OIC_LOG(DEBUG, TAG, "Send response");
2320 if (NULL != currData->blockDataId
2321 && NULL != currData->blockDataId->id
2322 && currData->blockDataId->idLength > 0
2323 && NULL != sendData->responseInfo->info.token)
2325 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2326 (CAToken_t)sendData->responseInfo->info.token,
2327 sendData->responseInfo->info.tokenLength,
2328 sendData->remoteEndpoint->port);
2330 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
2332 OIC_LOG(ERROR, TAG, "blockId is null");
2333 CADestroyBlockID(blockDataID);
2334 return CA_STATUS_FAILED;
2337 if (CABlockidMatches(currData, blockDataID))
2340 if (NULL != currData->sentData)
2342 OIC_LOG(DEBUG, TAG, "init block number");
2343 CADestroyDataSet(currData->sentData);
2345 currData->sentData = CACloneCAData(sendData);
2346 *blockData = currData;
2347 CADestroyBlockID(blockDataID);
2348 ca_mutex_unlock(g_context.blockDataListMutex);
2349 return CA_STATUS_OK;
2351 CADestroyBlockID(blockDataID);
2356 OIC_LOG(ERROR, TAG, "no CAInfo data");
2360 ca_mutex_unlock(g_context.blockDataListMutex);
2362 return CA_STATUS_FAILED;
2365 CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID)
2367 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2369 ca_mutex_lock(g_context.blockDataListMutex);
2371 size_t len = u_arraylist_length(g_context.dataList);
2372 for (size_t i = 0; i < len; i++)
2374 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2375 if (CABlockidMatches(currData, blockID))
2377 ca_mutex_unlock(g_context.blockDataListMutex);
2381 ca_mutex_unlock(g_context.blockDataListMutex);
2386 coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID,
2389 OIC_LOG(DEBUG, TAG, "IN-GetBlockOption");
2390 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2392 ca_mutex_lock(g_context.blockDataListMutex);
2394 size_t len = u_arraylist_length(g_context.dataList);
2395 for (size_t i = 0; i < len; i++)
2397 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2398 if (CABlockidMatches(currData, blockID))
2400 ca_mutex_unlock(g_context.blockDataListMutex);
2401 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2402 if (COAP_OPTION_BLOCK2 == blockType)
2404 return &currData->block2;
2408 return &currData->block1;
2412 ca_mutex_unlock(g_context.blockDataListMutex);
2414 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2418 CAPayload_t CAGetPayloadFromBlockDataList(const CABlockDataID_t *blockID,
2419 size_t *fullPayloadLen)
2421 OIC_LOG(DEBUG, TAG, "IN-GetFullPayload");
2422 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2423 VERIFY_NON_NULL_RET(fullPayloadLen, TAG, "fullPayloadLen", NULL);
2425 ca_mutex_lock(g_context.blockDataListMutex);
2427 size_t len = u_arraylist_length(g_context.dataList);
2428 for (size_t i = 0; i < len; i++)
2430 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2431 if (CABlockidMatches(currData, blockID))
2433 ca_mutex_unlock(g_context.blockDataListMutex);
2434 *fullPayloadLen = currData->receivedPayloadLen;
2435 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2436 return currData->payload;
2439 ca_mutex_unlock(g_context.blockDataListMutex);
2441 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2445 CABlockData_t *CACreateNewBlockData(const CAData_t *sendData)
2447 OIC_LOG(DEBUG, TAG, "IN-CACreateNewBlockData");
2448 VERIFY_NON_NULL_RET(sendData, TAG, "sendData", NULL);
2450 // create block data
2451 CABlockData_t *data = (CABlockData_t *) OICCalloc(1, sizeof(CABlockData_t));
2454 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2458 data->block1.szx = CA_DEFAULT_BLOCK_SIZE;
2459 data->block2.szx = CA_DEFAULT_BLOCK_SIZE;
2460 data->sentData = CACloneCAData(sendData);
2463 OIC_LOG(ERROR, TAG, PCF("memory alloc has failed"));
2468 CAToken_t token = NULL;
2469 uint8_t tokenLength = 0;
2470 if (data->sentData->requestInfo)
2472 // update token info
2473 tokenLength = data->sentData->requestInfo->info.tokenLength;
2474 token = data->sentData->requestInfo->info.token;
2476 else if(data->sentData->responseInfo)
2478 tokenLength = data->sentData->responseInfo->info.tokenLength;
2479 token = data->sentData->responseInfo->info.token;
2482 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2484 data->sentData->remoteEndpoint->port);
2485 if (NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
2487 OIC_LOG(ERROR, TAG, "blockId is null");
2488 CADestroyBlockID(blockDataID);
2489 CADestroyDataSet(data->sentData);
2493 data->blockDataId = blockDataID;
2495 ca_mutex_lock(g_context.blockDataListMutex);
2497 bool res = u_arraylist_add(g_context.dataList, (void *) data);
2500 OIC_LOG(ERROR, TAG, "add has failed");
2501 CADestroyBlockID(data->blockDataId);
2502 CADestroyDataSet(data->sentData);
2504 ca_mutex_unlock(g_context.blockDataListMutex);
2507 ca_mutex_unlock(g_context.blockDataListMutex);
2509 OIC_LOG(DEBUG, TAG, "OUT-CreateBlockData");
2513 CAResult_t CARemoveBlockDataFromList(const CABlockDataID_t *blockID)
2515 OIC_LOG(DEBUG, TAG, "CARemoveBlockData");
2516 VERIFY_NON_NULL(blockID, TAG, "blockID");
2518 ca_mutex_lock(g_context.blockDataListMutex);
2520 size_t len = u_arraylist_length(g_context.dataList);
2521 for (size_t i = 0; i < len; i++)
2523 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2524 if (CABlockidMatches(currData, blockID))
2526 CABlockData_t *removedData = u_arraylist_remove(g_context.dataList, i);
2529 OIC_LOG(ERROR, TAG, "data is NULL");
2530 ca_mutex_unlock(g_context.blockDataListMutex);
2531 return CA_STATUS_FAILED;
2535 if (currData->sentData)
2537 CADestroyDataSet(currData->sentData);
2539 CADestroyBlockID(currData->blockDataId);
2540 OICFree(currData->payload);
2542 ca_mutex_unlock(g_context.blockDataListMutex);
2543 return CA_STATUS_OK;
2546 ca_mutex_unlock(g_context.blockDataListMutex);
2548 return CA_STATUS_OK;
2551 bool CAIsBlockDataInList(const CABlockDataID_t *blockID)
2553 OIC_LOG(DEBUG, TAG, "IN-IsBlockDataInList");
2554 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", false);
2556 ca_mutex_lock(g_context.blockDataListMutex);
2558 size_t len = u_arraylist_length(g_context.dataList);
2559 for (size_t i = 0; i < len; i++)
2561 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2562 if (CABlockidMatches(currData, blockID))
2564 OIC_LOG(DEBUG, TAG, "found block data");
2565 ca_mutex_unlock(g_context.blockDataListMutex);
2569 ca_mutex_unlock(g_context.blockDataListMutex);
2571 OIC_LOG(DEBUG, TAG, "OUT-IsBlockDataInList");
2575 void CADestroyDataSet(CAData_t* data)
2577 VERIFY_NON_NULL_VOID(data, TAG, "data");
2579 CAFreeEndpoint(data->remoteEndpoint);
2580 CADestroyRequestInfoInternal(data->requestInfo);
2581 CADestroyResponseInfoInternal(data->responseInfo);
2585 CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength,
2586 uint16_t portNumber)
2588 VERIFY_NON_NULL_RET(token, TAG, "token", NULL);
2590 char port[PORT_LENGTH] = {0,};
2591 port[0] = (char)((portNumber>>8) & 0xFF);
2592 port[1] = (char)(portNumber & 0xFF);
2594 CABlockDataID_t* blockDataID = (CABlockDataID_t *) OICMalloc(sizeof(CABlockDataID_t));
2597 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2600 blockDataID->idLength = tokenLength + sizeof(port);
2601 blockDataID->id = (uint8_t *) OICMalloc(blockDataID->idLength);
2602 if (!blockDataID->id)
2604 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2605 OICFree(blockDataID);
2609 memcpy(blockDataID->id, token, tokenLength);
2610 memcpy(blockDataID->id + tokenLength, port, sizeof(port));
2612 OIC_LOG(DEBUG, TAG, "BlockID is ");
2613 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)blockDataID->id, blockDataID->idLength);
2618 void CADestroyBlockID(CABlockDataID_t *blockID)
2620 VERIFY_NON_NULL_VOID(blockID, TAG, "blockID");
2621 OICFree(blockID->id);
2626 bool CABlockidMatches(const CABlockData_t *currData, const CABlockDataID_t *blockID)
2628 VERIFY_NON_NULL_RET(currData, TAG, "currData", false);
2629 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", false);
2630 VERIFY_NON_NULL_RET(blockID->id, TAG, "blockID->id", false);
2632 if ((currData->blockDataId)
2633 && (currData->blockDataId->id)
2634 && (currData->blockDataId->idLength == blockID->idLength)
2635 && !memcmp(currData->blockDataId->id, blockID->id, currData->blockDataId->idLength))
2642 void CALogBlockInfo(coap_block_t *block)
2644 VERIFY_NON_NULL_VOID(block, TAG, "block");
2646 OIC_LOG(DEBUG, TAG, "block option info");
2648 OIC_LOG_V(DEBUG, TAG, "block option-num : %d", block->num);
2650 OIC_LOG_V(DEBUG, TAG, "block option-m : %d", block->m);
2652 OIC_LOG_V(DEBUG, TAG, "block option-szx : %d", block->szx);