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");
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);
137 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
140 OIC_LOG(ERROR, TAG, "memory allocation failed !");
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 CAFreeEndpoint(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(info->resourceUri);
188 OICFree(cadata->errorInfo);
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 coap_delete_pdu(pdu);
283 // for retransmission
284 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
286 if (CA_STATUS_OK != res)
288 OIC_LOG_V(INFO, TAG, "retransmission will be not working: %d", res);
289 coap_delete_pdu(pdu);
293 coap_delete_pdu(pdu);
296 else if (SEND_TYPE_MULTICAST == type)
298 OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
300 CAInfo_t *info = &data->requestInfo->info;
302 info->options = data->options;
303 info->numOptions = data->numOptions;
305 pdu = CAGeneratePDU(CA_GET, info);
310 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
311 if (CA_STATUS_OK != res)
313 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
314 coap_delete_pdu(pdu);
318 coap_delete_pdu(pdu);
322 OIC_LOG(DEBUG, TAG, "OUT");
325 static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
327 OIC_LOG(DEBUG, TAG, "IN");
328 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
329 VERIFY_NON_NULL_VOID(data, TAG, "data");
331 uint32_t code = CA_NOT_FOUND;
332 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
337 OIC_LOG(ERROR, TAG, "Parse PDU failed");
341 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
343 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
346 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
347 coap_delete_pdu(pdu);
351 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo);
352 if (CA_STATUS_OK != res)
354 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
356 coap_delete_pdu(pdu);
360 if (NULL != ReqInfo->info.options)
363 for (i = 0; i < ReqInfo->info.numOptions; i++)
365 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
367 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
371 if (NULL != ReqInfo->info.payload)
373 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
375 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
376 if (NULL != ReqInfo->info.token)
378 OIC_LOG(DEBUG, TAG, "Request- token:");
379 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
380 ReqInfo->info.tokenLength);
382 OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
383 // store the data at queue.
384 CAData_t *cadata = NULL;
385 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
388 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
389 CADestroyRequestInfoInternal(ReqInfo);
390 coap_delete_pdu(pdu);
394 cadata->type = SEND_TYPE_UNICAST;
395 cadata->remoteEndpoint = CACloneEndpoint(endpoint);
396 cadata->requestInfo = ReqInfo;
397 cadata->responseInfo = NULL;
398 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
402 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
405 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
406 coap_delete_pdu(pdu);
410 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo);
411 if (CA_STATUS_OK != res)
413 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
415 coap_delete_pdu(pdu);
419 if (NULL != ResInfo->info.options)
422 for (i = 0; i < ResInfo->info.numOptions; i++)
424 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
426 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
430 if (NULL != ResInfo->info.payload)
432 OIC_LOG_V(DEBUG, TAG, "Response- payload: %s", ResInfo->info.payload);
434 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
435 if (NULL != ResInfo->info.token)
437 OIC_LOG(DEBUG, TAG, "Response- token:");
438 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ResInfo->info.token,
439 ResInfo->info.tokenLength);
441 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
443 // store the data at queue.
444 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
447 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
448 CADestroyResponseInfoInternal(ResInfo);
449 coap_delete_pdu(pdu);
453 cadata->type = SEND_TYPE_UNICAST;
454 cadata->remoteEndpoint = CACloneEndpoint(endpoint);
455 cadata->requestInfo = NULL;
457 // for retransmission
458 void *retransmissionPdu = NULL;
459 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
462 // get token from saved data in retransmission list
463 if (retransmissionPdu && CA_EMPTY == code)
465 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
467 if (CA_STATUS_OK != res)
469 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
470 OICFree(ResInfo->info.token);
473 OICFree(retransmissionPdu);
474 cadata->responseInfo = ResInfo;
476 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
481 coap_delete_pdu(pdu);
483 OIC_LOG(DEBUG, TAG, "OUT");
486 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
488 OIC_LOG(DEBUG, TAG, "IN");
490 OIC_LOG(DEBUG, TAG, "OUT");
493 void CAHandleRequestResponseCallbacks()
497 // parse the data and call the callbacks.
501 ca_mutex_lock(g_receiveThread.threadMutex);
503 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
505 ca_mutex_unlock(g_receiveThread.threadMutex);
513 void *msg = item->msg;
521 CAData_t *td = (CAData_t *) msg;
523 if (td->requestInfo && g_requestHandler)
525 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
526 g_requestHandler(td->remoteEndpoint, td->requestInfo);
528 else if (td->responseInfo && g_responseHandler)
530 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
531 g_responseHandler(td->remoteEndpoint, td->responseInfo);
533 else if (td->errorInfo && g_errorHandler)
535 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
536 g_errorHandler(td->remoteEndpoint, td->errorInfo);
539 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;
576 data->options = NULL;
577 data->numOptions = 0;
578 if (NULL != requestInfo->info.options && 0 < requestInfo->info.numOptions)
580 uint8_t numOptions = requestInfo->info.numOptions;
582 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
584 CA_MEMORY_ALLOC_CHECK(headerOption);
586 memcpy(headerOption, requestInfo->info.options, sizeof(CAHeaderOption_t) * numOptions);
588 data->options = headerOption;
589 data->numOptions = numOptions;
593 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
594 OIC_LOG(DEBUG, TAG, "OUT");
597 // memory error label.
599 CAFreeEndpoint(remoteEndpoint);
600 CADestroyRequestInfoInternal(requestInfo);
603 OIC_LOG(DEBUG, TAG, "OUT");
604 return CA_MEMORY_ALLOC_FAILED;
607 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
608 const CAResponseInfo_t *response)
610 OIC_LOG(DEBUG, TAG, "IN");
611 VERIFY_NON_NULL(object, TAG, "object");
612 VERIFY_NON_NULL(response, TAG, "response");
614 if (false == CAIsSelectedNetworkAvailable())
616 return CA_STATUS_FAILED;
619 CAEndpoint_t *remoteEndpoint = NULL;
620 CAResponseInfo_t *responseInfo = NULL;
622 // allocate & initialize
623 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
624 CA_MEMORY_ALLOC_CHECK(data);
626 // clone remote endpoint
627 remoteEndpoint = CACloneEndpoint(object);
628 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
630 // clone response info
631 responseInfo = CACloneResponseInfo(response);
632 CA_MEMORY_ALLOC_CHECK(responseInfo);
635 data->type = SEND_TYPE_UNICAST;
636 data->remoteEndpoint = remoteEndpoint;
637 data->requestInfo = NULL;
638 data->responseInfo = responseInfo;
639 data->options = NULL;
640 data->numOptions = 0;
641 if (NULL != responseInfo->info.options && 0 < responseInfo->info.numOptions)
643 uint8_t numOptions = responseInfo->info.numOptions;
645 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
647 CA_MEMORY_ALLOC_CHECK(headerOption);
649 memcpy(headerOption, responseInfo->info.options, sizeof(CAHeaderOption_t) * numOptions);
651 data->options = headerOption;
652 data->numOptions = numOptions;
656 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
658 OIC_LOG(DEBUG, TAG, "OUT");
661 // memory error label.
663 CAFreeEndpoint(remoteEndpoint);
664 CADestroyResponseInfoInternal(responseInfo);
666 OIC_LOG(DEBUG, TAG, "OUT");
668 return CA_MEMORY_ALLOC_FAILED;
671 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
672 uint8_t tokenLength, const CAHeaderOption_t *options,
675 return CA_NOT_SUPPORTED;
678 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
679 CAErrorCallback errroHandler)
681 OIC_LOG(DEBUG, TAG, "IN");
682 g_requestHandler = ReqHandler;
683 g_responseHandler = RespHandler;
684 g_errorHandler = errroHandler;
685 OIC_LOG(DEBUG, TAG, "OUT");
688 CAResult_t CAInitializeMessageHandler()
690 OIC_LOG(DEBUG, TAG, "IN");
691 CASetPacketReceivedCallback(CAReceivedPacketCallback);
693 CASetNetworkChangeCallback(CANetworkChangedCallback);
694 CASetErrorHandleCallback(CAErrorHandler);
696 // create thread pool
697 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
699 if (res != CA_STATUS_OK)
701 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
705 // send thread initialize
706 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
707 CASendThreadProcess, CADataDestroyer))
709 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
710 return CA_STATUS_FAILED;
714 res = CAQueueingThreadStart(&g_sendThread);
716 if (res != CA_STATUS_OK)
718 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
719 ca_thread_pool_free(g_threadPoolHandle);
720 g_threadPoolHandle = NULL;
724 // receive thread initialize
725 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
726 CAReceiveThreadProcess, CADataDestroyer))
728 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
729 return CA_STATUS_FAILED;
732 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
733 // start receive thread
734 res = CAQueueingThreadStart(&gReceiveThread);
736 if (res != CA_STATUS_OK)
738 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
743 // retransmission initialize
744 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
745 CATimeoutCallback, NULL);
747 // start retransmission
748 res = CARetransmissionStart(&g_retransmissionContext);
750 if (res != CA_STATUS_OK)
752 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
756 // initialize interface adapters by controller
757 CAInitializeAdapters(g_threadPoolHandle);
758 OIC_LOG(DEBUG, TAG, "OUT");
762 void CATerminateMessageHandler()
764 OIC_LOG(DEBUG, TAG, "IN");
765 CATransportAdapter_t connType;
766 u_arraylist_t *list = CAGetSelectedNetworkList();
767 uint32_t length = u_arraylist_length(list);
770 for (i = 0; i < length; i++)
772 void* ptrType = u_arraylist_get(list, i);
779 connType = *(CATransportAdapter_t *)ptrType;
780 CAStopAdapter(connType);
783 // stop retransmission
784 if (NULL != g_retransmissionContext.threadMutex)
786 CARetransmissionStop(&g_retransmissionContext);
790 // delete thread data
791 if (NULL != g_sendThread.threadMutex)
793 CAQueueingThreadStop(&g_sendThread);
797 // delete thread data
798 if (NULL != g_receiveThread.threadMutex)
800 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
801 CAQueueingThreadStop(&gReceiveThread);
805 // destroy thread pool
806 if (NULL != g_threadPoolHandle)
808 ca_thread_pool_free(g_threadPoolHandle);
809 g_threadPoolHandle = NULL;
812 CARetransmissionDestroy(&g_retransmissionContext);
813 CAQueueingThreadDestroy(&g_sendThread);
814 CAQueueingThreadDestroy(&g_receiveThread);
816 // terminate interface adapters by controller
817 CATerminateAdapters();
819 OIC_LOG(DEBUG, TAG, "OUT");
822 void CALogPDUInfo(coap_pdu_t *pdu)
824 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
826 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
828 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
830 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
832 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
834 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
836 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
839 void CAErrorHandler(const CAEndpoint_t *endpoint,
840 const void *data, uint32_t dataLen,
843 OIC_LOG(DEBUG, TAG, "IN");
844 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
845 VERIFY_NON_NULL_VOID(data, TAG, "data");
847 uint32_t code = CA_NOT_FOUND;
848 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
850 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
853 OIC_LOG(ERROR, TAG, "Parse PDU failed");
857 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
858 if (NULL == errorInfo)
860 OIC_LOG(ERROR, TAG, "CAErrorHandler, Memory allocation failed!");
861 coap_delete_pdu(pdu);
865 CAResult_t res = CAGetErrorInfoFromPDU(pdu, errorInfo);
866 if (CA_STATUS_OK != res)
868 OIC_LOG_V(ERROR, TAG, "CAGetErrorInfoFromPDU failed : %d", res);
870 coap_delete_pdu(pdu);
874 errorInfo->result = result;
875 OIC_LOG_V(DEBUG, TAG, "error : %d", result);
876 if (NULL != errorInfo->info.payload)
878 OIC_LOG_V(DEBUG, TAG, "error, payload: %s", errorInfo->info.payload);
881 OIC_LOG(DEBUG, TAG, "error, token");
882 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) errorInfo->info.token,
883 errorInfo->info.tokenLength);
884 OIC_LOG_V(DEBUG, TAG, "CAErrorHandler, msgID : %d", errorInfo->info.messageId);
886 CAEndpoint_t *rep = NULL;
887 rep = CACloneEndpoint(endpoint);
890 OIC_LOG(ERROR, TAG, "CAErrorHandler, CloneEndpoint Failed");
892 coap_delete_pdu(pdu);
896 // store the data at queue.
897 CAData_t *cadata = NULL;
898 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
901 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
904 coap_delete_pdu(pdu);
908 cadata->remoteEndpoint = rep;
909 cadata->requestInfo = NULL;
910 cadata->responseInfo = NULL;
911 cadata->errorInfo = errorInfo;
912 cadata->dataType = CA_ERROR_DATA;
914 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
915 coap_delete_pdu(pdu);