#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
+#include <glib/gprintf.h>
#include <gtk/gtk.h>
#include <hildon/hildon.h>
#include "plugin.h"
#define MTETHERD_STATUS_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginPrivate))
-typedef enum {
- MTETHERD_STATUS_PLUGIN_USB_NET_UNKNOWN = 0,
- MTETHERD_STATUS_PLUGIN_USB_NET_DISABLED,
- MTETHERD_STATUS_PLUGIN_USB_NET_ENABLED,
-} MTetherDStatusPluginUsbNetState;
-
struct _MTetherDStatusPluginPrivate {
GtkWidget *enable_button;
gpointer devices;
gboolean usb_plugged;
- MTetherDStatusPluginUsbNetState usbnet_state;
+ gboolean usbnet_state;
+ FILE *log_fp;
+ guint log_handler;
};
#ifndef IMAGE_DIR
#define IMAGE_DIR "/usr/share/pixmaps"
#endif
+#ifndef BIN_DIR
+#define BIN_DIR "/usr/bin"
+#endif
#ifndef SBIN_DIR
#define SBIN_DIR "/usr/sbin"
#endif
+#ifndef TMP_DIR
+#define TMP_DIR "/tmp"
+#endif
-static const char *USBNET_MODULE = "g_ether";
// The UDI contains the MAC address and is thus unsuitable for
// loaded status checking, so we just use the interface name
static const char *USBNET_INTERFACE = "usb0";
+static const char *WAN_INTERFACE = "gprs0";
+const char *MTETHERD_LOG_DOMAIN = "mtetherd";
HD_DEFINE_PLUGIN_MODULE(MTetherDStatusPlugin, mtetherd_status_plugin, HD_TYPE_STATUS_MENU_ITEM);
static void mtetherd_status_plugin_class_finalize(MTetherDStatusPluginClass *klass) { }
static void mtetherd_status_plugin_finalize(GObject *object) {
- g_message("Destroying mtetherd status plugin");
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Destroying mtetherd status plugin");
MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(object);
if (plugin && plugin->priv) {
mtetherd_hal_finalize(plugin);
mtetherd_device_list_free(plugin->priv->devices);
+ if (plugin->priv->log_fp) {
+ g_log_remove_handler(MTETHERD_LOG_DOMAIN, plugin->priv->log_handler);
+ fclose(plugin->priv->log_fp);
+ }
}
}
static void mtetherd_status_plugin_enable_button_set_text(MTetherDStatusPlugin *plugin) {
if (plugin && plugin->priv && plugin->priv->enable_button) {
- switch (plugin->priv->usbnet_state) {
- case MTETHERD_STATUS_PLUGIN_USB_NET_ENABLED:
- hildon_button_set_text(HILDON_BUTTON(plugin->priv->enable_button), "Toggle USB Networking", "Enabled");
- break;
- case MTETHERD_STATUS_PLUGIN_USB_NET_DISABLED:
- hildon_button_set_text(HILDON_BUTTON(plugin->priv->enable_button), "Toggle USB Networking", "Disabled");
- break;
- default:
- hildon_button_set_text(HILDON_BUTTON(plugin->priv->enable_button), "Toggle USB Networking", "Unknown");
- break;
- }
+ if (plugin->priv->usbnet_state) {
+ hildon_button_set_text(HILDON_BUTTON(plugin->priv->enable_button), "USB networking", "Enabled");
+ } else {
+ hildon_button_set_text(HILDON_BUTTON(plugin->priv->enable_button), "USB networking", "Disabled");
+ }
}
}
if (plugin && plugin->priv && button == plugin->priv->enable_button) {
const char *arg;
- switch (plugin->priv->usbnet_state) {
- case MTETHERD_STATUS_PLUGIN_USB_NET_ENABLED:
- arg = SBIN_DIR "mtetherd-usbnet-disable.sh";
- break;
- case MTETHERD_STATUS_PLUGIN_USB_NET_DISABLED:
- arg = SBIN_DIR "mtetherd-usbnet-enable.sh";
- break;
- default:
- arg = NULL;
- break;
+ if (plugin->priv->usbnet_state) {
+ arg = SBIN_DIR "/mtetherd-usbnet-disable.sh";
+ } else {
+ arg = SBIN_DIR "/mtetherd-usbnet-enable.sh";
}
if (arg) {
- g_debug("Launching %s", arg);
- const char *command[] = { "/usr/bin/sudo", arg, NULL };
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Launching %s", arg);
+ const char *command[] = { BIN_DIR "/sudo", arg, NULL };
if (!mtetherd_launch_script(command)) {
- g_error("Error launching USB networking script");
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error launching USB networking script");
}
}
}
}
}
+static void mtetherd_status_plugin_log(const gchar *domain, GLogLevelFlags level, const gchar *message, gpointer data) {
+ MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(data);
+
+ if (plugin && plugin->priv && plugin->priv->log_fp) {
+ char *levelstr;
+ switch (level) {
+ case G_LOG_LEVEL_ERROR:
+ levelstr = "** Error **";
+ break;
+ case G_LOG_LEVEL_CRITICAL:
+ levelstr = "** CRITICAL **";
+ break;
+ case G_LOG_LEVEL_WARNING:
+ levelstr = "** Warning **";
+ break;
+ case G_LOG_LEVEL_MESSAGE:
+ levelstr = "-- Notice --";
+ break;
+ case G_LOG_LEVEL_INFO:
+ levelstr = "-- Info --";
+ break;
+ case G_LOG_LEVEL_DEBUG:
+ levelstr = "(Debug)";
+ break;
+ case G_LOG_FLAG_RECURSION:
+ levelstr = "** RECURSION **";
+ break;
+ case G_LOG_FLAG_FATAL:
+ levelstr = "** FATAL **";
+ break;
+ default:
+ levelstr = "";
+ break;
+ }
+ g_fprintf(plugin->priv->log_fp, "mtetherd: %s %s\n", levelstr, message);
+ fflush(plugin->priv->log_fp);
+ }
+}
+
static void mtetherd_status_plugin_init(MTetherDStatusPlugin *plugin) {
plugin->priv = MTETHERD_STATUS_PLUGIN_GET_PRIVATE(plugin);
- plugin->priv->enable_button = hildon_button_new(HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
- if (plugin->priv->enable_button) {
- GError *err = NULL;
- GdkPixbuf *icon = gdk_pixbuf_new_from_file(IMAGE_DIR "/mtetherd-net-icon.png", &err);
- if (err) {
- g_warning("Can't load mtetherd icon: %s", err->message);
- g_error_free(err);
- err = NULL;
+ if (plugin->priv) {
+ plugin->priv->log_fp = fopen(TMP_DIR "/mtetherd-status.log", "a");
+ if (plugin->priv->log_fp) {
+ plugin->priv->log_handler = g_log_set_handler(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_MASK, mtetherd_status_plugin_log, plugin);
}
- if (icon) {
- GtkWidget *image = gtk_image_new_from_pixbuf(icon);
- hildon_button_set_image(HILDON_BUTTON(plugin->priv->enable_button), image);
- hildon_button_set_image_position(HILDON_BUTTON(plugin->priv->enable_button), GTK_POS_LEFT);
- g_object_unref(icon);
+
+ plugin->priv->usbnet_state = FALSE;
+ plugin->priv->enable_button = hildon_button_new(HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
+ if (plugin->priv->enable_button) {
+ GError *err = NULL;
+ GdkPixbuf *icon = gdk_pixbuf_new_from_file(IMAGE_DIR "/mtetherd-net-icon.png", &err);
+ if (err) {
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Can't load mtetherd icon: %s", err->message);
+ g_error_free(err);
+ err = NULL;
+ }
+ if (icon) {
+ GtkWidget *image = gtk_image_new_from_pixbuf(icon);
+ hildon_button_set_image(HILDON_BUTTON(plugin->priv->enable_button), image);
+ hildon_button_set_image_position(HILDON_BUTTON(plugin->priv->enable_button), GTK_POS_LEFT);
+ gtk_button_set_alignment(GTK_BUTTON(plugin->priv->enable_button), 0, 0.5);
+ g_object_unref(icon);
+ }
+ mtetherd_status_plugin_enable_button_set_text(plugin);
+ g_signal_connect(plugin->priv->enable_button, "clicked", G_CALLBACK(mtetherd_status_plugin_enable_button_clicked), plugin);
+ gtk_container_add(GTK_CONTAINER(plugin), plugin->priv->enable_button);
+ gtk_widget_show_all(plugin->priv->enable_button);
}
- mtetherd_status_plugin_enable_button_set_text(plugin);
- g_signal_connect(plugin->priv->enable_button, "clicked", G_CALLBACK(mtetherd_status_plugin_enable_button_clicked), plugin);
- gtk_container_add(GTK_CONTAINER(plugin), plugin->priv->enable_button);
- gtk_widget_show_all(plugin->priv->enable_button);
- }
-
- if (mtetherd_hal_init(plugin)) {
- plugin->priv->usb_plugged = mtetherd_usb_state(plugin);
- plugin->priv->devices = mtetherd_device_list_new();
- mtetherd_hal_device_scan(plugin);
- } else {
- plugin->priv->usb_plugged = FALSE;
- }
-
- // Unnecessary, we scan the network device name instead
- //plugin->priv->usbnet_loaded = mtetherd_scan_modules(USBNET_MODULE);
- // Update the button status
- mtetherd_status_plugin_usb_plugged_show(plugin);
+ if (mtetherd_hal_init(plugin)) {
+ plugin->priv->devices = mtetherd_device_list_new();
+ mtetherd_hal_device_scan(plugin);
+ // Set initial button status
+ mtetherd_status_plugin_usb_plugged(plugin);
+ } else {
+ // For debugging
+ plugin->priv->usb_plugged = TRUE;
+ mtetherd_status_plugin_usb_plugged_show(plugin);
+ }
+ }
- hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Initialized mtetherd status plugin");
+ //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Initialized mtetherd status plugin");
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Initialized mtetherd status plugin");
}
void mtetherd_status_plugin_device_added(MTetherDStatusPlugin *plugin, MTetherDDevice *device) {
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "mtetherd_status_plugin_device_added(%p, %p)", plugin, device);
+ gboolean added = FALSE;
if (plugin && plugin->priv) {
const gchar *interface = mtetherd_device_get_interface(device);
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "interface=%s", interface);
if (mtetherd_device_ok(interface)) {
- if (!mtetherd_device_list_add(plugin->priv->devices, device)) {
- g_warning("Error adding network interface to list: Maximum number of devices exceeded");
- } else {
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "ok");
+ if (mtetherd_device_list_add(plugin->priv->devices, device)) {
if (g_strcmp0(USBNET_INTERFACE, interface) == 0) {
- plugin->priv->usbnet_state = MTETHERD_STATUS_PLUGIN_USB_NET_ENABLED;
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "is USB");
+ plugin->priv->usbnet_state = TRUE;
mtetherd_status_plugin_enable_button_set_text(plugin);
}
hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Starting network on %s", interface);
- // TODO: Launch network setup script
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Launching %s", SBIN_DIR "/mtetherd-net-setup.sh");
+ gchar *addr = mtetherd_device_get_addr(device);
+ gchar *netmask = mtetherd_device_get_netmask(device);
+ gchar *dhcp_start = mtetherd_device_get_dhcp_start(device);
+ gchar *dhcp_end = mtetherd_device_get_dhcp_end(device);
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "interface=%s wan=%s addr=%s netmask=%s dhcp_start=%s dhcp_end=%s", interface, WAN_INTERFACE, addr, netmask, dhcp_start, dhcp_end);
+ const char *command[] = { BIN_DIR "/sudo", SBIN_DIR "/mtetherd-net-setup.sh", interface, WAN_INTERFACE, addr, netmask, dhcp_start, dhcp_end, NULL };
+ if (!mtetherd_launch_script(command)) {
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error launching USB networking setup script");
+ }
+ g_free(addr);
+ g_free(netmask);
+ g_free(dhcp_start);
+ g_free(dhcp_end);
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "done");
+ added = TRUE;
+ } else {
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error adding network interface to list: Maximum number of devices exceeded");
}
}
}
+ if (!added) {
+ g_object_unref(G_OBJECT(device));
+ }
}
void mtetherd_status_plugin_device_removed(MTetherDStatusPlugin *plugin, const gchar *udi) {
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "mtetherd_status_plugin_device_removed(%s)", udi);
if (plugin && plugin->priv) {
MTetherDDevice *device = mtetherd_device_list_find(plugin->priv->devices, udi);
if (device) {
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "device=%p", device);
const gchar *interface = mtetherd_device_get_interface(device);
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "interface=%s", interface);
if (g_strcmp0(USBNET_INTERFACE, interface) == 0) {
- plugin->priv->usbnet_state = MTETHERD_STATUS_PLUGIN_USB_NET_DISABLED;
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "is USB");
+ plugin->priv->usbnet_state = FALSE;
mtetherd_status_plugin_enable_button_set_text(plugin);
}
hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Shutting down network on %s", interface);
- // TODO: Launch network teardown script
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Launching %s", SBIN_DIR "/mtetherd-net-shutdown.sh");
+ // TODO: Check if this is the last interface to be shut down and pass WAN_INTERFACE after interface if yes
+ const char *command[] = { BIN_DIR "/sudo", SBIN_DIR "/mtetherd-net-shutdown.sh", interface, NULL };
+ if (!mtetherd_launch_script(command)) {
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error launching USB networking shutdown script");
+ }
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "done");
}
if (!mtetherd_device_list_remove(plugin->priv->devices, udi)) {
- g_warning("Error removing network interface from list: Not found");
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Device %s not found in list, nothing removed", udi);
+ } else {
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Device %s removed from list", udi);
}
}
}
void mtetherd_status_plugin_usb_plugged(MTetherDStatusPlugin *plugin) {
+ plugin->priv->usb_plugged = mtetherd_usb_state(plugin);
+ g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "USB plugged status: %s", plugin->priv->usb_plugged ? "on" : "off");
mtetherd_status_plugin_usb_plugged_show(plugin);
}