Merge branch 'master' into easysetup and fixed OCStack issue
[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 /** 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_MAX_RETRANSMIT      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
55 /** retransmission timeout callback type**/
56 typedef void (*CATimeoutCallback_t)(const CAEndpoint_t *endpoint,
57                                     const void *pdu,
58                                     uint32_t size);
59
60 typedef struct
61 {
62     /** retransmission support transport type **/
63     CATransportAdapter_t supportType;
64
65     /** retransmission trying count **/
66     uint8_t tryingCount;
67
68 } CARetransmissionConfig_t;
69
70 typedef struct
71 {
72     /** Thread pool of the thread started **/
73     ca_thread_pool_t threadPool;
74
75     /** mutex for synchronization **/
76     ca_mutex threadMutex;
77
78     /** conditional mutex for synchronization **/
79     ca_cond threadCond;
80
81     /** send method for retransmission data **/
82     CADataSendMethod_t dataSendMethod;
83
84     /** callback function for retransmit timeout **/
85     CATimeoutCallback_t timeoutCallback;
86
87     /** retransmission configure data **/
88     CARetransmissionConfig_t config;
89
90     /** Variable to inform the thread to stop **/
91     bool isStop;
92
93     /** array list on which the thread is operating. **/
94     u_arraylist_t *dataList;
95
96 } CARetransmission_t;
97
98 #ifdef __cplusplus
99 extern "C"
100 {
101 #endif
102
103 /**
104  * @brief   Initializes the retransmission context
105  * @param   context                     [IN] context for retransmission
106  * @param   handle                      [IN] thread pool handle
107  * @param   retransmissionSendMethod    [IN] function to be called for retransmission
108  * @param   timeoutCallback             [IN] callback for retransmit timeout
109  * @param   config                      [IN] configuration for retransmission.
110  *                                           if NULL is coming, it will set default values.
111  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
112  */
113 CAResult_t CARetransmissionInitialize(CARetransmission_t *context, ca_thread_pool_t handle,
114                                       CADataSendMethod_t retransmissionSendMethod,
115                                       CATimeoutCallback_t timeoutCallback,
116                                       CARetransmissionConfig_t* config);
117
118 /**
119  * @brief   Starting the retransmission context
120  * @param   context     [IN] context for retransmission
121  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
122  */
123 CAResult_t CARetransmissionStart(CARetransmission_t *context);
124
125 /**
126  * @brief   Pass the sent pdu data. if retransmission process need, internal thread will wake up and
127  *          process the retransmission data
128  * @param   context     [IN] context for retransmission
129  * @param   endpoint    [IN] endpoint information
130  * @param   pdu         [IN] sent pdu binary data
131  * @param   size        [IN] sent pdu binary data size
132  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
133  */
134 CAResult_t CARetransmissionSentData(CARetransmission_t* context,
135                                     const CAEndpoint_t* endpoint,
136                                     const void* pdu, uint32_t size);
137
138 /**
139  * @brief   Pass the received pdu data. if received pdu is ACK data for the retransmission CON data,
140  *          the specified CON data will remove on retransmission list.
141  * @param   context             [IN] context for retransmission
142  * @param   endpoint            [IN] endpoint information
143  * @param   pdu                 [IN] received pdu binary data
144  * @param   size                [IN] received pdu binary data size
145  * @param   retransmissionPdu   [OUT] pdu data of the request for reset and ack
146  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
147  */
148 CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
149                                         const CAEndpoint_t *endpoint, const void *pdu,
150                                         uint32_t size, void **retransmissionPdu);
151
152 /**
153  * @brief   Stopping the retransmission context
154  * @param   context     [IN] context for retransmission
155  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
156  */
157 CAResult_t CARetransmissionStop(CARetransmission_t *context);
158
159 /**
160  * @brief   Terminating the retransmission context
161  * @param   context     [IN] context for retransmission
162  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
163  */
164 CAResult_t CARetransmissionDestroy(CARetransmission_t *context);
165
166 #ifdef __cplusplus
167 } /* extern "C" */
168 #endif
169
170 #endif  /* CA_RETRANSMISSION_H_ */