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"
43 #define TAG PCF("CA_MSG_HNDLR")
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 OIC_LOG(DEBUG,TAG,"Unicast message");
255 if (NULL != data->requestInfo)
257 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
259 pdu = CAGeneratePDU(data->requestInfo->method, &data->requestInfo->info);
261 else if (NULL != data->responseInfo)
263 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
265 pdu = CAGeneratePDU(data->responseInfo->result, &data->responseInfo->info);
269 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
273 // interface controller function call.
278 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
279 if (CA_STATUS_OK != res)
281 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
282 coap_delete_pdu(pdu);
285 // for retransmission
286 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
288 if (CA_STATUS_OK != res)
290 OIC_LOG_V(INFO, TAG, "retransmission will be not working: %d", res);
291 coap_delete_pdu(pdu);
295 coap_delete_pdu(pdu);
299 OIC_LOG_V(ERROR,TAG,"Failed to generate unicast PDU");
303 else if (SEND_TYPE_MULTICAST == type)
305 OIC_LOG(DEBUG,TAG,"Multicast message");
306 if (NULL != data->requestInfo)
308 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
309 CAInfo_t *info = &data->requestInfo->info;
311 info->options = data->options;
312 info->numOptions = data->numOptions;
314 pdu = CAGeneratePDU(CA_GET, info);
319 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
320 if (CA_STATUS_OK != res)
322 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
323 coap_delete_pdu(pdu);
327 coap_delete_pdu(pdu);
331 OIC_LOG_V(ERROR,TAG,"Failed to generate multicast PDU");
336 OIC_LOG_V(ERROR, TAG, "request info is empty");
340 OIC_LOG(DEBUG, TAG, "OUT");
343 static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
345 OIC_LOG(DEBUG, TAG, "IN");
346 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
347 VERIFY_NON_NULL_VOID(data, TAG, "data");
349 uint32_t code = CA_NOT_FOUND;
350 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
355 OIC_LOG(ERROR, TAG, "Parse PDU failed");
359 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
361 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
364 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
365 coap_delete_pdu(pdu);
369 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo);
370 if (CA_STATUS_OK != res)
372 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
374 coap_delete_pdu(pdu);
378 if (NULL != ReqInfo->info.options)
381 for (i = 0; i < ReqInfo->info.numOptions; i++)
383 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
385 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
389 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
390 if (NULL != ReqInfo->info.token)
392 OIC_LOG(DEBUG, TAG, "Request- token:");
393 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
394 ReqInfo->info.tokenLength);
397 OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
398 // store the data at queue.
399 CAData_t *cadata = NULL;
400 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
403 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
404 CADestroyRequestInfoInternal(ReqInfo);
405 coap_delete_pdu(pdu);
409 cadata->type = SEND_TYPE_UNICAST;
410 cadata->remoteEndpoint = CACloneEndpoint(endpoint);
411 cadata->requestInfo = ReqInfo;
412 cadata->responseInfo = NULL;
413 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
417 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
420 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
421 coap_delete_pdu(pdu);
425 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo);
426 if (CA_STATUS_OK != res)
428 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
430 coap_delete_pdu(pdu);
434 if (NULL != ResInfo->info.options)
437 for (i = 0; i < ResInfo->info.numOptions; i++)
439 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
441 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
445 if (NULL != ResInfo->info.payload)
447 OIC_LOG_V(DEBUG, TAG, "Response- payload: %p(%u) from %s", ResInfo->info.payload,
448 ResInfo->info.payloadSize, endpoint->addr);
450 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
451 if (NULL != ResInfo->info.token)
453 OIC_LOG(DEBUG, TAG, "Response- token:");
454 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ResInfo->info.token,
455 ResInfo->info.tokenLength);
457 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
459 // store the data at queue.
460 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
463 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
464 CADestroyResponseInfoInternal(ResInfo);
465 coap_delete_pdu(pdu);
469 cadata->type = SEND_TYPE_UNICAST;
470 cadata->remoteEndpoint = CACloneEndpoint(endpoint);
471 cadata->requestInfo = NULL;
473 // for retransmission
474 void *retransmissionPdu = NULL;
475 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
478 // get token from saved data in retransmission list
479 if (retransmissionPdu && CA_EMPTY == code)
481 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
483 if (CA_STATUS_OK != res)
485 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
486 OICFree(ResInfo->info.token);
489 OICFree(retransmissionPdu);
490 cadata->responseInfo = ResInfo;
492 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
497 coap_delete_pdu(pdu);
499 OIC_LOG(DEBUG, TAG, "OUT");
502 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
504 OIC_LOG(DEBUG, TAG, "IN");
506 OIC_LOG(DEBUG, TAG, "OUT");
509 void CAHandleRequestResponseCallbacks()
513 // parse the data and call the callbacks.
517 ca_mutex_lock(g_receiveThread.threadMutex);
519 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
521 ca_mutex_unlock(g_receiveThread.threadMutex);
529 void *msg = item->msg;
537 CAData_t *td = (CAData_t *) msg;
539 if (td->requestInfo && g_requestHandler)
541 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
542 g_requestHandler(td->remoteEndpoint, td->requestInfo);
544 else if (td->responseInfo && g_responseHandler)
546 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
547 g_responseHandler(td->remoteEndpoint, td->responseInfo);
549 else if (td->errorInfo && g_errorHandler)
551 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
552 g_errorHandler(td->remoteEndpoint, td->errorInfo);
555 CADataDestroyer(msg, sizeof(CAData_t));
559 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
562 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
564 OIC_LOG(DEBUG, TAG, "IN");
566 VERIFY_NON_NULL(object, TAG, "object");
567 VERIFY_NON_NULL(request, TAG, "request");
569 if (false == CAIsSelectedNetworkAvailable())
571 return CA_STATUS_FAILED;
574 CAEndpoint_t *remoteEndpoint = NULL;
575 CARequestInfo_t *requestInfo = NULL;
576 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
577 CA_MEMORY_ALLOC_CHECK(data);
579 // clone remote endpoint
580 remoteEndpoint = CACloneEndpoint(object);
581 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
583 // clone request info
584 requestInfo = CACloneRequestInfo(request);
585 CA_MEMORY_ALLOC_CHECK(requestInfo);
588 data->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
589 data->remoteEndpoint = remoteEndpoint;
590 data->requestInfo = requestInfo;
591 data->responseInfo = NULL;
592 data->options = NULL;
593 data->numOptions = 0;
594 if (NULL != requestInfo->info.options && 0 < requestInfo->info.numOptions)
596 uint8_t numOptions = requestInfo->info.numOptions;
598 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
600 CA_MEMORY_ALLOC_CHECK(headerOption);
602 memcpy(headerOption, requestInfo->info.options, sizeof(CAHeaderOption_t) * numOptions);
604 data->options = headerOption;
605 data->numOptions = numOptions;
609 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
610 OIC_LOG(DEBUG, TAG, "OUT");
613 // memory error label.
615 CAFreeEndpoint(remoteEndpoint);
616 CADestroyRequestInfoInternal(requestInfo);
619 OIC_LOG(DEBUG, TAG, "OUT");
620 return CA_MEMORY_ALLOC_FAILED;
623 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
624 const CAResponseInfo_t *response)
626 OIC_LOG(DEBUG, TAG, "IN");
627 VERIFY_NON_NULL(object, TAG, "object");
628 VERIFY_NON_NULL(response, TAG, "response");
630 if (false == CAIsSelectedNetworkAvailable())
632 return CA_STATUS_FAILED;
635 CAEndpoint_t *remoteEndpoint = NULL;
636 CAResponseInfo_t *responseInfo = NULL;
638 // allocate & initialize
639 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
640 CA_MEMORY_ALLOC_CHECK(data);
642 // clone remote endpoint
643 remoteEndpoint = CACloneEndpoint(object);
644 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
646 // clone response info
647 responseInfo = CACloneResponseInfo(response);
648 CA_MEMORY_ALLOC_CHECK(responseInfo);
651 data->type = SEND_TYPE_UNICAST;
652 data->remoteEndpoint = remoteEndpoint;
653 data->requestInfo = NULL;
654 data->responseInfo = responseInfo;
655 data->options = NULL;
656 data->numOptions = 0;
657 if (NULL != responseInfo->info.options && 0 < responseInfo->info.numOptions)
659 uint8_t numOptions = responseInfo->info.numOptions;
661 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
663 CA_MEMORY_ALLOC_CHECK(headerOption);
665 memcpy(headerOption, responseInfo->info.options, sizeof(CAHeaderOption_t) * numOptions);
667 data->options = headerOption;
668 data->numOptions = numOptions;
672 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
674 OIC_LOG(DEBUG, TAG, "OUT");
677 // memory error label.
679 CAFreeEndpoint(remoteEndpoint);
680 CADestroyResponseInfoInternal(responseInfo);
682 OIC_LOG(DEBUG, TAG, "OUT");
684 return CA_MEMORY_ALLOC_FAILED;
687 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
688 uint8_t tokenLength, const CAHeaderOption_t *options,
691 return CA_NOT_SUPPORTED;
694 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
695 CAErrorCallback errroHandler)
697 OIC_LOG(DEBUG, TAG, "IN");
698 g_requestHandler = ReqHandler;
699 g_responseHandler = RespHandler;
700 g_errorHandler = errroHandler;
701 OIC_LOG(DEBUG, TAG, "OUT");
704 CAResult_t CAInitializeMessageHandler()
706 OIC_LOG(DEBUG, TAG, "IN");
707 CASetPacketReceivedCallback(CAReceivedPacketCallback);
709 CASetNetworkChangeCallback(CANetworkChangedCallback);
710 CASetErrorHandleCallback(CAErrorHandler);
712 // create thread pool
713 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
715 if (res != CA_STATUS_OK)
717 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
721 // send thread initialize
722 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
723 CASendThreadProcess, CADataDestroyer))
725 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
726 return CA_STATUS_FAILED;
730 res = CAQueueingThreadStart(&g_sendThread);
732 if (res != CA_STATUS_OK)
734 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
735 ca_thread_pool_free(g_threadPoolHandle);
736 g_threadPoolHandle = NULL;
740 // receive thread initialize
741 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
742 CAReceiveThreadProcess, CADataDestroyer))
744 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
745 return CA_STATUS_FAILED;
748 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
749 // start receive thread
750 res = CAQueueingThreadStart(&gReceiveThread);
752 if (res != CA_STATUS_OK)
754 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
759 // retransmission initialize
760 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
761 CATimeoutCallback, NULL);
763 // start retransmission
764 res = CARetransmissionStart(&g_retransmissionContext);
766 if (res != CA_STATUS_OK)
768 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
772 // initialize interface adapters by controller
773 CAInitializeAdapters(g_threadPoolHandle);
774 OIC_LOG(DEBUG, TAG, "OUT");
778 void CATerminateMessageHandler()
780 OIC_LOG(DEBUG, TAG, "IN");
781 CATransportAdapter_t connType;
782 u_arraylist_t *list = CAGetSelectedNetworkList();
783 uint32_t length = u_arraylist_length(list);
786 for (i = 0; i < length; i++)
788 void* ptrType = u_arraylist_get(list, i);
795 connType = *(CATransportAdapter_t *)ptrType;
796 CAStopAdapter(connType);
799 // stop retransmission
800 if (NULL != g_retransmissionContext.threadMutex)
802 CARetransmissionStop(&g_retransmissionContext);
806 // delete thread data
807 if (NULL != g_sendThread.threadMutex)
809 CAQueueingThreadStop(&g_sendThread);
813 // delete thread data
814 if (NULL != g_receiveThread.threadMutex)
816 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
817 CAQueueingThreadStop(&gReceiveThread);
821 // destroy thread pool
822 if (NULL != g_threadPoolHandle)
824 ca_thread_pool_free(g_threadPoolHandle);
825 g_threadPoolHandle = NULL;
828 CARetransmissionDestroy(&g_retransmissionContext);
829 CAQueueingThreadDestroy(&g_sendThread);
830 CAQueueingThreadDestroy(&g_receiveThread);
832 // terminate interface adapters by controller
833 CATerminateAdapters();
835 OIC_LOG(DEBUG, TAG, "OUT");
838 void CALogPDUInfo(coap_pdu_t *pdu)
840 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
842 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
844 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
846 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
848 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
850 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
852 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
855 void CAErrorHandler(const CAEndpoint_t *endpoint,
856 const void *data, uint32_t dataLen,
859 OIC_LOG(DEBUG, TAG, "IN");
860 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
861 VERIFY_NON_NULL_VOID(data, TAG, "data");
863 uint32_t code = CA_NOT_FOUND;
864 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
866 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
869 OIC_LOG(ERROR, TAG, "Parse PDU failed");
873 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
874 if (NULL == errorInfo)
876 OIC_LOG(ERROR, TAG, "CAErrorHandler, Memory allocation failed!");
877 coap_delete_pdu(pdu);
881 CAResult_t res = CAGetErrorInfoFromPDU(pdu, errorInfo);
882 if (CA_STATUS_OK != res)
884 OIC_LOG_V(ERROR, TAG, "CAGetErrorInfoFromPDU failed : %d", res);
886 coap_delete_pdu(pdu);
890 errorInfo->result = result;
891 OIC_LOG_V(DEBUG, TAG, "error : %d", result);
892 if (NULL != errorInfo->info.payload)
894 OIC_LOG_V(DEBUG, TAG, "error, payload: %s", errorInfo->info.payload);
897 OIC_LOG(DEBUG, TAG, "error, token");
898 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) errorInfo->info.token,
899 errorInfo->info.tokenLength);
900 OIC_LOG_V(DEBUG, TAG, "CAErrorHandler, msgID : %d", errorInfo->info.messageId);
902 CAEndpoint_t *rep = NULL;
903 rep = CACloneEndpoint(endpoint);
906 OIC_LOG(ERROR, TAG, "CAErrorHandler, CloneEndpoint Failed");
908 coap_delete_pdu(pdu);
912 // store the data at queue.
913 CAData_t *cadata = NULL;
914 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
917 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
920 coap_delete_pdu(pdu);
924 cadata->remoteEndpoint = rep;
925 cadata->requestInfo = NULL;
926 cadata->responseInfo = NULL;
927 cadata->errorInfo = errorInfo;
928 cadata->dataType = CA_ERROR_DATA;
930 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
931 coap_delete_pdu(pdu);