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(cadata->errorInfo);
191 OIC_LOG(DEBUG, TAG, "OUT");
194 static void CAReceiveThreadProcess(void *threadData)
196 OIC_LOG(DEBUG, TAG, "IN");
197 // Currently not supported
198 // This will be enabled when RI supports multi threading
199 #ifndef SINGLE_HANDLE
200 CAData_t *data = (CAData_t *) threadData;
204 OIC_LOG(ERROR, TAG, "thread data error!!");
208 // parse the data and call the callbacks.
211 CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
215 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
219 if (data->requestInfo && g_requestHandler)
221 g_requestHandler(rep, data->requestInfo);
223 else if (data->responseInfo && g_responseHandler)
225 g_responseHandler(rep, data->responseInfo);
227 else if (data->errorInfo && g_errorHandler)
229 g_errorHandler(rep, data->errorInfo);
232 #endif /* SINGLE_HANDLE */
233 OIC_LOG(DEBUG, TAG, "OUT");
236 static void CASendThreadProcess(void *threadData)
238 OIC_LOG(DEBUG, TAG, "IN");
239 CAData_t *data = (CAData_t *) threadData;
241 VERIFY_NON_NULL_VOID(data, TAG, "data");
242 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");
244 CAResult_t res = CA_STATUS_FAILED;
246 CASendDataType_t type = data->type;
248 coap_pdu_t *pdu = NULL;
250 if (SEND_TYPE_UNICAST == type)
253 if (NULL != data->requestInfo)
255 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
257 pdu = CAGeneratePDU(data->requestInfo->method, &data->requestInfo->info);
259 else if (NULL != data->responseInfo)
261 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
263 pdu = CAGeneratePDU(data->responseInfo->result, &data->responseInfo->info);
267 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
270 // interface controller function call.
275 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
276 if (CA_STATUS_OK != res)
278 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
279 coap_delete_pdu(pdu);
282 // for retransmission
283 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
285 if (CA_STATUS_OK != res)
287 OIC_LOG_V(INFO, TAG, "retransmission will be not working: %d", res);
288 coap_delete_pdu(pdu);
292 coap_delete_pdu(pdu);
295 else if (SEND_TYPE_MULTICAST == type)
297 OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
299 CAInfo_t *info = &data->requestInfo->info;
301 info->options = data->options;
302 info->numOptions = data->numOptions;
304 pdu = CAGeneratePDU(CA_GET, info);
309 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
310 if (CA_STATUS_OK != res)
312 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
313 coap_delete_pdu(pdu);
317 coap_delete_pdu(pdu);
321 OIC_LOG(DEBUG, TAG, "OUT");
324 static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
326 OIC_LOG(DEBUG, TAG, "IN");
327 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
328 VERIFY_NON_NULL_VOID(data, TAG, "data");
330 uint32_t code = CA_NOT_FOUND;
331 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
336 OIC_LOG(ERROR, TAG, "Parse PDU failed");
340 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
342 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
345 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
346 coap_delete_pdu(pdu);
350 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo);
351 if (CA_STATUS_OK != res)
353 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
355 coap_delete_pdu(pdu);
359 if (NULL != ReqInfo->info.options)
362 for (i = 0; i < ReqInfo->info.numOptions; i++)
364 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
366 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
370 if (NULL != ReqInfo->info.payload)
372 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
374 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
375 if (NULL != ReqInfo->info.token)
377 OIC_LOG(DEBUG, TAG, "Request- token:");
378 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
379 ReqInfo->info.tokenLength);
381 OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
382 // store the data at queue.
383 CAData_t *cadata = NULL;
384 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
387 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
388 CADestroyRequestInfoInternal(ReqInfo);
389 coap_delete_pdu(pdu);
393 cadata->type = SEND_TYPE_UNICAST;
394 cadata->remoteEndpoint = CACloneEndpoint(endpoint);
395 cadata->requestInfo = ReqInfo;
396 cadata->responseInfo = NULL;
397 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
401 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
404 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
405 coap_delete_pdu(pdu);
409 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo);
410 if (CA_STATUS_OK != res)
412 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
414 coap_delete_pdu(pdu);
418 if (NULL != ResInfo->info.options)
421 for (i = 0; i < ResInfo->info.numOptions; i++)
423 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
425 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
429 if (NULL != ResInfo->info.payload)
431 OIC_LOG_V(DEBUG, TAG, "Response- payload: %s", ResInfo->info.payload);
433 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
434 if (NULL != ResInfo->info.token)
436 OIC_LOG(DEBUG, TAG, "Response- token:");
437 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ResInfo->info.token,
438 ResInfo->info.tokenLength);
440 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
442 // store the data at queue.
443 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
446 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
447 CADestroyResponseInfoInternal(ResInfo);
448 coap_delete_pdu(pdu);
452 cadata->type = SEND_TYPE_UNICAST;
453 cadata->remoteEndpoint = CACloneEndpoint(endpoint);
454 cadata->requestInfo = NULL;
456 // for retransmission
457 void *retransmissionPdu = NULL;
458 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
461 // get token from saved data in retransmission list
462 if (retransmissionPdu && CA_EMPTY == code)
464 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
466 if (CA_STATUS_OK != res)
468 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
469 OICFree(ResInfo->info.token);
472 OICFree(retransmissionPdu);
473 cadata->responseInfo = ResInfo;
475 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
480 coap_delete_pdu(pdu);
482 OIC_LOG(DEBUG, TAG, "OUT");
485 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
487 OIC_LOG(DEBUG, TAG, "IN");
489 OIC_LOG(DEBUG, TAG, "OUT");
492 void CAHandleRequestResponseCallbacks()
496 // parse the data and call the callbacks.
500 ca_mutex_lock(g_receiveThread.threadMutex);
502 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
504 ca_mutex_unlock(g_receiveThread.threadMutex);
512 void *msg = item->msg;
520 CAData_t *td = (CAData_t *) msg;
522 if (td->requestInfo && g_requestHandler)
524 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
525 g_requestHandler(td->remoteEndpoint, td->requestInfo);
527 else if (td->responseInfo && g_responseHandler)
529 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
530 g_responseHandler(td->remoteEndpoint, td->responseInfo);
532 else if (td->errorInfo && g_errorHandler)
534 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
535 g_errorHandler(td->remoteEndpoint, td->errorInfo);
538 CADataDestroyer(msg, sizeof(CAData_t));
541 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
544 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
546 OIC_LOG(DEBUG, TAG, "IN");
548 VERIFY_NON_NULL(object, TAG, "object");
549 VERIFY_NON_NULL(request, TAG, "request");
551 if (false == CAIsSelectedNetworkAvailable())
553 return CA_STATUS_FAILED;
556 CAEndpoint_t *remoteEndpoint = NULL;
557 CARequestInfo_t *requestInfo = NULL;
558 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
559 CA_MEMORY_ALLOC_CHECK(data);
561 // clone remote endpoint
562 remoteEndpoint = CACloneEndpoint(object);
563 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
565 // clone request info
566 requestInfo = CACloneRequestInfo(request);
567 CA_MEMORY_ALLOC_CHECK(requestInfo);
570 data->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
571 data->remoteEndpoint = remoteEndpoint;
572 data->requestInfo = requestInfo;
573 data->responseInfo = NULL;
574 data->options = NULL;
575 data->numOptions = 0;
576 if (NULL != requestInfo->info.options && 0 < requestInfo->info.numOptions)
578 uint8_t numOptions = requestInfo->info.numOptions;
580 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
582 CA_MEMORY_ALLOC_CHECK(headerOption);
584 memcpy(headerOption, requestInfo->info.options, sizeof(CAHeaderOption_t) * numOptions);
586 data->options = headerOption;
587 data->numOptions = numOptions;
591 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
592 OIC_LOG(DEBUG, TAG, "OUT");
595 // memory error label.
597 CAFreeEndpoint(remoteEndpoint);
598 CADestroyRequestInfoInternal(requestInfo);
601 OIC_LOG(DEBUG, TAG, "OUT");
602 return CA_MEMORY_ALLOC_FAILED;
605 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
606 const CAResponseInfo_t *response)
608 OIC_LOG(DEBUG, TAG, "IN");
609 VERIFY_NON_NULL(object, TAG, "object");
610 VERIFY_NON_NULL(response, TAG, "response");
612 if (false == CAIsSelectedNetworkAvailable())
614 return CA_STATUS_FAILED;
617 CAEndpoint_t *remoteEndpoint = NULL;
618 CAResponseInfo_t *responseInfo = NULL;
620 // allocate & initialize
621 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
622 CA_MEMORY_ALLOC_CHECK(data);
624 // clone remote endpoint
625 remoteEndpoint = CACloneEndpoint(object);
626 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
628 // clone response info
629 responseInfo = CACloneResponseInfo(response);
630 CA_MEMORY_ALLOC_CHECK(responseInfo);
633 data->type = SEND_TYPE_UNICAST;
634 data->remoteEndpoint = remoteEndpoint;
635 data->requestInfo = NULL;
636 data->responseInfo = responseInfo;
637 data->options = NULL;
638 data->numOptions = 0;
639 if (NULL != responseInfo->info.options && 0 < responseInfo->info.numOptions)
641 uint8_t numOptions = responseInfo->info.numOptions;
643 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
645 CA_MEMORY_ALLOC_CHECK(headerOption);
647 memcpy(headerOption, responseInfo->info.options, sizeof(CAHeaderOption_t) * numOptions);
649 data->options = headerOption;
650 data->numOptions = numOptions;
654 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
656 OIC_LOG(DEBUG, TAG, "OUT");
659 // memory error label.
661 CAFreeEndpoint(remoteEndpoint);
662 CADestroyResponseInfoInternal(responseInfo);
664 OIC_LOG(DEBUG, TAG, "OUT");
666 return CA_MEMORY_ALLOC_FAILED;
669 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
670 uint8_t tokenLength, const CAHeaderOption_t *options,
673 return CA_NOT_SUPPORTED;
676 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
677 CAErrorCallback errroHandler)
679 OIC_LOG(DEBUG, TAG, "IN");
680 g_requestHandler = ReqHandler;
681 g_responseHandler = RespHandler;
682 g_errorHandler = errroHandler;
683 OIC_LOG(DEBUG, TAG, "OUT");
686 CAResult_t CAInitializeMessageHandler()
688 OIC_LOG(DEBUG, TAG, "IN");
689 CASetPacketReceivedCallback(CAReceivedPacketCallback);
691 CASetNetworkChangeCallback(CANetworkChangedCallback);
692 CASetErrorHandleCallback(CAErrorHandler);
694 // create thread pool
695 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
697 if (res != CA_STATUS_OK)
699 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
703 // send thread initialize
704 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
705 CASendThreadProcess, CADataDestroyer))
707 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
708 return CA_STATUS_FAILED;
712 res = CAQueueingThreadStart(&g_sendThread);
714 if (res != CA_STATUS_OK)
716 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
717 ca_thread_pool_free(g_threadPoolHandle);
718 g_threadPoolHandle = NULL;
722 // receive thread initialize
723 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
724 CAReceiveThreadProcess, CADataDestroyer))
726 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
727 return CA_STATUS_FAILED;
730 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
731 // start receive thread
732 res = CAQueueingThreadStart(&gReceiveThread);
734 if (res != CA_STATUS_OK)
736 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
741 // retransmission initialize
742 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
743 CATimeoutCallback, NULL);
745 // start retransmission
746 res = CARetransmissionStart(&g_retransmissionContext);
748 if (res != CA_STATUS_OK)
750 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
754 // initialize interface adapters by controller
755 CAInitializeAdapters(g_threadPoolHandle);
756 OIC_LOG(DEBUG, TAG, "OUT");
760 void CATerminateMessageHandler()
762 OIC_LOG(DEBUG, TAG, "IN");
763 CATransportAdapter_t connType;
764 u_arraylist_t *list = CAGetSelectedNetworkList();
765 uint32_t length = u_arraylist_length(list);
768 for (i = 0; i < length; i++)
770 void* ptrType = u_arraylist_get(list, i);
777 connType = *(CATransportAdapter_t *)ptrType;
778 CAStopAdapter(connType);
781 // stop retransmission
782 if (NULL != g_retransmissionContext.threadMutex)
784 CARetransmissionStop(&g_retransmissionContext);
788 // delete thread data
789 if (NULL != g_sendThread.threadMutex)
791 CAQueueingThreadStop(&g_sendThread);
795 // delete thread data
796 if (NULL != g_receiveThread.threadMutex)
798 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
799 CAQueueingThreadStop(&gReceiveThread);
803 // destroy thread pool
804 if (NULL != g_threadPoolHandle)
806 ca_thread_pool_free(g_threadPoolHandle);
807 g_threadPoolHandle = NULL;
810 CARetransmissionDestroy(&g_retransmissionContext);
811 CAQueueingThreadDestroy(&g_sendThread);
812 CAQueueingThreadDestroy(&g_receiveThread);
814 // terminate interface adapters by controller
815 CATerminateAdapters();
817 OIC_LOG(DEBUG, TAG, "OUT");
820 void CALogPDUInfo(coap_pdu_t *pdu)
822 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
824 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
826 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
828 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
830 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
832 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
834 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
837 void CAErrorHandler(const CAEndpoint_t *endpoint,
838 const void *data, uint32_t dataLen,
841 OIC_LOG(DEBUG, TAG, "IN");
842 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
843 VERIFY_NON_NULL_VOID(data, TAG, "data");
845 uint32_t code = CA_NOT_FOUND;
846 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
848 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
851 OIC_LOG(ERROR, TAG, "Parse PDU failed");
855 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
856 if (NULL == errorInfo)
858 OIC_LOG(ERROR, TAG, "CAErrorHandler, Memory allocation failed!");
859 coap_delete_pdu(pdu);
863 CAResult_t res = CAGetErrorInfoFromPDU(pdu, errorInfo);
864 if (CA_STATUS_OK != res)
866 OIC_LOG_V(ERROR, TAG, "CAGetErrorInfoFromPDU failed : %d", res);
868 coap_delete_pdu(pdu);
872 errorInfo->result = result;
873 OIC_LOG_V(DEBUG, TAG, "error : %d", result);
874 if (NULL != errorInfo->info.payload)
876 OIC_LOG_V(DEBUG, TAG, "error, payload: %s", errorInfo->info.payload);
879 OIC_LOG(DEBUG, TAG, "error, token");
880 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) errorInfo->info.token,
881 errorInfo->info.tokenLength);
882 OIC_LOG_V(DEBUG, TAG, "CAErrorHandler, msgID : %d", errorInfo->info.messageId);
884 CAEndpoint_t *rep = NULL;
885 rep = CACloneEndpoint(endpoint);
888 OIC_LOG(ERROR, TAG, "CAErrorHandler, CloneEndpoint Failed");
890 coap_delete_pdu(pdu);
894 // store the data at queue.
895 CAData_t *cadata = NULL;
896 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
899 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
902 coap_delete_pdu(pdu);
906 cadata->remoteEndpoint = rep;
907 cadata->requestInfo = NULL;
908 cadata->responseInfo = NULL;
909 cadata->errorInfo = errorInfo;
910 cadata->dataType = CA_ERROR_DATA;
912 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
913 coap_delete_pdu(pdu);