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()
498 // parse the data and call the callbacks.
502 ca_mutex_lock(g_receiveThread.threadMutex);
504 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
506 ca_mutex_unlock(g_receiveThread.threadMutex);
514 void *msg = item->msg;
522 CAData_t *td = (CAData_t *) msg;
524 if (td->requestInfo && g_requestHandler)
526 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
527 g_requestHandler(td->remoteEndpoint, td->requestInfo);
529 else if (td->responseInfo && g_responseHandler)
531 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
532 g_responseHandler(td->remoteEndpoint, td->responseInfo);
534 else if (td->errorInfo && g_errorHandler)
536 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
537 g_errorHandler(td->remoteEndpoint, td->errorInfo);
540 CADataDestroyer(msg, sizeof(CAData_t));
543 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
546 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
548 OIC_LOG(DEBUG, TAG, "IN");
550 VERIFY_NON_NULL(object, TAG, "object");
551 VERIFY_NON_NULL(request, TAG, "request");
553 if (false == CAIsSelectedNetworkAvailable())
555 return CA_STATUS_FAILED;
558 CAEndpoint_t *remoteEndpoint = NULL;
559 CARequestInfo_t *requestInfo = NULL;
560 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
561 CA_MEMORY_ALLOC_CHECK(data);
563 // clone remote endpoint
564 remoteEndpoint = CACloneEndpoint(object);
565 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
567 // clone request info
568 requestInfo = CACloneRequestInfo(request);
569 CA_MEMORY_ALLOC_CHECK(requestInfo);
572 data->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
573 data->remoteEndpoint = remoteEndpoint;
574 data->requestInfo = requestInfo;
575 data->responseInfo = NULL;
578 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
579 OIC_LOG(DEBUG, TAG, "OUT");
582 // memory error label.
584 CAAdapterFreeEndpoint(remoteEndpoint);
585 CADestroyRequestInfoInternal(requestInfo);
588 OIC_LOG(DEBUG, TAG, "OUT");
589 return CA_MEMORY_ALLOC_FAILED;
592 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
593 const CAResponseInfo_t *response)
595 OIC_LOG(DEBUG, TAG, "IN");
596 VERIFY_NON_NULL(object, TAG, "object");
597 VERIFY_NON_NULL(response, TAG, "response");
599 if (false == CAIsSelectedNetworkAvailable())
601 return CA_STATUS_FAILED;
604 CAEndpoint_t *remoteEndpoint = NULL;
605 CAResponseInfo_t *responseInfo = NULL;
607 // allocate & initialize
608 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
609 CA_MEMORY_ALLOC_CHECK(data);
611 // clone remote endpoint
612 remoteEndpoint = CACloneEndpoint(object);
613 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
615 // clone response info
616 responseInfo = CACloneResponseInfo(response);
617 CA_MEMORY_ALLOC_CHECK(responseInfo);
620 data->type = SEND_TYPE_UNICAST;
621 data->remoteEndpoint = remoteEndpoint;
622 data->requestInfo = NULL;
623 data->responseInfo = responseInfo;
626 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
628 OIC_LOG(DEBUG, TAG, "OUT");
631 // memory error label.
633 CAAdapterFreeEndpoint(remoteEndpoint);
634 CADestroyResponseInfoInternal(responseInfo);
636 OIC_LOG(DEBUG, TAG, "OUT");
638 return CA_MEMORY_ALLOC_FAILED;
641 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
642 uint8_t tokenLength, const CAHeaderOption_t *options,
645 return CA_NOT_SUPPORTED;
648 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
649 CAErrorCallback errroHandler)
651 OIC_LOG(DEBUG, TAG, "IN");
652 g_requestHandler = ReqHandler;
653 g_responseHandler = RespHandler;
654 g_errorHandler = errroHandler;
655 OIC_LOG(DEBUG, TAG, "OUT");
658 CAResult_t CAInitializeMessageHandler()
660 OIC_LOG(DEBUG, TAG, "IN");
661 CASetPacketReceivedCallback(CAReceivedPacketCallback);
663 CASetNetworkChangeCallback(CANetworkChangedCallback);
664 CASetErrorHandleCallback(CAErrorHandler);
666 // create thread pool
667 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
669 if (res != CA_STATUS_OK)
671 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
675 // send thread initialize
676 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
677 CASendThreadProcess, CADataDestroyer))
679 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
680 return CA_STATUS_FAILED;
684 res = CAQueueingThreadStart(&g_sendThread);
686 if (res != CA_STATUS_OK)
688 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
689 ca_thread_pool_free(g_threadPoolHandle);
690 g_threadPoolHandle = NULL;
694 // receive thread initialize
695 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
696 CAReceiveThreadProcess, CADataDestroyer))
698 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
699 return CA_STATUS_FAILED;
702 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
703 // start receive thread
704 res = CAQueueingThreadStart(&gReceiveThread);
706 if (res != CA_STATUS_OK)
708 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
713 // retransmission initialize
714 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
715 CATimeoutCallback, NULL);
717 // start retransmission
718 res = CARetransmissionStart(&g_retransmissionContext);
720 if (res != CA_STATUS_OK)
722 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
726 // initialize interface adapters by controller
727 CAInitializeAdapters(g_threadPoolHandle);
728 OIC_LOG(DEBUG, TAG, "OUT");
732 void CATerminateMessageHandler()
734 OIC_LOG(DEBUG, TAG, "IN");
735 CATransportAdapter_t connType;
736 u_arraylist_t *list = CAGetSelectedNetworkList();
737 uint32_t length = u_arraylist_length(list);
740 for (i = 0; i < length; i++)
742 void* ptrType = u_arraylist_get(list, i);
749 connType = *(CATransportAdapter_t *)ptrType;
750 CAStopAdapter(connType);
753 // stop retransmission
754 if (NULL != g_retransmissionContext.threadMutex)
756 CARetransmissionStop(&g_retransmissionContext);
760 // delete thread data
761 if (NULL != g_sendThread.threadMutex)
763 CAQueueingThreadStop(&g_sendThread);
767 // delete thread data
768 if (NULL != g_receiveThread.threadMutex)
770 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
771 CAQueueingThreadStop(&gReceiveThread);
775 // destroy thread pool
776 if (NULL != g_threadPoolHandle)
778 ca_thread_pool_free(g_threadPoolHandle);
779 g_threadPoolHandle = NULL;
782 CARetransmissionDestroy(&g_retransmissionContext);
783 CAQueueingThreadDestroy(&g_sendThread);
784 CAQueueingThreadDestroy(&g_receiveThread);
786 // terminate interface adapters by controller
787 CATerminateAdapters();
789 OIC_LOG(DEBUG, TAG, "OUT");
792 void CALogPDUInfo(coap_pdu_t *pdu)
794 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
796 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
798 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
800 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
802 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
804 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
806 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
809 void CAErrorHandler(const CAEndpoint_t *endpoint,
810 const void *data, uint32_t dataLen,
813 OIC_LOG(DEBUG, TAG, "IN");
814 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
815 VERIFY_NON_NULL_VOID(data, TAG, "data");
817 uint32_t code = CA_NOT_FOUND;
818 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
820 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
823 OIC_LOG(ERROR, TAG, "Parse PDU failed");
827 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
828 if (NULL == errorInfo)
830 OIC_LOG(ERROR, TAG, "CAErrorHandler, Memory allocation failed!");
831 coap_delete_pdu(pdu);
835 CAResult_t res = CAGetErrorInfoFromPDU(pdu, errorInfo);
836 if (CA_STATUS_OK != res)
838 OIC_LOG_V(ERROR, TAG, "CAGetErrorInfoFromPDU failed : %d", res);
840 coap_delete_pdu(pdu);
844 errorInfo->result = result;
845 OIC_LOG_V(DEBUG, TAG, "error : %d", result);
846 if (NULL != errorInfo->info.payload)
848 OIC_LOG_V(DEBUG, TAG, "error, payload: %s", errorInfo->info.payload);
851 OIC_LOG(DEBUG, TAG, "error, token");
852 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) errorInfo->info.token,
853 errorInfo->info.tokenLength);
854 OIC_LOG_V(DEBUG, TAG, "CAErrorHandler, msgID : %d", errorInfo->info.messageId);
856 CAEndpoint_t *rep = NULL;
857 rep = CACloneEndpoint(endpoint);
860 OIC_LOG(ERROR, TAG, "CAErrorHandler, CloneEndpoint Failed");
862 coap_delete_pdu(pdu);
866 // store the data at queue.
867 CAData_t *cadata = NULL;
868 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
871 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
872 CADestroyEndpointInternal(rep);
874 coap_delete_pdu(pdu);
878 cadata->remoteEndpoint = rep;
879 cadata->requestInfo = NULL;
880 cadata->responseInfo = NULL;
881 cadata->errorInfo = errorInfo;
882 cadata->dataType = CA_ERROR_DATA;
884 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
885 coap_delete_pdu(pdu);