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