Bluetooth-Service: Add GetDiscoverableRemainTime function 90/18290/1
authorWu Jiangbo <jiangbox.wu@intel.com>
Thu, 20 Mar 2014 01:25:03 +0000 (09:25 +0800)
committerWu Jiangbo <jiangbox.wu@intel.com>
Thu, 20 Mar 2014 06:44:04 +0000 (14:44 +0800)
Change-Id: Ie84130727d8dc9d97000c7d4b619b5b523107518
Signed-off-by: Wu Jiangbo <jiangbox.wu@intel.com>
doc/bluetooth-service.txt
include/bluetooth-service.h
lib/bluetooth-service.c
src/manager.c

index 8b803ee..e95d899 100644 (file)
@@ -31,6 +31,10 @@ Object path  /org/tizen/comms/manager
 
                        Sets the default adapter to operate on.
 
+               void GetDiscoverableRemainTime(uint32 time)
+
+                       Get discoverable remain time.
+
 Properties     boolean BluetoothInService [readonly]
 
                        Indicates bluetooth service is ready for use.
index 53255ec..d14ced6 100644 (file)
@@ -32,7 +32,7 @@ int comms_lib_init(void);
 void comms_lib_deinit(void);
 void comms_manager_enable_bluetooth(void);
 void comms_manager_disable_bluetooth(void);
-
+int comms_manager_get_bt_adapter_visibale_time(void);
 
 typedef void (*comms_manager_bt_in_service_watch_t)(
                        uint in_service,
index b7223d3..7361536 100644 (file)
@@ -597,6 +597,33 @@ void comms_manager_disable_bluetooth(void)
                                        NULL, 0, -1, NULL, NULL, NULL);
 }
 
+int comms_manager_get_bt_adapter_visibale_time(void)
+{
+       GError *error = NULL;
+       unsigned int val;
+       GVariant *ret;
+
+       if (this_manager == NULL) {
+               ERROR("manager not reigster");
+               return -1;
+       }
+
+       ret = g_dbus_proxy_call_sync(this_manager->proxy,
+                                       "GetAdapterVisibleTime",
+                                       NULL, 0, -1, NULL, &error);
+       if (ret == NULL) {
+               ERROR("%s", error->message);
+               g_error_free(error);
+
+               return -1;
+       }
+
+       g_variant_get(ret, "(u)", &val);
+       g_variant_unref(ret);
+
+       return val;
+}
+
 void comms_manager_set_bt_in_service_watch(
                                comms_manager_bt_in_service_watch_t cb,
                                void *user_data)
index 31ea91c..f14503f 100644 (file)
@@ -113,6 +113,8 @@ static const GDBusMethodInfo * const _method_info_pointers[] =
        GDBUS_METHOD("DisableBluetoothService", NULL, NULL),
        GDBUS_METHOD("SetDefaultAdapter",
                        GDBUS_ARGS(_ARG("adapter", "s")), NULL),
+       GDBUS_METHOD("GetAdapterVisibleTime",
+                       NULL, GDBUS_ARGS(_ARG("time", "u"))),
        NULL
 };
 
@@ -234,6 +236,13 @@ struct bt_activate_data {
        GDBusMethodInvocation *invocation;
 };
 
+struct visible_time_t {
+       GTimeVal start_time;
+       guint32 timeout;
+};
+
+struct visible_time_t visible_time;
+
 static void adapter_powered_on(CommsManagerSkeleton *skeleton)
 {
        GDBusConnection *connection;
@@ -325,6 +334,56 @@ static void bt_adapter_set_enable(bluez_adapter_t *adapter, void *user_data)
                adapter_powered_on(adapter_activate_data->skeleton);
 }
 
