Adding Retry logic for LUX disconnection issue
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / canetworkconfigurator.c
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 #include <stdlib.h>
22
23 #include "canetworkconfigurator.h"
24 #include "cainterfacecontroller.h"
25 #include "uarraylist.h"
26 #include "logger.h"
27
28 #define TAG "OIC_CA_NW_CONFIG"
29
30 static u_arraylist_t *g_selectedNetworkList = NULL;
31
32 #ifdef EDR_ADAPTER
33 static uint32_t NETWORK_RFCOMM = CA_ADAPTER_RFCOMM_BTEDR;
34 #endif
35
36 #ifdef LE_ADAPTER
37 static uint32_t NETWORK_GATT = CA_ADAPTER_GATT_BTLE;
38 #endif
39
40 #ifdef IP_ADAPTER
41 static uint32_t NETWORK_IP = CA_ADAPTER_IP;
42 #endif
43
44 #ifdef NFC_ADAPTER
45 static uint32_t NETWORK_NFC = CA_ADAPTER_NFC;
46 #endif
47
48 #ifdef RA_ADAPTER
49 static uint32_t NETWORK_RA = CA_ADAPTER_REMOTE_ACCESS;
50 #endif
51
52 #ifdef TCP_ADAPTER
53 static uint32_t NETWORK_TCP = CA_ADAPTER_TCP;
54 #endif
55
56 CAResult_t CAAddNetworkType(CATransportAdapter_t transportType)
57 {
58     if (NULL == g_selectedNetworkList)
59     {
60         OIC_LOG(DEBUG, TAG, "Create network list");
61
62         g_selectedNetworkList = u_arraylist_create();
63
64         if (NULL == g_selectedNetworkList)
65         {
66             return CA_MEMORY_ALLOC_FAILED;
67         }
68     }
69     bool res = true;
70     switch (transportType)
71     {
72         case CA_ADAPTER_IP:
73 #ifndef IP_ADAPTER
74             return CA_NOT_SUPPORTED;
75 #else
76
77             if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_IP))
78             {
79                 goto exit;
80             }
81             res = u_arraylist_add(g_selectedNetworkList, &NETWORK_IP);
82
83             break;
84 #endif // IP_ADAPTER
85
86         case CA_ADAPTER_RFCOMM_BTEDR:
87 #ifndef EDR_ADAPTER
88             return CA_NOT_SUPPORTED;
89 #else
90
91             if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_RFCOMM))
92             {
93                 goto exit;
94             }
95             res = u_arraylist_add(g_selectedNetworkList, &NETWORK_RFCOMM);
96
97             break;
98 #endif // EDR_ADAPTER
99
100         case CA_ADAPTER_GATT_BTLE:
101 #ifndef LE_ADAPTER
102             return CA_NOT_SUPPORTED;
103 #else
104
105             if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_GATT))
106             {
107                 goto exit;
108             }
109             res = u_arraylist_add(g_selectedNetworkList, &NETWORK_GATT);
110
111             break;
112 #endif // LE_ADAPTER
113
114 #ifdef RA_ADAPTER
115         case CA_ADAPTER_REMOTE_ACCESS:
116
117            if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_RA))
118            {
119                goto exit;
120            }
121            res = u_arraylist_add(g_selectedNetworkList, &NETWORK_RA);
122            break;
123 #endif // RA_ADAPTER
124
125 #ifdef TCP_ADAPTER
126         case CA_ADAPTER_TCP:
127
128            if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_TCP))
129            {
130                goto exit;
131            }
132            res = u_arraylist_add(g_selectedNetworkList, &NETWORK_TCP);
133            break;
134 #endif // TCP_ADAPTER
135
136
137         case CA_ADAPTER_NFC:
138  #ifndef NFC_ADAPTER
139             return CA_NOT_SUPPORTED;
140
141 #else
142            if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_NFC))
143            {
144                goto exit;
145            }
146            res = u_arraylist_add(g_selectedNetworkList, &NETWORK_NFC);
147
148            break;
149 #endif // CA_ADAPTER_NFC
150
151         default:
152             break;
153     }
154
155     if (!res)
156     {
157         OIC_LOG_V(ERROR, TAG, "Add arraylist failed[Err code: %d]", res);
158         return CA_STATUS_FAILED;
159     }
160     // start selected interface adapter
161     CAResult_t result = CAStartAdapter(transportType);
162     return result;
163
164 exit:
165     OIC_LOG(DEBUG, TAG, "This adapter is already enabled");
166     return CA_STATUS_OK;
167 }
168
169 CAResult_t CARemoveNetworkType(CATransportAdapter_t transportType)
170 {
171     if (NULL == g_selectedNetworkList)
172     {
173         OIC_LOG(ERROR, TAG, "SelectedNetwork list is NULL");
174
175         return CA_STATUS_FAILED;
176     }
177
178     uint32_t selectedNetworkLength = u_arraylist_length(g_selectedNetworkList);
179     for (uint32_t index = 0; index < selectedNetworkLength; index++)
180     {
181         void* ptrType = u_arraylist_get(g_selectedNetworkList, index);
182         if (NULL == ptrType)
183         {
184             continue;
185         }
186
187         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
188
189         if (transportType == connType)
190         {
191             switch (transportType)
192             {
193                 case CA_ADAPTER_IP:
194 #ifndef IP_ADAPTER
195                     OIC_LOG(DEBUG, TAG, "Remove network type(IP) - Not Supported");
196                     return CA_NOT_SUPPORTED;
197 #else
198
199                     OIC_LOG(DEBUG, TAG, "Remove network type(IP)");
200                     u_arraylist_remove(g_selectedNetworkList, index);
201
202                     break;
203 #endif //IP_ADAPTER
204
205                 case CA_ADAPTER_RFCOMM_BTEDR:
206 #ifndef EDR_ADAPTER
207                     OIC_LOG(DEBUG, TAG, "Remove network type(EDR) - Not Supported");
208                     return CA_NOT_SUPPORTED;
209 #else
210                     OIC_LOG(DEBUG, TAG, "Remove network type(EDR)");
211                     u_arraylist_remove(g_selectedNetworkList, index);
212
213                     break;
214 #endif // EDR_ADAPTER
215
216                 case CA_ADAPTER_GATT_BTLE:
217 #ifndef LE_ADAPTER
218                     OIC_LOG(DEBUG, TAG, "Remove network type(LE) - Not Supported");
219                     return CA_NOT_SUPPORTED;
220 #else
221                     OIC_LOG(DEBUG, TAG, "Remove network type(LE)");
222                     u_arraylist_remove(g_selectedNetworkList, index);
223
224                     break;
225 #endif // LE_ADAPTER
226
227 #ifdef RA_ADAPTER
228                 case CA_ADAPTER_REMOTE_ACCESS:
229                     OIC_LOG(DEBUG, TAG, "Remove network type(RA)");
230                     u_arraylist_remove(g_selectedNetworkList, index);
231                     break;
232 #endif // RA_ADAPTER
233
234 #ifdef TCP_ADAPTER
235                 case CA_ADAPTER_TCP:
236                     OIC_LOG(DEBUG, TAG, "Remove network type(TCP)");
237                     u_arraylist_remove(g_selectedNetworkList, index);
238                     break;
239 #endif // TCP_ADAPTER
240
241 #ifndef NFC_ADAPTER
242                 case CA_ADAPTER_NFC:
243                     OIC_LOG(DEBUG, TAG, "Add network type(NFC) - Not Supported");
244                     return CA_NOT_SUPPORTED;
245 #else
246                    OIC_LOG(DEBUG, TAG, "Add network type(NFC)");
247                    u_arraylist_remove(g_selectedNetworkList, index);
248
249                    break;
250 #endif // CA_ADAPTER_NFC
251                 default:
252                     break;
253             }
254
255             // stop selected interface adapter
256             CAStopAdapter(connType);
257             return CA_STATUS_OK;
258         }
259     }
260
261     return CA_STATUS_FAILED;
262 }
263
264 u_arraylist_t *CAGetSelectedNetworkList()
265 {
266     return g_selectedNetworkList;
267 }
268
269 CAResult_t CAGetNetworkInformationInternal(CAEndpoint_t **info, uint32_t *size)
270 {
271     OIC_LOG(DEBUG, TAG, "get network information.");
272
273     if (NULL == info || NULL == size)
274     {
275         OIC_LOG(ERROR, TAG, "Input parameter is invalid value");
276
277         return CA_STATUS_INVALID_PARAM;
278     }
279
280     return CAGetNetworkInfo(info, size);
281 }
282
283 CAResult_t CATerminateNetworkType()
284 {
285     OIC_LOG(DEBUG, TAG, "CATerminateNetworkType()");
286     if (NULL != g_selectedNetworkList)
287     {
288         u_arraylist_free(&g_selectedNetworkList);
289     }
290     return CA_STATUS_OK;
291 }