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, const void *data,
75 CADataType_t dataType);
77 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info,
81 static void CAProcessReceivedData(CAData_t *data);
83 static void CADestroyData(void *data, uint32_t size);
84 static void CALogPayloadInfo(CAInfo_t *info);
85 static bool CADropSecondRequest(const CAEndpoint_t *endpoint, uint16_t messageId);
88 void CAAddDataToSendThread(CAData_t *data)
90 OIC_LOG(DEBUG, TAG, "IN");
91 VERIFY_NON_NULL_VOID(data, TAG, "data");
94 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
96 OIC_LOG(DEBUG, TAG, "OUT");
99 void CAAddDataToReceiveThread(CAData_t *data)
101 OIC_LOG(DEBUG, TAG, "IN - CAAddDataToReceiveThread");
102 VERIFY_NON_NULL_VOID(data, TAG, "data");
105 CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
107 OIC_LOG(DEBUG, TAG, "OUT - CAAddDataToReceiveThread");
111 static bool CAIsSelectedNetworkAvailable()
113 u_arraylist_t *list = CAGetSelectedNetworkList();
114 if (!list || list->length == 0)
116 OIC_LOG(ERROR, TAG, "No selected network");
123 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, const void *data, CADataType_t dataType)
125 OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN");
126 CAInfo_t *info = NULL;
127 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
130 OIC_LOG(ERROR, TAG, "memory allocation failed");
134 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
137 OIC_LOG(ERROR, TAG, "endpoint clone failed");
142 OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
145 if(CA_RESPONSE_DATA == dataType)
147 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
150 OIC_LOG(ERROR, TAG, "memory allocation failed");
156 result = CAGetResponseInfoFromPDU(data, resInfo);
157 if (CA_STATUS_OK != result)
159 OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed");
161 CADestroyResponseInfoInternal(resInfo);
165 cadata->responseInfo = resInfo;
166 info = &resInfo->info;
167 OIC_LOG(DEBUG, TAG, "Response Info :");
168 CALogPayloadInfo(info);
170 else if (CA_REQUEST_DATA == dataType)
172 CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t));
175 OIC_LOG(ERROR, TAG, "memory allocation failed");
181 result = CAGetRequestInfoFromPDU(data, reqInfo);
182 if (CA_STATUS_OK != result)
184 OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed");
186 CADestroyRequestInfoInternal(reqInfo);
191 if (CADropSecondRequest(endpoint, reqInfo->info.messageId))
193 OIC_LOG(ERROR, TAG, "Second Request with same Token, Drop it");
195 CADestroyRequestInfoInternal(reqInfo);
200 cadata->requestInfo = reqInfo;
201 info = &reqInfo->info;
202 OIC_LOG(DEBUG, TAG, "Request Info :");
203 CALogPayloadInfo(info);
205 else if (CA_ERROR_DATA == dataType)
207 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
210 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
216 CAResult_t result = CAGetErrorInfoFromPDU(data, errorInfo);
217 if (CA_STATUS_OK != result)
219 OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed");
226 cadata->errorInfo = errorInfo;
227 info = &errorInfo->info;
228 OIC_LOG(DEBUG, TAG, "error Info :");
229 CALogPayloadInfo(info);
232 cadata->remoteEndpoint = ep;
233 cadata->dataType = dataType;
237 OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
240 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
242 OIC_LOG(DEBUG, TAG, "IN");
243 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
244 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
246 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
249 OIC_LOG(ERROR, TAG, "clone failed");
253 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
257 OIC_LOG(ERROR, TAG, "calloc failed");
262 resInfo->result = CA_RETRANSMIT_TIMEOUT;
263 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
264 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
266 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info));
267 if (CA_STATUS_OK != res)
269 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
270 CADestroyResponseInfoInternal(resInfo);
275 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
278 OIC_LOG(ERROR, TAG, "memory allocation failed !");
280 CADestroyResponseInfoInternal(resInfo);
284 cadata->type = SEND_TYPE_UNICAST;
285 cadata->remoteEndpoint = ep;
286 cadata->requestInfo = NULL;
287 cadata->responseInfo = resInfo;
290 CAProcessReceivedData(cadata);
292 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
295 OIC_LOG(DEBUG, TAG, "OUT");
298 static void CADestroyData(void *data, uint32_t size)
300 OIC_LOG(DEBUG, TAG, "CADestroyData IN");
301 if ((size_t)size < sizeof(CAData_t))
303 OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
305 CAData_t *cadata = (CAData_t *) data;
309 OIC_LOG(ERROR, TAG, "cadata is NULL");
313 if (NULL != cadata->remoteEndpoint)
315 CAFreeEndpoint(cadata->remoteEndpoint);
318 if (NULL != cadata->requestInfo)
320 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
323 if (NULL != cadata->responseInfo)
325 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
328 if (NULL != cadata->errorInfo)
330 CADestroyErrorInfoInternal(cadata->errorInfo);
333 OICFree(cadata->options);
335 OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
339 static void CAProcessReceivedData(CAData_t *data)
341 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
344 OIC_LOG(ERROR, TAG, "thread data error!!");
348 // parse the data and call the callbacks.
351 CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
354 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
358 if (data->requestInfo && g_requestHandler)
360 g_requestHandler(rep, data->requestInfo);
362 else if (data->responseInfo && g_responseHandler)
364 g_responseHandler(rep, data->responseInfo);
366 else if (data->errorInfo && g_errorHandler)
368 g_errorHandler(rep, data->errorInfo);
372 CADestroyData(data, sizeof(CAData_t));
375 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
379 #ifndef SINGLE_THREAD
381 static void CAReceiveThreadProcess(void *threadData)
383 OIC_LOG(DEBUG, TAG, "IN");
384 #ifndef SINGLE_HANDLE
385 CAData_t *data = (CAData_t *) threadData;
386 CAProcessReceivedData(data);
390 OIC_LOG(DEBUG, TAG, "OUT");
394 static CAResult_t CAProcessSendData(const CAData_t *data)
396 OIC_LOG(DEBUG, TAG, "IN");
397 VERIFY_NON_NULL(data, TAG, "data");
398 VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
400 CAResult_t res = CA_STATUS_FAILED;
402 CASendDataType_t type = data->type;
404 coap_pdu_t *pdu = NULL;
405 CAInfo_t *info = NULL;
407 if (SEND_TYPE_UNICAST == type)
410 OIC_LOG(DEBUG,TAG,"Unicast message");
411 if (NULL != data->requestInfo)
413 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
415 info = &data->requestInfo->info;
416 pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint);
418 else if (NULL != data->responseInfo)
420 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
422 info = &data->responseInfo->info;
423 pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint);
427 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
428 return CA_STATUS_INVALID_PARAM;
431 // interface controller function call.
435 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
437 // Blockwise transfer
440 CAResult_t res = CAAddBlockOption(&pdu, *info,
441 data->remoteEndpoint);
442 if (CA_STATUS_OK != res)
444 OIC_LOG(INFO, TAG, "to write block option has failed");
445 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
446 coap_delete_pdu(pdu);
454 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
455 if (CA_STATUS_OK != res)
457 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
458 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
459 coap_delete_pdu(pdu);
462 // for retransmission
463 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
465 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
467 //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
468 OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
469 coap_delete_pdu(pdu);
473 coap_delete_pdu(pdu);
477 OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
478 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
479 return CA_SEND_FAILED;
482 else if (SEND_TYPE_MULTICAST == type)
484 OIC_LOG(DEBUG,TAG,"Multicast message");
485 if (NULL != data->requestInfo)
487 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
489 info = &data->requestInfo->info;
490 pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint);
494 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
496 // Blockwise transfer
497 CAResult_t res = CAAddBlockOption(&pdu, data->requestInfo->info,
498 data->remoteEndpoint);
499 if (CA_STATUS_OK != res)
501 OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
502 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
503 coap_delete_pdu(pdu);
510 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
511 if (CA_STATUS_OK != res)
513 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
514 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
515 coap_delete_pdu(pdu);
519 coap_delete_pdu(pdu);
523 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
524 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
525 return CA_SEND_FAILED;
530 OIC_LOG(ERROR, TAG, "request info is empty");
531 return CA_SEND_FAILED;
535 OIC_LOG(DEBUG, TAG, "OUT");
539 #ifndef SINGLE_THREAD
540 static void CASendThreadProcess(void *threadData)
542 CAData_t *data = (CAData_t *) threadData;
543 CAProcessSendData(data);
549 * If a second message arrives with the same token and the other address
550 * family, drop it. Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
551 * This can be made more robust (for instance, another message could arrive
552 * in between), but it is good enough for now.
554 static bool CADropSecondRequest(const CAEndpoint_t *endpoint, uint16_t messageId)
560 if (endpoint->adapter != CA_ADAPTER_IP)
566 CATransportFlags_t familyFlags = endpoint->flags & CA_IPFAMILY_MASK;
568 if (messageId == caglobals.ca.previousRequestMessageId)
570 if ((familyFlags ^ caglobals.ca.previousRequestFlags) == CA_IPFAMILY_MASK)
572 if (familyFlags & CA_IPV6)
574 OIC_LOG(INFO, TAG, "IPv6 duplicate response ignored");
578 OIC_LOG(INFO, TAG, "IPv4 duplicate response ignored");
583 caglobals.ca.previousRequestFlags = familyFlags;
584 caglobals.ca.previousRequestMessageId = messageId;
588 static void CAReceivedPacketCallback(const CAEndpoint_t *remoteEndpoint, const void *data, uint32_t dataLen)
590 OIC_LOG(DEBUG, TAG, "IN");
591 VERIFY_NON_NULL_VOID(remoteEndpoint, TAG, "remoteEndpoint");
592 VERIFY_NON_NULL_VOID(data, TAG, "data");
594 uint32_t code = CA_NOT_FOUND;
595 CAData_t *cadata = NULL;
597 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
600 OIC_LOG(ERROR, TAG, "Parse PDU failed");
604 OIC_LOG_V(DEBUG, TAG, "code = %d", code);
605 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
607 cadata = CAGenerateHandlerData(remoteEndpoint, pdu, CA_REQUEST_DATA);
610 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
611 coap_delete_pdu(pdu);
617 cadata = CAGenerateHandlerData(remoteEndpoint, pdu, CA_RESPONSE_DATA);
620 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
621 coap_delete_pdu(pdu);
625 // for retransmission
626 void *retransmissionPdu = NULL;
627 CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
628 pdu->length, &retransmissionPdu);
630 // get token from saved data in retransmission list
631 if (retransmissionPdu && CA_EMPTY == code)
633 if (cadata->responseInfo)
635 CAInfo_t *info = &cadata->responseInfo->info;
636 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
638 if (CA_STATUS_OK != res)
640 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
641 OICFree(info->token);
642 info->tokenLength = 0;
646 OICFree(retransmissionPdu);
649 cadata->type = SEND_TYPE_UNICAST;
652 CAProcessReceivedData(cadata);
655 if (CA_ADAPTER_GATT_BTLE != remoteEndpoint->adapter)
657 CAResult_t res = CAReceiveBlockWiseData(pdu, remoteEndpoint, cadata, dataLen);
658 if (CA_NOT_SUPPORTED == res)
660 OIC_LOG(ERROR, TAG, "this message does not have block option");
661 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
665 CADestroyData(cadata, sizeof(CAData_t));
671 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
675 coap_delete_pdu(pdu);
677 OIC_LOG(DEBUG, TAG, "OUT");
680 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
684 OIC_LOG(DEBUG, TAG, "IN");
686 OIC_LOG(DEBUG, TAG, "OUT");
689 void CAHandleRequestResponseCallbacks()
693 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
696 // parse the data and call the callbacks.
700 ca_mutex_lock(g_receiveThread.threadMutex);
702 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
704 ca_mutex_unlock(g_receiveThread.threadMutex);
712 void *msg = item->msg;
720 CAData_t *td = (CAData_t *) msg;
722 if (td->requestInfo && g_requestHandler)
724 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
725 g_requestHandler(td->remoteEndpoint, td->requestInfo);
727 else if (td->responseInfo && g_responseHandler)
729 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
730 g_responseHandler(td->remoteEndpoint, td->responseInfo);
732 else if (td->errorInfo && g_errorHandler)
734 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
735 g_errorHandler(td->remoteEndpoint, td->errorInfo);
738 CADestroyData(msg, sizeof(CAData_t));
741 #endif /* SINGLE_HANDLE */
745 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
746 CADataType_t dataType)
748 OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
749 CAInfo_t *info = NULL;
751 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
754 OIC_LOG(ERROR, TAG, "memory allocation failed");
758 if(CA_REQUEST_DATA == dataType)
760 // clone request info
761 CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
765 OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
770 cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
771 info = &request->info;
772 cadata->requestInfo = request;
774 else if(CA_RESPONSE_DATA == dataType)
776 // clone response info
777 CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
781 OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
786 cadata->type = SEND_TYPE_UNICAST;
787 info = &response->info;
788 cadata->responseInfo = response;
792 OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
797 if (NULL != info->options && 0 < info->numOptions)
799 uint8_t numOptions = info->numOptions;
801 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
805 OIC_LOG(ERROR, TAG, "memory allocation failed");
806 CADestroyData(cadata, sizeof(CAData_t));
810 memcpy(headerOption, info->options, sizeof(CAHeaderOption_t) * numOptions);
812 cadata->options = headerOption;
813 cadata->numOptions = numOptions;
816 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
819 OIC_LOG(ERROR, TAG, "endpoint clone failed");
820 CADestroyData(cadata, sizeof(CAData_t));
824 cadata->remoteEndpoint = ep;
825 cadata->dataType = dataType;
829 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
831 OIC_LOG(DEBUG, TAG, "IN");
833 VERIFY_NON_NULL(object, TAG, "object");
834 VERIFY_NON_NULL(request, TAG, "request");
836 if (false == CAIsSelectedNetworkAvailable())
838 return CA_STATUS_FAILED;
842 // If max retransmission queue is reached, then don't handle new request
843 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
845 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
846 return CA_SEND_FAILED;
850 CAData_t *data = CAPrepareSendData(object, request, CA_REQUEST_DATA);
853 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
854 return CA_MEMORY_ALLOC_FAILED;
858 CAResult_t result = CAProcessSendData(data);
859 if(CA_STATUS_OK != result)
861 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
865 CADestroyData(data, sizeof(CAData_t));
868 if (CA_ADAPTER_GATT_BTLE != object->adapter)
871 CAResult_t res = CASendBlockWiseData(data);
872 if(CA_NOT_SUPPORTED == res)
874 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
875 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
880 CADestroyData(data, sizeof(CAData_t));
887 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
891 OIC_LOG(DEBUG, TAG, "OUT");
895 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
896 const CAResponseInfo_t *response)
898 OIC_LOG(DEBUG, TAG, "IN");
899 VERIFY_NON_NULL(object, TAG, "object");
900 VERIFY_NON_NULL(response, TAG, "response");
902 if (false == CAIsSelectedNetworkAvailable())
904 return CA_STATUS_FAILED;
907 CAData_t *data = CAPrepareSendData(object, response, CA_RESPONSE_DATA);
910 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
911 return CA_MEMORY_ALLOC_FAILED;
915 CAResult_t result = CAProcessSendData(data);
916 if(result != CA_STATUS_OK)
918 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
922 CADestroyData(data, sizeof(CAData_t));
925 if (CA_ADAPTER_GATT_BTLE != object->adapter)
928 CAResult_t res = CASendBlockWiseData(data);
929 if(CA_NOT_SUPPORTED == res)
931 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
932 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
937 CADestroyData(data, sizeof(CAData_t));
944 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
948 OIC_LOG(DEBUG, TAG, "OUT");
952 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
953 uint8_t tokenLength, const CAHeaderOption_t *options,
961 return CA_NOT_SUPPORTED;
964 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
965 CAErrorCallback errorHandler)
967 OIC_LOG(DEBUG, TAG, "IN");
968 g_requestHandler = ReqHandler;
969 g_responseHandler = RespHandler;
970 g_errorHandler = errorHandler;
971 OIC_LOG(DEBUG, TAG, "OUT");
974 CAResult_t CAInitializeMessageHandler()
976 OIC_LOG(DEBUG, TAG, "IN");
977 CASetPacketReceivedCallback(CAReceivedPacketCallback);
979 CASetNetworkChangeCallback(CANetworkChangedCallback);
980 CASetErrorHandleCallback(CAErrorHandler);
982 #ifndef SINGLE_THREAD
983 // create thread pool
984 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
986 if (CA_STATUS_OK != res)
988 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
992 // send thread initialize
993 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
994 CASendThreadProcess, CADestroyData))
996 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
997 return CA_STATUS_FAILED;
1000 // start send thread
1001 res = CAQueueingThreadStart(&g_sendThread);
1003 if (CA_STATUS_OK != res)
1005 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1006 ca_thread_pool_free(g_threadPoolHandle);
1007 g_threadPoolHandle = NULL;
1011 // receive thread initialize
1012 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1013 CAReceiveThreadProcess, CADestroyData))
1015 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1016 return CA_STATUS_FAILED;
1019 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1020 // start receive thread
1021 res = CAQueueingThreadStart(&gReceiveThread);
1023 if (res != CA_STATUS_OK)
1025 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1028 #endif /* SINGLE_HANDLE */
1030 // retransmission initialize
1031 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
1032 CATimeoutCallback, NULL);
1035 // block-wise transfer initialize
1036 CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1039 // start retransmission
1040 res = CARetransmissionStart(&g_retransmissionContext);
1042 if (CA_STATUS_OK != res)
1044 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1048 // initialize interface adapters by controller
1049 CAInitializeAdapters(g_threadPoolHandle);
1051 // retransmission initialize
1052 CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1053 CATimeoutCallback, NULL);
1054 CAInitializeAdapters();
1057 OIC_LOG(DEBUG, TAG, "OUT");
1058 return CA_STATUS_OK;
1061 void CATerminateMessageHandler()
1063 OIC_LOG(DEBUG, TAG, "IN");
1064 #ifndef SINGLE_THREAD
1065 CATransportAdapter_t connType;
1066 u_arraylist_t *list = CAGetSelectedNetworkList();
1067 uint32_t length = u_arraylist_length(list);
1070 for (i = 0; i < length; i++)
1072 void* ptrType = u_arraylist_get(list, i);
1074 if (NULL == ptrType)
1079 connType = *(CATransportAdapter_t *)ptrType;
1080 CAStopAdapter(connType);
1083 // stop retransmission
1084 if (NULL != g_retransmissionContext.threadMutex)
1086 CARetransmissionStop(&g_retransmissionContext);
1090 // delete thread data
1091 if (NULL != g_sendThread.threadMutex)
1093 CAQueueingThreadStop(&g_sendThread);
1097 // delete thread data
1098 if (NULL != g_receiveThread.threadMutex)
1100 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1101 CAQueueingThreadStop(&gReceiveThread);
1102 #endif /* SINGLE_HANDLE */
1105 // destroy thread pool
1106 if (NULL != g_threadPoolHandle)
1108 ca_thread_pool_free(g_threadPoolHandle);
1109 g_threadPoolHandle = NULL;
1113 CATerminateBlockWiseTransfer();
1115 CARetransmissionDestroy(&g_retransmissionContext);
1116 CAQueueingThreadDestroy(&g_sendThread);
1117 CAQueueingThreadDestroy(&g_receiveThread);
1119 // terminate interface adapters by controller
1120 CATerminateAdapters();
1122 // terminate interface adapters by controller
1123 CATerminateAdapters();
1125 // stop retransmission
1126 CARetransmissionStop(&g_retransmissionContext);
1127 CARetransmissionDestroy(&g_retransmissionContext);
1130 OIC_LOG(DEBUG, TAG, "OUT");
1133 void CALogPDUInfo(coap_pdu_t *pdu)
1135 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1137 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1139 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
1141 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
1143 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1145 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
1148 static void CALogPayloadInfo(CAInfo_t *info)
1154 for (uint32_t i = 0; i < info->numOptions; i++)
1156 OIC_LOG_V(DEBUG, TAG, "optionID: %d", info->options[i].optionID);
1158 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1164 OIC_LOG_V(DEBUG, TAG, "payload: %p(%u)", info->payload,
1170 OIC_LOG(DEBUG, TAG, "token:");
1171 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1174 OIC_LOG_V(DEBUG, TAG, "msgID: %d", info->messageId);
1178 OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1182 void CAErrorHandler(const CAEndpoint_t *endpoint,
1183 const void *data, uint32_t dataLen,
1186 OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1188 #ifndef SINGLE_THREAD
1190 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1191 VERIFY_NON_NULL_VOID(data, TAG, "data");
1193 uint32_t code = CA_NOT_FOUND;
1194 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1196 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
1199 OIC_LOG(ERROR, TAG, "Parse PDU failed");
1203 CAData_t *cadata = CAGenerateHandlerData(endpoint, pdu, CA_ERROR_DATA);
1206 OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1207 coap_delete_pdu(pdu);
1211 cadata->errorInfo->result = result;
1213 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1214 coap_delete_pdu(pdu);
1217 OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1221 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1223 OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1224 #ifndef SINGLE_THREAD
1225 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1228 OIC_LOG(ERROR, TAG, "memory allocation failed");
1232 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1235 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1240 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1248 CAResult_t res = CACloneInfo(info, &errorInfo->info);
1249 if (CA_STATUS_OK != res)
1256 errorInfo->result = result;
1257 cadata->remoteEndpoint = ep;
1258 cadata->errorInfo = errorInfo;
1259 cadata->dataType = CA_ERROR_DATA;
1261 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1263 OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");