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