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