]> err.no Git - mapper/commitdiff
Cleanups
authorKaj-Michael Lang <milang@onion.tal.org>
Thu, 27 Sep 2007 09:45:52 +0000 (12:45 +0300)
committerKaj-Michael Lang <milang@onion.tal.org>
Thu, 27 Sep 2007 09:45:52 +0000 (12:45 +0300)
src/bt-bluez.c
src/bt-maemo.c
src/bt.h
src/cb.h
src/gps.h

index 28c4565a71c01a42e19ac5210ad9a364276b83ab..4b4ac3ee3cd2015b783f695ffaaa8ff74778008c 100644 (file)
@@ -2,7 +2,7 @@
 
 #include <config.h>
 
-/* 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 <unistd.h>
 
 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
index d12e2b25f6d52c435908347d2369c1415837cf0b..54f5398f4c679fe9ef1b6abbd5266f0b630c24bf 100644 (file)
@@ -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;
index 567c895982fbf018371ea1d84592c88dfc0f3fab..11d732c2d4dcd9f95f8ae4e52d53f36c6c6d1a01 100644 (file)
--- 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);
 
index 126e33d48cb0e7b6a881e59fb74fd18fcc21c87e..f0312f1f6265fb7722c227a073918eabb58406e3 100644 (file)
--- 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);
index d93b32369c8bad3e5607bf9ee5807baec719455c..26a2b45fc91bfb28c1c68c5dd5da67e7d712cee2 100644 (file)
--- 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