Home | History | Annotate | Download | only in ipphelper
      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 "ipp_print.h"
     20 #include <math.h>
     21 #include "ipphelper.h"
     22 #include "wprint_debug.h"
     23 
     24 #include "plugins/media.h"
     25 
     26 #define TAG "ipp_print"
     27 
     28 static status_t _init(const ifc_print_job_t *this_p, const char *printer_address, int port,
     29         const char *printer_uri);
     30 
     31 static status_t _validate_job(const ifc_print_job_t *this_p, wprint_job_params_t *job_params);
     32 
     33 static status_t _start_job(const ifc_print_job_t *this_p, const wprint_job_params_t *job_params);
     34 
     35 static int _send_data(const ifc_print_job_t *this_p, const char *buffer, size_t length);
     36 
     37 static status_t _end_job(const ifc_print_job_t *this_p);
     38 
     39 static void _destroy(const ifc_print_job_t *this_p);
     40 
     41 static const ifc_print_job_t _print_job_ifc = {
     42         .init = _init, .validate_job = _validate_job, .start_job = _start_job,
     43         .send_data = _send_data, .end_job = _end_job, .destroy = _destroy, .enable_timeout = NULL,
     44 };
     45 
     46 /*
     47  * Struct for handling an ipp print job
     48  */
     49 typedef struct {
     50     http_t *http;
     51     char printer_uri[1024];
     52     char http_resource[1024];
     53     http_status_t status;
     54     ifc_print_job_t ifc;
     55     const char *useragent;
     56 } ipp_print_job_t;
     57 
     58 /*
     59  * Returns a print job handle for an ipp print job
     60  */
     61 const ifc_print_job_t *ipp_get_print_ifc(const ifc_wprint_t *wprint_ifc) {
     62     LOGD("ipp_get_print_ifc: Enter");
     63     ipp_print_job_t *ipp_job = (ipp_print_job_t *) malloc(sizeof(ipp_print_job_t));
     64 
     65     if (ipp_job == NULL) {
     66         return NULL;
     67     }
     68 
     69     memset(ipp_job, 0, sizeof(ipp_print_job_t));
     70     ipp_job->status = HTTP_CONTINUE;
     71 
     72     memcpy(&ipp_job->ifc, &_print_job_ifc, sizeof(ifc_print_job_t));
     73 
     74     return &ipp_job->ifc;
     75 }
     76 
     77 static status_t _init(const ifc_print_job_t *this_p, const char *printer_address, int port,
     78         const char *printer_uri) {
     79     LOGD("_init: Enter");
     80     ipp_print_job_t *ipp_job;
     81     const char *ipp_scheme;
     82 
     83     if (this_p == NULL) {
     84         return ERROR;
     85     }
     86 
     87     ipp_job = IMPL(ipp_print_job_t, ifc, this_p);
     88     if (ipp_job->http != NULL) {
     89         httpClose(ipp_job->http);
     90     }
     91 
     92     if ((printer_uri == NULL) || (strlen(printer_uri) == 0)) {
     93         printer_uri = DEFAULT_IPP_URI_RESOURCE;
     94     }
     95 
     96     int ippPortNumber = ((port == IPP_PORT) ? ippPort() : port);
     97     LOGD("Normal URI for %s:%d", printer_address, ippPortNumber);
     98     ipp_scheme = IPP_PREFIX;
     99 
    100     httpAssembleURIf(HTTP_URI_CODING_ALL, ipp_job->printer_uri, sizeof(ipp_job->printer_uri),
    101             ipp_scheme, NULL, printer_address, ippPortNumber, printer_uri);
    102     getResourceFromURI(ipp_job->printer_uri, ipp_job->http_resource, 1024);
    103     ipp_job->http = httpConnect(printer_address, ippPortNumber);
    104 
    105     httpSetTimeout(ipp_job->http, DEFAULT_IPP_TIMEOUT, NULL, 0);
    106 
    107     return OK;
    108 }
    109 
    110 static void _destroy(const ifc_print_job_t *this_p) {
    111     LOGD("_destroy: Enter");
    112     ipp_print_job_t *ipp_job;
    113     if (this_p == NULL) {
    114         return;
    115     }
    116 
    117     ipp_job = IMPL(ipp_print_job_t, ifc, this_p);
    118     if (ipp_job->http != NULL) {
    119         httpClose(ipp_job->http);
    120     }
    121 
    122     free(ipp_job);
    123 }
    124 
    125 /*
    126  * Outputs width, height, and name for a given media size
    127  */
    128 static void _get_pwg_media_size(media_size_t media_size, float *mediaWidth, float *mediaHeight,
    129         const char **mediaSizeName) {
    130     int i = 0;
    131 
    132     for (i = 0; i < SUPPORTED_MEDIA_SIZE_COUNT; i++) {
    133         if (media_size == SupportedMediaSizes[i].media_size) {
    134             // Get the dimensions in 100 mm
    135             if ((SupportedMediaSizes[i].WidthInMm == UNKNOWN_VALUE) ||
    136                     (SupportedMediaSizes[i].HeightInMm == UNKNOWN_VALUE)) {
    137                 *mediaWidth = floorf(_MI_TO_100MM(SupportedMediaSizes[i].WidthInInches));
    138                 *mediaHeight = floorf(_MI_TO_100MM(SupportedMediaSizes[i].HeightInInches));
    139             } else {
    140                 *mediaWidth = SupportedMediaSizes[i].WidthInMm * 100;
    141                 *mediaHeight = SupportedMediaSizes[i].HeightInMm * 100;
    142             }
    143             *mediaSizeName = (char *) SupportedMediaSizes[i].PWGName;
    144 
    145             LOGD("_get_pwg_media_size(): match found: %d, %s, width=%f, height=%f",
    146                     media_size, SupportedMediaSizes[i].PCL6Name, *mediaWidth, *mediaHeight);
    147             break;  // we found a match, so break out of loop
    148         }
    149     }
    150 
    151     if (i == SUPPORTED_MEDIA_SIZE_COUNT) {
    152         // media size not found, defaulting to letter
    153         LOGD("_get_pwg_media_size(): media size, %d, NOT FOUND, setting to letter", media_size);
    154         _get_pwg_media_size(US_LETTER, mediaWidth, mediaHeight, mediaSizeName);
    155     }
    156 }
    157 
    158 /*
    159  * Fills and returns an ipp request object with the given job parameters
    160  */
    161 static ipp_t *_fill_job(int ipp_op, char *printer_uri, const wprint_job_params_t *job_params) {
    162     LOGD("_fill_job: Enter");
    163     ipp_t *request = NULL; // IPP request object
    164     ipp_attribute_t *attrptr; // Attribute pointer
    165     ipp_t *col[2];
    166     int col_index = -1;
    167 
    168     if (job_params == NULL) return NULL;
    169 
    170     request = ippNewRequest(ipp_op);
    171     if (request == NULL) {
    172         return request;
    173     }
    174 
    175     if (set_ipp_version(request, printer_uri, NULL, IPP_VERSION_RESOLVED) != 0) {
    176         ippDelete(request);
    177         return NULL;
    178     }
    179     bool is_2_0_capable = job_params->ipp_2_0_supported;
    180     bool is_ePCL_ipp_capable = job_params->epcl_ipp_supported;
    181 
    182     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
    183             printer_uri);
    184 
    185     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL,
    186             job_params->job_originating_user_name);
    187     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, job_params->job_name);
    188 
    189     // Fields for Document source application and source OS
    190     bool is_doc_format_details_supported = (
    191             job_params->accepts_app_name ||
    192                     job_params->accepts_app_version ||
    193                     job_params->accepts_os_name ||
    194                     job_params->accepts_os_version);
    195 
    196     if (is_doc_format_details_supported) {
    197         ipp_t *document_format_details = ippNew();
    198         if (job_params->accepts_app_name) {
    199             ippAddString(document_format_details, IPP_TAG_OPERATION, IPP_TAG_NAME,
    200                     "document-source-application-name", NULL, g_appName);
    201         }
    202         if (job_params->accepts_app_version) {
    203             ippAddString(document_format_details, IPP_TAG_OPERATION, IPP_TAG_TEXT,
    204                     "document-source-application-version", NULL, g_appVersion);
    205         }
    206         if (job_params->accepts_os_name) {
    207             ippAddString(document_format_details, IPP_TAG_OPERATION, IPP_TAG_NAME,
    208                     "document-source-os-name", NULL, g_osName);
    209         }
    210         if (job_params->accepts_os_version) {
    211             char version[40];
    212             sprintf(version, "%d", g_API_version);
    213             ippAddString(document_format_details, IPP_TAG_OPERATION, IPP_TAG_TEXT,
    214                     "document-source-os-version", NULL, version);
    215         }
    216 
    217         ippAddCollection(request, IPP_TAG_OPERATION, "document-format-details",
    218                 document_format_details);
    219         ippDelete(document_format_details);
    220     }
    221 
    222     LOGD("_fill_job: pcl_type(%d), print_format(%s)", job_params->pcl_type,
    223             job_params->print_format);
    224     if (strcmp(job_params->print_format, PRINT_FORMAT_PDF) == 0) {
    225         if (is_2_0_capable) {
    226             // document-format needs to be the very next attribute for some printers
    227             ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", NULL,
    228                     PRINT_FORMAT_PDF);
    229             LOGD("_fill_job: setting document-format: %s", PRINT_FORMAT_PDF);
    230         } else {
    231             // some earlier devices don't print pdfs when we send the other PRINT_FORMAT_PDF
    232             ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", NULL,
    233                     (job_params->accepts_pclm ? PRINT_FORMAT_PCLM : PRINT_FORMAT_PDF));
    234             LOGD("_fill_job: setting document-format: %s",
    235                     (job_params->accepts_pclm ? PRINT_FORMAT_PCLM : PRINT_FORMAT_PDF));
    236         }
    237 
    238         if (is_ePCL_ipp_capable) {
    239             if (job_params->render_flags & RENDER_FLAG_AUTO_SCALE) {
    240                 ippAddBoolean(request, IPP_TAG_JOB, "pdf-fit-to-page", 1); // true
    241             }
    242         }
    243 
    244         // Fix Orientation bug for PDF printers only.
    245         if (job_params->render_flags & RENDER_FLAG_PORTRAIT_MODE) {
    246             ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "orientation-requested",
    247                     IPP_PRINT_ORIENTATION_PORTRAIT);
    248         }
    249         if (job_params->render_flags & RENDER_FLAG_LANDSCAPE_MODE) {
    250             ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "orientation-requested",
    251                     IPP_PRINT_ORIENTATION_LANDSCAPE);
    252         }
    253     } else {
    254         switch (job_params->pcl_type) {
    255             case PCLm:
    256                 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", NULL,
    257                         PRINT_FORMAT_PCLM);
    258                 LOGD("_fill_job: setting document-format: %s", PRINT_FORMAT_PCLM);
    259                 if (is_ePCL_ipp_capable) {
    260                     ippAddResolution(request, IPP_TAG_JOB, "pclm-source-resolution",
    261                             IPP_RES_PER_INCH, job_params->pixel_units,
    262                             job_params->pixel_units);
    263                 }
    264                 break;
    265             case PCLPWG:
    266                 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", NULL,
    267                         PRINT_FORMAT_PWG);
    268                 LOGD("_fill_job: setting document-format: %s", PRINT_FORMAT_PWG);
    269                 break;
    270             default:
    271                 LOGD("_fill_job: unrecognized pcl_type: %d", job_params->pcl_type);
    272                 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", NULL,
    273                         PRINT_FORMAT_AUTO);
    274                 break;
    275         }
    276 
    277         if (is_ePCL_ipp_capable) {
    278             ippAddBoolean(request, IPP_TAG_JOB, "margins-pre-applied", 1); // true
    279         }
    280     }
    281 
    282     // Add copies support if required and allowed
    283     if (job_params->copies_supported && (strcmp(job_params->print_format, PRINT_FORMAT_PDF) == 0)) {
    284         ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", job_params->num_copies);
    285     }
    286 
    287     ippAddResolution(request, IPP_TAG_JOB, "printer-resolution", IPP_RES_PER_INCH,
    288             job_params->pixel_units, job_params->pixel_units);
    289     if (job_params->duplex == DUPLEX_MODE_BOOK) {
    290         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, IPP_SIDES_TAG, NULL,
    291                 IPP_SIDES_TWO_SIDED_LONG_EDGE);
    292     } else if (job_params->duplex == DUPLEX_MODE_TABLET) {
    293         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, IPP_SIDES_TAG, NULL,
    294                 IPP_SIDES_TWO_SIDED_SHORT_EDGE);
    295     } else {
    296         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, IPP_SIDES_TAG, NULL,
    297                 IPP_SIDES_ONE_SIDED);
    298     }
    299 
    300     if (job_params->color_space == COLOR_SPACE_MONO) {
    301         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, IPP_OUTPUT_MODE_TAG, NULL,
    302                 IPP_OUTPUT_MODE_MONO);
    303     } else {
    304         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, IPP_OUTPUT_MODE_TAG, NULL,
    305                 IPP_OUTPUT_MODE_COLOR);
    306     }
    307 
    308     if (is_2_0_capable) {
    309         // Not friendly to 1.1 devices.
    310         if (job_params->media_tray != TRAY_SRC_AUTO_SELECT) {
    311             if (job_params->media_tray == TRAY_SOURCE_PHOTO_TRAY) {
    312                 col[++col_index] = ippNew();
    313                 ippAddString(col[col_index], IPP_TAG_JOB, IPP_TAG_KEYWORD, "media-source", NULL,
    314                         "main-tray");
    315             } else if (job_params->media_tray == TRAY_SOURCE_TRAY_1) {
    316                 col[++col_index] = ippNew();
    317                 ippAddString(col[col_index], IPP_TAG_JOB, IPP_TAG_KEYWORD, "media-source", NULL,
    318                         "main-tray");
    319             }
    320         }
    321 
    322         // MEDIA-Type is IPP 2.0 only
    323         // put margins in with media-type
    324         // Client-Error-Attribute-Or-Values-Not-Supported
    325         col[++col_index] = ippNew();
    326 
    327         // set margins - if negative margins, set to full-bleed; otherwise set calculated values
    328         if (job_params->borderless) {
    329             LOGD("Setting Up BORDERLESS");
    330             ippAddInteger(col[col_index], IPP_TAG_JOB, IPP_TAG_INTEGER, "media-bottom-margin", 0);
    331             ippAddInteger(col[col_index], IPP_TAG_JOB, IPP_TAG_INTEGER, "media-top-margin", 0);
    332             ippAddInteger(col[col_index], IPP_TAG_JOB, IPP_TAG_INTEGER, "media-left-margin", 0);
    333             ippAddInteger(col[col_index], IPP_TAG_JOB, IPP_TAG_INTEGER, "media-right-margin", 0);
    334         }
    335 
    336         switch (job_params->media_type) {
    337             case MEDIA_PHOTO_GLOSSY:
    338                 ippAddString(col[col_index], IPP_TAG_JOB, IPP_TAG_KEYWORD, "media-type", NULL,
    339                         "photographic-glossy");
    340                 break;
    341             case MEDIA_PHOTO:
    342             case MEDIA_ADVANCED_PHOTO:
    343             case MEDIA_PHOTO_MATTE:
    344             case MEDIA_PREMIUM_PHOTO:
    345             case MEDIA_OTHER_PHOTO:
    346                 ippAddString(col[col_index], IPP_TAG_JOB, IPP_TAG_KEYWORD, "media-type", NULL,
    347                         "photographic");
    348                 break;
    349             default:
    350                 ippAddString(col[col_index], IPP_TAG_JOB, IPP_TAG_KEYWORD, "media-type", NULL,
    351                         "stationery");
    352                 break;
    353         }
    354 
    355         float mediaWidth;
    356         float mediaHeight;
    357         const char *mediaSizeName = NULL;
    358         _get_pwg_media_size(job_params->media_size, &mediaWidth, &mediaHeight, &mediaSizeName);
    359         ipp_t *mediaSize = ippNew();
    360 
    361         if ((job_params->media_size_name) && (mediaSizeName != NULL)) {
    362             ippAddString(mediaSize, IPP_TAG_JOB, IPP_TAG_KEYWORD, "media-size-name", NULL,
    363                     mediaSizeName);
    364         } else {
    365             ippAddInteger(mediaSize, IPP_TAG_JOB, IPP_TAG_INTEGER, "x-dimension", (int) mediaWidth);
    366             ippAddInteger(mediaSize, IPP_TAG_JOB, IPP_TAG_INTEGER, "y-dimension",
    367                     (int) mediaHeight);
    368         }
    369         ippAddCollection(col[col_index], IPP_TAG_JOB, "media-size", mediaSize);
    370 
    371         // can either set media or media-col.
    372         // if both sent, device should return client-error-bad-request
    373         ippAddCollections(request, IPP_TAG_JOB, "media-col", col_index + 1, (const ipp_t **) col);
    374         while (col_index >= 0) {
    375             ippDelete(col[col_index--]);
    376         }
    377     } else {
    378         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "media", NULL,
    379                 mapDFMediaToIPPKeyword(job_params->media_size));
    380     }
    381 
    382     LOGI("_fill_job (%d): request", ipp_op);
    383     for (attrptr = ippFirstAttribute(request); attrptr; attrptr = ippNextAttribute(request)) {
    384         print_attr(attrptr);
    385     }
    386 
    387     return request;
    388 }
    389 
    390 static status_t  _validate_job(const ifc_print_job_t *this_p, wprint_job_params_t *job_params) {
    391     LOGD("_validate_job: Enter");
    392     status_t result = ERROR;
    393     ipp_print_job_t *ipp_job;
    394     ipp_t *response;
    395     ipp_t *request = NULL;
    396     ipp_status_t ipp_status;
    397 
    398     LOGD("_validate_job: ** validatePrintJob:  Entry");
    399     do {
    400         if (this_p == NULL) {
    401             break;
    402         }
    403 
    404         if (job_params == NULL) {
    405             break;
    406         }
    407 
    408         ipp_job = IMPL(ipp_print_job_t, ifc, this_p);
    409         if (ipp_job->http == NULL) {
    410             break;
    411         }
    412 
    413         ipp_job->useragent = NULL;
    414         if ((job_params->useragent != NULL) && (strlen(job_params->useragent) > 0)) {
    415             ipp_job->useragent = job_params->useragent;
    416         }
    417 
    418         request = _fill_job(IPP_VALIDATE_JOB, ipp_job->printer_uri, job_params);
    419 
    420         if (ipp_job->useragent != NULL) {
    421             httpSetDefaultField(ipp_job->http, HTTP_FIELD_USER_AGENT, ipp_job->useragent);
    422         }
    423         if ((response = ipp_doCupsRequest(ipp_job->http, request, ipp_job->http_resource,
    424                 ipp_job->printer_uri))
    425                 == NULL) {
    426             ipp_status = cupsLastError();
    427             LOGE("_validate_job:  validatePrintJob:  response is null:  ipp_status %d %s",
    428                     ipp_status, ippErrorString(ipp_status));
    429         } else {
    430             ipp_status = cupsLastError();
    431             LOGI("_validate_job: %s ipp_status %d  %x received:", ippOpString(IPP_VALIDATE_JOB),
    432                     ipp_status, ipp_status);
    433             ipp_attribute_t *attrptr;
    434             for (attrptr = ippFirstAttribute(response); attrptr; attrptr = ippNextAttribute(
    435                     response)) {
    436                 print_attr(attrptr);
    437             }
    438 
    439             ippDelete(response);
    440         }
    441 
    442         LOGD("_validate_job : ipp_status: %d", ipp_status);
    443         if (strncmp(ippErrorString(ipp_status), ippErrorString(IPP_OK),
    444                 strlen(ippErrorString(IPP_OK))) == 0) {
    445             result = OK;
    446         } else {
    447             result = ERROR;
    448         }
    449     } while (0);
    450 
    451     ippDelete(request);
    452 
    453     LOGD("_validate_job: ** validate_job result: %d", result);
    454 
    455     return result;
    456 }
    457 
    458 static status_t _start_job(const ifc_print_job_t *this_p, const wprint_job_params_t *job_params) {
    459     LOGD("_start_job: Enter");
    460     status_t result;
    461     ipp_print_job_t *ipp_job;
    462     ipp_t *request = NULL;
    463     bool retry;
    464     int failed_count = 0;
    465 
    466     LOGD("_start_job entry");
    467     do {
    468         retry = false;
    469         if (this_p == NULL) {
    470             LOGE("_start_job; this_p == NULL");
    471             continue;
    472         }
    473 
    474         ipp_job = IMPL(ipp_print_job_t, ifc, this_p);
    475 
    476         ipp_job->useragent = NULL;
    477         if ((job_params->useragent != NULL) && (strlen(job_params->useragent) > 0)) {
    478             ipp_job->useragent = job_params->useragent;
    479         }
    480         request = _fill_job(IPP_PRINT_JOB, ipp_job->printer_uri, job_params);
    481 
    482         if (request == NULL) {
    483             continue;
    484         }
    485 
    486         if (ipp_job->useragent != NULL) {
    487             httpSetDefaultField(ipp_job->http, HTTP_FIELD_USER_AGENT, ipp_job->useragent);
    488         }
    489         ipp_job->status = cupsSendRequest(ipp_job->http, request, ipp_job->http_resource, 0);
    490         if (ipp_job->status != HTTP_CONTINUE) {
    491             failed_count++;
    492             if ((failed_count == 1) &&
    493                     ((ipp_job->status == HTTP_ERROR) || (ipp_job->status >= HTTP_BAD_REQUEST))) {
    494                 retry = true;
    495                 LOGI("_start_job retry due to internal error");
    496                 // We will retry for one of these failures since we could have just
    497                 // lost our connection to the server and cups will not always attempt
    498                 // a reconnect for us.
    499                 ippDelete(request);
    500                 continue;
    501             }
    502 
    503             _cupsSetHTTPError(ipp_job->status);
    504         }
    505         ippDelete(request);
    506         LOGI("_start_job httpPrint fd %d status %d ipp_status %d", ipp_job->http->fd,
    507                 ipp_job->status, cupsLastError());
    508 
    509         result = ((ipp_job->status == HTTP_CONTINUE) ? OK : ERROR);
    510     } while (retry);
    511 
    512     return result;
    513 }
    514 
    515 static int _send_data(const ifc_print_job_t *this_p, const char *buffer, size_t length) {
    516     ipp_print_job_t *ipp_job;
    517     if (this_p == NULL) {
    518         return ERROR;
    519     }
    520 
    521     ipp_job = IMPL(ipp_print_job_t, ifc, this_p);
    522     if (ipp_job->http == NULL) {
    523         return ERROR;
    524     }
    525 
    526     if (ipp_job->status != HTTP_CONTINUE) {
    527         return ERROR;
    528     }
    529 
    530     if (length != 0) {
    531         if (ipp_job->useragent != NULL) {
    532             httpSetDefaultField(ipp_job->http, HTTP_FIELD_USER_AGENT, ipp_job->useragent);
    533         }
    534         ipp_job->status = cupsWriteRequestData(ipp_job->http, buffer, length);
    535     }
    536     return ((ipp_job->status == HTTP_CONTINUE) ? length : (int) ERROR);
    537 }
    538 
    539 static status_t _end_job(const ifc_print_job_t *this_p) {
    540     LOGD("_end_job: Enter");
    541     status_t result = ERROR;
    542     ipp_t *response;
    543     ipp_attribute_t *attrptr;
    544     int op = IPP_PRINT_JOB;
    545     ipp_print_job_t *ipp_job;
    546     int job_id = -1;
    547 
    548     char buffer[1024];
    549 
    550     if (this_p == NULL) {
    551         return result;
    552     }
    553 
    554     ipp_job = IMPL(ipp_print_job_t, ifc, this_p);
    555 
    556     if (ipp_job->http == NULL) {
    557         return result;
    558     }
    559 
    560     LOGD("_end_job: entry httpPrint %d", ipp_job->http->fd);
    561 
    562     if (ipp_job->useragent != NULL) {
    563         httpSetDefaultField(ipp_job->http, HTTP_FIELD_USER_AGENT, ipp_job->useragent);
    564     }
    565     ipp_job->status = cupsWriteRequestData(ipp_job->http, buffer, 0);
    566 
    567     if (ipp_job->status != HTTP_CONTINUE) {
    568         LOGE("Error: from cupsWriteRequestData http.fd %d:  status %d",
    569                 ipp_job->http->fd, ipp_job->status);
    570     } else {
    571         result = OK;
    572         LOGD("0 length Bytes sent, status %d", ipp_job->status);
    573         response = cupsGetResponse(ipp_job->http, ipp_job->http_resource);
    574 
    575         if ((attrptr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) == NULL) {
    576             LOGE("sent cupsGetResponse %s job id is null; received", ippOpString(op));
    577         } else {
    578             job_id = ippGetInteger(attrptr, 0);
    579             LOGI("sent cupsGetResponse %s job_id %d; received", ippOpString(op), job_id);
    580         }
    581 
    582         if (response != NULL) {
    583             for (attrptr = ippFirstAttribute(response); attrptr; attrptr = ippNextAttribute(
    584                     response)) {
    585                 print_attr(attrptr);
    586                 if (strcmp(ippGetName(attrptr), "job-state-reasons") == 0) {
    587                     int i;
    588                     for (i = 0; i < ippGetCount(attrptr); i++) {
    589                         if (strcmp(ippGetString(attrptr, i, NULL), "job-canceled-at-device")
    590                                 == 0) {
    591                             result = CANCELLED;
    592                             break;
    593                         }
    594                     }
    595                 }
    596             }
    597             ippDelete(response);
    598         }
    599     }
    600     LOGD("_end_job: exit status %d job_id %d", ipp_job->status, job_id);
    601 
    602     return result;
    603 }