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 CAData_t *cadata = (CAData_t *) data;
305 OIC_LOG(ERROR, TAG, "cadata is NULL");
309 if (NULL != cadata->remoteEndpoint)
311 CAFreeEndpoint(cadata->remoteEndpoint);
314 if (NULL != cadata->requestInfo)
316 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
319 if (NULL != cadata->responseInfo)
321 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
324 if (NULL != cadata->errorInfo)
326 CADestroyErrorInfoInternal(cadata->errorInfo);
329 OICFree(cadata->options);
331 OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
335 static void CAProcessReceivedData(CAData_t *data)
337 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
340 OIC_LOG(ERROR, TAG, "thread data error!!");
344 // parse the data and call the callbacks.
347 CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
350 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
354 if (data->requestInfo && g_requestHandler)
356 g_requestHandler(rep, data->requestInfo);
358 else if (data->responseInfo && g_responseHandler)
360 g_responseHandler(rep, data->responseInfo);
362 else if (data->errorInfo && g_errorHandler)
364 g_errorHandler(rep, data->errorInfo);
368 CADestroyData(data, sizeof(CAData_t));
371 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
375 #ifndef SINGLE_THREAD
377 static void CAReceiveThreadProcess(void *threadData)
379 OIC_LOG(DEBUG, TAG, "IN");
380 #ifndef SINGLE_HANDLE
381 CAData_t *data = (CAData_t *) threadData;
382 CAProcessReceivedData(data);
384 OIC_LOG(DEBUG, TAG, "OUT");
388 static CAResult_t CAProcessSendData(const CAData_t *data)
390 OIC_LOG(DEBUG, TAG, "IN");
391 VERIFY_NON_NULL(data, TAG, "data");
392 VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
394 CAResult_t res = CA_STATUS_FAILED;
396 CASendDataType_t type = data->type;
398 coap_pdu_t *pdu = NULL;
399 CAInfo_t *info = NULL;
401 if (SEND_TYPE_UNICAST == type)
404 OIC_LOG(DEBUG,TAG,"Unicast message");
405 if (NULL != data->requestInfo)
407 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
409 info = &data->requestInfo->info;
410 pdu = CAGeneratePDU(data->requestInfo->method, info);
412 else if (NULL != data->responseInfo)
414 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
416 info = &data->responseInfo->info;
417 pdu = CAGeneratePDU(data->responseInfo->result, info);
421 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
422 return CA_STATUS_INVALID_PARAM;
425 // interface controller function call.
429 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
431 // Blockwise transfer
434 CAResult_t res = CAAddBlockOption(&pdu, *info,
435 data->remoteEndpoint);
436 if (CA_STATUS_OK != res)
438 OIC_LOG(INFO, TAG, "to write block option has failed");
439 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
440 coap_delete_pdu(pdu);
448 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
449 if (CA_STATUS_OK != res)
451 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
452 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
453 coap_delete_pdu(pdu);
456 // for retransmission
457 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
459 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
461 //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
462 OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
463 coap_delete_pdu(pdu);
467 coap_delete_pdu(pdu);
471 OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
472 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
473 return CA_SEND_FAILED;
476 else if (SEND_TYPE_MULTICAST == type)
478 OIC_LOG(DEBUG,TAG,"Multicast message");
479 if (NULL != data->requestInfo)
481 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
483 info = &data->requestInfo->info;
484 pdu = CAGeneratePDU(CA_GET, info);
488 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
490 // Blockwise transfer
491 CAResult_t res = CAAddBlockOption(&pdu, data->requestInfo->info,
492 data->remoteEndpoint);
493 if (CA_STATUS_OK != res)
495 OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
496 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
497 coap_delete_pdu(pdu);
504 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
505 if (CA_STATUS_OK != res)
507 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
508 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
509 coap_delete_pdu(pdu);
513 coap_delete_pdu(pdu);
517 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
518 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
519 return CA_SEND_FAILED;
524 OIC_LOG(ERROR, TAG, "request info is empty");
525 return CA_SEND_FAILED;
529 OIC_LOG(DEBUG, TAG, "OUT");
533 #ifndef SINGLE_THREAD
534 static void CASendThreadProcess(void *threadData)
536 CAData_t *data = (CAData_t *) threadData;
537 CAProcessSendData(data);
543 * If a second message arrives with the same token and the other address
544 * family, drop it. Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
545 * This can be made more robust (for instance, another message could arrive
546 * in between), but it is good enough for now.
548 static bool CADropSecondRequest(const CAEndpoint_t *endpoint, uint16_t messageId)
554 if (endpoint->adapter != CA_ADAPTER_IP)
560 CATransportFlags_t familyFlags = endpoint->flags & CA_IPFAMILY_MASK;
562 if (messageId == caglobals.ca.previousRequestMessageId)
564 if ((familyFlags ^ caglobals.ca.previousRequestFlags) == CA_IPFAMILY_MASK)
566 if (familyFlags & CA_IPV6)
568 OIC_LOG(INFO, TAG, "IPv6 duplicate response ignored");
572 OIC_LOG(INFO, TAG, "IPv4 duplicate response ignored");
577 caglobals.ca.previousRequestFlags = familyFlags;
578 caglobals.ca.previousRequestMessageId = messageId;
582 static void CAReceivedPacketCallback(const CAEndpoint_t *remoteEndpoint, const void *data, uint32_t dataLen)
584 OIC_LOG(DEBUG, TAG, "IN");
585 VERIFY_NON_NULL_VOID(remoteEndpoint, TAG, "remoteEndpoint");
586 VERIFY_NON_NULL_VOID(data, TAG, "data");
588 uint32_t code = CA_NOT_FOUND;
589 CAData_t *cadata = NULL;
591 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
594 OIC_LOG(ERROR, TAG, "Parse PDU failed");
598 OIC_LOG_V(DEBUG, TAG, "code = %d", code);
599 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
601 cadata = CAGenerateHandlerData(remoteEndpoint, pdu, CA_REQUEST_DATA);
604 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
605 coap_delete_pdu(pdu);
611 cadata = CAGenerateHandlerData(remoteEndpoint, pdu, CA_RESPONSE_DATA);
614 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
615 coap_delete_pdu(pdu);
619 // for retransmission
620 void *retransmissionPdu = NULL;
621 CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
622 pdu->length, &retransmissionPdu);
624 // get token from saved data in retransmission list
625 if (retransmissionPdu && CA_EMPTY == code)
627 if (cadata->responseInfo)
629 CAInfo_t *info = &cadata->responseInfo->info;
630 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
632 if (CA_STATUS_OK != res)
634 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
635 OICFree(info->token);
636 info->tokenLength = 0;
640 OICFree(retransmissionPdu);
643 cadata->type = SEND_TYPE_UNICAST;
646 CAProcessReceivedData(cadata);
649 if (CA_ADAPTER_GATT_BTLE != remoteEndpoint->adapter)
651 CAResult_t res = CAReceiveBlockWiseData(pdu, remoteEndpoint, cadata, dataLen);
652 if (CA_NOT_SUPPORTED == res)
654 OIC_LOG(ERROR, TAG, "this message does not have block option");
655 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
659 CADestroyData(cadata, sizeof(CAData_t));
665 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
669 coap_delete_pdu(pdu);
671 OIC_LOG(DEBUG, TAG, "OUT");
674 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
676 OIC_LOG(DEBUG, TAG, "IN");
678 OIC_LOG(DEBUG, TAG, "OUT");
681 void CAHandleRequestResponseCallbacks()
685 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
688 // parse the data and call the callbacks.
692 ca_mutex_lock(g_receiveThread.threadMutex);
694 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
696 ca_mutex_unlock(g_receiveThread.threadMutex);
704 void *msg = item->msg;
712 CAData_t *td = (CAData_t *) msg;
714 if (td->requestInfo && g_requestHandler)
716 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
717 g_requestHandler(td->remoteEndpoint, td->requestInfo);
719 else if (td->responseInfo && g_responseHandler)
721 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
722 g_responseHandler(td->remoteEndpoint, td->responseInfo);
724 else if (td->errorInfo && g_errorHandler)
726 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
727 g_errorHandler(td->remoteEndpoint, td->errorInfo);
730 CADestroyData(msg, sizeof(CAData_t));
733 #endif /* SINGLE_HANDLE */
737 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
738 CADataType_t dataType)
740 OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
741 CAInfo_t *info = NULL;
743 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
746 OIC_LOG(ERROR, TAG, "memory allocation failed");
750 if(CA_REQUEST_DATA == dataType)
752 // clone request info
753 CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
757 OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
762 cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
763 info = &request->info;
764 cadata->requestInfo = request;
766 else if(CA_RESPONSE_DATA == dataType)
768 // clone response info
769 CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
773 OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
778 cadata->type = SEND_TYPE_UNICAST;
779 info = &response->info;
780 cadata->responseInfo = response;
784 OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
789 if (NULL != info->options && 0 < info->numOptions)
791 uint8_t numOptions = info->numOptions;
793 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
797 OIC_LOG(ERROR, TAG, "memory allocation failed");
798 CADestroyData(cadata, sizeof(CAData_t));
802 memcpy(headerOption, info->options, sizeof(CAHeaderOption_t) * numOptions);
804 cadata->options = headerOption;
805 cadata->numOptions = numOptions;
808 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
811 OIC_LOG(ERROR, TAG, "endpoint clone failed");
812 CADestroyData(cadata, sizeof(CAData_t));
816 cadata->remoteEndpoint = ep;
817 cadata->dataType = dataType;
821 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
823 OIC_LOG(DEBUG, TAG, "IN");
825 VERIFY_NON_NULL(object, TAG, "object");
826 VERIFY_NON_NULL(request, TAG, "request");
828 if (false == CAIsSelectedNetworkAvailable())
830 return CA_STATUS_FAILED;
834 // If max retransmission queue is reached, then don't handle new request
835 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
837 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
838 return CA_SEND_FAILED;
842 CAData_t *data = CAPrepareSendData(object, request, CA_REQUEST_DATA);
845 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
846 return CA_MEMORY_ALLOC_FAILED;
850 CAResult_t result = CAProcessSendData(data);
851 if(CA_STATUS_OK != result)
853 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
857 CADestroyData(data, sizeof(CAData_t));
860 if (CA_ADAPTER_GATT_BTLE != object->adapter)
863 CAResult_t res = CASendBlockWiseData(data);
864 if(CA_NOT_SUPPORTED == res)
866 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
867 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
872 CADestroyData(data, sizeof(CAData_t));
879 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
883 OIC_LOG(DEBUG, TAG, "OUT");
887 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
888 const CAResponseInfo_t *response)
890 OIC_LOG(DEBUG, TAG, "IN");
891 VERIFY_NON_NULL(object, TAG, "object");
892 VERIFY_NON_NULL(response, TAG, "response");
894 if (false == CAIsSelectedNetworkAvailable())
896 return CA_STATUS_FAILED;
899 CAData_t *data = CAPrepareSendData(object, response, CA_RESPONSE_DATA);
902 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
903 return CA_MEMORY_ALLOC_FAILED;
907 CAResult_t result = CAProcessSendData(data);
908 if(result != CA_STATUS_OK)
910 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
914 CADestroyData(data, sizeof(CAData_t));
917 if (CA_ADAPTER_GATT_BTLE != object->adapter)
920 CAResult_t res = CASendBlockWiseData(data);
921 if(CA_NOT_SUPPORTED == res)
923 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
924 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
929 CADestroyData(data, sizeof(CAData_t));
936 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
940 OIC_LOG(DEBUG, TAG, "OUT");
944 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
945 uint8_t tokenLength, const CAHeaderOption_t *options,
948 return CA_NOT_SUPPORTED;
951 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
952 CAErrorCallback errorHandler)
954 OIC_LOG(DEBUG, TAG, "IN");
955 g_requestHandler = ReqHandler;
956 g_responseHandler = RespHandler;
957 g_errorHandler = errorHandler;
958 OIC_LOG(DEBUG, TAG, "OUT");
961 CAResult_t CAInitializeMessageHandler()
963 OIC_LOG(DEBUG, TAG, "IN");
964 CASetPacketReceivedCallback(CAReceivedPacketCallback);
966 CASetNetworkChangeCallback(CANetworkChangedCallback);
967 CASetErrorHandleCallback(CAErrorHandler);
969 #ifndef SINGLE_THREAD
970 // create thread pool
971 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
973 if (CA_STATUS_OK != res)
975 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
979 // send thread initialize
980 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
981 CASendThreadProcess, CADestroyData))
983 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
984 return CA_STATUS_FAILED;
988 res = CAQueueingThreadStart(&g_sendThread);
990 if (CA_STATUS_OK != res)
992 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
993 ca_thread_pool_free(g_threadPoolHandle);
994 g_threadPoolHandle = NULL;
998 // receive thread initialize
999 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1000 CAReceiveThreadProcess, CADestroyData))
1002 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1003 return CA_STATUS_FAILED;
1006 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1007 // start receive thread
1008 res = CAQueueingThreadStart(&gReceiveThread);
1010 if (res != CA_STATUS_OK)
1012 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1015 #endif /* SINGLE_HANDLE */
1017 // retransmission initialize
1018 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
1019 CATimeoutCallback, NULL);
1022 // block-wise transfer initialize
1023 CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1026 // start retransmission
1027 res = CARetransmissionStart(&g_retransmissionContext);
1029 if (CA_STATUS_OK != res)
1031 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1035 // initialize interface adapters by controller
1036 CAInitializeAdapters(g_threadPoolHandle);
1038 // retransmission initialize
1039 CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1040 CATimeoutCallback, NULL);
1041 CAInitializeAdapters();
1044 OIC_LOG(DEBUG, TAG, "OUT");
1045 return CA_STATUS_OK;
1048 void CATerminateMessageHandler()
1050 OIC_LOG(DEBUG, TAG, "IN");
1051 #ifndef SINGLE_THREAD
1052 CATransportAdapter_t connType;
1053 u_arraylist_t *list = CAGetSelectedNetworkList();
1054 uint32_t length = u_arraylist_length(list);
1057 for (i = 0; i < length; i++)
1059 void* ptrType = u_arraylist_get(list, i);
1061 if (NULL == ptrType)
1066 connType = *(CATransportAdapter_t *)ptrType;
1067 CAStopAdapter(connType);
1070 // stop retransmission
1071 if (NULL != g_retransmissionContext.threadMutex)
1073 CARetransmissionStop(&g_retransmissionContext);
1077 // delete thread data
1078 if (NULL != g_sendThread.threadMutex)
1080 CAQueueingThreadStop(&g_sendThread);
1084 // delete thread data
1085 if (NULL != g_receiveThread.threadMutex)
1087 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1088 CAQueueingThreadStop(&gReceiveThread);
1089 #endif /* SINGLE_HANDLE */
1092 // destroy thread pool
1093 if (NULL != g_threadPoolHandle)
1095 ca_thread_pool_free(g_threadPoolHandle);
1096 g_threadPoolHandle = NULL;
1100 CATerminateBlockWiseTransfer();
1102 CARetransmissionDestroy(&g_retransmissionContext);
1103 CAQueueingThreadDestroy(&g_sendThread);
1104 CAQueueingThreadDestroy(&g_receiveThread);
1106 // terminate interface adapters by controller
1107 CATerminateAdapters();
1109 // terminate interface adapters by controller
1110 CATerminateAdapters();
1112 // stop retransmission
1113 CARetransmissionStop(&g_retransmissionContext);
1114 CARetransmissionDestroy(&g_retransmissionContext);
1117 OIC_LOG(DEBUG, TAG, "OUT");
1120 void CALogPDUInfo(coap_pdu_t *pdu)
1122 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1124 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1126 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
1128 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
1130 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1132 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
1135 static void CALogPayloadInfo(CAInfo_t *info)
1141 for (uint32_t i = 0; i < info->numOptions; i++)
1143 OIC_LOG_V(DEBUG, TAG, "optionID: %d", info->options[i].optionID);
1145 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1151 OIC_LOG_V(DEBUG, TAG, "payload: %p(%u)", info->payload,
1157 OIC_LOG(DEBUG, TAG, "token:");
1158 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1161 OIC_LOG_V(DEBUG, TAG, "msgID: %d", info->messageId);
1165 OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1169 void CAErrorHandler(const CAEndpoint_t *endpoint,
1170 const void *data, uint32_t dataLen,
1173 OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1175 #ifndef SINGLE_THREAD
1177 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1178 VERIFY_NON_NULL_VOID(data, TAG, "data");
1180 uint32_t code = CA_NOT_FOUND;
1181 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1183 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
1186 OIC_LOG(ERROR, TAG, "Parse PDU failed");
1190 CAData_t *cadata = CAGenerateHandlerData(endpoint, pdu, CA_ERROR_DATA);
1193 OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1194 coap_delete_pdu(pdu);
1198 cadata->errorInfo->result = result;
1200 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1201 coap_delete_pdu(pdu);
1204 OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1208 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1210 OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1211 #ifndef SINGLE_THREAD
1212 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1215 OIC_LOG(ERROR, TAG, "memory allocation failed");
1219 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1222 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1227 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1235 CAResult_t res = CACloneInfo(info, &errorInfo->info);
1236 if (CA_STATUS_OK != res)
1243 errorInfo->result = result;
1244 cadata->remoteEndpoint = ep;
1245 cadata->errorInfo = errorInfo;
1246 cadata->dataType = CA_ERROR_DATA;
1248 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1250 OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");