code refactoring on camessagehandler
[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     VERIFY_NON_NULL_VOID(data, TAG, "data");
93
94     // add thread
95     CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
96 }
97
98 void CAAddDataToReceiveThread(CAData_t *data)
99 {
100     VERIFY_NON_NULL_VOID(data, TAG, "data");
101
102     // add thread
103     CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
104 }
105 #endif
106
107 static bool CAIsSelectedNetworkAvailable()
108 {
109     u_arraylist_t *list = CAGetSelectedNetworkList();
110     if (!list || u_arraylist_length(list) == 0)
111     {
112         OIC_LOG(ERROR, TAG, "No selected network");
113         return false;
114     }
115
116     return true;
117 }
118
119 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
120                                        const CARemoteId_t *identity,
121                                        const void *data, CADataType_t dataType)
122 {
123     OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN");
124     CAInfo_t *info = NULL;
125     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
126     if (!cadata)
127     {
128         OIC_LOG(ERROR, TAG, "memory allocation failed");
129         return NULL;
130     }
131
132     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
133     if (!ep)
134     {
135         OIC_LOG(ERROR, TAG, "endpoint clone failed");
136         OICFree(cadata);
137         return NULL;
138     }
139
140     OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
141     CAResult_t result;
142
143     if(CA_RESPONSE_DATA == dataType)
144     {
145         CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
146         if (!resInfo)
147         {
148             OIC_LOG(ERROR, TAG, "memory allocation failed");
149             OICFree(cadata);
150             CAFreeEndpoint(ep);
151             return NULL;
152         }
153
154         result = CAGetResponseInfoFromPDU(data, resInfo, endpoint);
155         if (CA_STATUS_OK != result)
156         {
157             OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed");
158             CAFreeEndpoint(ep);
159             CADestroyResponseInfoInternal(resInfo);
160             OICFree(cadata);
161             return NULL;
162         }
163         cadata->responseInfo = resInfo;
164         info = &resInfo->info;
165         if (identity)
166         {
167             info->identity = *identity;
168         }
169         OIC_LOG(DEBUG, TAG, "Response Info :");
170         CALogPayloadInfo(info);
171     }
172     else if (CA_REQUEST_DATA == dataType)
173     {
174         CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t));
175         if (!reqInfo)
176         {
177             OIC_LOG(ERROR, TAG, "memory allocation failed");
178             OICFree(cadata);
179             CAFreeEndpoint(ep);
180             return NULL;
181         }
182
183         result = CAGetRequestInfoFromPDU(data, endpoint, reqInfo);
184         if (CA_STATUS_OK != result)
185         {
186             OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed");
187             CAFreeEndpoint(ep);
188             CADestroyRequestInfoInternal(reqInfo);
189             OICFree(cadata);
190             return NULL;
191         }
192
193         if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId,
194                                 reqInfo->info.token, reqInfo->info.tokenLength))
195         {
196             OIC_LOG(ERROR, TAG, "Second Request with same Token, Drop it");
197             CAFreeEndpoint(ep);
198             CADestroyRequestInfoInternal(reqInfo);
199             OICFree(cadata);
200             return NULL;
201         }
202
203         cadata->requestInfo = reqInfo;
204         info = &reqInfo->info;
205         if (identity)
206         {
207             info->identity = *identity;
208         }
209         OIC_LOG(DEBUG, TAG, "Request Info :");
210         CALogPayloadInfo(info);
211    }
212     else if (CA_ERROR_DATA == dataType)
213     {
214         CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
215         if (!errorInfo)
216         {
217             OIC_LOG(ERROR, TAG, "Memory allocation failed!");
218             OICFree(cadata);
219             CAFreeEndpoint(ep);
220             return NULL;
221         }
222
223         CAResult_t result = CAGetErrorInfoFromPDU(data, endpoint, errorInfo);
224         if (CA_STATUS_OK != result)
225         {
226             OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed");
227             CAFreeEndpoint(ep);
228             OICFree(errorInfo);
229             OICFree(cadata);
230             return NULL;
231         }
232
233         cadata->errorInfo = errorInfo;
234         info = &errorInfo->info;
235         if (identity)
236         {
237             info->identity = *identity;
238         }
239         OIC_LOG(DEBUG, TAG, "error Info :");
240         CALogPayloadInfo(info);
241     }
242
243     cadata->remoteEndpoint = ep;
244     cadata->dataType = dataType;
245
246     OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
247     return cadata;
248 }
249
250 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
251 {
252     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
253     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
254
255     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
256     if (!ep)
257     {
258         OIC_LOG(ERROR, TAG, "clone failed");
259         return;
260     }
261
262     CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
263
264     if (!resInfo)
265     {
266         OIC_LOG(ERROR, TAG, "calloc failed");
267         CAFreeEndpoint(ep);
268         return;
269     }
270
271     resInfo->result = CA_RETRANSMIT_TIMEOUT;
272     resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
273     resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
274
275     CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info),
276                                        endpoint);
277     if (CA_STATUS_OK != res)
278     {
279         OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
280         CADestroyResponseInfoInternal(resInfo);
281         CAFreeEndpoint(ep);
282         return;
283     }
284
285     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
286     if (NULL == cadata)
287     {
288         OIC_LOG(ERROR, TAG, "memory allocation failed !");
289         CAFreeEndpoint(ep);
290         CADestroyResponseInfoInternal(resInfo);
291         return;
292     }
293
294     cadata->type = SEND_TYPE_UNICAST;
295     cadata->remoteEndpoint = ep;
296     cadata->requestInfo = NULL;
297     cadata->responseInfo = resInfo;
298
299 #ifdef SINGLE_THREAD
300     CAProcessReceivedData(cadata);
301 #else
302     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
303 #endif
304 }
305
306 static void CADestroyData(void *data, uint32_t size)
307 {
308     OIC_LOG(DEBUG, TAG, "CADestroyData IN");
309     if ((size_t)size < sizeof(CAData_t))
310     {
311         OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
312     }
313     CAData_t *cadata = (CAData_t *) data;
314
315     if (NULL == cadata)
316     {
317         OIC_LOG(ERROR, TAG, "cadata is NULL");
318         return;
319     }
320
321     if (NULL != cadata->remoteEndpoint)
322     {
323         CAFreeEndpoint(cadata->remoteEndpoint);
324     }
325
326     if (NULL != cadata->requestInfo)
327     {
328         CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
329     }
330
331     if (NULL != cadata->responseInfo)
332     {
333         CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
334     }
335
336     if (NULL != cadata->errorInfo)
337     {
338         CADestroyErrorInfoInternal(cadata->errorInfo);
339     }
340
341     OICFree(cadata);
342     OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
343 }
344
345 #ifdef SINGLE_THREAD
346 static void CAProcessReceivedData(CAData_t *data)
347 {
348     OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
349     if (!data)
350     {
351         OIC_LOG(ERROR, TAG, "thread data error!!");
352         return;
353     }
354
355     // parse the data and call the callbacks.
356     // #1 parse the data
357     // #2 get endpoint
358     CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
359     if (!rep)
360     {
361         OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
362         return;
363     }
364
365     if (data->requestInfo && g_requestHandler)
366     {
367         g_requestHandler(rep, data->requestInfo);
368     }
369     else if (data->responseInfo && g_responseHandler)
370     {
371         g_responseHandler(rep, data->responseInfo);
372     }
373     else if (data->errorInfo && g_errorHandler)
374     {
375         g_errorHandler(rep, data->errorInfo);
376     }
377
378     CADestroyData(data, sizeof(CAData_t));
379
380     OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
381 }
382 #endif
383
384 #ifndef SINGLE_THREAD
385 static void CAReceiveThreadProcess(void *threadData)
386 {
387 #ifndef SINGLE_HANDLE
388     CAData_t *data = (CAData_t *) threadData;
389     CAProcessReceivedData(data);
390 #else
391     (void)threadData;
392 #endif
393 }
394 #endif // SINGLE_THREAD
395
396 static CAResult_t CAProcessSendData(const CAData_t *data)
397 {
398     VERIFY_NON_NULL(data, TAG, "data");
399     VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
400
401     CAResult_t res = CA_STATUS_FAILED;
402
403     CASendDataType_t type = data->type;
404
405     coap_pdu_t *pdu = NULL;
406     CAInfo_t *info = NULL;
407     coap_list_t *options = NULL;
408     coap_transport_type transport;
409
410     if (SEND_TYPE_UNICAST == type)
411     {
412         OIC_LOG(DEBUG,TAG,"Unicast message");
413 #ifdef ROUTING_GATEWAY
414         /*
415          * When forwarding a packet, do not attempt retransmission as its the responsibility of
416          * packet originator node
417          */
418         bool skipRetransmission = false;
419 #endif
420
421         if (NULL != data->requestInfo)
422         {
423             OIC_LOG(DEBUG, TAG, "requestInfo is available..");
424
425             info = &data->requestInfo->info;
426 #ifdef ROUTING_GATEWAY
427             skipRetransmission = data->requestInfo->info.skipRetransmission;
428 #endif
429             pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint,
430                                 &options, &transport);
431         }
432         else if (NULL != data->responseInfo)
433         {
434             OIC_LOG(DEBUG, TAG, "responseInfo is available..");
435
436             info = &data->responseInfo->info;
437 #ifdef ROUTING_GATEWAY
438             skipRetransmission = data->responseInfo->info.skipRetransmission;
439 #endif
440             pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
441                                 &options, &transport);
442         }
443         else
444         {
445             OIC_LOG(DEBUG, TAG, "request info, response info is empty");
446             return CA_STATUS_INVALID_PARAM;
447         }
448
449         // interface controller function call.
450         if (NULL != pdu)
451         {
452 #ifdef WITH_BWT
453             if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
454 #ifdef TCP_ADAPTER
455                     && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
456 #endif
457                     )
458             {
459                 // Blockwise transfer
460                 if (NULL != info)
461                 {
462                     CAResult_t res = CAAddBlockOption(&pdu, info,
463                                                       data->remoteEndpoint,
464                                                       &options);
465                     if (CA_STATUS_OK != res)
466                     {
467                         OIC_LOG(INFO, TAG, "to write block option has failed");
468                         CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
469                         coap_delete_list(options);
470                         coap_delete_pdu(pdu);
471                         return res;
472                     }
473                 }
474             }
475 #endif // WITH_BWT
476             CALogPDUInfo(pdu, data->remoteEndpoint);
477
478             res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
479             if (CA_STATUS_OK != res)
480             {
481                 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
482                 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
483                 coap_delete_list(options);
484                 coap_delete_pdu(pdu);
485                 return res;
486             }
487
488 #ifdef TCP_ADAPTER
489             if (CA_ADAPTER_TCP == data->remoteEndpoint->adapter)
490             {
491                 OIC_LOG(INFO, TAG, "retransmission will be not worked");
492             }
493             else
494 #endif
495 #ifdef ROUTING_GATEWAY
496             if(!skipRetransmission)
497 #endif
498             {
499                 // for retransmission
500                 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint,
501                                                pdu->hdr, pdu->length);
502                 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
503                 {
504                     //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
505                     OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
506                     coap_delete_list(options);
507                     coap_delete_pdu(pdu);
508                     return res;
509                 }
510             }
511
512             coap_delete_list(options);
513             coap_delete_pdu(pdu);
514         }
515         else
516         {
517             OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
518             CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
519             return CA_SEND_FAILED;
520         }
521     }
522     else if (SEND_TYPE_MULTICAST == type)
523     {
524         OIC_LOG(DEBUG,TAG,"Multicast message");
525         if (NULL != data->requestInfo)
526         {
527             OIC_LOG(DEBUG, TAG, "requestInfo is available..");
528
529             info = &data->requestInfo->info;
530             pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);
531
532             if (NULL != pdu)
533             {
534 #ifdef WITH_BWT
535                 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
536 #ifdef TCP_ADAPTER
537                         && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
538 #endif
539                         )
540                 {
541                     // Blockwise transfer
542                     CAResult_t res = CAAddBlockOption(&pdu, info,
543                                                       data->remoteEndpoint,
544                                                       &options);
545                     if (CA_STATUS_OK != res)
546                     {
547                         OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
548                         CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
549                         coap_delete_list(options);
550                         coap_delete_pdu(pdu);
551                         return res;
552                     }
553                 }
554 #endif // WITH_BWT
555             }
556             else
557             {
558                 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
559                 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
560                 return CA_SEND_FAILED;
561             }
562         }
563         else if (NULL != data->responseInfo)
564         {
565             OIC_LOG(DEBUG, TAG, "responseInfo is available..");
566
567             info = &data->responseInfo->info;
568             pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
569                                 &options, &transport);
570
571             if (NULL != pdu)
572             {
573 #ifdef WITH_BWT
574                 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
575 #ifdef TCP_ADAPTER
576                         && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
577 #endif
578                         )
579                 {
580                     // Blockwise transfer
581                     if (NULL != info)
582                     {
583                         CAResult_t res = CAAddBlockOption(&pdu, info,
584                                                           data->remoteEndpoint,
585                                                           &options);
586                         if (CA_STATUS_OK != res)
587                         {
588                             OIC_LOG(INFO, TAG, "to write block option has failed");
589                             CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
590                             coap_delete_list(options);
591                             coap_delete_pdu(pdu);
592                             return res;
593                         }
594                     }
595                 }
596 #endif // WITH_BWT
597             }
598             else
599             {
600                 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
601                 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
602                 return CA_SEND_FAILED;
603             }
604         }
605         else
606         {
607             OIC_LOG(ERROR, TAG, "request or response info is empty");
608             return CA_SEND_FAILED;
609         }
610
611         CALogPDUInfo(pdu, data->remoteEndpoint);
612
613         OIC_LOG(DEBUG, TAG, "pdu to send :");
614         OIC_LOG_BUFFER(DEBUG, TAG,  (uint8_t*)pdu->hdr, pdu->length);
615
616         res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
617         if (CA_STATUS_OK != res)
618         {
619             OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
620             CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
621             coap_delete_list(options);
622             coap_delete_pdu(pdu);
623             return res;
624         }
625
626         coap_delete_list(options);
627         coap_delete_pdu(pdu);
628     }
629
630     return CA_STATUS_OK;
631 }
632
633 #ifndef SINGLE_THREAD
634 static void CASendThreadProcess(void *threadData)
635 {
636     CAData_t *data = (CAData_t *) threadData;
637     CAProcessSendData(data);
638 }
639 #endif
640
641 /*
642  * If a second message arrives with the same message ID, token and the other address
643  * family, drop it.  Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
644  */
645 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id,
646                                 CAToken_t token, uint8_t tokenLength)
647 {
648     if (!ep)
649     {
650         return true;
651     }
652     if (ep->adapter != CA_ADAPTER_IP)
653     {
654         return false;
655     }
656     if (!caglobals.ip.dualstack)
657     {
658         return false;
659     }
660
661     if (tokenLength > CA_MAX_TOKEN_LEN)
662     {
663         /*
664          * If token length is more than CA_MAX_TOKEN_LEN,
665          * we compare the first CA_MAX_TOKEN_LEN bytes only.
666          */
667         tokenLength = CA_MAX_TOKEN_LEN;
668     }
669
670     bool ret = false;
671     CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
672
673     for (size_t i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
674     {
675         CAHistoryItem_t *item = &(history->items[i]);
676         if (id == item->messageId && tokenLength == item->tokenLength
677             && memcmp(item->token, token, tokenLength) == 0)
678         {
679             if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
680             {
681                 OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
682                           familyFlags & CA_IPV6 ? '6' : '4');
683                 ret = true;
684                 break;
685             }
686         }
687     }
688
689     history->items[history->nextIndex].flags = familyFlags;
690     history->items[history->nextIndex].messageId = id;
691     if (token && tokenLength)
692     {
693         memcpy(history->items[history->nextIndex].token, token, tokenLength);
694         history->items[history->nextIndex].tokenLength = tokenLength;
695     }
696
697     if (++history->nextIndex >= HISTORYSIZE)
698     {
699         history->nextIndex = 0;
700     }
701
702     return ret;
703 }
704
705 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
706                                      const void *data, uint32_t dataLen)
707 {
708     VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
709     VERIFY_NON_NULL_VOID(data, TAG, "data");
710
711     OIC_LOG(DEBUG, TAG, "received pdu data :");
712     OIC_LOG_BUFFER(DEBUG, TAG,  data, dataLen);
713
714     uint32_t code = CA_NOT_FOUND;
715     CAData_t *cadata = NULL;
716
717     coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code,
718                                                 &(sep->endpoint));
719     if (NULL == pdu)
720     {
721         OIC_LOG(ERROR, TAG, "Parse PDU failed");
722         return;
723     }
724
725     OIC_LOG_V(DEBUG, TAG, "code = %d", code);
726     if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
727     {
728         cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
729         if (!cadata)
730         {
731             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
732             coap_delete_pdu(pdu);
733             return;
734         }
735     }
736     else
737     {
738         cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
739         if (!cadata)
740         {
741             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
742             coap_delete_pdu(pdu);
743             return;
744         }
745
746 #ifdef TCP_ADAPTER
747         if (CA_ADAPTER_TCP == sep->endpoint.adapter)
748         {
749             OIC_LOG(INFO, TAG, "retransmission is not supported");
750         }
751         else
752 #endif
753         {
754             // for retransmission
755             void *retransmissionPdu = NULL;
756             CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
757                                          pdu->length, &retransmissionPdu);
758
759             // get token from saved data in retransmission list
760             if (retransmissionPdu && CA_EMPTY == code)
761             {
762                 if (cadata->responseInfo)
763                 {
764                     CAInfo_t *info = &cadata->responseInfo->info;
765                     CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
766                                                        info, &(sep->endpoint));
767                     if (CA_STATUS_OK != res)
768                     {
769                         OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
770                         OICFree(info->token);
771                         info->tokenLength = 0;
772                     }
773                 }
774             }
775             OICFree(retransmissionPdu);
776         }
777     }
778
779     cadata->type = SEND_TYPE_UNICAST;
780
781 #ifdef SINGLE_THREAD
782     CAProcessReceivedData(cadata);
783 #else
784 #ifdef WITH_BWT
785     if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter
786 #ifdef TCP_ADAPTER
787             && CA_ADAPTER_TCP != sep->endpoint.adapter
788 #endif
789             )
790     {
791         CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
792         if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
793         {
794             OIC_LOG(ERROR, TAG, "this message does not have block option");
795             CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
796         }
797         else
798         {
799             CADestroyData(cadata, sizeof(CAData_t));
800         }
801     }
802     else
803 #endif
804     {
805         CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
806     }
807 #endif // SINGLE_THREAD
808
809     coap_delete_pdu(pdu);
810 }
811
812 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
813 {
814     (void)info;
815     (void)status;
816     OIC_LOG(DEBUG, TAG, "IN");
817
818     OIC_LOG(DEBUG, TAG, "OUT");
819 }
820
821 void CAHandleRequestResponseCallbacks()
822 {
823 #ifdef SINGLE_THREAD
824     CAReadData();
825     CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
826 #else
827 #ifdef SINGLE_HANDLE
828     // parse the data and call the callbacks.
829     // #1 parse the data
830     // #2 get endpoint
831
832     ca_mutex_lock(g_receiveThread.threadMutex);
833
834     u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
835
836     ca_mutex_unlock(g_receiveThread.threadMutex);
837
838     if (NULL == item)
839     {
840         return;
841     }
842
843     // get values
844     void *msg = item->msg;
845
846     if (NULL == msg)
847     {
848         return;
849     }
850
851     // get endpoint
852     CAData_t *td = (CAData_t *) msg;
853
854     if (td->requestInfo && g_requestHandler)
855     {
856         OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
857         g_requestHandler(td->remoteEndpoint, td->requestInfo);
858     }
859     else if (td->responseInfo && g_responseHandler)
860     {
861         OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
862         g_responseHandler(td->remoteEndpoint, td->responseInfo);
863     }
864     else if (td->errorInfo && g_errorHandler)
865     {
866         OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
867         g_errorHandler(td->remoteEndpoint, td->errorInfo);
868     }
869
870     CADestroyData(msg, sizeof(CAData_t));
871     OICFree(item);
872
873 #endif // SINGLE_HANDLE
874 #endif // SINGLE_THREAD
875 }
876
877 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
878                                    CADataType_t dataType)
879 {
880     OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
881
882     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
883     if (!cadata)
884     {
885         OIC_LOG(ERROR, TAG, "memory allocation failed");
886         return NULL;
887     }
888
889     if(CA_REQUEST_DATA == dataType)
890     {
891         // clone request info
892         CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
893
894         if(!request)
895         {
896             OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
897             OICFree(cadata);
898             return NULL;
899         }
900
901         cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
902         cadata->requestInfo =  request;
903     }
904     else if(CA_RESPONSE_DATA == dataType)
905     {
906         // clone response info
907         CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
908
909         if(!response)
910         {
911             OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
912             OICFree(cadata);
913             return NULL;
914         }
915
916         cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
917         cadata->responseInfo = response;
918     }
919     else
920     {
921         OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
922         OICFree(cadata);
923         return NULL;
924     }
925
926     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
927     if (!ep)
928     {
929         OIC_LOG(ERROR, TAG, "endpoint clone failed");
930         CADestroyData(cadata, sizeof(CAData_t));
931         return NULL;
932     }
933
934     cadata->remoteEndpoint = ep;
935     cadata->dataType = dataType;
936     return cadata;
937 }
938
939 CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg,
940                                CADataType_t dataType)
941 {
942     VERIFY_NON_NULL(endpoint, TAG, "endpoint");
943     VERIFY_NON_NULL(sendMsg, TAG, "sendMsg");
944
945     if (false == CAIsSelectedNetworkAvailable())
946     {
947         return CA_STATUS_FAILED;
948     }
949
950 #ifdef ARDUINO
951     // If max retransmission queue is reached, then don't handle new request
952     if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
953     {
954         OIC_LOG(ERROR, TAG, "max RT queue size reached!");
955         return CA_SEND_FAILED;
956     }
957 #endif // ARDUINO
958
959     CAData_t *data = CAPrepareSendData(endpoint, sendMsg, dataType);
960     if(!data)
961     {
962         OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
963         return CA_MEMORY_ALLOC_FAILED;
964     }
965
966 #ifdef SINGLE_THREAD
967     CAResult_t result = CAProcessSendData(data);
968     if(CA_STATUS_OK != result)
969     {
970         OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
971         CADestroyData(data, sizeof(CAData_t));
972         return result;
973     }
974
975     CADestroyData(data, sizeof(CAData_t));
976 #else
977 #ifdef WITH_BWT
978     if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
979 #ifdef TCP_ADAPTER
980             && CA_ADAPTER_TCP != endpoint->adapter
981 #endif
982             )
983     {
984         // send block data
985         CAResult_t res = CASendBlockWiseData(data);
986         if(CA_NOT_SUPPORTED == res)
987         {
988             OIC_LOG(DEBUG, TAG, "normal msg will be sent");
989             CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
990             return CA_STATUS_OK;
991         }
992         else
993         {
994             CADestroyData(data, sizeof(CAData_t));
995         }
996         return res;
997     }
998     else
999 #endif // WITH_BWT
1000     {
1001         CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1002     }
1003 #endif // SINGLE_THREAD
1004
1005     return CA_STATUS_OK;
1006 }
1007
1008 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
1009                                       uint8_t tokenLength, const CAHeaderOption_t *options,
1010                                       uint8_t numOptions)
1011 {
1012     (void)resourceUri;
1013     (void)token;
1014     (void)tokenLength;
1015     (void)options;
1016     (void)numOptions;
1017     return CA_NOT_SUPPORTED;
1018 }
1019
1020 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1021                              CAErrorCallback errorHandler)
1022 {
1023     g_requestHandler = ReqHandler;
1024     g_responseHandler = RespHandler;
1025     g_errorHandler = errorHandler;
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
1224     OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1225
1226 #ifdef TCP_ADAPTER
1227     if (CA_ADAPTER_TCP == endpoint->adapter)
1228     {
1229         OIC_LOG(DEBUG, TAG, "pdu header data :");
1230         OIC_LOG_BUFFER(DEBUG, TAG,  pdu->hdr, pdu->length);
1231     }
1232     else
1233 #else
1234     (void) endpoint;
1235 #endif
1236     {
1237         OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->coap_hdr_udp_t.type);
1238
1239         OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->coap_hdr_udp_t.code);
1240
1241         OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1242
1243         OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->coap_hdr_udp_t.token,
1244                        pdu->hdr->coap_hdr_udp_t.token_length);
1245     }
1246 }
1247
1248 static void CALogPayloadInfo(CAInfo_t *info)
1249 {
1250     if(info)
1251     {
1252         if (info->options)
1253         {
1254             for (uint32_t i = 0; i < info->numOptions; i++)
1255             {
1256                 OIC_LOG_V(DEBUG, TAG, "optionID: %u", info->options[i].optionID);
1257
1258                 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1259             }
1260         }
1261
1262         if (info->payload)
1263         {
1264             OIC_LOG_V(DEBUG, TAG, "payload: %p(%zu)", info->payload,
1265                       info->payloadSize);
1266         }
1267
1268         if (info->token)
1269         {
1270             OIC_LOG(DEBUG, TAG, "token:");
1271             OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1272                            info->tokenLength);
1273         }
1274         OIC_LOG_V(DEBUG, TAG, "msgID: %u", info->messageId);
1275     }
1276     else
1277     {
1278         OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1279     }
1280 }
1281
1282 void CAErrorHandler(const CAEndpoint_t *endpoint,
1283                     const void *data, uint32_t dataLen,
1284                     CAResult_t result)
1285 {
1286     OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1287
1288 #ifndef SINGLE_THREAD
1289     VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1290     VERIFY_NON_NULL_VOID(data, TAG, "data");
1291
1292     uint32_t code = CA_NOT_FOUND;
1293     //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1294     //Get PDU data
1295     coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
1296     if (NULL == pdu)
1297     {
1298         OIC_LOG(ERROR, TAG, "Parse PDU failed");
1299         return;
1300     }
1301
1302     CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1303     if(!cadata)
1304     {
1305         OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1306         coap_delete_pdu(pdu);
1307         return;
1308     }
1309
1310     cadata->errorInfo->result = result;
1311
1312     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1313     coap_delete_pdu(pdu);
1314 #endif
1315
1316     OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1317     return;
1318 }
1319
1320 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1321 {
1322     OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1323 #ifndef SINGLE_THREAD
1324     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1325     if (!cadata)
1326     {
1327         OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1328         return;
1329     }
1330
1331     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1332     if (!ep)
1333     {
1334         OIC_LOG(ERROR, TAG, "endpoint clone failed");
1335         OICFree(cadata);
1336         return;
1337     }
1338
1339     CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1340     if (!errorInfo)
1341     {
1342         OIC_LOG(ERROR, TAG, "errorInfo memory allocation failed");
1343         OICFree(cadata);
1344         CAFreeEndpoint(ep);
1345         return;
1346     }
1347
1348     CAResult_t res = CACloneInfo(info, &errorInfo->info);
1349     if (CA_STATUS_OK != res)
1350     {
1351         OIC_LOG(ERROR, TAG, "info clone failed");
1352         OICFree(cadata);
1353         OICFree(errorInfo);
1354         CAFreeEndpoint(ep);
1355         return;
1356     }
1357
1358     errorInfo->result = result;
1359     cadata->remoteEndpoint = ep;
1360     cadata->errorInfo = errorInfo;
1361     cadata->dataType = CA_ERROR_DATA;
1362
1363     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1364 #endif
1365     OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");
1366 }