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 !");
402 if (NULL != endpoint && NULL != endpoint->resourceUri)
404 OICFree(endpoint->resourceUri);
408 coap_delete_pdu(pdu);
409 CAAdapterFreeRemoteEndpoint(endpoint);
413 cadata->type = SEND_TYPE_UNICAST;
414 cadata->remoteEndpoint = endpoint;
415 cadata->requestInfo = ReqInfo;
416 cadata->responseInfo = NULL;
417 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
421 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
424 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
425 coap_delete_pdu(pdu);
426 CAAdapterFreeRemoteEndpoint(endpoint);
430 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo, uri, bufLen);
431 if (CA_STATUS_OK != res)
433 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
435 coap_delete_pdu(pdu);
436 CAAdapterFreeRemoteEndpoint(endpoint);
440 if (NULL != ResInfo->info.options)
443 for (i = 0; i < ResInfo->info.numOptions; i++)
445 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
447 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
451 if (NULL != ResInfo->info.payload)
453 OIC_LOG_V(DEBUG, TAG, "Response- payload: %s", ResInfo->info.payload);
455 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
456 OIC_LOG_V(DEBUG, TAG, "Response- token : %s", ResInfo->info.token);
457 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
459 if (NULL != endpoint)
461 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
462 if (NULL == endpoint->resourceUri)
464 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
466 coap_delete_pdu(pdu);
467 CAAdapterFreeRemoteEndpoint(endpoint);
470 memcpy(endpoint->resourceUri, uri, bufLen);
471 endpoint->resourceUri[bufLen] = '\0';
472 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
475 // store the data at queue.
476 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
479 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
480 if (NULL != endpoint && NULL != endpoint->resourceUri)
482 OICFree(endpoint->resourceUri);
485 coap_delete_pdu(pdu);
486 CAAdapterFreeRemoteEndpoint(endpoint);
490 cadata->type = SEND_TYPE_UNICAST;
491 cadata->remoteEndpoint = endpoint;
492 cadata->requestInfo = NULL;
494 // for retransmission
495 void *retransmissionPdu = NULL;
496 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
499 // get token from saved data in retransmission list
500 if (retransmissionPdu && CA_EMPTY == code)
502 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
504 if (CA_STATUS_OK != res)
506 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
507 OICFree(ResInfo->info.token);
510 OICFree(retransmissionPdu);
511 cadata->responseInfo = ResInfo;
513 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
518 coap_delete_pdu(pdu);
520 OIC_LOG(DEBUG, TAG, "OUT");
523 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
525 OIC_LOG(DEBUG, TAG, "IN");
527 OIC_LOG(DEBUG, TAG, "OUT");
530 void CAHandleRequestResponseCallbacks()
532 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks IN");
535 // parse the data and call the callbacks.
539 ca_mutex_lock(g_receiveThread.threadMutex);
541 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
543 ca_mutex_unlock(g_receiveThread.threadMutex);
551 void *msg = item->msg;
559 CAData_t *td = (CAData_t *) msg;
560 CARemoteEndpoint_t *rep = td->remoteEndpoint;
567 if (NULL != td->requestInfo)
569 if (g_requestHandler)
571 OIC_LOG_V(DEBUG, TAG, "callback will be sent : %d", td->requestInfo->info.numOptions);
572 g_requestHandler(rep, td->requestInfo);
576 if (NULL != td->responseInfo)
578 if (g_responseHandler)
580 g_responseHandler(rep, td->responseInfo);
584 CADataDestroyer(msg, sizeof(CAData_t));
587 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
590 CAResult_t CADetachRequestMessage(const CARemoteEndpoint_t *object, const CARequestInfo_t *request)
592 OIC_LOG(DEBUG, TAG, "IN");
594 VERIFY_NON_NULL(object, TAG, "object");
595 VERIFY_NON_NULL(request, TAG, "request");
597 if (false == CAIsSelectedNetworkAvailable())
599 return CA_STATUS_FAILED;
602 CARemoteEndpoint_t *remoteEndpoint = NULL;
603 CARequestInfo_t *requestInfo = NULL;
604 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
605 CA_MEMORY_ALLOC_CHECK(data);
607 // clone remote endpoint
608 remoteEndpoint = CACloneRemoteEndpoint(object);
609 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
611 // clone request info
612 requestInfo = CACloneRequestInfo(request);
613 CA_MEMORY_ALLOC_CHECK(requestInfo);
616 data->type = SEND_TYPE_UNICAST;
617 data->remoteEndpoint = remoteEndpoint;
618 data->requestInfo = requestInfo;
619 data->responseInfo = NULL;
622 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
623 OIC_LOG(DEBUG, TAG, "OUT");
626 // memory error label.
628 CADestroyRemoteEndpointInternal(remoteEndpoint);
629 CADestroyRequestInfoInternal(requestInfo);
632 OIC_LOG(DEBUG, TAG, "OUT");
633 return CA_MEMORY_ALLOC_FAILED;
636 CAResult_t CADetachRequestToAllMessage(const CAGroupEndpoint_t *object,
637 const CARequestInfo_t *request)
639 OIC_LOG(DEBUG, TAG, "IN");
641 if (NULL == object || NULL == request || NULL == object->resourceUri)
643 return CA_STATUS_INVALID_PARAM;
646 if ((request->method < CA_GET) || (request->method > CA_DELETE))
648 OIC_LOG(ERROR, TAG, "Invalid method type!");
650 return CA_STATUS_INVALID_PARAM;
653 if (false == CAIsSelectedNetworkAvailable())
655 return CA_STATUS_FAILED;
658 CARemoteEndpoint_t *remoteEndpoint = NULL;
659 CARequestInfo_t *requestInfo = NULL;
661 // allocate & initialize
662 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
663 CA_MEMORY_ALLOC_CHECK(data);
665 CAAddress_t addr = {};
666 remoteEndpoint = CACreateRemoteEndpointInternal(object->resourceUri, addr,
667 object->transportType);
669 // clone request info
670 requestInfo = CACloneRequestInfo(request);
671 CA_MEMORY_ALLOC_CHECK(requestInfo);
674 data->type = SEND_TYPE_MULTICAST;
675 data->remoteEndpoint = remoteEndpoint;
676 data->requestInfo = requestInfo;
677 data->responseInfo = NULL;
680 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
682 OIC_LOG(DEBUG, TAG, "OUT");
685 // memory error label.
688 CADestroyRequestInfoInternal(requestInfo);
689 CADestroyRemoteEndpointInternal(remoteEndpoint);
691 OIC_LOG(DEBUG, TAG, "OUT");
692 return CA_MEMORY_ALLOC_FAILED;
695 CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object,
696 const CAResponseInfo_t *response)
698 OIC_LOG(DEBUG, TAG, "IN");
699 VERIFY_NON_NULL(object, TAG, "object");
700 VERIFY_NON_NULL(response, TAG, "response");
702 if (false == CAIsSelectedNetworkAvailable())
704 return CA_STATUS_FAILED;
707 CARemoteEndpoint_t *remoteEndpoint = NULL;
708 CAResponseInfo_t *responseInfo = NULL;
710 // allocate & initialize
711 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
712 CA_MEMORY_ALLOC_CHECK(data);
714 // clone remote endpoint
715 remoteEndpoint = CACloneRemoteEndpoint(object);
716 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
718 // clone response info
719 responseInfo = CACloneResponseInfo(response);
720 CA_MEMORY_ALLOC_CHECK(responseInfo);
723 data->type = SEND_TYPE_UNICAST;
724 data->remoteEndpoint = remoteEndpoint;
725 data->requestInfo = NULL;
726 data->responseInfo = responseInfo;
729 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
731 OIC_LOG(DEBUG, TAG, "OUT");
734 // memory error label.
736 CADestroyRemoteEndpointInternal(remoteEndpoint);
737 CADestroyResponseInfoInternal(responseInfo);
739 OIC_LOG(DEBUG, TAG, "OUT");
741 return CA_MEMORY_ALLOC_FAILED;
744 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
745 uint8_t tokenLength, const CAHeaderOption_t *options,
748 OIC_LOG(DEBUG, TAG, "IN");
749 VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
750 VERIFY_NON_NULL(token, TAG, "Token is NULL");
752 if (false == CAIsSelectedNetworkAvailable())
754 return CA_STATUS_FAILED;
757 CARemoteEndpoint_t *remoteEndpoint = NULL;
758 CARequestInfo_t *reqInfo = NULL;
759 char *tempToken = NULL;
761 // allocate & initialize
762 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
763 CA_MEMORY_ALLOC_CHECK(data);
765 CAAddress_t addr = {};
766 remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr,
767 CA_IPV4 | CA_EDR | CA_LE);
769 // create request info
770 reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
771 CA_MEMORY_ALLOC_CHECK(reqInfo);
776 tempToken = (char *) OICMalloc(tokenLength);
777 CA_MEMORY_ALLOC_CHECK(tempToken);
778 memcpy(tempToken, token, tokenLength);
781 // save request info data
782 reqInfo->method = CA_GET;
783 reqInfo->info.type = CA_MSG_NONCONFIRM;
785 reqInfo->info.token = tempToken;
786 reqInfo->info.tokenLength = tokenLength;
789 data->type = SEND_TYPE_MULTICAST;
790 data->remoteEndpoint = remoteEndpoint;
791 data->requestInfo = reqInfo;
793 data->responseInfo = NULL;
794 data->options = NULL;
795 data->numOptions = 0;
796 if (NULL != options && 0 < numOptions)
799 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
801 CA_MEMORY_ALLOC_CHECK(headerOption);
803 memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
805 data->options = headerOption;
806 data->numOptions = numOptions;
810 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
812 OIC_LOG(DEBUG, TAG, "OUT");
815 // memory error label.
818 CADestroyRemoteEndpointInternal(remoteEndpoint);
823 OIC_LOG(DEBUG, TAG, "OUT");
824 return CA_MEMORY_ALLOC_FAILED;
827 void CASetRequestResponseCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
829 OIC_LOG(DEBUG, TAG, "IN");
830 g_requestHandler = ReqHandler;
831 g_responseHandler = RespHandler;
832 OIC_LOG(DEBUG, TAG, "OUT");
835 CAResult_t CAInitializeMessageHandler()
837 OIC_LOG(DEBUG, TAG, "IN");
838 CASetPacketReceivedCallback(CAReceivedPacketCallback);
840 CASetNetworkChangeCallback(CANetworkChangedCallback);
842 // create thread pool
843 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
845 if (res != CA_STATUS_OK)
847 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
851 // send thread initialize
852 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
853 CASendThreadProcess, CADataDestroyer))
855 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
856 return CA_STATUS_FAILED;
860 res = CAQueueingThreadStart(&g_sendThread);
862 if (res != CA_STATUS_OK)
864 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
865 ca_thread_pool_free(g_threadPoolHandle);
866 g_threadPoolHandle = NULL;
870 // receive thread initialize
871 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
872 CAReceiveThreadProcess, CADataDestroyer))
874 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
875 return CA_STATUS_FAILED;
878 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
879 // start receive thread
880 res = CAQueueingThreadStart(&gReceiveThread);
882 if (res != CA_STATUS_OK)
884 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
889 // retransmission initialize
890 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
891 CATimeoutCallback, NULL);
893 // start retransmission
894 res = CARetransmissionStart(&g_retransmissionContext);
896 if (res != CA_STATUS_OK)
898 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
902 // initialize interface adapters by controller
903 CAInitializeAdapters(g_threadPoolHandle);
904 OIC_LOG(DEBUG, TAG, "OUT");
908 void CATerminateMessageHandler()
910 OIC_LOG(DEBUG, TAG, "IN");
911 CATransportType_t connType;
912 u_arraylist_t *list = CAGetSelectedNetworkList();
913 uint32_t length = u_arraylist_length(list);
916 for (i = 0; i < length; i++)
918 void* ptrType = u_arraylist_get(list, i);
925 connType = *(CATransportType_t *) ptrType;
926 CAStopAdapter(connType);
929 // stop retransmission
930 if (NULL != g_retransmissionContext.threadMutex)
932 CARetransmissionStop(&g_retransmissionContext);
936 // delete thread data
937 if (NULL != g_sendThread.threadMutex)
939 CAQueueingThreadStop(&g_sendThread);
943 // delete thread data
944 if (NULL != g_receiveThread.threadMutex)
946 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
947 CAQueueingThreadStop(&gReceiveThread);
951 // destroy thread pool
952 if (NULL != g_threadPoolHandle)
954 ca_thread_pool_free(g_threadPoolHandle);
955 g_threadPoolHandle = NULL;
958 CARetransmissionDestroy(&g_retransmissionContext);
959 CAQueueingThreadDestroy(&g_sendThread);
960 CAQueueingThreadDestroy(&g_receiveThread);
962 // terminate interface adapters by controller
963 CATerminateAdapters();
965 OIC_LOG(DEBUG, TAG, "OUT");
968 void CALogPDUInfo(coap_pdu_t *pdu)
970 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
972 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
974 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
976 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
978 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
980 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
982 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);