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);
524 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
525 if (CA_STATUS_OK != res)
527 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
528 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
529 coap_delete_pdu(pdu);
533 coap_delete_pdu(pdu);
537 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
538 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
539 return CA_SEND_FAILED;
544 OIC_LOG(ERROR, TAG, "request info is empty");
545 return CA_SEND_FAILED;
549 OIC_LOG(DEBUG, TAG, "OUT");
553 #ifndef SINGLE_THREAD
554 static void CASendThreadProcess(void *threadData)
556 CAData_t *data = (CAData_t *) threadData;
557 CAProcessSendData(data);
563 * If a second message arrives with the same token and the other address
564 * family, drop it. Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
566 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id)
572 if (ep->adapter != CA_ADAPTER_IP)
576 if (!caglobals.ip.dualstack)
582 CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
584 for (int i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
586 CAHistoryItem_t *item = &(history->items[i]);
587 if (id == item->messageId)
589 if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
591 OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
592 familyFlags & CA_IPV6 ? '6' : '4');
599 history->items[history->nextIndex].flags = familyFlags;
600 history->items[history->nextIndex].messageId = id;
601 if (++history->nextIndex >= HISTORYSIZE)
603 history->nextIndex = 0;
609 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
610 const void *data, uint32_t dataLen)
612 OIC_LOG(DEBUG, TAG, "IN");
613 VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
614 VERIFY_NON_NULL_VOID(data, TAG, "data");
616 uint32_t code = CA_NOT_FOUND;
617 CAData_t *cadata = NULL;
619 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
622 OIC_LOG(ERROR, TAG, "Parse PDU failed");
626 OIC_LOG_V(DEBUG, TAG, "code = %d", code);
627 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
629 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
632 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
633 coap_delete_pdu(pdu);
639 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
642 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
643 coap_delete_pdu(pdu);
647 // for retransmission
648 void *retransmissionPdu = NULL;
649 CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
650 pdu->length, &retransmissionPdu);
652 // get token from saved data in retransmission list
653 if (retransmissionPdu && CA_EMPTY == code)
655 if (cadata->responseInfo)
657 CAInfo_t *info = &cadata->responseInfo->info;
658 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
660 if (CA_STATUS_OK != res)
662 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
663 OICFree(info->token);
664 info->tokenLength = 0;
668 OICFree(retransmissionPdu);
671 cadata->type = SEND_TYPE_UNICAST;
674 CAProcessReceivedData(cadata);
677 if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter)
679 CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
680 if (CA_NOT_SUPPORTED == res)
682 OIC_LOG(ERROR, TAG, "this message does not have block option");
683 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
687 CADestroyData(cadata, sizeof(CAData_t));
693 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
697 coap_delete_pdu(pdu);
699 OIC_LOG(DEBUG, TAG, "OUT");
702 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
706 OIC_LOG(DEBUG, TAG, "IN");
708 OIC_LOG(DEBUG, TAG, "OUT");
711 void CAHandleRequestResponseCallbacks()
715 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
718 // parse the data and call the callbacks.
722 ca_mutex_lock(g_receiveThread.threadMutex);
724 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
726 ca_mutex_unlock(g_receiveThread.threadMutex);
734 void *msg = item->msg;
742 CAData_t *td = (CAData_t *) msg;
744 if (td->requestInfo && g_requestHandler)
746 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
747 g_requestHandler(td->remoteEndpoint, td->requestInfo);
749 else if (td->responseInfo && g_responseHandler)
751 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
752 g_responseHandler(td->remoteEndpoint, td->responseInfo);
754 else if (td->errorInfo && g_errorHandler)
756 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
757 g_errorHandler(td->remoteEndpoint, td->errorInfo);
760 CADestroyData(msg, sizeof(CAData_t));
763 #endif /* SINGLE_HANDLE */
767 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
768 CADataType_t dataType)
770 OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
772 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
775 OIC_LOG(ERROR, TAG, "memory allocation failed");
779 if(CA_REQUEST_DATA == dataType)
781 // clone request info
782 CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
786 OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
791 cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
792 cadata->requestInfo = request;
794 else if(CA_RESPONSE_DATA == dataType)
796 // clone response info
797 CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
801 OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
806 cadata->type = SEND_TYPE_UNICAST;
807 cadata->responseInfo = response;
811 OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
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, NULL, 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");