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