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 ******************************************************************/
28 #include <linux/time.h>
31 #include "caretransmission.h"
32 #include "caremotehandler.h"
33 #include "caprotocolmessage.h"
34 #include "oic_malloc.h"
37 #define TAG PCF("RET")
41 /** last sent time. microseconds **/
43 /** timeout value. milliseconds **/
45 /** retransmission count **/
47 /** coap PDU message id **/
49 /** remote endpoint **/
50 CARemoteEndpoint_t *endpoint;
55 } CARetransmissionData_t;
58 * getCurrent monotonic time
62 uint64_t getCurrentTimeInMicroSeconds();
65 * @brief check timeout routine
66 * @param currentTime [IN]microseconds
67 * @param timeStamp [IN]microseconds
68 * @param timeoutValue [IN]milliseconds
69 * @param triedCount [IN]
70 * @return CA_TRUE(timeout) or CA_FALSE
74 static CABool_t CACheckTimeout(uint64_t currentTime, uint64_t timeStamp, uint32_t timeoutValue,
77 // #1. calculate timeout
78 uint64_t timeout = (timeoutValue << triedCount) * (uint64_t) 1000;
80 if (currentTime >= timeStamp + timeout)
82 OIC_LOG_V(DEBUG, TAG, "%d milliseconds time out!!, tried count(%d)",
83 (timeoutValue << triedCount), triedCount);
91 * @brief timeout value is
92 * between DEFAULT_ACK_TIMEOUT and (DEFAULT_ACK_TIMEOUT * DEFAULT_RANDOM_FACTOR) second.
93 * DEFAULT_RANDOM_FACTOR 1.5 (CoAP)
94 * @return milliseconds.
96 static uint32_t CAGetTimeoutValue()
98 return (DEFAULT_ACK_TIMEOUT * 1000) + ((1000 * (rand() & 0xFF)) >> 8);
101 static void CACheckRetransmissionList(CARetransmission_t *context)
104 u_mutex_lock(context->threadMutex);
105 uint64_t currentTime = 0;
108 uint32_t len = u_arraylist_length(context->dataList);
110 for (i = 0; i < len; i++)
112 CARetransmissionData_t *retData = u_arraylist_get(context->dataList, i);
117 currentTime = getCurrentTimeInMicroSeconds();
119 if (CACheckTimeout(currentTime, retData->timeStamp, retData->timeout, retData->triedCount))
121 // #2. if time's up, send the data.
122 if (context->dataSendMethod != NULL)
124 OIC_LOG_V(DEBUG, TAG, "retransmission CON data!!, message id(%d)",
126 context->dataSendMethod(retData->endpoint, retData->pdu, retData->size);
129 // #3. increase the retransmission count and update timestamp.
130 retData->timeStamp = currentTime;
131 retData->triedCount++;
134 // #4. if tried count is max, remove the retransmission data from list.
135 if (retData->triedCount >= context->config.tryingCount)
137 CARetransmissionData_t *removedData = u_arraylist_remove(context->dataList, i);
139 OIC_LOG_V(DEBUG, TAG, "max trying count, remove retransmission CON data!!,\
140 message id(%d)", removedData->messageId);
142 // callback for retransmit timeout
143 if (context->timeoutCallback != NULL)
145 context->timeoutCallback(removedData->endpoint, removedData->pdu,
149 CADestroyRemoteEndpointInternal(removedData->endpoint);
150 OICFree(removedData->pdu);
152 OICFree(removedData);
154 // modify loop value.
155 len = u_arraylist_length(context->dataList);
161 u_mutex_unlock(context->threadMutex);
164 static void CARetransmissionBaseRoutine(void *threadValue)
166 OIC_LOG_V(DEBUG, TAG, "retransmission main thread start..");
168 CARetransmission_t *context = (CARetransmission_t *) threadValue;
172 OIC_LOG_V(DEBUG, TAG, "thread data passing error!!");
177 while (!context->isStop)
180 u_mutex_lock(context->threadMutex);
182 if (u_arraylist_length(context->dataList) <= 0)
184 // if list is empty, thread will wait
185 OIC_LOG_V(DEBUG, TAG, "wait..there is no retransmission data.");
188 u_cond_wait(context->threadCond, context->threadMutex);
190 OIC_LOG_V(DEBUG, TAG, "wake up..");
194 // check each RETRANSMISSION_CHECK_PERIOD time.
195 OIC_LOG_V(DEBUG, TAG, "wait..(%d)microseconds", RETRANSMISSION_CHECK_PERIOD);
198 u_cond_timed_wait(context->threadCond, context->threadMutex,
199 RETRANSMISSION_CHECK_PERIOD);
203 u_mutex_unlock(context->threadMutex);
209 CACheckRetransmissionList(context);
212 u_cond_signal(context->threadCond);
214 OIC_LOG_V(DEBUG, TAG, "retransmission main thread end..");
218 CAResult_t CARetransmissionInitialize(CARetransmission_t *context, u_thread_pool_t handle,
219 CADataSendMethod_t retransmissionSendMethod, CATimeoutCallback_t timeoutCallback,
220 CARetransmissionConfig_t* config)
224 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
225 return CA_STATUS_FAILED;
230 OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
231 return CA_STATUS_FAILED;
234 OIC_LOG_V(DEBUG, TAG, "thread initialize..");
236 memset(context, 0, sizeof(CARetransmission_t));
238 CARetransmissionConfig_t cfg;
239 memset(&cfg, 0, sizeof(CARetransmissionConfig_t));
244 cfg.supportType = DEFAULT_RETRANSMISSION_TYPE;
245 cfg.tryingCount = DEFAULT_MAX_RETRANSMIT;
255 // set send thread data
256 context->threadPool = handle;
257 context->threadMutex = u_mutex_new();
258 context->threadCond = u_cond_new();
259 context->dataSendMethod = retransmissionSendMethod;
260 context->timeoutCallback = timeoutCallback;
261 context->config = cfg;
262 context->isStop = CA_FALSE;
263 context->dataList = u_arraylist_create();
268 CAResult_t CARetransmissionStart(CARetransmission_t *context)
272 OIC_LOG_V(DEBUG, TAG, "context is empty..");
273 return CA_STATUS_FAILED;
276 if (context->threadPool == NULL)
278 OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
279 return CA_STATUS_FAILED;
282 CAResult_t res = u_thread_pool_add_task(context->threadPool, CARetransmissionBaseRoutine,
285 if (res != CA_STATUS_OK)
287 OIC_LOG_V(DEBUG, TAG, "thread pool add task error(send thread).");
294 CAResult_t CARetransmissionSentData(CARetransmission_t *context,
295 const CARemoteEndpoint_t* endpoint,const void* pdu, uint32_t size)
297 if (context == NULL || endpoint == NULL || pdu == NULL)
299 OIC_LOG_V(DEBUG, TAG, "invalid parameter..");
300 return CA_STATUS_INVALID_PARAM;
303 // #0. check support connectivity type
304 if (!(context->config.supportType & endpoint->connectivityType))
306 OIC_LOG_V(DEBUG, TAG, "not supported connectivity type for retransmission..(%d)",
307 endpoint->connectivityType);
311 // #1. check PDU method type and get message id.
312 CAMessageType_t type = CAGetMessageTypeFromPduBinaryData(pdu, size);
313 uint16_t messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
315 OIC_LOG_V(DEBUG, TAG, "sent pdu, message type(%d), message id(%d)", type, messageId);
317 if (type != CA_MSG_CONFIRM)
322 // create retransmission data
323 CARetransmissionData_t *retData = (CARetransmissionData_t *) OICMalloc(
324 sizeof(CARetransmissionData_t));
328 OIC_LOG_V(DEBUG, TAG, "memory error!!");
329 return CA_MEMORY_ALLOC_FAILED;
331 memset(retData, 0, sizeof(CARetransmissionData_t));
334 void *pduData = (void *) OICMalloc(sizeof(int8_t) * size);
338 OIC_LOG_V(DEBUG, TAG, "memory error!!");
339 return CA_MEMORY_ALLOC_FAILED;
341 memset(pduData, 0, sizeof(int8_t) * size);
342 memcpy(pduData, pdu, sizeof(int8_t) * size);
344 // clone remote endpoint
345 CARemoteEndpoint_t *remoteEndpoint = CACloneRemoteEndpoint(endpoint);
346 if (remoteEndpoint == NULL)
350 OIC_LOG_V(DEBUG, TAG, "memory error!!");
351 return CA_MEMORY_ALLOC_FAILED;
354 // #2. add additional information. (time stamp, retransmission count...)
355 retData->timeStamp = getCurrentTimeInMicroSeconds();
356 retData->timeout = CAGetTimeoutValue();
357 retData->triedCount = 0;
358 retData->messageId = messageId;
359 retData->endpoint = remoteEndpoint;
360 retData->pdu = pduData;
361 retData->size = size;
364 u_mutex_lock(context->threadMutex);
366 // #3. add data into list
367 u_arraylist_add(context->dataList, (void *) retData);
370 u_cond_signal(context->threadCond);
373 u_mutex_unlock(context->threadMutex);
378 CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
379 const CARemoteEndpoint_t *endpoint, const void *pdu, uint32_t size)
381 if (context == NULL || endpoint == NULL || pdu == NULL)
383 OIC_LOG_V(DEBUG, TAG, "invalid parameter..");
384 return CA_STATUS_INVALID_PARAM;
387 // #0. check support connectivity type
388 if (!(context->config.supportType & endpoint->connectivityType))
390 OIC_LOG_V(DEBUG, TAG, "not supported connectivity type for retransmission..(%d)",
391 endpoint->connectivityType);
395 // #1. check PDU method type and get message id.
396 // ACK, RST --> remove the CON data
397 CAMessageType_t type = CAGetMessageTypeFromPduBinaryData(pdu, size);
398 uint16_t messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
400 OIC_LOG_V(DEBUG, TAG, "received pdu, message type(%d), message id(%d)", type, messageId);
402 if (type != CA_MSG_ACKNOWLEDGE && type != CA_MSG_RESET)
408 u_mutex_lock(context->threadMutex);
411 uint32_t len = u_arraylist_length(context->dataList);
414 for (i = 0; i < len; i++)
416 CARetransmissionData_t *retData = u_arraylist_get(context->dataList, i);
422 if ((retData->endpoint->connectivityType == endpoint->connectivityType)
423 && retData->messageId == messageId)
427 // #2. remove data from list
430 CARetransmissionData_t *removedData = u_arraylist_remove(context->dataList, i);
432 OIC_LOG_V(DEBUG, TAG, "remove retransmission CON data!!, message id(%d)", messageId);
434 CADestroyRemoteEndpointInternal(removedData->endpoint);
435 OICFree(removedData->pdu);
437 OICFree(removedData);
441 u_mutex_unlock(context->threadMutex);
446 CAResult_t CARetransmissionStop(CARetransmission_t *context)
450 OIC_LOG_V(DEBUG, TAG, "context is empty..");
451 return CA_STATUS_FAILED;
454 OIC_LOG_V(DEBUG, TAG, "retransmission stop request!!");
457 u_mutex_lock(context->threadMutex);
460 context->isStop = CA_TRUE;
463 u_cond_signal(context->threadCond);
465 u_cond_wait(context->threadCond, context->threadMutex);
468 u_mutex_unlock(context->threadMutex);
473 CAResult_t CARetransmissionDestroy(CARetransmission_t *context)
477 OIC_LOG_V(DEBUG, TAG, "context is empty..");
478 return CA_STATUS_FAILED;
481 OIC_LOG_V(DEBUG, TAG, "retransmission context destroy..");
483 u_mutex_free(context->threadMutex);
484 context->threadMutex = NULL;
485 u_cond_free(context->threadCond);
486 u_arraylist_free(context->dataList);
491 uint64_t getCurrentTimeInMicroSeconds()
493 uint64_t currentTime = 0;
496 struct timespec getTs;
498 memset(&getTs, 0, sizeof(getTs));
499 clock_gettime(CLOCK_MONOTONIC, &getTs);
501 currentTime = (getTs.tv_sec * (uint64_t)1000000000 + getTs.tv_nsec)/1000;
502 OIC_LOG_V(DEBUG, TAG, "current time = %d", currentTime);
504 currentTime = g_get_monotonic_time();