Merge branch 'master' into 'security-CKM' branch
[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 "CA_NW_CONFIG"
29
30 static u_arraylist_t *g_selectedNetworkList = NULL;
31 static uint32_t NETWORK_IP = CA_ADAPTER_IP;
32 static uint32_t NETWORK_RFCOMM = CA_ADAPTER_RFCOMM_BTEDR;
33 static uint32_t NETWORK_GATT = CA_ADAPTER_GATT_BTLE;
34
35 #ifdef RA_ADAPTER
36 static uint32_t NETWORK_RA = CA_ADAPTER_REMOTE_ACCESS;
37 #endif
38
39 CAResult_t CAAddNetworkType(CATransportAdapter_t transportType)
40 {
41     OIC_LOG(DEBUG, TAG, "IN");
42     if (NULL == g_selectedNetworkList)
43     {
44         OIC_LOG(DEBUG, TAG, "Create network list");
45
46         g_selectedNetworkList = u_arraylist_create();
47
48         if (NULL == g_selectedNetworkList)
49         {
50             return CA_MEMORY_ALLOC_FAILED;
51         }
52     }
53     bool res = true;
54     switch (transportType)
55     {
56         case CA_ADAPTER_IP:
57 #ifndef IP_ADAPTER
58             OIC_LOG(DEBUG, TAG, "Add network type(IP) - Not Supported");
59             return CA_NOT_SUPPORTED;
60 #endif /* IP_ADAPTER */
61
62             OIC_LOG(DEBUG, TAG, "Add network type(IP)");
63             if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_IP))
64             {
65                 goto exit;
66             }
67             res = u_arraylist_add(g_selectedNetworkList, &NETWORK_IP);
68             break;
69
70         case CA_ADAPTER_RFCOMM_BTEDR:
71 #ifndef EDR_ADAPTER
72             OIC_LOG(DEBUG, TAG, "Add network type(EDR) - Not Supported");
73             return CA_NOT_SUPPORTED;
74 #endif /* EDR_ADAPTER */
75
76             OIC_LOG(DEBUG, TAG, "Add network type(EDR)");
77             if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_RFCOMM))
78             {
79                 goto exit;
80             }
81             res = u_arraylist_add(g_selectedNetworkList, &NETWORK_RFCOMM);
82             break;
83
84         case CA_ADAPTER_GATT_BTLE:
85 #ifndef LE_ADAPTER
86             OIC_LOG(DEBUG, TAG, "Add network type(LE) - Not Supported");
87             return CA_NOT_SUPPORTED;
88 #endif /* LE_ADAPTER */
89
90             OIC_LOG(DEBUG, TAG, "Add network type(LE)");
91             if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_GATT))
92             {
93                 goto exit;
94             }
95             res = u_arraylist_add(g_selectedNetworkList, &NETWORK_GATT);
96             break;
97
98 #ifdef RA_ADAPTER
99         case CA_ADAPTER_REMOTE_ACCESS:
100
101            OIC_LOG(DEBUG, TAG, "Add network type(RA)");
102            if (u_arraylist_contains(g_selectedNetworkList, &NETWORK_RA))
103            {
104                goto exit;
105            }
106            res = u_arraylist_add(g_selectedNetworkList, &NETWORK_RA);
107            break;
108 #endif /* RA_ADAPTER */
109
110         default:
111             break;
112     }
113
114     if (!res)
115     {
116         OIC_LOG_V(ERROR, TAG, "Add arraylist failed[Err code: %d]", res);
117         return CA_STATUS_FAILED;
118     }
119     // start selected interface adapter
120     CAResult_t result = CAStartAdapter(transportType);
121     OIC_LOG(DEBUG, TAG, "OUT");
122     return result;
123
124 exit:
125     OIC_LOG(DEBUG, TAG, "This adapter is already enabled");
126     return CA_STATUS_OK;
127 }
128
129 CAResult_t CARemoveNetworkType(CATransportAdapter_t transportType)
130 {
131     OIC_LOG(DEBUG, TAG, "IN");
132
133     if (NULL == g_selectedNetworkList)
134     {
135         OIC_LOG(ERROR, TAG, "SelectedNetwork list is NULL");
136
137         return CA_STATUS_FAILED;
138     }
139
140     uint32_t selectedNetworkLength = u_arraylist_length(g_selectedNetworkList);
141     for (uint32_t index = 0; index < selectedNetworkLength; index++)
142     {
143         void* ptrType = u_arraylist_get(g_selectedNetworkList, index);
144         if (NULL == ptrType)
145         {
146             continue;
147         }
148
149         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
150
151         if (transportType == connType)
152         {
153             switch (transportType)
154             {
155                 case CA_ADAPTER_IP:
156 #ifndef IP_ADAPTER
157                     OIC_LOG(DEBUG, TAG, "Remove network type(IP) - Not Supported");
158                     return CA_NOT_SUPPORTED;
159 #else
160
161                     OIC_LOG(DEBUG, TAG, "Remove network type(IP)");
162                     u_arraylist_remove(g_selectedNetworkList, index);
163 #endif /* IP_ADAPTER */
164                     break;
165
166                 case CA_ADAPTER_RFCOMM_BTEDR:
167 #ifndef EDR_ADAPTER
168                     OIC_LOG(DEBUG, TAG, "Remove network type(EDR) - Not Supported");
169                     return CA_NOT_SUPPORTED;
170 #else
171                     OIC_LOG(DEBUG, TAG, "Remove network type(EDR)");
172                     u_arraylist_remove(g_selectedNetworkList, index);
173 #endif /* EDR_ADAPTER */
174                     break;
175
176                 case CA_ADAPTER_GATT_BTLE:
177 #ifndef LE_ADAPTER
178                     OIC_LOG(DEBUG, TAG, "Remove network type(LE) - Not Supported");
179                     return CA_NOT_SUPPORTED;
180 #else
181                     OIC_LOG(DEBUG, TAG, "Remove network type(LE)");
182                     u_arraylist_remove(g_selectedNetworkList, index);
183 #endif /* LE_ADAPTER */
184
185                     break;
186 #ifdef RA_ADAPTER
187                 case CA_ADAPTER_REMOTE_ACCESS:
188                     OIC_LOG(DEBUG, TAG, "Remove network type(RA)");
189                     u_arraylist_remove(g_selectedNetworkList, index);
190                     break;
191 #endif /* RA_ADAPTER */
192
193                 default:
194                     break;
195             }
196
197             // stop selected interface adapter
198             CAStopAdapter(connType);
199             return CA_STATUS_OK;
200         }
201     }
202
203     return CA_STATUS_FAILED;
204 }
205
206 u_arraylist_t *CAGetSelectedNetworkList()
207 {
208     return g_selectedNetworkList;
209 }
210
211 CAResult_t CAGetNetworkInformationInternal(CAEndpoint_t **info, uint32_t *size)
212 {
213     OIC_LOG(DEBUG, TAG, "get network information.");
214
215     if (NULL == info || NULL == size)
216     {
217         OIC_LOG(ERROR, TAG, "Input parameter is invalid value");
218
219         return CA_STATUS_INVALID_PARAM;
220     }
221
222     return CAGetNetworkInfo(info, size);
223 }
224
225 CAResult_t CATerminateNetworkType()
226 {
227     OIC_LOG(DEBUG, TAG, "CATerminateNetworkType()");
228     if (NULL != g_selectedNetworkList)
229     {
230         u_arraylist_free(&g_selectedNetworkList);
231     }
232     return CA_STATUS_OK;
233 }