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"
46 #define TAG "OIC_CA_BWT"
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 = { .sendThreadFunc = NULL,
57 .receivedThreadFunc = NULL,
60 static bool CACheckPayloadLength(const CAData_t *sendData)
62 size_t payloadLen = 0;
63 CAGetPayloadInfo(sendData, &payloadLen);
65 // check if message has to be transfered to a block
66 size_t maxBlockSize = BLOCK_SIZE(CA_DEFAULT_BLOCK_SIZE);
67 OIC_LOG_V(DEBUG, TAG, "payloadLen=%zu, maxBlockSize=%zu", payloadLen, maxBlockSize);
69 if (payloadLen <= maxBlockSize)
77 CAResult_t CAInitializeBlockWiseTransfer(CASendThreadFunc sendThreadFunc,
78 CAReceiveThreadFunc receivedThreadFunc)
80 OIC_LOG(DEBUG, TAG, "initialize");
82 // set block-wise transfer context
83 if (!g_context.sendThreadFunc)
85 g_context.sendThreadFunc = sendThreadFunc;
88 if (!g_context.receivedThreadFunc)
90 g_context.receivedThreadFunc = receivedThreadFunc;
93 if (!g_context.dataList)
95 g_context.dataList = u_arraylist_create();
98 CAResult_t res = CAInitBlockWiseMutexVariables();
99 if (CA_STATUS_OK != res)
101 u_arraylist_free(&g_context.dataList);
102 g_context.dataList = NULL;
103 OIC_LOG(ERROR, TAG, "init has failed");
109 CAResult_t CATerminateBlockWiseTransfer()
111 OIC_LOG(DEBUG, TAG, "terminate");
113 if (g_context.dataList)
115 u_arraylist_free(&g_context.dataList);
118 CATerminateBlockWiseMutexVariables();
123 CAResult_t CAInitBlockWiseMutexVariables()
125 if (!g_context.blockDataListMutex)
127 g_context.blockDataListMutex = ca_mutex_new();
128 if (!g_context.blockDataListMutex)
130 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
131 return CA_STATUS_FAILED;
135 if (!g_context.blockDataSenderMutex)
137 g_context.blockDataSenderMutex = ca_mutex_new();
138 if (!g_context.blockDataSenderMutex)
140 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
141 CATerminateBlockWiseMutexVariables();
142 return CA_STATUS_FAILED;
149 void CATerminateBlockWiseMutexVariables()
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->requestInfo)
171 if (CA_MSG_RESET == sendData->requestInfo->info.type)
173 OIC_LOG(DEBUG, TAG, "reset message can't be sent to the block");
174 return CA_NOT_SUPPORTED;
177 else if (sendData->responseInfo)
179 if (CA_MSG_RESET == sendData->responseInfo->info.type)
181 OIC_LOG(DEBUG, TAG, "reset message can't be sent to the block");
182 return CA_NOT_SUPPORTED;
186 // #1. check if it is already included in block data list
187 CABlockData_t *currData = NULL;
188 CAResult_t res = CACheckBlockDataValidation(sendData, &currData);
189 if (CA_STATUS_OK != res)
191 // #2. if it is not included, add the data into list
194 OIC_LOG(DEBUG, TAG, "There is no block data");
196 bool isBlock = CACheckPayloadLength(sendData);
199 if (sendData->requestInfo)
201 currData = CACreateNewBlockData(sendData);
204 OIC_LOG(ERROR, TAG, "failed to create block data");
205 return CA_MEMORY_ALLOC_FAILED;
208 return CA_NOT_SUPPORTED;
210 currData = CACreateNewBlockData(sendData);
213 OIC_LOG(ERROR, TAG, "failed to create block data");
214 return CA_MEMORY_ALLOC_FAILED;
219 // #3. check request/response block option type and payload length
220 res = CACheckBlockOptionType(currData);
221 if (CA_STATUS_OK == res)
223 // #4. send block message
224 OIC_LOG(DEBUG, TAG, "send first block msg");
225 res = CAAddSendThreadQueue(currData->sentData,
226 (const CABlockDataID_t *)&currData->blockDataId);
227 if (CA_STATUS_OK != res)
229 OIC_LOG(ERROR, TAG, "add has failed");
237 CAResult_t CAAddSendThreadQueue(const CAData_t *sendData, const CABlockDataID_t *blockID)
239 VERIFY_NON_NULL(sendData, TAG, "sendData");
240 VERIFY_NON_NULL(blockID, TAG, "blockID");
242 CAData_t *cloneData = CACloneCAData(sendData);
245 OIC_LOG(ERROR, TAG, "clone has failed");
246 CARemoveBlockDataFromList(blockID);
247 return CA_STATUS_FAILED;
250 if (g_context.sendThreadFunc)
252 ca_mutex_lock(g_context.blockDataSenderMutex);
253 g_context.sendThreadFunc(cloneData);
254 ca_mutex_unlock(g_context.blockDataSenderMutex);
258 CADestroyDataSet(cloneData);
263 CAResult_t CACheckBlockOptionType(CABlockData_t *currData)
265 VERIFY_NON_NULL(currData, TAG, "currData");
266 VERIFY_NON_NULL(currData->sentData, TAG, "currData->sentData");
268 bool isBlock = CACheckPayloadLength(currData->sentData);
271 return CA_NOT_SUPPORTED;
274 // set block option (COAP_OPTION_BLOCK2 or COAP_OPTION_BLOCK1)
275 if (currData->sentData->requestInfo) // request message
277 currData->type = COAP_OPTION_BLOCK1;
279 else // response message
281 currData->type = COAP_OPTION_BLOCK2;
287 // TODO make pdu const after libcoap is updated to support that.
288 CAResult_t CAReceiveBlockWiseData(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
289 const CAData_t *receivedData, size_t dataLen)
291 OIC_LOG(DEBUG, TAG, "CAReceiveBlockWiseData");
292 VERIFY_NON_NULL(pdu, TAG, "pdu");
293 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
294 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
295 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
297 // check if received message type is CA_MSG_RESET
298 if (CA_EMPTY == pdu->hdr->coap_hdr_udp_t.code)
300 OIC_LOG(DEBUG, TAG, "code is CA_EMPTY..");
302 // get token from block-wise transfer list when CA_EMPTY(RST/ACK) is received
303 CAResult_t res = CAGetTokenFromBlockDataList(pdu, endpoint, receivedData->responseInfo);
304 if (CA_STATUS_OK != res)
306 OIC_LOG(ERROR, TAG, "fail to get token");
310 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
311 receivedData->responseInfo->info.token,
312 receivedData->responseInfo->info.tokenLength,
314 if (NULL == blockDataID || blockDataID->idLength < 1)
316 // if retransmission is timeout, callback msg will be send without token.
317 if (NULL == blockDataID && !receivedData->responseInfo->info.token)
319 OIC_LOG(INFO, TAG, "retransmission was stopped");
320 return CA_REQUEST_TIMEOUT;
324 OIC_LOG(ERROR, TAG, "blockId is null");
325 CADestroyBlockID(blockDataID);
326 return CA_STATUS_FAILED;
330 CARemoveBlockDataFromList(blockDataID);
331 CADestroyBlockID(blockDataID);
332 return CA_NOT_SUPPORTED;
335 // check if block option is set and get block data
336 coap_block_t block = {0, 0, 0};
339 int isBlock1 = coap_get_block(pdu, COAP_OPTION_BLOCK1, &block);
342 CAResult_t res = CASetNextBlockOption1(pdu, endpoint, receivedData, block, dataLen);
343 if (CA_STATUS_OK != res)
345 OIC_LOG(ERROR, TAG, "setting has failed");
351 int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
354 CAResult_t res = CASetNextBlockOption2(pdu, endpoint, receivedData, block, dataLen);
355 if (CA_STATUS_OK != res)
357 OIC_LOG(ERROR, TAG, "setting has failed");
362 // check if there is error code
363 if (!isBlock1 && !isBlock2)
365 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code);
366 if (CA_REQUEST_ENTITY_INCOMPLETE == code)
368 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
369 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
370 pdu->hdr->coap_hdr_udp_t.token_length,
372 if (NULL == blockDataID || blockDataID->idLength < 1)
374 OIC_LOG(ERROR, TAG, "blockId is null");
375 CADestroyBlockID(blockDataID);
376 return CA_STATUS_FAILED;
379 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
382 OIC_LOG(ERROR, TAG, "getting has failed");
383 CADestroyBlockID(blockDataID);
384 return CA_STATUS_FAILED;
387 if (COAP_OPTION_BLOCK2 == data->type)
389 coap_block_t *block2 = CAGetBlockOption(blockDataID, COAP_OPTION_BLOCK2);
392 OIC_LOG(ERROR, TAG, "block is null");
393 CADestroyBlockID(blockDataID);
394 return CA_STATUS_FAILED;
397 CAResult_t res = CASetNextBlockOption2(pdu, endpoint, receivedData, *block2,
399 if (CA_STATUS_OK != res)
401 OIC_LOG(ERROR, TAG, "setting has failed");
402 CADestroyBlockID(blockDataID);
406 else if (COAP_OPTION_BLOCK1 == data->type)
408 coap_block_t *block1 = CAGetBlockOption(blockDataID, COAP_OPTION_BLOCK1);
411 OIC_LOG(ERROR, TAG, "block is null");
412 CADestroyBlockID(blockDataID);
413 return CA_STATUS_FAILED;
416 CAResult_t res = CASetNextBlockOption1(pdu, endpoint, receivedData, *block1,
418 if (CA_STATUS_OK != res)
420 OIC_LOG(ERROR, TAG, "setting has failed");
421 CADestroyBlockID(blockDataID);
427 CADestroyBlockID(blockDataID);
428 return CA_STATUS_FAILED;
434 OIC_LOG(DEBUG, TAG, "it's normal pdu");
436 // if received data is response message
437 // and sent data remain in block data list, remove block data
438 if (receivedData->responseInfo)
440 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
441 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
442 pdu->hdr->coap_hdr_udp_t.token_length,
444 if (NULL == blockDataID || blockDataID->idLength < 1)
446 OIC_LOG(ERROR, TAG, "blockId is null");
447 CADestroyBlockID(blockDataID);
448 return CA_STATUS_FAILED;
451 CARemoveBlockDataFromList(blockDataID);
452 CADestroyBlockID(blockDataID);
454 return CA_NOT_SUPPORTED;
461 CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData,
462 uint8_t blockWiseStatus, const CABlockDataID_t *blockID)
464 VERIFY_NON_NULL(pdu, TAG, "pdu");
465 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
466 VERIFY_NON_NULL(blockID, TAG, "blockID");
468 CAResult_t res = CA_STATUS_OK;
469 CAData_t *data = NULL;
471 // process blockWiseStatus
472 switch (blockWiseStatus)
474 case CA_OPTION2_FIRST_BLOCK:
475 res = CAAddSendThreadQueue(receivedData, blockID);
476 if (CA_STATUS_OK != res)
478 OIC_LOG(ERROR, TAG, "add has failed");
484 // add data to send thread
485 data = CAGetDataSetFromBlockDataList(blockID);
488 OIC_LOG(ERROR, TAG, "it's unavailable");
489 return CA_STATUS_FAILED;
492 if (data->requestInfo)
494 data->requestInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
497 if (data->responseInfo)
499 data->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
502 res = CAAddSendThreadQueue(data, blockID);
503 if (CA_STATUS_OK != res)
505 OIC_LOG(ERROR, TAG, "add has failed");
513 case CA_SENT_PREVIOUS_NON_MSG:
514 res = CASendBlockMessage(pdu, CA_MSG_CONFIRM, blockWiseStatus,
516 if (CA_STATUS_OK != res)
518 OIC_LOG(ERROR, TAG, "send has failed");
523 case CA_OPTION2_LAST_BLOCK:
524 // process last block and send upper layer
525 res = CAReceiveLastBlock(blockID, receivedData);
526 if (CA_STATUS_OK != res)
528 OIC_LOG(ERROR, TAG, "receive has failed");
532 // remove data from list
533 res = CARemoveBlockDataFromList(blockID);
534 if (CA_STATUS_OK != res)
536 OIC_LOG(ERROR, TAG, "remove has failed");
541 case CA_OPTION1_NO_ACK_LAST_BLOCK:
542 // process last block and send upper layer
543 res = CAReceiveLastBlock(blockID, receivedData);
544 if (CA_STATUS_OK != res)
546 OIC_LOG(ERROR, TAG, "receive has failed");
550 if (CA_MSG_NONCONFIRM == pdu->hdr->coap_hdr_udp_t.type)
552 // remove data from list
553 res = CARemoveBlockDataFromList(blockID);
554 if (CA_STATUS_OK != res)
556 OIC_LOG(ERROR, TAG, "remove has failed");
562 case CA_OPTION1_NO_ACK_BLOCK:
563 if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type)
565 // add data to send thread
566 res = CASendBlockMessage(pdu, CA_MSG_ACKNOWLEDGE, blockWiseStatus,
568 if (CA_STATUS_OK != res)
570 OIC_LOG(ERROR, TAG, "send has failed");
576 case CA_BLOCK_INCOMPLETE:
577 if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type ||
578 CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type)
580 // add data to send thread
581 res = CASendErrorMessage(pdu, blockWiseStatus,
582 CA_REQUEST_ENTITY_INCOMPLETE,
584 if (CA_STATUS_OK != res)
586 OIC_LOG(ERROR, TAG, "send has failed");
592 case CA_BLOCK_TOO_LARGE:
593 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type)
595 res = CASendBlockMessage(pdu, CA_MSG_CONFIRM, blockWiseStatus,
597 if (CA_STATUS_OK != res)
599 OIC_LOG(ERROR, TAG, "send has failed");
603 else if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type)
605 res = CASendErrorMessage(pdu, blockWiseStatus,
606 CA_REQUEST_ENTITY_TOO_LARGE,
608 if (CA_STATUS_OK != res)
610 OIC_LOG(ERROR, TAG, "send has failed");
616 OIC_LOG_V(ERROR, TAG, "no logic [%d]", blockWiseStatus);
621 CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType,
622 uint8_t status, const CABlockDataID_t *blockID)
624 VERIFY_NON_NULL(pdu, TAG, "pdu");
625 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
626 VERIFY_NON_NULL(blockID, TAG, "blockID");
628 CAData_t *data = CAGetDataSetFromBlockDataList(blockID);
631 OIC_LOG(ERROR, TAG, "CAData is unavailable");
632 return CA_STATUS_FAILED;
635 if (CA_MSG_CONFIRM == msgType)
637 OIC_LOG(DEBUG, TAG, "need new msgID");
638 if (data->requestInfo)
640 data->requestInfo->info.messageId = 0;
643 if (data->responseInfo)
645 data->responseInfo->info.messageId = 0;
648 else if (CA_MSG_ACKNOWLEDGE == msgType)
650 if (data->responseInfo)
652 OIC_LOG(DEBUG, TAG, "set ACK message");
653 data->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
654 data->responseInfo->info.type = CA_MSG_ACKNOWLEDGE;
655 if (CA_OPTION1_NO_ACK_LAST_BLOCK == status)
657 data->responseInfo->result = CA_CHANGED;
659 else if (CA_OPTION1_NO_ACK_BLOCK == status)
661 data->responseInfo->result = CA_CONTINUE;
666 // add data to send thread
667 CAResult_t res = CAAddSendThreadQueue(data, blockID);
668 if (CA_STATUS_OK != res)
670 OIC_LOG(ERROR, TAG, "add has failed");
676 CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status,
677 CAResponseResult_t responseResult,
678 const CABlockDataID_t *blockID)
680 VERIFY_NON_NULL(pdu, TAG, "pdu");
681 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
682 VERIFY_NON_NULL(blockID, TAG, "blockID");
684 // create error responseInfo
685 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockID);
688 OIC_LOG(ERROR, TAG, "data is unavailable");
689 return CA_STATUS_FAILED;
692 CAData_t *cloneData = NULL;
693 if (data->sentData && data->sentData->responseInfo)
695 data->sentData->responseInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
696 data->sentData->responseInfo->info.type = CA_MSG_ACKNOWLEDGE;
697 data->sentData->responseInfo->result = responseResult;
698 cloneData = CACloneCAData(data->sentData);
701 OIC_LOG(ERROR, TAG, "clone has failed");
702 return CA_MEMORY_ALLOC_FAILED;
704 OIC_LOG(DEBUG, TAG, "set ACK message");
706 else if (data->sentData)
708 cloneData = CACreateNewDataSet(pdu, data->sentData->remoteEndpoint);
711 OIC_LOG(ERROR, TAG, PCF("CACreateNewDataSet failed"));
712 return CA_MEMORY_ALLOC_FAILED;
715 cloneData->responseInfo->info.type = CA_MSG_CONFIRM;
716 cloneData->responseInfo->result = responseResult;
717 OIC_LOG(DEBUG, TAG, "set CON message");
721 OIC_LOG(ERROR, TAG, "data has no sent-data");
722 return CA_MEMORY_ALLOC_FAILED;
725 // add data to send thread
726 if (g_context.sendThreadFunc)
728 ca_mutex_lock(g_context.blockDataSenderMutex);
729 g_context.sendThreadFunc(cloneData);
730 ca_mutex_unlock(g_context.blockDataSenderMutex);
734 CADestroyDataSet(cloneData);
737 // if error code is 4.08, remove the stored payload and initialize block number
738 if (CA_BLOCK_INCOMPLETE == status)
740 OICFree(data->payload);
741 data->payload = NULL;
742 data->payloadLength = 0;
743 data->receivedPayloadLen = 0;
744 data->block1.num = 0;
745 data->block2.num = 0;
751 CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID,
752 const CAData_t *receivedData)
754 VERIFY_NON_NULL(blockID, TAG, "blockID");
755 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
757 // total block data have to notify to Application
758 CAData_t *cloneData = CACloneCAData(receivedData);
761 OIC_LOG(ERROR, TAG, "clone has failed");
762 return CA_MEMORY_ALLOC_FAILED;
766 size_t fullPayloadLen = 0;
767 CAPayload_t fullPayload = CAGetPayloadFromBlockDataList(blockID,
771 CAResult_t res = CAUpdatePayloadToCAData(cloneData, fullPayload, fullPayloadLen);
772 if (CA_STATUS_OK != res)
774 OIC_LOG(ERROR, TAG, "update has failed");
775 CADestroyDataSet(cloneData);
780 if (g_context.receivedThreadFunc)
782 g_context.receivedThreadFunc(cloneData);
786 CADestroyDataSet(cloneData);
792 // TODO make pdu const after libcoap is updated to support that.
793 CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
794 const CAData_t *receivedData, coap_block_t block,
797 OIC_LOG(INFO, TAG, "CASetNextBlockOption1");
798 VERIFY_NON_NULL(pdu, TAG, "pdu");
799 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
800 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
801 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
803 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
805 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
806 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
807 pdu->hdr->coap_hdr_udp_t.token_length,
809 if (NULL == blockDataID || blockDataID->idLength < 1)
811 OIC_LOG(ERROR, TAG, "blockId is null");
812 CADestroyBlockID(blockDataID);
813 return CA_STATUS_FAILED;
816 // Get BlockData data. If does not exist, create a new data
817 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
820 OIC_LOG(DEBUG, TAG, "block data doesn't exist in list. create new one");
822 CAData_t *cadata = CACreateNewDataSet(pdu, endpoint);
825 OIC_LOG(ERROR, TAG, "data is null");
826 CADestroyBlockID(blockDataID);
827 return CA_STATUS_FAILED;
830 CABlockData_t *currData = CACreateNewBlockData(cadata);
833 OIC_LOG(ERROR, TAG, "failed to create a new block data");
834 CADestroyDataSet(cadata);
835 CADestroyBlockID(blockDataID);
836 return CA_STATUS_FAILED;
838 CADestroyDataSet(cadata);
841 // update BLOCK OPTION1 type
842 CAResult_t res = CAUpdateBlockOptionType(blockDataID,
844 if (CA_STATUS_OK != res)
846 OIC_LOG(ERROR, TAG, "update has failed");
847 CARemoveBlockDataFromList(blockDataID);
848 CADestroyBlockID(blockDataID);
852 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
853 // received type from remote device
854 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type)
856 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code);
858 (CA_REQUEST_ENTITY_INCOMPLETE != code && CA_REQUEST_ENTITY_TOO_LARGE != code))
860 int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
863 OIC_LOG(INFO, TAG, "received data is combining block1 and block2");
864 // initialize block number for response message
865 data->block1.num = 0;
869 OIC_LOG(INFO, TAG, "received data is not bulk data");
870 CAReceiveLastBlock(blockDataID, receivedData);
871 CARemoveBlockDataFromList(blockDataID);
874 CADestroyBlockID(blockDataID);
878 blockWiseStatus = CA_OPTION1_ACK;
879 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus);
880 if (CA_STATUS_OK != res)
882 OIC_LOG(ERROR, TAG, "update has failed");
883 CADestroyBlockID(blockDataID);
887 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
888 if (CA_STATUS_OK != res)
890 OIC_LOG(ERROR, TAG, "update has failed");
891 CARemoveBlockDataFromList(blockDataID);
892 CADestroyBlockID(blockDataID);
896 else // CON or NON message
898 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
900 // check the size option
901 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
903 &(data->payloadLength));
905 // check if received payload is exact
906 if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type)
908 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
909 COAP_OPTION_BLOCK1, dataLen);
912 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
914 // store the received payload and merge
915 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
916 isSizeOption, COAP_OPTION_BLOCK1);
917 if (CA_STATUS_OK != res)
919 OIC_LOG(ERROR, TAG, "update has failed");
920 CARemoveBlockDataFromList(blockDataID);
921 CADestroyBlockID(blockDataID);
925 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1,
927 if (CA_STATUS_OK != res)
929 OIC_LOG(ERROR, TAG, "update has failed");
930 CARemoveBlockDataFromList(blockDataID);
931 CADestroyBlockID(blockDataID);
936 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
937 if (CA_STATUS_OK != res)
939 OIC_LOG(ERROR, TAG, "update has failed");
940 CARemoveBlockDataFromList(blockDataID);
941 CADestroyBlockID(blockDataID);
946 // check the blcok-wise transfer status for next step
947 if (CA_BLOCK_UNKNOWN == blockWiseStatus || CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
949 if (0 == block.m) // Last block is received
951 OIC_LOG(DEBUG, TAG, "M bit is 0");
952 blockWiseStatus = CA_OPTION1_NO_ACK_LAST_BLOCK;
956 OIC_LOG(DEBUG, TAG, "M bit is 1");
957 blockWiseStatus = CA_OPTION1_NO_ACK_BLOCK;
962 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
963 if (CA_STATUS_OK != res)
965 OIC_LOG(ERROR, TAG, "setting has failed");
966 CARemoveBlockDataFromList(blockDataID);
969 CADestroyBlockID(blockDataID);
973 // TODO make pdu const after libcoap is updated to support that.
974 CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
975 const CAData_t *receivedData, coap_block_t block,
978 OIC_LOG(DEBUG, TAG, "CASetNextBlockOption2");
979 OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
981 VERIFY_NON_NULL(pdu, TAG, "pdu");
982 VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
983 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
984 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
986 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
987 (CAToken_t)pdu->hdr->coap_hdr_udp_t.token,
988 pdu->hdr->coap_hdr_udp_t.token_length,
990 if (NULL == blockDataID || blockDataID->idLength < 1)
992 OIC_LOG(ERROR, TAG, "blockId is null");
993 CADestroyBlockID(blockDataID);
994 return CA_STATUS_FAILED;
997 // Get BlockData data. If does not exist, create a new data
998 CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
1001 OIC_LOG(DEBUG, TAG, "block data doesn't exist in list. create new one");
1003 CAData_t *cadata = CACreateNewDataSet(pdu, endpoint);
1006 OIC_LOG(ERROR, TAG, "data is null");
1007 CADestroyBlockID(blockDataID);
1008 return CA_STATUS_FAILED;
1011 CABlockData_t *currData = CACreateNewBlockData(cadata);
1014 OIC_LOG(ERROR, TAG, "failed to create a new block data");
1015 CADestroyDataSet(cadata);
1016 CADestroyBlockID(blockDataID);
1017 return CA_STATUS_FAILED;
1019 CADestroyDataSet(cadata);
1022 // set Block Option Type
1023 CAResult_t res = CAUpdateBlockOptionType(blockDataID, COAP_OPTION_BLOCK2);
1024 if (CA_STATUS_OK != res)
1026 OIC_LOG(ERROR, TAG, "update has failed");
1027 CARemoveBlockDataFromList(blockDataID);
1028 CADestroyBlockID(blockDataID);
1032 uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
1033 if (0 == block.num && CA_GET == pdu->hdr->coap_hdr_udp_t.code && 0 == block.m)
1035 OIC_LOG(INFO, TAG, "first block number");
1037 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1038 if (CA_STATUS_OK != res)
1040 OIC_LOG(ERROR, TAG, "update has failed");
1041 CARemoveBlockDataFromList(blockDataID);
1042 CADestroyBlockID(blockDataID);
1046 // first block data have to notify to Application
1047 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1048 if (CA_STATUS_OK != res)
1050 OIC_LOG(ERROR, TAG, "update has failed");
1051 CARemoveBlockDataFromList(blockDataID);
1052 CADestroyBlockID(blockDataID);
1055 blockWiseStatus = CA_OPTION2_FIRST_BLOCK;
1059 // received type from remote device
1060 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type ||
1061 (CA_MSG_NONCONFIRM == pdu->hdr->coap_hdr_udp_t.type &&
1062 NULL != receivedData->responseInfo))
1064 OIC_LOG(DEBUG, TAG, "received ACK or NON");
1066 // check the size option
1067 bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
1069 &(data->payloadLength));
1071 // check if received payload is exact
1072 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type)
1074 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
1075 COAP_OPTION_BLOCK2, dataLen);
1078 if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
1080 // store the received payload and merge
1081 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
1082 isSizeOption, COAP_OPTION_BLOCK2);
1083 if (CA_STATUS_OK != res)
1085 OIC_LOG(ERROR, TAG, "update has failed");
1086 CARemoveBlockDataFromList(blockDataID);
1087 CADestroyBlockID(blockDataID);
1092 if (0 == block.m && CA_BLOCK_UNKNOWN == blockWiseStatus) // Last block is received
1094 OIC_LOG(DEBUG, TAG, "M bit is 0");
1095 blockWiseStatus = CA_OPTION2_LAST_BLOCK;
1099 if (CA_BLOCK_UNKNOWN == blockWiseStatus ||
1100 CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
1102 OIC_LOG(DEBUG, TAG, "M bit is 1");
1104 if (CA_MSG_ACKNOWLEDGE == pdu->hdr->coap_hdr_udp_t.type)
1106 blockWiseStatus = CA_OPTION2_ACK;
1110 blockWiseStatus = CA_OPTION2_NON;
1114 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2,
1116 if (CA_STATUS_OK != res)
1118 OIC_LOG(ERROR, TAG, "update has failed");
1119 CARemoveBlockDataFromList(blockDataID);
1120 CADestroyBlockID(blockDataID);
1124 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1125 if (CA_STATUS_OK != res)
1127 OIC_LOG(ERROR, TAG, "update has failed");
1128 CARemoveBlockDataFromList(blockDataID);
1129 CADestroyBlockID(blockDataID);
1134 else // CON message and so on.
1136 OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
1138 blockWiseStatus = CA_OPTION2_CON;
1140 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1141 if (CA_STATUS_OK != res)
1143 OIC_LOG(ERROR, TAG, "update has failed");
1144 CARemoveBlockDataFromList(blockDataID);
1145 CADestroyBlockID(blockDataID);
1149 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1150 if (CA_STATUS_OK != res)
1152 OIC_LOG(ERROR, TAG, "update has failed");
1153 CARemoveBlockDataFromList(blockDataID);
1154 CADestroyBlockID(blockDataID);
1160 res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
1161 if (CA_STATUS_OK != res)
1163 OIC_LOG(ERROR, TAG, "setting has failed");
1164 CARemoveBlockDataFromList(blockDataID);
1165 CADestroyBlockID(blockDataID);
1169 CADestroyBlockID(blockDataID);
1170 return CA_STATUS_OK;
1173 CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu,
1174 coap_block_t *block, uint16_t blockType,
1177 VERIFY_NON_NULL(currData, TAG, "currData");
1178 VERIFY_NON_NULL(pdu, TAG, "pdu");
1179 VERIFY_NON_NULL(block, TAG, "block");
1181 // update block data
1182 CAResult_t res = CA_STATUS_OK;
1183 uint32_t code = CA_RESPONSE_CODE(pdu->hdr->coap_hdr_udp_t.code);
1185 if (CA_REQUEST_ENTITY_INCOMPLETE == code || CA_REQUEST_ENTITY_TOO_LARGE == code)
1187 // response error code of the received block message
1188 res = CAHandleBlockErrorResponse(block, blockType, code);
1189 if (CA_STATUS_OK != res)
1191 OIC_LOG(ERROR, TAG, "error handle has failed");
1197 // update block option items
1200 case CA_OPTION1_ACK:
1201 if (currData->block1.num > block->num)
1203 OIC_LOG(ERROR, TAG, "received incorrect block num");
1204 return CA_STATUS_FAILED;
1208 case CA_OPTION2_NON:
1212 case CA_OPTION2_CON:
1215 case CA_OPTION2_ACK:
1216 if (currData->block2.num > block->num)
1218 OIC_LOG(ERROR, TAG, "received incorrect block num");
1219 return CA_STATUS_FAILED;
1224 case CA_BLOCK_TOO_LARGE:
1225 // if state of received block message is CA_BLOCK_TOO_LARGE or CA_BLOCK_INCOMPLETE
1226 // we set the response error code appropriately and send
1227 if (COAP_OPTION_BLOCK2 == blockType)
1231 block->szx = currData->block2.szx;
1235 block->szx = currData->block1.szx;
1239 OIC_LOG_V(ERROR, TAG, "no logic [%d]", status);
1242 if (CA_BLOCK_INCOMPLETE != status && CA_BLOCK_TOO_LARGE != status)
1244 // negotiate block size
1245 res = CANegotiateBlockSize(currData, block, pdu->hdr->coap_hdr_udp_t.type, blockType);
1246 if (CA_STATUS_OK != res)
1248 OIC_LOG(ERROR, TAG, "negotiation has failed");
1256 CAResult_t CASetMoreBitFromBlock(size_t payloadLen, coap_block_t *block)
1258 VERIFY_NON_NULL(block, TAG, "block");
1260 if ((size_t)((block->num + 1) << (block->szx + BLOCK_NUMBER_IDX))
1263 OIC_LOG(DEBUG, TAG, "Set the M-bit(1)");
1268 OIC_LOG(DEBUG, TAG, "Set the M-bit(0)");
1272 return CA_STATUS_OK;
1275 CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block,
1276 CAMessageType_t msgType, uint16_t blockType)
1278 OIC_LOG(DEBUG, TAG, "IN-NegotiateBlockSize");
1280 VERIFY_NON_NULL(currData, TAG, "currData");
1281 VERIFY_NON_NULL(block, TAG, "block");
1283 // #1. check the block option type
1284 if (COAP_OPTION_BLOCK2 == blockType)
1286 // #2. check the message type
1287 if (CA_MSG_ACKNOWLEDGE == msgType)
1289 if (block->szx > currData->block2.szx)
1291 OIC_LOG(DEBUG, TAG, "sze is big");
1293 // #3. calculate new block number from block size
1294 unsigned int blockNum = BLOCK_SIZE(block->szx) /
1295 BLOCK_SIZE(currData->block2.szx) - 1;
1296 OIC_LOG(DEBUG, TAG, "num is set as Negotiation");
1297 block->num += blockNum;
1298 block->szx = currData->block2.szx;
1299 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1304 if (block->szx > currData->block2.szx)
1306 OIC_LOG(DEBUG, TAG, "sze is big");
1307 block->szx = currData->block2.szx;
1311 else if (COAP_OPTION_BLOCK1 == blockType)
1313 if (CA_MSG_ACKNOWLEDGE == msgType)
1315 if (block->szx < currData->block1.szx)
1317 OIC_LOG(DEBUG, TAG, "sze is small");
1319 unsigned int blockNum = BLOCK_SIZE(currData->block1.szx) /
1320 BLOCK_SIZE(block->szx) - 1;
1321 block->num += blockNum;
1322 OIC_LOG_V(DEBUG, TAG, "updated block num: %d", block->num);
1327 if (block->szx > currData->block1.szx)
1329 OIC_LOG(DEBUG, TAG, "sze is big");
1330 block->szx = currData->block1.szx;
1336 OIC_LOG(DEBUG, TAG, "Invalid block option");
1337 return CA_STATUS_FAILED;
1340 OIC_LOG(DEBUG, TAG, "OUT-NegotiateBlockSize");
1342 return CA_STATUS_OK;
1345 CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block,
1348 VERIFY_NON_NULL(currData, TAG, "currData");
1350 // check if block size is bigger than CABlockSize_t
1351 if (block.szx > CA_BLOCK_SIZE_1024_BYTE)
1353 OIC_LOG(DEBUG, TAG, "invalid block szx");
1354 return CA_STATUS_FAILED;
1357 // update block option
1358 if (COAP_OPTION_BLOCK2 == blockType)
1360 currData->block2 = block;
1364 currData->block1 = block;
1367 OIC_LOG(DEBUG, TAG, "data has updated");
1368 return CA_STATUS_OK;
1371 CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID)
1373 VERIFY_NON_NULL(pdu, TAG, "pdu");
1374 VERIFY_NON_NULL(blockID, TAG, "blockID");
1376 // if CON message is sent, update messageId in block-wise transfer list
1377 if (CA_MSG_CONFIRM == pdu->hdr->coap_hdr_udp_t.type)
1379 CAData_t * cadata = CAGetDataSetFromBlockDataList(blockID);
1382 OIC_LOG(ERROR, TAG, "CAData is unavailable");
1383 return CA_STATUS_FAILED;
1386 if (cadata->requestInfo)
1388 cadata->requestInfo->info.messageId = pdu->hdr->coap_hdr_udp_t.id;
1392 return CA_STATUS_OK;
1395 CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info,
1396 const CAEndpoint_t *endpoint, coap_list_t **options)
1398 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption");
1399 VERIFY_NON_NULL(pdu, TAG, "pdu");
1400 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1401 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1402 VERIFY_NON_NULL(info, TAG, "info");
1403 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1404 VERIFY_NON_NULL(options, TAG, "options");
1406 size_t dataLength = 0;
1409 dataLength = info->payloadSize;
1410 OIC_LOG_V(DEBUG, TAG, "dataLength - %zu", dataLength);
1413 OIC_LOG_V(DEBUG, TAG, "previous payload - %s", (*pdu)->data);
1415 CAResult_t res = CA_STATUS_OK;
1416 uint32_t code = CA_RESPONSE_CODE((*pdu)->hdr->coap_hdr_udp_t.code);
1417 if (CA_REQUEST_ENTITY_INCOMPLETE == code)
1419 OIC_LOG(INFO, TAG, "don't use option");
1423 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
1424 (CAToken_t)(*pdu)->hdr->coap_hdr_udp_t.token,
1425 (*pdu)->hdr->coap_hdr_udp_t.token_length,
1427 if (NULL == blockDataID || blockDataID->idLength < 1)
1429 OIC_LOG(ERROR, TAG, "blockId is null");
1430 res = CA_STATUS_FAILED;
1434 uint8_t blockType = CAGetBlockOptionType(blockDataID);
1435 if (COAP_OPTION_BLOCK2 == blockType)
1437 res = CAAddBlockOption2(pdu, info, dataLength, blockDataID, options);
1438 if (CA_STATUS_OK != res)
1440 OIC_LOG(ERROR, TAG, "add has failed");
1444 else if (COAP_OPTION_BLOCK1 == blockType)
1446 res = CAAddBlockOption1(pdu, info, dataLength, blockDataID, options);
1447 if (CA_STATUS_OK != res)
1449 OIC_LOG(ERROR, TAG, "add has failed");
1455 OIC_LOG(DEBUG, TAG, "no BLOCK option");
1457 // in case it is not large data, add option list to pdu.
1460 for (coap_list_t *opt = *options; opt; opt = opt->next)
1462 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
1463 COAP_OPTION_DATA(*(coap_option *) opt->data));
1465 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", (*pdu)->length);
1466 coap_add_option(*pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
1467 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
1468 COAP_OPTION_DATA(*(coap_option *) opt->data), coap_udp);
1472 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", (*pdu)->length);
1474 // if response data is so large. it have to send as block transfer
1475 if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload))
1477 OIC_LOG(INFO, TAG, "it have to use block");
1481 OIC_LOG(INFO, TAG, "not Blockwise Transfer");
1486 // if received message type is RESET from remote device,
1487 // we have to use the updated message id to find token.
1488 res = CAUpdateMessageId(*pdu, blockDataID);
1489 if (CA_STATUS_OK != res)
1491 OIC_LOG(ERROR, TAG, "fail to update CON message id ");
1496 if (CA_ADAPTER_IP == endpoint->adapter && 0 == endpoint->port)
1498 CARemoveBlockDataFromList(blockDataID);
1500 CADestroyBlockID(blockDataID);
1501 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption");
1505 CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
1506 const CABlockDataID_t *blockID, coap_list_t **options)
1508 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption2");
1509 VERIFY_NON_NULL(pdu, TAG, "pdu");
1510 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1511 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1512 VERIFY_NON_NULL(info, TAG, "info");
1513 VERIFY_NON_NULL(blockID, TAG, "blockID");
1514 VERIFY_NON_NULL(options, TAG, "options");
1516 // get set block data from CABlock list-set.
1517 coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1518 coap_block_t *block2 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK2);
1519 if (!block1 || !block2)
1521 OIC_LOG(ERROR, TAG, "getting has failed");
1522 return CA_STATUS_FAILED;
1525 CALogBlockInfo(block2);
1527 if (CA_MSG_ACKNOWLEDGE == (*pdu)->hdr->coap_hdr_udp_t.type ||
1528 (CA_MSG_NONCONFIRM == (*pdu)->hdr->coap_hdr_udp_t.type &&
1529 CA_GET != (*pdu)->hdr->coap_hdr_udp_t.code))
1531 CASetMoreBitFromBlock(dataLength, block2);
1532 CALogBlockInfo(block2);
1534 // if block number is 0, add size2 option
1535 if (0 == block2->num)
1537 CAResult_t res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE2, dataLength, options);
1538 if (CA_STATUS_OK != res)
1540 OIC_LOG(ERROR, TAG, "add has failed");
1541 CARemoveBlockDataFromList(blockID);
1546 CAResult_t res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options);
1547 if (CA_STATUS_OK != res)
1549 OIC_LOG(ERROR, TAG, "add has failed");
1550 CARemoveBlockDataFromList(blockID);
1556 OIC_LOG(DEBUG, TAG, "combining block1 and block2");
1557 res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1558 if (CA_STATUS_OK != res)
1560 OIC_LOG(ERROR, TAG, "add has failed");
1561 CARemoveBlockDataFromList(blockID);
1564 // initialize block number
1568 res = CAAddOptionToPDU(*pdu, options);
1569 if (CA_STATUS_OK != res)
1571 OIC_LOG(ERROR, TAG, "add has failed");
1572 CARemoveBlockDataFromList(blockID);
1576 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload,
1577 block2->num, block2->szx))
1579 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1580 return CA_STATUS_FAILED;
1585 // if sent message is last response block message, remove data
1586 CARemoveBlockDataFromList(blockID);
1590 if (CA_MSG_NONCONFIRM == (*pdu)->hdr->coap_hdr_udp_t.type)
1592 OIC_LOG(DEBUG, TAG, "NON, send next block..");
1593 // update block data
1595 CAResult_t res = CAProcessNextStep(*pdu, NULL,
1596 CA_SENT_PREVIOUS_NON_MSG,
1598 if (CA_STATUS_OK != res)
1600 OIC_LOG(ERROR, TAG, "failed to process next step");
1601 CARemoveBlockDataFromList(blockID);
1609 OIC_LOG(DEBUG, TAG, "option2, not ACK msg");
1610 CAResult_t res = CAAddBlockOptionImpl(block2, COAP_OPTION_BLOCK2, options);
1611 if (CA_STATUS_OK != res)
1613 OIC_LOG(ERROR, TAG, "add has failed");
1614 CARemoveBlockDataFromList(blockID);
1618 res = CAAddOptionToPDU(*pdu, options);
1619 if (CA_STATUS_OK != res)
1621 OIC_LOG(ERROR, TAG, "add has failed");
1622 CARemoveBlockDataFromList(blockID);
1627 return CA_STATUS_OK;
1630 CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
1631 const CABlockDataID_t *blockID, coap_list_t **options)
1633 OIC_LOG(DEBUG, TAG, "IN-AddBlockOption1");
1634 VERIFY_NON_NULL(pdu, TAG, "pdu");
1635 VERIFY_NON_NULL((*pdu), TAG, "(*pdu)");
1636 VERIFY_NON_NULL((*pdu)->hdr, TAG, "(*pdu)->hdr");
1637 VERIFY_NON_NULL(info, TAG, "info");
1638 VERIFY_NON_NULL(blockID, TAG, "blockID");
1639 VERIFY_NON_NULL(options, TAG, "options");
1641 // get set block data from CABlock list-set.
1642 coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1645 OIC_LOG(ERROR, TAG, "getting has failed");
1646 return CA_STATUS_FAILED;
1649 CALogBlockInfo(block1);
1651 if (CA_MSG_ACKNOWLEDGE == (*pdu)->hdr->coap_hdr_udp_t.type)
1653 OIC_LOG(DEBUG, TAG, "option1 and ACK msg..");
1654 CAResult_t res = CAAddBlockOptionImpl(block1, COAP_OPTION_BLOCK1, options);
1655 if (CA_STATUS_OK != res)
1657 OIC_LOG(ERROR, TAG, "add has failed");
1658 CARemoveBlockDataFromList(blockID);
1662 res = CAAddOptionToPDU(*pdu, 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 CASetMoreBitFromBlock(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(block1, COAP_OPTION_BLOCK1, options);
1706 if (CA_STATUS_OK != res)
1708 OIC_LOG(ERROR, TAG, "add has failed");
1709 CARemoveBlockDataFromList(blockID);
1713 res = CAAddOptionToPDU(*pdu, options);
1714 if (CA_STATUS_OK != res)
1716 OIC_LOG(ERROR, TAG, "add has failed");
1717 CARemoveBlockDataFromList(blockID);
1721 CALogBlockInfo(block1);
1723 if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info->payload,
1724 block1->num, block1->szx))
1726 OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1727 return CA_STATUS_FAILED;
1730 // check the message type and if message type is NON, next block message will be sent
1731 if (CA_MSG_NONCONFIRM == (*pdu)->hdr->coap_hdr_udp_t.type)
1735 OIC_LOG(DEBUG, TAG, "NON, send next block..");
1736 // update block data
1738 CAResult_t res = CAProcessNextStep(*pdu, NULL,
1739 CA_SENT_PREVIOUS_NON_MSG,
1741 if (CA_STATUS_OK != res)
1743 OIC_LOG(ERROR, TAG, "failed to process next step");
1744 CARemoveBlockDataFromList(blockID);
1750 CARemoveBlockDataFromList(blockID);
1755 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption1");
1757 return CA_STATUS_OK;
1760 CAResult_t CAAddBlockOptionImpl(coap_block_t *block, uint8_t blockType,
1761 coap_list_t **options)
1763 OIC_LOG(DEBUG, TAG, "IN-AddBlockOptionImpl");
1764 VERIFY_NON_NULL(block, TAG, "block");
1765 VERIFY_NON_NULL(options, TAG, "options");
1767 unsigned char buf[BLOCKWISE_OPTION_BUFFER] = { 0 };
1768 unsigned int optionLength = coap_encode_var_bytes(buf,
1769 ((block->num << BLOCK_NUMBER_IDX)
1770 | (block->m << BLOCK_M_BIT_IDX)
1773 int ret = coap_insert(options,
1774 CACreateNewOptionNode(blockType, optionLength, (char *) buf),
1778 return CA_STATUS_INVALID_PARAM;
1781 OIC_LOG(DEBUG, TAG, "OUT-AddBlockOptionImpl");
1782 return CA_STATUS_OK;
1785 CAResult_t CAAddOptionToPDU(coap_pdu_t *pdu, coap_list_t **options)
1787 // after adding the block option to option list, add option list to pdu.
1790 for (coap_list_t *opt = *options; opt; opt = opt->next)
1792 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
1793 COAP_OPTION_DATA(*(coap_option *) opt->data));
1795 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
1796 int ret = coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
1797 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
1798 COAP_OPTION_DATA(*(coap_option *) opt->data), coap_udp);
1801 return CA_STATUS_FAILED;
1806 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
1808 return CA_STATUS_OK;
1811 CAResult_t CAAddBlockSizeOption(coap_pdu_t *pdu, uint16_t sizeType, size_t dataLength,
1812 coap_list_t **options)
1814 OIC_LOG(DEBUG, TAG, "IN-CAAddBlockSizeOption");
1815 VERIFY_NON_NULL(pdu, TAG, "pdu");
1816 VERIFY_NON_NULL(options, TAG, "options");
1818 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1820 OIC_LOG(ERROR, TAG, "unknown option type");
1821 return CA_STATUS_FAILED;
1824 unsigned char value[BLOCKWISE_OPTION_BUFFER] = { 0 };
1825 unsigned int optionLength = coap_encode_var_bytes(value, dataLength);
1827 int ret = coap_insert(options,
1828 CACreateNewOptionNode(sizeType, optionLength, (char *) value),
1832 return CA_STATUS_INVALID_PARAM;
1835 OIC_LOG(DEBUG, TAG, "OUT-CAAddBlockSizeOption");
1837 return CA_STATUS_OK;
1840 // TODO make pdu const after libcoap is updated to support that.
1841 bool CAIsPayloadLengthInPduWithBlockSizeOption(coap_pdu_t *pdu,
1843 size_t *totalPayloadLen)
1845 OIC_LOG(DEBUG, TAG, "IN-CAIsPayloadLengthInPduWithBlockSizeOption");
1846 VERIFY_NON_NULL(pdu, TAG, "pdu");
1847 VERIFY_NON_NULL(totalPayloadLen, TAG, "totalPayloadLen");
1849 if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1851 OIC_LOG(ERROR, TAG, "unknown option type");
1852 return CA_STATUS_FAILED;
1855 coap_opt_iterator_t opt_iter;
1856 coap_opt_t *option = coap_check_option(pdu, sizeType, &opt_iter);
1859 OIC_LOG(DEBUG, TAG, "get size option from pdu");
1860 *totalPayloadLen = coap_decode_var_bytes(COAP_OPT_VALUE(option),
1861 COAP_OPT_LENGTH(option));
1863 OIC_LOG_V(DEBUG, TAG, "the total payload length to be received is [%zu]bytes",
1869 OIC_LOG(DEBUG, TAG, "OUT-CAIsPayloadLengthInPduWithBlockSizeOption");
1874 uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlock,
1875 const CAData_t *receivedData, uint16_t blockType,
1878 OIC_LOG(DEBUG, TAG, "IN-CheckBlockError");
1880 VERIFY_NON_NULL(currData, TAG, "currData is NULL");
1881 VERIFY_NON_NULL(receivedBlock, TAG, "receivedBlock is NULL");
1882 VERIFY_NON_NULL(receivedData, TAG, "receivedData is NULL");
1884 // #1. check the received payload length
1885 size_t blockPayloadLen = 0;
1886 CAGetPayloadInfo(receivedData, &blockPayloadLen);
1888 // #2. check if the block sequence is right
1889 if (COAP_OPTION_BLOCK1 == blockType)
1891 size_t prePayloadLen = currData->receivedPayloadLen;
1892 if (prePayloadLen != (size_t)BLOCK_SIZE(receivedBlock->szx)
1893 * receivedBlock->num)
1895 if (receivedBlock->num > currData->block1.num + 1)
1897 // 408 Error handling of block loss
1898 OIC_LOG(ERROR, TAG, "option1: error 4.08");
1899 OIC_LOG(ERROR, TAG, "it didn't order");
1900 return CA_BLOCK_INCOMPLETE;
1902 return CA_BLOCK_RECEIVED_ALREADY;
1905 else if (COAP_OPTION_BLOCK2 == blockType)
1907 if (receivedBlock->num != currData->block2.num)
1909 if (receivedBlock->num > currData->block2.num)
1911 // 408 Error handling of block loss
1912 OIC_LOG(ERROR, TAG, "option2: error 4.08");
1913 OIC_LOG(ERROR, TAG, "it didn't order");
1914 return CA_BLOCK_INCOMPLETE;
1916 return CA_BLOCK_RECEIVED_ALREADY;
1920 // #3. check if error check logic is required
1921 size_t optionLen = dataLen - blockPayloadLen;
1922 if (receivedBlock->m && blockPayloadLen !=
1923 (size_t)BLOCK_SIZE(receivedBlock->szx))
1925 // 413 Error handling of too large entity
1926 if (COAP_MAX_PDU_SIZE < BLOCK_SIZE(receivedBlock->szx) + optionLen)
1928 // buffer size is smaller than received block size
1929 OIC_LOG(ERROR, TAG, "error type 4.13");
1930 OIC_LOG(ERROR, TAG, "too large size");
1932 // set the block size to be smaller than COAP_MAX_PDU_SIZE
1933 for (int32_t size = CA_DEFAULT_BLOCK_SIZE; size >= 0; size--)
1935 if (COAP_MAX_PDU_SIZE >= BLOCK_SIZE(size) + optionLen)
1937 OIC_LOG_V(ERROR, TAG, "replace sze with %d", size);
1938 if (COAP_OPTION_BLOCK2 == blockType)
1940 currData->block2.szx = size;
1944 currData->block1.szx = size;
1949 return CA_BLOCK_TOO_LARGE;
1953 // 408 Error handling of payload loss
1954 OIC_LOG(ERROR, TAG, "error type 4.08");
1955 OIC_LOG(ERROR, TAG, "payload len != block sze");
1956 return CA_BLOCK_INCOMPLETE;
1959 else if (0 == receivedBlock->m && 0 != currData->payloadLength)
1961 // if the received block is last block, check the total payload length
1962 size_t receivedPayloadLen = currData->receivedPayloadLen;
1963 receivedPayloadLen += blockPayloadLen;
1965 if (receivedPayloadLen != currData->payloadLength)
1967 OIC_LOG(ERROR, TAG, "error type 4.08");
1968 OIC_LOG(ERROR, TAG, "total payload length is wrong");
1969 return CA_BLOCK_INCOMPLETE;
1973 OIC_LOG(DEBUG, TAG, "received all data normally");
1975 OIC_LOG(DEBUG, TAG, "OUT-CheckBlockError");
1977 return CA_BLOCK_UNKNOWN;
1980 CAResult_t CAUpdatePayloadData(CABlockData_t *currData, const CAData_t *receivedData,
1981 uint8_t status, bool isSizeOption, uint16_t blockType)
1983 OIC_LOG(DEBUG, TAG, "IN-UpdatePayloadData");
1985 VERIFY_NON_NULL(currData, TAG, "currData");
1986 VERIFY_NON_NULL(receivedData, TAG, "receivedData");
1988 // if error code is 4.08, do not update payload
1989 if (CA_BLOCK_INCOMPLETE == status)
1991 OIC_LOG(ERROR, TAG, "no require to update");
1992 return CA_STATUS_OK;
1995 size_t blockPayloadLen = 0;
1996 CAPayload_t blockPayload = CAGetPayloadInfo(receivedData, &blockPayloadLen);
1998 if (CA_BLOCK_TOO_LARGE == status)
2000 blockPayloadLen = (COAP_OPTION_BLOCK2 == blockType) ?
2001 BLOCK_SIZE(currData->block2.szx) : BLOCK_SIZE(currData->block1.szx);
2004 // memory allocation for the received block payload
2005 size_t prePayloadLen = currData->receivedPayloadLen;
2008 if (currData->payloadLength)
2010 // in case the block message has the size option
2011 // allocate the memory for the total payload
2014 CAPayload_t prePayload = currData->payload;
2016 OIC_LOG(DEBUG, TAG, "allocate memory for the total payload");
2017 currData->payload = (CAPayload_t) OICCalloc(1, currData->payloadLength);
2018 if (NULL == currData->payload)
2020 OIC_LOG(ERROR, TAG, "out of memory");
2021 return CA_MEMORY_ALLOC_FAILED;
2023 memcpy(currData->payload, prePayload, prePayloadLen);
2024 OICFree(prePayload);
2027 // update the total payload
2028 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
2032 OIC_LOG(DEBUG, TAG, "allocate memory for the received block payload");
2034 size_t totalPayloadLen = prePayloadLen + blockPayloadLen;
2035 CAPayload_t newPayload = OICRealloc(currData->payload, totalPayloadLen);
2036 if (NULL == newPayload)
2038 OIC_LOG(ERROR, TAG, "out of memory");
2039 return CA_MEMORY_ALLOC_FAILED;
2042 // update the total payload
2043 memset(newPayload + prePayloadLen, 0, blockPayloadLen);
2044 currData->payload = newPayload;
2045 memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
2048 // update received payload length
2049 currData->receivedPayloadLen += blockPayloadLen;
2051 OIC_LOG_V(DEBUG, TAG, "updated payload: %s, len: %zu", currData->payload,
2052 currData->receivedPayloadLen);
2055 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayloadData");
2056 return CA_STATUS_OK;
2059 CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
2061 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", NULL);
2062 VERIFY_NON_NULL_RET(pdu->hdr, TAG, "pdu->hdr", NULL);
2063 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
2065 CAInfo_t responseData = { .tokenLength = pdu->hdr->coap_hdr_udp_t.token_length };
2066 responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength);
2067 if (!responseData.token)
2069 OIC_LOG(ERROR, TAG, "out of memory");
2072 memcpy(responseData.token, pdu->hdr->coap_hdr_udp_t.token, responseData.tokenLength);
2074 CAResponseInfo_t* responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
2077 OIC_LOG(ERROR, TAG, "out of memory");
2078 OICFree(responseData.token);
2081 responseInfo->info = responseData;
2083 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2086 OIC_LOG(ERROR, TAG, "out of memory");
2087 OICFree(responseInfo);
2091 data->requestInfo = NULL;
2092 data->responseInfo = responseInfo;
2093 data->remoteEndpoint = CACloneEndpoint(endpoint);
2094 data->type = SEND_TYPE_UNICAST;
2099 CAData_t *CACloneCAData(const CAData_t *data)
2101 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2103 CAData_t *clone = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2106 OIC_LOG(ERROR, TAG, "out of memory");
2111 if (data->requestInfo)
2113 clone->requestInfo = CACloneRequestInfo(data->requestInfo);
2115 else if (data->responseInfo)
2117 clone->responseInfo = CACloneResponseInfo(data->responseInfo);
2120 if (data->remoteEndpoint)
2122 clone->remoteEndpoint = CACloneEndpoint(data->remoteEndpoint);
2128 CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
2131 OIC_LOG(DEBUG, TAG, "IN-UpdatePayload");
2133 VERIFY_NON_NULL(data, TAG, "data is NULL");
2134 VERIFY_NON_NULL(payload, TAG, "payload is NULL");
2136 CAPayload_t newPayload = NULL;
2137 switch (data->dataType)
2139 case CA_REQUEST_DATA:
2140 if (!data->requestInfo)
2142 OIC_LOG(ERROR, TAG, "request info is null");
2143 return CA_STATUS_FAILED;
2145 // allocate payload field
2146 newPayload = OICRealloc(data->requestInfo->info.payload, payloadLen);
2149 OIC_LOG(ERROR, TAG, "out of memory");
2150 return CA_STATUS_FAILED;
2152 data->requestInfo->info.payload = newPayload;
2153 memcpy(data->requestInfo->info.payload, payload, payloadLen);
2154 data->requestInfo->info.payloadSize = payloadLen;
2157 case CA_RESPONSE_DATA:
2158 if (!data->responseInfo)
2160 OIC_LOG(ERROR, TAG, "response info is null");
2161 return CA_STATUS_FAILED;
2163 // allocate payload field
2164 newPayload = OICRealloc(data->responseInfo->info.payload, payloadLen);
2167 OIC_LOG(ERROR, TAG, "out of memory");
2168 return CA_STATUS_FAILED;
2170 data->responseInfo->info.payload = newPayload;
2171 memcpy(data->responseInfo->info.payload, payload, payloadLen);
2172 data->responseInfo->info.payloadSize = payloadLen;
2176 // does not occur case
2177 OIC_LOG(ERROR, TAG, "not supported data type");
2178 return CA_NOT_SUPPORTED;
2181 OIC_LOG(DEBUG, TAG, "OUT-UpdatePayload");
2183 return CA_STATUS_OK;
2186 CAPayload_t CAGetPayloadInfo(const CAData_t *data, size_t *payloadLen)
2188 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2189 VERIFY_NON_NULL_RET(payloadLen, TAG, "payloadLen", NULL);
2191 if (data->requestInfo)
2193 if (data->requestInfo->info.payload)
2195 *payloadLen = data->requestInfo->info.payloadSize;
2196 return data->requestInfo->info.payload;
2201 if (data->responseInfo->info.payload)
2203 *payloadLen = data->responseInfo->info.payloadSize;
2204 return data->responseInfo->info.payload;
2211 CAResult_t CAHandleBlockErrorResponse(coap_block_t *block, uint16_t blockType,
2212 uint32_t responseResult)
2214 OIC_LOG(DEBUG, TAG, "IN-HandleBlockErrorRes");
2215 VERIFY_NON_NULL(block, TAG, "block is NULL");
2217 // update block data
2218 switch (responseResult)
2220 case CA_REQUEST_ENTITY_INCOMPLETE:
2223 case CA_REQUEST_ENTITY_TOO_LARGE:
2224 if (COAP_OPTION_BLOCK1 == blockType)
2231 OIC_LOG_V(ERROR, TAG, "there is no Error Code of BWT[%d]", responseResult);
2232 return CA_STATUS_FAILED;
2235 OIC_LOG(DEBUG, TAG, "OUT-HandleBlockErrorRes");
2236 return CA_STATUS_OK;
2239 CAResult_t CAUpdateBlockOptionType(const CABlockDataID_t *blockID,
2242 OIC_LOG(DEBUG, TAG, "IN-UpdateBlockOptionType");
2243 VERIFY_NON_NULL(blockID, TAG, "blockID");
2245 ca_mutex_lock(g_context.blockDataListMutex);
2247 size_t len = u_arraylist_length(g_context.dataList);
2248 for (size_t i = 0; i < len; i++)
2250 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2251 if (CABlockidMatches(currData, blockID))
2253 currData->type = blockType;
2254 ca_mutex_unlock(g_context.blockDataListMutex);
2255 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2256 return CA_STATUS_OK;
2259 ca_mutex_unlock(g_context.blockDataListMutex);
2261 OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2262 return CA_STATUS_FAILED;
2265 uint8_t CAGetBlockOptionType(const CABlockDataID_t *blockID)
2267 OIC_LOG(DEBUG, TAG, "IN-GetBlockOptionType");
2268 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", 0);
2270 ca_mutex_lock(g_context.blockDataListMutex);
2272 size_t len = u_arraylist_length(g_context.dataList);
2273 for (size_t i = 0; i < len; i++)
2275 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2276 if (CABlockidMatches(currData, blockID))
2278 ca_mutex_unlock(g_context.blockDataListMutex);
2279 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2280 return currData->type;
2283 ca_mutex_unlock(g_context.blockDataListMutex);
2285 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2289 CAData_t *CAGetDataSetFromBlockDataList(const CABlockDataID_t *blockID)
2291 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2293 ca_mutex_lock(g_context.blockDataListMutex);
2295 size_t len = u_arraylist_length(g_context.dataList);
2296 for (size_t i = 0; i < len; i++)
2298 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2299 if (CABlockidMatches(currData, blockID))
2301 ca_mutex_unlock(g_context.blockDataListMutex);
2302 return currData->sentData;
2305 ca_mutex_unlock(g_context.blockDataListMutex);
2310 CAResult_t CAGetTokenFromBlockDataList(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
2311 CAResponseInfo_t *responseInfo)
2313 OIC_LOG(DEBUG, TAG, "IN-CAGetTokenFromBlockDataList");
2314 VERIFY_NON_NULL(pdu, TAG, "pdu");
2315 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
2316 VERIFY_NON_NULL(responseInfo, TAG, "responseInfo");
2318 ca_mutex_lock(g_context.blockDataListMutex);
2320 size_t len = u_arraylist_length(g_context.dataList);
2321 for (size_t i = 0; i < len; i++)
2323 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2324 if (NULL == currData)
2329 if (NULL != currData->sentData && NULL != currData->sentData->requestInfo)
2331 if (pdu->hdr->coap_hdr_udp_t.id == currData->sentData->requestInfo->info.messageId &&
2332 endpoint->adapter == currData->sentData->remoteEndpoint->adapter)
2334 if (NULL != currData->sentData->requestInfo->info.token)
2336 uint8_t length = currData->sentData->requestInfo->info.tokenLength;
2337 responseInfo->info.tokenLength = length;
2338 responseInfo->info.token = (char *) OICMalloc(length);
2339 if (NULL == responseInfo->info.token)
2341 OIC_LOG(ERROR, TAG, "out of memory");
2342 ca_mutex_unlock(g_context.blockDataListMutex);
2343 return CA_MEMORY_ALLOC_FAILED;
2345 memcpy(responseInfo->info.token, currData->sentData->requestInfo->info.token,
2346 responseInfo->info.tokenLength);
2348 ca_mutex_unlock(g_context.blockDataListMutex);
2349 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2350 return CA_STATUS_OK;
2356 ca_mutex_unlock(g_context.blockDataListMutex);
2358 OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2359 return CA_STATUS_FAILED;
2362 CAResult_t CACheckBlockDataValidation(const CAData_t *sendData, CABlockData_t **blockData)
2364 VERIFY_NON_NULL(sendData, TAG, "sendData");
2365 VERIFY_NON_NULL(blockData, TAG, "blockData");
2367 CABlockDataID_t* blockDataID;
2368 if(sendData->requestInfo)
2370 blockDataID = CACreateBlockDatablockId(
2371 (CAToken_t)sendData->requestInfo->info.token,
2372 sendData->requestInfo->info.tokenLength,
2373 sendData->remoteEndpoint->port);
2375 else if(sendData->responseInfo)
2377 blockDataID = CACreateBlockDatablockId(
2378 (CAToken_t)sendData->responseInfo->info.token,
2379 sendData->responseInfo->info.tokenLength,
2380 sendData->remoteEndpoint->port);
2384 OIC_LOG(ERROR, TAG, "sendData doesn't have requestInfo or responseInfo");
2385 return CA_STATUS_FAILED;
2388 if (NULL == blockDataID || blockDataID->idLength < 1)
2390 OIC_LOG(ERROR, TAG, "blockId is null");
2391 CADestroyBlockID(blockDataID);
2392 return CA_STATUS_FAILED;
2395 ca_mutex_lock(g_context.blockDataListMutex);
2397 size_t len = u_arraylist_length(g_context.dataList);
2398 for (size_t i = 0; i < len; i++)
2400 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2407 if (sendData->requestInfo) // sendData is requestMessage
2409 OIC_LOG(DEBUG, TAG, "Send request");
2410 if (NULL != currData->blockDataId
2411 && NULL != currData->blockDataId->id
2412 && currData->blockDataId->idLength > 0
2413 && NULL != sendData->requestInfo->info.token)
2415 if (CABlockidMatches(currData, blockDataID))
2417 OIC_LOG(ERROR, TAG, "already sent");
2422 else if (sendData->responseInfo) // sendData is responseMessage
2424 OIC_LOG(DEBUG, TAG, "Send response");
2425 if (NULL != currData->blockDataId
2426 && NULL != currData->blockDataId->id
2427 && currData->blockDataId->idLength > 0
2428 && NULL != sendData->responseInfo->info.token)
2430 if (CABlockidMatches(currData, blockDataID))
2433 if (NULL != currData->sentData)
2435 OIC_LOG(DEBUG, TAG, "init block number");
2436 CADestroyDataSet(currData->sentData);
2438 currData->sentData = CACloneCAData(sendData);
2439 *blockData = currData;
2440 ca_mutex_unlock(g_context.blockDataListMutex);
2441 CADestroyBlockID(blockDataID);
2442 return CA_STATUS_OK;
2448 OIC_LOG(ERROR, TAG, "no CAInfo data");
2452 ca_mutex_unlock(g_context.blockDataListMutex);
2454 CADestroyBlockID(blockDataID);
2456 return CA_STATUS_FAILED;
2459 CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID)
2461 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2463 ca_mutex_lock(g_context.blockDataListMutex);
2465 size_t len = u_arraylist_length(g_context.dataList);
2466 for (size_t i = 0; i < len; i++)
2468 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2469 if (CABlockidMatches(currData, blockID))
2471 ca_mutex_unlock(g_context.blockDataListMutex);
2475 ca_mutex_unlock(g_context.blockDataListMutex);
2480 coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID,
2483 OIC_LOG(DEBUG, TAG, "IN-GetBlockOption");
2484 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2486 ca_mutex_lock(g_context.blockDataListMutex);
2488 size_t len = u_arraylist_length(g_context.dataList);
2489 for (size_t i = 0; i < len; i++)
2491 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2492 if (CABlockidMatches(currData, blockID))
2494 ca_mutex_unlock(g_context.blockDataListMutex);
2495 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2496 if (COAP_OPTION_BLOCK2 == blockType)
2498 return &currData->block2;
2502 return &currData->block1;
2506 ca_mutex_unlock(g_context.blockDataListMutex);
2508 OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2512 CAPayload_t CAGetPayloadFromBlockDataList(const CABlockDataID_t *blockID,
2513 size_t *fullPayloadLen)
2515 OIC_LOG(DEBUG, TAG, "IN-GetFullPayload");
2516 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2517 VERIFY_NON_NULL_RET(fullPayloadLen, TAG, "fullPayloadLen", NULL);
2519 ca_mutex_lock(g_context.blockDataListMutex);
2521 size_t len = u_arraylist_length(g_context.dataList);
2522 for (size_t i = 0; i < len; i++)
2524 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2525 if (CABlockidMatches(currData, blockID))
2527 ca_mutex_unlock(g_context.blockDataListMutex);
2528 *fullPayloadLen = currData->receivedPayloadLen;
2529 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2530 return currData->payload;
2533 ca_mutex_unlock(g_context.blockDataListMutex);
2535 OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2539 CABlockData_t *CACreateNewBlockData(const CAData_t *sendData)
2541 OIC_LOG(DEBUG, TAG, "IN-CACreateNewBlockData");
2542 VERIFY_NON_NULL_RET(sendData, TAG, "sendData", NULL);
2544 // create block data
2545 CABlockData_t *data = (CABlockData_t *) OICCalloc(1, sizeof(CABlockData_t));
2548 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2552 data->block1.szx = CA_DEFAULT_BLOCK_SIZE;
2553 data->block2.szx = CA_DEFAULT_BLOCK_SIZE;
2554 data->sentData = CACloneCAData(sendData);
2557 OIC_LOG(ERROR, TAG, PCF("memory alloc has failed"));
2562 CAToken_t token = NULL;
2563 uint8_t tokenLength = 0;
2564 if (data->sentData->requestInfo)
2566 // update token info
2567 tokenLength = data->sentData->requestInfo->info.tokenLength;
2568 token = data->sentData->requestInfo->info.token;
2570 else if(data->sentData->responseInfo)
2572 tokenLength = data->sentData->responseInfo->info.tokenLength;
2573 token = data->sentData->responseInfo->info.token;
2576 if (!data->sentData->remoteEndpoint)
2578 OIC_LOG(ERROR, TAG, "remoteEndpoint is null");
2579 CADestroyDataSet(data->sentData);
2584 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2586 data->sentData->remoteEndpoint->port);
2587 if (NULL == blockDataID || blockDataID->idLength < 1)
2589 OIC_LOG(ERROR, TAG, "blockId is null");
2590 CADestroyBlockID(blockDataID);
2591 CADestroyDataSet(data->sentData);
2595 data->blockDataId = blockDataID;
2597 ca_mutex_lock(g_context.blockDataListMutex);
2599 bool res = u_arraylist_add(g_context.dataList, (void *) data);
2602 OIC_LOG(ERROR, TAG, "add has failed");
2603 CADestroyBlockID(data->blockDataId);
2604 CADestroyDataSet(data->sentData);
2606 ca_mutex_unlock(g_context.blockDataListMutex);
2609 ca_mutex_unlock(g_context.blockDataListMutex);
2611 OIC_LOG(DEBUG, TAG, "OUT-CreateBlockData");
2615 CAResult_t CARemoveBlockDataFromList(const CABlockDataID_t *blockID)
2617 OIC_LOG(DEBUG, TAG, "CARemoveBlockData");
2618 VERIFY_NON_NULL(blockID, TAG, "blockID");
2620 ca_mutex_lock(g_context.blockDataListMutex);
2622 size_t len = u_arraylist_length(g_context.dataList);
2623 for (size_t i = 0; i < len; i++)
2625 CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2626 if (CABlockidMatches(currData, blockID))
2628 CABlockData_t *removedData = u_arraylist_remove(g_context.dataList, i);
2631 OIC_LOG(ERROR, TAG, "data is NULL");
2632 ca_mutex_unlock(g_context.blockDataListMutex);
2633 return CA_STATUS_FAILED;
2637 if (currData->sentData)
2639 CADestroyDataSet(currData->sentData);
2641 CADestroyBlockID(currData->blockDataId);
2642 OICFree(currData->payload);
2644 ca_mutex_unlock(g_context.blockDataListMutex);
2645 return CA_STATUS_OK;
2648 ca_mutex_unlock(g_context.blockDataListMutex);
2650 return CA_STATUS_OK;
2653 void CADestroyDataSet(CAData_t* data)
2655 VERIFY_NON_NULL_VOID(data, TAG, "data");
2657 CAFreeEndpoint(data->remoteEndpoint);
2658 CADestroyRequestInfoInternal(data->requestInfo);
2659 CADestroyResponseInfoInternal(data->responseInfo);
2663 CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength,
2664 uint16_t portNumber)
2666 char port[PORT_LENGTH] = {0,};
2667 port[0] = (char)((portNumber>>8) & 0xFF);
2668 port[1] = (char)(portNumber & 0xFF);
2670 CABlockDataID_t* blockDataID = (CABlockDataID_t *) OICMalloc(sizeof(CABlockDataID_t));
2673 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2676 blockDataID->idLength = tokenLength + sizeof(port);
2677 blockDataID->id = (uint8_t *) OICMalloc(blockDataID->idLength);
2678 if (!blockDataID->id)
2680 OIC_LOG(ERROR, TAG, "memory alloc has failed");
2681 OICFree(blockDataID);
2687 memcpy(blockDataID->id, token, tokenLength);
2690 memcpy(blockDataID->id + tokenLength, port, sizeof(port));
2692 OIC_LOG(DEBUG, TAG, "BlockID is ");
2693 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)blockDataID->id, blockDataID->idLength);
2698 void CADestroyBlockID(CABlockDataID_t *blockID)
2700 VERIFY_NON_NULL_VOID(blockID, TAG, "blockID");
2701 OICFree(blockID->id);
2706 bool CABlockidMatches(const CABlockData_t *currData, const CABlockDataID_t *blockID)
2708 VERIFY_NON_NULL_RET(currData, TAG, "currData", false);
2709 VERIFY_NON_NULL_RET(blockID, TAG, "blockID", false);
2710 VERIFY_NON_NULL_RET(blockID->id, TAG, "blockID->id", false);
2712 if ((currData->blockDataId)
2713 && (currData->blockDataId->id)
2714 && (currData->blockDataId->idLength == blockID->idLength)
2715 && !memcmp(currData->blockDataId->id, blockID->id, currData->blockDataId->idLength))
2722 void CALogBlockInfo(coap_block_t *block)
2724 VERIFY_NON_NULL_VOID(block, TAG, "block");
2726 OIC_LOG(DEBUG, TAG, "block option info");
2728 OIC_LOG_V(DEBUG, TAG, "block option-num : %d", block->num);
2730 OIC_LOG_V(DEBUG, TAG, "block option-m : %d", block->m);
2732 OIC_LOG_V(DEBUG, TAG, "block option-szx : %d", block->szx);