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