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