Blame profiles/cups/spp.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2003-2010  Marcel Holtmann <marcel@holtmann.org>
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This program is free software; you can redistribute it and/or modify
Packit 34410b
 *  it under the terms of the GNU General Public License as published by
Packit 34410b
 *  the Free Software Foundation; either version 2 of the License, or
Packit 34410b
 *  (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This program is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 34410b
 *  GNU General Public License for more details.
Packit 34410b
 *
Packit 34410b
 *  You should have received a copy of the GNU General Public License
Packit 34410b
 *  along with this program; if not, write to the Free Software
Packit 34410b
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#include <stdio.h>
Packit 34410b
#include <errno.h>
Packit 34410b
#include <unistd.h>
Packit 34410b
#include <signal.h>
Packit 34410b
#include <sys/socket.h>
Packit 34410b
Packit 34410b
#include "lib/bluetooth.h"
Packit 34410b
#include "lib/rfcomm.h"
Packit 34410b
#include "lib/sdp.h"
Packit 34410b
#include "lib/sdp_lib.h"
Packit 34410b
Packit 34410b
#include "cups.h"
Packit 34410b
Packit 34410b
int spp_print(bdaddr_t *src, bdaddr_t *dst, uint8_t channel, int fd, int copies, const char *cups_class)
Packit 34410b
{
Packit 34410b
	struct sockaddr_rc addr;
Packit 34410b
	unsigned char buf[2048];
Packit 34410b
	int i, sk, err, len;
Packit 34410b
Packit 34410b
	if ((sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
Packit 34410b
		perror("ERROR: Can't create socket");
Packit 34410b
		if (cups_class)
Packit 34410b
			return CUPS_BACKEND_FAILED;
Packit 34410b
		else
Packit 34410b
			return CUPS_BACKEND_RETRY;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	addr.rc_family = AF_BLUETOOTH;
Packit 34410b
	bacpy(&addr.rc_bdaddr, src);
Packit 34410b
	addr.rc_channel = 0;
Packit 34410b
Packit 34410b
	if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Packit 34410b
		perror("ERROR: Can't bind socket");
Packit 34410b
		close(sk);
Packit 34410b
		if (cups_class)
Packit 34410b
			return CUPS_BACKEND_FAILED;
Packit 34410b
		else
Packit 34410b
			return CUPS_BACKEND_RETRY;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	addr.rc_family = AF_BLUETOOTH;
Packit 34410b
	bacpy(&addr.rc_bdaddr, dst);
Packit 34410b
	addr.rc_channel = channel;
Packit 34410b
Packit 34410b
	if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Packit 34410b
		perror("ERROR: Can't connect to device");
Packit 34410b
		close(sk);
Packit 34410b
		if (cups_class)
Packit 34410b
			return CUPS_BACKEND_FAILED;
Packit 34410b
		else
Packit 34410b
			return CUPS_BACKEND_RETRY;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	fputs("STATE: -connecting-to-device\n", stderr);
Packit 34410b
Packit 34410b
	/* Ignore SIGTERM signals if printing from stdin */
Packit 34410b
	if (fd == 0) {
Packit 34410b
#ifdef HAVE_SIGSET
Packit 34410b
		sigset(SIGTERM, SIG_IGN);
Packit 34410b
#elif defined(HAVE_SIGACTION)
Packit 34410b
		memset(&action, 0, sizeof(action));
Packit 34410b
		sigemptyset(&action.sa_mask);
Packit 34410b
		action.sa_handler = SIG_IGN;
Packit 34410b
		sigaction(SIGTERM, &action, NULL);
Packit 34410b
#else
Packit 34410b
		signal(SIGTERM, SIG_IGN);
Packit 34410b
#endif /* HAVE_SIGSET */
Packit 34410b
	}
Packit 34410b
Packit 34410b
	for (i = 0; i < copies; i++) {
Packit 34410b
Packit 34410b
		if (fd != 0) {
Packit 34410b
			fprintf(stderr, "PAGE: 1 1\n");
Packit 34410b
			lseek(fd, 0, SEEK_SET);
Packit 34410b
		}
Packit 34410b
Packit 34410b
		while ((len = read(fd, buf, sizeof(buf))) > 0) {
Packit 34410b
			err = write(sk, buf, len);
Packit 34410b
			if (err < 0) {
Packit 34410b
				perror("ERROR: Error writing to device");
Packit 34410b
				close(sk);
Packit 34410b
				return CUPS_BACKEND_FAILED;
Packit 34410b
			}
Packit 34410b
		}
Packit 34410b
Packit 34410b
	}
Packit 34410b
Packit 34410b
	close(sk);
Packit 34410b
Packit 34410b
	return CUPS_BACKEND_OK;
Packit 34410b
}