[sane-devel] [patch] sane-find-scanner

Henning Meier-Geinitz henning@meier-geinitz.de
Sun, 28 Apr 2002 12:29:41 +0200


--cNdxnHkX5QqsyA0e
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi,

On Sun, Apr 28, 2002 at 02:39:00AM -0500, Frank Zago wrote:
> This patch fixes a bug where the return of sanei_scsi_cmd() wasn't checked.
> It also displays the inquiry buffer if -v is enabled:

Good idea. Works fine. I have attached a slightly modified version of
the patch that prints an error if it can't do the inquiry and prints
the SANE status when something goes wrong in some places. This also
needs an update of tools/Makefile.in.

If you don't find any errors, you can commit it to CVS. If you like,
you can run it through indent -gnu also as there seem to be about 7.5
different coding styles in this file. Ok, I know you don't like to :-)

Bye,
  Henning

--cNdxnHkX5QqsyA0e
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="sane-find-scanner.c.diff-2"

Index: sane-find-scanner.c
===================================================================
RCS file: /cvsroot/external/sane/sane-backends/tools/sane-find-scanner.c,v
retrieving revision 1.8
diff -u -u -r1.8 sane-find-scanner.c
--- sane-find-scanner.c	2001/12/25 16:07:35	1.8
+++ sane-find-scanner.c	2002/04/28 10:21:29
@@ -107,23 +107,86 @@
   return 1; /* Give up, and assume yes to avoid false negatives */
 }
 
+/* Display a buffer in the log. Display by lines of 16 bytes. */
 static void 
+hexdump (const char *comment, unsigned char *buf, const int length)
+{
+  int i;
+  char line[128];
+  char *ptr;
+  char asc_buf[17];
+  char *asc_ptr;
+
+  printf ("  %s\n", comment);
+
+  i = 0;
+  goto start;
+
+  do
+    {
+      if (i < length)
+	{
+	  ptr += sprintf (ptr, " %2.2x", *buf);
+
+	  if (*buf >= 32 && *buf <= 127)
+	    {
+	      asc_ptr += sprintf (asc_ptr, "%c", *buf);
+	    }
+	  else
+	    {
+	      asc_ptr += sprintf (asc_ptr, ".");
+	    }
+	}
+      else
+	{
+	  /* After the length; do nothing. */
+	  ptr += sprintf (ptr, "   ");
+	}
+
+      i++;
+      buf++;
+
+      if ((i % 16) == 0)
+	{
+	  /* It's a new line */
+	  printf ("  %s    %s\n", line, asc_buf);
+
+	start:
+	  ptr = line;
+	  *ptr = '\0';
+	  asc_ptr = asc_buf;
+	  *asc_ptr = '\0';
+
+	  ptr += sprintf (ptr, "  %3.3d:", i);
+	}
+
+    }
+  while (i < ((length + 15) & ~15));
+}
+
+static SANE_Status
 scanner_do_inquiry (unsigned char *buffer, int sfd)
 {
   size_t size;
+  SANE_Status status;
 
   DBG (5, "do_inquiry\n");
   memset (buffer, '\0', 256);	/* clear buffer */
 
   size = 5; /* first get only 5 bytes to get size of inquiry_return_block */
   set_inquiry_return_size (inquiry.cmd, size);
-  sanei_scsi_cmd (sfd, inquiry.cmd, inquiry.size, buffer, &size);
+  status = sanei_scsi_cmd (sfd, inquiry.cmd, inquiry.size, buffer, &size);
 
+  if (status != SANE_STATUS_GOOD) 
+    return (status);
+
   size = get_inquiry_additional_length (buffer) + 5;
 
   /* then get inquiry with actual size */
   set_inquiry_return_size (inquiry.cmd, size);
-  sanei_scsi_cmd (sfd, inquiry.cmd, inquiry.size, buffer, &size);
+  status = sanei_scsi_cmd (sfd, inquiry.cmd, inquiry.size, buffer, &size);
+
+  return (status);
 }
 
 static void 
@@ -134,14 +197,26 @@
   unsigned char version[5];
   unsigned char *pp;
   unsigned int devtype;
+  SANE_Status status;
   static char *devtypes[] =
     {
       "disk", "tape", "printer", "processor", "CD-writer",
       "CD-drive", "scanner", "optical-drive", "jukebox",
       "communicator"
     };
+  status = scanner_do_inquiry (buffer, sfd);
+  if (status != SANE_STATUS_GOOD)
+    {
+      if (verbose)
+	printf ("%s: inquiry for device %s failed (%s)\n",
+		prog_name, devicename, sane_strstatus (status));
+      return;
+    }
+
+  if (verbose) 
+    hexdump ("Inquiry for device:", buffer,
+	     get_inquiry_additional_length (buffer) + 5);
 
-  scanner_do_inquiry (buffer, sfd);	/* get inquiry */
   devtype = get_inquiry_periph_devtype (buffer);
   if (!verbose
       && devtype != IN_periph_devtype_scanner
@@ -488,12 +563,12 @@
       if (verbose)
         {
 	  if (result != 0)
-	    printf (" failed to open\n");
+	    printf (" failed to open (%s)\n", sane_strstatus (result));
           else
 	    printf (" open ok\n");
         }
 
-      if (result == 0)
+      if (result == SANE_STATUS_GOOD)
 	{
 	  scanner_identify_scanner (buffer, sfd, dev_name);
 	  sanei_scsi_close (sfd);
@@ -525,7 +600,7 @@
       if (result != SANE_STATUS_GOOD)
 	{
 	  if (verbose)
-	    printf (" failed to open (status %d)\n", result);
+	    printf (" failed to open (%s)\n", sane_strstatus (result));
 	}
       else
 	{

--cNdxnHkX5QqsyA0e
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="Makefile.in.diff"

Index: Makefile.in
===================================================================
RCS file: /cvsroot/external/sane/sane-backends/tools/Makefile.in,v
retrieving revision 1.15
diff -u -u -r1.15 Makefile.in
--- Makefile.in	2002/03/29 13:27:16	1.15
+++ Makefile.in	2002/04/28 10:24:05
@@ -88,10 +88,10 @@
 	cd $(top_builddir) \
           && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status
 
-sane-find-scanner: sane-find-scanner.o \
+sane-find-scanner: sane-find-scanner.o ../backend/sane_strstatus.lo \
 	../sanei/sanei_scsi.lo ../sanei/sanei_usb.lo ../sanei/sanei_init_debug.lo
-	@$(LIBTOOL) $(MLINK) $(LINK) sane-find-scanner.o $(LIBSANEI) $(LIBLIB) \
-		$(LIBS)
+	@$(LIBTOOL) $(MLINK) $(LINK) sane-find-scanner.o \
+	  ../backend/sane_strstatus.lo $(LIBSANEI) $(LIBLIB) $(LIBS)
 
 ../backend/umax_pp_low.o: ../backend/umax_pp_low.c 
 	$(COMPILE) ../backend/umax_pp_low.c -o ../backend/umax_pp_low.o \

--cNdxnHkX5QqsyA0e--