Fix paired device array exceed issue
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-adapter-dbus-handler.c
index 7ce1f77..190d84f 100644 (file)
@@ -559,8 +559,6 @@ int _bt_hal_dbus_get_energy_info(uint32_t *tx_time, uint32_t *rx_time,
        GError *error = NULL;
        GVariant *result;
 
-       DBG("+");
-
        if (!tx_time || !rx_time || !idle_time || !energy_used) {
                ERR("Invalid parameter");
                return BT_STATUS_FAIL;
@@ -592,12 +590,126 @@ int _bt_hal_dbus_get_energy_info(uint32_t *tx_time, uint32_t *rx_time,
 
        g_variant_get(result, "(uuuu)", tx_time, rx_time, idle_time, energy_used);
 
-       DBG("Tx time: %d", *tx_time);
-       DBG("Rx time: %d", *rx_time);
-       DBG("Idle time: %d", *idle_time);
+       DBG("Tx time: %d, Rx time: %d, Idle time: %d", *tx_time, *rx_time, *idle_time);
+
+       g_variant_unref(result);
+       return BT_STATUS_SUCCESS;
+}
+
+int _bt_hal_dbus_get_profile_connected_devices(const char *profile_uuid)
+{
+       GDBusConnection *conn;
+       GDBusProxy *manager_proxy;
+       GVariant *result = NULL;
+       GVariant *result1 = NULL;
+       GVariantIter *iter = NULL;
+       GError *error = NULL;
+       char *object_path = NULL;
+       GVariantIter *interface_iter;
+       char *interface_str = NULL;
+       GDBusProxy *device_proxy = NULL;
+       gboolean is_connected = FALSE;
+       struct hal_ev_adapter_profile_connected_devices ev;
+
+       DBG("Get Profile Connected Devices. UUID: %s", profile_uuid);
+       memset(&ev, 0, sizeof(ev));
+
+       conn = __bt_hal_get_system_gconn();
+       if (conn == NULL)
+               return BT_STATUS_FAIL;
+
+       manager_proxy = _bt_hal_get_manager_proxy();
+       if (manager_proxy == NULL)
+               return  BT_STATUS_FAIL;
+
+       result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
+                       NULL,
+                       G_DBUS_CALL_FLAGS_NONE,
+                       -1,
+                       NULL,
+                       &error);
+
+       if (!result) {
+               if (error != NULL) {
+                       ERR("GetManagedObjects failed (Error: %s)", error->message);
+                       g_clear_error(&error);
+                       error = NULL;
+               } else
+                       ERR("GetManagedObjects failed");
+               return BT_STATUS_FAIL;
+       }
+
+       /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
+       g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
+
+       /* Parse the signature:  oa{sa{sv}}} */
+       while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
+               if (object_path == NULL)
+                       continue;
+
+               while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
+                                       &interface_str, NULL)) {
+                       if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
+                               DBG("Found a device: %s", object_path);
+                               g_free(interface_str);
+
+                               device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                                               NULL, BT_HAL_BLUEZ_NAME,
+                                               object_path, BT_HAL_DEVICE_INTERFACE,  NULL, NULL);
+
+                               if (device_proxy == NULL) {
+                                       DBG("Device don't have this service");
+                                       break;
+                               }
+
+                               result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
+                                               g_variant_new("(s)", profile_uuid),
+                                               G_DBUS_CALL_FLAGS_NONE,
+                                               -1,
+                                               NULL,
+                                               &error);
+
+                               if (result1 == NULL) {
+                                       if (error) {
+                                               ERR("Error occured in Proxy call [%s]", error->message);
+                                               g_error_free(error);
+                                               error = NULL;
+                                       }
+                                       g_object_unref(device_proxy);
+                                       break;
+                               }
+                               g_variant_get(result1, "(b)", &is_connected);
+
+                               if (is_connected == TRUE) {
+                                       char address[BT_HAL_ADDRESS_STRING_SIZE];
+                                       _bt_hal_convert_device_path_to_address(object_path, address);
+                                       DBG("Address: %s", address);
+                                       if (ev.count >= 10) {
+                                               INFO("Too many address");
+                                               break;
+                                       }
+                                       _bt_hal_convert_addr_string_to_type(ev.bdaddr_list[ev.count], address);
+                                       ev.count++;
+                               }
+
+                               g_variant_unref(result1);
+                               g_object_unref(device_proxy);
+                               break;
+                       }
+               }
+       }
+
+       if (!event_cb)
+               event_cb = _bt_hal_get_stack_message_handler();
+       if (event_cb) {
+               DBG("Sending HAL_EV_ADAPTER_PROFILE_CONNECTED_DEVICES event");
+               event_cb(HAL_EV_ADAPTER_PROFILE_CONNECTED_DEVICES, (void *)&ev, sizeof(ev));
+       }
 
-       DBG("-");
        g_variant_unref(result);
+       g_variant_iter_free(iter);
+
+       DBG("-");
        return BT_STATUS_SUCCESS;
 }
 
@@ -765,6 +877,7 @@ static gboolean __bt_adapter_all_properties_cb(gpointer user_data)
                                        _bt_hal_update_le_feature_support(name, val, &le_features);
                                        le_features_present = TRUE;
                                }
+                               g_free(val);
                                g_variant_iter_free(iter);
 
                                if (le_features_present) {
@@ -1077,8 +1190,8 @@ done:
 int _bt_hal_dbus_get_scan_mode(void)
 {
        GDBusProxy *proxy;
-       gboolean discoverable;
-       gboolean connectable;
+       gboolean discoverable = FALSE;
+       gboolean connectable = FALSE;
        GVariant *result;
        GVariant *temp;
        GError *error = NULL;
@@ -1583,7 +1696,7 @@ static gboolean __bt_adapter_bonded_devices_cb(gpointer user_data)
        /* Buffer and propety count management */
        uint8_t buf[BT_HAL_MAX_PROPERTY_BUF_SIZE];
        uint8_t addresses[BT_HAL_MAX_PROPERTY_BUF_SIZE];
-       struct hal_ev_adapter_props_changed *ev = (void*) buf;;
+       struct hal_ev_adapter_props_changed *ev = (void *)buf;
        size_t size = 0;
        size_t count = 0;
 
@@ -1631,6 +1744,11 @@ static gboolean __bt_adapter_bonded_devices_cb(gpointer user_data)
                                break;
                        }
                }
+
+               if (count >= (int)(sizeof(addresses) / BT_HAL_ADDRESS_LENGTH_MAX)) {
+                       DBG("Reached the max length of addresses. do not stored anymore");
+                       break;
+               }
        }
 
        g_variant_iter_free(iter);