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 "caprotocolmessage.h"
31 #include "config.h" /* for coap protocol */
32 #include "oic_malloc.h"
33 #include "canetworkconfigurator.h"
34 #include "caadapterutils.h"
35 #include "cainterfacecontroller.h"
36 #include "caretransmission.h"
39 #include "cablockwisetransfer.h"
44 #include "cathreadpool.h" /* for thread pool */
45 #include "caqueueingthread.h"
48 #define MAX_THREAD_POOL_SIZE 20
51 static ca_thread_pool_t g_threadPoolHandle = NULL;
53 // message handler main thread
54 static CAQueueingThread_t g_sendThread;
55 static CAQueueingThread_t g_receiveThread;
58 #define CA_MAX_RT_ARRAY_SIZE 3
59 #endif /* SINGLE_THREAD */
61 #define TAG "CA_MSG_HNDLR"
63 static CARetransmission_t g_retransmissionContext;
66 static CARequestCallback g_requestHandler = NULL;
67 static CAResponseCallback g_responseHandler = NULL;
68 static CAErrorCallback g_errorHandler = NULL;
70 static void CAErrorHandler(const CAEndpoint_t *endpoint,
71 const void *data, uint32_t dataLen,
74 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
75 const CARemoteId_t *identity,
76 const void *data, CADataType_t dataType);
78 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info,
82 static void CAProcessReceivedData(CAData_t *data);
84 static void CADestroyData(void *data, uint32_t size);
85 static void CALogPayloadInfo(CAInfo_t *info);
86 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *endpoint, uint16_t id);
89 void CAAddDataToSendThread(CAData_t *data)
91 OIC_LOG(DEBUG, TAG, "IN");
92 VERIFY_NON_NULL_VOID(data, TAG, "data");
95 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
97 OIC_LOG(DEBUG, TAG, "OUT");
100 void CAAddDataToReceiveThread(CAData_t *data)
102 OIC_LOG(DEBUG, TAG, "IN - CAAddDataToReceiveThread");
103 VERIFY_NON_NULL_VOID(data, TAG, "data");
106 CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
108 OIC_LOG(DEBUG, TAG, "OUT - CAAddDataToReceiveThread");
112 static bool CAIsSelectedNetworkAvailable()
114 u_arraylist_t *list = CAGetSelectedNetworkList();
115 if (!list || u_arraylist_length(list) == 0)
117 OIC_LOG(ERROR, TAG, "No selected network");
124 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
125 const CARemoteId_t *identity,
126 const void *data, CADataType_t dataType)
128 OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN");
129 CAInfo_t *info = NULL;
130 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
133 OIC_LOG(ERROR, TAG, "memory allocation failed");
137 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
140 OIC_LOG(ERROR, TAG, "endpoint clone failed");
145 OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
148 if(CA_RESPONSE_DATA == dataType)
150 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
153 OIC_LOG(ERROR, TAG, "memory allocation failed");
159 result = CAGetResponseInfoFromPDU(data, resInfo);
160 if (CA_STATUS_OK != result)
162 OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed");
164 CADestroyResponseInfoInternal(resInfo);
168 cadata->responseInfo = resInfo;
169 info = &resInfo->info;
172 info->identity = *identity;
174 OIC_LOG(DEBUG, TAG, "Response Info :");
175 CALogPayloadInfo(info);
177 else if (CA_REQUEST_DATA == dataType)
179 CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t));
182 OIC_LOG(ERROR, TAG, "memory allocation failed");
188 result = CAGetRequestInfoFromPDU(data, reqInfo);
189 if (CA_STATUS_OK != result)
191 OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed");
193 CADestroyRequestInfoInternal(reqInfo);
198 if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId))
200 OIC_LOG(ERROR, TAG, "Second Request with same Token, Drop it");
202 CADestroyRequestInfoInternal(reqInfo);
207 cadata->requestInfo = reqInfo;
208 info = &reqInfo->info;
211 info->identity = *identity;
213 OIC_LOG(DEBUG, TAG, "Request Info :");
214 CALogPayloadInfo(info);
216 else if (CA_ERROR_DATA == dataType)
218 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
221 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
227 CAResult_t result = CAGetErrorInfoFromPDU(data, errorInfo);
228 if (CA_STATUS_OK != result)
230 OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed");
237 cadata->errorInfo = errorInfo;
238 info = &errorInfo->info;
241 info->identity = *identity;
243 OIC_LOG(DEBUG, TAG, "error Info :");
244 CALogPayloadInfo(info);
247 cadata->remoteEndpoint = ep;
248 cadata->dataType = dataType;
252 OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
255 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
257 OIC_LOG(DEBUG, TAG, "IN");
258 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
259 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
261 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
264 OIC_LOG(ERROR, TAG, "clone failed");
268 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
272 OIC_LOG(ERROR, TAG, "calloc failed");
277 resInfo->result = CA_RETRANSMIT_TIMEOUT;
278 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
279 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
281 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info));
282 if (CA_STATUS_OK != res)
284 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
285 CADestroyResponseInfoInternal(resInfo);
290 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
293 OIC_LOG(ERROR, TAG, "memory allocation failed !");
295 CADestroyResponseInfoInternal(resInfo);
299 cadata->type = SEND_TYPE_UNICAST;
300 cadata->remoteEndpoint = ep;
301 cadata->requestInfo = NULL;
302 cadata->responseInfo = resInfo;
305 CAProcessReceivedData(cadata);
307 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
310 OIC_LOG(DEBUG, TAG, "OUT");
313 static void CADestroyData(void *data, uint32_t size)
315 OIC_LOG(DEBUG, TAG, "CADestroyData IN");
316 if ((size_t)size < sizeof(CAData_t))
318 OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
320 CAData_t *cadata = (CAData_t *) data;
324 OIC_LOG(ERROR, TAG, "cadata is NULL");
328 if (NULL != cadata->remoteEndpoint)
330 CAFreeEndpoint(cadata->remoteEndpoint);
333 if (NULL != cadata->requestInfo)
335 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
338 if (NULL != cadata->responseInfo)
340 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
343 if (NULL != cadata->errorInfo)
345 CADestroyErrorInfoInternal(cadata->errorInfo);
349 OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
353 static void CAProcessReceivedData(CAData_t *data)
355 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
358 OIC_LOG(ERROR, TAG, "thread data error!!");
362 // parse the data and call the callbacks.
365 CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
368 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
372 if (data->requestInfo && g_requestHandler)
374 g_requestHandler(rep, data->requestInfo);
376 else if (data->responseInfo && g_responseHandler)
378 g_responseHandler(rep, data->responseInfo);
380 else if (data->errorInfo && g_errorHandler)
382 g_errorHandler(rep, data->errorInfo);
386 CADestroyData(data, sizeof(CAData_t));
389 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
393 #ifndef SINGLE_THREAD
395 static void CAReceiveThreadProcess(void *threadData)
397 OIC_LOG(DEBUG, TAG, "IN");
398 #ifndef SINGLE_HANDLE
399 CAData_t *data = (CAData_t *) threadData;
400 CAProcessReceivedData(data);
404 OIC_LOG(DEBUG, TAG, "OUT");
408 static CAResult_t CAProcessSendData(const CAData_t *data)
410 OIC_LOG(DEBUG, TAG, "IN");
411 VERIFY_NON_NULL(data, TAG, "data");
412 VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
414 CAResult_t res = CA_STATUS_FAILED;
416 CASendDataType_t type = data->type;
418 coap_pdu_t *pdu = NULL;
419 CAInfo_t *info = NULL;
421 if (SEND_TYPE_UNICAST == type)
424 OIC_LOG(DEBUG,TAG,"Unicast message");
425 if (NULL != data->requestInfo)
427 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
429 info = &data->requestInfo->info;
430 pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint);
432 else if (NULL != data->responseInfo)
434 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
436 info = &data->responseInfo->info;
437 pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint);
441 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
442 return CA_STATUS_INVALID_PARAM;
445 // interface controller function call.
449 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
451 // Blockwise transfer
454 CAResult_t res = CAAddBlockOption(&pdu, *info,
455 data->remoteEndpoint);
456 if (CA_STATUS_OK != res)
458 OIC_LOG(INFO, TAG, "to write block option has failed");
459 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
460 coap_delete_pdu(pdu);
468 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
469 if (CA_STATUS_OK != res)
471 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
472 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
473 coap_delete_pdu(pdu);
476 // for retransmission
477 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
479 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
481 //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
482 OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
483 coap_delete_pdu(pdu);
487 coap_delete_pdu(pdu);
491 OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
492 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
493 return CA_SEND_FAILED;
496 else if (SEND_TYPE_MULTICAST == type)
498 OIC_LOG(DEBUG,TAG,"Multicast message");
499 if (NULL != data->requestInfo)
501 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
503 info = &data->requestInfo->info;
504 pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint);
508 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
510 // Blockwise transfer
511 CAResult_t res = CAAddBlockOption(&pdu, data->requestInfo->info,
512 data->remoteEndpoint);
513 if (CA_STATUS_OK != res)
515 OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
516 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
517 coap_delete_pdu(pdu);
525 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
526 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
527 return CA_SEND_FAILED;
530 else if (NULL != data->responseInfo)
532 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
534 info = &data->responseInfo->info;
535 pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint);
540 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
542 // Blockwise transfer
545 CAResult_t res = CAAddBlockOption(&pdu, *info,
546 data->remoteEndpoint);
547 if (CA_STATUS_OK != res)
549 OIC_LOG(INFO, TAG, "to write block option has failed");
550 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
551 coap_delete_pdu(pdu);
560 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
561 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
562 return CA_SEND_FAILED;
567 OIC_LOG(ERROR, TAG, "request or response info is empty");
568 return CA_SEND_FAILED;
573 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
574 if (CA_STATUS_OK != res)
576 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
577 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
578 coap_delete_pdu(pdu);
582 coap_delete_pdu(pdu);
588 #ifndef SINGLE_THREAD
589 static void CASendThreadProcess(void *threadData)
591 CAData_t *data = (CAData_t *) threadData;
592 CAProcessSendData(data);
598 * If a second message arrives with the same token and the other address
599 * family, drop it. Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
601 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id)
607 if (ep->adapter != CA_ADAPTER_IP)
611 if (!caglobals.ip.dualstack)
617 CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
619 for (size_t i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
621 CAHistoryItem_t *item = &(history->items[i]);
622 if (id == item->messageId)
624 if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
626 OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
627 familyFlags & CA_IPV6 ? '6' : '4');
634 history->items[history->nextIndex].flags = familyFlags;
635 history->items[history->nextIndex].messageId = id;
636 if (++history->nextIndex >= HISTORYSIZE)
638 history->nextIndex = 0;
644 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
645 const void *data, uint32_t dataLen)
647 OIC_LOG(DEBUG, TAG, "IN");
648 VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
649 VERIFY_NON_NULL_VOID(data, TAG, "data");
651 uint32_t code = CA_NOT_FOUND;
652 CAData_t *cadata = NULL;
654 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
657 OIC_LOG(ERROR, TAG, "Parse PDU failed");
661 OIC_LOG_V(DEBUG, TAG, "code = %d", code);
662 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
664 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
667 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
668 coap_delete_pdu(pdu);
674 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
677 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
678 coap_delete_pdu(pdu);
682 // for retransmission
683 void *retransmissionPdu = NULL;
684 CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
685 pdu->length, &retransmissionPdu);
687 // get token from saved data in retransmission list
688 if (retransmissionPdu && CA_EMPTY == code)
690 if (cadata->responseInfo)
692 CAInfo_t *info = &cadata->responseInfo->info;
693 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
695 if (CA_STATUS_OK != res)
697 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
698 OICFree(info->token);
699 info->tokenLength = 0;
703 OICFree(retransmissionPdu);
706 cadata->type = SEND_TYPE_UNICAST;
709 CAProcessReceivedData(cadata);
712 if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter)
714 CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
715 if (CA_NOT_SUPPORTED == res)
717 OIC_LOG(ERROR, TAG, "this message does not have block option");
718 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
722 CADestroyData(cadata, sizeof(CAData_t));
728 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
732 coap_delete_pdu(pdu);
734 OIC_LOG(DEBUG, TAG, "OUT");
737 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
741 OIC_LOG(DEBUG, TAG, "IN");
743 OIC_LOG(DEBUG, TAG, "OUT");
746 void CAHandleRequestResponseCallbacks()
750 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
753 // parse the data and call the callbacks.
757 ca_mutex_lock(g_receiveThread.threadMutex);
759 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
761 ca_mutex_unlock(g_receiveThread.threadMutex);
769 void *msg = item->msg;
777 CAData_t *td = (CAData_t *) msg;
779 if (td->requestInfo && g_requestHandler)
781 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
782 g_requestHandler(td->remoteEndpoint, td->requestInfo);
784 else if (td->responseInfo && g_responseHandler)
786 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
787 g_responseHandler(td->remoteEndpoint, td->responseInfo);
789 else if (td->errorInfo && g_errorHandler)
791 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
792 g_errorHandler(td->remoteEndpoint, td->errorInfo);
795 CADestroyData(msg, sizeof(CAData_t));
798 #endif /* SINGLE_HANDLE */
802 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
803 CADataType_t dataType)
805 OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
807 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
810 OIC_LOG(ERROR, TAG, "memory allocation failed");
814 if(CA_REQUEST_DATA == dataType)
816 // clone request info
817 CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
821 OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
826 cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
827 cadata->requestInfo = request;
829 else if(CA_RESPONSE_DATA == dataType)
831 // clone response info
832 CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
836 OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
841 cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
842 cadata->responseInfo = response;
846 OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
851 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
854 OIC_LOG(ERROR, TAG, "endpoint clone failed");
855 CADestroyData(cadata, sizeof(CAData_t));
859 cadata->remoteEndpoint = ep;
860 cadata->dataType = dataType;
864 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
866 OIC_LOG(DEBUG, TAG, "IN");
868 VERIFY_NON_NULL(object, TAG, "object");
869 VERIFY_NON_NULL(request, TAG, "request");
871 if (false == CAIsSelectedNetworkAvailable())
873 return CA_STATUS_FAILED;
877 // If max retransmission queue is reached, then don't handle new request
878 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
880 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
881 return CA_SEND_FAILED;
885 CAData_t *data = CAPrepareSendData(object, request, CA_REQUEST_DATA);
888 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
889 return CA_MEMORY_ALLOC_FAILED;
893 CAResult_t result = CAProcessSendData(data);
894 if(CA_STATUS_OK != result)
896 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
900 CADestroyData(data, sizeof(CAData_t));
903 if (CA_ADAPTER_GATT_BTLE != object->adapter)
906 CAResult_t res = CASendBlockWiseData(data);
907 if(CA_NOT_SUPPORTED == res)
909 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
910 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
915 CADestroyData(data, sizeof(CAData_t));
922 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
926 OIC_LOG(DEBUG, TAG, "OUT");
930 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
931 const CAResponseInfo_t *response)
933 OIC_LOG(DEBUG, TAG, "IN");
934 VERIFY_NON_NULL(object, TAG, "object");
935 VERIFY_NON_NULL(response, TAG, "response");
937 if (false == CAIsSelectedNetworkAvailable())
939 return CA_STATUS_FAILED;
942 CAData_t *data = CAPrepareSendData(object, response, CA_RESPONSE_DATA);
945 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
946 return CA_MEMORY_ALLOC_FAILED;
950 CAResult_t result = CAProcessSendData(data);
951 if(result != CA_STATUS_OK)
953 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
957 CADestroyData(data, sizeof(CAData_t));
960 if (CA_ADAPTER_GATT_BTLE != object->adapter)
963 CAResult_t res = CASendBlockWiseData(data);
964 if(CA_NOT_SUPPORTED == res)
966 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
967 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
972 CADestroyData(data, sizeof(CAData_t));
979 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
983 OIC_LOG(DEBUG, TAG, "OUT");
987 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
988 uint8_t tokenLength, const CAHeaderOption_t *options,
996 return CA_NOT_SUPPORTED;
999 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1000 CAErrorCallback errorHandler)
1002 OIC_LOG(DEBUG, TAG, "IN");
1003 g_requestHandler = ReqHandler;
1004 g_responseHandler = RespHandler;
1005 g_errorHandler = errorHandler;
1006 OIC_LOG(DEBUG, TAG, "OUT");
1009 CAResult_t CAInitializeMessageHandler()
1011 OIC_LOG(DEBUG, TAG, "IN");
1012 CASetPacketReceivedCallback(CAReceivedPacketCallback);
1014 CASetNetworkChangeCallback(CANetworkChangedCallback);
1015 CASetErrorHandleCallback(CAErrorHandler);
1017 #ifndef SINGLE_THREAD
1018 // create thread pool
1019 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1021 if (CA_STATUS_OK != res)
1023 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1027 // send thread initialize
1028 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1029 CASendThreadProcess, CADestroyData))
1031 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1032 return CA_STATUS_FAILED;
1035 // start send thread
1036 res = CAQueueingThreadStart(&g_sendThread);
1038 if (CA_STATUS_OK != res)
1040 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1041 ca_thread_pool_free(g_threadPoolHandle);
1042 g_threadPoolHandle = NULL;
1046 // receive thread initialize
1047 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1048 CAReceiveThreadProcess, CADestroyData))
1050 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1051 return CA_STATUS_FAILED;
1054 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1055 // start receive thread
1056 res = CAQueueingThreadStart(&gReceiveThread);
1058 if (res != CA_STATUS_OK)
1060 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1063 #endif /* SINGLE_HANDLE */
1065 // retransmission initialize
1066 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
1067 CATimeoutCallback, NULL);
1070 // block-wise transfer initialize
1071 CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1074 // start retransmission
1075 res = CARetransmissionStart(&g_retransmissionContext);
1077 if (CA_STATUS_OK != res)
1079 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1083 // initialize interface adapters by controller
1084 CAInitializeAdapters(g_threadPoolHandle);
1086 // retransmission initialize
1087 CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1088 CATimeoutCallback, NULL);
1089 CAInitializeAdapters();
1092 OIC_LOG(DEBUG, TAG, "OUT");
1093 return CA_STATUS_OK;
1096 void CATerminateMessageHandler()
1098 OIC_LOG(DEBUG, TAG, "IN");
1099 #ifndef SINGLE_THREAD
1100 CATransportAdapter_t connType;
1101 u_arraylist_t *list = CAGetSelectedNetworkList();
1102 uint32_t length = u_arraylist_length(list);
1105 for (i = 0; i < length; i++)
1107 void* ptrType = u_arraylist_get(list, i);
1109 if (NULL == ptrType)
1114 connType = *(CATransportAdapter_t *)ptrType;
1115 CAStopAdapter(connType);
1118 // stop retransmission
1119 if (NULL != g_retransmissionContext.threadMutex)
1121 CARetransmissionStop(&g_retransmissionContext);
1125 // delete thread data
1126 if (NULL != g_sendThread.threadMutex)
1128 CAQueueingThreadStop(&g_sendThread);
1132 // delete thread data
1133 if (NULL != g_receiveThread.threadMutex)
1135 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1136 CAQueueingThreadStop(&gReceiveThread);
1137 #endif /* SINGLE_HANDLE */
1140 // destroy thread pool
1141 if (NULL != g_threadPoolHandle)
1143 ca_thread_pool_free(g_threadPoolHandle);
1144 g_threadPoolHandle = NULL;
1148 CATerminateBlockWiseTransfer();
1150 CARetransmissionDestroy(&g_retransmissionContext);
1151 CAQueueingThreadDestroy(&g_sendThread);
1152 CAQueueingThreadDestroy(&g_receiveThread);
1154 // terminate interface adapters by controller
1155 CATerminateAdapters();
1157 // terminate interface adapters by controller
1158 CATerminateAdapters();
1160 // stop retransmission
1161 CARetransmissionStop(&g_retransmissionContext);
1162 CARetransmissionDestroy(&g_retransmissionContext);
1165 OIC_LOG(DEBUG, TAG, "OUT");
1168 void CALogPDUInfo(coap_pdu_t *pdu)
1170 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1172 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1174 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
1176 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
1178 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1180 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
1183 static void CALogPayloadInfo(CAInfo_t *info)
1189 for (uint32_t i = 0; i < info->numOptions; i++)
1191 OIC_LOG_V(DEBUG, TAG, "optionID: %d", info->options[i].optionID);
1193 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1199 OIC_LOG_V(DEBUG, TAG, "payload: %p(%u)", info->payload,
1205 OIC_LOG(DEBUG, TAG, "token:");
1206 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1209 OIC_LOG_V(DEBUG, TAG, "msgID: %d", info->messageId);
1213 OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1217 void CAErrorHandler(const CAEndpoint_t *endpoint,
1218 const void *data, uint32_t dataLen,
1221 OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1223 #ifndef SINGLE_THREAD
1225 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1226 VERIFY_NON_NULL_VOID(data, TAG, "data");
1228 uint32_t code = CA_NOT_FOUND;
1229 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1231 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
1234 OIC_LOG(ERROR, TAG, "Parse PDU failed");
1238 CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1241 OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1242 coap_delete_pdu(pdu);
1246 cadata->errorInfo->result = result;
1248 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1249 coap_delete_pdu(pdu);
1252 OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1256 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1258 OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1259 #ifndef SINGLE_THREAD
1260 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1263 OIC_LOG(ERROR, TAG, "memory allocation failed");
1267 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1270 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1275 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1283 CAResult_t res = CACloneInfo(info, &errorInfo->info);
1284 if (CA_STATUS_OK != res)
1291 errorInfo->result = result;
1292 cadata->remoteEndpoint = ep;
1293 cadata->errorInfo = errorInfo;
1294 cadata->dataType = CA_ERROR_DATA;
1296 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1298 OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");