1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * Copyright (C) 2016 Mopria Alliance, Inc.
4  * Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include "lib_wprint.h"
23 #include "ippstatus_capabilities.h"   // move these to above the calls to cups files
24 #include "ipphelper.h"
25 #include "cups.h"
26 #include "http-private.h"
27 #include "wprint_debug.h"
28 
29 #define TAG "ippstatus_capabilities"
30 
31 /*
32  * Requested printer attributes
33  */
34 static const char *pattrs[] = {
35         "ipp-versions-supported",
36         "printer-make-and-model",
37         "printer-info",
38         "printer-dns-sd-name",
39         "printer-name",
40         "printer-location",
41         "printer-uuid",
42         "printer-uri-supported",
43         "uri-security-supported",
44         "uri-authentication-supported",
45         "color-supported",
46         "copies-supported",
47         "document-format-supported",
48         "media-col-default",
49         "media-default",
50         "media-left-margin-supported",
51         "media-right-margin-supported",
52         "media-top-margin-supported",
53         "media-bottom-margin-supported",
54         "media-supported",
55         "media-type-supported",
56         "output-bin-supported",
57         "print-color-mode-supported",
58         "print-quality-supported",
59         "printer-output-tray",
60         "printer-resolution-supported",
61         "sides-supported",
62         "printer-device-id",
63         "epcl-version-supported",
64         "pclm-raster-back-side",
65         "pclm-strip-height-preferred",
66         "pclm-compression-method-preferred",
67         "pclm-source-resolution-supported",
68         "pwg-raster-document-sheet-back",
69         "document-format-details-supported",
70         "media-ready",
71         "media-col-ready",
72         "print-scaling-supported",
73         "print-scaling-default",
74         "job-pages-per-set-supported"
75 };
76 
77 static void _init(const ifc_printer_capabilities_t *this_p,
78         const wprint_connect_info_t *info);
79 
80 static status_t _get_capabilities(const ifc_printer_capabilities_t *this_p,
81         printer_capabilities_t *capabilities);
82 
83 static void _destroy(const ifc_printer_capabilities_t *this_p);
84 
85 static ifc_printer_capabilities_t _capabilities_ifc = {
86         .init = _init, .get_capabilities = _get_capabilities, .get_margins = NULL,
87         .destroy = _destroy,
88 };
89 
90 typedef struct {
91     http_t *http;
92     printer_capabilities_t printer_caps;
93     ifc_printer_capabilities_t ifc;
94 } ipp_capabilities_t;
95 
ipp_status_get_capabilities_ifc(const ifc_wprint_t * wprint_ifc)96 const ifc_printer_capabilities_t *ipp_status_get_capabilities_ifc(const ifc_wprint_t *wprint_ifc) {
97     LOGD("ipp_status_get_capabilities_ifc: Enter");
98     ipp_capabilities_t *caps = (ipp_capabilities_t *) malloc(sizeof(ipp_capabilities_t));
99     if (caps == NULL) {
100         return NULL;
101     }
102 
103     memset(caps, 0, sizeof(ipp_capabilities_t));
104     caps->http = NULL;
105 
106     memcpy(&caps->ifc, &_capabilities_ifc, sizeof(ifc_printer_capabilities_t));
107     return &caps->ifc;
108 }
109 
_init(const ifc_printer_capabilities_t * this_p,const wprint_connect_info_t * connect_info)110 static void _init(const ifc_printer_capabilities_t *this_p,
111         const wprint_connect_info_t *connect_info) {
112     LOGD("_init: Enter");
113     ipp_capabilities_t *caps;
114     do {
115         if (this_p == NULL) {
116             continue;
117         }
118         caps = IMPL(ipp_capabilities_t, ifc, this_p);
119 
120         if (caps->http != NULL) {
121             LOGD("_init(): http != NULL closing HTTP");
122             httpClose(caps->http);
123         }
124 
125         caps->http = ipp_cups_connect(connect_info, caps->printer_caps.printerUri,
126                 sizeof(caps->printer_caps.printerUri));
127         getResourceFromURI(caps->printer_caps.printerUri, caps->printer_caps.httpResource, 1024);
128         if (caps->http == NULL) {
129             LOGE("_init(): http is NULL ");
130         }
131     } while (0);
132 }
133 
_get_capabilities(const ifc_printer_capabilities_t * this_p,printer_capabilities_t * capabilities)134 static status_t _get_capabilities(const ifc_printer_capabilities_t *this_p,
135         printer_capabilities_t *capabilities) {
136     LOGD("_get_capabilities: Enter");
137     status_t result = ERROR;
138     ipp_capabilities_t *caps = NULL;
139     ipp_t *request = NULL; // IPP request object
140     ipp_t *response = NULL; // IPP response object
141     ipp_attribute_t *attrptr; // Attribute pointer
142     int op = IPP_GET_PRINTER_ATTRIBUTES;
143 
144     ipp_status_t ipp_status; // Status of IPP request
145 
146     if (capabilities != NULL) {
147         memset(capabilities, 0, sizeof(printer_capabilities_t));
148     }
149 
150     do {
151         if (this_p == NULL) {
152             break;
153         }
154 
155         caps = IMPL(ipp_capabilities_t, ifc, this_p);
156         if (caps->http == NULL) {
157             LOGD("_get_capabilities: caps->http is NULL");
158             break;
159         }
160 
161         request = ippNewRequest(op);
162 
163         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
164                 caps->printer_caps.printerUri);
165 
166         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes",
167                 sizeof(pattrs) / sizeof(pattrs[0]), NULL, pattrs);
168 
169         LOGD("IPP_GET_PRINTER_ATTRIBUTES %s request:", ippOpString(op));
170         for (attrptr = ippFirstAttribute(request); attrptr; attrptr = ippNextAttribute(request)) {
171             print_attr(attrptr);
172         }
173 
174         response = ipp_doCupsRequest(caps->http, request, caps->printer_caps.httpResource,
175                 caps->printer_caps.printerUri);
176         if (response == NULL) {
177             ipp_status = cupsLastError();
178             LOGE("_get_capabilities: %s response is null:  ipp_status %d %s",
179                     caps->printer_caps.printerUri, ipp_status, ippErrorString(ipp_status));
180         } else {
181             ipp_status = cupsLastError();
182             LOGD("ipp CUPS last ERROR: %d, %s", ipp_status, ippErrorString(ipp_status));
183             LOGD("%s received, now call parse_printerAttributes:", ippOpString(op));
184             parse_printerAttributes(response, capabilities);
185 
186 #if LOG_LEVEL <= LEVEL_DEBUG
187             for (attrptr = ippFirstAttribute(response); attrptr; attrptr = ippNextAttribute(
188                     response)) {
189                 print_attr(attrptr);
190             }
191 #endif // LOG_LEVEL <= LEVEL_DEBUG
192             if ((attrptr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) == NULL) {
193                 LOGD("printer-state: null");
194             } else {
195                 LOGI("printer-state %d", (ipp_pstate_t) ippGetInteger(attrptr, 0));
196             }
197         }
198 
199         if (ipp_status >= IPP_OK && ipp_status < IPP_REDIRECTION_OTHER_SITE && response != NULL) {
200             result = OK;
201         } else {
202             result = ERROR;
203         }
204     } while (0);
205 
206     ippDelete(response);
207     ippDelete(request);
208 
209     if ((caps != NULL) && (capabilities != NULL)) {
210         memcpy(capabilities->httpResource, caps->printer_caps.httpResource,
211                 sizeof(capabilities->httpResource));
212     }
213 
214     LOGI(" ippstatus_capabilities: _get_capabilities: returning %d:", result);
215     return result;
216 }
217 
_destroy(const ifc_printer_capabilities_t * this_p)218 static void _destroy(const ifc_printer_capabilities_t *this_p) {
219     ipp_capabilities_t *caps;
220     LOGD("_destroy(): enter");
221     do {
222         if (this_p == NULL) {
223             continue;
224         }
225 
226         caps = IMPL(ipp_capabilities_t, ifc, this_p);
227         if (caps->http != NULL) {
228             httpClose(caps->http);
229         }
230         free(caps);
231     } while (0);
232 }