From 050e929df27620d814b0164de1a518727dbdad67 Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Thu, 27 Sep 2007 12:45:52 +0300 Subject: [PATCH] Cleanups --- src/bt-bluez.c | 373 ++++++++++++++++++++++++------------------------- src/bt-maemo.c | 14 +- src/bt.h | 8 +- src/cb.h | 4 - src/gps.h | 1 - 5 files changed, 194 insertions(+), 206 deletions(-) diff --git a/src/bt-bluez.c b/src/bt-bluez.c index 28c4565..4b4ac3e 100644 --- a/src/bt-bluez.c +++ b/src/bt-bluez.c @@ -2,7 +2,7 @@ #include -/* XXX: well, we not using dbus yet, but anyway.. */ +/* XXX: well, we are not using dbus yet, but anyway.. */ #ifdef WITH_BLUEZ_DBUS_BT #include @@ -36,7 +36,68 @@ struct sockaddr_rc _rcvr_addr = { 0 }; -gint scan_start_search(ScanInfo * scan_info) +static gboolean +channel_cb_connect(GIOChannel * src, GIOCondition condition, gpointer data) +{ +gint error, size = sizeof(error); + +if (*_rcvr_mac != '/' && (getsockopt(_fd, SOL_SOCKET, SO_ERROR, &error, &size) || error)) { + printf("%s(): Error connecting to receiver; retrying...\n", __PRETTY_FUNCTION__); + /* Try again. */ + rcvr_disconnect(); + rcvr_connect_later(); +} else { + printf("%s(): Connected to receiver!\n", __PRETTY_FUNCTION__); + set_conn_state(RCVR_UP); + _input_sid = g_io_add_watch_full(_channel, G_PRIORITY_LOW, + G_IO_IN | G_IO_PRI, + channel_cb_input, NULL, NULL); +} + +_connect_sid = 0; +return FALSE; +} + +static gboolean +rcvr_connect_bt(void) +{ +int r, e; +r = connect(_fd, (struct sockaddr *)&_rcvr_addr, sizeof(_rcvr_addr)); +e = errno; + +/* The socket is non blocking so handle it */ +if (r != 0) { + switch (e) { + case EAGAIN: + case EBUSY: + case EINPROGRESS: + case EALREADY: + g_printf("*** Connection in progress... %d %d\n", e, r); + perror("ERROR: "); + return TRUE; + break; + case EHOSTUNREACH: + g_printf("*** Bluetooth/GPS device not found.\n"); + rcvr_disconnect(); + popup_error(_window, _("No bluetooth or GPS device found.")); + set_action_activate("gps_enable", FALSE); + return FALSE; + break; + default: + /* Connection failed. Disconnect and try again later. */ + g_printf("### Connect failed, retrying... %d %d\n", e, r); + perror("ERROR: "); + rcvr_disconnect(); + rcvr_connect_later(); + return FALSE; + break; + } +} +return TRUE; +} + +gint +scan_start_search(ScanInfo * scan_info) { scan_info->sid = g_idle_add((GSourceFunc) scan_bluetooth_idle, scan_info); return 0; @@ -46,78 +107,39 @@ return 0; * Disconnect from the receiver. This method cleans up any and everything * that might be associated with the receiver. */ -void rcvr_disconnect() +void +rcvr_disconnect(void) { - printf("%s()\n", __PRETTY_FUNCTION__); - - /* Remove watches. */ - if (_clater_sid) { - g_source_remove(_clater_sid); - _clater_sid = 0; - } - if (_error_sid) { - g_source_remove(_error_sid); - _error_sid = 0; - } - if (_connect_sid) { - g_source_remove(_connect_sid); - _connect_sid = 0; - } - if (_input_sid) { - g_source_remove(_input_sid); - _input_sid = 0; - } - - /* Destroy the GIOChannel object. */ - if (_channel) { - g_io_channel_shutdown(_channel, FALSE, NULL); - g_io_channel_unref(_channel); - _channel = NULL; - } - - /* Close the file descriptor. */ - if (_fd != -1) { - close(_fd); - _fd = -1; - } - - vprintf("%s(): return\n", __PRETTY_FUNCTION__); +/* Remove watches. */ +if (_clater_sid) { + g_source_remove(_clater_sid); + _clater_sid = 0; +} +if (_error_sid) { + g_source_remove(_error_sid); + _error_sid = 0; +} +if (_connect_sid) { + g_source_remove(_connect_sid); + _connect_sid = 0; +} +if (_input_sid) { + g_source_remove(_input_sid); + _input_sid = 0; } -static gboolean rcvr_connect_bt() -{ - int r, e; - r = connect(_fd, (struct sockaddr *)&_rcvr_addr, sizeof(_rcvr_addr)); - e = errno; +/* Destroy the GIOChannel object. */ +if (_channel) { + g_io_channel_shutdown(_channel, FALSE, NULL); + g_io_channel_unref(_channel); + _channel = NULL; +} - /* The socket is non blocking so handle it */ - if (r != 0) { - switch (e) { - case EAGAIN: - case EBUSY: - case EINPROGRESS: - case EALREADY: - g_printf("*** Connection in progress... %d %d\n", e, r); - perror("ERROR: "); - return TRUE; - break; - case EHOSTUNREACH: - g_printf("*** Bluetooth/GPS device not found.\n"); - rcvr_disconnect(); - rcvr_connect_later(); - return FALSE; - break; - default: - /* Connection failed. Disconnect and try again later. */ - g_printf("### Connect failed, retrying... %d %d\n", e, r); - perror("ERROR: "); - rcvr_disconnect(); - rcvr_connect_later(); - return FALSE; - break; - } - } - return TRUE; +/* Close the file descriptor. */ +if (_fd != -1) { + close(_fd); + _fd = -1; +} } /** @@ -127,150 +149,117 @@ static gboolean rcvr_connect_bt() * Since this is an idle function, this function returns whether or not it * should be called again, which is always FALSE. */ -gboolean rcvr_connect_now() +gboolean +rcvr_connect_now() { - printf("%s(%d)\n", __PRETTY_FUNCTION__, _conn_state); - - _rcvr_addr.rc_family = AF_BLUETOOTH; - _rcvr_addr.rc_channel = 1; - str2ba(_rcvr_mac, &_rcvr_addr.rc_bdaddr); +_rcvr_addr.rc_family = AF_BLUETOOTH; +_rcvr_addr.rc_channel = 1; +str2ba(_rcvr_mac, &_rcvr_addr.rc_bdaddr); #if 0 - _rcvr_addr.rc_channel = - gconf_client_get_int(gconf_client, GCONF_KEY_RCVR_CHAN, NULL); - if (_rcvr_addr.rc_channel < 1) - _rcvr_addr.rc_channel = 1; +_rcvr_addr.rc_channel = gconf_client_get_int(gconf_client, GCONF_KEY_RCVR_CHAN, NULL); +if (_rcvr_addr.rc_channel < 1) + _rcvr_addr.rc_channel = 1; #endif - if (_conn_state == RCVR_DOWN && _rcvr_mac) { +if (_conn_state == RCVR_DOWN && _rcvr_mac) { #ifndef DEBUG_GPS - /* Create the file descriptor. */ - if (*_rcvr_mac == '/') - _fd = open(_rcvr_mac, O_RDONLY); - else - _fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); - - /* If file descriptor creation failed, try again later. Note that - * there is no need to call rcvr_disconnect() because the file - * descriptor creation is the first step, so if it fails, there's - * nothing to clean up. */ - if (_fd == -1) { - g_printf("Failed to create socket\n"); - rcvr_connect_later(); + /* Create the file descriptor. */ + if (*_rcvr_mac == '/') + _fd = open(_rcvr_mac, O_RDONLY); + else + _fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + + /* If file descriptor creation failed, try again later. Note that + * there is no need to call rcvr_disconnect() because the file + * descriptor creation is the first step, so if it fails, there's + * nothing to clean up. */ + if (_fd == -1) { + g_printf("Failed to create socket\n"); + rcvr_connect_later(); + } else { + /* Reset GPS read buffer */ + _gps_read_buf_curr = _gps_read_buf; + *_gps_read_buf_curr = '\0'; + _gps_read_buf_last = _gps_read_buf + GPS_READ_BUF_SIZE - 1; + + /* Create channel and add watches. */ + _channel = g_io_channel_unix_new(_fd); + g_io_channel_set_encoding(_channel, NULL, NULL); + g_io_channel_set_flags(_channel, G_IO_FLAG_NONBLOCK, NULL); + _error_sid = g_io_add_watch_full(_channel, G_PRIORITY_HIGH_IDLE, + G_IO_ERR | G_IO_HUP, channel_cb_error, NULL, NULL); + _connect_sid = g_io_add_watch_full(_channel, G_PRIORITY_HIGH_IDLE, + G_IO_OUT, channel_cb_connect, NULL, NULL); + if (*_rcvr_mac != '/') { + rcvr_connect_bt(); } else { - /* Reset GPS read buffer */ - _gps_read_buf_curr = _gps_read_buf; - *_gps_read_buf_curr = '\0'; - _gps_read_buf_last = _gps_read_buf + GPS_READ_BUF_SIZE - 1; - - /* Create channel and add watches. */ - _channel = g_io_channel_unix_new(_fd); - g_io_channel_set_encoding(_channel, NULL, NULL); - g_io_channel_set_flags(_channel, G_IO_FLAG_NONBLOCK, NULL); - _error_sid = g_io_add_watch_full(_channel, G_PRIORITY_HIGH_IDLE, - G_IO_ERR | G_IO_HUP, channel_cb_error, NULL, NULL); - _connect_sid = g_io_add_watch_full(_channel, G_PRIORITY_HIGH_IDLE, - G_IO_OUT, channel_cb_connect, NULL, NULL); - if (*_rcvr_mac != '/') { - rcvr_connect_bt(); - } else { - g_printf("Using dev node: %s\n", _rcvr_mac); - } + g_printf("Using dev node: %s\n", _rcvr_mac); } + } #else - /* We're in DEBUG mode, so instead of connecting, skip to FIXED. */ - set_conn_state(RCVR_FIXED); + /* We're in DEBUG mode, so instead of connecting, skip to FIXED. */ + set_conn_state(RCVR_FIXED); #endif - } - - _clater_sid = 0; +} - vprintf("%s(): return FALSE\n", __PRETTY_FUNCTION__); - return FALSE; +_clater_sid = 0; +return FALSE; } /** * Place a request to connect about 1 second after the function is called. */ -void rcvr_connect_later() +void +rcvr_connect_later(void) { - printf("%s()\n", __PRETTY_FUNCTION__); - - _clater_sid = g_timeout_add(1000, (GSourceFunc) rcvr_connect_now, NULL); - - vprintf("%s(): return\n", __PRETTY_FUNCTION__); +_clater_sid = g_timeout_add(1000, (GSourceFunc) rcvr_connect_now, NULL); } -gboolean -channel_cb_connect(GIOChannel * src, GIOCondition condition, gpointer data) +gboolean +scan_bluetooth_idle(ScanInfo * scan_info) { - gint error, size = sizeof(error); - printf("%s(%d)\n", __PRETTY_FUNCTION__, condition); - - if (*_rcvr_mac != '/' && (getsockopt(_fd, SOL_SOCKET, SO_ERROR, &error, &size) || error)) - { - printf("%s(): Error connecting to receiver; retrying...\n", __PRETTY_FUNCTION__); - /* Try again. */ - rcvr_disconnect(); - rcvr_connect_later(); - } else { - printf("%s(): Connected to receiver!\n", __PRETTY_FUNCTION__); - set_conn_state(RCVR_UP); - _input_sid = g_io_add_watch_full(_channel, G_PRIORITY_LOW, - G_IO_IN | G_IO_PRI, - channel_cb_input, NULL, NULL); +gint devid, num_rsp; +GtkTreeIter iter; +inquiry_info *ii = NULL; + +devid = hci_get_route(NULL); + +ii = (inquiry_info *) malloc(255 * sizeof(inquiry_info)); +num_rsp = hci_inquiry(devid, 4, 255, NULL, &ii, IREQ_CACHE_FLUSH); + +if (num_rsp < 0) { + gtk_widget_destroy(scan_info->banner); + gtk_widget_hide(scan_info->scan_dialog); + popup_error(scan_info->settings_dialog, _("An error occurred while scanning.")); +} else if (num_rsp == 0) { + gtk_widget_destroy(scan_info->banner); + gtk_widget_hide(scan_info->scan_dialog); + popup_error(scan_info->settings_dialog, _("No bluetooth devices found.")); +} else { + guint i; + gint sock = hci_open_dev(devid); + for (i = 0; i < num_rsp; i++) { + gchar addr[19] = { 0 }; + gchar name[256] = { 0 }; + + ba2str(&ii[i].bdaddr, addr); + memset(name, 0, sizeof(name)); + if (hci_read_remote_name + (sock, &ii[i].bdaddr, sizeof(name), name, 0)) + strcpy(name, _("Unknown")); + + gtk_list_store_append(scan_info->store, &iter); + gtk_list_store_set(scan_info->store, &iter, + 0, g_strdup(addr), + 1, g_strdup(name), -1); } - - _connect_sid = 0; - vprintf("%s(): return\n", __PRETTY_FUNCTION__); - return FALSE; + close(sock); + gtk_widget_destroy(scan_info->banner); } - -gboolean scan_bluetooth_idle(ScanInfo * scan_info) -{ - gint devid, num_rsp; - GtkTreeIter iter; - inquiry_info *ii = NULL; - - devid = hci_get_route(NULL); - - ii = (inquiry_info *) malloc(255 * sizeof(inquiry_info)); - num_rsp = hci_inquiry(devid, 4, 255, NULL, &ii, IREQ_CACHE_FLUSH); - - if (num_rsp < 0) { - gtk_widget_destroy(scan_info->banner); - gtk_widget_hide(scan_info->scan_dialog); - popup_error(scan_info->settings_dialog, - _("An error occurred while scanning.")); - } else if (num_rsp == 0) { - gtk_widget_destroy(scan_info->banner); - gtk_widget_hide(scan_info->scan_dialog); - popup_error(scan_info->settings_dialog, - _("No bluetooth devices found.")); - } else { - guint i; - gint sock = hci_open_dev(devid); - for (i = 0; i < num_rsp; i++) { - gchar addr[19] = { 0 }; - gchar name[256] = { 0 }; - - ba2str(&ii[i].bdaddr, addr); - memset(name, 0, sizeof(name)); - if (hci_read_remote_name - (sock, &ii[i].bdaddr, sizeof(name), name, 0)) - strcpy(name, _("Unknown")); - - gtk_list_store_append(scan_info->store, &iter); - gtk_list_store_set(scan_info->store, &iter, - 0, g_strdup(addr), - 1, g_strdup(name), -1); - } - close(sock); - gtk_widget_destroy(scan_info->banner); - } - free(ii); - scan_info->sid = 0; - return FALSE; +free(ii); +scan_info->sid = 0; +return FALSE; } #endif diff --git a/src/bt-maemo.c b/src/bt-maemo.c index d12e2b2..54f5398 100644 --- a/src/bt-maemo.c +++ b/src/bt-maemo.c @@ -22,7 +22,7 @@ /*****************************************************************************/ -gboolean +static gboolean channel_cb_connect(GIOChannel * src, GIOCondition condition, gpointer data) { _gps_read_buf_curr = _gps_read_buf; @@ -37,7 +37,8 @@ return FALSE; * Disconnect from the receiver. This method cleans up any and everything * that might be associated with the receiver. */ -void rcvr_disconnect() +void +rcvr_disconnect() { GError *error = NULL; @@ -87,7 +88,8 @@ if (_rfcomm_req_proxy) { void rcvr_connect_later(); /* Forward declaration. */ -void rcvr_connect_fd(gchar * fdpath) +void +rcvr_connect_fd(gchar * fdpath) { /* If file descriptor creation failed, try again later. */ if (-1 == (_fd = open(fdpath, O_RDONLY))) { @@ -193,7 +195,8 @@ return FALSE; /** * Place a request to connect about 1 second after the function is called. */ -void rcvr_connect_later() +void +rcvr_connect_later() { _clater_sid = g_timeout_add(1000, (GSourceFunc) rcvr_connect_now, NULL); } @@ -218,7 +221,8 @@ dbus_g_proxy_disconnect_signal(sig_proxy, BTSEARCH_SEARCH_COMPLETE_SIG, G_CALLBACK(scan_cb_search_complete), scan_info); } -gint scan_start_search(ScanInfo * scan_info) +gint +scan_start_search(ScanInfo * scan_info) { GError *error = NULL; DBusGConnection *dbus_conn; diff --git a/src/bt.h b/src/bt.h index 567c895..11d732c 100644 --- a/src/bt.h +++ b/src/bt.h @@ -44,10 +44,10 @@ struct _ScanInfo { ConnState _conn_state; -void rcvr_disconnect(); -void rcvr_connect_later(); -gint scan_start_search(); -gboolean rcvr_connect_now(); +void rcvr_disconnect(void); +void rcvr_connect_later(void); +gint scan_start_search(ScanInfo * scan_info); +gboolean rcvr_connect_now(void); gboolean scan_bluetooth(GtkWidget * widget, ScanInfo * scan_info); gboolean scan_bluetooth_idle(ScanInfo * scan_info); diff --git a/src/cb.h b/src/cb.h index 126e33d..f0312f1 100644 --- a/src/cb.h +++ b/src/cb.h @@ -14,10 +14,6 @@ gboolean heading_panel_expose(GtkWidget * widget, GdkEventExpose * event); gboolean sat_panel_expose(GtkWidget * widget, GdkEventExpose * event); gboolean sat_details_panel_expose(GtkWidget * widget, GdkEventExpose * event); -gboolean channel_cb_error(GIOChannel * src, GIOCondition condition, gpointer data); -gboolean channel_cb_connect(GIOChannel * src, GIOCondition condition, gpointer data); -gboolean channel_cb_input(GIOChannel * src, GIOCondition condition, gpointer data); - /* Callbacks for the "Route" submenu. */ gboolean menu_cb_route_download(GtkAction * action); gboolean menu_cb_route_open(GtkAction * action); diff --git a/src/gps.h b/src/gps.h index d93b323..26a2b45 100644 --- a/src/gps.h +++ b/src/gps.h @@ -87,7 +87,6 @@ GpsSatelliteData _gps_sat[12]; void gps_init(void); gboolean channel_cb_error(GIOChannel * src, GIOCondition condition, gpointer data); -gboolean channel_cb_connect(GIOChannel * src, GIOCondition condition, gpointer data); gboolean channel_cb_input(GIOChannel * src, GIOCondition condition, gpointer data); #endif -- 2.39.5