Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / caretransmission.h
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file caretransmission.h
23  * @brief This file contains common function for retransmission messages.
24  */
25
26 #ifndef CA_RETRANSMISSION_H_
27 #define CA_RETRANSMISSION_H_
28
29 #include <stdint.h>
30
31 #include "cathreadpool.h"
32 #include "camutex.h"
33 #include "uarraylist.h"
34 #include "cacommon.h"
35
36 /** CA_IPV4, CA_EDR, CA_LE **/
37 #define DEFAULT_RETRANSMISSION_TYPE     (CA_IPV4 | CA_EDR | CA_LE)
38
39 /** default ACK time is 2 sec.(CoAP) **/
40 #define DEFAULT_ACK_TIMEOUT_SEC     2
41
42 /** default max retransmission trying count is 4.(CoAP) **/
43 #define DEFAULT_MAX_RETRANSMIT      4
44
45 /** check period is 1 sec. **/
46 #define RETRANSMISSION_CHECK_PERIOD_SEC     1
47
48 /** retransmission data send method type**/
49 typedef CAResult_t (*CADataSendMethod_t)(const CARemoteEndpoint_t *endpoint, const void *pdu,
50                                          uint32_t size);
51
52 /** retransmission timeout callback type**/
53 typedef void (*CATimeoutCallback_t)(const CARemoteEndpoint_t *endpoint, const void *pdu,
54                                     uint32_t size);
55
56 typedef struct
57 {
58     /** retransmission support transport type **/
59     CATransportType_t supportType;
60
61     /** retransmission trying count **/
62     uint8_t tryingCount;
63
64 } CARetransmissionConfig_t;
65
66 typedef struct
67 {
68     /** Thread pool of the thread started **/
69     ca_thread_pool_t threadPool;
70
71     /** mutex for synchronization **/
72     ca_mutex threadMutex;
73
74     /** conditional mutex for synchronization **/
75     ca_cond threadCond;
76
77     /** send method for retransmission data **/
78     CADataSendMethod_t dataSendMethod;
79
80     /** callback function for retransmit timeout **/
81     CATimeoutCallback_t timeoutCallback;
82
83     /** retransmission configure data **/
84     CARetransmissionConfig_t config;
85
86     /** Variable to inform the thread to stop **/
87     bool isStop;
88
89     /** array list on which the thread is operating. **/
90     u_arraylist_t *dataList;
91
92 } CARetransmission_t;
93
94 #ifdef __cplusplus
95 extern "C"
96 {
97 #endif
98
99 /**
100  * @brief   Initializes the retransmission context
101  * @param   context                     [IN] context for retransmission
102  * @param   handle                      [IN] thread pool handle
103  * @param   retransmissionSendMethod    [IN] function to be called for retransmission
104  * @param   timeoutCallback             [IN] callback for retransmit timeout
105  * @param   config                      [IN] configuration for retransmission.
106  *                                           if NULL is coming, it will set default values.
107  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
108  */
109 CAResult_t CARetransmissionInitialize(CARetransmission_t *context, ca_thread_pool_t handle,
110                                       CADataSendMethod_t retransmissionSendMethod,
111                                       CATimeoutCallback_t timeoutCallback,
112                                       CARetransmissionConfig_t* config);
113
114 /**
115  * @brief   Starting the retransmission context
116  * @param   context     [IN] context for retransmission
117  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
118  */
119 CAResult_t CARetransmissionStart(CARetransmission_t *context);
120
121 /**
122  * @brief   Pass the sent pdu data. if retransmission process need, internal thread will wake up and
123  *          process the retransmission data
124  * @param   context     [IN] context for retransmission
125  * @param   endpoint    [IN] endpoint information
126  * @param   pdu         [IN] sent pdu binary data
127  * @param   size        [IN] sent pdu binary data size
128  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
129  */
130 CAResult_t CARetransmissionSentData(CARetransmission_t* context,
131                                     const CARemoteEndpoint_t* endpoint, const void* pdu,
132                                     uint32_t size);
133
134 /**
135  * @brief   Pass the received pdu data. if received pdu is ACK data for the retransmission CON data,
136  *          the specified CON data will remove on retransmission list.
137  * @param   context             [IN] context for retransmission
138  * @param   endpoint            [IN] endpoint information
139  * @param   pdu                 [IN] received pdu binary data
140  * @param   size                [IN] received pdu binary data size
141  * @param   retransmissionPdu   [OUT] pdu data of the request for reset and ack
142  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
143  */
144 CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
145                                         const CARemoteEndpoint_t *endpoint, const void *pdu,
146                                         uint32_t size, void **retransmissionPdu);
147
148 /**
149  * @brief   Stopping the retransmission context
150  * @param   context     [IN] context for retransmission
151  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
152  */
153 CAResult_t CARetransmissionStop(CARetransmission_t *context);
154
155 /**
156  * @brief   Terminating the retransmission context
157  * @param   context     [IN] context for retransmission
158  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
159  */
160 CAResult_t CARetransmissionDestroy(CARetransmission_t *context);
161
162 #ifdef __cplusplus
163 } /* extern "C" */
164 #endif
165
166 #endif  /* CA_RETRANSMISSION_H_ */