[sane-devel] RE: In Search of a Master

Henning Meier-Geinitz henning@meier-geinitz.de
Fri, 19 Dec 2003 20:33:54 +0100


Hi,

On Fri, Dec 19, 2003 at 11:36:10AM -0500, Jason Anderson wrote:
> What I am trying to do is just write a simple program that will
> access the device so that I can then try to read/write to the device.

Ok.

> This is just for starters so that I can get a feel of the sane API.

Ok. You haven't used any part of the SANE API until now, however. The
SANE API is the interface between backends and frontends. What you are
currently writing is an all-in-one program that doesn't really need
the SANE API. sanei_* is SANE internal and not part of the API. That's
just for clarification, the way you chose to learn is ok.

> Here's the code from "hubba.c" file:
> --------------
> #include "../include/sane/config.h"
> #include <unistd.h>
> #include "../include/sane/sanei_usb.h"
> 
> /*Declare any prototypes */
> SANE_Int fd;
> SANE_Status sanei_usb_open (SANE_String_Const devname, SANE_Int *dn);
> void sanei_usb_close (SANE_Int dn);
> void sanei_usb_init (void);

You don't need to declare the sanei_usb* functions if you include the
sanei_usb.h header file.

> int main (void)  {
> /* Start up the usb_interface */
> void sanei_usb_init(void);

You are again declaring a function here (?). That's probably not what
you want to do. If you want to call that function, just do:

  sanei_usb_init ();

> /* Try to access the usb device */
> sanei_usb_open("/dev/usb/scanner0",&fd);

Ok. But you should check the return status:

SANE_Status status; /* put that to the variables section */

  status = sanei_usb_open("/dev/usb/scanner0", &fd);
  if (status != SANE_STATUS_GOOD)
    {
      /* do some error handling, maybe exit */
    }    

> /* Try to close the USB device */
> sanei_usb_close(&fd);

Have a look at the declarations of the functions:
http://www.sane-project.org/sanei/sanei__usb_8h.html#a58

It's:
  sanei_usb_close (fd);

fd is an integer value. If you give an integer to a function by value
(some_func (fd)), you can't change the integer value inside the
function. Well, you can but that won't have any effect on the original
integer. That's the reason for using a pointer in sanei_usb_open (&fd).
In sanei_usb_close you don't need to change fd, so using the value
itsself is ok.
  
> And here's the error code I recieved when I typed "gcc hubba.c":
> 
> hubba.c: In function `main':
> hubba.c:19: warning: passing arg 1 of `sanei_usb_close' makes integer
> from pointer without a cast

The compiler is complaining abou the ponter/integer stuff I told you
above.

> /tmp/ccwP9p1i.o(.text+0x1e): In function `main':
> : undefined reference to `sanei_usb_open'
> /tmp/ccwP9p1i.o(.text+0x2e): In function `main':
> : undefined reference to `sanei_usb_close'
> collect2: ld returned 1 exit status

The linker can't find the functions you are trying to use. That's not
surprising as you haven't linked them to your program. I guess you
must link libsanei.lo or sanei_usb.o directly but I haven't tried.

Reading some book about C won't harm, I guess :-)

Bye,
  Henning