Merge branch 'master' into cloud-interface
[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             OIC_LOG(DEBUG, TAG, "Add network type(IP) - Not Supported");
75             return CA_NOT_SUPPORTED;
76 #else
77
78             OIC_LOG(DEBUG, TAG, "Add network type(IP)");
79             if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_IP))
80             {
81                 goto exit;
82             }
83             res = u_arraylist_add(g_selectedNetworkList, &NETWORK_IP);
84
85             break;
86 #endif // IP_ADAPTER
87
88         case CA_ADAPTER_RFCOMM_BTEDR:
89 #ifndef EDR_ADAPTER
90             OIC_LOG(DEBUG, TAG, "Add network type(EDR) - Not Supported");
91             return CA_NOT_SUPPORTED;
92 #else
93
94             OIC_LOG(DEBUG, TAG, "Add network type(EDR)");
95             if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_RFCOMM))
96             {
97                 goto exit;
98             }
99             res = u_arraylist_add(g_selectedNetworkList, &NETWORK_RFCOMM);
100
101             break;
102 #endif // EDR_ADAPTER
103
104         case CA_ADAPTER_GATT_BTLE:
105 #ifndef LE_ADAPTER
106             OIC_LOG(DEBUG, TAG, "Add network type(LE) - Not Supported");
107             return CA_NOT_SUPPORTED;
108 #else
109
110             OIC_LOG(DEBUG, TAG, "Add network type(LE)");
111             if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_GATT))
112             {
113                 goto exit;
114             }
115             res = u_arraylist_add(g_selectedNetworkList, &NETWORK_GATT);
116
117             break;
118 #endif // LE_ADAPTER
119
120 #ifdef RA_ADAPTER
121         case CA_ADAPTER_REMOTE_ACCESS:
122
123            OIC_LOG(DEBUG, TAG, "Add network type(RA)");
124            if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_RA))
125            {
126                goto exit;
127            }
128            res = u_arraylist_add(g_selectedNetworkList, &NETWORK_RA);
129            break;
130 #endif // RA_ADAPTER
131
132 #ifdef TCP_ADAPTER
133         case CA_ADAPTER_TCP:
134
135            OIC_LOG(DEBUG, TAG, "Add network type(TCP)");
136            if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_TCP))
137            {
138                goto exit;
139            }
140            res = u_arraylist_add(g_selectedNetworkList, &NETWORK_TCP);
141            break;
142 #endif // TCP_ADAPTER
143
144 #ifndef NFC_ADAPTER
145         case CA_ADAPTER_NFC:
146             OIC_LOG(DEBUG, TAG, "Add network type(NFC) - Not Supported");
147             return CA_NOT_SUPPORTED;
148
149 #else
150            OIC_LOG(DEBUG, TAG, "Add network type(NFC)");
151            if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_NFC))
152            {
153                goto exit;
154            }
155            res = u_arraylist_add(g_selectedNetworkList, &NETWORK_NFC);
156
157            break;
158 #endif // CA_ADAPTER_NFC
159
160         default:
161             break;
162     }
163
164     if (!res)
165     {
166         OIC_LOG_V(ERROR, TAG, "Add arraylist failed[Err code: %d]", res);
167         return CA_STATUS_FAILED;
168     }
169     // start selected interface adapter
170     CAResult_t result = CAStartAdapter(transportType);
171     return result;
172
173 exit:
174     OIC_LOG(DEBUG, TAG, "This adapter is already enabled");
175     return CA_STATUS_OK;
176 }
177
178 CAResult_t CARemoveNetworkType(CATransportAdapter_t transportType)
179 {
180     if (NULL == g_selectedNetworkList)
181     {
182         OIC_LOG(ERROR, TAG, "SelectedNetwork list is NULL");
183
184         return CA_STATUS_FAILED;
185     }
186
187     uint32_t selectedNetworkLength = u_arraylist_length(g_selectedNetworkList);
188     for (uint32_t index = 0; index < selectedNetworkLength; index++)
189     {
190         void* ptrType = u_arraylist_get(g_selectedNetworkList, index);
191         if (NULL == ptrType)
192         {
193             continue;
194         }
195
196         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
197
198         if (transportType == connType)
199         {
200             switch (transportType)
201             {
202                 case CA_ADAPTER_IP:
203 #ifndef IP_ADAPTER
204                     OIC_LOG(DEBUG, TAG, "Remove network type(IP) - Not Supported");
205                     return CA_NOT_SUPPORTED;
206 #else
207
208                     OIC_LOG(DEBUG, TAG, "Remove network type(IP)");
209                     u_arraylist_remove(g_selectedNetworkList, index);
210
211                     break;
212 #endif //IP_ADAPTER
213
214                 case CA_ADAPTER_RFCOMM_BTEDR:
215 #ifndef EDR_ADAPTER
216                     OIC_LOG(DEBUG, TAG, "Remove network type(EDR) - Not Supported");
217                     return CA_NOT_SUPPORTED;
218 #else
219                     OIC_LOG(DEBUG, TAG, "Remove network type(EDR)");
220                     u_arraylist_remove(g_selectedNetworkList, index);
221
222                     break;
223 #endif // EDR_ADAPTER
224
225                 case CA_ADAPTER_GATT_BTLE:
226 #ifndef LE_ADAPTER
227                     OIC_LOG(DEBUG, TAG, "Remove network type(LE) - Not Supported");
228                     return CA_NOT_SUPPORTED;
229 #else
230                     OIC_LOG(DEBUG, TAG, "Remove network type(LE)");
231                     u_arraylist_remove(g_selectedNetworkList, index);
232
233                     break;
234 #endif // LE_ADAPTER
235
236 #ifdef RA_ADAPTER
237                 case CA_ADAPTER_REMOTE_ACCESS:
238                     OIC_LOG(DEBUG, TAG, "Remove network type(RA)");
239                     u_arraylist_remove(g_selectedNetworkList, index);
240                     break;
241 #endif // RA_ADAPTER
242
243 #ifdef TCP_ADAPTER
244                 case CA_ADAPTER_TCP:
245                     OIC_LOG(DEBUG, TAG, "Remove network type(TCP)");
246                     u_arraylist_remove(g_selectedNetworkList, index);
247                     break;
248 #endif // TCP_ADAPTER
249
250 #ifndef NFC_ADAPTER
251                 case CA_ADAPTER_NFC:
252                     OIC_LOG(DEBUG, TAG, "Add network type(NFC) - Not Supported");
253                     return CA_NOT_SUPPORTED;
254 #else
255                    OIC_LOG(DEBUG, TAG, "Add network type(NFC)");
256                    u_arraylist_remove(g_selectedNetworkList, index);
257
258                    break;
259 #endif // CA_ADAPTER_NFC
260                 default:
261                     break;
262             }
263
264             // stop selected interface adapter
265             CAStopAdapter(connType);
266             return CA_STATUS_OK;
267         }
268     }
269
270     return CA_STATUS_FAILED;
271 }
272
273 u_arraylist_t *CAGetSelectedNetworkList()
274 {
275     return g_selectedNetworkList;
276 }
277
278 CAResult_t CAGetNetworkInformationInternal(CAEndpoint_t **info, uint32_t *size)
279 {
280     OIC_LOG(DEBUG, TAG, "get network information.");
281
282     if (NULL == info || NULL == size)
283     {
284         OIC_LOG(ERROR, TAG, "Input parameter is invalid value");
285
286         return CA_STATUS_INVALID_PARAM;
287     }
288
289     return CAGetNetworkInfo(info, size);
290 }
291
292 CAResult_t CATerminateNetworkType()
293 {
294     OIC_LOG(DEBUG, TAG, "CATerminateNetworkType()");
295     if (NULL != g_selectedNetworkList)
296     {
297         u_arraylist_free(&g_selectedNetworkList);
298     }
299     return CA_STATUS_OK;
300 }