Implemented logging into separate file for debugging
[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 <string.h>
22 #include "hal.h"
23 #include "net.h"
24 #include "plugin.h"
25
26 static const char *USBDEV_PATH = "/org/freedesktop/Hal/devices/usb_device_1d6b_2_musb_hdrc";
27
28 static void mtetherd_hal_device_condition(LibHalContext *ctx, const char *udi, const char *condition, const char *detail) {
29         MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
30         
31         if (plugin) {
32                 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Got HAL condition %s on %s: %s", condition, udi, detail);
33                 //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Got HAL condition %s on %s: %s", condition, udi, detail);
34                 if (strcmp("ButtonPressed", condition) == 0) {
35                         if (strcmp(USBDEV_PATH, udi) == 0) {
36                                 //mtetherd_status_plugin_usb_plugged_show(plugin);
37                                 mtetherd_status_plugin_usb_plugged(plugin);
38                         }
39                 }
40         }
41 }
42
43 static void mtetherd_hal_device_added(LibHalContext *ctx, const char *udi) {
44         MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
45         
46         if (plugin) {
47                 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Got HAL device added on %s", udi);
48                 DBusError derr;
49                 dbus_error_init(&derr);
50                 char *interface = libhal_device_get_property_string(plugin->hal_context, udi, "net.interface", &derr);
51                 if (dbus_error_is_set(&derr)) {
52                         g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error getting interface name of %s (%s): %s", udi, derr.name, derr.message);
53                         dbus_error_free(&derr);
54                 }
55                 if (interface) {
56                         MTetherDDevice *device = mtetherd_device_new(interface, udi);
57                         if (device) {
58                                 mtetherd_status_plugin_device_added(plugin, device);
59                         }
60                         libhal_free_string(interface);
61                 }
62                 //plugin->priv->net_on = TRUE;
63                 //enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on);
64         }
65 }
66
67 static void mtetherd_hal_device_removed(LibHalContext *ctx, const char *udi) {
68         MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
69         
70         if (plugin) {
71                 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Got HAL device added on %s", udi);
72                 mtetherd_status_plugin_device_removed(plugin, udi);
73                 //plugin->priv->net_on = FALSE;
74                 //enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on);
75         }
76 }
77
78 gboolean mtetherd_hal_init(MTetherDStatusPlugin *plugin) {
79         if (plugin) {
80                 GError *err = NULL;
81                 plugin->dbus_connection = hd_status_plugin_item_get_dbus_g_connection(HD_STATUS_PLUGIN_ITEM(plugin), DBUS_BUS_SYSTEM, &err);
82                 if (err) {
83                         g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Can't open DBUS connection: %s", err->message);
84                         g_error_free(err);
85                         err = NULL;
86                         return FALSE;
87                 } else {
88                         g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Got DBUS Glib connection: %p", plugin->dbus_connection);
89                 }
90                 if (plugin->dbus_connection) {
91                         plugin->hal_context = libhal_ctx_new();
92                         if (plugin->hal_context) {
93                                 if (libhal_ctx_set_dbus_connection(plugin->hal_context, dbus_g_connection_get_connection(plugin->dbus_connection))) {
94                                         if (!libhal_ctx_set_user_data(plugin->hal_context, plugin)) {
95                                                 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Can't set user data of HAL context");
96                                         }
97                                         if (!libhal_ctx_set_device_condition(plugin->hal_context, mtetherd_hal_device_condition)) {
98                                                 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error assigning device condition callback");
99                                         }
100                                         if (!libhal_ctx_set_device_added(plugin->hal_context, mtetherd_hal_device_added)) {
101                                                 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error assigning device added callback");
102                                         }
103                                         if (!libhal_ctx_set_device_removed(plugin->hal_context, mtetherd_hal_device_removed)) {
104                                                 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error assigning device removed callback");
105                                         }
106                                         DBusError derr;
107                                         dbus_error_init(&derr);
108                                         if (!libhal_ctx_init(plugin->hal_context, &derr)) {
109                                                 if (dbus_error_is_set(&derr)) {
110                                                         g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error initializing HAL context (%s): %s", derr.name, derr.message);
111                                                         dbus_error_free(&derr);
112                                                 } else {
113                                                         g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error initializing HAL context: unknown error");
114                                                 }
115                                                 libhal_ctx_free(plugin->hal_context);
116                                                 plugin->hal_context = NULL;
117                                                 return FALSE;
118                                         }
119                                 } else {
120                                         g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Can't set DBUS connection of HAL context");
121                                         libhal_ctx_free(plugin->hal_context);
122                                         plugin->hal_context = NULL;
123                                         return FALSE;
124                                 }
125                         } else {
126                                 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Can't allocate HAL context");
127                                 return FALSE;
128                         }
129                 }
130                 return TRUE;
131                 
132         }
133         return FALSE;
134 }
135
136 void mtetherd_hal_finalize(MTetherDStatusPlugin *plugin) {
137         if (plugin) {
138                 if (plugin->hal_context) {
139                         libhal_ctx_shutdown(plugin->hal_context, NULL);
140                         libhal_ctx_free(plugin->hal_context);
141                 }
142                 if (plugin->dbus_connection) {
143                         dbus_g_connection_unref(plugin->dbus_connection);
144                 }
145         }
146 }
147
148 void mtetherd_hal_device_scan(MTetherDStatusPlugin *plugin) {
149         if (plugin && plugin->hal_context) {
150                 DBusError derr;
151                 dbus_error_init(&derr);
152                 int ndevices = 0;
153                 char **udis = libhal_find_device_by_capability(plugin->hal_context, "net", &ndevices, &derr);
154                 if (dbus_error_is_set(&derr)) {
155                         g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error loading list of network devices (%s): %s", derr.name, derr.message);
156                         dbus_error_free(&derr);
157                 }
158                 if (udis) {
159                         int i;
160                         for (i = 0; i < ndevices; i++) {
161                                 char *interface = libhal_device_get_property_string(plugin->hal_context, udis[i], "net.interface", &derr);
162                                 if (dbus_error_is_set(&derr)) {
163                                         g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error getting interface name of %s (%s): %s", udis[i], derr.name, derr.message);
164                                         dbus_error_free(&derr);
165                                 }
166                                 if (interface) {
167                                         MTetherDDevice *device = mtetherd_device_new(interface, udis[i]);
168                                         if (device) {
169                                                 mtetherd_status_plugin_device_added(plugin, device);
170                                         }
171                                         libhal_free_string(interface);
172                                 }
173                         }
174                         libhal_free_string_array(udis);
175                 }
176         }
177 }
178
179 gboolean mtetherd_usb_state(MTetherDStatusPlugin *plugin) {
180         if (plugin) {
181                 if (plugin->hal_context) {
182                         DBusError derr;
183                         dbus_error_init(&derr);
184                         dbus_bool_t plugged = libhal_device_get_property_bool(plugin->hal_context, USBDEV_PATH, "button.state.value", &derr);
185                         if (dbus_error_is_set(&derr)) {
186                                 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error getting USB plugged status (%s): %s", derr.name, derr.message);
187                                 //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error getting USB plugged status (%s): %s", derr.name, derr.message);
188                                 dbus_error_free(&derr);
189                         } else {
190                                 return plugged;
191                         }
192                 }
193         }
194         return FALSE;
195 }
196
197