Esempio n. 1
0
static void connect_event(GIOChannel *io, GError *err, void *user_data)
{
	int sk = g_io_channel_unix_get_fd(io);
	struct bluetooth_profile *profile = user_data;
	struct obex_server *server = profile->server;
	int type;
	int omtu = BT_TX_MTU;
	int imtu = BT_RX_MTU;
	gboolean stream = TRUE;
	socklen_t len = sizeof(int);

	if (err)
		goto drop;

	if (getsockopt(sk, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
		goto done;

	if (type != SOCK_SEQPACKET)
		goto done;

	stream = FALSE;

	/* Read MTU if io is an L2CAP socket */
	bt_io_get(io, NULL, BT_IO_OPT_OMTU, &omtu, BT_IO_OPT_IMTU, &imtu,
							BT_IO_OPT_INVALID);

done:
	if (obex_server_new_connection(server, io, omtu, imtu, stream) < 0)
		g_io_channel_shutdown(io, TRUE, NULL);

	return;

drop:
	error("%s", err->message);
	g_io_channel_shutdown(io, TRUE, NULL);
	return;
}
Esempio n. 2
0
static int usb_connect(struct obex_server *server)
{
    struct termios options;
    int fd, err, arg;
    glong flags;

    if (usb_reconnecting > 0) {
        g_source_remove(usb_reconnecting);
        usb_reconnecting = 0;
    }

    /* already connected */
    if (usb_io != NULL)
        return 0;

    fd = open(USB_DEVNODE, O_RDWR | O_NOCTTY);
    if (fd < 0)
        return fd;

    flags = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);

    tcgetattr(fd, &options);
    cfmakeraw(&options);
    options.c_oflag &= ~ONLCR;
    tcsetattr(fd, TCSANOW, &options);

    arg = fcntl(fd, F_GETFL);
    if (arg < 0) {
        err = -errno;
        goto failed;
    }

    arg |= O_NONBLOCK;
    if (fcntl(fd, F_SETFL, arg) < 0) {
        err = -errno;
        goto failed;
    }

    usb_io = g_io_channel_unix_new(fd);
    g_io_channel_set_close_on_unref(usb_io, TRUE);

    err = obex_server_new_connection(server, usb_io,
                                     USB_TX_MTU, USB_RX_MTU);
    if (err < 0)
        goto failed;

    usb_watch = g_io_add_watch(usb_io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
                               usb_watchdog, server);

    DBG("Successfully opened %s", USB_DEVNODE);

    return 0;

failed:
    error("usb: %s (%d)", strerror(-err), -err);
    if (usb_io == NULL)
        close(fd);
    else
        usb_disconnect(server);
    return err;
}