#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;
* 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;
+}
}
/**
* 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