Fix incorrect usages of OIC_LOG_V macro.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / canetworkconfigurator_singlethread.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 "cainterfacecontroller_singlethread.h"
22
23 #include <stdlib.h>
24
25 #include "canetworkconfigurator.h"
26 #include "uarraylist.h"
27 #include "logger.h"
28
29 #define TAG "CANW_ST"
30
31 static u_arraylist_t *g_selectedNetworkList = NULL;
32 static uint32_t NETWORK_ETHERNET = CA_ETHERNET;
33 static uint32_t NETWORK_WIFI = CA_WIFI;
34 static uint32_t NETWORK_EDR = CA_EDR;
35 static uint32_t NETWORK_LE = CA_LE;
36
37 CAResult_t CAAddNetworkType(CAConnectivityType_t connectivityType)
38 {
39     OIC_LOG(DEBUG, TAG, "IN");
40     if (g_selectedNetworkList == NULL)
41     {
42         g_selectedNetworkList = u_arraylist_create();
43
44         if (NULL == g_selectedNetworkList)
45         {
46             return CA_MEMORY_ALLOC_FAILED;
47         }
48     }
49     CAResult_t res = CA_STATUS_OK;
50     switch (connectivityType)
51     {
52         case CA_ETHERNET:
53         {
54
55 #ifndef ETHERNET_ADAPTER
56             OIC_LOG(DEBUG, TAG, "Add network type(ETHERNET) - Not Supported");
57             return CA_NOT_SUPPORTED;
58 #endif /* ETHERNET_ADAPTER */
59
60             OIC_LOG(DEBUG, TAG, "Add network type(ETHERNET)");
61             if (!u_arraylist_contains(g_selectedNetworkList, &NETWORK_ETHERNET))
62             {
63                 res = u_arraylist_add(g_selectedNetworkList, &NETWORK_ETHERNET);
64                 if (CA_STATUS_OK != res)
65                 {
66                     OIC_LOG_V(ERROR, TAG, "Add arraylist failed[Err code: %d]", res);
67                     return res;
68                 }
69             }
70         }
71         break;
72
73         case CA_WIFI:
74         {
75
76 #ifndef WIFI_ADAPTER
77             OIC_LOG(DEBUG, TAG, "Add network type(WIFI) - Not Supported");
78             return CA_NOT_SUPPORTED;
79 #endif /* WIFI_ADAPTER */
80
81             OIC_LOG(DEBUG, TAG, "Add network type(WIFI)");
82             if (!u_arraylist_contains(g_selectedNetworkList, &NETWORK_WIFI))
83             {
84                 res = u_arraylist_add(g_selectedNetworkList, &NETWORK_WIFI);
85                 if (CA_STATUS_OK != res)
86                 {
87                     OIC_LOG_V(ERROR, TAG, "Add arraylist failed[Err code: %d]", res);
88                     return res;
89                 }
90             }
91         }
92         break;
93
94         case CA_EDR:
95         {
96
97 #ifndef EDR_ADAPTER
98             OIC_LOG(DEBUG, TAG, "Add network type(EDR) - Not Supported");
99             return CA_NOT_SUPPORTED;
100 #endif /* EDR_ADAPTER */
101
102             OIC_LOG(DEBUG, TAG, "Add network type(EDR)");
103             if (!u_arraylist_contains(g_selectedNetworkList, &NETWORK_EDR))
104             {
105                 res = u_arraylist_add(g_selectedNetworkList, &NETWORK_EDR);
106                 if (CA_STATUS_OK != res)
107                 {
108                     OIC_LOG_V(ERROR, TAG, "Add arraylist failed[Err code: %d]", res);
109                     return res;
110                 }
111             }
112         }
113         break;
114
115         case CA_LE:
116         {
117
118 #ifndef LE_ADAPTER
119             OIC_LOG(DEBUG, TAG, "Add network type(LE) - Not Supported");
120             return CA_NOT_SUPPORTED;
121 #endif /* LE_ADAPTER */
122
123             OIC_LOG(DEBUG, TAG, "Add network type(LE)");
124             if (!u_arraylist_contains(g_selectedNetworkList, &NETWORK_LE))
125             {
126                 res = u_arraylist_add(g_selectedNetworkList, &NETWORK_LE);
127                 if (CA_STATUS_OK != res)
128                 {
129                     OIC_LOG_V(ERROR, TAG, "Add arraylist failed[Err code: %d]", res);
130                     return res;
131                 }
132             }
133         }
134         break;
135
136     }
137
138     // start selected interface adapter
139     res = CAStartAdapter((CAConnectivityType_t)connectivityType);
140     OIC_LOG(DEBUG, TAG, "OUT");
141     return res;
142 }
143
144 CAResult_t CARemoveNetworkType(CAConnectivityType_t connectivityType)
145 {
146     OIC_LOG(DEBUG, TAG, "IN");
147
148     if (g_selectedNetworkList == NULL)
149     {
150         OIC_LOG(DEBUG, TAG, "Selected network not found");
151
152         return CA_STATUS_FAILED;
153     }
154
155     uint8_t index;
156     for (index = 0; index < u_arraylist_length(g_selectedNetworkList); index++)
157     {
158         void* ptrType = u_arraylist_get(g_selectedNetworkList, index);
159         if (NULL == ptrType)
160         {
161             continue;
162         }
163
164         CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
165
166         if (connectivityType == connType)
167         {
168             switch (connectivityType)
169             {
170                 case CA_ETHERNET:
171
172 #ifndef ETHERNET_ADAPTER
173                     OIC_LOG(DEBUG, TAG, "Remove network type(ETHERNET) - Not Supported");
174                     return CA_NOT_SUPPORTED;
175 #else
176
177                     OIC_LOG(DEBUG, TAG, "Remove network type(ETHERNET)");
178                     u_arraylist_remove(g_selectedNetworkList, index);
179 #endif /* ETHERNET_ADAPTER */
180                     break;
181
182                 case CA_WIFI:
183
184 #ifndef WIFI_ADAPTER
185                     OIC_LOG(DEBUG, TAG, "Remove network type(WIFI) - Not Supported");
186                     return CA_NOT_SUPPORTED;
187 #else
188                     OIC_LOG(DEBUG, TAG, "Remove network type(WIFI)");
189                     u_arraylist_remove(g_selectedNetworkList, index);
190 #endif /* WIFI_ADAPTER */
191
192                     break;
193
194                 case CA_EDR:
195
196 #ifndef EDR_ADAPTER
197                     OIC_LOG(DEBUG, TAG, "Remove network type(EDR) - Not Supported");
198                     return CA_NOT_SUPPORTED;
199 #else
200                     OIC_LOG(DEBUG, TAG, "Remove network type(EDR)");
201                     u_arraylist_remove(g_selectedNetworkList, index);
202 #endif /* EDR_ADAPTER */
203
204                     break;
205
206                 case CA_LE:
207
208 #ifndef LE_ADAPTER
209                     OIC_LOG(DEBUG, TAG, "Remove network type(LE) - Not Supported");
210                     return CA_NOT_SUPPORTED;
211 #else
212                     OIC_LOG(DEBUG, TAG, "Remove network type(LE)");
213                     u_arraylist_remove(g_selectedNetworkList, index);
214 #endif /* LE_ADAPTER */
215
216                     break;
217             }
218
219             // stop selected interface adapter
220             CAStopAdapter(connType);
221         }
222     }
223
224     return CA_STATUS_OK;
225 }
226
227 u_arraylist_t *CAGetSelectedNetworkList()
228 {
229     return g_selectedNetworkList;
230 }
231
232 CAResult_t CAGetNetworkInformationInternal(CALocalConnectivity_t **info, uint32_t *size)
233 {
234     OIC_LOG(DEBUG, TAG, "get network information.");
235
236     if (info == NULL || size == NULL)
237     {
238         OIC_LOG(DEBUG, TAG, "Input parameter is invalid value");
239
240         return CA_STATUS_INVALID_PARAM;
241     }
242
243     return CAGetNetworkInfo(info, size);
244 }
245
246 CAResult_t CATerminateNetworkType()
247 {
248     OIC_LOG(DEBUG, TAG, "CATerminateNetworkType()");
249     if(g_selectedNetworkList != NULL)
250     {
251         u_arraylist_free(&g_selectedNetworkList);
252     }
253     return CA_STATUS_OK;
254 }
255