OpenPrinting / cups

OpenPrinting CUPS Sources
https://openprinting.github.io/cups
Apache License 2.0
1.01k stars 182 forks source link

Hope to get whether the printer is open cover or paper jam #782

Closed NEKGod closed 1 year ago

NEKGod commented 1 year ago

Hello, handsome cups maintainer, I am developing an application, hoping to monitor the abnormal state of the printer, but my level is not good, can not successfully obtain, check the cgi-bin code can not solve my problem, I hope to get an answer image

zdohnal commented 1 year ago

Hi @NEKGod ,

if I were you I would check the code of IPP backend (backend/ipp.c) - we use a monitor thread for getting the printer and the job state, you probably can take an inspiration there.

NEKGod commented 1 year ago

Yes, I just read ipp.c and got the result I want. Thank you for your help

`void process_attributes(ipp_t *response) {
    for (ipp_attribute_t *attr = ippFirstAttribute(response); attr != nullptr; attr = ippNextAttribute(response)) {
        std::cout << "Attribute: " << ippGetName(attr) << std::endl;
        std::cout << "Value(s): ";

        int num_values = ippGetCount(attr);
        for (int i = 0; i < num_values; ++i) {
            if (i > 0) {
                std::cout << ", ";
            }

            ipp_tag_t value_tag = ippGetValueTag(attr);
            switch (value_tag) {
                case IPP_TAG_INTEGER:
                case IPP_TAG_ENUM:
                    std::cout << ippGetInteger(attr, i);
                    break;

                case IPP_TAG_BOOLEAN:
                    std::cout << (ippGetBoolean(attr, i) ? "true" : "false");
                    break;

                case IPP_TAG_STRING:
                case IPP_TAG_TEXT:
                case IPP_TAG_NAME:
                case IPP_TAG_KEYWORD:
                case IPP_TAG_URI:
                case IPP_TAG_URISCHEME:
                case IPP_TAG_CHARSET:
                case IPP_TAG_LANGUAGE:
                case IPP_TAG_MIMETYPE:
                    std::cout << ippGetString(attr, i, nullptr);
                    break;

                default:
                    std::cout << "Unsupported value type";
            }
        }

        std::cout << std::endl;
    }
}

ipp_jstate_t get_job_state(const char *printer_name, int job_id)
{
    char printer_uri[1024];
    ipp_t *request, *response;
    ipp_jstate_t job_state;

    httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", NULL, "localhost", 631, "/printers/%s", printer_name);
    request = ippNewRequest(IPP_OP_GET_JOB_ATTRIBUTES);
    char        uri[HTTP_MAX_URI];  /* Job URI */
    snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
                 NULL, uri);
    response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/jobs");
    process_attributes(response);
    job_state = (ipp_jstate_t)ippGetInteger(ippFindAttribute(response, "job-state", IPP_TAG_ENUM), 0);
    std::cout << job_state << std::endl;
    ippDelete(response);

    return (job_state);
}

`