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-resolution-supported",
60         "sides-supported",
61         "printer-device-id",
62         "epcl-version-supported",
63         "pclm-raster-back-side",
64         "pclm-strip-height-preferred",
65         "pclm-compression-method-preferred",
66         "pclm-source-resolution-supported",
67         "pwg-raster-document-sheet-back",
68         "document-format-details-supported"
69 };
70 
71 static void _init(const ifc_printer_capabilities_t *this_p,
72         const wprint_connect_info_t *info);
73 
74 static status_t _get_capabilities(const ifc_printer_capabilities_t *this_p,
75         printer_capabilities_t *capabilities);
76 
77 static void _destroy(const ifc_printer_capabilities_t *this_p);
78 
79 static ifc_printer_capabilities_t _capabilities_ifc = {
80         .init = _init, .get_capabilities = _get_capabilities, .get_margins = NULL,
81         .destroy = _destroy,
82 };
83 
84 typedef struct {
85     http_t *http;
86     printer_capabilities_t printer_caps;
87     ifc_printer_capabilities_t ifc;
88 } ipp_capabilities_t;
89 
ipp_status_get_capabilities_ifc(const ifc_wprint_t * wprint_ifc)90 const ifc_printer_capabilities_t *ipp_status_get_capabilities_ifc(const ifc_wprint_t *wprint_ifc) {
91     LOGD("ipp_status_get_capabilities_ifc: Enter");
92     ipp_capabilities_t *caps = (ipp_capabilities_t *) malloc(sizeof(ipp_capabilities_t));
93     if (caps == NULL) {
94         return NULL;
95     }
96 
97     memset(caps, 0, sizeof(ipp_capabilities_t));
98     caps->http = NULL;
99 
100     memcpy(&caps->ifc, &_capabilities_ifc, sizeof(ifc_printer_capabilities_t));
101     return &caps->ifc;
102 }
103 
_init(const ifc_printer_capabilities_t * this_p,const wprint_connect_info_t * connect_info)104 static void _init(const ifc_printer_capabilities_t *this_p,
105         const wprint_connect_info_t *connect_info) {
106     LOGD("_init: Enter");
107     ipp_capabilities_t *caps;
108     do {
109         if (this_p == NULL) {
110             continue;
111         }
112         caps = IMPL(ipp_capabilities_t, ifc, this_p);
113 
114         if (caps->http != NULL) {
115             LOGD("_init(): http != NULL closing HTTP");
116             httpClose(caps->http);
117         }
118 
119         caps->http = ipp_cups_connect(connect_info, caps->printer_caps.printerUri,
120                 sizeof(caps->printer_caps.printerUri));
121         getResourceFromURI(caps->printer_caps.printerUri, caps->printer_caps.httpResource, 1024);
122         if (caps->http == NULL) {
123             LOGE("_init(): http is NULL ");
124         }
125     } while (0);
126 }
127 
_get_capabilities(const ifc_printer_capabilities_t * this_p,printer_capabilities_t * capabilities)128 static status_t _get_capabilities(const ifc_printer_capabilities_t *this_p,
129         printer_capabilities_t *capabilities) {
130     LOGD("_get_capabilities: Enter");
131     status_t result = ERROR;
132     ipp_capabilities_t *caps = NULL;
133     ipp_t *request = NULL; // IPP request object
134     ipp_t *response = NULL; // IPP response object
135     ipp_attribute_t *attrptr; // Attribute pointer
136     int op = IPP_GET_PRINTER_ATTRIBUTES;
137 
138     ipp_status_t ipp_status; // Status of IPP request
139 
140     if (capabilities != NULL) {
141         memset(capabilities, 0, sizeof(printer_capabilities_t));
142     }
143 
144     do {
145         if (this_p == NULL) {
146             break;
147         }
148 
149         caps = IMPL(ipp_capabilities_t, ifc, this_p);
150         if (caps->http == NULL) {
151             LOGD("_get_capabilities: caps->http is NULL");
152             break;
153         }
154 
155         request = ippNewRequest(op);
156 
157         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
158                 caps->printer_caps.printerUri);
159 
160         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes",
161                 sizeof(pattrs) / sizeof(pattrs[0]), NULL, pattrs);
162 
163         LOGD("IPP_GET_PRINTER_ATTRIBUTES %s request:", ippOpString(op));
164         for (attrptr = ippFirstAttribute(request); attrptr; attrptr = ippNextAttribute(request)) {
165             print_attr(attrptr);
166         }
167 
168         response = ipp_doCupsRequest(caps->http, request, caps->printer_caps.httpResource,
169                 caps->printer_caps.printerUri);
170         if (response == NULL) {
171             ipp_status = cupsLastError();
172             LOGE("_get_capabilities: %s response is null:  ipp_status %d %s",
173                     caps->printer_caps.printerUri, ipp_status, ippErrorString(ipp_status));
174         } else {
175             ipp_status = cupsLastError();
176             LOGD("ipp CUPS last ERROR: %d, %s", ipp_status, ippErrorString(ipp_status));
177             LOGD("%s received, now call parse_printerAttributes:", ippOpString(op));
178             parse_printerAttributes(response, capabilities);
179 
180 #if LOG_LEVEL <= LEVEL_DEBUG
181             for (attrptr = ippFirstAttribute(response); attrptr; attrptr = ippNextAttribute(
182                     response)) {
183                 print_attr(attrptr);
184             }
185 #endif // LOG_LEVEL <= LEVEL_DEBUG
186             if ((attrptr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) == NULL) {
187                 LOGD("printer-state: null");
188             } else {
189                 LOGI("printer-state %d", (ipp_pstate_t) ippGetInteger(attrptr, 0));
190             }
191         }
192 
193         if (ipp_status >= IPP_OK && ipp_status < IPP_REDIRECTION_OTHER_SITE && response != NULL) {
194             result = OK;
195         } else {
196             result = ERROR;
197         }
198     } while (0);
199 
200     ippDelete(response);
201     ippDelete(request);
202 
203     if ((caps != NULL) && (capabilities != NULL)) {
204         memcpy(capabilities->httpResource, caps->printer_caps.httpResource,
205                 sizeof(capabilities->httpResource));
206     }
207 
208     LOGI(" ippstatus_capabilities: _get_capabilities: returning %d:", result);
209     return result;
210 }
211 
_destroy(const ifc_printer_capabilities_t * this_p)212 static void _destroy(const ifc_printer_capabilities_t *this_p) {
213     ipp_capabilities_t *caps;
214     LOGD("_destroy(): enter");
215     do {
216         if (this_p == NULL) {
217             continue;
218         }
219 
220         caps = IMPL(ipp_capabilities_t, ifc, this_p);
221         if (caps->http != NULL) {
222             httpClose(caps->http);
223         }
224         free(caps);
225     } while (0);
226 }