replace : iotivity -> iotivity-sec
[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
23  * 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 "octhread.h"
33 #include "uarraylist.h"
34 #include "cacommon.h"
35
36 /** IP, EDR, LE. **/
37 #define DEFAULT_RETRANSMISSION_TYPE (CA_ADAPTER_IP | \
38                                      CA_ADAPTER_RFCOMM_BTEDR | \
39                                      CA_ADAPTER_GATT_BTLE)
40
41 /** default ACK time is 2 sec(CoAP). **/
42 #define DEFAULT_ACK_TIMEOUT_SEC     2
43
44 /** default max retransmission trying count is 4(CoAP). **/
45 #define DEFAULT_RETRANSMISSION_COUNT      4
46
47 /** check period is 1 sec. **/
48 #define RETRANSMISSION_CHECK_PERIOD_SEC     1
49
50 /** retransmission data send method type. **/
51 typedef CAResult_t (*CADataSendMethod_t)(const CAEndpoint_t *endpoint,
52                                          const void *pdu,
53                                          uint32_t size,
54                                          CADataType_t dataType);
55
56 /** retransmission timeout callback type. **/
57 typedef void (*CATimeoutCallback_t)(const CAEndpoint_t *endpoint,
58                                     const void *pdu,
59                                     uint32_t size);
60
61 typedef struct
62 {
63     /** retransmission support transport type. **/
64     CATransportAdapter_t supportType;
65
66     /** retransmission trying count. **/
67     uint8_t tryingCount;
68
69 } CARetransmissionConfig_t;
70
71 typedef struct
72 {
73     /** Thread pool of the thread started. **/
74     ca_thread_pool_t threadPool;
75
76     /** mutex for synchronization. **/
77     oc_mutex threadMutex;
78
79     /** conditional mutex for synchronization. **/
80     oc_cond threadCond;
81
82     /** send method for retransmission data. **/
83     CADataSendMethod_t dataSendMethod;
84
85     /** callback function for retransmit timeout. **/
86     CATimeoutCallback_t timeoutCallback;
87
88     /** retransmission configure data. **/
89     CARetransmissionConfig_t config;
90
91     /** Variable to inform the thread to stop. **/
92     bool isStop;
93
94     /** array list on which the thread is operating. **/
95     u_arraylist_t *dataList;
96
97 } CARetransmission_t;
98
99 #ifdef __cplusplus
100 extern "C"
101 {
102 #endif
103
104 /**
105  * Initializes the retransmission context.
106  * @param[in]   context                      context for retransmission.
107  * @param[in]   handle                       thread pool handle.
108  * @param[in]   retransmissionSendMethod     function to be called for retransmission.
109  * @param[in]   timeoutCallback              callback for retransmit timeout.
110  * @param[in]   config                       configuration for retransmission.
111  *                                           if NULL is coming, it will set default values.
112  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
113  */
114 CAResult_t CARetransmissionInitialize(CARetransmission_t *context,
115                                       ca_thread_pool_t handle,
116                                       CADataSendMethod_t retransmissionSendMethod,
117                                       CATimeoutCallback_t timeoutCallback,
118                                       CARetransmissionConfig_t* config);
119
120 /**
121  * Starting the retransmission context.
122  * @param[in]   context      context for retransmission.
123  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
124  */
125 CAResult_t CARetransmissionStart(CARetransmission_t *context);
126
127 /**
128  * Pass the sent pdu data. if retransmission process need, internal thread will wake up and
129  * process the retransmission data.
130  * @param[in]   context      context for retransmission.
131  * @param[in]   endpoint     endpoint information.
132  * @param[in]   dataType     Data type which is REQUEST or RESPONSE.
133  * @param[in]   pdu          sent pdu binary data.
134  * @param[in]   size         sent pdu binary data size.
135  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
136  */
137 CAResult_t CARetransmissionSentData(CARetransmission_t *context,
138                                     const CAEndpoint_t *endpoint,
139                                     CADataType_t dataType,
140                                     const void *pdu, uint32_t size);
141
142 /**
143  * Pass the received pdu data. if received pdu is ACK data for the retransmission CON data,
144  * the specified CON data will remove on retransmission list.
145  * @param[in]   context              context for retransmission.
146  * @param[in]   endpoint             endpoint information.
147  * @param[in]   pdu                  received pdu binary data.
148  * @param[in]   size                 received pdu binary data size.
149  * @param[out]  retransmissionPdu    pdu data of the request for reset and ack.
150  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
151  */
152 CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
153                                         const CAEndpoint_t *endpoint, const void *pdu,
154                                         uint32_t size, void **retransmissionPdu);
155
156 /**
157  * Clears queue data.
158  * @param[in]   context              context for retransmission.
159  * @param[in]   type                 Adapter type.
160  * @return  ::CA_STATUS_OK or Appropriate error code.
161  */
162 CAResult_t CARetransmissionClearAdapterData(CARetransmission_t *context, CATransportAdapter_t type);
163
164 /**
165  * Stopping the retransmission context.
166  * @param[in]   context         context for retransmission.
167  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
168  */
169 CAResult_t CARetransmissionStop(CARetransmission_t *context);
170
171 /**
172  * Terminating the retransmission context.
173  * @param[in]   context         context for retransmission.
174  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
175  */
176 CAResult_t CARetransmissionDestroy(CARetransmission_t *context);
177
178 /**
179  * Invoke Retransmission according to TimedAction Response.
180  * @param[in]   threadValue     context for retransmission.
181  */
182 void CARetransmissionBaseRoutine(void *threadValue);
183
184 #ifdef __cplusplus
185 } /* extern "C" */
186 #endif
187
188 #endif  /* CA_RETRANSMISSION_H_ */