Add Network Printer through libcups API

Azuri azuri.shah at fmr.com
Tue Aug 30 13:59:10 PDT 2011


The code I am currently using from lpadmin.c file is below. I get error saying can't find printer or class.


 http_t *http ;
    char *printer = "socket://10.105.152.49:9100";
    char *pclass;
    int		i;			/* Looping var */
    ipp_t		*request,		/* IPP Request */
    *response;		/* IPP Response */
    ipp_attribute_t *attr,		/* Current attribute */
    *members;		/* Members in class */
    char		uri[HTTP_MAX_URI];	/* URI for printer/class */




    /*
     * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
     * attributes:
     *
     *    attributes-charset
     *    attributes-natural-language
     *    printer-uri
     */
    http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
    request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);

    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
                     "localhost", 0, "/classes/%s", pclass);
    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                 "printer-uri", NULL, uri);

    /*
     * Do the request and get back a response...
     */

    response = cupsDoRequest(http, request, "/");

    /*
     * Build a CUPS_ADD_CLASS request, which requires the following
     * attributes:
     *
     *    attributes-charset
     *    attributes-natural-language
     *    printer-uri
     *    member-uris
     */

    request = ippNewRequest(CUPS_ADD_CLASS);

    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                 "printer-uri", NULL, uri);

    /*
     * See if the printer is already in the class...
     */

    if (response != NULL &&
        (members = ippFindAttribute(response, "member-names", IPP_TAG_NAME)) != NULL)
        for (i = 0; i < members->num_values; i ++)
            if (strcasecmp(printer, members->values[i].string.text) == 0)
            {

                ippDelete(request);
                ippDelete(response);
                //return (0);
            }

    /*
     * OK, the printer isn't part of the class, so add it...
     */

    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
                     "localhost", 0, "/printers/%s", printer);

    if (response != NULL &&
        (members = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
    {
        /*
         * Add the printer to the existing list...
         */

        attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI,
                             "member-uris", members->num_values + 1, NULL, NULL);
        for (i = 0; i < members->num_values; i ++)
            attr->values[i].string.text = _cupsStrAlloc(members->values[i].string.text);

        attr->values[i].string.text = _cupsStrAlloc(uri);
    }
    else
        ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "member-uris", NULL,
                     uri);

    /*
     * Then send the request...
     */

    ippDelete(response);

    if ((response = cupsDoRequest(http, request, "/admin/")) == NULL)
    {

    }
    else if (response->request.status.status_code > IPP_OK_CONFLICT)
    {
        _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());

        ippDelete(response);

    }
    else
    {
        ippDelete(response);

    }







> I did look at lpadmin.c file both its not working . But I am totally confusd.
> I did look more into add_printer_to_class method but its not working. All I want is when user enters socket://123.45.45.67 to UI, my code should go ahead and add the printer tot he mac. I have spent hello lot of time trying to figure this out using API's but no success at all so far. Your help will be appreciated.
>
> Russell,
> >
> > Just download the CUPS source and look at the lpadmin tool.
> >
> > -Chad
> >
> > On 8/30/11 4:30 PM, "Azuri" <azuri.shah at fmr.com> thusly spake:
> >
> > >> Russell Stewart wrote:
> > >>> Michael Sweet wrote:
> > >>>
> > >>>> You need to create an IPP request and send it using cupsDoFileRequest
> > >>>> (assuming you are using a PPD file or interface script for the queue)
> > >>>
> > >>> OK, I see. So I assume that ippNewRequest() is the method to call for
> > >>> creating the IPP request, right?
> > >>
> > >> Yes.  See the "CUPS Implementation of IPP" document for documentation
> > >> on the attributes and operations:
> > >>
> > >>      http://www.cups.org/documentation.php/spec-ipp.html
> > >>
> > >> --
> > >> ______________________________________________________________________
> > >> Michael Sweet, Easy Software Products           mike at easysw dot com
> > >
> > >
> > > Can you please provice code on how u did it? I am trying to do the same time
> > > but have no success so far. How can I use API's to add printer to my mac. I am
> > > developing a Coca app which provides GUI and takes user input to add printer
> > > using cups. Please let me know
> > > _______________________________________________
> > > cups-dev mailing list
> > > cups-dev at easysw.com
> > > http://lists.easysw.com/mailman/listinfo/cups-dev
> >
> > Chad Hulbert
> > Software Engineer
> > Xerox Corporation
> > 800 Phillips Rd
> > Webster, NY 14580
> >
> > p 585.427.3295  (8*707.3295)
> >
>





More information about the cups mailing list