From 6b1a9e24cbbbb6fb5c2f2459f2556c1aee8cb23c Mon Sep 17 00:00:00 2001 From: Jiwan Kim Date: Wed, 15 Feb 2017 20:22:38 +0900 Subject: [PATCH] Change logic of zbl_disable - Change to zbl_disable only sends 'disable' dbus method. - Move its dbus deinitialize logic into zbl_dbus_stop() Change-Id: I0650fc9917c755ea66e57d22942814c5aaa74b15 Signed-off-by: Jiwan Kim --- lib/zbl-dbus.c | 165 +++++++++++++++++++++++++------------------------ lib/zbl-dbus.h | 2 +- lib/zbl.c | 2 +- 3 files changed, 85 insertions(+), 84 deletions(-) diff --git a/lib/zbl-dbus.c b/lib/zbl-dbus.c index fcf5f37..4baa43b 100644 --- a/lib/zbl-dbus.c +++ b/lib/zbl-dbus.c @@ -139,13 +139,6 @@ typedef struct { #define ZCL_REPORTING_DIRECTION_REPORTED 0x00 #define ZCL_REPORTING_DIRECTION_RECEIVED 0x01 -static GDBusConnection *_zbl_get_connection(void) -{ - GError *error = NULL; - - return g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); -} - static GDBusProxy *_zbl_get_manager_proxy(void) { GDBusProxy *proxy = NULL; @@ -937,14 +930,14 @@ static void _zbl_signal_handler(GDBusConnection *connection, static void _zbl_dbus_unsubscribe_signal(GList *sub_ids) { - GDBusConnection *conn = _zbl_get_connection(); - if (NULL == conn || NULL == sub_ids) - return; + RET_IF(NULL == gdbus_conn); + RET_IF(NULL == sub_ids); while (sub_ids) { - g_dbus_connection_signal_unsubscribe(conn, GPOINTER_TO_UINT(sub_ids)); - sub_ids = g_list_remove(sub_ids, sub_ids); + g_dbus_connection_signal_unsubscribe(gdbus_conn, GPOINTER_TO_UINT(sub_ids->data)); + sub_ids = g_list_remove(sub_ids, sub_ids->data); } + g_list_free(sub_ids); } static int _zbl_dbus_subscribe_signal(zigbee_h handle) @@ -968,6 +961,7 @@ static int _zbl_dbus_subscribe_signal(zigbee_h handle) if (0 == id) { ERR("g_dbus_connection_signal_subscribe(child_rejoined) Fail(%d)\n", errno); _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; return ZIGBEE_ERROR_IO_ERROR; } handle->dbus_sub_ids = g_list_append(handle->dbus_sub_ids, GUINT_TO_POINTER(id)); @@ -979,6 +973,7 @@ static int _zbl_dbus_subscribe_signal(zigbee_h handle) if (0 == id) { ERR("g_dbus_connection_signal_subscribe(child_rejoined) Fail(%d)\n", errno); _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; return ZIGBEE_ERROR_IO_ERROR; } handle->dbus_sub_ids = g_list_append(handle->dbus_sub_ids, GUINT_TO_POINTER(id)); @@ -990,6 +985,7 @@ static int _zbl_dbus_subscribe_signal(zigbee_h handle) if (0 == id) { ERR("g_dbus_connection_signal_subscribe(child_left) Fail(%d)\n", errno); _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; return ZIGBEE_ERROR_IO_ERROR; } handle->dbus_sub_ids = g_list_append(handle->dbus_sub_ids, GUINT_TO_POINTER(id)); @@ -1001,6 +997,7 @@ static int _zbl_dbus_subscribe_signal(zigbee_h handle) if (0 == id) { ERR("g_dbus_connection_signal_subscribe(leave_network_done) Fail(%d)\n", errno); _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; return ZIGBEE_ERROR_IO_ERROR; } handle->dbus_sub_ids = g_list_append(handle->dbus_sub_ids, GUINT_TO_POINTER(id)); @@ -1012,6 +1009,7 @@ static int _zbl_dbus_subscribe_signal(zigbee_h handle) if (0 == id) { ERR("g_dbus_connection_signal_subscribe(form_network_done) Fail(%d)\n", errno); _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; return ZIGBEE_ERROR_IO_ERROR; } handle->dbus_sub_ids = g_list_append(handle->dbus_sub_ids, GUINT_TO_POINTER(id)); @@ -1025,6 +1023,7 @@ static int _zbl_dbus_subscribe_signal(zigbee_h handle) if (0 == id) { ERR("g_dbus_connection_signal_subscribe(zcl_global_default_response) Fail(%d)\n", errno); _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; return ZIGBEE_ERROR_IO_ERROR; } handle->dbus_sub_ids = g_list_append(handle->dbus_sub_ids, GUINT_TO_POINTER(id)); @@ -1037,6 +1036,7 @@ static int _zbl_dbus_subscribe_signal(zigbee_h handle) if (0 == id) { ERR("g_dbus_connection_signal_subscribe(report_attr_handler_rsp) Fail(%d)\n", errno); _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; return ZIGBEE_ERROR_IO_ERROR; } handle->dbus_sub_ids = g_list_append(handle->dbus_sub_ids, GUINT_TO_POINTER(id)); @@ -1049,6 +1049,7 @@ static int _zbl_dbus_subscribe_signal(zigbee_h handle) if (0 == id) { ERR("g_dbus_connection_signal_subscribe(alarm_count) Fail(%d)\n", errno); _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; return ZIGBEE_ERROR_IO_ERROR; } handle->dbus_sub_ids = g_list_append(handle->dbus_sub_ids, GUINT_TO_POINTER(id)); @@ -1061,6 +1062,7 @@ static int _zbl_dbus_subscribe_signal(zigbee_h handle) if (0 == id) { ERR("g_dbus_connection_signal_subscribe(status_change_rpt) Fail(%d)\n", errno); _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; return ZIGBEE_ERROR_IO_ERROR; } handle->dbus_sub_ids = g_list_append(handle->dbus_sub_ids, GUINT_TO_POINTER(id)); @@ -1072,6 +1074,7 @@ static int _zbl_dbus_subscribe_signal(zigbee_h handle) if (0 == id) { ERR("g_dbus_connection_signal_subscribe(enroll_request) Fail(%d)\n", errno); _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; return ZIGBEE_ERROR_IO_ERROR; } handle->dbus_sub_ids = g_list_append(handle->dbus_sub_ids, GUINT_TO_POINTER(id)); @@ -1085,9 +1088,12 @@ static void _zbl_dbus_name_owner_notify(GObject *object, GParamSpec *pspec, { GDBusProxy *proxy = G_DBUS_PROXY(object); gchar *name_owner = g_dbus_proxy_get_name_owner(proxy); + zigbee_h handle = (zigbee_h)user_data; + if (name_owner) return; - zbl_dbus_stop(); + + zbl_dbus_stop(handle); } static void _zbl_request_cleanup(gpointer data) @@ -3009,74 +3015,29 @@ int zbl_enable(zigbee_h handle, zb_enable_cb cb, void *user_data) int zbl_disable(void) { + int result = ZIGBEE_ERROR_NONE; GVariant *variant = NULL; GError *dbus_err = NULL; - int result = ZIGBEE_ERROR_NONE; + + RETV_IF(NULL == gdbus_conn, ZIGBEE_ERROR_IO_ERROR); + RETV_IF(NULL == manager_gproxy, ZIGBEE_ERROR_IO_ERROR); DBG("zbl_disable()"); - if (0 >= zbl_ref_count) { - WARN("dbus does not initiaized\n"); - return ZIGBEE_ERROR_NO_DATA; - } + variant = g_dbus_proxy_call_sync(manager_gproxy, "disable", NULL, + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &dbus_err); - if (0 > --zbl_ref_count) { - DBG("all connections closed\n"); - return ZIGBEE_ERROR_NONE; + if (!variant) { + ERR("Failed to get 'disable' [%s]", dbus_err->message); + g_error_free(dbus_err); + return ZIGBEE_ERROR_IO_ERROR; } - if (gdbus_conn) { - variant = g_dbus_connection_call_sync(gdbus_conn, - ZIGBEE_SERVER_NAME, - ZIGBEE_DBUS_OBJPATH, - ZIGBEE_DBUS_INTERFACE, - "disable", - NULL, NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &dbus_err); - - if (!variant) { - ERR("Failed to get 'disable' [%s]", dbus_err->message); - g_error_free(dbus_err); - } - - g_variant_get(variant, "(i)", &result); - DBG("ret = [0x%x]", result); - g_variant_unref(variant); - - g_object_unref(manager_gproxy); - manager_gproxy = NULL; - g_object_unref(service_gproxy); - service_gproxy = NULL; - g_object_unref(on_off_gproxy); - on_off_gproxy = NULL; - g_object_unref(door_lock_gproxy); - door_lock_gproxy = NULL; - g_object_unref(level_control_gproxy); - level_control_gproxy = NULL; - g_object_unref(thermostat_gproxy); - thermostat_gproxy = NULL; - g_object_unref(alarm_gproxy); - alarm_gproxy = NULL; - g_object_unref(fan_control_gproxy); - fan_control_gproxy = NULL; - - g_object_unref(mfglib_gproxy); - mfglib_gproxy = NULL; - g_object_unref(zcl_global_proxy); - zcl_global_proxy = NULL; - g_object_unref(zcl_color_control_proxy); - zcl_color_control_proxy = NULL; - g_object_unref(custom_gproxy); - custom_gproxy = NULL; - - g_object_unref(gdbus_conn); - gdbus_conn = NULL; - } + g_variant_get(variant, "(i)", &result); + DBG("ret = [0x%x]", result); + g_variant_unref(variant); - return ZIGBEE_ERROR_NONE; + return result; } int zbl_hw_reset(void) @@ -7792,6 +7753,7 @@ int zbl_dbus_start(zigbee_h handle) { FN_CALL; + GError *error = NULL; unsigned int id; if (gdbus_conn) { @@ -7799,14 +7761,15 @@ int zbl_dbus_start(zigbee_h handle) return ZIGBEE_ERROR_NONE; } - gdbus_conn = _zbl_get_connection(); - if (!gdbus_conn) { - ERR("Couldn't connect to the System bus"); + gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); + if (NULL == gdbus_conn) { + ERR("Couldn't connect to the System bus [%s]\n", error->message); + g_error_free(error); return ZIGBEE_ERROR_IO_ERROR; } id = g_signal_connect(gdbus_conn, "notify::g-name-owner", - G_CALLBACK(_zbl_dbus_name_owner_notify), NULL); + G_CALLBACK(_zbl_dbus_name_owner_notify), handle); if (0 == id) { ERR("g_signal_connect() Fail\n"); return ZIGBEE_ERROR_IO_ERROR; @@ -7901,18 +7864,56 @@ int zbl_dbus_start(zigbee_h handle) return ZIGBEE_ERROR_NONE; } -void zbl_dbus_stop(void) +void zbl_dbus_stop(zigbee_h handle) { - if (0 <= zbl_ref_count) { + DBG("zbl_dbus_stop()"); + + if (0 >= zbl_ref_count) { WARN("dbus does not initiaized\n"); return; } - if (0 > --zbl_ref_count) + + if (0 > --zbl_ref_count) { DBG("all connections closed\n"); + zbl_ref_count = 0; + return; + } - g_object_unref(gdbus_conn); - gdbus_conn = NULL; - return; + if (gdbus_conn) { + _zbl_dbus_unsubscribe_signal(handle->dbus_sub_ids); + handle->dbus_sub_ids = NULL; + + g_object_unref(manager_gproxy); + manager_gproxy = NULL; + g_object_unref(service_gproxy); + service_gproxy = NULL; + g_object_unref(on_off_gproxy); + on_off_gproxy = NULL; + g_object_unref(door_lock_gproxy); + door_lock_gproxy = NULL; + g_object_unref(level_control_gproxy); + level_control_gproxy = NULL; + g_object_unref(thermostat_gproxy); + thermostat_gproxy = NULL; + g_object_unref(alarm_gproxy); + alarm_gproxy = NULL; + g_object_unref(fan_control_gproxy); + fan_control_gproxy = NULL; + + g_object_unref(mfglib_gproxy); + mfglib_gproxy = NULL; + g_object_unref(zcl_global_proxy); + zcl_global_proxy = NULL; + g_object_unref(zcl_color_control_proxy); + zcl_color_control_proxy = NULL; + g_object_unref(custom_gproxy); + custom_gproxy = NULL; + + g_object_unref(gdbus_conn); + gdbus_conn = NULL; + + DBG("zbl_dbus_stop() finished"); + } } GDBusConnection* zbl_dbus_get_object(void) diff --git a/lib/zbl-dbus.h b/lib/zbl-dbus.h index 28d4282..c2886fe 100644 --- a/lib/zbl-dbus.h +++ b/lib/zbl-dbus.h @@ -23,7 +23,7 @@ #include int zbl_dbus_start(zigbee_h handle); -void zbl_dbus_stop(void); +void zbl_dbus_stop(zigbee_h handle); GDBusConnection* zbl_dbus_get_object(void); int zbl_dbus_get_timeout(GDBusProxy *proxy); int zbl_enable(zigbee_h handle, zb_enable_cb cb, void *user_data); diff --git a/lib/zbl.c b/lib/zbl.c index e056467..592ded9 100644 --- a/lib/zbl.c +++ b/lib/zbl.c @@ -48,7 +48,7 @@ API int zb_create(zigbee_h *handle) API void zb_destroy(zigbee_h handle) { RET_IF(NULL == handle); - zbl_dbus_stop(); + zbl_dbus_stop(handle); } API int zb_enable(zigbee_h handle, zb_enable_cb cb, void *user_data) -- 2.34.1