Resolved build warning for linux
[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 <inttypes.h>
25
26 #include "logger.h"
27 #include "oic_malloc.h"
28 #include "caadapterutils.h"
29 #include "canetworkconfigurator.h"
30 #include "cainterfacecontroller.h"
31 #include "caedradapter.h"
32 #include "caleadapter.h"
33 #include "canfcadapter.h"
34 #include "caremotehandler.h"
35 #include "cathreadpool.h"
36 #include "caipadapter.h"
37 #include "cainterface.h"
38
39 #ifdef RA_ADAPTER
40 #include "caraadapter.h"
41 #endif
42
43 #ifdef TCP_ADAPTER
44 #include "catcpadapter.h"
45 #endif
46
47 #define TAG "OIC_CA_INF_CTR"
48
49 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
50     {OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
51
52 static CAConnectivityHandler_t *g_adapterHandler = NULL;
53
54 static uint32_t g_numberOfAdapters = 0;
55
56 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
57
58 static CAAdapterStateChangedCB g_adapterChangeCallback = NULL;
59
60 static CAConnectionStateChangedCB g_connChangeCallback = NULL;
61
62 static CAErrorHandleCallback g_errorHandleCallback = NULL;
63
64 static int CAGetAdapterIndex(CATransportAdapter_t cType)
65 {
66     for (uint32_t index=0 ; index < g_numberOfAdapters ; index++)
67     {
68         if (cType == g_adapterHandler[index].cType )
69          {
70              return index;
71          }
72     }
73     return -1;
74 }
75
76 static void CARegisterCallback(CAConnectivityHandler_t handler)
77 {
78     if (handler.startAdapter == NULL ||
79         handler.startListenServer == NULL ||
80         handler.stopListenServer == NULL ||
81         handler.startDiscoveryServer == NULL ||
82         handler.sendData == NULL ||
83         handler.sendDataToAll == NULL ||
84         handler.GetnetInfo == NULL ||
85         handler.readData == NULL ||
86         handler.stopAdapter == NULL ||
87         handler.terminate == NULL)
88     {
89         OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
90         return;
91     }
92     uint32_t numberofAdapters = g_numberOfAdapters + 1;
93     CAConnectivityHandler_t *adapterHandler = OICRealloc(g_adapterHandler,
94                                    (numberofAdapters) * sizeof(*adapterHandler));
95     if (NULL == adapterHandler)
96     {
97         OIC_LOG(ERROR, TAG, "Memory allocation failed during registration");
98         return;
99     }
100     g_adapterHandler = adapterHandler;
101     g_numberOfAdapters = numberofAdapters;
102     g_adapterHandler[g_numberOfAdapters-1] = handler;
103
104     OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", handler.cType);
105 }
106
107 #ifdef RA_ADAPTER
108 CAResult_t CASetAdapterRAInfo(const CARAInfo_t *caraInfo)
109 {
110     return CASetRAInfo(caraInfo);
111 }
112 #endif
113
114 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
115                                      const void *data, uint32_t dataLen)
116 {
117     if (g_networkPacketReceivedCallback != NULL)
118     {
119         g_networkPacketReceivedCallback(sep, data, dataLen);
120     }
121     else
122     {
123         OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
124     }
125 }
126
127 static void CAAdapterChangedCallback(CATransportAdapter_t adapter, CANetworkStatus_t status)
128 {
129     // Call the callback.
130     if (g_adapterChangeCallback != NULL)
131     {
132         if (CA_INTERFACE_UP == status)
133         {
134             g_adapterChangeCallback(adapter, true);
135         }
136         else if (CA_INTERFACE_DOWN == status)
137         {
138             g_adapterChangeCallback(adapter, false);
139         }
140     }
141     OIC_LOG_V(DEBUG, TAG, "[%d]adapter status is changed to [%d]", adapter, status);
142 }
143
144 static void CAConnectionChangedCallback(const CAEndpoint_t *info, bool isConnected)
145 {
146     // Call the callback.
147     if (g_connChangeCallback != NULL)
148     {
149         g_connChangeCallback(info, isConnected);
150     }
151     OIC_LOG_V(DEBUG, TAG, "[%s] connection status is changed to [%d]", info->addr, isConnected);
152 }
153
154 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
155                                          const void *data, uint32_t dataLen,
156                                          CAResult_t result)
157 {
158     OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
159
160     // Call the callback.
161     if (g_errorHandleCallback != NULL)
162     {
163         g_errorHandleCallback(endpoint, data, dataLen, result);
164     }
165 }
166
167 void CAInitializeAdapters(ca_thread_pool_t handle)
168 {
169     OIC_LOG(DEBUG, TAG, "initialize adapters..");
170
171     // Initialize adapters and register callback.
172 #ifdef IP_ADAPTER
173     CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
174                    CAAdapterErrorHandleCallback, handle);
175 #endif /* IP_ADAPTER */
176
177 #ifdef EDR_ADAPTER
178     CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
179                     CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
180 #endif /* EDR_ADAPTER */
181
182 #ifdef LE_ADAPTER
183     CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
184                    CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
185 #endif /* LE_ADAPTER */
186
187 #ifdef RA_ADAPTER
188     CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
189                    handle);
190 #endif /* RA_ADAPTER */
191
192 #ifdef TCP_ADAPTER
193     CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
194                     CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
195 #endif /* TCP_ADAPTER */
196
197 #ifdef NFC_ADAPTER
198     CAInitializeNFC(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
199                     CAAdapterErrorHandleCallback, handle);
200 #endif /* NFC_ADAPTER */
201 }
202
203 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
204 {
205     OIC_LOG(DEBUG, TAG, "Set Receiver handle callback");
206
207     g_networkPacketReceivedCallback = callback;
208 }
209
210 void CASetNetworkMonitorCallbacks(CAAdapterStateChangedCB adapterCB,
211                                   CAConnectionStateChangedCB connCB)
212 {
213     OIC_LOG(DEBUG, TAG, "Set network monitoring callback");
214
215     g_adapterChangeCallback = adapterCB;
216     g_connChangeCallback = connCB;
217 }
218
219 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
220 {
221     OIC_LOG(DEBUG, TAG, "Set error handle callback");
222     g_errorHandleCallback = errorCallback;
223 }
224
225 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
226 {
227     OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
228
229     int index = CAGetAdapterIndex(transportType);
230     if (0 > index)
231     {
232         OIC_LOG(ERROR, TAG, "unknown connectivity type!");
233         return CA_STATUS_FAILED;
234     }
235
236     CAResult_t res = CA_STATUS_FAILED;
237     if (g_adapterHandler[index].startAdapter != NULL)
238     {
239         res = g_adapterHandler[index].startAdapter();
240     }
241
242     return res;
243 }
244
245 void CAStopAdapter(CATransportAdapter_t transportType)
246 {
247     OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
248
249     int index = CAGetAdapterIndex(transportType);
250     if (0 > index)
251     {
252         OIC_LOG(ERROR, TAG, "unknown transport type!");
253         return;
254     }
255
256     if (g_adapterHandler[index].stopAdapter != NULL)
257     {
258         g_adapterHandler[index].stopAdapter();
259     }
260 }
261
262 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
263 {
264     if (info == NULL || size == NULL)
265     {
266         return CA_STATUS_INVALID_PARAM;
267     }
268
269     CAEndpoint_t **tempInfo = (CAEndpoint_t**) OICCalloc(g_numberOfAdapters, sizeof(*tempInfo));
270     if (!tempInfo)
271     {
272         OIC_LOG(ERROR, TAG, "Out of memory!");
273         return CA_MEMORY_ALLOC_FAILED;
274     }
275     uint32_t *tempSize =(uint32_t*) OICCalloc(g_numberOfAdapters, sizeof(*tempSize));
276     if (!tempSize)
277     {
278         OIC_LOG(ERROR, TAG, "Out of memory!");
279         OICFree(tempInfo);
280         return CA_MEMORY_ALLOC_FAILED;
281     }
282
283     CAResult_t res = CA_STATUS_FAILED;
284     size_t resSize = 0;
285     for (uint32_t index = 0; index < g_numberOfAdapters; index++)
286     {
287         if (g_adapterHandler[index].GetnetInfo != NULL)
288         {
289             // #1. get information for each adapter
290             res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
291                                                      &tempSize[index]);
292
293             // #2. total size
294             if (res == CA_STATUS_OK)
295             {
296                 resSize += tempSize[index];
297             }
298
299             OIC_LOG_V(DEBUG,
300                       TAG,
301                       "%zu adapter network info size is %" PRIu32 " res:%d",
302                       index,
303                       tempSize[index],
304                       res);
305         }
306     }
307
308     OIC_LOG_V(DEBUG, TAG, "network info total size is %zu!", resSize);
309
310     if (resSize == 0)
311     {
312         OICFree(tempInfo);
313         OICFree(tempSize);
314         if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
315         {
316             return res;
317         }
318         else
319         {
320             return CA_STATUS_FAILED;
321         }
322     }
323
324     // #3. add data into result
325     // memory allocation
326     CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
327     CA_MEMORY_ALLOC_CHECK(resInfo);
328
329     // #4. save data
330     *info = resInfo;
331     *size = resSize;
332
333     for (uint32_t index = 0; index < g_numberOfAdapters; index++)
334     {
335         // check information
336         if (tempSize[index] == 0)
337         {
338             continue;
339         }
340
341         memcpy(resInfo,
342                tempInfo[index],
343                sizeof(*resInfo) * tempSize[index]);
344
345         resInfo += tempSize[index];
346
347         // free adapter data
348         OICFree(tempInfo[index]);
349         tempInfo[index] = NULL;
350     }
351     OICFree(tempInfo);
352     OICFree(tempSize);
353
354     OIC_LOG(DEBUG, TAG, "each network info save success!");
355     return CA_STATUS_OK;
356
357     // memory error label.
358 memory_error_exit:
359
360     for (uint32_t index = 0; index < g_numberOfAdapters; index++)
361     {
362         OICFree(tempInfo[index]);
363         tempInfo[index] = NULL;
364     }
365     OICFree(tempInfo);
366     OICFree(tempSize);
367
368     return CA_MEMORY_ALLOC_FAILED;
369 }
370
371 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length,
372                              CADataType_t dataType)
373 {
374     if (endpoint == NULL)
375     {
376         OIC_LOG(DEBUG, TAG, "Invalid endpoint");
377         return CA_STATUS_INVALID_PARAM;
378     }
379
380
381     u_arraylist_t *list = CAGetSelectedNetworkList();
382     if (!list)
383     {
384         OIC_LOG(ERROR, TAG, "No selected network");
385         return CA_SEND_FAILED;
386     }
387     CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
388
389     for (uint32_t i = 0; i < u_arraylist_length(list); i++)
390     {
391         void* ptrType = u_arraylist_get(list, i);
392
393         if (NULL == ptrType)
394         {
395             continue;
396         }
397
398         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
399         if (0 == (connType & requestedAdapter))
400         {
401             continue;
402         }
403
404         int index = CAGetAdapterIndex(connType);
405
406         if (-1 == index)
407         {
408             OIC_LOG(ERROR, TAG, "unknown transport type!");
409             return CA_STATUS_INVALID_PARAM;
410         }
411
412         int32_t sentDataLen = 0;
413
414         if (NULL != g_adapterHandler[index].sendData)
415         {
416             OIC_LOG(DEBUG, TAG, "unicast message to adapter");
417             sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length, dataType);
418         }
419
420         if (sentDataLen != (int32_t)length)
421         {
422             OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
423 #ifdef SINGLE_THREAD
424             //in case of single thread, no error handler. Report error immediately
425             return CA_SEND_FAILED;
426 #endif
427         }
428
429     }
430
431     return CA_STATUS_OK;
432 }
433
434 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length,
435                                CADataType_t dataType)
436 {
437     u_arraylist_t *list = CAGetSelectedNetworkList();
438     if (!list)
439     {
440         OIC_LOG(DEBUG, TAG, "No selected network");
441         return CA_SEND_FAILED;
442     }
443
444     CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
445     size_t selectedLength = u_arraylist_length(list);
446     for (size_t i = 0; i < selectedLength; i++)
447     {
448         void* ptrType = u_arraylist_get(list, i);
449
450         if (NULL == ptrType)
451         {
452             continue;
453         }
454
455         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
456         if (0 == (connType & requestedAdapter))
457         {
458             continue;
459         }
460
461         int index = CAGetAdapterIndex(connType);
462         if (0 > index)
463         {
464             OIC_LOG(ERROR, TAG, "unknown connectivity type!");
465             continue;
466         }
467
468         uint32_t sentDataLen = 0;
469
470         if (NULL != g_adapterHandler[index].sendDataToAll)
471         {
472             void *payload = (void *) OICMalloc(length);
473             if (!payload)
474             {
475                 OIC_LOG(ERROR, TAG, "Out of memory!");
476                 return CA_MEMORY_ALLOC_FAILED;
477             }
478             memcpy(payload, data, length);
479             sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length, dataType);
480             OICFree(payload);
481         }
482
483         if (sentDataLen != length)
484         {
485             OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
486 #ifdef SINGLE_THREAD
487             //in case of single thread, no error handler. Report error immediately
488             return CA_SEND_FAILED;
489 #endif
490         }
491     }
492
493     return CA_STATUS_OK;
494 }
495
496 CAResult_t CAStartListeningServerAdapters()
497 {
498     CAResult_t result = CA_STATUS_FAILED;
499
500     u_arraylist_t *list = CAGetSelectedNetworkList();
501     if (!list)
502     {
503         OIC_LOG(ERROR, TAG, "No selected network");
504         return result;
505     }
506
507     size_t length = u_arraylist_length(list);
508     for (size_t i = 0; i < length; i++)
509     {
510         void* ptrType = u_arraylist_get(list, i);
511
512         if(ptrType == NULL)
513         {
514             continue;
515         }
516
517         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
518
519         int index = CAGetAdapterIndex(connType);
520         if (0 > index)
521         {
522             OIC_LOG(ERROR, TAG, "unknown connectivity type!");
523             continue;
524         }
525
526         if (g_adapterHandler[index].startListenServer != NULL)
527         {
528             const CAResult_t tmp =
529                 g_adapterHandler[index].startListenServer();
530
531             // Successful listen if at least one adapter started.
532             if (CA_STATUS_OK == tmp)
533             {
534                 result = tmp;
535             }
536         }
537     }
538
539     return result;
540 }
541
542 CAResult_t CAStopListeningServerAdapters()
543 {
544     u_arraylist_t *list = CAGetSelectedNetworkList();
545     if (!list)
546     {
547         OIC_LOG(ERROR, TAG, "No selected network");
548         return CA_STATUS_FAILED;
549     }
550
551     size_t length = u_arraylist_length(list);
552     for (size_t i = 0; i < length; i++)
553     {
554         void* ptrType = u_arraylist_get(list, i);
555         if(ptrType == NULL)
556         {
557             continue;
558         }
559
560         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
561
562         int index = CAGetAdapterIndex(connType);
563         if (0 > index)
564         {
565             OIC_LOG(ERROR, TAG, "unknown connectivity type!");
566             continue;
567         }
568
569         if (g_adapterHandler[index].stopListenServer != NULL)
570         {
571             g_adapterHandler[index].stopListenServer();
572         }
573     }
574
575     return CA_STATUS_OK;
576 }
577
578 CAResult_t CAStartDiscoveryServerAdapters()
579 {
580     CAResult_t result = CA_STATUS_FAILED;
581
582     u_arraylist_t *list = CAGetSelectedNetworkList();
583
584     if (!list)
585     {
586         OIC_LOG(ERROR, TAG, "No selected network");
587         return result;
588     }
589
590     size_t length = u_arraylist_length(list);
591     for (size_t i = 0; i < length; i++)
592     {
593         void* ptrType = u_arraylist_get(list, i);
594
595         if(ptrType == NULL)
596         {
597             continue;
598         }
599
600         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
601
602         int index = CAGetAdapterIndex(connType);
603         if (0 > index)
604         {
605             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
606             continue;
607         }
608
609         if (g_adapterHandler[index].startDiscoveryServer != NULL)
610         {
611             const CAResult_t tmp =
612                 g_adapterHandler[index].startDiscoveryServer();
613
614             // Successful discovery if at least one adapter started.
615             if (CA_STATUS_OK == tmp)
616             {
617                 result = tmp;
618             }
619         }
620     }
621
622     return result;
623 }
624
625 void CATerminateAdapters()
626 {
627     for (uint32_t index = 0; index < g_numberOfAdapters; index++)
628     {
629         if (g_adapterHandler[index].terminate != NULL)
630         {
631             g_adapterHandler[index].terminate();
632         }
633     }
634
635     OICFree(g_adapterHandler);
636     g_adapterHandler = NULL;
637     g_numberOfAdapters = 0;
638 }
639
640 #ifdef SINGLE_THREAD
641 CAResult_t CAReadData()
642 {
643     u_arraylist_t *list = CAGetSelectedNetworkList();
644
645     if (!list)
646     {
647         return CA_STATUS_FAILED;
648     }
649
650     uint8_t i = 0;
651     for (i = 0; i < u_arraylist_length(list); i++)
652     {
653         void *ptrType = u_arraylist_get(list, i);
654         if (NULL == ptrType)
655         {
656             OIC_LOG(ERROR, TAG, "get list fail");
657             return CA_STATUS_FAILED;
658         }
659
660         CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
661
662         int index = CAGetAdapterIndex(connType);
663         if (0 > index)
664         {
665             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
666             continue;
667         }
668
669         if (g_adapterHandler[index].readData != NULL)
670         {
671             g_adapterHandler[index].readData();
672         }
673     }
674
675     return CA_STATUS_OK;
676 }
677 #endif
678