mirror of git://git.sysmocom.de/ofono
Refactor dbus initialization & reconnect
This commit is contained in:
parent
73fe7ac6dc
commit
c31350294f
|
@ -28,6 +28,8 @@ extern "C" {
|
|||
|
||||
#include <dbus/dbus.h>
|
||||
|
||||
#define OFONO_SERVICE "org.ofono"
|
||||
|
||||
DBusConnection *ofono_dbus_get_connection();
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -31,8 +31,6 @@
|
|||
|
||||
#include "dbus-gsm.h"
|
||||
|
||||
#define SERVICE_NAME "org.ofono"
|
||||
|
||||
#define RECONNECT_RETRY_TIMEOUT 2000
|
||||
|
||||
static DBusConnection *g_connection;
|
||||
|
@ -196,53 +194,8 @@ static void dbus_gsm_set_connection(DBusConnection *conn)
|
|||
g_connection = conn;
|
||||
}
|
||||
|
||||
static gboolean system_bus_reconnect(void *user_data)
|
||||
int __ofono_dbus_init(DBusConnection *conn)
|
||||
{
|
||||
DBusConnection *conn = ofono_dbus_get_connection();
|
||||
|
||||
if (!conn && (__ofono_dbus_init() < 0))
|
||||
return TRUE;
|
||||
|
||||
conn = ofono_dbus_get_connection();
|
||||
|
||||
if (conn && dbus_connection_get_is_connected(conn))
|
||||
return FALSE;
|
||||
|
||||
ofono_error("While attempting to reconnect, conn != NULL,"
|
||||
" but not connected");
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void system_bus_disconnected(DBusConnection *conn, void *user_data)
|
||||
{
|
||||
ofono_error("System bus has disconnected!");
|
||||
|
||||
dbus_gsm_set_connection(NULL);
|
||||
|
||||
g_timeout_add(RECONNECT_RETRY_TIMEOUT,
|
||||
system_bus_reconnect, NULL);
|
||||
}
|
||||
|
||||
int __ofono_dbus_init(void)
|
||||
{
|
||||
DBusConnection *conn;
|
||||
DBusError error;
|
||||
|
||||
dbus_error_init(&error);
|
||||
|
||||
conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, SERVICE_NAME, &error);
|
||||
if (!conn) {
|
||||
ofono_error("Unable to hop onto D-Bus: %s", error.message);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (g_dbus_set_disconnect_function(conn, system_bus_disconnected,
|
||||
NULL, NULL) == FALSE) {
|
||||
dbus_connection_unref(conn);
|
||||
return -1;
|
||||
}
|
||||
|
||||
dbus_gsm_set_connection(conn);
|
||||
|
||||
return 0;
|
||||
|
@ -256,6 +209,4 @@ void __ofono_dbus_cleanup(void)
|
|||
return;
|
||||
|
||||
dbus_gsm_set_connection(NULL);
|
||||
|
||||
dbus_connection_unref(conn);
|
||||
}
|
||||
|
|
30
src/main.c
30
src/main.c
|
@ -45,6 +45,13 @@ static void sig_term(int sig)
|
|||
g_main_loop_quit(event_loop);
|
||||
}
|
||||
|
||||
static void system_bus_disconnected(DBusConnection *conn, void *user_data)
|
||||
{
|
||||
ofono_error("System bus has disconnected!");
|
||||
|
||||
g_main_loop_quit(event_loop);
|
||||
}
|
||||
|
||||
static gboolean option_detach = TRUE;
|
||||
static gboolean option_debug = FALSE;
|
||||
|
||||
|
@ -62,6 +69,8 @@ int main(int argc, char **argv)
|
|||
GOptionContext *context;
|
||||
GError *err = NULL;
|
||||
struct sigaction sa;
|
||||
DBusConnection *conn;
|
||||
DBusError error;
|
||||
|
||||
#ifdef NEED_THREADS
|
||||
if (g_thread_supported() == FALSE)
|
||||
|
@ -102,11 +111,25 @@ int main(int argc, char **argv)
|
|||
|
||||
__ofono_log_init(option_detach, option_debug);
|
||||
|
||||
if (__ofono_dbus_init() != 0)
|
||||
goto cleanup;
|
||||
dbus_error_init(&error);
|
||||
|
||||
if (__ofono_manager_init() < 0)
|
||||
conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, OFONO_SERVICE, &error);
|
||||
if (!conn) {
|
||||
if (dbus_error_is_set(&error) == TRUE) {
|
||||
ofono_error("Unable to hop onto D-Bus: %s",
|
||||
error.message);
|
||||
dbus_error_free(&error);
|
||||
} else
|
||||
ofono_error("Unable to hop onto D-Bus");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
g_dbus_set_disconnect_function(conn, system_bus_disconnected,
|
||||
NULL, NULL);
|
||||
|
||||
__ofono_dbus_init(conn);
|
||||
|
||||
__ofono_manager_init();
|
||||
|
||||
__ofono_plugin_init(NULL, NULL);
|
||||
|
||||
|
@ -129,6 +152,7 @@ int main(int argc, char **argv)
|
|||
__ofono_manager_cleanup();
|
||||
|
||||
__ofono_dbus_cleanup();
|
||||
dbus_connection_unref(conn);
|
||||
|
||||
cleanup:
|
||||
g_main_loop_unref(event_loop);
|
||||
|
|
|
@ -37,7 +37,7 @@ void __ofono_toggle_debug(void);
|
|||
|
||||
#include <ofono/dbus.h>
|
||||
|
||||
int __ofono_dbus_init(void);
|
||||
int __ofono_dbus_init(DBusConnection *conn);
|
||||
void __ofono_dbus_cleanup(void);
|
||||
|
||||
#include <ofono/plugin.h>
|
||||
|
|
Loading…
Reference in New Issue