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");
344 * If a second message arrives with the same token and the other address
345 * family, drop it. Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
346 * This can be made more robust (for instance, another message could arrive
347 * in between), but it is good enough for now.
349 static bool CADropSecondRequest(const CAEndpoint_t *endpoint, uint16_t messageId)
355 if (endpoint->adapter != CA_ADAPTER_IP)
361 CATransportFlags_t familyFlags = endpoint->flags & CA_IPFAMILY_MASK;
363 if (messageId == caglobals.ca.previousRequestMessageId)
365 if ((familyFlags ^ caglobals.ca.previousRequestFlags) == CA_IPFAMILY_MASK)
367 if (familyFlags & CA_IPV6)
369 OIC_LOG(INFO, TAG, PCF("IPv6 duplicate response ignored"));
373 OIC_LOG(INFO, TAG, PCF("IPv4 duplicate response ignored"));
378 caglobals.ca.previousRequestFlags = familyFlags;
379 caglobals.ca.previousRequestMessageId = messageId;
383 static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
385 OIC_LOG(DEBUG, TAG, "IN");
386 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
387 VERIFY_NON_NULL_VOID(data, TAG, "data");
389 uint32_t code = CA_NOT_FOUND;
390 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
395 OIC_LOG(ERROR, TAG, "Parse PDU failed");
399 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
401 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
404 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
405 coap_delete_pdu(pdu);
409 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo);
410 if (CA_STATUS_OK != res)
412 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
414 coap_delete_pdu(pdu);
418 if (CADropSecondRequest(endpoint, ReqInfo->info.messageId))
421 coap_delete_pdu(pdu);
425 if (NULL != ReqInfo->info.options)
428 for (i = 0; i < ReqInfo->info.numOptions; i++)
430 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
432 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
436 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
437 if (NULL != ReqInfo->info.token)
439 OIC_LOG(DEBUG, TAG, "Request- token:");
440 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
441 ReqInfo->info.tokenLength);
444 OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
445 // store the data at queue.
446 CAData_t *cadata = NULL;
447 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
450 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
451 CADestroyRequestInfoInternal(ReqInfo);
452 coap_delete_pdu(pdu);
456 cadata->type = SEND_TYPE_UNICAST;
457 cadata->remoteEndpoint = CACloneEndpoint(endpoint);
458 cadata->requestInfo = ReqInfo;
459 cadata->responseInfo = NULL;
460 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
464 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
467 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
468 coap_delete_pdu(pdu);
472 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo);
473 if (CA_STATUS_OK != res)
475 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
477 coap_delete_pdu(pdu);
481 if (NULL != ResInfo->info.options)
484 for (i = 0; i < ResInfo->info.numOptions; i++)
486 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
488 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
492 if (NULL != ResInfo->info.payload)
494 OIC_LOG_V(DEBUG, TAG, "Response- payload: %p(%u) from %s", ResInfo->info.payload,
495 ResInfo->info.payloadSize, endpoint->addr);
497 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
498 if (NULL != ResInfo->info.token)
500 OIC_LOG(DEBUG, TAG, "Response- token:");
501 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ResInfo->info.token,
502 ResInfo->info.tokenLength);
504 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
506 // store the data at queue.
507 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
510 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
511 CADestroyResponseInfoInternal(ResInfo);
512 coap_delete_pdu(pdu);
516 cadata->type = SEND_TYPE_UNICAST;
517 cadata->remoteEndpoint = CACloneEndpoint(endpoint);
518 cadata->requestInfo = NULL;
520 // for retransmission
521 void *retransmissionPdu = NULL;
522 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
525 // get token from saved data in retransmission list
526 if (retransmissionPdu && CA_EMPTY == code)
528 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
530 if (CA_STATUS_OK != res)
532 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
533 OICFree(ResInfo->info.token);
536 OICFree(retransmissionPdu);
537 cadata->responseInfo = ResInfo;
539 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
544 coap_delete_pdu(pdu);
546 OIC_LOG(DEBUG, TAG, "OUT");
549 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
551 OIC_LOG(DEBUG, TAG, "IN");
553 OIC_LOG(DEBUG, TAG, "OUT");
556 void CAHandleRequestResponseCallbacks()
560 // parse the data and call the callbacks.
564 ca_mutex_lock(g_receiveThread.threadMutex);
566 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
568 ca_mutex_unlock(g_receiveThread.threadMutex);
576 void *msg = item->msg;
584 CAData_t *td = (CAData_t *) msg;
586 if (td->requestInfo && g_requestHandler)
588 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
589 g_requestHandler(td->remoteEndpoint, td->requestInfo);
591 else if (td->responseInfo && g_responseHandler)
593 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
594 g_responseHandler(td->remoteEndpoint, td->responseInfo);
596 else if (td->errorInfo && g_errorHandler)
598 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
599 g_errorHandler(td->remoteEndpoint, td->errorInfo);
602 CADataDestroyer(msg, sizeof(CAData_t));
606 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
609 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
611 OIC_LOG(DEBUG, TAG, "IN");
613 VERIFY_NON_NULL(object, TAG, "object");
614 VERIFY_NON_NULL(request, TAG, "request");
616 if (false == CAIsSelectedNetworkAvailable())
618 return CA_STATUS_FAILED;
621 CAEndpoint_t *remoteEndpoint = NULL;
622 CARequestInfo_t *requestInfo = NULL;
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 request info
631 requestInfo = CACloneRequestInfo(request);
632 CA_MEMORY_ALLOC_CHECK(requestInfo);
635 data->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
636 data->remoteEndpoint = remoteEndpoint;
637 data->requestInfo = requestInfo;
638 data->responseInfo = NULL;
639 data->options = NULL;
640 data->numOptions = 0;
641 if (NULL != requestInfo->info.options && 0 < requestInfo->info.numOptions)
643 uint8_t numOptions = requestInfo->info.numOptions;
645 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
647 CA_MEMORY_ALLOC_CHECK(headerOption);
649 memcpy(headerOption, requestInfo->info.options, sizeof(CAHeaderOption_t) * numOptions);
651 data->options = headerOption;
652 data->numOptions = numOptions;
656 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
657 OIC_LOG(DEBUG, TAG, "OUT");
660 // memory error label.
662 CAFreeEndpoint(remoteEndpoint);
663 CADestroyRequestInfoInternal(requestInfo);
666 OIC_LOG(DEBUG, TAG, "OUT");
667 return CA_MEMORY_ALLOC_FAILED;
670 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
671 const CAResponseInfo_t *response)
673 OIC_LOG(DEBUG, TAG, "IN");
674 VERIFY_NON_NULL(object, TAG, "object");
675 VERIFY_NON_NULL(response, TAG, "response");
677 if (false == CAIsSelectedNetworkAvailable())
679 return CA_STATUS_FAILED;
682 CAEndpoint_t *remoteEndpoint = NULL;
683 CAResponseInfo_t *responseInfo = NULL;
685 // allocate & initialize
686 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
687 CA_MEMORY_ALLOC_CHECK(data);
689 // clone remote endpoint
690 remoteEndpoint = CACloneEndpoint(object);
691 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
693 // clone response info
694 responseInfo = CACloneResponseInfo(response);
695 CA_MEMORY_ALLOC_CHECK(responseInfo);
698 data->type = SEND_TYPE_UNICAST;
699 data->remoteEndpoint = remoteEndpoint;
700 data->requestInfo = NULL;
701 data->responseInfo = responseInfo;
702 data->options = NULL;
703 data->numOptions = 0;
704 if (NULL != responseInfo->info.options && 0 < responseInfo->info.numOptions)
706 uint8_t numOptions = responseInfo->info.numOptions;
708 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
710 CA_MEMORY_ALLOC_CHECK(headerOption);
712 memcpy(headerOption, responseInfo->info.options, sizeof(CAHeaderOption_t) * numOptions);
714 data->options = headerOption;
715 data->numOptions = numOptions;
719 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
721 OIC_LOG(DEBUG, TAG, "OUT");
724 // memory error label.
726 CAFreeEndpoint(remoteEndpoint);
727 CADestroyResponseInfoInternal(responseInfo);
729 OIC_LOG(DEBUG, TAG, "OUT");
731 return CA_MEMORY_ALLOC_FAILED;
734 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
735 uint8_t tokenLength, const CAHeaderOption_t *options,
738 return CA_NOT_SUPPORTED;
741 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
742 CAErrorCallback errroHandler)
744 OIC_LOG(DEBUG, TAG, "IN");
745 g_requestHandler = ReqHandler;
746 g_responseHandler = RespHandler;
747 g_errorHandler = errroHandler;
748 OIC_LOG(DEBUG, TAG, "OUT");
751 CAResult_t CAInitializeMessageHandler()
753 OIC_LOG(DEBUG, TAG, "IN");
754 CASetPacketReceivedCallback(CAReceivedPacketCallback);
756 CASetNetworkChangeCallback(CANetworkChangedCallback);
757 CASetErrorHandleCallback(CAErrorHandler);
759 // create thread pool
760 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
762 if (res != CA_STATUS_OK)
764 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
768 // send thread initialize
769 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
770 CASendThreadProcess, CADataDestroyer))
772 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
773 return CA_STATUS_FAILED;
777 res = CAQueueingThreadStart(&g_sendThread);
779 if (res != CA_STATUS_OK)
781 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
782 ca_thread_pool_free(g_threadPoolHandle);
783 g_threadPoolHandle = NULL;
787 // receive thread initialize
788 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
789 CAReceiveThreadProcess, CADataDestroyer))
791 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
792 return CA_STATUS_FAILED;
795 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
796 // start receive thread
797 res = CAQueueingThreadStart(&gReceiveThread);
799 if (res != CA_STATUS_OK)
801 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
806 // retransmission initialize
807 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
808 CATimeoutCallback, NULL);
810 // start retransmission
811 res = CARetransmissionStart(&g_retransmissionContext);
813 if (res != CA_STATUS_OK)
815 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
819 // initialize interface adapters by controller
820 CAInitializeAdapters(g_threadPoolHandle);
821 OIC_LOG(DEBUG, TAG, "OUT");
825 void CATerminateMessageHandler()
827 OIC_LOG(DEBUG, TAG, "IN");
828 CATransportAdapter_t connType;
829 u_arraylist_t *list = CAGetSelectedNetworkList();
830 uint32_t length = u_arraylist_length(list);
833 for (i = 0; i < length; i++)
835 void* ptrType = u_arraylist_get(list, i);
842 connType = *(CATransportAdapter_t *)ptrType;
843 CAStopAdapter(connType);
846 // stop retransmission
847 if (NULL != g_retransmissionContext.threadMutex)
849 CARetransmissionStop(&g_retransmissionContext);
853 // delete thread data
854 if (NULL != g_sendThread.threadMutex)
856 CAQueueingThreadStop(&g_sendThread);
860 // delete thread data
861 if (NULL != g_receiveThread.threadMutex)
863 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
864 CAQueueingThreadStop(&gReceiveThread);
868 // destroy thread pool
869 if (NULL != g_threadPoolHandle)
871 ca_thread_pool_free(g_threadPoolHandle);
872 g_threadPoolHandle = NULL;
875 CARetransmissionDestroy(&g_retransmissionContext);
876 CAQueueingThreadDestroy(&g_sendThread);
877 CAQueueingThreadDestroy(&g_receiveThread);
879 // terminate interface adapters by controller
880 CATerminateAdapters();
882 OIC_LOG(DEBUG, TAG, "OUT");
885 void CALogPDUInfo(coap_pdu_t *pdu)
887 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
889 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
891 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
893 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
895 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
897 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
899 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
902 void CAErrorHandler(const CAEndpoint_t *endpoint,
903 const void *data, uint32_t dataLen,
906 OIC_LOG(DEBUG, TAG, "IN");
907 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
908 VERIFY_NON_NULL_VOID(data, TAG, "data");
910 uint32_t code = CA_NOT_FOUND;
911 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
913 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
916 OIC_LOG(ERROR, TAG, "Parse PDU failed");
920 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
921 if (NULL == errorInfo)
923 OIC_LOG(ERROR, TAG, "CAErrorHandler, Memory allocation failed!");
924 coap_delete_pdu(pdu);
928 CAResult_t res = CAGetErrorInfoFromPDU(pdu, errorInfo);
929 if (CA_STATUS_OK != res)
931 OIC_LOG_V(ERROR, TAG, "CAGetErrorInfoFromPDU failed : %d", res);
933 coap_delete_pdu(pdu);
937 errorInfo->result = result;
938 OIC_LOG_V(DEBUG, TAG, "error : %d", result);
939 if (NULL != errorInfo->info.payload)
941 OIC_LOG_V(DEBUG, TAG, "error, payload: %s", errorInfo->info.payload);
944 OIC_LOG(DEBUG, TAG, "error, token");
945 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) errorInfo->info.token,
946 errorInfo->info.tokenLength);
947 OIC_LOG_V(DEBUG, TAG, "CAErrorHandler, msgID : %d", errorInfo->info.messageId);
949 CAEndpoint_t *rep = NULL;
950 rep = CACloneEndpoint(endpoint);
953 OIC_LOG(ERROR, TAG, "CAErrorHandler, CloneEndpoint Failed");
955 coap_delete_pdu(pdu);
959 // store the data at queue.
960 CAData_t *cadata = NULL;
961 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
964 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
967 coap_delete_pdu(pdu);
971 cadata->remoteEndpoint = rep;
972 cadata->requestInfo = NULL;
973 cadata->responseInfo = NULL;
974 cadata->errorInfo = errorInfo;
975 cadata->dataType = CA_ERROR_DATA;
977 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
978 coap_delete_pdu(pdu);