1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2003-2009 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include <netinet/in.h>
#include "cups.h"
#define HCRP_PDU_CREDIT_GRANT 0x0001
#define HCRP_PDU_CREDIT_REQUEST 0x0002
#define HCRP_PDU_GET_LPT_STATUS 0x0005
#define HCRP_STATUS_FEATURE_UNSUPPORTED 0x0000
#define HCRP_STATUS_SUCCESS 0x0001
#define HCRP_STATUS_CREDIT_SYNC_ERROR 0x0002
#define HCRP_STATUS_GENERIC_FAILURE 0xffff
struct hcrp_pdu_hdr {
uint16_t pid;
uint16_t tid;
uint16_t plen;
} __attribute__ ((packed));
#define HCRP_PDU_HDR_SIZE 6
struct hcrp_credit_grant_cp {
uint32_t credit;
} __attribute__ ((packed));
#define HCRP_CREDIT_GRANT_CP_SIZE 4
struct hcrp_credit_grant_rp {
uint16_t status;
} __attribute__ ((packed));
#define HCRP_CREDIT_GRANT_RP_SIZE 2
struct hcrp_credit_request_rp {
uint16_t status;
uint32_t credit;
} __attribute__ ((packed));
#define HCRP_CREDIT_REQUEST_RP_SIZE 6
struct hcrp_get_lpt_status_rp {
uint16_t status;
uint8_t lpt_status;
} __attribute__ ((packed));
#define HCRP_GET_LPT_STATUS_RP_SIZE 3
static int hcrp_credit_grant(int sk, uint16_t tid, uint32_t credit)
{
struct hcrp_pdu_hdr hdr;
struct hcrp_credit_grant_cp cp;
struct hcrp_credit_grant_rp rp;
unsigned char buf[128];
int len;
hdr.pid = htons(HCRP_PDU_CREDIT_GRANT);
hdr.tid = htons(tid);
hdr.plen = htons(HCRP_CREDIT_GRANT_CP_SIZE);
cp.credit = credit;
memcpy(buf, &hdr, HCRP_PDU_HDR_SIZE);
memcpy(buf + HCRP_PDU_HDR_SIZE, &cp, HCRP_CREDIT_GRANT_CP_SIZE);
len = write(sk, buf, HCRP_PDU_HDR_SIZE + HCRP_CREDIT_GRANT_CP_SIZE);
len = read(sk, buf, sizeof(buf));
memcpy(&hdr, buf, HCRP_PDU_HDR_SIZE);
memcpy(&rp, buf + HCRP_PDU_HDR_SIZE, HCRP_CREDIT_GRANT_RP_SIZE);
if (ntohs(rp.status) != HCRP_STATUS_SUCCESS) {
errno = EIO;
return -1;
}
return 0;
}
static int hcrp_credit_request(int sk, uint16_t tid, uint32_t *credit)
{
struct hcrp_pdu_hdr hdr;
struct hcrp_credit_request_rp rp;
unsigned char buf[128];
int len;
hdr.pid = htons(HCRP_PDU_CREDIT_REQUEST);
hdr.tid = htons(tid);
hdr.plen = htons(0);
memcpy(buf, &hdr, HCRP_PDU_HDR_SIZE);
len = write(sk, buf, HCRP_PDU_HDR_SIZE);
len = read(sk, buf, sizeof(buf));
memcpy(&hdr, buf, HCRP_PDU_HDR_SIZE);
memcpy(&rp, buf + HCRP_PDU_HDR_SIZE, HCRP_CREDIT_REQUEST_RP_SIZE);
if (ntohs(rp.status) != HCRP_STATUS_SUCCESS) {
errno = EIO;
return -1;
}
if (credit)
*credit = ntohl(rp.credit);
return 0;
}
static int hcrp_get_lpt_status(int sk, uint16_t tid, uint8_t *lpt_status)
{
struct hcrp_pdu_hdr hdr;
struct hcrp_get_lpt_status_rp rp;
unsigned char buf[128];
int len;
hdr.pid = htons(HCRP_PDU_GET_LPT_STATUS);
hdr.tid = htons(tid);
hdr.plen = htons(0);
memcpy(buf, &hdr, HCRP_PDU_HDR_SIZE);
len = write(sk, buf, HCRP_PDU_HDR_SIZE);
len = read(sk, buf, sizeof(buf));
memcpy(&hdr, buf, HCRP_PDU_HDR_SIZE);
memcpy(&rp, buf + HCRP_PDU_HDR_SIZE, HCRP_GET_LPT_STATUS_RP_SIZE);
if (ntohs(rp.status) != HCRP_STATUS_SUCCESS) {
errno = EIO;
return -1;
}
if (lpt_status)
*lpt_status = rp.lpt_status;
return 0;
}
static inline int hcrp_get_next_tid(int tid)
{
if (tid > 0xf000)
return 0;
else
return tid + 1;
}
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)
{
struct sockaddr_l2 addr;
struct l2cap_options opts;
socklen_t size;
unsigned char buf[2048];
int i, ctrl_sk, data_sk, mtu, count, len, timeout = 0;
uint8_t status;
uint16_t tid = 0;
uint32_t tmp, credit = 0;
if ((ctrl_sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) < 0) {
perror("ERROR: Can't create socket");
if (cups_class)
return CUPS_BACKEND_FAILED;
else
return CUPS_BACKEND_RETRY;
}
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
bacpy(&addr.l2_bdaddr, src);
if (bind(ctrl_sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("ERROR: Can't bind socket");
close(ctrl_sk);
if (cups_class)
return CUPS_BACKEND_FAILED;
else
return CUPS_BACKEND_RETRY;
}
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
bacpy(&addr.l2_bdaddr, dst);
addr.l2_psm = htobs(ctrl_psm);
if (connect(ctrl_sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("ERROR: Can't connect to device");
close(ctrl_sk);
if (cups_class)
return CUPS_BACKEND_FAILED;
else
return CUPS_BACKEND_RETRY;
}
if ((data_sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) < 0) {
perror("ERROR: Can't create socket");
close(ctrl_sk);
if (cups_class)
return CUPS_BACKEND_FAILED;
else
return CUPS_BACKEND_RETRY;
}
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
bacpy(&addr.l2_bdaddr, src);
if (bind(data_sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("ERROR: Can't bind socket");
close(data_sk);
close(ctrl_sk);
if (cups_class)
return CUPS_BACKEND_FAILED;
else
return CUPS_BACKEND_RETRY;
}
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
bacpy(&addr.l2_bdaddr, dst);
addr.l2_psm = htobs(data_psm);
if (connect(data_sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("ERROR: Can't connect to device");
close(data_sk);
close(ctrl_sk);
if (cups_class)
return CUPS_BACKEND_FAILED;
else
return CUPS_BACKEND_RETRY;
}
fputs("STATE: -connecting-to-device\n", stderr);
memset(&opts, 0, sizeof(opts));
size = sizeof(opts);
if (getsockopt(data_sk, SOL_L2CAP, L2CAP_OPTIONS, &opts, &size) < 0) {
perror("ERROR: Can't get socket options");
close(data_sk);
close(ctrl_sk);
if (cups_class)
return CUPS_BACKEND_FAILED;
else
return CUPS_BACKEND_RETRY;
}
mtu = opts.omtu;
/* Ignore SIGTERM signals if printing from stdin */
if (fd == 0) {
#ifdef HAVE_SIGSET
sigset(SIGTERM, SIG_IGN);
#elif defined(HAVE_SIGACTION)
memset(&action, 0, sizeof(action));
sigemptyset(&action.sa_mask);
action.sa_handler = SIG_IGN;
sigaction(SIGTERM, &action, NULL);
#else
signal(SIGTERM, SIG_IGN);
#endif /* HAVE_SIGSET */
}
tid = hcrp_get_next_tid(tid);
if (hcrp_credit_grant(ctrl_sk, tid, 0) < 0) {
fprintf(stderr, "ERROR: Can't grant initial credits\n");
close(data_sk);
close(ctrl_sk);
if (cups_class)
return CUPS_BACKEND_FAILED;
else
return CUPS_BACKEND_RETRY;
}
for (i = 0; i < copies; i++) {
if (fd != 0) {
fprintf(stderr, "PAGE: 1 1\n");
lseek(fd, 0, SEEK_SET);
}
while (1) {
if (credit < mtu) {
tid = hcrp_get_next_tid(tid);
if (!hcrp_credit_request(ctrl_sk, tid, &tmp)) {
credit += tmp;
timeout = 0;
}
}
if (!credit) {
if (timeout++ > 300) {
tid = hcrp_get_next_tid(tid);
if (!hcrp_get_lpt_status(ctrl_sk, tid, &status))
fprintf(stderr, "ERROR: LPT status 0x%02x\n", status);
break;
}
sleep(1);
continue;
}
count = read(fd, buf, (credit > mtu) ? mtu : credit);
if (count <= 0)
break;
len = write(data_sk, buf, count);
if (len < 0) {
perror("ERROR: Error writing to device");
close(data_sk);
close(ctrl_sk);
return CUPS_BACKEND_FAILED;
}
if (len != count)
fprintf(stderr, "ERROR: Can't send complete data\n");
credit -= len;
}
}
close(data_sk);
close(ctrl_sk);
return CUPS_BACKEND_OK;
}
|