Split up functionality of plugin in separate modules
[mtetherd] / hal.c
1 /*
2   mtetherd
3   (c) 2010 Gregor Riepl <onitake@gmail.com>
4   
5   Tethering utility for Maemo
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 as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11   
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16   
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "hal.h"
22
23 static const char *USBDEV_PATH = "/org/freedesktop/Hal/devices/usb_device_1d6b_2_musb_hdrc";
24 static const char *USBNET_MODULE = "g_ether";
25
26 static void mtetherd_hal_device_condition(LibHalContext *ctx, const char *udi, const char *condition, const char *detail) {
27         MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
28         
29         if (plugin) {
30                 g_message("Got HAL condition %s on %s: %s", condition, udi, detail);
31                 //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Got HAL condition %s on %s: %s", condition, udi, detail);
32                 if (strcmp("ButtonPressed", condition) == 0) {
33                         if (strcmp(USBDEV_PATH, udi) == 0) {
34                                 mtetherd_status_plugin_usb_plugged_show(plugin);
35                         }
36                 }
37         }
38 }
39
40 static void mtetherd_hal_device_added(LibHalContext *ctx, const char *udi) {
41         MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
42         
43         if (plugin) {
44                 g_message("Got HAL device added on %s", udi);
45                 if (strcmp(USBDEV_PATH, udi) == 0) {
46                         plugin->priv->net_on = TRUE;
47                         enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on);
48                 }
49         }
50 }
51
52 static void mtetherd_hal_device_removed(LibHalContext *ctx, const char *udi) {
53         MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
54         
55         if (plugin) {
56                 g_message("Got HAL device added on %s", udi);
57                 if (strcmp(USBDEV_PATH, udi) == 0) {
58                         plugin->priv->net_on = FALSE;
59                         enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on);
60                 }
61         }
62 }
63
64 gboolean mtetherd_hal_init(MTetherDStatusPlugin *plugin) {
65         if (plugin) {
66                 plugin->devices = NULL;
67                 
68                 GError *err = NULL;
69                 plugin->dbus_connection = hd_status_plugin_item_get_dbus_g_connection(HD_STATUS_PLUGIN_ITEM(plugin), DBUS_BUS_SYSTEM, &err);
70                 if (err) {
71                         g_warning("Can't open DBUS connection: %s", err->message);
72                         g_error_free(err);
73                         err = NULL;
74                         return FALSE;
75                 } else {
76                         g_message("Got DBUS Glib connection: %p", plugin->priv->dbus_connection);
77                 }
78                 if (plugin->dbus_connection) {
79                         plugin->hal_context = libhal_ctx_new();
80                         if (plugin->hal_context) {
81                                 if (libhal_ctx_set_dbus_connection(plugin->hal_context, dbus_g_connection_get_connection(plugin->dbus_connection))) {
82                                         if (!libhal_ctx_set_user_data(plugin->hal_context, plugin)) {
83                                                 g_warning("Can't set user data of HAL context");
84                                         }
85                                         if (!libhal_ctx_set_device_condition(plugin->hal_context, mtetherd_hal_device_condition)) {
86                                                 g_warning("Error assigning device condition callback");
87                                         }
88                                         if (!libhal_ctx_set_device_added(plugin->hal_context, mtetherd_hal_device_added)) {
89                                                 g_warning("Error assigning device added callback");
90                                         }
91                                         if (!libhal_ctx_set_device_removed(plugin->hal_context, mtetherd_hal_device_removed)) {
92                                                 g_warning("Error assigning device removed callback");
93                                         }
94                                         DBusError derr;
95                                         dbus_error_init(&derr);
96                                         if (!libhal_ctx_init(plugin->hal_context, &derr)) {
97                                                 if (dbus_error_is_set(&derr)) {
98                                                         g_warning("Error initializing HAL context (%s): %s", derr.name, derr.message);
99                                                         dbus_error_free(&derr);
100                                                 } else {
101                                                         g_warning("Error initializing HAL context: unknown error");
102                                                 }
103                                                 libhal_ctx_free(plugin->priv->hal_context);
104                                                 plugin->hal_context = NULL;
105                                                 return FALSE;
106                                         }
107                                 } else {
108                                         g_warning("Can't set DBUS connection of HAL context");
109                                         libhal_ctx_free(plugin->hal_context);
110                                         plugin->hal_context = NULL;
111                                         return FALSE;
112                                 }
113                         } else {
114                                 g_warning("Can't allocate HAL context");
115                                 return FALSE;
116                         }
117                 }
118                 return TRUE;
119                 
120         }
121         return FALSE;
122 }
123
124 void mtetherd_hal_finalize(MTetherDStatusPlugin *plugin) {
125         if (plugin) {
126                 if (plugin->hal_context) {
127                         libhal_ctx_shutdown(plugin->hal_context, NULL);
128                         libhal_ctx_free(plugin->hal_context);
129                 }
130                 if (plugin->dbus_connection) {
131                         dbus_g_connection_unref(plugin->dbus_connection);
132                 }
133         }
134 }
135
136 void mtetherd_hal_device_scan(MTetherDStatusPlugin *plugin) {
137         if (plugin && plugin->hal_context) {
138                 DBusError derr;
139                 dbus_error_init(&derr);
140                 int ndevices = 0;
141                 char **udis = libhal_find_device_by_capability(plugin->hal_context, "net", &ndevices, &derr);
142                 if (dbus_error_is_set(&derr)) {
143                         g_warning("Error loading list of network devices (%s): %s", derr.name, derr.message);
144                         dbus_error_free(&derr);
145                 }
146                 if (udis) {
147                         int i;
148                         for (i = 0; i < ndevices; i++) {
149                                 char *device = libhal_device_get_property_string(plugin->hal_context, udis[i], "net.interface", &derr);
150                                 if (dbus_error_is_set(&derr)) {
151                                         g_warning("Error getting interface name of %s (%s): %s", udis[i], derr.name, derr.message);
152                                         dbus_error_free(&derr);
153                                 }
154                                 if (device) {
155                                         // Check if this one of the supported devices
156                                         plugin->devices = g_list_prepend(plugin->devices, udis[i]);
157                                         libhal_free_string(device);
158                                 }
159                         }
160                         libhal_free_string_array(udis);
161                 }
162         }
163 }
164