Fixed memory leak issue in CA common logic
[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 CAProcessSendData(const CAData_t *data)
398 {
399     VERIFY_NON_NULL(data, TAG, "data");
400     VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
401
402     CAResult_t res = CA_STATUS_FAILED;
403
404     CASendDataType_t type = data->type;
405
406     coap_pdu_t *pdu = NULL;
407     CAInfo_t *info = NULL;
408     coap_list_t *options = NULL;
409     coap_transport_type transport;
410
411     if (SEND_TYPE_UNICAST == type)
412     {
413         OIC_LOG(DEBUG,TAG,"Unicast message");
414 #ifdef ROUTING_GATEWAY
415         /*
416          * When forwarding a packet, do not attempt retransmission as its the responsibility of
417          * packet originator node
418          */
419         bool skipRetransmission = false;
420 #endif
421
422         if (NULL != data->requestInfo)
423         {
424             OIC_LOG(DEBUG, TAG, "requestInfo is available..");
425
426             info = &data->requestInfo->info;
427 #ifdef ROUTING_GATEWAY
428             skipRetransmission = data->requestInfo->info.skipRetransmission;
429 #endif
430             pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint,
431                                 &options, &transport);
432         }
433         else if (NULL != data->responseInfo)
434         {
435             OIC_LOG(DEBUG, TAG, "responseInfo is available..");
436
437             info = &data->responseInfo->info;
438 #ifdef ROUTING_GATEWAY
439             skipRetransmission = data->responseInfo->info.skipRetransmission;
440 #endif
441             pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
442                                 &options, &transport);
443         }
444         else
445         {
446             OIC_LOG(DEBUG, TAG, "request info, response info is empty");
447             return CA_STATUS_INVALID_PARAM;
448         }
449
450         // interface controller function call.
451         if (NULL != pdu)
452         {
453 #ifdef WITH_BWT
454             if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
455 #ifdef WITH_TCP
456                     && !CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter)
457 #endif
458                     )
459             {
460                 // Blockwise transfer
461                 if (NULL != info)
462                 {
463                     CAResult_t res = CAAddBlockOption(&pdu, info,
464                                                       data->remoteEndpoint,
465                                                       &options);
466                     if (CA_STATUS_OK != res)
467                     {
468                         OIC_LOG(INFO, TAG, "to write block option has failed");
469                         CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
470                         coap_delete_list(options);
471                         coap_delete_pdu(pdu);
472                         return res;
473                     }
474                 }
475             }
476 #endif // WITH_BWT
477             CALogPDUInfo(pdu, data->remoteEndpoint);
478
479             res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
480             if (CA_STATUS_OK != res)
481             {
482                 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
483                 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
484                 coap_delete_list(options);
485                 coap_delete_pdu(pdu);
486                 return res;
487             }
488
489 #ifdef WITH_TCP
490             if (CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter))
491             {
492                 OIC_LOG(INFO, TAG, "retransmission will be not worked");
493             }
494             else
495 #endif
496 #ifdef ROUTING_GATEWAY
497             if(!skipRetransmission)
498 #endif
499             {
500                 // for retransmission
501                 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint,
502                                                pdu->hdr, pdu->length);
503                 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
504                 {
505                     //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
506                     OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
507                     coap_delete_list(options);
508                     coap_delete_pdu(pdu);
509                     return res;
510                 }
511             }
512
513             coap_delete_list(options);
514             coap_delete_pdu(pdu);
515         }
516         else
517         {
518             OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
519             CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
520             return CA_SEND_FAILED;
521         }
522     }
523     else if (SEND_TYPE_MULTICAST == type)
524     {
525         OIC_LOG(DEBUG,TAG,"Multicast message");
526         if (NULL != data->requestInfo)
527         {
528             OIC_LOG(DEBUG, TAG, "requestInfo is available..");
529
530             info = &data->requestInfo->info;
531             pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);
532
533             if (NULL != pdu)
534             {
535 #ifdef WITH_BWT
536                 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
537 #ifdef WITH_TCP
538                         && !CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter)
539 #endif
540                         )
541                 {
542                     // Blockwise transfer
543                     CAResult_t res = CAAddBlockOption(&pdu, info,
544                                                       data->remoteEndpoint,
545                                                       &options);
546                     if (CA_STATUS_OK != res)
547                     {
548                         OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
549                         CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
550                         coap_delete_list(options);
551                         coap_delete_pdu(pdu);
552                         return res;
553                     }
554                 }
555 #endif // WITH_BWT
556             }
557             else
558             {
559                 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
560                 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
561                 return CA_SEND_FAILED;
562             }
563         }
564         else if (NULL != data->responseInfo)
565         {
566             OIC_LOG(DEBUG, TAG, "responseInfo is available..");
567
568             info = &data->responseInfo->info;
569             pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
570                                 &options, &transport);
571
572             if (NULL != pdu)
573             {
574 #ifdef WITH_BWT
575                 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
576 #ifdef WITH_TCP
577                         && !CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter)
578 #endif
579                         )
580                 {
581                     // Blockwise transfer
582                     if (NULL != info)
583                     {
584                         CAResult_t res = CAAddBlockOption(&pdu, info,
585                                                           data->remoteEndpoint,
586                                                           &options);
587                         if (CA_STATUS_OK != res)
588                         {
589                             OIC_LOG(INFO, TAG, "to write block option has failed");
590                             CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
591                             coap_delete_list(options);
592                             coap_delete_pdu(pdu);
593                             return res;
594                         }
595                     }
596                 }
597 #endif // WITH_BWT
598             }
599             else
600             {
601                 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
602                 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
603                 return CA_SEND_FAILED;
604             }
605         }
606         else
607         {
608             OIC_LOG(ERROR, TAG, "request or response info is empty");
609             return CA_SEND_FAILED;
610         }
611
612         CALogPDUInfo(pdu, data->remoteEndpoint);
613
614         OIC_LOG(DEBUG, TAG, "pdu to send :");
615         OIC_LOG_BUFFER(DEBUG, TAG,  (uint8_t*)pdu->hdr, pdu->length);
616
617         res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
618         if (CA_STATUS_OK != res)
619         {
620             OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
621             CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
622             coap_delete_list(options);
623             coap_delete_pdu(pdu);
624             return res;
625         }
626
627         coap_delete_list(options);
628         coap_delete_pdu(pdu);
629     }
630
631     return CA_STATUS_OK;
632 }
633
634 #ifndef SINGLE_THREAD
635 static void CASendThreadProcess(void *threadData)
636 {
637     CAData_t *data = (CAData_t *) threadData;
638     CAProcessSendData(data);
639 }
640 #endif
641
642 /*
643  * If a second message arrives with the same message ID, token and the other address
644  * family, drop it.  Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
645  */
646 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id,
647                                 CAToken_t token, uint8_t tokenLength)
648 {
649     if (!ep)
650     {
651         return true;
652     }
653     if (ep->adapter != CA_ADAPTER_IP)
654     {
655         return false;
656     }
657     if (!caglobals.ip.dualstack)
658     {
659         return false;
660     }
661
662     if (tokenLength > CA_MAX_TOKEN_LEN)
663     {
664         /*
665          * If token length is more than CA_MAX_TOKEN_LEN,
666          * we compare the first CA_MAX_TOKEN_LEN bytes only.
667          */
668         tokenLength = CA_MAX_TOKEN_LEN;
669     }
670
671     bool ret = false;
672     CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
673
674     for (size_t i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
675     {
676         CAHistoryItem_t *item = &(history->items[i]);
677         if (id == item->messageId && tokenLength == item->tokenLength
678             && memcmp(item->token, token, tokenLength) == 0)
679         {
680             if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
681             {
682                 OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
683                           familyFlags & CA_IPV6 ? '6' : '4');
684                 ret = true;
685                 break;
686             }
687         }
688     }
689
690     history->items[history->nextIndex].flags = familyFlags;
691     history->items[history->nextIndex].messageId = id;
692     if (token && tokenLength)
693     {
694         memcpy(history->items[history->nextIndex].token, token, tokenLength);
695         history->items[history->nextIndex].tokenLength = tokenLength;
696     }
697
698     if (++history->nextIndex >= HISTORYSIZE)
699     {
700         history->nextIndex = 0;
701     }
702
703     return ret;
704 }
705
706 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
707                                      const void *data, uint32_t dataLen)
708 {
709     VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
710     VERIFY_NON_NULL_VOID(data, TAG, "data");
711
712     OIC_LOG(DEBUG, TAG, "received pdu data :");
713     OIC_LOG_BUFFER(DEBUG, TAG,  data, dataLen);
714
715     uint32_t code = CA_NOT_FOUND;
716     CAData_t *cadata = NULL;
717
718     coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code,
719                                                 &(sep->endpoint));
720     if (NULL == pdu)
721     {
722         OIC_LOG(ERROR, TAG, "Parse PDU failed");
723         return;
724     }
725
726     OIC_LOG_V(DEBUG, TAG, "code = %d", code);
727     if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
728     {
729         cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
730         if (!cadata)
731         {
732             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
733             coap_delete_pdu(pdu);
734             return;
735         }
736     }
737     else
738     {
739         cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
740         if (!cadata)
741         {
742             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
743             coap_delete_pdu(pdu);
744             return;
745         }
746
747 #ifdef WITH_TCP
748         if (CAIsSupportedCoAPOverTCP(sep->endpoint.adapter))
749         {
750             OIC_LOG(INFO, TAG, "retransmission is not supported");
751         }
752         else
753 #endif
754         {
755             // for retransmission
756             void *retransmissionPdu = NULL;
757             CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
758                                          pdu->length, &retransmissionPdu);
759
760 #ifndef WITH_BWT
761             // get token from saved data in retransmission list
762             if (retransmissionPdu && CA_EMPTY == code)
763             {
764                 if (cadata->responseInfo)
765                 {
766                     CAInfo_t *info = &cadata->responseInfo->info;
767                     CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
768                                                        info, &(sep->endpoint));
769                     if (CA_STATUS_OK != res)
770                     {
771                         OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
772                         OICFree(info->token);
773                         info->tokenLength = 0;
774                     }
775                 }
776             }
777 #endif
778             OICFree(retransmissionPdu);
779         }
780     }
781
782     cadata->type = SEND_TYPE_UNICAST;
783
784 #ifdef SINGLE_THREAD
785     CAProcessReceivedData(cadata);
786 #else
787 #ifdef WITH_BWT
788     if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter
789 #ifdef WITH_TCP
790             && !CAIsSupportedCoAPOverTCP(sep->endpoint.adapter)
791 #endif
792             )
793     {
794         CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
795         if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
796         {
797             OIC_LOG(ERROR, TAG, "this message does not have block option");
798             CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
799         }
800         else
801         {
802             CADestroyData(cadata, sizeof(CAData_t));
803         }
804     }
805     else
806 #endif
807     {
808         CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
809     }
810 #endif // SINGLE_THREAD
811
812     coap_delete_pdu(pdu);
813 }
814
815 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
816 {
817     (void)info;
818     (void)status;
819
820     if (g_nwMonitorHandler)
821     {
822         g_nwMonitorHandler(info, status);
823     }
824 }
825
826 void CAHandleRequestResponseCallbacks()
827 {
828 #ifdef SINGLE_THREAD
829     CAReadData();
830     CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
831 #else
832 #ifdef SINGLE_HANDLE
833     // parse the data and call the callbacks.
834     // #1 parse the data
835     // #2 get endpoint
836
837     ca_mutex_lock(g_receiveThread.threadMutex);
838
839     u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
840
841     ca_mutex_unlock(g_receiveThread.threadMutex);
842
843     if (NULL == item || NULL == item->msg)
844     {
845         return;
846     }
847
848     // get endpoint
849     CAData_t *td = (CAData_t *) item->msg;
850
851     if (td->requestInfo && g_requestHandler)
852     {
853         OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
854         g_requestHandler(td->remoteEndpoint, td->requestInfo);
855     }
856     else if (td->responseInfo && g_responseHandler)
857     {
858         OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
859         g_responseHandler(td->remoteEndpoint, td->responseInfo);
860     }
861     else if (td->errorInfo && g_errorHandler)
862     {
863         OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
864         g_errorHandler(td->remoteEndpoint, td->errorInfo);
865     }
866
867     CADestroyData(item->msg, sizeof(CAData_t));
868     OICFree(item);
869
870 #endif // SINGLE_HANDLE
871 #endif // SINGLE_THREAD
872 }
873
874 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
875                                    CADataType_t dataType)
876 {
877     OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
878
879     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
880     if (!cadata)
881     {
882         OIC_LOG(ERROR, TAG, "memory allocation failed");
883         return NULL;
884     }
885
886     if(CA_REQUEST_DATA == dataType)
887     {
888         // clone request info
889         CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
890
891         if(!request)
892         {
893             OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
894             OICFree(cadata);
895             return NULL;
896         }
897
898         cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
899         cadata->requestInfo =  request;
900     }
901     else if(CA_RESPONSE_DATA == dataType)
902     {
903         // clone response info
904         CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
905
906         if(!response)
907         {
908             OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
909             OICFree(cadata);
910             return NULL;
911         }
912
913         cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
914         cadata->responseInfo = response;
915     }
916     else
917     {
918         OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
919         OICFree(cadata);
920         return NULL;
921     }
922
923     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
924     if (!ep)
925     {
926         OIC_LOG(ERROR, TAG, "endpoint clone failed");
927         CADestroyData(cadata, sizeof(CAData_t));
928         return NULL;
929     }
930
931     cadata->remoteEndpoint = ep;
932     cadata->dataType = dataType;
933     return cadata;
934 }
935
936 CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg,
937                                CADataType_t dataType)
938 {
939     VERIFY_NON_NULL(endpoint, TAG, "endpoint");
940     VERIFY_NON_NULL(sendMsg, TAG, "sendMsg");
941
942     if (false == CAIsSelectedNetworkAvailable())
943     {
944         return CA_STATUS_FAILED;
945     }
946
947 #ifdef ARDUINO
948     // If max retransmission queue is reached, then don't handle new request
949     if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
950     {
951         OIC_LOG(ERROR, TAG, "max RT queue size reached!");
952         return CA_SEND_FAILED;
953     }
954 #endif // ARDUINO
955
956     CAData_t *data = CAPrepareSendData(endpoint, sendMsg, dataType);
957     if(!data)
958     {
959         OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
960         return CA_MEMORY_ALLOC_FAILED;
961     }
962
963 #ifdef SINGLE_THREAD
964     CAResult_t result = CAProcessSendData(data);
965     if(CA_STATUS_OK != result)
966     {
967         OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
968         CADestroyData(data, sizeof(CAData_t));
969         return result;
970     }
971
972     CADestroyData(data, sizeof(CAData_t));
973 #else
974 #ifdef WITH_BWT
975     if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
976 #ifdef WITH_TCP
977             && !CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter)
978 #endif
979             )
980     {
981         // send block data
982         CAResult_t res = CASendBlockWiseData(data);
983         if(CA_NOT_SUPPORTED == res)
984         {
985             OIC_LOG(DEBUG, TAG, "normal msg will be sent");
986             CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
987             return CA_STATUS_OK;
988         }
989         else
990         {
991             CADestroyData(data, sizeof(CAData_t));
992         }
993         return res;
994     }
995     else
996 #endif // WITH_BWT
997     {
998         CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
999     }
1000 #endif // SINGLE_THREAD
1001
1002     return CA_STATUS_OK;
1003 }
1004
1005 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
1006                                       uint8_t tokenLength, const CAHeaderOption_t *options,
1007                                       uint8_t numOptions)
1008 {
1009     (void)resourceUri;
1010     (void)token;
1011     (void)tokenLength;
1012     (void)options;
1013     (void)numOptions;
1014     return CA_NOT_SUPPORTED;
1015 }
1016
1017 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1018                              CAErrorCallback errorHandler)
1019 {
1020     g_requestHandler = ReqHandler;
1021     g_responseHandler = RespHandler;
1022     g_errorHandler = errorHandler;
1023 }
1024
1025 void CASetNetworkMonitorCallback(CANetworkMonitorCallback nwMonitorHandler)
1026 {
1027     g_nwMonitorHandler = nwMonitorHandler;
1028 }
1029
1030 CAResult_t CAInitializeMessageHandler()
1031 {
1032     CASetPacketReceivedCallback(CAReceivedPacketCallback);
1033
1034     CASetNetworkChangeCallback(CANetworkChangedCallback);
1035     CASetErrorHandleCallback(CAErrorHandler);
1036
1037 #ifndef SINGLE_THREAD
1038     // create thread pool
1039     CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1040     if (CA_STATUS_OK != res)
1041     {
1042         OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1043         return res;
1044     }
1045
1046     // send thread initialize
1047     res = CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1048                                      CASendThreadProcess, CADestroyData);
1049     if (CA_STATUS_OK != res)
1050     {
1051         OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1052         ca_thread_pool_free(g_threadPoolHandle);
1053         g_threadPoolHandle = NULL;
1054         return res;
1055     }
1056
1057     // start send thread
1058     res = CAQueueingThreadStart(&g_sendThread);
1059     if (CA_STATUS_OK != res)
1060     {
1061         OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1062         ca_thread_pool_free(g_threadPoolHandle);
1063         g_threadPoolHandle = NULL;
1064         CAQueueingThreadDestroy(&g_sendThread);
1065         return res;
1066     }
1067
1068     // receive thread initialize
1069     res = CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1070                                      CAReceiveThreadProcess, CADestroyData);
1071     if (CA_STATUS_OK != res)
1072     {
1073         OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1074         ca_thread_pool_free(g_threadPoolHandle);
1075         g_threadPoolHandle = NULL;
1076         CAQueueingThreadDestroy(&g_sendThread);
1077         return res;
1078     }
1079
1080 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1081     // start receive thread
1082     res = CAQueueingThreadStart(&g_receiveThread);
1083     if (CA_STATUS_OK != res)
1084     {
1085         OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1086         ca_thread_pool_free(g_threadPoolHandle);
1087         g_threadPoolHandle = NULL;
1088         CAQueueingThreadDestroy(&g_sendThread);
1089         CAQueueingThreadDestroy(&g_receiveThread);
1090         return res;
1091     }
1092 #endif // SINGLE_HANDLE
1093
1094     // retransmission initialize
1095     res = CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle,
1096                                      CASendUnicastData, CATimeoutCallback, NULL);
1097     if (CA_STATUS_OK != res)
1098     {
1099         OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1100         ca_thread_pool_free(g_threadPoolHandle);
1101         g_threadPoolHandle = NULL;
1102         CAQueueingThreadDestroy(&g_sendThread);
1103         CAQueueingThreadDestroy(&g_receiveThread);
1104         return res;
1105     }
1106
1107 #ifdef WITH_BWT
1108     // block-wise transfer initialize
1109     res = CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1110     if (CA_STATUS_OK != res)
1111     {
1112         OIC_LOG(ERROR, TAG, "Failed to Initialize BlockWiseTransfer.");
1113         ca_thread_pool_free(g_threadPoolHandle);
1114         g_threadPoolHandle = NULL;
1115         CAQueueingThreadDestroy(&g_sendThread);
1116         CAQueueingThreadDestroy(&g_receiveThread);
1117         CARetransmissionDestroy(&g_retransmissionContext);
1118         return res;
1119     }
1120 #endif
1121
1122     // start retransmission
1123     res = CARetransmissionStart(&g_retransmissionContext);
1124     if (CA_STATUS_OK != res)
1125     {
1126         OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1127         ca_thread_pool_free(g_threadPoolHandle);
1128         g_threadPoolHandle = NULL;
1129         CAQueueingThreadDestroy(&g_sendThread);
1130         CAQueueingThreadDestroy(&g_receiveThread);
1131         CARetransmissionDestroy(&g_retransmissionContext);
1132         return res;
1133     }
1134
1135     // initialize interface adapters by controller
1136     CAInitializeAdapters(g_threadPoolHandle);
1137 #else
1138     // retransmission initialize
1139     CAResult_t res = CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1140                                                 CATimeoutCallback, NULL);
1141     if (CA_STATUS_OK != res)
1142     {
1143         OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1144         return res;
1145     }
1146
1147     CAInitializeAdapters();
1148 #endif // SINGLE_THREAD
1149
1150     return CA_STATUS_OK;
1151 }
1152
1153 void CATerminateMessageHandler()
1154 {
1155 #ifndef SINGLE_THREAD
1156     CATransportAdapter_t connType;
1157     u_arraylist_t *list = CAGetSelectedNetworkList();
1158     uint32_t length = u_arraylist_length(list);
1159
1160     uint32_t i = 0;
1161     for (i = 0; i < length; i++)
1162     {
1163         void* ptrType = u_arraylist_get(list, i);
1164
1165         if (NULL == ptrType)
1166         {
1167             continue;
1168         }
1169
1170         connType = *(CATransportAdapter_t *)ptrType;
1171         CAStopAdapter(connType);
1172     }
1173
1174     // stop retransmission
1175     if (NULL != g_retransmissionContext.threadMutex)
1176     {
1177         CARetransmissionStop(&g_retransmissionContext);
1178     }
1179
1180     // stop thread
1181     // delete thread data
1182     if (NULL != g_sendThread.threadMutex)
1183     {
1184         CAQueueingThreadStop(&g_sendThread);
1185     }
1186
1187     // stop thread
1188     // delete thread data
1189     if (NULL != g_receiveThread.threadMutex)
1190     {
1191 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1192         CAQueueingThreadStop(&g_receiveThread);
1193 #endif
1194     }
1195
1196     // destroy thread pool
1197     if (NULL != g_threadPoolHandle)
1198     {
1199         ca_thread_pool_free(g_threadPoolHandle);
1200         g_threadPoolHandle = NULL;
1201     }
1202
1203 #ifdef WITH_BWT
1204     CATerminateBlockWiseTransfer();
1205 #endif
1206     CARetransmissionDestroy(&g_retransmissionContext);
1207     CAQueueingThreadDestroy(&g_sendThread);
1208     CAQueueingThreadDestroy(&g_receiveThread);
1209
1210     // terminate interface adapters by controller
1211     CATerminateAdapters();
1212 #else
1213     // terminate interface adapters by controller
1214     CATerminateAdapters();
1215
1216     // stop retransmission
1217     CARetransmissionStop(&g_retransmissionContext);
1218     CARetransmissionDestroy(&g_retransmissionContext);
1219 #endif // SINGLE_THREAD
1220 }
1221
1222 void CALogPDUInfo(coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
1223 {
1224     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1225     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
1226
1227     OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1228
1229 #ifdef WITH_TCP
1230     if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
1231     {
1232         OIC_LOG(DEBUG, TAG, "pdu header data :");
1233         OIC_LOG_BUFFER(DEBUG, TAG,  (const uint8_t *) pdu->hdr, pdu->length);
1234     }
1235     else
1236 #endif
1237     {
1238         OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->coap_hdr_udp_t.type);
1239
1240         OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->coap_hdr_udp_t.code);
1241
1242         OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1243
1244         OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->coap_hdr_udp_t.token,
1245                        pdu->hdr->coap_hdr_udp_t.token_length);
1246     }
1247 }
1248
1249 static void CALogPayloadInfo(CAInfo_t *info)
1250 {
1251     if(info)
1252     {
1253         if (info->options)
1254         {
1255             for (uint32_t i = 0; i < info->numOptions; i++)
1256             {
1257                 OIC_LOG_V(DEBUG, TAG, "optionID: %u", info->options[i].optionID);
1258
1259                 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1260             }
1261         }
1262
1263         if (info->payload)
1264         {
1265             OIC_LOG_V(DEBUG, TAG, "payload: %p(%zu)", info->payload,
1266                       info->payloadSize);
1267         }
1268
1269         if (info->token)
1270         {
1271             OIC_LOG(DEBUG, TAG, "token:");
1272             OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1273                            info->tokenLength);
1274         }
1275         OIC_LOG_V(DEBUG, TAG, "msgID: %u", info->messageId);
1276     }
1277     else
1278     {
1279         OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1280     }
1281 }
1282
1283 void CAErrorHandler(const CAEndpoint_t *endpoint,
1284                     const void *data, uint32_t dataLen,
1285                     CAResult_t result)
1286 {
1287     OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1288
1289 #ifndef SINGLE_THREAD
1290     VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1291     VERIFY_NON_NULL_VOID(data, TAG, "data");
1292
1293     uint32_t code = CA_NOT_FOUND;
1294     //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1295     //Get PDU data
1296     coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
1297     if (NULL == pdu)
1298     {
1299         OIC_LOG(ERROR, TAG, "Parse PDU failed");
1300         return;
1301     }
1302
1303     CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1304     if(!cadata)
1305     {
1306         OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1307         coap_delete_pdu(pdu);
1308         return;
1309     }
1310
1311     cadata->errorInfo->result = result;
1312
1313     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1314     coap_delete_pdu(pdu);
1315 #endif
1316
1317     OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1318     return;
1319 }
1320
1321 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1322 {
1323     OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1324 #ifndef SINGLE_THREAD
1325     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1326     if (!cadata)
1327     {
1328         OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1329         return;
1330     }
1331
1332     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1333     if (!ep)
1334     {
1335         OIC_LOG(ERROR, TAG, "endpoint clone failed");
1336         OICFree(cadata);
1337         return;
1338     }
1339
1340     CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1341     if (!errorInfo)
1342     {
1343         OIC_LOG(ERROR, TAG, "errorInfo memory allocation failed");
1344         OICFree(cadata);
1345         CAFreeEndpoint(ep);
1346         return;
1347     }
1348
1349     CAResult_t res = CACloneInfo(info, &errorInfo->info);
1350     if (CA_STATUS_OK != res)
1351     {
1352         OIC_LOG(ERROR, TAG, "info clone failed");
1353         OICFree(cadata);
1354         OICFree(errorInfo);
1355         CAFreeEndpoint(ep);
1356         return;
1357     }
1358
1359     errorInfo->result = result;
1360     cadata->remoteEndpoint = ep;
1361     cadata->errorInfo = errorInfo;
1362     cadata->dataType = CA_ERROR_DATA;
1363
1364     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1365 #endif
1366     OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");
1367 }