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/>.
26 static const char *USBDEV_PATH = "/org/freedesktop/Hal/devices/usb_device_1d6b_2_musb_hdrc";
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));
32 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Got HAL condition %s on %s: %s", condition, udi, detail);
33 if (g_strcmp0("ButtonPressed", condition) == 0) {
34 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "is ButtonPressed");
35 if (g_strcmp0(USBDEV_PATH, udi) == 0) {
36 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "is %s", USBDEV_PATH);
37 mtetherd_status_plugin_usb_plugged(plugin);
43 static void mtetherd_hal_device_added(LibHalContext *ctx, const char *udi) {
44 MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
47 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Got HAL device added on %s", udi);
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);
56 MTetherDDevice *device = mtetherd_device_new(interface, udi);
58 mtetherd_status_plugin_device_added(plugin, device);
60 libhal_free_string(interface);
65 static void mtetherd_hal_device_removed(LibHalContext *ctx, const char *udi) {
66 MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx));
69 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Got HAL device removed on %s", udi);
70 mtetherd_status_plugin_device_removed(plugin, udi);
74 gboolean mtetherd_hal_init(MTetherDStatusPlugin *plugin) {
77 plugin->dbus_connection = hd_status_plugin_item_get_dbus_g_connection(HD_STATUS_PLUGIN_ITEM(plugin), DBUS_BUS_SYSTEM, &err);
79 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Can't open DBUS connection: %s", err->message);
84 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Got GDBUS connection: %p", plugin->dbus_connection);
86 if (plugin->dbus_connection) {
87 plugin->hal_context = libhal_ctx_new();
88 if (plugin->hal_context) {
89 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Got HAL context: %p", plugin->hal_context);
90 if (libhal_ctx_set_dbus_connection(plugin->hal_context, dbus_g_connection_get_connection(plugin->dbus_connection))) {
91 if (!libhal_ctx_set_user_data(plugin->hal_context, plugin)) {
92 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Can't set user data of HAL context");
94 if (!libhal_ctx_set_device_condition(plugin->hal_context, mtetherd_hal_device_condition)) {
95 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error assigning device condition callback");
97 if (!libhal_ctx_set_device_added(plugin->hal_context, mtetherd_hal_device_added)) {
98 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error assigning device added callback");
100 if (!libhal_ctx_set_device_removed(plugin->hal_context, mtetherd_hal_device_removed)) {
101 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error assigning device removed callback");
104 dbus_error_init(&derr);
105 if (!libhal_ctx_init(plugin->hal_context, &derr)) {
106 if (dbus_error_is_set(&derr)) {
107 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error initializing HAL context (%s): %s", derr.name, derr.message);
108 dbus_error_free(&derr);
110 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error initializing HAL context: unknown error");
112 libhal_ctx_free(plugin->hal_context);
113 plugin->hal_context = NULL;
117 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Can't set DBUS connection of HAL context");
118 libhal_ctx_free(plugin->hal_context);
119 plugin->hal_context = NULL;
123 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Can't allocate HAL context");
127 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "HAL context initialized and connected");
134 void mtetherd_hal_finalize(MTetherDStatusPlugin *plugin) {
136 if (plugin->hal_context) {
137 libhal_ctx_shutdown(plugin->hal_context, NULL);
138 libhal_ctx_free(plugin->hal_context);
140 if (plugin->dbus_connection) {
141 dbus_g_connection_unref(plugin->dbus_connection);
146 void mtetherd_hal_device_scan(MTetherDStatusPlugin *plugin) {
147 if (plugin && plugin->hal_context) {
149 dbus_error_init(&derr);
151 char **udis = libhal_find_device_by_capability(plugin->hal_context, "net", &ndevices, &derr);
152 if (dbus_error_is_set(&derr)) {
153 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error loading list of network devices (%s): %s", derr.name, derr.message);
154 dbus_error_free(&derr);
158 for (i = 0; i < ndevices; i++) {
159 char *interface = libhal_device_get_property_string(plugin->hal_context, udis[i], "net.interface", &derr);
160 if (dbus_error_is_set(&derr)) {
161 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error getting interface name of %s (%s): %s", udis[i], derr.name, derr.message);
162 dbus_error_free(&derr);
165 MTetherDDevice *device = mtetherd_device_new(interface, udis[i]);
167 mtetherd_status_plugin_device_added(plugin, device);
169 libhal_free_string(interface);
172 libhal_free_string_array(udis);
177 gboolean mtetherd_usb_state(MTetherDStatusPlugin *plugin) {
179 if (plugin->hal_context) {
181 dbus_error_init(&derr);
182 dbus_bool_t plugged = libhal_device_get_property_bool(plugin->hal_context, USBDEV_PATH, "button.state.value", &derr);
183 if (dbus_error_is_set(&derr)) {
184 g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error getting USB plugged status (%s): %s", derr.name, derr.message);
185 //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error getting USB plugged status (%s): %s", derr.name, derr.message);
186 dbus_error_free(&derr);