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