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);
105 CADestroyRemoteEndpointInternal(ep);
109 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
112 OIC_LOG(ERROR, TAG, "memory allocation failed !");
113 CADestroyRemoteEndpointInternal(ep);
118 cadata->type = SEND_TYPE_UNICAST;
119 cadata->remoteEndpoint = ep;
120 cadata->requestInfo = NULL;
121 cadata->responseInfo = resInfo;
123 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
124 OIC_LOG(DEBUG, TAG, "OUT");
127 static void CADataDestroyer(void *data, uint32_t size)
129 OIC_LOG(DEBUG, TAG, "IN");
130 CAData_t *cadata = (CAData_t *) data;
134 OIC_LOG(ERROR, TAG, "cadata is NULL");
138 if (NULL != cadata->remoteEndpoint)
140 CADestroyRemoteEndpointInternal((CARemoteEndpoint_t *) cadata->remoteEndpoint);
143 if (NULL != cadata->requestInfo)
145 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
148 if (NULL != cadata->responseInfo)
150 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
153 OICFree(cadata->options);
155 OIC_LOG(DEBUG, TAG, "OUT");
158 static void CAReceiveThreadProcess(void *threadData)
160 OIC_LOG(DEBUG, TAG, "IN");
161 // Currently not supported
162 // This will be enabled when RI supports multi threading
163 #ifndef SINGLE_HANDLE
164 CAData_t *data = (CAData_t *) threadData;
168 OIC_LOG(ERROR, TAG, "thread data error!!");
172 // parse the data and call the callbacks.
175 CARemoteEndpoint_t *rep = (CARemoteEndpoint_t *)(data->remoteEndpoint);
179 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
183 if (NULL != data->requestInfo)
185 if (g_requestHandler)
187 g_requestHandler(rep, data->requestInfo);
191 if (NULL != data->responseInfo)
193 if (g_responseHandler)
195 g_responseHandler(rep, data->responseInfo);
199 OIC_LOG(DEBUG, TAG, "OUT");
202 static void CASendThreadProcess(void *threadData)
204 OIC_LOG(DEBUG, TAG, "IN");
205 CAData_t *data = (CAData_t *) threadData;
207 VERIFY_NON_NULL_VOID(data, TAG, "data");
208 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");
210 CAResult_t res = CA_STATUS_FAILED;
212 CASendDataType_t type = data->type;
214 if (SEND_TYPE_UNICAST == type)
216 coap_pdu_t *pdu = NULL;
218 if (NULL != data->requestInfo)
220 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
222 pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri,
223 data->requestInfo->method,
224 data->requestInfo->info);
226 else if (NULL != data->responseInfo)
228 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
230 pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri,
231 data->responseInfo->result,
232 data->responseInfo->info);
236 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
239 // interface controller function call.
244 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
245 if (CA_STATUS_OK != res)
247 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
248 coap_delete_pdu(pdu);
251 // for retransmission
252 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
254 if (CA_STATUS_OK != res)
256 OIC_LOG_V(INFO, TAG, "retransmission will be not working: %d", res);
257 coap_delete_pdu(pdu);
261 coap_delete_pdu(pdu);
264 else if (SEND_TYPE_MULTICAST == type)
266 OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
270 info.options = data->options;
271 info.numOptions = data->numOptions;
272 info.token = data->requestInfo->info.token;
273 info.tokenLength = data->requestInfo->info.tokenLength;
274 info.type = data->requestInfo->info.type;
275 info.messageId = data->requestInfo->info.messageId;
276 info.payload = data->requestInfo->info.payload;
278 coap_pdu_t *pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri, CA_GET,
285 res = CASendMulticastData(pdu->hdr, pdu->length);
286 if(CA_STATUS_OK != res)
288 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
289 coap_delete_pdu(pdu);
293 coap_delete_pdu(pdu);
297 OIC_LOG(DEBUG, TAG, "OUT");
300 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
302 OIC_LOG(DEBUG, TAG, "IN");
303 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
304 VERIFY_NON_NULL_VOID(data, TAG, "data");
306 uint32_t code = CA_NOT_FOUND;
307 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
312 OIC_LOG(ERROR, TAG, "Parse PDU failed");
313 CAAdapterFreeRemoteEndpoint(endpoint);
317 char uri[CA_MAX_URI_LENGTH] = { 0, };
318 uint32_t bufLen = sizeof(uri);
320 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
322 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
325 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
326 coap_delete_pdu(pdu);
327 CAAdapterFreeRemoteEndpoint(endpoint);
331 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo, uri, bufLen);
332 if (CA_STATUS_OK != res)
334 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
336 coap_delete_pdu(pdu);
337 CAAdapterFreeRemoteEndpoint(endpoint);
341 if (NULL != ReqInfo->info.options)
344 for (i = 0; i < ReqInfo->info.numOptions; i++)
346 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
348 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
352 if (NULL != ReqInfo->info.payload)
354 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
356 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
357 if (NULL != ReqInfo->info.token)
359 OIC_LOG(DEBUG, TAG, "Request- token:");
360 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
361 ReqInfo->info.tokenLength);
364 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
365 OIC_LOG(DEBUG, TAG, "Request- token");
366 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
367 OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
368 if (NULL != endpoint)
370 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
371 if (NULL == endpoint->resourceUri)
373 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
375 coap_delete_pdu(pdu);
376 CAAdapterFreeRemoteEndpoint(endpoint);
379 memcpy(endpoint->resourceUri, uri, bufLen);
380 endpoint->resourceUri[bufLen] = '\0';
381 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
383 // store the data at queue.
384 CAData_t *cadata = NULL;
385 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
388 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
389 if (NULL != endpoint && NULL != endpoint->resourceUri)
391 OICFree(endpoint->resourceUri);
395 coap_delete_pdu(pdu);
396 CAAdapterFreeRemoteEndpoint(endpoint);
400 cadata->type = SEND_TYPE_UNICAST;
401 cadata->remoteEndpoint = endpoint;
402 cadata->requestInfo = ReqInfo;
403 cadata->responseInfo = NULL;
404 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
408 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
411 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
412 coap_delete_pdu(pdu);
413 CAAdapterFreeRemoteEndpoint(endpoint);
417 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo, uri, bufLen);
418 if (CA_STATUS_OK != res)
420 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
422 coap_delete_pdu(pdu);
423 CAAdapterFreeRemoteEndpoint(endpoint);
427 if (NULL != ResInfo->info.options)
430 for (i = 0; i < ResInfo->info.numOptions; i++)
432 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
434 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
438 if (NULL != ResInfo->info.payload)
440 OIC_LOG_V(DEBUG, TAG, "Response- payload: %s", ResInfo->info.payload);
442 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
443 OIC_LOG_V(DEBUG, TAG, "Response- token : %s", ResInfo->info.token);
444 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
446 if (NULL != endpoint)
448 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
449 if (NULL == endpoint->resourceUri)
451 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
453 coap_delete_pdu(pdu);
454 CAAdapterFreeRemoteEndpoint(endpoint);
457 memcpy(endpoint->resourceUri, uri, bufLen);
458 endpoint->resourceUri[bufLen] = '\0';
459 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
462 // store the data at queue.
463 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
466 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
467 if (NULL != endpoint && NULL != endpoint->resourceUri)
469 OICFree(endpoint->resourceUri);
472 coap_delete_pdu(pdu);
473 CAAdapterFreeRemoteEndpoint(endpoint);
477 cadata->type = SEND_TYPE_UNICAST;
478 cadata->remoteEndpoint = endpoint;
479 cadata->requestInfo = NULL;
481 // for retransmission
482 void *retransmissionPdu = NULL;
483 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
486 // get token from saved data in retransmission list
487 if (retransmissionPdu && CA_EMPTY == code)
489 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
491 if (CA_STATUS_OK != res)
493 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
494 OICFree(ResInfo->info.token);
497 OICFree(retransmissionPdu);
498 cadata->responseInfo = ResInfo;
500 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
505 coap_delete_pdu(pdu);
507 OIC_LOG(DEBUG, TAG, "OUT");
510 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
512 OIC_LOG(DEBUG, TAG, "IN");
514 OIC_LOG(DEBUG, TAG, "OUT");
517 void CAHandleRequestResponseCallbacks()
519 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks IN");
522 // parse the data and call the callbacks.
526 u_mutex_lock(g_receiveThread.threadMutex);
528 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
530 u_mutex_unlock(g_receiveThread.threadMutex);
538 void *msg = item->msg;
546 CAData_t *td = (CAData_t *) msg;
547 CARemoteEndpoint_t *rep = td->remoteEndpoint;
554 if (NULL != td->requestInfo)
556 if (g_requestHandler)
558 OIC_LOG_V(DEBUG, TAG, "callback will be sent : %d", td->requestInfo->info.numOptions);
559 g_requestHandler(rep, td->requestInfo);
563 if (NULL != td->responseInfo)
565 if (g_responseHandler)
567 g_responseHandler(rep, td->responseInfo);
571 CADataDestroyer(msg, sizeof(CAData_t));
574 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
577 CAResult_t CADetachRequestMessage(const CARemoteEndpoint_t *object, const CARequestInfo_t *request)
579 OIC_LOG(DEBUG, TAG, "IN");
581 VERIFY_NON_NULL(object, TAG, "object");
582 VERIFY_NON_NULL(request, TAG, "request");
584 CARemoteEndpoint_t *remoteEndpoint = NULL;
585 CARequestInfo_t *requestInfo = NULL;
586 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
587 CA_MEMORY_ALLOC_CHECK(data);
589 // clone remote endpoint
590 remoteEndpoint = CACloneRemoteEndpoint(object);
591 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
593 // clone request info
594 requestInfo = CACloneRequestInfo(request);
595 CA_MEMORY_ALLOC_CHECK(requestInfo);
598 data->type = SEND_TYPE_UNICAST;
599 data->remoteEndpoint = remoteEndpoint;
600 data->requestInfo = requestInfo;
601 data->responseInfo = NULL;
604 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
605 OIC_LOG(DEBUG, TAG, "OUT");
608 // memory error label.
610 CADestroyRemoteEndpointInternal(remoteEndpoint);
611 CADestroyRequestInfoInternal(requestInfo);
614 OIC_LOG(DEBUG, TAG, "OUT");
615 return CA_MEMORY_ALLOC_FAILED;
618 CAResult_t CADetachRequestToAllMessage(const CAGroupEndpoint_t *object,
619 const CARequestInfo_t *request)
621 OIC_LOG(DEBUG, TAG, "IN");
623 if (NULL == object || NULL == request || NULL == object->resourceUri)
625 return CA_STATUS_INVALID_PARAM;
628 if ((request->method < CA_GET) || (request->method > CA_DELETE))
630 OIC_LOG(ERROR, TAG, "Invalid method type!");
632 return CA_STATUS_INVALID_PARAM;
635 CARemoteEndpoint_t *remoteEndpoint = NULL;
636 CARequestInfo_t *requestInfo = NULL;
638 // allocate & initialize
639 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
640 CA_MEMORY_ALLOC_CHECK(data);
642 CAAddress_t addr = {};
643 remoteEndpoint = CACreateRemoteEndpointInternal(object->resourceUri, addr,
644 object->connectivityType);
646 // clone request info
647 requestInfo = CACloneRequestInfo(request);
648 CA_MEMORY_ALLOC_CHECK(requestInfo);
651 data->type = SEND_TYPE_MULTICAST;
652 data->remoteEndpoint = remoteEndpoint;
653 data->requestInfo = requestInfo;
654 data->responseInfo = NULL;
657 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
659 OIC_LOG(DEBUG, TAG, "OUT");
662 // memory error label.
665 CADestroyRequestInfoInternal(requestInfo);
666 CADestroyRemoteEndpointInternal(remoteEndpoint);
668 OIC_LOG(DEBUG, TAG, "OUT");
669 return CA_MEMORY_ALLOC_FAILED;
672 CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object,
673 const CAResponseInfo_t *response)
675 OIC_LOG(DEBUG, TAG, "IN");
676 VERIFY_NON_NULL(object, TAG, "object");
677 VERIFY_NON_NULL(response, TAG, "response");
679 CARemoteEndpoint_t *remoteEndpoint = NULL;
680 CAResponseInfo_t *responseInfo = NULL;
682 // allocate & initialize
683 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
684 CA_MEMORY_ALLOC_CHECK(data);
686 // clone remote endpoint
687 remoteEndpoint = CACloneRemoteEndpoint(object);
688 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
690 // clone response info
691 responseInfo = CACloneResponseInfo(response);
692 CA_MEMORY_ALLOC_CHECK(responseInfo);
695 data->type = SEND_TYPE_UNICAST;
696 data->remoteEndpoint = remoteEndpoint;
697 data->requestInfo = NULL;
698 data->responseInfo = responseInfo;
701 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
703 OIC_LOG(DEBUG, TAG, "OUT");
706 // memory error label.
708 CADestroyRemoteEndpointInternal(remoteEndpoint);
709 CADestroyResponseInfoInternal(responseInfo);
711 OIC_LOG(DEBUG, TAG, "OUT");
713 return CA_MEMORY_ALLOC_FAILED;
716 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
717 uint8_t tokenLength, const CAHeaderOption_t *options,
720 OIC_LOG(DEBUG, TAG, "IN");
721 VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
722 VERIFY_NON_NULL(token, TAG, "Token is NULL");
724 CARemoteEndpoint_t *remoteEndpoint = NULL;
725 CARequestInfo_t *reqInfo = NULL;
726 char *tempToken = NULL;
728 // allocate & initialize
729 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
730 CA_MEMORY_ALLOC_CHECK(data);
732 CAAddress_t addr = {};
733 remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr,
734 CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE);
736 // create request info
737 reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
738 CA_MEMORY_ALLOC_CHECK(reqInfo);
743 tempToken = (char *) OICMalloc(tokenLength);
744 CA_MEMORY_ALLOC_CHECK(tempToken);
745 memcpy(tempToken, token, tokenLength);
748 // save request info data
749 reqInfo->method = CA_GET;
750 reqInfo->info.type = CA_MSG_NONCONFIRM;
752 reqInfo->info.token = tempToken;
753 reqInfo->info.tokenLength = tokenLength;
756 data->type = SEND_TYPE_MULTICAST;
757 data->remoteEndpoint = remoteEndpoint;
758 data->requestInfo = reqInfo;
760 data->responseInfo = NULL;
761 data->options = NULL;
762 data->numOptions = 0;
763 if (NULL != options && 0 < numOptions)
766 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
768 CA_MEMORY_ALLOC_CHECK(headerOption);
770 memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
772 data->options = headerOption;
773 data->numOptions = numOptions;
777 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
779 OIC_LOG(DEBUG, TAG, "OUT");
782 // memory error label.
785 CADestroyRemoteEndpointInternal(remoteEndpoint);
790 OIC_LOG(DEBUG, TAG, "OUT");
791 return CA_MEMORY_ALLOC_FAILED;
794 void CASetRequestResponseCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
796 OIC_LOG(DEBUG, TAG, "IN");
797 g_requestHandler = ReqHandler;
798 g_responseHandler = RespHandler;
799 OIC_LOG(DEBUG, TAG, "OUT");
802 CAResult_t CAInitializeMessageHandler()
804 OIC_LOG(DEBUG, TAG, "IN");
805 CASetPacketReceivedCallback(CAReceivedPacketCallback);
807 CASetNetworkChangeCallback(CANetworkChangedCallback);
809 // create thread pool
810 CAResult_t res = u_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
812 if (res != CA_STATUS_OK)
814 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
818 // send thread initialize
819 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
820 CASendThreadProcess, CADataDestroyer))
822 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
823 return CA_STATUS_FAILED;
827 res = CAQueueingThreadStart(&g_sendThread);
829 if (res != CA_STATUS_OK)
831 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
832 u_thread_pool_free(g_threadPoolHandle);
833 g_threadPoolHandle = NULL;
837 // receive thread initialize
838 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
839 CAReceiveThreadProcess, CADataDestroyer))
841 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
842 return CA_STATUS_FAILED;
845 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
846 // start receive thread
847 res = CAQueueingThreadStart(&gReceiveThread);
849 if (res != CA_STATUS_OK)
851 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
856 // retransmission initialize
857 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
858 CATimeoutCallback, NULL);
860 // start retransmission
861 res = CARetransmissionStart(&g_retransmissionContext);
863 if (res != CA_STATUS_OK)
865 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
869 // initialize interface adapters by controller
870 CAInitializeAdapters(g_threadPoolHandle);
871 OIC_LOG(DEBUG, TAG, "OUT");
875 void CATerminateMessageHandler()
877 OIC_LOG(DEBUG, TAG, "IN");
878 CAConnectivityType_t connType;
879 u_arraylist_t *list = CAGetSelectedNetworkList();
880 uint32_t length = u_arraylist_length(list);
883 for (i = 0; i < length; i++)
885 void* ptrType = u_arraylist_get(list, i);
892 connType = *(CAConnectivityType_t *) ptrType;
893 CAStopAdapter(connType);
896 // stop retransmission
897 if (NULL != g_retransmissionContext.threadMutex)
899 CARetransmissionStop(&g_retransmissionContext);
903 // delete thread data
904 if (NULL != g_sendThread.threadMutex)
906 CAQueueingThreadStop(&g_sendThread);
910 // delete thread data
911 if (NULL != g_receiveThread.threadMutex)
913 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
914 CAQueueingThreadStop(&gReceiveThread);
918 // destroy thread pool
919 if (NULL != g_threadPoolHandle)
921 u_thread_pool_free(g_threadPoolHandle);
922 g_threadPoolHandle = NULL;
925 CARetransmissionDestroy(&g_retransmissionContext);
926 CAQueueingThreadDestroy(&g_sendThread);
927 CAQueueingThreadDestroy(&g_receiveThread);
929 // terminate interface adapters by controller
930 CATerminateAdapters();
932 OIC_LOG(DEBUG, TAG, "OUT");
935 void CALogPDUInfo(coap_pdu_t *pdu)
937 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
939 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
941 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
943 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
945 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
947 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
949 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);