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