Beispiel #1
0
int
main (int argc, char *argv[])
{
    int			fd;
    unsigned char	argp[1024];
    int			length;

    --argc;
    ++argv;

    if (argc != 1)
	error(1, "usage: usb_printerid /dev/usb/lp0\n");

    fd = open(argv[0], O_RDWR);
    if (fd < 0)
	error(1, "can't open '%s'\n", argv[0]);

    if (ioctl(fd, LPIOC_GET_DEVICE_ID(sizeof(argp)), argp) < 0)
	error(1, "GET_DEVICE_ID on '%s'\n", argv[0]);

    length = (argp[0] << 8) + argp[1] - 2;
    printf("GET_DEVICE_ID string:\n");
    fwrite(argp + 2, 1, length, stdout);
    printf("\n");

    #if 0
	if (ioctl(fd, LPGETSTATUS, &status) < 0)
	    error(1, "LPGETSTATUS on '%s'\n", argv[0]);

	printf("Status: 0x%02x\n", status);
    #endif

    close(fd);
    exit(0);
}
Beispiel #2
0
int					/* O - 0 on success, -1 on failure */
backendGetDeviceID(
    int        fd,			/* I - File descriptor */
    char       *device_id,		/* O - 1284 device ID */
    int        device_id_size,		/* I - Size of buffer */
    char       *make_model,		/* O - Make/model */
    int        make_model_size,		/* I - Size of buffer */
    const char *scheme,			/* I - URI scheme */
    char       *uri,			/* O - Device URI */
    int        uri_size)		/* I - Size of buffer */
{
#ifdef __APPLE__ /* This function is a no-op */
  (void)fd;
  (void)device_id;
  (void)device_id_size;
  (void)make_model;
  (void)make_model_size;
  (void)scheme;
  (void)uri;
  (void)uri_size;

  return (-1);

#else /* Get the device ID from the specified file descriptor... */
#  ifdef __linux
  int	length;				/* Length of device ID info */
  int   got_id = 0;
#  endif /* __linux */
#  if defined(__sun) && defined(ECPPIOC_GETDEVID)
  struct ecpp_device_id did;		/* Device ID buffer */
#  endif /* __sun && ECPPIOC_GETDEVID */
  char	*ptr;				/* Pointer into device ID */


  DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
                "make_model=%p, make_model_size=%d, scheme=\"%s\", "
		"uri=%p, uri_size=%d)\n", fd, device_id, device_id_size,
		make_model, make_model_size, scheme ? scheme : "(null)",
		uri, uri_size));

 /*
  * Range check input...
  */

  if (!device_id || device_id_size < 32)
  {
    DEBUG_puts("backendGetDeviceID: Bad args!");
    return (-1);
  }

  if (make_model)
    *make_model = '\0';

  if (fd >= 0)
  {
   /*
    * Get the device ID string...
    */

    *device_id = '\0';

#  ifdef __linux
    if (ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id))
    {
     /*
      * Linux has to implement things differently for every device it seems.
      * Since the standard parallel port driver does not provide a simple
      * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
      * device corresponding to this port and do some negotiation trickery
      * to get the current device ID.
      */

      if (uri && !strncmp(uri, "parallel:/dev/", 14))
      {
	char	devparport[16];		/* /dev/parportN */
	int	devparportfd,		/* File descriptor for raw device */
		  mode;			/* Port mode */


       /*
	* Since the Linux parallel backend only supports 4 parallel port
	* devices, just grab the trailing digit and use it to construct a
	* /dev/parportN filename...
	*/

	snprintf(devparport, sizeof(devparport), "/dev/parport%s",
		 uri + strlen(uri) - 1);

	if ((devparportfd = open(devparport, O_RDWR | O_NOCTTY)) != -1)
	{
	 /*
	  * Claim the device...
	  */

	  if (!ioctl(devparportfd, PPCLAIM))
	  {
	    fcntl(devparportfd, F_SETFL, fcntl(devparportfd, F_GETFL) | O_NONBLOCK);

	    mode = IEEE1284_MODE_COMPAT;

	    if (!ioctl(devparportfd, PPNEGOT, &mode))
	    {
	     /*
	      * Put the device into Device ID mode...
	      */

	      mode = IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID;

	      if (!ioctl(devparportfd, PPNEGOT, &mode))
	      {
	       /*
		* Read the 1284 device ID...
		*/

		if ((length = read(devparportfd, device_id, (size_t)device_id_size - 1)) >= 2)
		{
		  device_id[length] = '\0';
		  got_id = 1;
		}
	      }
	    }

	   /*
	    * Release the device...
	    */

	    ioctl(devparportfd, PPRELEASE);
	  }

	  close(devparportfd);
	}
      }
    }
    else
      got_id = 1;

    if (got_id)
    {
     /*
      * Extract the length of the device ID string from the first two
      * bytes.  The 1284 spec says the length is stored MSB first...
      */

      length = (int)((((unsigned)device_id[0] & 255) << 8) + ((unsigned)device_id[1] & 255));

     /*
      * Check to see if the length is larger than our buffer; first
      * assume that the vendor incorrectly implemented the 1284 spec,
      * and then limit the length to the size of our buffer...
      */

      if (length > device_id_size || length < 14)
	length = (int)((((unsigned)device_id[1] & 255) << 8) + ((unsigned)device_id[0] & 255));

      if (length > device_id_size)
	length = device_id_size;

     /*
      * The length field counts the number of bytes in the string
      * including the length field itself (2 bytes).  The minimum
      * length for a valid/usable device ID is 14 bytes:
      *
      *     <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
      *        2  +   4  +  1  +  5 +  1 +  1
      */

      if (length < 14)
      {
       /*
	* Can't use this device ID, so don't try to copy it...
	*/

	device_id[0] = '\0';
	got_id       = 0;
      }
      else
      {
       /*
	* Copy the device ID text to the beginning of the buffer and
	* nul-terminate.
	*/

	length -= 2;

	memmove(device_id, device_id + 2, (size_t)length);
	device_id[length] = '\0';
      }
    }
    else
    {
      DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
                    strerror(errno)));
      *device_id = '\0';
    }
