1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
26 #include "cainterface.h"
27 #include "camessagehandler.h"
28 #include "caremotehandler.h"
29 #include "cainterfacecontroller.h"
30 #include "caprotocolmessage.h"
31 #include "caretransmission.h"
32 #include "caadapterutils.h"
35 #include "config.h" /* for coap protocol */
36 #include "cathreadpool.h" /* for thread pool */
37 #include "caqueueingthread.h"
39 #include "oic_malloc.h"
40 #include "oic_string.h"
41 #include "canetworkconfigurator.h"
46 #define MAX_THREAD_POOL_SIZE 20
50 SEND_TYPE_MULTICAST = 0, SEND_TYPE_UNICAST
62 CASendDataType_t type;
63 CAEndpoint_t *remoteEndpoint;
64 CARequestInfo_t *requestInfo;
65 CAResponseInfo_t *responseInfo;
66 CAErrorInfo_t *errorInfo;
67 CAHeaderOption_t *options;
68 CADataType_t dataType;
73 static ca_thread_pool_t g_threadPoolHandle = NULL;
75 // message handler main thread
76 static CAQueueingThread_t g_sendThread;
77 static CAQueueingThread_t g_receiveThread;
79 static CARetransmission_t g_retransmissionContext;
82 static CARequestCallback g_requestHandler = NULL;
83 static CAResponseCallback g_responseHandler = NULL;
84 static CAErrorCallback g_errorHandler = NULL;
86 static void CAErrorHandler(const CAEndpoint_t *endpoint,
87 const void *data, uint32_t dataLen,
90 static bool CAIsSelectedNetworkAvailable()
92 u_arraylist_t *list = CAGetSelectedNetworkList();
93 if (!list || list->length == 0)
95 OIC_LOG(ERROR, TAG, "No selected network");
102 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
104 OIC_LOG(DEBUG, TAG, "IN");
105 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
106 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
108 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
111 OIC_LOG(ERROR, TAG, "clone failed");
115 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
119 OIC_LOG(ERROR, TAG, "calloc failed");
120 CAAdapterFreeEndpoint(ep);
124 resInfo->result = CA_RETRANSMIT_TIMEOUT;
125 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
126 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
127 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info));
128 if (CA_STATUS_OK != res)
130 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
131 OICFree(resInfo->info.token);
133 CAAdapterFreeEndpoint(ep);
137 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
140 OIC_LOG(ERROR, TAG, "memory allocation failed !");
141 CAAdapterFreeEndpoint(ep);
146 cadata->type = SEND_TYPE_UNICAST;
147 cadata->remoteEndpoint = ep;
148 cadata->requestInfo = NULL;
149 cadata->responseInfo = resInfo;
151 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
152 OIC_LOG(DEBUG, TAG, "OUT");
155 static void CADataDestroyer(void *data, uint32_t size)
157 OIC_LOG(DEBUG, TAG, "IN");
158 CAData_t *cadata = (CAData_t *) data;
162 OIC_LOG(ERROR, TAG, "cadata is NULL");
166 if (NULL != cadata->remoteEndpoint)
168 CADestroyEndpointInternal(cadata->remoteEndpoint);
171 if (NULL != cadata->requestInfo)
173 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
176 if (NULL != cadata->responseInfo)
178 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
181 if (NULL != cadata->errorInfo)
183 CAInfo_t *info = &cadata->errorInfo->info;
184 OICFree(info->token);
185 OICFree(info->options);
186 OICFree(info->payload);
187 OICFree(cadata->errorInfo);
190 OICFree(cadata->options);
192 OIC_LOG(DEBUG, TAG, "OUT");
195 static void CAReceiveThreadProcess(void *threadData)
197 OIC_LOG(DEBUG, TAG, "IN");
198 // Currently not supported
199 // This will be enabled when RI supports multi threading
200 #ifndef SINGLE_HANDLE
201 CAData_t *data = (CAData_t *) threadData;
205 OIC_LOG(ERROR, TAG, "thread data error!!");
209 // parse the data and call the callbacks.
212 CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
216 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
220 if (data->requestInfo && g_requestHandler)
222 g_requestHandler(rep, data->requestInfo);
224 else if (data->responseInfo && g_responseHandler)
226 g_responseHandler(rep, data->responseInfo);
228 else if (data->errorInfo && g_errorHandler)
230 g_errorHandler(rep, data->errorInfo);
233 #endif /* SINGLE_HANDLE */
234 OIC_LOG(DEBUG, TAG, "OUT");
237 static void CASendThreadProcess(void *threadData)
239 OIC_LOG(DEBUG, TAG, "IN");
240 CAData_t *data = (CAData_t *) threadData;
242 VERIFY_NON_NULL_VOID(data, TAG, "data");
243 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");
245 CAResult_t res = CA_STATUS_FAILED;
247 CASendDataType_t type = data->type;
249 coap_pdu_t *pdu = NULL;
251 if (SEND_TYPE_UNICAST == type)
254 if (NULL != data->requestInfo)
256 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
258 pdu = CAGeneratePDU(data->requestInfo->method, &data->requestInfo->info);
260 else if (NULL != data->responseInfo)
262 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
264 pdu = CAGeneratePDU(data->responseInfo->result, &data->responseInfo->info);
268 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
271 // interface controller function call.
276 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
277 if (CA_STATUS_OK != res)
279 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
280 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
281 coap_delete_pdu(pdu);
284 // for retransmission
285 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
287 if (CA_STATUS_OK != res)
289 OIC_LOG_V(INFO, TAG, "retransmission will be not working: %d", res);
290 coap_delete_pdu(pdu);
294 coap_delete_pdu(pdu);
297 else if (SEND_TYPE_MULTICAST == type)
299 OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
301 CAInfo_t *info = &data->requestInfo->info;
303 info->options = data->options;
304 info->numOptions = data->numOptions;
306 pdu = CAGeneratePDU(CA_GET, info);
311 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
312 if (CA_STATUS_OK != res)
314 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
315 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
316 coap_delete_pdu(pdu);
320 coap_delete_pdu(pdu);
324 OIC_LOG(DEBUG, TAG, "OUT");
327 static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
329 OIC_LOG(DEBUG, TAG, "IN");
330 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
331 VERIFY_NON_NULL_VOID(data, TAG, "data");
333 uint32_t code = CA_NOT_FOUND;
334 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
339 OIC_LOG(ERROR, TAG, "Parse PDU failed");
343 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
345 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
348 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
349 coap_delete_pdu(pdu);
353 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo);
354 if (CA_STATUS_OK != res)
356 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
358 coap_delete_pdu(pdu);
362 if (NULL != ReqInfo->info.options)
365 for (i = 0; i < ReqInfo->info.numOptions; i++)
367 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
369 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
373 if (NULL != ReqInfo->info.payload)
375 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
377 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
378 if (NULL != ReqInfo->info.token)
380 OIC_LOG(DEBUG, TAG, "Request- token:");
381 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
382 ReqInfo->info.tokenLength);
385 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
386 OIC_LOG(DEBUG, TAG, "Request- token");
387 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
388 OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
389 // store the data at queue.
390 CAData_t *cadata = NULL;
391 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
394 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
395 CADestroyRequestInfoInternal(ReqInfo);
396 coap_delete_pdu(pdu);
400 cadata->type = SEND_TYPE_UNICAST;
401 cadata->remoteEndpoint = CAAdapterCloneEndpoint(endpoint);
402 cadata->requestInfo = ReqInfo;
403 cadata->responseInfo = NULL;
404 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
408 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
411 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
412 coap_delete_pdu(pdu);
416 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo);
417 if (CA_STATUS_OK != res)
419 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
421 coap_delete_pdu(pdu);
425 if (NULL != ResInfo->info.options)
428 for (i = 0; i < ResInfo->info.numOptions; i++)
430 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
432 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
436 if (NULL != ResInfo->info.payload)
438 OIC_LOG_V(DEBUG, TAG, "Response- payload: %s", ResInfo->info.payload);
440 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
441 OIC_LOG_V(DEBUG, TAG, "Response- token : %s", ResInfo->info.token);
442 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
444 // store the data at queue.
445 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
448 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
449 CADestroyResponseInfoInternal(ResInfo);
450 coap_delete_pdu(pdu);
454 cadata->type = SEND_TYPE_UNICAST;
455 cadata->remoteEndpoint = CAAdapterCloneEndpoint(endpoint);
456 cadata->requestInfo = NULL;
458 // for retransmission
459 void *retransmissionPdu = NULL;
460 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
463 // get token from saved data in retransmission list
464 if (retransmissionPdu && CA_EMPTY == code)
466 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
468 if (CA_STATUS_OK != res)
470 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
471 OICFree(ResInfo->info.token);
474 OICFree(retransmissionPdu);
475 cadata->responseInfo = ResInfo;
477 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
482 coap_delete_pdu(pdu);
484 OIC_LOG(DEBUG, TAG, "OUT");
487 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
489 OIC_LOG(DEBUG, TAG, "IN");
491 OIC_LOG(DEBUG, TAG, "OUT");
494 void CAHandleRequestResponseCallbacks()
496 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks IN");
499 // parse the data and call the callbacks.
503 ca_mutex_lock(g_receiveThread.threadMutex);
505 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
507 ca_mutex_unlock(g_receiveThread.threadMutex);
515 void *msg = item->msg;
523 CAData_t *td = (CAData_t *) msg;
525 if (td->requestInfo && g_requestHandler)
527 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
528 g_requestHandler(td->remoteEndpoint, td->requestInfo);
530 else if (td->responseInfo && g_responseHandler)
532 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
533 g_responseHandler(td->remoteEndpoint, td->responseInfo);
535 else if (td->errorInfo && g_errorHandler)
537 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
538 g_errorHandler(td->remoteEndpoint, td->errorInfo);
541 CADataDestroyer(msg, sizeof(CAData_t));
544 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
547 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
549 OIC_LOG(DEBUG, TAG, "IN");
551 VERIFY_NON_NULL(object, TAG, "object");
552 VERIFY_NON_NULL(request, TAG, "request");
554 if (false == CAIsSelectedNetworkAvailable())
556 return CA_STATUS_FAILED;
559 CAEndpoint_t *remoteEndpoint = NULL;
560 CARequestInfo_t *requestInfo = NULL;
561 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
562 CA_MEMORY_ALLOC_CHECK(data);
564 // clone remote endpoint
565 remoteEndpoint = CACloneEndpoint(object);
566 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
568 // clone request info
569 requestInfo = CACloneRequestInfo(request);
570 CA_MEMORY_ALLOC_CHECK(requestInfo);
573 data->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
574 data->remoteEndpoint = remoteEndpoint;
575 data->requestInfo = requestInfo;
576 data->responseInfo = NULL;
579 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
580 OIC_LOG(DEBUG, TAG, "OUT");
583 // memory error label.
585 CAAdapterFreeEndpoint(remoteEndpoint);
586 CADestroyRequestInfoInternal(requestInfo);
589 OIC_LOG(DEBUG, TAG, "OUT");
590 return CA_MEMORY_ALLOC_FAILED;
593 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
594 const CAResponseInfo_t *response)
596 OIC_LOG(DEBUG, TAG, "IN");
597 VERIFY_NON_NULL(object, TAG, "object");
598 VERIFY_NON_NULL(response, TAG, "response");
600 if (false == CAIsSelectedNetworkAvailable())
602 return CA_STATUS_FAILED;
605 CAEndpoint_t *remoteEndpoint = NULL;
606 CAResponseInfo_t *responseInfo = NULL;
608 // allocate & initialize
609 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
610 CA_MEMORY_ALLOC_CHECK(data);
612 // clone remote endpoint
613 remoteEndpoint = CACloneEndpoint(object);
614 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
616 // clone response info
617 responseInfo = CACloneResponseInfo(response);
618 CA_MEMORY_ALLOC_CHECK(responseInfo);
621 data->type = SEND_TYPE_UNICAST;
622 data->remoteEndpoint = remoteEndpoint;
623 data->requestInfo = NULL;
624 data->responseInfo = responseInfo;
627 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
629 OIC_LOG(DEBUG, TAG, "OUT");
632 // memory error label.
634 CAAdapterFreeEndpoint(remoteEndpoint);
635 CADestroyResponseInfoInternal(responseInfo);
637 OIC_LOG(DEBUG, TAG, "OUT");
639 return CA_MEMORY_ALLOC_FAILED;
642 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
643 uint8_t tokenLength, const CAHeaderOption_t *options,
646 return CA_NOT_SUPPORTED;
649 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
650 CAErrorCallback errroHandler)
652 OIC_LOG(DEBUG, TAG, "IN");
653 g_requestHandler = ReqHandler;
654 g_responseHandler = RespHandler;
655 g_errorHandler = errroHandler;
656 OIC_LOG(DEBUG, TAG, "OUT");
659 CAResult_t CAInitializeMessageHandler()
661 OIC_LOG(DEBUG, TAG, "IN");
662 CASetPacketReceivedCallback(CAReceivedPacketCallback);
664 CASetNetworkChangeCallback(CANetworkChangedCallback);
665 CASetErrorHandleCallback(CAErrorHandler);
667 // create thread pool
668 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
670 if (res != CA_STATUS_OK)
672 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
676 // send thread initialize
677 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
678 CASendThreadProcess, CADataDestroyer))
680 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
681 return CA_STATUS_FAILED;
685 res = CAQueueingThreadStart(&g_sendThread);
687 if (res != CA_STATUS_OK)
689 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
690 ca_thread_pool_free(g_threadPoolHandle);
691 g_threadPoolHandle = NULL;
695 // receive thread initialize
696 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
697 CAReceiveThreadProcess, CADataDestroyer))
699 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
700 return CA_STATUS_FAILED;
703 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
704 // start receive thread
705 res = CAQueueingThreadStart(&gReceiveThread);
707 if (res != CA_STATUS_OK)
709 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
714 // retransmission initialize
715 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
716 CATimeoutCallback, NULL);
718 // start retransmission
719 res = CARetransmissionStart(&g_retransmissionContext);
721 if (res != CA_STATUS_OK)
723 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
727 // initialize interface adapters by controller
728 CAInitializeAdapters(g_threadPoolHandle);
729 OIC_LOG(DEBUG, TAG, "OUT");
733 void CATerminateMessageHandler()
735 OIC_LOG(DEBUG, TAG, "IN");
736 CATransportAdapter_t connType;
737 u_arraylist_t *list = CAGetSelectedNetworkList();
738 uint32_t length = u_arraylist_length(list);
741 for (i = 0; i < length; i++)
743 void* ptrType = u_arraylist_get(list, i);
750 connType = *(CATransportAdapter_t *)ptrType;
751 CAStopAdapter(connType);
754 // stop retransmission
755 if (NULL != g_retransmissionContext.threadMutex)
757 CARetransmissionStop(&g_retransmissionContext);
761 // delete thread data
762 if (NULL != g_sendThread.threadMutex)
764 CAQueueingThreadStop(&g_sendThread);
768 // delete thread data
769 if (NULL != g_receiveThread.threadMutex)
771 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
772 CAQueueingThreadStop(&gReceiveThread);
776 // destroy thread pool
777 if (NULL != g_threadPoolHandle)
779 ca_thread_pool_free(g_threadPoolHandle);
780 g_threadPoolHandle = NULL;
783 CARetransmissionDestroy(&g_retransmissionContext);
784 CAQueueingThreadDestroy(&g_sendThread);
785 CAQueueingThreadDestroy(&g_receiveThread);
787 // terminate interface adapters by controller
788 CATerminateAdapters();
790 OIC_LOG(DEBUG, TAG, "OUT");
793 void CALogPDUInfo(coap_pdu_t *pdu)
795 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
797 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
799 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
801 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
803 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
805 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
807 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
810 void CAErrorHandler(const CAEndpoint_t *endpoint,
811 const void *data, uint32_t dataLen,
814 OIC_LOG(DEBUG, TAG, "IN");
815 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
816 VERIFY_NON_NULL_VOID(data, TAG, "data");
818 uint32_t code = CA_NOT_FOUND;
819 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
821 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
824 OIC_LOG(ERROR, TAG, "Parse PDU failed");
828 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
829 if (NULL == errorInfo)
831 OIC_LOG(ERROR, TAG, "CAErrorHandler, Memory allocation failed!");
832 coap_delete_pdu(pdu);
836 CAResult_t res = CAGetErrorInfoFromPDU(pdu, errorInfo);
837 if (CA_STATUS_OK != res)
839 OIC_LOG_V(ERROR, TAG, "CAGetErrorInfoFromPDU failed : %d", res);
841 coap_delete_pdu(pdu);
845 errorInfo->result = result;
846 OIC_LOG_V(DEBUG, TAG, "error : %d", result);
847 if (NULL != errorInfo->info.payload)
849 OIC_LOG_V(DEBUG, TAG, "error, payload: %s", errorInfo->info.payload);
852 OIC_LOG(DEBUG, TAG, "error, token");
853 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) errorInfo->info.token,
854 errorInfo->info.tokenLength);
855 OIC_LOG_V(DEBUG, TAG, "CAErrorHandler, msgID : %d", errorInfo->info.messageId);
857 CAEndpoint_t *rep = NULL;
858 rep = CACloneEndpoint(endpoint);
861 OIC_LOG(ERROR, TAG, "CAErrorHandler, CloneEndpoint Failed");
863 coap_delete_pdu(pdu);
867 // store the data at queue.
868 CAData_t *cadata = NULL;
869 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
872 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
873 CADestroyEndpointInternal(rep);
875 coap_delete_pdu(pdu);
879 cadata->remoteEndpoint = rep;
880 cadata->requestInfo = NULL;
881 cadata->responseInfo = NULL;
882 cadata->errorInfo = errorInfo;
883 cadata->dataType = CA_ERROR_DATA;
885 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
886 coap_delete_pdu(pdu);