Merge branch 'master' into security-summit
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / cablockwisetransfer.c
1 /* ****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
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
26 //
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
31 #define _BSD_SOURCE
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "caadapterutils.h"
38 #include "cainterface.h"
39 #include "camessagehandler.h"
40 #include "caremotehandler.h"
41 #include "cablockwisetransfer.h"
42 #include "oic_malloc.h"
43 #include "camutex.h"
44 #include "logger.h"
45
46 #define TAG "CA_BWT"
47
48 #define BLOCKWISE_OPTION_BUFFER    (sizeof(unsigned int))
49 #define BLOCK_NUMBER_IDX           4
50 #define BLOCK_M_BIT_IDX            3
51 #define PORT_LENGTH                2
52
53 #define BLOCK_SIZE(arg) (1 << ((arg) + 4))
54
55 // context for block-wise transfer
56 static CABlockWiseContext_t g_context = { 0 };
57
58 static bool CACheckPayloadLength(const CAData_t *sendData)
59 {
60     size_t payloadLen = 0;
61     CAGetPayloadInfo(sendData, &payloadLen);
62
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);
66
67     if (payloadLen <= maxBlockSize)
68     {
69         return false;
70     }
71
72     return true;
73 }
74
75 CAResult_t CAInitializeBlockWiseTransfer(CASendThreadFunc sendThreadFunc,
76                                          CAReceiveThreadFunc receivedThreadFunc)
77 {
78     OIC_LOG(DEBUG, TAG, "initialize");
79
80     // set block-wise transfer context
81     if (!g_context.sendThreadFunc)
82     {
83         g_context.sendThreadFunc = sendThreadFunc;
84     }
85
86     if (!g_context.receivedThreadFunc)
87     {
88         g_context.receivedThreadFunc = receivedThreadFunc;
89     }
90
91     if (!g_context.dataList)
92     {
93         g_context.dataList = u_arraylist_create();
94     }
95
96     CAResult_t res = CAInitBlockWiseMutexVariables();
97     if (CA_STATUS_OK != res)
98     {
99         OIC_LOG(ERROR, TAG, "init has failed");
100     }
101
102     return res;
103 }
104
105 CAResult_t CATerminateBlockWiseTransfer()
106 {
107     OIC_LOG(DEBUG, TAG, "terminate");
108
109     if (g_context.dataList)
110     {
111         u_arraylist_free(&g_context.dataList);
112     }
113
114     CATerminateBlockWiseMutexVariables();
115
116     return CA_STATUS_OK;
117 }
118
119 CAResult_t CAInitBlockWiseMutexVariables()
120 {
121     OIC_LOG(DEBUG, TAG, "IN");
122
123     if (!g_context.blockDataListMutex)
124     {
125         g_context.blockDataListMutex = ca_mutex_new();
126         if (!g_context.blockDataListMutex)
127         {
128             OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
129             return CA_STATUS_FAILED;
130         }
131     }
132
133     if (!g_context.blockDataSenderMutex)
134     {
135         g_context.blockDataSenderMutex = ca_mutex_new();
136         if (!g_context.blockDataSenderMutex)
137         {
138             OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
139             CATerminateBlockWiseMutexVariables();
140             return CA_STATUS_FAILED;
141         }
142     }
143
144     return CA_STATUS_OK;
145 }
146
147 void CATerminateBlockWiseMutexVariables()
148 {
149     OIC_LOG(DEBUG, TAG, "IN");
150
151     if (g_context.blockDataListMutex)
152     {
153         ca_mutex_free(g_context.blockDataListMutex);
154         g_context.blockDataListMutex = NULL;
155     }
156
157     if (g_context.blockDataSenderMutex)
158     {
159         ca_mutex_free(g_context.blockDataSenderMutex);
160         g_context.blockDataSenderMutex = NULL;
161     }
162 }
163
164 CAResult_t CASendBlockWiseData(const CAData_t *sendData)
165 {
166     VERIFY_NON_NULL(sendData, TAG, "sendData");
167
168     // check if message type is CA_MSG_RESET
169     if (sendData->responseInfo)
170     {
171         if (CA_MSG_RESET == sendData->responseInfo->info.type)
172         {
173             OIC_LOG(DEBUG, TAG, "reset message can't be sent to the block");
174             return CA_NOT_SUPPORTED;
175         }
176     }
177
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)
182     {
183         // #2. if it is not included, add the data into list
184         if (!currData)
185         {
186             OIC_LOG(DEBUG, TAG, "There is no block data");
187
188             bool isBlock = CACheckPayloadLength(sendData);
189             if (!isBlock)
190             {
191                 if (sendData->requestInfo)
192                 {
193                     currData = CACreateNewBlockData(sendData);
194                     if (!currData)
195                     {
196                         OIC_LOG(ERROR, TAG, "failed to create block data");
197                         return CA_MEMORY_ALLOC_FAILED;
198                     }
199                 }
200                 return CA_NOT_SUPPORTED;
201             }
202             currData = CACreateNewBlockData(sendData);
203             if (!currData)
204             {
205                 OIC_LOG(ERROR, TAG, "failed to create block data");
206                 return CA_MEMORY_ALLOC_FAILED;
207             }
208         }
209     }
210
211     // #3. check request/response block option type and payload length
212     res = CACheckBlockOptionType(currData);
213     if (CA_STATUS_OK == res)
214     {
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)
220         {
221             OIC_LOG(ERROR, TAG, "add has failed");
222             return res;
223         }
224     }
225
226     return res;
227 }
228
229 CAResult_t CAAddSendThreadQueue(const CAData_t *sendData, const CABlockDataID_t *blockID)
230 {
231     VERIFY_NON_NULL(sendData, TAG, "sendData");
232     VERIFY_NON_NULL(blockID, TAG, "blockID");
233
234     CAData_t *cloneData = CACloneCAData(sendData);
235     if (!cloneData)
236     {
237         OIC_LOG(ERROR, TAG, "clone has failed");
238         CARemoveBlockDataFromList(blockID);
239         return CA_STATUS_FAILED;
240     }
241
242     if (g_context.sendThreadFunc)
243     {
244         ca_mutex_lock(g_context.blockDataSenderMutex);
245         g_context.sendThreadFunc(cloneData);
246         ca_mutex_unlock(g_context.blockDataSenderMutex);
247     }
248     else
249     {
250         CADestroyDataSet(cloneData);
251     }
252     return CA_STATUS_OK;
253 }
254
255 CAResult_t CACheckBlockOptionType(CABlockData_t *currData)
256 {
257     VERIFY_NON_NULL(currData, TAG, "currData");
258     VERIFY_NON_NULL(currData->sentData, TAG, "currData->sentData");
259
260     bool isBlock = CACheckPayloadLength(currData->sentData);
261     if (!isBlock)
262     {
263         return CA_NOT_SUPPORTED;
264     }
265
266     // set block option (COAP_OPTION_BLOCK2 or COAP_OPTION_BLOCK1)
267     if (currData->sentData->requestInfo) // request message
268     {
269         currData->type = COAP_OPTION_BLOCK1;
270     }
271     else // response message
272     {
273         currData->type = COAP_OPTION_BLOCK2;
274     }
275
276     return CA_STATUS_OK;
277 }
278
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)
282 {
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");
288
289     // check if received message type is CA_MSG_RESET
290     if (CA_EMPTY == pdu->hdr->code)
291     {
292         OIC_LOG(DEBUG, TAG, "code is CA_EMPTY..");
293
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)
297         {
298             OIC_LOG(ERROR, TAG, "fail to get token");
299             return res;
300         }
301
302         CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
303                 receivedData->responseInfo->info.token,
304                 receivedData->responseInfo->info.tokenLength,
305                 endpoint->port);
306         if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
307         {
308             OIC_LOG(ERROR, TAG, "blockId is null");
309             CADestroyBlockID(blockDataID);
310             return CA_STATUS_FAILED;
311         }
312
313         CARemoveBlockDataFromList(blockDataID);
314         CADestroyBlockID(blockDataID);
315         return CA_NOT_SUPPORTED;
316     }
317
318     // check if block option is set and get block data
319     coap_block_t block = {0, 0, 0};
320
321     // get block1 option
322     int isBlock1 = coap_get_block(pdu, COAP_OPTION_BLOCK1, &block);
323     if (isBlock1)
324     {
325         CAResult_t res = CASetNextBlockOption1(pdu, endpoint, receivedData, block, dataLen);
326         if (CA_STATUS_OK != res)
327         {
328             OIC_LOG(ERROR, TAG, "setting has failed");
329             return res;
330         }
331     }
332
333     // get block2 option
334     int isBlock2 = coap_get_block(pdu, COAP_OPTION_BLOCK2, &block);
335     if (isBlock2)
336     {
337         CAResult_t res = CASetNextBlockOption2(pdu, endpoint, receivedData, block, dataLen);
338         if (CA_STATUS_OK != res)
339         {
340             OIC_LOG(ERROR, TAG, "setting has failed");
341             return res;
342         }
343     }
344
345     // check if there is error code
346     if (!isBlock1 && !isBlock2)
347     {
348         uint32_t code = CA_RESPONSE_CODE(pdu->hdr->code);
349         if (CA_REQUEST_ENTITY_INCOMPLETE == code)
350         {
351             CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
352                     (CAToken_t)pdu->hdr->token,
353                      pdu->hdr->token_length,
354                      endpoint->port);
355
356             if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
357             {
358                 OIC_LOG(ERROR, TAG, "blockId is null");
359                 CADestroyBlockID(blockDataID);
360                 return CA_STATUS_FAILED;
361             }
362
363             CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
364             if (!data)
365             {
366                 OIC_LOG(ERROR, TAG, "getting has failed");
367                 CADestroyBlockID(blockDataID);
368                 return CA_STATUS_FAILED;
369             }
370
371             if (COAP_OPTION_BLOCK2 == data->type)
372             {
373                 coap_block_t *block2 = CAGetBlockOption(blockDataID, COAP_OPTION_BLOCK2);
374                 if (!block2)
375                 {
376                     OIC_LOG(ERROR, TAG, "block is null");
377                     CADestroyBlockID(blockDataID);
378                     return CA_STATUS_FAILED;
379                 }
380
381                 CAResult_t res = CASetNextBlockOption2(pdu, endpoint, receivedData, *block2,
382                                                        dataLen);
383                 if (CA_STATUS_OK != res)
384                 {
385                     OIC_LOG(ERROR, TAG, "setting has failed");
386                     CADestroyBlockID(blockDataID);
387                     return res;
388                 }
389             }
390             else if (COAP_OPTION_BLOCK1 == data->type)
391             {
392                 coap_block_t *block1 = CAGetBlockOption(blockDataID, COAP_OPTION_BLOCK1);
393                 if (!block1)
394                 {
395                     OIC_LOG(ERROR, TAG, "block is null");
396                     CADestroyBlockID(blockDataID);
397                     return CA_STATUS_FAILED;
398                 }
399
400                 CAResult_t res = CASetNextBlockOption1(pdu, endpoint, receivedData, *block1,
401                                                        dataLen);
402                 if (CA_STATUS_OK != res)
403                 {
404                     OIC_LOG(ERROR, TAG, "setting has failed");
405                     CADestroyBlockID(blockDataID);
406                     return res;
407                 }
408             }
409         }
410         else
411         {
412             // normal pdu data
413             OIC_LOG(DEBUG, TAG, "it's normal pdu");
414
415             // if received data is response message
416             // and sent data remain in block data list, remove block data
417             if (receivedData->responseInfo)
418             {
419                 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
420                         (CAToken_t)pdu->hdr->token,
421                         pdu->hdr->token_length,
422                         endpoint->port);
423                 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
424                 {
425                     OIC_LOG(ERROR, TAG, "blockId is null");
426                     CADestroyBlockID(blockDataID);
427                     return CA_STATUS_FAILED;
428                 }
429
430                 CARemoveBlockDataFromList(blockDataID);
431                 CADestroyBlockID(blockDataID);
432             }
433             return CA_NOT_SUPPORTED;
434         }
435     }
436
437     return CA_STATUS_OK;
438 }
439
440 CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData,
441                              uint8_t blockWiseStatus, const CABlockDataID_t *blockID)
442 {
443     VERIFY_NON_NULL(pdu, TAG, "pdu");
444     VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
445     VERIFY_NON_NULL(blockID, TAG, "blockID");
446
447     CAResult_t res = CA_STATUS_OK;
448     CAData_t *data = NULL;
449
450     // process blockWiseStatus
451     switch (blockWiseStatus)
452     {
453         case CA_OPTION2_FIRST_BLOCK:
454             res = CAAddSendThreadQueue(receivedData, blockID);
455             if (CA_STATUS_OK != res)
456             {
457                 OIC_LOG(ERROR, TAG, "add has failed");
458                 return res;
459             }
460             break;
461
462         case CA_OPTION2_CON:
463             // add data to send thread
464             data = CAGetDataSetFromBlockDataList(blockID);
465             if (!data)
466             {
467                 OIC_LOG(ERROR, TAG, "it's unavailable");
468                 return CA_STATUS_FAILED;
469             }
470
471             if (data->requestInfo)
472             {
473                 data->requestInfo->info.messageId = pdu->hdr->id;
474             }
475
476             if (data->responseInfo)
477             {
478                 data->responseInfo->info.messageId = pdu->hdr->id;
479             }
480
481             res = CAAddSendThreadQueue(data, blockID);
482             if (CA_STATUS_OK != res)
483             {
484                 OIC_LOG(ERROR, TAG, "add has failed");
485                 return res;
486             }
487
488             break;
489
490         case CA_OPTION1_ACK:
491         case CA_OPTION2_ACK:
492         case CA_SENT_PREVIOUS_NON_MSG:
493             res = CASendBlockMessage(pdu, CA_MSG_CONFIRM, blockWiseStatus,
494                                      blockID);
495             if (CA_STATUS_OK != res)
496             {
497                 OIC_LOG(ERROR, TAG, "send has failed");
498                 return res;
499             }
500             break;
501
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)
506             {
507                 OIC_LOG(ERROR, TAG, "receive has failed");
508                 return res;
509             }
510
511             // remove data from list
512             res = CARemoveBlockDataFromList(blockID);
513             if (CA_STATUS_OK != res)
514             {
515                 OIC_LOG(ERROR, TAG, "remove has failed");
516                 return res;
517             }
518             break;
519
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)
524             {
525                 OIC_LOG(ERROR, TAG, "receive has failed");
526                 return res;
527             }
528
529             if (CA_MSG_NONCONFIRM == pdu->hdr->type)
530             {
531                 // remove data from list
532                 res = CARemoveBlockDataFromList(blockID);
533                 if (CA_STATUS_OK != res)
534                 {
535                     OIC_LOG(ERROR, TAG, "remove has failed");
536                     return res;
537                 }
538             }
539             break;
540
541         case CA_OPTION1_NO_ACK_BLOCK:
542             if (CA_MSG_CONFIRM == pdu->hdr->type)
543             {
544                 // add data to send thread
545                 res = CASendBlockMessage(pdu, CA_MSG_ACKNOWLEDGE, blockWiseStatus,
546                                          blockID);
547                 if (CA_STATUS_OK != res)
548                 {
549                     OIC_LOG(ERROR, TAG, "send has failed");
550                     return res;
551                 }
552             }
553             break;
554
555         case CA_BLOCK_INCOMPLETE:
556             if (CA_MSG_CONFIRM == pdu->hdr->type || CA_MSG_ACKNOWLEDGE == pdu->hdr->type)
557             {
558                 // add data to send thread
559                 res = CASendErrorMessage(pdu, blockWiseStatus,
560                                          CA_REQUEST_ENTITY_INCOMPLETE,
561                                          blockID);
562                 if (CA_STATUS_OK != res)
563                 {
564                     OIC_LOG(ERROR, TAG, "send has failed");
565                     return res;
566                 }
567             }
568             break;
569
570         case CA_BLOCK_TOO_LARGE:
571             if (CA_MSG_ACKNOWLEDGE == pdu->hdr->type)
572             {
573                 res = CASendBlockMessage(pdu, CA_MSG_CONFIRM, blockWiseStatus,
574                                          blockID);
575                 if (CA_STATUS_OK != res)
576                 {
577                     OIC_LOG(ERROR, TAG, "send has failed");
578                     return res;
579                 }
580             }
581             else if (CA_MSG_CONFIRM == pdu->hdr->type)
582             {
583                 res = CASendErrorMessage(pdu, blockWiseStatus,
584                                          CA_REQUEST_ENTITY_TOO_LARGE,
585                                          blockID);
586                 if (CA_STATUS_OK != res)
587                 {
588                     OIC_LOG(ERROR, TAG, "send has failed");
589                     return res;
590                 }
591             }
592             break;
593         default:
594             OIC_LOG_V(ERROR, TAG, "no logic [%d]", blockWiseStatus);
595     }
596     return CA_STATUS_OK;
597 }
598
599 CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType,
600                               uint8_t status, const CABlockDataID_t *blockID)
601 {
602     VERIFY_NON_NULL(pdu, TAG, "pdu");
603     VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
604     VERIFY_NON_NULL(blockID, TAG, "blockID");
605
606     CAData_t *data = CAGetDataSetFromBlockDataList(blockID);
607     if (!data)
608     {
609         OIC_LOG(ERROR, TAG, "CAData is unavailable");
610         return CA_STATUS_FAILED;
611     }
612
613     if (CA_MSG_CONFIRM == msgType)
614     {
615         OIC_LOG(DEBUG, TAG, "need new msgID");
616         if (data->requestInfo)
617         {
618             data->requestInfo->info.messageId = 0;
619         }
620
621         if (data->responseInfo)
622         {
623             data->responseInfo->info.messageId = 0;
624         }
625     }
626     else if (CA_MSG_ACKNOWLEDGE == msgType)
627     {
628         if (data->responseInfo)
629         {
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)
634             {
635                 data->responseInfo->result = CA_CHANGED;
636             }
637             else if (CA_OPTION1_NO_ACK_BLOCK == status)
638             {
639                 data->responseInfo->result = CA_CONTINUE;
640             }
641         }
642     }
643
644     // add data to send thread
645     CAResult_t res = CAAddSendThreadQueue(data, blockID);
646     if (CA_STATUS_OK != res)
647     {
648         OIC_LOG(ERROR, TAG, "add has failed");
649     }
650
651     return res;
652 }
653
654 CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status,
655                               CAResponseResult_t responseResult,
656                               const CABlockDataID_t *blockID)
657 {
658     VERIFY_NON_NULL(pdu, TAG, "pdu");
659     VERIFY_NON_NULL(pdu->hdr, TAG, "pdu->hdr");
660     VERIFY_NON_NULL(blockID, TAG, "blockID");
661
662     // create error responseInfo
663     CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockID);
664     if (!data)
665     {
666         OIC_LOG(ERROR, TAG, "data is unavailable");
667         return CA_STATUS_FAILED;
668     }
669
670     CAData_t *cloneData = NULL;
671     if (data->sentData && data->sentData->responseInfo)
672     {
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);
677         if (!cloneData)
678         {
679             OIC_LOG(ERROR, TAG, "clone has failed");
680             return CA_MEMORY_ALLOC_FAILED;
681         }
682         OIC_LOG(DEBUG, TAG, "set ACK message");
683     }
684     else if (data->sentData)
685     {
686         cloneData = CACreateNewDataSet(pdu, data->sentData->remoteEndpoint);
687         if(!cloneData)
688         {
689             OIC_LOG(ERROR, TAG, PCF("CACreateNewDataSet failed"));
690             return CA_MEMORY_ALLOC_FAILED;
691         }
692
693         cloneData->responseInfo->info.type = CA_MSG_CONFIRM;
694         cloneData->responseInfo->result = responseResult;
695         OIC_LOG(DEBUG, TAG, "set CON message");
696     }
697     else
698     {
699         OIC_LOG(ERROR, TAG, "data has no sent-data");
700         return CA_MEMORY_ALLOC_FAILED;
701     }
702
703     // add data to send thread
704     if (g_context.sendThreadFunc)
705     {
706         ca_mutex_lock(g_context.blockDataSenderMutex);
707         g_context.sendThreadFunc(cloneData);
708         ca_mutex_unlock(g_context.blockDataSenderMutex);
709     }
710     else
711     {
712         CADestroyDataSet(cloneData);
713     }
714
715     // if error code is 4.08, remove the stored payload and initialize block number
716     if (CA_BLOCK_INCOMPLETE == status)
717     {
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;
724     }
725
726     return CA_STATUS_OK;
727 }
728
729 CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID,
730                               const CAData_t *receivedData)
731 {
732     VERIFY_NON_NULL(blockID, TAG, "blockID");
733     VERIFY_NON_NULL(receivedData, TAG, "receivedData");
734
735     // total block data have to notify to Application
736     CAData_t *cloneData = CACloneCAData(receivedData);
737     if (!cloneData)
738     {
739         OIC_LOG(ERROR, TAG, "clone has failed");
740         return CA_MEMORY_ALLOC_FAILED;
741     }
742
743     // update payload
744     size_t fullPayloadLen = 0;
745     CAPayload_t fullPayload = CAGetPayloadFromBlockDataList(blockID,
746                                                             &fullPayloadLen);
747     if (fullPayload)
748     {
749         CAResult_t res = CAUpdatePayloadToCAData(cloneData, fullPayload, fullPayloadLen);
750         if (CA_STATUS_OK != res)
751         {
752             OIC_LOG(ERROR, TAG, "update has failed");
753             CADestroyDataSet(cloneData);
754             return res;
755         }
756     }
757
758     if (g_context.receivedThreadFunc)
759     {
760         g_context.receivedThreadFunc(cloneData);
761     }
762     else
763     {
764         CADestroyDataSet(cloneData);
765     }
766
767     return CA_STATUS_OK;
768 }
769
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,
773                                  size_t dataLen)
774 {
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");
780
781     OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
782
783     CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
784             (CAToken_t)pdu->hdr->token,
785             pdu->hdr->token_length,
786             endpoint->port);
787
788     if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
789     {
790         OIC_LOG(ERROR, TAG, "blockId is null");
791         CADestroyBlockID(blockDataID);
792         return CA_STATUS_FAILED;
793     }
794
795     // BlockData data is created if it not existed
796     if (!CAIsBlockDataInList(blockDataID))
797     {
798         OIC_LOG(DEBUG, TAG, "no message in list");
799
800         CAData_t *data = CACreateNewDataSet(pdu, endpoint);
801         if (!data)
802         {
803             OIC_LOG(ERROR, TAG, "data is null");
804             CADestroyBlockID(blockDataID);
805             return CA_STATUS_FAILED;
806         }
807
808         CABlockData_t *currData = CACreateNewBlockData(data);
809         if (!currData)
810         {
811             OIC_LOG(ERROR, TAG, "currData is null");
812             CADestroyDataSet(data);
813             CADestroyBlockID(blockDataID);
814             return CA_STATUS_FAILED;
815         }
816         CADestroyDataSet(data);
817     }
818
819     // update BLOCK OPTION1 type
820     CAResult_t res = CAUpdateBlockOptionType(blockDataID,
821                                              COAP_OPTION_BLOCK1);
822     if (CA_STATUS_OK != res)
823     {
824         OIC_LOG(ERROR, TAG, "update has failed");
825         CARemoveBlockDataFromList(blockDataID);
826         CADestroyBlockID(blockDataID);
827         return res;
828     }
829
830     CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
831     if (!data)
832     {
833         OIC_LOG(ERROR, TAG, "getting has failed");
834         CADestroyBlockID(blockDataID);
835         return CA_STATUS_FAILED;
836     }
837
838     uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
839     // received type from remote device
840     if (CA_MSG_ACKNOWLEDGE == pdu->hdr->type)
841     {
842         uint32_t code = CA_RESPONSE_CODE(pdu->hdr->code);
843         if (0 == block.m &&
844                 (CA_REQUEST_ENTITY_INCOMPLETE != code && CA_REQUEST_ENTITY_TOO_LARGE != code))
845         {
846             OIC_LOG(INFO, TAG, "Data has sent");
847             // initialize block number for response message
848             data->block1.num = 0;
849             CADestroyBlockID(blockDataID);
850             return CA_STATUS_OK;
851         }
852
853         blockWiseStatus = CA_OPTION1_ACK;
854         res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus);
855         if (CA_STATUS_OK != res)
856         {
857             OIC_LOG(ERROR, TAG, "update has failed");
858             CADestroyBlockID(blockDataID);
859             return res;
860         }
861
862         res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
863         if (CA_STATUS_OK != res)
864         {
865             OIC_LOG(ERROR, TAG, "update has failed");
866             CARemoveBlockDataFromList(blockDataID);
867             CADestroyBlockID(blockDataID);
868             return res;
869         }
870     }
871     else // CON or NON message
872     {
873         OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
874
875         // check the size option
876         bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
877                                                                       COAP_OPTION_SIZE1,
878                                                                       &(data->payloadLength));
879
880         // check if received payload is exact
881         if (CA_MSG_CONFIRM == pdu->hdr->type)
882         {
883             blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
884                                                     COAP_OPTION_BLOCK1, dataLen);
885         }
886
887         if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
888         {
889             // store the received payload and merge
890             res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
891                                       isSizeOption, COAP_OPTION_BLOCK1);
892             if (CA_STATUS_OK != res)
893             {
894                 OIC_LOG(ERROR, TAG, "update has failed");
895                 CARemoveBlockDataFromList(blockDataID);
896                 CADestroyBlockID(blockDataID);
897                 return res;
898             }
899
900             res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK1, blockWiseStatus);
901             if (CA_STATUS_OK != res)
902             {
903                 OIC_LOG(ERROR, TAG, "update has failed");
904                 CARemoveBlockDataFromList(blockDataID);
905                 CADestroyBlockID(blockDataID);
906                 return res;
907             }
908
909             // update block data
910             res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK1);
911             if (CA_STATUS_OK != res)
912             {
913                 OIC_LOG(ERROR, TAG, "update has failed");
914                 CARemoveBlockDataFromList(blockDataID);
915                 CADestroyBlockID(blockDataID);
916                 return res;
917             }
918         }
919
920         // check the blcok-wise transfer status for next step
921         if (CA_BLOCK_UNKNOWN == blockWiseStatus || CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
922         {
923             if (0 == block.m) // Last block is received
924             {
925                 OIC_LOG(DEBUG, TAG, "M bit is 0");
926                 blockWiseStatus = CA_OPTION1_NO_ACK_LAST_BLOCK;
927             }
928             else
929             {
930                 OIC_LOG(DEBUG, TAG, "M bit is 1");
931                 blockWiseStatus = CA_OPTION1_NO_ACK_BLOCK;
932             }
933         }
934     }
935
936     res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
937     if (CA_STATUS_OK != res)
938     {
939         OIC_LOG(ERROR, TAG, "setting has failed");
940         CARemoveBlockDataFromList(blockDataID);
941     }
942
943     CADestroyBlockID(blockDataID);
944     return res;
945 }
946
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,
950                                  size_t dataLen)
951 {
952     OIC_LOG(DEBUG, TAG, "CASetNextBlockOption2");
953     OIC_LOG_V(INFO, TAG, "num:%d, M:%d, sze:%d", block.num, block.m, block.szx);
954
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");
959
960     CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
961             (CAToken_t)pdu->hdr->token,
962             pdu->hdr->token_length,
963             endpoint->port);
964
965     if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
966     {
967         OIC_LOG(ERROR, TAG, "blockId is null");
968         CADestroyBlockID(blockDataID);
969         return CA_STATUS_FAILED;
970     }
971
972     // BlockData data is created if it not existed
973     if (!CAIsBlockDataInList(blockDataID))
974     {
975         OIC_LOG(DEBUG, TAG, "no msg in list.");
976
977         CAData_t *data = CACreateNewDataSet(pdu, endpoint);
978         if (!data)
979         {
980             OIC_LOG(ERROR, TAG, "data is null");
981             CADestroyBlockID(blockDataID);
982             return CA_STATUS_FAILED;
983         }
984
985         CABlockData_t *currData = CACreateNewBlockData(data);
986         if (!currData)
987         {
988             OIC_LOG(ERROR, TAG, "data is null");
989             CADestroyDataSet(data);
990             CADestroyBlockID(blockDataID);
991             return CA_STATUS_FAILED;
992         }
993         CADestroyDataSet(data);
994     }
995
996     // set Block Option Type
997     CAResult_t res = CAUpdateBlockOptionType(blockDataID,
998                                              COAP_OPTION_BLOCK2);
999     if (CA_STATUS_OK != res)
1000     {
1001         OIC_LOG(ERROR, TAG, "update has failed");
1002         CARemoveBlockDataFromList(blockDataID);
1003         CADestroyBlockID(blockDataID);
1004         return res;
1005     }
1006
1007     CABlockData_t *data = CAGetBlockDataFromBlockDataList(blockDataID);
1008     if (!data)
1009     {
1010         OIC_LOG(ERROR, TAG, "getting has failed");
1011         CARemoveBlockDataFromList(blockDataID);
1012         CADestroyBlockID(blockDataID);
1013         return CA_STATUS_FAILED;
1014     }
1015
1016     uint8_t blockWiseStatus = CA_BLOCK_UNKNOWN;
1017     if (0 == block.num && CA_GET == pdu->hdr->code && 0 == block.m)
1018     {
1019         OIC_LOG(INFO, TAG, "first block number");
1020
1021         res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1022         if (CA_STATUS_OK != res)
1023         {
1024             OIC_LOG(ERROR, TAG, "update has failed");
1025             CARemoveBlockDataFromList(blockDataID);
1026             CADestroyBlockID(blockDataID);
1027             return res;
1028         }
1029
1030         // first block data have to notify to Application
1031         res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1032         if (CA_STATUS_OK != res)
1033         {
1034             OIC_LOG(ERROR, TAG, "update has failed");
1035             CARemoveBlockDataFromList(blockDataID);
1036             CADestroyBlockID(blockDataID);
1037             return res;
1038         }
1039         blockWiseStatus = CA_OPTION2_FIRST_BLOCK;
1040     }
1041     else
1042     {
1043         // received type from remote device
1044         if (CA_MSG_ACKNOWLEDGE == pdu->hdr->type ||
1045                 (CA_MSG_NONCONFIRM == pdu->hdr->type && NULL != receivedData->responseInfo))
1046         {
1047             OIC_LOG(DEBUG, TAG, "received ACK or NON");
1048
1049             // check the size option
1050             bool isSizeOption = CAIsPayloadLengthInPduWithBlockSizeOption(pdu,
1051                                                                           COAP_OPTION_SIZE2,
1052                                                                           &(data->payloadLength));
1053
1054             // check if received payload is exact
1055             if (CA_MSG_ACKNOWLEDGE == pdu->hdr->type)
1056             {
1057                 blockWiseStatus = CACheckBlockErrorType(data, &block, receivedData,
1058                                                         COAP_OPTION_BLOCK2, dataLen);
1059             }
1060
1061             if (CA_BLOCK_RECEIVED_ALREADY != blockWiseStatus)
1062             {
1063                 // store the received payload and merge
1064                 res = CAUpdatePayloadData(data, receivedData, blockWiseStatus,
1065                                           isSizeOption, COAP_OPTION_BLOCK2);
1066                 if (CA_STATUS_OK != res)
1067                 {
1068                     OIC_LOG(ERROR, TAG, "update has failed");
1069                     CARemoveBlockDataFromList(blockDataID);
1070                     CADestroyBlockID(blockDataID);
1071                     return res;
1072                 }
1073             }
1074
1075             if (0 == block.m && CA_BLOCK_UNKNOWN == blockWiseStatus) // Last block is received
1076             {
1077                 OIC_LOG(DEBUG, TAG, "M bit is 0");
1078                 blockWiseStatus = CA_OPTION2_LAST_BLOCK;
1079             }
1080             else
1081             {
1082                 if (CA_BLOCK_UNKNOWN == blockWiseStatus ||
1083                         CA_BLOCK_RECEIVED_ALREADY == blockWiseStatus)
1084                 {
1085                     OIC_LOG(DEBUG, TAG, "M bit is 1");
1086
1087                     if (CA_MSG_ACKNOWLEDGE == pdu->hdr->type)
1088                     {
1089                         blockWiseStatus = CA_OPTION2_ACK;
1090                     }
1091                     else
1092                     {
1093                         blockWiseStatus = CA_OPTION2_NON;
1094                     }
1095                 }
1096
1097                 res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2,
1098                                                blockWiseStatus);
1099                 if (CA_STATUS_OK != res)
1100                 {
1101                     OIC_LOG(ERROR, TAG, "update has failed");
1102                     CARemoveBlockDataFromList(blockDataID);
1103                     CADestroyBlockID(blockDataID);
1104                     return res;
1105                 }
1106
1107                 res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1108                 if (CA_STATUS_OK != res)
1109                 {
1110                     OIC_LOG(ERROR, TAG, "update has failed");
1111                     CARemoveBlockDataFromList(blockDataID);
1112                     CADestroyBlockID(blockDataID);
1113                     return res;
1114                 }
1115             }
1116         }
1117         else // CON message and so on.
1118         {
1119             OIC_LOG_V(INFO, TAG, "num:%d, M:%d", block.num, block.m);
1120
1121             blockWiseStatus = CA_OPTION2_CON;
1122
1123             res = CAUpdateBlockOptionItems(data, pdu, &block, COAP_OPTION_BLOCK2, blockWiseStatus);
1124             if (CA_STATUS_OK != res)
1125             {
1126                 OIC_LOG(ERROR, TAG, "update has failed");
1127                 CARemoveBlockDataFromList(blockDataID);
1128                 CADestroyBlockID(blockDataID);
1129                 return res;
1130             }
1131
1132             res = CAUpdateBlockData(data, block, COAP_OPTION_BLOCK2);
1133             if (CA_STATUS_OK != res)
1134             {
1135                 OIC_LOG(ERROR, TAG, "update has failed");
1136                 CARemoveBlockDataFromList(blockDataID);
1137                 CADestroyBlockID(blockDataID);
1138                 return res;
1139             }
1140         }
1141     }
1142
1143     res = CAProcessNextStep(pdu, receivedData, blockWiseStatus, blockDataID);
1144     if (CA_STATUS_OK != res)
1145     {
1146         OIC_LOG(ERROR, TAG, "setting has failed");
1147         CARemoveBlockDataFromList(blockDataID);
1148         CADestroyBlockID(blockDataID);
1149         return res;
1150     }
1151
1152     CADestroyBlockID(blockDataID);
1153     return CA_STATUS_OK;
1154 }
1155
1156 CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu,
1157                                     coap_block_t *block, uint16_t blockType,
1158                                     uint32_t status)
1159 {
1160     VERIFY_NON_NULL(currData, TAG, "currData");
1161     VERIFY_NON_NULL(pdu, TAG, "pdu");
1162     VERIFY_NON_NULL(block, TAG, "block");
1163
1164     // update block data
1165     CAResult_t res = CA_STATUS_OK;
1166     uint32_t code = CA_RESPONSE_CODE(pdu->hdr->code);
1167
1168     if (CA_REQUEST_ENTITY_INCOMPLETE == code || CA_REQUEST_ENTITY_TOO_LARGE == code)
1169     {
1170         // response error code of the received block message
1171         res = CAHandleBlockErrorResponse(block, blockType, code);
1172         if (CA_STATUS_OK != res)
1173         {
1174             OIC_LOG(ERROR, TAG, "error handle has failed");
1175             return res;
1176         }
1177     }
1178     else
1179     {
1180         // update block option items
1181         switch (status)
1182         {
1183             case CA_OPTION1_ACK:
1184                 if (currData->block1.num > block->num)
1185                 {
1186                     OIC_LOG(ERROR, TAG, "received incorrect block num");
1187                     return CA_STATUS_FAILED;
1188                 }
1189                 block->num++;
1190                 break;
1191             case CA_OPTION2_NON:
1192                 block->num++;
1193                 block->m = 0;
1194                 break;
1195             case CA_OPTION2_CON:
1196                 block->m = 0;
1197                 break;
1198             case CA_OPTION2_ACK:
1199                 if (currData->block2.num > block->num)
1200                 {
1201                     OIC_LOG(ERROR, TAG, "received incorrect block num");
1202                     return CA_STATUS_FAILED;
1203                 }
1204                 block->num++;
1205                 block->m = 0;
1206                 break;
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)
1211                 {
1212                     block->num++;
1213                     block->m = 0;
1214                     block->szx = currData->block2.szx;
1215                 }
1216                 else
1217                 {
1218                     block->szx = currData->block1.szx;
1219                 }
1220                 break;
1221             default:
1222                 OIC_LOG_V(ERROR, TAG, "no logic [%d]", status);
1223         }
1224
1225         if (CA_BLOCK_INCOMPLETE != status && CA_BLOCK_TOO_LARGE != status)
1226         {
1227             // negotiate block size
1228             res = CANegotiateBlockSize(currData, block, pdu->hdr->type, blockType);
1229             if (CA_STATUS_OK != res)
1230             {
1231                 OIC_LOG(ERROR, TAG, "negotiation has failed");
1232                 return res;
1233             }
1234         }
1235     }
1236     return res;
1237 }
1238
1239 CAResult_t CAGetMoreBitFromBlock(size_t payloadLen, coap_block_t *block)
1240 {
1241     VERIFY_NON_NULL(block, TAG, "block");
1242
1243     if ((size_t)((block->num + 1) << (block->szx + BLOCK_NUMBER_IDX))
1244         < payloadLen)
1245     {
1246         OIC_LOG(DEBUG, TAG, "Set the M-bit(1)");
1247         block->m = 1;
1248     }
1249     else
1250     {
1251         OIC_LOG(DEBUG, TAG, "Set the M-bit(0)");
1252         block->m = 0;
1253     }
1254
1255     return CA_STATUS_OK;
1256 }
1257
1258 CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block,
1259                                 CAMessageType_t msgType, uint16_t blockType)
1260 {
1261     OIC_LOG(DEBUG, TAG, "IN-NegotiateBlockSize");
1262
1263     VERIFY_NON_NULL(currData, TAG, "currData");
1264     VERIFY_NON_NULL(block, TAG, "block");
1265
1266     // #1. check the block option type
1267     if (COAP_OPTION_BLOCK2 == blockType)
1268     {
1269         // #2. check the message type
1270         if (CA_MSG_ACKNOWLEDGE == msgType)
1271         {
1272             if (block->szx > currData->block2.szx)
1273             {
1274                 OIC_LOG(DEBUG, TAG, "sze is big");
1275
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);
1283             }
1284         }
1285         else
1286         {
1287             if (block->szx > currData->block2.szx)
1288             {
1289                 OIC_LOG(DEBUG, TAG, "sze is big");
1290                 block->szx = currData->block2.szx;
1291             }
1292         }
1293     }
1294     else if (COAP_OPTION_BLOCK1 == blockType)
1295     {
1296         if (CA_MSG_ACKNOWLEDGE == msgType)
1297         {
1298             if (block->szx < currData->block1.szx)
1299             {
1300                 OIC_LOG(DEBUG, TAG, "sze is small");
1301
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);
1306             }
1307         }
1308         else
1309         {
1310             if (block->szx > currData->block1.szx)
1311             {
1312                 OIC_LOG(DEBUG, TAG, "sze is big");
1313                 block->szx = currData->block1.szx;
1314             }
1315         }
1316     }
1317     else
1318     {
1319         OIC_LOG(DEBUG, TAG, "Invalid block option");
1320         return CA_STATUS_FAILED;
1321     }
1322
1323     OIC_LOG(DEBUG, TAG, "OUT-NegotiateBlockSize");
1324
1325     return CA_STATUS_OK;
1326 }
1327
1328 CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block,
1329                              uint16_t blockType)
1330 {
1331     VERIFY_NON_NULL(currData, TAG, "currData");
1332
1333     // check if block size is bigger than CABlockSize_t
1334     if (block.szx > CA_BLOCK_SIZE_1024_BYTE)
1335     {
1336         OIC_LOG(DEBUG, TAG, "invalid block szx");
1337         return CA_STATUS_FAILED;
1338     }
1339
1340     // update block option
1341     if (COAP_OPTION_BLOCK2 == blockType)
1342     {
1343         currData->block2 = block;
1344     }
1345     else
1346     {
1347         currData->block1 = block;
1348     }
1349
1350     OIC_LOG(DEBUG, TAG, "data has updated");
1351     return CA_STATUS_OK;
1352 }
1353
1354 CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID)
1355 {
1356     VERIFY_NON_NULL(pdu, TAG, "pdu");
1357     VERIFY_NON_NULL(blockID, TAG, "blockID");
1358
1359     // if CON message is sent, update messageId in block-wise transfer list
1360     if (CA_MSG_CONFIRM == pdu->hdr->type)
1361     {
1362         CAData_t * cadata = CAGetDataSetFromBlockDataList(blockID);
1363         if (!cadata)
1364         {
1365             OIC_LOG(ERROR, TAG, "CAData is unavailable");
1366             return CA_STATUS_FAILED;
1367         }
1368
1369         if (cadata->requestInfo)
1370         {
1371             cadata->requestInfo->info.messageId = pdu->hdr->id;
1372         }
1373     }
1374
1375     return CA_STATUS_OK;
1376 }
1377
1378 CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t info,
1379                             const CAEndpoint_t *endpoint)
1380 {
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");
1386
1387     size_t dataLength = 0;
1388     if (info.payload)
1389     {
1390         dataLength = info.payloadSize;
1391         OIC_LOG_V(DEBUG, TAG, "dataLength - %d", dataLength);
1392     }
1393
1394     OIC_LOG_V(DEBUG, TAG, "previous payload - %s", (*pdu)->data);
1395
1396     uint32_t code = CA_RESPONSE_CODE((*pdu)->hdr->code);
1397     if (CA_REQUEST_ENTITY_INCOMPLETE == code)
1398     {
1399         OIC_LOG(INFO, TAG, "don't use option");
1400         return CA_STATUS_OK;
1401     }
1402
1403     CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
1404             (CAToken_t)(*pdu)->hdr->token,
1405             (*pdu)->hdr->token_length,
1406             endpoint->port);
1407
1408     if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
1409     {
1410         OIC_LOG(ERROR, TAG, "blockId is null");
1411         CADestroyBlockID(blockDataID);
1412         return CA_STATUS_FAILED;
1413     }
1414
1415     uint8_t blockType = CAGetBlockOptionType(blockDataID);
1416     if (COAP_OPTION_BLOCK2 == blockType)
1417     {
1418         CAResult_t res = CAAddBlockOption2(pdu, info, dataLength,
1419                                            blockDataID);
1420         if (CA_STATUS_OK != res)
1421         {
1422             OIC_LOG(ERROR, TAG, "add has failed");
1423             CADestroyBlockID(blockDataID);
1424             return res;
1425         }
1426     }
1427     else if (COAP_OPTION_BLOCK1 == blockType)
1428     {
1429         CAResult_t res = CAAddBlockOption1(pdu, info, dataLength,
1430                                            blockDataID);
1431         if (CA_STATUS_OK != res)
1432         {
1433             OIC_LOG(ERROR, TAG, "add has failed");
1434             CADestroyBlockID(blockDataID);
1435             return res;
1436         }
1437     }
1438     else
1439     {
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))
1443         {
1444             OIC_LOG(INFO, TAG, "it have to use block");
1445         }
1446         else
1447         {
1448             OIC_LOG(INFO, TAG, "not Blockwise Transfer");
1449             CADestroyBlockID(blockDataID);
1450             return CA_STATUS_OK;
1451         }
1452     }
1453
1454     CAResult_t res = CAUpdateMessageId(*pdu, blockDataID);
1455     if (CA_STATUS_OK != res)
1456     {
1457         OIC_LOG(ERROR, TAG, "fail to update CON message id ");
1458         CADestroyBlockID(blockDataID);
1459         return res;
1460     }
1461
1462     CADestroyBlockID(blockDataID);
1463     OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption");
1464     return CA_STATUS_OK;
1465 }
1466
1467 CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t info, size_t dataLength,
1468                              const CABlockDataID_t *blockID)
1469 {
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");
1475
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)
1482     {
1483         OIC_LOG(ERROR, TAG, "getting has failed");
1484         return CA_STATUS_FAILED;
1485     }
1486
1487     CALogBlockInfo(block2);
1488
1489     uint8_t code = 0;
1490     if (CA_MSG_ACKNOWLEDGE == (*pdu)->hdr->type ||
1491             (CA_MSG_NONCONFIRM == (*pdu)->hdr->type && CA_GET != (*pdu)->hdr->code))
1492     {
1493         int32_t res = coap_write_block_opt(block2, COAP_OPTION_BLOCK2, *pdu, dataLength);
1494         switch (res)
1495         {
1496             case -2: /* illegal block */
1497                 code = COAP_RESPONSE_CODE(CA_BAD_REQ);
1498                 OIC_LOG(ERROR, TAG, "write block option : -2");
1499                 goto error;
1500             case -1: /* should really not happen */
1501                 OIC_LOG(ERROR, TAG, "write block option : -1");
1502                 break;
1503             case -3: /* cannot handle request */
1504                 code = COAP_RESPONSE_CODE(CA_INTERNAL_SERVER_ERROR);
1505                 OIC_LOG(ERROR, TAG, "write block option : -3");
1506                 goto error;
1507             default:
1508                 OIC_LOG(INFO, TAG, "success write block option");
1509         }
1510         CALogBlockInfo(block2);
1511
1512         if (block1->num)
1513         {
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)
1517             {
1518                 OIC_LOG(ERROR, TAG, "add has failed");
1519                 CARemoveBlockDataFromList(blockID);
1520                 return res;
1521             }
1522             // initialize block number
1523             block1->num = 0;
1524         }
1525
1526         // if block number is 0, add size2 option
1527         if (0 == block2->num)
1528         {
1529             res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE2, dataLength);
1530             if (CA_STATUS_OK != res)
1531             {
1532                 OIC_LOG(ERROR, TAG, "add has failed");
1533                 CARemoveBlockDataFromList(blockID);
1534                 return res;
1535             }
1536         }
1537
1538         if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info.payload,
1539                             block2->num, block2->szx))
1540         {
1541             OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1542             return CA_STATUS_FAILED;
1543         }
1544
1545         if (!block2->m)
1546         {
1547             // if sent message is last response block message, remove data
1548             CARemoveBlockDataFromList(blockID);
1549         }
1550         else
1551         {
1552             if (CA_MSG_NONCONFIRM == (*pdu)->hdr->type)
1553             {
1554                 OIC_LOG(DEBUG, TAG, "NON, send next block..");
1555                 // update block data
1556                 block2->num++;
1557                 CAResult_t res = CAProcessNextStep(*pdu, NULL,
1558                                                    CA_SENT_PREVIOUS_NON_MSG,
1559                                                    blockID);
1560                 if (CA_STATUS_OK != res)
1561                 {
1562                     OIC_LOG(ERROR, TAG, "failed to process next step");
1563                     CARemoveBlockDataFromList(blockID);
1564                     return res;
1565                 }
1566             }
1567         }
1568     }
1569     else
1570     {
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)
1574         {
1575             OIC_LOG(ERROR, TAG, "add has failed");
1576             CARemoveBlockDataFromList(blockID);
1577             return res;
1578         }
1579     }
1580
1581     return CA_STATUS_OK;
1582
1583 error:
1584     OIC_LOG_V(ERROR, TAG, "error : %d", code);
1585
1586     char* phrase = coap_response_phrase(code);
1587     if(phrase)
1588     {
1589         coap_add_data(*pdu, strlen(phrase),
1590                       (unsigned char *) phrase);
1591     }
1592     return CA_STATUS_FAILED;
1593 }
1594
1595 CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t info, size_t dataLength,
1596                              const CABlockDataID_t *blockID)
1597 {
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");
1603
1604     // get set block data from CABlock list-set.
1605     coap_block_t *block1 = CAGetBlockOption(blockID, COAP_OPTION_BLOCK1);
1606     if (!block1)
1607     {
1608         OIC_LOG(ERROR, TAG, "getting has failed");
1609         return CA_STATUS_FAILED;
1610     }
1611
1612     CALogBlockInfo(block1);
1613
1614     if (CA_MSG_ACKNOWLEDGE == (*pdu)->hdr->type)
1615     {
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)
1619         {
1620             OIC_LOG(ERROR, TAG, "add has failed");
1621             CARemoveBlockDataFromList(blockID);
1622             return res;
1623         }
1624
1625         // reset block-list after write block
1626         if (0 == block1->m)
1627         {
1628             // remove data from list
1629             CAResult_t res = CARemoveBlockDataFromList(blockID);
1630             if (CA_STATUS_OK != res)
1631             {
1632                 OIC_LOG(ERROR, TAG, "remove has failed");
1633                 return res;
1634             }
1635         }
1636     }
1637     else
1638     {
1639         CAGetMoreBitFromBlock(dataLength, block1);
1640         CAResult_t res = CAAddBlockOptionImpl(*pdu, block1, COAP_OPTION_BLOCK1);
1641         if (CA_STATUS_OK != res)
1642         {
1643             OIC_LOG(ERROR, TAG, "add has failed");
1644             CARemoveBlockDataFromList(blockID);
1645             return res;
1646         }
1647         CALogBlockInfo(block1);
1648
1649         // if block number is 0, add size1 option
1650         if (0 == block1->num)
1651         {
1652             res = CAAddBlockSizeOption(*pdu, COAP_OPTION_SIZE1, dataLength);
1653             if (CA_STATUS_OK != res)
1654             {
1655                 OIC_LOG(ERROR, TAG, "add has failed");
1656                 CARemoveBlockDataFromList(blockID);
1657                 return res;
1658             }
1659         }
1660
1661         if (!coap_add_block(*pdu, dataLength, (const unsigned char *) info.payload,
1662                             block1->num, block1->szx))
1663         {
1664             OIC_LOG(ERROR, TAG, "Data length is smaller than the start index");
1665             return CA_STATUS_FAILED;
1666         }
1667
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)
1670         {
1671             if (block1->m)
1672             {
1673                 OIC_LOG(DEBUG, TAG, "NON, send next block..");
1674                 // update block data
1675                 block1->num++;
1676                 CAResult_t res = CAProcessNextStep(*pdu, NULL,
1677                                                    CA_SENT_PREVIOUS_NON_MSG,
1678                                                    blockID);
1679                 if (CA_STATUS_OK != res)
1680                 {
1681                     OIC_LOG(ERROR, TAG, "failed to process next step");
1682                     CARemoveBlockDataFromList(blockID);
1683                     return res;
1684                 }
1685             }
1686             else
1687             {
1688                 CARemoveBlockDataFromList(blockID);
1689             }
1690         }
1691     }
1692
1693     OIC_LOG(DEBUG, TAG, "OUT-AddBlockOption1");
1694
1695     return CA_STATUS_OK;
1696 }
1697
1698 CAResult_t CAAddBlockOptionImpl(coap_pdu_t *pdu, coap_block_t *block, uint8_t blockType)
1699 {
1700     OIC_LOG(DEBUG, TAG, "IN-AddBlockOptionImpl");
1701     VERIFY_NON_NULL(pdu, TAG, "pdu");
1702     VERIFY_NON_NULL(block, TAG, "block");
1703
1704     coap_option *option = (coap_option *) OICMalloc(sizeof(coap_option));
1705     if (!option)
1706     {
1707         OIC_LOG(ERROR, TAG, "out of memory");
1708         return CA_MEMORY_ALLOC_FAILED;
1709     }
1710
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))
1717     {
1718         OIC_LOG(ERROR, TAG, "coap_add_option has failed");
1719         OICFree(option);
1720         return CA_STATUS_FAILED;
1721     }
1722
1723     OICFree(option);
1724
1725     OIC_LOG(DEBUG, TAG, "OUT-AddBlockOptionImpl");
1726     return CA_STATUS_OK;
1727 }
1728
1729 CAResult_t CAAddBlockSizeOption(coap_pdu_t *pdu, uint16_t sizeType, size_t dataLength)
1730 {
1731     OIC_LOG(DEBUG, TAG, "IN-CAAddBlockSizeOption");
1732     VERIFY_NON_NULL(pdu, TAG, "pdu");
1733
1734     if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1735     {
1736         OIC_LOG(ERROR, TAG, "unknown option type");
1737         return CA_STATUS_FAILED;
1738     }
1739
1740     unsigned char value[BLOCKWISE_OPTION_BUFFER] = { 0 };
1741     unsigned int optionLength = coap_encode_var_bytes(value, dataLength);
1742
1743     if (!coap_add_option(pdu, sizeType, optionLength, value))
1744     {
1745         OIC_LOG(ERROR, TAG, "failed to add size option");
1746         return CA_STATUS_FAILED;
1747     }
1748
1749     OIC_LOG(DEBUG, TAG, "OUT-CAAddBlockSizeOption");
1750
1751     return CA_STATUS_OK;
1752 }
1753
1754 // TODO make pdu const after libcoap is updated to support that.
1755 bool CAIsPayloadLengthInPduWithBlockSizeOption(coap_pdu_t *pdu,
1756                                                uint16_t sizeType,
1757                                                size_t *totalPayloadLen)
1758 {
1759     OIC_LOG(DEBUG, TAG, "IN-CAIsPayloadLengthInPduWithBlockSizeOption");
1760     VERIFY_NON_NULL(pdu, TAG, "pdu");
1761     VERIFY_NON_NULL(totalPayloadLen, TAG, "totalPayloadLen");
1762
1763     if (sizeType != COAP_OPTION_SIZE1 && sizeType != COAP_OPTION_SIZE2)
1764     {
1765         OIC_LOG(ERROR, TAG, "unknown option type");
1766         return CA_STATUS_FAILED;
1767     }
1768
1769     coap_opt_iterator_t opt_iter;
1770     coap_opt_t *option = coap_check_option(pdu, sizeType, &opt_iter);
1771     if (option)
1772     {
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));
1776
1777         OIC_LOG_V(DEBUG, TAG, "the total payload length to be received is [%d]bytes",
1778                   *totalPayloadLen);
1779
1780         return true;
1781     }
1782
1783     OIC_LOG(DEBUG, TAG, "OUT-CAIsPayloadLengthInPduWithBlockSizeOption");
1784
1785     return false;
1786 }
1787
1788 uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlock,
1789                               const CAData_t *receivedData, uint16_t blockType,
1790                               size_t dataLen)
1791 {
1792     OIC_LOG(DEBUG, TAG, "IN-CheckBlockError");
1793
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");
1797
1798     // #1. check the received payload length
1799     size_t blockPayloadLen = 0;
1800     CAGetPayloadInfo(receivedData, &blockPayloadLen);
1801
1802     // #2. check if the block sequence is right
1803     if (COAP_OPTION_BLOCK1 == blockType)
1804     {
1805         size_t prePayloadLen = currData->receivedPayloadLen;
1806         if (prePayloadLen != (size_t)BLOCK_SIZE(receivedBlock->szx)
1807             * receivedBlock->num)
1808         {
1809             if (receivedBlock->num > currData->block1.num + 1)
1810             {
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;
1815             }
1816             return CA_BLOCK_RECEIVED_ALREADY;
1817         }
1818     }
1819     else if (COAP_OPTION_BLOCK2 == blockType)
1820     {
1821         if (receivedBlock->num != currData->block2.num)
1822         {
1823             if (receivedBlock->num > currData->block2.num)
1824             {
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;
1829             }
1830             return CA_BLOCK_RECEIVED_ALREADY;
1831         }
1832     }
1833
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))
1838     {
1839         // 413 Error handling of too large entity
1840         if (COAP_MAX_PDU_SIZE < BLOCK_SIZE(receivedBlock->szx) + optionLen)
1841         {
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");
1845
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--)
1848             {
1849                 if (COAP_MAX_PDU_SIZE >= BLOCK_SIZE(size) + optionLen)
1850                 {
1851                     OIC_LOG_V(ERROR, TAG, "replace sze with %d", size);
1852                     if (COAP_OPTION_BLOCK2 == blockType)
1853                     {
1854                         currData->block2.szx = size;
1855                     }
1856                     else
1857                     {
1858                         currData->block1.szx = size;
1859                     }
1860                     break;
1861                 }
1862             }
1863             return CA_BLOCK_TOO_LARGE;
1864         }
1865         else
1866         {
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;
1871         }
1872     }
1873     else if (0 == receivedBlock->m && 0 != currData->payloadLength)
1874     {
1875         // if the received block is last block, check the total payload length
1876         size_t receivedPayloadLen = currData->receivedPayloadLen;
1877         receivedPayloadLen += blockPayloadLen;
1878
1879         if (receivedPayloadLen != currData->payloadLength)
1880         {
1881             OIC_LOG(ERROR, TAG, "error type 4.08");
1882             OIC_LOG(ERROR, TAG, "total payload length is wrong");
1883             return CA_BLOCK_INCOMPLETE;
1884         }
1885     }
1886
1887     OIC_LOG(DEBUG, TAG, "received all data normally");
1888
1889     OIC_LOG(DEBUG, TAG, "OUT-CheckBlockError");
1890
1891     return CA_BLOCK_UNKNOWN;
1892 }
1893
1894 CAResult_t CAUpdatePayloadData(CABlockData_t *currData, const CAData_t *receivedData,
1895                                uint8_t status, bool isSizeOption, uint16_t blockType)
1896 {
1897     OIC_LOG(DEBUG, TAG, "IN-UpdatePayloadData");
1898
1899     VERIFY_NON_NULL(currData, TAG, "currData");
1900     VERIFY_NON_NULL(receivedData, TAG, "receivedData");
1901
1902     // if error code is 4.08, do not update payload
1903     if (CA_BLOCK_INCOMPLETE == status)
1904     {
1905         OIC_LOG(ERROR, TAG, "no require to update");
1906         return CA_STATUS_OK;
1907     }
1908
1909     size_t blockPayloadLen = 0;
1910     CAPayload_t blockPayload = CAGetPayloadInfo(receivedData, &blockPayloadLen);
1911
1912     if (CA_BLOCK_TOO_LARGE == status)
1913     {
1914         blockPayloadLen = (COAP_OPTION_BLOCK2 == blockType) ?
1915                 BLOCK_SIZE(currData->block2.szx) : BLOCK_SIZE(currData->block1.szx);
1916     }
1917
1918     // memory allocation for the received block payload
1919     size_t prePayloadLen = currData->receivedPayloadLen;
1920     if (blockPayload)
1921     {
1922         if (currData->payloadLength)
1923         {
1924             // in case the block message has the size option
1925             // allocate the memory for the total payload
1926             if (isSizeOption)
1927             {
1928                 CAPayload_t prePayload = currData->payload;
1929
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)
1933                 {
1934                     OIC_LOG(ERROR, TAG, "out of memory");
1935                     return CA_MEMORY_ALLOC_FAILED;
1936                 }
1937                 memcpy(currData->payload, prePayload, prePayloadLen);
1938                 OICFree(prePayload);
1939             }
1940
1941             // update the total payload
1942             memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
1943         }
1944         else
1945         {
1946             OIC_LOG(DEBUG, TAG, "allocate memory for the received block payload");
1947
1948             size_t totalPayloadLen = prePayloadLen + blockPayloadLen;
1949             CAPayload_t newPayload = OICRealloc(currData->payload, totalPayloadLen);
1950             if (NULL == newPayload)
1951             {
1952                 OIC_LOG(ERROR, TAG, "out of memory");
1953                 return CA_MEMORY_ALLOC_FAILED;
1954             }
1955
1956             // update the total payload
1957             memset(newPayload + prePayloadLen, 0, blockPayloadLen);
1958             currData->payload = newPayload;
1959             memcpy(currData->payload + prePayloadLen, blockPayload, blockPayloadLen);
1960         }
1961
1962         // update received payload length
1963         currData->receivedPayloadLen += blockPayloadLen;
1964
1965         OIC_LOG_V(DEBUG, TAG, "updated payload: %s, len: %d", currData->payload,
1966                   currData->receivedPayloadLen);
1967     }
1968
1969     OIC_LOG(DEBUG, TAG, "OUT-UpdatePayloadData");
1970     return CA_STATUS_OK;
1971 }
1972
1973 CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
1974 {
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);
1978
1979     CAInfo_t responseData = { .tokenLength = pdu->hdr->token_length };
1980     responseData.token = (CAToken_t) OICMalloc(responseData.tokenLength);
1981     if (!responseData.token)
1982     {
1983         OIC_LOG(ERROR, TAG, "out of memory");
1984         return NULL;
1985     }
1986     memcpy(responseData.token, pdu->hdr->token, responseData.tokenLength);
1987
1988     CAResponseInfo_t* responseInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
1989     if (!responseInfo)
1990     {
1991         OIC_LOG(ERROR, TAG, "out of memory");
1992         OICFree(responseData.token);
1993         return NULL;
1994     }
1995     responseInfo->info = responseData;
1996
1997     CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1998     if (!data)
1999     {
2000         OIC_LOG(ERROR, TAG, "out of memory");
2001         OICFree(responseInfo);
2002         return NULL;
2003     }
2004
2005     data->requestInfo = NULL;
2006     data->responseInfo = responseInfo;
2007     data->remoteEndpoint = CACloneEndpoint(endpoint);
2008     data->type = SEND_TYPE_UNICAST;
2009
2010     return data;
2011 }
2012
2013 CAData_t *CACloneCAData(const CAData_t *data)
2014 {
2015     VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2016
2017     CAData_t *clone = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
2018     if (!clone)
2019     {
2020         OIC_LOG(ERROR, TAG, "out of memory");
2021         return NULL;
2022     }
2023     *clone = *data;
2024
2025     if (data->requestInfo)
2026     {
2027         clone->requestInfo = CACloneRequestInfo(data->requestInfo);
2028     }
2029
2030     if (data->responseInfo)
2031     {
2032         clone->responseInfo = CACloneResponseInfo(data->responseInfo);
2033     }
2034
2035     if (data->remoteEndpoint)
2036     {
2037         clone->remoteEndpoint = CACloneEndpoint(data->remoteEndpoint);
2038     }
2039
2040     return clone;
2041 }
2042
2043 CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
2044                                    size_t payloadLen)
2045 {
2046     OIC_LOG(DEBUG, TAG, "IN-UpdatePayload");
2047
2048     VERIFY_NON_NULL(data, TAG, "data is NULL");
2049     VERIFY_NON_NULL(payload, TAG, "payload is NULL");
2050
2051     if (data->requestInfo)
2052     {
2053         // allocate payload field
2054         if (data->requestInfo->info.payload)
2055         {
2056             char *temp = (char *) OICCalloc(payloadLen, sizeof(char));
2057             if (!temp)
2058             {
2059                 OIC_LOG(ERROR, TAG, "out of memory");
2060                 return CA_STATUS_FAILED;
2061             }
2062             memcpy(temp, payload, payloadLen);
2063
2064             // save the full payload
2065             OICFree(data->requestInfo->info.payload);
2066             data->requestInfo->info.payload = (CAPayload_t) temp;
2067         }
2068         data->requestInfo->info.payloadSize = payloadLen;
2069     }
2070
2071     if (data->responseInfo)
2072     {
2073         // allocate payload field
2074         if (data->responseInfo->info.payload)
2075         {
2076             char *temp = (char *) OICCalloc(payloadLen, sizeof(char));
2077             if (!temp)
2078             {
2079                 OIC_LOG(ERROR, TAG, "out of memory");
2080                 return CA_STATUS_FAILED;
2081             }
2082             memcpy(temp, payload, payloadLen);
2083
2084             // save the full payload
2085             OICFree(data->responseInfo->info.payload);
2086             data->responseInfo->info.payload = (CAPayload_t) temp;
2087         }
2088         data->responseInfo->info.payloadSize = payloadLen;
2089     }
2090
2091     OIC_LOG(DEBUG, TAG, "OUT-UpdatePayload");
2092
2093     return CA_STATUS_OK;
2094 }
2095
2096 CAPayload_t CAGetPayloadInfo(const CAData_t *data, size_t *payloadLen)
2097 {
2098     VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
2099     VERIFY_NON_NULL_RET(payloadLen, TAG, "payloadLen", NULL);
2100
2101     if (data->requestInfo)
2102     {
2103         if (data->requestInfo->info.payload)
2104         {
2105             *payloadLen = data->requestInfo->info.payloadSize;
2106             return data->requestInfo->info.payload;
2107         }
2108     }
2109     else
2110     {
2111         if (data->responseInfo->info.payload)
2112         {
2113             *payloadLen = data->responseInfo->info.payloadSize;
2114             return data->responseInfo->info.payload;
2115         }
2116     }
2117
2118     return NULL;
2119 }
2120
2121 CAResult_t CAHandleBlockErrorResponse(coap_block_t *block, uint16_t blockType,
2122                                       uint32_t responseResult)
2123 {
2124     OIC_LOG(DEBUG, TAG, "IN-HandleBlockErrorRes");
2125     VERIFY_NON_NULL(block, TAG, "block is NULL");
2126
2127     // update block data
2128     switch (responseResult)
2129     {
2130         case CA_REQUEST_ENTITY_INCOMPLETE:
2131             block->num = 0;
2132             break;
2133         case CA_REQUEST_ENTITY_TOO_LARGE:
2134             if (COAP_OPTION_BLOCK1 == blockType)
2135             {
2136                 block->num++;
2137             }
2138             block->m = 0;
2139             break;
2140         default:
2141             OIC_LOG_V(ERROR, TAG, "there is no Error Code of BWT[%d]", responseResult);
2142     }
2143
2144     OIC_LOG(DEBUG, TAG, "OUT-HandleBlockErrorRes");
2145     return CA_STATUS_OK;
2146 }
2147
2148 CAResult_t CAUpdateBlockOptionType(const CABlockDataID_t *blockID,
2149                                    uint8_t blockType)
2150 {
2151     OIC_LOG(DEBUG, TAG, "IN-UpdateBlockOptionType");
2152     VERIFY_NON_NULL(blockID, TAG, "blockID");
2153
2154     ca_mutex_lock(g_context.blockDataListMutex);
2155
2156     size_t len = u_arraylist_length(g_context.dataList);
2157     for (size_t i = 0; i < len; i++)
2158     {
2159         CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2160         if (CABlockidMatches(currData, blockID))
2161         {
2162             currData->type = blockType;
2163             ca_mutex_unlock(g_context.blockDataListMutex);
2164             OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2165             return CA_STATUS_OK;
2166         }
2167     }
2168     ca_mutex_unlock(g_context.blockDataListMutex);
2169
2170     OIC_LOG(DEBUG, TAG, "OUT-UpdateBlockOptionType");
2171     return CA_STATUS_FAILED;
2172 }
2173
2174 uint8_t CAGetBlockOptionType(const CABlockDataID_t *blockID)
2175 {
2176     OIC_LOG(DEBUG, TAG, "IN-GetBlockOptionType");
2177     VERIFY_NON_NULL_RET(blockID, TAG, "blockID", 0);
2178
2179     ca_mutex_lock(g_context.blockDataListMutex);
2180
2181     size_t len = u_arraylist_length(g_context.dataList);
2182     for (size_t i = 0; i < len; i++)
2183     {
2184         CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2185         if (CABlockidMatches(currData, blockID))
2186         {
2187             ca_mutex_unlock(g_context.blockDataListMutex);
2188             OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2189             return currData->type;
2190         }
2191     }
2192     ca_mutex_unlock(g_context.blockDataListMutex);
2193
2194     OIC_LOG(DEBUG, TAG, "OUT-GetBlockOptionType");
2195     return 0;
2196 }
2197
2198 CAData_t *CAGetDataSetFromBlockDataList(const CABlockDataID_t *blockID)
2199 {
2200     VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2201
2202     ca_mutex_lock(g_context.blockDataListMutex);
2203
2204     size_t len = u_arraylist_length(g_context.dataList);
2205     for (size_t i = 0; i < len; i++)
2206     {
2207         CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2208         if (CABlockidMatches(currData, blockID))
2209         {
2210             ca_mutex_unlock(g_context.blockDataListMutex);
2211             return currData->sentData;
2212         }
2213     }
2214     ca_mutex_unlock(g_context.blockDataListMutex);
2215
2216     return NULL;
2217 }
2218
2219 CAResult_t CAGetTokenFromBlockDataList(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
2220                                        CAResponseInfo_t *responseInfo)
2221 {
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");
2226
2227     ca_mutex_lock(g_context.blockDataListMutex);
2228
2229     size_t len = u_arraylist_length(g_context.dataList);
2230     for (size_t i = 0; i < len; i++)
2231     {
2232         CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2233         if (NULL == currData)
2234         {
2235             continue;
2236         }
2237
2238         if (NULL != currData->sentData && NULL != currData->sentData->requestInfo)
2239         {
2240             if (pdu->hdr->id == currData->sentData->requestInfo->info.messageId &&
2241                     endpoint->adapter == currData->sentData->remoteEndpoint->adapter)
2242             {
2243                 if (NULL != currData->sentData->requestInfo->info.token)
2244                 {
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)
2249                     {
2250                         OIC_LOG(ERROR, TAG, "out of memory");
2251                         ca_mutex_unlock(g_context.blockDataListMutex);
2252                         return CA_MEMORY_ALLOC_FAILED;
2253                     }
2254                     memcpy(responseInfo->info.token, currData->sentData->requestInfo->info.token,
2255                            responseInfo->info.tokenLength);
2256
2257                     ca_mutex_unlock(g_context.blockDataListMutex);
2258                     OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2259                     return CA_STATUS_OK;
2260                 }
2261             }
2262         }
2263     }
2264
2265     ca_mutex_unlock(g_context.blockDataListMutex);
2266
2267     OIC_LOG(DEBUG, TAG, "OUT-CAGetTokenFromBlockDataList");
2268     return CA_STATUS_OK;
2269 }
2270
2271 CAResult_t CACheckBlockDataValidation(const CAData_t *sendData, CABlockData_t **blockData)
2272 {
2273     VERIFY_NON_NULL(sendData, TAG, "sendData");
2274     VERIFY_NON_NULL(blockData, TAG, "blockData");
2275
2276     ca_mutex_lock(g_context.blockDataListMutex);
2277
2278     size_t len = u_arraylist_length(g_context.dataList);
2279     for (size_t i = 0; i < len; i++)
2280     {
2281         CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2282
2283         if (!currData)
2284         {
2285             continue;
2286         }
2287
2288         if (sendData->requestInfo) // sendData is requestMessage
2289         {
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)
2295             {
2296                 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2297                         (CAToken_t)sendData->requestInfo->info.token,
2298                         sendData->requestInfo->info.tokenLength,
2299                         sendData->remoteEndpoint->port);
2300
2301                 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
2302                 {
2303                     OIC_LOG(ERROR, TAG, "blockId is null");
2304                     CADestroyBlockID(blockDataID);
2305                     return CA_STATUS_FAILED;
2306                 }
2307
2308                 if (CABlockidMatches(currData, blockDataID))
2309                 {
2310                     OIC_LOG(ERROR, TAG, "already sent");
2311                     CADestroyBlockID(blockDataID);
2312                     continue;
2313                 }
2314                 CADestroyBlockID(blockDataID);
2315             }
2316         }
2317         else if (sendData->responseInfo) // sendData is responseMessage
2318         {
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)
2324             {
2325                 CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2326                         (CAToken_t)sendData->responseInfo->info.token,
2327                         sendData->responseInfo->info.tokenLength,
2328                         sendData->remoteEndpoint->port);
2329
2330                 if(NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
2331                 {
2332                     OIC_LOG(ERROR, TAG, "blockId is null");
2333                     CADestroyBlockID(blockDataID);
2334                     return CA_STATUS_FAILED;
2335                 }
2336
2337                 if (CABlockidMatches(currData, blockDataID))
2338                 {
2339                     // set sendData
2340                     if (NULL != currData->sentData)
2341                     {
2342                         OIC_LOG(DEBUG, TAG, "init block number");
2343                         CADestroyDataSet(currData->sentData);
2344                     }
2345                     currData->sentData = CACloneCAData(sendData);
2346                     *blockData = currData;
2347                     CADestroyBlockID(blockDataID);
2348                     ca_mutex_unlock(g_context.blockDataListMutex);
2349                     return CA_STATUS_OK;
2350                 }
2351                 CADestroyBlockID(blockDataID);
2352             }
2353         }
2354         else
2355         {
2356             OIC_LOG(ERROR, TAG, "no CAInfo data");
2357             continue;
2358         }
2359     }
2360     ca_mutex_unlock(g_context.blockDataListMutex);
2361
2362     return CA_STATUS_FAILED;
2363 }
2364
2365 CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID)
2366 {
2367     VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2368
2369     ca_mutex_lock(g_context.blockDataListMutex);
2370
2371     size_t len = u_arraylist_length(g_context.dataList);
2372     for (size_t i = 0; i < len; i++)
2373     {
2374         CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2375         if (CABlockidMatches(currData, blockID))
2376         {
2377             ca_mutex_unlock(g_context.blockDataListMutex);
2378             return currData;
2379         }
2380     }
2381     ca_mutex_unlock(g_context.blockDataListMutex);
2382
2383     return NULL;
2384 }
2385
2386 coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID,
2387                                uint16_t blockType)
2388 {
2389     OIC_LOG(DEBUG, TAG, "IN-GetBlockOption");
2390     VERIFY_NON_NULL_RET(blockID, TAG, "blockID", NULL);
2391
2392     ca_mutex_lock(g_context.blockDataListMutex);
2393
2394     size_t len = u_arraylist_length(g_context.dataList);
2395     for (size_t i = 0; i < len; i++)
2396     {
2397         CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2398         if (CABlockidMatches(currData, blockID))
2399         {
2400             ca_mutex_unlock(g_context.blockDataListMutex);
2401             OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2402             if (COAP_OPTION_BLOCK2 == blockType)
2403             {
2404                 return &currData->block2;
2405             }
2406             else
2407             {
2408                 return &currData->block1;
2409             }
2410         }
2411     }
2412     ca_mutex_unlock(g_context.blockDataListMutex);
2413
2414     OIC_LOG(DEBUG, TAG, "OUT-GetBlockOption");
2415     return NULL;
2416 }
2417
2418 CAPayload_t CAGetPayloadFromBlockDataList(const CABlockDataID_t *blockID,
2419                                           size_t *fullPayloadLen)
2420 {
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);
2424
2425     ca_mutex_lock(g_context.blockDataListMutex);
2426
2427     size_t len = u_arraylist_length(g_context.dataList);
2428     for (size_t i = 0; i < len; i++)
2429     {
2430         CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2431         if (CABlockidMatches(currData, blockID))
2432         {
2433             ca_mutex_unlock(g_context.blockDataListMutex);
2434             *fullPayloadLen = currData->receivedPayloadLen;
2435             OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2436             return currData->payload;
2437         }
2438     }
2439     ca_mutex_unlock(g_context.blockDataListMutex);
2440
2441     OIC_LOG(DEBUG, TAG, "OUT-GetFullPayload");
2442     return NULL;
2443 }
2444
2445 CABlockData_t *CACreateNewBlockData(const CAData_t *sendData)
2446 {
2447     OIC_LOG(DEBUG, TAG, "IN-CACreateNewBlockData");
2448     VERIFY_NON_NULL_RET(sendData, TAG, "sendData", NULL);
2449
2450     // create block data
2451     CABlockData_t *data = (CABlockData_t *) OICCalloc(1, sizeof(CABlockData_t));
2452     if (!data)
2453     {
2454         OIC_LOG(ERROR, TAG, "memory alloc has failed");
2455         return NULL;
2456     }
2457
2458     data->block1.szx = CA_DEFAULT_BLOCK_SIZE;
2459     data->block2.szx = CA_DEFAULT_BLOCK_SIZE;
2460     data->sentData = CACloneCAData(sendData);
2461     if(!data->sentData)
2462     {
2463         OIC_LOG(ERROR, TAG, PCF("memory alloc has failed"));
2464         OICFree(data);
2465         return NULL;
2466     }
2467
2468     CAToken_t token = NULL;
2469     uint8_t tokenLength = 0;
2470     if (data->sentData->requestInfo)
2471     {
2472         // update token info
2473         tokenLength = data->sentData->requestInfo->info.tokenLength;
2474         token = data->sentData->requestInfo->info.token;
2475     }
2476     else if(data->sentData->responseInfo)
2477     {
2478         tokenLength = data->sentData->responseInfo->info.tokenLength;
2479         token = data->sentData->responseInfo->info.token;
2480     }
2481
2482     CABlockDataID_t* blockDataID = CACreateBlockDatablockId(
2483             token, tokenLength,
2484             data->sentData->remoteEndpoint->port);
2485     if (NULL == blockDataID || NULL == blockDataID->id || blockDataID->idLength < 1)
2486     {
2487         OIC_LOG(ERROR, TAG, "blockId is null");
2488         CADestroyBlockID(blockDataID);
2489         CADestroyDataSet(data->sentData);
2490         OICFree(data);
2491         return NULL;
2492     }
2493     data->blockDataId = blockDataID;
2494
2495     ca_mutex_lock(g_context.blockDataListMutex);
2496
2497     bool res = u_arraylist_add(g_context.dataList, (void *) data);
2498     if (!res)
2499     {
2500         OIC_LOG(ERROR, TAG, "add has failed");
2501         CADestroyBlockID(data->blockDataId);
2502         CADestroyDataSet(data->sentData);
2503         OICFree(data);
2504         ca_mutex_unlock(g_context.blockDataListMutex);
2505         return NULL;
2506     }
2507     ca_mutex_unlock(g_context.blockDataListMutex);
2508
2509     OIC_LOG(DEBUG, TAG, "OUT-CreateBlockData");
2510     return data;
2511 }
2512
2513 CAResult_t CARemoveBlockDataFromList(const CABlockDataID_t *blockID)
2514 {
2515     OIC_LOG(DEBUG, TAG, "CARemoveBlockData");
2516     VERIFY_NON_NULL(blockID, TAG, "blockID");
2517
2518     ca_mutex_lock(g_context.blockDataListMutex);
2519
2520     size_t len = u_arraylist_length(g_context.dataList);
2521     for (size_t i = 0; i < len; i++)
2522     {
2523         CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2524         if (CABlockidMatches(currData, blockID))
2525         {
2526             CABlockData_t *removedData = u_arraylist_remove(g_context.dataList, i);
2527             if (!removedData)
2528             {
2529                 OIC_LOG(ERROR, TAG, "data is NULL");
2530                 ca_mutex_unlock(g_context.blockDataListMutex);
2531                 return CA_STATUS_FAILED;
2532             }
2533
2534             // destroy memory
2535             if (currData->sentData)
2536             {
2537                 CADestroyDataSet(currData->sentData);
2538             }
2539             CADestroyBlockID(currData->blockDataId);
2540             OICFree(currData->payload);
2541             OICFree(currData);
2542             ca_mutex_unlock(g_context.blockDataListMutex);
2543             return CA_STATUS_OK;
2544         }
2545     }
2546     ca_mutex_unlock(g_context.blockDataListMutex);
2547
2548     return CA_STATUS_OK;
2549 }
2550
2551 bool CAIsBlockDataInList(const CABlockDataID_t *blockID)
2552 {
2553     OIC_LOG(DEBUG, TAG, "IN-IsBlockDataInList");
2554     VERIFY_NON_NULL_RET(blockID, TAG, "blockID", false);
2555
2556     ca_mutex_lock(g_context.blockDataListMutex);
2557
2558     size_t len = u_arraylist_length(g_context.dataList);
2559     for (size_t i = 0; i < len; i++)
2560     {
2561         CABlockData_t *currData = (CABlockData_t *) u_arraylist_get(g_context.dataList, i);
2562         if (CABlockidMatches(currData, blockID))
2563         {
2564             OIC_LOG(DEBUG, TAG, "found block data");
2565             ca_mutex_unlock(g_context.blockDataListMutex);
2566             return true;
2567         }
2568     }
2569     ca_mutex_unlock(g_context.blockDataListMutex);
2570
2571     OIC_LOG(DEBUG, TAG, "OUT-IsBlockDataInList");
2572     return false;
2573 }
2574
2575 void CADestroyDataSet(CAData_t* data)
2576 {
2577     VERIFY_NON_NULL_VOID(data, TAG, "data");
2578
2579     CAFreeEndpoint(data->remoteEndpoint);
2580     CADestroyRequestInfoInternal(data->requestInfo);
2581     CADestroyResponseInfoInternal(data->responseInfo);
2582     OICFree(data);
2583 }
2584
2585 CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength,
2586                                           uint16_t portNumber)
2587 {
2588     VERIFY_NON_NULL_RET(token, TAG, "token", NULL);
2589
2590     char port[PORT_LENGTH] = {0,};
2591     port[0] = (char)((portNumber>>8) & 0xFF);
2592     port[1] = (char)(portNumber & 0xFF);
2593
2594     CABlockDataID_t* blockDataID = (CABlockDataID_t *) OICMalloc(sizeof(CABlockDataID_t));
2595     if (!blockDataID)
2596     {
2597         OIC_LOG(ERROR, TAG, "memory alloc has failed");
2598         return NULL;
2599     }
2600     blockDataID->idLength = tokenLength + sizeof(port);
2601     blockDataID->id = (uint8_t *) OICMalloc(blockDataID->idLength);
2602     if (!blockDataID->id)
2603     {
2604         OIC_LOG(ERROR, TAG, "memory alloc has failed");
2605         OICFree(blockDataID);
2606         return NULL;
2607     }
2608
2609     memcpy(blockDataID->id, token, tokenLength);
2610     memcpy(blockDataID->id + tokenLength, port, sizeof(port));
2611
2612     OIC_LOG(DEBUG, TAG, "BlockID is ");
2613     OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)blockDataID->id, blockDataID->idLength);
2614
2615     return blockDataID;
2616 }
2617
2618 void CADestroyBlockID(CABlockDataID_t *blockID)
2619 {
2620     VERIFY_NON_NULL_VOID(blockID, TAG, "blockID");
2621     OICFree(blockID->id);
2622     OICFree(blockID);
2623     blockID = NULL;
2624 }
2625
2626 bool CABlockidMatches(const CABlockData_t *currData, const CABlockDataID_t *blockID)
2627 {
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);
2631
2632     if ((currData->blockDataId)
2633         && (currData->blockDataId->id)
2634         && (currData->blockDataId->idLength == blockID->idLength)
2635         && !memcmp(currData->blockDataId->id, blockID->id, currData->blockDataId->idLength))
2636     {
2637         return true;
2638     }
2639     return false;
2640 }
2641
2642 void CALogBlockInfo(coap_block_t *block)
2643 {
2644     VERIFY_NON_NULL_VOID(block, TAG, "block");
2645
2646     OIC_LOG(DEBUG, TAG, "block option info");
2647
2648     OIC_LOG_V(DEBUG, TAG, "block option-num : %d", block->num);
2649
2650     OIC_LOG_V(DEBUG, TAG, "block option-m   : %d", block->m);
2651
2652     OIC_LOG_V(DEBUG, TAG, "block option-szx : %d", block->szx);
2653 }