#include <bluetooth/hci_lib.h>
#include <bluetooth/rfcomm.h>
-#include "bt.h"
+#include "bluetooth-scan.h"
#include "gps.h"
#include "map.h"
#include "utils.h"
#include "ui-common.h"
#include "gps-conn.h"
-struct sockaddr_rc _rcvr_addr = { 0 };
-
-static gint fd;
-
-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)) {
- g_printf("Error connecting to receiver; retrying...\n");
- /* Try again. */
- rcvr_disconnect();
- rcvr_connect_later();
-} else {
- g_printf("Connected to receiver!\n");
- gps_conn_set_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("INFO: ");
- return TRUE;
- break;
- case EHOSTUNREACH:
- g_printf("*** Bluetooth/GPS device not found.\n");
- rcvr_disconnect();
-#if 0
- popup_error(_window, _("No bluetooth or GPS device found."));
- set_action_activate("gps_enable", FALSE);
-#endif
- 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);
+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)
-{
-/* 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;
-}
-}
-
-/**
- * Connect to the receiver.
- * This method assumes that fd is -1 and _channel is NULL. If unsure, call
- * rcvr_disconnect() first.
- * 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()
-{
-if (!_rcvr_mac)
- return FALSE;
-
-_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;
-#endif
-
-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();
- } 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);
- }
- }
-#else
- /* We're in DEBUG mode, so instead of connecting, skip to FIXED. */
- gps_conn_set_state(RCVR_FIXED);
-#endif
-}
-
-_clater_sid = 0;
-return FALSE;
-}
-
-/**
- * Place a request to connect about 1 second after the function is called.
- */
-void
-rcvr_connect_later(void)
-{
-_clater_sid = g_timeout_add(1000, (GSourceFunc) rcvr_connect_now, NULL);
-}
-
gboolean
scan_bluetooth_idle(ScanInfo * scan_info)
{