Debian Codegraph @

lib/interface.c

raw symbols SCIP
1 /*
2 *
3 * Connection Manager UI
4 *
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include <gdbus/gdbus.h>
23
24 #include <connman-private.h>
25
26 DBusConnection *dbus_cnx_session = NULL;
27 struct connman_interface *connman = NULL;
28
29 static void connman_watch_interface_connected(DBusConnection *dbus_cnx,
30 void *user_data)
31 {
32 if (connman == NULL || connman->interface_connected_cb == NULL)
33 return;
34
35 connman->interface_connected_cb(connman->user_data);
36 }
37
38 static void connman_watch_interface_disconnected(DBusConnection *dbus_cnx,
39 void *user_data)
40 {
41 if (connman == NULL || connman->interface_disconnected_cb == NULL)
42 return;
43
44 connman->interface_disconnected_cb(connman->user_data);
45 }
46
47 int connman_interface_init(connman_interface_cb_f interface_connected_cb,
48 connman_interface_cb_f interface_disconnected_cb,
49 void *user_data)
50 {
51 DBusConnection *dbus_cnx;
52 DBusError error;
53
54 if (connman != NULL)
55 return 0;
56
57 if (interface_connected_cb == NULL ||
58 interface_disconnected_cb == NULL)
59 return -EINVAL;
60
61 dbus_cnx = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
62 if (dbus_cnx == NULL)
63 return -ENOMEM;
64
65 dbus_cnx_session = g_dbus_setup_bus(DBUS_BUS_SESSION, NULL, NULL);
66 if (dbus_cnx_session == NULL) {
67 dbus_connection_unref(dbus_cnx);
68 return -ENOMEM;
69 }
70
71 dbus_error_init(&error);
72 if (g_dbus_request_name(dbus_cnx_session,
73 "net.connman.ConnmanUI", &error) == FALSE) {
74 if (dbus_error_is_set(&error) == TRUE) {
75 printf("Error: %s\n", error.message);
76 dbus_error_free(&error);
77 }
78
79 dbus_connection_unref(dbus_cnx);
80 dbus_connection_unref(dbus_cnx_session);
81
82 return -EALREADY;
83 }
84
85 connman = g_try_malloc0(sizeof(struct connman_interface));
86 if (connman == NULL) {
87 dbus_connection_unref(dbus_cnx);
88 dbus_connection_unref(dbus_cnx_session);
89
90 return -ENOMEM;
91 }
92
93 connman->dbus_cnx = dbus_cnx;
94
95 connman->interface_connected_cb = interface_connected_cb;
96 connman->interface_disconnected_cb = interface_disconnected_cb;
97
98 connman->interface_watch_id = g_dbus_add_service_watch(dbus_cnx,
99 CONNMAN_DBUS_NAME,
100 connman_watch_interface_connected,
101 connman_watch_interface_disconnected,
102 NULL, NULL);
103 if (connman->interface_watch_id == 0) {
104 dbus_connection_unref(dbus_cnx);
105 dbus_connection_unref(dbus_cnx_session);
106 g_free(connman);
107
108 return -EINVAL;
109 }
110
111 /* We don't know yet if connman is present */
112 connman_watch_interface_disconnected(dbus_cnx, NULL);
113
114 connman->user_data = user_data;
115
116 return 0;
117 }
118
119 void connman_interface_finalize(void)
120 {
121 if (connman == NULL)
122 return;
123
124 g_dbus_remove_all_watches(connman->dbus_cnx);
125
126 dbus_connection_unref(connman->dbus_cnx);
127
128 if (dbus_cnx_session != NULL)
129 dbus_connection_unref(dbus_cnx_session);
130
131 dbus_cnx_session = NULL;
132
133 g_free(connman);
134
135 connman = NULL;
136 }