Fix parsing logic for dbus return value
authorJiwan Kim <ji-wan.kim@samsung.com>
Thu, 1 Jun 2017 04:20:36 +0000 (13:20 +0900)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 17 Jul 2017 02:35:36 +0000 (11:35 +0900)
- return value is an array of separated dictionary : {sv}
  so g_variant_dict_lookup_value won't work this type.

src/mesh-gdbus.c

index b9b65f9..c023208 100644 (file)
@@ -461,6 +461,7 @@ static void _get_joined_network(mesh_service *service, GVariant *variant)
 {
        GVariantIter *peer = NULL;
        GVariantIter *property = NULL;
+       gchar *key = NULL;
        GVariant *val = NULL;
        gsize len = 0;
        GVariant *child;
@@ -470,33 +471,21 @@ static void _get_joined_network(mesh_service *service, GVariant *variant)
 
        g_variant_get(variant, "(a(oa{sv}))", &peer);
        while ((child = g_variant_iter_next_value(peer))) {
-               GVariantDict *dict = NULL;
-               GVariant *tmp = NULL;
                mesh_network_info_s *joined_info = NULL;
+               gboolean valid_state = TRUE;
 
                MESH_LOGD("    Child : [%s]", g_variant_get_type_string(child));
                var_string = g_variant_print(child, FALSE);
                MESH_LOGD("    %s", var_string);
                g_free(var_string);
 
-               g_variant_get(child, "(oa{sv})", &obj_path, &dict);
+               g_variant_get(child, "(oa{sv})", &obj_path, &property);
+               MESH_LOGD("  Object: [%s]", obj_path);
                if (NULL == obj_path) {
                        MESH_LOGE("Null object");
                        continue;
                }
 
-               tmp = g_variant_dict_lookup_value(dict, "State", G_VARIANT_TYPE_STRING);
-               if (NULL == tmp) continue;
-               buf = g_variant_get_string(tmp, &len);
-               MESH_LOGD("  Object: [%s]", obj_path);
-               MESH_LOGD("  State : [%s]", buf);
-
-               /* Skip ignorable state */
-               if (g_strcmp0(buf, "idle") == 0
-                       && g_strcmp0(buf, "disconnect") == 0
-                       && g_strcmp0(buf, "failure") == 0)
-                       continue;
-
                /* Create an information structure for joined network */
                joined_info = g_try_new0(mesh_network_info_s, 1);
                if (NULL == joined_info) {
@@ -504,22 +493,42 @@ static void _get_joined_network(mesh_service *service, GVariant *variant)
                        return;
                }
 
-               tmp = g_variant_dict_lookup_value(dict, "Name", G_VARIANT_TYPE_STRING);
-               if (NULL == tmp) continue;
-               buf = g_variant_get_string(tmp, &len);
-               joined_info->mesh_id = g_strdup(buf);
-               MESH_LOGD("    Mesh ID : %s", joined_info->mesh_id);
+               while (g_variant_iter_loop(property, "{sv}", &key, &val)) {
+                       if (strcasecmp(key, "Name") == 0)  {
+                               buf = g_variant_get_string(val, &len);
+                               joined_info->mesh_id = g_strdup(buf);
+                       }
+                       else if (strcasecmp(key, "Address") == 0)  {
+                               buf = g_variant_get_string(val, &len);
+                               joined_info->bssid = g_strdup(buf);
+                       }
+                       else if (strcasecmp(key, "State") == 0)  {
+                               buf = g_variant_get_string(val, &len);
+                               MESH_LOGD("    State : %s", buf);
+
+                               /* Skip ignorable state */
+                               if (g_strcmp0(buf, "idle") == 0
+                                       || g_strcmp0(buf, "disconnect") == 0
+                                       || g_strcmp0(buf, "failure") == 0) {
+                                       valid_state = FALSE;
+                                       break;
+                               }
+                       }
+                       else if (strcasecmp(key, "Frequency") == 0)  {
+                               joined_info->channel = __frequency_to_channel(g_variant_get_uint16(val));
+                       }
+               }
 
-               tmp = g_variant_dict_lookup_value(dict, "Address", G_VARIANT_TYPE_STRING);
-               if (NULL == tmp) continue;
-               buf = g_variant_get_string(tmp, &len);
-               joined_info->bssid = g_strdup(buf);
-               MESH_LOGD("    BSSID : %s", joined_info->bssid);
+               /* Skip ignorable state */
+               if (FALSE == valid_state) {
+                       g_free(joined_info->mesh_id);
+                       g_free(joined_info->bssid);
+                       continue;
+               }
 
-               tmp = g_variant_dict_lookup_value(dict, "Frequency", G_VARIANT_TYPE_UINT16);
-               joined_info->channel = __frequency_to_channel(g_variant_get_uint16(val));
+               MESH_LOGD("    Mesh ID : %s", joined_info->mesh_id);
+               MESH_LOGD("    BSSID   : %s", joined_info->bssid);
                MESH_LOGD("    Channel : %d", joined_info->channel);
-
                service->joined_network = joined_info;
 
                g_variant_iter_free(property);
@@ -560,6 +569,7 @@ static void _get_mesh_peers(mesh_service *service, GVariant *variant)
                g_variant_get(child, "(oa{sv})", &obj_path, &property);
                if (NULL == obj_path) {
                        MESH_LOGE("Null object");
+                       g_free(scan_info);
                        continue;
                }
                MESH_LOGD("    Obj path : [%s]", obj_path);
@@ -583,12 +593,12 @@ static void _get_mesh_peers(mesh_service *service, GVariant *variant)
                        else if (strcasecmp(key, "Strength") == 0)  {
                                scan_info->rssi = (gint)g_variant_get_byte(val);
                                MESH_LOGD("    RSSI : %d", scan_info->rssi);
-
-                               /* Last element */
-                               service->scanned_mesh_network =
-                                       g_list_prepend(service->scanned_mesh_network, scan_info);
                        }
                }
+               /* Last element */
+               service->scanned_mesh_network =
+                       g_list_prepend(service->scanned_mesh_network, scan_info);
+
                g_variant_iter_free(property);
        }
        g_variant_iter_free(peer);