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 ******************************************************************/
21 // Defining _BSD_SOURCE or _DEFAULT_SOURCE causes header files to expose
22 // definitions that may otherwise be skipped. Skipping can cause implicit
23 // declaration warnings and/or bugs and subtle problems in code execution.
24 // For glibc information on feature test macros,
25 // Refer http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
27 // This file requires #define use due to random()
28 // For details on compatibility and glibc support,
29 // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html
30 #define _DEFAULT_SOURCE
32 // Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value
33 // causes header files to expose definitions
34 // corresponding to the POSIX.1b, Real-time extensions
35 // (IEEE Std 1003.1b-1993) specification
37 // For this specific file, see use of clock_gettime,
38 // Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html
39 // and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html
40 #ifndef _POSIX_C_SOURCE
41 #define _POSIX_C_SOURCE 200809L
52 #ifdef HAVE_SYS_TIME_H
56 #include <sys/timeb.h>
63 #if defined(__ANDROID__)
64 #include <linux/time.h>
67 #include "caretransmission.h"
68 #include "caremotehandler.h"
69 #include "caprotocolmessage.h"
70 #include "oic_malloc.h"
74 #define TAG "OIC_CA_RETRANS"
78 uint64_t timeStamp; /**< last sent time. microseconds */
80 uint64_t timeout; /**< timeout value. microseconds */
82 uint8_t triedCount; /**< retransmission count */
83 uint16_t messageId; /**< coap PDU message id */
84 CAEndpoint_t *endpoint; /**< remote endpoint */
85 void *pdu; /**< coap PDU */
86 uint32_t size; /**< coap PDU size */
87 } CARetransmissionData_t;
89 static const uint64_t USECS_PER_SEC = 1000000;
90 static const uint64_t MSECS_PER_SEC = 1000;
93 * @brief getCurrent monotonic time
94 * @return current time in microseconds
96 uint64_t getCurrentTimeInMicroSeconds();
100 * @brief timeout value is
101 * between DEFAULT_ACK_TIMEOUT_SEC and
102 * (DEFAULT_ACK_TIMEOUT_SEC * DEFAULT_RANDOM_FACTOR) second.
103 * DEFAULT_RANDOM_FACTOR 1.5 (CoAP)
104 * @return microseconds.
106 static uint64_t CAGetTimeoutValue()
109 return ((DEFAULT_ACK_TIMEOUT_SEC * 1000) + ((1000 * OCGetRandomByte()) >> 8)) *
112 return ((DEFAULT_ACK_TIMEOUT_SEC * 1000) + ((1000 * OCGetRandomByte()) >> 8)) *
117 CAResult_t CARetransmissionStart(CARetransmission_t *context)
121 OIC_LOG(ERROR, TAG, "context is empty");
122 return CA_STATUS_INVALID_PARAM;
125 if (NULL == context->threadPool)
127 OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
128 return CA_STATUS_INVALID_PARAM;
131 CAResult_t res = ca_thread_pool_add_task(context->threadPool, CARetransmissionBaseRoutine,
134 if (CA_STATUS_OK != res)
136 OIC_LOG(ERROR, TAG, "thread pool add task error(send thread).");
145 * @brief check timeout routine
146 * @param currentTime [IN]microseconds
147 * @param retData [IN]retransmission data
148 * @return true if the timeout period has elapsed, false otherwise
150 static bool CACheckTimeout(uint64_t currentTime, CARetransmissionData_t *retData)
152 #ifndef SINGLE_THREAD
153 // #1. calculate timeout
154 uint32_t milliTimeoutValue = retData->timeout * 0.001;
155 uint64_t timeout = (milliTimeoutValue << retData->triedCount) * (uint64_t) 1000;
157 if (currentTime >= retData->timeStamp + timeout)
159 OIC_LOG_V(DEBUG, TAG, "%llu microseconds time out!!, tried count(%d)",
160 timeout, retData->triedCount);
164 // #1. calculate timeout
165 uint64_t timeOut = (2 << retData->triedCount) * 1000000;
167 if (currentTime >= retData->timeStamp + timeOut)
169 OIC_LOG_V(DEBUG, TAG, "timeout=%d, tried cnt=%d",
170 (2 << retData->triedCount), retData->triedCount);
177 static void CACheckRetransmissionList(CARetransmission_t *context)
181 OIC_LOG(ERROR, TAG, "context is null");
186 ca_mutex_lock(context->threadMutex);
189 uint32_t len = u_arraylist_length(context->dataList);
191 for (i = 0; i < len; i++)
193 CARetransmissionData_t *retData = u_arraylist_get(context->dataList, i);
200 uint64_t currentTime = getCurrentTimeInMicroSeconds();
202 if (CACheckTimeout(currentTime, retData))
204 // #2. if time's up, send the data.
205 if (NULL != context->dataSendMethod)
207 OIC_LOG_V(DEBUG, TAG, "retransmission CON data!!, msgid=%d",
209 context->dataSendMethod(retData->endpoint, retData->pdu, retData->size);
212 // #3. increase the retransmission count and update timestamp.
213 retData->timeStamp = currentTime;
214 retData->triedCount++;
217 // #4. if tried count is max, remove the retransmission data from list.
218 if (retData->triedCount >= context->config.tryingCount)
220 CARetransmissionData_t *removedData = u_arraylist_remove(context->dataList, i);
221 if (NULL == removedData)
223 OIC_LOG(ERROR, TAG, "Removed data is NULL");
225 ca_mutex_unlock(context->threadMutex);
228 OIC_LOG_V(DEBUG, TAG, "max trying count, remove RTCON data,"
229 "msgid=%d", removedData->messageId);
231 // callback for retransmit timeout
232 if (NULL != context->timeoutCallback)
234 context->timeoutCallback(removedData->endpoint, removedData->pdu,
238 CAFreeEndpoint(removedData->endpoint);
239 OICFree(removedData->pdu);
241 OICFree(removedData);
243 // modify loop value.
244 len = u_arraylist_length(context->dataList);
250 ca_mutex_unlock(context->threadMutex);
253 void CARetransmissionBaseRoutine(void *threadValue)
255 OIC_LOG(DEBUG, TAG, "retransmission main thread start");
257 CARetransmission_t *context = (CARetransmission_t *) threadValue;
261 OIC_LOG(ERROR, TAG, "thread data passing error");
267 if (true == context->isStop)
269 OIC_LOG(DEBUG, TAG, "thread stopped");
272 CACheckRetransmissionList(context);
275 while (!context->isStop)
278 ca_mutex_lock(context->threadMutex);
280 if (!context->isStop && u_arraylist_length(context->dataList) <= 0)
282 // if list is empty, thread will wait
283 OIC_LOG(DEBUG, TAG, "wait..there is no retransmission data.");
286 ca_cond_wait(context->threadCond, context->threadMutex);
288 OIC_LOG(DEBUG, TAG, "wake up..");
290 else if (!context->isStop)
292 // check each RETRANSMISSION_CHECK_PERIOD_SEC time.
293 OIC_LOG_V(DEBUG, TAG, "wait..(%lld)microseconds",
294 RETRANSMISSION_CHECK_PERIOD_SEC * (uint64_t) USECS_PER_SEC);
297 uint64_t absTime = RETRANSMISSION_CHECK_PERIOD_SEC * (uint64_t) USECS_PER_SEC;
298 ca_cond_wait_for(context->threadCond, context->threadMutex, absTime );
302 // we are stopping, so we want to unlock and finish stopping
306 ca_mutex_unlock(context->threadMutex);
314 CACheckRetransmissionList(context);
317 ca_mutex_lock(context->threadMutex);
318 ca_cond_signal(context->threadCond);
319 ca_mutex_unlock(context->threadMutex);
322 OIC_LOG(DEBUG, TAG, "retransmission main thread end");
326 CAResult_t CARetransmissionInitialize(CARetransmission_t *context,
327 ca_thread_pool_t handle,
328 CADataSendMethod_t retransmissionSendMethod,
329 CATimeoutCallback_t timeoutCallback,
330 CARetransmissionConfig_t* config)
334 OIC_LOG(ERROR, TAG, "thread instance is empty");
335 return CA_STATUS_INVALID_PARAM;
337 #ifndef SINGLE_THREAD
340 OIC_LOG(ERROR, TAG, "thread pool handle is empty");
341 return CA_STATUS_INVALID_PARAM;
344 OIC_LOG(DEBUG, TAG, "thread initialize");
346 memset(context, 0, sizeof(CARetransmission_t));
348 CARetransmissionConfig_t cfg = { .supportType = DEFAULT_RETRANSMISSION_TYPE,
349 .tryingCount = DEFAULT_RETRANSMISSION_COUNT };
356 // set send thread data
357 context->threadPool = handle;
358 context->threadMutex = ca_mutex_new();
359 context->threadCond = ca_cond_new();
360 context->dataSendMethod = retransmissionSendMethod;
361 context->timeoutCallback = timeoutCallback;
362 context->config = cfg;
363 context->isStop = false;
364 context->dataList = u_arraylist_create();
369 CAResult_t CARetransmissionSentData(CARetransmission_t *context,
370 const CAEndpoint_t *endpoint,
371 const void *pdu, uint32_t size)
373 if (NULL == context || NULL == endpoint || NULL == pdu)
375 OIC_LOG(ERROR, TAG, "invalid parameter");
376 return CA_STATUS_INVALID_PARAM;
379 // #0. check support transport type
380 if (!(context->config.supportType & endpoint->adapter))
382 OIC_LOG_V(DEBUG, TAG, "not supported transport type=%d", endpoint->adapter);
383 return CA_NOT_SUPPORTED;
386 // #1. check PDU method type and get message id.
387 CAMessageType_t type = CAGetMessageTypeFromPduBinaryData(pdu, size);
388 uint16_t messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
390 OIC_LOG_V(DEBUG, TAG, "sent pdu, msgtype=%d, msgid=%d", type, messageId);
392 if (CA_MSG_CONFIRM != type)
394 OIC_LOG(DEBUG, TAG, "not supported message type");
395 return CA_NOT_SUPPORTED;
398 // create retransmission data
399 CARetransmissionData_t *retData = (CARetransmissionData_t *) OICCalloc(
400 1, sizeof(CARetransmissionData_t));
404 OIC_LOG(ERROR, TAG, "memory error");
405 return CA_MEMORY_ALLOC_FAILED;
409 void *pduData = (void *) OICMalloc(size);
413 OIC_LOG(ERROR, TAG, "memory error");
414 return CA_MEMORY_ALLOC_FAILED;
416 memcpy(pduData, pdu, size);
418 // clone remote endpoint
419 CAEndpoint_t *remoteEndpoint = CACloneEndpoint(endpoint);
420 if (NULL == remoteEndpoint)
424 OIC_LOG(ERROR, TAG, "memory error");
425 return CA_MEMORY_ALLOC_FAILED;
428 // #2. add additional information. (time stamp, retransmission count...)
429 retData->timeStamp = getCurrentTimeInMicroSeconds();
430 #ifndef SINGLE_THREAD
431 retData->timeout = CAGetTimeoutValue();
433 retData->triedCount = 0;
434 retData->messageId = messageId;
435 retData->endpoint = remoteEndpoint;
436 retData->pdu = pduData;
437 retData->size = size;
438 #ifndef SINGLE_THREAD
440 ca_mutex_lock(context->threadMutex);
443 uint32_t len = u_arraylist_length(context->dataList);
445 // #3. add data into list
446 for (i = 0; i < len; i++)
448 CARetransmissionData_t *currData = u_arraylist_get(context->dataList, i);
450 if (NULL == currData)
456 if (NULL != currData->endpoint && currData->messageId == messageId
457 && (currData->endpoint->adapter == endpoint->adapter))
459 OIC_LOG(ERROR, TAG, "Duplicate message ID");
462 ca_mutex_unlock(context->threadMutex);
466 OICFree(remoteEndpoint);
467 return CA_STATUS_FAILED;
471 u_arraylist_add(context->dataList, (void *) retData);
474 ca_cond_signal(context->threadCond);
477 ca_mutex_unlock(context->threadMutex);
480 u_arraylist_add(context->dataList, (void *) retData);
482 CACheckRetransmissionList(context);
487 CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
488 const CAEndpoint_t *endpoint, const void *pdu,
489 uint32_t size, void **retransmissionPdu)
491 OIC_LOG(DEBUG, TAG, "IN");
492 if (NULL == context || NULL == endpoint || NULL == pdu || NULL == retransmissionPdu)
494 OIC_LOG(ERROR, TAG, "invalid parameter");
495 return CA_STATUS_INVALID_PARAM;
498 // #0. check support transport type
499 if (!(context->config.supportType & endpoint->adapter))
501 OIC_LOG_V(DEBUG, TAG, "not supported transport type=%d", endpoint->adapter);
505 // #1. check PDU method type and get message id.
506 // ACK, RST --> remove the CON data
507 CAMessageType_t type = CAGetMessageTypeFromPduBinaryData(pdu, size);
508 uint16_t messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
509 CAResponseResult_t code = CAGetCodeFromPduBinaryData(pdu, size);
511 OIC_LOG_V(DEBUG, TAG, "received pdu, msgtype=%d, msgid=%d, code=%d",
512 type, messageId, code);
514 if (((CA_MSG_ACKNOWLEDGE != type) && (CA_MSG_RESET != type))
515 || (CA_MSG_RESET == type && CA_EMPTY != code))
521 ca_mutex_lock(context->threadMutex);
522 uint32_t len = u_arraylist_length(context->dataList);
526 for (i = 0; i < len; i++)
528 CARetransmissionData_t *retData = (CARetransmissionData_t *) u_arraylist_get(
529 context->dataList, i);
537 if (NULL != retData->endpoint && retData->messageId == messageId
538 && (retData->endpoint->adapter == endpoint->adapter))
540 // get pdu data for getting token when CA_EMPTY(RST/ACK) is received from remote device
541 // if retransmission was finish..token will be unavailable.
542 if (CA_EMPTY == CAGetCodeFromPduBinaryData(pdu, size))
544 OIC_LOG(DEBUG, TAG, "code is CA_EMPTY");
546 if (NULL == retData->pdu)
548 OIC_LOG(ERROR, TAG, "retData->pdu is null");
551 ca_mutex_unlock(context->threadMutex);
553 return CA_STATUS_FAILED;
557 (*retransmissionPdu) = (void *) OICCalloc(1, retData->size);
558 if ((*retransmissionPdu) == NULL)
561 OIC_LOG(ERROR, TAG, "memory error");
564 ca_mutex_unlock(context->threadMutex);
566 return CA_MEMORY_ALLOC_FAILED;
568 memcpy((*retransmissionPdu), retData->pdu, retData->size);
571 // #2. remove data from list
572 CARetransmissionData_t *removedData = u_arraylist_remove(context->dataList, i);
573 if (NULL == removedData)
575 OIC_LOG(ERROR, TAG, "Removed data is NULL");
578 ca_mutex_unlock(context->threadMutex);
580 return CA_STATUS_FAILED;
583 OIC_LOG_V(DEBUG, TAG, "remove RTCON data!!, msgid=%d", messageId);
585 CAFreeEndpoint(removedData->endpoint);
586 OICFree(removedData->pdu);
587 OICFree(removedData);
594 ca_mutex_unlock(context->threadMutex);
596 OIC_LOG(DEBUG, TAG, "OUT");
600 CAResult_t CARetransmissionStop(CARetransmission_t *context)
604 OIC_LOG(ERROR, TAG, "context is empty..");
605 return CA_STATUS_INVALID_PARAM;
608 OIC_LOG(DEBUG, TAG, "retransmission stop request!!");
611 ca_mutex_lock(context->threadMutex);
614 context->isStop = true;
617 ca_cond_signal(context->threadCond);
619 ca_cond_wait(context->threadCond, context->threadMutex);
622 ca_mutex_unlock(context->threadMutex);
627 CAResult_t CARetransmissionDestroy(CARetransmission_t *context)
631 OIC_LOG(ERROR, TAG, "context is empty..");
632 return CA_STATUS_INVALID_PARAM;
635 OIC_LOG(DEBUG, TAG, "retransmission context destroy..");
637 ca_mutex_free(context->threadMutex);
638 context->threadMutex = NULL;
639 ca_cond_free(context->threadCond);
640 u_arraylist_free(&context->dataList);
645 uint64_t getCurrentTimeInMicroSeconds()
647 OIC_LOG(DEBUG, TAG, "IN");
648 uint64_t currentTime = 0;
651 struct timespec getTs;
653 clock_gettime(CLOCK_MONOTONIC, &getTs);
655 currentTime = (getTs.tv_sec * (uint64_t)1000000000 + getTs.tv_nsec)/1000;
656 OIC_LOG_V(DEBUG, TAG, "current time = %lld", currentTime);
657 #elif defined __ARDUINO__
658 currentTime = millis() * 1000;
659 OIC_LOG_V(DEBUG, TAG, "currtime=%lu", currentTime);
661 #if _POSIX_TIMERS > 0
663 clock_gettime(CLOCK_MONOTONIC, &ts);
664 currentTime = ts.tv_sec * USECS_PER_SEC + ts.tv_nsec / 1000;
665 #elif defined(_WIN32)
668 currentTime = tb.time * USECS_PER_SEC + tb.millitm * MSECS_PER_SEC;
671 gettimeofday(&tv, NULL);
672 currentTime = tv.tv_sec * USECS_PER_SEC + tv.tv_usec;
676 OIC_LOG(DEBUG, TAG, "OUT");