Fixed CAGetNetworkInformation() aggregation problem.
authorOssama Othman <ossama.othman@intel.com>
Tue, 19 May 2015 00:00:55 +0000 (17:00 -0700)
committerErich Keane <erich.keane@intel.com>
Wed, 20 May 2015 16:45:16 +0000 (16:45 +0000)
CAGetNetworkInformation() was not displaying information of adapters
for all transports.  Fixed CALocalConnectivity_t* array aggregation in
underlying CAGetNetworkInfo() function so that interface information
from all transports is correctly combined into the array passed back
to the caller.

Change-Id: I699ceeb3e8652acc495037085f88c359e206fa7d
Signed-off-by: Ossama Othman <ossama.othman@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1028
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Abhishek Sharma <ce.abhishek@samsung.com>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/connectivity/src/cainterfacecontroller.c

index 18a8257..2abc0a8 100644 (file)
@@ -21,7 +21,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdint.h>
+#include <inttypes.h>
 
 #include "cainterfacecontroller.h"
 #include "caipadapter.h"
@@ -207,39 +207,42 @@ void CAStopAdapter(CATransportType_t transportType)
 
 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
 {
+    if (info == NULL || size == NULL)
+    {
+        return CA_STATUS_INVALID_PARAM;
+    }
+
     CALocalConnectivity_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
     uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
 
-    // #1. get information each adapter
-    uint8_t index = 0;
     CAResult_t res = CA_STATUS_FAILED;
-    for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
+    uint32_t resSize = 0;
+    for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
     {
         if (g_adapterHandler[index].GetnetInfo != NULL)
         {
-            res = g_adapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
+            // #1. get information for each adapter
+            res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
+                                                     &tempSize[index]);
 
-            OIC_LOG_V(DEBUG, TAG, "%d adapter network info size is %d res:%d", index,
-                      tempSize[index], res);
-        }
-    }
+            // #2. total size
+            if (res == CA_STATUS_OK)
+            {
+                resSize += tempSize[index];
+            }
 
-    uint32_t resSize = 0;
-    for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
-    {
-        // check information
-        if (tempInfo[index] == NULL || tempSize[index] <= 0)
-        {
-            continue;
+            OIC_LOG_V(DEBUG,
+                      TAG,
+                      "%d adapter network info size is %" PRIu32 " res:%d",
+                      index,
+                      tempSize[index],
+                      res);
         }
-
-        // #2. total size
-        resSize += tempSize[index];
     }
 
     OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
 
-    if (resSize <= 0)
+    if (resSize == 0)
     {
         if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
         {
@@ -251,46 +254,39 @@ CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
     // #3. add data into result
     // memory allocation
 
-    CALocalConnectivity_t *resInfo = (CALocalConnectivity_t *) OICCalloc(
-            resSize, sizeof(CALocalConnectivity_t));
+    CALocalConnectivity_t * resInfo = OICCalloc(resSize, sizeof(*resInfo));
     CA_MEMORY_ALLOC_CHECK(resInfo);
 
-    uint8_t pos = 0;
-    for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
+    // #4. save data
+    *info = resInfo;
+    *size = resSize;
+
+    for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
     {
         // check information
-        if (tempInfo[index] == NULL || tempSize[index] <= 0)
+        if (tempSize[index] == 0)
         {
             continue;
         }
 
-        memcpy(resInfo + pos, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
+        memcpy(resInfo,
+               tempInfo[index],
+               sizeof(*resInfo) * tempSize[index]);
 
-        pos += tempSize[index];
+        resInfo += tempSize[index];
 
         // free adapter data
         OICFree(tempInfo[index]);
         tempInfo[index] = NULL;
     }
 
-    // #5. save data
-    if ( info != NULL )
-    {
-        *info = resInfo;
-    }
-
-    if (size)
-    {
-        *size = resSize;
-    }
-
     OIC_LOG(DEBUG, TAG, "each network info save success!");
     return CA_STATUS_OK;
 
     // memory error label.
 memory_error_exit:
 
-    for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
+    for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
     {
 
         OICFree(tempInfo[index]);
@@ -490,4 +486,3 @@ void CATerminateAdapters()
         }
     }
 }
-