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"
34 #include "config.h" /* for coap protocol */
35 #include "uthreadpool.h" /* for thread pool */
36 #include "caqueueingthread.h"
38 #include "oic_malloc.h"
39 #include "canetworkconfigurator.h"
44 #define MAX_THREAD_POOL_SIZE 20
48 SEND_TYPE_MULTICAST = 0, SEND_TYPE_UNICAST
53 CASendDataType_t type;
54 CARemoteEndpoint_t *remoteEndpoint;
55 CARequestInfo_t *requestInfo;
56 CAResponseInfo_t *responseInfo;
57 CAHeaderOption_t *options;
62 static u_thread_pool_t g_threadPoolHandle = NULL;
64 // message handler main thread
65 static CAQueueingThread_t g_sendThread;
66 static CAQueueingThread_t g_receiveThread;
68 static CARetransmission_t g_retransmissionContext;
71 static CARequestCallback g_requestHandler = NULL;
72 static CAResponseCallback g_responseHandler = NULL;
74 static void CATimeoutCallback(const CARemoteEndpoint_t *endpoint, const void *pdu, uint32_t size)
76 OIC_LOG(DEBUG, TAG, "IN");
77 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
78 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
80 CARemoteEndpoint_t* ep = CACloneRemoteEndpoint(endpoint);
83 OIC_LOG(ERROR, TAG, "clone failed");
87 CAResponseInfo_t* resInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
91 OIC_LOG(ERROR, TAG, "calloc failed");
92 CADestroyRemoteEndpointInternal(ep);
96 resInfo->result = CA_RETRANSMIT_TIMEOUT;
97 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
98 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
99 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info));
100 if (CA_STATUS_OK != res)
102 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
103 OICFree(resInfo->info.token);
107 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
110 OIC_LOG(ERROR, TAG, "memory allocation failed !");
111 CADestroyRemoteEndpointInternal(ep);
116 cadata->type = SEND_TYPE_UNICAST;
117 cadata->remoteEndpoint = ep;
118 cadata->requestInfo = NULL;
119 cadata->responseInfo = resInfo;
121 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
122 OIC_LOG(DEBUG, TAG, "OUT");
125 static void CADataDestroyer(void *data, uint32_t size)
127 OIC_LOG(DEBUG, TAG, "IN");
128 CAData_t *cadata = (CAData_t *) data;
132 OIC_LOG(ERROR, TAG, "cadata is NULL");
136 if (NULL != cadata->remoteEndpoint)
138 CADestroyRemoteEndpointInternal((CARemoteEndpoint_t *) cadata->remoteEndpoint);
141 if (NULL != cadata->requestInfo)
143 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
146 if (NULL != cadata->responseInfo)
148 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
151 OICFree(cadata->options);
153 OIC_LOG(DEBUG, TAG, "OUT");
156 static void CAReceiveThreadProcess(void *threadData)
158 OIC_LOG(DEBUG, TAG, "IN");
159 // Currently not supported
160 // This will be enabled when RI supports multi threading
161 #ifndef SINGLE_HANDLE
162 CAData_t *data = (CAData_t *) threadData;
166 OIC_LOG(ERROR, TAG, "thread data error!!");
170 // parse the data and call the callbacks.
173 CARemoteEndpoint_t *rep = (CARemoteEndpoint_t *)(data->remoteEndpoint);
177 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
181 if (NULL != data->requestInfo)
183 if (g_requestHandler)
185 g_requestHandler(rep, data->requestInfo);
189 if (NULL != data->responseInfo)
191 if (g_responseHandler)
193 g_responseHandler(rep, data->responseInfo);
197 OIC_LOG(DEBUG, TAG, "OUT");
200 static void CASendThreadProcess(void *threadData)
202 OIC_LOG(DEBUG, TAG, "IN");
203 CAData_t *data = (CAData_t *) threadData;
205 VERIFY_NON_NULL_VOID(data, TAG, "data");
206 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");
208 CAResult_t res = CA_STATUS_FAILED;
210 CASendDataType_t type = data->type;
212 if (SEND_TYPE_UNICAST == type)
214 coap_pdu_t *pdu = NULL;
216 if (NULL != data->requestInfo)
218 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
220 pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri,
221 data->requestInfo->method,
222 data->requestInfo->info);
224 else if (NULL != data->responseInfo)
226 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
228 pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri,
229 data->responseInfo->result,
230 data->responseInfo->info);
234 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
237 // interface controller function call.
242 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
243 if (CA_STATUS_OK != res)
245 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
246 coap_delete_pdu(pdu);
249 // for retransmission
250 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
252 if (CA_STATUS_OK != res)
254 OIC_LOG_V(INFO, TAG, "retransmission will be not working: %d", res);
255 coap_delete_pdu(pdu);
259 coap_delete_pdu(pdu);
262 else if (SEND_TYPE_MULTICAST == type)
264 OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
268 info.options = data->options;
269 info.numOptions = data->numOptions;
270 info.token = data->requestInfo->info.token;
271 info.tokenLength = data->requestInfo->info.tokenLength;
272 info.type = data->requestInfo->info.type;
273 info.messageId = data->requestInfo->info.messageId;
274 info.payload = data->requestInfo->info.payload;
276 coap_pdu_t *pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri, CA_GET,
283 res = CASendMulticastData(pdu->hdr, pdu->length);
284 if(CA_STATUS_OK != res)
286 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
287 coap_delete_pdu(pdu);
291 coap_delete_pdu(pdu);
295 OIC_LOG(DEBUG, TAG, "OUT");
298 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
300 OIC_LOG(DEBUG, TAG, "IN");
301 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
302 VERIFY_NON_NULL_VOID(data, TAG, "data");
304 uint32_t code = CA_NOT_FOUND;
305 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
310 OIC_LOG(ERROR, TAG, "Parse PDU failed");
311 CAAdapterFreeRemoteEndpoint(endpoint);
315 char uri[CA_MAX_URI_LENGTH] = { 0, };
316 uint32_t bufLen = sizeof(uri);
318 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
320 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
323 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
324 coap_delete_pdu(pdu);
325 CAAdapterFreeRemoteEndpoint(endpoint);
329 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo, uri, bufLen);
330 if (CA_STATUS_OK != res)
332 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
334 coap_delete_pdu(pdu);
335 CAAdapterFreeRemoteEndpoint(endpoint);
339 if (NULL != ReqInfo->info.options)
342 for (i = 0; i < ReqInfo->info.numOptions; i++)
344 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
346 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
350 if (NULL != ReqInfo->info.payload)
352 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
354 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
355 if (NULL != ReqInfo->info.token)
357 OIC_LOG(DEBUG, TAG, "Request- token:");
358 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
359 ReqInfo->info.tokenLength);
362 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
363 OIC_LOG(DEBUG, TAG, "Request- token");
364 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
365 OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
366 if (NULL != endpoint)
368 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
369 if (NULL == endpoint->resourceUri)
371 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
373 coap_delete_pdu(pdu);
374 CAAdapterFreeRemoteEndpoint(endpoint);
377 memcpy(endpoint->resourceUri, uri, bufLen);
378 endpoint->resourceUri[bufLen] = '\0';
379 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
381 // store the data at queue.
382 CAData_t *cadata = NULL;
383 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
386 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
387 if (NULL != endpoint && NULL != endpoint->resourceUri)
389 OICFree(endpoint->resourceUri);
393 coap_delete_pdu(pdu);
394 CAAdapterFreeRemoteEndpoint(endpoint);
398 cadata->type = SEND_TYPE_UNICAST;
399 cadata->remoteEndpoint = endpoint;
400 cadata->requestInfo = ReqInfo;
401 cadata->responseInfo = NULL;
402 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
406 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
409 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
410 coap_delete_pdu(pdu);
411 CAAdapterFreeRemoteEndpoint(endpoint);
415 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo, uri, bufLen);
416 if (CA_STATUS_OK != res)
418 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
420 coap_delete_pdu(pdu);
421 CAAdapterFreeRemoteEndpoint(endpoint);
425 if (NULL != ResInfo->info.options)
428 for (i = 0; i < ResInfo->info.numOptions; i++)
430 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
432 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
436 if (NULL != ResInfo->info.payload)
438 OIC_LOG_V(DEBUG, TAG, "Response- payload: %s", ResInfo->info.payload);
440 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
441 OIC_LOG_V(DEBUG, TAG, "Response- token : %s", ResInfo->info.token);
442 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
444 if (NULL != endpoint)
446 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
447 if (NULL == endpoint->resourceUri)
449 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
451 coap_delete_pdu(pdu);
452 CAAdapterFreeRemoteEndpoint(endpoint);
455 memcpy(endpoint->resourceUri, uri, bufLen);
456 endpoint->resourceUri[bufLen] = '\0';
457 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
460 // store the data at queue.
461 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
464 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
465 if (NULL != endpoint && NULL != endpoint->resourceUri)
467 OICFree(endpoint->resourceUri);
470 coap_delete_pdu(pdu);
471 CAAdapterFreeRemoteEndpoint(endpoint);
475 cadata->type = SEND_TYPE_UNICAST;
476 cadata->remoteEndpoint = endpoint;
477 cadata->requestInfo = NULL;
479 // for retransmission
480 void *retransmissionPdu = NULL;
481 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
484 // get token from saved data in retransmission list
485 if (retransmissionPdu && CA_EMPTY == code)
487 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
489 if (CA_STATUS_OK != res)
491 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
492 OICFree(ResInfo->info.token);
495 OICFree(retransmissionPdu);
496 cadata->responseInfo = ResInfo;
498 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
503 coap_delete_pdu(pdu);
505 OIC_LOG(DEBUG, TAG, "OUT");
508 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
510 OIC_LOG(DEBUG, TAG, "IN");
512 OIC_LOG(DEBUG, TAG, "OUT");
515 void CAHandleRequestResponseCallbacks()
517 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks IN");
520 // parse the data and call the callbacks.
524 u_mutex_lock(g_receiveThread.threadMutex);
526 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
528 u_mutex_unlock(g_receiveThread.threadMutex);
536 void *msg = item->msg;
544 CAData_t *td = (CAData_t *) msg;
545 CARemoteEndpoint_t *rep = td->remoteEndpoint;
552 if (NULL != td->requestInfo)
554 if (g_requestHandler)
556 OIC_LOG_V(DEBUG, TAG, "callback will be sent : %d", td->requestInfo->info.numOptions);
557 g_requestHandler(rep, td->requestInfo);
561 if (NULL != td->responseInfo)
563 if (g_responseHandler)
565 g_responseHandler(rep, td->responseInfo);
569 CADataDestroyer(msg, sizeof(CAData_t));
572 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
575 CAResult_t CADetachRequestMessage(const CARemoteEndpoint_t *object, const CARequestInfo_t *request)
577 OIC_LOG(DEBUG, TAG, "IN");
579 VERIFY_NON_NULL(object, TAG, "object");
580 VERIFY_NON_NULL(request, TAG, "request");
582 CARemoteEndpoint_t *remoteEndpoint = NULL;
583 CARequestInfo_t *requestInfo = NULL;
584 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
585 CA_MEMORY_ALLOC_CHECK(data);
587 // clone remote endpoint
588 remoteEndpoint = CACloneRemoteEndpoint(object);
589 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
591 // clone request info
592 requestInfo = CACloneRequestInfo(request);
593 CA_MEMORY_ALLOC_CHECK(requestInfo);
596 data->type = SEND_TYPE_UNICAST;
597 data->remoteEndpoint = remoteEndpoint;
598 data->requestInfo = requestInfo;
599 data->responseInfo = NULL;
602 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
603 OIC_LOG(DEBUG, TAG, "OUT");
606 // memory error label.
608 CADestroyRemoteEndpointInternal(remoteEndpoint);
609 CADestroyRequestInfoInternal(requestInfo);
612 OIC_LOG(DEBUG, TAG, "OUT");
613 return CA_MEMORY_ALLOC_FAILED;
616 CAResult_t CADetachRequestToAllMessage(const CAGroupEndpoint_t *object,
617 const CARequestInfo_t *request)
619 OIC_LOG(DEBUG, TAG, "IN");
621 if (NULL == object || NULL == request || NULL == object->resourceUri)
623 return CA_STATUS_INVALID_PARAM;
626 if ((request->method < CA_GET) || (request->method > CA_DELETE))
628 OIC_LOG(ERROR, TAG, "Invalid method type!");
630 return CA_STATUS_INVALID_PARAM;
633 CARemoteEndpoint_t *remoteEndpoint = NULL;
634 CARequestInfo_t *requestInfo = NULL;
636 // allocate & initialize
637 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
638 CA_MEMORY_ALLOC_CHECK(data);
640 CAAddress_t addr = {};
641 remoteEndpoint = CACreateRemoteEndpointInternal(object->resourceUri, addr,
642 object->connectivityType);
644 // clone request info
645 requestInfo = CACloneRequestInfo(request);
646 CA_MEMORY_ALLOC_CHECK(requestInfo);
649 data->type = SEND_TYPE_MULTICAST;
650 data->remoteEndpoint = remoteEndpoint;
651 data->requestInfo = requestInfo;
652 data->responseInfo = NULL;
655 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
657 OIC_LOG(DEBUG, TAG, "OUT");
660 // memory error label.
663 CADestroyRequestInfoInternal(requestInfo);
664 CADestroyRemoteEndpointInternal(remoteEndpoint);
666 OIC_LOG(DEBUG, TAG, "OUT");
667 return CA_MEMORY_ALLOC_FAILED;
670 CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object,
671 const CAResponseInfo_t *response)
673 OIC_LOG(DEBUG, TAG, "IN");
674 VERIFY_NON_NULL(object, TAG, "object");
675 VERIFY_NON_NULL(response, TAG, "response");
677 CARemoteEndpoint_t *remoteEndpoint = NULL;
678 CAResponseInfo_t *responseInfo = NULL;
680 // allocate & initialize
681 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
682 CA_MEMORY_ALLOC_CHECK(data);
684 // clone remote endpoint
685 remoteEndpoint = CACloneRemoteEndpoint(object);
686 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
688 // clone response info
689 responseInfo = CACloneResponseInfo(response);
690 CA_MEMORY_ALLOC_CHECK(responseInfo);
693 data->type = SEND_TYPE_UNICAST;
694 data->remoteEndpoint = remoteEndpoint;
695 data->requestInfo = NULL;
696 data->responseInfo = responseInfo;
699 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
701 OIC_LOG(DEBUG, TAG, "OUT");
704 // memory error label.
706 CADestroyRemoteEndpointInternal(remoteEndpoint);
707 CADestroyResponseInfoInternal(responseInfo);
709 OIC_LOG(DEBUG, TAG, "OUT");
711 return CA_MEMORY_ALLOC_FAILED;
714 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
715 uint8_t tokenLength, const CAHeaderOption_t *options,
718 OIC_LOG(DEBUG, TAG, "IN");
719 VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
720 VERIFY_NON_NULL(token, TAG, "Token is NULL");
722 CARemoteEndpoint_t *remoteEndpoint = NULL;
723 CARequestInfo_t *reqInfo = NULL;
724 char *tempToken = NULL;
726 // allocate & initialize
727 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
728 CA_MEMORY_ALLOC_CHECK(data);
730 CAAddress_t addr = {};
731 remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr,
732 CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE);
734 // create request info
735 reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
736 CA_MEMORY_ALLOC_CHECK(reqInfo);
741 tempToken = (char *) OICMalloc(tokenLength);
742 CA_MEMORY_ALLOC_CHECK(tempToken);
743 memcpy(tempToken, token, tokenLength);
746 // save request info data
747 reqInfo->method = CA_GET;
748 reqInfo->info.type = CA_MSG_NONCONFIRM;
750 reqInfo->info.token = tempToken;
751 reqInfo->info.tokenLength = tokenLength;
754 data->type = SEND_TYPE_MULTICAST;
755 data->remoteEndpoint = remoteEndpoint;
756 data->requestInfo = reqInfo;
758 data->responseInfo = NULL;
759 data->options = NULL;
760 data->numOptions = 0;
761 if (NULL != options && 0 < numOptions)
764 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
766 CA_MEMORY_ALLOC_CHECK(headerOption);
768 memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
770 data->options = headerOption;
771 data->numOptions = numOptions;
775 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
777 OIC_LOG(DEBUG, TAG, "OUT");
780 // memory error label.
783 CADestroyRemoteEndpointInternal(remoteEndpoint);
788 OIC_LOG(DEBUG, TAG, "OUT");
789 return CA_MEMORY_ALLOC_FAILED;
792 void CASetRequestResponseCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
794 OIC_LOG(DEBUG, TAG, "IN");
795 g_requestHandler = ReqHandler;
796 g_responseHandler = RespHandler;
797 OIC_LOG(DEBUG, TAG, "OUT");
800 CAResult_t CAInitializeMessageHandler()
802 OIC_LOG(DEBUG, TAG, "IN");
803 CASetPacketReceivedCallback(CAReceivedPacketCallback);
805 CASetNetworkChangeCallback(CANetworkChangedCallback);
807 // create thread pool
808 CAResult_t res = u_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
810 if (res != CA_STATUS_OK)
812 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
816 // send thread initialize
817 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
818 CASendThreadProcess, CADataDestroyer))
820 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
821 return CA_STATUS_FAILED;
825 res = CAQueueingThreadStart(&g_sendThread);
827 if (res != CA_STATUS_OK)
829 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
830 u_thread_pool_free(g_threadPoolHandle);
831 g_threadPoolHandle = NULL;
835 // receive thread initialize
836 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
837 CAReceiveThreadProcess, CADataDestroyer))
839 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
840 return CA_STATUS_FAILED;
843 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
844 // start receive thread
845 res = CAQueueingThreadStart(&gReceiveThread);
847 if (res != CA_STATUS_OK)
849 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
854 // retransmission initialize
855 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
856 CATimeoutCallback, NULL);
858 // start retransmission
859 res = CARetransmissionStart(&g_retransmissionContext);
861 if (res != CA_STATUS_OK)
863 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
867 // initialize interface adapters by controller
868 CAInitializeAdapters(g_threadPoolHandle);
869 OIC_LOG(DEBUG, TAG, "OUT");
873 void CATerminateMessageHandler()
875 OIC_LOG(DEBUG, TAG, "IN");
876 CAConnectivityType_t connType;
877 u_arraylist_t *list = CAGetSelectedNetworkList();
878 uint32_t length = u_arraylist_length(list);
881 for (i = 0; i < length; i++)
883 void* ptrType = u_arraylist_get(list, i);
890 connType = *(CAConnectivityType_t *) ptrType;
891 CAStopAdapter(connType);
894 // stop retransmission
895 if (NULL != g_retransmissionContext.threadMutex)
897 CARetransmissionStop(&g_retransmissionContext);
901 // delete thread data
902 if (NULL != g_sendThread.threadMutex)
904 CAQueueingThreadStop(&g_sendThread);
908 // delete thread data
909 if (NULL != g_receiveThread.threadMutex)
911 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
912 CAQueueingThreadStop(&gReceiveThread);
916 // destroy thread pool
917 if (NULL != g_threadPoolHandle)
919 u_thread_pool_free(g_threadPoolHandle);
920 g_threadPoolHandle = NULL;
923 CARetransmissionDestroy(&g_retransmissionContext);
924 CAQueueingThreadDestroy(&g_sendThread);
925 CAQueueingThreadDestroy(&g_receiveThread);
927 // terminate interface adapters by controller
928 CATerminateAdapters();
930 OIC_LOG(DEBUG, TAG, "OUT");
933 void CALogPDUInfo(coap_pdu_t *pdu)
935 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
937 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
939 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
941 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
943 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
945 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
947 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);