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