[sane-devel] sane_get_devices and sanei_usb_init

ABC abc at telekom.ru
Tue Dec 2 03:23:48 UTC 2008


First of all sanei_usb_init() is not designed to be used for rescanning
after any other sanei_usb functions is called. It is just initialization
and rescanning ability is not documented side effect. As stated in
documentation: "Call this before any other sanei_usb function". So don't
call it after. (This doesn't state it should be called just once, so we
could rescan before first device is opened.)

Second, we accessing devices just by index number, so if there is
appeared new device indexes could get changed. And third, yes it clears
internal structures, opened descriptors, whatever.


As I understand nobody of backend writers code so sane_get_devices()
could detect appearance of new devices in system. sanei_usb_init() is
usually called in sane_init(), there is get and remmebers internally
list (array) of present usb devices forever until program terminates.
Moreover, backend don't have access to that list directly but only via
attach callbacks, so config is parsed usually in sane_init() too, and
all found devices get "attached" to backend. Now backend have list of
devices that in practice can not be changed. In most backends
sane_get_devices() only _rebuild_ linear array of pointers to that list
created in sane_init. Actially that array doesn't need to be rebuilt
each time, it would not change.

But when you write backend for a first time you don't know that. You
think "Why attach all devices in init if sane_get_devices should 'detect
when new devices become available'?" You trying to code it "right" way,
but later you realize it's don't work like this, at least for usb.



On Tue, Dec 02, 2008 at 08:14:13AM +0900, Olaf Meeuwissen wrote:
> "m. allan noah" <kitno455 at gmail.com> writes:
> 
> > On Mon, Dec 1, 2008 at 12:10 AM, ABC <abc at telekom.ru> wrote:
> >>> http://www.sane-project.org/html/doc012.html
> >>> 4.3.3 sane_get_devices
> >>> ...
> >>> This function can be called repeatedly to detect when new devices
> >>> become available.
> >>
> >> Nice writing but it is not possible with usb now, because sanei usb
> >> infrastructure get list of devices only when sanei_usb_init() is called.
> >> And it is not possible to call sanei_usb_init() more than once without
> >> breaking already opened devices.
> >
> > how do they break?
> 
> sanei_usb_open() returns an index into the devices array.  That index
> is passed to the sanei_usb I/O functions and sanei_usb_close() to find
> which one of the devices you want to use.
> Problem is that sanei_usb_init() memset()s the devices array.
> 
> Hope this helps,
> -- 
> Olaf Meeuwissen, LPIC-2           FLOSS Engineer -- AVASYS Corporation
> FSF Associate Member #1962               Help support software freedom
>                  http://www.fsf.org/jf?referrer=1962



More information about the sane-devel mailing list