]>
Commit | Line | Data |
---|---|---|
7a14d768 MS |
1 | /* |
2 | * "$Id$" | |
3 | * | |
4 | * Common network APIs for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2007-2008 by Apple Inc. | |
7 | * Copyright 2006-2007 by Easy Software Products, all rights reserved. | |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
10 | * property of Apple Inc. and are protected by Federal copyright | |
7a14d768 MS |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" |
12 | * "LICENSE" which should have been included with this file. If this | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
14 | * | |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
a0f6818e MS |
19 | * backendCheckSideChannel() - Check the side-channel for pending requests. |
20 | * backendNetworkSideCB() - Handle common network side-channel commands. | |
7a14d768 MS |
21 | */ |
22 | ||
23 | /* | |
24 | * Include necessary headers. | |
25 | */ | |
26 | ||
27 | #include "backend-private.h" | |
28 | #include <limits.h> | |
29 | #ifdef __hpux | |
30 | # include <sys/time.h> | |
31 | #else | |
32 | # include <sys/select.h> | |
33 | #endif /* __hpux */ | |
7a14d768 MS |
34 | |
35 | ||
36 | /* | |
37 | * Local functions... | |
38 | */ | |
39 | ||
7a14d768 MS |
40 | |
41 | ||
42 | /* | |
43 | * 'backendCheckSideChannel()' - Check the side-channel for pending requests. | |
44 | */ | |
45 | ||
46 | ||
47 | void | |
48 | backendCheckSideChannel( | |
49 | int snmp_fd, /* I - SNMP socket */ | |
50 | http_addr_t *addr) /* I - Address of device */ | |
51 | { | |
52 | fd_set input; /* Select input set */ | |
53 | struct timeval timeout; /* Select timeout */ | |
54 | ||
55 | ||
56 | FD_ZERO(&input); | |
57 | FD_SET(CUPS_SC_FD, &input); | |
58 | ||
59 | timeout.tv_sec = timeout.tv_usec = 0; | |
60 | ||
61 | if (select(CUPS_SC_FD + 1, &input, NULL, NULL, &timeout) > 0) | |
62 | backendNetworkSideCB(-1, -1, snmp_fd, addr, 0); | |
63 | } | |
64 | ||
65 | ||
66 | /* | |
67 | * 'backendNetworkSideCB()' - Handle common network side-channel commands. | |
68 | */ | |
69 | ||
70 | void | |
71 | backendNetworkSideCB( | |
72 | int print_fd, /* I - Print file or -1 */ | |
73 | int device_fd, /* I - Device file or -1 */ | |
74 | int snmp_fd, /* I - SNMP socket */ | |
75 | http_addr_t *addr, /* I - Address of device */ | |
76 | int use_bc) /* I - Use back-channel data? */ | |
77 | { | |
78 | cups_sc_command_t command; /* Request command */ | |
79 | cups_sc_status_t status; /* Request/response status */ | |
80 | char data[2048]; /* Request/response data */ | |
81 | int datalen; /* Request/response data size */ | |
82 | const char *device_id; /* 1284DEVICEID env var */ | |
83 | ||
84 | ||
85 | datalen = sizeof(data); | |
86 | ||
87 | if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0)) | |
88 | { | |
89 | _cupsLangPuts(stderr, _("WARNING: Failed to read side-channel request!\n")); | |
90 | return; | |
91 | } | |
92 | ||
93 | switch (command) | |
94 | { | |
95 | case CUPS_SC_CMD_DRAIN_OUTPUT : | |
96 | /* | |
97 | * Our sockets disable the Nagle algorithm and data is sent immediately. | |
98 | */ | |
99 | ||
100 | if (device_fd < 0) | |
101 | status = CUPS_SC_STATUS_NOT_IMPLEMENTED; | |
102 | else if (backendDrainOutput(print_fd, device_fd)) | |
103 | status = CUPS_SC_STATUS_IO_ERROR; | |
104 | else | |
105 | status = CUPS_SC_STATUS_OK; | |
106 | ||
107 | datalen = 0; | |
108 | break; | |
109 | ||
110 | case CUPS_SC_CMD_GET_BIDI : | |
111 | data[0] = use_bc; | |
112 | datalen = 1; | |
113 | break; | |
114 | ||
115 | case CUPS_SC_CMD_GET_DEVICE_ID : | |
116 | if (snmp_fd >= 0) | |
117 | { | |
118 | cups_snmp_t packet; /* Packet from printer */ | |
119 | static const int ppmPrinterIEEE1284DeviceId[] = | |
120 | { CUPS_OID_ppmPrinterIEEE1284DeviceId,1,-1 }; | |
121 | ||
122 | if (_cupsSNMPWrite(snmp_fd, addr, 1, _cupsSNMPDefaultCommunity(), | |
123 | CUPS_ASN1_GET_REQUEST, 1, | |
124 | ppmPrinterIEEE1284DeviceId)) | |
125 | { | |
126 | if (_cupsSNMPRead(snmp_fd, &packet, 1.0) && | |
127 | packet.object_type == CUPS_ASN1_OCTET_STRING) | |
128 | { | |
129 | strlcpy(data, packet.object_value.string, sizeof(data)); | |
130 | datalen = (int)strlen(data); | |
131 | break; | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | if ((device_id = getenv("1284DEVICEID")) != NULL) | |
137 | { | |
138 | strlcpy(data, device_id, sizeof(data)); | |
139 | datalen = (int)strlen(data); | |
140 | break; | |
141 | } | |
142 | ||
143 | default : | |
144 | status = CUPS_SC_STATUS_NOT_IMPLEMENTED; | |
145 | datalen = 0; | |
146 | break; | |
147 | } | |
148 | ||
149 | cupsSideChannelWrite(command, status, data, datalen, 1.0); | |
150 | } | |
151 | ||
152 | ||
7a14d768 MS |
153 | /* |
154 | * End of "$Id$". | |
155 | */ |