+static void discoverable_changed(bluez_adapter_t *adapter,
+                               gboolean discoverable, void *user_data)
+{
+       guint32 timeout;
+
+       bluez_adapter_get_property_discoverable_timeout(adapter, &timeout);
+       if (timeout == 0)
+               return;
+
+       if (discoverable == FALSE)
+               return;
+
+       g_get_current_time(&visible_time.start_time);
+       visible_time.timeout = timeout;
+}
+
+static void discoverable_timeout_changed(bluez_adapter_t *adapter,
+                                       guint32 timeout, void *user_data)
+{
+       gboolean discoverable;
+
+       bluez_adapter_get_property_discoverable(adapter, &discoverable);
+       if (discoverable == FALSE)
+               return;
+
+       g_get_current_time(&visible_time.start_time);
+       visible_time.timeout = timeout;
+}
+
+static void set_discoverable_timer(void)
+{
+       guint32 discoverable_timeout;
+       gboolean discoverable;
+
+       bluez_adapter_get_property_discoverable(default_adapter,
+                                               &discoverable);
+       bluez_adapter_get_property_discoverable_timeout(default_adapter,
+                                               &discoverable_timeout);
+
+       if (discoverable && discoverable_timeout > 0) {
+               g_get_current_time(&visible_time.start_time);
+               visible_time.timeout = discoverable_timeout;
+       }
+
+       bluez_adapter_set_discoverable_changed_cb(default_adapter,
+                                       discoverable_changed, NULL);
+       bluez_adapter_set_discoverable_timeout_changed_cb(default_adapter,
+                                       discoverable_timeout_changed, NULL);
+}
+
 static void adapter_added_cb(bluez_adapter_t *adapter, void *user_data)
 {
        struct bt_activate_data *data = user_data;
@@ -341,6 +400,8 @@ static void adapter_added_cb(bluez_adapter_t *adapter, void *user_data)
        if (default_adapter == NULL)
                return;
 
+       set_discoverable_timer();
+
        bt_adapter_set_enable(default_adapter, data);
 
        bluez_adapter_unset_adapter_added();
@@ -444,6 +505,8 @@ static void handle_enable_bluetooth_service(GDBusConnection *connection,
                return;
        }
 
+       set_discoverable_timer();
+
        bt_adapter_set_enable(default_adapter, adapter_activate_data);
 
        return;
@@ -500,6 +563,54 @@ static void handle_set_default_adapter(GDBusConnection *connection,
        g_dbus_method_invocation_return_value(invocation, NULL);
 }
 
+static void handle_get_adapter_visible_time(GDBusConnection *connection,
+                                       GVariant *parameters,
+                                       GDBusMethodInvocation *invocation,
+                                       gpointer user_data)
+{
+       CommsManagerSkeleton *skeleton = COMMS_MANAGER_SKELETON(user_data);
+       guint32 remain_time, during, timeout;
+       gboolean discoverable;
+       GTimeVal current_time;
+
+       if (get_bluetooth_activating(skeleton)) {
+               comms_error_busy(invocation);
+               return;
+       }
+
+       if (default_adapter == NULL) {
+               comms_error_no_such_adapter(invocation);
+               return;
+       }
+
+       if (!get_bluetooth_in_service(skeleton)) {
+               comms_error_not_available(invocation);
+               return;
+       }
+
+       bluez_adapter_get_property_discoverable(default_adapter,
+                                               &discoverable);
+       bluez_adapter_get_property_discoverable_timeout(default_adapter,
+                                               &timeout);
+
+       if (discoverable == FALSE || timeout == 0) {
+               remain_time = 0;
+               goto done;
+       }
+
+       g_get_current_time(&current_time);
+
+       during = current_time.tv_sec - visible_time.start_time.tv_sec;
+       remain_time = visible_time.timeout - during;
+
+       if (remain_time < 0)
+               remain_time = 0;
+
+done:
+       g_dbus_method_invocation_return_value(invocation,
+                                       g_variant_new("(u)", remain_time));
+}
+
 static void _manager_skeleton_handle_method_call(
                                GDBusConnection *connection,
                                const gchar *sender,
@@ -510,6 +621,7 @@ static void _manager_skeleton_handle_method_call(
                                GDBusMethodInvocation *invocation,
                                gpointer user_data)
 {
+       DBG("Method name %s", method_name);
        if (g_strcmp0(method_name, "EnableBluetoothService") == 0)
                handle_enable_bluetooth_service(connection, parameters,
                                                invocation, user_data);
@@ -519,6 +631,9 @@ static void _manager_skeleton_handle_method_call(
        else if (g_strcmp0(method_name, "SetDefaultAdapter") == 0)
                handle_set_default_adapter(connection, parameters,
                                                invocation, user_data);
+       else if (g_strcmp0(method_name, "GetAdapterVisibleTime") == 0)
+               handle_get_adapter_visible_time(connection, parameters,
+                                               invocation, user_data);
        else
                WARN("Unknown method");
 }