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 "OIC_CA_MSG_HANDLE"
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,
87 CAToken_t token, uint8_t tokenLength);
90 void CAAddDataToSendThread(CAData_t *data)
92 VERIFY_NON_NULL_VOID(data, TAG, "data");
95 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
98 void CAAddDataToReceiveThread(CAData_t *data)
100 VERIFY_NON_NULL_VOID(data, TAG, "data");
103 CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
107 static bool CAIsSelectedNetworkAvailable()
109 u_arraylist_t *list = CAGetSelectedNetworkList();
110 if (!list || u_arraylist_length(list) == 0)
112 OIC_LOG(ERROR, TAG, "No selected network");
119 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
120 const CARemoteId_t *identity,
121 const void *data, CADataType_t dataType)
123 OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN");
124 CAInfo_t *info = NULL;
125 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
128 OIC_LOG(ERROR, TAG, "memory allocation failed");
132 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
135 OIC_LOG(ERROR, TAG, "endpoint clone failed");
140 OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
143 if(CA_RESPONSE_DATA == dataType)
145 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
148 OIC_LOG(ERROR, TAG, "memory allocation failed");
154 result = CAGetResponseInfoFromPDU(data, resInfo, endpoint);
155 if (CA_STATUS_OK != result)
157 OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed");
159 CADestroyResponseInfoInternal(resInfo);
163 cadata->responseInfo = resInfo;
164 info = &resInfo->info;
167 info->identity = *identity;
169 OIC_LOG(DEBUG, TAG, "Response Info :");
170 CALogPayloadInfo(info);
172 else if (CA_REQUEST_DATA == dataType)
174 CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t));
177 OIC_LOG(ERROR, TAG, "memory allocation failed");
183 result = CAGetRequestInfoFromPDU(data, endpoint, reqInfo);
184 if (CA_STATUS_OK != result)
186 OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed");
188 CADestroyRequestInfoInternal(reqInfo);
193 if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId,
194 reqInfo->info.token, reqInfo->info.tokenLength))
196 OIC_LOG(ERROR, TAG, "Second Request with same Token, Drop it");
198 CADestroyRequestInfoInternal(reqInfo);
203 cadata->requestInfo = reqInfo;
204 info = &reqInfo->info;
207 info->identity = *identity;
209 OIC_LOG(DEBUG, TAG, "Request Info :");
210 CALogPayloadInfo(info);
212 else if (CA_ERROR_DATA == dataType)
214 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
217 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
223 CAResult_t result = CAGetErrorInfoFromPDU(data, endpoint, errorInfo);
224 if (CA_STATUS_OK != result)
226 OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed");
233 cadata->errorInfo = errorInfo;
234 info = &errorInfo->info;
237 info->identity = *identity;
239 OIC_LOG(DEBUG, TAG, "error Info :");
240 CALogPayloadInfo(info);
243 cadata->remoteEndpoint = ep;
244 cadata->dataType = dataType;
246 OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
250 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
252 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
253 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
255 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
258 OIC_LOG(ERROR, TAG, "clone failed");
262 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
266 OIC_LOG(ERROR, TAG, "calloc failed");
271 resInfo->result = CA_RETRANSMIT_TIMEOUT;
272 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
273 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
275 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info),
277 if (CA_STATUS_OK != res)
279 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
280 CADestroyResponseInfoInternal(resInfo);
285 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
288 OIC_LOG(ERROR, TAG, "memory allocation failed !");
290 CADestroyResponseInfoInternal(resInfo);
294 cadata->type = SEND_TYPE_UNICAST;
295 cadata->remoteEndpoint = ep;
296 cadata->requestInfo = NULL;
297 cadata->responseInfo = resInfo;
300 CAProcessReceivedData(cadata);
302 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
306 static void CADestroyData(void *data, uint32_t size)
308 OIC_LOG(DEBUG, TAG, "CADestroyData IN");
309 if ((size_t)size < sizeof(CAData_t))
311 OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
313 CAData_t *cadata = (CAData_t *) data;
317 OIC_LOG(ERROR, TAG, "cadata is NULL");
321 if (NULL != cadata->remoteEndpoint)
323 CAFreeEndpoint(cadata->remoteEndpoint);
326 if (NULL != cadata->requestInfo)
328 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
331 if (NULL != cadata->responseInfo)
333 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
336 if (NULL != cadata->errorInfo)
338 CADestroyErrorInfoInternal(cadata->errorInfo);
342 OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
346 static void CAProcessReceivedData(CAData_t *data)
348 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
351 OIC_LOG(ERROR, TAG, "thread data error!!");
355 // parse the data and call the callbacks.
358 CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
361 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
365 if (data->requestInfo && g_requestHandler)
367 g_requestHandler(rep, data->requestInfo);
369 else if (data->responseInfo && g_responseHandler)
371 g_responseHandler(rep, data->responseInfo);
373 else if (data->errorInfo && g_errorHandler)
375 g_errorHandler(rep, data->errorInfo);
378 CADestroyData(data, sizeof(CAData_t));
380 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
384 #ifndef SINGLE_THREAD
385 static void CAReceiveThreadProcess(void *threadData)
387 #ifndef SINGLE_HANDLE
388 CAData_t *data = (CAData_t *) threadData;
389 CAProcessReceivedData(data);
394 #endif // SINGLE_THREAD
396 static CAResult_t CAProcessSendData(const CAData_t *data)
398 VERIFY_NON_NULL(data, TAG, "data");
399 VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
401 CAResult_t res = CA_STATUS_FAILED;
403 CASendDataType_t type = data->type;
405 coap_pdu_t *pdu = NULL;
406 CAInfo_t *info = NULL;
407 coap_list_t *options = NULL;
408 coap_transport_type transport;
410 if (SEND_TYPE_UNICAST == type)
412 OIC_LOG(DEBUG,TAG,"Unicast message");
413 #ifdef ROUTING_GATEWAY
415 * When forwarding a packet, do not attempt retransmission as its the responsibility of
416 * packet originator node
418 bool skipRetransmission = false;
421 if (NULL != data->requestInfo)
423 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
425 info = &data->requestInfo->info;
426 #ifdef ROUTING_GATEWAY
427 skipRetransmission = data->requestInfo->info.skipRetransmission;
429 pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint,
430 &options, &transport);
432 else if (NULL != data->responseInfo)
434 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
436 info = &data->responseInfo->info;
437 #ifdef ROUTING_GATEWAY
438 skipRetransmission = data->responseInfo->info.skipRetransmission;
440 pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
441 &options, &transport);
445 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
446 return CA_STATUS_INVALID_PARAM;
449 // interface controller function call.
453 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
455 && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
459 // Blockwise transfer
462 CAResult_t res = CAAddBlockOption(&pdu, info,
463 data->remoteEndpoint,
465 if (CA_STATUS_OK != res)
467 OIC_LOG(INFO, TAG, "to write block option has failed");
468 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
469 coap_delete_list(options);
470 coap_delete_pdu(pdu);
476 CALogPDUInfo(pdu, data->remoteEndpoint);
478 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
479 if (CA_STATUS_OK != res)
481 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
482 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
483 coap_delete_list(options);
484 coap_delete_pdu(pdu);
489 if (CA_ADAPTER_TCP == data->remoteEndpoint->adapter)
491 OIC_LOG(INFO, TAG, "retransmission will be not worked");
495 #ifdef ROUTING_GATEWAY
496 if(!skipRetransmission)
499 // for retransmission
500 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint,
501 pdu->hdr, pdu->length);
502 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
504 //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
505 OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
506 coap_delete_list(options);
507 coap_delete_pdu(pdu);
512 coap_delete_list(options);
513 coap_delete_pdu(pdu);
517 OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
518 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
519 return CA_SEND_FAILED;
522 else if (SEND_TYPE_MULTICAST == type)
524 OIC_LOG(DEBUG,TAG,"Multicast message");
525 if (NULL != data->requestInfo)
527 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
529 info = &data->requestInfo->info;
530 pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);
535 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
537 && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
541 // Blockwise transfer
542 CAResult_t res = CAAddBlockOption(&pdu, info,
543 data->remoteEndpoint,
545 if (CA_STATUS_OK != res)
547 OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
548 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
549 coap_delete_list(options);
550 coap_delete_pdu(pdu);
558 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
559 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
560 return CA_SEND_FAILED;
563 else if (NULL != data->responseInfo)
565 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
567 info = &data->responseInfo->info;
568 pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
569 &options, &transport);
574 if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
576 && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
580 // Blockwise transfer
583 CAResult_t res = CAAddBlockOption(&pdu, info,
584 data->remoteEndpoint,
586 if (CA_STATUS_OK != res)
588 OIC_LOG(INFO, TAG, "to write block option has failed");
589 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
590 coap_delete_list(options);
591 coap_delete_pdu(pdu);
600 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
601 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
602 return CA_SEND_FAILED;
607 OIC_LOG(ERROR, TAG, "request or response info is empty");
608 return CA_SEND_FAILED;
611 CALogPDUInfo(pdu, data->remoteEndpoint);
613 OIC_LOG(DEBUG, TAG, "pdu to send :");
614 OIC_LOG_BUFFER(DEBUG, TAG, (uint8_t*)pdu->hdr, pdu->length);
616 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
617 if (CA_STATUS_OK != res)
619 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
620 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
621 coap_delete_list(options);
622 coap_delete_pdu(pdu);
626 coap_delete_list(options);
627 coap_delete_pdu(pdu);
633 #ifndef SINGLE_THREAD
634 static void CASendThreadProcess(void *threadData)
636 CAData_t *data = (CAData_t *) threadData;
637 CAProcessSendData(data);
642 * If a second message arrives with the same message ID, token and the other address
643 * family, drop it. Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
645 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id,
646 CAToken_t token, uint8_t tokenLength)
652 if (ep->adapter != CA_ADAPTER_IP)
656 if (!caglobals.ip.dualstack)
661 if (tokenLength > CA_MAX_TOKEN_LEN)
664 * If token length is more than CA_MAX_TOKEN_LEN,
665 * we compare the first CA_MAX_TOKEN_LEN bytes only.
667 tokenLength = CA_MAX_TOKEN_LEN;
671 CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
673 for (size_t i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
675 CAHistoryItem_t *item = &(history->items[i]);
676 if (id == item->messageId && tokenLength == item->tokenLength
677 && memcmp(item->token, token, tokenLength) == 0)
679 if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
681 OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
682 familyFlags & CA_IPV6 ? '6' : '4');
689 history->items[history->nextIndex].flags = familyFlags;
690 history->items[history->nextIndex].messageId = id;
691 if (token && tokenLength)
693 memcpy(history->items[history->nextIndex].token, token, tokenLength);
694 history->items[history->nextIndex].tokenLength = tokenLength;
697 if (++history->nextIndex >= HISTORYSIZE)
699 history->nextIndex = 0;
705 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
706 const void *data, uint32_t dataLen)
708 VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
709 VERIFY_NON_NULL_VOID(data, TAG, "data");
711 OIC_LOG(DEBUG, TAG, "received pdu data :");
712 OIC_LOG_BUFFER(DEBUG, TAG, data, dataLen);
714 uint32_t code = CA_NOT_FOUND;
715 CAData_t *cadata = NULL;
717 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code,
721 OIC_LOG(ERROR, TAG, "Parse PDU failed");
725 OIC_LOG_V(DEBUG, TAG, "code = %d", code);
726 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
728 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
731 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
732 coap_delete_pdu(pdu);
738 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
741 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
742 coap_delete_pdu(pdu);
747 if (CA_ADAPTER_TCP == sep->endpoint.adapter)
749 OIC_LOG(INFO, TAG, "retransmission is not supported");
754 // for retransmission
755 void *retransmissionPdu = NULL;
756 CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
757 pdu->length, &retransmissionPdu);
759 // get token from saved data in retransmission list
760 if (retransmissionPdu && CA_EMPTY == code)
762 if (cadata->responseInfo)
764 CAInfo_t *info = &cadata->responseInfo->info;
765 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
766 info, &(sep->endpoint));
767 if (CA_STATUS_OK != res)
769 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
770 OICFree(info->token);
771 info->tokenLength = 0;
775 OICFree(retransmissionPdu);
779 cadata->type = SEND_TYPE_UNICAST;
782 CAProcessReceivedData(cadata);
785 if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter
787 && CA_ADAPTER_TCP != sep->endpoint.adapter
791 CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
792 if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
794 OIC_LOG(ERROR, TAG, "this message does not have block option");
795 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
799 CADestroyData(cadata, sizeof(CAData_t));
805 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
807 #endif // SINGLE_THREAD
809 coap_delete_pdu(pdu);
812 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
816 OIC_LOG(DEBUG, TAG, "IN");
818 OIC_LOG(DEBUG, TAG, "OUT");
821 void CAHandleRequestResponseCallbacks()
825 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
828 // parse the data and call the callbacks.
832 ca_mutex_lock(g_receiveThread.threadMutex);
834 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
836 ca_mutex_unlock(g_receiveThread.threadMutex);
838 if (NULL == item || NULL == item->msg)
844 CAData_t *td = (CAData_t *) item->msg;
846 if (td->requestInfo && g_requestHandler)
848 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
849 g_requestHandler(td->remoteEndpoint, td->requestInfo);
851 else if (td->responseInfo && g_responseHandler)
853 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
854 g_responseHandler(td->remoteEndpoint, td->responseInfo);
856 else if (td->errorInfo && g_errorHandler)
858 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
859 g_errorHandler(td->remoteEndpoint, td->errorInfo);
862 CADestroyData(item->msg, sizeof(CAData_t));
865 #endif // SINGLE_HANDLE
866 #endif // SINGLE_THREAD
869 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
870 CADataType_t dataType)
872 OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
874 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
877 OIC_LOG(ERROR, TAG, "memory allocation failed");
881 if(CA_REQUEST_DATA == dataType)
883 // clone request info
884 CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
888 OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
893 cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
894 cadata->requestInfo = request;
896 else if(CA_RESPONSE_DATA == dataType)
898 // clone response info
899 CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
903 OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
908 cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
909 cadata->responseInfo = response;
913 OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
918 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
921 OIC_LOG(ERROR, TAG, "endpoint clone failed");
922 CADestroyData(cadata, sizeof(CAData_t));
926 cadata->remoteEndpoint = ep;
927 cadata->dataType = dataType;
931 CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg,
932 CADataType_t dataType)
934 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
935 VERIFY_NON_NULL(sendMsg, TAG, "sendMsg");
937 if (false == CAIsSelectedNetworkAvailable())
939 return CA_STATUS_FAILED;
943 // If max retransmission queue is reached, then don't handle new request
944 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
946 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
947 return CA_SEND_FAILED;
951 CAData_t *data = CAPrepareSendData(endpoint, sendMsg, dataType);
954 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
955 return CA_MEMORY_ALLOC_FAILED;
959 CAResult_t result = CAProcessSendData(data);
960 if(CA_STATUS_OK != result)
962 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
963 CADestroyData(data, sizeof(CAData_t));
967 CADestroyData(data, sizeof(CAData_t));
970 if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
972 && CA_ADAPTER_TCP != endpoint->adapter
977 CAResult_t res = CASendBlockWiseData(data);
978 if(CA_NOT_SUPPORTED == res)
980 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
981 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
986 CADestroyData(data, sizeof(CAData_t));
993 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
995 #endif // SINGLE_THREAD
1000 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
1001 uint8_t tokenLength, const CAHeaderOption_t *options,
1009 return CA_NOT_SUPPORTED;
1012 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1013 CAErrorCallback errorHandler)
1015 g_requestHandler = ReqHandler;
1016 g_responseHandler = RespHandler;
1017 g_errorHandler = errorHandler;
1020 CAResult_t CAInitializeMessageHandler()
1022 CASetPacketReceivedCallback(CAReceivedPacketCallback);
1024 CASetNetworkChangeCallback(CANetworkChangedCallback);
1025 CASetErrorHandleCallback(CAErrorHandler);
1027 #ifndef SINGLE_THREAD
1028 // create thread pool
1029 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1030 if (CA_STATUS_OK != res)
1032 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1036 // send thread initialize
1037 res = CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1038 CASendThreadProcess, CADestroyData);
1039 if (CA_STATUS_OK != res)
1041 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1042 ca_thread_pool_free(g_threadPoolHandle);
1043 g_threadPoolHandle = NULL;
1047 // start send thread
1048 res = CAQueueingThreadStart(&g_sendThread);
1049 if (CA_STATUS_OK != res)
1051 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1052 ca_thread_pool_free(g_threadPoolHandle);
1053 g_threadPoolHandle = NULL;
1054 CAQueueingThreadDestroy(&g_sendThread);
1058 // receive thread initialize
1059 res = CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1060 CAReceiveThreadProcess, CADestroyData);
1061 if (CA_STATUS_OK != res)
1063 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1064 ca_thread_pool_free(g_threadPoolHandle);
1065 g_threadPoolHandle = NULL;
1066 CAQueueingThreadDestroy(&g_sendThread);
1070 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1071 // start receive thread
1072 res = CAQueueingThreadStart(&g_receiveThread);
1073 if (CA_STATUS_OK != res)
1075 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1076 ca_thread_pool_free(g_threadPoolHandle);
1077 g_threadPoolHandle = NULL;
1078 CAQueueingThreadDestroy(&g_sendThread);
1079 CAQueueingThreadDestroy(&g_receiveThread);
1082 #endif // SINGLE_HANDLE
1084 // retransmission initialize
1085 res = CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle,
1086 CASendUnicastData, CATimeoutCallback, NULL);
1087 if (CA_STATUS_OK != res)
1089 OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1090 ca_thread_pool_free(g_threadPoolHandle);
1091 g_threadPoolHandle = NULL;
1092 CAQueueingThreadDestroy(&g_sendThread);
1093 CAQueueingThreadDestroy(&g_receiveThread);
1098 // block-wise transfer initialize
1099 res = CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1100 if (CA_STATUS_OK != res)
1102 OIC_LOG(ERROR, TAG, "Failed to Initialize BlockWiseTransfer.");
1103 ca_thread_pool_free(g_threadPoolHandle);
1104 g_threadPoolHandle = NULL;
1105 CAQueueingThreadDestroy(&g_sendThread);
1106 CAQueueingThreadDestroy(&g_receiveThread);
1107 CARetransmissionDestroy(&g_retransmissionContext);
1112 // start retransmission
1113 res = CARetransmissionStart(&g_retransmissionContext);
1114 if (CA_STATUS_OK != res)
1116 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1117 ca_thread_pool_free(g_threadPoolHandle);
1118 g_threadPoolHandle = NULL;
1119 CAQueueingThreadDestroy(&g_sendThread);
1120 CAQueueingThreadDestroy(&g_receiveThread);
1121 CARetransmissionDestroy(&g_retransmissionContext);
1125 // initialize interface adapters by controller
1126 CAInitializeAdapters(g_threadPoolHandle);
1128 // retransmission initialize
1129 CAResult_t res = CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1130 CATimeoutCallback, NULL);
1131 if (CA_STATUS_OK != res)
1133 OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1137 CAInitializeAdapters();
1138 #endif // SINGLE_THREAD
1140 return CA_STATUS_OK;
1143 void CATerminateMessageHandler()
1145 #ifndef SINGLE_THREAD
1146 CATransportAdapter_t connType;
1147 u_arraylist_t *list = CAGetSelectedNetworkList();
1148 uint32_t length = u_arraylist_length(list);
1151 for (i = 0; i < length; i++)
1153 void* ptrType = u_arraylist_get(list, i);
1155 if (NULL == ptrType)
1160 connType = *(CATransportAdapter_t *)ptrType;
1161 CAStopAdapter(connType);
1164 // stop retransmission
1165 if (NULL != g_retransmissionContext.threadMutex)
1167 CARetransmissionStop(&g_retransmissionContext);
1171 // delete thread data
1172 if (NULL != g_sendThread.threadMutex)
1174 CAQueueingThreadStop(&g_sendThread);
1178 // delete thread data
1179 if (NULL != g_receiveThread.threadMutex)
1181 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1182 CAQueueingThreadStop(&g_receiveThread);
1186 // destroy thread pool
1187 if (NULL != g_threadPoolHandle)
1189 ca_thread_pool_free(g_threadPoolHandle);
1190 g_threadPoolHandle = NULL;
1194 CATerminateBlockWiseTransfer();
1196 CARetransmissionDestroy(&g_retransmissionContext);
1197 CAQueueingThreadDestroy(&g_sendThread);
1198 CAQueueingThreadDestroy(&g_receiveThread);
1200 // terminate interface adapters by controller
1201 CATerminateAdapters();
1203 // terminate interface adapters by controller
1204 CATerminateAdapters();
1206 // stop retransmission
1207 CARetransmissionStop(&g_retransmissionContext);
1208 CARetransmissionDestroy(&g_retransmissionContext);
1209 #endif // SINGLE_THREAD
1212 void CALogPDUInfo(coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
1214 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1215 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
1217 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1220 if (CA_ADAPTER_TCP == endpoint->adapter)
1222 OIC_LOG(DEBUG, TAG, "pdu header data :");
1223 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) pdu->hdr, pdu->length);
1230 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->coap_hdr_udp_t.type);
1232 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->coap_hdr_udp_t.code);
1234 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1236 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->coap_hdr_udp_t.token,
1237 pdu->hdr->coap_hdr_udp_t.token_length);
1241 static void CALogPayloadInfo(CAInfo_t *info)
1247 for (uint32_t i = 0; i < info->numOptions; i++)
1249 OIC_LOG_V(DEBUG, TAG, "optionID: %u", info->options[i].optionID);
1251 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1257 OIC_LOG_V(DEBUG, TAG, "payload: %p(%zu)", info->payload,
1263 OIC_LOG(DEBUG, TAG, "token:");
1264 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1267 OIC_LOG_V(DEBUG, TAG, "msgID: %u", info->messageId);
1271 OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1275 void CAErrorHandler(const CAEndpoint_t *endpoint,
1276 const void *data, uint32_t dataLen,
1279 OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1281 #ifndef SINGLE_THREAD
1282 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1283 VERIFY_NON_NULL_VOID(data, TAG, "data");
1285 uint32_t code = CA_NOT_FOUND;
1286 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1288 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
1291 OIC_LOG(ERROR, TAG, "Parse PDU failed");
1295 CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1298 OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1299 coap_delete_pdu(pdu);
1303 cadata->errorInfo->result = result;
1305 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1306 coap_delete_pdu(pdu);
1309 OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1313 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1315 OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1316 #ifndef SINGLE_THREAD
1317 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1320 OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1324 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1327 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1332 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1335 OIC_LOG(ERROR, TAG, "errorInfo memory allocation failed");
1341 CAResult_t res = CACloneInfo(info, &errorInfo->info);
1342 if (CA_STATUS_OK != res)
1344 OIC_LOG(ERROR, TAG, "info clone failed");
1351 errorInfo->result = result;
1352 cadata->remoteEndpoint = ep;
1353 cadata->errorInfo = errorInfo;
1354 cadata->dataType = CA_ERROR_DATA;
1356 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1358 OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");