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);
348 OICFree(cadata->options);
350 OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
354 static void CAProcessReceivedData(CAData_t *data)
356 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
359 OIC_LOG(ERROR, TAG, "thread data error!!");
363 // parse the data and call the callbacks.
366 CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
369 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
373 if (data->requestInfo && g_requestHandler)
375 g_requestHandler(rep, data->requestInfo);
377 else if (data->responseInfo && g_responseHandler)
379 g_responseHandler(rep, data->responseInfo);
381 else if (data->errorInfo && g_errorHandler)
383 g_errorHandler(rep, data->errorInfo);
387 CADestroyData(data, sizeof(CAData_t));
390 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
394 #ifndef SINGLE_THREAD
396 static void CAReceiveThreadProcess(void *threadData)
398 OIC_LOG(DEBUG, TAG, "IN");
399 #ifndef SINGLE_HANDLE
400 CAData_t *data = (CAData_t *) threadData;
401 CAProcessReceivedData(data);
405 OIC_LOG(DEBUG, TAG, "OUT");
409 static CAResult_t CAProcessSendData(const CAData_t *data)
411 OIC_LOG(DEBUG, TAG, "IN");
412 VERIFY_NON_NULL(data, TAG, "data");
413 VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
415 CAResult_t res = CA_STATUS_FAILED;
417 CASendDataType_t type = data->type;
419 coap_pdu_t *pdu = NULL;
420 CAInfo_t *info = NULL;
422 if (SEND_TYPE_UNICAST == type)
425 OIC_LOG(DEBUG,TAG,"Unicast message");
426 if (NULL != data->requestInfo)
428 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
430 info = &data->requestInfo->info;
431 pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint);
433 else if (NULL != data->responseInfo)
435 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
437 info = &data->responseInfo->info;
438 pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint);
442 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
443 return CA_STATUS_INVALID_PARAM;
446 // interface controller function call.
450 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
452 // Blockwise transfer
455 CAResult_t res = CAAddBlockOption(&pdu, *info,
456 data->remoteEndpoint);
457 if (CA_STATUS_OK != res)
459 OIC_LOG(INFO, TAG, "to write block option has failed");
460 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
461 coap_delete_pdu(pdu);
469 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
470 if (CA_STATUS_OK != res)
472 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
473 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
474 coap_delete_pdu(pdu);
477 // for retransmission
478 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
480 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
482 //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
483 OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
484 coap_delete_pdu(pdu);
488 coap_delete_pdu(pdu);
492 OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
493 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
494 return CA_SEND_FAILED;
497 else if (SEND_TYPE_MULTICAST == type)
499 OIC_LOG(DEBUG,TAG,"Multicast message");
500 if (NULL != data->requestInfo)
502 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
504 info = &data->requestInfo->info;
505 pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint);
509 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
511 // Blockwise transfer
512 CAResult_t res = CAAddBlockOption(&pdu, data->requestInfo->info,
513 data->remoteEndpoint);
514 if (CA_STATUS_OK != res)
516 OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
517 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
518 coap_delete_pdu(pdu);
525 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
526 if (CA_STATUS_OK != res)
528 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
529 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
530 coap_delete_pdu(pdu);
534 coap_delete_pdu(pdu);
538 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
539 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
540 return CA_SEND_FAILED;
545 OIC_LOG(ERROR, TAG, "request info is empty");
546 return CA_SEND_FAILED;
550 OIC_LOG(DEBUG, TAG, "OUT");
554 #ifndef SINGLE_THREAD
555 static void CASendThreadProcess(void *threadData)
557 CAData_t *data = (CAData_t *) threadData;
558 CAProcessSendData(data);
564 * If a second message arrives with the same token and the other address
565 * family, drop it. Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
567 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id)
573 if (ep->adapter != CA_ADAPTER_IP)
577 if (!caglobals.ip.dualstack)
583 CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
585 for (int i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
587 CAHistoryItem_t *item = &(history->items[i]);
588 if (id == item->messageId)
590 if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
592 OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
593 familyFlags & CA_IPV6 ? '6' : '4');
600 history->items[history->nextIndex].flags = familyFlags;
601 history->items[history->nextIndex].messageId = id;
602 if (++history->nextIndex >= HISTORYSIZE)
604 history->nextIndex = 0;
610 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
611 const void *data, uint32_t dataLen)
613 OIC_LOG(DEBUG, TAG, "IN");
614 VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
615 VERIFY_NON_NULL_VOID(data, TAG, "data");
617 uint32_t code = CA_NOT_FOUND;
618 CAData_t *cadata = NULL;
620 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
623 OIC_LOG(ERROR, TAG, "Parse PDU failed");
627 OIC_LOG_V(DEBUG, TAG, "code = %d", code);
628 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
630 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
633 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
634 coap_delete_pdu(pdu);
640 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
643 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
644 coap_delete_pdu(pdu);
648 // for retransmission
649 void *retransmissionPdu = NULL;
650 CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
651 pdu->length, &retransmissionPdu);
653 // get token from saved data in retransmission list
654 if (retransmissionPdu && CA_EMPTY == code)
656 if (cadata->responseInfo)
658 CAInfo_t *info = &cadata->responseInfo->info;
659 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
661 if (CA_STATUS_OK != res)
663 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
664 OICFree(info->token);
665 info->tokenLength = 0;
669 OICFree(retransmissionPdu);
672 cadata->type = SEND_TYPE_UNICAST;
675 CAProcessReceivedData(cadata);
678 if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter)
680 CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
681 if (CA_NOT_SUPPORTED == res)
683 OIC_LOG(ERROR, TAG, "this message does not have block option");
684 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
688 CADestroyData(cadata, sizeof(CAData_t));
694 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
698 coap_delete_pdu(pdu);
700 OIC_LOG(DEBUG, TAG, "OUT");
703 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
707 OIC_LOG(DEBUG, TAG, "IN");
709 OIC_LOG(DEBUG, TAG, "OUT");
712 void CAHandleRequestResponseCallbacks()
716 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
719 // parse the data and call the callbacks.
723 ca_mutex_lock(g_receiveThread.threadMutex);
725 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
727 ca_mutex_unlock(g_receiveThread.threadMutex);
735 void *msg = item->msg;
743 CAData_t *td = (CAData_t *) msg;
745 if (td->requestInfo && g_requestHandler)
747 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
748 g_requestHandler(td->remoteEndpoint, td->requestInfo);
750 else if (td->responseInfo && g_responseHandler)
752 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
753 g_responseHandler(td->remoteEndpoint, td->responseInfo);
755 else if (td->errorInfo && g_errorHandler)
757 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
758 g_errorHandler(td->remoteEndpoint, td->errorInfo);
761 CADestroyData(msg, sizeof(CAData_t));
764 #endif /* SINGLE_HANDLE */
768 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
769 CADataType_t dataType)
771 OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
772 CAInfo_t *info = NULL;
774 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
777 OIC_LOG(ERROR, TAG, "memory allocation failed");
781 if(CA_REQUEST_DATA == dataType)
783 // clone request info
784 CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
788 OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
793 cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
794 info = &request->info;
795 cadata->requestInfo = request;
797 else if(CA_RESPONSE_DATA == dataType)
799 // clone response info
800 CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
804 OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
809 cadata->type = SEND_TYPE_UNICAST;
810 info = &response->info;
811 cadata->responseInfo = response;
815 OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
820 if (NULL != info->options && 0 < info->numOptions)
822 uint8_t numOptions = info->numOptions;
824 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
828 OIC_LOG(ERROR, TAG, "memory allocation failed");
829 CADestroyData(cadata, sizeof(CAData_t));
833 memcpy(headerOption, info->options, sizeof(CAHeaderOption_t) * numOptions);
835 cadata->options = headerOption;
836 cadata->numOptions = numOptions;
839 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
842 OIC_LOG(ERROR, TAG, "endpoint clone failed");
843 CADestroyData(cadata, sizeof(CAData_t));
847 cadata->remoteEndpoint = ep;
848 cadata->dataType = dataType;
852 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
854 OIC_LOG(DEBUG, TAG, "IN");
856 VERIFY_NON_NULL(object, TAG, "object");
857 VERIFY_NON_NULL(request, TAG, "request");
859 if (false == CAIsSelectedNetworkAvailable())
861 return CA_STATUS_FAILED;
865 // If max retransmission queue is reached, then don't handle new request
866 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
868 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
869 return CA_SEND_FAILED;
873 CAData_t *data = CAPrepareSendData(object, request, CA_REQUEST_DATA);
876 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
877 return CA_MEMORY_ALLOC_FAILED;
881 CAResult_t result = CAProcessSendData(data);
882 if(CA_STATUS_OK != result)
884 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
888 CADestroyData(data, sizeof(CAData_t));
891 if (CA_ADAPTER_GATT_BTLE != object->adapter)
894 CAResult_t res = CASendBlockWiseData(data);
895 if(CA_NOT_SUPPORTED == res)
897 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
898 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
903 CADestroyData(data, sizeof(CAData_t));
910 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
914 OIC_LOG(DEBUG, TAG, "OUT");
918 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
919 const CAResponseInfo_t *response)
921 OIC_LOG(DEBUG, TAG, "IN");
922 VERIFY_NON_NULL(object, TAG, "object");
923 VERIFY_NON_NULL(response, TAG, "response");
925 if (false == CAIsSelectedNetworkAvailable())
927 return CA_STATUS_FAILED;
930 CAData_t *data = CAPrepareSendData(object, response, CA_RESPONSE_DATA);
933 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
934 return CA_MEMORY_ALLOC_FAILED;
938 CAResult_t result = CAProcessSendData(data);
939 if(result != CA_STATUS_OK)
941 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
945 CADestroyData(data, sizeof(CAData_t));
948 if (CA_ADAPTER_GATT_BTLE != object->adapter)
951 CAResult_t res = CASendBlockWiseData(data);
952 if(CA_NOT_SUPPORTED == res)
954 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
955 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
960 CADestroyData(data, sizeof(CAData_t));
967 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
971 OIC_LOG(DEBUG, TAG, "OUT");
975 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
976 uint8_t tokenLength, const CAHeaderOption_t *options,
984 return CA_NOT_SUPPORTED;
987 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
988 CAErrorCallback errorHandler)
990 OIC_LOG(DEBUG, TAG, "IN");
991 g_requestHandler = ReqHandler;
992 g_responseHandler = RespHandler;
993 g_errorHandler = errorHandler;
994 OIC_LOG(DEBUG, TAG, "OUT");
997 CAResult_t CAInitializeMessageHandler()
999 OIC_LOG(DEBUG, TAG, "IN");
1000 CASetPacketReceivedCallback(CAReceivedPacketCallback);
1002 CASetNetworkChangeCallback(CANetworkChangedCallback);
1003 CASetErrorHandleCallback(CAErrorHandler);
1005 #ifndef SINGLE_THREAD
1006 // create thread pool
1007 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1009 if (CA_STATUS_OK != res)
1011 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1015 // send thread initialize
1016 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1017 CASendThreadProcess, CADestroyData))
1019 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1020 return CA_STATUS_FAILED;
1023 // start send thread
1024 res = CAQueueingThreadStart(&g_sendThread);
1026 if (CA_STATUS_OK != res)
1028 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1029 ca_thread_pool_free(g_threadPoolHandle);
1030 g_threadPoolHandle = NULL;
1034 // receive thread initialize
1035 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1036 CAReceiveThreadProcess, CADestroyData))
1038 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1039 return CA_STATUS_FAILED;
1042 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1043 // start receive thread
1044 res = CAQueueingThreadStart(&gReceiveThread);
1046 if (res != CA_STATUS_OK)
1048 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1051 #endif /* SINGLE_HANDLE */
1053 // retransmission initialize
1054 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
1055 CATimeoutCallback, NULL);
1058 // block-wise transfer initialize
1059 CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1062 // start retransmission
1063 res = CARetransmissionStart(&g_retransmissionContext);
1065 if (CA_STATUS_OK != res)
1067 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1071 // initialize interface adapters by controller
1072 CAInitializeAdapters(g_threadPoolHandle);
1074 // retransmission initialize
1075 CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1076 CATimeoutCallback, NULL);
1077 CAInitializeAdapters();
1080 OIC_LOG(DEBUG, TAG, "OUT");
1081 return CA_STATUS_OK;
1084 void CATerminateMessageHandler()
1086 OIC_LOG(DEBUG, TAG, "IN");
1087 #ifndef SINGLE_THREAD
1088 CATransportAdapter_t connType;
1089 u_arraylist_t *list = CAGetSelectedNetworkList();
1090 uint32_t length = u_arraylist_length(list);
1093 for (i = 0; i < length; i++)
1095 void* ptrType = u_arraylist_get(list, i);
1097 if (NULL == ptrType)
1102 connType = *(CATransportAdapter_t *)ptrType;
1103 CAStopAdapter(connType);
1106 // stop retransmission
1107 if (NULL != g_retransmissionContext.threadMutex)
1109 CARetransmissionStop(&g_retransmissionContext);
1113 // delete thread data
1114 if (NULL != g_sendThread.threadMutex)
1116 CAQueueingThreadStop(&g_sendThread);
1120 // delete thread data
1121 if (NULL != g_receiveThread.threadMutex)
1123 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1124 CAQueueingThreadStop(&gReceiveThread);
1125 #endif /* SINGLE_HANDLE */
1128 // destroy thread pool
1129 if (NULL != g_threadPoolHandle)
1131 ca_thread_pool_free(g_threadPoolHandle);
1132 g_threadPoolHandle = NULL;
1136 CATerminateBlockWiseTransfer();
1138 CARetransmissionDestroy(&g_retransmissionContext);
1139 CAQueueingThreadDestroy(&g_sendThread);
1140 CAQueueingThreadDestroy(&g_receiveThread);
1142 // terminate interface adapters by controller
1143 CATerminateAdapters();
1145 // terminate interface adapters by controller
1146 CATerminateAdapters();
1148 // stop retransmission
1149 CARetransmissionStop(&g_retransmissionContext);
1150 CARetransmissionDestroy(&g_retransmissionContext);
1153 OIC_LOG(DEBUG, TAG, "OUT");
1156 void CALogPDUInfo(coap_pdu_t *pdu)
1158 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1160 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1162 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
1164 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
1166 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1168 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
1171 static void CALogPayloadInfo(CAInfo_t *info)
1177 for (uint32_t i = 0; i < info->numOptions; i++)
1179 OIC_LOG_V(DEBUG, TAG, "optionID: %d", info->options[i].optionID);
1181 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1187 OIC_LOG_V(DEBUG, TAG, "payload: %p(%u)", info->payload,
1193 OIC_LOG(DEBUG, TAG, "token:");
1194 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1197 OIC_LOG_V(DEBUG, TAG, "msgID: %d", info->messageId);
1201 OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1205 void CAErrorHandler(const CAEndpoint_t *endpoint,
1206 const void *data, uint32_t dataLen,
1209 OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1211 #ifndef SINGLE_THREAD
1213 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1214 VERIFY_NON_NULL_VOID(data, TAG, "data");
1216 uint32_t code = CA_NOT_FOUND;
1217 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1219 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
1222 OIC_LOG(ERROR, TAG, "Parse PDU failed");
1226 CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1229 OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1230 coap_delete_pdu(pdu);
1234 cadata->errorInfo->result = result;
1236 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1237 coap_delete_pdu(pdu);
1240 OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1244 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1246 OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1247 #ifndef SINGLE_THREAD
1248 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1251 OIC_LOG(ERROR, TAG, "memory allocation failed");
1255 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1258 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1263 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1271 CAResult_t res = CACloneInfo(info, &errorInfo->info);
1272 if (CA_STATUS_OK != res)
1279 errorInfo->result = result;
1280 cadata->remoteEndpoint = ep;
1281 cadata->errorInfo = errorInfo;
1282 cadata->dataType = CA_ERROR_DATA;
1284 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1286 OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");