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 ******************************************************************/
20 #include "caretransmission_singlethread.h"
26 #include "caremotehandler.h"
27 #include "caprotocolmessage.h"
28 #include "oic_malloc.h"
39 /** last sent time. microseconds **/
42 /** retransmission count **/
45 /** coap PDU message id **/
48 /** remote endpoint **/
49 CARemoteEndpoint_t *endpoint;
57 } CARetransmissionData_t;
59 static CARetransmission_t *g_retransmissionPtr = NULL;
62 * getCurrent monotonic time.
64 * @return current time in microseconds.
66 uint64_t getCurrentTimeInMicroSeconds();
69 * @brief check timeout routine
70 * @param currentTime [IN]microseconds
71 * @param timeStamp [IN]microseconds
72 * @param triedCount [IN]Number of retransmission tried.
73 * @return true if the timeout period has elapsed, false otherwise.
75 static bool CACheckTimeout(uint64_t currentTime, uint64_t timeStamp, uint8_t triedCount)
77 OIC_LOG(DEBUG, TAG, "IN");
78 // #1. calculate timeout
79 uint64_t timeOut = (2 << triedCount) * 1000000;
81 if (currentTime >= timeStamp + timeOut)
83 OIC_LOG_V(DEBUG, TAG, "timeout=%d, tried cnt=%d", (2 << triedCount), triedCount);
87 OIC_LOG(DEBUG, TAG, "OUT");
91 void CACheckRetransmissionList()
93 uint32_t len = u_arraylist_length(g_retransmissionPtr->dataList);
95 OIC_LOG_V(DEBUG, TAG, "len=%d", len);
96 for (uint32_t i = 0; i < len; i++)
98 CARetransmissionData_t *retData =
99 (CARetransmissionData_t *) u_arraylist_get(g_retransmissionPtr->dataList, i);
106 uint64_t currentTime = getCurrentTimeInMicroSeconds();
108 OIC_LOG_V(DEBUG, TAG, "currtime=%lu", currentTime);
109 if (CACheckTimeout(currentTime, retData->timeStamp, retData->triedCount))
112 OIC_LOG(DEBUG, TAG, "RTdata-Success");
113 // #2. if time's up, send the data.
114 if (NULL != g_retransmissionPtr->dataSendMethod)
116 OIC_LOG_V(DEBUG, TAG, "retry CON data-msgid=%d", retData->messageId);
117 g_retransmissionPtr->dataSendMethod(retData->endpoint, retData->pdu, retData->size);
120 // #3. increase the retransmission count and update timestamp.
121 retData->timeStamp = currentTime;
122 retData->triedCount++;
125 // #4. if tried count is max, remove the retransmission data from list.
126 if (retData->triedCount >= g_retransmissionPtr->config.tryingCount)
128 CARetransmissionData_t *removedData = (CARetransmissionData_t *) u_arraylist_remove(
129 g_retransmissionPtr->dataList, i);
130 if (NULL == removedData)
132 OIC_LOG(ERROR, TAG, "Removed data is NULL");
136 OIC_LOG(DEBUG, TAG, "max trycount rchd");
138 OIC_LOG_V(DEBUG, TAG, "max trycount, remove retransmission CON data!!, messageid=%d",
139 removedData->messageId);
141 // callback for retransmit timeout
142 if (NULL != g_retransmissionPtr->timeoutCallback)
144 g_retransmissionPtr->timeoutCallback(removedData->endpoint, removedData->pdu,
148 CADestroyRemoteEndpointInternal(removedData->endpoint);
149 OICFree(removedData->pdu);
151 OICFree(removedData);
153 // modify loop value.
154 len = u_arraylist_length(g_retransmissionPtr->dataList);
160 void CARetransmissionBaseRoutine(void *threadValue)
162 CARetransmission_t *context = (CARetransmission_t *) threadValue;
166 OIC_LOG(ERROR, TAG, "cnxt null");
170 if (true == context->isStop)
172 OIC_LOG(DEBUG, TAG, "thread stopped");
175 g_retransmissionPtr = context;
176 CACheckRetransmissionList();
179 CAResult_t CARetransmissionInitialize(CARetransmission_t *context,
180 CADataSendMethod_t retransmissionSendMethod,
181 CATimeoutCallback_t timeoutCallback,
182 CARetransmissionConfig_t *config)
184 OIC_LOG(DEBUG, TAG, "IN");
187 OIC_LOG(ERROR, TAG, "cnxt null");
188 return CA_STATUS_INVALID_PARAM;
191 memset(context, 0, sizeof(CARetransmission_t));
193 CARetransmissionConfig_t cfg = {};
198 cfg.supportType = (CATransportType_t) DEFAULT_RETRANSMISSION_TYPE;
199 cfg.tryingCount = DEFAULT_RETRANSMISSION_COUNT;
206 // set send thread data
207 context->dataSendMethod = retransmissionSendMethod;
208 context->timeoutCallback = timeoutCallback;
209 context->config = cfg;
210 context->isStop = false;
211 context->dataList = u_arraylist_create();
213 // Enable TimedAction for CACheckRetransmissionList API
214 g_retransmissionPtr = context;
215 OIC_LOG(DEBUG, TAG, "OUT");
219 CAResult_t CARetransmissionSentData(CARetransmission_t *context, const CARemoteEndpoint_t *endpoint,
220 const void *pdu, uint32_t size)
222 OIC_LOG(DEBUG, TAG, "IN");
223 if (NULL == context || NULL == endpoint || NULL == pdu)
225 OIC_LOG(ERROR, TAG, "error");
226 return CA_STATUS_INVALID_PARAM;
229 // #0. check support connectivity type
230 if (!(context->config.supportType & endpoint->transportType))
232 OIC_LOG(ERROR, TAG, "error");
233 OIC_LOG_V(ERROR, TAG, "not supported conntype=%d", endpoint->transportType);
234 return CA_NOT_SUPPORTED;
237 // #1. check PDU method type and get message id.
238 CAMessageType_t type = CAGetMessageTypeFromPduBinaryData(pdu, size);
239 uint16_t messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
241 OIC_LOG_V(DEBUG, TAG, "sent pdu, msgtype=%d,msgid=%d", type, messageId);
243 if (CA_MSG_CONFIRM != type)
245 OIC_LOG(DEBUG, TAG, "not supported message type");
246 return CA_NOT_SUPPORTED;
249 // create retransmission data
250 CARetransmissionData_t *retData = (CARetransmissionData_t *) OICCalloc(
251 1, sizeof(CARetransmissionData_t));
255 OIC_LOG(ERROR, TAG, "error");
256 return CA_MEMORY_ALLOC_FAILED;
260 void *pduData = (void *) OICMalloc(size);
264 OIC_LOG(ERROR, TAG, "error");
265 return CA_MEMORY_ALLOC_FAILED;
267 memcpy(pduData, pdu, size);
269 // clone remote endpoint
270 CARemoteEndpoint_t *remoteEndpoint = CACloneRemoteEndpoint(endpoint);
271 if (NULL == remoteEndpoint)
275 OIC_LOG(ERROR, TAG, "error");
276 return CA_MEMORY_ALLOC_FAILED;
279 // #2. add additional information. (time stamp, retransmission count...)
280 retData->timeStamp = getCurrentTimeInMicroSeconds();
281 retData->triedCount = 0;
282 retData->messageId = messageId;
283 retData->endpoint = remoteEndpoint;
284 retData->pdu = pduData;
285 retData->size = size;
287 // #3. add data into list
288 u_arraylist_add(context->dataList, (void *) retData);
290 // #4. Initiate Re-transmission for added entry
291 g_retransmissionPtr = context;
292 CACheckRetransmissionList();
293 OIC_LOG(DEBUG, TAG, "OUT");
297 CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
298 const CARemoteEndpoint_t *endpoint, const void *pdu,
299 uint32_t size, void **retransmissionPdu)
301 OIC_LOG(DEBUG, TAG, "IN");
302 if (NULL == context || NULL == endpoint || NULL == pdu || NULL == retransmissionPdu)
304 OIC_LOG(ERROR, TAG, "error");
305 return CA_STATUS_INVALID_PARAM;
308 // #0. check support connectivity type
309 if (!(context->config.supportType & endpoint->transportType))
311 OIC_LOG_V(DEBUG, TAG, "not supp conntype=%d", endpoint->transportType);
315 // #1. check PDU method type and get message id.
316 // ACK, RST --> remove the CON data
317 CAMessageType_t type = CAGetMessageTypeFromPduBinaryData(pdu, size);
318 uint16_t messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
320 OIC_LOG_V(DEBUG, TAG, "recv pdu, msgtype=%d,msgid=%d", type, messageId);
322 if (CA_MSG_ACKNOWLEDGE != type && CA_MSG_RESET != type)
327 uint32_t len = u_arraylist_length(context->dataList);
330 for (uint32_t i = 0; i < len; i++)
332 CARetransmissionData_t *retData = (CARetransmissionData_t *) u_arraylist_get(
333 context->dataList, i);
341 if (NULL != retData->endpoint && retData->messageId == messageId
342 && (retData->endpoint->transportType == endpoint->transportType))
344 // get pdu data for getting token when CA_EMPTY(RST/ACK) is received from remote device
345 // if retransmission was finish..token will be unavailable.
346 if (CA_EMPTY == CAGetCodeFromPduBinaryData(pdu, size))
348 OIC_LOG(DEBUG, TAG, "CA_EMPTY");
350 if (NULL == retData->pdu)
352 OIC_LOG(ERROR, TAG, "retData->pdu is null");
354 return CA_STATUS_FAILED;
358 (*retransmissionPdu) = (void *) OICCalloc(1, retData->size);
359 if (NULL == (*retransmissionPdu))
362 OIC_LOG(ERROR, TAG, "error");
363 return CA_MEMORY_ALLOC_FAILED;
365 memcpy((*retransmissionPdu), retData->pdu, retData->size);
368 // #2. remove data from list
369 CARetransmissionData_t *removedData = (CARetransmissionData_t *) u_arraylist_remove(
370 context->dataList, i);
371 if (NULL == removedData)
373 OIC_LOG(ERROR, TAG, "Removed data is NULL");
374 return CA_STATUS_FAILED;
377 OIC_LOG_V(DEBUG, TAG, "remove RTCON data, msgid=%d", messageId);
379 CADestroyRemoteEndpointInternal(removedData->endpoint);
380 OICFree(removedData->pdu);
382 OICFree(removedData);
388 OIC_LOG(DEBUG, TAG, "OUT");
392 CAResult_t CARetransmissionStop(CARetransmission_t *context)
394 OIC_LOG(DEBUG, TAG, "IN");
397 OIC_LOG(ERROR, TAG, "error");
398 return CA_STATUS_INVALID_PARAM;
402 context->isStop = true;
403 OIC_LOG(DEBUG, TAG, "OUT");
407 CAResult_t CARetransmissionDestroy(CARetransmission_t *context)
409 OIC_LOG(DEBUG, TAG, "IN");
412 OIC_LOG(ERROR, TAG, "error");
413 return CA_STATUS_INVALID_PARAM;
416 u_arraylist_free(&context->dataList);
417 OIC_LOG(DEBUG, TAG, "OUT");
421 uint64_t getCurrentTimeInMicroSeconds()
423 OIC_LOG(DEBUG, TAG, "IN");
424 uint64_t currentTime = 0;
427 currentTime = millis() * 1000;
429 OIC_LOG_V(DEBUG, TAG, "currtime=%lu", currentTime);
432 gettimeofday(&tv, NULL);
433 currentTime = tv.tv_sec * USECS_PER_SEC + tv.tv_usec;
436 OIC_LOG(DEBUG, TAG, "OUT");