#  endif /* __linux */

#   if defined(__sun) && defined(ECPPIOC_GETDEVID)
    did.mode = ECPP_CENTRONICS;
    did.len  = device_id_size - 1;
    did.rlen = 0;
    did.addr = device_id;

    if (!ioctl(fd, ECPPIOC_GETDEVID, &did))
    {
     /*
      * Nul-terminate the device ID text.
      */

      if (did.rlen < (device_id_size - 1))
	device_id[did.rlen] = '\0';
      else
	device_id[device_id_size - 1] = '\0';
    }
#    ifdef DEBUG
    else
      DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
                    strerror(errno)));
#    endif /* DEBUG */
#  endif /* __sun && ECPPIOC_GETDEVID */
  }

 /*
  * Check whether device ID is valid. Turn line breaks and tabs to spaces and
  * reject device IDs with non-printable characters.
  */

  for (ptr = device_id; *ptr; ptr ++)
    if (_cups_isspace(*ptr))
      *ptr = ' ';
    else if ((*ptr & 255) < ' ' || *ptr == 127)
    {
      DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.",
                    *ptr & 255));
      *device_id = '\0';
      break;
    }

  DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));

  if (scheme && uri)
    *uri = '\0';

  if (!*device_id)
    return (-1);

 /*
  * Get the make and model...
  */

  if (make_model)
    backendGetMakeModel(device_id, make_model, (size_t)make_model_size);

 /*
  * Then generate a device URI...
  */

  if (scheme && uri && uri_size > 32)
  {
    int			num_values;	/* Number of keys and values */
    cups_option_t	*values;	/* Keys and values in device ID */
    const char		*mfg,		/* Manufacturer */
			*mdl,		/* Model */
			*sern;		/* Serial number */
    char		temp[256],	/* Temporary manufacturer string */
			*tempptr;	/* Pointer into temp string */


   /*
    * Get the make, model, and serial numbers...
    */

    num_values = _cupsGet1284Values(device_id, &values);

    if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
      if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
        sern = cupsGetOption("SN", num_values, values);

    if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
      mfg = cupsGetOption("MFG", num_values, values);

    if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
      mdl = cupsGetOption("MDL", num_values, values);

    if (mfg)
    {
      if (!_cups_strcasecmp(mfg, "Hewlett-Packard"))
        mfg = "HP";
      else if (!_cups_strcasecmp(mfg, "Lexmark International"))
        mfg = "Lexmark";
    }
    else
    {
      strlcpy(temp, make_model, sizeof(temp));

      if ((tempptr = strchr(temp, ' ')) != NULL)
        *tempptr = '\0';

      mfg = temp;
    }

    if (!mdl)
      mdl = "";

    if (!_cups_strncasecmp(mdl, mfg, strlen(mfg)))
    {
      mdl += strlen(mfg);

      while (isspace(*mdl & 255))
        mdl ++;
    }

   /*
    * Generate the device URI from the manufacturer, make_model, and
    * serial number strings.
    */

    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, scheme, NULL, mfg, 0,
                     "/%s%s%s", mdl, sern ? "?serial=" : "", sern ? sern : "");

    cupsFreeOptions(num_values, values);
  }

  return (0);
