PXP: Modify APIs and Signal handle implementation
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-proximity.c
index ad1cf28..f78e301 100644 (file)
@@ -30,7 +30,9 @@
 #include "bt-service-device.h"
 #include "bt-service-proximity.h"
 
-char *_bt_convert_alert_level_to_string(int value)
+GSList *reporter_list = NULL;
+
+static char *_bt_convert_alert_level_to_string(int value)
 {
        if (value == BT_PXP_ALERT_MILD)
                return g_strdup("mild");
@@ -40,7 +42,7 @@ char *_bt_convert_alert_level_to_string(int value)
                return g_strdup("none");
 }
 
-int _bt_convert_string_to_alert_level(const char *str)
+static int _bt_convert_string_to_alert_level(const char *str)
 {
        if (g_strcmp0("high", str) == 0)
                return BT_PXP_ALERT_HIGH;
@@ -50,7 +52,19 @@ int _bt_convert_string_to_alert_level(const char *str)
        return BT_PXP_ALERT_NONE;
 }
 
-char *_bt_convert_property_to_string(int value)
+static int _bt_convert_string_to_signal_level(const char *str)
+{
+       if (g_strcmp0("good", str) == 0)
+               return BT_PXP_SIGNAL_GOOD;
+       else if (g_strcmp0("regular", str) == 0)
+               return BT_PXP_SIGNAL_REGULAR;
+       else if (g_strcmp0("weak", str) == 0)
+               return BT_PXP_SIGNAL_WEAK;
+
+       return BT_PXP_SIGNAL_NONE;
+}
+
+static char *_bt_convert_property_to_string(int value)
 {
        if (value == BT_PXP_PROPERTY_LLS)
                return g_strdup("LinkLossAlertLevel");
@@ -62,7 +76,20 @@ char *_bt_convert_property_to_string(int value)
        return NULL;
 }
 
