changed block-wise transfer logic to process non-confirmable message
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / camessagehandler.c
1 /* *****************************************************************
2  *
3  * Copyright 2014 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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25
26 #include "cainterface.h"
27 #include "camessagehandler.h"
28 #include "caremotehandler.h"
29 #include "caprotocolmessage.h"
30 #include "logger.h"
31 #include "config.h" /* for coap protocol */
32 #include "oic_malloc.h"
33 #include "canetworkconfigurator.h"
34 #include "caadapterutils.h"
35 #include "cainterfacecontroller.h"
36 #include "caretransmission.h"
37
38 #ifdef WITH_BWT
39 #include "cablockwisetransfer.h"
40 #endif
41
42 #ifndef  SINGLE_THREAD
43 #include "uqueue.h"
44 #include "cathreadpool.h" /* for thread pool */
45 #include "caqueueingthread.h"
46
47 #define SINGLE_HANDLE
48 #define MAX_THREAD_POOL_SIZE    20
49
50 // thread pool handle
51 static ca_thread_pool_t g_threadPoolHandle = NULL;
52
53 // message handler main thread
54 static CAQueueingThread_t g_sendThread;
55 static CAQueueingThread_t g_receiveThread;
56
57 #else
58 #define CA_MAX_RT_ARRAY_SIZE    3
59 #endif  // SINGLE_THREAD
60
61 #define TAG "OIC_CA_MSG_HANDLE"
62
63 static CARetransmission_t g_retransmissionContext;
64
65 // handler field
66 static CARequestCallback g_requestHandler = NULL;
67 static CAResponseCallback g_responseHandler = NULL;
68 static CAErrorCallback g_errorHandler = NULL;
69 static CANetworkMonitorCallback g_nwMonitorHandler = NULL;
70
71 static void CAErrorHandler(const CAEndpoint_t *endpoint,
72                            const void *data, uint32_t dataLen,
73                            CAResult_t result);
74
75 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
76                                        const CARemoteId_t *identity,
77                                        const void *data, CADataType_t dataType);
78
79 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info,
80                             CAResult_t result);
81
82 #ifdef SINGLE_THREAD
83 static void CAProcessReceivedData(CAData_t *data);
84 #endif
85 static void CADestroyData(void *data, uint32_t size);
86 static void CALogPayloadInfo(CAInfo_t *info);
87 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *endpoint, uint16_t id,
88                                 CAToken_t token, uint8_t tokenLength);
89
90 #ifdef WITH_BWT
91 void CAAddDataToSendThread(CAData_t *data)
92 {
93     VERIFY_NON_NULL_VOID(data, TAG, "data");
94
95     // add thread
96     CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
97 }
98
99 void CAAddDataToReceiveThread(CAData_t *data)
100 {
101     VERIFY_NON_NULL_VOID(data, TAG, "data");
102
103     // add thread
104     CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
105 }
106 #endif
107
108 static bool CAIsSelectedNetworkAvailable()
109 {
110     u_arraylist_t *list = CAGetSelectedNetworkList();
111     if (!list || u_arraylist_length(list) == 0)
112     {
113         OIC_LOG(ERROR, TAG, "No selected network");
114         return false;
115     }
116
117     return true;
118 }
119
120 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
121                                        const CARemoteId_t *identity,
122                                        const void *data, CADataType_t dataType)
123 {
124     OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN");
125     CAInfo_t *info = NULL;
126     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
127     if (!cadata)
128     {
129         OIC_LOG(ERROR, TAG, "memory allocation failed");
130         return NULL;
131     }
132
133     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
134     if (!ep)
135     {
136         OIC_LOG(ERROR, TAG, "endpoint clone failed");
137         OICFree(cadata);
138         return NULL;
139     }
140
141     OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
142     CAResult_t result;
143
144     if(CA_RESPONSE_DATA == dataType)
145     {
146         CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
147         if (!resInfo)
148         {
149             OIC_LOG(ERROR, TAG, "memory allocation failed");
150             OICFree(cadata);
151             CAFreeEndpoint(ep);
152             return NULL;
153         }
154
155         result = CAGetResponseInfoFromPDU(data, resInfo, endpoint);
156         if (CA_STATUS_OK != result)
157         {
158             OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed");
159             CAFreeEndpoint(ep);
160             CADestroyResponseInfoInternal(resInfo);
161             OICFree(cadata);
162             return NULL;
163         }
164         cadata->responseInfo = resInfo;
165         info = &resInfo->info;
166         if (identity)
167         {
168             info->identity = *identity;
169         }
170         OIC_LOG(DEBUG, TAG, "Response Info :");
171         CALogPayloadInfo(info);
172     }
173     else if (CA_REQUEST_DATA == dataType)
174     {
175         CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t));
176         if (!reqInfo)
177         {
178             OIC_LOG(ERROR, TAG, "memory allocation failed");
179             OICFree(cadata);
180             CAFreeEndpoint(ep);
181             return NULL;
182         }
183
184         result = CAGetRequestInfoFromPDU(data, endpoint, reqInfo);
185         if (CA_STATUS_OK != result)
186         {
187             OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed");
188             CAFreeEndpoint(ep);
189             CADestroyRequestInfoInternal(reqInfo);
190             OICFree(cadata);
191             return NULL;
192         }
193
194         if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId,
195                                 reqInfo->info.token, reqInfo->info.tokenLength))
196         {
197             OIC_LOG(ERROR, TAG, "Second Request with same Token, Drop it");
198             CAFreeEndpoint(ep);
199             CADestroyRequestInfoInternal(reqInfo);
200             OICFree(cadata);
201             return NULL;
202         }
203
204         cadata->requestInfo = reqInfo;
205         info = &reqInfo->info;
206         if (identity)
207         {
208             info->identity = *identity;
209         }
210         OIC_LOG(DEBUG, TAG, "Request Info :");
211         CALogPayloadInfo(info);
212    }
213     else if (CA_ERROR_DATA == dataType)
214     {
215         CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
216         if (!errorInfo)
217         {
218             OIC_LOG(ERROR, TAG, "Memory allocation failed!");
219             OICFree(cadata);
220             CAFreeEndpoint(ep);
221             return NULL;
222         }
223
224         CAResult_t result = CAGetErrorInfoFromPDU(data, endpoint, errorInfo);
225         if (CA_STATUS_OK != result)
226         {
227             OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed");
228             CAFreeEndpoint(ep);
229             OICFree(errorInfo);
230             OICFree(cadata);
231             return NULL;
232         }
233
234         cadata->errorInfo = errorInfo;
235         info = &errorInfo->info;
236         if (identity)
237         {
238             info->identity = *identity;
239         }
240         OIC_LOG(DEBUG, TAG, "error Info :");
241         CALogPayloadInfo(info);
242     }
243
244     cadata->remoteEndpoint = ep;
245     cadata->dataType = dataType;
246
247     OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
248     return cadata;
249 }
250
251 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
252 {
253     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
254     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
255
256     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
257     if (!ep)
258     {
259         OIC_LOG(ERROR, TAG, "clone failed");
260         return;
261     }
262
263     CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
264
265     if (!resInfo)
266     {
267         OIC_LOG(ERROR, TAG, "calloc failed");
268         CAFreeEndpoint(ep);
269         return;
270     }
271
272     resInfo->result = CA_RETRANSMIT_TIMEOUT;
273     resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
274     resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
275
276     CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info),
277                                        endpoint);
278     if (CA_STATUS_OK != res)
279     {
280         OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
281         CADestroyResponseInfoInternal(resInfo);
282         CAFreeEndpoint(ep);
283         return;
284     }
285
286     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
287     if (NULL == cadata)
288     {
289         OIC_LOG(ERROR, TAG, "memory allocation failed !");
290         CAFreeEndpoint(ep);
291         CADestroyResponseInfoInternal(resInfo);
292         return;
293     }
294
295     cadata->type = SEND_TYPE_UNICAST;
296     cadata->remoteEndpoint = ep;
297     cadata->requestInfo = NULL;
298     cadata->responseInfo = resInfo;
299
300 #ifdef SINGLE_THREAD
301     CAProcessReceivedData(cadata);
302 #else
303     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
304 #endif
305 }
306
307 static void CADestroyData(void *data, uint32_t size)
308 {
309     OIC_LOG(DEBUG, TAG, "CADestroyData IN");
310     if ((size_t)size < sizeof(CAData_t))
311     {
312         OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
313     }
314     CAData_t *cadata = (CAData_t *) data;
315
316     if (NULL == cadata)
317     {
318         OIC_LOG(ERROR, TAG, "cadata is NULL");
319         return;
320     }
321
322     if (NULL != cadata->remoteEndpoint)
323     {
324         CAFreeEndpoint(cadata->remoteEndpoint);
325     }
326
327     if (NULL != cadata->requestInfo)
328     {
329         CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
330     }
331
332     if (NULL != cadata->responseInfo)
333     {
334         CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
335     }
336
337     if (NULL != cadata->errorInfo)
338     {
339         CADestroyErrorInfoInternal(cadata->errorInfo);
340     }
341
342     OICFree(cadata);
343     OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
344 }
345
346 #ifdef SINGLE_THREAD
347 static void CAProcessReceivedData(CAData_t *data)
348 {
349     OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
350     if (!data)
351     {
352         OIC_LOG(ERROR, TAG, "thread data error!!");
353         return;
354     }
355
356     // parse the data and call the callbacks.
357     // #1 parse the data
358     // #2 get endpoint
359     CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
360     if (!rep)
361     {
362         OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
363         return;
364     }
365
366     if (data->requestInfo && g_requestHandler)
367     {
368         g_requestHandler(rep, data->requestInfo);
369     }
370     else if (data->responseInfo && g_responseHandler)
371     {
372         g_responseHandler(rep, data->responseInfo);
373     }
374     else if (data->errorInfo && g_errorHandler)
375     {
376         g_errorHandler(rep, data->errorInfo);
377     }
378
379     CADestroyData(data, sizeof(CAData_t));
380
381     OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
382 }
383 #endif
384
385 #ifndef SINGLE_THREAD
386 static void CAReceiveThreadProcess(void *threadData)
387 {
388 #ifndef SINGLE_HANDLE
389     CAData_t *data = (CAData_t *) threadData;
390     CAProcessReceivedData(data);
391 #else
392     (void)threadData;
393 #endif
394 }
395 #endif // SINGLE_THREAD
396
397 static CAResult_t CAProcessMulticastData(const CAData_t *data)
398 {
399     VERIFY_NON_NULL(data, TAG, "data");
400     VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
401
402     coap_pdu_t *pdu = NULL;
403     CAInfo_t *info = NULL;
404     coap_list_t *options = NULL;
405     coap_transport_type transport;
406     CAResult_t res = CA_SEND_FAILED;
407     if (NULL != data->requestInfo)
408     {
409         OIC_LOG(DEBUG, TAG, "requestInfo is available..");
410
411         info = &data->requestInfo->info;
412         pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);
413
414         if (NULL != pdu)
415         {
416 #ifdef WITH_BWT
417             if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
418             {
419                 // Blockwise transfer
420                 res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options);
421                 if (CA_STATUS_OK != res)
422                 {
423                     OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
424                     goto exit;
425                 }
426             }
427 #endif // WITH_BWT
428         }
429         else
430         {
431             OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
432             CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
433             return res;
434         }
435     }
436     else if (NULL != data->responseInfo)
437     {
438         OIC_LOG(DEBUG, TAG, "responseInfo is available..");
439
440         info = &data->responseInfo->info;
441         pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
442                             &options, &transport);
443
444         if (NULL != pdu)
445         {
446 #ifdef WITH_BWT
447             if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
448             {
449                 // Blockwise transfer
450                 if (NULL != info)
451                 {
452                     res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options);
453                     if (CA_STATUS_OK != res)
454                     {
455                         OIC_LOG(INFO, TAG, "to write block option has failed");
456                         goto exit;
457                     }
458                 }
459             }
460 #endif // WITH_BWT
461         }
462         else
463         {
464             OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
465             CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
466             return res;
467         }
468     }
469     else
470     {
471         OIC_LOG(ERROR, TAG, "request or response info is empty");
472         return res;
473     }
474
475     CALogPDUInfo(pdu, data->remoteEndpoint);
476
477     OIC_LOG(DEBUG, TAG, "pdu to send :");
478     OIC_LOG_BUFFER(DEBUG, TAG,  (uint8_t*)pdu->hdr, pdu->length);
479
480     res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
481     if (CA_STATUS_OK != res)
482     {
483         OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
484         goto exit;
485     }
486
487     coap_delete_list(options);
488     coap_delete_pdu(pdu);
489     return res;
490
491 exit:
492     CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
493     coap_delete_list(options);
494     coap_delete_pdu(pdu);
495     return res;
496 }
497
498 static CAResult_t CAProcessSendData(const CAData_t *data)
499 {
500     VERIFY_NON_NULL(data, TAG, "data");
501     VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
502
503     CAResult_t res = CA_STATUS_FAILED;
504
505     CASendDataType_t type = data->type;
506
507     coap_pdu_t *pdu = NULL;
508     CAInfo_t *info = NULL;
509     coap_list_t *options = NULL;
510     coap_transport_type transport;
511
512     if (SEND_TYPE_UNICAST == type)
513     {
514         OIC_LOG(DEBUG,TAG,"Unicast message");
515 #ifdef ROUTING_GATEWAY
516         /*
517          * When forwarding a packet, do not attempt retransmission as its the responsibility of
518          * packet originator node
519          */
520         bool skipRetransmission = false;
521 #endif
522
523         if (NULL != data->requestInfo)
524         {
525             OIC_LOG(DEBUG, TAG, "requestInfo is available..");
526
527             info = &data->requestInfo->info;
528 #ifdef ROUTING_GATEWAY
529             skipRetransmission = data->requestInfo->info.skipRetransmission;
530 #endif
531             pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint,
532                                 &options, &transport);
533         }
534         else if (NULL != data->responseInfo)
535         {
536             OIC_LOG(DEBUG, TAG, "responseInfo is available..");
537
538             info = &data->responseInfo->info;
539 #ifdef ROUTING_GATEWAY
540             skipRetransmission = data->responseInfo->info.skipRetransmission;
541 #endif
542             pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
543                                 &options, &transport);
544         }
545         else
546         {
547             OIC_LOG(DEBUG, TAG, "request info, response info is empty");
548             return CA_STATUS_INVALID_PARAM;
549         }
550
551         // interface controller function call.
552         if (NULL != pdu)
553         {
554 #ifdef WITH_BWT
555             if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
556             {
557                 // Blockwise transfer
558                 if (NULL != info)
559                 {
560                     CAResult_t res = CAAddBlockOption(&pdu, info,
561                                                       data->remoteEndpoint,
562                                                       &options);
563                     if (CA_STATUS_OK != res)
564                     {
565                         OIC_LOG(INFO, TAG, "to write block option has failed");
566                         CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
567                         coap_delete_list(options);
568                         coap_delete_pdu(pdu);
569                         return res;
570                     }
571                 }
572             }
573 #endif // WITH_BWT
574             CALogPDUInfo(pdu, data->remoteEndpoint);
575
576             res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
577             if (CA_STATUS_OK != res)
578             {
579                 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
580                 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
581                 coap_delete_list(options);
582                 coap_delete_pdu(pdu);
583                 return res;
584             }
585
586 #ifdef WITH_TCP
587             if (CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter))
588             {
589                 OIC_LOG(INFO, TAG, "retransmission will be not worked");
590             }
591             else
592 #endif
593 #ifdef ROUTING_GATEWAY
594             if(!skipRetransmission)
595 #endif
596             {
597                 // for retransmission
598                 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint,
599                                                pdu->hdr, pdu->length);
600                 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
601                 {
602                     //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
603                     OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
604                     coap_delete_list(options);
605                     coap_delete_pdu(pdu);
606                     return res;
607                 }
608             }
609
610             coap_delete_list(options);
611             coap_delete_pdu(pdu);
612         }
613         else
614         {
615             OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
616             CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
617             return CA_SEND_FAILED;
618         }
619     }
620     else if (SEND_TYPE_MULTICAST == type)
621     {
622         OIC_LOG(DEBUG,TAG,"Multicast message");
623 #ifdef WITH_TCP
624         /*
625          * If CoAP over TCP is enabled, the CoAP pdu wont be same for IP and other adapters.
626          * That's why we need to generate two pdu's, one for IP and second for other transports.
627          * Two possible cases we might have to split: a) when adapter is CA_DEFAULT_ADAPTER
628          * b) when one of the adapter is IP adapter(ex: CA_ADAPTER_IP | CA_ADAPTER_GATT_BTLE)
629          */
630         if (data->remoteEndpoint->adapter == CA_DEFAULT_ADAPTER ||
631                 (CA_ADAPTER_IP & data->remoteEndpoint->adapter &&
632                     CA_ADAPTER_IP != data->remoteEndpoint->adapter))
633         {
634             if (data->remoteEndpoint->adapter == CA_DEFAULT_ADAPTER)
635             {
636                 data->remoteEndpoint->adapter = CA_ALL_ADAPTERS ^ CA_ADAPTER_IP;
637             }
638             else
639             {
640                 data->remoteEndpoint->adapter = data->remoteEndpoint->adapter ^ CA_ADAPTER_IP;
641             }
642             CAProcessMulticastData(data);
643             data->remoteEndpoint->adapter = CA_ADAPTER_IP;
644             CAProcessMulticastData(data);
645         }
646         else
647         {
648             CAProcessMulticastData(data);
649         }
650 #else
651         CAProcessMulticastData(data);
652 #endif
653     }
654
655     return CA_STATUS_OK;
656 }
657
658 #ifndef SINGLE_THREAD
659 static void CASendThreadProcess(void *threadData)
660 {
661     CAData_t *data = (CAData_t *) threadData;
662     CAProcessSendData(data);
663 }
664 #endif
665
666 /*
667  * If a second message arrives with the same message ID, token and the other address
668  * family, drop it.  Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
669  */
670 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id,
671                                 CAToken_t token, uint8_t tokenLength)
672 {
673     if (!ep)
674     {
675         return true;
676     }
677     if (ep->adapter != CA_ADAPTER_IP)
678     {
679         return false;
680     }
681     if (!caglobals.ip.dualstack)
682     {
683         return false;
684     }
685
686     if (tokenLength > CA_MAX_TOKEN_LEN)
687     {
688         /*
689          * If token length is more than CA_MAX_TOKEN_LEN,
690          * we compare the first CA_MAX_TOKEN_LEN bytes only.
691          */
692         tokenLength = CA_MAX_TOKEN_LEN;
693     }
694
695     bool ret = false;
696     CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
697
698     for (size_t i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
699     {
700         CAHistoryItem_t *item = &(history->items[i]);
701         if (id == item->messageId && tokenLength == item->tokenLength
702             && memcmp(item->token, token, tokenLength) == 0)
703         {
704             if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
705             {
706                 OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
707                           familyFlags & CA_IPV6 ? '6' : '4');
708                 ret = true;
709                 break;
710             }
711         }
712     }
713
714     history->items[history->nextIndex].flags = familyFlags;
715     history->items[history->nextIndex].messageId = id;
716     if (token && tokenLength)
717     {
718         memcpy(history->items[history->nextIndex].token, token, tokenLength);
719         history->items[history->nextIndex].tokenLength = tokenLength;
720     }
721
722     if (++history->nextIndex >= HISTORYSIZE)
723     {
724         history->nextIndex = 0;
725     }
726
727     return ret;
728 }
729
730 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
731                                      const void *data, uint32_t dataLen)
732 {
733     VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
734     VERIFY_NON_NULL_VOID(data, TAG, "data");
735
736     OIC_LOG(DEBUG, TAG, "received pdu data :");
737     OIC_LOG_BUFFER(DEBUG, TAG,  data, dataLen);
738
739     uint32_t code = CA_NOT_FOUND;
740     CAData_t *cadata = NULL;
741
742     coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code,
743                                                 &(sep->endpoint));
744     if (NULL == pdu)
745     {
746         OIC_LOG(ERROR, TAG, "Parse PDU failed");
747         return;
748     }
749
750     OIC_LOG_V(DEBUG, TAG, "code = %d", code);
751     if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
752     {
753         cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
754         if (!cadata)
755         {
756             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
757             coap_delete_pdu(pdu);
758             return;
759         }
760     }
761     else
762     {
763         cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
764         if (!cadata)
765         {
766             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
767             coap_delete_pdu(pdu);
768             return;
769         }
770
771 #ifdef WITH_TCP
772         if (CAIsSupportedCoAPOverTCP(sep->endpoint.adapter))
773         {
774             OIC_LOG(INFO, TAG, "retransmission is not supported");
775         }
776         else
777 #endif
778         {
779             // for retransmission
780             void *retransmissionPdu = NULL;
781             CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
782                                          pdu->length, &retransmissionPdu);
783
784 #ifndef WITH_BWT
785             // get token from saved data in retransmission list
786             if (retransmissionPdu && CA_EMPTY == code)
787             {
788                 if (cadata->responseInfo)
789                 {
790                     CAInfo_t *info = &cadata->responseInfo->info;
791                     CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
792                                                        info, &(sep->endpoint));
793                     if (CA_STATUS_OK != res)
794                     {
795                         OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
796                         OICFree(info->token);
797                         info->tokenLength = 0;
798                     }
799                 }
800             }
801 #endif
802             OICFree(retransmissionPdu);
803         }
804     }
805
806     cadata->type = SEND_TYPE_UNICAST;
807
808 #ifdef SINGLE_THREAD
809     CAProcessReceivedData(cadata);
810 #else
811 #ifdef WITH_BWT
812     if (CAIsSupportedBlockwiseTransfer(sep->endpoint.adapter))
813     {
814         CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
815         if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
816         {
817             OIC_LOG(ERROR, TAG, "this message does not have block option");
818             CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
819         }
820         else
821         {
822             CADestroyData(cadata, sizeof(CAData_t));
823         }
824     }
825     else
826 #endif
827     {
828         CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
829     }
830 #endif // SINGLE_THREAD
831
832     coap_delete_pdu(pdu);
833 }
834
835 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
836 {
837     (void)info;
838     (void)status;
839
840     if (g_nwMonitorHandler)
841     {
842         g_nwMonitorHandler(info, status);
843     }
844 }
845
846 void CAHandleRequestResponseCallbacks()
847 {
848 #ifdef SINGLE_THREAD
849     CAReadData();
850     CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
851 #else
852 #ifdef SINGLE_HANDLE
853     // parse the data and call the callbacks.
854     // #1 parse the data
855     // #2 get endpoint
856
857     ca_mutex_lock(g_receiveThread.threadMutex);
858
859     u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
860
861     ca_mutex_unlock(g_receiveThread.threadMutex);
862
863     if (NULL == item || NULL == item->msg)
864     {
865         return;
866     }
867
868     // get endpoint
869     CAData_t *td = (CAData_t *) item->msg;
870
871     if (td->requestInfo && g_requestHandler)
872     {
873         OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
874         g_requestHandler(td->remoteEndpoint, td->requestInfo);
875     }
876     else if (td->responseInfo && g_responseHandler)
877     {
878         OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
879         g_responseHandler(td->remoteEndpoint, td->responseInfo);
880     }
881     else if (td->errorInfo && g_errorHandler)
882     {
883         OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
884         g_errorHandler(td->remoteEndpoint, td->errorInfo);
885     }
886
887     CADestroyData(item->msg, sizeof(CAData_t));
888     OICFree(item);
889
890 #endif // SINGLE_HANDLE
891 #endif // SINGLE_THREAD
892 }
893
894 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
895                                    CADataType_t dataType)
896 {
897     OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
898
899     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
900     if (!cadata)
901     {
902         OIC_LOG(ERROR, TAG, "memory allocation failed");
903         return NULL;
904     }
905
906     if(CA_REQUEST_DATA == dataType)
907     {
908         // clone request info
909         CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
910
911         if(!request)
912         {
913             OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
914             goto exit;
915         }
916
917         cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
918         cadata->requestInfo =  request;
919     }
920     else if(CA_RESPONSE_DATA == dataType)
921     {
922         // clone response info
923         CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
924
925         if(!response)
926         {
927             OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
928             goto exit;
929         }
930
931         cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
932         cadata->responseInfo = response;
933     }
934     else
935     {
936         OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
937         goto exit;
938     }
939
940     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
941     if (!ep)
942     {
943         OIC_LOG(ERROR, TAG, "endpoint clone failed");
944         CADestroyData(cadata, sizeof(CAData_t));
945         return NULL;
946     }
947
948     cadata->remoteEndpoint = ep;
949     cadata->dataType = dataType;
950     return cadata;
951
952 exit:
953     OICFree(cadata);
954     return NULL;
955 }
956
957 CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg,
958                                CADataType_t dataType)
959 {
960     VERIFY_NON_NULL(endpoint, TAG, "endpoint");
961     VERIFY_NON_NULL(sendMsg, TAG, "sendMsg");
962
963     if (false == CAIsSelectedNetworkAvailable())
964     {
965         return CA_STATUS_FAILED;
966     }
967
968 #ifdef ARDUINO
969     // If max retransmission queue is reached, then don't handle new request
970     if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
971     {
972         OIC_LOG(ERROR, TAG, "max RT queue size reached!");
973         return CA_SEND_FAILED;
974     }
975 #endif // ARDUINO
976
977     CAData_t *data = CAPrepareSendData(endpoint, sendMsg, dataType);
978     if(!data)
979     {
980         OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
981         return CA_MEMORY_ALLOC_FAILED;
982     }
983
984 #ifdef SINGLE_THREAD
985     CAResult_t result = CAProcessSendData(data);
986     if(CA_STATUS_OK != result)
987     {
988         OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
989         CADestroyData(data, sizeof(CAData_t));
990         return result;
991     }
992
993     CADestroyData(data, sizeof(CAData_t));
994 #else
995 #ifdef WITH_BWT
996     if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
997     {
998         // send block data
999         CAResult_t res = CASendBlockWiseData(data);
1000         if(CA_NOT_SUPPORTED == res)
1001         {
1002             OIC_LOG(DEBUG, TAG, "normal msg will be sent");
1003             CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1004             return CA_STATUS_OK;
1005         }
1006         else
1007         {
1008             CADestroyData(data, sizeof(CAData_t));
1009         }
1010         return res;
1011     }
1012     else
1013 #endif // WITH_BWT
1014     {
1015         CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1016     }
1017 #endif // SINGLE_THREAD
1018
1019     return CA_STATUS_OK;
1020 }
1021
1022 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1023                              CAErrorCallback errorHandler)
1024 {
1025     g_requestHandler = ReqHandler;
1026     g_responseHandler = RespHandler;
1027     g_errorHandler = errorHandler;
1028 }
1029
1030 void CASetNetworkMonitorCallback(CANetworkMonitorCallback nwMonitorHandler)
1031 {
1032     g_nwMonitorHandler = nwMonitorHandler;
1033 }
1034
1035 CAResult_t CAInitializeMessageHandler()
1036 {
1037     CASetPacketReceivedCallback(CAReceivedPacketCallback);
1038
1039     CASetNetworkChangeCallback(CANetworkChangedCallback);
1040     CASetErrorHandleCallback(CAErrorHandler);
1041
1042 #ifndef SINGLE_THREAD
1043     // create thread pool
1044     CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1045     if (CA_STATUS_OK != res)
1046     {
1047         OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1048         return res;
1049     }
1050
1051     // send thread initialize
1052     res = CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1053                                      CASendThreadProcess, CADestroyData);
1054     if (CA_STATUS_OK != res)
1055     {
1056         OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1057         ca_thread_pool_free(g_threadPoolHandle);
1058         g_threadPoolHandle = NULL;
1059         return res;
1060     }
1061
1062     // start send thread
1063     res = CAQueueingThreadStart(&g_sendThread);
1064     if (CA_STATUS_OK != res)
1065     {
1066         OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1067         ca_thread_pool_free(g_threadPoolHandle);
1068         g_threadPoolHandle = NULL;
1069         CAQueueingThreadDestroy(&g_sendThread);
1070         return res;
1071     }
1072
1073     // receive thread initialize
1074     res = CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1075                                      CAReceiveThreadProcess, CADestroyData);
1076     if (CA_STATUS_OK != res)
1077     {
1078         OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1079         ca_thread_pool_free(g_threadPoolHandle);
1080         g_threadPoolHandle = NULL;
1081         CAQueueingThreadDestroy(&g_sendThread);
1082         return res;
1083     }
1084
1085 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1086     // start receive thread
1087     res = CAQueueingThreadStart(&g_receiveThread);
1088     if (CA_STATUS_OK != res)
1089     {
1090         OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1091         ca_thread_pool_free(g_threadPoolHandle);
1092         g_threadPoolHandle = NULL;
1093         CAQueueingThreadDestroy(&g_sendThread);
1094         CAQueueingThreadDestroy(&g_receiveThread);
1095         return res;
1096     }
1097 #endif // SINGLE_HANDLE
1098
1099     // retransmission initialize
1100     res = CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle,
1101                                      CASendUnicastData, CATimeoutCallback, NULL);
1102     if (CA_STATUS_OK != res)
1103     {
1104         OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1105         ca_thread_pool_free(g_threadPoolHandle);
1106         g_threadPoolHandle = NULL;
1107         CAQueueingThreadDestroy(&g_sendThread);
1108         CAQueueingThreadDestroy(&g_receiveThread);
1109         return res;
1110     }
1111
1112 #ifdef WITH_BWT
1113     // block-wise transfer initialize
1114     res = CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1115     if (CA_STATUS_OK != res)
1116     {
1117         OIC_LOG(ERROR, TAG, "Failed to Initialize BlockWiseTransfer.");
1118         ca_thread_pool_free(g_threadPoolHandle);
1119         g_threadPoolHandle = NULL;
1120         CAQueueingThreadDestroy(&g_sendThread);
1121         CAQueueingThreadDestroy(&g_receiveThread);
1122         CARetransmissionDestroy(&g_retransmissionContext);
1123         return res;
1124     }
1125 #endif
1126
1127     // start retransmission
1128     res = CARetransmissionStart(&g_retransmissionContext);
1129     if (CA_STATUS_OK != res)
1130     {
1131         OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1132         ca_thread_pool_free(g_threadPoolHandle);
1133         g_threadPoolHandle = NULL;
1134         CAQueueingThreadDestroy(&g_sendThread);
1135         CAQueueingThreadDestroy(&g_receiveThread);
1136         CARetransmissionDestroy(&g_retransmissionContext);
1137         return res;
1138     }
1139
1140     // initialize interface adapters by controller
1141     CAInitializeAdapters(g_threadPoolHandle);
1142 #else
1143     // retransmission initialize
1144     CAResult_t res = CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1145                                                 CATimeoutCallback, NULL);
1146     if (CA_STATUS_OK != res)
1147     {
1148         OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1149         return res;
1150     }
1151
1152     CAInitializeAdapters();
1153 #endif // SINGLE_THREAD
1154
1155     return CA_STATUS_OK;
1156 }
1157
1158 void CATerminateMessageHandler()
1159 {
1160 #ifndef SINGLE_THREAD
1161     CATransportAdapter_t connType;
1162     u_arraylist_t *list = CAGetSelectedNetworkList();
1163     uint32_t length = u_arraylist_length(list);
1164
1165     uint32_t i = 0;
1166     for (i = 0; i < length; i++)
1167     {
1168         void* ptrType = u_arraylist_get(list, i);
1169
1170         if (NULL == ptrType)
1171         {
1172             continue;
1173         }
1174
1175         connType = *(CATransportAdapter_t *)ptrType;
1176         CAStopAdapter(connType);
1177     }
1178
1179     // stop retransmission
1180     if (NULL != g_retransmissionContext.threadMutex)
1181     {
1182         CARetransmissionStop(&g_retransmissionContext);
1183     }
1184
1185     // stop thread
1186     // delete thread data
1187     if (NULL != g_sendThread.threadMutex)
1188     {
1189         CAQueueingThreadStop(&g_sendThread);
1190     }
1191
1192     // stop thread
1193     // delete thread data
1194     if (NULL != g_receiveThread.threadMutex)
1195     {
1196 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1197         CAQueueingThreadStop(&g_receiveThread);
1198 #endif
1199     }
1200
1201     // destroy thread pool
1202     if (NULL != g_threadPoolHandle)
1203     {
1204         ca_thread_pool_free(g_threadPoolHandle);
1205         g_threadPoolHandle = NULL;
1206     }
1207
1208 #ifdef WITH_BWT
1209     CATerminateBlockWiseTransfer();
1210 #endif
1211     CARetransmissionDestroy(&g_retransmissionContext);
1212     CAQueueingThreadDestroy(&g_sendThread);
1213     CAQueueingThreadDestroy(&g_receiveThread);
1214
1215     // terminate interface adapters by controller
1216     CATerminateAdapters();
1217 #else
1218     // terminate interface adapters by controller
1219     CATerminateAdapters();
1220
1221     // stop retransmission
1222     CARetransmissionStop(&g_retransmissionContext);
1223     CARetransmissionDestroy(&g_retransmissionContext);
1224 #endif // SINGLE_THREAD
1225 }
1226
1227 void CALogPDUInfo(coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
1228 {
1229     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1230     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
1231
1232     OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1233
1234 #ifdef WITH_TCP
1235     if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
1236     {
1237         OIC_LOG(DEBUG, TAG, "pdu header data :");
1238         OIC_LOG_BUFFER(DEBUG, TAG,  (const uint8_t *) pdu->hdr, pdu->length);
1239     }
1240     else
1241 #endif
1242     {
1243         OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->coap_hdr_udp_t.type);
1244
1245         OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->coap_hdr_udp_t.code);
1246
1247         OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1248
1249         OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->coap_hdr_udp_t.token,
1250                        pdu->hdr->coap_hdr_udp_t.token_length);
1251     }
1252 }
1253
1254 static void CALogPayloadInfo(CAInfo_t *info)
1255 {
1256     if(info)
1257     {
1258         if (info->options)
1259         {
1260             for (uint32_t i = 0; i < info->numOptions; i++)
1261             {
1262                 OIC_LOG_V(DEBUG, TAG, "optionID: %u", info->options[i].optionID);
1263
1264                 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1265             }
1266         }
1267
1268         if (info->payload)
1269         {
1270             OIC_LOG_V(DEBUG, TAG, "payload: %p(%zu)", info->payload,
1271                       info->payloadSize);
1272         }
1273
1274         if (info->token)
1275         {
1276             OIC_LOG(DEBUG, TAG, "token:");
1277             OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1278                            info->tokenLength);
1279         }
1280         OIC_LOG_V(DEBUG, TAG, "msgID: %u", info->messageId);
1281     }
1282     else
1283     {
1284         OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1285     }
1286 }
1287
1288 void CAErrorHandler(const CAEndpoint_t *endpoint,
1289                     const void *data, uint32_t dataLen,
1290                     CAResult_t result)
1291 {
1292     OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1293
1294 #ifndef SINGLE_THREAD
1295     VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1296     VERIFY_NON_NULL_VOID(data, TAG, "data");
1297
1298     uint32_t code = CA_NOT_FOUND;
1299     //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1300     //Get PDU data
1301     coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
1302     if (NULL == pdu)
1303     {
1304         OIC_LOG(ERROR, TAG, "Parse PDU failed");
1305         return;
1306     }
1307
1308     CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1309     if(!cadata)
1310     {
1311         OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1312         coap_delete_pdu(pdu);
1313         return;
1314     }
1315
1316     cadata->errorInfo->result = result;
1317
1318     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1319     coap_delete_pdu(pdu);
1320 #endif
1321
1322     OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1323     return;
1324 }
1325
1326 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1327 {
1328     OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1329 #ifndef SINGLE_THREAD
1330     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1331     if (!cadata)
1332     {
1333         OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1334         return;
1335     }
1336
1337     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1338     if (!ep)
1339     {
1340         OIC_LOG(ERROR, TAG, "endpoint clone failed");
1341         OICFree(cadata);
1342         return;
1343     }
1344
1345     CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1346     if (!errorInfo)
1347     {
1348         OIC_LOG(ERROR, TAG, "errorInfo memory allocation failed");
1349         OICFree(cadata);
1350         CAFreeEndpoint(ep);
1351         return;
1352     }
1353
1354     CAResult_t res = CACloneInfo(info, &errorInfo->info);
1355     if (CA_STATUS_OK != res)
1356     {
1357         OIC_LOG(ERROR, TAG, "info clone failed");
1358         OICFree(cadata);
1359         OICFree(errorInfo);
1360         CAFreeEndpoint(ep);
1361         return;
1362     }
1363
1364     errorInfo->result = result;
1365     cadata->remoteEndpoint = ep;
1366     cadata->errorInfo = errorInfo;
1367     cadata->dataType = CA_ERROR_DATA;
1368
1369     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1370 #endif
1371     OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");
1372 }