Imported Upstream version 1.1.0
[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             // get token from saved data in retransmission list
785             if (retransmissionPdu && CA_EMPTY == code)
786             {
787                 if (cadata->responseInfo)
788                 {
789                     CAInfo_t *info = &cadata->responseInfo->info;
790                     CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
791                                                        info, &(sep->endpoint));
792                     if (CA_STATUS_OK != res)
793                     {
794                         OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
795                         OICFree(info->token);
796                         info->tokenLength = 0;
797                     }
798                 }
799             }
800             OICFree(retransmissionPdu);
801         }
802     }
803
804     cadata->type = SEND_TYPE_UNICAST;
805
806 #ifdef SINGLE_THREAD
807     CAProcessReceivedData(cadata);
808 #else
809 #ifdef WITH_BWT
810     if (CAIsSupportedBlockwiseTransfer(sep->endpoint.adapter))
811     {
812         CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
813         if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
814         {
815             OIC_LOG(ERROR, TAG, "this message does not have block option");
816             CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
817         }
818         else
819         {
820             CADestroyData(cadata, sizeof(CAData_t));
821         }
822     }
823     else
824 #endif
825     {
826         CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
827     }
828 #endif // SINGLE_THREAD
829
830     coap_delete_pdu(pdu);
831 }
832
833 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
834 {
835     (void)info;
836     (void)status;
837
838     if (g_nwMonitorHandler)
839     {
840         g_nwMonitorHandler(info, status);
841     }
842 }
843
844 void CAHandleRequestResponseCallbacks()
845 {
846 #ifdef SINGLE_THREAD
847     CAReadData();
848     CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
849 #else
850 #ifdef SINGLE_HANDLE
851     // parse the data and call the callbacks.
852     // #1 parse the data
853     // #2 get endpoint
854
855     ca_mutex_lock(g_receiveThread.threadMutex);
856
857     u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
858
859     ca_mutex_unlock(g_receiveThread.threadMutex);
860
861     if (NULL == item || NULL == item->msg)
862     {
863         return;
864     }
865
866     // get endpoint
867     CAData_t *td = (CAData_t *) item->msg;
868
869     if (td->requestInfo && g_requestHandler)
870     {
871         OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
872         g_requestHandler(td->remoteEndpoint, td->requestInfo);
873     }
874     else if (td->responseInfo && g_responseHandler)
875     {
876         OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
877         g_responseHandler(td->remoteEndpoint, td->responseInfo);
878     }
879     else if (td->errorInfo && g_errorHandler)
880     {
881         OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
882         g_errorHandler(td->remoteEndpoint, td->errorInfo);
883     }
884
885     CADestroyData(item->msg, sizeof(CAData_t));
886     OICFree(item);
887
888 #endif // SINGLE_HANDLE
889 #endif // SINGLE_THREAD
890 }
891
892 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
893                                    CADataType_t dataType)
894 {
895     OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
896
897     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
898     if (!cadata)
899     {
900         OIC_LOG(ERROR, TAG, "memory allocation failed");
901         return NULL;
902     }
903
904     if(CA_REQUEST_DATA == dataType)
905     {
906         // clone request info
907         CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
908
909         if(!request)
910         {
911             OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
912             goto exit;
913         }
914
915         cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
916         cadata->requestInfo =  request;
917     }
918     else if(CA_RESPONSE_DATA == dataType)
919     {
920         // clone response info
921         CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
922
923         if(!response)
924         {
925             OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
926             goto exit;
927         }
928
929         cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
930         cadata->responseInfo = response;
931     }
932     else
933     {
934         OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
935         goto exit;
936     }
937
938     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
939     if (!ep)
940     {
941         OIC_LOG(ERROR, TAG, "endpoint clone failed");
942         CADestroyData(cadata, sizeof(CAData_t));
943         return NULL;
944     }
945
946     cadata->remoteEndpoint = ep;
947     cadata->dataType = dataType;
948     return cadata;
949
950 exit:
951     OICFree(cadata);
952     return NULL;
953 }
954
955 CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg,
956                                CADataType_t dataType)
957 {
958     VERIFY_NON_NULL(endpoint, TAG, "endpoint");
959     VERIFY_NON_NULL(sendMsg, TAG, "sendMsg");
960
961     if (false == CAIsSelectedNetworkAvailable())
962     {
963         return CA_STATUS_FAILED;
964     }
965
966 #ifdef ARDUINO
967     // If max retransmission queue is reached, then don't handle new request
968     if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
969     {
970         OIC_LOG(ERROR, TAG, "max RT queue size reached!");
971         return CA_SEND_FAILED;
972     }
973 #endif // ARDUINO
974
975     CAData_t *data = CAPrepareSendData(endpoint, sendMsg, dataType);
976     if(!data)
977     {
978         OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
979         return CA_MEMORY_ALLOC_FAILED;
980     }
981
982 #ifdef SINGLE_THREAD
983     CAResult_t result = CAProcessSendData(data);
984     if(CA_STATUS_OK != result)
985     {
986         OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
987         CADestroyData(data, sizeof(CAData_t));
988         return result;
989     }
990
991     CADestroyData(data, sizeof(CAData_t));
992 #else
993 #ifdef WITH_BWT
994     if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
995     {
996         // send block data
997         CAResult_t res = CASendBlockWiseData(data);
998         if(CA_NOT_SUPPORTED == res)
999         {
1000             OIC_LOG(DEBUG, TAG, "normal msg will be sent");
1001             CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1002             return CA_STATUS_OK;
1003         }
1004         else
1005         {
1006             CADestroyData(data, sizeof(CAData_t));
1007         }
1008         return res;
1009     }
1010     else
1011 #endif // WITH_BWT
1012     {
1013         CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1014     }
1015 #endif // SINGLE_THREAD
1016
1017     return CA_STATUS_OK;
1018 }
1019
1020 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1021                              CAErrorCallback errorHandler)
1022 {
1023     g_requestHandler = ReqHandler;
1024     g_responseHandler = RespHandler;
1025     g_errorHandler = errorHandler;
1026 }
1027
1028 void CASetNetworkMonitorCallback(CANetworkMonitorCallback nwMonitorHandler)
1029 {
1030     g_nwMonitorHandler = nwMonitorHandler;
1031 }
1032
1033 CAResult_t CAInitializeMessageHandler()
1034 {
1035     CASetPacketReceivedCallback(CAReceivedPacketCallback);
1036
1037     CASetNetworkChangeCallback(CANetworkChangedCallback);
1038     CASetErrorHandleCallback(CAErrorHandler);
1039
1040 #ifndef SINGLE_THREAD
1041     // create thread pool
1042     CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1043     if (CA_STATUS_OK != res)
1044     {
1045         OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1046         return res;
1047     }
1048
1049     // send thread initialize
1050     res = CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1051                                      CASendThreadProcess, CADestroyData);
1052     if (CA_STATUS_OK != res)
1053     {
1054         OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1055         ca_thread_pool_free(g_threadPoolHandle);
1056         g_threadPoolHandle = NULL;
1057         return res;
1058     }
1059
1060     // start send thread
1061     res = CAQueueingThreadStart(&g_sendThread);
1062     if (CA_STATUS_OK != res)
1063     {
1064         OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1065         ca_thread_pool_free(g_threadPoolHandle);
1066         g_threadPoolHandle = NULL;
1067         CAQueueingThreadDestroy(&g_sendThread);
1068         return res;
1069     }
1070
1071     // receive thread initialize
1072     res = CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1073                                      CAReceiveThreadProcess, CADestroyData);
1074     if (CA_STATUS_OK != res)
1075     {
1076         OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1077         ca_thread_pool_free(g_threadPoolHandle);
1078         g_threadPoolHandle = NULL;
1079         CAQueueingThreadDestroy(&g_sendThread);
1080         return res;
1081     }
1082
1083 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1084     // start receive thread
1085     res = CAQueueingThreadStart(&g_receiveThread);
1086     if (CA_STATUS_OK != res)
1087     {
1088         OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1089         ca_thread_pool_free(g_threadPoolHandle);
1090         g_threadPoolHandle = NULL;
1091         CAQueueingThreadDestroy(&g_sendThread);
1092         CAQueueingThreadDestroy(&g_receiveThread);
1093         return res;
1094     }
1095 #endif // SINGLE_HANDLE
1096
1097     // retransmission initialize
1098     res = CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle,
1099                                      CASendUnicastData, CATimeoutCallback, NULL);
1100     if (CA_STATUS_OK != res)
1101     {
1102         OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1103         ca_thread_pool_free(g_threadPoolHandle);
1104         g_threadPoolHandle = NULL;
1105         CAQueueingThreadDestroy(&g_sendThread);
1106         CAQueueingThreadDestroy(&g_receiveThread);
1107         return res;
1108     }
1109
1110 #ifdef WITH_BWT
1111     // block-wise transfer initialize
1112     res = CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1113     if (CA_STATUS_OK != res)
1114     {
1115         OIC_LOG(ERROR, TAG, "Failed to Initialize BlockWiseTransfer.");
1116         ca_thread_pool_free(g_threadPoolHandle);
1117         g_threadPoolHandle = NULL;
1118         CAQueueingThreadDestroy(&g_sendThread);
1119         CAQueueingThreadDestroy(&g_receiveThread);
1120         CARetransmissionDestroy(&g_retransmissionContext);
1121         return res;
1122     }
1123 #endif
1124
1125     // start retransmission
1126     res = CARetransmissionStart(&g_retransmissionContext);
1127     if (CA_STATUS_OK != res)
1128     {
1129         OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1130         ca_thread_pool_free(g_threadPoolHandle);
1131         g_threadPoolHandle = NULL;
1132         CAQueueingThreadDestroy(&g_sendThread);
1133         CAQueueingThreadDestroy(&g_receiveThread);
1134         CARetransmissionDestroy(&g_retransmissionContext);
1135         return res;
1136     }
1137
1138     // initialize interface adapters by controller
1139     CAInitializeAdapters(g_threadPoolHandle);
1140 #else
1141     // retransmission initialize
1142     CAResult_t res = CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1143                                                 CATimeoutCallback, NULL);
1144     if (CA_STATUS_OK != res)
1145     {
1146         OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1147         return res;
1148     }
1149
1150     CAInitializeAdapters();
1151 #endif // SINGLE_THREAD
1152
1153     return CA_STATUS_OK;
1154 }
1155
1156 void CATerminateMessageHandler()
1157 {
1158 #ifndef SINGLE_THREAD
1159     CATransportAdapter_t connType;
1160     u_arraylist_t *list = CAGetSelectedNetworkList();
1161     uint32_t length = u_arraylist_length(list);
1162
1163     uint32_t i = 0;
1164     for (i = 0; i < length; i++)
1165     {
1166         void* ptrType = u_arraylist_get(list, i);
1167
1168         if (NULL == ptrType)
1169         {
1170             continue;
1171         }
1172
1173         connType = *(CATransportAdapter_t *)ptrType;
1174         CAStopAdapter(connType);
1175     }
1176
1177     // stop retransmission
1178     if (NULL != g_retransmissionContext.threadMutex)
1179     {
1180         CARetransmissionStop(&g_retransmissionContext);
1181     }
1182
1183     // stop thread
1184     // delete thread data
1185     if (NULL != g_sendThread.threadMutex)
1186     {
1187         CAQueueingThreadStop(&g_sendThread);
1188     }
1189
1190     // stop thread
1191     // delete thread data
1192     if (NULL != g_receiveThread.threadMutex)
1193     {
1194 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1195         CAQueueingThreadStop(&g_receiveThread);
1196 #endif
1197     }
1198
1199     // destroy thread pool
1200     if (NULL != g_threadPoolHandle)
1201     {
1202         ca_thread_pool_free(g_threadPoolHandle);
1203         g_threadPoolHandle = NULL;
1204     }
1205
1206 #ifdef WITH_BWT
1207     CATerminateBlockWiseTransfer();
1208 #endif
1209     CARetransmissionDestroy(&g_retransmissionContext);
1210     CAQueueingThreadDestroy(&g_sendThread);
1211     CAQueueingThreadDestroy(&g_receiveThread);
1212
1213     // terminate interface adapters by controller
1214     CATerminateAdapters();
1215 #else
1216     // terminate interface adapters by controller
1217     CATerminateAdapters();
1218
1219     // stop retransmission
1220     CARetransmissionStop(&g_retransmissionContext);
1221     CARetransmissionDestroy(&g_retransmissionContext);
1222 #endif // SINGLE_THREAD
1223 }
1224
1225 void CALogPDUInfo(coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
1226 {
1227     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1228     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
1229
1230     OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1231
1232 #ifdef WITH_TCP
1233     if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
1234     {
1235         OIC_LOG(DEBUG, TAG, "pdu header data :");
1236         OIC_LOG_BUFFER(DEBUG, TAG,  (const uint8_t *) pdu->hdr, pdu->length);
1237     }
1238     else
1239 #endif
1240     {
1241         OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->coap_hdr_udp_t.type);
1242
1243         OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->coap_hdr_udp_t.code);
1244
1245         OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1246
1247         OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->coap_hdr_udp_t.token,
1248                        pdu->hdr->coap_hdr_udp_t.token_length);
1249     }
1250 }
1251
1252 static void CALogPayloadInfo(CAInfo_t *info)
1253 {
1254     if(info)
1255     {
1256         if (info->options)
1257         {
1258             for (uint32_t i = 0; i < info->numOptions; i++)
1259             {
1260                 OIC_LOG_V(DEBUG, TAG, "optionID: %u", info->options[i].optionID);
1261
1262                 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1263             }
1264         }
1265
1266         if (info->payload)
1267         {
1268             OIC_LOG_V(DEBUG, TAG, "payload: %p(%zu)", info->payload,
1269                       info->payloadSize);
1270         }
1271
1272         if (info->token)
1273         {
1274             OIC_LOG(DEBUG, TAG, "token:");
1275             OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1276                            info->tokenLength);
1277         }
1278         OIC_LOG_V(DEBUG, TAG, "msgID: %u", info->messageId);
1279     }
1280     else
1281     {
1282         OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1283     }
1284 }
1285
1286 void CAErrorHandler(const CAEndpoint_t *endpoint,
1287                     const void *data, uint32_t dataLen,
1288                     CAResult_t result)
1289 {
1290     OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1291
1292 #ifndef SINGLE_THREAD
1293     VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1294     VERIFY_NON_NULL_VOID(data, TAG, "data");
1295
1296     uint32_t code = CA_NOT_FOUND;
1297     //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1298     //Get PDU data
1299     coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
1300     if (NULL == pdu)
1301     {
1302         OIC_LOG(ERROR, TAG, "Parse PDU failed");
1303         return;
1304     }
1305
1306     CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1307     if(!cadata)
1308     {
1309         OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1310         coap_delete_pdu(pdu);
1311         return;
1312     }
1313
1314     cadata->errorInfo->result = result;
1315
1316     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1317     coap_delete_pdu(pdu);
1318 #endif
1319
1320     OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1321     return;
1322 }
1323
1324 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1325 {
1326     OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1327 #ifndef SINGLE_THREAD
1328     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1329     if (!cadata)
1330     {
1331         OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1332         return;
1333     }
1334
1335     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1336     if (!ep)
1337     {
1338         OIC_LOG(ERROR, TAG, "endpoint clone failed");
1339         OICFree(cadata);
1340         return;
1341     }
1342
1343     CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1344     if (!errorInfo)
1345     {
1346         OIC_LOG(ERROR, TAG, "errorInfo memory allocation failed");
1347         OICFree(cadata);
1348         CAFreeEndpoint(ep);
1349         return;
1350     }
1351
1352     CAResult_t res = CACloneInfo(info, &errorInfo->info);
1353     if (CA_STATUS_OK != res)
1354     {
1355         OIC_LOG(ERROR, TAG, "info clone failed");
1356         OICFree(cadata);
1357         OICFree(errorInfo);
1358         CAFreeEndpoint(ep);
1359         return;
1360     }
1361
1362     errorInfo->result = result;
1363     cadata->remoteEndpoint = ep;
1364     cadata->errorInfo = errorInfo;
1365     cadata->dataType = CA_ERROR_DATA;
1366
1367     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1368 #endif
1369     OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");
1370 }