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");
283 info.options = data->options;
284 info.numOptions = data->numOptions;
285 info.token = data->requestInfo->info.token;
286 info.tokenLength = data->requestInfo->info.tokenLength;
287 info.type = data->requestInfo->info.type;
288 info.messageId = data->requestInfo->info.messageId;
289 info.payload = data->requestInfo->info.payload;
291 coap_pdu_t *pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri, CA_GET,
298 res = CASendMulticastData(pdu->hdr, pdu->length);
299 if(CA_STATUS_OK != res)
301 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
302 coap_delete_pdu(pdu);
306 coap_delete_pdu(pdu);
310 OIC_LOG(DEBUG, TAG, "OUT");
313 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
315 OIC_LOG(DEBUG, TAG, "IN");
316 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
317 VERIFY_NON_NULL_VOID(data, TAG, "data");
319 uint32_t code = CA_NOT_FOUND;
320 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
325 OIC_LOG(ERROR, TAG, "Parse PDU failed");
326 CAAdapterFreeRemoteEndpoint(endpoint);
330 char uri[CA_MAX_URI_LENGTH] = { 0, };
331 uint32_t bufLen = sizeof(uri);
333 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
335 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
338 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
339 coap_delete_pdu(pdu);
340 CAAdapterFreeRemoteEndpoint(endpoint);
344 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo, uri, bufLen);
345 if (CA_STATUS_OK != res)
347 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
349 coap_delete_pdu(pdu);
350 CAAdapterFreeRemoteEndpoint(endpoint);
354 if (NULL != ReqInfo->info.options)
357 for (i = 0; i < ReqInfo->info.numOptions; i++)
359 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
361 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
365 if (NULL != ReqInfo->info.payload)
367 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
369 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
370 if (NULL != ReqInfo->info.token)
372 OIC_LOG(DEBUG, TAG, "Request- token:");
373 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
374 ReqInfo->info.tokenLength);
377 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
378 OIC_LOG(DEBUG, TAG, "Request- token");
379 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
380 OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
381 if (NULL != endpoint)
383 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
384 if (NULL == endpoint->resourceUri)
386 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
388 coap_delete_pdu(pdu);
389 CAAdapterFreeRemoteEndpoint(endpoint);
392 memcpy(endpoint->resourceUri, uri, bufLen);
393 endpoint->resourceUri[bufLen] = '\0';
394 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
396 // store the data at queue.
397 CAData_t *cadata = NULL;
398 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
401 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
403 coap_delete_pdu(pdu);
404 CAAdapterFreeRemoteEndpoint(endpoint);
408 cadata->type = SEND_TYPE_UNICAST;
409 cadata->remoteEndpoint = endpoint;
410 cadata->requestInfo = ReqInfo;
411 cadata->responseInfo = NULL;
412 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
416 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
419 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
420 coap_delete_pdu(pdu);
421 CAAdapterFreeRemoteEndpoint(endpoint);
425 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo, uri, bufLen);
426 if (CA_STATUS_OK != res)
428 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
430 coap_delete_pdu(pdu);
431 CAAdapterFreeRemoteEndpoint(endpoint);
435 if (NULL != ResInfo->info.options)
438 for (i = 0; i < ResInfo->info.numOptions; i++)
440 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
442 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
446 if (NULL != ResInfo->info.payload)
448 OIC_LOG_V(DEBUG, TAG, "Response- payload: %s", ResInfo->info.payload);
450 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
451 OIC_LOG_V(DEBUG, TAG, "Response- token : %s", ResInfo->info.token);
452 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
454 if (NULL != endpoint)
456 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
457 if (NULL == endpoint->resourceUri)
459 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
461 coap_delete_pdu(pdu);
462 CAAdapterFreeRemoteEndpoint(endpoint);
465 memcpy(endpoint->resourceUri, uri, bufLen);
466 endpoint->resourceUri[bufLen] = '\0';
467 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
470 // store the data at queue.
471 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
474 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
476 coap_delete_pdu(pdu);
477 CAAdapterFreeRemoteEndpoint(endpoint);
481 cadata->type = SEND_TYPE_UNICAST;
482 cadata->remoteEndpoint = endpoint;
483 cadata->requestInfo = NULL;
485 // for retransmission
486 void *retransmissionPdu = NULL;
487 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
490 // get token from saved data in retransmission list
491 if (retransmissionPdu && CA_EMPTY == code)
493 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
495 if (CA_STATUS_OK != res)
497 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
498 OICFree(ResInfo->info.token);
501 OICFree(retransmissionPdu);
502 cadata->responseInfo = ResInfo;
504 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
509 coap_delete_pdu(pdu);
511 OIC_LOG(DEBUG, TAG, "OUT");
514 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
516 OIC_LOG(DEBUG, TAG, "IN");
518 OIC_LOG(DEBUG, TAG, "OUT");
521 void CAHandleRequestResponseCallbacks()
523 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks IN");
526 // parse the data and call the callbacks.
530 ca_mutex_lock(g_receiveThread.threadMutex);
532 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
534 ca_mutex_unlock(g_receiveThread.threadMutex);
542 void *msg = item->msg;
550 CAData_t *td = (CAData_t *) msg;
551 CARemoteEndpoint_t *rep = td->remoteEndpoint;
558 if (NULL != td->requestInfo)
560 if (g_requestHandler)
562 OIC_LOG_V(DEBUG, TAG, "callback will be sent : %d", td->requestInfo->info.numOptions);
563 g_requestHandler(rep, td->requestInfo);
567 if (NULL != td->responseInfo)
569 if (g_responseHandler)
571 g_responseHandler(rep, td->responseInfo);
575 CADataDestroyer(msg, sizeof(CAData_t));
578 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
581 CAResult_t CADetachRequestMessage(const CARemoteEndpoint_t *object, const CARequestInfo_t *request)
583 OIC_LOG(DEBUG, TAG, "IN");
585 VERIFY_NON_NULL(object, TAG, "object");
586 VERIFY_NON_NULL(request, TAG, "request");
588 if (false == CAIsSelectedNetworkAvailable())
590 return CA_STATUS_FAILED;
593 CARemoteEndpoint_t *remoteEndpoint = NULL;
594 CARequestInfo_t *requestInfo = NULL;
595 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
596 CA_MEMORY_ALLOC_CHECK(data);
598 // clone remote endpoint
599 remoteEndpoint = CACloneRemoteEndpoint(object);
600 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
602 // clone request info
603 requestInfo = CACloneRequestInfo(request);
604 CA_MEMORY_ALLOC_CHECK(requestInfo);
607 data->type = SEND_TYPE_UNICAST;
608 data->remoteEndpoint = remoteEndpoint;
609 data->requestInfo = requestInfo;
610 data->responseInfo = NULL;
613 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
614 OIC_LOG(DEBUG, TAG, "OUT");
617 // memory error label.
619 CADestroyRemoteEndpointInternal(remoteEndpoint);
620 CADestroyRequestInfoInternal(requestInfo);
623 OIC_LOG(DEBUG, TAG, "OUT");
624 return CA_MEMORY_ALLOC_FAILED;
627 CAResult_t CADetachRequestToAllMessage(const CAGroupEndpoint_t *object,
628 const CARequestInfo_t *request)
630 OIC_LOG(DEBUG, TAG, "IN");
632 if (NULL == object || NULL == request || NULL == object->resourceUri)
634 return CA_STATUS_INVALID_PARAM;
637 if ((request->method < CA_GET) || (request->method > CA_DELETE))
639 OIC_LOG(ERROR, TAG, "Invalid method type!");
641 return CA_STATUS_INVALID_PARAM;
644 if (false == CAIsSelectedNetworkAvailable())
646 return CA_STATUS_FAILED;
649 CARemoteEndpoint_t *remoteEndpoint = NULL;
650 CARequestInfo_t *requestInfo = NULL;
652 // allocate & initialize
653 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
654 CA_MEMORY_ALLOC_CHECK(data);
656 CAAddress_t addr = {};
657 remoteEndpoint = CACreateRemoteEndpointInternal(object->resourceUri, addr,
658 object->transportType);
660 // clone request info
661 requestInfo = CACloneRequestInfo(request);
662 CA_MEMORY_ALLOC_CHECK(requestInfo);
665 data->type = SEND_TYPE_MULTICAST;
666 data->remoteEndpoint = remoteEndpoint;
667 data->requestInfo = requestInfo;
668 data->responseInfo = NULL;
671 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
673 OIC_LOG(DEBUG, TAG, "OUT");
676 // memory error label.
679 CADestroyRequestInfoInternal(requestInfo);
680 CADestroyRemoteEndpointInternal(remoteEndpoint);
682 OIC_LOG(DEBUG, TAG, "OUT");
683 return CA_MEMORY_ALLOC_FAILED;
686 CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object,
687 const CAResponseInfo_t *response)
689 OIC_LOG(DEBUG, TAG, "IN");
690 VERIFY_NON_NULL(object, TAG, "object");
691 VERIFY_NON_NULL(response, TAG, "response");
693 if (false == CAIsSelectedNetworkAvailable())
695 return CA_STATUS_FAILED;
698 CARemoteEndpoint_t *remoteEndpoint = NULL;
699 CAResponseInfo_t *responseInfo = NULL;
701 // allocate & initialize
702 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
703 CA_MEMORY_ALLOC_CHECK(data);
705 // clone remote endpoint
706 remoteEndpoint = CACloneRemoteEndpoint(object);
707 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
709 // clone response info
710 responseInfo = CACloneResponseInfo(response);
711 CA_MEMORY_ALLOC_CHECK(responseInfo);
714 data->type = SEND_TYPE_UNICAST;
715 data->remoteEndpoint = remoteEndpoint;
716 data->requestInfo = NULL;
717 data->responseInfo = responseInfo;
720 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
722 OIC_LOG(DEBUG, TAG, "OUT");
725 // memory error label.
727 CADestroyRemoteEndpointInternal(remoteEndpoint);
728 CADestroyResponseInfoInternal(responseInfo);
730 OIC_LOG(DEBUG, TAG, "OUT");
732 return CA_MEMORY_ALLOC_FAILED;
735 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
736 uint8_t tokenLength, const CAHeaderOption_t *options,
739 OIC_LOG(DEBUG, TAG, "IN");
740 VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
741 VERIFY_NON_NULL(token, TAG, "Token is NULL");
743 if (false == CAIsSelectedNetworkAvailable())
745 return CA_STATUS_FAILED;
748 CARemoteEndpoint_t *remoteEndpoint = NULL;
749 CARequestInfo_t *reqInfo = NULL;
750 char *tempToken = NULL;
752 // allocate & initialize
753 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
754 CA_MEMORY_ALLOC_CHECK(data);
756 CAAddress_t addr = {};
757 remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr,
758 CA_IPV4 | CA_EDR | CA_LE);
760 // create request info
761 reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
762 CA_MEMORY_ALLOC_CHECK(reqInfo);
767 tempToken = (char *) OICMalloc(tokenLength);
768 CA_MEMORY_ALLOC_CHECK(tempToken);
769 memcpy(tempToken, token, tokenLength);
772 // save request info data
773 reqInfo->method = CA_GET;
774 reqInfo->info.type = CA_MSG_NONCONFIRM;
776 reqInfo->info.token = tempToken;
777 reqInfo->info.tokenLength = tokenLength;
780 data->type = SEND_TYPE_MULTICAST;
781 data->remoteEndpoint = remoteEndpoint;
782 data->requestInfo = reqInfo;
784 data->responseInfo = NULL;
785 data->options = NULL;
786 data->numOptions = 0;
787 if (NULL != options && 0 < numOptions)
790 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
792 CA_MEMORY_ALLOC_CHECK(headerOption);
794 memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
796 data->options = headerOption;
797 data->numOptions = numOptions;
801 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
803 OIC_LOG(DEBUG, TAG, "OUT");
806 // memory error label.
809 CADestroyRemoteEndpointInternal(remoteEndpoint);
814 OIC_LOG(DEBUG, TAG, "OUT");
815 return CA_MEMORY_ALLOC_FAILED;
818 void CASetRequestResponseCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
820 OIC_LOG(DEBUG, TAG, "IN");
821 g_requestHandler = ReqHandler;
822 g_responseHandler = RespHandler;
823 OIC_LOG(DEBUG, TAG, "OUT");
826 CAResult_t CAInitializeMessageHandler()
828 OIC_LOG(DEBUG, TAG, "IN");
829 CASetPacketReceivedCallback(CAReceivedPacketCallback);
831 CASetNetworkChangeCallback(CANetworkChangedCallback);
833 // create thread pool
834 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
836 if (res != CA_STATUS_OK)
838 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
842 // send thread initialize
843 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
844 CASendThreadProcess, CADataDestroyer))
846 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
847 return CA_STATUS_FAILED;
851 res = CAQueueingThreadStart(&g_sendThread);
853 if (res != CA_STATUS_OK)
855 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
856 ca_thread_pool_free(g_threadPoolHandle);
857 g_threadPoolHandle = NULL;
861 // receive thread initialize
862 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
863 CAReceiveThreadProcess, CADataDestroyer))
865 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
866 return CA_STATUS_FAILED;
869 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
870 // start receive thread
871 res = CAQueueingThreadStart(&gReceiveThread);
873 if (res != CA_STATUS_OK)
875 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
880 // retransmission initialize
881 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
882 CATimeoutCallback, NULL);
884 // start retransmission
885 res = CARetransmissionStart(&g_retransmissionContext);
887 if (res != CA_STATUS_OK)
889 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
893 // initialize interface adapters by controller
894 CAInitializeAdapters(g_threadPoolHandle);
895 OIC_LOG(DEBUG, TAG, "OUT");
899 void CATerminateMessageHandler()
901 OIC_LOG(DEBUG, TAG, "IN");
902 CATransportType_t connType;
903 u_arraylist_t *list = CAGetSelectedNetworkList();
904 uint32_t length = u_arraylist_length(list);
907 for (i = 0; i < length; i++)
909 void* ptrType = u_arraylist_get(list, i);
916 connType = *(CATransportType_t *) ptrType;
917 CAStopAdapter(connType);
920 // stop retransmission
921 if (NULL != g_retransmissionContext.threadMutex)
923 CARetransmissionStop(&g_retransmissionContext);
927 // delete thread data
928 if (NULL != g_sendThread.threadMutex)
930 CAQueueingThreadStop(&g_sendThread);
934 // delete thread data
935 if (NULL != g_receiveThread.threadMutex)
937 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
938 CAQueueingThreadStop(&gReceiveThread);
942 // destroy thread pool
943 if (NULL != g_threadPoolHandle)
945 ca_thread_pool_free(g_threadPoolHandle);
946 g_threadPoolHandle = NULL;
949 CARetransmissionDestroy(&g_retransmissionContext);
950 CAQueueingThreadDestroy(&g_sendThread);
951 CAQueueingThreadDestroy(&g_receiveThread);
953 // terminate interface adapters by controller
954 CATerminateAdapters();
956 OIC_LOG(DEBUG, TAG, "OUT");
959 void CALogPDUInfo(coap_pdu_t *pdu)
961 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
963 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
965 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
967 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
969 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
971 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
973 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);