Update RD with latest master
[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 "CA_MSG_HNDLR"
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     OIC_LOG(DEBUG, TAG, "IN");
93     VERIFY_NON_NULL_VOID(data, TAG, "data");
94
95     // add thread
96     CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
97
98     OIC_LOG(DEBUG, TAG, "OUT");
99 }
100
101 void CAAddDataToReceiveThread(CAData_t *data)
102 {
103     OIC_LOG(DEBUG, TAG, "IN - CAAddDataToReceiveThread");
104     VERIFY_NON_NULL_VOID(data, TAG, "data");
105
106     // add thread
107     CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
108
109     OIC_LOG(DEBUG, TAG, "OUT - CAAddDataToReceiveThread");
110 }
111 #endif
112
113 static bool CAIsSelectedNetworkAvailable()
114 {
115     u_arraylist_t *list = CAGetSelectedNetworkList();
116     if (!list || u_arraylist_length(list) == 0)
117     {
118         OIC_LOG(ERROR, TAG, "No selected network");
119         return false;
120     }
121
122     return true;
123 }
124
125 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
126                                        const CARemoteId_t *identity,
127                                        const void *data, CADataType_t dataType)
128 {
129     OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN");
130     CAInfo_t *info = NULL;
131     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
132     if (!cadata)
133     {
134         OIC_LOG(ERROR, TAG, "memory allocation failed");
135         return NULL;
136     }
137
138     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
139     if (!ep)
140     {
141         OIC_LOG(ERROR, TAG, "endpoint clone failed");
142         OICFree(cadata);
143         return NULL;
144     }
145
146     OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
147     CAResult_t result;
148
149     if(CA_RESPONSE_DATA == dataType)
150     {
151         CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
152         if (!resInfo)
153         {
154             OIC_LOG(ERROR, TAG, "memory allocation failed");
155             OICFree(cadata);
156             CAFreeEndpoint(ep);
157             return NULL;
158         }
159
160         result = CAGetResponseInfoFromPDU(data, resInfo, endpoint);
161         if (CA_STATUS_OK != result)
162         {
163             OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed");
164             CAFreeEndpoint(ep);
165             CADestroyResponseInfoInternal(resInfo);
166             OICFree(cadata);
167             return NULL;
168         }
169         cadata->responseInfo = resInfo;
170         info = &resInfo->info;
171         if (identity)
172         {
173             info->identity = *identity;
174         }
175         OIC_LOG(DEBUG, TAG, "Response Info :");
176         CALogPayloadInfo(info);
177     }
178     else if (CA_REQUEST_DATA == dataType)
179     {
180         CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t));
181         if (!reqInfo)
182         {
183             OIC_LOG(ERROR, TAG, "memory allocation failed");
184             OICFree(cadata);
185             CAFreeEndpoint(ep);
186             return NULL;
187         }
188
189         result = CAGetRequestInfoFromPDU(data, endpoint, reqInfo);
190         if (CA_STATUS_OK != result)
191         {
192             OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed");
193             CAFreeEndpoint(ep);
194             CADestroyRequestInfoInternal(reqInfo);
195             OICFree(cadata);
196             return NULL;
197         }
198
199         if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId,
200                                 reqInfo->info.token, reqInfo->info.tokenLength))
201         {
202             OIC_LOG(ERROR, TAG, "Second Request with same Token, Drop it");
203             CAFreeEndpoint(ep);
204             CADestroyRequestInfoInternal(reqInfo);
205             OICFree(cadata);
206             return NULL;
207         }
208
209         cadata->requestInfo = reqInfo;
210         info = &reqInfo->info;
211         if (identity)
212         {
213             info->identity = *identity;
214         }
215         OIC_LOG(DEBUG, TAG, "Request Info :");
216         CALogPayloadInfo(info);
217    }
218     else if (CA_ERROR_DATA == dataType)
219     {
220         CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
221         if (!errorInfo)
222         {
223             OIC_LOG(ERROR, TAG, "Memory allocation failed!");
224             OICFree(cadata);
225             CAFreeEndpoint(ep);
226             return NULL;
227         }
228
229         CAResult_t result = CAGetErrorInfoFromPDU(data, endpoint, errorInfo);
230         if (CA_STATUS_OK != result)
231         {
232             OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed");
233             CAFreeEndpoint(ep);
234             OICFree(errorInfo);
235             OICFree(cadata);
236             return NULL;
237         }
238
239         cadata->errorInfo = errorInfo;
240         info = &errorInfo->info;
241         if (identity)
242         {
243             info->identity = *identity;
244         }
245         OIC_LOG(DEBUG, TAG, "error Info :");
246         CALogPayloadInfo(info);
247     }
248
249     cadata->remoteEndpoint = ep;
250     cadata->dataType = dataType;
251
252     return cadata;
253
254     OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
255 }
256
257 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
258 {
259     OIC_LOG(DEBUG, TAG, "IN");
260     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
261     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
262
263     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
264     if (!ep)
265     {
266         OIC_LOG(ERROR, TAG, "clone failed");
267         return;
268     }
269
270     CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
271
272     if (!resInfo)
273     {
274         OIC_LOG(ERROR, TAG, "calloc failed");
275         CAFreeEndpoint(ep);
276         return;
277     }
278
279     resInfo->result = CA_RETRANSMIT_TIMEOUT;
280     resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
281     resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
282
283     CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info),
284                                        endpoint);
285     if (CA_STATUS_OK != res)
286     {
287         OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
288         CADestroyResponseInfoInternal(resInfo);
289         CAFreeEndpoint(ep);
290         return;
291     }
292
293     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
294     if (NULL == cadata)
295     {
296         OIC_LOG(ERROR, TAG, "memory allocation failed !");
297         CAFreeEndpoint(ep);
298         CADestroyResponseInfoInternal(resInfo);
299         return;
300     }
301
302     cadata->type = SEND_TYPE_UNICAST;
303     cadata->remoteEndpoint = ep;
304     cadata->requestInfo = NULL;
305     cadata->responseInfo = resInfo;
306
307 #ifdef SINGLE_THREAD
308     CAProcessReceivedData(cadata);
309 #else
310     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
311 #endif
312
313     OIC_LOG(DEBUG, TAG, "OUT");
314 }
315
316 static void CADestroyData(void *data, uint32_t size)
317 {
318     OIC_LOG(DEBUG, TAG, "CADestroyData IN");
319     if ((size_t)size < sizeof(CAData_t))
320     {
321         OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
322     }
323     CAData_t *cadata = (CAData_t *) data;
324
325     if (NULL == cadata)
326     {
327         OIC_LOG(ERROR, TAG, "cadata is NULL");
328         return;
329     }
330
331     if (NULL != cadata->remoteEndpoint)
332     {
333         CAFreeEndpoint(cadata->remoteEndpoint);
334     }
335
336     if (NULL != cadata->requestInfo)
337     {
338         CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
339     }
340
341     if (NULL != cadata->responseInfo)
342     {
343         CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
344     }
345
346     if (NULL != cadata->errorInfo)
347     {
348         CADestroyErrorInfoInternal(cadata->errorInfo);
349     }
350
351     OICFree(cadata);
352     OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
353 }
354
355 #ifdef SINGLE_THREAD
356 static void CAProcessReceivedData(CAData_t *data)
357 {
358     OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
359     if (!data)
360     {
361         OIC_LOG(ERROR, TAG, "thread data error!!");
362         return;
363     }
364
365     // parse the data and call the callbacks.
366     // #1 parse the data
367     // #2 get endpoint
368     CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
369     if (!rep)
370     {
371         OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
372         return;
373     }
374
375     if (data->requestInfo && g_requestHandler)
376     {
377         g_requestHandler(rep, data->requestInfo);
378     }
379     else if (data->responseInfo && g_responseHandler)
380     {
381         g_responseHandler(rep, data->responseInfo);
382     }
383     else if (data->errorInfo && g_errorHandler)
384     {
385         g_errorHandler(rep, data->errorInfo);
386     }
387
388 #ifdef SINGLE_THREAD
389     CADestroyData(data, sizeof(CAData_t));
390 #endif
391
392     OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
393 }
394 #endif
395
396 #ifndef SINGLE_THREAD
397
398 static void CAReceiveThreadProcess(void *threadData)
399 {
400     OIC_LOG(DEBUG, TAG, "IN");
401 #ifndef SINGLE_HANDLE
402     CAData_t *data = (CAData_t *) threadData;
403     CAProcessReceivedData(data);
404 #else
405     (void)threadData;
406 #endif
407     OIC_LOG(DEBUG, TAG, "OUT");
408 }
409 #endif
410
411 static CAResult_t CAProcessSendData(const CAData_t *data)
412 {
413     OIC_LOG(DEBUG, TAG, "IN");
414     VERIFY_NON_NULL(data, TAG, "data");
415     VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
416
417     CAResult_t res = CA_STATUS_FAILED;
418
419     CASendDataType_t type = data->type;
420
421     coap_pdu_t *pdu = NULL;
422     CAInfo_t *info = NULL;
423
424     if (SEND_TYPE_UNICAST == type)
425     {
426         OIC_LOG(DEBUG,TAG,"Unicast message");
427 #ifdef ROUTING_GATEWAY
428         /*
429          * When forwarding a packet, do not attempt retransmission as its the responsibility of
430          * packet originator node
431          */
432         bool skipRetransmission = false;
433 #endif
434
435         if (NULL != data->requestInfo)
436         {
437             OIC_LOG(DEBUG, TAG, "requestInfo is available..");
438
439             info = &data->requestInfo->info;
440 #ifdef ROUTING_GATEWAY
441             skipRetransmission = data->requestInfo->info.skipRetransmission;
442 #endif
443             pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint);
444         }
445         else if (NULL != data->responseInfo)
446         {
447             OIC_LOG(DEBUG, TAG, "responseInfo is available..");
448
449             info = &data->responseInfo->info;
450 #ifdef ROUTING_GATEWAY
451             skipRetransmission = data->responseInfo->info.skipRetransmission;
452 #endif
453             pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint);
454         }
455         else
456         {
457             OIC_LOG(DEBUG, TAG, "request info, response info is empty");
458             return CA_STATUS_INVALID_PARAM;
459         }
460
461         // interface controller function call.
462         if (NULL != pdu)
463         {
464 #ifdef WITH_BWT
465             if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
466 #ifdef TCP_ADAPTER
467                     && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
468 #endif
469                     )
470             {
471                 // Blockwise transfer
472                 if (NULL != info)
473                 {
474                     CAResult_t res = CAAddBlockOption(&pdu, info,
475                                                       data->remoteEndpoint);
476                     if (CA_STATUS_OK != res)
477                     {
478                         OIC_LOG(INFO, TAG, "to write block option has failed");
479                         CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
480                         coap_delete_pdu(pdu);
481                         return res;
482                     }
483                 }
484             }
485 #endif
486             CALogPDUInfo(pdu, data->remoteEndpoint);
487
488             res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
489             if (CA_STATUS_OK != res)
490             {
491                 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
492                 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
493                 coap_delete_pdu(pdu);
494                 return res;
495             }
496
497 #ifdef TCP_ADAPTER
498             if (CA_ADAPTER_TCP == data->remoteEndpoint->adapter)
499             {
500                 OIC_LOG(INFO, TAG, "retransmission will be not worked");
501             }
502             else
503 #endif
504 #ifdef ROUTING_GATEWAY
505             if(!skipRetransmission)
506 #endif
507             {
508                 // for retransmission
509                 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint,
510                                                pdu->hdr, pdu->length);
511                 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
512                 {
513                     //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
514                     OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
515                     coap_delete_pdu(pdu);
516                     return res;
517                 }
518             }
519
520             coap_delete_pdu(pdu);
521         }
522         else
523         {
524             OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
525             CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
526             return CA_SEND_FAILED;
527         }
528     }
529     else if (SEND_TYPE_MULTICAST == type)
530     {
531         OIC_LOG(DEBUG,TAG,"Multicast message");
532         if (NULL != data->requestInfo)
533         {
534             OIC_LOG(DEBUG, TAG, "requestInfo is available..");
535
536             info = &data->requestInfo->info;
537             pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint);
538             if (NULL != pdu)
539             {
540 #ifdef WITH_BWT
541                 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
542 #ifdef TCP_ADAPTER
543                         && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
544 #endif
545                         )
546                 {
547                     // Blockwise transfer
548                     CAResult_t res = CAAddBlockOption(&pdu, &data->requestInfo->info,
549                                                       data->remoteEndpoint);
550                     if (CA_STATUS_OK != res)
551                     {
552                         OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
553                         CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
554                         coap_delete_pdu(pdu);
555                         return res;
556                     }
557                 }
558 #endif
559             }
560             else
561             {
562                 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
563                 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
564                 return CA_SEND_FAILED;
565             }
566         }
567         else if (NULL != data->responseInfo)
568         {
569             OIC_LOG(DEBUG, TAG, "responseInfo is available..");
570
571             info = &data->responseInfo->info;
572             pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint);
573
574             if (NULL != pdu)
575             {
576 #ifdef WITH_BWT
577                 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
578 #ifdef TCP_ADAPTER
579                         && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
580 #endif
581                         )
582                 {
583                     // Blockwise transfer
584                     if (NULL != info)
585                     {
586                         CAResult_t res = CAAddBlockOption(&pdu, info,
587                                                           data->remoteEndpoint);
588                         if (CA_STATUS_OK != res)
589                         {
590                             OIC_LOG(INFO, TAG, "to write block option has failed");
591                             CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
592                             coap_delete_pdu(pdu);
593                             return res;
594                         }
595                     }
596                 }
597 #endif
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,  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_pdu(pdu);
623             return res;
624         }
625
626         coap_delete_pdu(pdu);
627     }
628
629     return CA_STATUS_OK;
630 }
631
632 #ifndef SINGLE_THREAD
633 static void CASendThreadProcess(void *threadData)
634 {
635     CAData_t *data = (CAData_t *) threadData;
636     CAProcessSendData(data);
637 }
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     OIC_LOG(DEBUG, TAG, "IN");
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 TCP_ADAPTER
748         if (CA_ADAPTER_TCP == 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 TCP_ADAPTER
788             && CA_ADAPTER_TCP != sep->endpoint.adapter
789 #endif
790             )
791     {
792         CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
793         if (CA_NOT_SUPPORTED == 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
809
810     coap_delete_pdu(pdu);
811
812     OIC_LOG(DEBUG, TAG, "OUT");
813 }
814
815 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
816 {
817     (void)info;
818     (void)status;
819     OIC_LOG(DEBUG, TAG, "IN");
820
821     OIC_LOG(DEBUG, TAG, "OUT");
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)
842     {
843         return;
844     }
845
846     // get values
847     void *msg = item->msg;
848
849     if (NULL == msg)
850     {
851         return;
852     }
853
854     // get endpoint
855     CAData_t *td = (CAData_t *) msg;
856
857     if (td->requestInfo && g_requestHandler)
858     {
859         OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
860         g_requestHandler(td->remoteEndpoint, td->requestInfo);
861     }
862     else if (td->responseInfo && g_responseHandler)
863     {
864         OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
865         g_responseHandler(td->remoteEndpoint, td->responseInfo);
866     }
867     else if (td->errorInfo && g_errorHandler)
868     {
869         OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
870         g_errorHandler(td->remoteEndpoint, td->errorInfo);
871     }
872
873     CADestroyData(msg, sizeof(CAData_t));
874     OICFree(item);
875
876 #endif /* SINGLE_HANDLE */
877 #endif
878 }
879
880 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
881                                    CADataType_t dataType)
882 {
883     OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
884
885     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
886     if (!cadata)
887     {
888         OIC_LOG(ERROR, TAG, "memory allocation failed");
889         return NULL;
890     }
891
892     if(CA_REQUEST_DATA == dataType)
893     {
894         // clone request info
895         CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
896
897         if(!request)
898         {
899             OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
900             OICFree(cadata);
901             return NULL;
902         }
903
904         cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
905         cadata->requestInfo =  request;
906     }
907     else if(CA_RESPONSE_DATA == dataType)
908     {
909         // clone response info
910         CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
911
912         if(!response)
913         {
914             OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
915             OICFree(cadata);
916             return NULL;
917         }
918
919         cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
920         cadata->responseInfo = response;
921     }
922     else
923     {
924         OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
925         OICFree(cadata);
926         return NULL;
927     }
928
929     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
930     if (!ep)
931     {
932         OIC_LOG(ERROR, TAG, "endpoint clone failed");
933         CADestroyData(cadata, sizeof(CAData_t));
934         return NULL;
935     }
936
937     cadata->remoteEndpoint = ep;
938     cadata->dataType = dataType;
939     return cadata;
940 }
941
942 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
943 {
944     OIC_LOG(DEBUG, TAG, "IN");
945
946     VERIFY_NON_NULL(object, TAG, "object");
947     VERIFY_NON_NULL(request, TAG, "request");
948
949     if (false == CAIsSelectedNetworkAvailable())
950     {
951         return CA_STATUS_FAILED;
952     }
953
954 #ifdef ARDUINO
955     // If max retransmission queue is reached, then don't handle new request
956     if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
957     {
958         OIC_LOG(ERROR, TAG, "max RT queue size reached!");
959         return CA_SEND_FAILED;
960     }
961 #endif /* ARDUINO */
962
963     CAData_t *data = CAPrepareSendData(object, request, CA_REQUEST_DATA);
964     if(!data)
965     {
966         OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
967         return CA_MEMORY_ALLOC_FAILED;
968     }
969
970 #ifdef SINGLE_THREAD
971     CAResult_t result = CAProcessSendData(data);
972     if(CA_STATUS_OK != result)
973     {
974         OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
975         return result;
976     }
977
978     CADestroyData(data, sizeof(CAData_t));
979 #else
980 #ifdef WITH_BWT
981     if (CA_ADAPTER_GATT_BTLE != object->adapter
982 #ifdef TCP_ADAPTER
983             && CA_ADAPTER_TCP != object->adapter
984 #endif
985             )
986     {
987         // send block data
988         CAResult_t res = CASendBlockWiseData(data);
989         if(CA_NOT_SUPPORTED == res)
990         {
991             OIC_LOG(DEBUG, TAG, "normal msg will be sent");
992             CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
993             return CA_STATUS_OK;
994         }
995         else
996         {
997             CADestroyData(data, sizeof(CAData_t));
998         }
999         return res;
1000     }
1001     else
1002 #endif
1003     {
1004         CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1005     }
1006 #endif
1007
1008     OIC_LOG(DEBUG, TAG, "OUT");
1009     return CA_STATUS_OK;
1010 }
1011
1012 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
1013                                    const CAResponseInfo_t *response)
1014 {
1015     OIC_LOG(DEBUG, TAG, "IN");
1016     VERIFY_NON_NULL(object, TAG, "object");
1017     VERIFY_NON_NULL(response, TAG, "response");
1018
1019     if (false == CAIsSelectedNetworkAvailable())
1020     {
1021         return CA_STATUS_FAILED;
1022     }
1023
1024     CAData_t *data = CAPrepareSendData(object, response, CA_RESPONSE_DATA);
1025     if(!data)
1026     {
1027         OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
1028         return CA_MEMORY_ALLOC_FAILED;
1029     }
1030
1031 #ifdef SINGLE_THREAD
1032     CAResult_t result = CAProcessSendData(data);
1033     if(result != CA_STATUS_OK)
1034     {
1035         OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
1036         return result;
1037     }
1038
1039     CADestroyData(data, sizeof(CAData_t));
1040 #else
1041 #ifdef WITH_BWT
1042     if (CA_ADAPTER_GATT_BTLE != object->adapter
1043 #ifdef TCP_ADAPTER
1044             && CA_ADAPTER_TCP != object->adapter
1045 #endif
1046             )
1047     {
1048         // send block data
1049         CAResult_t res = CASendBlockWiseData(data);
1050         if(CA_NOT_SUPPORTED == res)
1051         {
1052             OIC_LOG(DEBUG, TAG, "normal msg will be sent");
1053             CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1054             return CA_STATUS_OK;
1055         }
1056         else
1057         {
1058             CADestroyData(data, sizeof(CAData_t));
1059         }
1060         return res;
1061     }
1062     else
1063 #endif
1064     {
1065         CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1066     }
1067 #endif
1068
1069     OIC_LOG(DEBUG, TAG, "OUT");
1070     return CA_STATUS_OK;
1071 }
1072
1073 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
1074                                       uint8_t tokenLength, const CAHeaderOption_t *options,
1075                                       uint8_t numOptions)
1076 {
1077     (void)resourceUri;
1078     (void)token;
1079     (void)tokenLength;
1080     (void)options;
1081     (void)numOptions;
1082     return CA_NOT_SUPPORTED;
1083 }
1084
1085 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1086                              CAErrorCallback errorHandler)
1087 {
1088     OIC_LOG(DEBUG, TAG, "IN");
1089     g_requestHandler = ReqHandler;
1090     g_responseHandler = RespHandler;
1091     g_errorHandler = errorHandler;
1092     OIC_LOG(DEBUG, TAG, "OUT");
1093 }
1094
1095 CAResult_t CAInitializeMessageHandler()
1096 {
1097     OIC_LOG(DEBUG, TAG, "IN");
1098     CASetPacketReceivedCallback(CAReceivedPacketCallback);
1099
1100     CASetNetworkChangeCallback(CANetworkChangedCallback);
1101     CASetErrorHandleCallback(CAErrorHandler);
1102
1103 #ifndef SINGLE_THREAD
1104     // create thread pool
1105     CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1106
1107     if (CA_STATUS_OK != res)
1108     {
1109         OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1110         return res;
1111     }
1112
1113     // send thread initialize
1114     if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1115                                                    CASendThreadProcess, CADestroyData))
1116     {
1117         OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1118         return CA_STATUS_FAILED;
1119     }
1120
1121     // start send thread
1122     res = CAQueueingThreadStart(&g_sendThread);
1123
1124     if (CA_STATUS_OK != res)
1125     {
1126         OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1127         ca_thread_pool_free(g_threadPoolHandle);
1128         g_threadPoolHandle = NULL;
1129         return res;
1130     }
1131
1132     // receive thread initialize
1133     if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1134                                                    CAReceiveThreadProcess, CADestroyData))
1135     {
1136         OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1137         return CA_STATUS_FAILED;
1138     }
1139
1140 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1141     // start receive thread
1142     res = CAQueueingThreadStart(&gReceiveThread);
1143
1144     if (res != CA_STATUS_OK)
1145     {
1146         OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1147         return res;
1148     }
1149 #endif /* SINGLE_HANDLE */
1150
1151     // retransmission initialize
1152     CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
1153                                CATimeoutCallback, NULL);
1154
1155 #ifdef WITH_BWT
1156     // block-wise transfer initialize
1157     CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1158 #endif
1159
1160     // start retransmission
1161     res = CARetransmissionStart(&g_retransmissionContext);
1162
1163     if (CA_STATUS_OK != res)
1164     {
1165         OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1166         return res;
1167     }
1168
1169     // initialize interface adapters by controller
1170     CAInitializeAdapters(g_threadPoolHandle);
1171 #else
1172     // retransmission initialize
1173     CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1174                                CATimeoutCallback, NULL);
1175     CAInitializeAdapters();
1176 #endif
1177
1178     OIC_LOG(DEBUG, TAG, "OUT");
1179     return CA_STATUS_OK;
1180 }
1181
1182 void CATerminateMessageHandler()
1183 {
1184     OIC_LOG(DEBUG, TAG, "IN");
1185 #ifndef SINGLE_THREAD
1186     CATransportAdapter_t connType;
1187     u_arraylist_t *list = CAGetSelectedNetworkList();
1188     uint32_t length = u_arraylist_length(list);
1189
1190     uint32_t i = 0;
1191     for (i = 0; i < length; i++)
1192     {
1193         void* ptrType = u_arraylist_get(list, i);
1194
1195         if (NULL == ptrType)
1196         {
1197             continue;
1198         }
1199
1200         connType = *(CATransportAdapter_t *)ptrType;
1201         CAStopAdapter(connType);
1202     }
1203
1204     // stop retransmission
1205     if (NULL != g_retransmissionContext.threadMutex)
1206     {
1207         CARetransmissionStop(&g_retransmissionContext);
1208     }
1209
1210     // stop thread
1211     // delete thread data
1212     if (NULL != g_sendThread.threadMutex)
1213     {
1214         CAQueueingThreadStop(&g_sendThread);
1215     }
1216
1217     // stop thread
1218     // delete thread data
1219     if (NULL != g_receiveThread.threadMutex)
1220     {
1221 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1222         CAQueueingThreadStop(&gReceiveThread);
1223 #endif /* SINGLE_HANDLE */
1224     }
1225
1226     // destroy thread pool
1227     if (NULL != g_threadPoolHandle)
1228     {
1229         ca_thread_pool_free(g_threadPoolHandle);
1230         g_threadPoolHandle = NULL;
1231     }
1232
1233 #ifdef WITH_BWT
1234     CATerminateBlockWiseTransfer();
1235 #endif
1236     CARetransmissionDestroy(&g_retransmissionContext);
1237     CAQueueingThreadDestroy(&g_sendThread);
1238     CAQueueingThreadDestroy(&g_receiveThread);
1239
1240     // terminate interface adapters by controller
1241     CATerminateAdapters();
1242 #else
1243     // terminate interface adapters by controller
1244     CATerminateAdapters();
1245
1246     // stop retransmission
1247     CARetransmissionStop(&g_retransmissionContext);
1248     CARetransmissionDestroy(&g_retransmissionContext);
1249 #endif
1250
1251     OIC_LOG(DEBUG, TAG, "OUT");
1252 }
1253
1254 void CALogPDUInfo(coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
1255 {
1256     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1257
1258     OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1259
1260 #ifdef TCP_ADAPTER
1261     if (CA_ADAPTER_TCP == endpoint->adapter)
1262     {
1263         OIC_LOG(DEBUG, TAG, "pdu header data :");
1264         OIC_LOG_BUFFER(DEBUG, TAG,  pdu->hdr, pdu->length);
1265     }
1266     else
1267 #endif
1268     {
1269         OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->coap_hdr_udp_t.type);
1270
1271         OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->coap_hdr_udp_t.code);
1272
1273         OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1274
1275         OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->coap_hdr_udp_t.token,
1276                        pdu->hdr->coap_hdr_udp_t.token_length);
1277     }
1278 }
1279
1280 static void CALogPayloadInfo(CAInfo_t *info)
1281 {
1282     if(info)
1283     {
1284         if (info->options)
1285         {
1286             for (uint32_t i = 0; i < info->numOptions; i++)
1287             {
1288                 OIC_LOG_V(DEBUG, TAG, "optionID: %d", info->options[i].optionID);
1289
1290                 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1291             }
1292         }
1293
1294         if (info->payload)
1295         {
1296             OIC_LOG_V(DEBUG, TAG, "payload: %p(%u)", info->payload,
1297                       info->payloadSize);
1298         }
1299
1300         if (info->token)
1301         {
1302             OIC_LOG(DEBUG, TAG, "token:");
1303             OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1304                            info->tokenLength);
1305         }
1306         OIC_LOG_V(DEBUG, TAG, "msgID: %d", info->messageId);
1307     }
1308     else
1309     {
1310         OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1311     }
1312 }
1313
1314 void CAErrorHandler(const CAEndpoint_t *endpoint,
1315                     const void *data, uint32_t dataLen,
1316                     CAResult_t result)
1317 {
1318     OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1319
1320 #ifndef SINGLE_THREAD
1321
1322     VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1323     VERIFY_NON_NULL_VOID(data, TAG, "data");
1324
1325     uint32_t code = CA_NOT_FOUND;
1326     //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1327     //Get PDU data
1328     coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
1329     if (NULL == pdu)
1330     {
1331         OIC_LOG(ERROR, TAG, "Parse PDU failed");
1332         return;
1333     }
1334
1335     CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1336     if(!cadata)
1337     {
1338         OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1339         coap_delete_pdu(pdu);
1340         return;
1341     }
1342
1343     cadata->errorInfo->result = result;
1344
1345     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1346     coap_delete_pdu(pdu);
1347 #endif
1348
1349     OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1350     return;
1351 }
1352
1353 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1354 {
1355     OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1356 #ifndef SINGLE_THREAD
1357     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1358     if (!cadata)
1359     {
1360         OIC_LOG(ERROR, TAG, "memory allocation failed");
1361         return;
1362     }
1363
1364     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1365     if (!ep)
1366     {
1367         OIC_LOG(ERROR, TAG, "endpoint clone failed");
1368         OICFree(cadata);
1369         return;
1370     }
1371
1372     CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1373     if (!errorInfo)
1374     {
1375         OICFree(cadata);
1376         CAFreeEndpoint(ep);
1377         return;
1378     }
1379
1380     CAResult_t res = CACloneInfo(info, &errorInfo->info);
1381     if (CA_STATUS_OK != res)
1382     {
1383         OICFree(cadata);
1384         CAFreeEndpoint(ep);
1385         return;
1386     }
1387
1388     errorInfo->result = result;
1389     cadata->remoteEndpoint = ep;
1390     cadata->errorInfo = errorInfo;
1391     cadata->dataType = CA_ERROR_DATA;
1392
1393     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1394 #endif
1395     OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");
1396 }