#endif /* __APPLE__ */
}
Beispiel #3
0
void print(stk::rectangle rect, const unsigned char* raster_data)
{
    IjsClientCtx* ctx;
    int job_id=1;
    char buf[2048];
    int output_fd=open("/dev/usb/lp0", O_RDWR);
    
    std::string iee_device_id;	// Will hold the device id
    int res=ioctl (output_fd, LPIOC_GET_DEVICE_ID(2048), buf);
    if(res==0)
    {				// Could get Device ID
	short len;
	len = (unsigned)buf[0] << 8 | (unsigned)buf[1];
	if(len>2048)
	    len=2048;
	char* device_id=buf+2;
	device_id[len]=0;
	iee_device_id=std::string((const char*)device_id);
    }
    // Parsing printer Device ID
    int mfg_begin=iee_device_id.find("MFG:");
    int mfg_end=iee_device_id.find(";",mfg_begin);
    std::string mfg=iee_device_id.substr(mfg_begin+4,mfg_end-mfg_begin-4);    
    int mdl_begin=iee_device_id.find("MDL:");
    int mdl_end=iee_device_id.find(";",mdl_begin);
    std::string mdl=iee_device_id.substr(mdl_begin+4,mdl_end-mdl_begin-4);
    
    std::cout << "Printer's Manufacturer is " << mfg << "  Modell is " << mdl << std::endl;

    
    ctx = ijs_invoke_server ("hpijs");
    
    ijs_client_open (ctx);
    ijs_client_begin_job (ctx, job_id);
    
    std::string str_output_fd=boost::lexical_cast<std::string>(output_fd);
    int err = ijs_client_set_param (ctx, job_id, "OutputFD", str_output_fd.c_str(), str_output_fd.size());
    if(err<0)
	std::cerr << "didnt set parameter, err = " << err << std::endl;

    err = ijs_client_set_param (ctx, job_id, "DeviceManufacturer", mfg.c_str(), mfg.size());
    if(err<0)
	std::cerr << "Couldnt set DeviceManufacturer, err = " << err << std::endl;
    
    err = ijs_client_set_param (ctx, job_id, "DeviceModel", mdl.c_str(), mdl.size());
    if(err<0)
	std::cerr << "Couldnt set devicemodel, err = " << err << std::endl;

    set_parameter(ctx, job_id, "PaperSize","8.27x11.69"); //A4
    //set_parameter(ctx, job_id, "PaperSize","3x3");
    get_parameter(ctx, job_id, "PrintableArea");
    set_parameter(ctx, job_id, "Height",boost::lexical_cast<std::string>(rect.height()));
    set_parameter(ctx, job_id, "Width",boost::lexical_cast<std::string>(rect.width()));
    set_parameter(ctx, job_id, "Dpi","300x300");

    set_parameter(ctx, job_id, "BitsPerSample","8");
    set_parameter(ctx, job_id, "NumChan","3");
    set_parameter(ctx, job_id, "ColorSpace","DeviceRGB");

  
//    unsigned char data[128*128*3];
//    memset(data,0xff,sizeof(data));
    
    err = ijs_client_begin_page (ctx, job_id);
    std::cerr << "begin page err = " << err << std::endl;

    err = ijs_client_send_data_wait (ctx, job_id, (const char*)raster_data, rect.width()*rect.height()*3);
    if(err!=0)
	std::cerr << "Data send, err is " << err << std::endl;
/*    for(int i=0;i<128;i++)
    {
	unsigned char* row=data+i*128*3;
	row[i*3+0]=0;
	row[i*3+1]=0;
	row[i*3+2]=0;
	row[i*3+(i%3)]=0xff;
	err = ijs_client_send_data_wait (ctx, job_id, (const char*)row, 128*3);
	if(err!=0)
	    std::cerr << "Data send, err is " << err << std::endl;
	    }*/

    
    err = ijs_client_end_page (ctx, job_id);

    err = ijs_client_list_params (ctx, job_id, buf, 1024);
    if(err<0)
	std::cerr << "didnt get parameter list, err = " << err << std::endl;
    else
    {
	buf[err]=0;
	std::cerr << "Parameter list is " << buf << std::endl;
    }
    get_parameter(ctx, job_id, "Quality:Quality");
    get_parameter(ctx, job_id, "Quality:ColorMode");
    enum_parameter(ctx, job_id, "Dpi");

    sleep(2);
    
    ijs_client_end_job (ctx, job_id);    
    ijs_client_close (ctx);
}