3 (c) 2010 Gregor Riepl <onitake@gmail.com>
5 Tethering utility for Maemo
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.
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.
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/>.
23 static const char *USBDEV_PATH = "/org/freedesktop/Hal/devices/usb_device_1d6b_2_musb_hdrc";
24 static const char *USBNET_MODULE = "g_ether";
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));
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);
40 static void mtetherd_hal_device_added(LibHalContext *ctx, const char *udi) {
41 MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
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);
52 static void mtetherd_hal_device_removed(LibHalContext *ctx, const char *udi) {
53 MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
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);
64 gboolean mtetherd_hal_init(MTetherDStatusPlugin *plugin) {
66 plugin->devices = NULL;
69 plugin->dbus_connection = hd_status_plugin_item_get_dbus_g_connection(HD_STATUS_PLUGIN_ITEM(plugin), DBUS_BUS_SYSTEM, &err);
71 g_warning("Can't open DBUS connection: %s", err->message);
76 g_message("Got DBUS Glib connection: %p", plugin->priv->dbus_connection);
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");
85 if (!libhal_ctx_set_device_condition(plugin->hal_context, mtetherd_hal_device_condition)) {
86 g_warning("Error assigning device condition callback");
88 if (!libhal_ctx_set_device_added(plugin->hal_context, mtetherd_hal_device_added)) {
89 g_warning("Error assigning device added callback");
91 if (!libhal_ctx_set_device_removed(plugin->hal_context, mtetherd_hal_device_removed)) {
92 g_warning("Error assigning device removed callback");
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);
101 g_warning("Error initializing HAL context: unknown error");
103 libhal_ctx_free(plugin->priv->hal_context);
104 plugin->hal_context = NULL;
108 g_warning("Can't set DBUS connection of HAL context");
109 libhal_ctx_free(plugin->hal_context);
110 plugin->hal_context = NULL;
114 g_warning("Can't allocate HAL context");
124 void mtetherd_hal_finalize(MTetherDStatusPlugin *plugin) {
126 if (plugin->hal_context) {
127 libhal_ctx_shutdown(plugin->hal_context, NULL);
128 libhal_ctx_free(plugin->hal_context);
130 if (plugin->dbus_connection) {
131 dbus_g_connection_unref(plugin->dbus_connection);
136 void mtetherd_hal_device_scan(MTetherDStatusPlugin *plugin) {
137 if (plugin && plugin->hal_context) {
139 dbus_error_init(&derr);
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);
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);
155 // Check if this one of the supported devices
156 plugin->devices = g_list_prepend(plugin->devices, udis[i]);
157 libhal_free_string(device);
160 libhal_free_string_array(udis);