1bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann/* 2bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann * 3bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann * BlueZ - Bluetooth protocol stack for Linux 4bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann * 59184e2eeb7b97371c6b83b747c8984e2340d2b47Marcel Holtmann * Copyright (C) 2003-2010 Marcel Holtmann <marcel@holtmann.org> 6bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann * 7bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann * 8bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann * This program is free software; you can redistribute it and/or modify 9632a9432774ff3a0c6e556e8f32a565b38890767Marcel Holtmann * it under the terms of the GNU General Public License as published by 10632a9432774ff3a0c6e556e8f32a565b38890767Marcel Holtmann * the Free Software Foundation; either version 2 of the License, or 11632a9432774ff3a0c6e556e8f32a565b38890767Marcel Holtmann * (at your option) any later version. 12bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann * 13632a9432774ff3a0c6e556e8f32a565b38890767Marcel Holtmann * This program is distributed in the hope that it will be useful, 14632a9432774ff3a0c6e556e8f32a565b38890767Marcel Holtmann * but WITHOUT ANY WARRANTY; without even the implied warranty of 15632a9432774ff3a0c6e556e8f32a565b38890767Marcel Holtmann * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16632a9432774ff3a0c6e556e8f32a565b38890767Marcel Holtmann * GNU General Public License for more details. 17bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann * 18632a9432774ff3a0c6e556e8f32a565b38890767Marcel Holtmann * You should have received a copy of the GNU General Public License 19632a9432774ff3a0c6e556e8f32a565b38890767Marcel Holtmann * along with this program; if not, write to the Free Software 20632a9432774ff3a0c6e556e8f32a565b38890767Marcel Holtmann * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann * 22bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann */ 23bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 24bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#ifdef HAVE_CONFIG_H 25bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#include <config.h> 26bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#endif 27bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 28bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#include <stdio.h> 29bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#include <errno.h> 30bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#include <unistd.h> 31bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#include <signal.h> 32bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#include <sys/socket.h> 33bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 34bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#include <bluetooth/bluetooth.h> 35bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#include <bluetooth/rfcomm.h> 360a14f620f8be275db4a8f88a50d12044c9e6cf8aMarcel Holtmann#include <bluetooth/sdp.h> 370a14f620f8be275db4a8f88a50d12044c9e6cf8aMarcel Holtmann#include <bluetooth/sdp_lib.h> 38bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 39a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann#include "cups.h" 40a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann 41a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmannint spp_print(bdaddr_t *src, bdaddr_t *dst, uint8_t channel, int fd, int copies, const char *cups_class) 42bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann{ 43bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann struct sockaddr_rc addr; 44bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann unsigned char buf[2048]; 45b102348e988e4abc5d579ce13c067ce2c885aaf7Marcel Holtmann int i, sk, err, len; 46bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 47bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann if ((sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) { 48bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann perror("ERROR: Can't create socket"); 49a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann if (cups_class) 50a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann return CUPS_BACKEND_FAILED; 51a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann else 52a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann return CUPS_BACKEND_RETRY; 53bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann } 54bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 55bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann addr.rc_family = AF_BLUETOOTH; 56bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann bacpy(&addr.rc_bdaddr, src); 57bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann addr.rc_channel = 0; 58bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 59bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 60bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann perror("ERROR: Can't bind socket"); 61bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann close(sk); 62a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann if (cups_class) 63a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann return CUPS_BACKEND_FAILED; 64a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann else 65a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann return CUPS_BACKEND_RETRY; 66bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann } 67bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 68bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann addr.rc_family = AF_BLUETOOTH; 69bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann bacpy(&addr.rc_bdaddr, dst); 70bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann addr.rc_channel = channel; 71bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 72bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 73bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann perror("ERROR: Can't connect to device"); 74bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann close(sk); 75a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann if (cups_class) 76a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann return CUPS_BACKEND_FAILED; 77a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann else 78a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann return CUPS_BACKEND_RETRY; 79bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann } 80bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 8169f9e21b46e2827a24cad0436d52a47fdd4ee36aMarcel Holtmann fputs("STATE: -connecting-to-device\n", stderr); 8269f9e21b46e2827a24cad0436d52a47fdd4ee36aMarcel Holtmann 83bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann /* Ignore SIGTERM signals if printing from stdin */ 84bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann if (fd == 0) { 85bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#ifdef HAVE_SIGSET 86bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann sigset(SIGTERM, SIG_IGN); 87bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#elif defined(HAVE_SIGACTION) 88bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann memset(&action, 0, sizeof(action)); 89bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann sigemptyset(&action.sa_mask); 90bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann action.sa_handler = SIG_IGN; 91bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann sigaction(SIGTERM, &action, NULL); 92bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#else 93bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann signal(SIGTERM, SIG_IGN); 94bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann#endif /* HAVE_SIGSET */ 95bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann } 96bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 97bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann for (i = 0; i < copies; i++) { 98bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 99bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann if (fd != 0) { 100bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann fprintf(stderr, "PAGE: 1 1\n"); 101bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann lseek(fd, 0, SEEK_SET); 102bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann } 103bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 104bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann while ((len = read(fd, buf, sizeof(buf))) > 0) { 105b102348e988e4abc5d579ce13c067ce2c885aaf7Marcel Holtmann err = write(sk, buf, len); 10669f9e21b46e2827a24cad0436d52a47fdd4ee36aMarcel Holtmann if (err < 0) { 10769f9e21b46e2827a24cad0436d52a47fdd4ee36aMarcel Holtmann perror("ERROR: Error writing to device"); 10869f9e21b46e2827a24cad0436d52a47fdd4ee36aMarcel Holtmann close(sk); 109a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann return CUPS_BACKEND_FAILED; 11069f9e21b46e2827a24cad0436d52a47fdd4ee36aMarcel Holtmann } 111bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann } 112bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 113bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann } 114bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 115bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann close(sk); 116bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann 117a71ed7ebdf0599e0a019c9087669a62e026109a2Marcel Holtmann return CUPS_BACKEND_OK; 118bc085054220d80e9308fe5f4c2260e7ccc06e7e3Marcel Holtmann} 119