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