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