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
61 CASendDataType_t type;
62 CARemoteEndpoint_t *remoteEndpoint;
63 CARequestInfo_t *requestInfo;
64 CAResponseInfo_t *responseInfo;
65 CAErrorInfo_t *errorInfo;
66 CAHeaderOption_t *options;
67 CADataType_t dataType;
72 static ca_thread_pool_t g_threadPoolHandle = NULL;
74 // message handler main thread
75 static CAQueueingThread_t g_sendThread;
76 static CAQueueingThread_t g_receiveThread;
78 static CARetransmission_t g_retransmissionContext;
81 static CARequestCallback g_requestHandler = NULL;
82 static CAResponseCallback g_responseHandler = NULL;
83 static CAErrorCallback g_errorHandler = NULL;
85 static void CAErrorHandler(const CARemoteEndpoint_t *remoteEndpoint,
86 const void *data, uint32_t dataLen, CAResult_t result);
88 static bool CAIsSelectedNetworkAvailable()
90 u_arraylist_t *list = CAGetSelectedNetworkList();
91 if (!list || list->length == 0)
93 OIC_LOG(ERROR, TAG, "No selected network");
100 static void CATimeoutCallback(const CARemoteEndpoint_t *endpoint, const void *pdu, uint32_t size)
102 OIC_LOG(DEBUG, TAG, "IN");
103 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
104 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
106 CARemoteEndpoint_t* ep = CACloneRemoteEndpoint(endpoint);
109 OIC_LOG(ERROR, TAG, "clone failed");
113 CAResponseInfo_t* resInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
117 OIC_LOG(ERROR, TAG, "calloc failed");
118 CADestroyRemoteEndpointInternal(ep);
122 resInfo->result = CA_RETRANSMIT_TIMEOUT;
123 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
124 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
125 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info));
126 if (CA_STATUS_OK != res)
128 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
129 OICFree(resInfo->info.token);
131 CADestroyRemoteEndpointInternal(ep);
135 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
138 OIC_LOG(ERROR, TAG, "memory allocation failed !");
139 CADestroyRemoteEndpointInternal(ep);
144 cadata->type = SEND_TYPE_UNICAST;
145 cadata->remoteEndpoint = ep;
146 cadata->requestInfo = NULL;
147 cadata->responseInfo = resInfo;
149 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
150 OIC_LOG(DEBUG, TAG, "OUT");
153 static void CADataDestroyer(void *data, uint32_t size)
155 OIC_LOG(DEBUG, TAG, "IN");
156 CAData_t *cadata = (CAData_t *) data;
160 OIC_LOG(ERROR, TAG, "cadata is NULL");
164 if (NULL != cadata->remoteEndpoint)
166 CADestroyRemoteEndpointInternal((CARemoteEndpoint_t *) cadata->remoteEndpoint);
169 if (NULL != cadata->requestInfo)
171 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
174 if (NULL != cadata->responseInfo)
176 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
179 if (NULL != cadata->errorInfo)
181 CAInfo_t *info = &cadata->errorInfo->info;
182 OICFree(info->token);
183 OICFree(info->options);
184 OICFree(info->payload);
185 OICFree(cadata->errorInfo);
188 OICFree(cadata->options);
190 OIC_LOG(DEBUG, TAG, "OUT");
193 static void CAReceiveThreadProcess(void *threadData)
195 OIC_LOG(DEBUG, TAG, "IN");
196 // Currently not supported
197 // This will be enabled when RI supports multi threading
198 #ifndef SINGLE_HANDLE
199 CAData_t *data = (CAData_t *) threadData;
203 OIC_LOG(ERROR, TAG, "thread data error!!");
207 // parse the data and call the callbacks.
210 CARemoteEndpoint_t *rep = (CARemoteEndpoint_t *)(data->remoteEndpoint);
214 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
218 if (data->requestInfo && g_requestHandler)
220 g_requestHandler(rep, data->requestInfo);
222 else if (data->responseInfo && g_responseHandler)
224 g_responseHandler(rep, data->responseInfo);
226 else if (data->errorInfo && g_errorHandler)
228 g_errorHandler(rep, data->errorInfo);
231 #endif /* SINGLE_HANDLE */
232 OIC_LOG(DEBUG, TAG, "OUT");
235 static void CASendThreadProcess(void *threadData)
237 OIC_LOG(DEBUG, TAG, "IN");
238 CAData_t *data = (CAData_t *) threadData;
240 VERIFY_NON_NULL_VOID(data, TAG, "data");
241 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");
243 CAResult_t res = CA_STATUS_FAILED;
245 CASendDataType_t type = data->type;
247 if (SEND_TYPE_UNICAST == type)
249 coap_pdu_t *pdu = NULL;
251 if (NULL != data->requestInfo)
253 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
255 pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri,
256 data->requestInfo->method,
257 data->requestInfo->info);
259 else if (NULL != data->responseInfo)
261 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
263 pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri,
264 data->responseInfo->result,
265 data->responseInfo->info);
269 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
272 // interface controller function call.
277 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
278 if (CA_STATUS_OK != res)
280 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
281 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
282 coap_delete_pdu(pdu);
285 // for retransmission
286 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
288 if (CA_STATUS_OK != res)
290 OIC_LOG_V(INFO, TAG, "retransmission will be not working: %d", res);
291 coap_delete_pdu(pdu);
295 coap_delete_pdu(pdu);
298 else if (SEND_TYPE_MULTICAST == type)
300 OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
302 CAInfo_t info = data->requestInfo->info;
304 info.options = data->options;
305 info.numOptions = data->numOptions;
307 coap_pdu_t *pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri, CA_GET,
314 res = CASendMulticastData(pdu->hdr, pdu->length);
315 if(CA_STATUS_OK != res)
317 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
318 CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
319 coap_delete_pdu(pdu);
323 coap_delete_pdu(pdu);
327 OIC_LOG(DEBUG, TAG, "OUT");
330 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
332 OIC_LOG(DEBUG, TAG, "IN");
333 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
334 VERIFY_NON_NULL_VOID(data, TAG, "data");
336 uint32_t code = CA_NOT_FOUND;
337 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
342 OIC_LOG(ERROR, TAG, "Parse PDU failed");
343 CAAdapterFreeRemoteEndpoint(endpoint);
347 char uri[CA_MAX_URI_LENGTH] = { 0, };
348 uint32_t bufLen = sizeof(uri);
350 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
352 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
355 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
356 coap_delete_pdu(pdu);
357 CAAdapterFreeRemoteEndpoint(endpoint);
361 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo, uri, bufLen);
362 if (CA_STATUS_OK != res)
364 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
366 coap_delete_pdu(pdu);
367 CAAdapterFreeRemoteEndpoint(endpoint);
371 if (NULL != ReqInfo->info.options)
374 for (i = 0; i < ReqInfo->info.numOptions; i++)
376 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
378 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
382 if (NULL != ReqInfo->info.payload)
384 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
386 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
387 if (NULL != ReqInfo->info.token)
389 OIC_LOG(DEBUG, TAG, "Request- token:");
390 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
391 ReqInfo->info.tokenLength);
394 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
395 OIC_LOG(DEBUG, TAG, "Request- token");
396 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
397 OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
398 if (NULL != endpoint)
400 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
401 if (NULL == endpoint->resourceUri)
403 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
405 coap_delete_pdu(pdu);
406 CAAdapterFreeRemoteEndpoint(endpoint);
409 memcpy(endpoint->resourceUri, uri, bufLen);
410 endpoint->resourceUri[bufLen] = '\0';
411 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
413 // store the data at queue.
414 CAData_t *cadata = NULL;
415 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
418 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
420 coap_delete_pdu(pdu);
421 CAAdapterFreeRemoteEndpoint(endpoint);
425 cadata->type = SEND_TYPE_UNICAST;
426 cadata->remoteEndpoint = endpoint;
427 cadata->requestInfo = ReqInfo;
428 cadata->responseInfo = NULL;
429 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
433 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
436 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
437 coap_delete_pdu(pdu);
438 CAAdapterFreeRemoteEndpoint(endpoint);
442 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo, uri, bufLen);
443 if (CA_STATUS_OK != res)
445 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
447 coap_delete_pdu(pdu);
448 CAAdapterFreeRemoteEndpoint(endpoint);
452 if (NULL != ResInfo->info.options)
455 for (i = 0; i < ResInfo->info.numOptions; i++)
457 OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
459 OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
463 if (NULL != ResInfo->info.payload)
465 OIC_LOG_V(DEBUG, TAG, "Response- payload: %s", ResInfo->info.payload);
467 OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
468 OIC_LOG_V(DEBUG, TAG, "Response- token : %s", ResInfo->info.token);
469 OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
471 if (NULL != endpoint)
473 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
474 if (NULL == endpoint->resourceUri)
476 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
478 coap_delete_pdu(pdu);
479 CAAdapterFreeRemoteEndpoint(endpoint);
482 memcpy(endpoint->resourceUri, uri, bufLen);
483 endpoint->resourceUri[bufLen] = '\0';
484 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
487 // store the data at queue.
488 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
491 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
493 coap_delete_pdu(pdu);
494 CAAdapterFreeRemoteEndpoint(endpoint);
498 cadata->type = SEND_TYPE_UNICAST;
499 cadata->remoteEndpoint = endpoint;
500 cadata->requestInfo = NULL;
502 // for retransmission
503 void *retransmissionPdu = NULL;
504 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
507 // get token from saved data in retransmission list
508 if (retransmissionPdu && CA_EMPTY == code)
510 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
512 if (CA_STATUS_OK != res)
514 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
515 OICFree(ResInfo->info.token);
518 OICFree(retransmissionPdu);
519 cadata->responseInfo = ResInfo;
521 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
526 coap_delete_pdu(pdu);
528 OIC_LOG(DEBUG, TAG, "OUT");
531 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
533 OIC_LOG(DEBUG, TAG, "IN");
535 OIC_LOG(DEBUG, TAG, "OUT");
538 void CAHandleRequestResponseCallbacks()
540 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks IN");
543 // parse the data and call the callbacks.
547 ca_mutex_lock(g_receiveThread.threadMutex);
549 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
551 ca_mutex_unlock(g_receiveThread.threadMutex);
559 void *msg = item->msg;
567 CAData_t *td = (CAData_t *) msg;
568 CARemoteEndpoint_t *rep = td->remoteEndpoint;
575 if (td->requestInfo && g_requestHandler)
577 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
578 g_requestHandler(rep, td->requestInfo);
580 else if (td->responseInfo && g_responseHandler)
582 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
583 g_responseHandler(rep, td->responseInfo);
585 else if (td->errorInfo && g_errorHandler)
587 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
588 g_errorHandler(rep, td->errorInfo);
591 CADataDestroyer(msg, sizeof(CAData_t));
594 OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
597 CAResult_t CADetachRequestMessage(const CARemoteEndpoint_t *object, const CARequestInfo_t *request)
599 OIC_LOG(DEBUG, TAG, "IN");
601 VERIFY_NON_NULL(object, TAG, "object");
602 VERIFY_NON_NULL(request, TAG, "request");
604 if (false == CAIsSelectedNetworkAvailable())
606 return CA_STATUS_FAILED;
609 CARemoteEndpoint_t *remoteEndpoint = NULL;
610 CARequestInfo_t *requestInfo = NULL;
611 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
612 CA_MEMORY_ALLOC_CHECK(data);
614 // clone remote endpoint
615 remoteEndpoint = CACloneRemoteEndpoint(object);
616 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
618 // clone request info
619 requestInfo = CACloneRequestInfo(request);
620 CA_MEMORY_ALLOC_CHECK(requestInfo);
623 data->type = SEND_TYPE_UNICAST;
624 data->remoteEndpoint = remoteEndpoint;
625 data->requestInfo = requestInfo;
626 data->responseInfo = NULL;
629 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
630 OIC_LOG(DEBUG, TAG, "OUT");
633 // memory error label.
635 CADestroyRemoteEndpointInternal(remoteEndpoint);
636 CADestroyRequestInfoInternal(requestInfo);
639 OIC_LOG(DEBUG, TAG, "OUT");
640 return CA_MEMORY_ALLOC_FAILED;
643 CAResult_t CADetachRequestToAllMessage(const CAGroupEndpoint_t *object,
644 const CARequestInfo_t *request)
646 OIC_LOG(DEBUG, TAG, "IN");
648 if (NULL == object || NULL == request || NULL == object->resourceUri)
650 return CA_STATUS_INVALID_PARAM;
653 if ((request->method < CA_GET) || (request->method > CA_DELETE))
655 OIC_LOG(ERROR, TAG, "Invalid method type!");
657 return CA_STATUS_INVALID_PARAM;
660 if (false == CAIsSelectedNetworkAvailable())
662 return CA_STATUS_FAILED;
665 CARemoteEndpoint_t *remoteEndpoint = NULL;
666 CARequestInfo_t *requestInfo = NULL;
668 // allocate & initialize
669 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
670 CA_MEMORY_ALLOC_CHECK(data);
672 CAAddress_t addr = {};
673 remoteEndpoint = CACreateRemoteEndpointInternal(object->resourceUri, addr,
674 object->transportType);
676 // clone request info
677 requestInfo = CACloneRequestInfo(request);
678 CA_MEMORY_ALLOC_CHECK(requestInfo);
681 data->type = SEND_TYPE_MULTICAST;
682 data->remoteEndpoint = remoteEndpoint;
683 data->requestInfo = requestInfo;
684 data->responseInfo = NULL;
687 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
689 OIC_LOG(DEBUG, TAG, "OUT");
692 // memory error label.
695 CADestroyRequestInfoInternal(requestInfo);
696 CADestroyRemoteEndpointInternal(remoteEndpoint);
698 OIC_LOG(DEBUG, TAG, "OUT");
699 return CA_MEMORY_ALLOC_FAILED;
702 CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object,
703 const CAResponseInfo_t *response)
705 OIC_LOG(DEBUG, TAG, "IN");
706 VERIFY_NON_NULL(object, TAG, "object");
707 VERIFY_NON_NULL(response, TAG, "response");
709 if (false == CAIsSelectedNetworkAvailable())
711 return CA_STATUS_FAILED;
714 CARemoteEndpoint_t *remoteEndpoint = NULL;
715 CAResponseInfo_t *responseInfo = NULL;
717 // allocate & initialize
718 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
719 CA_MEMORY_ALLOC_CHECK(data);
721 // clone remote endpoint
722 remoteEndpoint = CACloneRemoteEndpoint(object);
723 CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
725 // clone response info
726 responseInfo = CACloneResponseInfo(response);
727 CA_MEMORY_ALLOC_CHECK(responseInfo);
730 data->type = SEND_TYPE_UNICAST;
731 data->remoteEndpoint = remoteEndpoint;
732 data->requestInfo = NULL;
733 data->responseInfo = responseInfo;
736 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
738 OIC_LOG(DEBUG, TAG, "OUT");
741 // memory error label.
743 CADestroyRemoteEndpointInternal(remoteEndpoint);
744 CADestroyResponseInfoInternal(responseInfo);
746 OIC_LOG(DEBUG, TAG, "OUT");
748 return CA_MEMORY_ALLOC_FAILED;
751 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
752 uint8_t tokenLength, const CAHeaderOption_t *options,
755 OIC_LOG(DEBUG, TAG, "IN");
756 VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
757 VERIFY_NON_NULL(token, TAG, "Token is NULL");
759 if (false == CAIsSelectedNetworkAvailable())
761 return CA_STATUS_FAILED;
764 CARemoteEndpoint_t *remoteEndpoint = NULL;
765 CARequestInfo_t *reqInfo = NULL;
766 char *tempToken = NULL;
768 // allocate & initialize
769 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
770 CA_MEMORY_ALLOC_CHECK(data);
772 CAAddress_t addr = {};
773 remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr,
774 CA_IPV4 | CA_EDR | CA_LE);
776 // create request info
777 reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
778 CA_MEMORY_ALLOC_CHECK(reqInfo);
783 tempToken = (char *) OICMalloc(tokenLength);
784 CA_MEMORY_ALLOC_CHECK(tempToken);
785 memcpy(tempToken, token, tokenLength);
788 // save request info data
789 reqInfo->method = CA_GET;
790 reqInfo->info.type = CA_MSG_NONCONFIRM;
792 reqInfo->info.token = tempToken;
793 reqInfo->info.tokenLength = tokenLength;
796 data->type = SEND_TYPE_MULTICAST;
797 data->remoteEndpoint = remoteEndpoint;
798 data->requestInfo = reqInfo;
800 data->responseInfo = NULL;
801 data->options = NULL;
802 data->numOptions = 0;
803 if (NULL != options && 0 < numOptions)
806 CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
808 CA_MEMORY_ALLOC_CHECK(headerOption);
810 memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
812 data->options = headerOption;
813 data->numOptions = numOptions;
817 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
819 OIC_LOG(DEBUG, TAG, "OUT");
822 // memory error label.
825 CADestroyRemoteEndpointInternal(remoteEndpoint);
830 OIC_LOG(DEBUG, TAG, "OUT");
831 return CA_MEMORY_ALLOC_FAILED;
834 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
835 CAErrorCallback errroHandler)
837 OIC_LOG(DEBUG, TAG, "IN");
838 g_requestHandler = ReqHandler;
839 g_responseHandler = RespHandler;
840 g_errorHandler = errroHandler;
841 OIC_LOG(DEBUG, TAG, "OUT");
844 CAResult_t CAInitializeMessageHandler()
846 OIC_LOG(DEBUG, TAG, "IN");
847 CASetPacketReceivedCallback(CAReceivedPacketCallback);
849 CASetNetworkChangeCallback(CANetworkChangedCallback);
850 CASetErrorHandleCallback(CAErrorHandler);
852 // create thread pool
853 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
855 if (res != CA_STATUS_OK)
857 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
861 // send thread initialize
862 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
863 CASendThreadProcess, CADataDestroyer))
865 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
866 return CA_STATUS_FAILED;
870 res = CAQueueingThreadStart(&g_sendThread);
872 if (res != CA_STATUS_OK)
874 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
875 ca_thread_pool_free(g_threadPoolHandle);
876 g_threadPoolHandle = NULL;
880 // receive thread initialize
881 if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
882 CAReceiveThreadProcess, CADataDestroyer))
884 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
885 return CA_STATUS_FAILED;
888 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
889 // start receive thread
890 res = CAQueueingThreadStart(&gReceiveThread);
892 if (res != CA_STATUS_OK)
894 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
899 // retransmission initialize
900 CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
901 CATimeoutCallback, NULL);
903 // start retransmission
904 res = CARetransmissionStart(&g_retransmissionContext);
906 if (res != CA_STATUS_OK)
908 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
912 // initialize interface adapters by controller
913 CAInitializeAdapters(g_threadPoolHandle);
914 OIC_LOG(DEBUG, TAG, "OUT");
918 void CATerminateMessageHandler()
920 OIC_LOG(DEBUG, TAG, "IN");
921 CATransportType_t connType;
922 u_arraylist_t *list = CAGetSelectedNetworkList();
923 uint32_t length = u_arraylist_length(list);
926 for (i = 0; i < length; i++)
928 void* ptrType = u_arraylist_get(list, i);
935 connType = *(CATransportType_t *) ptrType;
936 CAStopAdapter(connType);
939 // stop retransmission
940 if (NULL != g_retransmissionContext.threadMutex)
942 CARetransmissionStop(&g_retransmissionContext);
946 // delete thread data
947 if (NULL != g_sendThread.threadMutex)
949 CAQueueingThreadStop(&g_sendThread);
953 // delete thread data
954 if (NULL != g_receiveThread.threadMutex)
956 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
957 CAQueueingThreadStop(&gReceiveThread);
961 // destroy thread pool
962 if (NULL != g_threadPoolHandle)
964 ca_thread_pool_free(g_threadPoolHandle);
965 g_threadPoolHandle = NULL;
968 CARetransmissionDestroy(&g_retransmissionContext);
969 CAQueueingThreadDestroy(&g_sendThread);
970 CAQueueingThreadDestroy(&g_receiveThread);
972 // terminate interface adapters by controller
973 CATerminateAdapters();
975 OIC_LOG(DEBUG, TAG, "OUT");
978 void CALogPDUInfo(coap_pdu_t *pdu)
980 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
982 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
984 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
986 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
988 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
990 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
992 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
995 void CAErrorHandler(const CARemoteEndpoint_t *remoteEndpoint, const void *data,
996 uint32_t dataLen, CAResult_t result)
998 OIC_LOG(DEBUG, TAG, "IN");
999 VERIFY_NON_NULL_VOID(remoteEndpoint, TAG, "remoteEndpoint");
1000 VERIFY_NON_NULL_VOID(data, TAG, "data");
1002 uint32_t code = CA_NOT_FOUND;
1003 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1005 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
1008 OIC_LOG(ERROR, TAG, "Parse PDU failed");
1012 char uri[CA_MAX_URI_LENGTH] = { 0, };
1014 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *) OICCalloc(1, sizeof(CAErrorInfo_t));
1015 if (NULL == errorInfo)
1017 OIC_LOG(ERROR, TAG, "CAErrorHandler, Memory allocation failed!");
1018 coap_delete_pdu(pdu);
1022 CAResult_t res = CAGetErrorInfoFromPDU(pdu, errorInfo, uri, CA_MAX_URI_LENGTH);
1023 if (CA_STATUS_OK != res)
1025 OIC_LOG_V(ERROR, TAG, "CAGetErrorInfoFromPDU failed : %d", res);
1027 coap_delete_pdu(pdu);
1031 errorInfo->result = result;
1032 OIC_LOG_V(DEBUG, TAG, "error : %d", result);
1033 if (NULL != errorInfo->info.payload)
1035 OIC_LOG_V(DEBUG, TAG, "error, payload: %s", errorInfo->info.payload);
1038 OIC_LOG(DEBUG, TAG, "error, token");
1039 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) errorInfo->info.token,
1040 errorInfo->info.tokenLength);
1041 OIC_LOG_V(DEBUG, TAG, "CAErrorHandler, msgID : %d", errorInfo->info.messageId);
1043 CARemoteEndpoint_t *rep = NULL;
1044 rep = CACloneRemoteEndpoint(remoteEndpoint);
1047 OIC_LOG(ERROR, TAG, "CAErrorHandler, CloneRemoteEndpoint Failed");
1049 coap_delete_pdu(pdu);
1053 if(NULL == rep->resourceUri)
1055 uint32_t bufLen = sizeof(uri);
1057 CAURI_t resourceUri = (CAURI_t) OICMalloc(bufLen + 1);
1058 if (NULL == resourceUri)
1060 OIC_LOG(ERROR, TAG, "CAErrorHandler, Memory allocation failed!");
1062 coap_delete_pdu(pdu);
1066 memcpy(resourceUri, uri, bufLen);
1067 resourceUri[bufLen] = '\0';
1068 OIC_LOG_V(DEBUG, TAG, "URI : %s", resourceUri);
1069 rep->resourceUri = resourceUri;
1072 // store the data at queue.
1073 CAData_t *cadata = NULL;
1074 cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1077 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
1078 CADestroyRemoteEndpointInternal(rep);
1080 coap_delete_pdu(pdu);
1084 cadata->remoteEndpoint = rep;
1085 cadata->requestInfo = NULL;
1086 cadata->responseInfo = NULL;
1087 cadata->errorInfo = errorInfo;
1088 cadata->dataType = CA_ERROR_DATA;
1089 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1090 coap_delete_pdu(pdu);