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 "cainterfacecontroller.h"
30 #include "caprotocolmessage.h"
31 #include "caretransmission.h"
32 #include "caadapterutils.h"
35 #include "config.h" /* for coap protocol */
36 #include "cathreadpool.h" /* for thread pool */
37 #include "caqueueingthread.h"
39 #include "oic_malloc.h"
40 #include "canetworkconfigurator.h"
45 #define MAX_THREAD_POOL_SIZE 20
49 SEND_TYPE_MULTICAST = 0, SEND_TYPE_UNICAST
54 CASendDataType_t type;
55 CARemoteEndpoint_t *remoteEndpoint;
56 CARequestInfo_t *requestInfo;
57 CAResponseInfo_t *responseInfo;
58 CAHeaderOption_t *options;
63 static ca_thread_pool_t g_threadPoolHandle = NULL;
65 // message handler main thread
66 static CAQueueingThread_t g_sendThread;
67 static CAQueueingThread_t g_receiveThread;
69 static CARetransmission_t g_retransmissionContext;
72 static CARequestCallback g_requestHandler = NULL;
73 static CAResponseCallback g_responseHandler = NULL;
75 static bool CAIsSelectedNetworkAvailable()
77 u_arraylist_t *list = CAGetSelectedNetworkList();
78 if (!list || list->length == 0)
80 OIC_LOG(ERROR, TAG, "No selected network");
87 static void CATimeoutCallback(const CARemoteEndpoint_t *endpoint, const void *pdu, uint32_t size)
89 OIC_LOG(DEBUG, TAG, "IN");
90 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
91 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
93 CARemoteEndpoint_t* ep = CACloneRemoteEndpoint(endpoint);
96 OIC_LOG(ERROR, TAG, "clone failed");
100 CAResponseInfo_t* resInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
104 OIC_LOG(ERROR, TAG, "calloc failed");
105 CADestroyRemoteEndpointInternal(ep);
109 resInfo->result = CA_RETRANSMIT_TIMEOUT;
110 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
111 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
112 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info));
113 if (CA_STATUS_OK != res)
115 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
116 OICFree(resInfo->info.token);
118 CADestroyRemoteEndpointInternal(ep);
122 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
125 OIC_LOG(ERROR, TAG, "memory allocation failed !");
126 CADestroyRemoteEndpointInternal(ep);
131 cadata->type = SEND_TYPE_UNICAST;
132 cadata->remoteEndpoint = ep;
133 cadata->requestInfo = NULL;
134 cadata->responseInfo = resInfo;
136 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
137 OIC_LOG(DEBUG, TAG, "OUT");
140 static void CADataDestroyer(void *data, uint32_t size)
142 OIC_LOG(DEBUG, TAG, "IN");
143 CAData_t *cadata = (CAData_t *) data;
147 OIC_LOG(ERROR, TAG, "cadata is NULL");
151 if (NULL != cadata->remoteEndpoint)
153 CADestroyRemoteEndpointInternal((CARemoteEndpoint_t *) cadata->remoteEndpoint);
156 if (NULL != cadata->requestInfo)
158 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
161 if (NULL != cadata->responseInfo)
163 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
166 OICFree(cadata->options);
168 OIC_LOG(DEBUG, TAG, "OUT");
171 static void CAReceiveThreadProcess(void *threadData)
173 OIC_LOG(DEBUG, TAG, "IN");
174 // Currently not supported
175 // This will be enabled when RI supports multi threading
176 #ifndef SINGLE_HANDLE
177 CAData_t *data = (CAData_t *) threadData;
181 OIC_LOG(ERROR, TAG, "thread data error!!");
185 // parse the data and call the callbacks.
188 CARemoteEndpoint_t *rep = (CARemoteEndpoint_t *)(data->remoteEndpoint);
192 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
196 if (NULL != data->requestInfo)
198 if (g_requestHandler)
200 g_requestHandler(rep, data->requestInfo);
204 if (NULL != data->responseInfo)
206 if (g_responseHandler)
208 g_responseHandler(rep, data->responseInfo);
212 OIC_LOG(DEBUG, TAG, "OUT");
215 static void CASendThreadProcess(void *threadData)
217 OIC_LOG(DEBUG, TAG, "IN");
218 CAData_t *data = (CAData_t *) threadData;
220 VERIFY_NON_NULL_VOID(data, TAG, "data");
221 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");
223 CAResult_t res = CA_STATUS_FAILED;
225 CASendDataType_t type = data->type;
227 if (SEND_TYPE_UNICAST == type)
229 coap_pdu_t *pdu = NULL;
231 if (NULL != data->requestInfo)
233 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
235 pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri,
236 data->requestInfo->method,
237 data->requestInfo->info);
239 else if (NULL != data->responseInfo)
241 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
243 pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri,
244 data->responseInfo->result,
245 data->responseInfo->info);
249 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
252 // interface controller function call.
257 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
258 if (CA_STATUS_OK != res)
260 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
261 coap_delete_pdu(pdu);
264 // for retransmission
265 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
267 if (CA_STATUS_OK != res)
269 OIC_LOG_V(INFO, TAG, "retransmission will be not working: %d", res);
270 coap_delete_pdu(pdu);
274 coap_delete_pdu(pdu);
277 else if (SEND_TYPE_MULTICAST == type)
279 OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
281 CAInfo_t info = data->requestInfo->info;
283 info.options = data->options;
284 info.numOptions = data->numOptions;
286 coap_pdu_t *pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri, CA_GET,
293 res = CASendMulticastData(pdu->hdr, pdu->length);
294 if(CA_STATUS_OK != res)
296 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
297 coap_delete_pdu(pdu);
301 coap_delete_pdu(pdu);
305 OIC_LOG(DEBUG, TAG, "OUT");
308 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
310 OIC_LOG(DEBUG, TAG, "IN");
311 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
312 VERIFY_NON_NULL_VOID(data, TAG, "data");
314 uint32_t code = CA_NOT_FOUND;
315 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
320 OIC_LOG(ERROR, TAG, "Parse PDU failed");
321 CAAdapterFreeRemoteEndpoint(endpoint);
325 char uri[CA_MAX_URI_LENGTH] = { 0, };
326 uint32_t bufLen = sizeof(uri);
328 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
330 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
333 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
334 coap_delete_pdu(pdu);
335 CAAdapterFreeRemoteEndpoint(endpoint);
339 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo, uri, bufLen);
340 if (CA_STATUS_OK != res)
342 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
344 coap_delete_pdu(pdu);
345 CAAdapterFreeRemoteEndpoint(endpoint);
349 if (NULL != ReqInfo->info.options)
352 for (i = 0; i < ReqInfo->info.numOptions; i++)
354 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
356 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
360 if (NULL != ReqInfo->info.payload)
362 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
364 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
365 if (NULL != ReqInfo->info.token)
367 OIC_LOG(DEBUG, TAG, "Request- token:");
368 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
369 ReqInfo->info.tokenLength);
372 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
373 OIC_LOG(DEBUG, TAG, "Request- token");
374 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
375 OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
376 if (NULL != endpoint)
378 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
379 if (NULL == endpoint->resourceUri)
381 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
383 coap_delete_pdu(pdu);
384 CAAdapterFreeRemoteEndpoint(endpoint);
387 memcpy(endpoint->resourceUri, uri, bufLen);
388 endpoint->resourceUri[bufLen] = '\0';
389 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
391 // store the data at queue.
392 CAData_t *cadata = NULL;
393 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
396 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
398 coap_delete_pdu(pdu);
399 CAAdapterFreeRemoteEndpoint(endpoint);
403 cadata->type = SEND_TYPE_UNICAST;
404 cadata->remoteEndpoint = endpoint;
405 cadata->requestInfo = ReqInfo;
406 cadata->responseInfo = NULL;
407 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
411 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
414 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
415 coap_delete_pdu(pdu);
416 CAAdapterFreeRemoteEndpoint(endpoint);
420 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo, uri, bufLen);
421 if (CA_STATUS_OK != res)
423 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
425 coap_delete_pdu(pdu);
426 CAAdapterFreeRemoteEndpoint(endpoint);
430 if (NULL != ResInfo->info.options)
433 for (i = 0; i < ResInfo->info.numOptions; i++)
435 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
437 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
441 if (NULL != ResInfo->info.payload)
443 OIC_LOG_V(DEBUG, TAG, "Response- payload: %s", ResInfo->info.payload);
445 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
446 OIC_LOG_V(DEBUG, TAG, "Response- token : %s", ResInfo->info.token);
447 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
449 if (NULL != endpoint)
451 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
452 if (NULL == endpoint->resourceUri)
454 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
456 coap_delete_pdu(pdu);
457 CAAdapterFreeRemoteEndpoint(endpoint);
460 memcpy(endpoint->resourceUri, uri, bufLen);
461 endpoint->resourceUri[bufLen] = '\0';
462 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
465 // store the data at queue.
466 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
469 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
471 coap_delete_pdu(pdu);
472 CAAdapterFreeRemoteEndpoint(endpoint);
476 cadata->type = SEND_TYPE_UNICAST;
477 cadata->remoteEndpoint = endpoint;
478 cadata->requestInfo = NULL;
480 // for retransmission
481 void *retransmissionPdu = NULL;
482 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
485 // get token from saved data in retransmission list
486 if (retransmissionPdu && CA_EMPTY == code)
488 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
490 if (CA_STATUS_OK != res)
492 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
493 OICFree(ResInfo->info.token);
496 OICFree(retransmissionPdu);
497 cadata->responseInfo = ResInfo;
499 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
504 coap_delete_pdu(pdu);
506 OIC_LOG(DEBUG, TAG, "OUT");
509 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
511 OIC_LOG(DEBUG, TAG, "IN");
513 OIC_LOG(DEBUG, TAG, "OUT");
516 void CAHandleRequestResponseCallbacks()
518 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks IN");
521 // parse the data and call the callbacks.
525 ca_mutex_lock(g_receiveThread.threadMutex);
527 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
529 ca_mutex_unlock(g_receiveThread.threadMutex);
537 void *msg = item->msg;
545 CAData_t *td = (CAData_t *) msg;
546 CARemoteEndpoint_t *rep = td->remoteEndpoint;
553 if (NULL != td->requestInfo)
555 if (g_requestHandler)
557 OIC_LOG_V(DEBUG, TAG, "callback will be sent : %d", td->requestInfo->info.numOptions);
558 g_requestHandler(rep, td->requestInfo);
562 if (NULL != td->responseInfo)
564 if (g_responseHandler)
566 g_responseHandler(rep, td->responseInfo);
570 CADataDestroyer(msg, sizeof(CAData_t));
573 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
576 CAResult_t CADetachRequestMessage(const CARemoteEndpoint_t *object, const CARequestInfo_t *request)
578 OIC_LOG(DEBUG, TAG, "IN");
580 VERIFY_NON_NULL(object, TAG, "object");
581 VERIFY_NON_NULL(request, TAG, "request");
583 if (false == CAIsSelectedNetworkAvailable())
585 return CA_STATUS_FAILED;
588 CARemoteEndpoint_t *remoteEndpoint = NULL;
589 CARequestInfo_t *requestInfo = NULL;
590 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
591 CA_MEMORY_ALLOC_CHECK(data);
593 // clone remote endpoint
594 remoteEndpoint = CACloneRemoteEndpoint(object);
595 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
597 // clone request info
598 requestInfo = CACloneRequestInfo(request);
599 CA_MEMORY_ALLOC_CHECK(requestInfo);
602 data->type = SEND_TYPE_UNICAST;
603 data->remoteEndpoint = remoteEndpoint;
604 data->requestInfo = requestInfo;
605 data->responseInfo = NULL;
608 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
609 OIC_LOG(DEBUG, TAG, "OUT");
612 // memory error label.
614 CADestroyRemoteEndpointInternal(remoteEndpoint);
615 CADestroyRequestInfoInternal(requestInfo);
618 OIC_LOG(DEBUG, TAG, "OUT");
619 return CA_MEMORY_ALLOC_FAILED;
622 CAResult_t CADetachRequestToAllMessage(const CAGroupEndpoint_t *object,
623 const CARequestInfo_t *request)
625 OIC_LOG(DEBUG, TAG, "IN");
627 if (NULL == object || NULL == request || NULL == object->resourceUri)
629 return CA_STATUS_INVALID_PARAM;
632 if ((request->method < CA_GET) || (request->method > CA_DELETE))
634 OIC_LOG(ERROR, TAG, "Invalid method type!");
636 return CA_STATUS_INVALID_PARAM;
639 if (false == CAIsSelectedNetworkAvailable())
641 return CA_STATUS_FAILED;
644 CARemoteEndpoint_t *remoteEndpoint = NULL;
645 CARequestInfo_t *requestInfo = NULL;
647 // allocate & initialize
648 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
649 CA_MEMORY_ALLOC_CHECK(data);
651 CAAddress_t addr = {};
652 remoteEndpoint = CACreateRemoteEndpointInternal(object->resourceUri, addr,
653 object->transportType);
655 // clone request info
656 requestInfo = CACloneRequestInfo(request);
657 CA_MEMORY_ALLOC_CHECK(requestInfo);
660 data->type = SEND_TYPE_MULTICAST;
661 data->remoteEndpoint = remoteEndpoint;
662 data->requestInfo = requestInfo;
663 data->responseInfo = NULL;
666 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
668 OIC_LOG(DEBUG, TAG, "OUT");
671 // memory error label.
674 CADestroyRequestInfoInternal(requestInfo);
675 CADestroyRemoteEndpointInternal(remoteEndpoint);
677 OIC_LOG(DEBUG, TAG, "OUT");
678 return CA_MEMORY_ALLOC_FAILED;
681 CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object,
682 const CAResponseInfo_t *response)
684 OIC_LOG(DEBUG, TAG, "IN");
685 VERIFY_NON_NULL(object, TAG, "object");
686 VERIFY_NON_NULL(response, TAG, "response");
688 if (false == CAIsSelectedNetworkAvailable())
690 return CA_STATUS_FAILED;
693 CARemoteEndpoint_t *remoteEndpoint = NULL;
694 CAResponseInfo_t *responseInfo = NULL;
696 // allocate & initialize
697 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
698 CA_MEMORY_ALLOC_CHECK(data);
700 // clone remote endpoint
701 remoteEndpoint = CACloneRemoteEndpoint(object);
702 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
704 // clone response info
705 responseInfo = CACloneResponseInfo(response);
706 CA_MEMORY_ALLOC_CHECK(responseInfo);
709 data->type = SEND_TYPE_UNICAST;
710 data->remoteEndpoint = remoteEndpoint;
711 data->requestInfo = NULL;
712 data->responseInfo = responseInfo;
715 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
717 OIC_LOG(DEBUG, TAG, "OUT");
720 // memory error label.
722 CADestroyRemoteEndpointInternal(remoteEndpoint);
723 CADestroyResponseInfoInternal(responseInfo);
725 OIC_LOG(DEBUG, TAG, "OUT");
727 return CA_MEMORY_ALLOC_FAILED;
730 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
731 uint8_t tokenLength, const CAHeaderOption_t *options,
734 OIC_LOG(DEBUG, TAG, "IN");
735 VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
736 VERIFY_NON_NULL(token, TAG, "Token is NULL");
738 if (false == CAIsSelectedNetworkAvailable())
740 return CA_STATUS_FAILED;
743 CARemoteEndpoint_t *remoteEndpoint = NULL;
744 CARequestInfo_t *reqInfo = NULL;
745 char *tempToken = NULL;
747 // allocate & initialize
748 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
749 CA_MEMORY_ALLOC_CHECK(data);
751 CAAddress_t addr = {};
752 remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr,
753 CA_IPV4 | CA_EDR | CA_LE);
755 // create request info
756 reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
757 CA_MEMORY_ALLOC_CHECK(reqInfo);
762 tempToken = (char *) OICMalloc(tokenLength);
763 CA_MEMORY_ALLOC_CHECK(tempToken);
764 memcpy(tempToken, token, tokenLength);
767 // save request info data
768 reqInfo->method = CA_GET;
769 reqInfo->info.type = CA_MSG_NONCONFIRM;
771 reqInfo->info.token = tempToken;
772 reqInfo->info.tokenLength = tokenLength;
775 data->type = SEND_TYPE_MULTICAST;
776 data->remoteEndpoint = remoteEndpoint;
777 data->requestInfo = reqInfo;
779 data->responseInfo = NULL;
780 data->options = NULL;
781 data->numOptions = 0;
782 if (NULL != options && 0 < numOptions)
785 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
787 CA_MEMORY_ALLOC_CHECK(headerOption);
789 memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
791 data->options = headerOption;
792 data->numOptions = numOptions;
796 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
798 OIC_LOG(DEBUG, TAG, "OUT");
801 // memory error label.
804 CADestroyRemoteEndpointInternal(remoteEndpoint);
809 OIC_LOG(DEBUG, TAG, "OUT");
810 return CA_MEMORY_ALLOC_FAILED;
813 void CASetRequestResponseCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
815 OIC_LOG(DEBUG, TAG, "IN");
816 g_requestHandler = ReqHandler;
817 g_responseHandler = RespHandler;
818 OIC_LOG(DEBUG, TAG, "OUT");
821 CAResult_t CAInitializeMessageHandler()
823 OIC_LOG(DEBUG, TAG, "IN");
824 CASetPacketReceivedCallback(CAReceivedPacketCallback);
826 CASetNetworkChangeCallback(CANetworkChangedCallback);
828 // create thread pool
829 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
831 if (res != CA_STATUS_OK)
833 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
837 // send thread initialize
838 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
839 CASendThreadProcess, CADataDestroyer))
841 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
842 return CA_STATUS_FAILED;
846 res = CAQueueingThreadStart(&g_sendThread);
848 if (res != CA_STATUS_OK)
850 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
851 ca_thread_pool_free(g_threadPoolHandle);
852 g_threadPoolHandle = NULL;
856 // receive thread initialize
857 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
858 CAReceiveThreadProcess, CADataDestroyer))
860 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
861 return CA_STATUS_FAILED;
864 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
865 // start receive thread
866 res = CAQueueingThreadStart(&gReceiveThread);
868 if (res != CA_STATUS_OK)
870 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
875 // retransmission initialize
876 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
877 CATimeoutCallback, NULL);
879 // start retransmission
880 res = CARetransmissionStart(&g_retransmissionContext);
882 if (res != CA_STATUS_OK)
884 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
888 // initialize interface adapters by controller
889 CAInitializeAdapters(g_threadPoolHandle);
890 OIC_LOG(DEBUG, TAG, "OUT");
894 void CATerminateMessageHandler()
896 OIC_LOG(DEBUG, TAG, "IN");
897 CATransportType_t connType;
898 u_arraylist_t *list = CAGetSelectedNetworkList();
899 uint32_t length = u_arraylist_length(list);
902 for (i = 0; i < length; i++)
904 void* ptrType = u_arraylist_get(list, i);
911 connType = *(CATransportType_t *) ptrType;
912 CAStopAdapter(connType);
915 // stop retransmission
916 if (NULL != g_retransmissionContext.threadMutex)
918 CARetransmissionStop(&g_retransmissionContext);
922 // delete thread data
923 if (NULL != g_sendThread.threadMutex)
925 CAQueueingThreadStop(&g_sendThread);
929 // delete thread data
930 if (NULL != g_receiveThread.threadMutex)
932 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
933 CAQueueingThreadStop(&gReceiveThread);
937 // destroy thread pool
938 if (NULL != g_threadPoolHandle)
940 ca_thread_pool_free(g_threadPoolHandle);
941 g_threadPoolHandle = NULL;
944 CARetransmissionDestroy(&g_retransmissionContext);
945 CAQueueingThreadDestroy(&g_sendThread);
946 CAQueueingThreadDestroy(&g_receiveThread);
948 // terminate interface adapters by controller
949 CATerminateAdapters();
951 OIC_LOG(DEBUG, TAG, "OUT");
954 void CALogPDUInfo(coap_pdu_t *pdu)
956 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
958 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
960 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
962 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
964 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
966 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
968 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);