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