--- /dev/null
+/*
+ mtetherd
+ (c) 2010 Gregor Riepl <onitake@gmail.com>
+
+ Tethering utility for Maemo
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "hal.h"
+
+static const char *USBDEV_PATH = "/org/freedesktop/Hal/devices/usb_device_1d6b_2_musb_hdrc";
+static const char *USBNET_MODULE = "g_ether";
+
+static void mtetherd_hal_device_condition(LibHalContext *ctx, const char *udi, const char *condition, const char *detail) {
+ MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
+
+ if (plugin) {
+ g_message("Got HAL condition %s on %s: %s", condition, udi, detail);
+ //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Got HAL condition %s on %s: %s", condition, udi, detail);
+ if (strcmp("ButtonPressed", condition) == 0) {
+ if (strcmp(USBDEV_PATH, udi) == 0) {
+ mtetherd_status_plugin_usb_plugged_show(plugin);
+ }
+ }
+ }
+}
+
+static void mtetherd_hal_device_added(LibHalContext *ctx, const char *udi) {
+ MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
+
+ if (plugin) {
+ g_message("Got HAL device added on %s", udi);
+ if (strcmp(USBDEV_PATH, udi) == 0) {
+ plugin->priv->net_on = TRUE;
+ enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on);
+ }
+ }
+}
+
+static void mtetherd_hal_device_removed(LibHalContext *ctx, const char *udi) {
+ MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
+
+ if (plugin) {
+ g_message("Got HAL device added on %s", udi);
+ if (strcmp(USBDEV_PATH, udi) == 0) {
+ plugin->priv->net_on = FALSE;
+ enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on);
+ }
+ }
+}
+
+gboolean mtetherd_hal_init(MTetherDStatusPlugin *plugin) {
+ if (plugin) {
+ plugin->devices = NULL;
+
+ GError *err = NULL;
+ plugin->dbus_connection = hd_status_plugin_item_get_dbus_g_connection(HD_STATUS_PLUGIN_ITEM(plugin), DBUS_BUS_SYSTEM, &err);
+ if (err) {
+ g_warning("Can't open DBUS connection: %s", err->message);
+ g_error_free(err);
+ err = NULL;
+ return FALSE;
+ } else {
+ g_message("Got DBUS Glib connection: %p", plugin->priv->dbus_connection);
+ }
+ if (plugin->dbus_connection) {
+ plugin->hal_context = libhal_ctx_new();
+ if (plugin->hal_context) {
+ if (libhal_ctx_set_dbus_connection(plugin->hal_context, dbus_g_connection_get_connection(plugin->dbus_connection))) {
+ if (!libhal_ctx_set_user_data(plugin->hal_context, plugin)) {
+ g_warning("Can't set user data of HAL context");
+ }
+ if (!libhal_ctx_set_device_condition(plugin->hal_context, mtetherd_hal_device_condition)) {
+ g_warning("Error assigning device condition callback");
+ }
+ if (!libhal_ctx_set_device_added(plugin->hal_context, mtetherd_hal_device_added)) {
+ g_warning("Error assigning device added callback");
+ }
+ if (!libhal_ctx_set_device_removed(plugin->hal_context, mtetherd_hal_device_removed)) {
+ g_warning("Error assigning device removed callback");
+ }
+ DBusError derr;
+ dbus_error_init(&derr);
+ if (!libhal_ctx_init(plugin->hal_context, &derr)) {
+ if (dbus_error_is_set(&derr)) {
+ g_warning("Error initializing HAL context (%s): %s", derr.name, derr.message);
+ dbus_error_free(&derr);
+ } else {
+ g_warning("Error initializing HAL context: unknown error");
+ }
+ libhal_ctx_free(plugin->priv->hal_context);
+ plugin->hal_context = NULL;
+ return FALSE;
+ }
+ } else {
+ g_warning("Can't set DBUS connection of HAL context");
+ libhal_ctx_free(plugin->hal_context);
+ plugin->hal_context = NULL;
+ return FALSE;
+ }
+ } else {
+ g_warning("Can't allocate HAL context");
+ return FALSE;
+ }
+ }
+ return TRUE;
+
+ }
+ return FALSE;
+}
+
+void mtetherd_hal_finalize(MTetherDStatusPlugin *plugin) {
+ if (plugin) {
+ if (plugin->hal_context) {
+ libhal_ctx_shutdown(plugin->hal_context, NULL);
+ libhal_ctx_free(plugin->hal_context);
+ }
+ if (plugin->dbus_connection) {
+ dbus_g_connection_unref(plugin->dbus_connection);
+ }
+ }
+}
+
+void mtetherd_hal_device_scan(MTetherDStatusPlugin *plugin) {
+ if (plugin && plugin->hal_context) {
+ DBusError derr;
+ dbus_error_init(&derr);
+ int ndevices = 0;
+ char **udis = libhal_find_device_by_capability(plugin->hal_context, "net", &ndevices, &derr);
+ if (dbus_error_is_set(&derr)) {
+ g_warning("Error loading list of network devices (%s): %s", derr.name, derr.message);
+ dbus_error_free(&derr);
+ }
+ if (udis) {
+ int i;
+ for (i = 0; i < ndevices; i++) {
+ char *device = libhal_device_get_property_string(plugin->hal_context, udis[i], "net.interface", &derr);
+ if (dbus_error_is_set(&derr)) {
+ g_warning("Error getting interface name of %s (%s): %s", udis[i], derr.name, derr.message);
+ dbus_error_free(&derr);
+ }
+ if (device) {
+ // Check if this one of the supported devices
+ plugin->devices = g_list_prepend(plugin->devices, udis[i]);
+ libhal_free_string(device);
+ }
+ }
+ libhal_free_string_array(udis);
+ }
+ }
+}
+