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