Merge "Merge branch 'master' into easysetup" into easysetup
[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
53
54 static CAConnectivityHandler_t *g_adapterHandler;
55
56 static uint32_t g_numberOfAdapters;
57
58 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
59
60 static CANetworkChangeCallback g_networkChangeCallback = 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 CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
128 {
129     // Call the callback.
130     if (g_networkChangeCallback != NULL)
131     {
132         g_networkChangeCallback(info, status);
133     }
134 }
135
136 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
137                                          const void *data, uint32_t dataLen,
138                                          CAResult_t result)
139 {
140     OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
141
142     // Call the callback.
143     if (g_errorHandleCallback != NULL)
144     {
145         g_errorHandleCallback(endpoint, data, dataLen, result);
146     }
147 }
148
149 void CAInitializeAdapters(ca_thread_pool_t handle)
150 {
151     OIC_LOG(DEBUG, TAG, "initialize adapters..");
152
153     // Initialize adapters and register callback.
154 #ifdef IP_ADAPTER
155     CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
156                    CAAdapterErrorHandleCallback, handle);
157 #endif /* IP_ADAPTER */
158
159 #ifdef EDR_ADAPTER
160     CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
161                     CAAdapterErrorHandleCallback, handle);
162 #endif /* EDR_ADAPTER */
163
164 #ifdef LE_ADAPTER
165     CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
166                    CAAdapterErrorHandleCallback, handle);
167 #endif /* LE_ADAPTER */
168
169 #ifdef RA_ADAPTER
170     CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
171                    handle);
172 #endif /* RA_ADAPTER */
173
174 #ifdef TCP_ADAPTER
175     CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
176                     CAAdapterErrorHandleCallback, handle);
177 #endif /* TCP_ADAPTER */
178
179 #ifdef NFC_ADAPTER
180     CAInitializeNFC(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
181                     CAAdapterErrorHandleCallback, handle);
182 #endif /* NFC_ADAPTER */
183 }
184
185 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
186 {
187     OIC_LOG(DEBUG, TAG, "Set Receiver handle callback");
188
189     g_networkPacketReceivedCallback = callback;
190 }
191
192 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
193 {
194     OIC_LOG(DEBUG, TAG, "Set network handle callback");
195
196     g_networkChangeCallback = callback;
197 }
198
199 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
200 {
201     OIC_LOG(DEBUG, TAG, "Set error handle callback");
202     g_errorHandleCallback = errorCallback;
203 }
204
205 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
206 {
207     OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
208
209     int index = CAGetAdapterIndex(transportType);
210     if (0 > index)
211     {
212         OIC_LOG(ERROR, TAG, "unknown connectivity type!");
213         return CA_STATUS_FAILED;
214     }
215
216     CAResult_t res = CA_STATUS_FAILED;
217     if (g_adapterHandler[index].startAdapter != NULL)
218     {
219         res = g_adapterHandler[index].startAdapter();
220     }
221
222     return res;
223 }
224
225 void CAStopAdapter(CATransportAdapter_t transportType)
226 {
227     OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
228
229     int index = CAGetAdapterIndex(transportType);
230     if (0 > index)
231     {
232         OIC_LOG(ERROR, TAG, "unknown transport type!");
233         return;
234     }
235
236     if (g_adapterHandler[index].stopAdapter != NULL)
237     {
238         g_adapterHandler[index].stopAdapter();
239     }
240 }
241
242 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
243 {
244     if (info == NULL || size == NULL)
245     {
246         return CA_STATUS_INVALID_PARAM;
247     }
248
249     CAEndpoint_t **tempInfo = (CAEndpoint_t**) OICCalloc(g_numberOfAdapters, sizeof(*tempInfo));
250     uint32_t *tempSize =(uint32_t*) OICCalloc(g_numberOfAdapters, sizeof(*tempSize));
251
252     CAResult_t res = CA_STATUS_FAILED;
253     size_t resSize = 0;
254     for (uint32_t index = 0; index < g_numberOfAdapters; index++)
255     {
256         if (g_adapterHandler[index].GetnetInfo != NULL)
257         {
258             // #1. get information for each adapter
259             res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
260                                                      &tempSize[index]);
261
262             // #2. total size
263             if (res == CA_STATUS_OK)
264             {
265                 resSize += tempSize[index];
266             }
267
268             OIC_LOG_V(DEBUG,
269                       TAG,
270                       "%zu adapter network info size is %" PRIu32 " res:%d",
271                       index,
272                       tempSize[index],
273                       res);
274         }
275     }
276
277     OIC_LOG_V(DEBUG, TAG, "network info total size is %zu!", resSize);
278
279     if (resSize == 0)
280     {
281         if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
282         {
283             OICFree(tempInfo);
284             OICFree(tempSize);
285             return res;
286         }
287         return CA_STATUS_FAILED;
288     }
289
290     // #3. add data into result
291     // memory allocation
292     CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
293     CA_MEMORY_ALLOC_CHECK(resInfo);
294
295     // #4. save data
296     *info = resInfo;
297     *size = resSize;
298
299     for (uint32_t index = 0; index < g_numberOfAdapters; index++)
300     {
301         // check information
302         if (tempSize[index] == 0)
303         {
304             continue;
305         }
306
307         memcpy(resInfo,
308                tempInfo[index],
309                sizeof(*resInfo) * tempSize[index]);
310
311         resInfo += tempSize[index];
312
313         // free adapter data
314         OICFree(tempInfo[index]);
315         tempInfo[index] = NULL;
316     }
317     OICFree(tempInfo);
318     OICFree(tempSize);
319
320     OIC_LOG(DEBUG, TAG, "each network info save success!");
321     return CA_STATUS_OK;
322
323     // memory error label.
324 memory_error_exit:
325
326     for (uint32_t index = 0; index < g_numberOfAdapters; index++)
327     {
328         OICFree(tempInfo[index]);
329         tempInfo[index] = NULL;
330     }
331     OICFree(tempInfo);
332     OICFree(tempSize);
333
334     return CA_MEMORY_ALLOC_FAILED;
335 }
336
337 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
338 {
339     if (endpoint == NULL)
340     {
341         OIC_LOG(DEBUG, TAG, "Invalid endpoint");
342         return CA_STATUS_INVALID_PARAM;
343     }
344
345
346     u_arraylist_t *list = CAGetSelectedNetworkList();
347     if (!list)
348     {
349         OIC_LOG(ERROR, TAG, "No selected network");
350         return CA_SEND_FAILED;
351     }
352     CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
353
354     for (uint32_t i = 0; i < u_arraylist_length(list); i++)
355     {
356         void* ptrType = u_arraylist_get(list, i);
357
358         if (NULL == ptrType)
359         {
360             continue;
361         }
362
363         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
364         if (0 == (connType & requestedAdapter))
365         {
366             continue;
367         }
368
369         int index = CAGetAdapterIndex(connType);
370
371         if (-1 == index)
372         {
373             OIC_LOG(ERROR, TAG, "unknown transport type!");
374             return CA_STATUS_INVALID_PARAM;
375         }
376
377         int32_t sentDataLen = 0;
378
379         if (NULL != g_adapterHandler[index].sendData)
380         {
381             OIC_LOG(DEBUG, TAG, "unicast message to adapter");
382             sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
383         }
384
385         if (sentDataLen != (int32_t)length)
386         {
387             OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
388 #ifdef SINGLE_THREAD
389             //in case of single thread, no error handler. Report error immediately
390             return CA_SEND_FAILED;
391 #endif
392         }
393
394     }
395
396     return CA_STATUS_OK;
397 }
398
399 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
400 {
401     u_arraylist_t *list = CAGetSelectedNetworkList();
402     if (!list)
403     {
404         OIC_LOG(DEBUG, TAG, "No selected network");
405         return CA_SEND_FAILED;
406     }
407
408     CATransportFlags_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
409     size_t selectedLength = u_arraylist_length(list);
410     for (size_t i = 0; i < selectedLength; i++)
411     {
412         void* ptrType = u_arraylist_get(list, i);
413
414         if(NULL == ptrType)
415         {
416             continue;
417         }
418
419         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
420         if (0 == (connType & requestedAdapter))
421         {
422             continue;
423         }
424
425         int index = CAGetAdapterIndex(connType);
426         if (0 > index)
427         if (-1 == index)
428         {
429             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
430             continue;
431         }
432
433         uint32_t sentDataLen = 0;
434
435         if (NULL != g_adapterHandler[index].sendDataToAll)
436         {
437             void *payload = (void *) OICMalloc(length);
438             if (!payload)
439             {
440                 OIC_LOG(ERROR, TAG, "Out of memory!");
441                 return CA_MEMORY_ALLOC_FAILED;
442             }
443             memcpy(payload, data, length);
444             sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
445             OICFree(payload);
446         }
447
448         if (sentDataLen != length)
449         {
450             OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
451 #ifdef SINGLE_THREAD
452             //in case of single thread, no error handler. Report error immediately
453             return CA_SEND_FAILED;
454 #endif
455         }
456     }
457
458     return CA_STATUS_OK;
459 }
460
461 CAResult_t CAStartListeningServerAdapters()
462 {
463     CAResult_t result = CA_STATUS_FAILED;
464
465     u_arraylist_t *list = CAGetSelectedNetworkList();
466     if (!list)
467     {
468         OIC_LOG(ERROR, TAG, "No selected network");
469         return result;
470     }
471
472     size_t length = u_arraylist_length(list);
473     for (size_t i = 0; i < length; i++)
474     {
475         void* ptrType = u_arraylist_get(list, i);
476
477         if(ptrType == NULL)
478         {
479             continue;
480         }
481
482         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
483
484         int index = CAGetAdapterIndex(connType);
485         if (0 > index)
486         {
487             OIC_LOG(ERROR, TAG, "unknown connectivity type!");
488             continue;
489         }
490
491         if (g_adapterHandler[index].startListenServer != NULL)
492         {
493             const CAResult_t tmp =
494                 g_adapterHandler[index].startListenServer();
495
496             // Successful listen if at least one adapter started.
497             if (CA_STATUS_OK == tmp)
498             {
499                 result = tmp;
500             }
501         }
502     }
503
504     return result;
505 }
506
507 CAResult_t CAStopListeningServerAdapters()
508 {
509     u_arraylist_t *list = CAGetSelectedNetworkList();
510     if (!list)
511     {
512         OIC_LOG(ERROR, TAG, "No selected network");
513         return CA_STATUS_FAILED;
514     }
515
516     size_t length = u_arraylist_length(list);
517     for (size_t i = 0; i < length; i++)
518     {
519         void* ptrType = u_arraylist_get(list, i);
520         if(ptrType == NULL)
521         {
522             continue;
523         }
524
525         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
526
527         int index = CAGetAdapterIndex(connType);
528         if (0 > index)
529         {
530             OIC_LOG(ERROR, TAG, "unknown connectivity type!");
531             continue;
532         }
533
534         if (g_adapterHandler[index].stopListenServer != NULL)
535         {
536             g_adapterHandler[index].stopListenServer();
537         }
538     }
539
540     return CA_STATUS_OK;
541 }
542
543 CAResult_t CAStartDiscoveryServerAdapters()
544 {
545     CAResult_t result = CA_STATUS_FAILED;
546
547     u_arraylist_t *list = CAGetSelectedNetworkList();
548
549     if (!list)
550     {
551         OIC_LOG(ERROR, TAG, "No selected network");
552         return result;
553     }
554
555     size_t length = u_arraylist_length(list);
556     for (size_t i = 0; i < length; i++)
557     {
558         void* ptrType = u_arraylist_get(list, i);
559
560         if(ptrType == NULL)
561         {
562             continue;
563         }
564
565         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
566
567         int index = CAGetAdapterIndex(connType);
568         if (0 > index)
569         {
570             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
571             continue;
572         }
573
574         if (g_adapterHandler[index].startDiscoveryServer != NULL)
575         {
576             const CAResult_t tmp =
577                 g_adapterHandler[index].startDiscoveryServer();
578
579             // Successful discovery if at least one adapter started.
580             if (CA_STATUS_OK == tmp)
581             {
582                 result = tmp;
583             }
584         }
585     }
586
587     return result;
588 }
589
590 void CATerminateAdapters()
591 {
592     for (uint32_t index = 0; index < g_numberOfAdapters; index++)
593     {
594         if (g_adapterHandler[index].terminate != NULL)
595         {
596             g_adapterHandler[index].terminate();
597         }
598     }
599     g_numberOfAdapters = 0;
600 }
601
602 #ifdef SINGLE_THREAD
603 CAResult_t CAReadData()
604 {
605     u_arraylist_t *list = CAGetSelectedNetworkList();
606
607     if (!list)
608     {
609         return CA_STATUS_FAILED;
610     }
611
612     uint8_t i = 0;
613     for (i = 0; i < u_arraylist_length(list); i++)
614     {
615         void *ptrType = u_arraylist_get(list, i);
616         if (NULL == ptrType)
617         {
618             OIC_LOG(ERROR, TAG, "get list fail");
619             return CA_STATUS_FAILED;
620         }
621
622         CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
623
624         int index = CAGetAdapterIndex(connType);
625         if (0 > index)
626         {
627             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
628             continue;
629         }
630
631         if (g_adapterHandler[index].readData != NULL)
632         {
633             g_adapterHandler[index].readData();
634         }
635     }
636
637     return CA_STATUS_OK;
638 }
639 #endif
640