[CA] [All] Klockwork , Prevent , Memory leaks fixed
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / cainterfacecontroller.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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25
26 #include "cainterfacecontroller.h"
27 #include "caethernetadapter.h"
28 #include "caedradapter.h"
29 #include "caleadapter.h"
30 #include "cawifiadapter.h"
31 #include "canetworkconfigurator.h"
32 #include "caremotehandler.h"
33 #include "oic_malloc.h"
34 #include "logger.h"
35 #include "uthreadpool.h"
36
37 #define TAG PCF("CA")
38
39 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
40     {OIC_LOG_V(DEBUG, TAG, "memory error");goto memory_error_exit;} }
41
42 #define CA_CONNECTIVITY_TYPE_NUM   4
43
44 static CAConnectivityHandler_t gAdapterHandler[CA_CONNECTIVITY_TYPE_NUM];
45
46 static CANetworkPacketReceivedCallback gNetworkPacketReceivedCallback = NULL;
47
48 static CANetworkChangeCallback gNetworkChangeCallback = NULL;
49
50 static int8_t CAGetAdapterIndex(CAConnectivityType_t cType)
51 {
52     switch (cType)
53     {
54         case CA_ETHERNET:
55             return 0;
56         case CA_WIFI:
57             return 1;
58         case CA_EDR:
59             return 2;
60         case CA_LE:
61             return 3;
62     }
63     return -1;
64 }
65
66 static void CARegisterCallback(CAConnectivityHandler_t handler, CAConnectivityType_t cType)
67 {
68     OIC_LOG(DEBUG, TAG, "CARegisterCallback - Entry");
69     int8_t index = -1;
70
71     index = CAGetAdapterIndex(cType);
72
73     if (index == -1)
74     {
75         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
76         return;
77     }
78
79     memcpy(&gAdapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
80
81     OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
82 }
83
84 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data,
85                                      uint32_t dataLen)
86 {
87     OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
88
89     // Call the callback.
90     if (gNetworkPacketReceivedCallback != NULL)
91     {
92         gNetworkPacketReceivedCallback(endpoint, data, dataLen);
93     }
94 }
95
96 static void CANetworkChangedCallback(CALocalConnectivity_t *info,
97                                      CANetworkStatus_t status)
98 {
99     OIC_LOG(DEBUG, TAG, "Network Changed callback");
100
101     // Call the callback.
102     if (gNetworkChangeCallback != NULL)
103     {
104         gNetworkChangeCallback(info, status);
105     }
106 }
107
108 void CAInitializeAdapters(u_thread_pool_t handle)
109 {
110     OIC_LOG(DEBUG, TAG, "initialize adapters..");
111
112     memset(gAdapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
113
114     // Initialize adapters and register callback.
115 #ifdef ETHERNET_ADAPTER
116     CAInitializeEthernet(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
117                          handle);
118 #endif /* ETHERNET_ADAPTER */
119
120 #ifdef WIFI_ADAPTER
121     CAInitializeWifi(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
122                      handle);
123 #endif /* WIFI_ADAPTER */
124
125 #ifdef EDR_ADAPTER
126     CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
127                     handle);
128 #endif /* EDR_ADAPTER */
129
130 #ifdef LE_ADAPTER
131     CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
132                    handle);
133 #endif /* LE_ADAPTER */
134
135 }
136
137 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
138 {
139     OIC_LOG(DEBUG, TAG, "Set packet received callback");
140
141     gNetworkPacketReceivedCallback = callback;
142 }
143
144 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
145 {
146     OIC_LOG(DEBUG, TAG, "Set network change callback");
147
148     gNetworkChangeCallback = callback;
149 }
150
151 void CAStartAdapter(CAConnectivityType_t cType)
152 {
153     OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", cType);
154
155     int8_t index = -1;
156
157     index = CAGetAdapterIndex(cType);
158
159     if (index == -1)
160     {
161         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
162         return;
163     }
164
165     if (gAdapterHandler[index].startAdapter != NULL)
166     {
167         gAdapterHandler[index].startAdapter();
168     }
169 }
170
171 void CAStopAdapter(CAConnectivityType_t cType)
172 {
173     OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CAConnectivityType[%d]", cType);
174
175     int8_t index = -1;
176
177     index = CAGetAdapterIndex(cType);
178
179     if (index == -1)
180     {
181         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
182         return;
183     }
184
185     if (gAdapterHandler[index].stopAdapter != NULL)
186     {
187         gAdapterHandler[index].stopAdapter();
188     }
189 }
190
191 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
192 {
193     CAResult_t res = CA_STATUS_FAILED;
194     int8_t index = 0;
195     int8_t i = 0;
196
197     CALocalConnectivity_t *resInfo = NULL;
198     uint32_t resSize = 0;
199
200     CALocalConnectivity_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM];
201     uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM];
202
203     memset(tempInfo, 0, sizeof(CALocalConnectivity_t *) * CA_CONNECTIVITY_TYPE_NUM);
204     memset(tempSize, 0, sizeof(uint32_t) * CA_CONNECTIVITY_TYPE_NUM);
205
206     // #1. get information each adapter
207     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
208     {
209         if (gAdapterHandler[index].GetnetInfo != NULL)
210         {
211             res = gAdapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
212
213             OIC_LOG_V(DEBUG, TAG, "%d adapter network info size is %d res:%d", index,
214                       tempSize[index], res);
215         }
216     }
217
218     resSize = 0;
219     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
220     {
221         // check information
222         if (tempInfo[index] == NULL || tempSize[index] <= 0)
223         {
224             continue;
225         }
226
227         // #2. total size
228         resSize += tempSize[index];
229     }
230
231     OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
232
233     if (resSize <= 0)
234     {
235         res = CA_STATUS_FAILED;
236         return res;
237     }
238
239     // #3. add data into result
240     // memory allocation
241     resInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * resSize);
242     CA_MEMORY_ALLOC_CHECK(resInfo);
243     memset(resInfo, 0, sizeof(CALocalConnectivity_t) * resSize);
244
245     i = 0;
246     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
247     {
248         // check information
249         if (tempInfo[index] == NULL || tempSize[index] <= 0)
250         {
251             continue;
252         }
253
254         memcpy(resInfo + i, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
255
256         i += tempSize[index];
257
258         // free adapter data
259         OICFree(tempInfo[index]);
260     }
261
262     // #5. save data
263     *info = resInfo;
264     *size = resSize;
265
266     OIC_LOG_V(DEBUG, TAG, "each network info save success!");
267
268     return res;
269
270     // memory error label.
271 memory_error_exit:
272
273     return CA_MEMORY_ALLOC_FAILED;
274 }
275
276 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, void *data, uint32_t length)
277 {
278     OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
279
280     int8_t index = -1;
281     CAResult_t res = CA_STATUS_FAILED;
282
283     if (endpoint == NULL)
284     {
285         OIC_LOG_V(DEBUG, TAG, "Invalid endpoint");
286         return CA_STATUS_INVALID_PARAM;
287     }
288
289     CAConnectivityType_t type = endpoint->connectivityType;
290
291     index = CAGetAdapterIndex(type);
292
293     if (index == -1)
294     {
295         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
296         return CA_STATUS_INVALID_PARAM;
297     }
298
299     if (gAdapterHandler[index].sendData != NULL)
300     {
301         res = gAdapterHandler[index].sendData(endpoint, data, length);
302     }
303
304     return res;
305 }
306
307 CAResult_t CASendMulticastData(void *data, uint32_t length)
308 {
309     OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
310
311     uint8_t i, type;
312     int8_t index = -1;
313     CAResult_t res = CA_STATUS_FAILED;
314     u_arraylist_t *list = CAGetSelectedNetworkList();
315
316     if (!list)
317     {
318         OIC_LOG(DEBUG, TAG, "No selected network");
319         return CA_STATUS_FAILED;
320     }
321
322     for (i = 0; i < u_arraylist_length(list); i++)
323     {
324         void* cType = u_arraylist_get(list, i);
325
326         if(cType == NULL)
327         {
328             continue;
329         }
330
331         type = *(uint8_t *) cType;
332
333         index = CAGetAdapterIndex(type);
334
335         if (index == -1)
336         {
337             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
338             continue;
339         }
340
341         if (gAdapterHandler[index].sendDataToAll != NULL)
342         {
343             void *payload = (void *) OICMalloc(length);
344             if (!payload)
345             {
346                 OIC_LOG_V(ERROR, TAG, "Out of memory!");
347                 return CA_MEMORY_ALLOC_FAILED;
348             }
349             memcpy(payload, data, length);
350             res = gAdapterHandler[index].sendDataToAll(payload, length);
351         }
352     }
353     return res;
354 }
355
356 CAResult_t CAStartListeningServerAdapters()
357 {
358     OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
359
360     uint8_t i, type;
361     int8_t index = -1;
362     u_arraylist_t *list = CAGetSelectedNetworkList();
363
364     if (!list)
365     {
366         OIC_LOG(DEBUG, TAG, "No selected network");
367         return CA_STATUS_FAILED;
368     }
369
370     for (i = 0; i < u_arraylist_length(list); i++)
371     {
372         void* cType = u_arraylist_get(list, i);
373
374         if(cType == NULL)
375         {
376             continue;
377         }
378
379         type = *(uint8_t *) cType;
380
381         index = CAGetAdapterIndex(type);
382
383         if (index == -1)
384         {
385             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
386             continue;
387         }
388
389         if (gAdapterHandler[index].startListenServer != NULL)
390         {
391             gAdapterHandler[index].startListenServer();
392         }
393     }
394
395     return CA_STATUS_OK;
396 }
397
398 CAResult_t CAStartDiscoveryServerAdapters()
399 {
400     OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
401
402     uint8_t i, type;
403     int8_t index = -1;
404     u_arraylist_t *list = CAGetSelectedNetworkList();
405
406     if (!list)
407     {
408         OIC_LOG(DEBUG, TAG, "No selected network");
409         return CA_STATUS_FAILED;
410     }
411
412     for (i = 0; i < u_arraylist_length(list); i++)
413     {
414         void* cType = u_arraylist_get(list, i);
415
416         if(cType == NULL)
417         {
418             continue;
419         }
420
421         type = *(uint8_t *) cType;
422
423         index = CAGetAdapterIndex(type);
424
425         if (index == -1)
426         {
427             OIC_LOG_V(DEBUG, TAG, "unknown connectivity type!");
428             continue;
429         }
430
431         if (gAdapterHandler[index].startDiscoverServer != NULL)
432         {
433             gAdapterHandler[index].startDiscoverServer();
434         }
435     }
436
437     return CA_STATUS_OK;
438 }
439
440 void CATerminateAdapters()
441 {
442     OIC_LOG(DEBUG, TAG, "terminate all adapters..");
443
444     uint8_t index;
445
446     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
447     {
448         if (gAdapterHandler[index].terminate != NULL)
449         {
450             gAdapterHandler[index].stopAdapter();
451             gAdapterHandler[index].terminate();
452         }
453     }
454 }