3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2003-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <sys/socket.h>
35 #include <bluetooth/bluetooth.h>
36 #include <bluetooth/l2cap.h>
37 #include <bluetooth/sdp.h>
38 #include <bluetooth/sdp_lib.h>
40 #include <netinet/in.h>
44 #define HCRP_PDU_CREDIT_GRANT 0x0001
45 #define HCRP_PDU_CREDIT_REQUEST 0x0002
46 #define HCRP_PDU_GET_LPT_STATUS 0x0005
48 #define HCRP_STATUS_FEATURE_UNSUPPORTED 0x0000
49 #define HCRP_STATUS_SUCCESS 0x0001
50 #define HCRP_STATUS_CREDIT_SYNC_ERROR 0x0002
51 #define HCRP_STATUS_GENERIC_FAILURE 0xffff
57 } __attribute__ ((packed));
58 #define HCRP_PDU_HDR_SIZE 6
60 struct hcrp_credit_grant_cp {
62 } __attribute__ ((packed));
63 #define HCRP_CREDIT_GRANT_CP_SIZE 4
65 struct hcrp_credit_grant_rp {
67 } __attribute__ ((packed));
68 #define HCRP_CREDIT_GRANT_RP_SIZE 2
70 struct hcrp_credit_request_rp {
73 } __attribute__ ((packed));
74 #define HCRP_CREDIT_REQUEST_RP_SIZE 6
76 struct hcrp_get_lpt_status_rp {
79 } __attribute__ ((packed));
80 #define HCRP_GET_LPT_STATUS_RP_SIZE 3
82 static int hcrp_credit_grant(int sk, uint16_t tid, uint32_t credit)
84 struct hcrp_pdu_hdr hdr;
85 struct hcrp_credit_grant_cp cp;
86 struct hcrp_credit_grant_rp rp;
87 unsigned char buf[128];
90 hdr.pid = htons(HCRP_PDU_CREDIT_GRANT);
92 hdr.plen = htons(HCRP_CREDIT_GRANT_CP_SIZE);
94 memcpy(buf, &hdr, HCRP_PDU_HDR_SIZE);
95 memcpy(buf + HCRP_PDU_HDR_SIZE, &cp, HCRP_CREDIT_GRANT_CP_SIZE);
96 len = write(sk, buf, HCRP_PDU_HDR_SIZE + HCRP_CREDIT_GRANT_CP_SIZE);
100 len = read(sk, buf, sizeof(buf));
104 memcpy(&hdr, buf, HCRP_PDU_HDR_SIZE);
105 memcpy(&rp, buf + HCRP_PDU_HDR_SIZE, HCRP_CREDIT_GRANT_RP_SIZE);
107 if (ntohs(rp.status) != HCRP_STATUS_SUCCESS) {
115 static int hcrp_credit_request(int sk, uint16_t tid, uint32_t *credit)
117 struct hcrp_pdu_hdr hdr;
118 struct hcrp_credit_request_rp rp;
119 unsigned char buf[128];
122 hdr.pid = htons(HCRP_PDU_CREDIT_REQUEST);
123 hdr.tid = htons(tid);
125 memcpy(buf, &hdr, HCRP_PDU_HDR_SIZE);
126 len = write(sk, buf, HCRP_PDU_HDR_SIZE);
130 len = read(sk, buf, sizeof(buf));
134 memcpy(&hdr, buf, HCRP_PDU_HDR_SIZE);
135 memcpy(&rp, buf + HCRP_PDU_HDR_SIZE, HCRP_CREDIT_REQUEST_RP_SIZE);
137 if (ntohs(rp.status) != HCRP_STATUS_SUCCESS) {
143 *credit = ntohl(rp.credit);
148 static int hcrp_get_lpt_status(int sk, uint16_t tid, uint8_t *lpt_status)
150 struct hcrp_pdu_hdr hdr;
151 struct hcrp_get_lpt_status_rp rp;
152 unsigned char buf[128];
155 hdr.pid = htons(HCRP_PDU_GET_LPT_STATUS);
156 hdr.tid = htons(tid);
158 memcpy(buf, &hdr, HCRP_PDU_HDR_SIZE);
159 len = write(sk, buf, HCRP_PDU_HDR_SIZE);
163 len = read(sk, buf, sizeof(buf));
167 memcpy(&hdr, buf, HCRP_PDU_HDR_SIZE);
168 memcpy(&rp, buf + HCRP_PDU_HDR_SIZE, HCRP_GET_LPT_STATUS_RP_SIZE);
170 if (ntohs(rp.status) != HCRP_STATUS_SUCCESS) {
176 *lpt_status = rp.lpt_status;
181 static inline int hcrp_get_next_tid(int tid)
189 int hcrp_print(bdaddr_t *src, bdaddr_t *dst, unsigned short ctrl_psm, unsigned short data_psm, int fd, int copies, const char *cups_class)
191 struct sockaddr_l2 addr;
192 struct l2cap_options opts;
194 unsigned char buf[2048];
195 int i, ctrl_sk, data_sk, count, len, timeout = 0;
199 uint32_t tmp, credit = 0;
201 if ((ctrl_sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) < 0) {
202 perror("ERROR: Can't create socket");
204 return CUPS_BACKEND_FAILED;
206 return CUPS_BACKEND_RETRY;
209 memset(&addr, 0, sizeof(addr));
210 addr.l2_family = AF_BLUETOOTH;
211 bacpy(&addr.l2_bdaddr, src);
213 if (bind(ctrl_sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
214 perror("ERROR: Can't bind socket");
217 return CUPS_BACKEND_FAILED;
219 return CUPS_BACKEND_RETRY;
222 memset(&addr, 0, sizeof(addr));
223 addr.l2_family = AF_BLUETOOTH;
224 bacpy(&addr.l2_bdaddr, dst);
225 addr.l2_psm = htobs(ctrl_psm);
227 if (connect(ctrl_sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
228 perror("ERROR: Can't connect to device");
231 return CUPS_BACKEND_FAILED;
233 return CUPS_BACKEND_RETRY;
236 if ((data_sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) < 0) {
237 perror("ERROR: Can't create socket");
240 return CUPS_BACKEND_FAILED;
242 return CUPS_BACKEND_RETRY;
245 memset(&addr, 0, sizeof(addr));
246 addr.l2_family = AF_BLUETOOTH;
247 bacpy(&addr.l2_bdaddr, src);
249 if (bind(data_sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
250 perror("ERROR: Can't bind socket");
254 return CUPS_BACKEND_FAILED;
256 return CUPS_BACKEND_RETRY;
259 memset(&addr, 0, sizeof(addr));
260 addr.l2_family = AF_BLUETOOTH;
261 bacpy(&addr.l2_bdaddr, dst);
262 addr.l2_psm = htobs(data_psm);
264 if (connect(data_sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
265 perror("ERROR: Can't connect to device");
269 return CUPS_BACKEND_FAILED;
271 return CUPS_BACKEND_RETRY;
274 fputs("STATE: -connecting-to-device\n", stderr);
276 memset(&opts, 0, sizeof(opts));
279 if (getsockopt(data_sk, SOL_L2CAP, L2CAP_OPTIONS, &opts, &size) < 0) {
280 perror("ERROR: Can't get socket options");
284 return CUPS_BACKEND_FAILED;
286 return CUPS_BACKEND_RETRY;
291 /* Ignore SIGTERM signals if printing from stdin */
294 sigset(SIGTERM, SIG_IGN);
295 #elif defined(HAVE_SIGACTION)
296 memset(&action, 0, sizeof(action));
297 sigemptyset(&action.sa_mask);
298 action.sa_handler = SIG_IGN;
299 sigaction(SIGTERM, &action, NULL);
301 signal(SIGTERM, SIG_IGN);
302 #endif /* HAVE_SIGSET */
305 tid = hcrp_get_next_tid(tid);
306 if (hcrp_credit_grant(ctrl_sk, tid, 0) < 0) {
307 fprintf(stderr, "ERROR: Can't grant initial credits\n");
311 return CUPS_BACKEND_FAILED;
313 return CUPS_BACKEND_RETRY;
316 for (i = 0; i < copies; i++) {
319 fprintf(stderr, "PAGE: 1 1\n");
320 lseek(fd, 0, SEEK_SET);
325 tid = hcrp_get_next_tid(tid);
326 if (!hcrp_credit_request(ctrl_sk, tid, &tmp)) {
333 if (timeout++ > 300) {
334 tid = hcrp_get_next_tid(tid);
335 if (!hcrp_get_lpt_status(ctrl_sk, tid, &status))
336 fprintf(stderr, "ERROR: LPT status 0x%02x\n", status);
344 count = read(fd, buf, (credit > mtu) ? mtu : credit);
348 len = write(data_sk, buf, count);
350 perror("ERROR: Error writing to device");
353 return CUPS_BACKEND_FAILED;
357 fprintf(stderr, "ERROR: Can't send complete data\n");
367 return CUPS_BACKEND_OK;