-int bt_set_proximity_property(bluetooth_device_address_t *device_address,
+static char *_bt_proximity_reporter_find_from_list(const char *reporter_name)
+{
+       GSList *l;
+       char *reporter = NULL;
+
+       for (l = reporter_list; l != NULL; l = g_slist_next(l)) {
+               reporter = l->data;
+               if (reporter && g_strcmp0(reporter, reporter_name) == 0)
+                       return reporter;
+       }
+       return NULL;
+}
+
+int _bt_proximity_monitor_set_property(bluetooth_device_address_t *device_address,
                unsigned int property, int alert_level)
 {
        GDBusProxy *proxy;
@@ -129,8 +156,8 @@ int bt_set_proximity_property(bluetooth_device_address_t *device_address,
        return BLUETOOTH_ERROR_NONE;
 }
 
-int bt_get_proximity_property(bluetooth_device_address_t *device_address,
-               unsigned int property, int *alert_level)
+int _bt_proximity_monitor_get_property(bluetooth_device_address_t *device_address,
+               unsigned int property, int *level)
 {
        GDBusProxy *proxy;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
@@ -192,19 +219,96 @@ int bt_get_proximity_property(bluetooth_device_address_t *device_address,
        }
 
        value_str = (char *)g_variant_get_string(tmp_value, NULL);
+       if (value_str) {
+               if (property != BT_PXP_PROPERTY_TX_POWER)
+                       *level = _bt_convert_string_to_alert_level(value_str);
+               else
+                       *level = _bt_convert_string_to_signal_level(value_str);
+       }
+
+       g_variant_unref(tmp_value);
+       g_variant_unref(value);
+       g_object_unref(proxy);
+       g_free(property_str);
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_proximity_reporter_get_property(bluetooth_device_address_t *device_address,
+               unsigned int property, int *level)
+{
+       GDBusProxy *proxy;
+       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       GDBusConnection *conn;
+       char *device_path = NULL;
+       GError *error = NULL;
+       GVariant *result = NULL;
+       GVariant *tmp_value;
+       GVariant *value;
+       char *value_str = NULL;
+       char *property_str = NULL;
+
+       BT_CHECK_PARAMETER(device_address, return);
+
+       conn = _bt_gdbus_get_system_gconn();
+       retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       _bt_convert_addr_type_to_string(address, device_address->addr);
+
+       device_path = _bt_get_device_object_path(address);
+       retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_CONNECTED);
+
+       BT_INFO("device_path is created[%s]", device_path);
+
+       property_str = _bt_convert_property_to_string(property);
+
+       if (property_str == NULL)
+               return BLUETOOTH_ERROR_INTERNAL;
+
+       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                                                       NULL, BT_BLUEZ_NAME,
+                                                       device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
+
+       g_free(device_path);
+       retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       result = g_dbus_proxy_call_sync(proxy, "GetAll",
+                       g_variant_new("(s)", BT_PROXIMITY_REPORTER_INTERFACE),
+                       G_DBUS_CALL_FLAGS_NONE,
+                       -1,
+                       NULL,
+                       &error);
+       if (result == NULL) {
+               if (error != NULL) {
+                       BT_ERR("Error occured in Proxy call [%s]\n", error->message);
+                       g_error_free(error);
+               }
+               g_object_unref(proxy);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+       g_variant_get(result , "(@a{sv})", &value);
+       g_variant_unref(result);
+
+       tmp_value = g_variant_lookup_value(value, property_str, G_VARIANT_TYPE_STRING);
+       if (tmp_value == NULL) {
+               g_object_unref(proxy);
+               g_variant_unref(value);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       value_str = (char *)g_variant_get_string(tmp_value, NULL);
        if (value_str)
-               *alert_level = _bt_convert_string_to_alert_level(value_str);
+               *level = _bt_convert_string_to_alert_level(value_str);
 
        g_variant_unref(tmp_value);
        g_variant_unref(value);
        g_object_unref(proxy);
        g_free(property_str);
-       g_free(value_str);
 
        return BLUETOOTH_ERROR_NONE;
 }
 
-int bt_get_proximity_supported_services(bluetooth_device_address_t *device_address,
+int _bt_proximity_monitor_get_supported_services(bluetooth_device_address_t *device_address,
                unsigned int *supported_services)
 {
        GDBusProxy *proxy;
@@ -294,18 +398,22 @@ int bt_get_proximity_supported_services(bluetooth_device_address_t *device_addre
        return BLUETOOTH_ERROR_NONE;
 }
 
-int bt_register_proximity_reporter()
+int _bt_proximity_reporter_register(const char *sender)
 {
        GDBusProxy *proxy;
 
        GDBusConnection *conn;
        char *adapter_path = NULL;
+       char *reporter = NULL;
        GError *error = NULL;
        GVariant *result = NULL;
 
        conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
+       if (_bt_proximity_reporter_find_from_list(sender))
+               return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
+
        adapter_path = _bt_get_adapter_path();
        if (adapter_path == NULL) {
                BT_ERR("Could not get adapter path\n");
@@ -332,12 +440,15 @@ int bt_register_proximity_reporter()
                g_object_unref(proxy);
                return BLUETOOTH_ERROR_INTERNAL;
        }
+       reporter = g_strdup(sender);
+       reporter_list = g_slist_append(reporter_list, reporter);
+
        g_object_unref(proxy);
 
        return BLUETOOTH_ERROR_NONE;
 }
 
-int bt_unregister_proximity_reporter()
+int _bt_proximity_reporter_unregister(const char *sender)
 {
        GDBusProxy *proxy;
 
@@ -345,10 +456,15 @@ int bt_unregister_proximity_reporter()
        char *adapter_path = NULL;
        GError *error = NULL;
        GVariant *result = NULL;
+       char *reporter = NULL;
 
        conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
+       reporter = _bt_proximity_reporter_find_from_list(sender);
+       if (!reporter)
+               return BLUETOOTH_ERROR_NOT_INITIALIZED;
+
        adapter_path = _bt_get_adapter_path();
        if (adapter_path == NULL) {
                BT_ERR("Could not get adapter path\n");
@@ -375,7 +491,18 @@ int bt_unregister_proximity_reporter()
                g_object_unref(proxy);
                return BLUETOOTH_ERROR_INTERNAL;
        }
+       reporter_list = g_slist_remove(reporter_list, reporter);
+       g_free(reporter);
+
        g_object_unref(proxy);
 
        return BLUETOOTH_ERROR_NONE;
 }
+
+void _bt_proximity_reporter_stop_by_terminated_process(const char *terminated_name)
+{
+       if (!_bt_proximity_reporter_find_from_list(terminated_name))
+               return;
+       BT_ERR("Stop Proximity Reporter by terminated process(%s).", terminated_name);
+       _bt_proximity_reporter_unregister(terminated_name);
+}